@aztec/foundation 0.0.1-commit.0208eb9 → 0.0.1-commit.031e54b

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 (307) hide show
  1. package/dest/array/sorted_array.d.ts +6 -1
  2. package/dest/array/sorted_array.d.ts.map +1 -1
  3. package/dest/array/sorted_array.js +18 -15
  4. package/dest/bigint-buffer/index.d.ts +1 -1
  5. package/dest/bigint-buffer/index.d.ts.map +1 -1
  6. package/dest/bigint-buffer/index.js +6 -1
  7. package/dest/branded-types/block_number.d.ts +3 -3
  8. package/dest/branded-types/block_number.d.ts.map +1 -1
  9. package/dest/branded-types/block_number.js +15 -4
  10. package/dest/branded-types/buffer32_hash.d.ts +27 -0
  11. package/dest/branded-types/buffer32_hash.d.ts.map +1 -0
  12. package/dest/branded-types/buffer32_hash.js +12 -0
  13. package/dest/branded-types/checkpoint_number.d.ts +9 -3
  14. package/dest/branded-types/checkpoint_number.d.ts.map +1 -1
  15. package/dest/branded-types/checkpoint_number.js +11 -0
  16. package/dest/branded-types/epoch.d.ts +2 -2
  17. package/dest/branded-types/epoch.d.ts.map +1 -1
  18. package/dest/branded-types/index.d.ts +2 -1
  19. package/dest/branded-types/index.d.ts.map +1 -1
  20. package/dest/branded-types/index.js +1 -0
  21. package/dest/branded-types/index_within_checkpoint.d.ts +2 -2
  22. package/dest/branded-types/index_within_checkpoint.d.ts.map +1 -1
  23. package/dest/branded-types/slot.d.ts +5 -2
  24. package/dest/branded-types/slot.d.ts.map +1 -1
  25. package/dest/branded-types/slot.js +3 -0
  26. package/dest/buffer/buffer16.d.ts +3 -1
  27. package/dest/buffer/buffer16.d.ts.map +1 -1
  28. package/dest/buffer/buffer32.d.ts +28 -61
  29. package/dest/buffer/buffer32.d.ts.map +1 -1
  30. package/dest/buffer/buffer32.js +29 -63
  31. package/dest/buffer/index.d.ts +2 -1
  32. package/dest/buffer/index.d.ts.map +1 -1
  33. package/dest/buffer/index.js +1 -0
  34. package/dest/buffer/utils.d.ts +3 -0
  35. package/dest/buffer/utils.d.ts.map +1 -0
  36. package/dest/buffer/utils.js +7 -0
  37. package/dest/collection/array.d.ts +11 -1
  38. package/dest/collection/array.d.ts.map +1 -1
  39. package/dest/collection/array.js +33 -0
  40. package/dest/collection/index.d.ts +3 -1
  41. package/dest/collection/index.d.ts.map +1 -1
  42. package/dest/collection/index.js +2 -0
  43. package/dest/collection/lru_map.d.ts +41 -0
  44. package/dest/collection/lru_map.d.ts.map +1 -0
  45. package/dest/collection/lru_map.js +119 -0
  46. package/dest/collection/lru_set.d.ts +35 -0
  47. package/dest/collection/lru_set.d.ts.map +1 -0
  48. package/dest/collection/lru_set.js +95 -0
  49. package/dest/committable/committable.js +1 -1
  50. package/dest/config/env_var.d.ts +2 -2
  51. package/dest/config/env_var.d.ts.map +1 -1
  52. package/dest/config/index.d.ts +47 -30
  53. package/dest/config/index.d.ts.map +1 -1
  54. package/dest/config/index.js +44 -44
  55. package/dest/config/network_config.d.ts +15 -41
  56. package/dest/config/network_config.d.ts.map +1 -1
  57. package/dest/config/network_config.js +4 -1
  58. package/dest/config/network_name.d.ts +2 -2
  59. package/dest/config/network_name.d.ts.map +1 -1
  60. package/dest/config/network_name.js +3 -3
  61. package/dest/config/secret_value.d.ts +2 -2
  62. package/dest/config/secret_value.d.ts.map +1 -1
  63. package/dest/crypto/aes128/index.d.ts +2 -1
  64. package/dest/crypto/aes128/index.d.ts.map +1 -1
  65. package/dest/crypto/aes128/index.js +11 -2
  66. package/dest/crypto/bls/bn254_keystore.d.ts +9 -176
  67. package/dest/crypto/bls/bn254_keystore.d.ts.map +1 -1
  68. package/dest/crypto/bls/bn254_keystore.js +0 -17
  69. package/dest/crypto/poseidon/index.d.ts +1 -1
  70. package/dest/crypto/poseidon/index.d.ts.map +1 -1
  71. package/dest/crypto/poseidon/index.js +40 -33
  72. package/dest/crypto/schnorr/index.d.ts +8 -7
  73. package/dest/crypto/schnorr/index.d.ts.map +1 -1
  74. package/dest/crypto/schnorr/index.js +6 -6
  75. package/dest/crypto/schnorr/signature.d.ts +9 -29
  76. package/dest/crypto/schnorr/signature.d.ts.map +1 -1
  77. package/dest/crypto/schnorr/signature.js +20 -36
  78. package/dest/crypto/secp256k1-signer/utils.d.ts +12 -1
  79. package/dest/crypto/secp256k1-signer/utils.d.ts.map +1 -1
  80. package/dest/crypto/secp256k1-signer/utils.js +26 -0
  81. package/dest/curves/bls12/field.d.ts +3 -3
  82. package/dest/curves/bls12/field.d.ts.map +1 -1
  83. package/dest/curves/bls12/field.js +0 -5
  84. package/dest/curves/bls12/point.d.ts +2 -2
  85. package/dest/curves/bls12/point.d.ts.map +1 -1
  86. package/dest/curves/bn254/field.d.ts +24 -19
  87. package/dest/curves/bn254/field.d.ts.map +1 -1
  88. package/dest/curves/bn254/field.js +28 -19
  89. package/dest/curves/grumpkin/point.d.ts +14 -28
  90. package/dest/curves/grumpkin/point.d.ts.map +1 -1
  91. package/dest/curves/grumpkin/point.js +29 -43
  92. package/dest/eth-address/index.d.ts +2 -2
  93. package/dest/eth-address/index.d.ts.map +1 -1
  94. package/dest/eth-address/index.js +0 -3
  95. package/dest/eth-signature/eth_signature.d.ts +3 -2
  96. package/dest/eth-signature/eth_signature.d.ts.map +1 -1
  97. package/dest/eth-signature/eth_signature.js +7 -2
  98. package/dest/fifo/fifo_frame_reader.d.ts +41 -0
  99. package/dest/fifo/fifo_frame_reader.d.ts.map +1 -0
  100. package/dest/fifo/fifo_frame_reader.js +91 -0
  101. package/dest/fifo/index.d.ts +2 -0
  102. package/dest/fifo/index.d.ts.map +1 -0
  103. package/dest/fifo/index.js +1 -0
  104. package/dest/fifo_set/fifo_set.d.ts +15 -0
  105. package/dest/fifo_set/fifo_set.d.ts.map +1 -0
  106. package/dest/fifo_set/fifo_set.js +39 -0
  107. package/dest/fifo_set/index.d.ts +2 -0
  108. package/dest/fifo_set/index.d.ts.map +1 -0
  109. package/dest/fifo_set/index.js +1 -0
  110. package/dest/jest/setup.js +24 -0
  111. package/dest/json-rpc/client/fetch.d.ts +1 -1
  112. package/dest/json-rpc/client/fetch.d.ts.map +1 -1
  113. package/dest/json-rpc/client/fetch.js +4 -3
  114. package/dest/json-rpc/client/safe_json_rpc_client.d.ts +2 -1
  115. package/dest/json-rpc/client/safe_json_rpc_client.d.ts.map +1 -1
  116. package/dest/json-rpc/client/safe_json_rpc_client.js +3 -3
  117. package/dest/json-rpc/client/undici.d.ts +1 -1
  118. package/dest/json-rpc/client/undici.d.ts.map +1 -1
  119. package/dest/json-rpc/client/undici.js +5 -3
  120. package/dest/json-rpc/fixtures/test_state.d.ts +4 -8
  121. package/dest/json-rpc/fixtures/test_state.d.ts.map +1 -1
  122. package/dest/json-rpc/fixtures/test_state.js +57 -17
  123. package/dest/json-rpc/server/api_key_auth.d.ts +19 -0
  124. package/dest/json-rpc/server/api_key_auth.d.ts.map +1 -0
  125. package/dest/json-rpc/server/api_key_auth.js +57 -0
  126. package/dest/json-rpc/server/index.d.ts +2 -1
  127. package/dest/json-rpc/server/index.d.ts.map +1 -1
  128. package/dest/json-rpc/server/index.js +1 -0
  129. package/dest/json-rpc/server/safe_json_rpc_server.d.ts +8 -3
  130. package/dest/json-rpc/server/safe_json_rpc_server.d.ts.map +1 -1
  131. package/dest/json-rpc/server/safe_json_rpc_server.js +27 -13
  132. package/dest/log/bigint-utils.d.ts +5 -0
  133. package/dest/log/bigint-utils.d.ts.map +1 -0
  134. package/dest/log/bigint-utils.js +21 -0
  135. package/dest/log/gcloud-logger-config.d.ts +1 -1
  136. package/dest/log/gcloud-logger-config.d.ts.map +1 -1
  137. package/dest/log/gcloud-logger-config.js +3 -0
  138. package/dest/log/log-filters.d.ts +17 -4
  139. package/dest/log/log-filters.d.ts.map +1 -1
  140. package/dest/log/log-filters.js +26 -12
  141. package/dest/log/pino-logger.d.ts +1 -1
  142. package/dest/log/pino-logger.d.ts.map +1 -1
  143. package/dest/log/pino-logger.js +23 -4
  144. package/dest/noir/noir_package_config.d.ts +19 -104
  145. package/dest/noir/noir_package_config.d.ts.map +1 -1
  146. package/dest/noir/noir_package_config.js +1 -1
  147. package/dest/queue/base_memory_queue.d.ts +2 -2
  148. package/dest/queue/base_memory_queue.d.ts.map +1 -1
  149. package/dest/queue/batch_queue.d.ts +1 -1
  150. package/dest/queue/batch_queue.d.ts.map +1 -1
  151. package/dest/queue/batch_queue.js +1 -0
  152. package/dest/retry/index.d.ts +12 -1
  153. package/dest/retry/index.d.ts.map +1 -1
  154. package/dest/retry/index.js +21 -0
  155. package/dest/schemas/api.d.ts +12 -10
  156. package/dest/schemas/api.d.ts.map +1 -1
  157. package/dest/schemas/api.js +7 -1
  158. package/dest/schemas/parse.d.ts +4 -4
  159. package/dest/schemas/parse.d.ts.map +1 -1
  160. package/dest/schemas/parse.js +6 -5
  161. package/dest/schemas/schemas.d.ts +15 -15
  162. package/dest/schemas/types.d.ts +3 -3
  163. package/dest/schemas/types.d.ts.map +1 -1
  164. package/dest/schemas/utils.d.ts +7 -11
  165. package/dest/schemas/utils.d.ts.map +1 -1
  166. package/dest/schemas/utils.js +2 -18
  167. package/dest/serialize/buffer_reader.d.ts +8 -1
  168. package/dest/serialize/buffer_reader.d.ts.map +1 -1
  169. package/dest/serialize/buffer_reader.js +13 -0
  170. package/dest/serialize/buffer_sink.d.ts +134 -0
  171. package/dest/serialize/buffer_sink.d.ts.map +1 -0
  172. package/dest/serialize/buffer_sink.js +297 -0
  173. package/dest/serialize/index.d.ts +2 -1
  174. package/dest/serialize/index.d.ts.map +1 -1
  175. package/dest/serialize/index.js +1 -0
  176. package/dest/serialize/serialize.d.ts +19 -1
  177. package/dest/serialize/serialize.d.ts.map +1 -1
  178. package/dest/serialize/serialize.js +31 -0
  179. package/dest/sleep/index.d.ts +2 -1
  180. package/dest/sleep/index.d.ts.map +1 -1
  181. package/dest/sleep/index.js +10 -1
  182. package/dest/string/index.js +1 -1
  183. package/dest/timer/date.d.ts +25 -1
  184. package/dest/timer/date.d.ts.map +1 -1
  185. package/dest/timer/date.js +33 -0
  186. package/dest/timer/index.d.ts +2 -2
  187. package/dest/timer/index.d.ts.map +1 -1
  188. package/dest/timer/index.js +1 -1
  189. package/dest/timer/timeout.d.ts +3 -1
  190. package/dest/timer/timeout.d.ts.map +1 -1
  191. package/dest/timer/timeout.js +26 -0
  192. package/dest/transport/dispatch/create_dispatch_proxy.d.ts +11 -2
  193. package/dest/transport/dispatch/create_dispatch_proxy.d.ts.map +1 -1
  194. package/dest/transport/index.d.ts +1 -2
  195. package/dest/transport/index.d.ts.map +1 -1
  196. package/dest/transport/index.js +0 -1
  197. package/dest/transport/transport_client.js +2 -2
  198. package/dest/trees/balanced_merkle_tree_root.d.ts +2 -3
  199. package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -1
  200. package/dest/trees/balanced_merkle_tree_root.js +2 -3
  201. package/dest/trees/hasher.d.ts +3 -2
  202. package/dest/trees/hasher.d.ts.map +1 -1
  203. package/dest/trees/hasher.js +5 -5
  204. package/dest/trees/indexed_merkle_tree_calculator.d.ts +1 -1
  205. package/dest/trees/indexed_merkle_tree_calculator.d.ts.map +1 -1
  206. package/dest/trees/indexed_merkle_tree_calculator.js +5 -1
  207. package/dest/trees/membership_witness.d.ts +2 -2
  208. package/dest/trees/membership_witness.d.ts.map +1 -1
  209. package/dest/trees/merkle_tree_calculator.d.ts +4 -2
  210. package/dest/trees/merkle_tree_calculator.d.ts.map +1 -1
  211. package/dest/trees/merkle_tree_calculator.js +1 -5
  212. package/dest/trees/sibling_path.d.ts +5 -4
  213. package/dest/trees/sibling_path.d.ts.map +1 -1
  214. package/dest/trees/sibling_path.js +10 -8
  215. package/dest/trees/unbalanced_merkle_tree_root.d.ts +2 -3
  216. package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -1
  217. package/dest/trees/unbalanced_merkle_tree_root.js +2 -3
  218. package/dest/types/index.d.ts +23 -1
  219. package/dest/types/index.d.ts.map +1 -1
  220. package/dest/types/index.js +15 -0
  221. package/package.json +26 -4
  222. package/src/array/sorted_array.ts +22 -17
  223. package/src/bigint-buffer/index.ts +6 -1
  224. package/src/branded-types/block_number.ts +20 -2
  225. package/src/branded-types/buffer32_hash.ts +38 -0
  226. package/src/branded-types/checkpoint_number.ts +16 -1
  227. package/src/branded-types/epoch.ts +1 -1
  228. package/src/branded-types/index.ts +1 -0
  229. package/src/branded-types/index_within_checkpoint.ts +1 -1
  230. package/src/branded-types/slot.ts +6 -1
  231. package/src/buffer/buffer16.ts +3 -0
  232. package/src/buffer/buffer32.ts +39 -68
  233. package/src/buffer/index.ts +1 -0
  234. package/src/buffer/utils.ts +8 -0
  235. package/src/collection/array.ts +34 -0
  236. package/src/collection/index.ts +2 -0
  237. package/src/collection/lru_map.ts +143 -0
  238. package/src/collection/lru_set.ts +115 -0
  239. package/src/committable/committable.ts +1 -1
  240. package/src/config/env_var.ts +62 -23
  241. package/src/config/index.ts +116 -96
  242. package/src/config/network_config.ts +3 -0
  243. package/src/config/network_name.ts +6 -6
  244. package/src/config/secret_value.ts +1 -1
  245. package/src/crypto/aes128/index.ts +11 -2
  246. package/src/crypto/bls/bn254_keystore.ts +0 -23
  247. package/src/crypto/poseidon/index.ts +42 -34
  248. package/src/crypto/schnorr/index.ts +9 -8
  249. package/src/crypto/schnorr/signature.ts +17 -46
  250. package/src/crypto/secp256k1-signer/utils.ts +32 -0
  251. package/src/curves/bls12/field.ts +0 -7
  252. package/src/curves/bn254/field.ts +41 -36
  253. package/src/curves/grumpkin/point.ts +23 -40
  254. package/src/eth-address/index.ts +0 -4
  255. package/src/eth-signature/eth_signature.ts +7 -1
  256. package/src/fifo/fifo_frame_reader.ts +113 -0
  257. package/src/fifo/index.ts +1 -0
  258. package/src/fifo_set/fifo_set.ts +52 -0
  259. package/src/fifo_set/index.ts +1 -0
  260. package/src/jest/setup.mjs +27 -0
  261. package/src/json-rpc/client/fetch.ts +4 -3
  262. package/src/json-rpc/client/safe_json_rpc_client.ts +4 -2
  263. package/src/json-rpc/client/undici.ts +5 -3
  264. package/src/json-rpc/fixtures/test_state.ts +10 -10
  265. package/src/json-rpc/server/api_key_auth.ts +63 -0
  266. package/src/json-rpc/server/index.ts +1 -0
  267. package/src/json-rpc/server/safe_json_rpc_server.ts +31 -13
  268. package/src/log/bigint-utils.ts +25 -0
  269. package/src/log/gcloud-logger-config.ts +5 -0
  270. package/src/log/log-filters.ts +29 -11
  271. package/src/log/pino-logger.ts +25 -4
  272. package/src/noir/noir_package_config.ts +32 -20
  273. package/src/queue/base_memory_queue.ts +1 -1
  274. package/src/queue/batch_queue.ts +1 -0
  275. package/src/retry/index.ts +30 -0
  276. package/src/schemas/api.ts +25 -25
  277. package/src/schemas/parse.ts +9 -6
  278. package/src/schemas/schemas.ts +4 -4
  279. package/src/schemas/types.ts +2 -2
  280. package/src/schemas/utils.ts +4 -38
  281. package/src/serialize/buffer_reader.ts +15 -0
  282. package/src/serialize/buffer_sink.ts +350 -0
  283. package/src/serialize/index.ts +1 -0
  284. package/src/serialize/serialize.ts +32 -0
  285. package/src/sleep/index.ts +10 -1
  286. package/src/string/index.ts +1 -1
  287. package/src/timer/date.ts +48 -0
  288. package/src/timer/index.ts +1 -1
  289. package/src/timer/timeout.ts +31 -0
  290. package/src/transport/dispatch/create_dispatch_proxy.ts +11 -1
  291. package/src/transport/index.ts +0 -1
  292. package/src/transport/transport_client.ts +2 -2
  293. package/src/trees/balanced_merkle_tree_root.ts +2 -5
  294. package/src/trees/hasher.ts +6 -3
  295. package/src/trees/indexed_merkle_tree_calculator.ts +5 -1
  296. package/src/trees/merkle_tree_calculator.ts +4 -10
  297. package/src/trees/sibling_path.ts +11 -6
  298. package/src/trees/unbalanced_merkle_tree_root.ts +2 -5
  299. package/src/types/index.ts +47 -0
  300. package/dest/crypto/serialize.d.ts +0 -51
  301. package/dest/crypto/serialize.d.ts.map +0 -1
  302. package/dest/crypto/serialize.js +0 -68
  303. package/dest/transport/dispatch/create_dispatch_fn.d.ts +0 -25
  304. package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +0 -1
  305. package/dest/transport/dispatch/create_dispatch_fn.js +0 -17
  306. package/src/crypto/serialize.ts +0 -85
  307. package/src/transport/dispatch/create_dispatch_fn.ts +0 -35
