mirror of
https://github.com/tornadocash/fixed-merkle-tree.git
synced 2024-11-22 01:37:09 +01:00
options object in constructor, rename pathIndices
This commit is contained in:
parent
3aff8f4515
commit
8ec8ffe58b
@ -1,4 +0,0 @@
|
||||
.github
|
||||
.eslintrc
|
||||
.editorconfig
|
||||
test
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "fixed-merkle-tree",
|
||||
"version": "0.1.0",
|
||||
"version": "0.2.0",
|
||||
"description": "Fixed depth merkle tree implementation with sequential inserts",
|
||||
"main": "src/merkleTree.js",
|
||||
"scripts": {
|
||||
@ -10,6 +10,7 @@
|
||||
"keywords": ["merkle", "tree", "merkleTree"],
|
||||
"author": "Roman Semenov <semenov.roma@gmail.com>",
|
||||
"license": "ISC",
|
||||
"files": ["src/*"],
|
||||
"dependencies": {
|
||||
"snarkjs": "git+https://github.com/tornadocash/snarkjs.git#869181cfaf7526fe8972073d31655493a04326d5",
|
||||
"circomlib": "git+https://github.com/tornadocash/circomlib.git#c372f14d324d57339c88451834bf2824e73bbdbc"
|
||||
|
@ -18,10 +18,11 @@ class MerkleTree {
|
||||
* Constructor
|
||||
* @param {number} levels Number of levels in the tree
|
||||
* @param {Array} [elements] Initial elements
|
||||
* @param [zeroElement] Value for non-existent leaves
|
||||
* @param {hashFunction} [hashFunction] Function used to hash 2 leaves
|
||||
* @param {Object} options
|
||||
* @param {hashFunction} [options.hashFunction] Function used to hash 2 leaves
|
||||
* @param [options.zeroElement] Value for non-existent leaves
|
||||
*/
|
||||
constructor(levels, elements = [], zeroElement = DEFAULT_ZERO, hashFunction) {
|
||||
constructor(levels, elements = [], { hashFunction, zeroElement = DEFAULT_ZERO } = {}) {
|
||||
this.levels = levels
|
||||
this.capacity = 2 << levels
|
||||
this.zeroElement = zeroElement
|
||||
@ -113,9 +114,9 @@ class MerkleTree {
|
||||
throw new Error('Index out of bounds: ' + index)
|
||||
}
|
||||
const pathElements = []
|
||||
const pathIndex = []
|
||||
const pathIndices = []
|
||||
for (let level = 0; level < this.levels; level++) {
|
||||
pathIndex[level] = index % 2
|
||||
pathIndices[level] = index % 2
|
||||
pathElements[level] = (index ^ 1) < this._layers[level].length ?
|
||||
this._layers[level][index ^ 1] :
|
||||
this._zeros[level]
|
||||
@ -123,7 +124,7 @@ class MerkleTree {
|
||||
}
|
||||
return {
|
||||
pathElements,
|
||||
pathIndex,
|
||||
pathIndices,
|
||||
}
|
||||
}
|
||||
|
||||
@ -135,6 +136,14 @@ class MerkleTree {
|
||||
indexOf(element) {
|
||||
return this._layers[0].indexOf(element)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of non-zero tree elements
|
||||
* @returns {Object[]}
|
||||
*/
|
||||
elements() {
|
||||
return this._layers[0].slice()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MerkleTree
|
||||
|
@ -89,7 +89,7 @@ describe('MerkleTree', () => {
|
||||
it('should work for even index', () => {
|
||||
const tree = new MerkleTree(10, [1, 2, 3, 4, 5])
|
||||
const proof = tree.proof(2)
|
||||
proof.pathIndex.should.be.deep.equal([0, 1, 0, 0, 0, 0, 0, 0, 0, 0])
|
||||
proof.pathIndices.should.be.deep.equal([0, 1, 0, 0, 0, 0, 0, 0, 0, 0])
|
||||
proof.pathElements.should.be.deep.equal([
|
||||
4,
|
||||
'19814528709687996974327303300007262407299502847885145507292406548098437687919',
|
||||
@ -107,7 +107,7 @@ describe('MerkleTree', () => {
|
||||
it('should work for odd index', () => {
|
||||
const tree = new MerkleTree(10, [1, 2, 3, 4, 5])
|
||||
const proof = tree.proof(3)
|
||||
proof.pathIndex.should.be.deep.equal([1, 1, 0, 0, 0, 0, 0, 0, 0, 0])
|
||||
proof.pathIndices.should.be.deep.equal([1, 1, 0, 0, 0, 0, 0, 0, 0, 0])
|
||||
proof.pathElements.should.be.deep.equal([
|
||||
3,
|
||||
'19814528709687996974327303300007262407299502847885145507292406548098437687919',
|
||||
|
Loading…
Reference in New Issue
Block a user