tornado-core/scripts/utils.js

20 lines
845 B
JavaScript
Raw Normal View History

2019-07-16 17:58:42 +02:00
const snarkjs = require('snarkjs')
const groth = snarkjs['groth']
const crypto = require('crypto')
const circomlib = require('circomlib')
const pedersen = circomlib.pedersenHash
const babyjub = circomlib.babyJub
const websnarkUtils = require('websnark/src/utils')
const unstringifyBigInts2 = require('snarkjs/src/stringifybigint').unstringifyBigInts
2019-07-12 18:34:25 +02:00
2019-07-16 17:58:42 +02:00
const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes))
const pedersenHash = (data) => babyjub.unpackPoint(pedersen.hash(data))[0]
2019-07-12 18:34:25 +02:00
2019-07-16 17:38:55 +02:00
function snarkVerify(proof) {
2019-07-16 17:58:42 +02:00
proof = unstringifyBigInts2(websnarkUtils.fromSolidityInput(proof))
const verification_key = unstringifyBigInts2(require('../build/circuits/withdraw_verification_key.json'))
return groth.isValid(verification_key, proof, proof.publicSignals)
2019-07-12 18:34:25 +02:00
}
2019-07-16 17:58:42 +02:00
module.exports = { rbigint, pedersenHash, snarkVerify }