@@ -1,6 +1,5 @@
1
- import { randomBytes } from '@aztec/foundation/crypto/random';
2
1
  import { Fr } from '@aztec/foundation/curves/bn254';
3
- import { BufferReader, mapTuple } from '@aztec/foundation/serialize';
2
+ import { mapTuple } from '@aztec/foundation/serialize';
4
3
 
5
4
  import type { Signature } from '../signature/index.js';
6
5
 
@@ -14,46 +13,12 @@ export class SchnorrSignature implements Signature {
14
13
  */
15
14
  public static SIZE = 64;
16
15
 
17
- /**
18
- * An empty signature.
19
- */
20
- public static EMPTY = new SchnorrSignature(Buffer.alloc(64));
21
-
22
16
  constructor(private buffer: Buffer) {
23
17
  if (buffer.length !== SchnorrSignature.SIZE) {
24
18
  throw new Error(`Invalid signature buffer of length ${buffer.length}.`);
25
19
  }
26
20
  }
27
21
 
28
- /**
29
- * Determines if the provided signature is valid or not.
30
- * @param signature - The data to be checked.
31
- * @returns Boolean indicating if the provided data is a valid schnorr signature.
32
- */
33
- public static isSignature(signature: string) {
34
- return /^(0x)?[0-9a-f]{128}$/i.test(signature);
35
- }
36
-
37
- /**
38
- * Constructs a SchnorrSignature from the provided string.
39
- * @param signature - The string to be converted to a schnorr signature.
40
- * @returns The constructed schnorr signature.
41
- */
42
- public static fromString(signature: string) {
43
- if (!SchnorrSignature.isSignature(signature)) {
44
- throw new Error(`Invalid signature string: ${signature}`);
45
- }
46
- return new SchnorrSignature(Buffer.from(signature.replace(/^0x/i, ''), 'hex'));
47
- }
48
-
49
- /**
50
- * Generates a random schnorr signature.
51
- * @returns The randomly constructed signature.
52
- */
53
- public static random() {
54
- return new SchnorrSignature(randomBytes(64));
55
- }
56
-
57
22
  /**
58
23
  * Returns the 's' component of the signature.
59
24
  * @returns A buffer containing the signature's 's' component.
@@ -78,16 +43,6 @@ export class SchnorrSignature implements Signature {
78
43
  return this.buffer;
79
44
  }
80
45
 
81
- /**
82
- * Deserializes from a buffer.
83
- * @param buffer - The buffer representation of the object.
84
- * @returns The new object.
85
- */
86
- static fromBuffer(buffer: Buffer | BufferReader): SchnorrSignature {
87
- const reader = BufferReader.asReader(buffer);
88
- return new SchnorrSignature(reader.readBytes(SchnorrSignature.SIZE));
89
- }
90
-
91
46
  /**
92
47
  * Returns the full signature as a hex string.
93
48
  * @returns A string containing the signature in hex format.
@@ -113,4 +68,20 @@ export class SchnorrSignature implements Signature {
113
68
 
114
69
  return mapTuple([buf1, buf2, buf3], Fr.fromBuffer);
115
70
  }
71
+
72
+ /**
73
+ * Splits the signature into the four 128-bit limbs that Noir's `EmbeddedCurveScalar` consumes:
74
+ * `[s.lo, s.hi, e.lo, e.hi]`, where each component scalar is encoded as `lo + hi * 2^128`.
75
+ *
76
+ * Each 32-byte big-endian component is sliced into its top 16 bytes (`hi`) and bottom 16 bytes
77
+ * (`lo`); each half is zero-padded into a 32-byte buffer and decoded as `Fr` (big-endian).
78
+ */
79
+ toLimbFields(): [Fr, Fr, Fr, Fr] {
80
+ const limb = (start: number) => {
81
+ const buf = Buffer.alloc(32);
82
+ this.buffer.copy(buf, 16, start, start + 16);
83
+ return Fr.fromBuffer(buf);
84
+ };
85
+ return [limb(16), limb(0), limb(48), limb(32)];
86
+ }
116
87
  }
