1 Commits

Author SHA1 Message Date
poma
a0c40b2138 remove mimc and snarkjs dependency 2021-09-15 20:07:10 +03:00
7 changed files with 868 additions and 4309 deletions

0
.npmrc
View File

1
.nvmrc
View File

@@ -1 +0,0 @@
v14.21.3

5136
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +1,8 @@
{
"name": "@tornado/fixed-merkle-tree",
"version": "0.6.1",
"name": "fixed-merkle-tree",
"version": "1.0.0",
"description": "Fixed depth merkle tree implementation with sequential inserts",
"repository": "https://git.tornado.ws/tornado-packages/fixed-merkle-tree.git",
"repository": "https://github.com/tornadocash/fixed-merkle-tree.git",
"main": "src/merkleTree.js",
"scripts": {
"test": "mocha",
@@ -18,10 +18,6 @@
"files": [
"src/*"
],
"dependencies": {
"@tornado/circomlib": "0.0.21",
"@tornado/snarkjs": "0.1.20"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
"chai": "^4.2.0",

View File

@@ -1,9 +1,5 @@
// keccak256("tornado") % BN254_FIELD_SIZE
const DEFAULT_ZERO = '21663839004416932945382355908790599225266501822907911457504978515578255421292'
const defaultHash = require('./mimc')
const defaultHash = require('./sha256')
// 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
@@ -22,7 +18,7 @@ class MerkleTree {
* @param {hashFunction} [options.hashFunction] Function used to hash 2 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.capacity = 2 ** levels
if (elements.length > this.capacity) {
@@ -78,10 +74,6 @@ class MerkleTree {
* @param {Array} elements Elements to insert
*/
bulkInsert(elements) {
if (!elements.length) {
return
}
if (this._layers[0].length + elements.length > this.capacity) {
throw new Error('Tree is full')
}

View File

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

15
src/sha256.js Normal file
View 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