@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.
Files changed (57) hide show
  1. package/dest/buffer/buffer16.d.ts +4 -1
  2. package/dest/buffer/buffer16.d.ts.map +1 -1
  3. package/dest/buffer/buffer32.d.ts +4 -1
  4. package/dest/buffer/buffer32.d.ts.map +1 -1
  5. package/dest/config/index.d.ts +5 -1
  6. package/dest/config/index.d.ts.map +1 -1
  7. package/dest/config/index.js +14 -0
  8. package/dest/crypto/ecdsa/signature.d.ts +10 -1
  9. package/dest/crypto/ecdsa/signature.d.ts.map +1 -1
  10. package/dest/curves/bls12/point.d.ts +10 -1
  11. package/dest/curves/bls12/point.d.ts.map +1 -1
  12. package/dest/curves/grumpkin/point.d.ts +11 -2
  13. package/dest/curves/grumpkin/point.d.ts.map +1 -1
  14. package/dest/error/index.d.ts +4 -4
  15. package/dest/error/index.d.ts.map +1 -1
  16. package/dest/eth-signature/eth_signature.d.ts +4 -1
  17. package/dest/eth-signature/eth_signature.d.ts.map +1 -1
  18. package/dest/json-rpc/fixtures/class_a.d.ts +3 -3
  19. package/dest/json-rpc/fixtures/class_a.d.ts.map +1 -1
  20. package/dest/json-rpc/fixtures/class_b.d.ts +3 -3
  21. package/dest/json-rpc/fixtures/class_b.d.ts.map +1 -1
  22. package/dest/serialize/buffer_reader.d.ts +7 -4
  23. package/dest/serialize/buffer_reader.d.ts.map +1 -1
  24. package/dest/serialize/buffer_reader.js +13 -4
  25. package/dest/trees/{balanced_merkle_tree.d.ts → balanced_merkle_tree_root.d.ts} +1 -5
  26. package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -0
  27. package/dest/trees/{balanced_merkle_tree.js → balanced_merkle_tree_root.js} +1 -15
  28. package/dest/trees/hasher.d.ts +4 -1
  29. package/dest/trees/hasher.d.ts.map +1 -1
  30. package/dest/trees/hasher.js +15 -5
  31. package/dest/trees/index.d.ts +4 -4
  32. package/dest/trees/index.d.ts.map +1 -1
  33. package/dest/trees/index.js +3 -3
  34. package/dest/trees/membership_witness.d.ts +7 -1
  35. package/dest/trees/membership_witness.d.ts.map +1 -1
  36. package/dest/trees/sibling_path.d.ts +2 -1
  37. package/dest/trees/sibling_path.d.ts.map +1 -1
  38. package/dest/trees/unbalanced_merkle_tree_calculator.d.ts +2 -2
  39. package/dest/trees/unbalanced_merkle_tree_calculator.d.ts.map +1 -1
  40. package/dest/trees/unbalanced_merkle_tree_calculator.js +1 -1
  41. package/dest/trees/{unbalanced_merkle_tree.d.ts → unbalanced_merkle_tree_root.d.ts} +3 -7
  42. package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -0
  43. package/dest/trees/{unbalanced_merkle_tree.js → unbalanced_merkle_tree_root.js} +8 -52
  44. package/dest/trees/unbalanced_tree_store.d.ts +5 -1
  45. package/dest/trees/unbalanced_tree_store.d.ts.map +1 -1
  46. package/dest/trees/unbalanced_tree_store.js +49 -1
  47. package/package.json +3 -3
  48. package/src/config/index.ts +17 -0
  49. package/src/serialize/buffer_reader.ts +21 -9
  50. package/src/trees/{balanced_merkle_tree.ts → balanced_merkle_tree_root.ts} +1 -14
  51. package/src/trees/hasher.ts +13 -0
  52. package/src/trees/index.ts +3 -3
  53. package/src/trees/unbalanced_merkle_tree_calculator.ts +1 -2
  54. package/src/trees/{unbalanced_merkle_tree.ts → unbalanced_merkle_tree_root.ts} +8 -66
  55. package/src/trees/unbalanced_tree_store.ts +57 -2
  56. package/dest/trees/balanced_merkle_tree.d.ts.map +0 -1
  57. package/dest/trees/unbalanced_merkle_tree.d.ts.map +0 -1
@@ -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,yEAC8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