2 Commits
0.3.4 ... 0.4.0

Author SHA1 Message Date
c79653ccba Update dependecies & change package name for self-hosting in tornado scope 2023-09-18 01:35:33 -07:00
poma
96e254a46b serialization 2020-09-23 19:22:14 +03:00
6 changed files with 6320 additions and 144 deletions

1
.npmrc Normal file
View File

@@ -0,0 +1 @@
@tornado:registry=https://git.tornado.ws/api/packages/tornado-packages/npm/

6399
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +1,12 @@
{
"name": "fixed-merkle-tree",
"version": "0.3.4",
"name": "@tornado/fixed-merkle-tree",
"version": "0.4.0",
"description": "Fixed depth merkle tree implementation with sequential inserts",
"main": "src/merkleTree.js",
"repository": {
"type": "git",
"url": "https://git.tornado.ws/tornado-packages/fixed-merkle-tree.git"
},
"scripts": {
"test": "mocha",
"lint": "eslint ."
@@ -18,8 +22,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

@@ -152,6 +152,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,
capacity: this.capacity,
zeroElement: this.zeroElement,
_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
return instance
}
}
module.exports = MerkleTree

View File

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

View File

@@ -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())
})
})
})