remove mimc and snarkjs dependency

This commit is contained in:
poma 2021-09-14 22:48:57 +03:00
parent 3e31a2f875
commit a0c40b2138
No known key found for this signature in database
GPG Key ID: BA20CB01FE165657
5 changed files with 196 additions and 3702 deletions

3866
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -1,6 +1,6 @@
{ {
"name": "fixed-merkle-tree", "name": "fixed-merkle-tree",
"version": "0.6.0", "version": "1.0.0",
"description": "Fixed depth merkle tree implementation with sequential inserts", "description": "Fixed depth merkle tree implementation with sequential inserts",
"repository": "https://github.com/tornadocash/fixed-merkle-tree.git", "repository": "https://github.com/tornadocash/fixed-merkle-tree.git",
"main": "src/merkleTree.js", "main": "src/merkleTree.js",
@ -18,10 +18,6 @@
"files": [ "files": [
"src/*" "src/*"
], ],
"dependencies": {
"snarkjs": "git+https://github.com/tornadocash/snarkjs.git#869181cfaf7526fe8972073d31655493a04326d5",
"circomlib": "git+https://github.com/tornadocash/circomlib.git#5beb6aee94923052faeecea40135d45b6ce6172c"
},
"devDependencies": { "devDependencies": {
"babel-eslint": "^10.1.0", "babel-eslint": "^10.1.0",
"chai": "^4.2.0", "chai": "^4.2.0",

@ -1,9 +1,5 @@
// keccak256("tornado") % BN254_FIELD_SIZE const defaultHash = require('./sha256')
const DEFAULT_ZERO = '21663839004416932945382355908790599225266501822907911457504978515578255421292'
const defaultHash = require('./mimc')
// todo ensure consistent types in tree and inserted elements? // todo ensure consistent types in tree and inserted elements?
// todo make sha3 default hasher (and update tests) to get rid of mimc/snarkjs/circomlib dependency
/** /**
* @callback hashFunction * @callback hashFunction
@ -22,7 +18,7 @@ class MerkleTree {
* @param {hashFunction} [options.hashFunction] Function used to hash 2 leaves * @param {hashFunction} [options.hashFunction] Function used to hash 2 leaves
* @param [options.zeroElement] Value for non-existent leaves * @param [options.zeroElement] Value for non-existent leaves
*/ */
constructor(levels, elements = [], { hashFunction, zeroElement = DEFAULT_ZERO } = {}) { constructor(levels, elements = [], { hashFunction, zeroElement = 0 } = {}) {
this.levels = levels this.levels = levels
this.capacity = 2 ** levels this.capacity = 2 ** levels
if (elements.length > this.capacity) { if (elements.length > this.capacity) {

@ -1,3 +0,0 @@
const { mimcsponge } = require('circomlib')
const { bigInt } = require('snarkjs')
module.exports = (left, right) => mimcsponge.multiHash([bigInt(left), bigInt(right)]).toString()

15
src/sha256.js Normal file

@ -0,0 +1,15 @@
const crypto = require('crypto')
function toBuffer256(number) {
return Buffer.from(number.toString(16).padStart(512, '0'), 'hex')
}
function sha256(left, right) {
return crypto
.createHash('sha256')
.update(toBuffer256(left))
.update(toBuffer256(right))
.digest('hex')
}
module.exports = sha256