@aztec/foundation 0.0.1-commit.b6e433891 → 0.0.1-commit.b8a057fa

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 (258) hide show
  1. package/dest/bigint-buffer/index.d.ts +1 -1
  2. package/dest/bigint-buffer/index.d.ts.map +1 -1
  3. package/dest/bigint-buffer/index.js +6 -1
  4. package/dest/branded-types/block_number.d.ts +3 -3
  5. package/dest/branded-types/block_number.d.ts.map +1 -1
  6. package/dest/branded-types/block_number.js +15 -4
  7. package/dest/branded-types/buffer32_hash.d.ts +27 -0
  8. package/dest/branded-types/buffer32_hash.d.ts.map +1 -0
  9. package/dest/branded-types/buffer32_hash.js +12 -0
  10. package/dest/branded-types/checkpoint_number.d.ts +9 -3
  11. package/dest/branded-types/checkpoint_number.d.ts.map +1 -1
  12. package/dest/branded-types/checkpoint_number.js +11 -0
  13. package/dest/branded-types/epoch.d.ts +2 -2
  14. package/dest/branded-types/epoch.d.ts.map +1 -1
  15. package/dest/branded-types/index.d.ts +2 -1
  16. package/dest/branded-types/index.d.ts.map +1 -1
  17. package/dest/branded-types/index.js +1 -0
  18. package/dest/branded-types/index_within_checkpoint.d.ts +2 -2
  19. package/dest/branded-types/index_within_checkpoint.d.ts.map +1 -1
  20. package/dest/branded-types/slot.d.ts +5 -2
  21. package/dest/branded-types/slot.d.ts.map +1 -1
  22. package/dest/branded-types/slot.js +3 -0
  23. package/dest/buffer/buffer16.d.ts +3 -1
  24. package/dest/buffer/buffer16.d.ts.map +1 -1
  25. package/dest/buffer/buffer32.d.ts +28 -61
  26. package/dest/buffer/buffer32.d.ts.map +1 -1
  27. package/dest/buffer/buffer32.js +29 -63
  28. package/dest/collection/array.d.ts +11 -1
  29. package/dest/collection/array.d.ts.map +1 -1
  30. package/dest/collection/array.js +33 -0
  31. package/dest/collection/index.d.ts +3 -1
  32. package/dest/collection/index.d.ts.map +1 -1
  33. package/dest/collection/index.js +2 -0
  34. package/dest/collection/lru_map.d.ts +41 -0
  35. package/dest/collection/lru_map.d.ts.map +1 -0
  36. package/dest/collection/lru_map.js +119 -0
  37. package/dest/collection/lru_set.d.ts +35 -0
  38. package/dest/collection/lru_set.d.ts.map +1 -0
  39. package/dest/collection/lru_set.js +95 -0
  40. package/dest/committable/committable.js +1 -1
  41. package/dest/config/env_var.d.ts +2 -2
  42. package/dest/config/env_var.d.ts.map +1 -1
  43. package/dest/config/index.d.ts +67 -37
  44. package/dest/config/index.d.ts.map +1 -1
  45. package/dest/config/index.js +72 -71
  46. package/dest/config/network_config.d.ts +11 -55
  47. package/dest/config/network_config.d.ts.map +1 -1
  48. package/dest/config/network_name.d.ts +2 -2
  49. package/dest/config/network_name.d.ts.map +1 -1
  50. package/dest/config/network_name.js +1 -3
  51. package/dest/config/secret_value.d.ts +2 -2
  52. package/dest/config/secret_value.d.ts.map +1 -1
  53. package/dest/crypto/aes128/index.d.ts +2 -1
  54. package/dest/crypto/aes128/index.d.ts.map +1 -1
  55. package/dest/crypto/aes128/index.js +11 -2
  56. package/dest/crypto/bls/bn254_keystore.d.ts +10 -177
  57. package/dest/crypto/bls/bn254_keystore.d.ts.map +1 -1
  58. package/dest/crypto/bls/bn254_keystore.js +22 -34
  59. package/dest/crypto/keys/index.d.ts +3 -2
  60. package/dest/crypto/keys/index.d.ts.map +1 -1
  61. package/dest/crypto/keys/index.js +30 -6
  62. package/dest/crypto/poseidon/index.d.ts +1 -1
  63. package/dest/crypto/poseidon/index.d.ts.map +1 -1
  64. package/dest/crypto/poseidon/index.js +40 -33
  65. package/dest/crypto/schnorr/index.d.ts +8 -7
  66. package/dest/crypto/schnorr/index.d.ts.map +1 -1
  67. package/dest/crypto/schnorr/index.js +6 -6
  68. package/dest/crypto/schnorr/signature.d.ts +9 -29
  69. package/dest/crypto/schnorr/signature.d.ts.map +1 -1
  70. package/dest/crypto/schnorr/signature.js +20 -36
  71. package/dest/curves/bls12/field.d.ts +3 -3
  72. package/dest/curves/bls12/field.d.ts.map +1 -1
  73. package/dest/curves/bls12/field.js +0 -5
  74. package/dest/curves/bls12/point.d.ts +2 -2
  75. package/dest/curves/bls12/point.d.ts.map +1 -1
  76. package/dest/curves/bn254/field.d.ts +23 -19
  77. package/dest/curves/bn254/field.d.ts.map +1 -1
  78. package/dest/curves/bn254/field.js +23 -17
  79. package/dest/curves/grumpkin/point.d.ts +14 -28
  80. package/dest/curves/grumpkin/point.d.ts.map +1 -1
  81. package/dest/curves/grumpkin/point.js +29 -43
  82. package/dest/eth-address/index.d.ts +2 -2
  83. package/dest/eth-address/index.d.ts.map +1 -1
  84. package/dest/eth-address/index.js +0 -3
  85. package/dest/eth-signature/eth_signature.d.ts +2 -2
  86. package/dest/eth-signature/eth_signature.d.ts.map +1 -1
  87. package/dest/fifo/fifo_frame_reader.d.ts +41 -0
  88. package/dest/fifo/fifo_frame_reader.d.ts.map +1 -0
  89. package/dest/fifo/fifo_frame_reader.js +91 -0
  90. package/dest/fifo/index.d.ts +2 -0
  91. package/dest/fifo/index.d.ts.map +1 -0
  92. package/dest/fifo/index.js +1 -0
  93. package/dest/fifo_set/fifo_set.d.ts +15 -0
  94. package/dest/fifo_set/fifo_set.d.ts.map +1 -0
  95. package/dest/fifo_set/fifo_set.js +39 -0
  96. package/dest/fifo_set/index.d.ts +2 -0
  97. package/dest/fifo_set/index.d.ts.map +1 -0
  98. package/dest/fifo_set/index.js +1 -0
  99. package/dest/json-rpc/client/fetch.d.ts +1 -1
  100. package/dest/json-rpc/client/fetch.d.ts.map +1 -1
  101. package/dest/json-rpc/client/fetch.js +4 -3
  102. package/dest/json-rpc/client/safe_json_rpc_client.d.ts +1 -1
  103. package/dest/json-rpc/client/safe_json_rpc_client.d.ts.map +1 -1
  104. package/dest/json-rpc/client/safe_json_rpc_client.js +2 -2
  105. package/dest/json-rpc/client/undici.d.ts +1 -1
  106. package/dest/json-rpc/client/undici.d.ts.map +1 -1
  107. package/dest/json-rpc/client/undici.js +5 -3
  108. package/dest/json-rpc/fixtures/test_state.d.ts +4 -8
  109. package/dest/json-rpc/fixtures/test_state.d.ts.map +1 -1
  110. package/dest/json-rpc/fixtures/test_state.js +57 -17
  111. package/dest/json-rpc/server/safe_json_rpc_server.d.ts +8 -3
  112. package/dest/json-rpc/server/safe_json_rpc_server.d.ts.map +1 -1
  113. package/dest/json-rpc/server/safe_json_rpc_server.js +27 -13
  114. package/dest/log/pino-logger.d.ts +1 -1
  115. package/dest/log/pino-logger.d.ts.map +1 -1
  116. package/dest/log/pino-logger.js +17 -2
  117. package/dest/noir/noir_package_config.d.ts +19 -104
  118. package/dest/noir/noir_package_config.d.ts.map +1 -1
  119. package/dest/noir/noir_package_config.js +1 -1
  120. package/dest/promise/running-promise.d.ts +5 -4
  121. package/dest/promise/running-promise.d.ts.map +1 -1
  122. package/dest/promise/running-promise.js +7 -3
  123. package/dest/queue/batch_queue.d.ts +1 -1
  124. package/dest/queue/batch_queue.d.ts.map +1 -1
  125. package/dest/queue/batch_queue.js +1 -0
  126. package/dest/retry/index.d.ts +31 -3
  127. package/dest/retry/index.d.ts.map +1 -1
  128. package/dest/retry/index.js +44 -2
  129. package/dest/schemas/api.d.ts +12 -10
  130. package/dest/schemas/api.d.ts.map +1 -1
  131. package/dest/schemas/api.js +7 -1
  132. package/dest/schemas/parse.d.ts +4 -4
  133. package/dest/schemas/parse.d.ts.map +1 -1
  134. package/dest/schemas/parse.js +6 -5
  135. package/dest/schemas/schemas.d.ts +15 -15
  136. package/dest/schemas/types.d.ts +3 -3
  137. package/dest/schemas/types.d.ts.map +1 -1
  138. package/dest/schemas/utils.d.ts +7 -11
  139. package/dest/schemas/utils.d.ts.map +1 -1
  140. package/dest/schemas/utils.js +2 -18
  141. package/dest/serialize/buffer_sink.d.ts +134 -0
  142. package/dest/serialize/buffer_sink.d.ts.map +1 -0
  143. package/dest/serialize/buffer_sink.js +297 -0
  144. package/dest/serialize/index.d.ts +2 -1
  145. package/dest/serialize/index.d.ts.map +1 -1
  146. package/dest/serialize/index.js +1 -0
  147. package/dest/string/index.js +1 -1
  148. package/dest/testing/files/index.d.ts +2 -3
  149. package/dest/testing/files/index.d.ts.map +1 -1
  150. package/dest/testing/files/index.js +1 -2
  151. package/dest/timer/index.d.ts +2 -2
  152. package/dest/timer/index.d.ts.map +1 -1
  153. package/dest/timer/index.js +1 -1
  154. package/dest/timer/timeout.d.ts +3 -1
  155. package/dest/timer/timeout.d.ts.map +1 -1
  156. package/dest/timer/timeout.js +26 -0
  157. package/dest/transport/dispatch/create_dispatch_proxy.d.ts +11 -2
  158. package/dest/transport/dispatch/create_dispatch_proxy.d.ts.map +1 -1
  159. package/dest/transport/index.d.ts +1 -2
  160. package/dest/transport/index.d.ts.map +1 -1
  161. package/dest/transport/index.js +0 -1
  162. package/dest/trees/balanced_merkle_tree_root.d.ts +2 -3
  163. package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -1
  164. package/dest/trees/balanced_merkle_tree_root.js +2 -3
  165. package/dest/trees/hasher.d.ts +3 -2
  166. package/dest/trees/hasher.d.ts.map +1 -1
  167. package/dest/trees/hasher.js +5 -5
  168. package/dest/trees/indexed_merkle_tree_calculator.d.ts +1 -1
  169. package/dest/trees/indexed_merkle_tree_calculator.d.ts.map +1 -1
  170. package/dest/trees/indexed_merkle_tree_calculator.js +5 -1
  171. package/dest/trees/membership_witness.d.ts +2 -6
  172. package/dest/trees/membership_witness.d.ts.map +1 -1
  173. package/dest/trees/membership_witness.js +0 -9
  174. package/dest/trees/merkle_tree_calculator.d.ts +4 -2
  175. package/dest/trees/merkle_tree_calculator.d.ts.map +1 -1
  176. package/dest/trees/merkle_tree_calculator.js +1 -5
  177. package/dest/trees/sibling_path.d.ts +5 -4
  178. package/dest/trees/sibling_path.d.ts.map +1 -1
  179. package/dest/trees/sibling_path.js +10 -8
  180. package/dest/trees/unbalanced_merkle_tree_root.d.ts +2 -3
  181. package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -1
  182. package/dest/trees/unbalanced_merkle_tree_root.js +2 -3
  183. package/dest/types/index.d.ts +23 -1
  184. package/dest/types/index.d.ts.map +1 -1
  185. package/dest/types/index.js +15 -0
  186. package/package.json +6 -4
  187. package/src/bigint-buffer/index.ts +6 -1
  188. package/src/branded-types/block_number.ts +20 -2
  189. package/src/branded-types/buffer32_hash.ts +38 -0
  190. package/src/branded-types/checkpoint_number.ts +16 -1
  191. package/src/branded-types/epoch.ts +1 -1
  192. package/src/branded-types/index.ts +1 -0
  193. package/src/branded-types/index_within_checkpoint.ts +1 -1
  194. package/src/branded-types/slot.ts +6 -1
  195. package/src/buffer/buffer16.ts +3 -0
  196. package/src/buffer/buffer32.ts +39 -68
  197. package/src/collection/array.ts +34 -0
  198. package/src/collection/index.ts +2 -0
  199. package/src/collection/lru_map.ts +143 -0
  200. package/src/collection/lru_set.ts +115 -0
  201. package/src/committable/committable.ts +1 -1
  202. package/src/config/env_var.ts +48 -24
  203. package/src/config/index.ts +143 -122
  204. package/src/config/network_name.ts +2 -5
  205. package/src/config/secret_value.ts +1 -1
  206. package/src/crypto/aes128/index.ts +11 -2
  207. package/src/crypto/bls/bn254_keystore.ts +33 -43
  208. package/src/crypto/keys/index.ts +20 -4
  209. package/src/crypto/poseidon/index.ts +42 -34
  210. package/src/crypto/schnorr/index.ts +9 -8
  211. package/src/crypto/schnorr/signature.ts +17 -46
  212. package/src/curves/bls12/field.ts +0 -7
  213. package/src/curves/bn254/field.ts +35 -34
  214. package/src/curves/grumpkin/point.ts +23 -40
  215. package/src/eth-address/index.ts +0 -4
  216. package/src/fifo/fifo_frame_reader.ts +113 -0
  217. package/src/fifo/index.ts +1 -0
  218. package/src/fifo_set/fifo_set.ts +52 -0
  219. package/src/fifo_set/index.ts +1 -0
  220. package/src/json-rpc/client/fetch.ts +4 -3
  221. package/src/json-rpc/client/safe_json_rpc_client.ts +2 -2
  222. package/src/json-rpc/client/undici.ts +5 -3
  223. package/src/json-rpc/fixtures/test_state.ts +10 -10
  224. package/src/json-rpc/server/safe_json_rpc_server.ts +31 -13
  225. package/src/log/pino-logger.ts +19 -2
  226. package/src/noir/noir_package_config.ts +32 -20
  227. package/src/promise/running-promise.ts +8 -5
  228. package/src/queue/batch_queue.ts +1 -0
  229. package/src/retry/index.ts +66 -4
  230. package/src/schemas/api.ts +25 -25
  231. package/src/schemas/parse.ts +9 -6
  232. package/src/schemas/schemas.ts +4 -4
  233. package/src/schemas/types.ts +2 -2
  234. package/src/schemas/utils.ts +4 -38
  235. package/src/serialize/buffer_sink.ts +350 -0
  236. package/src/serialize/index.ts +1 -0
  237. package/src/string/index.ts +1 -1
  238. package/src/testing/files/index.ts +1 -2
  239. package/src/timer/index.ts +1 -1
  240. package/src/timer/timeout.ts +31 -0
  241. package/src/transport/dispatch/create_dispatch_proxy.ts +11 -1
  242. package/src/transport/index.ts +0 -1
  243. package/src/trees/balanced_merkle_tree_root.ts +2 -5
  244. package/src/trees/hasher.ts +6 -3
  245. package/src/trees/indexed_merkle_tree_calculator.ts +5 -1
  246. package/src/trees/membership_witness.ts +0 -8
  247. package/src/trees/merkle_tree_calculator.ts +4 -10
  248. package/src/trees/sibling_path.ts +11 -6
  249. package/src/trees/unbalanced_merkle_tree_root.ts +2 -5
  250. package/src/types/index.ts +47 -0
  251. package/dest/crypto/serialize.d.ts +0 -51
  252. package/dest/crypto/serialize.d.ts.map +0 -1
  253. package/dest/crypto/serialize.js +0 -68
  254. package/dest/transport/dispatch/create_dispatch_fn.d.ts +0 -25
  255. package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +0 -1
  256. package/dest/transport/dispatch/create_dispatch_fn.js +0 -17
  257. package/src/crypto/serialize.ts +0 -85
  258. package/src/transport/dispatch/create_dispatch_fn.ts +0 -35
