@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,142 +0,0 @@
1
- import { CircuitsWasm } from '@aztec/circuits.js';
2
- import { IWasmModule } from '@aztec/foundation/wasm';
3
- import { Hasher, SiblingPath } from '@aztec/types';
4
-
5
- import { randomBytes } from 'crypto';
6
- import { default as levelup } from 'levelup';
7
-
8
- import { INITIAL_LEAF, Pedersen } from '../index.js';
9
- import { AppendOnlyTree } from '../interfaces/append_only_tree.js';
10
- import { UpdateOnlyTree } from '../interfaces/update_only_tree.js';
11
- import { appendLeaves } from './utils/append_leaves.js';
12
- import { createMemDown } from './utils/create_mem_down.js';
13
-
14
- const TEST_TREE_DEPTH = 2;
15
-
16
- export const standardBasedTreeTestSuite = (
17
- testName: string,
18
- createDb: (
19
- levelup: levelup.LevelUp,
20
- hasher: Hasher,
21
- name: string,
22
- depth: number,
23
- ) => Promise<AppendOnlyTree | UpdateOnlyTree>,
24
- ) => {
25
- describe(testName, () => {
26
- let wasm: IWasmModule;
27
- let pedersen: Pedersen;
28
- const values: Buffer[] = [];
29
-
30
- beforeAll(async () => {
31
- wasm = await CircuitsWasm.get();
32
- pedersen = new Pedersen(wasm);
33
-
34
- for (let i = 0; i < 4; ++i) {
35
- const v = Buffer.alloc(32, i + 1);
36
- v.writeUInt32BE(i, 28);
37
- values[i] = v;
38
- }
39
- });
40
-
41
- it('should have correct empty tree root for depth 32', async () => {
42
- const db = levelup(createMemDown());
43
- const tree = await createDb(db, pedersen, 'test', 32);
44
- const root = tree.getRoot(false);
45
- expect(root.toString('hex')).toEqual('20efbe2c7b675f26ab71689279908bbab33a6963e7e0dcb80e4c46583d094113');
46
- });
47
-
48
- it('should throw when appending beyond max index', async () => {
49
- const db = levelup(createMemDown());
50
- const tree = await createDb(db, pedersen, 'test', 2);
51
- const leaves = Array.from({ length: 5 }, _ => randomBytes(32));
52
- await expect(appendLeaves(tree, leaves)).rejects.toThrow();
53
- });
54
-
55
- it('should have correct root and sibling paths', async () => {
56
- const db = levelup(createMemDown());
57
- const tree = await createDb(db, pedersen, 'test', 2);
58
-
59
- const level1ZeroHash = pedersen.compress(INITIAL_LEAF, INITIAL_LEAF);
60
- expect(tree.getNumLeaves(false)).toEqual(0n);
61
- expect(tree.getRoot(false)).toEqual(pedersen.compress(level1ZeroHash, level1ZeroHash));
62
- expect(await tree.getSiblingPath(0n, false)).toEqual(
63
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash]),
64
- );
65
-
66
- await appendLeaves(tree, [values[0]]);
67
- expect(tree.getNumLeaves(true)).toEqual(1n);
68
- expect(tree.getNumLeaves(false)).toEqual(0n);
69
- expect(tree.getRoot(true)).toEqual(pedersen.compress(pedersen.compress(values[0], INITIAL_LEAF), level1ZeroHash));
70
- expect(await tree.getSiblingPath(0n, true)).toEqual(
71
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash]),
72
- );
73
- expect(tree.getRoot(false)).toEqual(pedersen.compress(level1ZeroHash, level1ZeroHash));
74
- expect(await tree.getSiblingPath(0n, false)).toEqual(
75
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash]),
76
- );
77
-
78
- await appendLeaves(tree, [values[1]]);
79
- expect(tree.getNumLeaves(true)).toEqual(2n);
80
- expect(tree.getRoot(true)).toEqual(pedersen.compress(pedersen.compress(values[0], values[1]), level1ZeroHash));
81
- expect(await tree.getSiblingPath(1n, true)).toEqual(
82
- new SiblingPath(TEST_TREE_DEPTH, [values[0], level1ZeroHash]),
83
- );
84
- expect(tree.getNumLeaves(false)).toEqual(0n);
85
- expect(tree.getRoot(false)).toEqual(pedersen.compress(level1ZeroHash, level1ZeroHash));
86
- expect(await tree.getSiblingPath(1n, false)).toEqual(
87
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash]),
88
- );
89
-
90
- await appendLeaves(tree, [values[2]]);
91
- expect(tree.getNumLeaves(true)).toEqual(3n);
92
- expect(tree.getRoot(true)).toEqual(
93
- pedersen.compress(pedersen.compress(values[0], values[1]), pedersen.compress(values[2], INITIAL_LEAF)),
94
- );
95
- expect(await tree.getSiblingPath(2n, true)).toEqual(
96
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, pedersen.compress(values[0], values[1])]),
97
- );
98
- expect(tree.getNumLeaves(false)).toEqual(0n);
99
- expect(tree.getRoot(false)).toEqual(pedersen.compress(level1ZeroHash, level1ZeroHash));
100
- expect(await tree.getSiblingPath(2n, false)).toEqual(
101
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash]),
102
- );
103
-
104
- await appendLeaves(tree, [values[3]]);
105
- expect(tree.getNumLeaves(true)).toEqual(4n);
106
- expect(tree.getRoot(true)).toEqual(
107
- pedersen.compress(pedersen.compress(values[0], values[1]), pedersen.compress(values[2], values[3])),
108
- );
109
- expect(await tree.getSiblingPath(3n, true)).toEqual(
110
- new SiblingPath(TEST_TREE_DEPTH, [values[2], pedersen.compress(values[0], values[1])]),
111
- );
112
- expect(tree.getNumLeaves(false)).toEqual(0n);
113
- expect(tree.getRoot(false)).toEqual(pedersen.compress(level1ZeroHash, level1ZeroHash));
114
- expect(await tree.getSiblingPath(3n, false)).toEqual(
115
- new SiblingPath(TEST_TREE_DEPTH, [INITIAL_LEAF, level1ZeroHash]),
116
- );
117
- // Lifted from memory_tree.test.cpp to ensure consistency.
118
- //expect(root.toString('hex')).toEqual('0bf2e78afd70f72b0e6eafb03c41faef167a82441b05e517cdf35d813302061f');
119
- expect(await tree.getSiblingPath(0n, true)).toEqual(
120
- new SiblingPath(TEST_TREE_DEPTH, [values[1], pedersen.compress(values[2], values[3])]),
121
- );
122
- expect(await tree.getSiblingPath(1n, true)).toEqual(
123
- new SiblingPath(TEST_TREE_DEPTH, [values[0], pedersen.compress(values[2], values[3])]),
124
- );
125
- expect(await tree.getSiblingPath(2n, true)).toEqual(
126
- new SiblingPath(TEST_TREE_DEPTH, [values[3], pedersen.compress(values[0], values[1])]),
127
- );
128
- expect(await tree.getSiblingPath(3n, true)).toEqual(
129
- new SiblingPath(TEST_TREE_DEPTH, [values[2], pedersen.compress(values[0], values[1])]),
130
- );
131
-
132
- await tree.commit();
133
- // now committed state should equal uncommitted state
134
- expect(await tree.getSiblingPath(0n, false)).toEqual(await tree.getSiblingPath(0n, true));
135
- expect(await tree.getSiblingPath(1n, false)).toEqual(await tree.getSiblingPath(1n, true));
136
- expect(await tree.getSiblingPath(2n, false)).toEqual(await tree.getSiblingPath(2n, true));
137
- expect(await tree.getSiblingPath(3n, false)).toEqual(await tree.getSiblingPath(3n, true));
138
- expect(tree.getNumLeaves(false)).toEqual(tree.getNumLeaves(true));
139
- expect(tree.getRoot(false)).toEqual(tree.getRoot(true));
140
- });
141
- });
142
- };
@@ -1,165 +0,0 @@
1
- import { CircuitsWasm } from '@aztec/circuits.js';
2
- import { IWasmModule } from '@aztec/foundation/wasm';
3
- import { Hasher, SiblingPath } from '@aztec/types';
4
-
5
- import { default as levelup } from 'levelup';
6
-
7
- import { Pedersen } from '../index.js';
8
- import { AppendOnlyTree } from '../interfaces/append_only_tree.js';
9
- import { UpdateOnlyTree } from '../interfaces/update_only_tree.js';
10
- import { appendLeaves } from './utils/append_leaves.js';
11
- import { createMemDown } from './utils/create_mem_down.js';
12
-
13
- const expectSameTrees = async (
14
- tree1: AppendOnlyTree | UpdateOnlyTree,
15
- tree2: AppendOnlyTree | UpdateOnlyTree,
16
- includeUncommitted = true,
17
- ) => {
18
- const size = tree1.getNumLeaves(includeUncommitted);
19
- expect(size).toBe(tree2.getNumLeaves(includeUncommitted));
20
- expect(tree1.getRoot(includeUncommitted).toString('hex')).toBe(tree2.getRoot(includeUncommitted).toString('hex'));
21
-
22
- for (let i = 0; i < size; ++i) {
23
- const siblingPath1 = await tree1.getSiblingPath(BigInt(i), includeUncommitted);
24
- const siblingPath2 = await tree2.getSiblingPath(BigInt(i), includeUncommitted);
25
- expect(siblingPath2).toStrictEqual(siblingPath1);
26
- }
27
- };
28
-
29
- export const treeTestSuite = (
30
- testName: string,
31
- createDb: (
32
- levelup: levelup.LevelUp,
33
- hasher: Hasher,
34
- name: string,
35
- depth: number,
36
- ) => Promise<AppendOnlyTree | UpdateOnlyTree>,
37
- createFromName: (levelup: levelup.LevelUp, hasher: Hasher, name: string) => Promise<AppendOnlyTree | UpdateOnlyTree>,
38
- ) => {
39
- describe(testName, () => {
40
- const values: Buffer[] = [];
41
- let wasm: IWasmModule;
42
- let pedersen: Pedersen;
43
-
44
- beforeAll(() => {
45
- for (let i = 0; i < 32; ++i) {
46
- const v = Buffer.alloc(32, i + 1);
47
- v.writeUInt32BE(i, 28);
48
- values[i] = v;
49
- }
50
- });
51
-
52
- beforeEach(async () => {
53
- wasm = await CircuitsWasm.get();
54
- pedersen = new Pedersen(wasm);
55
- });
56
-
57
- it('should revert changes on rollback', async () => {
58
- const levelDownEmpty = createMemDown();
59
- const dbEmpty = levelup(levelDownEmpty);
60
- const emptyTree = await createDb(dbEmpty, pedersen, 'test', 10);
61
-
62
- const levelDown = createMemDown();
63
- const db = levelup(levelDown);
64
- const tree = await createDb(db, pedersen, 'test2', 10);
65
- await appendLeaves(tree, values.slice(0, 4));
66
-
67
- const firstRoot = tree.getRoot(true);
68
- expect(firstRoot).not.toEqual(emptyTree.getRoot(true));
69
- // committed root should still be the empty root
70
- expect(tree.getRoot(false)).toEqual(emptyTree.getRoot(false));
71
-
72
- await tree.rollback();
73
-
74
- // both committed and uncommitted trees should be equal to the empty tree
75
- await expectSameTrees(tree, emptyTree, true);
76
- await expectSameTrees(tree, emptyTree, false);
77
-
78
- // append the leaves again
79
- await appendLeaves(tree, values.slice(0, 4));
80
-
81
- expect(tree.getRoot(true)).toEqual(firstRoot);
82
- // committed root should still be the empty root
83
- expect(tree.getRoot(false)).toEqual(emptyTree.getRoot(false));
84
-
85
- expect(firstRoot).not.toEqual(emptyTree.getRoot(true));
86
-
87
- await tree.rollback();
88
-
89
- // both committed and uncommitted trees should be equal to the empty tree
90
- await expectSameTrees(tree, emptyTree, true);
91
- await expectSameTrees(tree, emptyTree, false);
92
- });
93
-
94
- it('should not revert changes after commit', async () => {
95
- const levelDownEmpty = createMemDown();
96
- const dbEmpty = levelup(levelDownEmpty);
97
- const emptyTree = await createDb(dbEmpty, pedersen, 'test', 10);
98
-
99
- const levelDown = createMemDown();
100
- const db = levelup(levelDown);
101
- const tree = await createDb(db, pedersen, 'test2', 10);
102
- await appendLeaves(tree, values.slice(0, 4));
103
-
104
- expect(tree.getRoot(true)).not.toEqual(emptyTree.getRoot(true));
105
- // committed root should still be the empty root
106
- expect(tree.getRoot(false)).toEqual(emptyTree.getRoot(false));
107
-
108
- await tree.commit();
109
- await tree.rollback();
110
-
111
- expect(tree.getRoot(true)).not.toEqual(emptyTree.getRoot(true));
112
- expect(tree.getRoot(false)).not.toEqual(emptyTree.getRoot(true));
113
- });
114
-
115
- it('should be able to restore from previous committed data', async () => {
116
- const levelDown = createMemDown();
117
- const db = levelup(levelDown);
118
- const tree = await createDb(db, pedersen, 'test', 10);
119
- await appendLeaves(tree, values.slice(0, 4));
120
- await tree.commit();
121
-
122
- const db2 = levelup(levelDown);
123
- const tree2 = await createFromName(db2, pedersen, 'test');
124
-
125
- // both committed and uncommitted should be equal to the restored data
126
- expect(tree.getRoot(true)).toEqual(tree2.getRoot(true));
127
- expect(tree.getRoot(false)).toEqual(tree2.getRoot(false));
128
- for (let i = 0; i < 4; ++i) {
129
- expect(await tree.getSiblingPath(BigInt(i), true)).toEqual(await tree2.getSiblingPath(BigInt(i), true));
130
- expect(await tree.getSiblingPath(BigInt(i), false)).toEqual(await tree2.getSiblingPath(BigInt(i), false));
131
- }
132
- });
133
-
134
- it('should throw an error if previous data does not exist for the given name', async () => {
135
- const db = levelup(createMemDown());
136
- await expect(
137
- (async () => {
138
- await createFromName(db, pedersen, 'a_whole_new_tree');
139
- })(),
140
- ).rejects.toThrow();
141
- });
142
-
143
- it('should serialize sibling path data to a buffer and be able to deserialize it back', async () => {
144
- const db = levelup(createMemDown());
145
- const tree = await createDb(db, pedersen, 'test', 10);
146
- await appendLeaves(tree, values.slice(0, 1));
147
-
148
- const siblingPath = await tree.getSiblingPath(0n, true);
149
- const buf = siblingPath.toBuffer();
150
- const recovered = SiblingPath.fromBuffer(buf);
151
- expect(recovered).toEqual(siblingPath);
152
- const deserialized = SiblingPath.deserialize(buf);
153
- expect(deserialized.elem).toEqual(siblingPath);
154
- expect(deserialized.adv).toBe(4 + 10 * 32);
155
-
156
- const dummyData = Buffer.alloc(23, 1);
157
- const paddedBuf = Buffer.concat([dummyData, buf]);
158
- const recovered2 = SiblingPath.fromBuffer(paddedBuf, 23);
159
- expect(recovered2).toEqual(siblingPath);
160
- const deserialized2 = SiblingPath.deserialize(buf);
161
- expect(deserialized2.elem).toEqual(siblingPath);
162
- expect(deserialized2.adv).toBe(4 + 10 * 32);
163
- });
164
- });
165
- };
@@ -1,15 +0,0 @@
1
- import { AppendOnlyTree } from '../../interfaces/append_only_tree.js';
2
- import { UpdateOnlyTree } from '../../interfaces/update_only_tree.js';
3
-
4
- export const appendLeaves = async (tree: AppendOnlyTree | UpdateOnlyTree, leaves: Buffer[]) => {
5
- if ('appendLeaves' in tree) {
6
- // This branch is used by the standard tree test suite, which implements appendLeaves
7
- await tree.appendLeaves(leaves);
8
- } else {
9
- // This branch is used by the sparse tree test suite, which does not implement appendLeaves
10
- for (const value of leaves) {
11
- const index = tree.getNumLeaves(true);
12
- await tree.updateLeaf(value, index);
13
- }
14
- }
15
- };
@@ -1,3 +0,0 @@
1
- import { type MemDown, default as memdown } from 'memdown';
2
-
3
- export const createMemDown = () => (memdown as any)() as MemDown<any, any>;
@@ -1,30 +0,0 @@
1
- import { Pedersen } from '../../index.js';
2
-
3
- /**
4
- * A test utility allowing us to count the number of times the compress function has been called.
5
- */
6
- export class PedersenWithCounter extends Pedersen {
7
- /**
8
- * The number of times the compress function has been called.
9
- */
10
- public compressCounter = 0;
11
-
12
- /**
13
- * Compresses two 32-byte hashes.
14
- * @param lhs - The first hash.
15
- * @param rhs - The second hash.
16
- * @returns The new 32-byte hash.
17
- */
18
- public compress(lhs: Uint8Array, rhs: Uint8Array): Buffer {
19
- this.compressCounter++;
20
- return super.compress(lhs, rhs);
21
- }
22
-
23
- /**
24
- * Resets the compress counter.
25
- * @returns void
26
- */
27
- public resetCounter() {
28
- this.compressCounter = 0;
29
- }
30
- }
package/tsconfig.json DELETED
@@ -1,20 +0,0 @@
1
- {
2
- "extends": "..",
3
- "compilerOptions": {
4
- "outDir": "dest",
5
- "rootDir": "src",
6
- "tsBuildInfoFile": ".tsbuildinfo"
7
- },
8
- "references": [
9
- {
10
- "path": "../circuits.js"
11
- },
12
- {
13
- "path": "../foundation"
14
- },
15
- {
16
- "path": "../types"
17
- }
18
- ],
19
- "include": ["src"]
20
- }