changed emptyElement to constant

This commit is contained in:
poma 2019-11-02 15:35:22 +03:00
parent 27a00bfd5f
commit 1fdabcc97c
13 changed files with 27 additions and 46 deletions

View File

@ -2,7 +2,6 @@ MERKLE_TREE_HEIGHT=16
# in wei # in wei
ETH_AMOUNT=100000000000000000 ETH_AMOUNT=100000000000000000
TOKEN_AMOUNT=100000000000000000 TOKEN_AMOUNT=100000000000000000
EMPTY_ELEMENT=1
PRIVATE_KEY= PRIVATE_KEY=
ERC20_TOKEN= ERC20_TOKEN=

8
cli.js
View File

@ -13,7 +13,7 @@ const buildGroth16 = require('websnark/src/groth16')
const websnarkUtils = require('websnark/src/utils') const websnarkUtils = require('websnark/src/utils')
let web3, mixer, erc20mixer, circuit, proving_key, groth16, erc20 let web3, mixer, erc20mixer, circuit, proving_key, groth16, erc20
let MERKLE_TREE_HEIGHT, ETH_AMOUNT, EMPTY_ELEMENT, ERC20_TOKEN let MERKLE_TREE_HEIGHT, ETH_AMOUNT, ERC20_TOKEN
const inBrowser = (typeof window !== 'undefined') const inBrowser = (typeof window !== 'undefined')
/** Generate random number of specified byte length */ /** Generate random number of specified byte length */
@ -83,7 +83,7 @@ async function withdrawErc20(note, receiver, relayer) {
} }
return e.returnValues.commitment return e.returnValues.commitment
}) })
const tree = new merkleTree(MERKLE_TREE_HEIGHT, EMPTY_ELEMENT, leaves) const tree = new merkleTree(MERKLE_TREE_HEIGHT, leaves)
const validRoot = await erc20mixer.methods.isKnownRoot(await tree.root()).call() const validRoot = await erc20mixer.methods.isKnownRoot(await tree.root()).call()
const nullifierHash = pedersenHash(deposit.nullifier.leInt2Buff(31)) const nullifierHash = pedersenHash(deposit.nullifier.leInt2Buff(31))
const nullifierHashToCheck = nullifierHash.toString(16).padStart('66', '0x000000') const nullifierHashToCheck = nullifierHash.toString(16).padStart('66', '0x000000')
@ -152,7 +152,7 @@ async function withdraw(note, receiver) {
const leaves = events const leaves = events
.sort((a, b) => a.returnValues.leafIndex.sub(b.returnValues.leafIndex)) // Sort events in chronological order .sort((a, b) => a.returnValues.leafIndex.sub(b.returnValues.leafIndex)) // Sort events in chronological order
.map(e => e.returnValues.commitment) .map(e => e.returnValues.commitment)
const tree = new merkleTree(MERKLE_TREE_HEIGHT, EMPTY_ELEMENT, leaves) const tree = new merkleTree(MERKLE_TREE_HEIGHT, leaves)
// Find current commitment in the tree // Find current commitment in the tree
let depositEvent = events.find(e => e.returnValues.commitment.eq(paddedCommitment)) let depositEvent = events.find(e => e.returnValues.commitment.eq(paddedCommitment))
@ -210,7 +210,6 @@ async function init() {
proving_key = await (await fetch('build/circuits/withdraw_proving_key.bin')).arrayBuffer() proving_key = await (await fetch('build/circuits/withdraw_proving_key.bin')).arrayBuffer()
MERKLE_TREE_HEIGHT = 16 MERKLE_TREE_HEIGHT = 16
ETH_AMOUNT = 1e18 ETH_AMOUNT = 1e18
EMPTY_ELEMENT = 1
} else { } else {
// Initialize from local node // Initialize from local node
web3 = new Web3('http://localhost:8545', null, { transactionConfirmationBlocks: 1 }) web3 = new Web3('http://localhost:8545', null, { transactionConfirmationBlocks: 1 })
@ -220,7 +219,6 @@ async function init() {
require('dotenv').config() require('dotenv').config()
MERKLE_TREE_HEIGHT = process.env.MERKLE_TREE_HEIGHT MERKLE_TREE_HEIGHT = process.env.MERKLE_TREE_HEIGHT
ETH_AMOUNT = process.env.ETH_AMOUNT ETH_AMOUNT = process.env.ETH_AMOUNT
EMPTY_ELEMENT = process.env.EMPTY_ELEMENT
ERC20_TOKEN = process.env.ERC20_TOKEN ERC20_TOKEN = process.env.ERC20_TOKEN
erc20ContractJson = require('./build/contracts/ERC20Mock.json') erc20ContractJson = require('./build/contracts/ERC20Mock.json')
erc20mixerJson = require('./build/contracts/ERC20Mixer.json') erc20mixerJson = require('./build/contracts/ERC20Mixer.json')

View File

@ -20,10 +20,9 @@ contract ERC20Mixer is Mixer {
IVerifier _verifier, IVerifier _verifier,
uint256 _denomination, uint256 _denomination,
uint8 _merkleTreeHeight, uint8 _merkleTreeHeight,
uint256 _emptyElement,
address _operator, address _operator,
address _token address _token
) Mixer(_verifier, _denomination, _merkleTreeHeight, _emptyElement, _operator) public { ) Mixer(_verifier, _denomination, _merkleTreeHeight, _operator) public {
token = _token; token = _token;
} }

View File

@ -18,9 +18,8 @@ contract ETHMixer is Mixer {
IVerifier _verifier, IVerifier _verifier,
uint256 _denomination, uint256 _denomination,
uint8 _merkleTreeHeight, uint8 _merkleTreeHeight,
uint256 _emptyElement,
address _operator address _operator
) Mixer(_verifier, _denomination, _merkleTreeHeight, _emptyElement, _operator) public { ) Mixer(_verifier, _denomination, _merkleTreeHeight, _operator) public {
} }
function _processWithdraw(address payable _receiver, address payable _relayer, uint256 _fee, uint256 _refund) internal { function _processWithdraw(address payable _receiver, address payable _relayer, uint256 _fee, uint256 _refund) internal {

View File

@ -18,8 +18,9 @@ library Hasher {
contract MerkleTreeWithHistory { contract MerkleTreeWithHistory {
uint256 public levels; uint256 public levels;
uint256 constant FIELD_SIZE = 21888242871839275222246405745257275088548364400416034343698204186575808495617; uint256 public constant FIELD_SIZE = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
uint256 constant ROOT_HISTORY_SIZE = 100; uint256 public constant ZERO_VALUE = 5702960885942360421128284892092891246826997279710054143430547229469817701242; // = MiMC("tornado")
uint256 public constant ROOT_HISTORY_SIZE = 100;
uint256[ROOT_HISTORY_SIZE] public _roots; uint256[ROOT_HISTORY_SIZE] public _roots;
uint256 public current_root_index = 0; uint256 public current_root_index = 0;
@ -28,12 +29,12 @@ contract MerkleTreeWithHistory {
uint32 public next_index = 0; uint32 public next_index = 0;
constructor(uint256 tree_levels, uint256 zero_value) public { constructor(uint256 tree_levels) public {
require(tree_levels > 0, "tree_levels should be greater than zero"); require(tree_levels > 0, "tree_levels should be greater than zero");
levels = tree_levels; levels = tree_levels;
uint256 current_zero = zero_value; uint256 current_zero = ZERO_VALUE;
_zeros.push(zero_value); _zeros.push(ZERO_VALUE);
_filled_subtrees.push(current_zero); _filled_subtrees.push(current_zero);
for (uint8 i = 1; i < levels; i++) { for (uint8 i = 1; i < levels; i++) {

View File

@ -43,16 +43,14 @@ contract Mixer is MerkleTreeWithHistory {
@dev The constructor @dev The constructor
@param _verifier the address of SNARK verifier for this contract @param _verifier the address of SNARK verifier for this contract
@param _merkleTreeHeight the height of deposits' Merkle Tree @param _merkleTreeHeight the height of deposits' Merkle Tree
@param _emptyElement default element of the deposits' Merkle Tree
@param _operator operator address (see operator above) @param _operator operator address (see operator above)
*/ */
constructor( constructor(
IVerifier _verifier, IVerifier _verifier,
uint256 _denomination, uint256 _denomination,
uint8 _merkleTreeHeight, uint8 _merkleTreeHeight,
uint256 _emptyElement,
address _operator address _operator
) MerkleTreeWithHistory(_merkleTreeHeight, _emptyElement) public { ) MerkleTreeWithHistory(_merkleTreeHeight) public {
require(_denomination > 0, "denomination should be greater than 0"); require(_denomination > 0, "denomination should be greater than 0");
verifier = _verifier; verifier = _verifier;
operator = _operator; operator = _operator;

View File

@ -4,7 +4,7 @@ import '../MerkleTreeWithHistory.sol';
contract MerkleTreeWithHistoryMock is MerkleTreeWithHistory { contract MerkleTreeWithHistoryMock is MerkleTreeWithHistory {
constructor (uint8 tree_levels, uint256 zero_value) MerkleTreeWithHistory(tree_levels, zero_value) public {} constructor (uint8 tree_levels) MerkleTreeWithHistory(tree_levels) public {}
function insert(uint256 leaf) public { function insert(uint256 leaf) public {
_insert(leaf); _insert(leaf);

View File

@ -1,9 +1,10 @@
const jsStorage = require('./Storage') const jsStorage = require('./Storage')
const hasherImpl = require('./MiMC') const hasherImpl = require('./MiMC')
const { bigInt } = require('snarkjs')
class MerkleTree { class MerkleTree {
constructor(n_levels, zero_value, defaultElements, prefix, storage, hasher) { constructor(n_levels, defaultElements, prefix, storage, hasher) {
this.prefix = prefix this.prefix = prefix
this.storage = storage || new jsStorage() this.storage = storage || new jsStorage()
this.hasher = hasher || new hasherImpl() this.hasher = hasher || new hasherImpl()
@ -11,7 +12,7 @@ class MerkleTree {
this.zero_values = [] this.zero_values = []
this.totalElements = 0 this.totalElements = 0
let current_zero_value = zero_value || 0 let current_zero_value = bigInt('5702960885942360421128284892092891246826997279710054143430547229469817701242')
this.zero_values.push(current_zero_value) this.zero_values.push(current_zero_value)
for (let i = 0; i < n_levels; i++) { for (let i = 0; i < n_levels; i++) {
current_zero_value = this.hasher.hash(i, current_zero_value, current_zero_value) current_zero_value = this.hasher.hash(i, current_zero_value, current_zero_value)

View File

@ -7,11 +7,11 @@ const hasherContract = artifacts.require('Hasher')
module.exports = function(deployer, network, accounts) { module.exports = function(deployer, network, accounts) {
return deployer.then(async () => { return deployer.then(async () => {
const { MERKLE_TREE_HEIGHT, ETH_AMOUNT, EMPTY_ELEMENT } = process.env const { MERKLE_TREE_HEIGHT, ETH_AMOUNT } = process.env
const verifier = await Verifier.deployed() const verifier = await Verifier.deployed()
const hasherInstance = await hasherContract.deployed() const hasherInstance = await hasherContract.deployed()
await ETHMixer.link(hasherContract, hasherInstance.address) await ETHMixer.link(hasherContract, hasherInstance.address)
const mixer = await deployer.deploy(ETHMixer, verifier.address, ETH_AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT, accounts[0]) const mixer = await deployer.deploy(ETHMixer, verifier.address, ETH_AMOUNT, MERKLE_TREE_HEIGHT, accounts[0])
console.log('ETHMixer\'s address ', mixer.address) console.log('ETHMixer\'s address ', mixer.address)
}) })
} }

View File

@ -8,7 +8,7 @@ const ERC20Mock = artifacts.require('ERC20Mock')
module.exports = function(deployer, network, accounts) { module.exports = function(deployer, network, accounts) {
return deployer.then(async () => { return deployer.then(async () => {
const { MERKLE_TREE_HEIGHT, EMPTY_ELEMENT, ERC20_TOKEN, TOKEN_AMOUNT } = process.env const { MERKLE_TREE_HEIGHT, ERC20_TOKEN, TOKEN_AMOUNT } = process.env
const verifier = await Verifier.deployed() const verifier = await Verifier.deployed()
const hasherInstance = await hasherContract.deployed() const hasherInstance = await hasherContract.deployed()
await ERC20Mixer.link(hasherContract, hasherInstance.address) await ERC20Mixer.link(hasherContract, hasherInstance.address)
@ -22,7 +22,6 @@ module.exports = function(deployer, network, accounts) {
verifier.address, verifier.address,
TOKEN_AMOUNT, TOKEN_AMOUNT,
MERKLE_TREE_HEIGHT, MERKLE_TREE_HEIGHT,
EMPTY_ELEMENT,
accounts[0], accounts[0],
token, token,
) )

View File

@ -11,7 +11,7 @@ const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper')
const Mixer = artifacts.require('./ERC20Mixer.sol') const Mixer = artifacts.require('./ERC20Mixer.sol')
const Token = artifacts.require('./ERC20Mock.sol') const Token = artifacts.require('./ERC20Mock.sol')
const USDTToken = artifacts.require('./IUSDT.sol') const USDTToken = artifacts.require('./IUSDT.sol')
const { ETH_AMOUNT, TOKEN_AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT, ERC20_TOKEN } = process.env const { ETH_AMOUNT, TOKEN_AMOUNT, MERKLE_TREE_HEIGHT, ERC20_TOKEN } = process.env
const websnarkUtils = require('websnark/src/utils') const websnarkUtils = require('websnark/src/utils')
const buildGroth16 = require('websnark/src/groth16') const buildGroth16 = require('websnark/src/groth16')
@ -50,7 +50,6 @@ contract('ERC20Mixer', accounts => {
const sender = accounts[0] const sender = accounts[0]
const operator = accounts[0] const operator = accounts[0]
const levels = MERKLE_TREE_HEIGHT || 16 const levels = MERKLE_TREE_HEIGHT || 16
const zeroValue = EMPTY_ELEMENT || 1337
let tokenDenomination = TOKEN_AMOUNT || '1000000000000000000' // 1 ether let tokenDenomination = TOKEN_AMOUNT || '1000000000000000000' // 1 ether
let snapshotId let snapshotId
let prefix = 'test' let prefix = 'test'
@ -66,7 +65,6 @@ contract('ERC20Mixer', accounts => {
before(async () => { before(async () => {
tree = new MerkleTree( tree = new MerkleTree(
levels, levels,
zeroValue,
null, null,
prefix, prefix,
) )
@ -401,7 +399,6 @@ contract('ERC20Mixer', accounts => {
snapshotId = await takeSnapshot() snapshotId = await takeSnapshot()
tree = new MerkleTree( tree = new MerkleTree(
levels, levels,
zeroValue,
null, null,
prefix, prefix,
) )

View File

@ -9,7 +9,7 @@ const { toBN, toHex, randomHex } = require('web3-utils')
const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper') const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper')
const Mixer = artifacts.require('./ETHMixer.sol') const Mixer = artifacts.require('./ETHMixer.sol')
const { ETH_AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT } = process.env const { ETH_AMOUNT, MERKLE_TREE_HEIGHT } = process.env
const websnarkUtils = require('websnark/src/utils') const websnarkUtils = require('websnark/src/utils')
const buildGroth16 = require('websnark/src/groth16') const buildGroth16 = require('websnark/src/groth16')
@ -62,7 +62,6 @@ contract('ETHMixer', accounts => {
const sender = accounts[0] const sender = accounts[0]
const operator = accounts[0] const operator = accounts[0]
const levels = MERKLE_TREE_HEIGHT || 16 const levels = MERKLE_TREE_HEIGHT || 16
const zeroValue = EMPTY_ELEMENT || 1337
const value = ETH_AMOUNT || '1000000000000000000' // 1 ether const value = ETH_AMOUNT || '1000000000000000000' // 1 ether
let snapshotId let snapshotId
let prefix = 'test' let prefix = 'test'
@ -78,7 +77,6 @@ contract('ETHMixer', accounts => {
before(async () => { before(async () => {
tree = new MerkleTree( tree = new MerkleTree(
levels, levels,
zeroValue,
null, null,
prefix, prefix,
) )
@ -521,7 +519,6 @@ contract('ETHMixer', accounts => {
snapshotId = await takeSnapshot() snapshotId = await takeSnapshot()
tree = new MerkleTree( tree = new MerkleTree(
levels, levels,
zeroValue,
null, null,
prefix, prefix,
) )

View File

@ -12,7 +12,7 @@ const hasherContract = artifacts.require('./Hasher.sol')
const MerkleTree = require('../lib/MerkleTree') const MerkleTree = require('../lib/MerkleTree')
const hasherImpl = require('../lib/MiMC') const hasherImpl = require('../lib/MiMC')
const { ETH_AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT } = process.env const { ETH_AMOUNT, MERKLE_TREE_HEIGHT } = process.env
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
function BNArrayToStringArray(array) { function BNArrayToStringArray(array) {
@ -27,7 +27,6 @@ contract('MerkleTreeWithHistory', accounts => {
let merkleTreeWithHistory let merkleTreeWithHistory
let hasherInstance let hasherInstance
let levels = MERKLE_TREE_HEIGHT || 16 let levels = MERKLE_TREE_HEIGHT || 16
let zeroValue = EMPTY_ELEMENT || 1337
const sender = accounts[0] const sender = accounts[0]
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
const value = ETH_AMOUNT || '1000000000000000000' const value = ETH_AMOUNT || '1000000000000000000'
@ -39,19 +38,19 @@ contract('MerkleTreeWithHistory', accounts => {
before(async () => { before(async () => {
tree = new MerkleTree( tree = new MerkleTree(
levels, levels,
zeroValue,
null, null,
prefix, prefix,
) )
hasherInstance = await hasherContract.deployed() hasherInstance = await hasherContract.deployed()
await MerkleTreeWithHistory.link(hasherContract, hasherInstance.address) await MerkleTreeWithHistory.link(hasherContract, hasherInstance.address)
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, zeroValue) merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
snapshotId = await takeSnapshot() snapshotId = await takeSnapshot()
}) })
describe('#constructor', () => { describe('#constructor', () => {
it('should initialize', async () => { it('should initialize', async () => {
const filled_subtrees = await merkleTreeWithHistory.filled_subtrees() const filled_subtrees = await merkleTreeWithHistory.filled_subtrees()
const zeroValue = await merkleTreeWithHistory.ZERO_VALUE()
filled_subtrees[0].should.be.eq.BN(zeroValue) filled_subtrees[0].should.be.eq.BN(zeroValue)
const zeros = await merkleTreeWithHistory.zeros() const zeros = await merkleTreeWithHistory.zeros()
zeros[0].should.be.eq.BN(zeroValue) zeros[0].should.be.eq.BN(zeroValue)
@ -70,7 +69,6 @@ contract('MerkleTreeWithHistory', accounts => {
hasher = new hasherImpl() hasher = new hasherImpl()
tree = new MerkleTree( tree = new MerkleTree(
2, 2,
zeroValue,
null, null,
prefix, prefix,
) )
@ -91,7 +89,6 @@ contract('MerkleTreeWithHistory', accounts => {
const batchTree = new MerkleTree( const batchTree = new MerkleTree(
levels, levels,
zeroValue,
elements, elements,
prefix, prefix,
) )
@ -131,7 +128,6 @@ contract('MerkleTreeWithHistory', accounts => {
const batchTree = new MerkleTree( const batchTree = new MerkleTree(
levels, levels,
zeroValue,
elements, elements,
prefix, prefix,
) )
@ -150,7 +146,6 @@ contract('MerkleTreeWithHistory', accounts => {
console.time('MerkleTree') console.time('MerkleTree')
tree = new MerkleTree( tree = new MerkleTree(
levels, levels,
zeroValue,
elements, elements,
prefix, prefix,
) )
@ -177,8 +172,7 @@ contract('MerkleTreeWithHistory', accounts => {
it('should reject if tree is full', async () => { it('should reject if tree is full', async () => {
levels = 6 levels = 6
zeroValue = 1337 merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, zeroValue)
for (let i = 0; i < 2**levels; i++) { for (let i = 0; i < 2**levels; i++) {
await merkleTreeWithHistory.insert(i+42).should.be.fulfilled await merkleTreeWithHistory.insert(i+42).should.be.fulfilled
@ -193,8 +187,8 @@ contract('MerkleTreeWithHistory', accounts => {
it.skip('hasher gas', async () => { it.skip('hasher gas', async () => {
levels = 6 levels = 6
zeroValue = 1337 merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, zeroValue) const zeroValue = await merkleTreeWithHistory.zeroValue()
const gas = await merkleTreeWithHistory.hashLeftRight.estimateGas(zeroValue, zeroValue) const gas = await merkleTreeWithHistory.hashLeftRight.estimateGas(zeroValue, zeroValue)
console.log('gas', gas - 21000) console.log('gas', gas - 21000)
@ -208,7 +202,6 @@ contract('MerkleTreeWithHistory', accounts => {
hasher = new hasherImpl() hasher = new hasherImpl()
tree = new MerkleTree( tree = new MerkleTree(
levels, levels,
zeroValue,
null, null,
prefix, prefix,
null, null,