@aztec/foundation 3.0.0-nightly.20251002 → 3.0.0-nightly.20251003
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.
- package/dest/trees/unbalanced_merkle_tree.d.ts +0 -1
- package/dest/trees/unbalanced_merkle_tree.d.ts.map +1 -1
- package/dest/trees/unbalanced_merkle_tree.js +1 -1
- package/dest/trees/unbalanced_merkle_tree_calculator.d.ts +25 -22
- package/dest/trees/unbalanced_merkle_tree_calculator.d.ts.map +1 -1
- package/dest/trees/unbalanced_merkle_tree_calculator.js +124 -94
- package/dest/trees/unbalanced_tree_store.d.ts +1 -0
- package/dest/trees/unbalanced_tree_store.d.ts.map +1 -1
- package/dest/trees/unbalanced_tree_store.js +6 -0
- package/package.json +2 -2
- package/src/trees/unbalanced_merkle_tree.ts +1 -1
- package/src/trees/unbalanced_merkle_tree_calculator.ts +140 -92
- package/src/trees/unbalanced_tree_store.ts +5 -1
|
@@ -21,7 +21,6 @@ import { sha256Trunc } from '../crypto/sha256/index.js';
|
|
|
21
21
|
* (depth 1), the final tree has a depth of 5.
|
|
22
22
|
*/
|
|
23
23
|
export declare function computeUnbalancedMerkleTreeRoot(leaves: Buffer[], hasher?: typeof sha256Trunc): Buffer;
|
|
24
|
-
export declare function getMaxUnbalancedTreeDepth(numLeaves: number): number;
|
|
25
24
|
export declare function findLeafLevelAndIndex(numLeaves: number, leafIndex: number): {
|
|
26
25
|
level: number;
|
|
27
26
|
indexAtLevel: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unbalanced_merkle_tree.d.ts","sourceRoot":"","sources":["../../src/trees/unbalanced_merkle_tree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAGxD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,+BAA+B,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,MAAM,qBAAc,GAAG,MAAM,CA6B9F;
|
|
1
|
+
{"version":3,"file":"unbalanced_merkle_tree.d.ts","sourceRoot":"","sources":["../../src/trees/unbalanced_merkle_tree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAGxD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,+BAA+B,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,MAAM,qBAAc,GAAG,MAAM,CA6B9F;AAsDD,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;WAhC9D,MAAM;kBAAgB,MAAM;EAmCvC"}
|
|
@@ -57,7 +57,7 @@ function getMaxBalancedSubtreeDepth(numLeaves) {
|
|
|
57
57
|
return Math.floor(Math.log2(numLeaves));
|
|
58
58
|
}
|
|
59
59
|
/// Get the maximum depth of an unbalanced tree that can be created with the given number of leaves.
|
|
60
|
-
|
|
60
|
+
function getMaxUnbalancedTreeDepth(numLeaves) {
|
|
61
61
|
return Math.ceil(Math.log2(numLeaves));
|
|
62
62
|
}
|
|
63
63
|
function findPosition(rootLevel, leafLevel, numLeaves, indexOffset, targetIndex) {
|
|
@@ -1,45 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
1
|
+
import type { Hasher } from './hasher.js';
|
|
2
|
+
import { SiblingPath } from './sibling_path.js';
|
|
3
|
+
import { type TreeNodeLocation } from './unbalanced_tree_store.js';
|
|
4
|
+
export declare function computeCompressedUnbalancedMerkleTreeRoot(leaves: Buffer[], valueToCompress?: Buffer<ArrayBuffer>, hasher?: Hasher['hash']): Buffer;
|
|
4
5
|
/**
|
|
5
6
|
* An ephemeral unbalanced Merkle tree implementation.
|
|
6
7
|
* Follows the rollup implementation which greedily hashes pairs of nodes up the tree.
|
|
7
8
|
* Remaining rightmost nodes are shifted up until they can be paired.
|
|
9
|
+
* The values that match the `valueToCompress` are skipped and the sibling of the compressed leaf are shifted up until
|
|
10
|
+
* they can be paired.
|
|
8
11
|
* If there is only one leaf, the root is the leaf.
|
|
9
12
|
*/
|
|
10
13
|
export declare class UnbalancedMerkleTreeCalculator {
|
|
11
|
-
private
|
|
12
|
-
private
|
|
13
|
-
private
|
|
14
|
-
private
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
static create(height: number, hasher?: (left: Buffer, right: Buffer) => Promise<Buffer<ArrayBuffer>>): UnbalancedMerkleTreeCalculator;
|
|
14
|
+
private readonly leaves;
|
|
15
|
+
private readonly valueToCompress;
|
|
16
|
+
private readonly hasher;
|
|
17
|
+
private store;
|
|
18
|
+
private leafLocations;
|
|
19
|
+
constructor(leaves: Buffer[], valueToCompress: Buffer, hasher: Hasher['hash']);
|
|
20
|
+
static create(leaves: Buffer[], valueToCompress?: Buffer<ArrayBuffer>, hasher?: (left: Buffer, right: Buffer) => Buffer<ArrayBuffer>): UnbalancedMerkleTreeCalculator;
|
|
19
21
|
/**
|
|
20
22
|
* Returns the root of the tree.
|
|
21
23
|
* @returns The root of the tree.
|
|
22
24
|
*/
|
|
23
25
|
getRoot(): Buffer;
|
|
24
26
|
/**
|
|
25
|
-
* Returns a sibling path for the element
|
|
27
|
+
* Returns a sibling path for the element.
|
|
26
28
|
* @param value - The value of the element.
|
|
27
29
|
* @returns A sibling path for the element.
|
|
28
30
|
* Note: The sibling path is an array of sibling hashes, with the lowest hash (leaf hash) first, and the highest hash last.
|
|
29
31
|
*/
|
|
30
|
-
getSiblingPath<N extends number>(value:
|
|
32
|
+
getSiblingPath<N extends number>(value: Buffer): SiblingPath<N>;
|
|
31
33
|
/**
|
|
32
|
-
*
|
|
33
|
-
* @param
|
|
34
|
-
* @returns
|
|
34
|
+
* Returns a sibling path for the leaf at the given index.
|
|
35
|
+
* @param leafIndex - The index of the leaf.
|
|
36
|
+
* @returns A sibling path for the leaf.
|
|
35
37
|
*/
|
|
36
|
-
|
|
38
|
+
getSiblingPathByLeafIndex<N extends number>(leafIndex: number): SiblingPath<N>;
|
|
39
|
+
getLeafLocation(leafIndex: number): TreeNodeLocation;
|
|
37
40
|
/**
|
|
38
|
-
*
|
|
39
|
-
* @param leaves - The leaves
|
|
40
|
-
* @returns Resulting root of the tree.
|
|
41
|
+
* Adds leaves and nodes to the store. Updates the leafLocations.
|
|
42
|
+
* @param leaves - The leaves of the tree.
|
|
41
43
|
*/
|
|
42
|
-
private
|
|
43
|
-
private
|
|
44
|
+
private buildTree;
|
|
45
|
+
private shiftNodeUp;
|
|
46
|
+
private shiftChildrenUp;
|
|
44
47
|
}
|
|
45
48
|
//# sourceMappingURL=unbalanced_merkle_tree_calculator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unbalanced_merkle_tree_calculator.d.ts","sourceRoot":"","sources":["../../src/trees/unbalanced_merkle_tree_calculator.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"unbalanced_merkle_tree_calculator.d.ts","sourceRoot":"","sources":["../../src/trees/unbalanced_merkle_tree_calculator.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,KAAK,gBAAgB,EAAuB,MAAM,4BAA4B,CAAC;AAExF,wBAAgB,yCAAyC,CACvD,MAAM,EAAE,MAAM,EAAE,EAChB,eAAe,sBAAmB,EAClC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,GACtB,MAAM,CAGR;AAOD;;;;;;;GAOG;AACH,qBAAa,8BAA8B;IAKvC,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,eAAe;IAChC,OAAO,CAAC,QAAQ,CAAC,MAAM;IANzB,OAAO,CAAC,KAAK,CAAgC;IAC7C,OAAO,CAAC,aAAa,CAA0B;gBAG5B,MAAM,EAAE,MAAM,EAAE,EAChB,eAAe,EAAE,MAAM,EACvB,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC;IAUzC,MAAM,CAAC,MAAM,CACX,MAAM,EAAE,MAAM,EAAE,EAChB,eAAe,sBAAkB,EACjC,MAAM,IAAI,MAAM,MAAM,EAAE,OAAO,MAAM,KAAkD,MAAM,CAAC,WAAW,CAAC;IAK5G;;;OAGG;IACI,OAAO,IAAI,MAAM;IAIxB;;;;;OAKG;IACI,cAAc,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC;IAStE;;;;OAIG;IACI,yBAAyB,CAAC,CAAC,SAAS,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC;IAqB9E,eAAe,CAAC,SAAS,EAAE,MAAM;IAIxC;;;OAGG;IACH,OAAO,CAAC,SAAS;IAkDjB,OAAO,CAAC,WAAW;IAcnB,OAAO,CAAC,eAAe;CAoBxB"}
|
|
@@ -1,132 +1,162 @@
|
|
|
1
|
-
import { serializeToBuffer } from '@aztec/foundation/serialize';
|
|
2
|
-
import { SiblingPath } from '@aztec/foundation/trees';
|
|
3
1
|
import { sha256Trunc } from '../crypto/index.js';
|
|
4
|
-
|
|
2
|
+
import { SiblingPath } from './sibling_path.js';
|
|
3
|
+
import { UnbalancedTreeStore } from './unbalanced_tree_store.js';
|
|
4
|
+
export function computeCompressedUnbalancedMerkleTreeRoot(leaves, valueToCompress = Buffer.alloc(32), hasher) {
|
|
5
|
+
const calculator = UnbalancedMerkleTreeCalculator.create(leaves, valueToCompress, hasher);
|
|
6
|
+
return calculator.getRoot();
|
|
7
|
+
}
|
|
5
8
|
/**
|
|
6
9
|
* An ephemeral unbalanced Merkle tree implementation.
|
|
7
10
|
* Follows the rollup implementation which greedily hashes pairs of nodes up the tree.
|
|
8
11
|
* Remaining rightmost nodes are shifted up until they can be paired.
|
|
12
|
+
* The values that match the `valueToCompress` are skipped and the sibling of the compressed leaf are shifted up until
|
|
13
|
+
* they can be paired.
|
|
9
14
|
* If there is only one leaf, the root is the leaf.
|
|
10
15
|
*/ export class UnbalancedMerkleTreeCalculator {
|
|
11
|
-
|
|
16
|
+
leaves;
|
|
17
|
+
valueToCompress;
|
|
12
18
|
hasher;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
root;
|
|
19
|
-
constructor(maxDepth, hasher){
|
|
20
|
-
this.maxDepth = maxDepth;
|
|
19
|
+
store;
|
|
20
|
+
leafLocations;
|
|
21
|
+
constructor(leaves, valueToCompress, hasher){
|
|
22
|
+
this.leaves = leaves;
|
|
23
|
+
this.valueToCompress = valueToCompress;
|
|
21
24
|
this.hasher = hasher;
|
|
22
|
-
this.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
this.leafLocations = [];
|
|
26
|
+
if (leaves.length === 0) {
|
|
27
|
+
throw Error('Cannot create a compressed unbalanced tree with 0 leaves.');
|
|
28
|
+
}
|
|
29
|
+
this.store = new UnbalancedTreeStore(leaves.length);
|
|
30
|
+
this.buildTree();
|
|
26
31
|
}
|
|
27
|
-
static create(
|
|
32
|
+
static create(leaves, valueToCompress = Buffer.alloc(0), hasher = (left, right)=>sha256Trunc(Buffer.concat([
|
|
28
33
|
left,
|
|
29
34
|
right
|
|
30
|
-
])))
|
|
31
|
-
return new UnbalancedMerkleTreeCalculator(
|
|
35
|
+
]))) {
|
|
36
|
+
return new UnbalancedMerkleTreeCalculator(leaves, valueToCompress, hasher);
|
|
32
37
|
}
|
|
33
38
|
/**
|
|
34
39
|
* Returns the root of the tree.
|
|
35
40
|
* @returns The root of the tree.
|
|
36
41
|
*/ getRoot() {
|
|
37
|
-
return this.
|
|
42
|
+
return this.store.getRoot().value;
|
|
38
43
|
}
|
|
39
44
|
/**
|
|
40
|
-
* Returns a sibling path for the element
|
|
45
|
+
* Returns a sibling path for the element.
|
|
41
46
|
* @param value - The value of the element.
|
|
42
47
|
* @returns A sibling path for the element.
|
|
43
48
|
* Note: The sibling path is an array of sibling hashes, with the lowest hash (leaf hash) first, and the highest hash last.
|
|
44
49
|
*/ getSiblingPath(value) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const path = [];
|
|
49
|
-
const [depth, _index] = this.valueCache[serializeToBuffer(value).toString('hex')].split(':');
|
|
50
|
-
let level = parseInt(depth, 10);
|
|
51
|
-
let index = BigInt(_index);
|
|
52
|
-
while(level > 0){
|
|
53
|
-
const isRight = index & 0x01n;
|
|
54
|
-
const key = indexToKeyHash(level, isRight ? index - 1n : index + 1n);
|
|
55
|
-
const sibling = this.cache[key];
|
|
56
|
-
path.push(sibling);
|
|
57
|
-
level -= 1;
|
|
58
|
-
index >>= 1n;
|
|
50
|
+
const leafIndex = this.leaves.findIndex((leaf)=>leaf.equals(value));
|
|
51
|
+
if (leafIndex === -1) {
|
|
52
|
+
throw Error(`Leaf value ${value.toString('hex')} not found in tree.`);
|
|
59
53
|
}
|
|
60
|
-
return
|
|
54
|
+
return this.getSiblingPathByLeafIndex(leafIndex);
|
|
61
55
|
}
|
|
62
56
|
/**
|
|
63
|
-
*
|
|
64
|
-
* @param
|
|
65
|
-
* @returns
|
|
66
|
-
*/
|
|
67
|
-
if (this.
|
|
68
|
-
throw Error(`
|
|
57
|
+
* Returns a sibling path for the leaf at the given index.
|
|
58
|
+
* @param leafIndex - The index of the leaf.
|
|
59
|
+
* @returns A sibling path for the leaf.
|
|
60
|
+
*/ getSiblingPathByLeafIndex(leafIndex) {
|
|
61
|
+
if (leafIndex >= this.leaves.length) {
|
|
62
|
+
throw Error(`Leaf index ${leafIndex} out of bounds. Tree has ${this.leaves.length} leaves.`);
|
|
69
63
|
}
|
|
70
|
-
|
|
71
|
-
|
|
64
|
+
const leaf = this.leaves[leafIndex];
|
|
65
|
+
if (leaf.equals(this.valueToCompress)) {
|
|
66
|
+
throw Error(`Leaf at index ${leafIndex} has been compressed.`);
|
|
72
67
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
68
|
+
const path = [];
|
|
69
|
+
let location = this.leafLocations[leafIndex];
|
|
70
|
+
while(location.level > 0){
|
|
71
|
+
const sibling = this.store.getSibling(location);
|
|
72
|
+
path.push(sibling.value);
|
|
73
|
+
location = this.store.getParentLocation(location);
|
|
77
74
|
}
|
|
78
|
-
|
|
79
|
-
|
|
75
|
+
return new SiblingPath(path.length, path);
|
|
76
|
+
}
|
|
77
|
+
getLeafLocation(leafIndex) {
|
|
78
|
+
return this.leafLocations[leafIndex];
|
|
80
79
|
}
|
|
81
80
|
/**
|
|
82
|
-
*
|
|
83
|
-
* @param leaves - The leaves
|
|
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
|
-
|
|
81
|
+
* Adds leaves and nodes to the store. Updates the leafLocations.
|
|
82
|
+
* @param leaves - The leaves of the tree.
|
|
83
|
+
*/ buildTree() {
|
|
84
|
+
this.leafLocations = this.leaves.map((value, i)=>this.store.setLeaf(i, {
|
|
85
|
+
value,
|
|
86
|
+
leafIndex: i
|
|
87
|
+
}));
|
|
88
|
+
// Start with the leaves that are not compressed.
|
|
89
|
+
let toProcess = this.leafLocations.filter((_, i)=>!this.leaves[i].equals(this.valueToCompress));
|
|
90
|
+
if (!toProcess.length) {
|
|
91
|
+
// All leaves are compressed. Set 0 to the root.
|
|
92
|
+
this.store.setNode({
|
|
93
|
+
level: 0,
|
|
94
|
+
index: 0
|
|
95
|
+
}, {
|
|
96
|
+
value: Buffer.alloc(32)
|
|
97
|
+
});
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const level = toProcess[0].level;
|
|
101
|
+
for(let i = level; i > 0; i--){
|
|
102
|
+
const toProcessNext = [];
|
|
103
|
+
for (const location of toProcess){
|
|
104
|
+
if (location.level !== i) {
|
|
105
|
+
toProcessNext.push(location);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const parentLocation = this.store.getParentLocation(location);
|
|
109
|
+
if (this.store.getNode(parentLocation)) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const sibling = this.store.getSibling(location);
|
|
113
|
+
// If sibling is undefined, all its children are compressed.
|
|
114
|
+
const shouldShiftUp = !sibling || sibling.value.equals(this.valueToCompress);
|
|
115
|
+
if (shouldShiftUp) {
|
|
116
|
+
// The node becomes the parent if the sibling is a compressed leaf.
|
|
117
|
+
const isLeaf = this.shiftNodeUp(location, parentLocation);
|
|
118
|
+
if (!isLeaf) {
|
|
119
|
+
this.shiftChildrenUp(location);
|
|
120
|
+
}
|
|
114
121
|
} else {
|
|
115
|
-
//
|
|
116
|
-
|
|
117
|
-
|
|
122
|
+
// Hash the value with the (right) sibling and update the parent node.
|
|
123
|
+
const node = this.store.getNode(location);
|
|
124
|
+
const parentValue = this.hasher(node.value, sibling.value);
|
|
125
|
+
this.store.setNode(parentLocation, {
|
|
126
|
+
value: parentValue
|
|
127
|
+
});
|
|
118
128
|
}
|
|
129
|
+
// Add the parent location to be processed next.
|
|
130
|
+
toProcessNext.push(parentLocation);
|
|
119
131
|
}
|
|
120
|
-
|
|
121
|
-
thisLayer = nextLayer;
|
|
122
|
-
nextLayer = [];
|
|
132
|
+
toProcess = toProcessNext;
|
|
123
133
|
}
|
|
124
|
-
// return the root
|
|
125
|
-
return thisLayer[0];
|
|
126
134
|
}
|
|
127
|
-
|
|
128
|
-
const
|
|
129
|
-
this.
|
|
130
|
-
|
|
135
|
+
shiftNodeUp(fromLocation, toLocation) {
|
|
136
|
+
const node = this.store.getNode(fromLocation);
|
|
137
|
+
this.store.setNode(toLocation, node);
|
|
138
|
+
const isLeaf = node.leafIndex !== undefined;
|
|
139
|
+
if (isLeaf) {
|
|
140
|
+
// Update the location if the node is a leaf.
|
|
141
|
+
this.leafLocations[node.leafIndex] = toLocation;
|
|
142
|
+
}
|
|
143
|
+
return isLeaf;
|
|
144
|
+
}
|
|
145
|
+
shiftChildrenUp(parent) {
|
|
146
|
+
const [left, right] = this.store.getChildLocations(parent);
|
|
147
|
+
const level = parent.level;
|
|
148
|
+
const groupSize = 2 ** level;
|
|
149
|
+
const computeNewLocation = (index)=>({
|
|
150
|
+
level,
|
|
151
|
+
index: Math.floor(index / (groupSize * 2)) * groupSize + index % groupSize
|
|
152
|
+
});
|
|
153
|
+
const isLeftLeaf = this.shiftNodeUp(left, computeNewLocation(left.index));
|
|
154
|
+
const isRightLeaf = this.shiftNodeUp(right, computeNewLocation(right.index));
|
|
155
|
+
if (!isLeftLeaf) {
|
|
156
|
+
this.shiftChildrenUp(left);
|
|
157
|
+
}
|
|
158
|
+
if (!isRightLeaf) {
|
|
159
|
+
this.shiftChildrenUp(right);
|
|
160
|
+
}
|
|
131
161
|
}
|
|
132
162
|
}
|
|
@@ -12,6 +12,7 @@ export declare class UnbalancedTreeStore<T> {
|
|
|
12
12
|
getChildLocations({ level, index }: TreeNodeLocation): [TreeNodeLocation, TreeNodeLocation];
|
|
13
13
|
getLeaf(leafIndex: number): T | undefined;
|
|
14
14
|
getNode(location: TreeNodeLocation): T | undefined;
|
|
15
|
+
getRoot(): T | undefined;
|
|
15
16
|
getParent(location: TreeNodeLocation): T | undefined;
|
|
16
17
|
getSibling(location: TreeNodeLocation): T | undefined;
|
|
17
18
|
getChildren(location: TreeNodeLocation): [T | undefined, T | undefined];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unbalanced_tree_store.d.ts","sourceRoot":"","sources":["../../src/trees/unbalanced_tree_store.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;CACf;AAOD,qBAAa,mBAAmB,CAAC,CAAC;;gBAIpB,SAAS,EAAE,MAAM;IAI7B,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,gBAAgB;IAiBtD,OAAO,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,gBAAgB,EAAE,KAAK,EAAE,CAAC;IAWpD,iBAAiB,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,gBAAgB,GAAG,gBAAgB;IAQvE,kBAAkB,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,gBAAgB,GAAG,gBAAgB;IAQxE,iBAAiB,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,gBAAgB,GAAG,CAAC,gBAAgB,EAAE,gBAAgB,CAAC;IAM3F,OAAO,CAAC,SAAS,EAAE,MAAM;
|
|
1
|
+
{"version":3,"file":"unbalanced_tree_store.d.ts","sourceRoot":"","sources":["../../src/trees/unbalanced_tree_store.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;CACf;AAOD,qBAAa,mBAAmB,CAAC,CAAC;;gBAIpB,SAAS,EAAE,MAAM;IAI7B,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,gBAAgB;IAiBtD,OAAO,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,gBAAgB,EAAE,KAAK,EAAE,CAAC;IAWpD,iBAAiB,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,gBAAgB,GAAG,gBAAgB;IAQvE,kBAAkB,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,gBAAgB,GAAG,gBAAgB;IAQxE,iBAAiB,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,gBAAgB,GAAG,CAAC,gBAAgB,EAAE,gBAAgB,CAAC;IAM3F,OAAO,CAAC,SAAS,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS;IASzC,OAAO,CAAC,QAAQ,EAAE,gBAAgB,GAAG,CAAC,GAAG,SAAS;IAIlD,OAAO,IAAI,CAAC,GAAG,SAAS;IAIxB,SAAS,CAAC,QAAQ,EAAE,gBAAgB,GAAG,CAAC,GAAG,SAAS;IAKpD,UAAU,CAAC,QAAQ,EAAE,gBAAgB,GAAG,CAAC,GAAG,SAAS;IAKrD,WAAW,CAAC,QAAQ,EAAE,gBAAgB,GAAG,CAAC,CAAC,GAAG,SAAS,EAAE,CAAC,GAAG,SAAS,CAAC;CAQxE"}
|
|
@@ -73,6 +73,12 @@ export class UnbalancedTreeStore {
|
|
|
73
73
|
getNode(location) {
|
|
74
74
|
return this.#nodeMapping.get(this.#getKey(location))?.value;
|
|
75
75
|
}
|
|
76
|
+
getRoot() {
|
|
77
|
+
return this.getNode({
|
|
78
|
+
level: 0,
|
|
79
|
+
index: 0
|
|
80
|
+
});
|
|
81
|
+
}
|
|
76
82
|
getParent(location) {
|
|
77
83
|
const parentLocation = this.getParentLocation(location);
|
|
78
84
|
return this.getNode(parentLocation);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/foundation",
|
|
3
|
-
"version": "3.0.0-nightly.
|
|
3
|
+
"version": "3.0.0-nightly.20251003",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dest/index.js",
|
|
6
6
|
"types": "./dest/index.d.ts",
|
|
@@ -103,7 +103,7 @@
|
|
|
103
103
|
"testEnvironment": "../../foundation/src/jest/env.mjs"
|
|
104
104
|
},
|
|
105
105
|
"dependencies": {
|
|
106
|
-
"@aztec/bb.js": "3.0.0-nightly.
|
|
106
|
+
"@aztec/bb.js": "3.0.0-nightly.20251003",
|
|
107
107
|
"@koa/cors": "^5.0.0",
|
|
108
108
|
"@noble/curves": "=1.7.0",
|
|
109
109
|
"bn.js": "^5.2.1",
|
|
@@ -63,7 +63,7 @@ function getMaxBalancedSubtreeDepth(numLeaves: number) {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
/// Get the maximum depth of an unbalanced tree that can be created with the given number of leaves.
|
|
66
|
-
|
|
66
|
+
function getMaxUnbalancedTreeDepth(numLeaves: number) {
|
|
67
67
|
return Math.ceil(Math.log2(numLeaves));
|
|
68
68
|
}
|
|
69
69
|
|
|
@@ -1,37 +1,53 @@
|
|
|
1
|
-
import { type Bufferable, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
2
|
-
import type { AsyncHasher } from '@aztec/foundation/trees';
|
|
3
|
-
import { SiblingPath } from '@aztec/foundation/trees';
|
|
4
|
-
|
|
5
1
|
import { sha256Trunc } from '../crypto/index.js';
|
|
2
|
+
import type { Hasher } from './hasher.js';
|
|
3
|
+
import { SiblingPath } from './sibling_path.js';
|
|
4
|
+
import { type TreeNodeLocation, UnbalancedTreeStore } from './unbalanced_tree_store.js';
|
|
5
|
+
|
|
6
|
+
export function computeCompressedUnbalancedMerkleTreeRoot(
|
|
7
|
+
leaves: Buffer[],
|
|
8
|
+
valueToCompress = Buffer.alloc(32),
|
|
9
|
+
hasher?: Hasher['hash'],
|
|
10
|
+
): Buffer {
|
|
11
|
+
const calculator = UnbalancedMerkleTreeCalculator.create(leaves, valueToCompress, hasher);
|
|
12
|
+
return calculator.getRoot();
|
|
13
|
+
}
|
|
6
14
|
|
|
7
|
-
|
|
15
|
+
interface TreeNode {
|
|
16
|
+
value: Buffer;
|
|
17
|
+
leafIndex?: number;
|
|
18
|
+
}
|
|
8
19
|
|
|
9
20
|
/**
|
|
10
21
|
* An ephemeral unbalanced Merkle tree implementation.
|
|
11
22
|
* Follows the rollup implementation which greedily hashes pairs of nodes up the tree.
|
|
12
23
|
* Remaining rightmost nodes are shifted up until they can be paired.
|
|
24
|
+
* The values that match the `valueToCompress` are skipped and the sibling of the compressed leaf are shifted up until
|
|
25
|
+
* they can be paired.
|
|
13
26
|
* If there is only one leaf, the root is the leaf.
|
|
14
27
|
*/
|
|
15
28
|
export class UnbalancedMerkleTreeCalculator {
|
|
16
|
-
|
|
17
|
-
private
|
|
18
|
-
// This map stores value -> index and depth, since we have variable depth
|
|
19
|
-
private valueCache: { [key: string]: string } = {};
|
|
20
|
-
protected size: bigint = 0n;
|
|
21
|
-
|
|
22
|
-
root: Buffer = Buffer.alloc(32);
|
|
29
|
+
private store: UnbalancedTreeStore<TreeNode>;
|
|
30
|
+
private leafLocations: TreeNodeLocation[] = [];
|
|
23
31
|
|
|
24
32
|
public constructor(
|
|
25
|
-
private
|
|
26
|
-
private
|
|
27
|
-
|
|
33
|
+
private readonly leaves: Buffer[],
|
|
34
|
+
private readonly valueToCompress: Buffer,
|
|
35
|
+
private readonly hasher: Hasher['hash'],
|
|
36
|
+
) {
|
|
37
|
+
if (leaves.length === 0) {
|
|
38
|
+
throw Error('Cannot create a compressed unbalanced tree with 0 leaves.');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
this.store = new UnbalancedTreeStore(leaves.length);
|
|
42
|
+
this.buildTree();
|
|
43
|
+
}
|
|
28
44
|
|
|
29
45
|
static create(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
46
|
+
leaves: Buffer[],
|
|
47
|
+
valueToCompress = Buffer.alloc(0),
|
|
48
|
+
hasher = (left: Buffer, right: Buffer) => sha256Trunc(Buffer.concat([left, right])) as Buffer<ArrayBuffer>,
|
|
33
49
|
) {
|
|
34
|
-
return new UnbalancedMerkleTreeCalculator(
|
|
50
|
+
return new UnbalancedMerkleTreeCalculator(leaves, valueToCompress, hasher);
|
|
35
51
|
}
|
|
36
52
|
|
|
37
53
|
/**
|
|
@@ -39,108 +55,140 @@ export class UnbalancedMerkleTreeCalculator {
|
|
|
39
55
|
* @returns The root of the tree.
|
|
40
56
|
*/
|
|
41
57
|
public getRoot(): Buffer {
|
|
42
|
-
return this.
|
|
58
|
+
return this.store.getRoot()!.value;
|
|
43
59
|
}
|
|
44
60
|
|
|
45
61
|
/**
|
|
46
|
-
* Returns a sibling path for the element
|
|
62
|
+
* Returns a sibling path for the element.
|
|
47
63
|
* @param value - The value of the element.
|
|
48
64
|
* @returns A sibling path for the element.
|
|
49
65
|
* Note: The sibling path is an array of sibling hashes, with the lowest hash (leaf hash) first, and the highest hash last.
|
|
50
66
|
*/
|
|
51
|
-
public getSiblingPath<N extends number>(value:
|
|
52
|
-
|
|
53
|
-
|
|
67
|
+
public getSiblingPath<N extends number>(value: Buffer): SiblingPath<N> {
|
|
68
|
+
const leafIndex = this.leaves.findIndex(leaf => leaf.equals(value));
|
|
69
|
+
if (leafIndex === -1) {
|
|
70
|
+
throw Error(`Leaf value ${value.toString('hex')} not found in tree.`);
|
|
54
71
|
}
|
|
55
72
|
|
|
56
|
-
|
|
57
|
-
const [depth, _index] = this.valueCache[serializeToBuffer(value).toString('hex')].split(':');
|
|
58
|
-
let level = parseInt(depth, 10);
|
|
59
|
-
let index = BigInt(_index);
|
|
60
|
-
while (level > 0) {
|
|
61
|
-
const isRight = index & 0x01n;
|
|
62
|
-
const key = indexToKeyHash(level, isRight ? index - 1n : index + 1n);
|
|
63
|
-
const sibling = this.cache[key];
|
|
64
|
-
path.push(sibling);
|
|
65
|
-
level -= 1;
|
|
66
|
-
index >>= 1n;
|
|
67
|
-
}
|
|
68
|
-
return Promise.resolve(new SiblingPath<N>(parseInt(depth, 10) as N, path));
|
|
73
|
+
return this.getSiblingPathByLeafIndex(leafIndex);
|
|
69
74
|
}
|
|
70
75
|
|
|
71
76
|
/**
|
|
72
|
-
*
|
|
73
|
-
* @param
|
|
74
|
-
* @returns
|
|
77
|
+
* Returns a sibling path for the leaf at the given index.
|
|
78
|
+
* @param leafIndex - The index of the leaf.
|
|
79
|
+
* @returns A sibling path for the leaf.
|
|
75
80
|
*/
|
|
76
|
-
public
|
|
77
|
-
if (this.
|
|
78
|
-
throw Error(`
|
|
81
|
+
public getSiblingPathByLeafIndex<N extends number>(leafIndex: number): SiblingPath<N> {
|
|
82
|
+
if (leafIndex >= this.leaves.length) {
|
|
83
|
+
throw Error(`Leaf index ${leafIndex} out of bounds. Tree has ${this.leaves.length} leaves.`);
|
|
79
84
|
}
|
|
80
|
-
|
|
81
|
-
|
|
85
|
+
|
|
86
|
+
const leaf = this.leaves[leafIndex];
|
|
87
|
+
if (leaf.equals(this.valueToCompress)) {
|
|
88
|
+
throw Error(`Leaf at index ${leafIndex} has been compressed.`);
|
|
82
89
|
}
|
|
83
90
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
91
|
+
const path: Buffer[] = [];
|
|
92
|
+
let location = this.leafLocations[leafIndex];
|
|
93
|
+
while (location.level > 0) {
|
|
94
|
+
const sibling = this.store.getSibling(location)!;
|
|
95
|
+
path.push(sibling.value);
|
|
96
|
+
location = this.store.getParentLocation(location);
|
|
88
97
|
}
|
|
89
98
|
|
|
90
|
-
|
|
99
|
+
return new SiblingPath<N>(path.length as N, path);
|
|
100
|
+
}
|
|
91
101
|
|
|
92
|
-
|
|
102
|
+
public getLeafLocation(leafIndex: number) {
|
|
103
|
+
return this.leafLocations[leafIndex];
|
|
93
104
|
}
|
|
94
105
|
|
|
95
106
|
/**
|
|
96
|
-
*
|
|
97
|
-
* @param leaves - The leaves
|
|
98
|
-
* @returns Resulting root of the tree.
|
|
107
|
+
* Adds leaves and nodes to the store. Updates the leafLocations.
|
|
108
|
+
* @param leaves - The leaves of the tree.
|
|
99
109
|
*/
|
|
100
|
-
private
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
for (let i =
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
110
|
+
private buildTree() {
|
|
111
|
+
this.leafLocations = this.leaves.map((value, i) => this.store.setLeaf(i, { value, leafIndex: i }));
|
|
112
|
+
|
|
113
|
+
// Start with the leaves that are not compressed.
|
|
114
|
+
let toProcess = this.leafLocations.filter((_, i) => !this.leaves[i].equals(this.valueToCompress));
|
|
115
|
+
if (!toProcess.length) {
|
|
116
|
+
// All leaves are compressed. Set 0 to the root.
|
|
117
|
+
this.store.setNode({ level: 0, index: 0 }, { value: Buffer.alloc(32) });
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const level = toProcess[0].level;
|
|
122
|
+
for (let i = level; i > 0; i--) {
|
|
123
|
+
const toProcessNext = [];
|
|
124
|
+
for (const location of toProcess) {
|
|
125
|
+
if (location.level !== i) {
|
|
126
|
+
toProcessNext.push(location);
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const parentLocation = this.store.getParentLocation(location);
|
|
131
|
+
if (this.store.getNode(parentLocation)) {
|
|
132
|
+
// Parent has been updated by its (left) sibling.
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const sibling = this.store.getSibling(location);
|
|
137
|
+
// If sibling is undefined, all its children are compressed.
|
|
138
|
+
const shouldShiftUp = !sibling || sibling.value.equals(this.valueToCompress);
|
|
139
|
+
if (shouldShiftUp) {
|
|
140
|
+
// The node becomes the parent if the sibling is a compressed leaf.
|
|
141
|
+
const isLeaf = this.shiftNodeUp(location, parentLocation);
|
|
142
|
+
if (!isLeaf) {
|
|
143
|
+
this.shiftChildrenUp(location);
|
|
144
|
+
}
|
|
126
145
|
} else {
|
|
127
|
-
//
|
|
128
|
-
|
|
129
|
-
|
|
146
|
+
// Hash the value with the (right) sibling and update the parent node.
|
|
147
|
+
const node = this.store.getNode(location)!;
|
|
148
|
+
const parentValue = this.hasher(node.value, sibling.value);
|
|
149
|
+
this.store.setNode(parentLocation, { value: parentValue });
|
|
130
150
|
}
|
|
151
|
+
|
|
152
|
+
// Add the parent location to be processed next.
|
|
153
|
+
toProcessNext.push(parentLocation);
|
|
131
154
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
nextLayer = [];
|
|
155
|
+
|
|
156
|
+
toProcess = toProcessNext;
|
|
135
157
|
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private shiftNodeUp(fromLocation: TreeNodeLocation, toLocation: TreeNodeLocation): boolean {
|
|
161
|
+
const node = this.store.getNode(fromLocation)!;
|
|
136
162
|
|
|
137
|
-
|
|
138
|
-
|
|
163
|
+
this.store.setNode(toLocation, node);
|
|
164
|
+
|
|
165
|
+
const isLeaf = node.leafIndex !== undefined;
|
|
166
|
+
if (isLeaf) {
|
|
167
|
+
// Update the location if the node is a leaf.
|
|
168
|
+
this.leafLocations[node.leafIndex!] = toLocation;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return isLeaf;
|
|
139
172
|
}
|
|
140
173
|
|
|
141
|
-
private
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
174
|
+
private shiftChildrenUp(parent: TreeNodeLocation) {
|
|
175
|
+
const [left, right] = this.store.getChildLocations(parent);
|
|
176
|
+
|
|
177
|
+
const level = parent.level;
|
|
178
|
+
const groupSize = 2 ** level;
|
|
179
|
+
const computeNewLocation = (index: number) => ({
|
|
180
|
+
level,
|
|
181
|
+
index: Math.floor(index / (groupSize * 2)) * groupSize + (index % groupSize),
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
const isLeftLeaf = this.shiftNodeUp(left, computeNewLocation(left.index));
|
|
185
|
+
const isRightLeaf = this.shiftNodeUp(right, computeNewLocation(right.index));
|
|
186
|
+
|
|
187
|
+
if (!isLeftLeaf) {
|
|
188
|
+
this.shiftChildrenUp(left);
|
|
189
|
+
}
|
|
190
|
+
if (!isRightLeaf) {
|
|
191
|
+
this.shiftChildrenUp(right);
|
|
192
|
+
}
|
|
145
193
|
}
|
|
146
194
|
}
|
|
@@ -68,7 +68,7 @@ export class UnbalancedTreeStore<T> {
|
|
|
68
68
|
return [left, right];
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
getLeaf(leafIndex: number) {
|
|
71
|
+
getLeaf(leafIndex: number): T | undefined {
|
|
72
72
|
const { level, indexAtLevel } = findLeafLevelAndIndex(this.#numLeaves, leafIndex);
|
|
73
73
|
const location = {
|
|
74
74
|
level,
|
|
@@ -81,6 +81,10 @@ export class UnbalancedTreeStore<T> {
|
|
|
81
81
|
return this.#nodeMapping.get(this.#getKey(location))?.value;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
getRoot(): T | undefined {
|
|
85
|
+
return this.getNode({ level: 0, index: 0 });
|
|
86
|
+
}
|
|
87
|
+
|
|
84
88
|
getParent(location: TreeNodeLocation): T | undefined {
|
|
85
89
|
const parentLocation = this.getParentLocation(location);
|
|
86
90
|
return this.getNode(parentLocation);
|