diff --git a/test/test_snark.js b/test/test_snark.js index 084301d..5d6dcf2 100644 --- a/test/test_snark.js +++ b/test/test_snark.js @@ -1,4 +1,5 @@ const fs = require('fs'); +const assert = require('assert'); const circom = require("circom"); const snarkjs = require("snarkjs"); const circomlib = require('circomlib'); @@ -34,7 +35,7 @@ function generateDeposit() { const {root, path_elements, path_index} = await tree.path(1); // Circuit input - const input = stringifyBigInts({ + const input = { // public root: root, nullifier: dep2.nullifier, @@ -45,11 +46,21 @@ function generateDeposit() { secret: dep2.secret, pathElements: path_elements, pathIndex: path_index, - }); + }; console.log("Input:\n", input); console.time("Time"); const proof = await utils.snarkProof(input); console.log("Proof:\n", proof); console.timeEnd("Time"); + + const verify = await utils.snarkVerify(proof); + assert(verify); + + // try to cheat with recipient + proof.publicSignals[2] = '0x000000000000000000000000000000000000000000000000000000000000beef'; + const verifyScam = await utils.snarkVerify(proof); + assert(!verifyScam); + + console.log("Done."); })(); diff --git a/test/utils.js b/test/utils.js index f53474d..9c5350d 100644 --- a/test/utils.js +++ b/test/utils.js @@ -1,3 +1,6 @@ +// This file is a bit of a mess because of different bigInt formats in websnark and snarkjs +// It will be rewritten during browser integration + const fs = require('fs'); const circom = require("circom"); const snarkjs = require("snarkjs"); @@ -8,12 +11,30 @@ const pedersen = circomlib.pedersenHash; const babyjub = circomlib.babyJub; const mimcsponge = circomlib.mimcsponge; const bigInt = snarkjs.bigInt; -const buildGroth16 = require('../node_modules/websnark/src/groth16.js'); -const stringifyBigInts = require("../node_modules/websnark/tools/stringifybigint.js").stringifyBigInts; -const unstringifyBigInts = require("../node_modules/websnark/tools/stringifybigint.js").unstringifyBigInts; +const buildGroth16 = require('websnark/src/groth16'); +const stringifyBigInts = require("websnark/tools/stringifybigint").stringifyBigInts; +const unstringifyBigInts = require("websnark/tools/stringifybigint").unstringifyBigInts; +const stringifyBigInts2 = require("snarkjs/src/stringifybigint").stringifyBigInts; +const unstringifyBigInts2 = require("snarkjs/src/stringifybigint").unstringifyBigInts; const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes)); +function unhexBigInts(o) { + if ((typeof(o) == "string") && (/^0x[0-9a-fA-F]+$/.test(o))) { + return bigInt(o); + } else if (Array.isArray(o)) { + return o.map(unhexBigInts); + } else if (typeof o == "object") { + const res = {}; + for (let k in o) { + res[k] = unhexBigInts(o[k]); + } + return res; + } else { + return o; + } +} + function pedersenHash(data) { return babyjub.unpackPoint(pedersen.hash(data))[0]; } @@ -46,6 +67,7 @@ function p256(o) { } function convertWitness(witness) { + witness = unstringifyBigInts(witness); const buffLen = witness.length * 32; const buff = new ArrayBuffer(buffLen); const h = { @@ -53,9 +75,8 @@ function convertWitness(witness) { offset: 0 }; for (let i=0; i