@@ -210,3 +210,35 @@ export function recoverPublicKey(hash: Buffer32, signature: Signature, opts: Rec
210
210
  const publicKey = sig.recoverPublicKey(hash.buffer).toHex(false);
211
211
  return Buffer.from(publicKey, 'hex');
212
212
  }
213
+
214
+ /** Arbitrary hash used for testing signature recoverability. */
215
+ const PROBE_HASH = Buffer32.fromBuffer(keccak256(Buffer.from('signature-recoverability-probe')));
216
+
217
+ /**
218
+ * Generates a random valid ECDSA signature that is recoverable to some address.
219
+ * Since Signature.random() produces real signatures via secp256k1 signing, the result is always
220
+ * recoverable, but we verify defensively by checking tryRecoverAddress.
221
+ */
222
+ export function generateRecoverableSignature(): Signature {
223
+ for (let i = 0; i < 100; i++) {
224
+ const sig = Signature.random();
225
+ if (tryRecoverAddress(PROBE_HASH, sig) !== undefined) {
226
+ return sig;
227
+ }
228
+ }
229
+ throw new Secp256k1Error('Failed to generate a recoverable signature after 100 attempts');
230
+ }
231
+
232
+ /**
233
+ * Generates a random signature where ECDSA address recovery fails.
234
+ * Uses random r/s values (not from real signing) so that r is unlikely to be a valid secp256k1 x-coordinate.
235
+ */
236
+ export function generateUnrecoverableSignature(): Signature {
237
+ for (let i = 0; i < 100; i++) {
238
+ const sig = new Signature(Buffer32.random(), Buffer32.random(), 27);
239
+ if (tryRecoverAddress(PROBE_HASH, sig) === undefined) {
240
+ return sig;
241
+ }
242
+ }
243
+ throw new Secp256k1Error('Failed to generate an unrecoverable signature after 100 attempts');
244
+ }
@@ -6,7 +6,6 @@ import { toBigIntBE, toBufferBE } from '../../bigint-buffer/index.js';
6
6
  import { randomBytes } from '../../crypto/random/index.js';
