Merge branch 'master' of github.com:poma/mixer-huyixer

This commit is contained in:
Roman Storm 2019-07-15 12:19:42 -07:00
commit a84bf5be55
2 changed files with 11 additions and 31 deletions

View File

@ -1,40 +1,15 @@
// 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");
const groth = snarkjs["groth"];
const crypto = require("crypto");
const circomlib = require('circomlib');
const pedersen = circomlib.pedersenHash;
const babyjub = circomlib.babyJub;
const bigInt = snarkjs.bigInt;
const buildGroth16 = require('websnark/src/groth16');
const websnarkUtils = require('websnark/src/utils');
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));
const pedersenHash = (data) => babyjub.unpackPoint(pedersen.hash(data))[0];
async function snarkProof(input) {
const witness = require("../build/circuits/withdraw.json");
const pwd = process.cwd();
let pathToProvingKey = 'build/circuits/withdraw_proving_key.bin';
if (pwd.split('/').pop() === 'scripts') {
pathToProvingKey = '../build/circuits/withdraw_proving_key.bin'
}
const proving_key = fs.readFileSync(pathToProvingKey);
const groth16 = await buildGroth16();
let proof = await websnarkUtils.genWitnessAndProve(groth16, input, witness, proving_key.buffer);
return websnarkUtils.toSolidityInput(proof);
}
async function snarkVerify(proof) {
proof = unstringifyBigInts2(websnarkUtils.fromSolidityInput(proof));
const verification_key = unstringifyBigInts2(require('../build/circuits/withdraw_verification_key.json'));

View File

@ -172,7 +172,8 @@ contract('Mixer', async accounts => {
pathIndex: path_index,
})
const { pi_a, pi_b, pi_c, publicSignals } = await utils.snarkProof(input)
const proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
const { pi_a, pi_b, pi_c, publicSignals } = websnarkUtils.toSolidityInput(proof)
const balanceMixerBefore = await web3.eth.getBalance(mixer.address)
const balanceRelayerBefore = await web3.eth.getBalance(relayer)
@ -210,7 +211,8 @@ contract('Mixer', async accounts => {
pathIndex: path_index,
})
const { pi_a, pi_b, pi_c, publicSignals } = await utils.snarkProof(input)
const proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
const { pi_a, pi_b, pi_c, publicSignals } = websnarkUtils.toSolidityInput(proof)
await mixer.withdraw(pi_a, pi_b, pi_c, publicSignals, { from: relayer }).should.be.fulfilled
const error = await mixer.withdraw(pi_a, pi_b, pi_c, publicSignals, { from: relayer }).should.be.rejected
error.reason.should.be.equal('The note has been already spent')
@ -233,7 +235,8 @@ contract('Mixer', async accounts => {
pathIndex: path_index,
})
const { pi_a, pi_b, pi_c, publicSignals } = await utils.snarkProof(input)
const proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
const { pi_a, pi_b, pi_c, publicSignals } = websnarkUtils.toSolidityInput(proof)
const error = await mixer.withdraw(pi_a, pi_b, pi_c, publicSignals, { from: relayer }).should.be.rejected
error.reason.should.be.equal('Fee exceeds transfer value')
})
@ -256,7 +259,8 @@ contract('Mixer', async accounts => {
})
const dummyRoot = randomHex(32)
const { pi_a, pi_b, pi_c, publicSignals } = await utils.snarkProof(input)
const proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
const { pi_a, pi_b, pi_c, publicSignals } = websnarkUtils.toSolidityInput(proof)
publicSignals[0] = dummyRoot
const error = await mixer.withdraw(pi_a, pi_b, pi_c, publicSignals, { from: relayer }).should.be.rejected
@ -270,7 +274,7 @@ contract('Mixer', async accounts => {
let {root, path_elements, path_index} = await tree.path(0)
const userInput = stringifyBigInts({
const input = stringifyBigInts({
root,
nullifier: deposit.nullifier,
receiver,
@ -280,7 +284,8 @@ contract('Mixer', async accounts => {
pathIndex: path_index,
})
let { pi_a, pi_b, pi_c, publicSignals } = await utils.snarkProof(userInput)
const proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
let { pi_a, pi_b, pi_c, publicSignals } = websnarkUtils.toSolidityInput(proof)
const originalPublicSignals = publicSignals.slice()
const originalPi_a = pi_a.slice()