remove utils.js

This commit is contained in:
poma 2019-07-18 21:27:51 +03:00
parent 5cd7544a29
commit 6b3b7ca72b
No known key found for this signature in database
GPG Key ID: 530BBEE4AE8C3604
3 changed files with 28 additions and 32 deletions

10
cli.js
View File

@ -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 })

View File

@ -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 }

View File

@ -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
})