@aztec/foundation 4.0.0-nightly.20260113 → 4.0.0-nightly.20260114
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/serialize/buffer_reader.d.ts +7 -4
- package/dest/serialize/buffer_reader.d.ts.map +1 -1
- package/dest/serialize/buffer_reader.js +13 -4
- package/dest/trees/{balanced_merkle_tree.d.ts → balanced_merkle_tree_root.d.ts} +1 -5
- package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -0
- package/dest/trees/{balanced_merkle_tree.js → balanced_merkle_tree_root.js} +1 -15
- package/dest/trees/hasher.d.ts +4 -1
- package/dest/trees/hasher.d.ts.map +1 -1
- package/dest/trees/hasher.js +15 -5
- package/dest/trees/index.d.ts +4 -4
- package/dest/trees/index.d.ts.map +1 -1
- package/dest/trees/index.js +3 -3
- package/dest/trees/unbalanced_merkle_tree_calculator.d.ts +2 -2
- package/dest/trees/unbalanced_merkle_tree_calculator.d.ts.map +1 -1
- package/dest/trees/unbalanced_merkle_tree_calculator.js +1 -1
- package/dest/trees/{unbalanced_merkle_tree.d.ts → unbalanced_merkle_tree_root.d.ts} +3 -7
- package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -0
- package/dest/trees/{unbalanced_merkle_tree.js → unbalanced_merkle_tree_root.js} +8 -52
- package/dest/trees/unbalanced_tree_store.d.ts +5 -1
- package/dest/trees/unbalanced_tree_store.d.ts.map +1 -1
- package/dest/trees/unbalanced_tree_store.js +49 -1
- package/package.json +2 -2
- package/src/serialize/buffer_reader.ts +21 -9
- package/src/trees/{balanced_merkle_tree.ts → balanced_merkle_tree_root.ts} +1 -14
- package/src/trees/hasher.ts +13 -0
- package/src/trees/index.ts +3 -3
- package/src/trees/unbalanced_merkle_tree_calculator.ts +1 -2
- package/src/trees/{unbalanced_merkle_tree.ts → unbalanced_merkle_tree_root.ts} +8 -66
- package/src/trees/unbalanced_tree_store.ts +57 -2
- package/dest/trees/balanced_merkle_tree.d.ts.map +0 -1
- package/dest/trees/unbalanced_merkle_tree.d.ts.map +0 -1
|
@@ -128,6 +128,7 @@ export declare class BufferReader {
|
|
|
128
128
|
* deserializing each one using the 'fromBuffer' method of 'itemDeserializer'.
|
|
129
129
|
*
|
|
130
130
|
* @param itemDeserializer - Object with 'fromBuffer' method to deserialize vector elements.
|
|
131
|
+
* @param maxSize - Optional maximum allowed size for the vector. If the size exceeds this, an error is thrown.
|
|
131
132
|
* @returns An array of deserialized elements of type T.
|
|
132
133
|
*/
|
|
133
134
|
readVector<T>(itemDeserializer: {
|
|
@@ -135,7 +136,7 @@ export declare class BufferReader {
|
|
|
135
136
|
* A method to deserialize data from a buffer.
|
|
136
137
|
*/
|
|
137
138
|
fromBuffer: (reader: BufferReader) => T;
|
|
138
|
-
}): T[];
|
|
139
|
+
}, maxSize?: number): T[];
|
|
139
140
|
/**
|
|
140
141
|
* Reads a vector of fixed size from the buffer and deserializes its elements using the provided itemDeserializer object.
|
|
141
142
|
* The 'itemDeserializer' object should have a 'fromBuffer' method that takes a BufferReader instance and returns the deserialized element.
|
|
@@ -202,18 +203,20 @@ export declare class BufferReader {
|
|
|
202
203
|
* The method first reads the size of the string, then reads the corresponding
|
|
203
204
|
* number of bytes from the buffer and converts them to a string.
|
|
204
205
|
*
|
|
206
|
+
* @param maxSize - Optional maximum allowed size for the string buffer. If the size exceeds this, an error is thrown.
|
|
205
207
|
* @returns The read string from the buffer.
|
|
206
208
|
*/
|
|
207
|
-
readString(): string;
|
|
209
|
+
readString(maxSize?: number): string;
|
|
208
210
|
/**
|
|
209
211
|
* Reads a buffer from the current position of the reader and advances the index.
|
|
210
212
|
* The method first reads the size (number) of bytes to be read, and then returns
|
|
211
213
|
* a Buffer with that size containing the bytes. Useful for reading variable-length
|
|
212
214
|
* binary data encoded as (size, data) format.
|
|
213
215
|
*
|
|
216
|
+
* @param maxSize - Optional maximum allowed size for the buffer. If the size exceeds this, an error is thrown.
|
|
214
217
|
* @returns A Buffer containing the read bytes.
|
|
215
218
|
*/
|
|
216
|
-
readBuffer(): Buffer;
|
|
219
|
+
readBuffer(maxSize?: number): Buffer;
|
|
217
220
|
/**
|
|
218
221
|
* Reads a buffer from the current position of the reader and advances the index.
|
|
219
222
|
* The method first reads the size (number) of bytes to be read, and then returns
|
|
@@ -261,4 +264,4 @@ export interface FromBuffer<T> {
|
|
|
261
264
|
*/
|
|
262
265
|
fromBuffer(buffer: Buffer): T;
|
|
263
266
|
}
|
|
264
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
267
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnVmZmVyX3JlYWRlci5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3NlcmlhbGl6ZS9idWZmZXJfcmVhZGVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxFQUFFLEtBQUssRUFBRSxNQUFNLFlBQVksQ0FBQztBQUV4Qzs7Ozs7Ozs7Ozs7Ozs7OztHQWdCRztBQUNILHFCQUFhLFlBQVk7O0lBR3JCLE9BQU8sQ0FBQyxNQUFNO0lBRmhCLE9BQU8sQ0FBQyxLQUFLLENBQVM7SUFDdEIsWUFDVSxNQUFNLEVBQUUsTUFBTSxFQUN0QixNQUFNLFNBQUksRUFHWDtJQUVEOzs7Ozs7O09BT0c7SUFDSCxPQUFjLFFBQVEsQ0FBQyxjQUFjLEVBQUUsVUFBVSxHQUFHLE1BQU0sR0FBRyxZQUFZLEdBQUcsWUFBWSxDQVV2RjtJQUVELDBFQUEwRTtJQUNuRSxPQUFPLElBQUksT0FBTyxDQUV4QjtJQUVEOzs7OztPQUtHO0lBQ0ksVUFBVSxJQUFJLE1BQU0sQ0FJMUI7SUFFRDs7OztPQUlHO0lBQ0ksV0FBVyxDQUFDLENBQUMsU0FBUyxNQUFNLEVBQUUsS0FBSyxFQUFFLENBQUMsR0FBR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WAAW,IAAI,MAAM,CAU3B;IAED,4BAA4B;IACrB,UAAU,IAAI,MAAM,CAE1B;IAED;;;;;OAKG;IACI,UAAU,IAAI,MAAM,CAI1B;IAED;;;;;OAKG;IACI,SAAS,IAAI,MAAM,CAIzB;IAED;;;;;;OAMG;IACI,WAAW,IAAI,OAAO,CAI5B;IAED;;;;;;;OAOG;IACI,SAAS,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAIlC;IAED,yCAAyC;IAClC,SAAS,IAAI,MAAM,CAIzB;IAED;;;;;OAKG;IACI,gBAAgB,IAAI,MAAM,EAAE,CAIlC;IAED;;;;;OAKG;IACI,iBAAiB,IAAI,MAAM,EAAE,CAInC;IAED
|
|
1
|
+
{"version":3,"file":"buffer_reader.d.ts","sourceRoot":"","sources":["../../src/serialize/buffer_reader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,YAAY;;IAGrB,OAAO,CAAC,MAAM;IAFhB,OAAO,CAAC,KAAK,CAAS;IACtB,YACU,MAAM,EAAE,MAAM,EACtB,MAAM,SAAI,EAGX;IAED;;;;;;;OAOG;IACH,OAAc,QAAQ,CAAC,cAAc,EAAE,UAAU,GAAG,MAAM,GAAG,YAAY,GAAG,YAAY,CAUvF;IAED,0EAA0E;IACnE,OAAO,IAAI,OAAO,CAExB;IAED;;;;;OAKG;IACI,UAAU,IAAI,MAAM,CAI1B;IAED;;;;OAIG;IACI,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAG/D;IAED;;;;;;;OAOG;IACI,UAAU,IAAI,MAAM,CAO1B;IAED;;;;;;;OAOG;IACI,WAAW,IAAI,MAAM,CAU3B;IAED;;;;;;;OAOG;IACI,WAAW,IAAI,MAAM,CAU3B;IAED,4BAA4B;IACrB,UAAU,IAAI,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AC,CAAC,EAAE,MAAM,GAAG,MAAM,CAGnC;IAED;;;;;;;OAOG;IACI,UAAU,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAE1C;IAED;;;;;;;;OAQG;IACI,UAAU,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAO1C;IAED;;;;;;;OAOG;IACI,cAAc,IAAI,UAAU,CAIlC;IAED;;;;;;;;OAQG;IACI,OAAO,CAAC,CAAC,EAAE,YAAY,EAAE;QAC9B;;WAEG;QACH,UAAU,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,CAAC,CAAC;KACzC,GAAG;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,CAAA;KAAE,CASvB;IAED;;;OAGG;IACI,SAAS,IAAI,MAAM,CAEzB;IAED;;;OAGG;IACI,cAAc,IAAI,MAAM,CAE9B;CASF;AAED;;GAEG;AACH,MAAM,WAAW,UAAU,CAAC,CAAC;IAC3B;;;OAGG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC,CAAC;CAC/B"}
|
|
@@ -181,9 +181,13 @@
|
|
|
181
181
|
* deserializing each one using the 'fromBuffer' method of 'itemDeserializer'.
|
|
182
182
|
*
|
|
183
183
|
* @param itemDeserializer - Object with 'fromBuffer' method to deserialize vector elements.
|
|
184
|
+
* @param maxSize - Optional maximum allowed size for the vector. If the size exceeds this, an error is thrown.
|
|
184
185
|
* @returns An array of deserialized elements of type T.
|
|
185
|
-
*/ readVector(itemDeserializer) {
|
|
186
|
+
*/ readVector(itemDeserializer, maxSize) {
|
|
186
187
|
const size = this.readNumber();
|
|
188
|
+
if (maxSize !== undefined && size > maxSize) {
|
|
189
|
+
throw new Error(`Vector size ${size} exceeds maximum allowed ${maxSize}`);
|
|
190
|
+
}
|
|
187
191
|
const result = new Array(size);
|
|
188
192
|
for(let i = 0; i < size; i++){
|
|
189
193
|
result[i] = itemDeserializer.fromBuffer(this);
|
|
@@ -266,9 +270,10 @@
|
|
|
266
270
|
* The method first reads the size of the string, then reads the corresponding
|
|
267
271
|
* number of bytes from the buffer and converts them to a string.
|
|
268
272
|
*
|
|
273
|
+
* @param maxSize - Optional maximum allowed size for the string buffer. If the size exceeds this, an error is thrown.
|
|
269
274
|
* @returns The read string from the buffer.
|
|
270
|
-
*/ readString() {
|
|
271
|
-
return this.readBuffer().toString();
|
|
275
|
+
*/ readString(maxSize) {
|
|
276
|
+
return this.readBuffer(maxSize).toString();
|
|
272
277
|
}
|
|
273
278
|
/**
|
|
274
279
|
* Reads a buffer from the current position of the reader and advances the index.
|
|
@@ -276,9 +281,13 @@
|
|
|
276
281
|
* a Buffer with that size containing the bytes. Useful for reading variable-length
|
|
277
282
|
* binary data encoded as (size, data) format.
|
|
278
283
|
*
|
|
284
|
+
* @param maxSize - Optional maximum allowed size for the buffer. If the size exceeds this, an error is thrown.
|
|
279
285
|
* @returns A Buffer containing the read bytes.
|
|
280
|
-
*/ readBuffer() {
|
|
286
|
+
*/ readBuffer(maxSize) {
|
|
281
287
|
const size = this.readNumber();
|
|
288
|
+
if (maxSize !== undefined && size > maxSize) {
|
|
289
|
+
throw new Error(`Buffer size ${size} exceeds maximum allowed ${maxSize}`);
|
|
290
|
+
}
|
|
282
291
|
this.#rangeCheck(size);
|
|
283
292
|
return this.readBytes(size);
|
|
284
293
|
}
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import type { AsyncHasher, Hasher } from './hasher.js';
|
|
2
|
-
export declare const shaMerkleHash: Hasher['hash'];
|
|
3
|
-
export declare const pedersenMerkleHash: AsyncHasher['hash'];
|
|
4
|
-
export declare const poseidonMerkleHash: AsyncHasher['hash'];
|
|
5
1
|
export declare const computeBalancedShaRoot: (leaves: Buffer<ArrayBufferLike>[]) => Buffer<ArrayBufferLike>;
|
|
6
2
|
export declare const computeBalancedPedersenRoot: (leaves: Buffer<ArrayBufferLike>[]) => Promise<Buffer<ArrayBufferLike>>;
|
|
7
3
|
export declare const computeBalancedPoseidonRoot: (leaves: Buffer<ArrayBufferLike>[]) => Promise<Buffer<ArrayBufferLike>>;
|
|
@@ -19,4 +15,4 @@ export declare function computeBalancedMerkleTreeRoot(leaves: Buffer[], hasher?:
|
|
|
19
15
|
* @throws If the number of leaves is not a power of two.
|
|
20
16
|
*/
|
|
21
17
|
export declare function computeBalancedMerkleTreeRootAsync(leaves: Buffer[], hasher?: (lhs: Uint8Array<ArrayBufferLike>, rhs: Uint8Array<ArrayBufferLike>) => Promise<Buffer<ArrayBuffer>>): Promise<Buffer>;
|
|
22
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
18
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmFsYW5jZWRfbWVya2xlX3RyZWVfcm9vdC5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3RyZWVzL2JhbGFuY2VkX21lcmtsZV90cmVlX3Jvb3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBRUEsZUFBTyxNQUFNLHNCQUFzQixnRUFBOEQsQ0FBQztBQUVsRyxlQUFPLE1BQU0sMkJBQTJCLHlFQUM4QixDQUFDO0FBRXZFLGVBQU8sTUFBTSwyQkFBMkIseUVBQzhCLENBQUM7QUFFdkU7Ozs7O0dBS0c7QUFDSCx3QkFBZ0IsNkJBQTZCLENBQUMsTUFBTSxFQUFFLE1BQU0sRUFBRSxFQUFFLE1BQU0sOEZBQWdCLEdBQUcsTUFBTSxDQWU5RjtBQUVEOzs7OztHQUtHO0FBQ0gsd0JBQXNCLGtDQUFrQyxDQUN0RCxNQUFNLEVBQUUsTUFBTSxFQUFFLEVBQ2hCLE1BQU0sdUdBQXFCLEdBQzFCLE9BQU8sQ0FBQyxNQUFNLENBQUMsQ0FlakIifQ==
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"balanced_merkle_tree_root.d.ts","sourceRoot":"","sources":["../../src/trees/balanced_merkle_tree_root.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,sBAAsB,gEAA8D,CAAC;AAElG,eAAO,MAAM,2BAA2B,yEAC8B,CAAC;AAEvE,eAAO,MAAM,2BAA2B,yEAC8B,CAAC;AAEvE;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,MAAM,8FAAgB,GAAG,MAAM,CAe9F;AAED;;;;;GAKG;AACH,wBAAsB,kCAAkC,CACtD,MAAM,EAAE,MAAM,EAAE,EAChB,MAAM,uGAAqB,GAC1B,OAAO,CAAC,MAAM,CAAC,CAejB"}
|
|
@@ -1,18 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';
|
|
3
|
-
import { sha256Trunc } from '@aztec/foundation/crypto/sha256';
|
|
4
|
-
export const shaMerkleHash = (left, right)=>sha256Trunc(Buffer.concat([
|
|
5
|
-
left,
|
|
6
|
-
right
|
|
7
|
-
]));
|
|
8
|
-
export const pedersenMerkleHash = async (left, right)=>(await pedersenHashArray([
|
|
9
|
-
left,
|
|
10
|
-
right
|
|
11
|
-
])).toBuffer();
|
|
12
|
-
export const poseidonMerkleHash = async (left, right)=>(await poseidon2Hash([
|
|
13
|
-
left,
|
|
14
|
-
right
|
|
15
|
-
])).toBuffer();
|
|
1
|
+
import { pedersenMerkleHash, poseidonMerkleHash, shaMerkleHash } from './hasher.js';
|
|
16
2
|
export const computeBalancedShaRoot = (leaves)=>computeBalancedMerkleTreeRoot(leaves);
|
|
17
3
|
export const computeBalancedPedersenRoot = async (leaves)=>await computeBalancedMerkleTreeRootAsync(leaves, pedersenMerkleHash);
|
|
18
4
|
export const computeBalancedPoseidonRoot = async (leaves)=>await computeBalancedMerkleTreeRootAsync(leaves, poseidonMerkleHash);
|
package/dest/trees/hasher.d.ts
CHANGED
|
@@ -34,4 +34,7 @@ export interface AsyncHasher {
|
|
|
34
34
|
*/
|
|
35
35
|
hashInputs(inputs: Buffer[]): Promise<Buffer<ArrayBuffer>>;
|
|
36
36
|
}
|
|
37
|
-
|
|
37
|
+
export declare const shaMerkleHash: Hasher['hash'];
|
|
38
|
+
export declare const pedersenMerkleHash: AsyncHasher['hash'];
|
|
39
|
+
export declare const poseidonMerkleHash: AsyncHasher['hash'];
|
|
40
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaGFzaGVyLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdHJlZXMvaGFzaGVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUlBOztHQUVHO0FBQ0gsTUFBTSxXQUFXLE1BQU07SUFDckI7Ozs7O09BS0c7SUFDSCxJQUFJLENBQUMsR0FBRyxFQUFFLFVBQVUsRUFBRSxHQUFHLEVBQUUsVUFBVSxHQUFHLE1BQU0sQ0FBQyxXQUFXLENBQUMsQ0FBQztJQUU1RDs7OztPQUlHO0lBQ0gsVUFBVSxDQUFDLE1BQU0sRUFBRSxNQUFNLEVBQUUsR0FBRyxNQUFNLENBQUMsV0FBVyxDQUFDLENBQUM7Q0FDbkQ7QUFFRDs7R0FFRztBQUNILE1BQU0sV0FBVyxXQUFXO0lBQzFCOzs7OztPQUtHO0lBQ0gsSUFBSSxDQUFDLEdBQUcsRUFBRSxVQUFVLEVBQUUsR0FBRyxFQUFFLFVBQVUsR0FBRyxPQUFPLENBQUMsTUFBTSxDQUFDLFdBQVcsQ0FBQyxDQUFDLENBQUM7SUFFckU7Ozs7T0FJRztJQUNILFVBQVUsQ0FBQyxNQUFNLEVBQUUsTUFBTSxFQUFFLEdBQUcsT0FBTyxDQUFDLE1BQU0sQ0FBQyxXQUFXLENBQUMsQ0FBQyxDQUFDO0NBQzVEO0FBRUQsZUFBTyxNQUFNLGFBQWEsRUFBRSxNQUFNLENBQUMsTUFBTSxDQUN5QixDQUFDO0FBRW5FLGVBQU8sTUFBTSxrQkFBa0IsRUFBRSxXQUFXLENBQUMsTUFBTSxDQUN5QixDQUFDO0FBRTdFLGVBQU8sTUFBTSxrQkFBa0IsRUFBRSxXQUFXLENBQUMsTUFBTSxDQUNxQixDQUFDIn0=
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hasher.d.ts","sourceRoot":"","sources":["../../src/trees/hasher.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hasher.d.ts","sourceRoot":"","sources":["../../src/trees/hasher.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,MAAM,WAAW,MAAM;IACrB;;;;;OAKG;IACH,IAAI,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;IAE5D;;;;OAIG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;CACnD;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B;;;;;OAKG;IACH,IAAI,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC;IAErE;;;;OAIG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC;CAC5D;AAED,eAAO,MAAM,aAAa,EAAE,MAAM,CAAC,MAAM,CACyB,CAAC;AAEnE,eAAO,MAAM,kBAAkB,EAAE,WAAW,CAAC,MAAM,CACyB,CAAC;AAE7E,eAAO,MAAM,kBAAkB,EAAE,WAAW,CAAC,MAAM,CACqB,CAAC"}
|
package/dest/trees/hasher.js
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { pedersenHash as pedersenHashArray } from '../crypto/pedersen/index.js';
|
|
2
|
+
import { poseidon2Hash } from '../crypto/poseidon/index.js';
|
|
3
|
+
import { sha256Trunc } from '../crypto/sha256/index.js';
|
|
4
|
+
export const shaMerkleHash = (left, right)=>sha256Trunc(Buffer.concat([
|
|
5
|
+
left,
|
|
6
|
+
right
|
|
7
|
+
]));
|
|
8
|
+
export const pedersenMerkleHash = async (left, right)=>(await pedersenHashArray([
|
|
9
|
+
left,
|
|
10
|
+
right
|
|
11
|
+
])).toBuffer();
|
|
12
|
+
export const poseidonMerkleHash = async (left, right)=>(await poseidon2Hash([
|
|
13
|
+
left,
|
|
14
|
+
right
|
|
15
|
+
])).toBuffer();
|
package/dest/trees/index.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './unbalanced_merkle_tree.js';
|
|
3
|
-
export * from './unbalanced_tree_store.js';
|
|
1
|
+
export * from './balanced_merkle_tree_root.js';
|
|
4
2
|
export * from './merkle_tree_calculator.js';
|
|
5
3
|
export * from './merkle_tree.js';
|
|
6
4
|
export * from './indexed_merkle_tree_calculator.js';
|
|
@@ -10,4 +8,6 @@ export * from './membership_witness.js';
|
|
|
10
8
|
export * from './hasher.js';
|
|
11
9
|
export * from './indexed_tree_leaf.js';
|
|
12
10
|
export * from './unbalanced_merkle_tree_calculator.js';
|
|
13
|
-
|
|
11
|
+
export * from './unbalanced_merkle_tree_root.js';
|
|
12
|
+
export * from './unbalanced_tree_store.js';
|
|
13
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy90cmVlcy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFjLGdDQUFnQyxDQUFDO0FBQy9DLGNBQWMsNkJBQTZCLENBQUM7QUFDNUMsY0FBYyxrQkFBa0IsQ0FBQztBQUNqQyxjQUFjLHFDQUFxQyxDQUFDO0FBQ3BELGNBQWMsMEJBQTBCLENBQUM7QUFDekMsY0FBYyxtQkFBbUIsQ0FBQztBQUNsQyxjQUFjLHlCQUF5QixDQUFDO0FBQ3hDLGNBQWMsYUFBYSxDQUFDO0FBQzVCLGNBQWMsd0JBQXdCLENBQUM7QUFDdkMsY0FBYyx3Q0FBd0MsQ0FBQztBQUN2RCxjQUFjLGtDQUFrQyxDQUFDO0FBQ2pELGNBQWMsNEJBQTRCLENBQUMifQ==
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/trees/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/trees/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAC;AAC/C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,kBAAkB,CAAC;AACjC,cAAc,qCAAqC,CAAC;AACpD,cAAc,0BAA0B,CAAC;AACzC,cAAc,mBAAmB,CAAC;AAClC,cAAc,yBAAyB,CAAC;AACxC,cAAc,aAAa,CAAC;AAC5B,cAAc,wBAAwB,CAAC;AACvC,cAAc,wCAAwC,CAAC;AACvD,cAAc,kCAAkC,CAAC;AACjD,cAAc,4BAA4B,CAAC"}
|
package/dest/trees/index.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './unbalanced_merkle_tree.js';
|
|
3
|
-
export * from './unbalanced_tree_store.js';
|
|
1
|
+
export * from './balanced_merkle_tree_root.js';
|
|
4
2
|
export * from './merkle_tree_calculator.js';
|
|
5
3
|
export * from './merkle_tree.js';
|
|
6
4
|
export * from './indexed_merkle_tree_calculator.js';
|
|
@@ -10,3 +8,5 @@ export * from './membership_witness.js';
|
|
|
10
8
|
export * from './hasher.js';
|
|
11
9
|
export * from './indexed_tree_leaf.js';
|
|
12
10
|
export * from './unbalanced_merkle_tree_calculator.js';
|
|
11
|
+
export * from './unbalanced_merkle_tree_root.js';
|
|
12
|
+
export * from './unbalanced_tree_store.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type Hasher } from './hasher.js';
|
|
2
2
|
import { SiblingPath } from './sibling_path.js';
|
|
3
3
|
import { type TreeNodeLocation } from './unbalanced_tree_store.js';
|
|
4
4
|
/**
|
|
@@ -45,4 +45,4 @@ export declare class UnbalancedMerkleTreeCalculator {
|
|
|
45
45
|
private shiftNodeUp;
|
|
46
46
|
private collectNodesToShift;
|
|
47
47
|
}
|
|
48
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
48
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidW5iYWxhbmNlZF9tZXJrbGVfdHJlZV9jYWxjdWxhdG9yLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdHJlZXMvdW5iYWxhbmNlZF9tZXJrbGVfdHJlZV9jYWxjdWxhdG9yLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxLQUFLLE1BQU0sRUFBaUIsTUFBTSxhQUFhLENBQUM7QUFDekQsT0FBTyxFQUFFLFdBQVcsRUFBRSxNQUFNLG1CQUFtQixDQUFDO0FBQ2hELE9BQU8sRUFBRSxLQUFLLGdCQUFnQixFQUF1QixNQUFNLDRCQUE0QixDQUFDO0FBT3hGOzs7Ozs7O0dBT0c7QUFDSCxxQkFBYSw4QkFBOEI7SUFLdkMsT0FBTyxDQUFDLFFBQVEsQ0FBQyxNQUFNO0lBQ3ZCLE9BQU8sQ0FBQyxRQUFRLENBQUMsZUFBZTtJQUNoQyxPQUFPLENBQUMsUUFBUSxDQUFDLFNBQVM7SUFDMUIsT0FBTyxDQUFDLFFBQVEsQ0FBQyxNQUFNO0lBUHpCLE9BQU8sQ0FBQyxLQUFLLENBQWdDO0lBQzdDLE9BQU8sQ0FBQyxhQUFhLENBQTBCO0lBRS9DLFlBQ21CLE1BQU0sRUFBRSxNQUFNLEVBQUUsRUFDaEIsZUFBZSxFQUFFLE1BQU0sRUFDdkIsU0FBUyxFQUFFLE1BQU0sRUFDakIsTUFBTSxFQUFFLE1BQU0sQ0FBQyxNQUFNLENBQUMsRUFJeEM7SUFFRCxNQUFNLENBQUMsTUFBTSxDQUNYLE1BQU0sRUFBRSxNQUFNLEVBQUUsRUFDaEIsZUFBZSxzQkFBa0IsRUFDakMsU0FBUyxzQkFBbUIsRUFDNUIsTUFBTSw4RkFBZ0Isa0NBR3ZCO0lBRUQ7OztPQUdHO0lBQ0ksT0FBTyxJQUFJLE1BQU0sQ0FFdkI7SUFFRDs7Ozs7T0FLRztJQUNJLGNBQWMsQ0FBQyxDQUFDLFNBQVMsTUFBTSxFQUFFLEtBQUssRUFBRSxNQUFNLEdBQUcsV0FBVyxDQUFDLENBQUMsQ0FBQyxDQU9yRTtJQUVEOzs7O09BSUc7SUFDSSx5QkFBeUIsQ0FBQyxDQUFDLFNBQVMsTUFBTSxFQUFFLFNBQVMsRUFBRSxNQUFNLEdBQUcsV0FBVyxDQUFDLENBQUMsQ0FBQyxDQW1CcEY7SUFFTSxlQUFlLENBQUMsU0FBUyxFQUFFLE1BQU0sb0JBRXZDO0lBRUQ7OztPQUdHO0lBQ0gsT0FBTyxDQUFDLFNBQVM7SUErQ2pCLE9BQU8sQ0FBQyxXQUFXO0lBWW5CLE9BQU8sQ0FBQyxtQkFBbUI7Q0FtQjVCIn0=
|
|
@@ -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":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAiB,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,KAAK,gBAAgB,EAAuB,MAAM,4BAA4B,CAAC;AAOxF;;;;;;;GAOG;AACH,qBAAa,8BAA8B;IAKvC,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,eAAe;IAChC,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,MAAM;IAPzB,OAAO,CAAC,KAAK,CAAgC;IAC7C,OAAO,CAAC,aAAa,CAA0B;IAE/C,YACmB,MAAM,EAAE,MAAM,EAAE,EAChB,eAAe,EAAE,MAAM,EACvB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,EAIxC;IAED,MAAM,CAAC,MAAM,CACX,MAAM,EAAE,MAAM,EAAE,EAChB,eAAe,sBAAkB,EACjC,SAAS,sBAAmB,EAC5B,MAAM,8FAAgB,kCAGvB;IAED;;;OAGG;IACI,OAAO,IAAI,MAAM,CAEvB;IAED;;;;;OAKG;IACI,cAAc,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC,CAOrE;IAED;;;;OAIG;IACI,yBAAyB,CAAC,CAAC,SAAS,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC,CAmBpF;IAEM,eAAe,CAAC,SAAS,EAAE,MAAM,oBAEvC;IAED;;;OAGG;IACH,OAAO,CAAC,SAAS;IA+CjB,OAAO,CAAC,WAAW;IAYnB,OAAO,CAAC,mBAAmB;CAmB5B"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare const computeUnbalancedShaRoot: (leaves: Buffer<ArrayBufferLike>[]) => Buffer<ArrayBufferLike>;
|
|
2
2
|
export declare const computeUnbalancedPoseidonRoot: (leaves: Buffer<ArrayBufferLike>[]) => Promise<Buffer<ArrayBufferLike>>;
|
|
3
|
-
export declare const
|
|
3
|
+
export declare const computeWonkyShaRoot: (leaves: Buffer<ArrayBufferLike>[]) => Buffer<ArrayBufferLike>;
|
|
4
4
|
/**
|
|
5
5
|
* Computes the Merkle root of an unbalanced tree.
|
|
6
6
|
*
|
|
@@ -24,9 +24,5 @@ export declare const computeCompressedUnbalancedShaRoot: (leaves: Buffer<ArrayBu
|
|
|
24
24
|
*/
|
|
25
25
|
export declare function computeUnbalancedMerkleTreeRoot(leaves: Buffer[], hasher?: (lhs: Uint8Array<ArrayBufferLike>, rhs: Uint8Array<ArrayBufferLike>) => Buffer<ArrayBuffer>, emptyRoot?: Buffer<ArrayBuffer>): Buffer;
|
|
26
26
|
export declare function computeUnbalancedMerkleTreeRootAsync(leaves: Buffer[], hasher?: (lhs: Uint8Array<ArrayBufferLike>, rhs: Uint8Array<ArrayBufferLike>) => Promise<Buffer<ArrayBuffer>>, emptyRoot?: Buffer<ArrayBuffer>): Promise<Buffer>;
|
|
27
|
-
export declare function
|
|
28
|
-
|
|
29
|
-
level: number;
|
|
30
|
-
indexAtLevel: number;
|
|
31
|
-
};
|
|
32
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidW5iYWxhbmNlZF9tZXJrbGVfdHJlZS5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3RyZWVzL3VuYmFsYW5jZWRfbWVya2xlX3RyZWUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBUUEsZUFBTyxNQUFNLHdCQUF3QixnRUFBK0UsQ0FBQztBQUVySCxlQUFPLE1BQU0sNkJBQTZCLHlFQUM4QixDQUFDO0FBRXpFLGVBQU8sTUFBTSxrQ0FBa0MsZ0VBQ0ksQ0FBQztBQUVwRDs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7R0FvQkc7QUFDSCx3QkFBZ0IsK0JBQStCLENBQzdDLE1BQU0sRUFBRSxNQUFNLEVBQUUsRUFDaEIsTUFBTSw4RkFBZ0IsRUFDdEIsU0FBUyxzQkFBbUIsR0FDM0IsTUFBTSxDQTZCUjtBQUVELHdCQUFzQixvQ0FBb0MsQ0FDeEQsTUFBTSxFQUFFLE1BQU0sRUFBRSxFQUNoQixNQUFNLHVHQUFxQixFQUMzQixTQUFTLHNCQUFtQixHQUMzQixPQUFPLENBQUMsTUFBTSxDQUFDLENBNkJqQjtBQUVELHdCQUFnQix5Q0FBeUMsQ0FDdkQsTUFBTSxFQUFFLE1BQU0sRUFBRSxFQUNoQixlQUFlLHNCQUFtQixFQUNsQyxTQUFTLHNCQUFtQixFQUM1QixNQUFNLDhGQUFnQixHQUNyQixNQUFNLENBR1I7QUFzREQsd0JBQWdCLHFCQUFxQixDQUFDLFNBQVMsRUFBRSxNQUFNLEVBQUUsU0FBUyxFQUFFLE1BQU07OztFQUd6RSJ9
|
|
27
|
+
export declare function computeWonkyMerkleTreeRoot(leaves: Buffer[], valueToCompress?: Buffer<ArrayBuffer>, emptyRoot?: Buffer<ArrayBuffer>, hasher?: (lhs: Uint8Array<ArrayBufferLike>, rhs: Uint8Array<ArrayBufferLike>) => Buffer<ArrayBuffer>): Buffer;
|
|
28
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidW5iYWxhbmNlZF9tZXJrbGVfdHJlZV9yb290LmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdHJlZXMvdW5iYWxhbmNlZF9tZXJrbGVfdHJlZV9yb290LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUlBLGVBQU8sTUFBTSx3QkFBd0IsZ0VBQStFLENBQUM7QUFFckgsZUFBTyxNQUFNLDZCQUE2Qix5RUFDOEIsQ0FBQztBQUV6RSxlQUFPLE1BQU0sbUJBQW1CLGdFQUEyRCxDQUFDO0FBRTVGOzs7Ozs7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AGR"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { computeBalancedMerkleTreeRoot, computeBalancedMerkleTreeRootAsync
|
|
1
|
+
import { computeBalancedMerkleTreeRoot, computeBalancedMerkleTreeRootAsync } from './balanced_merkle_tree_root.js';
|
|
2
|
+
import { poseidonMerkleHash, shaMerkleHash } from './hasher.js';
|
|
2
3
|
import { UnbalancedMerkleTreeCalculator } from './unbalanced_merkle_tree_calculator.js';
|
|
3
4
|
export const computeUnbalancedShaRoot = (leaves)=>computeUnbalancedMerkleTreeRoot(leaves, shaMerkleHash);
|
|
4
5
|
export const computeUnbalancedPoseidonRoot = async (leaves)=>await computeUnbalancedMerkleTreeRootAsync(leaves, poseidonMerkleHash);
|
|
5
|
-
export const
|
|
6
|
+
export const computeWonkyShaRoot = (leaves)=>computeWonkyMerkleTreeRoot(leaves);
|
|
6
7
|
/**
|
|
7
8
|
* Computes the Merkle root of an unbalanced tree.
|
|
8
9
|
*
|
|
@@ -73,56 +74,11 @@ export async function computeUnbalancedMerkleTreeRootAsync(leaves, hasher = pose
|
|
|
73
74
|
}
|
|
74
75
|
return root;
|
|
75
76
|
}
|
|
76
|
-
|
|
77
|
+
// A **wonky** tree is a "compressed" unbalanced Merkle tree.
|
|
78
|
+
// It is constructed in the same way as an unbalanced tree: by first creating the largest possible left subtree, with
|
|
79
|
+
// the remaining leaves forming a right subtree that follows the same process recursively.
|
|
80
|
+
// During construction, leaves equal to `valueToCompress` are skipped (compressed) and do not contribute to the tree.
|
|
81
|
+
export function computeWonkyMerkleTreeRoot(leaves, valueToCompress = Buffer.alloc(32), emptyRoot = Buffer.alloc(32), hasher = shaMerkleHash) {
|
|
77
82
|
const calculator = UnbalancedMerkleTreeCalculator.create(leaves, valueToCompress, emptyRoot, hasher);
|
|
78
83
|
return calculator.getRoot();
|
|
79
84
|
}
|
|
80
|
-
/// Get the depth of the maximum balanced tree that can be created with the given number of leaves. The subtree will be
|
|
81
|
-
/// the left most subtree of the wonky tree with a total of `numLeaves` leaves.
|
|
82
|
-
///
|
|
83
|
-
/// Note: All the leaves may not be used to form the tree. For example, if there are 5 leaves, the maximum depth is 2,
|
|
84
|
-
/// only 4 leaves are used to form a balanced tree.
|
|
85
|
-
function getMaxBalancedSubtreeDepth(numLeaves) {
|
|
86
|
-
return Math.floor(Math.log2(numLeaves));
|
|
87
|
-
}
|
|
88
|
-
/// Get the maximum depth of an unbalanced tree that can be created with the given number of leaves.
|
|
89
|
-
function getMaxUnbalancedTreeDepth(numLeaves) {
|
|
90
|
-
return Math.ceil(Math.log2(numLeaves));
|
|
91
|
-
}
|
|
92
|
-
function findPosition(rootLevel, leafLevel, numLeaves, indexOffset, targetIndex) {
|
|
93
|
-
if (numLeaves <= 1) {
|
|
94
|
-
// Single leaf.
|
|
95
|
-
return {
|
|
96
|
-
level: rootLevel,
|
|
97
|
-
indexAtLevel: indexOffset
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
// The largest balanced tree that can be created with the given number of leaves.
|
|
101
|
-
const maxBalancedTreeDepth = getMaxBalancedSubtreeDepth(numLeaves);
|
|
102
|
-
const numBalancedLeaves = 2 ** maxBalancedTreeDepth;
|
|
103
|
-
const numRemainingLeaves = numLeaves - numBalancedLeaves;
|
|
104
|
-
if (targetIndex < numBalancedLeaves) {
|
|
105
|
-
// Target is in the balanced tree.
|
|
106
|
-
// - If numRemainingLeaves is 0: this balanced tree is grown from the current root.
|
|
107
|
-
// - If numRemainingLeaves is not 0: the remaining leaves will form another tree, which will become the right child of the root.
|
|
108
|
-
// And the balanced tree will be the left child of the root.
|
|
109
|
-
// There will be an extra level between the root of the balanced tree and the current root.
|
|
110
|
-
const extraLevel = numRemainingLeaves ? 1 : 0;
|
|
111
|
-
return {
|
|
112
|
-
level: rootLevel + maxBalancedTreeDepth + extraLevel,
|
|
113
|
-
indexAtLevel: indexOffset + targetIndex
|
|
114
|
-
};
|
|
115
|
-
} else {
|
|
116
|
-
// Target is in the right branch.
|
|
117
|
-
const rightBranchMaxLevel = getMaxUnbalancedTreeDepth(numRemainingLeaves);
|
|
118
|
-
const shiftedUp = leafLevel - rootLevel - rightBranchMaxLevel - 1;
|
|
119
|
-
const nextLeafLevel = leafLevel - shiftedUp;
|
|
120
|
-
const newIndexOffset = indexOffset + numBalancedLeaves >> shiftedUp;
|
|
121
|
-
const shiftedTargetIndex = targetIndex - numBalancedLeaves;
|
|
122
|
-
return findPosition(rootLevel + 1, nextLeafLevel, numRemainingLeaves, newIndexOffset, shiftedTargetIndex);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
export function findLeafLevelAndIndex(numLeaves, leafIndex) {
|
|
126
|
-
const maxLevel = getMaxUnbalancedTreeDepth(numLeaves);
|
|
127
|
-
return findPosition(0, maxLevel, numLeaves, 0, leafIndex);
|
|
128
|
-
}
|
|
@@ -17,4 +17,8 @@ export declare class UnbalancedTreeStore<T> {
|
|
|
17
17
|
getSibling(location: TreeNodeLocation): T | undefined;
|
|
18
18
|
getChildren(location: TreeNodeLocation): [T | undefined, T | undefined];
|
|
19
19
|
}
|
|
20
|
-
|
|
20
|
+
export declare function findLeafLevelAndIndex(numLeaves: number, leafIndex: number): {
|
|
21
|
+
level: number;
|
|
22
|
+
indexAtLevel: number;
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidW5iYWxhbmNlZF90cmVlX3N0b3JlLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdHJlZXMvdW5iYWxhbmNlZF90cmVlX3N0b3JlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE1BQU0sV0FBVyxnQkFBZ0I7SUFDL0IsS0FBSyxFQUFFLE1BQU0sQ0FBQztJQUNkLEtBQUssRUFBRSxNQUFNLENBQUM7Q0FDZjtBQU9ELHFCQUFhLG1CQUFtQixDQUFDLENBQUM7O0lBSWhDLFlBQVksU0FBUy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UMsU0FBUyxFQUFFLE1BQU0sRUFBRSxTQUFTLEVBQUUsTUFBTTs7O0VBR3pFIn0=
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unbalanced_tree_store.d.ts","sourceRoot":"","sources":["../../src/trees/unbalanced_tree_store.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"unbalanced_tree_store.d.ts","sourceRoot":"","sources":["../../src/trees/unbalanced_tree_store.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;CACf;AAOD,qBAAa,mBAAmB,CAAC,CAAC;;IAIhC,YAAY,SAAS,EAAE,MAAM,EAE5B;IAED,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,gBAAgB,CAerD;IAED,OAAO,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,gBAAgB,EAAE,KAAK,EAAE,CAAC,QASnD;IAED,iBAAiB,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,gBAAgB,GAAG,gBAAgB,CAMtE;IAED,kBAAkB,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,gBAAgB,GAAG,gBAAgB,CAMvE;IAED,iBAAiB,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,gBAAgB,GAAG,CAAC,gBAAgB,EAAE,gBAAgB,CAAC,CAI1F;IAED,OAAO,CAAC,SAAS,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS,CAOxC;IAED,OAAO,CAAC,QAAQ,EAAE,gBAAgB,GAAG,CAAC,GAAG,SAAS,CAEjD;IAED,OAAO,IAAI,CAAC,GAAG,SAAS,CAEvB;IAED,SAAS,CAAC,QAAQ,EAAE,gBAAgB,GAAG,CAAC,GAAG,SAAS,CAGnD;IAED,UAAU,CAAC,QAAQ,EAAE,gBAAgB,GAAG,CAAC,GAAG,SAAS,CAGpD;IAED,WAAW,CAAC,QAAQ,EAAE,gBAAgB,GAAG,CAAC,CAAC,GAAG,SAAS,EAAE,CAAC,GAAG,SAAS,CAAC,CAGtE;CAKF;AAsDD,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;;;EAGzE"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { findLeafLevelAndIndex } from './unbalanced_merkle_tree.js';
|
|
2
1
|
export class UnbalancedTreeStore {
|
|
3
2
|
#nodeMapping = new Map();
|
|
4
3
|
#numLeaves;
|
|
@@ -98,3 +97,52 @@ export class UnbalancedTreeStore {
|
|
|
98
97
|
return `${location.level}-${location.index}`;
|
|
99
98
|
}
|
|
100
99
|
}
|
|
100
|
+
/// Get the depth of the maximum balanced tree that can be created with the given number of leaves. The subtree will be
|
|
101
|
+
/// the left most subtree of the unbalanced tree with a total of `numLeaves` leaves.
|
|
102
|
+
///
|
|
103
|
+
/// Note: All the leaves may not be used to form the tree. For example, if there are 5 leaves, the maximum depth is 2,
|
|
104
|
+
/// only 4 leaves are used to form a balanced tree.
|
|
105
|
+
function getMaxBalancedSubtreeDepth(numLeaves) {
|
|
106
|
+
return Math.floor(Math.log2(numLeaves));
|
|
107
|
+
}
|
|
108
|
+
/// Get the maximum depth of an unbalanced tree that can be created with the given number of leaves.
|
|
109
|
+
function getMaxUnbalancedTreeDepth(numLeaves) {
|
|
110
|
+
return Math.ceil(Math.log2(numLeaves));
|
|
111
|
+
}
|
|
112
|
+
function findPosition(rootLevel, leafLevel, numLeaves, indexOffset, targetIndex) {
|
|
113
|
+
if (numLeaves <= 1) {
|
|
114
|
+
// Single leaf.
|
|
115
|
+
return {
|
|
116
|
+
level: rootLevel,
|
|
117
|
+
indexAtLevel: indexOffset
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
// The largest balanced tree that can be created with the given number of leaves.
|
|
121
|
+
const maxBalancedTreeDepth = getMaxBalancedSubtreeDepth(numLeaves);
|
|
122
|
+
const numBalancedLeaves = 2 ** maxBalancedTreeDepth;
|
|
123
|
+
const numRemainingLeaves = numLeaves - numBalancedLeaves;
|
|
124
|
+
if (targetIndex < numBalancedLeaves) {
|
|
125
|
+
// Target is in the balanced tree.
|
|
126
|
+
// - If numRemainingLeaves is 0: this balanced tree is grown from the current root.
|
|
127
|
+
// - If numRemainingLeaves is not 0: the remaining leaves will form another tree, which will become the right child of the root.
|
|
128
|
+
// And the balanced tree will be the left child of the root.
|
|
129
|
+
// There will be an extra level between the root of the balanced tree and the current root.
|
|
130
|
+
const extraLevel = numRemainingLeaves ? 1 : 0;
|
|
131
|
+
return {
|
|
132
|
+
level: rootLevel + maxBalancedTreeDepth + extraLevel,
|
|
133
|
+
indexAtLevel: indexOffset + targetIndex
|
|
134
|
+
};
|
|
135
|
+
} else {
|
|
136
|
+
// Target is in the right branch.
|
|
137
|
+
const rightBranchMaxLevel = getMaxUnbalancedTreeDepth(numRemainingLeaves);
|
|
138
|
+
const shiftedUp = leafLevel - rootLevel - rightBranchMaxLevel - 1;
|
|
139
|
+
const nextLeafLevel = leafLevel - shiftedUp;
|
|
140
|
+
const newIndexOffset = indexOffset + numBalancedLeaves >> shiftedUp;
|
|
141
|
+
const shiftedTargetIndex = targetIndex - numBalancedLeaves;
|
|
142
|
+
return findPosition(rootLevel + 1, nextLeafLevel, numRemainingLeaves, newIndexOffset, shiftedTargetIndex);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
export function findLeafLevelAndIndex(numLeaves, leafIndex) {
|
|
146
|
+
const maxLevel = getMaxUnbalancedTreeDepth(numLeaves);
|
|
147
|
+
return findPosition(0, maxLevel, numLeaves, 0, leafIndex);
|
|
148
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/foundation",
|
|
3
|
-
"version": "4.0.0-nightly.
|
|
3
|
+
"version": "4.0.0-nightly.20260114",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dest/index.js",
|
|
6
6
|
"types": "./dest/index.d.ts",
|
|
@@ -123,7 +123,7 @@
|
|
|
123
123
|
"testEnvironment": "../../foundation/src/jest/env.mjs"
|
|
124
124
|
},
|
|
125
125
|
"dependencies": {
|
|
126
|
-
"@aztec/bb.js": "4.0.0-nightly.
|
|
126
|
+
"@aztec/bb.js": "4.0.0-nightly.20260114",
|
|
127
127
|
"@koa/cors": "^5.0.0",
|
|
128
128
|
"@noble/curves": "=1.7.0",
|
|
129
129
|
"@noble/hashes": "^1.6.1",
|
|
@@ -224,15 +224,22 @@ export class BufferReader {
|
|
|
224
224
|
* deserializing each one using the 'fromBuffer' method of 'itemDeserializer'.
|
|
225
225
|
*
|
|
226
226
|
* @param itemDeserializer - Object with 'fromBuffer' method to deserialize vector elements.
|
|
227
|
+
* @param maxSize - Optional maximum allowed size for the vector. If the size exceeds this, an error is thrown.
|
|
227
228
|
* @returns An array of deserialized elements of type T.
|
|
228
229
|
*/
|
|
229
|
-
public readVector<T>(
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
230
|
+
public readVector<T>(
|
|
231
|
+
itemDeserializer: {
|
|
232
|
+
/**
|
|
233
|
+
* A method to deserialize data from a buffer.
|
|
234
|
+
*/
|
|
235
|
+
fromBuffer: (reader: BufferReader) => T;
|
|
236
|
+
},
|
|
237
|
+
maxSize?: number,
|
|
238
|
+
): T[] {
|
|
235
239
|
const size = this.readNumber();
|
|
240
|
+
if (maxSize !== undefined && size > maxSize) {
|
|
241
|
+
throw new Error(`Vector size ${size} exceeds maximum allowed ${maxSize}`);
|
|
242
|
+
}
|
|
236
243
|
const result = new Array<T>(size);
|
|
237
244
|
for (let i = 0; i < size; i++) {
|
|
238
245
|
result[i] = itemDeserializer.fromBuffer(this);
|
|
@@ -344,10 +351,11 @@ export class BufferReader {
|
|
|
344
351
|
* The method first reads the size of the string, then reads the corresponding
|
|
345
352
|
* number of bytes from the buffer and converts them to a string.
|
|
346
353
|
*
|
|
354
|
+
* @param maxSize - Optional maximum allowed size for the string buffer. If the size exceeds this, an error is thrown.
|
|
347
355
|
* @returns The read string from the buffer.
|
|
348
356
|
*/
|
|
349
|
-
public readString(): string {
|
|
350
|
-
return this.readBuffer().toString();
|
|
357
|
+
public readString(maxSize?: number): string {
|
|
358
|
+
return this.readBuffer(maxSize).toString();
|
|
351
359
|
}
|
|
352
360
|
|
|
353
361
|
/**
|
|
@@ -356,10 +364,14 @@ export class BufferReader {
|
|
|
356
364
|
* a Buffer with that size containing the bytes. Useful for reading variable-length
|
|
357
365
|
* binary data encoded as (size, data) format.
|
|
358
366
|
*
|
|
367
|
+
* @param maxSize - Optional maximum allowed size for the buffer. If the size exceeds this, an error is thrown.
|
|
359
368
|
* @returns A Buffer containing the read bytes.
|
|
360
369
|
*/
|
|
361
|
-
public readBuffer(): Buffer {
|
|
370
|
+
public readBuffer(maxSize?: number): Buffer {
|
|
362
371
|
const size = this.readNumber();
|
|
372
|
+
if (maxSize !== undefined && size > maxSize) {
|
|
373
|
+
throw new Error(`Buffer size ${size} exceeds maximum allowed ${maxSize}`);
|
|
374
|
+
}
|
|
363
375
|
this.#rangeCheck(size);
|
|
364
376
|
return this.readBytes(size);
|
|
365
377
|
}
|
|
@@ -1,17 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';
|
|
3
|
-
import { sha256Trunc } from '@aztec/foundation/crypto/sha256';
|
|
4
|
-
|
|
5
|
-
import type { AsyncHasher, Hasher } from './hasher.js';
|
|
6
|
-
|
|
7
|
-
export const shaMerkleHash: Hasher['hash'] = (left: Buffer, right: Buffer) =>
|
|
8
|
-
sha256Trunc(Buffer.concat([left, right])) as Buffer<ArrayBuffer>;
|
|
9
|
-
|
|
10
|
-
export const pedersenMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
|
|
11
|
-
(await pedersenHashArray([left, right])).toBuffer() as Buffer<ArrayBuffer>;
|
|
12
|
-
|
|
13
|
-
export const poseidonMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
|
|
14
|
-
(await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>;
|
|
1
|
+
import { pedersenMerkleHash, poseidonMerkleHash, shaMerkleHash } from './hasher.js';
|
|
15
2
|
|
|
16
3
|
export const computeBalancedShaRoot = (leaves: Buffer[]) => computeBalancedMerkleTreeRoot(leaves);
|
|
17
4
|
|
package/src/trees/hasher.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { pedersenHash as pedersenHashArray } from '../crypto/pedersen/index.js';
|
|
2
|
+
import { poseidon2Hash } from '../crypto/poseidon/index.js';
|
|
3
|
+
import { sha256Trunc } from '../crypto/sha256/index.js';
|
|
4
|
+
|
|
1
5
|
/**
|
|
2
6
|
* Defines hasher interface used by Merkle trees.
|
|
3
7
|
*/
|
|
@@ -37,3 +41,12 @@ export interface AsyncHasher {
|
|
|
37
41
|
*/
|
|
38
42
|
hashInputs(inputs: Buffer[]): Promise<Buffer<ArrayBuffer>>;
|
|
39
43
|
}
|
|
44
|
+
|
|
45
|
+
export const shaMerkleHash: Hasher['hash'] = (left: Buffer, right: Buffer) =>
|
|
46
|
+
sha256Trunc(Buffer.concat([left, right])) as Buffer<ArrayBuffer>;
|
|
47
|
+
|
|
48
|
+
export const pedersenMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
|
|
49
|
+
(await pedersenHashArray([left, right])).toBuffer() as Buffer<ArrayBuffer>;
|
|
50
|
+
|
|
51
|
+
export const poseidonMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
|
|
52
|
+
(await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>;
|
package/src/trees/index.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './unbalanced_merkle_tree.js';
|
|
3
|
-
export * from './unbalanced_tree_store.js';
|
|
1
|
+
export * from './balanced_merkle_tree_root.js';
|
|
4
2
|
export * from './merkle_tree_calculator.js';
|
|
5
3
|
export * from './merkle_tree.js';
|
|
6
4
|
export * from './indexed_merkle_tree_calculator.js';
|
|
@@ -10,3 +8,5 @@ export * from './membership_witness.js';
|
|
|
10
8
|
export * from './hasher.js';
|
|
11
9
|
export * from './indexed_tree_leaf.js';
|
|
12
10
|
export * from './unbalanced_merkle_tree_calculator.js';
|
|
11
|
+
export * from './unbalanced_merkle_tree_root.js';
|
|
12
|
+
export * from './unbalanced_tree_store.js';
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { shaMerkleHash } from './
|
|
2
|
-
import type { Hasher } from './hasher.js';
|
|
1
|
+
import { type Hasher, shaMerkleHash } from './hasher.js';
|
|
3
2
|
import { SiblingPath } from './sibling_path.js';
|
|
4
3
|
import { type TreeNodeLocation, UnbalancedTreeStore } from './unbalanced_tree_store.js';
|
|
5
4
|
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
computeBalancedMerkleTreeRootAsync,
|
|
4
|
-
poseidonMerkleHash,
|
|
5
|
-
shaMerkleHash,
|
|
6
|
-
} from './balanced_merkle_tree.js';
|
|
1
|
+
import { computeBalancedMerkleTreeRoot, computeBalancedMerkleTreeRootAsync } from './balanced_merkle_tree_root.js';
|
|
2
|
+
import { poseidonMerkleHash, shaMerkleHash } from './hasher.js';
|
|
7
3
|
import { UnbalancedMerkleTreeCalculator } from './unbalanced_merkle_tree_calculator.js';
|
|
8
4
|
|
|
9
5
|
export const computeUnbalancedShaRoot = (leaves: Buffer[]) => computeUnbalancedMerkleTreeRoot(leaves, shaMerkleHash);
|
|
@@ -11,8 +7,7 @@ export const computeUnbalancedShaRoot = (leaves: Buffer[]) => computeUnbalancedM
|
|
|
11
7
|
export const computeUnbalancedPoseidonRoot = async (leaves: Buffer[]) =>
|
|
12
8
|
await computeUnbalancedMerkleTreeRootAsync(leaves, poseidonMerkleHash);
|
|
13
9
|
|
|
14
|
-
export const
|
|
15
|
-
computeCompressedUnbalancedMerkleTreeRoot(leaves);
|
|
10
|
+
export const computeWonkyShaRoot = (leaves: Buffer[]) => computeWonkyMerkleTreeRoot(leaves);
|
|
16
11
|
|
|
17
12
|
/**
|
|
18
13
|
* Computes the Merkle root of an unbalanced tree.
|
|
@@ -105,7 +100,11 @@ export async function computeUnbalancedMerkleTreeRootAsync(
|
|
|
105
100
|
return root!;
|
|
106
101
|
}
|
|
107
102
|
|
|
108
|
-
|
|
103
|
+
// A **wonky** tree is a "compressed" unbalanced Merkle tree.
|
|
104
|
+
// It is constructed in the same way as an unbalanced tree: by first creating the largest possible left subtree, with
|
|
105
|
+
// the remaining leaves forming a right subtree that follows the same process recursively.
|
|
106
|
+
// During construction, leaves equal to `valueToCompress` are skipped (compressed) and do not contribute to the tree.
|
|
107
|
+
export function computeWonkyMerkleTreeRoot(
|
|
109
108
|
leaves: Buffer[],
|
|
110
109
|
valueToCompress = Buffer.alloc(32),
|
|
111
110
|
emptyRoot = Buffer.alloc(32),
|
|
@@ -114,60 +113,3 @@ export function computeCompressedUnbalancedMerkleTreeRoot(
|
|
|
114
113
|
const calculator = UnbalancedMerkleTreeCalculator.create(leaves, valueToCompress, emptyRoot, hasher);
|
|
115
114
|
return calculator.getRoot();
|
|
116
115
|
}
|
|
117
|
-
|
|
118
|
-
/// Get the depth of the maximum balanced tree that can be created with the given number of leaves. The subtree will be
|
|
119
|
-
/// the left most subtree of the wonky tree with a total of `numLeaves` leaves.
|
|
120
|
-
///
|
|
121
|
-
/// Note: All the leaves may not be used to form the tree. For example, if there are 5 leaves, the maximum depth is 2,
|
|
122
|
-
/// only 4 leaves are used to form a balanced tree.
|
|
123
|
-
function getMaxBalancedSubtreeDepth(numLeaves: number) {
|
|
124
|
-
return Math.floor(Math.log2(numLeaves));
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/// Get the maximum depth of an unbalanced tree that can be created with the given number of leaves.
|
|
128
|
-
function getMaxUnbalancedTreeDepth(numLeaves: number) {
|
|
129
|
-
return Math.ceil(Math.log2(numLeaves));
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function findPosition(
|
|
133
|
-
rootLevel: number,
|
|
134
|
-
leafLevel: number,
|
|
135
|
-
numLeaves: number,
|
|
136
|
-
indexOffset: number,
|
|
137
|
-
targetIndex: number,
|
|
138
|
-
): { level: number; indexAtLevel: number } {
|
|
139
|
-
if (numLeaves <= 1) {
|
|
140
|
-
// Single leaf.
|
|
141
|
-
return { level: rootLevel, indexAtLevel: indexOffset };
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// The largest balanced tree that can be created with the given number of leaves.
|
|
145
|
-
const maxBalancedTreeDepth = getMaxBalancedSubtreeDepth(numLeaves);
|
|
146
|
-
const numBalancedLeaves = 2 ** maxBalancedTreeDepth;
|
|
147
|
-
const numRemainingLeaves = numLeaves - numBalancedLeaves;
|
|
148
|
-
|
|
149
|
-
if (targetIndex < numBalancedLeaves) {
|
|
150
|
-
// Target is in the balanced tree.
|
|
151
|
-
|
|
152
|
-
// - If numRemainingLeaves is 0: this balanced tree is grown from the current root.
|
|
153
|
-
// - If numRemainingLeaves is not 0: the remaining leaves will form another tree, which will become the right child of the root.
|
|
154
|
-
// And the balanced tree will be the left child of the root.
|
|
155
|
-
// There will be an extra level between the root of the balanced tree and the current root.
|
|
156
|
-
const extraLevel = numRemainingLeaves ? 1 : 0;
|
|
157
|
-
|
|
158
|
-
return { level: rootLevel + maxBalancedTreeDepth + extraLevel, indexAtLevel: indexOffset + targetIndex };
|
|
159
|
-
} else {
|
|
160
|
-
// Target is in the right branch.
|
|
161
|
-
const rightBranchMaxLevel = getMaxUnbalancedTreeDepth(numRemainingLeaves);
|
|
162
|
-
const shiftedUp = leafLevel - rootLevel - rightBranchMaxLevel - 1;
|
|
163
|
-
const nextLeafLevel = leafLevel - shiftedUp;
|
|
164
|
-
const newIndexOffset = (indexOffset + numBalancedLeaves) >> shiftedUp;
|
|
165
|
-
const shiftedTargetIndex = targetIndex - numBalancedLeaves;
|
|
166
|
-
return findPosition(rootLevel + 1, nextLeafLevel, numRemainingLeaves, newIndexOffset, shiftedTargetIndex);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export function findLeafLevelAndIndex(numLeaves: number, leafIndex: number) {
|
|
171
|
-
const maxLevel = getMaxUnbalancedTreeDepth(numLeaves);
|
|
172
|
-
return findPosition(0, maxLevel, numLeaves, 0, leafIndex);
|
|
173
|
-
}
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { findLeafLevelAndIndex } from './unbalanced_merkle_tree.js';
|
|
2
|
-
|
|
3
1
|
export interface TreeNodeLocation {
|
|
4
2
|
level: number;
|
|
5
3
|
index: number;
|
|
@@ -104,3 +102,60 @@ export class UnbalancedTreeStore<T> {
|
|
|
104
102
|
return `${location.level}-${location.index}`;
|
|
105
103
|
}
|
|
106
104
|
}
|
|
105
|
+
|
|
106
|
+
/// Get the depth of the maximum balanced tree that can be created with the given number of leaves. The subtree will be
|
|
107
|
+
/// the left most subtree of the unbalanced tree with a total of `numLeaves` leaves.
|
|
108
|
+
///
|
|
109
|
+
/// Note: All the leaves may not be used to form the tree. For example, if there are 5 leaves, the maximum depth is 2,
|
|
110
|
+
/// only 4 leaves are used to form a balanced tree.
|
|
111
|
+
function getMaxBalancedSubtreeDepth(numLeaves: number) {
|
|
112
|
+
return Math.floor(Math.log2(numLeaves));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// Get the maximum depth of an unbalanced tree that can be created with the given number of leaves.
|
|
116
|
+
function getMaxUnbalancedTreeDepth(numLeaves: number) {
|
|
117
|
+
return Math.ceil(Math.log2(numLeaves));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function findPosition(
|
|
121
|
+
rootLevel: number,
|
|
122
|
+
leafLevel: number,
|
|
123
|
+
numLeaves: number,
|
|
124
|
+
indexOffset: number,
|
|
125
|
+
targetIndex: number,
|
|
126
|
+
): { level: number; indexAtLevel: number } {
|
|
127
|
+
if (numLeaves <= 1) {
|
|
128
|
+
// Single leaf.
|
|
129
|
+
return { level: rootLevel, indexAtLevel: indexOffset };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// The largest balanced tree that can be created with the given number of leaves.
|
|
133
|
+
const maxBalancedTreeDepth = getMaxBalancedSubtreeDepth(numLeaves);
|
|
134
|
+
const numBalancedLeaves = 2 ** maxBalancedTreeDepth;
|
|
135
|
+
const numRemainingLeaves = numLeaves - numBalancedLeaves;
|
|
136
|
+
|
|
137
|
+
if (targetIndex < numBalancedLeaves) {
|
|
138
|
+
// Target is in the balanced tree.
|
|
139
|
+
|
|
140
|
+
// - If numRemainingLeaves is 0: this balanced tree is grown from the current root.
|
|
141
|
+
// - If numRemainingLeaves is not 0: the remaining leaves will form another tree, which will become the right child of the root.
|
|
142
|
+
// And the balanced tree will be the left child of the root.
|
|
143
|
+
// There will be an extra level between the root of the balanced tree and the current root.
|
|
144
|
+
const extraLevel = numRemainingLeaves ? 1 : 0;
|
|
145
|
+
|
|
146
|
+
return { level: rootLevel + maxBalancedTreeDepth + extraLevel, indexAtLevel: indexOffset + targetIndex };
|
|
147
|
+
} else {
|
|
148
|
+
// Target is in the right branch.
|
|
149
|
+
const rightBranchMaxLevel = getMaxUnbalancedTreeDepth(numRemainingLeaves);
|
|
150
|
+
const shiftedUp = leafLevel - rootLevel - rightBranchMaxLevel - 1;
|
|
151
|
+
const nextLeafLevel = leafLevel - shiftedUp;
|
|
152
|
+
const newIndexOffset = (indexOffset + numBalancedLeaves) >> shiftedUp;
|
|
153
|
+
const shiftedTargetIndex = targetIndex - numBalancedLeaves;
|
|
154
|
+
return findPosition(rootLevel + 1, nextLeafLevel, numRemainingLeaves, newIndexOffset, shiftedTargetIndex);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function findLeafLevelAndIndex(numLeaves: number, leafIndex: number) {
|
|
159
|
+
const maxLevel = getMaxUnbalancedTreeDepth(numLeaves);
|
|
160
|
+
return findPosition(0, maxLevel, numLeaves, 0, leafIndex);
|
|
161
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"balanced_merkle_tree.d.ts","sourceRoot":"","sources":["../../src/trees/balanced_merkle_tree.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAEvD,eAAO,MAAM,aAAa,EAAE,MAAM,CAAC,MAAM,CACyB,CAAC;AAEnE,eAAO,MAAM,kBAAkB,EAAE,WAAW,CAAC,MAAM,CACyB,CAAC;AAE7E,eAAO,MAAM,kBAAkB,EAAE,WAAW,CAAC,MAAM,CACqB,CAAC;AAEzE,eAAO,MAAM,sBAAsB,gEAA8D,CAAC;AAElG,eAAO,MAAM,2BAA2B,yEAC8B,CAAC;AAEvE,eAAO,MAAM,2BAA2B,yEAC8B,CAAC;AAEvE;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,MAAM,8FAAgB,GAAG,MAAM,CAe9F;AAED;;;;;GAKG;AACH,wBAAsB,kCAAkC,CACtD,MAAM,EAAE,MAAM,EAAE,EAChB,MAAM,uGAAqB,GAC1B,OAAO,CAAC,MAAM,CAAC,CAejB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"unbalanced_merkle_tree.d.ts","sourceRoot":"","sources":["../../src/trees/unbalanced_merkle_tree.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,wBAAwB,gEAA+E,CAAC;AAErH,eAAO,MAAM,6BAA6B,yEAC8B,CAAC;AAEzE,eAAO,MAAM,kCAAkC,gEACI,CAAC;AAEpD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,+BAA+B,CAC7C,MAAM,EAAE,MAAM,EAAE,EAChB,MAAM,8FAAgB,EACtB,S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