@aztec/merkle-tree 0.7.10 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dest/standard_indexed_tree/standard_indexed_tree.js +2 -2
  2. package/package.json +57 -5
  3. package/src/standard_indexed_tree/standard_indexed_tree.ts +2 -2
  4. package/.eslintrc.cjs +0 -1
  5. package/.tsbuildinfo +0 -1
  6. package/dest/sparse_tree/sparse_tree.test.d.ts +0 -2
  7. package/dest/sparse_tree/sparse_tree.test.d.ts.map +0 -1
  8. package/dest/sparse_tree/sparse_tree.test.js +0 -133
  9. package/dest/standard_indexed_tree/test/standard_indexed_tree.test.d.ts +0 -2
  10. package/dest/standard_indexed_tree/test/standard_indexed_tree.test.d.ts.map +0 -1
  11. package/dest/standard_indexed_tree/test/standard_indexed_tree.test.js +0 -336
  12. package/dest/standard_tree/standard_tree.test.d.ts +0 -2
  13. package/dest/standard_tree/standard_tree.test.d.ts.map +0 -1
  14. package/dest/standard_tree/standard_tree.test.js +0 -58
  15. package/dest/test/standard_based_test_suite.d.ts +0 -6
  16. package/dest/test/standard_based_test_suite.d.ts.map +0 -1
  17. package/dest/test/standard_based_test_suite.js +0 -87
  18. package/dest/test/test_suite.d.ts +0 -6
  19. package/dest/test/test_suite.d.ts.map +0 -1
  20. package/dest/test/test_suite.js +0 -119
  21. package/dest/test/utils/append_leaves.d.ts +0 -5
  22. package/dest/test/utils/append_leaves.d.ts.map +0 -1
  23. package/dest/test/utils/append_leaves.js +0 -14
  24. package/dest/test/utils/create_mem_down.d.ts +0 -3
  25. package/dest/test/utils/create_mem_down.d.ts.map +0 -1
  26. package/dest/test/utils/create_mem_down.js +0 -3
  27. package/dest/test/utils/pedersen_with_counter.d.ts +0 -24
  28. package/dest/test/utils/pedersen_with_counter.d.ts.map +0 -1
  29. package/dest/test/utils/pedersen_with_counter.js +0 -31
  30. package/package.local.json +0 -3
  31. package/src/sparse_tree/sparse_tree.test.ts +0 -179
  32. package/src/standard_indexed_tree/test/standard_indexed_tree.test.ts +0 -476
  33. package/src/standard_tree/standard_tree.test.ts +0 -76
  34. package/src/test/standard_based_test_suite.ts +0 -142
  35. package/src/test/test_suite.ts +0 -165
  36. package/src/test/utils/append_leaves.ts +0 -15
  37. package/src/test/utils/create_mem_down.ts +0 -3
  38. package/src/test/utils/pedersen_with_counter.ts +0 -30
  39. package/tsconfig.json +0 -20
