-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathlean-imt.ts
418 lines (353 loc) · 15.4 KB
/
lean-imt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import {
requireArray,
requireFunction,
requireDefined,
requireNumber,
requireString
} from "@zk-kit/utils/error-handlers"
import { LeanIMTHashFunction, LeanIMTMerkleProof } from "./types"
/**
* The {@link LeanIMT} is an optimized binary version of the {@link IMT}.
* This implementation exclusively supports binary trees, eliminates the use of
* zeroes, and the tree's {@link LeanIMT#depth} is dynamic. When a node doesn't have the right child,
* instead of using a zero hash as in the IMT, the node's value becomes that
* of its left child. Furthermore, rather than utilizing a static tree depth,
* it is updated based on the number of {@link LeanIMT#leaves} in the tree. This approach
* results in the calculation of significantly fewer hashes, making the tree more efficient.
*/
export default class LeanIMT<N = bigint> {
/**
* The matrix where all the tree nodes are stored. The first index indicates
* the level of the tree, while the second index represents the node's
* position within that specific level. The last level will always contain
* a list with a single element, which is the root.
* Most of the attributes of this class are getters which can retrieve
* their values from this matrix.
*/
private _nodes: N[][]
/**
* The hash function used to compute the tree nodes.
*/
private readonly _hash: LeanIMTHashFunction<N>
/**
* It initializes the tree with a given hash function and an optional list of leaves.
* @param hash The hash function used to create nodes.
* @param leaves The list of leaves.
*/
constructor(hash: LeanIMTHashFunction<N>, leaves: N[] = []) {
requireDefined(hash, "hash")
requireFunction(hash, "hash")
requireArray(leaves, "leaves")
// Initialize the attributes.
this._nodes = [[]]
this._hash = hash
// Initialize the tree with a list of leaves if there are any.
if (leaves.length > 0) {
this.insertMany(leaves)
}
}
/**
* The root of the tree. This value doesn't need to be stored as
* it is always the first and unique element of the last level of the tree.
* Its value can be retrieved in {@link LeanIMT#_nodes}.
* @returns The root hash of the tree.
*/
public get root(): N {
return this._nodes[this.depth][0]
}
/**
* The depth of the tree, which equals the number of levels - 1.
* @returns The depth of the tree.
*/
public get depth(): number {
return this._nodes.length - 1
}
/**
* The leaves of the tree. They can be retrieved from the first
* level of the tree using {@link LeanIMT#_nodes}. The returned
* value is a copy of the array and not the original object.
* @returns The list of tree leaves.
*/
public get leaves(): N[] {
return this._nodes[0].slice()
}
/**
* The size of the tree, which the number of its leaves.
* It's the length of the first level's list.
* @returns The number of leaves of the tree.
*/
public get size(): number {
return this._nodes[0].length
}
/**
* It returns the index of a leaf. If the leaf does not exist it returns -1.
* @param leaf A leaf of the tree.
* @returns The index of the leaf.
*/
public indexOf(leaf: N): number {
requireDefined(leaf, "leaf")
return this._nodes[0].indexOf(leaf)
}
/**
* It returns true if the leaf exists, and false otherwise
* @param leaf A leaf of the tree.
* @returns True if the tree has the leaf, and false otherwise.
*/
public has(leaf: N): boolean {
requireDefined(leaf, "leaf")
return this._nodes[0].includes(leaf)
}
/**
* The leaves are inserted incrementally. If 'i' is the index of the last
* leaf, the new one will be inserted at position 'i + 1'. Every time a
* new leaf is inserted, the nodes that separate the new leaf from the root
* of the tree are created or updated if they already exist, from bottom to top.
* When a node has only one child (the left one), its value takes on the value
* of the child. Otherwise, the hash of the children is calculated.
* @param leaf The new leaf to be inserted in the tree.
*/
public insert(leaf: N) {
requireDefined(leaf, "leaf")
// If the next depth is greater, a new tree level will be added.
if (this.depth < Math.ceil(Math.log2(this.size + 1))) {
// Adding an array is like adding a new level.
this._nodes.push([])
}
let node = leaf
// The index of the new leaf equals the number of leaves in the tree.
let index = this.size
for (let level = 0; level < this.depth; level += 1) {
this._nodes[level][index] = node
// Bitwise AND, 0 -> left or 1 -> right.
// If the node is a right node the parent node will be the hash
// of the child nodes. Otherwise, parent will equal left child node.
if (index & 1) {
const sibling = this._nodes[level][index - 1]
node = this._hash(sibling, node)
}
// Right shift, it divides a number by 2 and discards the remainder.
index >>= 1
}
// Store the new root.
this._nodes[this.depth] = [node]
}
/**
* This function is useful when you want to insert N leaves all at once.
* It is more efficient than using the {@link LeanIMT#insert} method N times because it
* significantly reduces the number of cases where a node has only one
* child, which is a common occurrence in gradual insertion.
* @param leaves The list of leaves to be inserted.
*/
public insertMany(leaves: N[]) {
requireDefined(leaves, "leaves")
requireArray(leaves, "leaves")
if (leaves.length === 0) {
throw new Error("There are no leaves to add")
}
let startIndex = this.size >> 1
this._nodes[0].push(...leaves)
// Calculate how many tree levels will need to be added
// using the number of leaves.
const numberOfNewLevels = Math.ceil(Math.log2(this.size)) - this.depth
// Add the new levels.
for (let i = 0; i < numberOfNewLevels; i += 1) {
this._nodes.push([])
}
for (let level = 0; level < this.depth; level += 1) {
// Calculate the number of nodes of the next level.
const numberOfNodes = Math.ceil(this._nodes[level].length / 2)
for (let index = startIndex; index < numberOfNodes; index += 1) {
const rightNode = this._nodes[level][index * 2 + 1]
const leftNode = this._nodes[level][index * 2]
const parentNode = rightNode ? this._hash(leftNode, rightNode) : leftNode
this._nodes[level + 1][index] = parentNode
}
startIndex >>= 1
}
}
/**
* It updates a leaf in the tree. It's very similar to the {@link LeanIMT#insert} function.
* @param index The index of the leaf to be updated.
* @param newLeaf The new leaf to be inserted.
*/
public update(index: number, newLeaf: N) {
requireDefined(index, "index")
requireDefined(newLeaf, "newLeaf")
requireNumber(index, "index")
let node = newLeaf
for (let level = 0; level < this.depth; level += 1) {
this._nodes[level][index] = node
if (index & 1) {
const sibling = this._nodes[level][index - 1]
node = this._hash(sibling, node)
} else {
// In this case there could still be a right node
// because the path might not be the rightmost one
// (like the 'insert' function).
const sibling = this._nodes[level][index + 1]
// If the sibling node does not exist, it means that the node at
// this level has the same value as its child. Therefore, there
// no hash to calculate.
if (sibling !== undefined) {
node = this._hash(node, sibling)
}
}
index >>= 1
}
this._nodes[this.depth] = [node]
}
/**
* Updates m leaves all at once.
* It is more efficient than using the {@link LeanIMT#update} method m times because it
* prevents updating middle nodes several times. This would happen when updating leaves
* with common ancestors. The naive approach of calling 'update' m times has complexity
* O(m*log(n)) (where n is the number of leaves of the tree), which ends up in
* O(n*log(n)) when m ~ n. With this new approach, this ends up being O(n) because every
* node is updated at most once and there are around 2*n nodes in the tree.
* @param indices The list of indices of the respective leaves.
* @param leaves The list of leaves to be updated.
*/
public updateMany(indices: number[], leaves: N[]) {
requireDefined(leaves, "leaves")
requireDefined(indices, "indices")
requireArray(leaves, "leaves")
requireArray(indices, "indices")
if (leaves.length !== indices.length) {
throw new Error("There is no correspondence between indices and leaves")
}
// This will keep track of the outdated nodes of each level.
let modifiedIndices = new Set<number>()
for (let i = 0; i < indices.length; i += 1) {
requireNumber(indices[i], `index ${i}`)
if (indices[i] < 0 || indices[i] >= this.size) {
throw new Error(`Index ${i} is out of range`)
}
if (modifiedIndices.has(indices[i])) {
throw new Error(`Leaf ${indices[i]} is repeated`)
}
modifiedIndices.add(indices[i])
}
modifiedIndices.clear()
// First, modify the first level, which consists only of raw, un-hashed values
for (let leaf = 0; leaf < indices.length; leaf += 1) {
this._nodes[0][indices[leaf]] = leaves[leaf]
modifiedIndices.add(indices[leaf] >> 1)
}
// Now update each node of the corresponding levels
for (let level = 1; level <= this.depth; level += 1) {
const newModifiedIndices: number[] = []
for (const index of modifiedIndices) {
const leftChild = this._nodes[level - 1][2 * index]
const rightChild = this._nodes[level - 1][2 * index + 1]
this._nodes[level][index] = rightChild ? this._hash(leftChild, rightChild) : leftChild
newModifiedIndices.push(index >> 1)
}
modifiedIndices = new Set<number>(newModifiedIndices)
}
}
/**
* It generates a {@link LeanIMTMerkleProof} for a leaf of the tree.
* That proof can be verified by this tree using the same hash function.
* @param index The index of the leaf for which a Merkle proof will be generated.
* @returns The Merkle proof of the leaf.
*/
public generateProof(index: number): LeanIMTMerkleProof<N> {
requireDefined(index, "index")
requireNumber(index, "index")
if (index < 0 || index >= this.size) {
throw new Error(`The leaf at index '${index}' does not exist in this tree`)
}
const leaf = this.leaves[index]
const siblings: N[] = []
const path: number[] = []
for (let level = 0; level < this.depth; level += 1) {
const isRightNode = index & 1
const siblingIndex = isRightNode ? index - 1 : index + 1
const sibling = this._nodes[level][siblingIndex]
// If the sibling node does not exist, it means that the node at
// this level has the same value as its child. Therefore, there
// is no need to include it in the proof since there is no hash to calculate.
if (sibling !== undefined) {
path.push(isRightNode)
siblings.push(sibling)
}
index >>= 1
}
// The index might be different from the original index of the leaf, since
// in some cases some siblings are not included (as explained above).
return { root: this.root, leaf, index: Number.parseInt(path.reverse().join(""), 2), siblings }
}
/**
* It verifies a {@link LeanIMTMerkleProof} to confirm that a leaf indeed
* belongs to a tree. Does not verify that the node belongs to this
* tree in particular. Equivalent to
* `LeanIMT.verifyProof(proof, this._hash)`.
* @param proof The Merkle tree proof.
* @returns True if the leaf is part of the tree, and false otherwise.
*/
public verifyProof(proof: LeanIMTMerkleProof<N>): boolean {
return LeanIMT.verifyProof(proof, this._hash)
}
/**
* It verifies a {@link LeanIMTMerkleProof} to confirm that a leaf indeed
* belongs to a tree.
* @param proof The Merkle tree proof.
* @returns True if the leaf is part of the tree, and false otherwise.
*/
public static verifyProof<N>(proof: LeanIMTMerkleProof<N>, hash: LeanIMTHashFunction<N>): boolean {
requireDefined(proof, "proof")
const { root, leaf, siblings, index } = proof
requireDefined(proof.root, "proof.root")
requireDefined(proof.leaf, "proof.leaf")
requireDefined(proof.siblings, "proof.siblings")
requireDefined(proof.index, "proof.index")
requireArray(proof.siblings, "proof.siblings")
requireNumber(proof.index, "proof.index")
let node = leaf
for (let i = 0; i < siblings.length; i += 1) {
if ((index >> i) & 1) {
node = hash(siblings[i], node)
} else {
node = hash(node, siblings[i])
}
}
return root === node
}
/**
* It enables the conversion of the full tree structure into a JSON string,
* facilitating future imports of the tree. This approach is beneficial for
* large trees, as it saves time by storing hashes instead of recomputing them
* @returns The stringified JSON of the tree.
*/
public export(): string {
return JSON.stringify(this._nodes, (_, v) => (typeof v === "bigint" ? v.toString() : v))
}
/**
* It imports an entire tree by initializing the nodes without calculating
* any hashes. Note that it is crucial to ensure the integrity of the tree
* before or after importing it. If the map function is not defined, node
* values will be converted to bigints by default.
* @param hash The hash function used to create nodes.
* @param nodes The stringified JSON of the tree.
* @param map A function to map each node of the tree and convert their types.
* @returns A LeanIMT instance.
*/
static import<N = bigint>(hash: LeanIMTHashFunction<N>, nodes: string, map?: (value: string) => N): LeanIMT<N> {
requireDefined(hash, "hash")
requireDefined(nodes, "nodes")
requireFunction(hash, "hash")
requireString(nodes, "nodes")
if (map) {
requireDefined(map, "map")
requireFunction(map, "map")
}
const tree = new LeanIMT<N>(hash)
tree._nodes = JSON.parse(nodes, (_, value) => {
if (typeof value === "string") {
return map ? map(value) : BigInt(value)
}
return value
})
return tree
}
}