2019-07-16 13:27:20 -04:00
|
|
|
const jsStorage = require('./Storage')
|
2019-10-04 08:12:22 -04:00
|
|
|
const hasherImpl = require('./MiMC')
|
2019-07-11 06:38:22 -04:00
|
|
|
|
|
|
|
class MerkleTree {
|
|
|
|
|
2019-11-02 08:35:22 -04:00
|
|
|
constructor(n_levels, defaultElements, prefix, storage, hasher) {
|
2019-07-16 13:27:20 -04:00
|
|
|
this.prefix = prefix
|
|
|
|
this.storage = storage || new jsStorage()
|
2019-10-04 08:12:22 -04:00
|
|
|
this.hasher = hasher || new hasherImpl()
|
2019-07-16 13:27:20 -04:00
|
|
|
this.n_levels = n_levels
|
|
|
|
this.zero_values = []
|
|
|
|
this.totalElements = 0
|
|
|
|
|
2019-11-15 03:42:48 -05:00
|
|
|
let current_zero_value = '21663839004416932945382355908790599225266501822907911457504978515578255421292'
|
2019-07-16 13:27:20 -04:00
|
|
|
this.zero_values.push(current_zero_value)
|
2019-07-11 06:38:22 -04:00
|
|
|
for (let i = 0; i < n_levels; i++) {
|
2019-07-16 13:27:20 -04:00
|
|
|
current_zero_value = this.hasher.hash(i, current_zero_value, current_zero_value)
|
2019-07-11 06:38:22 -04:00
|
|
|
this.zero_values.push(
|
|
|
|
current_zero_value.toString(),
|
2019-07-16 13:27:20 -04:00
|
|
|
)
|
2019-07-11 06:38:22 -04:00
|
|
|
}
|
2019-07-11 10:04:36 -04:00
|
|
|
if (defaultElements) {
|
2019-07-16 13:27:20 -04:00
|
|
|
let level = 0
|
|
|
|
this.totalElements = defaultElements.length
|
|
|
|
defaultElements.forEach((element, i) => {
|
|
|
|
this.storage.put(MerkleTree.index_to_key(prefix, level, i), element)
|
|
|
|
})
|
|
|
|
level++
|
2019-07-22 17:19:50 -04:00
|
|
|
let numberOfElementsInLevel = Math.ceil(defaultElements.length / 2)
|
2019-07-16 13:27:20 -04:00
|
|
|
for (level; level <= this.n_levels; level++) {
|
2019-07-22 17:19:50 -04:00
|
|
|
for(let i = 0; i < numberOfElementsInLevel; i++) {
|
2019-07-16 13:27:20 -04:00
|
|
|
const leftKey = MerkleTree.index_to_key(prefix, level - 1, 2 * i)
|
|
|
|
const rightKey = MerkleTree.index_to_key(prefix, level - 1, 2 * i + 1)
|
|
|
|
|
|
|
|
const left = this.storage.get(leftKey)
|
|
|
|
const right = this.storage.get_or_element(rightKey, this.zero_values[level - 1])
|
|
|
|
|
|
|
|
const subRoot = this.hasher.hash(null, left, right)
|
|
|
|
this.storage.put(MerkleTree.index_to_key(prefix, level, i), subRoot)
|
2019-07-11 10:04:36 -04:00
|
|
|
}
|
2019-07-22 17:19:50 -04:00
|
|
|
numberOfElementsInLevel = Math.ceil(numberOfElementsInLevel / 2)
|
2019-07-16 13:27:20 -04:00
|
|
|
}
|
2019-07-11 10:04:36 -04:00
|
|
|
}
|
2019-07-11 06:38:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static index_to_key(prefix, level, index) {
|
2019-07-16 13:27:20 -04:00
|
|
|
const key = `${prefix}_tree_${level}_${index}`
|
|
|
|
return key
|
2019-07-11 06:38:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async root() {
|
|
|
|
let root = await this.storage.get_or_element(
|
|
|
|
MerkleTree.index_to_key(this.prefix, this.n_levels, 0),
|
|
|
|
this.zero_values[this.n_levels],
|
2019-07-16 13:27:20 -04:00
|
|
|
)
|
2019-07-11 06:38:22 -04:00
|
|
|
|
2019-07-16 13:27:20 -04:00
|
|
|
return root
|
2019-07-11 06:38:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async path(index) {
|
|
|
|
class PathTraverser {
|
|
|
|
constructor(prefix, storage, zero_values) {
|
2019-07-16 13:27:20 -04:00
|
|
|
this.prefix = prefix
|
|
|
|
this.storage = storage
|
|
|
|
this.zero_values = zero_values
|
|
|
|
this.path_elements = []
|
|
|
|
this.path_index = []
|
2019-07-11 06:38:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async handle_index(level, element_index, sibling_index) {
|
|
|
|
const sibling = await this.storage.get_or_element(
|
|
|
|
MerkleTree.index_to_key(this.prefix, level, sibling_index),
|
|
|
|
this.zero_values[level],
|
2019-07-16 13:27:20 -04:00
|
|
|
)
|
|
|
|
this.path_elements.push(sibling)
|
|
|
|
this.path_index.push(element_index % 2)
|
2019-07-11 06:38:22 -04:00
|
|
|
}
|
|
|
|
}
|
2019-11-09 08:49:31 -05:00
|
|
|
index = Number(index)
|
2019-07-16 13:27:20 -04:00
|
|
|
let traverser = new PathTraverser(this.prefix, this.storage, this.zero_values)
|
2019-07-11 06:38:22 -04:00
|
|
|
const root = await this.storage.get_or_element(
|
|
|
|
MerkleTree.index_to_key(this.prefix, this.n_levels, 0),
|
|
|
|
this.zero_values[this.n_levels],
|
2019-07-16 13:27:20 -04:00
|
|
|
)
|
2019-07-11 06:38:22 -04:00
|
|
|
|
|
|
|
const element = await this.storage.get_or_element(
|
|
|
|
MerkleTree.index_to_key(this.prefix, 0, index),
|
|
|
|
this.zero_values[0],
|
2019-07-16 13:27:20 -04:00
|
|
|
)
|
2019-07-11 06:38:22 -04:00
|
|
|
|
2019-07-16 13:27:20 -04:00
|
|
|
await this.traverse(index, traverser)
|
2019-07-11 06:38:22 -04:00
|
|
|
return {
|
|
|
|
root,
|
|
|
|
path_elements: traverser.path_elements,
|
|
|
|
path_index: traverser.path_index,
|
|
|
|
element
|
2019-07-16 13:27:20 -04:00
|
|
|
}
|
2019-07-11 06:38:22 -04:00
|
|
|
}
|
|
|
|
|
2019-07-12 06:24:59 -04:00
|
|
|
async update(index, element, insert = false) {
|
|
|
|
if (!insert && index >= this.totalElements) {
|
|
|
|
throw Error('Use insert method for new elements.')
|
|
|
|
} else if(insert && index < this.totalElements) {
|
|
|
|
throw Error('Use update method for existing elements.')
|
|
|
|
}
|
2019-07-11 06:38:22 -04:00
|
|
|
try {
|
|
|
|
class UpdateTraverser {
|
|
|
|
constructor(prefix, storage, hasher, element, zero_values) {
|
2019-07-16 13:27:20 -04:00
|
|
|
this.prefix = prefix
|
|
|
|
this.current_element = element
|
|
|
|
this.zero_values = zero_values
|
|
|
|
this.storage = storage
|
|
|
|
this.hasher = hasher
|
|
|
|
this.key_values_to_put = []
|
2019-07-11 06:38:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async handle_index(level, element_index, sibling_index) {
|
|
|
|
if (level == 0) {
|
|
|
|
this.original_element = await this.storage.get_or_element(
|
|
|
|
MerkleTree.index_to_key(this.prefix, level, element_index),
|
|
|
|
this.zero_values[level],
|
2019-07-16 13:27:20 -04:00
|
|
|
)
|
2019-07-11 06:38:22 -04:00
|
|
|
}
|
|
|
|
const sibling = await this.storage.get_or_element(
|
|
|
|
MerkleTree.index_to_key(this.prefix, level, sibling_index),
|
|
|
|
this.zero_values[level],
|
2019-07-16 13:27:20 -04:00
|
|
|
)
|
|
|
|
let left, right
|
2019-07-11 06:38:22 -04:00
|
|
|
if (element_index % 2 == 0) {
|
2019-07-16 13:27:20 -04:00
|
|
|
left = this.current_element
|
|
|
|
right = sibling
|
2019-07-11 06:38:22 -04:00
|
|
|
} else {
|
2019-07-16 13:27:20 -04:00
|
|
|
left = sibling
|
|
|
|
right = this.current_element
|
2019-07-11 06:38:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.key_values_to_put.push({
|
|
|
|
key: MerkleTree.index_to_key(this.prefix, level, element_index),
|
|
|
|
value: this.current_element,
|
2019-07-16 13:27:20 -04:00
|
|
|
})
|
|
|
|
this.current_element = this.hasher.hash(level, left, right)
|
2019-07-11 06:38:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let traverser = new UpdateTraverser(
|
|
|
|
this.prefix,
|
|
|
|
this.storage,
|
|
|
|
this.hasher,
|
|
|
|
element,
|
|
|
|
this.zero_values
|
2019-07-16 13:27:20 -04:00
|
|
|
)
|
2019-07-11 06:38:22 -04:00
|
|
|
|
2019-07-16 13:27:20 -04:00
|
|
|
await this.traverse(index, traverser)
|
2019-07-11 06:38:22 -04:00
|
|
|
traverser.key_values_to_put.push({
|
|
|
|
key: MerkleTree.index_to_key(this.prefix, this.n_levels, 0),
|
|
|
|
value: traverser.current_element,
|
2019-07-16 13:27:20 -04:00
|
|
|
})
|
2019-07-11 06:38:22 -04:00
|
|
|
|
2019-07-16 13:27:20 -04:00
|
|
|
await this.storage.put_batch(traverser.key_values_to_put)
|
2019-07-11 10:04:36 -04:00
|
|
|
} catch(e) {
|
2019-07-16 13:27:20 -04:00
|
|
|
console.error(e)
|
2019-07-11 06:38:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 06:24:59 -04:00
|
|
|
async insert(element) {
|
|
|
|
const index = this.totalElements
|
|
|
|
await this.update(index, element, true)
|
|
|
|
this.totalElements++
|
|
|
|
}
|
|
|
|
|
2019-07-11 06:38:22 -04:00
|
|
|
async traverse(index, handler) {
|
2019-07-16 13:27:20 -04:00
|
|
|
let current_index = index
|
2019-07-11 06:38:22 -04:00
|
|
|
for (let i = 0; i < this.n_levels; i++) {
|
2019-07-16 13:27:20 -04:00
|
|
|
let sibling_index = current_index
|
|
|
|
if (current_index % 2 == 0) {
|
|
|
|
sibling_index += 1
|
|
|
|
} else {
|
|
|
|
sibling_index -= 1
|
2019-07-11 06:38:22 -04:00
|
|
|
}
|
2019-07-16 13:27:20 -04:00
|
|
|
await handler.handle_index(i, current_index, sibling_index)
|
|
|
|
current_index = Math.floor(current_index / 2)
|
|
|
|
}
|
2019-07-11 06:38:22 -04:00
|
|
|
}
|
2019-07-17 17:25:16 -04:00
|
|
|
|
|
|
|
getIndexByElement(element) {
|
|
|
|
for(let i = this.totalElements - 1; i >= 0; i--) {
|
|
|
|
const elementFromTree = this.storage.get(MerkleTree.index_to_key(this.prefix, 0, i))
|
|
|
|
if (elementFromTree === element) {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-07-11 06:38:22 -04:00
|
|
|
}
|
|
|
|
|
2019-07-16 13:27:20 -04:00
|
|
|
module.exports = MerkleTree
|