use mimc from circomlibjs

This commit is contained in:
smart_ex 2022-03-11 00:12:55 +10:00
parent ad634a6b3a
commit e9f123a8b2
4 changed files with 5527 additions and 4196 deletions

9710
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -39,6 +39,6 @@
"mocha": "^9.2.1", "mocha": "^9.2.1",
"ts-mocha": "^9.0.2", "ts-mocha": "^9.0.2",
"typescript": "^4.5.5", "typescript": "^4.5.5",
"circomlib": "git+https://github.com/tornadocash/circomlib.git#5beb6aee94923052faeecea40135d45b6ce6172c" "circomlibjs": "^0.1.1"
} }
} }

View File

@ -1,16 +1,20 @@
import { MerkleTree, TreeEdge } from '../src' import { MerkleTree, TreeEdge } from '../src'
import { assert, should } from 'chai' import { assert, should } from 'chai'
import { mimcsponge } from 'circomlib' import { buildMimcSponge } from 'circomlibjs'
import { createHash } from 'crypto' import { createHash } from 'crypto'
import { it } from 'mocha' import { it } from 'mocha'
const sha256Hash = (left, right) => createHash('sha256').update(`${left}${right}`).digest('hex') const sha256Hash = (left, right) => createHash('sha256').update(`${left}${right}`).digest('hex')
const mimcHash = (left, right) => mimcsponge.multiHash([BigInt(left), BigInt(right)]).toString()
const ZERO_ELEMENT = '21663839004416932945382355908790599225266501822907911457504978515578255421292' const ZERO_ELEMENT = '21663839004416932945382355908790599225266501822907911457504978515578255421292'
describe('MerkleTree', () => { describe('MerkleTree', () => {
describe('#constructor', () => { describe('#constructor', () => {
let mimcSponge
before(async () => {
mimcSponge = await buildMimcSponge()
})
it('should have correct zero root', () => { it('should have correct zero root', () => {
const tree = new MerkleTree(10, []) const tree = new MerkleTree(10, [])
@ -47,6 +51,7 @@ describe('MerkleTree', () => {
}) })
it('should work with mimc hash function and zero element', () => { it('should work with mimc hash function and zero element', () => {
const mimcHash = (left, right) => mimcSponge.F.toString(mimcSponge.multiHash([BigInt(left), BigInt(right)]))
const tree = new MerkleTree(10, [1, 2, 3], { hashFunction: mimcHash, zeroElement: ZERO_ELEMENT }) const tree = new MerkleTree(10, [1, 2, 3], { hashFunction: mimcHash, zeroElement: ZERO_ELEMENT })
should().equal(tree.root, '13605252518346649016266481317890801910232739395710162921320863289825142055129') should().equal(tree.root, '13605252518346649016266481317890801910232739395710162921320863289825142055129')
}) })

View File

@ -3,7 +3,7 @@
"lib": [ "lib": [
"es2019" "es2019"
], ],
"target": "es2018", "target": "es2020",
"module": "CommonJS", "module": "CommonJS",
"moduleResolution": "Node", "moduleResolution": "Node",
"outDir": "./lib", "outDir": "./lib",