@@ -0,0 +1,350 @@
1
+ /**
2
+ * A value that can be appended to a {@link BufferSink}. Mirrors `Bufferable`, but the object arm allows the
3
+ * optional-sink overload of `toBuffer` so a legacy `toBuffer(): Buffer` and a migrated `toBuffer(sink?)` both fit.
4
+ */
5
+ export type Sinkable =
6
+ | boolean
7
+ | Buffer
8
+ | Uint8Array
9
+ | number
10
+ | bigint
11
+ | string
12
+ | { toBuffer(sink?: BufferSink): Buffer | void }
13
+ | Sinkable[];
14
+
15
+ const DEFAULT_INITIAL_CAPACITY = 1024;
16
+ const MASK_64 = (1n << 64n) - 1n;
17
+
18
+ /**
19
+ * A growable, append-only binary sink backed by a single `ArrayBuffer`.
20
+ *
21
+ * It exists to replace the recursive `toBuffer()` chain — which allocates a `Buffer` at every node and
22
+ * `Buffer.concat`s at every level — with one buffer that the whole object graph streams into and that is sliced
23
+ * exactly once at the root. Encodings are big-endian, matching `serializeToBuffer` byte-for-byte.
24
+ *
25
+ * The migration contract is the optional-sink `toBuffer`:
26
+ *
27
+ * ```ts
28
+ * toBuffer(): Buffer;
29
+ * toBuffer(sink: BufferSink): void;
30
+ * toBuffer(sink?: BufferSink): Buffer | void {
31
+ * if (!sink) return BufferSink.serialize(this); // own a sink, fill it, slice once
32
+ * serializeToSink(sink, this.a, this.b, this.c); // stream into the caller's sink
33
+ * }
34
+ * ```
35
+ *
36
+ * When a sink is passed it writes and returns `undefined`; otherwise it returns its own buffer. Nodes that have
37
+ * not been migrated keep their plain `toBuffer(): Buffer` and are folded in via the return-value fallback in
38
+ * {@link serializeToSink}, so the tree stays valid at every step of an incremental migration.
39
+ *
40
+ * Performance note: bigints are written via `DataView.setBigUint64` limbs ({@link writeBigInt},
41
+ * {@link writeField}), NOT a per-byte shift loop. The limb form is ~16x faster than the legacy hex round-trip
42
+ * on field-heavy structures, and faster than the legacy path at every width — a naive per-byte shift loop is
43
+ * actually the slowest option, since each byte allocates a fresh BigInt. See the spike benchmark.
44
+ */
45
+ export class BufferSink {
46
+ private buffer: Uint8Array;
47
+ private view: DataView;
48
+ private offset = 0;
49
+
50
+ constructor(initialCapacity: number = DEFAULT_INITIAL_CAPACITY) {
51
+ const ab = new ArrayBuffer(initialCapacity);
52
+ this.buffer = new Uint8Array(ab);
53
+ this.view = new DataView(ab);
54
+ }
55
+
56
+ /** Number of bytes written so far. */
57
+ public get length(): number {
58
+ return this.offset;
59
+ }
60
+
61
+ /**
62
+ * Serialize a migrated value into a fresh sink and return the single resulting buffer. This is the no-sink
63
+ * branch of every migrated `toBuffer`: one allocation, one slice, only at the root.
64
+ *
65
+ * @param obj - The value to serialize via its sink-aware `toBuffer`.
66
+ * @param initialCapacity - Optional size hint (e.g. from `getSize()`) to skip reallocations on hot paths.
67
+ */
68
+ public static serialize(obj: { toBuffer(sink: BufferSink): void }, initialCapacity?: number): Buffer {
69
+ const sink = new BufferSink(initialCapacity);
70
+ obj.toBuffer(sink);
71
+ return sink.toBuffer();
72
+ }
73
+
74
+ private ensure(extra: number): void {
75
+ const required = this.offset + extra;
76
+ if (required <= this.buffer.length) {
77
+ return;
78
+ }
79
+ let next = this.buffer.length * 2;
80
+ while (next < required) {
81
+ next *= 2;
82
+ }
83
+ const grown = new Uint8Array(next);
84
+ grown.set(this.buffer.subarray(0, this.offset));
85
+ this.buffer = grown;
86
+ this.view = new DataView(grown.buffer);
87
+ }
88
+
89
+ /** Append a single byte (0/1) for a boolean, matching `boolToBuffer`. */
90
+ public writeBool(value: boolean): void {
91
+ this.ensure(1);
92
+ this.buffer[this.offset++] = value ? 1 : 0;
93
+ }
94
+
95
+ /** Append an unsigned 8-bit integer. */
96
+ public writeUInt8(value: number): void {
97
+ this.ensure(1);
98
+ this.buffer[this.offset++] = value & 0xff;
99
+ }
100
+
101
+ /** Append a big-endian unsigned 16-bit integer. */
102
+ public writeUInt16(value: number): void {
103
+ this.ensure(2);
104
+ this.view.setUint16(this.offset, value, false);
105
+ this.offset += 2;
106
+ }
107
+
108
+ /** Append a big-endian unsigned 32-bit integer, matching `numToUInt32BE`. */
109
+ public writeNumber(value: number): void {
110
+ this.ensure(4);
111
+ this.view.setUint32(this.offset, value, false);
112
+ this.offset += 4;
113
+ }
114
+
115
+ /** Append a big-endian unsigned 64-bit integer, matching `bigintToUInt64BE`. */
116
+ public writeUInt64(value: bigint): void {
117
+ this.ensure(8);
118
+ this.view.setBigUint64(this.offset, value, false);
119
+ this.offset += 8;
120
+ }
121
+
122
+ /**
123
+ * Append a non-negative bigint as a big-endian unsigned integer of `width` bytes (default 32), matching
124
+ * `serializeBigInt` / `toBufferBE`. Every width is written limb-wise via `setBigUint64` rather than the
125
+ * legacy per-byte BigInt shift loop — that loop allocated a fresh BigInt per byte and benchmarked as the
126
+ * *slowest* option (slower even than the legacy hex round-trip); the limb form is faster at every width.
127
+ *
128
+ * The bulk of the value is written as 64-bit big-endian limbs from the least-significant tail upward, and
129
+ * a final `width % 8` (at most 7) high bytes that don't fill a whole limb are written individually. The
130
+ * common widths (8/16/32) are exact multiples of 8, so they take the pure-limb path with no remainder.
131
+ * `width === 32` keeps its own straight-line four-limb form (shared with {@link writeField}) since it is
132
+ * the `Fr`/`Fq`/Chonk-proof hot path and the unrolled body is marginally faster than the loop.
133
+ *
134
+ * @param value - The non-negative bigint to serialize.
135
+ * @param width - Output width in bytes.
136
+ */
137
+ public writeBigInt(value: bigint, width = 32): void {
138
+ if (value < 0n) {
139
+ throw new Error(`Cannot serialize negative bigint ${value}`);
140
+ }
141
+ this.ensure(width);
142
+ const o = this.offset;
143
+ if (width === 32) {
144
+ let x = value;
145
+ this.view.setBigUint64(o + 24, x & MASK_64, false);
146
+ x >>= 64n;
147
+ this.view.setBigUint64(o + 16, x & MASK_64, false);
148
+ x >>= 64n;
149
+ this.view.setBigUint64(o + 8, x & MASK_64, false);
150
+ x >>= 64n;
151
+ this.view.setBigUint64(o, x & MASK_64, false);
152
+ x >>= 64n;
153
+ if (x !== 0n) {
154
+ throw new Error(`BigInt ${value} does not fit into 32 bytes`);
155
+ }
156
+ this.offset += 32;
157
+ return;
158
+ }
159
+ let x = value;
160
+ let pos = o + width;
161
+ while (pos - 8 >= o) {
162
+ pos -= 8;
163
+ this.view.setBigUint64(pos, x & MASK_64, false);
164
+ x >>= 64n;
165
+ }
166
+ for (let i = pos - 1; i >= o; i--) {
167
+ this.buffer[i] = Number(x & 0xffn);
168
+ x >>= 8n;
169
+ }
170
+ if (x !== 0n) {
171
+ throw new Error(`BigInt ${value} does not fit into ${width} bytes`);
172
+ }
173
+ this.offset += width;
174
+ }
175
+
176
+ /**
177
+ * Append a 32-byte (256-bit) big-endian non-negative bigint. Specialized form of {@link writeBigInt}
178
+ * with no width parameter and no width-branch, so V8 can inline the four `setBigUint64` limb writes
179
+ * without the wider branchy form's instability. This is the Fr/Fq leaf hot path; prefer it over
180
+ * `writeBigInt(value, 32)` in callers that always serialize 32-byte fields.
181
+ */
182
+ public writeField(value: bigint): void {
183
+ if (value < 0n) {
184
+ throw new Error(`Cannot serialize negative bigint ${value}`);
185
+ }
186
+ this.ensure(32);
187
+ const o = this.offset;
188
+ let x = value;
189
+ this.view.setBigUint64(o + 24, x & MASK_64, false);
190
+ x >>= 64n;
191
+ this.view.setBigUint64(o + 16, x & MASK_64, false);
192
+ x >>= 64n;
193
+ this.view.setBigUint64(o + 8, x & MASK_64, false);
194
+ x >>= 64n;
195
+ this.view.setBigUint64(o, x & MASK_64, false);
196
+ if (x >> 64n !== 0n) {
197
+ throw new Error(`BigInt ${value} does not fit into 32 bytes`);
198
+ }
199
+ this.offset = o + 32;
200
+ }
201
+
202
+ /**
203
+ * Append an array of field elements (or any objects exposing `toBigInt(): bigint`) as a contiguous
204
+ * sequence of 32-byte big-endian limbs. Iterates inline — no per-element `toBuffer` dispatch and no
205
+ * per-element function-call indirection through the generic sinkable dispatcher. Use this in classes
206
+ * that hold large flat field arrays (e.g. the 1632-element ChonkProof field list).
207
+ */
208
+ public writeFields(fields: ReadonlyArray<{ toBigInt(): bigint }>): void {
209
+ this.ensure(32 * fields.length);
210
+ for (let i = 0, n = fields.length; i < n; i++) {
211
+ const value = fields[i].toBigInt();
212
+ if (value < 0n) {
213
+ throw new Error(`Cannot serialize negative bigint ${value}`);
214
+ }
215
+ const o = this.offset;
216
+ let x = value;
217
+ this.view.setBigUint64(o + 24, x & MASK_64, false);
218
+ x >>= 64n;
219
+ this.view.setBigUint64(o + 16, x & MASK_64, false);
220
+ x >>= 64n;
221
+ this.view.setBigUint64(o + 8, x & MASK_64, false);
222
+ x >>= 64n;
223
+ this.view.setBigUint64(o, x & MASK_64, false);
224
+ if (x >> 64n !== 0n) {
225
+ throw new Error(`BigInt ${value} does not fit into 32 bytes`);
226
+ }
227
+ this.offset = o + 32;
228
+ }
229
+ }
230
+
231
+ /** Append raw bytes verbatim (no length prefix), matching how a `Buffer`/`Uint8Array` is serialized inline. */
232
+ public writeBytes(bytes: Uint8Array): void {
233
+ this.ensure(bytes.length);
234
+ this.buffer.set(bytes, this.offset);
235
+ this.offset += bytes.length;
236
+ }
237
+
238
+ /** Append a string as a 4-byte big-endian length prefix followed by its UTF-8 bytes, matching the string case. */
239
+ public writeString(value: string): void {
240
+ const bytes = Buffer.from(value);
241
+ this.writeNumber(bytes.length);
242
+ this.writeBytes(bytes);
243
+ }
244
+
245
+ /**
246
+ * Reset the write cursor without releasing the backing buffer. Lets the same sink be reused across many
247
+ * serializations without reallocating — the per-call cost drops to the bytes written (no `new ArrayBuffer`).
248
+ * Any `Buffer` previously returned by {@link toBuffer} is a fresh copy and remains valid; any view from
249
+ * {@link asUint8Array} aliases the buffer and is invalidated.
250
+ */
251
+ public reset(): void {
252
+ this.offset = 0;
253
+ }
254
+
255
+ /** Copy the written region into a freshly allocated `Buffer`. The single allocation of the whole serialization. */
256
+ public toBuffer(): Buffer {
257
+ return Buffer.from(this.buffer.subarray(0, this.offset));
258
+ }
259
+
260
+ /** A zero-copy view of the written region. Valid only until the next write (which may reallocate). */
261
+ public asUint8Array(): Uint8Array {
262
+ return this.buffer.subarray(0, this.offset);
263
+ }
264
+ }
265
+
266
+ /**
267
+ * Streaming counterpart to `serializeToBufferArray`: walk a list of {@link Sinkable}s and append each to the sink.
268
+ * Dispatch matches `serializeToBufferArray` byte-for-byte. An object is serialized via `obj.toBuffer(sink)`; a
269
+ * migrated node writes into the sink and returns `undefined`, while a not-yet-migrated node ignores the argument
270
+ * and returns its `Buffer`, which we copy in. That return-value fallback is what makes the migration incremental.
271
+ *
272
+ * Dispatch is hot-pathed for the common case of objects exposing `toBuffer` (Fr/Fq and every other migrated leaf),
273
+ * and arrays recurse via an inner-array helper to avoid the per-call rest-args allocation that a spread-recurse
274
+ * would force on large flat arrays like the 1632-element ChonkProof field list.
275
+ *
276
+ * @param sink - The destination sink.
277
+ * @param objs - The values to serialize, in order.
278
+ */
279
+ export function serializeToSink(sink: BufferSink, ...objs: Sinkable[]): void {
280
+ serializeArrayToSinkInner(sink, objs);
281
+ }
282
+
283
+ function serializeArrayToSinkInner(sink: BufferSink, objs: Sinkable[]): void {
284
+ for (let i = 0, n = objs.length; i < n; i++) {
285
+ serializeOneToSink(sink, objs[i]);
286
+ }
287
+ }
288
+
289
+ function serializeOneToSink(sink: BufferSink, obj: Sinkable): void {
290
+ if (typeof obj === 'object' && obj !== null) {
291
+ // Hot path: migrated nodes (anything implementing toBuffer, including Fr/Fq via BaseField). This branch
292
+ // is taken for the vast majority of recursive serialization, so it is checked before the typeof primitives.
293
+ if ((obj as any).toBuffer !== undefined) {
294
+ const ret = (obj as { toBuffer(sink: BufferSink): Buffer | void }).toBuffer(sink);
295
+ if (ret !== undefined) {
296
+ sink.writeBytes(ret);
297
+ }
298
+ return;
299
+ }
300
+ if (Array.isArray(obj)) {
301
+ serializeArrayToSinkInner(sink, obj);
302
+ return;
303
+ }
304
+ if (Buffer.isBuffer(obj) || obj instanceof Uint8Array) {
305
+ sink.writeBytes(obj);
306
+ return;
307
+ }
308
+ throw new Error(`Cannot serialize input to sink: ${typeof obj} ${(obj as any).constructor?.name}`);
309
+ }
310
+ if (typeof obj === 'boolean') {
311
+ sink.writeBool(obj);
312
+ } else if (typeof obj === 'bigint') {
313
+ sink.writeBigInt(obj);
314
+ } else if (typeof obj === 'number') {
315
+ sink.writeNumber(obj);
316
+ } else if (typeof obj === 'string') {
317
+ sink.writeString(obj);
318
+ } else {
319
+ throw new Error(`Cannot serialize input to sink: ${typeof obj}`);
320
+ }
321
+ }
322
+
323
+ /**
324
+ * Streaming counterpart to `serializeArrayOfBufferableToVector`: write a length prefix (1 or 4 bytes) giving the
325
+ * number of serialized elements, then each element. Byte-identical to the buffer version.
326
+ *
327
+ * @param sink - The destination sink.
328
+ * @param objs - The vector elements.
329
+ * @param prefixLength - Width of the count prefix in bytes (1 or 4).
330
+ */
331
+ export function serializeArrayToSink(sink: BufferSink, objs: Sinkable[], prefixLength = 4): void {
332
+ const count = countSinkables(objs);
333
+ if (prefixLength === 1) {
334
+ sink.writeUInt8(count);
335
+ } else if (prefixLength === 4) {
336
+ sink.writeNumber(count);
337
+ } else {
338
+ throw new Error(`Unsupported prefix length. Got ${prefixLength}, expected 1 or 4`);
339
+ }
340
+ serializeArrayToSinkInner(sink, objs);
341
+ }
342
+
343
+ /** Count how many top-level buffers `serializeToBufferArray` would emit, flattening nested arrays as it does. */
344
+ function countSinkables(objs: Sinkable[]): number {
345
+ let count = 0;
346
+ for (const obj of objs) {
347
+ count += Array.isArray(obj) ? countSinkables(obj) : 1;
348
+ }
349
+ return count;
350
+ }
@@ -1,5 +1,6 @@
1
1
  export * from './free_funcs.js';
