rename mimc mentions to a generic hasher

This commit is contained in:
poma 2019-10-04 15:12:22 +03:00
parent 6b067f067f
commit 71b767ade1
6 changed files with 28 additions and 28 deletions

View File

@ -11,7 +11,7 @@
pragma solidity ^0.5.8; 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); 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]); _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 k = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
uint256 R = 0; uint256 R = 0;
uint256 C = 0; uint256 C = 0;
R = addmod(R, left, k); 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 = 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 { function _insert(uint256 leaf) internal {

View File

@ -1,12 +1,12 @@
const jsStorage = require('./Storage') const jsStorage = require('./Storage')
const mimcHasher = require('./MiMC') const hasherImpl = require('./MiMC')
class MerkleTree { class MerkleTree {
constructor(n_levels, zero_value, defaultElements, prefix, storage, hasher) { constructor(n_levels, zero_value, defaultElements, prefix, storage, hasher) {
this.prefix = prefix this.prefix = prefix
this.storage = storage || new jsStorage() this.storage = storage || new jsStorage()
this.hasher = hasher || new mimcHasher() this.hasher = hasher || new hasherImpl()
this.n_levels = n_levels this.n_levels = n_levels
this.zero_values = [] this.zero_values = []
this.totalElements = 0 this.totalElements = 0

View File

@ -1,7 +1,7 @@
/* global artifacts */ /* global artifacts */
const path = require('path') 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 Artifactor = require('truffle-artifactor')
const SEED = 'mimcsponge' const SEED = 'mimcsponge'
@ -11,14 +11,14 @@ module.exports = function(deployer) {
return deployer.then( async () => { return deployer.then( async () => {
const contractsDir = path.join(__dirname, '..', 'build/contracts') const contractsDir = path.join(__dirname, '..', 'build/contracts')
let artifactor = new Artifactor(contractsDir) let artifactor = new Artifactor(contractsDir)
let mimcContractName = 'MiMC' let contractName = 'Hasher'
await artifactor.save({ await artifactor.save({
contractName: mimcContractName, contractName,
abi: mimcGenContract.abi, abi: genContract.abi,
unlinked_binary: mimcGenContract.createCode(SEED, 220), unlinked_binary: genContract.createCode(SEED, 220),
}).then(async () => { }).then(async () => {
const MiMC = artifacts.require(mimcContractName) const hasherContract = artifacts.require(contractName)
await deployer.deploy(MiMC) await deployer.deploy(hasherContract)
}) })
}) })
} }

View File

@ -2,15 +2,15 @@
require('dotenv').config({ path: '../.env' }) require('dotenv').config({ path: '../.env' })
const ETHMixer = artifacts.require('ETHMixer') const ETHMixer = artifacts.require('ETHMixer')
const Verifier = artifacts.require('Verifier') const Verifier = artifacts.require('Verifier')
const MiMC = artifacts.require('MiMC') 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, EMPTY_ELEMENT } = process.env
const verifier = await Verifier.deployed() const verifier = await Verifier.deployed()
const miMC = await MiMC.deployed() const hasherInstance = await hasherContract.deployed()
await ETHMixer.link(MiMC, miMC.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, EMPTY_ELEMENT, accounts[0])
console.log('ETHMixer\'s address ', mixer.address) console.log('ETHMixer\'s address ', mixer.address)
}) })

View File

@ -2,7 +2,7 @@
require('dotenv').config({ path: '../.env' }) require('dotenv').config({ path: '../.env' })
const ERC20Mixer = artifacts.require('ERC20Mixer') const ERC20Mixer = artifacts.require('ERC20Mixer')
const Verifier = artifacts.require('Verifier') const Verifier = artifacts.require('Verifier')
const MiMC = artifacts.require('MiMC') const hasherContract = artifacts.require('hasher')
const ERC20Mock = artifacts.require('ERC20Mock') const ERC20Mock = artifacts.require('ERC20Mock')
@ -10,8 +10,8 @@ module.exports = function(deployer, network, accounts) {
return deployer.then(async () => { return deployer.then(async () => {
const { MERKLE_TREE_HEIGHT, ETH_AMOUNT, EMPTY_ELEMENT, ERC20_TOKEN, TOKEN_AMOUNT } = process.env const { MERKLE_TREE_HEIGHT, ETH_AMOUNT, EMPTY_ELEMENT, ERC20_TOKEN, TOKEN_AMOUNT } = process.env
const verifier = await Verifier.deployed() const verifier = await Verifier.deployed()
const miMC = await MiMC.deployed() const hasherInstance = await hasherContract.deployed()
await ERC20Mixer.link(MiMC, miMC.address) await ERC20Mixer.link(hasherContract, hasherInstance.address)
let token = ERC20_TOKEN let token = ERC20_TOKEN
if(token === '') { if(token === '') {
const tokenInstance = await deployer.deploy(ERC20Mock) const tokenInstance = await deployer.deploy(ERC20Mock)

View File

@ -7,10 +7,10 @@ require('chai')
const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper') const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper')
const MerkleTreeWithHistory = artifacts.require('./MerkleTreeWithHistoryMock.sol') const MerkleTreeWithHistory = artifacts.require('./MerkleTreeWithHistoryMock.sol')
const MiMC = artifacts.require('./MiMC.sol') const hasherContract = artifacts.require('./Hasher.sol')
const MerkleTree = require('../lib/MerkleTree') 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 const { ETH_AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT } = process.env
@ -25,7 +25,7 @@ function BNArrayToStringArray(array) {
contract('MerkleTreeWithHistory', accounts => { contract('MerkleTreeWithHistory', accounts => {
let merkleTreeWithHistory let merkleTreeWithHistory
let miMC let hasherInstance
let levels = MERKLE_TREE_HEIGHT || 16 let levels = MERKLE_TREE_HEIGHT || 16
let zeroValue = EMPTY_ELEMENT || 1337 let zeroValue = EMPTY_ELEMENT || 1337
const sender = accounts[0] const sender = accounts[0]
@ -43,8 +43,8 @@ contract('MerkleTreeWithHistory', accounts => {
null, null,
prefix, prefix,
) )
miMC = await MiMC.deployed() hasherInstance = await hasherContract.deployed()
await MerkleTreeWithHistory.link(MiMC, miMC.address) await MerkleTreeWithHistory.link(hasherContract, hasherInstance.address)
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, zeroValue) merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, zeroValue)
snapshotId = await takeSnapshot() snapshotId = await takeSnapshot()
}) })
@ -67,7 +67,7 @@ contract('MerkleTreeWithHistory', accounts => {
}) })
it('tests insert', async () => { it('tests insert', async () => {
hasher = new MimcHasher() hasher = new hasherImpl()
tree = new MerkleTree( tree = new MerkleTree(
2, 2,
zeroValue, zeroValue,
@ -191,7 +191,7 @@ contract('MerkleTreeWithHistory', accounts => {
error.reason.should.be.equal('Merkle tree is full. No more leafs can be added') 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 levels = 6
zeroValue = 1337 zeroValue = 1337
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, zeroValue) merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, zeroValue)
@ -205,7 +205,7 @@ contract('MerkleTreeWithHistory', accounts => {
await revertSnapshot(snapshotId.result) await revertSnapshot(snapshotId.result)
// eslint-disable-next-line require-atomic-updates // eslint-disable-next-line require-atomic-updates
snapshotId = await takeSnapshot() snapshotId = await takeSnapshot()
hasher = new MimcHasher() hasher = new hasherImpl()
tree = new MerkleTree( tree = new MerkleTree(
levels, levels,
zeroValue, zeroValue,