From 6b3b7ca72b34fdbd3c68b8ef94145313bf6c7227 Mon Sep 17 00:00:00 2001 From: poma Date: Thu, 18 Jul 2019 21:27:51 +0300 Subject: [PATCH] remove utils.js --- cli.js | 10 +++++++--- scripts/utils.js | 19 ------------------- test/Mixer.test.js | 31 +++++++++++++++++++++---------- 3 files changed, 28 insertions(+), 32 deletions(-) delete mode 100644 scripts/utils.js diff --git a/cli.js b/cli.js index 8a37b0d..c5802d9 100755 --- a/cli.js +++ b/cli.js @@ -3,8 +3,9 @@ const fs = require('fs') const assert = require('assert') const snarkjs = require('snarkjs') +const crypto = require('crypto') +const circomlib = require('circomlib') const bigInt = snarkjs.bigInt -const utils = require('./scripts/utils') const merkleTree = require('./lib/MerkleTree') const Web3 = require('web3') const buildGroth16 = require('websnark/src/groth16') @@ -14,15 +15,18 @@ let web3, mixer, circuit, proving_key, groth16 let MERKLE_TREE_HEIGHT, AMOUNT, EMPTY_ELEMENT const inBrowser = (typeof window !== 'undefined') +const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes)) +const pedersenHash = (data) => circomlib.babyJub.unpackPoint(circomlib.pedersenHash.hash(data))[0] + function createDeposit(nullifier, secret) { let deposit = { nullifier, secret } deposit.preimage = Buffer.concat([deposit.nullifier.leInt2Buff(32), deposit.secret.leInt2Buff(32)]) - deposit.commitment = utils.pedersenHash(deposit.preimage) + deposit.commitment = pedersenHash(deposit.preimage) return deposit } async function deposit() { - const deposit = createDeposit(utils.rbigint(31), utils.rbigint(31)) + const deposit = createDeposit(rbigint(31), rbigint(31)) console.log('Submitting deposit transaction') await mixer.methods.deposit('0x' + deposit.commitment.toString(16)).send({ value: AMOUNT, from: (await web3.eth.getAccounts())[0], gas:1e6 }) diff --git a/scripts/utils.js b/scripts/utils.js deleted file mode 100644 index 2da67cb..0000000 --- a/scripts/utils.js +++ /dev/null @@ -1,19 +0,0 @@ -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 - -const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes)) -const pedersenHash = (data) => babyjub.unpackPoint(pedersen.hash(data))[0] - -function snarkVerify(proof) { - proof = unstringifyBigInts2(websnarkUtils.fromSolidityInput(proof)) - const verification_key = unstringifyBigInts2(require('../build/circuits/withdraw_verification_key.json')) - return groth.isValid(verification_key, proof, proof.publicSignals) -} - -module.exports = { rbigint, pedersenHash, snarkVerify } diff --git a/test/Mixer.test.js b/test/Mixer.test.js index ba0017f..ba009ed 100644 --- a/test/Mixer.test.js +++ b/test/Mixer.test.js @@ -11,21 +11,26 @@ const { takeSnapshot, revertSnapshot } = require('../scripts/ganacheHelper') const Mixer = artifacts.require('./Mixer.sol') const { AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT } = process.env -const utils = require('../scripts/utils') const websnarkUtils = require('websnark/src/utils') const buildGroth16 = require('websnark/src/groth16') const stringifyBigInts = require('websnark/tools/stringifybigint').stringifyBigInts +const unstringifyBigInts = require('websnark/tools/stringifybigint').unstringifyBigInts const snarkjs = require('snarkjs') const bigInt = snarkjs.bigInt +const crypto = require('crypto') +const circomlib = require('circomlib') const MerkleTree = require('../lib/MerkleTree') +const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes)) +const pedersenHash = (data) => circomlib.babyJub.unpackPoint(circomlib.pedersenHash.hash(data))[0] + function generateDeposit() { let deposit = { - secret: utils.rbigint(31), - nullifier: utils.rbigint(31), + secret: rbigint(31), + nullifier: rbigint(31), } const preimage = Buffer.concat([deposit.nullifier.leInt2Buff(32), deposit.secret.leInt2Buff(32)]) - deposit.commitment = utils.pedersenHash(preimage) + deposit.commitment = pedersenHash(preimage) return deposit } @@ -39,13 +44,19 @@ function BNArrayToStringArray(array) { } function getRandomReceiver() { - let receiver = utils.rbigint(20) + let receiver = rbigint(20) while (toHex(receiver.toString()).length !== 42) { - receiver = utils.rbigint(20) + receiver = rbigint(20) } return receiver } +function snarkVerify(proof) { + proof = unstringifyBigInts(websnarkUtils.fromSolidityInput(proof)) + const verification_key = unstringifyBigInts(require('../build/circuits/withdraw_verification_key.json')) + return snarkjs['groth'].isValid(verification_key, proof, proof.publicSignals) +} + contract('Mixer', accounts => { let mixer const sender = accounts[0] @@ -122,24 +133,24 @@ contract('Mixer', accounts => { let proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key) const originalProof = JSON.parse(JSON.stringify(proof)) - let result = await utils.snarkVerify(proof) + let result = snarkVerify(proof) result.should.be.equal(true) // nullifier proof.publicSignals[1] = '133792158246920651341275668520530514036799294649489851421007411546007850802' - result = await utils.snarkVerify(proof) + result = snarkVerify(proof) result.should.be.equal(false) proof = originalProof // try to cheat with recipient proof.publicSignals[2] = '133738360804642228759657445999390850076318544422' - result = await utils.snarkVerify(proof) + result = snarkVerify(proof) result.should.be.equal(false) proof = originalProof // fee proof.publicSignals[3] = '1337100000000000000000' - result = await utils.snarkVerify(proof) + result = snarkVerify(proof) result.should.be.equal(false) proof = originalProof })