14 Commits

Author SHA1 Message Date
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
Alexey
29021a8028 fix trailingComma 2021-07-06 13:51:06 +03:00
Alexey
4a63b3b7fb bump 2021-07-06 13:44:44 +03:00
Alexey
acc7e53b01 fix capacity calculation 2021-07-06 13:44:25 +03:00
Alexey
823545596e prettier 2021-07-06 13:44:12 +03:00
poma
4f6ff9b777 simpler serialization 2020-09-25 17:16:01 +03:00
poma
96e254a46b serialization 2020-09-23 19:22:14 +03:00
poma
53cb08d9b9 build failure notification 2020-08-19 11:35:14 +03:00
poma
0eb38e0b2d update dependency 2020-08-19 01:35:12 +03:00
poma
1bc8dfa338 update circomlib 2020-08-16 02:24:55 +03:00
Alexey
3d95212b1d findIndex optimization 2020-08-06 18:04:08 +03:00
Alexey
523094bdc8 add comparator for indexOf 2020-08-06 18:00:13 +03:00
7 changed files with 1218 additions and 769 deletions

View File

@@ -1,4 +1,4 @@
name: Node.js CI
name: build
on:
pull_request:
@@ -16,6 +16,14 @@ jobs:
node-version: 14.7.0
- run: npm ci
- run: npm run test
- name: Telegram Failure Notification
uses: appleboy/telegram-action@0.0.7
if: failure()
with:
message: ❗ Build failed for [${{ github.repository }}](https://github.com/${{ github.repository }}/actions) because of ${{ github.actor }}
format: markdown
to: ${{ secrets.TELEGRAM_CHAT_ID }}
token: ${{ secrets.TELEGRAM_BOT_TOKEN }}
lint:
runs-on: ubuntu-latest
@@ -26,6 +34,14 @@ jobs:
node-version: 14.7.0
- run: npm ci
- run: npm run lint
- name: Telegram Failure Notification
uses: appleboy/telegram-action@0.0.7
if: failure()
with:
message: ❗ Build failed for [${{ github.repository }}](https://github.com/${{ github.repository }}/actions) because of ${{ github.actor }}
format: markdown
to: ${{ secrets.TELEGRAM_CHAT_ID }}
token: ${{ secrets.TELEGRAM_BOT_TOKEN }}
publish:
runs-on: ubuntu-latest
@@ -65,3 +81,12 @@ jobs:
message: 🚀 Published [${{ steps.vars.outputs.repo_name }}](https://github.com/${{ github.repository }}) version [${{ steps.vars.outputs.version }}](https://www.npmjs.com/package/${{ steps.vars.outputs.repo_name }}/v/${{ steps.vars.outputs.version }}) to npm
debug: true
format: markdown
- name: Telegram Failure Notification
uses: appleboy/telegram-action@0.0.7
if: failure()
with:
message: ❗ Failed to publish [${{ steps.vars.outputs.repo_name }}](https://github.com/${{ github.repository }}/actions) because of ${{ env.GITHUB_ACTOR }}
format: markdown
to: ${{ secrets.TELEGRAM_CHAT_ID }}
token: ${{ secrets.TELEGRAM_BOT_TOKEN }}

7
.prettierrc Normal file
View File

@@ -0,0 +1,7 @@
{
"semi": false,
"arrowParens": "always",
"singleQuote": true,
"printWidth": 110,
"trailingComma": "all"
}

View File

@@ -1,4 +1,4 @@
# Merkle Tree [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/tornadocash/fixed-merkle-tree/Node.js%20CI)](https://github.com/tornadocash/fixed-merkle-tree/actions) [![npm](https://img.shields.io/npm/v/fixed-merkle-tree)](https://www.npmjs.com/package/fixed-merkle-tree)
# Merkle Tree [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/tornadocash/fixed-merkle-tree/build)](https://github.com/tornadocash/fixed-merkle-tree/actions) [![npm](https://img.shields.io/npm/v/fixed-merkle-tree)](https://www.npmjs.com/package/fixed-merkle-tree)
This is a fixed depth merkle tree implementation with sequential inserts

1797
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,19 +1,26 @@
{
"name": "fixed-merkle-tree",
"version": "0.3.1",
"version": "0.6.0",
"description": "Fixed depth merkle tree implementation with sequential inserts",
"repository": "https://github.com/tornadocash/fixed-merkle-tree.git",
"main": "src/merkleTree.js",
"scripts": {
"test": "mocha",
"lint": "eslint ."
},
"keywords": ["merkle", "tree", "merkleTree"],
"keywords": [
"merkle",
"tree",
"merkleTree"
],
"author": "Roman Semenov <semenov.roma@gmail.com>",
"license": "ISC",
"files": ["src/*"],
"files": [
"src/*"
],
"dependencies": {
"snarkjs": "git+https://github.com/tornadocash/snarkjs.git#869181cfaf7526fe8972073d31655493a04326d5",
"circomlib": "git+https://github.com/tornadocash/circomlib.git#c372f14d324d57339c88451834bf2824e73bbdbc"
"circomlib": "git+https://github.com/tornadocash/circomlib.git#5beb6aee94923052faeecea40135d45b6ce6172c"
},
"devDependencies": {
"babel-eslint": "^10.1.0",

View File

@@ -24,20 +24,19 @@ class MerkleTree {
*/
constructor(levels, elements = [], { hashFunction, zeroElement = DEFAULT_ZERO } = {}) {
this.levels = levels
this.capacity = 1 << levels
this.zeroElement = zeroElement
this._hash = hashFunction || defaultHash
this.capacity = 2 ** levels
if (elements.length > this.capacity) {
throw new Error('Tree is full')
}
this._hash = hashFunction || defaultHash
this.zeroElement = zeroElement
this._zeros = []
this._layers = []
this._layers[0] = elements
this._zeros[0] = this.zeroElement
this._zeros[0] = zeroElement
for (let i = 1; i <= levels; i++) {
this._zeros[i] = this._hash(this._zeros[i - 1], this._zeros[i - 1])
}
this._layers = []
this._layers[0] = elements.slice()
this._rebuild()
}
@@ -47,9 +46,9 @@ class MerkleTree {
for (let i = 0; i < Math.ceil(this._layers[level - 1].length / 2); i++) {
this._layers[level][i] = this._hash(
this._layers[level - 1][i * 2],
i * 2 + 1 < this._layers[level - 1].length ?
this._layers[level - 1][i * 2 + 1] :
this._zeros[level - 1],
i * 2 + 1 < this._layers[level - 1].length
? this._layers[level - 1][i * 2 + 1]
: this._zeros[level - 1],
)
}
}
@@ -75,15 +74,30 @@ class MerkleTree {
}
/**
* Insert multiple elements into the tree. Tree will be fully rebuilt during this operation.
* Insert multiple elements into the tree.
* @param {Array} elements Elements to insert
*/
bulkInsert(elements) {
if (this._layers[0].length + elements.length > this.capacity) {
throw new Error('Tree is full')
}
this._layers[0].push(...elements)
this._rebuild()
// First we insert all elements except the last one
// updating only full subtree hashes (all layers where inserted element has odd index)
// the last element will update the full path to the root making the tree consistent again
for (let i = 0; i < elements.length - 1; i++) {
this._layers[0].push(elements[i])
let level = 0
let index = this._layers[0].length - 1
while (index % 2 === 1) {
level++
index >>= 1
this._layers[level][index] = this._hash(
this._layers[level - 1][index * 2],
this._layers[level - 1][index * 2 + 1],
)
}
}
this.insert(elements[elements.length - 1])
}
/**
@@ -100,9 +114,9 @@ class MerkleTree {
index >>= 1
this._layers[level][index] = this._hash(
this._layers[level - 1][index * 2],
index * 2 + 1 < this._layers[level - 1].length ?
this._layers[level - 1][index * 2 + 1] :
this._zeros[level - 1],
index * 2 + 1 < this._layers[level - 1].length
? this._layers[level - 1][index * 2 + 1]
: this._zeros[level - 1],
)
}
}
@@ -120,9 +134,8 @@ class MerkleTree {
const pathIndices = []
for (let level = 0; level < this.levels; level++) {
pathIndices[level] = index % 2
pathElements[level] = (index ^ 1) < this._layers[level].length ?
this._layers[level][index ^ 1] :
this._zeros[level]
pathElements[level] =
(index ^ 1) < this._layers[level].length ? this._layers[level][index ^ 1] : this._zeros[level]
index >>= 1
}
return {
@@ -134,10 +147,15 @@ class MerkleTree {
/**
* Find an element in the tree
* @param element An element to find
* @param comparator A function that checks leaf value equality
* @returns {number} Index if element is found, otherwise -1
*/
indexOf(element) {
return this._layers[0].indexOf(element)
indexOf(element, comparator) {
if (comparator) {
return this._layers[0].findIndex((el) => comparator(element, el))
} else {
return this._layers[0].indexOf(element)
}
}
/**
@@ -147,6 +165,44 @@ class MerkleTree {
elements() {
return this._layers[0].slice()
}
/**
* Returns a copy of n-th zero elements array
* @returns {Object[]}
*/
zeros() {
return this._zeros.slice()
}
/**
* Serialize entire tree state including intermediate layers into a plain object
* Deserializing it back will not require to recompute any hashes
* Elements are not converted to a plain type, this is responsibility of the caller
*/
serialize() {
return {
levels: this.levels,
_zeros: this._zeros,
_layers: this._layers,
}
}
/**
* Deserialize data into a MerkleTree instance
* Make sure to provide the same hashFunction as was used in the source tree,
* otherwise the tree state will be invalid
*
* @param data
* @param hashFunction
* @returns {MerkleTree}
*/
static deserialize(data, hashFunction) {
const instance = Object.assign(Object.create(this.prototype), data)
instance._hash = hashFunction || defaultHash
instance.capacity = 2 ** instance.levels
instance.zeroElement = instance._zeros[0]
return instance
}
}
module.exports = MerkleTree

View File

@@ -71,6 +71,32 @@ describe('MerkleTree', () => {
tree.root().should.equal('10132905325673518287563057607527946096399700874345297651940963130460267058606')
})
it('should give the same result as sequental inserts', () => {
const initialArray = [
[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
]
const insertedArray = [
[11],
[11, 12],
[11, 12, 13],
[11, 12, 13, 14],
]
for (const initial of initialArray) {
for (const inserted of insertedArray) {
const tree1 = new MerkleTree(10, initial)
const tree2 = new MerkleTree(10, initial)
tree1.bulkInsert(inserted)
for (const item of inserted) {
tree2.insert(item)
}
tree1.root().should.equal(tree2.root())
}
}
}).timeout(10000)
it('should work with max elements', () => {
const tree = new MerkleTree(2, [1, 2])
tree.bulkInsert([3, 4])
@@ -202,4 +228,19 @@ describe('MerkleTree', () => {
])
})
})
describe('#serialize', () => {
it('should work', () => {
const src = new MerkleTree(10, [1, 2, 3])
const data = src.serialize()
const dst = MerkleTree.deserialize(data)
src.root().should.equal(dst.root())
src.insert(10)
dst.insert(10)
src.root().should.equal(dst.root())
})
})
})