2
2
  export * from './buffer_reader.js';
3
+ export * from './buffer_sink.js';
3
4
  export * from './field_reader.js';
4
5
  export * from './types.js';
5
6
  export * from './serialize.js';
@@ -58,7 +58,7 @@ export function urlJoin(...args: string[]): string {
58
58
  end--;
59
59
  }
60
60
 
61
- if (start < end) {
61
+ if (start <= end) {
62
62
  processed.push(arg.slice(start, end + 1));
63
63
  }
64
64
  }
@@ -52,8 +52,7 @@ export function updateInlineTestData(targetFileFromRepoRoot: string, itemName: s
52
52
  /**
53
53
  * Updates the sample Prover.toml files in noir-projects/noir-protocol-circuits/crates/.
54
54
  * @remarks Requires AZTEC_GENERATE_TEST_DATA=1 to be set
55
- * To re-gen, run 'AZTEC_GENERATE_TEST_DATA=1 FAKE_PROOFS=1 yarn test:e2e full.test'
56
- * To re-gen public base only, run 'AZTEC_GENERATE_TEST_DATA=1 yarn workspace @aztec/prover-client test orchestrator_public_functions'
55
+ * To re-gen, run 'AZTEC_GENERATE_TEST_DATA=1 FAKE_PROOFS=1 yarn test:e2e e2e_prover/full.test'
57
56
  */
58
57
  export function updateProtocolCircuitSampleInputs(circuitName: string, value: string) {
59
58
  const logger = createConsoleLogger('aztec:testing:test_data');
@@ -1,4 +1,4 @@
1
1
  export * from './date.js';
2
2
  export { elapsed, elapsedSync } from './elapsed.js';
3
- export { TimeoutTask, executeTimeout, timeoutPromise } from './timeout.js';
3
+ export { TimeoutTask, execWithSignal, executeTimeout, timeoutPromise } from './timeout.js';
4
4
  export { Timer } from './timer.js';
@@ -92,6 +92,37 @@ export async function executeTimeout<T>(
92
92
  return await task.exec();
93
93
  }
94
94
 
95
+ function defaultSignalError(signal: AbortSignal) {
96
+ if (signal.reason instanceof Error) {
97
+ return signal.reason;
98
+ }
99
+
100
+ return new Error(signal.reason ? String(signal.reason) : 'Operation aborted');
101
+ }
102
+
103
+ /** Executes a function until it completes or the given signal aborts. */
104
+ export async function execWithSignal<T>(
105
+ fn: (signal: AbortSignal) => Promise<T>,
106
+ signal: AbortSignal,
107
+ errorFn: (signal: AbortSignal) => Error = defaultSignalError,
108
+ ) {
109
+ if (signal.aborted) {
110
+ throw errorFn(signal);
111
+ }
112
+
113
+ const abortPromise = promiseWithResolvers<never>();
114
+ const abortListener = () => abortPromise.reject(errorFn(signal));
115
+ signal.addEventListener('abort', abortListener, { once: true });
116
+
117
+ try {
118
+ return await Promise.race<T>([fn(signal), abortPromise.promise]);
119
+ } finally {
120
+ if (abortListener) {
121
+ signal.removeEventListener('abort', abortListener);
122
+ }
123
+ }
124
+ }
125
+
95
126
  /** Returns a promise that rejects after the given timeout */
96
127
  export function timeoutPromise(timeoutMs: number, errorMessage?: string) {
97
128
  const promise = promiseWithResolvers<never>();
@@ -2,7 +2,17 @@ import { EventEmitter } from 'events';
2
2
 
3
3
  import { type TransferDescriptor, isTransferDescriptor } from '../interface/transferable.js';
4
4
  import type { TransportClient } from '../transport_client.js';
5
- import type { DispatchMsg } from './create_dispatch_fn.js';
5
+
6
+ /**
7
+ * Represents a message object for dispatching function calls.
8
+ * Contains the function name ('fn') and an array of arguments ('args') required to call the target method.
9
+ */
10
+ export interface DispatchMsg {
11
+ /** Name of the target method to be called. */
12
+ fn: string;
13
+ /** An array of arguments to be passed to the target method. */
14
+ args: any[];
15
+ }
6
16
 
7
17
  /**
8
18
  * FilterOutAttributes type filters out all non-method properties of an object, leaving only the attributes
@@ -1,4 +1,3 @@
1
- export * from './dispatch/create_dispatch_fn.js';
2
1
  export * from './dispatch/create_dispatch_proxy.js';
3
2
  export * from './dispatch/messages.js';
4
3
  export * from './interface/connector.js';
@@ -1,10 +1,7 @@
1
- import { poseidonMerkleHash, shaMerkleHash } from './hasher.js';
1
+ import { shaMerkleHash } from './hasher.js';
2
2
 
3
3
  export const computeBalancedShaRoot = (leaves: Buffer[]) => computeBalancedMerkleTreeRoot(leaves);
4
4
 
5
- export const computeBalancedPoseidonRoot = async (leaves: Buffer[]) =>
6
- await computeBalancedMerkleTreeRootAsync(leaves, poseidonMerkleHash);
7
-
8
5
  /**
9
6
  * Computes the Merkle root with the provided leaves **synchronously**.
10
7
  * This method uses a synchronous hash function (defaults to `sha256Trunc`).
@@ -36,7 +33,7 @@ export function computeBalancedMerkleTreeRoot(leaves: Buffer[], hasher = shaMerk
36
33
  */
37
34
  export async function computeBalancedMerkleTreeRootAsync(
38
35
  leaves: Buffer[],
39
- hasher = poseidonMerkleHash,
36
+ hasher: (left: Buffer, right: Buffer) => Promise<Buffer<ArrayBuffer>>,
40
37
  ): Promise<Buffer> {
41
38
  const height = getTreeHeight(leaves);
42
39
  let nodes = leaves.slice();
@@ -1,4 +1,4 @@
1
- import { poseidon2Hash } from '../crypto/poseidon/index.js';
1
+ import { poseidon2HashWithSeparator } from '../crypto/poseidon/index.js';
2
2
  import { sha256Trunc } from '../crypto/sha256/index.js';
3
3
 
4
4
  /**
@@ -44,5 +44,8 @@ export interface AsyncHasher {
44
44
  export const shaMerkleHash: Hasher['hash'] = (left: Buffer, right: Buffer) =>
45
45
  sha256Trunc(Buffer.concat([left, right])) as Buffer<ArrayBuffer>;
46
46
 
47
- export const poseidonMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
48
- (await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>;
47
+ /** Creates a poseidon2 merkle hasher with the given domain separator. */
48
+ export const makePoseidonMerkleHash =
49
+ (separator: number): AsyncHasher['hash'] =>
50
+ async (left: Buffer, right: Buffer) =>
51
+ (await poseidon2HashWithSeparator([left, right], separator)).toBuffer() as Buffer<ArrayBuffer>;
@@ -40,7 +40,11 @@ export class IndexedMerkleTreeCalculator<T extends IndexedTreeLeafPreimage, N ex
40
40
  }
41
41
  const sorted = values
42
42
  .map((v, i) => ({ value: v, index: i }))
43
- .sort((a, b) => Number(toBigIntBE(b.value) - toBigIntBE(a.value)));
43
+ .sort((a, b): -1 | 0 | 1 => {
44
+ const aBigInt = toBigIntBE(a.value);
45
+ const bBigInt = toBigIntBE(b.value);
46
+ return aBigInt < bBigInt ? 1 : aBigInt > bBigInt ? -1 : 0;
47
+ });
44
48
  const indexedLeaves = sorted.map((item, i) => ({
45
49
  leaf: this.factory.fromBuffer(
46
50
  Buffer.concat([
@@ -38,14 +38,6 @@ export class MembershipWitness<N extends number> {
38
38
  return [new Fr(this.leafIndex), ...this.siblingPath];
39
39
  }
40
40
 
41
- /**
42
- * Returns a representation of the membership witness as expected by intrinsic Noir deserialization.
43
- */
44
- public toNoirRepresentation(): (string | string[])[] {
45
- // TODO(#12874): remove the stupid as string conversion by modifying ForeignCallOutput type in acvm.js
46
- return [new Fr(this.leafIndex).toString() as string, this.siblingPath.map(fr => fr.toString()) as string[]];
47
- }
48
-
49
41
  static schemaFor<N extends number>(size: N) {
50
42
  return schemas.Buffer.transform(b => MembershipWitness.fromBuffer(b, size));
51
43
  }
@@ -1,8 +1,7 @@
1
- import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';
2
-
3
- import type { AsyncHasher } from './hasher.js';
4
1
  import { MerkleTree } from './merkle_tree.js';
5
2
 
3
+ type MerkleHashFn = (left: Buffer, right: Buffer) => Promise<Buffer<ArrayBuffer>>;
4
+
6
5
  /**
7
6
  * Merkle tree calculator.
8
7
  */
@@ -10,17 +9,12 @@ export class MerkleTreeCalculator {
10
9
  private constructor(
11
10
  private height: number,
12
11
  private zeroHashes: Buffer[],
13
- private hasher: AsyncHasher['hash'],
12
+ private hasher: MerkleHashFn,
14
13
  ) {
15
14
  this.hasher = hasher;
16
15
  }
17
16
 
18
- static async create(
19
- height: number,
20
- zeroLeaf: Buffer = Buffer.alloc(32),
21
- hasher = async (left: Buffer, right: Buffer) =>
22
- (await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>,
23
- ) {
17
+ static async create(height: number, zeroLeaf: Buffer = Buffer.alloc(32), hasher: MerkleHashFn) {
24
18
  const zeroHashes = [zeroLeaf];
25
19
  for (let i = 0; i < height; i++) {
26
20
  zeroHashes.push((await hasher(zeroHashes[i], zeroHashes[i])) as Buffer<ArrayBuffer>);
@@ -1,5 +1,6 @@
1
+ import { z } from 'zod';
2
+
1
3
  import { makeTuple } from '../array/array.js';
2
- import { poseidon2Hash } from '../crypto/poseidon/index.js';
3
4
  import { Fr } from '../curves/bn254/index.js';
4
5
  import { schemas } from '../schemas/index.js';
5
6
  import {
@@ -43,10 +44,14 @@ export class SiblingPath<N extends number> {
43
44
  }
44
45
 
45
46
  static schemaFor<N extends number>(size: N) {
46
- return schemas.Buffer.transform(b => SiblingPath.fromBuffer(b) as SiblingPath<N>).refine(
47
- path => path.pathSize === size,
48
- path => ({ message: `Expected sibling path size ${size} but got ${path.pathSize}` }),
49
- );
47
+ return schemas.Buffer.transform(b => SiblingPath.fromBuffer(b) as SiblingPath<N>).superRefine((path, ctx) => {
48
+ if (path.pathSize !== size) {
49
+ ctx.addIssue({
50
+ code: z.ZodIssueCode.custom,
51
+ message: `Expected sibling path size ${size} but got ${path.pathSize}`,
52
+ });
53
+ }
54
+ });
50
55
  }
51
56
 
52
57
  toJSON() {
@@ -172,7 +177,7 @@ export async function computeRootFromSiblingPath(
172
177
  leaf: Buffer,
173
178
  siblingPath: Buffer[],
174
179
  index: number,
175
- hasher = async (left: Buffer, right: Buffer) => (await poseidon2Hash([left, right])).toBuffer(),
180
+ hasher: (left: Buffer, right: Buffer) => Promise<Buffer>,
176
181
  ) {
177
182
  let result = leaf;
178
183
  for (const sibling of siblingPath) {
@@ -1,12 +1,9 @@
1
1
  import { computeBalancedMerkleTreeRoot, computeBalancedMerkleTreeRootAsync } from './balanced_merkle_tree_root.js';
2
- import { poseidonMerkleHash, shaMerkleHash } from './hasher.js';
2
+ import { shaMerkleHash } from './hasher.js';
3
3
  import { UnbalancedMerkleTreeCalculator } from './unbalanced_merkle_tree_calculator.js';
4
4
 
5
5
  export const computeUnbalancedShaRoot = (leaves: Buffer[]) => computeUnbalancedMerkleTreeRoot(leaves, shaMerkleHash);
6
6
 
7
- export const computeUnbalancedPoseidonRoot = async (leaves: Buffer[]) =>
8
- await computeUnbalancedMerkleTreeRootAsync(leaves, poseidonMerkleHash);
9
-
10
7
  export const computeWonkyShaRoot = (leaves: Buffer[]) => computeWonkyMerkleTreeRoot(leaves);
11
8
 
12
9
  /**
@@ -67,7 +64,7 @@ export function computeUnbalancedMerkleTreeRoot(
67
64
 
68
65
  export async function computeUnbalancedMerkleTreeRootAsync(
69
66
  leaves: Buffer[],
70
- hasher = poseidonMerkleHash,
67
+ hasher: (left: Uint8Array, right: Uint8Array) => Promise<Buffer<ArrayBuffer>>,
71
68
  emptyRoot = Buffer.alloc(32),
72
69
  ): Promise<Buffer> {
73
70
  if (!leaves.length) {
@@ -29,11 +29,58 @@ export function isErrorClass<T extends Error>(value: unknown, errorClass: new (.
29
29
  return value instanceof errorClass || (value instanceof Error && value.name === errorClass.name);
30
30
  }
31
31
 
32
+ const MAX_ERR_DEPTH = 10;
33
+
34
+ /** Returns the first error in the cause chain matching the given error class. */
35
+ export function getErrorCause<T extends Error>(err: unknown, errorClass: new (...args: any[]) => T): T | undefined {
36
+ let current = err;
37
+ for (let i = 0; current !== undefined && current !== null && i < MAX_ERR_DEPTH; i++) {
38
+ if (isErrorClass(current, errorClass)) {
39
+ return current;
40
+ }
41
+
42
+ if (typeof current === 'object' && Object.hasOwn(current, 'cause')) {
43
+ current = (current as { cause: unknown }).cause;
44
+ } else {
45
+ return undefined;
46
+ }
47
+ }
48
+ return undefined;
49
+ }
50
+
32
51
  /** Resolves a record-like type. Lifted from viem. */
33
52
  export type Prettify<T> = {
34
53
  [K in keyof T]: T[K];
35
54
  } & {};
36
55
 
56
+ /** Returns a type T based on a flag: T if true, undefined if false, optional otherwise. */
57
+ export type DefineIfFlag<Opts, Key extends keyof Opts, T> = Opts extends {
58
+ [K in Key]: true;
59
+ }
60
+ ? T
61
+ : Opts extends { [K in Key]: false }
62
+ ? never
63
+ : Opts extends { [K in Key]?: boolean }
64
+ ? T | undefined
65
+ : never;
66
+
67
+ /** Returns a type with fields conditionally required based on a flag */
68
+ export type PickIfFlag<
69
+ OptsSchema,
70
+ Opts extends OptsSchema,
71
+ Key extends keyof OptsSchema,
72
+ Field extends object,
73
+ > = Opts extends { [K in Key]: true }
74
+ ? Field
75
+ : Opts extends { [K in Key]: false }
76
+ ? {}
77
+ : Opts extends { [K in Key]?: boolean }
78
+ ? Partial<Field>
79
+ : {};
80
+
81
+ /** Picks only the defined (non-undefined) properties of a type. */
82
+ export type PickDefined<T> = Prettify<Pick<T, { [K in keyof T]: T[K] extends undefined ? never : K }[keyof T]>>;
83
+
37
84
  /**
38
85
  * Type-safe Event Emitter type
39
86
  * @example