From 71b767ade15991f32325ebb86aa9aa2ba9c52a7d Mon Sep 17 00:00:00 2001 From: poma Date: Fri, 4 Oct 2019 15:12:22 +0300 Subject: [PATCH] rename mimc mentions to a generic hasher --- contracts/MerkleTreeWithHistory.sol | 10 +++++----- lib/MerkleTree.js | 4 ++-- .../{2_deploy_mimc.js => 2_deploy_hasher.js} | 14 +++++++------- migrations/4_deploy_eth_mixer.js | 6 +++--- migrations/5_deploy_erc20_mixer.js | 6 +++--- test/MerkleTreeWithHistory.test.js | 16 ++++++++-------- 6 files changed, 28 insertions(+), 28 deletions(-) rename migrations/{2_deploy_mimc.js => 2_deploy_hasher.js} (54%) diff --git a/contracts/MerkleTreeWithHistory.sol b/contracts/MerkleTreeWithHistory.sol index f75311a..9d2f189 100644 --- a/contracts/MerkleTreeWithHistory.sol +++ b/contracts/MerkleTreeWithHistory.sol @@ -11,7 +11,7 @@ pragma solidity ^0.5.8; -library MiMC { +library Hasher { function MiMCSponge(uint256 in_xL, uint256 in_xR, uint256 in_k) public pure returns (uint256 xL, uint256 xR); } @@ -42,18 +42,18 @@ contract MerkleTreeWithHistory { _roots[0] = hashLeftRight(_zeros[levels - 1], _zeros[levels - 1]); } - function hashLeftRight(uint256 left, uint256 right) public pure returns (uint256 mimc_hash) { + function hashLeftRight(uint256 left, uint256 right) public pure returns (uint256 hash) { uint256 k = 21888242871839275222246405745257275088548364400416034343698204186575808495617; uint256 R = 0; uint256 C = 0; R = addmod(R, left, k); - (R, C) = MiMC.MiMCSponge(R, C, 0); + (R, C) = Hasher.MiMCSponge(R, C, 0); R = addmod(R, right, k); - (R, C) = MiMC.MiMCSponge(R, C, 0); + (R, C) = Hasher.MiMCSponge(R, C, 0); - mimc_hash = R; + hash = R; } function _insert(uint256 leaf) internal { diff --git a/lib/MerkleTree.js b/lib/MerkleTree.js index b9b134c..f18f6f3 100644 --- a/lib/MerkleTree.js +++ b/lib/MerkleTree.js @@ -1,12 +1,12 @@ const jsStorage = require('./Storage') -const mimcHasher = require('./MiMC') +const hasherImpl = require('./MiMC') class MerkleTree { constructor(n_levels, zero_value, defaultElements, prefix, storage, hasher) { this.prefix = prefix this.storage = storage || new jsStorage() - this.hasher = hasher || new mimcHasher() + this.hasher = hasher || new hasherImpl() this.n_levels = n_levels this.zero_values = [] this.totalElements = 0 diff --git a/migrations/2_deploy_mimc.js b/migrations/2_deploy_hasher.js similarity index 54% rename from migrations/2_deploy_mimc.js rename to migrations/2_deploy_hasher.js index 6c0b797..39df3c3 100644 --- a/migrations/2_deploy_mimc.js +++ b/migrations/2_deploy_hasher.js @@ -1,7 +1,7 @@ /* global artifacts */ const path = require('path') -const mimcGenContract = require('circomlib/src/mimcsponge_gencontract.js') +const genContract = require('circomlib/src/mimcsponge_gencontract.js') const Artifactor = require('truffle-artifactor') const SEED = 'mimcsponge' @@ -11,14 +11,14 @@ module.exports = function(deployer) { return deployer.then( async () => { const contractsDir = path.join(__dirname, '..', 'build/contracts') let artifactor = new Artifactor(contractsDir) - let mimcContractName = 'MiMC' + let contractName = 'Hasher' await artifactor.save({ - contractName: mimcContractName, - abi: mimcGenContract.abi, - unlinked_binary: mimcGenContract.createCode(SEED, 220), + contractName, + abi: genContract.abi, + unlinked_binary: genContract.createCode(SEED, 220), }).then(async () => { - const MiMC = artifacts.require(mimcContractName) - await deployer.deploy(MiMC) + const hasherContract = artifacts.require(contractName) + await deployer.deploy(hasherContract) }) }) } diff --git a/migrations/4_deploy_eth_mixer.js b/migrations/4_deploy_eth_mixer.js index 3626019..2406da3 100644 --- a/migrations/4_deploy_eth_mixer.js +++ b/migrations/4_deploy_eth_mixer.js @@ -2,15 +2,15 @@ require('dotenv').config({ path: '../.env' }) const ETHMixer = artifacts.require('ETHMixer') const Verifier = artifacts.require('Verifier') -const MiMC = artifacts.require('MiMC') +const hasherContract = artifacts.require('hasher') module.exports = function(deployer, network, accounts) { return deployer.then(async () => { const { MERKLE_TREE_HEIGHT, ETH_AMOUNT, EMPTY_ELEMENT } = process.env const verifier = await Verifier.deployed() - const miMC = await MiMC.deployed() - await ETHMixer.link(MiMC, miMC.address) + const hasherInstance = await hasherContract.deployed() + await ETHMixer.link(hasherContract, hasherInstance.address) const mixer = await deployer.deploy(ETHMixer, verifier.address, ETH_AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT, accounts[0]) console.log('ETHMixer\'s address ', mixer.address) }) diff --git a/migrations/5_deploy_erc20_mixer.js b/migrations/5_deploy_erc20_mixer.js index 8ee907c..b2eb21b 100644 --- a/migrations/5_deploy_erc20_mixer.js +++ b/migrations/5_deploy_erc20_mixer.js @@ -2,7 +2,7 @@ require('dotenv').config({ path: '../.env' }) const ERC20Mixer = artifacts.require('ERC20Mixer') const Verifier = artifacts.require('Verifier') -const MiMC = artifacts.require('MiMC') +const hasherContract = artifacts.require('hasher') const ERC20Mock = artifacts.require('ERC20Mock') @@ -10,8 +10,8 @@ module.exports = function(deployer, network, accounts) { return deployer.then(async () => { const { MERKLE_TREE_HEIGHT, ETH_AMOUNT, EMPTY_ELEMENT, ERC20_TOKEN, TOKEN_AMOUNT } = process.env const verifier = await Verifier.deployed() - const miMC = await MiMC.deployed() - await ERC20Mixer.link(MiMC, miMC.address) + const hasherInstance = await hasherContract.deployed() + await ERC20Mixer.link(hasherContract, hasherInstance.address) let token = ERC20_TOKEN if(token === '') { const tokenInstance = await deployer.deploy(ERC20Mock) diff --git a/test/MerkleTreeWithHistory.test.js b/test/MerkleTreeWithHistory.test.js index 9b39516..64c48d2 100644 --- a/test/MerkleTreeWithHistory.test.js +++ b/test/MerkleTreeWithHistory.test.js @@ -7,10 +7,10 @@ require('chai') const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper') const MerkleTreeWithHistory = artifacts.require('./MerkleTreeWithHistoryMock.sol') -const MiMC = artifacts.require('./MiMC.sol') +const hasherContract = artifacts.require('./Hasher.sol') const MerkleTree = require('../lib/MerkleTree') -const MimcHasher = require('../lib/MiMC') +const hasherImpl = require('../lib/MiMC') const { ETH_AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT } = process.env @@ -25,7 +25,7 @@ function BNArrayToStringArray(array) { contract('MerkleTreeWithHistory', accounts => { let merkleTreeWithHistory - let miMC + let hasherInstance let levels = MERKLE_TREE_HEIGHT || 16 let zeroValue = EMPTY_ELEMENT || 1337 const sender = accounts[0] @@ -43,8 +43,8 @@ contract('MerkleTreeWithHistory', accounts => { null, prefix, ) - miMC = await MiMC.deployed() - await MerkleTreeWithHistory.link(MiMC, miMC.address) + hasherInstance = await hasherContract.deployed() + await MerkleTreeWithHistory.link(hasherContract, hasherInstance.address) merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, zeroValue) snapshotId = await takeSnapshot() }) @@ -67,7 +67,7 @@ contract('MerkleTreeWithHistory', accounts => { }) it('tests insert', async () => { - hasher = new MimcHasher() + hasher = new hasherImpl() tree = new MerkleTree( 2, zeroValue, @@ -191,7 +191,7 @@ contract('MerkleTreeWithHistory', accounts => { error.reason.should.be.equal('Merkle tree is full. No more leafs can be added') }) - it.skip('mimc gas', async () => { + it.skip('hasher gas', async () => { levels = 6 zeroValue = 1337 merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, zeroValue) @@ -205,7 +205,7 @@ contract('MerkleTreeWithHistory', accounts => { await revertSnapshot(snapshotId.result) // eslint-disable-next-line require-atomic-updates snapshotId = await takeSnapshot() - hasher = new MimcHasher() + hasher = new hasherImpl() tree = new MerkleTree( levels, zeroValue,