fixed-merkle-tree/src/merkleTree.js

150 lines
4.2 KiB
JavaScript
Raw Normal View History

2020-07-31 03:00:33 +02:00
// keccak256("tornado") % BN254_FIELD_SIZE
const DEFAULT_ZERO = '21663839004416932945382355908790599225266501822907911457504978515578255421292'
const defaultHash = require('./mimc')
// todo ensure consistent types in tree and inserted elements?
// todo make sha3 default hasher (and update tests) to get rid of mimc/snarkjs/circomlib dependency
2020-07-31 06:12:45 +02:00
/**
* @callback hashFunction
* @param left Left leaf
* @param right Right leaf
*/
/**
* Merkle tree
*/
2020-07-31 03:00:33 +02:00
class MerkleTree {
2020-07-31 06:12:45 +02:00
/**
* Constructor
* @param {number} levels Number of levels in the tree
* @param {Array} [elements] Initial elements
* @param {Object} options
* @param {hashFunction} [options.hashFunction] Function used to hash 2 leaves
* @param [options.zeroElement] Value for non-existent leaves
2020-07-31 06:12:45 +02:00
*/
constructor(levels, elements = [], { hashFunction, zeroElement = DEFAULT_ZERO } = {}) {
2020-07-31 03:00:33 +02:00
this.levels = levels
this.capacity = 2 << levels
this.zeroElement = zeroElement
2020-07-31 14:02:01 +02:00
this._hash = hashFunction || defaultHash
2020-07-31 03:00:33 +02:00
this._zeros = []
this._layers = []
this._layers[0] = elements
this._zeros[0] = this.zeroElement
for (let i = 1; i <= levels; i++) {
this._zeros[i] = this._hash(this._zeros[i - 1], this._zeros[i - 1])
}
this._rebuild()
}
_rebuild() {
for (let level = 1; level <= this.levels; level++) {
this._layers[level] = []
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],
2020-07-31 14:02:01 +02:00
i * 2 + 1 < this._layers[level - 1].length ?
this._layers[level - 1][i * 2 + 1] :
this._zeros[level - 1],
2020-07-31 03:00:33 +02:00
)
}
}
}
2020-07-31 06:12:45 +02:00
/**
* Get tree root
* @returns {*}
*/
2020-07-31 03:00:33 +02:00
root() {
2020-07-31 14:02:01 +02:00
return this._layers[this.levels].length > 0 ? this._layers[this.levels][0] : this._zeros[this.levels]
2020-07-31 03:00:33 +02:00
}
2020-07-31 06:12:45 +02:00
/**
* Insert new element into the tree
* @param element Element to insert
*/
2020-07-31 03:00:33 +02:00
insert(element) {
if (this._layers[0].length >= this.capacity) {
throw new Error('Tree is full')
}
this.update(this._layers[0].length, element)
}
2020-07-31 06:12:45 +02:00
/**
* Insert multiple elements into the tree. Tree will be fully rebuilt during this operation.
* @param {Array} elements Elements to insert
*/
2020-07-31 03:00:33 +02:00
bulkInsert(elements) {
2020-07-31 06:12:45 +02:00
if (this._layers[0].length + elements.length > this.capacity) {
2020-07-31 03:00:33 +02:00
throw new Error('Tree is full')
}
this._layers[0].push(...elements)
this._rebuild()
}
2020-07-31 06:12:45 +02:00
/**
* Change an element in the tree
* @param {number} index Index of element to change
* @param element Updated element value
*/
2020-07-31 03:00:33 +02:00
update(index, element) {
if (index < 0 || index > this._layers[0].length || index >= this.capacity) {
throw new Error('Insert index out of bounds: ' + index)
}
this._layers[0][index] = element
for (let level = 1; level <= this.levels; level++) {
index >>= 1
this._layers[level][index] = this._hash(
this._layers[level - 1][index * 2],
2020-07-31 14:02:01 +02:00
index * 2 + 1 < this._layers[level - 1].length ?
this._layers[level - 1][index * 2 + 1] :
this._zeros[level - 1],
2020-07-31 03:00:33 +02:00
)
}
}
2020-07-31 06:12:45 +02:00
/**
* Get merkle proof for a leaf
* @param index Leaf index to generate proof for
* @returns {{pathElements: Object[], pathIndex: number[]}} An object containing adjacent elements and left-right index
*/
2020-07-31 03:00:33 +02:00
proof(index) {
if (index < 0 || index >= this._layers[0].length) {
throw new Error('Index out of bounds: ' + index)
}
const pathElements = []
const pathIndices = []
2020-07-31 03:00:33 +02:00
for (let level = 0; level < this.levels; level++) {
pathIndices[level] = index % 2
2020-07-31 14:02:01 +02:00
pathElements[level] = (index ^ 1) < this._layers[level].length ?
this._layers[level][index ^ 1] :
this._zeros[level]
2020-07-31 03:00:33 +02:00
index >>= 1
}
return {
pathElements,
pathIndices,
2020-07-31 03:00:33 +02:00
}
}
2020-07-31 06:12:45 +02:00
/**
* Find an element in the tree
* @param element An element to find
* @returns {number} Index if element is found, otherwise -1
*/
2020-07-31 03:00:33 +02:00
indexOf(element) {
return this._layers[0].indexOf(element)
}
/**
* Returns a copy of non-zero tree elements
* @returns {Object[]}
*/
elements() {
return this._layers[0].slice()
}
2020-07-31 03:00:33 +02:00
}
module.exports = MerkleTree