7
7
  import { hexSchemaFor } from '../../schemas/utils.js';
8
8
  import { BufferReader } from '../../serialize/buffer_reader.js';
9
- import { TypeRegistry } from '../../serialize/type_registry.js';
10
9
  import { Fr } from '../bn254/field.js';
11
10
 
12
11
  /**
@@ -323,9 +322,6 @@ export class BLS12Fr extends BLS12Field {
323
322
  }
324
323
  }
325
324
 
326
- // For deserializing JSON.
327
- TypeRegistry.register('BLS12Fr', BLS12Fr);
328
-
329
325
  /**
330
326
  * Fq field class.
331
327
  * @dev This class is used to represent elements of BLS12-381 base field.
@@ -458,6 +454,3 @@ export class BLS12Fq extends BLS12Field {
458
454
  return hexSchemaFor(BLS12Fq);
459
455
  }
460
456
  }
461
-
462
- // For deserializing JSON.
463
- TypeRegistry.register('BLS12Fq', BLS12Fq);
@@ -6,9 +6,7 @@ import { toBigIntBE, toBufferBE } from '../../bigint-buffer/index.js';
6
6
  import { randomBytes } from '../../crypto/random/index.js';
7
7
  import { hexSchemaFor } from '../../schemas/utils.js';
8
8
  import { BufferReader } from '../../serialize/buffer_reader.js';
9
- import { TypeRegistry } from '../../serialize/type_registry.js';
10
-
11
- /* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
9
+ import type { BufferSink } from '../../serialize/buffer_sink.js';
12
10
 
13
11
  /**
14
12
  * Represents a field derived from BaseField.
@@ -67,10 +65,16 @@ abstract class BaseField {
67
65
  protected abstract modulus(): bigint;
68
66
 
69
67
  /**
70
- * Converts the bigint to a Buffer.
68
+ * Converts the bigint to a Buffer. With a sink, streams the 32 big-endian bytes straight in (no allocation)
69
+ * and returns undefined; without one, returns a freshly allocated buffer.
71
70
  */
