2019-07-16 18:23:12 +02:00
|
|
|
/* global artifacts, web3, contract */
|
|
|
|
require('chai')
|
2019-07-12 17:04:45 +02:00
|
|
|
.use(require('bn-chai')(web3.utils.BN))
|
|
|
|
.use(require('chai-as-promised'))
|
2019-07-16 18:23:12 +02:00
|
|
|
.should()
|
2019-07-15 19:21:58 +02:00
|
|
|
const fs = require('fs')
|
2019-07-12 17:04:45 +02:00
|
|
|
|
2019-07-16 18:23:12 +02:00
|
|
|
const { toBN, toHex, randomHex } = require('web3-utils')
|
2019-07-18 20:38:21 +02:00
|
|
|
const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper')
|
2019-07-12 17:04:45 +02:00
|
|
|
|
|
|
|
const Mixer = artifacts.require('./Mixer.sol')
|
2019-07-16 11:50:52 +02:00
|
|
|
const { AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT } = process.env
|
2019-07-12 17:04:45 +02:00
|
|
|
|
2019-07-15 19:21:58 +02:00
|
|
|
const websnarkUtils = require('websnark/src/utils')
|
2019-07-16 18:23:12 +02:00
|
|
|
const buildGroth16 = require('websnark/src/groth16')
|
2019-07-15 17:04:48 +02:00
|
|
|
const stringifyBigInts = require('websnark/tools/stringifybigint').stringifyBigInts
|
2019-07-19 14:06:06 +02:00
|
|
|
const unstringifyBigInts2 = require('snarkjs/src/stringifybigint').unstringifyBigInts
|
2019-07-16 18:23:12 +02:00
|
|
|
const snarkjs = require('snarkjs')
|
|
|
|
const bigInt = snarkjs.bigInt
|
2019-07-18 20:27:51 +02:00
|
|
|
const crypto = require('crypto')
|
|
|
|
const circomlib = require('circomlib')
|
2019-07-12 21:11:37 +02:00
|
|
|
const MerkleTree = require('../lib/MerkleTree')
|
|
|
|
|
2019-07-18 20:27:51 +02:00
|
|
|
const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes))
|
|
|
|
const pedersenHash = (data) => circomlib.babyJub.unpackPoint(circomlib.pedersenHash.hash(data))[0]
|
|
|
|
|
2019-07-12 21:11:37 +02:00
|
|
|
function generateDeposit() {
|
|
|
|
let deposit = {
|
2019-07-18 20:27:51 +02:00
|
|
|
secret: rbigint(31),
|
|
|
|
nullifier: rbigint(31),
|
2019-07-16 18:23:12 +02:00
|
|
|
}
|
2019-08-01 16:49:34 +02:00
|
|
|
const preimage = Buffer.concat([deposit.nullifier.leInt2Buff(31), deposit.secret.leInt2Buff(31)])
|
2019-07-18 20:27:51 +02:00
|
|
|
deposit.commitment = pedersenHash(preimage)
|
2019-07-16 18:23:12 +02:00
|
|
|
return deposit
|
2019-07-12 21:11:37 +02:00
|
|
|
}
|
2019-07-12 17:04:45 +02:00
|
|
|
|
2019-07-16 18:23:12 +02:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2019-07-15 17:04:48 +02:00
|
|
|
function BNArrayToStringArray(array) {
|
|
|
|
const arrayToPrint = []
|
|
|
|
array.forEach(item => {
|
|
|
|
arrayToPrint.push(item.toString())
|
|
|
|
})
|
|
|
|
return arrayToPrint
|
|
|
|
}
|
|
|
|
|
2019-07-15 19:21:58 +02:00
|
|
|
function getRandomReceiver() {
|
2019-07-18 20:27:51 +02:00
|
|
|
let receiver = rbigint(20)
|
2019-07-15 19:21:58 +02:00
|
|
|
while (toHex(receiver.toString()).length !== 42) {
|
2019-07-18 20:27:51 +02:00
|
|
|
receiver = rbigint(20)
|
2019-07-15 19:21:58 +02:00
|
|
|
}
|
|
|
|
return receiver
|
|
|
|
}
|
|
|
|
|
2019-07-18 20:27:51 +02:00
|
|
|
function snarkVerify(proof) {
|
2019-07-19 14:06:06 +02:00
|
|
|
proof = unstringifyBigInts2(websnarkUtils.fromSolidityInput(proof))
|
|
|
|
const verification_key = unstringifyBigInts2(require('../build/circuits/withdraw_verification_key.json'))
|
2019-07-18 20:27:51 +02:00
|
|
|
return snarkjs['groth'].isValid(verification_key, proof, proof.publicSignals)
|
|
|
|
}
|
|
|
|
|
2019-07-16 18:23:12 +02:00
|
|
|
contract('Mixer', accounts => {
|
2019-07-12 17:04:45 +02:00
|
|
|
let mixer
|
|
|
|
const sender = accounts[0]
|
2019-07-16 11:50:52 +02:00
|
|
|
const levels = MERKLE_TREE_HEIGHT || 16
|
|
|
|
const zeroValue = EMPTY_ELEMENT || 1337
|
2019-08-01 11:58:57 +02:00
|
|
|
const value = AMOUNT || '1000000000000000000' // 1 ether
|
2019-07-12 17:04:45 +02:00
|
|
|
let snapshotId
|
|
|
|
let prefix = 'test'
|
|
|
|
let tree
|
2019-08-01 11:58:57 +02:00
|
|
|
const fee = bigInt(AMOUNT).shr(1) || bigInt(1e17)
|
2019-07-15 19:21:58 +02:00
|
|
|
const receiver = getRandomReceiver()
|
2019-07-15 17:04:48 +02:00
|
|
|
const relayer = accounts[1]
|
2019-07-15 19:21:58 +02:00
|
|
|
let groth16
|
|
|
|
let circuit
|
|
|
|
let proving_key
|
2019-07-12 17:04:45 +02:00
|
|
|
|
|
|
|
before(async () => {
|
2019-07-12 21:11:37 +02:00
|
|
|
tree = new MerkleTree(
|
|
|
|
levels,
|
|
|
|
zeroValue,
|
2019-07-13 11:14:27 +02:00
|
|
|
null,
|
|
|
|
prefix,
|
2019-07-12 21:11:37 +02:00
|
|
|
)
|
2019-07-12 17:04:45 +02:00
|
|
|
mixer = await Mixer.deployed()
|
|
|
|
snapshotId = await takeSnapshot()
|
2019-07-15 19:21:58 +02:00
|
|
|
groth16 = await buildGroth16()
|
2019-07-16 18:23:12 +02:00
|
|
|
circuit = require('../build/circuits/withdraw.json')
|
|
|
|
proving_key = fs.readFileSync('build/circuits/withdraw_proving_key.bin').buffer
|
2019-07-12 17:04:45 +02:00
|
|
|
})
|
|
|
|
|
2019-07-16 18:23:12 +02:00
|
|
|
describe('#constructor', () => {
|
2019-07-12 17:04:45 +02:00
|
|
|
it('should initialize', async () => {
|
|
|
|
const transferValue = await mixer.transferValue()
|
2019-07-16 11:50:52 +02:00
|
|
|
transferValue.should.be.eq.BN(toBN(value))
|
2019-07-12 17:04:45 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-07-16 18:23:12 +02:00
|
|
|
describe('#deposit', () => {
|
2019-07-12 21:11:37 +02:00
|
|
|
it('should emit event', async () => {
|
2019-07-19 19:08:47 +02:00
|
|
|
let commitment = 42
|
|
|
|
let { logs } = await mixer.deposit(commitment, { value, from: sender })
|
|
|
|
|
|
|
|
logs[0].event.should.be.equal('Deposit')
|
|
|
|
logs[0].args.commitment.should.be.eq.BN(toBN(commitment))
|
|
|
|
logs[0].args.leafIndex.should.be.eq.BN(toBN(0))
|
|
|
|
|
|
|
|
commitment = 12;
|
|
|
|
({ logs } = await mixer.deposit(commitment, { value, from: accounts[2] }))
|
|
|
|
|
|
|
|
logs[0].event.should.be.equal('Deposit')
|
|
|
|
logs[0].args.commitment.should.be.eq.BN(toBN(commitment))
|
|
|
|
logs[0].args.leafIndex.should.be.eq.BN(toBN(1))
|
2019-07-12 21:11:37 +02:00
|
|
|
})
|
2019-07-15 17:04:48 +02:00
|
|
|
|
2019-08-01 10:41:22 +02:00
|
|
|
it('should not deposit if disabled', async () => {
|
|
|
|
let commitment = 42;
|
|
|
|
(await mixer.isDepositsEnabled()).should.be.equal(true)
|
|
|
|
const err = await mixer.toggleDeposits({ from: accounts[1] }).should.be.rejected
|
|
|
|
err.reason.should.be.equal('unauthorized')
|
|
|
|
await mixer.toggleDeposits({ from: sender });
|
|
|
|
(await mixer.isDepositsEnabled()).should.be.equal(false)
|
|
|
|
let error = await mixer.deposit(commitment, { value, from: sender }).should.be.rejected
|
|
|
|
error.reason.should.be.equal('deposits disabled')
|
|
|
|
})
|
|
|
|
|
2019-07-15 17:04:48 +02:00
|
|
|
it('should throw if there is a such commitment', async () => {
|
|
|
|
const commitment = 42
|
2019-07-16 11:50:52 +02:00
|
|
|
await mixer.deposit(commitment, { value, from: sender }).should.be.fulfilled
|
|
|
|
const error = await mixer.deposit(commitment, { value, from: sender }).should.be.rejected
|
2019-07-15 17:04:48 +02:00
|
|
|
error.reason.should.be.equal('The commitment has been submitted')
|
|
|
|
})
|
2019-07-12 21:11:37 +02:00
|
|
|
})
|
|
|
|
|
2019-07-16 18:23:12 +02:00
|
|
|
describe('snark proof verification on js side', () => {
|
2019-07-15 19:21:58 +02:00
|
|
|
it('should detect tampering', async () => {
|
|
|
|
const deposit = generateDeposit()
|
|
|
|
await tree.insert(deposit.commitment)
|
2019-07-16 18:23:12 +02:00
|
|
|
const { root, path_elements, path_index } = await tree.path(0)
|
2019-07-15 19:21:58 +02:00
|
|
|
|
|
|
|
const input = stringifyBigInts({
|
|
|
|
root,
|
2019-08-01 16:49:34 +02:00
|
|
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
2019-07-15 19:21:58 +02:00
|
|
|
nullifier: deposit.nullifier,
|
|
|
|
receiver,
|
|
|
|
fee,
|
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
|
|
|
pathIndex: path_index,
|
|
|
|
})
|
|
|
|
|
|
|
|
let proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
|
|
|
const originalProof = JSON.parse(JSON.stringify(proof))
|
2019-07-18 20:27:51 +02:00
|
|
|
let result = snarkVerify(proof)
|
2019-07-15 19:21:58 +02:00
|
|
|
result.should.be.equal(true)
|
|
|
|
|
|
|
|
// nullifier
|
|
|
|
proof.publicSignals[1] = '133792158246920651341275668520530514036799294649489851421007411546007850802'
|
2019-07-18 20:27:51 +02:00
|
|
|
result = snarkVerify(proof)
|
2019-07-15 19:21:58 +02:00
|
|
|
result.should.be.equal(false)
|
|
|
|
proof = originalProof
|
|
|
|
|
|
|
|
// try to cheat with recipient
|
|
|
|
proof.publicSignals[2] = '133738360804642228759657445999390850076318544422'
|
2019-07-18 20:27:51 +02:00
|
|
|
result = snarkVerify(proof)
|
2019-07-15 19:21:58 +02:00
|
|
|
result.should.be.equal(false)
|
|
|
|
proof = originalProof
|
|
|
|
|
|
|
|
// fee
|
|
|
|
proof.publicSignals[3] = '1337100000000000000000'
|
2019-07-18 20:27:51 +02:00
|
|
|
result = snarkVerify(proof)
|
2019-07-15 19:21:58 +02:00
|
|
|
result.should.be.equal(false)
|
|
|
|
proof = originalProof
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-07-16 18:23:12 +02:00
|
|
|
describe('#withdraw', () => {
|
2019-07-12 23:50:26 +02:00
|
|
|
it('should work', async () => {
|
2019-07-12 21:11:37 +02:00
|
|
|
const deposit = generateDeposit()
|
2019-07-15 12:42:59 +02:00
|
|
|
const user = accounts[4]
|
2019-07-12 21:11:37 +02:00
|
|
|
await tree.insert(deposit.commitment)
|
2019-07-15 12:42:59 +02:00
|
|
|
|
|
|
|
const balanceUserBefore = await web3.eth.getBalance(user)
|
|
|
|
|
2019-08-01 11:44:20 +02:00
|
|
|
// Uncomment to measure gas usage
|
|
|
|
// let gas = await mixer.deposit.estimateGas(toBN(deposit.commitment.toString()), { value, from: user, gasPrice: '0' })
|
|
|
|
// console.log('deposit gas:', gas)
|
2019-07-16 11:50:52 +02:00
|
|
|
await mixer.deposit(toBN(deposit.commitment.toString()), { value, from: user, gasPrice: '0' })
|
2019-07-15 12:42:59 +02:00
|
|
|
|
|
|
|
const balanceUserAfter = await web3.eth.getBalance(user)
|
2019-07-16 11:50:52 +02:00
|
|
|
balanceUserAfter.should.be.eq.BN(toBN(balanceUserBefore).sub(toBN(value)))
|
2019-07-12 21:11:37 +02:00
|
|
|
|
2019-07-16 18:23:12 +02:00
|
|
|
const { root, path_elements, path_index } = await tree.path(0)
|
2019-07-12 21:11:37 +02:00
|
|
|
|
|
|
|
// Circuit input
|
|
|
|
const input = stringifyBigInts({
|
|
|
|
// public
|
2019-07-15 12:42:59 +02:00
|
|
|
root,
|
2019-08-01 16:49:34 +02:00
|
|
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
2019-07-15 12:42:59 +02:00
|
|
|
receiver,
|
|
|
|
fee,
|
2019-07-12 21:11:37 +02:00
|
|
|
|
|
|
|
// private
|
2019-07-23 22:00:45 +02:00
|
|
|
nullifier: deposit.nullifier,
|
2019-07-12 21:11:37 +02:00
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
|
|
|
pathIndex: path_index,
|
|
|
|
})
|
|
|
|
|
2019-07-23 22:00:45 +02:00
|
|
|
|
2019-07-15 20:57:06 +02:00
|
|
|
const proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
|
|
|
const { pi_a, pi_b, pi_c, publicSignals } = websnarkUtils.toSolidityInput(proof)
|
2019-07-12 23:50:26 +02:00
|
|
|
|
2019-07-15 12:42:59 +02:00
|
|
|
const balanceMixerBefore = await web3.eth.getBalance(mixer.address)
|
|
|
|
const balanceRelayerBefore = await web3.eth.getBalance(relayer)
|
|
|
|
const balanceRecieverBefore = await web3.eth.getBalance(toHex(receiver.toString()))
|
2019-07-25 15:58:21 +02:00
|
|
|
let isSpent = await mixer.isSpent(input.nullifierHash.toString(16).padStart(66, '0x00000'))
|
|
|
|
isSpent.should.be.equal(false)
|
2019-07-15 12:42:59 +02:00
|
|
|
|
2019-08-01 11:44:20 +02:00
|
|
|
// Uncomment to measure gas usage
|
|
|
|
// gas = await mixer.withdraw.estimateGas(pi_a, pi_b, pi_c, publicSignals, { from: relayer, gasPrice: '0' })
|
|
|
|
// console.log('withdraw gas:', gas)
|
2019-07-15 12:42:59 +02:00
|
|
|
const { logs } = await mixer.withdraw(pi_a, pi_b, pi_c, publicSignals, { from: relayer, gasPrice: '0' })
|
|
|
|
|
|
|
|
const balanceMixerAfter = await web3.eth.getBalance(mixer.address)
|
|
|
|
const balanceRelayerAfter = await web3.eth.getBalance(relayer)
|
|
|
|
const balanceRecieverAfter = await web3.eth.getBalance(toHex(receiver.toString()))
|
2019-07-15 17:04:48 +02:00
|
|
|
const feeBN = toBN(fee.toString())
|
2019-07-16 11:50:52 +02:00
|
|
|
balanceMixerAfter.should.be.eq.BN(toBN(balanceMixerBefore).sub(toBN(value)))
|
2019-07-15 17:04:48 +02:00
|
|
|
balanceRelayerAfter.should.be.eq.BN(toBN(balanceRelayerBefore).add(feeBN))
|
2019-07-16 11:50:52 +02:00
|
|
|
balanceRecieverAfter.should.be.eq.BN(toBN(balanceRecieverBefore).add(toBN(value)).sub(feeBN))
|
2019-07-15 12:42:59 +02:00
|
|
|
|
2019-07-25 15:58:21 +02:00
|
|
|
|
2019-07-12 23:50:26 +02:00
|
|
|
logs[0].event.should.be.equal('Withdraw')
|
2019-07-25 15:58:21 +02:00
|
|
|
logs[0].args.nullifierHash.should.be.eq.BN(toBN(input.nullifierHash.toString()))
|
2019-07-15 17:04:48 +02:00
|
|
|
logs[0].args.fee.should.be.eq.BN(feeBN)
|
2019-07-25 15:58:21 +02:00
|
|
|
isSpent = await mixer.isSpent(input.nullifierHash.toString(16).padStart(66, '0x00000'))
|
|
|
|
isSpent.should.be.equal(true)
|
2019-07-15 17:04:48 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should prevent double spend', async () => {
|
|
|
|
const deposit = generateDeposit()
|
|
|
|
await tree.insert(deposit.commitment)
|
2019-07-16 11:50:52 +02:00
|
|
|
await mixer.deposit(toBN(deposit.commitment.toString()), { value, from: sender })
|
2019-07-15 17:04:48 +02:00
|
|
|
|
2019-07-16 18:23:12 +02:00
|
|
|
const { root, path_elements, path_index } = await tree.path(0)
|
2019-07-15 17:04:48 +02:00
|
|
|
|
|
|
|
const input = stringifyBigInts({
|
|
|
|
root,
|
2019-08-01 16:49:34 +02:00
|
|
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
2019-07-15 17:04:48 +02:00
|
|
|
nullifier: deposit.nullifier,
|
|
|
|
receiver,
|
|
|
|
fee,
|
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
|
|
|
pathIndex: path_index,
|
|
|
|
})
|
2019-07-15 20:57:06 +02:00
|
|
|
const proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
|
|
|
const { pi_a, pi_b, pi_c, publicSignals } = websnarkUtils.toSolidityInput(proof)
|
2019-07-15 17:04:48 +02:00
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
2019-08-01 09:33:12 +02:00
|
|
|
it('should prevent double spend with overflow', async () => {
|
|
|
|
const deposit = generateDeposit()
|
|
|
|
await tree.insert(deposit.commitment)
|
|
|
|
await mixer.deposit(toBN(deposit.commitment.toString()), { value, from: sender })
|
|
|
|
|
|
|
|
const { root, path_elements, path_index } = await tree.path(0)
|
|
|
|
|
|
|
|
const input = stringifyBigInts({
|
|
|
|
root,
|
2019-08-01 16:49:34 +02:00
|
|
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
2019-08-01 09:33:12 +02:00
|
|
|
nullifier: deposit.nullifier,
|
|
|
|
receiver,
|
|
|
|
fee,
|
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
|
|
|
pathIndex: path_index,
|
|
|
|
})
|
|
|
|
const proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
|
|
|
const { pi_a, pi_b, pi_c, publicSignals } = websnarkUtils.toSolidityInput(proof)
|
|
|
|
publicSignals[1] ='0x' + toBN(publicSignals[1]).add(toBN('21888242871839275222246405745257275088548364400416034343698204186575808495617')).toString('hex')
|
|
|
|
const error = await mixer.withdraw(pi_a, pi_b, pi_c, publicSignals, { from: relayer }).should.be.rejected
|
|
|
|
error.reason.should.be.equal('verifier-gte-snark-scalar-field')
|
|
|
|
})
|
|
|
|
|
2019-07-15 17:04:48 +02:00
|
|
|
it('fee should be less or equal transfer value', async () => {
|
|
|
|
const deposit = generateDeposit()
|
|
|
|
await tree.insert(deposit.commitment)
|
2019-07-16 11:50:52 +02:00
|
|
|
await mixer.deposit(toBN(deposit.commitment.toString()), { value, from: sender })
|
2019-07-15 17:04:48 +02:00
|
|
|
|
2019-07-16 18:23:12 +02:00
|
|
|
const { root, path_elements, path_index } = await tree.path(0)
|
|
|
|
const oneEtherFee = bigInt(1e18) // 1 ether
|
2019-07-15 17:04:48 +02:00
|
|
|
const input = stringifyBigInts({
|
|
|
|
root,
|
2019-08-01 16:49:34 +02:00
|
|
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
2019-07-15 17:04:48 +02:00
|
|
|
nullifier: deposit.nullifier,
|
|
|
|
receiver,
|
|
|
|
fee: oneEtherFee,
|
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
|
|
|
pathIndex: path_index,
|
|
|
|
})
|
|
|
|
|
2019-07-15 20:57:06 +02:00
|
|
|
const proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
|
|
|
const { pi_a, pi_b, pi_c, publicSignals } = websnarkUtils.toSolidityInput(proof)
|
2019-07-15 17:04:48 +02:00
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should throw for corrupted merkle tree root', async () => {
|
|
|
|
const deposit = generateDeposit()
|
|
|
|
await tree.insert(deposit.commitment)
|
2019-07-16 11:50:52 +02:00
|
|
|
await mixer.deposit(toBN(deposit.commitment.toString()), { value, from: sender })
|
2019-07-15 17:04:48 +02:00
|
|
|
|
2019-07-16 18:23:12 +02:00
|
|
|
const { root, path_elements, path_index } = await tree.path(0)
|
2019-07-15 17:04:48 +02:00
|
|
|
|
|
|
|
const input = stringifyBigInts({
|
2019-08-01 16:49:34 +02:00
|
|
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
2019-07-15 17:04:48 +02:00
|
|
|
root,
|
|
|
|
nullifier: deposit.nullifier,
|
|
|
|
receiver,
|
|
|
|
fee,
|
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
|
|
|
pathIndex: path_index,
|
|
|
|
})
|
|
|
|
|
|
|
|
const dummyRoot = randomHex(32)
|
2019-07-15 20:57:06 +02:00
|
|
|
const proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
|
|
|
const { pi_a, pi_b, pi_c, publicSignals } = websnarkUtils.toSolidityInput(proof)
|
2019-07-15 17:04:48 +02:00
|
|
|
publicSignals[0] = dummyRoot
|
|
|
|
|
|
|
|
const error = await mixer.withdraw(pi_a, pi_b, pi_c, publicSignals, { from: relayer }).should.be.rejected
|
|
|
|
error.reason.should.be.equal('Cannot find your merkle root')
|
2019-07-12 21:11:37 +02:00
|
|
|
})
|
2019-07-15 19:21:58 +02:00
|
|
|
|
|
|
|
it('should reject with tampered public inputs', async () => {
|
|
|
|
const deposit = generateDeposit()
|
|
|
|
await tree.insert(deposit.commitment)
|
2019-07-16 11:50:52 +02:00
|
|
|
await mixer.deposit(toBN(deposit.commitment.toString()), { value, from: sender })
|
2019-07-15 19:21:58 +02:00
|
|
|
|
2019-07-16 18:23:12 +02:00
|
|
|
let { root, path_elements, path_index } = await tree.path(0)
|
2019-07-15 19:21:58 +02:00
|
|
|
|
2019-07-15 20:57:06 +02:00
|
|
|
const input = stringifyBigInts({
|
2019-07-15 19:21:58 +02:00
|
|
|
root,
|
2019-08-01 16:49:34 +02:00
|
|
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
2019-07-15 19:21:58 +02:00
|
|
|
nullifier: deposit.nullifier,
|
|
|
|
receiver,
|
|
|
|
fee,
|
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
|
|
|
pathIndex: path_index,
|
|
|
|
})
|
2019-07-15 20:57:06 +02:00
|
|
|
const proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
|
|
|
let { pi_a, pi_b, pi_c, publicSignals } = websnarkUtils.toSolidityInput(proof)
|
2019-07-15 19:21:58 +02:00
|
|
|
const originalPublicSignals = publicSignals.slice()
|
|
|
|
const originalPi_a = pi_a.slice()
|
|
|
|
|
|
|
|
// receiver
|
|
|
|
publicSignals[2] = '0x0000000000000000000000007a1f9131357404ef86d7c38dbffed2da70321337'
|
|
|
|
|
|
|
|
let error = await mixer.withdraw(pi_a, pi_b, pi_c, publicSignals, { from: relayer }).should.be.rejected
|
2019-07-16 18:23:12 +02:00
|
|
|
error.reason.should.be.equal('Invalid withdraw proof')
|
2019-07-15 19:21:58 +02:00
|
|
|
|
|
|
|
// fee
|
|
|
|
publicSignals = originalPublicSignals.slice()
|
|
|
|
publicSignals[3] = '0x000000000000000000000000000000000000000000000000015345785d8a0000'
|
|
|
|
|
|
|
|
error = await mixer.withdraw(pi_a, pi_b, pi_c, publicSignals, { from: relayer }).should.be.rejected
|
2019-07-16 18:23:12 +02:00
|
|
|
error.reason.should.be.equal('Invalid withdraw proof')
|
2019-07-15 19:21:58 +02:00
|
|
|
|
|
|
|
// nullifier
|
|
|
|
publicSignals = originalPublicSignals.slice()
|
|
|
|
publicSignals[1] = '0x00abdfc78211f8807b9c6504a6e537e71b8788b2f529a95f1399ce124a8642ad'
|
|
|
|
|
|
|
|
error = await mixer.withdraw(pi_a, pi_b, pi_c, publicSignals, { from: relayer }).should.be.rejected
|
2019-07-16 18:23:12 +02:00
|
|
|
error.reason.should.be.equal('Invalid withdraw proof')
|
2019-07-15 19:21:58 +02:00
|
|
|
|
|
|
|
// proof itself
|
|
|
|
pi_a[0] = '0x261d81d8203437f29b38a88c4263476d858e6d9645cf21740461684412b31337'
|
|
|
|
await mixer.withdraw(pi_a, pi_b, pi_c, originalPublicSignals, { from: relayer }).should.be.rejected
|
|
|
|
|
|
|
|
// should work with original values
|
|
|
|
await mixer.withdraw(originalPi_a, pi_b, pi_c, originalPublicSignals, { from: relayer }).should.be.fulfilled
|
|
|
|
})
|
2019-07-12 21:11:37 +02:00
|
|
|
})
|
|
|
|
|
2019-07-12 17:04:45 +02:00
|
|
|
afterEach(async () => {
|
|
|
|
await revertSnapshot(snapshotId.result)
|
2019-07-16 18:23:12 +02:00
|
|
|
// eslint-disable-next-line require-atomic-updates
|
2019-07-12 17:04:45 +02:00
|
|
|
snapshotId = await takeSnapshot()
|
2019-07-15 17:04:48 +02:00
|
|
|
tree = new MerkleTree(
|
|
|
|
levels,
|
|
|
|
zeroValue,
|
|
|
|
null,
|
|
|
|
prefix,
|
|
|
|
)
|
2019-07-12 17:04:45 +02:00
|
|
|
})
|
|
|
|
})
|