tornado-core/test/ETHTornado.test.js

542 lines
20 KiB
JavaScript
Raw Normal View History

2019-07-16 18:23:12 +02:00
/* global artifacts, web3, contract */
2021-02-11 07:23:18 +01:00
require('chai').use(require('bn-chai')(web3.utils.BN)).use(require('chai-as-promised')).should()
2019-07-15 19:21:58 +02:00
const fs = require('fs')
2019-07-12 17:04:45 +02:00
2019-11-28 09:04:09 +01:00
const { toBN, 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
2019-12-13 14:49:19 +01:00
const Tornado = artifacts.require('./ETHTornado.sol')
2019-11-02 13:35:22 +01:00
const { ETH_AMOUNT, MERKLE_TREE_HEIGHT } = 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]
2021-02-11 07:23:18 +01:00
const toFixedHex = (number, length = 32) =>
'0x' +
bigInt(number)
.toString(16)
.padStart(length * 2, '0')
2019-11-28 05:52:17 +01:00
const getRandomRecipient = () => rbigint(20)
2019-07-18 20:27:51 +02:00
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 = []
2021-02-11 07:23:18 +01:00
array.forEach((item) => {
2019-07-15 17:04:48 +02:00
arrayToPrint.push(item.toString())
})
return arrayToPrint
}
2019-07-18 20:27:51 +02:00
function snarkVerify(proof) {
2019-10-04 17:20:20 +02:00
proof = unstringifyBigInts2(proof)
2019-07-19 14:06:06 +02:00
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)
}
2021-02-11 07:23:18 +01:00
contract('ETHTornado', (accounts) => {
2019-12-13 14:49:19 +01:00
let tornado
2019-07-12 17:04:45 +02:00
const sender = accounts[0]
2019-08-02 19:12:30 +02:00
const operator = accounts[0]
2019-07-16 11:50:52 +02:00
const levels = MERKLE_TREE_HEIGHT || 16
2019-08-27 22:42:24 +02:00
const value = ETH_AMOUNT || '1000000000000000000' // 1 ether
2019-07-12 17:04:45 +02:00
let snapshotId
let prefix = 'test'
let tree
2019-08-27 22:42:24 +02:00
const fee = bigInt(ETH_AMOUNT).shr(1) || bigInt(1e17)
const refund = bigInt(0)
2019-11-07 08:04:29 +01:00
const recipient = getRandomRecipient()
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 () => {
2021-02-11 07:23:18 +01:00
tree = new MerkleTree(levels, null, prefix)
2019-12-13 14:49:19 +01:00
tornado = await Tornado.deployed()
2019-07-12 17:04:45 +02:00
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 () => {
2019-12-13 14:49:19 +01:00
const etherDenomination = await tornado.denomination()
2019-08-27 22:42:24 +02:00
etherDenomination.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-11-04 22:04:22 +01:00
let commitment = toFixedHex(42)
2019-12-13 14:49:19 +01:00
let { logs } = await tornado.deposit(commitment, { value, from: sender })
2019-07-19 19:08:47 +02:00
logs[0].event.should.be.equal('Deposit')
2019-11-04 22:04:22 +01:00
logs[0].args.commitment.should.be.equal(commitment)
logs[0].args.leafIndex.should.be.eq.BN(0)
2019-07-19 19:08:47 +02:00
2021-02-11 07:23:18 +01:00
commitment = toFixedHex(12)
;({ logs } = await tornado.deposit(commitment, { value, from: accounts[2] }))
2019-07-19 19:08:47 +02:00
logs[0].event.should.be.equal('Deposit')
2019-11-04 22:04:22 +01:00
logs[0].args.commitment.should.be.equal(commitment)
logs[0].args.leafIndex.should.be.eq.BN(1)
2019-07-12 21:11:37 +02:00
})
2019-07-15 17:04:48 +02:00
it('should throw if there is a such commitment', async () => {
2019-11-04 22:04:22 +01:00
const commitment = toFixedHex(42)
2019-12-13 14:49:19 +01:00
await tornado.deposit(commitment, { value, from: sender }).should.be.fulfilled
const error = await tornado.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,
2019-09-06 22:54:37 +02:00
relayer: operator,
2019-11-07 08:04:29 +01:00
recipient,
2019-07-15 19:21:58 +02:00
fee,
refund,
2019-07-15 19:21:58 +02:00
secret: deposit.secret,
pathElements: path_elements,
2019-11-02 03:05:25 +01:00
pathIndices: path_index,
2019-07-15 19:21:58 +02:00
})
2019-10-04 17:20:20 +02:00
let proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
const originalProof = JSON.parse(JSON.stringify(proofData))
let result = snarkVerify(proofData)
2019-07-15 19:21:58 +02:00
result.should.be.equal(true)
// nullifier
2021-02-11 07:23:18 +01:00
proofData.publicSignals[1] =
'133792158246920651341275668520530514036799294649489851421007411546007850802'
2019-10-04 17:20:20 +02:00
result = snarkVerify(proofData)
2019-07-15 19:21:58 +02:00
result.should.be.equal(false)
2019-10-04 17:20:20 +02:00
proofData = originalProof
2019-07-15 19:21:58 +02:00
// try to cheat with recipient
2019-10-04 17:20:20 +02:00
proofData.publicSignals[2] = '133738360804642228759657445999390850076318544422'
result = snarkVerify(proofData)
2019-07-15 19:21:58 +02:00
result.should.be.equal(false)
2019-10-04 17:20:20 +02:00
proofData = originalProof
2019-07-15 19:21:58 +02:00
// fee
2019-10-04 17:20:20 +02:00
proofData.publicSignals[3] = '1337100000000000000000'
result = snarkVerify(proofData)
2019-07-15 19:21:58 +02:00
result.should.be.equal(false)
2019-10-04 17:20:20 +02:00
proofData = originalProof
2019-07-15 19:21:58 +02:00
})
})
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
2019-12-13 14:49:19 +01:00
// let gas = await tornado.deposit.estimateGas(toBN(deposit.commitment.toString()), { value, from: user, gasPrice: '0' })
2019-08-01 11:44:20 +02:00
// console.log('deposit gas:', gas)
2019-12-13 14:49:19 +01:00
await tornado.deposit(toFixedHex(deposit.commitment), { 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-09-06 22:54:37 +02:00
relayer: operator,
2019-11-07 08:04:29 +01:00
recipient,
2019-07-15 12:42:59 +02:00
fee,
refund,
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,
2019-11-02 03:05:25 +01:00
pathIndices: path_index,
2019-07-12 21:11:37 +02:00
})
2019-10-04 17:20:20 +02:00
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
2019-11-04 20:42:41 +01:00
const { proof } = websnarkUtils.toSolidityInput(proofData)
2019-07-12 23:50:26 +02:00
2019-12-13 14:49:19 +01:00
const balanceTornadoBefore = await web3.eth.getBalance(tornado.address)
2019-07-15 12:42:59 +02:00
const balanceRelayerBefore = await web3.eth.getBalance(relayer)
2019-08-02 19:12:30 +02:00
const balanceOperatorBefore = await web3.eth.getBalance(operator)
2019-11-28 05:52:17 +01:00
const balanceRecieverBefore = await web3.eth.getBalance(toFixedHex(recipient, 20))
2019-12-13 14:49:19 +01:00
let isSpent = await tornado.isSpent(toFixedHex(input.nullifierHash))
2019-07-25 15:58:21 +02:00
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
2019-12-13 14:49:19 +01:00
// gas = await tornado.withdraw.estimateGas(proof, publicSignals, { from: relayer, gasPrice: '0' })
2019-08-01 11:44:20 +02:00
// console.log('withdraw gas:', gas)
2019-11-04 20:42:41 +01:00
const args = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
2019-11-07 08:04:29 +01:00
toFixedHex(input.recipient, 20),
2019-11-04 20:42:41 +01:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 07:23:18 +01:00
toFixedHex(input.refund),
2019-11-04 20:42:41 +01:00
]
2019-12-13 14:49:19 +01:00
const { logs } = await tornado.withdraw(proof, ...args, { from: relayer, gasPrice: '0' })
2019-07-15 12:42:59 +02:00
2019-12-13 14:49:19 +01:00
const balanceTornadoAfter = await web3.eth.getBalance(tornado.address)
2019-07-15 12:42:59 +02:00
const balanceRelayerAfter = await web3.eth.getBalance(relayer)
2019-08-02 19:12:30 +02:00
const balanceOperatorAfter = await web3.eth.getBalance(operator)
2019-11-28 05:52:17 +01:00
const balanceRecieverAfter = await web3.eth.getBalance(toFixedHex(recipient, 20))
2019-07-15 17:04:48 +02:00
const feeBN = toBN(fee.toString())
2019-12-13 14:49:19 +01:00
balanceTornadoAfter.should.be.eq.BN(toBN(balanceTornadoBefore).sub(toBN(value)))
2019-08-02 19:12:30 +02:00
balanceRelayerAfter.should.be.eq.BN(toBN(balanceRelayerBefore))
balanceOperatorAfter.should.be.eq.BN(toBN(balanceOperatorBefore).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-11-01 02:12:32 +01:00
logs[0].event.should.be.equal('Withdrawal')
2019-11-04 22:04:22 +01:00
logs[0].args.nullifierHash.should.be.equal(toFixedHex(input.nullifierHash))
2019-09-06 22:54:37 +02:00
logs[0].args.relayer.should.be.eq.BN(operator)
2019-07-15 17:04:48 +02:00
logs[0].args.fee.should.be.eq.BN(feeBN)
2019-12-13 14:49:19 +01:00
isSpent = await tornado.isSpent(toFixedHex(input.nullifierHash))
2019-07-25 15:58:21 +02:00
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-12-13 14:49:19 +01:00
await tornado.deposit(toFixedHex(deposit.commitment), { 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,
2019-09-06 22:54:37 +02:00
relayer: operator,
2019-11-07 08:04:29 +01:00
recipient,
2019-07-15 17:04:48 +02:00
fee,
refund,
2019-07-15 17:04:48 +02:00
secret: deposit.secret,
pathElements: path_elements,
2019-11-02 03:05:25 +01:00
pathIndices: path_index,
2019-07-15 17:04:48 +02:00
})
2019-10-04 17:20:20 +02:00
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
2019-11-04 20:42:41 +01:00
const { proof } = websnarkUtils.toSolidityInput(proofData)
const args = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
2019-11-07 08:04:29 +01:00
toFixedHex(input.recipient, 20),
2019-11-04 20:42:41 +01:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 07:23:18 +01:00
toFixedHex(input.refund),
2019-11-04 20:42:41 +01:00
]
2019-12-13 14:49:19 +01:00
await tornado.withdraw(proof, ...args, { from: relayer }).should.be.fulfilled
const error = await tornado.withdraw(proof, ...args, { from: relayer }).should.be.rejected
2019-07-15 17:04:48 +02:00
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)
2019-12-13 14:49:19 +01:00
await tornado.deposit(toFixedHex(deposit.commitment), { value, from: sender })
2019-08-01 09:33:12 +02:00
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,
2019-09-06 22:54:37 +02:00
relayer: operator,
2019-11-07 08:04:29 +01:00
recipient,
2019-08-01 09:33:12 +02:00
fee,
refund,
2019-08-01 09:33:12 +02:00
secret: deposit.secret,
pathElements: path_elements,
2019-11-02 03:05:25 +01:00
pathIndices: path_index,
2019-08-01 09:33:12 +02:00
})
2019-10-04 17:20:20 +02:00
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
2019-11-04 20:42:41 +01:00
const { proof } = websnarkUtils.toSolidityInput(proofData)
const args = [
toFixedHex(input.root),
2021-02-11 07:23:18 +01:00
toFixedHex(
toBN(input.nullifierHash).add(
toBN('21888242871839275222246405745257275088548364400416034343698204186575808495617'),
),
),
2019-11-07 08:04:29 +01:00
toFixedHex(input.recipient, 20),
2019-11-04 20:42:41 +01:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 07:23:18 +01:00
toFixedHex(input.refund),
2019-11-04 20:42:41 +01:00
]
2019-12-13 14:49:19 +01:00
const error = await tornado.withdraw(proof, ...args, { from: relayer }).should.be.rejected
2021-02-11 08:00:53 +01:00
error.reason.should.be.equal('verifier-input-gte-snark-scalar-field')
2019-08-01 09:33:12 +02:00
})
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-12-13 14:49:19 +01:00
await tornado.deposit(toFixedHex(deposit.commitment), { 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-11-15 20:42:59 +01:00
const largeFee = bigInt(value).add(bigInt(1))
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,
2019-09-06 22:54:37 +02:00
relayer: operator,
2019-11-07 08:04:29 +01:00
recipient,
2019-11-15 20:42:59 +01:00
fee: largeFee,
refund,
2019-07-15 17:04:48 +02:00
secret: deposit.secret,
pathElements: path_elements,
2019-11-02 03:05:25 +01:00
pathIndices: path_index,
2019-07-15 17:04:48 +02:00
})
2019-10-04 17:20:20 +02:00
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
2019-11-04 20:42:41 +01:00
const { proof } = websnarkUtils.toSolidityInput(proofData)
const args = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
2019-11-07 08:04:29 +01:00
toFixedHex(input.recipient, 20),
2019-11-04 20:42:41 +01:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 07:23:18 +01:00
toFixedHex(input.refund),
2019-11-04 20:42:41 +01:00
]
2019-12-13 14:49:19 +01:00
const error = await tornado.withdraw(proof, ...args, { from: relayer }).should.be.rejected
2019-07-15 17:04:48 +02:00
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-12-13 14:49:19 +01:00
await tornado.deposit(toFixedHex(deposit.commitment), { 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,
2019-09-06 22:54:37 +02:00
relayer: operator,
2019-11-07 08:04:29 +01:00
recipient,
2019-07-15 17:04:48 +02:00
fee,
refund,
2019-07-15 17:04:48 +02:00
secret: deposit.secret,
pathElements: path_elements,
2019-11-02 03:05:25 +01:00
pathIndices: path_index,
2019-07-15 17:04:48 +02:00
})
2019-10-04 17:20:20 +02:00
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
2019-11-04 20:42:41 +01:00
const { proof } = websnarkUtils.toSolidityInput(proofData)
const args = [
toFixedHex(randomHex(32)),
toFixedHex(input.nullifierHash),
2019-11-07 08:04:29 +01:00
toFixedHex(input.recipient, 20),
2019-11-04 20:42:41 +01:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 07:23:18 +01:00
toFixedHex(input.refund),
2019-11-04 20:42:41 +01:00
]
2019-12-13 14:49:19 +01:00
const error = await tornado.withdraw(proof, ...args, { from: relayer }).should.be.rejected
2019-07-15 17:04:48 +02:00
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-12-13 14:49:19 +01:00
await tornado.deposit(toFixedHex(deposit.commitment), { 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,
2019-09-06 22:54:37 +02:00
relayer: operator,
2019-11-07 08:04:29 +01:00
recipient,
2019-07-15 19:21:58 +02:00
fee,
refund,
2019-07-15 19:21:58 +02:00
secret: deposit.secret,
pathElements: path_elements,
2019-11-02 03:05:25 +01:00
pathIndices: path_index,
2019-07-15 19:21:58 +02:00
})
2019-10-04 17:20:20 +02:00
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
2019-11-04 20:42:41 +01:00
let { proof } = websnarkUtils.toSolidityInput(proofData)
const args = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
2019-11-07 08:04:29 +01:00
toFixedHex(input.recipient, 20),
2019-11-04 20:42:41 +01:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 07:23:18 +01:00
toFixedHex(input.refund),
2019-11-04 20:42:41 +01:00
]
let incorrectArgs
2019-10-04 17:20:20 +02:00
const originalProof = proof.slice()
2019-07-15 19:21:58 +02:00
2019-11-07 08:04:29 +01:00
// recipient
2019-11-04 20:42:41 +01:00
incorrectArgs = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
toFixedHex('0x0000000000000000000000007a1f9131357404ef86d7c38dbffed2da70321337', 20),
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 07:23:18 +01:00
toFixedHex(input.refund),
2019-11-04 20:42:41 +01:00
]
2019-12-13 14:49:19 +01:00
let error = await tornado.withdraw(proof, ...incorrectArgs, { 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
2019-11-04 20:42:41 +01:00
incorrectArgs = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
2019-11-07 08:04:29 +01:00
toFixedHex(input.recipient, 20),
2019-11-04 20:42:41 +01:00
toFixedHex(input.relayer, 20),
toFixedHex('0x000000000000000000000000000000000000000000000000015345785d8a0000'),
2021-02-11 07:23:18 +01:00
toFixedHex(input.refund),
2019-11-04 20:42:41 +01:00
]
2019-12-13 14:49:19 +01:00
error = await tornado.withdraw(proof, ...incorrectArgs, { 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
2019-11-04 20:42:41 +01:00
incorrectArgs = [
toFixedHex(input.root),
toFixedHex('0x00abdfc78211f8807b9c6504a6e537e71b8788b2f529a95f1399ce124a8642ad'),
2019-11-07 08:04:29 +01:00
toFixedHex(input.recipient, 20),
2019-11-04 20:42:41 +01:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 07:23:18 +01:00
toFixedHex(input.refund),
2019-11-04 20:42:41 +01:00
]
2019-12-13 14:49:19 +01:00
error = await tornado.withdraw(proof, ...incorrectArgs, { 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
2019-11-04 20:42:41 +01:00
proof = '0xbeef' + proof.substr(6)
2019-12-13 14:49:19 +01:00
await tornado.withdraw(proof, ...args, { from: relayer }).should.be.rejected
2019-07-15 19:21:58 +02:00
// should work with original values
2019-12-13 14:49:19 +01:00
await tornado.withdraw(originalProof, ...args, { from: relayer }).should.be.fulfilled
2019-07-15 19:21:58 +02:00
})
2019-10-17 17:54:21 +02:00
it('should reject with non zero refund', async () => {
const deposit = generateDeposit()
await tree.insert(deposit.commitment)
2019-12-13 14:49:19 +01:00
await tornado.deposit(toFixedHex(deposit.commitment), { value, from: sender })
2019-10-17 17:54:21 +02:00
const { root, path_elements, path_index } = await tree.path(0)
const input = stringifyBigInts({
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
root,
nullifier: deposit.nullifier,
relayer: operator,
2019-11-07 08:04:29 +01:00
recipient,
2019-10-17 17:54:21 +02:00
fee,
refund: bigInt(1),
secret: deposit.secret,
pathElements: path_elements,
2019-11-02 03:05:25 +01:00
pathIndices: path_index,
2019-10-17 17:54:21 +02:00
})
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
2019-11-04 20:42:41 +01:00
const { proof } = websnarkUtils.toSolidityInput(proofData)
const args = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
2019-11-07 08:04:29 +01:00
toFixedHex(input.recipient, 20),
2019-11-04 20:42:41 +01:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 07:23:18 +01:00
toFixedHex(input.refund),
2019-11-04 20:42:41 +01:00
]
2019-12-13 14:49:19 +01:00
const error = await tornado.withdraw(proof, ...args, { from: relayer }).should.be.rejected
error.reason.should.be.equal('Refund value is supposed to be zero for ETH instance')
2019-10-17 17:54:21 +02:00
})
2019-07-12 21:11:37 +02:00
})
2019-11-28 04:45:52 +01:00
describe('#isSpent', () => {
it('should work', async () => {
const deposit1 = generateDeposit()
const deposit2 = generateDeposit()
await tree.insert(deposit1.commitment)
await tree.insert(deposit2.commitment)
2019-12-13 14:49:19 +01:00
await tornado.deposit(toFixedHex(deposit1.commitment), { value, gasPrice: '0' })
await tornado.deposit(toFixedHex(deposit2.commitment), { value, gasPrice: '0' })
2019-11-28 04:45:52 +01:00
const { root, path_elements, path_index } = await tree.path(1)
// Circuit input
const input = stringifyBigInts({
// public
root,
nullifierHash: pedersenHash(deposit2.nullifier.leInt2Buff(31)),
relayer: operator,
recipient,
fee,
refund,
// private
nullifier: deposit2.nullifier,
secret: deposit2.secret,
pathElements: path_elements,
pathIndices: path_index,
})
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
const { proof } = websnarkUtils.toSolidityInput(proofData)
const args = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
toFixedHex(input.recipient, 20),
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 07:23:18 +01:00
toFixedHex(input.refund),
2019-11-28 04:45:52 +01:00
]
2019-12-13 14:49:19 +01:00
await tornado.withdraw(proof, ...args, { from: relayer, gasPrice: '0' })
2019-11-28 04:45:52 +01:00
const nullifierHash1 = toFixedHex(pedersenHash(deposit1.nullifier.leInt2Buff(31)))
const nullifierHash2 = toFixedHex(pedersenHash(deposit2.nullifier.leInt2Buff(31)))
2019-12-13 14:49:19 +01:00
const spentArray = await tornado.isSpentArray([nullifierHash1, nullifierHash2])
2019-11-28 04:45:52 +01:00
spentArray.should.be.deep.equal([false, true])
})
})
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()
2021-02-11 07:23:18 +01:00
tree = new MerkleTree(levels, null, prefix)
2019-07-12 17:04:45 +02:00
})
})