72
- toBuffer(): Buffer {
73
- return toBufferBE(this.asBigInt, 32);
71
+ toBuffer(): Buffer;
72
+ toBuffer(sink: BufferSink): void;
73
+ toBuffer(sink?: BufferSink): Buffer | void {
74
+ if (!sink) {
75
+ return toBufferBE(this.asBigInt, BaseField.SIZE_IN_BYTES);
76
+ }
77
+ sink.writeField(this.asBigInt);
74
78
  }
75
79
 
76
80
  toString(): `0x${string}` {
@@ -118,14 +122,18 @@ abstract class BaseField {
118
122
  }
119
123
 
120
124
  cmp(rhs: BaseField): -1 | 0 | 1 {
121
- const rhsBigInt = rhs.asBigInt;
122
- return this.asBigInt === rhsBigInt ? 0 : this.asBigInt < rhsBigInt ? -1 : 1;
125
+ return BaseField.cmpAsBigInt(this.asBigInt, rhs.asBigInt);
123
126
  }
124
127
 
125
128
  static cmp(lhs: BaseField, rhs: BaseField): -1 | 0 | 1 {
126
129
  return lhs.cmp(rhs);
127
130
  }
128
131
 
132
+ // Actual bigint comparison. Arguments must have been validated previously.
133
+ static cmpAsBigInt(lhs: bigint, rhs: bigint): -1 | 0 | 1 {
134
+ return lhs === rhs ? 0 : lhs < rhs ? -1 : 1;
135
+ }
136
+
129
137
  isZero(): boolean {
130
138
  return this.asBigInt === 0n;
131
139
  }
@@ -183,10 +191,25 @@ function fromHexString<T extends BaseField>(buf: string, f: DerivedField<T>) {
183
191
  return new f(toBigIntBE(buffer));
184
192
  }
185
193
 
186
- /** Branding to ensure fields are not interchangeable types. */
187
- export interface Fr {
188
- /** Brand. */
189
- _branding: 'Fr';
194
+ /**
195
+ * Abstract unbranded base class for BN254 scalar field elements.
196
+ * Extend this instead of Fr when defining a branded subtype (e.g. BlockHash)
197
+ * to avoid inheriting Fr's `_branding`.
198
+ */
199
+ export abstract class BaseFr extends BaseField {
200
+ static MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
201
+
202
+ constructor(value: number | bigint | boolean | BaseField | Buffer) {
203
+ super(value);
204
+ }
205
+
206
+ protected modulus() {
207
+ return BaseFr.MODULUS;
208
+ }
209
+
210
+ toJSON() {
211
+ return this.toString();
212
+ }
190
213
  }
191
214
 
192
215
  /**
@@ -194,10 +217,12 @@ export interface Fr {
194
217
  * @dev This class is used to represent elements of BN254 scalar field or elements in the base field of Grumpkin.
195
218
  * (Grumpkin's scalar field corresponds to BN254's base field and vice versa.)
196
219
  */
197
- export class Fr extends BaseField {
220
+ export class Fr extends BaseFr {
221
+ /** Branding for nominal typing. */
222
+ declare private readonly _branding: 'Fr';
223
+ static override MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
198
224
  static ZERO = new Fr(0n);
199
225
  static ONE = new Fr(1n);
200
- static MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
201
226
  static MAX_FIELD_VALUE = new Fr(this.MODULUS - 1n);
202
227
 
203
228
  constructor(value: number | bigint | boolean | Fr | Buffer) {
@@ -208,10 +233,6 @@ export class Fr extends BaseField {
208
233
  return `Fr<${this.toString()}>`;
209
234
  }
210
235
 
211
- protected modulus() {
212
- return Fr.MODULUS;
213
- }
214
-
215
236
  static random() {
216
237
  return random(Fr);
217
238
  }
@@ -316,10 +337,6 @@ export class Fr extends BaseField {
316
337
  return Fr.fromBuffer(Buffer.from(response.value));
317
338
  }
318
339
 
319
- toJSON() {
320
- return this.toString();
321
- }
322
-
323
340
  /**
324
341
  * Creates an Fr instance from a plain object without Zod validation.
325
342
  * This method is optimized for performance and skips validation, making it suitable
@@ -341,23 +358,14 @@ export class Fr extends BaseField {
341
358
  }
342
359
  }
343
360
 
344
- // For deserializing JSON.
345
- TypeRegistry.register('Fr', Fr);
346
-
347
- /**
348
- * Branding to ensure fields are not interchangeable types.
349
- */
350
- export interface Fq {
351
- /** Brand. */
352
- _branding: 'Fq';
353
- }
354
-
355
361
  /**
356
362
  * Fq field class.
357
363
  * @dev This class is used to represent elements of BN254 base field or elements in the scalar field of Grumpkin.
358
364
  * (Grumpkin's scalar field corresponds to BN254's base field and vice versa.)
359
365
  */
360
366
  export class Fq extends BaseField {
367
+ /** Branding for nominal typing. */
368
+ declare private readonly _branding: 'Fq';
361
369
  static ZERO = new Fq(0n);
362
370
  static MODULUS = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47n;
363
371
  private static HIGH_SHIFT = BigInt((BaseField.SIZE_IN_BYTES / 2) * 8);
@@ -466,9 +474,6 @@ export class Fq extends BaseField {
466
474
  }
467
475
  }
468
476
 
469
- // For deserializing JSON.
470
- TypeRegistry.register('Fq', Fq);
471
-
472
477
  // Beware: Performance bottleneck below
473
478
 
474
479
  /**
@@ -1,5 +1,4 @@
1
1
  import { toBigIntBE } from '../../bigint-buffer/index.js';
2
- import { poseidon2Hash } from '../../crypto/poseidon/index.js';
3
2
  import { randomBoolean } from '../../crypto/random/index.js';
4
3
  import { hexSchemaFor } from '../../schemas/utils.js';
5
4
  import { BufferReader, FieldReader, serializeToBuffer } from '../../serialize/index.js';
@@ -13,13 +12,16 @@ import { Fr } from '../bn254/field.js';
13
12
  * TODO(#7386): Clean up this class.
14
13
  */
15
14
  export class Point {
16
- static ZERO = new Point(Fr.ZERO, Fr.ZERO, false);
15
+ static INFINITY = new Point(Fr.ZERO, Fr.ZERO);
17
16
  static SIZE_IN_BYTES = Fr.SIZE_IN_BYTES * 2;
18
17
  static COMPRESSED_SIZE_IN_BYTES = Fr.SIZE_IN_BYTES;
19
18
 
20
19
  /** Used to differentiate this class from AztecAddress */
21
20
  public readonly kind = 'point';
22
21
 
22
+ /** Whether the point is at infinity */
23
+ public readonly isInfinite: boolean;
24
+
23
25
  constructor(
24
26
  /**
25
27
  * The point's x coordinate
@@ -29,12 +31,10 @@ export class Point {
29
31
  * The point's y coordinate
30
32
  */
31
33
  public readonly y: Fr,
32
- /**
33
- * Whether the point is at infinity
34
- */
35
- public readonly isInfinite: boolean,
36
34
  ) {
37
35
  // TODO(#7386): check if on curve
36
+ // NOTE: now there is no isInfinite in the struct, empty == inf, so an empty class would pass an on-curve check. This may be fine depending on the usage.
37
+ this.isInfinite = x.isZero() && y.isZero();
38
38
  }
39
39
 
40
40
  toJSON() {
@@ -61,11 +61,11 @@ export class Point {
61
61
  if (obj instanceof Buffer || Buffer.isBuffer(obj)) {
62
62
  return Point.fromBuffer(obj);
63
63
  }
64
- return new Point(Fr.fromPlainObject(obj.x), Fr.fromPlainObject(obj.y), obj.isInfinite ?? false);
64
+ return new this(Fr.fromPlainObject(obj.x), Fr.fromPlainObject(obj.y));
65
65
  }
66
66
 
67
67
  /**
68
- * Generate a random Point instance.
68
+ * Generate a random Point instance that is on the curve.
69
69
  *
70
70
  * @returns A randomly generated Point instance.
71
71
  */
@@ -92,7 +92,7 @@ export class Point {
92
92
  */
93
93
  static fromBuffer(buffer: Buffer | BufferReader) {
94
94
  const reader = BufferReader.asReader(buffer);
95
- return new this(Fr.fromBuffer(reader), Fr.fromBuffer(reader), false);
95
+ return new this(Fr.fromBuffer(reader), Fr.fromBuffer(reader));
96
96
  }
97
97
 
98
98
  /**
@@ -129,12 +129,13 @@ export class Point {
129
129
  * @returns The point as an array of 2 fields
130
130
  */
131
131
  toFields() {
132
- return [this.x, this.y, new Fr(this.isInfinite)];
132
+ return [this.x, this.y];
133
133
  }
134
134
 
135
135
  static fromFields(fields: Fr[] | FieldReader) {
136
136
  const reader = FieldReader.asReader(fields);
137
- return new this(reader.readField(), reader.readField(), reader.readBoolean());
137
+ const [x, y] = [reader.readField(), reader.readField()];
138
+ return new this(x, y);
138
139
  }
139
140
 
140
141
  /**
@@ -159,11 +160,12 @@ export class Point {
159
160
  const finalY = sign ? new Fr(yPositiveBigInt) : new Fr(yNegativeBigInt);
160
161
 
161
162
  // Create and return the new Point
162
- return new this(x, finalY, false);
163
+ return new this(x, finalY);
163
164
  }
164
165
 
165
166
  /**
166
- * @returns
167
+ * @param x - The x coordinate of the point
168
+ * @returns y^2 such that y^2 = x^3 - 17
167
169
  */
168
170
  static YFromX(x: Fr): Promise<Fr | null> {
169
171
  // Calculate y^2 = x^3 - 17 (i.e. the Grumpkin curve equation)
@@ -191,24 +193,15 @@ export class Point {
191
193
  return {
192
194
  x: this.x.toBigInt(),
193
195
  y: this.y.toBigInt(),
194
- isInfinite: this.isInfinite ? 1n : 0n,
195
196
  };
196
197
  }
197
198
 
198
199
  /**
199
200
  * Converts the Point instance to a Buffer representation of the coordinates.
200
201
  * @returns A Buffer representation of the Point instance.
201
- * @dev Note that toBuffer does not include the isInfinite flag and other serialization methods do (e.g. toFields).
202
- * This is because currently when we work with point as bytes we don't want to populate the extra bytes for
203
- * isInfinite flag because:
204
- * 1. Our Grumpkin BB API currently does not handle point at infinity,
205
- * 2. we use toBuffer when serializing notes and events and there we only work with public keys and point at infinity
206
- * is not considered a valid public key and the extra byte would raise DA cost.
202
+ * @dev Note that toBuffer does not include the isInfinite flag. The point at infinity is serialized as (0, 0).
207
203
  */
208
204
  toBuffer() {
209
- if (this.isInfinite) {
210
- throw new Error('Cannot serialize infinite point with isInfinite flag');
211
- }
212
205
  const buf = serializeToBuffer([this.x, this.y]);
213
206
  if (buf.length !== Point.SIZE_IN_BYTES) {
214
207
  throw new Error(`Invalid buffer length for Point: ${buf.length}`);
@@ -258,12 +251,10 @@ export class Point {
258
251
  }
259
252
 
260
253
  toNoirStruct() {
261
- /* eslint-disable camelcase */
262
- return { x: this.x, y: this.y, is_infinite: this.isInfinite };
263
- /* eslint-enable camelcase */
254
+ return { x: this.x, y: this.y };
264
255
  }
265
256
 
266
- // Used for IvpkM, OvpkM, NpkM and TpkM. TODO(#8124): Consider removing this method.
257
+ // Used for IvpkM. TODO(#8124): Consider removing this method.
267
258
  toWrappedNoirStruct() {
268
259
  return { inner: this.toNoirStruct() };
269
260
  }
@@ -283,21 +274,13 @@ export class Point {
283
274
  return this.x.isZero() && this.y.isZero();
284
275
  }
285
276
 
286
- hash() {
287
- return poseidon2Hash(this.toFields());
288
- }
289
-
290
277
  /**
291
- * Check if this is point at infinity.
292
- * Check this is consistent with how bb is encoding the point at infinity
278
+ * @param x - The x coordinate of the point
279
+ * @param y - The y coordinate of the point
280
+ * @returns Whether the point exists on Grumpkin
293
281
  */
294
- public get inf() {
295
- return this.isInfinite;
296
- }
297
-
298
- isOnGrumpkin() {
299
- // TODO: Check this against how bb handles curve check and infinity point check
300
- if (this.inf) {
282
+ isOnCurve() {
283
+ if (this.isInfinite) {
301
284
  return true;
302
285
  }
303
286
 
@@ -5,7 +5,6 @@ import { randomBytes } from '../crypto/random/index.js';
5
5
  import { Fr } from '../curves/bn254/index.js';
6
6
  import { hexSchemaFor } from '../schemas/utils.js';
7
7
  import { BufferReader, FieldReader } from '../serialize/index.js';
8
- import { TypeRegistry } from '../serialize/type_registry.js';
9
8
  import { bufferToHex } from '../string/index.js';
10
9
 
11
10
  /**
@@ -268,6 +267,3 @@ export class EthAddress {
268
267
  return addrA.equals(addrB);
269
268
  }
270
269
  }
271
-
272
- // For deserializing JSON.
273
- TypeRegistry.register('EthAddress', EthAddress);
@@ -1,8 +1,10 @@
1
1
  import { Buffer32 } from '@aztec/foundation/buffer';
2
2
  import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
3
3
 
4
+ import { secp256k1 } from '@noble/curves/secp256k1';
4
5
  import { z } from 'zod';
5
6
 
7
+ import { randomBytes } from '../crypto/random/index.js';
6
8
  import { hasHexPrefix, hexToBuffer } from '../string/index.js';
7
9
 
8
10
  /**
@@ -77,8 +79,12 @@ export class Signature {
77
79
  return new Signature(Buffer32.fromBuffer(hexToBuffer(sig.r)), Buffer32.fromBuffer(hexToBuffer(sig.s)), sig.yParity);
78
80
  }
79
81
 
82
+ /** Generates a random valid ECDSA signature with a low s-value by signing a random message with a random key. */
80
83
  static random(): Signature {
81
- return new Signature(Buffer32.random(), Buffer32.random(), 1);
84
+ const privateKey = randomBytes(32);
85
+ const message = randomBytes(32);
86
+ const { r, s, recovery } = secp256k1.sign(message, privateKey);
87
+ return new Signature(Buffer32.fromBigInt(r), Buffer32.fromBigInt(s), recovery ? 28 : 27);
82
88
  }
83
89
 
84
90
  static empty(): Signature {
@@ -0,0 +1,113 @@
1
+ import EventEmitter from 'node:events';
2
+ import * as fs from 'node:fs';
3
+ import * as net from 'node:net';
4
+ import type { Readable } from 'node:stream';
5
+
6
+ /**
7
+ * Events emitted by FifoFrameReader.
8
+ *
9
+ * - `frame`: A complete frame payload (without the 4-byte length header).
10
+ * - `error`: An unrecoverable error (invalid frame length, stream error).
11
+ * - `end`: The underlying stream has ended.
12
+ */
13
+ export interface FifoFrameReaderEvents {
14
+ frame: [payload: Buffer];
15
+ error: [error: Error];
16
+ end: [];
17
+ }
18
+
19
+ /**
20
+ * Reads length-delimited frames from a readable stream (typically a named FIFO pipe).
21
+ *
22
+ * Wire format: `[4-byte big-endian payload length][payload bytes]`
23
+ *
24
+ * Emits a `frame` event for each complete frame with the raw payload buffer.
25
+ * Callers are responsible for deserializing the payload (e.g., via msgpack).
26
+ *
27
+ * On encountering an invalid payload length (0 or >maxPayloadSize), emits `error`
28
+ * and destroys the stream.
29
+ */
30
+ export class FifoFrameReader extends EventEmitter<FifoFrameReaderEvents> {
31
+ private stream: Readable | null = null;
32
+ private pendingBuf: Buffer = Buffer.alloc(0);
33
+ private running = false;
34
+
35
+ constructor(private readonly maxPayloadSize = 10 * 1024 * 1024) {
36
+ super();
37
+ }
38
+
39
+ /** Open a FIFO at the given path and start reading frames. */
40
+ start(fifoPath: string): void {
41
+ // Read the FIFO through a libuv pipe handle (net.Socket) rather than fs.createReadStream.
42
+ // An fs read stream services the pipe with a blocking threadpool read that destroy() cannot
43
+ // cancel: if a writer never closes, that read stays parked and keeps the host process alive
44
+ // (manifesting as Jest "did not exit"). Opening O_NONBLOCK and wrapping the fd in a pipe
45
+ // handle makes reads epoll-based, so stop()'s destroy() releases the handle immediately,
46
+ // regardless of whether the writer is still attached.
47
+ const fd = fs.openSync(fifoPath, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK);
48
+ let socket: net.Socket;
49
+ try {
50
+ socket = new net.Socket({ fd, readable: true, writable: false });
51
+ } catch (err) {
52
+ fs.closeSync(fd);
53
+ throw err;
54
+ }
55
+ this.startFromStream(socket);
56
+ }
57
+
58
+ /** Start reading frames from an existing readable stream. */
59
+ startFromStream(stream: Readable): void {
60
+ if (this.running) {
61
+ throw new Error('FifoFrameReader is already running');
62
+ }
63
+ this.running = true;
64
+ this.pendingBuf = Buffer.alloc(0);
65
+ this.stream = stream;
66
+
67
+ stream.on('data', (chunk: Buffer | string) => {
68
+ const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
69
+ this.pendingBuf = this.pendingBuf.length > 0 ? Buffer.concat([this.pendingBuf, buf]) : buf;
70
+ this.drainFrames();
71
+ });
72
+
73
+ stream.on('error', (err: Error) => {
74
+ if (this.running) {
75
+ this.emit('error', err);
76
+ }
77
+ });
78
+
79
+ stream.on('end', () => {
80
+ this.emit('end');
81
+ });
82
+ }
83
+
84
+ /** Stop reading and destroy the underlying stream. */
85
+ stop(): void {
86
+ this.running = false;
87
+ if (this.stream) {
88
+ this.stream.destroy();
89
+ this.stream = null;
90
+ }
91
+ }
92
+
93
+ /** Parse complete frames out of the pending buffer. */
94
+ private drainFrames(): void {
95
+ while (this.pendingBuf.length >= 4) {
96
+ const payloadLen = this.pendingBuf.readUInt32BE(0);
97
+ if (payloadLen === 0 || payloadLen > this.maxPayloadSize) {
98
+ this.emit('error', new Error(`Invalid payload length: ${payloadLen}`));
99
+ this.stop();
100
+ return;
101
+ }
102
+
103
+ const frameLen = 4 + payloadLen;
104
+ if (this.pendingBuf.length < frameLen) {
105
+ break; // Wait for more data
106
+ }
107
+
108
+ const payload = this.pendingBuf.subarray(4, frameLen);
109
+ this.pendingBuf = this.pendingBuf.subarray(frameLen);
110
+ this.emit('frame', Buffer.from(payload));
111
+ }
112
+ }
113
+ }
@@ -0,0 +1 @@
1
+ export { FifoFrameReader } from './fifo_frame_reader.js';
@@ -0,0 +1,52 @@
1
+ /** A Set capped to a fixed number of entries, evicting the oldest inserted value when full. */
2
+ export class FifoSet<T> extends Set<T> {
3
+ private _limit: number | undefined;
4
+
5
+ private constructor(values?: Iterable<T>) {
6
+ super(values);
7
+ }
8
+
9
+ /** Creates a bounded set with a positive integer limit. */
10
+ static withLimit<T>(limit: number, values?: Iterable<T>): FifoSet<T> {
11
+ if (!Number.isSafeInteger(limit) || limit <= 0) {
12
+ throw new TypeError(`FifoSet limit must be a positive safe integer: ${limit}`);
13
+ }
14
+
15
+ const set = new FifoSet(values);
16
+ set._limit = limit;
17
+ set.compact();
18
+
19
+ return set;
20
+ }
21
+
22
+ override add(value: T): this {
23
+ super.add(value);
24
+ this.compact();
25
+ return this;
26
+ }
27
+
28
+ /** Maximum number of entries retained by this set. */
29
+ public get limit(): number {
30
+ return this._limit ?? Infinity;
31
+ }
32
+
33
+ /** Evicts oldest entries until the set is within its limit. */
34
+ public compact(): void {
35
+ while (this.size > this.limit) {
36
+ const head = this.values().next();
37
+ if (head.done) {
38
+ return;
39
+ }
40
+ this.delete(head.value);
41
+ }
42
+ }
43
+
44
+ /** Adds a value only if it is absent, returning whether the set changed. */
45
+ public addIfAbsent(value: T): boolean {
46
+ if (this.has(value)) {
47
+ return false;
48
+ }
49
+ this.add(value);
50
+ return true;
51
+ }
52
+ }
@@ -0,0 +1 @@
1
+ export * from './fifo_set.js';