diff --git a/.gitignore b/.gitignore index cba6509..169fdb6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ build +.vscode # Created by .ignore support plugin (hsz.mobi) ### Node template diff --git a/README.md b/README.md index d163bcb..405a251 100644 --- a/README.md +++ b/README.md @@ -7,5 +7,8 @@ ## Deploy 1. `npx truffle migrate --network kovan --reset` +## Requirements +1. `node v11.15.0` + diff --git a/test/test_snark.js b/scripts/test_snark.js similarity index 87% rename from test/test_snark.js rename to scripts/test_snark.js index 084301d..f1c9e0c 100644 --- a/test/test_snark.js +++ b/scripts/test_snark.js @@ -3,8 +3,8 @@ const circom = require("circom"); const snarkjs = require("snarkjs"); const circomlib = require('circomlib'); const bigInt = snarkjs.bigInt; -const stringifyBigInts = require("../node_modules/websnark/tools/stringifybigint.js").stringifyBigInts; -const unstringifyBigInts = require("../node_modules/websnark/tools/stringifybigint.js").unstringifyBigInts; +const stringifyBigInts = require("websnark/tools/stringifybigint").stringifyBigInts; +const unstringifyBigInts = require("websnark/tools/stringifybigint").unstringifyBigInts; const utils = require("./utils"); const merkleTree = require('../lib/MerkleTree'); const jsStorage = require("../lib/Storage"); diff --git a/test/utils.js b/scripts/utils.js similarity index 86% rename from test/utils.js rename to scripts/utils.js index f53474d..232c5ae 100644 --- a/test/utils.js +++ b/scripts/utils.js @@ -8,9 +8,9 @@ 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 rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes)); @@ -72,7 +72,7 @@ async function snarkProof(input) { const witnessArray = circuit.calculateWitness(input); const witness = convertWitness(witnessArray); const publicSignals = witnessArray.slice(1, circuit.nPubInputs + circuit.nOutputs + 1); - const key = toArrayBuffer(fs.readFileSync("../build/circuits/withdraw_proving_key.bin")); + const key = toArrayBuffer(fs.readFileSync("build/circuits/withdraw_proving_key.bin")); const groth16 = await buildGroth16(); let proof = await groth16.proof(witness, key); proof = unstringifyBigInts(proof); diff --git a/test/Mixer.test.js b/test/Mixer.test.js index 1a3a79c..a4532f3 100644 --- a/test/Mixer.test.js +++ b/test/Mixer.test.js @@ -7,7 +7,25 @@ const { toWei, toBN, fromWei } = require('web3-utils') const { takeSnapshot, revertSnapshot, increaseTime } = require('../scripts/ganacheHelper'); const Mixer = artifacts.require('./Mixer.sol') +const { AMOUNT } = process.env +const utils = require("../scripts/utils") +const stringifyBigInts = require("websnark/tools/stringifybigint").stringifyBigInts +const snarkjs = require("snarkjs"); +const bigInt = snarkjs.bigInt; +const JsStorage = require('../lib/Storage') +const MerkleTree = require('../lib/MerkleTree') +const MimcHacher = require('../lib/MiMC') + +function generateDeposit() { + let deposit = { + secret: utils.rbigint(31), + nullifier: utils.rbigint(31), + }; + const preimage = Buffer.concat([deposit.nullifier.leInt2Buff(32), deposit.secret.leInt2Buff(32)]); + deposit.commitment = utils.pedersenHash(preimage); + return deposit; +} contract('Mixer', async accounts => { let mixer @@ -22,18 +40,69 @@ contract('Mixer', async accounts => { let hasher before(async () => { + const storage = new JsStorage() + hasher = new MimcHacher() + tree = new MerkleTree( + prefix, + storage, + hasher, + levels, + zeroValue, + ) mixer = await Mixer.deployed() snapshotId = await takeSnapshot() }) describe('#constructor', async () => { it('should initialize', async () => { - const { AMOUNT } = process.env const transferValue = await mixer.transferValue() transferValue.should.be.eq.BN(toBN(AMOUNT)) }) }) + describe('#deposit', async () => { + it('should emit event', async () => { + const commitment = 42 + const { logs } = await mixer.deposit(commitment, { value: AMOUNT, from: sender }) + logs[0].event.should.be.equal('LeafAdded') + logs[0].args.leaf.should.be.eq.BN(toBN(commitment)) + logs[0].args.leaf_index.should.be.eq.BN(toBN(0)) + + logs[1].event.should.be.equal('Deposit') + logs[1].args.from.should.be.equal(sender) + logs[1].args.commitment.should.be.eq.BN(toBN(commitment)) + }) + }) + + describe('#withdraw', async () => { + it.skip('should work', async () => { + const deposit = generateDeposit() + await tree.insert(deposit.commitment) + await mixer.deposit(toBN(deposit.commitment.toString()), { value: AMOUNT, from: sender }) + + const {root, path_elements, path_index} = await tree.path(0); + + // Circuit input + const input = stringifyBigInts({ + // public + root: root, + nullifier: deposit.nullifier, + receiver: utils.rbigint(20), + fee: bigInt(1e17), + + // private + secret: deposit.secret, + pathElements: path_elements, + pathIndex: path_index, + }) + + const { pi_a, pi_b, pi_c, publicSignals } = await utils.snarkProof(input) + console.log('proof', pi_a, pi_b, pi_c, publicSignals) + const { logs } = await mixer.withdraw(pi_a, pi_b, pi_c, publicSignals, { from: sender }) + console.log('logs', logs) + }) + }) + afterEach(async () => { await revertSnapshot(snapshotId.result) snapshotId = await takeSnapshot()