tornado-core/test/ETHTornado.test.js

541 lines
20 KiB
JavaScript
Raw Permalink Normal View History

2019-07-16 16:23:12 +00:00
/* global artifacts, web3, contract */
2021-02-11 06:23:18 +00:00
require('chai').use(require('bn-chai')(web3.utils.BN)).use(require('chai-as-promised')).should()
2019-07-15 17:21:58 +00:00
const fs = require('fs')
2019-07-12 15:04:45 +00:00
2019-11-28 08:04:09 +00:00
const { toBN, randomHex } = require('web3-utils')
2020-08-01 02:28:14 +00:00
const { takeSnapshot, revertSnapshot } = require('../scripts/ganacheHelper')
2019-07-12 15:04:45 +00:00
2019-12-13 13:49:19 +00:00
const Tornado = artifacts.require('./ETHTornado.sol')
2019-11-02 12:35:22 +00:00
const { ETH_AMOUNT, MERKLE_TREE_HEIGHT } = process.env
2019-07-12 15:04:45 +00:00
2019-07-15 17:21:58 +00:00
const websnarkUtils = require('websnark/src/utils')
2019-07-16 16:23:12 +00:00
const buildGroth16 = require('websnark/src/groth16')
2019-07-15 15:04:48 +00:00
const stringifyBigInts = require('websnark/tools/stringifybigint').stringifyBigInts
2019-07-19 12:06:06 +00:00
const unstringifyBigInts2 = require('snarkjs/src/stringifybigint').unstringifyBigInts
2019-07-16 16:23:12 +00:00
const snarkjs = require('snarkjs')
const bigInt = snarkjs.bigInt
2019-07-18 18:27:51 +00:00
const crypto = require('crypto')
const circomlib = require('circomlib')
2020-07-31 17:38:50 +00:00
const MerkleTree = require('fixed-merkle-tree')
2019-07-12 19:11:37 +00:00
2019-07-18 18:27:51 +00:00
const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes))
const pedersenHash = (data) => circomlib.babyJub.unpackPoint(circomlib.pedersenHash.hash(data))[0]
2021-02-11 06:23:18 +00:00
const toFixedHex = (number, length = 32) =>
'0x' +
bigInt(number)
.toString(16)
.padStart(length * 2, '0')
2019-11-28 04:52:17 +00:00
const getRandomRecipient = () => rbigint(20)
2019-07-18 18:27:51 +00:00
2019-07-12 19:11:37 +00:00
function generateDeposit() {
let deposit = {
2019-07-18 18:27:51 +00:00
secret: rbigint(31),
nullifier: rbigint(31),
2019-07-16 16:23:12 +00:00
}
2019-08-01 14:49:34 +00:00
const preimage = Buffer.concat([deposit.nullifier.leInt2Buff(31), deposit.secret.leInt2Buff(31)])
2019-07-18 18:27:51 +00:00
deposit.commitment = pedersenHash(preimage)
2019-07-16 16:23:12 +00:00
return deposit
2019-07-12 19:11:37 +00:00
}
2019-07-12 15:04:45 +00:00
2019-07-16 16:23:12 +00:00
// eslint-disable-next-line no-unused-vars
2019-07-15 15:04:48 +00:00
function BNArrayToStringArray(array) {
const arrayToPrint = []
2021-02-11 06:23:18 +00:00
array.forEach((item) => {
2019-07-15 15:04:48 +00:00
arrayToPrint.push(item.toString())
})
return arrayToPrint
}
2019-07-18 18:27:51 +00:00
function snarkVerify(proof) {
2019-10-04 15:20:20 +00:00
proof = unstringifyBigInts2(proof)
2019-07-19 12:06:06 +00:00
const verification_key = unstringifyBigInts2(require('../build/circuits/withdraw_verification_key.json'))
2019-07-18 18:27:51 +00:00
return snarkjs['groth'].isValid(verification_key, proof, proof.publicSignals)
}
2021-02-11 06:23:18 +00:00
contract('ETHTornado', (accounts) => {
2019-12-13 13:49:19 +00:00
let tornado
2019-07-12 15:04:45 +00:00
const sender = accounts[0]
2019-08-02 17:12:30 +00:00
const operator = accounts[0]
2019-07-16 09:50:52 +00:00
const levels = MERKLE_TREE_HEIGHT || 16
2019-08-27 20:42:24 +00:00
const value = ETH_AMOUNT || '1000000000000000000' // 1 ether
2019-07-12 15:04:45 +00:00
let snapshotId
let tree
2019-08-27 20:42:24 +00:00
const fee = bigInt(ETH_AMOUNT).shr(1) || bigInt(1e17)
const refund = bigInt(0)
2019-11-07 07:04:29 +00:00
const recipient = getRandomRecipient()
2019-07-15 15:04:48 +00:00
const relayer = accounts[1]
2019-07-15 17:21:58 +00:00
let groth16
let circuit
let proving_key
2019-07-12 15:04:45 +00:00
before(async () => {
2020-07-31 17:38:50 +00:00
tree = new MerkleTree(levels)
2019-12-13 13:49:19 +00:00
tornado = await Tornado.deployed()
2019-07-12 15:04:45 +00:00
snapshotId = await takeSnapshot()
2019-07-15 17:21:58 +00:00
groth16 = await buildGroth16()
2019-07-16 16:23:12 +00:00
circuit = require('../build/circuits/withdraw.json')
proving_key = fs.readFileSync('build/circuits/withdraw_proving_key.bin').buffer
2019-07-12 15:04:45 +00:00
})
2019-07-16 16:23:12 +00:00
describe('#constructor', () => {
2019-07-12 15:04:45 +00:00
it('should initialize', async () => {
2019-12-13 13:49:19 +00:00
const etherDenomination = await tornado.denomination()
2019-08-27 20:42:24 +00:00
etherDenomination.should.be.eq.BN(toBN(value))
2019-07-12 15:04:45 +00:00
})
})
2019-07-16 16:23:12 +00:00
describe('#deposit', () => {
2019-07-12 19:11:37 +00:00
it('should emit event', async () => {
2019-11-04 21:04:22 +00:00
let commitment = toFixedHex(42)
2019-12-13 13:49:19 +00:00
let { logs } = await tornado.deposit(commitment, { value, from: sender })
2019-07-19 17:08:47 +00:00
logs[0].event.should.be.equal('Deposit')
2019-11-04 21:04:22 +00:00
logs[0].args.commitment.should.be.equal(commitment)
logs[0].args.leafIndex.should.be.eq.BN(0)
2019-07-19 17:08:47 +00:00
2021-02-11 06:23:18 +00:00
commitment = toFixedHex(12)
;({ logs } = await tornado.deposit(commitment, { value, from: accounts[2] }))
2019-07-19 17:08:47 +00:00
logs[0].event.should.be.equal('Deposit')
2019-11-04 21:04:22 +00:00
logs[0].args.commitment.should.be.equal(commitment)
logs[0].args.leafIndex.should.be.eq.BN(1)
2019-07-12 19:11:37 +00:00
})
2019-07-15 15:04:48 +00:00
it('should throw if there is a such commitment', async () => {
2019-11-04 21:04:22 +00:00
const commitment = toFixedHex(42)
2019-12-13 13:49:19 +00: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 15:04:48 +00:00
error.reason.should.be.equal('The commitment has been submitted')
})
2019-07-12 19:11:37 +00:00
})
2019-07-16 16:23:12 +00:00
describe('snark proof verification on js side', () => {
2019-07-15 17:21:58 +00:00
it('should detect tampering', async () => {
const deposit = generateDeposit()
2020-07-31 17:38:50 +00:00
tree.insert(deposit.commitment)
const { pathElements, pathIndices } = tree.path(0)
2019-07-15 17:21:58 +00:00
const input = stringifyBigInts({
2020-07-31 17:38:50 +00:00
root: tree.root(),
2019-08-01 14:49:34 +00:00
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
2019-07-15 17:21:58 +00:00
nullifier: deposit.nullifier,
2019-09-06 20:54:37 +00:00
relayer: operator,
2019-11-07 07:04:29 +00:00
recipient,
2019-07-15 17:21:58 +00:00
fee,
refund,
2019-07-15 17:21:58 +00:00
secret: deposit.secret,
2020-07-31 17:38:50 +00:00
pathElements: pathElements,
pathIndices: pathIndices,
2019-07-15 17:21:58 +00:00
})
2019-10-04 15:20:20 +00: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 17:21:58 +00:00
result.should.be.equal(true)
// nullifier
2021-02-11 06:23:18 +00:00
proofData.publicSignals[1] =
'133792158246920651341275668520530514036799294649489851421007411546007850802'
2019-10-04 15:20:20 +00:00
result = snarkVerify(proofData)
2019-07-15 17:21:58 +00:00
result.should.be.equal(false)
2019-10-04 15:20:20 +00:00
proofData = originalProof
2019-07-15 17:21:58 +00:00
// try to cheat with recipient
2019-10-04 15:20:20 +00:00
proofData.publicSignals[2] = '133738360804642228759657445999390850076318544422'
result = snarkVerify(proofData)
2019-07-15 17:21:58 +00:00
result.should.be.equal(false)
2019-10-04 15:20:20 +00:00
proofData = originalProof
2019-07-15 17:21:58 +00:00
// fee
2019-10-04 15:20:20 +00:00
proofData.publicSignals[3] = '1337100000000000000000'
result = snarkVerify(proofData)
2019-07-15 17:21:58 +00:00
result.should.be.equal(false)
2019-10-04 15:20:20 +00:00
proofData = originalProof
2019-07-15 17:21:58 +00:00
})
})
2019-07-16 16:23:12 +00:00
describe('#withdraw', () => {
2019-07-12 21:50:26 +00:00
it('should work', async () => {
2019-07-12 19:11:37 +00:00
const deposit = generateDeposit()
2019-07-15 10:42:59 +00:00
const user = accounts[4]
2020-07-31 17:38:50 +00:00
tree.insert(deposit.commitment)
2019-07-15 10:42:59 +00:00
const balanceUserBefore = await web3.eth.getBalance(user)
2019-08-01 09:44:20 +00:00
// Uncomment to measure gas usage
2019-12-13 13:49:19 +00:00
// let gas = await tornado.deposit.estimateGas(toBN(deposit.commitment.toString()), { value, from: user, gasPrice: '0' })
2019-08-01 09:44:20 +00:00
// console.log('deposit gas:', gas)
2019-12-13 13:49:19 +00:00
await tornado.deposit(toFixedHex(deposit.commitment), { value, from: user, gasPrice: '0' })
2019-07-15 10:42:59 +00:00
const balanceUserAfter = await web3.eth.getBalance(user)
2019-07-16 09:50:52 +00:00
balanceUserAfter.should.be.eq.BN(toBN(balanceUserBefore).sub(toBN(value)))
2019-07-12 19:11:37 +00:00
2020-07-31 17:38:50 +00:00
const { pathElements, pathIndices } = tree.path(0)
2019-07-12 19:11:37 +00:00
// Circuit input
const input = stringifyBigInts({
// public
2020-07-31 17:38:50 +00:00
root: tree.root(),
2019-08-01 14:49:34 +00:00
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
2019-09-06 20:54:37 +00:00
relayer: operator,
2019-11-07 07:04:29 +00:00
recipient,
2019-07-15 10:42:59 +00:00
fee,
refund,
2019-07-12 19:11:37 +00:00
// private
2019-07-23 20:00:45 +00:00
nullifier: deposit.nullifier,
2019-07-12 19:11:37 +00:00
secret: deposit.secret,
2020-07-31 17:38:50 +00:00
pathElements: pathElements,
pathIndices: pathIndices,
2019-07-12 19:11:37 +00:00
})
2019-10-04 15:20:20 +00:00
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
2019-11-04 19:42:41 +00:00
const { proof } = websnarkUtils.toSolidityInput(proofData)
2019-07-12 21:50:26 +00:00
2019-12-13 13:49:19 +00:00
const balanceTornadoBefore = await web3.eth.getBalance(tornado.address)
2019-07-15 10:42:59 +00:00
const balanceRelayerBefore = await web3.eth.getBalance(relayer)
2019-08-02 17:12:30 +00:00
const balanceOperatorBefore = await web3.eth.getBalance(operator)
2021-10-31 14:07:39 +00:00
const balanceReceiverBefore = await web3.eth.getBalance(toFixedHex(recipient, 20))
2019-12-13 13:49:19 +00:00
let isSpent = await tornado.isSpent(toFixedHex(input.nullifierHash))
2019-07-25 13:58:21 +00:00
isSpent.should.be.equal(false)
2019-07-15 10:42:59 +00:00
2019-08-01 09:44:20 +00:00
// Uncomment to measure gas usage
2019-12-13 13:49:19 +00:00
// gas = await tornado.withdraw.estimateGas(proof, publicSignals, { from: relayer, gasPrice: '0' })
2019-08-01 09:44:20 +00:00
// console.log('withdraw gas:', gas)
2019-11-04 19:42:41 +00:00
const args = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
2019-11-07 07:04:29 +00:00
toFixedHex(input.recipient, 20),
2019-11-04 19:42:41 +00:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 06:23:18 +00:00
toFixedHex(input.refund),
2019-11-04 19:42:41 +00:00
]
2019-12-13 13:49:19 +00:00
const { logs } = await tornado.withdraw(proof, ...args, { from: relayer, gasPrice: '0' })
2019-07-15 10:42:59 +00:00
2019-12-13 13:49:19 +00:00
const balanceTornadoAfter = await web3.eth.getBalance(tornado.address)
2019-07-15 10:42:59 +00:00
const balanceRelayerAfter = await web3.eth.getBalance(relayer)
2019-08-02 17:12:30 +00:00
const balanceOperatorAfter = await web3.eth.getBalance(operator)
2021-10-31 14:07:39 +00:00
const balanceReceiverAfter = await web3.eth.getBalance(toFixedHex(recipient, 20))
2019-07-15 15:04:48 +00:00
const feeBN = toBN(fee.toString())
2019-12-13 13:49:19 +00:00
balanceTornadoAfter.should.be.eq.BN(toBN(balanceTornadoBefore).sub(toBN(value)))
2019-08-02 17:12:30 +00:00
balanceRelayerAfter.should.be.eq.BN(toBN(balanceRelayerBefore))
balanceOperatorAfter.should.be.eq.BN(toBN(balanceOperatorBefore).add(feeBN))
2021-10-31 14:07:39 +00:00
balanceReceiverAfter.should.be.eq.BN(toBN(balanceReceiverBefore).add(toBN(value)).sub(feeBN))
2019-07-15 10:42:59 +00:00
2019-11-01 01:12:32 +00:00
logs[0].event.should.be.equal('Withdrawal')
2019-11-04 21:04:22 +00:00
logs[0].args.nullifierHash.should.be.equal(toFixedHex(input.nullifierHash))
2019-09-06 20:54:37 +00:00
logs[0].args.relayer.should.be.eq.BN(operator)
2019-07-15 15:04:48 +00:00
logs[0].args.fee.should.be.eq.BN(feeBN)
2019-12-13 13:49:19 +00:00
isSpent = await tornado.isSpent(toFixedHex(input.nullifierHash))
2019-07-25 13:58:21 +00:00
isSpent.should.be.equal(true)
2019-07-15 15:04:48 +00:00
})
it('should prevent double spend', async () => {
const deposit = generateDeposit()
2020-07-31 17:38:50 +00:00
tree.insert(deposit.commitment)
2019-12-13 13:49:19 +00:00
await tornado.deposit(toFixedHex(deposit.commitment), { value, from: sender })
2019-07-15 15:04:48 +00:00
2020-07-31 17:38:50 +00:00
const { pathElements, pathIndices } = tree.path(0)
2019-07-15 15:04:48 +00:00
const input = stringifyBigInts({
2020-07-31 17:38:50 +00:00
root: tree.root(),
2019-08-01 14:49:34 +00:00
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
2019-07-15 15:04:48 +00:00
nullifier: deposit.nullifier,
2019-09-06 20:54:37 +00:00
relayer: operator,
2019-11-07 07:04:29 +00:00
recipient,
2019-07-15 15:04:48 +00:00
fee,
refund,
2019-07-15 15:04:48 +00:00
secret: deposit.secret,
2020-07-31 17:38:50 +00:00
pathElements: pathElements,
pathIndices: pathIndices,
2019-07-15 15:04:48 +00:00
})
2019-10-04 15:20:20 +00:00
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
2019-11-04 19:42:41 +00:00
const { proof } = websnarkUtils.toSolidityInput(proofData)
const args = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
2019-11-07 07:04:29 +00:00
toFixedHex(input.recipient, 20),
2019-11-04 19:42:41 +00:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 06:23:18 +00:00
toFixedHex(input.refund),
2019-11-04 19:42:41 +00:00
]
2019-12-13 13:49:19 +00: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 15:04:48 +00:00
error.reason.should.be.equal('The note has been already spent')
})
2019-08-01 07:33:12 +00:00
it('should prevent double spend with overflow', async () => {
const deposit = generateDeposit()
2020-07-31 17:38:50 +00:00
tree.insert(deposit.commitment)
2019-12-13 13:49:19 +00:00
await tornado.deposit(toFixedHex(deposit.commitment), { value, from: sender })
2019-08-01 07:33:12 +00:00
2020-07-31 17:38:50 +00:00
const { pathElements, pathIndices } = tree.path(0)
2019-08-01 07:33:12 +00:00
const input = stringifyBigInts({
2020-07-31 17:38:50 +00:00
root: tree.root(),
2019-08-01 14:49:34 +00:00
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
2019-08-01 07:33:12 +00:00
nullifier: deposit.nullifier,
2019-09-06 20:54:37 +00:00
relayer: operator,
2019-11-07 07:04:29 +00:00
recipient,
2019-08-01 07:33:12 +00:00
fee,
refund,
2019-08-01 07:33:12 +00:00
secret: deposit.secret,
2020-07-31 17:38:50 +00:00
pathElements: pathElements,
pathIndices: pathIndices,
2019-08-01 07:33:12 +00:00
})
2019-10-04 15:20:20 +00:00
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
2019-11-04 19:42:41 +00:00
const { proof } = websnarkUtils.toSolidityInput(proofData)
const args = [
toFixedHex(input.root),
2021-02-11 06:23:18 +00:00
toFixedHex(
toBN(input.nullifierHash).add(
toBN('21888242871839275222246405745257275088548364400416034343698204186575808495617'),
),
),
2019-11-07 07:04:29 +00:00
toFixedHex(input.recipient, 20),
2019-11-04 19:42:41 +00:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 06:23:18 +00:00
toFixedHex(input.refund),
2019-11-04 19:42:41 +00:00
]
2019-12-13 13:49:19 +00:00
const error = await tornado.withdraw(proof, ...args, { from: relayer }).should.be.rejected
2021-03-10 16:37:17 +00:00
error.reason.should.be.equal('verifier-gte-snark-scalar-field')
2019-08-01 07:33:12 +00:00
})
2019-07-15 15:04:48 +00:00
it('fee should be less or equal transfer value', async () => {
const deposit = generateDeposit()
2020-07-31 17:38:50 +00:00
tree.insert(deposit.commitment)
2019-12-13 13:49:19 +00:00
await tornado.deposit(toFixedHex(deposit.commitment), { value, from: sender })
2019-07-15 15:04:48 +00:00
2020-07-31 17:38:50 +00:00
const { pathElements, pathIndices } = tree.path(0)
2019-11-15 19:42:59 +00:00
const largeFee = bigInt(value).add(bigInt(1))
2019-07-15 15:04:48 +00:00
const input = stringifyBigInts({
2020-07-31 17:38:50 +00:00
root: tree.root(),
2019-08-01 14:49:34 +00:00
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
2019-07-15 15:04:48 +00:00
nullifier: deposit.nullifier,
2019-09-06 20:54:37 +00:00
relayer: operator,
2019-11-07 07:04:29 +00:00
recipient,
2019-11-15 19:42:59 +00:00
fee: largeFee,
refund,
2019-07-15 15:04:48 +00:00
secret: deposit.secret,
2020-07-31 17:38:50 +00:00
pathElements: pathElements,
pathIndices: pathIndices,
2019-07-15 15:04:48 +00:00
})
2019-10-04 15:20:20 +00:00
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
2019-11-04 19:42:41 +00:00
const { proof } = websnarkUtils.toSolidityInput(proofData)
const args = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
2019-11-07 07:04:29 +00:00
toFixedHex(input.recipient, 20),
2019-11-04 19:42:41 +00:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 06:23:18 +00:00
toFixedHex(input.refund),
2019-11-04 19:42:41 +00:00
]
2019-12-13 13:49:19 +00:00
const error = await tornado.withdraw(proof, ...args, { from: relayer }).should.be.rejected
2019-07-15 15:04:48 +00:00
error.reason.should.be.equal('Fee exceeds transfer value')
})
it('should throw for corrupted merkle tree root', async () => {
const deposit = generateDeposit()
2020-07-31 17:38:50 +00:00
tree.insert(deposit.commitment)
2019-12-13 13:49:19 +00:00
await tornado.deposit(toFixedHex(deposit.commitment), { value, from: sender })
2019-07-15 15:04:48 +00:00
2020-07-31 17:38:50 +00:00
const { pathElements, pathIndices } = tree.path(0)
2019-07-15 15:04:48 +00:00
const input = stringifyBigInts({
2019-08-01 14:49:34 +00:00
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
2020-07-31 17:38:50 +00:00
root: tree.root(),
2019-07-15 15:04:48 +00:00
nullifier: deposit.nullifier,
2019-09-06 20:54:37 +00:00
relayer: operator,
2019-11-07 07:04:29 +00:00
recipient,
2019-07-15 15:04:48 +00:00
fee,
refund,
2019-07-15 15:04:48 +00:00
secret: deposit.secret,
2020-07-31 17:38:50 +00:00
pathElements: pathElements,
pathIndices: pathIndices,
2019-07-15 15:04:48 +00:00
})
2019-10-04 15:20:20 +00:00
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
2019-11-04 19:42:41 +00:00
const { proof } = websnarkUtils.toSolidityInput(proofData)
const args = [
toFixedHex(randomHex(32)),
toFixedHex(input.nullifierHash),
2019-11-07 07:04:29 +00:00
toFixedHex(input.recipient, 20),
2019-11-04 19:42:41 +00:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 06:23:18 +00:00
toFixedHex(input.refund),
2019-11-04 19:42:41 +00:00
]
2019-12-13 13:49:19 +00:00
const error = await tornado.withdraw(proof, ...args, { from: relayer }).should.be.rejected
2019-07-15 15:04:48 +00:00
error.reason.should.be.equal('Cannot find your merkle root')
2019-07-12 19:11:37 +00:00
})
2019-07-15 17:21:58 +00:00
it('should reject with tampered public inputs', async () => {
const deposit = generateDeposit()
2020-07-31 17:38:50 +00:00
tree.insert(deposit.commitment)
2019-12-13 13:49:19 +00:00
await tornado.deposit(toFixedHex(deposit.commitment), { value, from: sender })
2019-07-15 17:21:58 +00:00
2020-07-31 17:38:50 +00:00
let { pathElements, pathIndices } = tree.path(0)
2019-07-15 17:21:58 +00:00
2019-07-15 18:57:06 +00:00
const input = stringifyBigInts({
2020-07-31 17:38:50 +00:00
root: tree.root(),
2019-08-01 14:49:34 +00:00
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
2019-07-15 17:21:58 +00:00
nullifier: deposit.nullifier,
2019-09-06 20:54:37 +00:00
relayer: operator,
2019-11-07 07:04:29 +00:00
recipient,
2019-07-15 17:21:58 +00:00
fee,
refund,
2019-07-15 17:21:58 +00:00
secret: deposit.secret,
2020-07-31 17:38:50 +00:00
pathElements: pathElements,
pathIndices: pathIndices,
2019-07-15 17:21:58 +00:00
})
2019-10-04 15:20:20 +00:00
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
2019-11-04 19:42:41 +00:00
let { proof } = websnarkUtils.toSolidityInput(proofData)
const args = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
2019-11-07 07:04:29 +00:00
toFixedHex(input.recipient, 20),
2019-11-04 19:42:41 +00:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 06:23:18 +00:00
toFixedHex(input.refund),
2019-11-04 19:42:41 +00:00
]
let incorrectArgs
2019-10-04 15:20:20 +00:00
const originalProof = proof.slice()
2019-07-15 17:21:58 +00:00
2019-11-07 07:04:29 +00:00
// recipient
2019-11-04 19:42:41 +00:00
incorrectArgs = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
toFixedHex('0x0000000000000000000000007a1f9131357404ef86d7c38dbffed2da70321337', 20),
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 06:23:18 +00:00
toFixedHex(input.refund),
2019-11-04 19:42:41 +00:00
]
2019-12-13 13:49:19 +00:00
let error = await tornado.withdraw(proof, ...incorrectArgs, { from: relayer }).should.be.rejected
2019-07-16 16:23:12 +00:00
error.reason.should.be.equal('Invalid withdraw proof')
2019-07-15 17:21:58 +00:00
// fee
2019-11-04 19:42:41 +00:00
incorrectArgs = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
2019-11-07 07:04:29 +00:00
toFixedHex(input.recipient, 20),
2019-11-04 19:42:41 +00:00
toFixedHex(input.relayer, 20),
toFixedHex('0x000000000000000000000000000000000000000000000000015345785d8a0000'),
2021-02-11 06:23:18 +00:00
toFixedHex(input.refund),
2019-11-04 19:42:41 +00:00
]
2019-12-13 13:49:19 +00:00
error = await tornado.withdraw(proof, ...incorrectArgs, { from: relayer }).should.be.rejected
2019-07-16 16:23:12 +00:00
error.reason.should.be.equal('Invalid withdraw proof')
2019-07-15 17:21:58 +00:00
// nullifier
2019-11-04 19:42:41 +00:00
incorrectArgs = [
toFixedHex(input.root),
toFixedHex('0x00abdfc78211f8807b9c6504a6e537e71b8788b2f529a95f1399ce124a8642ad'),
2019-11-07 07:04:29 +00:00
toFixedHex(input.recipient, 20),
2019-11-04 19:42:41 +00:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 06:23:18 +00:00
toFixedHex(input.refund),
2019-11-04 19:42:41 +00:00
]
2019-12-13 13:49:19 +00:00
error = await tornado.withdraw(proof, ...incorrectArgs, { from: relayer }).should.be.rejected
2019-07-16 16:23:12 +00:00
error.reason.should.be.equal('Invalid withdraw proof')
2019-07-15 17:21:58 +00:00
// proof itself
2019-11-04 19:42:41 +00:00
proof = '0xbeef' + proof.substr(6)
2019-12-13 13:49:19 +00:00
await tornado.withdraw(proof, ...args, { from: relayer }).should.be.rejected
2019-07-15 17:21:58 +00:00
// should work with original values
2019-12-13 13:49:19 +00:00
await tornado.withdraw(originalProof, ...args, { from: relayer }).should.be.fulfilled
2019-07-15 17:21:58 +00:00
})
2019-10-17 15:54:21 +00:00
it('should reject with non zero refund', async () => {
const deposit = generateDeposit()
2020-07-31 17:38:50 +00:00
tree.insert(deposit.commitment)
2019-12-13 13:49:19 +00:00
await tornado.deposit(toFixedHex(deposit.commitment), { value, from: sender })
2019-10-17 15:54:21 +00:00
2020-07-31 17:38:50 +00:00
const { pathElements, pathIndices } = tree.path(0)
2019-10-17 15:54:21 +00:00
const input = stringifyBigInts({
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
2020-07-31 17:38:50 +00:00
root: tree.root(),
2019-10-17 15:54:21 +00:00
nullifier: deposit.nullifier,
relayer: operator,
2019-11-07 07:04:29 +00:00
recipient,
2019-10-17 15:54:21 +00:00
fee,
refund: bigInt(1),
secret: deposit.secret,
2020-07-31 17:38:50 +00:00
pathElements: pathElements,
pathIndices: pathIndices,
2019-10-17 15:54:21 +00:00
})
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
2019-11-04 19:42:41 +00:00
const { proof } = websnarkUtils.toSolidityInput(proofData)
const args = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
2019-11-07 07:04:29 +00:00
toFixedHex(input.recipient, 20),
2019-11-04 19:42:41 +00:00
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 06:23:18 +00:00
toFixedHex(input.refund),
2019-11-04 19:42:41 +00:00
]
2019-12-13 13:49:19 +00: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 15:54:21 +00:00
})
2019-07-12 19:11:37 +00:00
})
2019-11-28 03:45:52 +00:00
describe('#isSpent', () => {
it('should work', async () => {
const deposit1 = generateDeposit()
const deposit2 = generateDeposit()
2020-07-31 17:38:50 +00:00
tree.insert(deposit1.commitment)
tree.insert(deposit2.commitment)
2019-12-13 13:49:19 +00:00
await tornado.deposit(toFixedHex(deposit1.commitment), { value, gasPrice: '0' })
await tornado.deposit(toFixedHex(deposit2.commitment), { value, gasPrice: '0' })
2019-11-28 03:45:52 +00:00
2020-07-31 17:38:50 +00:00
const { pathElements, pathIndices } = tree.path(1)
2019-11-28 03:45:52 +00:00
// Circuit input
const input = stringifyBigInts({
// public
2020-07-31 17:38:50 +00:00
root: tree.root(),
2019-11-28 03:45:52 +00:00
nullifierHash: pedersenHash(deposit2.nullifier.leInt2Buff(31)),
relayer: operator,
recipient,
fee,
refund,
// private
nullifier: deposit2.nullifier,
secret: deposit2.secret,
2020-07-31 17:38:50 +00:00
pathElements: pathElements,
pathIndices: pathIndices,
2019-11-28 03:45:52 +00:00
})
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 06:23:18 +00:00
toFixedHex(input.refund),
2019-11-28 03:45:52 +00:00
]
2019-12-13 13:49:19 +00:00
await tornado.withdraw(proof, ...args, { from: relayer, gasPrice: '0' })
2019-11-28 03:45:52 +00:00
const nullifierHash1 = toFixedHex(pedersenHash(deposit1.nullifier.leInt2Buff(31)))
const nullifierHash2 = toFixedHex(pedersenHash(deposit2.nullifier.leInt2Buff(31)))
2019-12-13 13:49:19 +00:00
const spentArray = await tornado.isSpentArray([nullifierHash1, nullifierHash2])
2019-11-28 03:45:52 +00:00
spentArray.should.be.deep.equal([false, true])
})
})
2019-07-12 15:04:45 +00:00
afterEach(async () => {
await revertSnapshot(snapshotId.result)
2019-07-16 16:23:12 +00:00
// eslint-disable-next-line require-atomic-updates
2019-07-12 15:04:45 +00:00
snapshotId = await takeSnapshot()
2020-07-31 17:38:50 +00:00
tree = new MerkleTree(levels)
2019-07-12 15:04:45 +00:00
})
})