@aztec/foundation 4.0.0-nightly.20260113 → 4.0.0-nightly.20260115
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/buffer/buffer16.d.ts +4 -1
- package/dest/buffer/buffer16.d.ts.map +1 -1
- package/dest/buffer/buffer32.d.ts +4 -1
- package/dest/buffer/buffer32.d.ts.map +1 -1
- package/dest/config/index.d.ts +5 -1
- package/dest/config/index.d.ts.map +1 -1
- package/dest/config/index.js +14 -0
- package/dest/crypto/ecdsa/signature.d.ts +10 -1
- package/dest/crypto/ecdsa/signature.d.ts.map +1 -1
- package/dest/curves/bls12/point.d.ts +10 -1
- package/dest/curves/bls12/point.d.ts.map +1 -1
- package/dest/curves/grumpkin/point.d.ts +11 -2
- package/dest/curves/grumpkin/point.d.ts.map +1 -1
- package/dest/error/index.d.ts +4 -4
- package/dest/error/index.d.ts.map +1 -1
- package/dest/eth-signature/eth_signature.d.ts +4 -1
- package/dest/eth-signature/eth_signature.d.ts.map +1 -1
- package/dest/json-rpc/fixtures/class_a.d.ts +3 -3
- package/dest/json-rpc/fixtures/class_a.d.ts.map +1 -1
- package/dest/json-rpc/fixtures/class_b.d.ts +3 -3
- package/dest/json-rpc/fixtures/class_b.d.ts.map +1 -1
- 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/membership_witness.d.ts +7 -1
- package/dest/trees/membership_witness.d.ts.map +1 -1
- package/dest/trees/sibling_path.d.ts +2 -1
- package/dest/trees/sibling_path.d.ts.map +1 -1
- 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 +3 -3
- package/src/config/index.ts +17 -0
- 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
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';
|
|
@@ -5,7 +5,13 @@ import type { SiblingPath } from './sibling_path.js';
|
|
|
5
5
|
* Contains information which can be used to prove that a leaf is a member of a Merkle tree.
|
|
6
6
|
*/
|
|
7
7
|
export declare class MembershipWitness<N extends number> {
|
|
8
|
+
/**
|
|
9
|
+
* Index of a leaf in the Merkle tree.
|
|
10
|
+
*/
|
|
8
11
|
leafIndex: bigint;
|
|
12
|
+
/**
|
|
13
|
+
* Sibling path of the leaf in the Merkle tree.
|
|
14
|
+
*/
|
|
9
15
|
siblingPath: Tuple<Fr, N>;
|
|
10
16
|
constructor(
|
|
11
17
|
/**
|
|
@@ -54,4 +60,4 @@ export declare class MembershipWitness<N extends number> {
|
|
|
54
60
|
};
|
|
55
61
|
static fromSiblingPath<N extends number>(leafIndex: bigint, siblingPath: SiblingPath<N>): MembershipWitness<N>;
|
|
56
62
|
}
|
|
57
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
63
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWVtYmVyc2hpcF93aXRuZXNzLmQudHM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;OAIG;IACH,MAAM,CAAC,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,YAAY,EAAE,IAAI,EAAE,CAAC,GAAG,iBAAiB,CAAC,CAAC,CAAC,CAKhG;IAED;;;;OAIG;IACH,MAAM,CAAC,YAAY,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,CAAC,GAAG;QAAE,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,GAAG,iBAAiB,CAAC,CAAC,CAAC,CAAA;KAAE,CASlH;IAED,MAAM,CAAC,eAAe,CAAC,CAAC,SAAS,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,iBAAiB,CAAC,CAAC,CAAC,CAE7G;CACF"}
|
|
@@ -12,6 +12,7 @@ import type { Hasher } from './hasher.js';
|
|
|
12
12
|
* And the elements would be ordered as: [ leaf_at_index_2, node_at_level_2_index_0, node_at_level_1_index_1 ].
|
|
13
13
|
*/
|
|
14
14
|
export declare class SiblingPath<N extends number> {
|
|
15
|
+
/** Size of the sibling path (number of fields it contains). */
|
|
15
16
|
pathSize: N;
|
|
16
17
|
private data;
|
|
17
18
|
/**
|
|
@@ -93,4 +94,4 @@ export declare class SiblingPath<N extends number> {
|
|
|
93
94
|
}
|
|
94
95
|
/** Computes the expected root of a merkle tree given a leaf and its sibling path. */
|
|
95
96
|
export declare function computeRootFromSiblingPath(leaf: Buffer, siblingPath: Buffer[], index: number, hasher?: (left: Buffer<ArrayBufferLike>, right: Buffer<ArrayBufferLike>) => Promise<Buffer<ArrayBufferLike>>): Promise<Buffer<ArrayBufferLike>>;
|
|
96
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
97
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2libGluZ19wYXRoLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdH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Root":"","sources":["../../src/trees/sibling_path.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAE9C,OAAO,EACL,KAAK,KAAK,EAIX,MAAM,uBAAuB,CAAC;AAE/B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C;;;;;;;;;GASG;AACH,qBAAa,WAAW,CAAC,CAAC,SAAS,MAAM;
|
|
1
|
+
{"version":3,"file":"sibling_path.d.ts","sourceRoot":"","sources":["../../src/trees/sibling_path.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAE9C,OAAO,EACL,KAAK,KAAK,EAIX,MAAM,uBAAuB,CAAC;AAE/B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C;;;;;;;;;GASG;AACH,qBAAa,WAAW,CAAC,CAAC,SAAS,MAAM;IASrC,+DAA+D;IACxD,QAAQ,EAAE,CAAC;IATpB,OAAO,CAAC,IAAI,CAAmB;IAE/B;;;;OAIG;IACH;IACE,+DAA+D;IACxD,QAAQ,EAAE,CAAC;IAClB,6BAA6B;IAC7B,IAAI,EAAE,MAAM,EAAE,EAGf;IAED,MAAM,KAAK,MAAM,sHAEhB;IAED,MAAM,CAAC,SAAS,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,CAAC,+JAKzC;IAED,MAAM,4BAEL;IAED;;;;;;OAMG;IACH,OAAc,IAAI,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC,CAQjG;IAED,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,CAAC,kBAGxC;IAED;;;OAGG;IACI,QAAQ,IAAI,MAAM,CAExB;IAED;;;OAGG;IACI,aAAa,IAAI,MAAM,EAAE,CAE/B;IAED;;;OAGG;IACI,QAAQ,IAAI,EAAE,EAAE,CAEtB;IAED;;;OAGG;IACI,OAAO,IAAI,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,CAG7B;IAED;;;;;OAKG;IACH,MAAM,CAAC,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,SAAI,GAAG,WAAW,CAAC,CAAC,CAAC,CAG3E;IAED;;;;;OAKG;IACH,MAAM,CAAC,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,SAAI;;;MAQ3D;IAED;;;OAGG;IACI,QAAQ,IAAI,MAAM,CAExB;IAED;;;;OAIG;IACH,OAAc,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC,CAEvE;IAED;;;;OAIG;IACI,qBAAqB,CAAC,aAAa,SAAS,MAAM,EAAE,wBAAwB,SAAS,MAAM,EAChG,aAAa,EAAE,aAAa,GAC3B,WAAW,CAAC,wBAAwB,CAAC,CAKvC;CACF;AAED,qFAAqF;AACrF,wBAAsB,0BAA0B,CAC9C,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EAAE,EACrB,KAAK,EAAE,MAAM,EACb,MAAM,sGAAwF,oCAQ/F"}
|
|
@@ -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,eyJ2ZXJzaW9uIjozLCJm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T0FLRztJQUNJLGNBQWMsQ0FBQyxDQUFDLFNBQVMsTUFBTSxFQUFFLEtBQUssRUFBRSxNQUFNLEdBQUcsV0FBVyxDQUFDLENBQUMsQ0FBQyxDQU9yRTtJQUVEOzs7O09BSUc7SUFDSSx5QkFBeUIsQ0FBQyxDQUFDLFNBQVMsTUFBTSxFQUFFLFNBQVMsRUFBRSxNQUFNLEdBQUcsV0FBVyxDQUFDLENBQUMsQ0FBQyxDQW1CcEY7SUFFTSxlQUFlLENBQUMsU0FBUyxFQUFFLE1BQU0sb0JBRXZDO0lBRUQ7OztPQUdHO0lBQ0gsT0FBTyxDQUFDLFNBQVM7SUErQ2pCLE9BQU8sQ0FBQyxXQUFXO0lBWW5CLE9BQU8sQ0FBQyxtQkFBbUI7Q0FtQjVCIn0=
|
|
@@ -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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidW5iYWxhbmNlZF9tZXJrbGVfdHJlZV9yb290LmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdHJlZXMvdW5iYWxhbmNlZF9tZXJrbGVfdHJlZV9yb290LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUlBLGVBQU8sTUFBTSx3QkFBd0IsZ0VBQStFLENBQUM7QUFFckgsZUFBTyxNQUFNLDZCQUE2Qix5RUFDOEIsQ0FBQztBQUV6RSxlQUFPLE1BQU0sbUJBQW1CLGdFQUEyRCxDQUFDO0FBRTVGOzs7Ozs7Ozs7Ozs7Ozs7Ozs7OztHQW9CRztBQUNILHdCQUFnQiwrQkFBK0IsQ0FDN0MsTUFBTSxFQUFFLE1BQU0sRUFBRSxFQUNoQixNQUFNLDhGQUFnQixFQUN0QixTQUFTLHNCQUFtQixHQUMzQixNQUFNLENBNkJSO0FBRUQsd0JBQXNCLG9DQUFvQyxDQUN4RCxNQUFNLEVBQUUsTUFBTSxFQUFFLEVBQ2hCLE1BQU0sdUdBQXFCLEVBQzNCLFNBQVMsc0JBQW1CLEdBQzNCLE9BQU8sQ0FBQyxNQUFNLENBQUMsQ0E2QmpCO0FBTUQsd0JBQWdCLDBCQUEwQixDQUN4QyxNQUFNLEVBQUUsTUFBTSxFQUFFLEVBQ2hCLGVBQWUsc0JBQW1CLEVBQ2xDLFNBQVMsc0JBQW1CLEVBQzVCLE1BQU0sOEZBQWdCLEdBQ3JCLE1BQU0sQ0FHUiJ9
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unbalanced_merkle_tree_root.d.ts","sourceRoot":"","sources":["../../src/trees/unbalanced_merkle_tree_root.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,wBAAwB,gEAA+E,CAAC;AAErH,eAAO,MAAM,6BAA6B,yEAC8B,CAAC;AAEzE,eAAO,MAAM,mBAAmB,gEAA2D,CAAC;AAE5F;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,+BAA+B,CAC7C,MAAM,EAAE,MAAM,EAAE,EAChB,MAAM,8FAAgB,EACtB,SAAS,sBAAmB,GAC3B,MAAM,CA6BR;AAED,wBAAsB,oCAAoC,CACxD,MAAM,EAAE,MAAM,EAAE,EAChB,MAAM,uGAAqB,EAC3B,SAAS,sBAAmB,GAC3B,OAAO,CAAC,MAAM,CAAC,CA6BjB;AAMD,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,MAAM,EAAE,EAChB,eAAe,sBAAmB,EAClC,SAAS,sBAAmB,EAC5B,MAAM,8FAAgB,GACrB,MAAM,CAGR"}
|
|
@@ -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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidW5iYWxhbmNlZF90cmVlX3N0b3JlLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdHJlZXMvdW5iYWxhbmNlZF90cmVlX3N0b3JlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE1BQU0sV0FBVyxnQkFBZ0I7SUFDL0IsS0FBSyxFQUFFLE1BQU0sQ0FBQztJQUNkLEtBQUssRUFBRSxNQUFNLENBQUM7Q0FDZjtBQU9ELHFCQUFhLG1CQUFtQixDQUFDLENBQUM7O0lBSWhDLFlBQVksU0FBUyxFQUFFLE1BQU0sRUFFNUI7SUFFRCxPQUFPLENBQUMsU0FBUyxFQUFFLE1BQU0sRUFBRSxLQUFLLEVBQUUsQ0FBQyxHQUFHLGdCQUFnQixDQWVyRDtJQUVELE9BQU8sQ0FBQyxFQUFFLEtBQUssRUFBRSxLQUFLLEVBQUUsRUFBRSxnQkFBZ0IsRUFBRSxLQUFLLEVBQUUsQ0FBQyxRQVNuRDtJQUVELGlCQUFpQixDQUFDLEVBQUUsS0FBSyxFQUFFLEtBQUssRUFBRSxFQUFFLGdCQUFnQixHQUFHLGdCQUFnQixDQU10RTtJQUVELGtCQUFrQixDQUFDLEVBQUUsS0FBSyxFQUFFLEtBQUssRUFBRSxFQUFFLGdCQUFnQixHQUFHLGdCQUFnQixDQU12RTtJQUVELGlCQUFpQixDQUFDLEVBQUUsS0FBSyxFQUFFLEtBQUssRUFBRSxFQUFFLGdCQUFnQixHQUFHLENBQUMsZ0JBQWdCLEVBQUUsZ0JBQWdCLENBQUMsQ0FJMUY7SUFFRCxPQUFPLENBQUMsU0FBUyxFQUFFLE1BQU0sR0FBRyxDQUFDLEdBQUcsU0FBUyxDQU94QztJQUVELE9BQU8sQ0FBQyxRQUFRLEVBQUUsZ0JBQWdCLEdBQUcsQ0FBQyxHQUFHLFNBQVMsQ0FFakQ7SUFFRCxPQUFPLElBQUksQ0FBQyxHQUFHLFNBQVMsQ0FFdkI7SUFFRCxTQUFTLENBQUMsUUFBUSxFQUFFLGdCQUFnQixHQUFHLENBQUMsR0FBRyxTQUFTLENBR25EO0lBRUQsVUFBVSxDQUFDLFFBQVEsRUFBRSxnQkFBZ0IsR0FBRyxDQUFDLEdBQUcsU0FBUyxDQUdwRDtJQUVELFdBQVcsQ0FBQyxRQUFRLEVBQUUsZ0JBQWdCLEdBQUcsQ0FBQyxDQUFDLEdBQUcsU0FBUyxFQUFFLENBQUMsR0FBRyxTQUFTLENBQUMsQ0FHdEU7Q0FLRjtBQXNERCx3QkFBZ0IscUJBQXFCLENBQUMsU0FBUyxFQUFFLE1BQU0sRUFBRSxTQUFTLEVBQUUsTUFBTTs7O0VBR3pFIn0=
|
|
@@ -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.20260115",
|
|
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.20260115",
|
|
127
127
|
"@koa/cors": "^5.0.0",
|
|
128
128
|
"@noble/curves": "=1.7.0",
|
|
129
129
|
"@noble/hashes": "^1.6.1",
|
|
@@ -162,7 +162,7 @@
|
|
|
162
162
|
"@types/node": "^22.15.17",
|
|
163
163
|
"@types/pako": "^2.0.0",
|
|
164
164
|
"@types/supertest": "^2.0.12",
|
|
165
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
165
|
+
"@typescript/native-preview": "7.0.0-dev.20260113.1",
|
|
166
166
|
"comlink": "^4.4.1",
|
|
167
167
|
"eslint": "^9.26.0",
|
|
168
168
|
"eslint-config-prettier": "^10.1.5",
|
package/src/config/index.ts
CHANGED
|
@@ -149,6 +149,23 @@ export function floatConfigHelper(
|
|
|
149
149
|
};
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
+
/**
|
|
153
|
+
* Parses an environment variable to a 0-1 percentage value
|
|
154
|
+
*/
|
|
155
|
+
export function percentageConfigHelper(defaultVal: number): Pick<ConfigMapping, 'parseEnv' | 'defaultValue'> {
|
|
156
|
+
return {
|
|
157
|
+
parseEnv: (val: string): number => {
|
|
158
|
+
const parsed = safeParseFloat(val, defaultVal);
|
|
159
|
+
if (parsed < 0 || parsed > 1) {
|
|
160
|
+
throw new TypeError(`Invalid percentage value: ${parsed} should be between 0 and 1`);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return parsed;
|
|
164
|
+
},
|
|
165
|
+
defaultValue: defaultVal,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
152
169
|
/**
|
|
153
170
|
* Generates parseEnv and default values for a numerical config value.
|
|
154
171
|
* @param defaultVal - The default numerical value to use if the environment variable is not set or is invalid
|
|
@@ -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
|
-
}
|