mirror of
https://github.com/tornadocash/tornado-core.git
synced 2025-07-22 14:50:38 -04:00
use merkle tree from npm
This commit is contained in:
parent
801f29a4b7
commit
0c6e638852
10 changed files with 396 additions and 420 deletions
|
@ -18,7 +18,7 @@ const snarkjs = require('snarkjs')
|
|||
const bigInt = snarkjs.bigInt
|
||||
const crypto = require('crypto')
|
||||
const circomlib = require('circomlib')
|
||||
const MerkleTree = require('../lib/MerkleTree')
|
||||
const MerkleTree = require('fixed-merkle-tree')
|
||||
|
||||
const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes))
|
||||
const pedersenHash = (data) => circomlib.babyJub.unpackPoint(circomlib.pedersenHash.hash(data))[0]
|
||||
|
@ -49,7 +49,6 @@ contract('ERC20Tornado', (accounts) => {
|
|||
const levels = MERKLE_TREE_HEIGHT || 16
|
||||
let tokenDenomination = TOKEN_AMOUNT || '1000000000000000000' // 1 ether
|
||||
let snapshotId
|
||||
let prefix = 'test'
|
||||
let tree
|
||||
const fee = bigInt(ETH_AMOUNT).shr(1) || bigInt(1e17)
|
||||
const refund = ETH_AMOUNT || '1000000000000000000' // 1 ether
|
||||
|
@ -60,7 +59,7 @@ contract('ERC20Tornado', (accounts) => {
|
|||
let proving_key
|
||||
|
||||
before(async () => {
|
||||
tree = new MerkleTree(levels, null, prefix)
|
||||
tree = new MerkleTree(levels)
|
||||
tornado = await Tornado.deployed()
|
||||
if (ERC20_TOKEN) {
|
||||
token = await Token.at(ERC20_TOKEN)
|
||||
|
@ -108,7 +107,7 @@ contract('ERC20Tornado', (accounts) => {
|
|||
it('should work', async () => {
|
||||
const deposit = generateDeposit()
|
||||
const user = accounts[4]
|
||||
await tree.insert(deposit.commitment)
|
||||
tree.insert(deposit.commitment)
|
||||
await token.mint(user, tokenDenomination)
|
||||
|
||||
const balanceUserBefore = await token.balanceOf(user)
|
||||
|
@ -121,11 +120,11 @@ contract('ERC20Tornado', (accounts) => {
|
|||
const balanceUserAfter = await token.balanceOf(user)
|
||||
balanceUserAfter.should.be.eq.BN(toBN(balanceUserBefore).sub(toBN(tokenDenomination)))
|
||||
|
||||
const { root, path_elements, path_index } = await tree.path(0)
|
||||
const { pathElements, pathIndices } = tree.path(0)
|
||||
// Circuit input
|
||||
const input = stringifyBigInts({
|
||||
// public
|
||||
root,
|
||||
root: tree.root(),
|
||||
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
||||
relayer,
|
||||
recipient,
|
||||
|
@ -135,8 +134,8 @@ contract('ERC20Tornado', (accounts) => {
|
|||
// private
|
||||
nullifier: deposit.nullifier,
|
||||
secret: deposit.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndices: path_index,
|
||||
pathElements: pathElements,
|
||||
pathIndices: pathIndices,
|
||||
})
|
||||
|
||||
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
||||
|
@ -193,7 +192,7 @@ contract('ERC20Tornado', (accounts) => {
|
|||
const deposit = generateDeposit()
|
||||
const user = accounts[4]
|
||||
recipient = bigInt(badRecipient.address)
|
||||
await tree.insert(deposit.commitment)
|
||||
tree.insert(deposit.commitment)
|
||||
await token.mint(user, tokenDenomination)
|
||||
|
||||
const balanceUserBefore = await token.balanceOf(user)
|
||||
|
@ -203,11 +202,11 @@ contract('ERC20Tornado', (accounts) => {
|
|||
const balanceUserAfter = await token.balanceOf(user)
|
||||
balanceUserAfter.should.be.eq.BN(toBN(balanceUserBefore).sub(toBN(tokenDenomination)))
|
||||
|
||||
const { root, path_elements, path_index } = await tree.path(0)
|
||||
const { pathElements, pathIndices } = tree.path(0)
|
||||
// Circuit input
|
||||
const input = stringifyBigInts({
|
||||
// public
|
||||
root,
|
||||
root: tree.root(),
|
||||
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
||||
relayer,
|
||||
recipient,
|
||||
|
@ -217,8 +216,8 @@ contract('ERC20Tornado', (accounts) => {
|
|||
// private
|
||||
nullifier: deposit.nullifier,
|
||||
secret: deposit.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndices: path_index,
|
||||
pathElements: pathElements,
|
||||
pathIndices: pathIndices,
|
||||
})
|
||||
|
||||
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
||||
|
@ -272,16 +271,16 @@ contract('ERC20Tornado', (accounts) => {
|
|||
it('should reject with wrong refund value', async () => {
|
||||
const deposit = generateDeposit()
|
||||
const user = accounts[4]
|
||||
await tree.insert(deposit.commitment)
|
||||
tree.insert(deposit.commitment)
|
||||
await token.mint(user, tokenDenomination)
|
||||
await token.approve(tornado.address, tokenDenomination, { from: user })
|
||||
await tornado.deposit(toFixedHex(deposit.commitment), { from: user, gasPrice: '0' })
|
||||
|
||||
const { root, path_elements, path_index } = await tree.path(0)
|
||||
const { pathElements, pathIndices } = tree.path(0)
|
||||
// Circuit input
|
||||
const input = stringifyBigInts({
|
||||
// public
|
||||
root,
|
||||
root: tree.root(),
|
||||
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
||||
relayer,
|
||||
recipient,
|
||||
|
@ -291,8 +290,8 @@ contract('ERC20Tornado', (accounts) => {
|
|||
// private
|
||||
nullifier: deposit.nullifier,
|
||||
secret: deposit.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndices: path_index,
|
||||
pathElements: pathElements,
|
||||
pathIndices: pathIndices,
|
||||
})
|
||||
|
||||
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
||||
|
@ -329,7 +328,7 @@ contract('ERC20Tornado', (accounts) => {
|
|||
console.log('userBal', userBal.toString())
|
||||
const senderBal = await usdtToken.balanceOf(sender)
|
||||
console.log('senderBal', senderBal.toString())
|
||||
await tree.insert(deposit.commitment)
|
||||
tree.insert(deposit.commitment)
|
||||
await usdtToken.transfer(user, tokenDenomination, { from: sender })
|
||||
console.log('transfer done')
|
||||
|
||||
|
@ -345,12 +344,12 @@ contract('ERC20Tornado', (accounts) => {
|
|||
const balanceUserAfter = await usdtToken.balanceOf(user)
|
||||
balanceUserAfter.should.be.eq.BN(toBN(balanceUserBefore).sub(toBN(tokenDenomination)))
|
||||
|
||||
const { root, path_elements, path_index } = await tree.path(0)
|
||||
const { pathElements, pathIndices } = tree.path(0)
|
||||
|
||||
// Circuit input
|
||||
const input = stringifyBigInts({
|
||||
// public
|
||||
root,
|
||||
root: tree.root(),
|
||||
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
||||
relayer: operator,
|
||||
recipient,
|
||||
|
@ -360,8 +359,8 @@ contract('ERC20Tornado', (accounts) => {
|
|||
// private
|
||||
nullifier: deposit.nullifier,
|
||||
secret: deposit.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndices: path_index,
|
||||
pathElements: pathElements,
|
||||
pathIndices: pathIndices,
|
||||
})
|
||||
|
||||
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
||||
|
@ -418,7 +417,7 @@ contract('ERC20Tornado', (accounts) => {
|
|||
console.log('userBal', userBal.toString())
|
||||
const senderBal = await token.balanceOf(sender)
|
||||
console.log('senderBal', senderBal.toString())
|
||||
await tree.insert(deposit.commitment)
|
||||
tree.insert(deposit.commitment)
|
||||
await token.transfer(user, tokenDenomination, { from: sender })
|
||||
console.log('transfer done')
|
||||
|
||||
|
@ -432,12 +431,12 @@ contract('ERC20Tornado', (accounts) => {
|
|||
const balanceUserAfter = await token.balanceOf(user)
|
||||
balanceUserAfter.should.be.eq.BN(toBN(balanceUserBefore).sub(toBN(tokenDenomination)))
|
||||
|
||||
const { root, path_elements, path_index } = await tree.path(0)
|
||||
const { pathElements, pathIndices } = tree.path(0)
|
||||
|
||||
// Circuit input
|
||||
const input = stringifyBigInts({
|
||||
// public
|
||||
root,
|
||||
root: tree.root(),
|
||||
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
||||
relayer: operator,
|
||||
recipient,
|
||||
|
@ -447,8 +446,8 @@ contract('ERC20Tornado', (accounts) => {
|
|||
// private
|
||||
nullifier: deposit.nullifier,
|
||||
secret: deposit.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndices: path_index,
|
||||
pathElements: pathElements,
|
||||
pathIndices: pathIndices,
|
||||
})
|
||||
|
||||
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
||||
|
@ -501,6 +500,6 @@ contract('ERC20Tornado', (accounts) => {
|
|||
await revertSnapshot(snapshotId.result)
|
||||
// eslint-disable-next-line require-atomic-updates
|
||||
snapshotId = await takeSnapshot()
|
||||
tree = new MerkleTree(levels, null, prefix)
|
||||
tree = new MerkleTree(levels)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -16,7 +16,7 @@ const snarkjs = require('snarkjs')
|
|||
const bigInt = snarkjs.bigInt
|
||||
const crypto = require('crypto')
|
||||
const circomlib = require('circomlib')
|
||||
const MerkleTree = require('../lib/MerkleTree')
|
||||
const MerkleTree = require('fixed-merkle-tree')
|
||||
|
||||
const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes))
|
||||
const pedersenHash = (data) => circomlib.babyJub.unpackPoint(circomlib.pedersenHash.hash(data))[0]
|
||||
|
@ -59,7 +59,6 @@ contract('ETHTornado', (accounts) => {
|
|||
const levels = MERKLE_TREE_HEIGHT || 16
|
||||
const value = ETH_AMOUNT || '1000000000000000000' // 1 ether
|
||||
let snapshotId
|
||||
let prefix = 'test'
|
||||
let tree
|
||||
const fee = bigInt(ETH_AMOUNT).shr(1) || bigInt(1e17)
|
||||
const refund = bigInt(0)
|
||||
|
@ -70,7 +69,7 @@ contract('ETHTornado', (accounts) => {
|
|||
let proving_key
|
||||
|
||||
before(async () => {
|
||||
tree = new MerkleTree(levels, null, prefix)
|
||||
tree = new MerkleTree(levels)
|
||||
tornado = await Tornado.deployed()
|
||||
snapshotId = await takeSnapshot()
|
||||
groth16 = await buildGroth16()
|
||||
|
@ -113,11 +112,11 @@ contract('ETHTornado', (accounts) => {
|
|||
describe('snark proof verification on js side', () => {
|
||||
it('should detect tampering', async () => {
|
||||
const deposit = generateDeposit()
|
||||
await tree.insert(deposit.commitment)
|
||||
const { root, path_elements, path_index } = await tree.path(0)
|
||||
tree.insert(deposit.commitment)
|
||||
const { pathElements, pathIndices } = tree.path(0)
|
||||
|
||||
const input = stringifyBigInts({
|
||||
root,
|
||||
root: tree.root(),
|
||||
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
||||
nullifier: deposit.nullifier,
|
||||
relayer: operator,
|
||||
|
@ -125,8 +124,8 @@ contract('ETHTornado', (accounts) => {
|
|||
fee,
|
||||
refund,
|
||||
secret: deposit.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndices: path_index,
|
||||
pathElements: pathElements,
|
||||
pathIndices: pathIndices,
|
||||
})
|
||||
|
||||
let proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
||||
|
@ -159,7 +158,7 @@ contract('ETHTornado', (accounts) => {
|
|||
it('should work', async () => {
|
||||
const deposit = generateDeposit()
|
||||
const user = accounts[4]
|
||||
await tree.insert(deposit.commitment)
|
||||
tree.insert(deposit.commitment)
|
||||
|
||||
const balanceUserBefore = await web3.eth.getBalance(user)
|
||||
|
||||
|
@ -171,12 +170,12 @@ contract('ETHTornado', (accounts) => {
|
|||
const balanceUserAfter = await web3.eth.getBalance(user)
|
||||
balanceUserAfter.should.be.eq.BN(toBN(balanceUserBefore).sub(toBN(value)))
|
||||
|
||||
const { root, path_elements, path_index } = await tree.path(0)
|
||||
const { pathElements, pathIndices } = tree.path(0)
|
||||
|
||||
// Circuit input
|
||||
const input = stringifyBigInts({
|
||||
// public
|
||||
root,
|
||||
root: tree.root(),
|
||||
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
||||
relayer: operator,
|
||||
recipient,
|
||||
|
@ -186,8 +185,8 @@ contract('ETHTornado', (accounts) => {
|
|||
// private
|
||||
nullifier: deposit.nullifier,
|
||||
secret: deposit.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndices: path_index,
|
||||
pathElements: pathElements,
|
||||
pathIndices: pathIndices,
|
||||
})
|
||||
|
||||
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
||||
|
@ -233,13 +232,13 @@ contract('ETHTornado', (accounts) => {
|
|||
|
||||
it('should prevent double spend', async () => {
|
||||
const deposit = generateDeposit()
|
||||
await tree.insert(deposit.commitment)
|
||||
tree.insert(deposit.commitment)
|
||||
await tornado.deposit(toFixedHex(deposit.commitment), { value, from: sender })
|
||||
|
||||
const { root, path_elements, path_index } = await tree.path(0)
|
||||
const { pathElements, pathIndices } = tree.path(0)
|
||||
|
||||
const input = stringifyBigInts({
|
||||
root,
|
||||
root: tree.root(),
|
||||
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
||||
nullifier: deposit.nullifier,
|
||||
relayer: operator,
|
||||
|
@ -247,8 +246,8 @@ contract('ETHTornado', (accounts) => {
|
|||
fee,
|
||||
refund,
|
||||
secret: deposit.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndices: path_index,
|
||||
pathElements: pathElements,
|
||||
pathIndices: pathIndices,
|
||||
})
|
||||
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
||||
const { proof } = websnarkUtils.toSolidityInput(proofData)
|
||||
|
@ -267,13 +266,13 @@ contract('ETHTornado', (accounts) => {
|
|||
|
||||
it('should prevent double spend with overflow', async () => {
|
||||
const deposit = generateDeposit()
|
||||
await tree.insert(deposit.commitment)
|
||||
tree.insert(deposit.commitment)
|
||||
await tornado.deposit(toFixedHex(deposit.commitment), { value, from: sender })
|
||||
|
||||
const { root, path_elements, path_index } = await tree.path(0)
|
||||
const { pathElements, pathIndices } = tree.path(0)
|
||||
|
||||
const input = stringifyBigInts({
|
||||
root,
|
||||
root: tree.root(),
|
||||
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
||||
nullifier: deposit.nullifier,
|
||||
relayer: operator,
|
||||
|
@ -281,8 +280,8 @@ contract('ETHTornado', (accounts) => {
|
|||
fee,
|
||||
refund,
|
||||
secret: deposit.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndices: path_index,
|
||||
pathElements: pathElements,
|
||||
pathIndices: pathIndices,
|
||||
})
|
||||
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
||||
const { proof } = websnarkUtils.toSolidityInput(proofData)
|
||||
|
@ -304,13 +303,13 @@ contract('ETHTornado', (accounts) => {
|
|||
|
||||
it('fee should be less or equal transfer value', async () => {
|
||||
const deposit = generateDeposit()
|
||||
await tree.insert(deposit.commitment)
|
||||
tree.insert(deposit.commitment)
|
||||
await tornado.deposit(toFixedHex(deposit.commitment), { value, from: sender })
|
||||
|
||||
const { root, path_elements, path_index } = await tree.path(0)
|
||||
const { pathElements, pathIndices } = tree.path(0)
|
||||
const largeFee = bigInt(value).add(bigInt(1))
|
||||
const input = stringifyBigInts({
|
||||
root,
|
||||
root: tree.root(),
|
||||
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
||||
nullifier: deposit.nullifier,
|
||||
relayer: operator,
|
||||
|
@ -318,8 +317,8 @@ contract('ETHTornado', (accounts) => {
|
|||
fee: largeFee,
|
||||
refund,
|
||||
secret: deposit.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndices: path_index,
|
||||
pathElements: pathElements,
|
||||
pathIndices: pathIndices,
|
||||
})
|
||||
|
||||
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
||||
|
@ -338,22 +337,22 @@ contract('ETHTornado', (accounts) => {
|
|||
|
||||
it('should throw for corrupted merkle tree root', async () => {
|
||||
const deposit = generateDeposit()
|
||||
await tree.insert(deposit.commitment)
|
||||
tree.insert(deposit.commitment)
|
||||
await tornado.deposit(toFixedHex(deposit.commitment), { value, from: sender })
|
||||
|
||||
const { root, path_elements, path_index } = await tree.path(0)
|
||||
const { pathElements, pathIndices } = tree.path(0)
|
||||
|
||||
const input = stringifyBigInts({
|
||||
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
||||
root,
|
||||
root: tree.root(),
|
||||
nullifier: deposit.nullifier,
|
||||
relayer: operator,
|
||||
recipient,
|
||||
fee,
|
||||
refund,
|
||||
secret: deposit.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndices: path_index,
|
||||
pathElements: pathElements,
|
||||
pathIndices: pathIndices,
|
||||
})
|
||||
|
||||
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
||||
|
@ -373,13 +372,13 @@ contract('ETHTornado', (accounts) => {
|
|||
|
||||
it('should reject with tampered public inputs', async () => {
|
||||
const deposit = generateDeposit()
|
||||
await tree.insert(deposit.commitment)
|
||||
tree.insert(deposit.commitment)
|
||||
await tornado.deposit(toFixedHex(deposit.commitment), { value, from: sender })
|
||||
|
||||
let { root, path_elements, path_index } = await tree.path(0)
|
||||
let { pathElements, pathIndices } = tree.path(0)
|
||||
|
||||
const input = stringifyBigInts({
|
||||
root,
|
||||
root: tree.root(),
|
||||
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
||||
nullifier: deposit.nullifier,
|
||||
relayer: operator,
|
||||
|
@ -387,8 +386,8 @@ contract('ETHTornado', (accounts) => {
|
|||
fee,
|
||||
refund,
|
||||
secret: deposit.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndices: path_index,
|
||||
pathElements: pathElements,
|
||||
pathIndices: pathIndices,
|
||||
})
|
||||
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
||||
let { proof } = websnarkUtils.toSolidityInput(proofData)
|
||||
|
@ -449,22 +448,22 @@ contract('ETHTornado', (accounts) => {
|
|||
|
||||
it('should reject with non zero refund', async () => {
|
||||
const deposit = generateDeposit()
|
||||
await tree.insert(deposit.commitment)
|
||||
tree.insert(deposit.commitment)
|
||||
await tornado.deposit(toFixedHex(deposit.commitment), { value, from: sender })
|
||||
|
||||
const { root, path_elements, path_index } = await tree.path(0)
|
||||
const { pathElements, pathIndices } = tree.path(0)
|
||||
|
||||
const input = stringifyBigInts({
|
||||
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
||||
root,
|
||||
root: tree.root(),
|
||||
nullifier: deposit.nullifier,
|
||||
relayer: operator,
|
||||
recipient,
|
||||
fee,
|
||||
refund: bigInt(1),
|
||||
secret: deposit.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndices: path_index,
|
||||
pathElements: pathElements,
|
||||
pathIndices: pathIndices,
|
||||
})
|
||||
|
||||
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
||||
|
@ -487,17 +486,17 @@ contract('ETHTornado', (accounts) => {
|
|||
it('should work', async () => {
|
||||
const deposit1 = generateDeposit()
|
||||
const deposit2 = generateDeposit()
|
||||
await tree.insert(deposit1.commitment)
|
||||
await tree.insert(deposit2.commitment)
|
||||
tree.insert(deposit1.commitment)
|
||||
tree.insert(deposit2.commitment)
|
||||
await tornado.deposit(toFixedHex(deposit1.commitment), { value, gasPrice: '0' })
|
||||
await tornado.deposit(toFixedHex(deposit2.commitment), { value, gasPrice: '0' })
|
||||
|
||||
const { root, path_elements, path_index } = await tree.path(1)
|
||||
const { pathElements, pathIndices } = tree.path(1)
|
||||
|
||||
// Circuit input
|
||||
const input = stringifyBigInts({
|
||||
// public
|
||||
root,
|
||||
root: tree.root(),
|
||||
nullifierHash: pedersenHash(deposit2.nullifier.leInt2Buff(31)),
|
||||
relayer: operator,
|
||||
recipient,
|
||||
|
@ -507,8 +506,8 @@ contract('ETHTornado', (accounts) => {
|
|||
// private
|
||||
nullifier: deposit2.nullifier,
|
||||
secret: deposit2.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndices: path_index,
|
||||
pathElements: pathElements,
|
||||
pathIndices: pathIndices,
|
||||
})
|
||||
|
||||
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
||||
|
@ -536,6 +535,6 @@ contract('ETHTornado', (accounts) => {
|
|||
await revertSnapshot(snapshotId.result)
|
||||
// eslint-disable-next-line require-atomic-updates
|
||||
snapshotId = await takeSnapshot()
|
||||
tree = new MerkleTree(levels, null, prefix)
|
||||
tree = new MerkleTree(levels)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* global artifacts, web3, contract, assert */
|
||||
/* global artifacts, web3, contract */
|
||||
require('chai').use(require('bn-chai')(web3.utils.BN)).use(require('chai-as-promised')).should()
|
||||
|
||||
const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper')
|
||||
|
@ -6,8 +6,7 @@ const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper')
|
|||
const MerkleTreeWithHistory = artifacts.require('./MerkleTreeWithHistoryMock.sol')
|
||||
const hasherContract = artifacts.require('./Hasher.sol')
|
||||
|
||||
const MerkleTree = require('../lib/MerkleTree')
|
||||
const hasherImpl = require('../lib/MiMC')
|
||||
const MerkleTree = require('fixed-merkle-tree')
|
||||
|
||||
const snarkjs = require('snarkjs')
|
||||
const bigInt = snarkjs.bigInt
|
||||
|
@ -38,12 +37,10 @@ contract('MerkleTreeWithHistory', (accounts) => {
|
|||
// eslint-disable-next-line no-unused-vars
|
||||
const value = ETH_AMOUNT || '1000000000000000000'
|
||||
let snapshotId
|
||||
let prefix = 'test'
|
||||
let tree
|
||||
let hasher
|
||||
|
||||
before(async () => {
|
||||
tree = new MerkleTree(levels, null, prefix)
|
||||
tree = new MerkleTree(levels)
|
||||
hasherInstance = await hasherContract.deployed()
|
||||
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, hasherInstance.address)
|
||||
snapshotId = await takeSnapshot()
|
||||
|
@ -59,94 +56,15 @@ contract('MerkleTreeWithHistory', (accounts) => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('merkleTreeLib', () => {
|
||||
it('index_to_key', () => {
|
||||
assert.equal(MerkleTree.index_to_key('test', 5, 20), 'test_tree_5_20')
|
||||
})
|
||||
|
||||
it('tests insert', async () => {
|
||||
hasher = new hasherImpl()
|
||||
tree = new MerkleTree(2, null, prefix)
|
||||
await tree.insert(toFixedHex('5'))
|
||||
let { root, path_elements } = await tree.path(0)
|
||||
const calculated_root = hasher.hash(null, hasher.hash(null, '5', path_elements[0]), path_elements[1])
|
||||
// console.log(root)
|
||||
assert.equal(root, calculated_root)
|
||||
})
|
||||
it('creation odd elements count', async () => {
|
||||
const elements = [12, 13, 14, 15, 16, 17, 18, 19, 20]
|
||||
for (const [, el] of Object.entries(elements)) {
|
||||
await tree.insert(el)
|
||||
}
|
||||
|
||||
const batchTree = new MerkleTree(levels, elements, prefix)
|
||||
for (const [i] of Object.entries(elements)) {
|
||||
const pathViaConstructor = await batchTree.path(i)
|
||||
const pathViaUpdate = await tree.path(i)
|
||||
pathViaConstructor.should.be.deep.equal(pathViaUpdate)
|
||||
}
|
||||
})
|
||||
|
||||
it('should find an element', async () => {
|
||||
const elements = [12, 13, 14, 15, 16, 17, 18, 19, 20]
|
||||
for (const [, el] of Object.entries(elements)) {
|
||||
await tree.insert(el)
|
||||
}
|
||||
let index = tree.getIndexByElement(13)
|
||||
index.should.be.equal(1)
|
||||
|
||||
index = tree.getIndexByElement(19)
|
||||
index.should.be.equal(7)
|
||||
|
||||
index = tree.getIndexByElement(12)
|
||||
index.should.be.equal(0)
|
||||
|
||||
index = tree.getIndexByElement(20)
|
||||
index.should.be.equal(8)
|
||||
|
||||
index = tree.getIndexByElement(42)
|
||||
index.should.be.equal(false)
|
||||
})
|
||||
|
||||
it('creation even elements count', async () => {
|
||||
const elements = [12, 13, 14, 15, 16, 17]
|
||||
for (const [, el] of Object.entries(elements)) {
|
||||
await tree.insert(el)
|
||||
}
|
||||
|
||||
const batchTree = new MerkleTree(levels, elements, prefix)
|
||||
for (const [i] of Object.entries(elements)) {
|
||||
const pathViaConstructor = await batchTree.path(i)
|
||||
const pathViaUpdate = await tree.path(i)
|
||||
pathViaConstructor.should.be.deep.equal(pathViaUpdate)
|
||||
}
|
||||
})
|
||||
|
||||
it.skip('creation using 30000 elements', () => {
|
||||
const elements = []
|
||||
for (let i = 1000; i < 31001; i++) {
|
||||
elements.push(i)
|
||||
}
|
||||
console.time('MerkleTree')
|
||||
tree = new MerkleTree(levels, elements, prefix)
|
||||
console.timeEnd('MerkleTree')
|
||||
// 2,7 GHz Intel Core i7
|
||||
// 1000 : 1949.084ms
|
||||
// 10000: 19456.220ms
|
||||
// 30000: 63406.679ms
|
||||
})
|
||||
})
|
||||
|
||||
describe('#insert', () => {
|
||||
it('should insert', async () => {
|
||||
let rootFromContract
|
||||
|
||||
for (let i = 1; i < 11; i++) {
|
||||
await merkleTreeWithHistory.insert(toFixedHex(i), { from: sender })
|
||||
await tree.insert(i)
|
||||
let { root } = await tree.path(i - 1)
|
||||
tree.insert(i)
|
||||
rootFromContract = await merkleTreeWithHistory.getLastRoot()
|
||||
toFixedHex(root).should.be.equal(rootFromContract.toString())
|
||||
toFixedHex(tree.root()).should.be.equal(rootFromContract.toString())
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -177,19 +95,16 @@ contract('MerkleTreeWithHistory', (accounts) => {
|
|||
|
||||
describe('#isKnownRoot', () => {
|
||||
it('should work', async () => {
|
||||
let path
|
||||
|
||||
for (let i = 1; i < 5; i++) {
|
||||
await merkleTreeWithHistory.insert(toFixedHex(i), { from: sender }).should.be.fulfilled
|
||||
await tree.insert(i)
|
||||
path = await tree.path(i - 1)
|
||||
let isKnown = await merkleTreeWithHistory.isKnownRoot(toFixedHex(path.root))
|
||||
let isKnown = await merkleTreeWithHistory.isKnownRoot(toFixedHex(tree.root()))
|
||||
isKnown.should.be.equal(true)
|
||||
}
|
||||
|
||||
await merkleTreeWithHistory.insert(toFixedHex(42), { from: sender }).should.be.fulfilled
|
||||
// check outdated root
|
||||
let isKnown = await merkleTreeWithHistory.isKnownRoot(toFixedHex(path.root))
|
||||
let isKnown = await merkleTreeWithHistory.isKnownRoot(toFixedHex(tree.root()))
|
||||
isKnown.should.be.equal(true)
|
||||
})
|
||||
|
||||
|
@ -204,7 +119,6 @@ contract('MerkleTreeWithHistory', (accounts) => {
|
|||
await revertSnapshot(snapshotId.result)
|
||||
// eslint-disable-next-line require-atomic-updates
|
||||
snapshotId = await takeSnapshot()
|
||||
hasher = new hasherImpl()
|
||||
tree = new MerkleTree(levels, null, prefix, null, hasher)
|
||||
tree = new MerkleTree(levels)
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue