@aztec/foundation 0.55.1 → 0.56.0

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 (73) hide show
  1. package/dest/abi/decoder.d.ts +6 -6
  2. package/dest/abi/decoder.d.ts.map +1 -1
  3. package/dest/abi/decoder.js +23 -24
  4. package/dest/buffer/buffer32.d.ts +9 -0
  5. package/dest/buffer/buffer32.d.ts.map +1 -1
  6. package/dest/buffer/buffer32.js +18 -1
  7. package/dest/config/env_var.d.ts +1 -1
  8. package/dest/config/env_var.d.ts.map +1 -1
  9. package/dest/crypto/index.d.ts +1 -0
  10. package/dest/crypto/index.d.ts.map +1 -1
  11. package/dest/crypto/index.js +2 -1
  12. package/dest/crypto/secp256k1-signer/index.d.ts +3 -0
  13. package/dest/crypto/secp256k1-signer/index.d.ts.map +1 -0
  14. package/dest/crypto/secp256k1-signer/index.js +3 -0
  15. package/dest/crypto/secp256k1-signer/secp256k1_signer.d.ts +24 -0
  16. package/dest/crypto/secp256k1-signer/secp256k1_signer.d.ts.map +1 -0
  17. package/dest/crypto/secp256k1-signer/secp256k1_signer.js +31 -0
  18. package/dest/crypto/secp256k1-signer/utils.d.ts +39 -0
  19. package/dest/crypto/secp256k1-signer/utils.d.ts.map +1 -0
  20. package/dest/crypto/secp256k1-signer/utils.js +89 -0
  21. package/dest/crypto/sha256/index.d.ts +15 -3
  22. package/dest/crypto/sha256/index.d.ts.map +1 -1
  23. package/dest/crypto/sha256/index.js +110 -5
  24. package/dest/eth-signature/eth_signature.d.ts +54 -0
  25. package/dest/eth-signature/eth_signature.d.ts.map +1 -0
  26. package/dest/eth-signature/eth_signature.js +69 -0
  27. package/dest/eth-signature/index.d.ts +2 -0
  28. package/dest/eth-signature/index.d.ts.map +1 -0
  29. package/dest/eth-signature/index.js +2 -0
  30. package/dest/fields/point.d.ts.map +1 -1
  31. package/dest/fields/point.js +3 -2
  32. package/dest/index.d.ts +1 -0
  33. package/dest/index.d.ts.map +1 -1
  34. package/dest/index.js +2 -1
  35. package/dest/serialize/buffer_reader.d.ts +9 -0
  36. package/dest/serialize/buffer_reader.d.ts.map +1 -1
  37. package/dest/serialize/buffer_reader.js +18 -1
  38. package/dest/serialize/serialize.js +2 -2
  39. package/dest/serialize/types.d.ts +1 -1
  40. package/dest/serialize/types.d.ts.map +1 -1
  41. package/dest/serialize/types.js +1 -1
  42. package/dest/testing/index.d.ts +1 -0
  43. package/dest/testing/index.d.ts.map +1 -1
  44. package/dest/testing/index.js +2 -1
  45. package/dest/testing/port_allocator.d.ts +10 -0
  46. package/dest/testing/port_allocator.d.ts.map +1 -0
  47. package/dest/testing/port_allocator.js +32 -0
  48. package/dest/trees/index.d.ts +1 -0
  49. package/dest/trees/index.d.ts.map +1 -1
  50. package/dest/trees/index.js +2 -2
  51. package/dest/trees/unbalanced_merkle_root.d.ts +12 -0
  52. package/dest/trees/unbalanced_merkle_root.d.ts.map +1 -0
  53. package/dest/trees/unbalanced_merkle_root.js +52 -0
  54. package/package.json +5 -3
  55. package/src/abi/decoder.ts +24 -25
  56. package/src/buffer/buffer32.ts +18 -0
  57. package/src/config/env_var.ts +1 -1
  58. package/src/crypto/index.ts +1 -0
  59. package/src/crypto/secp256k1-signer/index.ts +2 -0
  60. package/src/crypto/secp256k1-signer/secp256k1_signer.ts +38 -0
  61. package/src/crypto/secp256k1-signer/utils.ts +99 -0
  62. package/src/crypto/sha256/index.ts +137 -4
  63. package/src/eth-signature/eth_signature.ts +90 -0
  64. package/src/eth-signature/index.ts +1 -0
  65. package/src/fields/point.ts +2 -1
  66. package/src/index.ts +1 -0
  67. package/src/serialize/buffer_reader.ts +20 -0
  68. package/src/serialize/serialize.ts +1 -1
  69. package/src/serialize/types.ts +1 -1
  70. package/src/testing/index.ts +1 -0
  71. package/src/testing/port_allocator.ts +31 -0
  72. package/src/trees/index.ts +2 -0
  73. package/src/trees/unbalanced_merkle_root.ts +52 -0
package/src/index.ts CHANGED
@@ -30,3 +30,4 @@ export * as worker from './worker/index.js';
30
30
  export * as testing from './testing/index.js';
31
31
  export * as config from './config/index.js';
32
32
  export * as buffer from './buffer/index.js';
33
+ export * as ethSignature from './eth-signature/index.js';
@@ -70,6 +70,26 @@ export class BufferReader {
70
70
  return result as Tuple<number, N>;
71
71
  }
72
72
 
73
+ /**
74
+ * Reads a 256-bit unsigned integer from the buffer at the current index position.
75
+ * Updates the index position by 32 bytes after reading the number.
76
+ *
77
+ * Assumes the number is stored in big-endian format.
78
+ *
79
+ * @returns The read 256 bit value as a bigint.
80
+ */
81
+ public readUInt256(): bigint {
82
+ this.#rangeCheck(32);
83
+
84
+ let result = BigInt(0);
85
+ for (let i = 0; i < 32; i++) {
86
+ result = (result << BigInt(8)) | BigInt(this.buffer[this.index + i]);
87
+ }
88
+
89
+ this.index += 32;
90
+ return result;
91
+ }
92
+
73
93
  /**
74
94
  * Reads a 16-bit unsigned integer from the buffer at the current index position.
75
95
  * Updates the index position by 2 bytes after reading the number.
@@ -242,7 +242,7 @@ export function toFriendlyJSON(obj: object): string {
242
242
  ).toFriendlyJSON
243
243
  ) {
244
244
  return value.toFriendlyJSON();
245
- } else if (value && value.type && ['Fr', 'Fq', 'AztecAddress'].includes(value.type)) {
245
+ } else if (value && value.type && ['Fr', 'Fq', 'AztecAddress', 'EthAddress'].includes(value.type)) {
246
246
  return value.value;
247
247
  } else {
248
248
  return value;
@@ -35,6 +35,6 @@ type MapTuple<T extends any[], F extends (item: any) => any> = {
35
35
  * @see https://github.com/microsoft/TypeScript/issues/29841.
36
36
  * @param array - A tuple array.
37
37
  */
38
- export function mapTuple<T extends any[], F extends (item: any) => any>(tuple: T, fn: F): MapTuple<T, F> {
38
+ export function mapTuple<T extends any[], F extends (item: T[number]) => any>(tuple: T, fn: F): MapTuple<T, F> {
39
39
  return tuple.map(fn) as MapTuple<T, F>;
40
40
  }
@@ -1,2 +1,3 @@
1
1
  export * from './test_data.js';
2
2
  export * from './snapshot_serializer.js';
3
+ export * from './port_allocator.js';
@@ -0,0 +1,31 @@
1
+ import net from 'net';
2
+
3
+ /**
4
+ * Get a random port that is free to use.
5
+ * Returns undefined if it fails to get a port.
6
+ *
7
+ * @attribution: adapted from https://stackoverflow.com/a/71178451
8
+ *
9
+ * @returns a random port that is free to use.
10
+ */
11
+ export function getRandomPort(): Promise<number | undefined> {
12
+ return new Promise(resolve => {
13
+ const server = net.createServer();
14
+ server.listen(0, () => {
15
+ const address = server.address();
16
+ if (address && typeof address === 'object' && 'port' in address) {
17
+ const port = address.port;
18
+ server.close(() => {
19
+ resolve(port);
20
+ });
21
+ } else {
22
+ server.close(() => {
23
+ resolve(undefined);
24
+ });
25
+ }
26
+ });
27
+ server.on('error', () => {
28
+ resolve(undefined);
29
+ });
30
+ });
31
+ }
@@ -1,3 +1,5 @@
1
+ export * from './unbalanced_merkle_root.js';
2
+
1
3
  /**
2
4
  * A leaf of an indexed merkle tree.
3
5
  */
@@ -0,0 +1,52 @@
1
+ import { padArrayEnd } from '@aztec/foundation/collection';
2
+ import { sha256Trunc } from '@aztec/foundation/crypto';
3
+
4
+ /**
5
+ * Computes the merkle root for an unbalanced tree.
6
+ *
7
+ * @dev Adapted from proving-state.ts -> findMergeLevel and unbalanced_tree.ts.
8
+ * Calculates the tree upwards layer by layer until we reach the root.
9
+ * The L1 calculation instead computes the tree from right to left (slightly cheaper gas).
10
+ * TODO: A more thorough investigation of which method is cheaper, then use that method everywhere.
11
+ */
12
+ export function computeUnbalancedMerkleRoot(leaves: Buffer[], emptyLeaf?: Buffer, hasher = sha256Trunc): Buffer {
13
+ // Pad leaves to 2
14
+ if (leaves.length < 2) {
15
+ if (emptyLeaf === undefined) {
16
+ throw new Error('Cannot compute a Merkle root with less than 2 leaves');
17
+ } else {
18
+ leaves = padArrayEnd(leaves, emptyLeaf, 2);
19
+ }
20
+ }
21
+
22
+ const depth = Math.ceil(Math.log2(leaves.length));
23
+ let [layerWidth, nodeToShift] =
24
+ leaves.length & 1 ? [leaves.length - 1, leaves[leaves.length - 1]] : [leaves.length, Buffer.alloc(0)];
25
+ // Allocate this layer's leaves and init the next layer up
26
+ let thisLayer = leaves.slice(0, layerWidth);
27
+ let nextLayer = [];
28
+ for (let i = 0; i < depth; i++) {
29
+ for (let j = 0; j < layerWidth; j += 2) {
30
+ // Store the hash of each pair one layer up
31
+ nextLayer[j / 2] = hasher(Buffer.concat([thisLayer[j], thisLayer[j + 1]]));
32
+ }
33
+ layerWidth /= 2;
34
+ if (layerWidth & 1) {
35
+ if (nodeToShift.length) {
36
+ // If the next layer has odd length, and we have a node that needs to be shifted up, add it here
37
+ nextLayer.push(nodeToShift);
38
+ layerWidth += 1;
39
+ nodeToShift = Buffer.alloc(0);
40
+ } else {
41
+ // If we don't have a node waiting to be shifted, store the next layer's final node to be shifted
42
+ layerWidth -= 1;
43
+ nodeToShift = nextLayer[layerWidth];
44
+ }
45
+ }
46
+ // reset the layers
47
+ thisLayer = nextLayer;
48
+ nextLayer = [];
49
+ }
50
+ // return the root
51
+ return thisLayer[0];
52
+ }