2019-07-12 18:34:25 +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;
|
2019-07-15 18:23:03 +02:00
|
|
|
const websnarkUtils = require('websnark/src/utils');
|
2019-07-12 22:52:10 +02:00
|
|
|
const unstringifyBigInts2 = require("snarkjs/src/stringifybigint").unstringifyBigInts;
|
2019-07-12 18:34:25 +02:00
|
|
|
|
|
|
|
const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes));
|
2019-07-15 18:23:03 +02:00
|
|
|
const pedersenHash = (data) => babyjub.unpackPoint(pedersen.hash(data))[0];
|
2019-07-12 18:34:25 +02:00
|
|
|
|
2019-07-12 22:52:10 +02:00
|
|
|
async function snarkVerify(proof) {
|
2019-07-15 18:23:03 +02:00
|
|
|
proof = unstringifyBigInts2(websnarkUtils.fromSolidityInput(proof));
|
2019-07-12 22:52:10 +02:00
|
|
|
const verification_key = unstringifyBigInts2(require('../build/circuits/withdraw_verification_key.json'));
|
2019-07-15 18:23:03 +02:00
|
|
|
return groth.isValid(verification_key, proof, proof.publicSignals);
|
2019-07-12 18:34:25 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 22:15:41 +02:00
|
|
|
module.exports = {rbigint, pedersenHash, snarkVerify};
|