5 Commits
123 ... 0.6.1

Author SHA1 Message Date
d96a6adc1a Prepare package to self-host in Gitea npm registry 2023-09-12 03:05:43 -07:00
Danil Kovtonyuk
e3c54ea818 fix: bulk insert empty elements 2022-02-17 01:05:45 +10:00
Roman Semenov
d5816e7cf4 Merge pull request #1 from tornadocash/bulk-insert
Bulk insert
2021-09-15 20:09:08 +03:00
poma
3e31a2f875 efficient bulk insert
(cherry picked from commit c83a4a4dc0)
2021-09-15 20:02:06 +03:00
poma
c775fc35f4 minor refactor 2021-09-15 20:01:17 +03:00
6 changed files with 2149 additions and 2214 deletions

0
.npmrc Normal file
View File

1
.nvmrc Normal file
View File

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

4340
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +1,8 @@
{
"name": "fixed-merkle-tree",
"version": "0.6.0",
"name": "@tornado/fixed-merkle-tree",
"version": "0.6.1",
"description": "Fixed depth merkle tree implementation with sequential inserts",
"repository": "https://github.com/tornadocash/fixed-merkle-tree.git",
"repository": "https://git.tornado.ws/tornado-packages/fixed-merkle-tree.git",
"main": "src/merkleTree.js",
"scripts": {
"test": "mocha",
@@ -19,8 +19,8 @@
"src/*"
],
"dependencies": {
"snarkjs": "git+https://github.com/tornadocash/snarkjs.git#869181cfaf7526fe8972073d31655493a04326d5",
"circomlib": "git+https://github.com/tornadocash/circomlib.git#5beb6aee94923052faeecea40135d45b6ce6172c"
"@tornado/circomlib": "0.0.21",
"@tornado/snarkjs": "0.1.20"
},
"devDependencies": {
"babel-eslint": "^10.1.0",

View File

@@ -22,13 +22,13 @@ class MerkleTree {
* @param {hashFunction} [options.hashFunction] Function used to hash 2 leaves
* @param [options.zeroElement] Value for non-existent leaves
*/
constructor(levels, elements = [], { hashFunction = defaultHash, zeroElement = DEFAULT_ZERO } = {}) {
constructor(levels, elements = [], { hashFunction, zeroElement = DEFAULT_ZERO } = {}) {
this.levels = levels
this.capacity = 2 ** levels
if (elements.length > this.capacity) {
throw new Error('Tree is full')
}
this._hash = hashFunction
this._hash = hashFunction || defaultHash
this.zeroElement = zeroElement
this._zeros = []
this._zeros[0] = zeroElement
@@ -78,6 +78,10 @@ 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 +1,3 @@
const { mimcsponge } = require('circomlib')
const { bigInt } = require('snarkjs')
const { mimcsponge } = require('@tornado/circomlib')
const { bigInt } = require('@tornado/snarkjs')
module.exports = (left, right) => mimcsponge.multiHash([bigInt(left), bigInt(right)]).toString()