@@ -1,476 +0,0 @@
1
- import { CircuitsWasm } from '@aztec/circuits.js';
2
- import { toBufferBE } from '@aztec/foundation/bigint-buffer';
3
- import { IWasmModule } from '@aztec/foundation/wasm';
4
- import { Hasher, SiblingPath } from '@aztec/types';
5
-
6
- import { default as levelup } from 'levelup';
7
-
8
- import { INITIAL_LEAF, MerkleTree, Pedersen, loadTree, newTree } from '../../index.js';
9
- import { treeTestSuite } from '../../test/test_suite.js';
10
- import { createMemDown } from '../../test/utils/create_mem_down.js';
11
- import { StandardIndexedTreeWithAppend } from './standard_indexed_tree_with_append.js';
12
-
13
- const createDb = async (levelUp: levelup.LevelUp, hasher: Hasher, name: string, depth: number, prefilledSize = 1) => {
14
- return await newTree(StandardIndexedTreeWithAppend, levelUp, hasher, name, depth, prefilledSize);
15
- };
16
-
17
- const createFromName = async (levelUp: levelup.LevelUp, hasher: Hasher, name: string) => {
18
- return await loadTree(StandardIndexedTreeWithAppend, levelUp, hasher, name);
19
- };
20
-
21
- const createIndexedTreeLeaf = (value: number, nextIndex: number, nextValue: number) => {
22
- return [toBufferBE(BigInt(value), 32), toBufferBE(BigInt(nextIndex), 32), toBufferBE(BigInt(nextValue), 32)];
23
- };
24
-
25
- const verifyCommittedState = async <N extends number>(
26
- tree: MerkleTree,
27
- root: Buffer,
28
- siblingPathIndex: bigint,
29
- emptySiblingPath: SiblingPath<N>,
30
- ) => {
31
- expect(tree.getRoot(false)).toEqual(root);
32
- expect(tree.getNumLeaves(false)).toEqual(1n);
33
- expect(await tree.getSiblingPath(siblingPathIndex, false)).toEqual(emptySiblingPath);
34
- };
35
-
36
- const TEST_TREE_DEPTH = 3;
37
-
38
- treeTestSuite('StandardIndexedTree', createDb, createFromName);
39
-
40
- describe('StandardIndexedTreeSpecific', () => {
41
- let wasm: IWasmModule;
42
- let pedersen: Pedersen;
43
-
44
- beforeEach(async () => {
45
- wasm = await CircuitsWasm.get();
46
- pedersen = new Pedersen(wasm);
47
- });
48
-
49
- it('produces the correct roots and sibling paths', async () => {
50
- // Create a depth-3 indexed merkle tree
51
- const db = levelup(createMemDown());
52
- const tree = await createDb(db, pedersen, 'test', 3);
53
-
54
- /**
55
- * Initial state:
56
- *
57
- * index 0 1 2 3 4 5 6 7
58
- * ---------------------------------------------------------------------
59
- * val 0 0 0 0 0 0 0 0
60
- * nextIdx 0 0 0 0 0 0 0 0
61
- * nextVal 0 0 0 0 0 0 0 0.
62
- */
63
-
64
- const initialLeafHash = pedersen.compressInputs(createIndexedTreeLeaf(0, 0, 0));
65
- const level1ZeroHash = pedersen.compress(INITIAL_LEAF, INITIAL_LEAF);
66
- const level2ZeroHash = pedersen.compress(level1ZeroHash, level1ZeroHash);
67
-
68
- let index0Hash = initialLeafHash;
69
- // Each element is named by the level followed by the index on that level. E.g. e10 -> level 1, index 0, e21 -> level 2, index 1
70
- let e10 = pedersen.compress(index0Hash, INITIAL_LEAF);
71
- let e20 = pedersen.compress(e10, level1ZeroHash);
72
-
73
- const initialE20 = e20; // Kept for calculating committed state later
74
- const initialE10 = e10;
75
-
76
- let root = pedersen.compress(e20, level2ZeroHash);
77
- const initialRoot = root;
78
-
79
- const emptySiblingPath = new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash, level2ZeroHash]);
80
-
81
- expect(tree.getRoot(true)).toEqual(root);
82
- expect(tree.getNumLeaves(true)).toEqual(1n);
83
- expect(await tree.getSiblingPath(0n, true)).toEqual(
84
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash, level2ZeroHash]),
85
- );
86
-
87
- await verifyCommittedState(tree, initialRoot, 0n, emptySiblingPath);
88
-
89
- /**
90
- * Add new value 30:
91
- *
92
- * index 0 1 2 3 4 5 6 7
93
- * ---------------------------------------------------------------------
94
- * val 0 30 0 0 0 0 0 0
95
- * nextIdx 1 0 0 0 0 0 0 0
96
- * nextVal 30 0 0 0 0 0 0 0.
97
- */
98
- index0Hash = pedersen.compressInputs(createIndexedTreeLeaf(0, 1, 30));
99
- let index1Hash = pedersen.compressInputs(createIndexedTreeLeaf(30, 0, 0));
100
- e10 = pedersen.compress(index0Hash, index1Hash);
101
- e20 = pedersen.compress(e10, level1ZeroHash);
102
- root = pedersen.compress(e20, level2ZeroHash);
103
-
104
- await tree.appendLeaves([toBufferBE(30n, 32)]);
105
-
106
- expect(tree.getRoot(true)).toEqual(root);
107
- expect(tree.getNumLeaves(true)).toEqual(2n);
108
- expect(await tree.getSiblingPath(1n, true)).toEqual(
109
- new SiblingPath(TEST_TREE_DEPTH, [index0Hash, level1ZeroHash, level2ZeroHash]),
110
- );
111
-
112
- // ensure the committed state is correct
113
- const initialSiblingPath = new SiblingPath(TEST_TREE_DEPTH, [initialLeafHash, level1ZeroHash, level2ZeroHash]);
114
- await verifyCommittedState(tree, initialRoot, 1n, initialSiblingPath);
115
-
116
- /**
117
- * Add new value 10:
118
- *
119
- * index 0 1 2 3 4 5 6 7
120
- * ---------------------------------------------------------------------
121
- * val 0 30 10 0 0 0 0 0
122
- * nextIdx 2 0 1 0 0 0 0 0
123
- * nextVal 10 0 30 0 0 0 0 0.
124
- */
125
- index0Hash = pedersen.compressInputs(createIndexedTreeLeaf(0, 2, 10));
126
- let index2Hash = pedersen.compressInputs(createIndexedTreeLeaf(10, 1, 30));
127
- e10 = pedersen.compress(index0Hash, index1Hash);
128
- let e11 = pedersen.compress(index2Hash, INITIAL_LEAF);
129
- e20 = pedersen.compress(e10, e11);
130
- root = pedersen.compress(e20, level2ZeroHash);
131
-
132
- await tree.appendLeaves([toBufferBE(10n, 32)]);
133
-
134
- expect(tree.getRoot(true)).toEqual(root);
135
- expect(tree.getNumLeaves(true)).toEqual(3n);
136
- expect(await tree.getSiblingPath(2n, true)).toEqual(
137
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, e10, level2ZeroHash]),
138
- );
139
-
140
- // ensure the committed state is correct
141
- await verifyCommittedState(
142
- tree,
143
- initialRoot,
144
- 2n,
145
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, initialE10, level2ZeroHash]),
146
- );
147
-
148
- /**
149
- * Add new value 20:
150
- *
151
- * index 0 1 2 3 4 5 6 7
152
- * ---------------------------------------------------------------------
153
- * val 0 30 10 20 0 0 0 0
154
- * nextIdx 2 0 3 1 0 0 0 0
155
- * nextVal 10 0 20 30 0 0 0 0.
156
- */
157
- e10 = pedersen.compress(index0Hash, index1Hash);
158
- index2Hash = pedersen.compressInputs(createIndexedTreeLeaf(10, 3, 20));
159
- const index3Hash = pedersen.compressInputs(createIndexedTreeLeaf(20, 1, 30));
160
- e11 = pedersen.compress(index2Hash, index3Hash);
161
- e20 = pedersen.compress(e10, e11);
162
- root = pedersen.compress(e20, level2ZeroHash);
163
-
164
- await tree.appendLeaves([toBufferBE(20n, 32)]);
165
-
166
- expect(tree.getRoot(true)).toEqual(root);
167
- expect(tree.getNumLeaves(true)).toEqual(4n);
168
- expect(await tree.getSiblingPath(3n, true)).toEqual(
169
- new SiblingPath(TEST_TREE_DEPTH, [index2Hash, e10, level2ZeroHash]),
170
- );
171
-
172
- // ensure the committed state is correct
173
- await verifyCommittedState(
174
- tree,
175
- initialRoot,
176
- 3n,
177
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, initialE10, level2ZeroHash]),
178
- );
179
-
180
- /**
181
- * Add new value 50:
182
- *
183
- * index 0 1 2 3 4 5 6 7
184
- * ---------------------------------------------------------------------
185
- * val 0 30 10 20 50 0 0 0
186
- * nextIdx 2 4 3 1 0 0 0 0
187
- * nextVal 10 50 20 30 0 0 0 0.
188
- */
189
- index1Hash = pedersen.compressInputs(createIndexedTreeLeaf(30, 4, 50));
190
- const index4Hash = pedersen.compressInputs(createIndexedTreeLeaf(50, 0, 0));
191
- e10 = pedersen.compress(index0Hash, index1Hash);
192
- e20 = pedersen.compress(e10, e11);
193
- const e12 = pedersen.compress(index4Hash, INITIAL_LEAF);
194
- const e21 = pedersen.compress(e12, level1ZeroHash);
195
- root = pedersen.compress(e20, e21);
196
-
197
- await tree.appendLeaves([toBufferBE(50n, 32)]);
198
-
199
- expect(tree.getRoot(true)).toEqual(root);
200
- expect(tree.getNumLeaves(true)).toEqual(5n);
201
-
202
- // ensure the committed state is correct
203
- await verifyCommittedState(
204
- tree,
205
- initialRoot,
206
- 4n,
207
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash, initialE20]),
208
- );
209
-
210
- // check all uncommitted hash paths
211
- expect(await tree.getSiblingPath(0n, true)).toEqual(new SiblingPath(TEST_TREE_DEPTH, [index1Hash, e11, e21]));
212
- expect(await tree.getSiblingPath(1n, true)).toEqual(new SiblingPath(TEST_TREE_DEPTH, [index0Hash, e11, e21]));
213
- expect(await tree.getSiblingPath(2n, true)).toEqual(new SiblingPath(TEST_TREE_DEPTH, [index3Hash, e10, e21]));
214
- expect(await tree.getSiblingPath(3n, true)).toEqual(new SiblingPath(TEST_TREE_DEPTH, [index2Hash, e10, e21]));
215
- expect(await tree.getSiblingPath(4n, true)).toEqual(
216
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash, e20]),
217
- );
218
- expect(await tree.getSiblingPath(5n, true)).toEqual(
219
- new SiblingPath(TEST_TREE_DEPTH, [index4Hash, level1ZeroHash, e20]),
220
- );
221
- expect(await tree.getSiblingPath(6n, true)).toEqual(new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, e12, e20]));
222
- expect(await tree.getSiblingPath(7n, true)).toEqual(new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, e12, e20]));
223
-
224
- // check all committed hash paths
225
- expect(await tree.getSiblingPath(0n, false)).toEqual(emptySiblingPath);
226
- expect(await tree.getSiblingPath(1n, false)).toEqual(initialSiblingPath);
227
- expect(await tree.getSiblingPath(2n, false)).toEqual(
228
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, initialE10, level2ZeroHash]),
229
- );
230
- expect(await tree.getSiblingPath(3n, false)).toEqual(
231
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, initialE10, level2ZeroHash]),
232
- );
233
- const e2SiblingPath = new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash, initialE20]);
234
- expect(await tree.getSiblingPath(4n, false)).toEqual(e2SiblingPath);
235
- expect(await tree.getSiblingPath(5n, false)).toEqual(e2SiblingPath);
236
- expect(await tree.getSiblingPath(6n, false)).toEqual(e2SiblingPath);
237
- expect(await tree.getSiblingPath(7n, false)).toEqual(e2SiblingPath);
238
-
239
- await tree.commit();
240
- // check all committed hash paths equal uncommitted hash paths
241
- for (let i = 0; i < 8; i++) {
242
- expect(await tree.getSiblingPath(BigInt(i), false)).toEqual(await tree.getSiblingPath(BigInt(i), true));
243
- }
244
- });
245
-
246
- it('Can append empty leaves and handle insertions', async () => {
247
- // Create a depth-3 indexed merkle tree
248
- const db = levelup(createMemDown());
249
- const tree = await createDb(db, pedersen, 'test', 3);
250
-
251
- /**
252
- * Initial state:
253
- *
254
- * index 0 1 2 3 4 5 6 7
255
- * ---------------------------------------------------------------------
256
- * val 0 0 0 0 0 0 0 0
257
- * nextIdx 0 0 0 0 0 0 0 0
258
- * nextVal 0 0 0 0 0 0 0 0.
259
- */
260
-
261
- const INITIAL_LEAF = toBufferBE(0n, 32);
262
- const initialLeafHash = pedersen.compressInputs(createIndexedTreeLeaf(0, 0, 0));
263
- const level1ZeroHash = pedersen.compress(INITIAL_LEAF, INITIAL_LEAF);
264
- const level2ZeroHash = pedersen.compress(level1ZeroHash, level1ZeroHash);
265
- let index0Hash = initialLeafHash;
266
-
267
- let e10 = pedersen.compress(index0Hash, INITIAL_LEAF);
268
- let e20 = pedersen.compress(e10, level1ZeroHash);
269
-
270
- const inite10 = e10;
271
- const inite20 = e20;
272
-
273
- let root = pedersen.compress(e20, level2ZeroHash);
274
- const initialRoot = root;
275
-
276
- const emptySiblingPath = new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash, level2ZeroHash]);
277
- const initialSiblingPath = new SiblingPath(TEST_TREE_DEPTH, [initialLeafHash, level1ZeroHash, level2ZeroHash]);
278
-
279
- expect(tree.getRoot(true)).toEqual(root);
280
- expect(tree.getNumLeaves(true)).toEqual(1n);
281
- expect(await tree.getSiblingPath(0n, true)).toEqual(
282
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash, level2ZeroHash]),
283
- );
284
-
285
- await verifyCommittedState(tree, initialRoot, 0n, emptySiblingPath);
286
-
287
- /**
288
- * Add new value 30:
289
- *
290
- * index 0 1 2 3 4 5 6 7
291
- * ---------------------------------------------------------------------
292
- * val 0 30 0 0 0 0 0 0
293
- * nextIdx 1 0 0 0 0 0 0 0
294
- * nextVal 30 0 0 0 0 0 0 0.
295
- */
296
- index0Hash = pedersen.compressInputs(createIndexedTreeLeaf(0, 1, 30));
297
- let index1Hash = pedersen.compressInputs(createIndexedTreeLeaf(30, 0, 0));
298
- e10 = pedersen.compress(index0Hash, index1Hash);
299
- e20 = pedersen.compress(e10, level1ZeroHash);
300
- root = pedersen.compress(e20, level2ZeroHash);
301
-
302
- await tree.appendLeaves([toBufferBE(30n, 32)]);
303
-
304
- expect(tree.getRoot(true)).toEqual(root);
305
- expect(tree.getNumLeaves(true)).toEqual(2n);
306
- expect(await tree.getSiblingPath(1n, true)).toEqual(
307
- new SiblingPath(TEST_TREE_DEPTH, [index0Hash, level1ZeroHash, level2ZeroHash]),
308
- );
309
-
310
- // ensure the committed state is correct
311
- await verifyCommittedState(tree, initialRoot, 1n, initialSiblingPath);
312
-
313
- /**
314
- * Add new value 10:
315
- *
316
- * index 0 1 2 3 4 5 6 7
317
- * ---------------------------------------------------------------------
318
- * val 0 30 10 0 0 0 0 0
319
- * nextIdx 2 0 1 0 0 0 0 0
320
- * nextVal 10 0 30 0 0 0 0 0.
321
- */
322
- index0Hash = pedersen.compressInputs(createIndexedTreeLeaf(0, 2, 10));
323
- let index2Hash = pedersen.compressInputs(createIndexedTreeLeaf(10, 1, 30));
324
- e10 = pedersen.compress(index0Hash, index1Hash);
325
- let e11 = pedersen.compress(index2Hash, INITIAL_LEAF);
326
- e20 = pedersen.compress(e10, e11);
327
- root = pedersen.compress(e20, level2ZeroHash);
328
-
329
- await tree.appendLeaves([toBufferBE(10n, 32)]);
330
-
331
- expect(tree.getRoot(true)).toEqual(root);
332
- expect(tree.getNumLeaves(true)).toEqual(3n);
333
- expect(await tree.getSiblingPath(2n, true)).toEqual(
334
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, e10, level2ZeroHash]),
335
- );
336
-
337
- // ensure the committed state is correct
338
- await verifyCommittedState(
339
- tree,
340
- initialRoot,
341
- 2n,
342
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, inite10, level2ZeroHash]),
343
- );
344
-
345
- /**
346
- * Add new value 20:
347
- *
348
- * index 0 1 2 3 4 5 6 7
349
- * ---------------------------------------------------------------------
350
- * val 0 30 10 20 0 0 0 0
351
- * nextIdx 2 0 3 1 0 0 0 0
352
- * nextVal 10 0 20 30 0 0 0 0.
353
- */
354
- e10 = pedersen.compress(index0Hash, index1Hash);
355
- index2Hash = pedersen.compressInputs(createIndexedTreeLeaf(10, 3, 20));
356
- const index3Hash = pedersen.compressInputs(createIndexedTreeLeaf(20, 1, 30));
357
- e11 = pedersen.compress(index2Hash, index3Hash);
358
- e20 = pedersen.compress(e10, e11);
359
- root = pedersen.compress(e20, level2ZeroHash);
360
-
361
- await tree.appendLeaves([toBufferBE(20n, 32)]);
362
-
363
- expect(tree.getRoot(true)).toEqual(root);
364
- expect(tree.getNumLeaves(true)).toEqual(4n);
365
- expect(await tree.getSiblingPath(3n, true)).toEqual(
366
- new SiblingPath(TEST_TREE_DEPTH, [index2Hash, e10, level2ZeroHash]),
367
- );
368
-
369
- // ensure the committed state is correct
370
- await verifyCommittedState(
371
- tree,
372
- initialRoot,
373
- 3n,
374
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, inite10, level2ZeroHash]),
375
- );
376
-
377
- // Add 2 empty values
378
- const emptyLeaves = [toBufferBE(0n, 32), toBufferBE(0n, 32)];
379
- await tree.appendLeaves(emptyLeaves);
380
-
381
- // The root should be the same but the size should have increased
382
- expect(tree.getRoot(true)).toEqual(root);
383
- expect(tree.getNumLeaves(true)).toEqual(6n);
384
-
385
- /**
386
- * Add new value 50:
387
- *
388
- * index 0 1 2 3 4 5 6 7
389
- * --------------------------------------------------------------------
390
- * val 0 30 10 20 0 0 50 0
391
- * nextIdx 2 6 3 1 0 0 0 0
392
- * nextVal 10 50 20 30 0 0 0 0.
393
- */
394
- index1Hash = pedersen.compressInputs(createIndexedTreeLeaf(30, 6, 50));
395
- const index6Hash = pedersen.compressInputs(createIndexedTreeLeaf(50, 0, 0));
396
- e10 = pedersen.compress(index0Hash, index1Hash);
397
- e20 = pedersen.compress(e10, e11);
398
- const e13 = pedersen.compress(index6Hash, INITIAL_LEAF);
399
- const e21 = pedersen.compress(level1ZeroHash, e13);
400
- root = pedersen.compress(e20, e21);
401
-
402
- await tree.appendLeaves([toBufferBE(50n, 32)]);
403
-
404
- expect(tree.getRoot(true)).toEqual(root);
405
- expect(tree.getNumLeaves(true)).toEqual(7n);
406
-
407
- // ensure the committed state is correct
408
- await verifyCommittedState(
409
- tree,
410
- initialRoot,
411
- 6n,
412
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash, inite20]),
413
- );
414
-
415
- // // check all uncommitted hash paths
416
- expect(await tree.getSiblingPath(0n, true)).toEqual(new SiblingPath(TEST_TREE_DEPTH, [index1Hash, e11, e21]));
417
- expect(await tree.getSiblingPath(1n, true)).toEqual(new SiblingPath(TEST_TREE_DEPTH, [index0Hash, e11, e21]));
418
- expect(await tree.getSiblingPath(2n, true)).toEqual(new SiblingPath(TEST_TREE_DEPTH, [index3Hash, e10, e21]));
419
- expect(await tree.getSiblingPath(3n, true)).toEqual(new SiblingPath(TEST_TREE_DEPTH, [index2Hash, e10, e21]));
420
- expect(await tree.getSiblingPath(4n, true)).toEqual(new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, e13, e20]));
421
- expect(await tree.getSiblingPath(5n, true)).toEqual(new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, e13, e20]));
422
- expect(await tree.getSiblingPath(6n, true)).toEqual(
423
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash, e20]),
424
- );
425
- expect(await tree.getSiblingPath(7n, true)).toEqual(
426
- new SiblingPath(TEST_TREE_DEPTH, [index6Hash, level1ZeroHash, e20]),
427
- );
428
-
429
- // check all committed hash paths
430
- expect(await tree.getSiblingPath(0n, false)).toEqual(emptySiblingPath);
431
- expect(await tree.getSiblingPath(1n, false)).toEqual(initialSiblingPath);
432
- expect(await tree.getSiblingPath(2n, false)).toEqual(
433
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, inite10, level2ZeroHash]),
434
- );
435
- expect(await tree.getSiblingPath(3n, false)).toEqual(
436
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, inite10, level2ZeroHash]),
437
- );
438
- const e2SiblingPath = new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash, inite20]);
439
- expect(await tree.getSiblingPath(4n, false)).toEqual(e2SiblingPath);
440
- expect(await tree.getSiblingPath(5n, false)).toEqual(e2SiblingPath);
441
- expect(await tree.getSiblingPath(6n, false)).toEqual(e2SiblingPath);
442
- expect(await tree.getSiblingPath(7n, false)).toEqual(e2SiblingPath);
443
-
444
- await tree.commit();
445
- // check all committed hash paths equal uncommitted hash paths
446
- for (let i = 0; i < 8; i++) {
447
- expect(await tree.getSiblingPath(BigInt(i), false)).toEqual(await tree.getSiblingPath(BigInt(i), true));
448
- }
449
- });
450
-
451
- // For varying orders of insertions assert the local batch insertion generator creates the correct proofs
452
- it.each([
453
- // These are arbitrary but it needs to be higher than the constant `INITIAL_NULLIFIER_TREE_SIZE` and `KERNEL_NEW_NULLIFIERS_LENGTH * 2`
454
- [[1003, 1002, 1001, 1000, 0, 0, 0, 0]],
455
- [[1003, 1004, 1005, 1006, 0, 0, 0, 0]],
456
- [[1234, 1098, 0, 0, 99999, 1096, 1054, 0]],
457
- [[1970, 1980, 1040, 0, 99999, 1880, 100001, 9000000]],
458
- ] as const)('performs nullifier tree batch insertion correctly', async nullifiers => {
459
- const leaves = nullifiers.map(i => toBufferBE(BigInt(i), 32));
460
-
461
- const TREE_HEIGHT = 16; // originally from NULLIFIER_TREE_HEIGHT
462
- const INITIAL_TREE_SIZE = 8; // originally from INITIAL_NULLIFIER_TREE_SIZE
463
- const SUBTREE_HEIGHT = 5; // originally from BaseRollupInputs.NULLIFIER_SUBTREE_HEIGHT
464
-
465
- // Create a depth-3 indexed merkle tree
466
- const appendTree = await createDb(levelup(createMemDown()), pedersen, 'test', TREE_HEIGHT, INITIAL_TREE_SIZE);
467
- const insertTree = await createDb(levelup(createMemDown()), pedersen, 'test', TREE_HEIGHT, INITIAL_TREE_SIZE);
468
-
469
- await appendTree.appendLeaves(leaves);
470
- await insertTree.batchInsert(leaves, SUBTREE_HEIGHT);
471
-
472
- const expectedRoot = appendTree.getRoot(true);
473
- const actualRoot = insertTree.getRoot(true);
474
- expect(actualRoot).toEqual(expectedRoot);
475
- });
476
- });
@@ -1,76 +0,0 @@
1
- import { CircuitsWasm } from '@aztec/circuits.js';
2
- import { randomBytes } from '@aztec/foundation/crypto';
3
- import { IWasmModule } from '@aztec/foundation/wasm';
4
- import { Hasher } from '@aztec/types';
5
-
6
- import { default as levelup } from 'levelup';
7
-
8
- import { loadTree } from '../load_tree.js';
9
- import { newTree } from '../new_tree.js';
10
- import { standardBasedTreeTestSuite } from '../test/standard_based_test_suite.js';
11
- import { treeTestSuite } from '../test/test_suite.js';
12
- import { createMemDown } from '../test/utils/create_mem_down.js';
13
- import { PedersenWithCounter } from '../test/utils/pedersen_with_counter.js';
14
- import { INITIAL_LEAF } from '../tree_base.js';
15
- import { StandardTree } from './standard_tree.js';
16
-
17
- const createDb = async (levelUp: levelup.LevelUp, hasher: Hasher, name: string, depth: number) => {
18
- return await newTree(StandardTree, levelUp, hasher, name, depth);
19
- };
20
-
21
- const createFromName = async (levelUp: levelup.LevelUp, hasher: Hasher, name: string) => {
22
- return await loadTree(StandardTree, levelUp, hasher, name);
23
- };
24
-
25
- treeTestSuite('StandardTree', createDb, createFromName);
26
- standardBasedTreeTestSuite('StandardTree', createDb);
27
-
28
- describe('StandardTree_batchAppend', () => {
29
- let wasm: IWasmModule;
30
- let pedersen: PedersenWithCounter;
31
-
32
- beforeAll(async () => {
33
- wasm = await CircuitsWasm.get();
34
- pedersen = new PedersenWithCounter(wasm);
35
- });
36
-
37
- afterEach(() => {
38
- pedersen.resetCounter();
39
- });
40
-
41
- it('correctly computes root when batch appending and calls compress function expected num times', async () => {
42
- const db = levelup(createMemDown());
43
- const tree = await createDb(db, pedersen, 'test', 3);
44
- const leaves = Array.from({ length: 5 }, _ => randomBytes(32));
45
-
46
- pedersen.resetCounter();
47
- await tree.appendLeaves(leaves);
48
-
49
- // We append 5 leaves so to update values we do the following hashing on each level:
50
- // level2Node0 level2Node1 level2Node2
51
- // LEVEL2: [newLeaf0, newLeaf1], [newLeaf2, newLeaf3], [newLeaf4, INITIAL_LEAF].
52
- // level1Node0 level1Node1
53
- // LEVEL1: [level2Node0, level2Node1], [level2Node2, level2ZeroHash].
54
- // ROOT
55
- // LEVEL0: [level1Node0, level1Node1].
56
- const level2NumHashing = 3;
57
- const level1NumHashing = 2;
58
- const level0NumHashing = 1;
59
- const expectedNumHashing = level2NumHashing + level1NumHashing + level0NumHashing;
60
-
61
- expect(pedersen.compressCounter).toEqual(expectedNumHashing);
62
-
63
- const level2Node0 = pedersen.compress(leaves[0], leaves[1]);
64
- const level2Node1 = pedersen.compress(leaves[2], leaves[3]);
65
- const level2Node2 = pedersen.compress(leaves[4], INITIAL_LEAF);
66
-
67
- const level2ZeroHash = pedersen.compress(INITIAL_LEAF, INITIAL_LEAF);
68
-
69
- const level1Node0 = pedersen.compress(level2Node0, level2Node1);
70
- const level1Node1 = pedersen.compress(level2Node2, level2ZeroHash);
71
-
72
- const root = pedersen.compress(level1Node0, level1Node1);
73
-
74
- expect(tree.getRoot(true)).toEqual(root);
75
- });
76
- });