Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c89146c3e5 | |||
|
|
29021a8028 | ||
|
|
4a63b3b7fb | ||
|
|
acc7e53b01 | ||
|
|
823545596e | ||
|
|
4f6ff9b777 | ||
|
|
96e254a46b | ||
|
|
53cb08d9b9 | ||
|
|
0eb38e0b2d | ||
|
|
1bc8dfa338 |
@@ -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 }}
|
||||
1
.npmrc
Normal file
1
.npmrc
Normal file
@@ -0,0 +1 @@
|
||||
@tornado:registry=https://git.tornado.ws/api/packages/tornado-packages/npm/
|
||||
7
.prettierrc
Normal file
7
.prettierrc
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"semi": false,
|
||||
"arrowParens": "always",
|
||||
"singleQuote": true,
|
||||
"printWidth": 110,
|
||||
"trailingComma": "all"
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
# Merkle Tree [](https://github.com/tornadocash/fixed-merkle-tree/actions) [](https://www.npmjs.com/package/fixed-merkle-tree)
|
||||
# Merkle Tree [](https://github.com/tornadocash/fixed-merkle-tree/actions) [](https://www.npmjs.com/package/fixed-merkle-tree)
|
||||
|
||||
This is a fixed depth merkle tree implementation with sequential inserts
|
||||
|
||||
|
||||
4627
package-lock.json
generated
4627
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
19
package.json
19
package.json
@@ -1,19 +1,26 @@
|
||||
{
|
||||
"name": "fixed-merkle-tree",
|
||||
"version": "0.3.2",
|
||||
"name": "@tornado/fixed-merkle-tree",
|
||||
"version": "0.5.1",
|
||||
"description": "Fixed depth merkle tree implementation with sequential inserts",
|
||||
"main": "src/merkleTree.js",
|
||||
"repository": "https://git.tornado.ws/tornado-packages/fixed-merkle-tree.git",
|
||||
"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"
|
||||
"@tornado/circomlib": "0.0.21",
|
||||
"@tornado/snarkjs": "0.1.20"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-eslint": "^10.1.0",
|
||||
|
||||
@@ -24,7 +24,7 @@ class MerkleTree {
|
||||
*/
|
||||
constructor(levels, elements = [], { hashFunction, zeroElement = DEFAULT_ZERO } = {}) {
|
||||
this.levels = levels
|
||||
this.capacity = 1 << levels
|
||||
this.capacity = 2 ** levels
|
||||
this.zeroElement = zeroElement
|
||||
this._hash = hashFunction || defaultHash
|
||||
if (elements.length > this.capacity) {
|
||||
@@ -47,9 +47,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],
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -100,9 +100,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 +120,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 {
|
||||
@@ -152,6 +151,36 @@ class MerkleTree {
|
||||
elements() {
|
||||
return this._layers[0].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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -202,4 +202,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())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user