@aztec/foundation 0.0.1-fake-c83136db25 → 0.0.2-commit.217f559981

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 (507) hide show
  1. package/dest/array/array.d.ts +1 -9
  2. package/dest/array/array.d.ts.map +1 -1
  3. package/dest/array/array.js +0 -11
  4. package/dest/array/index.d.ts +2 -1
  5. package/dest/array/index.d.ts.map +1 -1
  6. package/dest/array/index.js +1 -0
  7. package/dest/array/sorted_array.d.ts +15 -0
  8. package/dest/array/sorted_array.d.ts.map +1 -0
  9. package/dest/array/sorted_array.js +109 -0
  10. package/dest/async-map/index.d.ts +1 -1
  11. package/dest/async-pool/index.d.ts +1 -1
  12. package/dest/bigint/index.d.ts +1 -1
  13. package/dest/bigint-buffer/index.d.ts +1 -1
  14. package/dest/branded-types/block_number.d.ts +56 -0
  15. package/dest/branded-types/block_number.d.ts.map +1 -0
  16. package/dest/branded-types/block_number.js +78 -0
  17. package/dest/branded-types/checkpoint_number.d.ts +54 -0
  18. package/dest/branded-types/checkpoint_number.d.ts.map +1 -0
  19. package/dest/branded-types/checkpoint_number.js +77 -0
  20. package/dest/branded-types/epoch.d.ts +42 -0
  21. package/dest/branded-types/epoch.d.ts.map +1 -0
  22. package/dest/branded-types/epoch.js +59 -0
  23. package/dest/branded-types/index.d.ts +7 -0
  24. package/dest/branded-types/index.d.ts.map +1 -0
  25. package/dest/branded-types/index.js +5 -0
  26. package/dest/branded-types/index_within_checkpoint.d.ts +42 -0
  27. package/dest/branded-types/index_within_checkpoint.d.ts.map +1 -0
  28. package/dest/branded-types/index_within_checkpoint.js +59 -0
  29. package/dest/branded-types/slot.d.ts +42 -0
  30. package/dest/branded-types/slot.d.ts.map +1 -0
  31. package/dest/branded-types/slot.js +59 -0
  32. package/dest/branded-types/types.d.ts +5 -0
  33. package/dest/branded-types/types.d.ts.map +1 -0
  34. package/dest/branded-types/types.js +1 -0
  35. package/dest/buffer/buffer16.d.ts +2 -2
  36. package/dest/buffer/buffer16.d.ts.map +1 -1
  37. package/dest/buffer/buffer16.js +4 -2
  38. package/dest/buffer/buffer32.d.ts +2 -2
  39. package/dest/buffer/buffer32.d.ts.map +1 -1
  40. package/dest/buffer/buffer32.js +4 -2
  41. package/dest/buffer/index.d.ts +1 -1
  42. package/dest/collection/array.d.ts +14 -1
  43. package/dest/collection/array.d.ts.map +1 -1
  44. package/dest/collection/array.js +63 -3
  45. package/dest/collection/index.d.ts +1 -1
  46. package/dest/collection/object.d.ts +1 -1
  47. package/dest/committable/committable.d.ts +1 -1
  48. package/dest/committable/committable.d.ts.map +1 -1
  49. package/dest/committable/index.d.ts +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 +15 -4
  53. package/dest/config/index.d.ts.map +1 -1
  54. package/dest/config/index.js +32 -9
  55. package/dest/config/network_config.d.ts +13 -1
  56. package/dest/config/network_config.d.ts.map +1 -1
  57. package/dest/config/network_config.js +3 -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 +2 -0
  61. package/dest/config/parse-env.d.ts +3 -0
  62. package/dest/config/parse-env.d.ts.map +1 -0
  63. package/dest/config/parse-env.js +7 -0
  64. package/dest/config/secret_value.d.ts +1 -1
  65. package/dest/config/secret_value.d.ts.map +1 -1
  66. package/dest/config/secret_value.js +3 -1
  67. package/dest/crypto/aes128/index.d.ts +1 -1
  68. package/dest/crypto/aes128/index.d.ts.map +1 -1
  69. package/dest/crypto/aes128/index.js +2 -6
  70. package/dest/crypto/bls/bn254_keystore.d.ts +296 -0
  71. package/dest/crypto/bls/bn254_keystore.d.ts.map +1 -0
  72. package/dest/crypto/bls/bn254_keystore.js +218 -0
  73. package/dest/crypto/bls/index.d.ts +13 -0
  74. package/dest/crypto/bls/index.d.ts.map +1 -0
  75. package/dest/crypto/bls/index.js +87 -0
  76. package/dest/crypto/bn254/index.d.ts +35 -50
  77. package/dest/crypto/bn254/index.d.ts.map +1 -1
  78. package/dest/crypto/bn254/index.js +51 -135
  79. package/dest/crypto/ecdsa/index.d.ts +1 -1
  80. package/dest/crypto/ecdsa/index.d.ts.map +1 -1
  81. package/dest/crypto/ecdsa/index.js +4 -12
  82. package/dest/crypto/ecdsa/signature.d.ts +2 -2
  83. package/dest/crypto/ecdsa/signature.d.ts.map +1 -1
  84. package/dest/crypto/ecdsa/signature.js +2 -2
  85. package/dest/crypto/grumpkin/index.d.ts +4 -2
  86. package/dest/crypto/grumpkin/index.d.ts.map +1 -1
  87. package/dest/crypto/grumpkin/index.js +7 -16
  88. package/dest/crypto/keccak/index.d.ts +1 -1
  89. package/dest/crypto/keys/index.d.ts +2 -2
  90. package/dest/crypto/keys/index.d.ts.map +1 -1
  91. package/dest/crypto/keys/index.js +2 -4
  92. package/dest/crypto/pedersen/index.d.ts +1 -1
  93. package/dest/crypto/pedersen/pedersen.noble.d.ts +1 -1
  94. package/dest/crypto/pedersen/pedersen.wasm.d.ts +2 -2
  95. package/dest/crypto/pedersen/pedersen.wasm.d.ts.map +1 -1
  96. package/dest/crypto/pedersen/pedersen.wasm.js +4 -10
  97. package/dest/crypto/poseidon/index.d.ts +2 -3
  98. package/dest/crypto/poseidon/index.d.ts.map +1 -1
  99. package/dest/crypto/poseidon/index.js +5 -24
  100. package/dest/crypto/random/index.d.ts +1 -1
  101. package/dest/crypto/random/index.d.ts.map +1 -1
  102. package/dest/crypto/random/randomness_singleton.d.ts +4 -3
  103. package/dest/crypto/random/randomness_singleton.d.ts.map +1 -1
  104. package/dest/crypto/random/randomness_singleton.js +5 -5
  105. package/dest/crypto/schnorr/index.d.ts +3 -2
  106. package/dest/crypto/schnorr/index.d.ts.map +1 -1
  107. package/dest/crypto/schnorr/index.js +4 -10
  108. package/dest/crypto/schnorr/signature.d.ts +2 -2
  109. package/dest/crypto/schnorr/signature.d.ts.map +1 -1
  110. package/dest/crypto/schnorr/signature.js +2 -2
  111. package/dest/crypto/secp256k1/index.d.ts +1 -1
  112. package/dest/crypto/secp256k1/index.d.ts.map +1 -1
  113. package/dest/crypto/secp256k1/index.js +3 -9
  114. package/dest/crypto/secp256k1-signer/index.d.ts +1 -1
  115. package/dest/crypto/secp256k1-signer/secp256k1_signer.d.ts +1 -1
  116. package/dest/crypto/secp256k1-signer/secp256k1_signer.d.ts.map +1 -1
  117. package/dest/crypto/secp256k1-signer/utils.d.ts +43 -5
  118. package/dest/crypto/secp256k1-signer/utils.d.ts.map +1 -1
  119. package/dest/crypto/secp256k1-signer/utils.js +76 -8
  120. package/dest/crypto/serialize.d.ts +1 -1
  121. package/dest/crypto/sha256/index.d.ts +2 -2
  122. package/dest/crypto/sha256/index.d.ts.map +1 -1
  123. package/dest/crypto/sha256/index.js +1 -1
  124. package/dest/crypto/sha512/index.d.ts +3 -3
  125. package/dest/crypto/sha512/index.d.ts.map +1 -1
  126. package/dest/crypto/sha512/index.js +1 -1
  127. package/dest/crypto/signature/index.d.ts +2 -2
  128. package/dest/crypto/signature/index.d.ts.map +1 -1
  129. package/dest/crypto/sync/index.d.ts +1 -1
  130. package/dest/crypto/sync/index.js +1 -3
  131. package/dest/crypto/sync/pedersen/index.d.ts +2 -2
  132. package/dest/crypto/sync/pedersen/index.d.ts.map +1 -1
  133. package/dest/crypto/sync/pedersen/index.js +1 -1
  134. package/dest/crypto/sync/poseidon/index.d.ts +2 -3
  135. package/dest/crypto/sync/poseidon/index.d.ts.map +1 -1
  136. package/dest/crypto/sync/poseidon/index.js +1 -9
  137. package/dest/{fields/bls12_fields.d.ts → curves/bls12/field.d.ts} +3 -3
  138. package/dest/curves/bls12/field.d.ts.map +1 -0
  139. package/dest/{fields/bls12_fields.js → curves/bls12/field.js} +12 -9
  140. package/dest/curves/bls12/index.d.ts +3 -0
  141. package/dest/curves/bls12/index.d.ts.map +1 -0
  142. package/dest/curves/bls12/index.js +2 -0
  143. package/dest/{fields/bls12_point.d.ts → curves/bls12/point.d.ts} +4 -4
  144. package/dest/curves/bls12/point.d.ts.map +1 -0
  145. package/dest/{fields/bls12_point.js → curves/bls12/point.js} +10 -8
  146. package/dest/{fields/fields.d.ts → curves/bn254/field.d.ts} +16 -15
  147. package/dest/curves/bn254/field.d.ts.map +1 -0
  148. package/dest/{fields/fields.js → curves/bn254/field.js} +49 -49
  149. package/dest/curves/bn254/index.d.ts +3 -0
  150. package/dest/curves/bn254/index.d.ts.map +1 -0
  151. package/dest/curves/bn254/index.js +2 -0
  152. package/dest/curves/bn254/point.d.ts +54 -0
  153. package/dest/curves/bn254/point.d.ts.map +1 -0
  154. package/dest/curves/bn254/point.js +143 -0
  155. package/dest/curves/grumpkin/index.d.ts +10 -0
  156. package/dest/curves/grumpkin/index.d.ts.map +1 -0
  157. package/dest/curves/grumpkin/index.js +3 -0
  158. package/dest/{fields → curves/grumpkin}/point.d.ts +12 -3
  159. package/dest/curves/grumpkin/point.d.ts.map +1 -0
  160. package/dest/{fields → curves/grumpkin}/point.js +24 -7
  161. package/dest/decorators/index.d.ts +1 -1
  162. package/dest/decorators/memoize.d.ts +1 -1
  163. package/dest/decorators/memoize.d.ts.map +1 -1
  164. package/dest/error/index.d.ts +1 -1
  165. package/dest/eth-address/index.d.ts +12 -2
  166. package/dest/eth-address/index.d.ts.map +1 -1
  167. package/dest/eth-address/index.js +27 -3
  168. package/dest/eth-signature/eth_signature.d.ts +1 -1
  169. package/dest/eth-signature/eth_signature.d.ts.map +1 -1
  170. package/dest/eth-signature/index.d.ts +1 -1
  171. package/dest/fs/index.d.ts +1 -1
  172. package/dest/fs/run_in_dir.d.ts +1 -1
  173. package/dest/fs/try_rm_dir.d.ts +1 -1
  174. package/dest/iterable/all.d.ts +1 -1
  175. package/dest/iterable/filter.d.ts +1 -1
  176. package/dest/iterable/index.d.ts +1 -1
  177. package/dest/iterable/isAsyncIt.d.ts +1 -1
  178. package/dest/iterable/map.d.ts +1 -1
  179. package/dest/iterable/peek.d.ts +1 -1
  180. package/dest/iterable/sort.d.ts +1 -1
  181. package/dest/iterable/take.d.ts +1 -1
  182. package/dest/iterable/toArray.d.ts +1 -1
  183. package/dest/iterator/filter.d.ts +3 -0
  184. package/dest/iterator/filter.d.ts.map +1 -0
  185. package/dest/iterator/filter.js +7 -0
  186. package/dest/iterator/index.d.ts +2 -0
  187. package/dest/iterator/index.d.ts.map +1 -0
  188. package/dest/iterator/index.js +1 -0
  189. package/dest/jest/setup.js +4 -1
  190. package/dest/json-rpc/client/fetch.d.ts +1 -1
  191. package/dest/json-rpc/client/index.d.ts +1 -1
  192. package/dest/json-rpc/client/safe_json_rpc_client.d.ts +2 -1
  193. package/dest/json-rpc/client/safe_json_rpc_client.d.ts.map +1 -1
  194. package/dest/json-rpc/client/safe_json_rpc_client.js +1 -1
  195. package/dest/json-rpc/client/undici.d.ts +1 -1
  196. package/dest/json-rpc/client/undici.d.ts.map +1 -1
  197. package/dest/json-rpc/client/undici.js +21 -4
  198. package/dest/json-rpc/convert.d.ts +1 -1
  199. package/dest/json-rpc/errors.d.ts +1 -1
  200. package/dest/json-rpc/errors.d.ts.map +1 -1
  201. package/dest/json-rpc/fixtures/class_a.d.ts +1 -1
  202. package/dest/json-rpc/fixtures/class_a.d.ts.map +1 -1
  203. package/dest/json-rpc/fixtures/class_b.d.ts +1 -1
  204. package/dest/json-rpc/fixtures/class_b.d.ts.map +1 -1
  205. package/dest/json-rpc/fixtures/test_state.d.ts +1 -1
  206. package/dest/json-rpc/fixtures/test_state.d.ts.map +1 -1
  207. package/dest/json-rpc/index.d.ts +1 -1
  208. package/dest/json-rpc/js_utils.d.ts +1 -1
  209. package/dest/json-rpc/js_utils.d.ts.map +1 -1
  210. package/dest/json-rpc/server/api_key_auth.d.ts +19 -0
  211. package/dest/json-rpc/server/api_key_auth.d.ts.map +1 -0
  212. package/dest/json-rpc/server/api_key_auth.js +57 -0
  213. package/dest/json-rpc/server/index.d.ts +2 -1
  214. package/dest/json-rpc/server/index.d.ts.map +1 -1
  215. package/dest/json-rpc/server/index.js +1 -0
  216. package/dest/json-rpc/server/safe_json_rpc_server.d.ts +1 -1
  217. package/dest/json-rpc/server/safe_json_rpc_server.d.ts.map +1 -1
  218. package/dest/json-rpc/server/safe_json_rpc_server.js +1 -1
  219. package/dest/json-rpc/test/index.d.ts +1 -1
  220. package/dest/json-rpc/test/integration.d.ts +1 -1
  221. package/dest/log/bigint-utils.d.ts +5 -0
  222. package/dest/log/bigint-utils.d.ts.map +1 -0
  223. package/dest/log/bigint-utils.js +18 -0
  224. package/dest/log/console.d.ts +1 -1
  225. package/dest/log/gcloud-logger-config.d.ts +1 -1
  226. package/dest/log/gcloud-logger-config.d.ts.map +1 -1
  227. package/dest/log/gcloud-logger-config.js +3 -0
  228. package/dest/log/index.d.ts +1 -1
  229. package/dest/log/libp2p_logger.d.ts +5 -2
  230. package/dest/log/libp2p_logger.d.ts.map +1 -1
  231. package/dest/log/libp2p_logger.js +14 -4
  232. package/dest/log/log-filters.d.ts +17 -4
  233. package/dest/log/log-filters.d.ts.map +1 -1
  234. package/dest/log/log-filters.js +26 -12
  235. package/dest/log/log-levels.d.ts +1 -1
  236. package/dest/log/log_fn.d.ts +1 -1
  237. package/dest/log/noir_debug_log_util.d.ts +1 -1
  238. package/dest/log/pino-logger-server.d.ts +9 -0
  239. package/dest/log/pino-logger-server.d.ts.map +1 -0
  240. package/dest/log/pino-logger-server.js +18 -0
  241. package/dest/log/pino-logger.d.ts +38 -9
  242. package/dest/log/pino-logger.d.ts.map +1 -1
  243. package/dest/log/pino-logger.js +122 -29
  244. package/dest/message/index.d.ts +1 -1
  245. package/dest/message/index.d.ts.map +1 -1
  246. package/dest/mutex/index.d.ts +1 -1
  247. package/dest/mutex/index.d.ts.map +1 -1
  248. package/dest/mutex/mutex_database.d.ts +1 -1
  249. package/dest/noir/index.d.ts +1 -1
  250. package/dest/noir/noir_package_config.d.ts +9 -9
  251. package/dest/number/index.d.ts +1 -1
  252. package/dest/profiler/index.d.ts +1 -1
  253. package/dest/profiler/profiler.d.ts +1 -1
  254. package/dest/promise/index.d.ts +1 -1
  255. package/dest/promise/running-promise.d.ts +1 -1
  256. package/dest/promise/running-promise.d.ts.map +1 -1
  257. package/dest/promise/utils.d.ts +1 -1
  258. package/dest/queue/base_memory_queue.d.ts +2 -2
  259. package/dest/queue/base_memory_queue.d.ts.map +1 -1
  260. package/dest/queue/batch_queue.d.ts +1 -1
  261. package/dest/queue/batch_queue.d.ts.map +1 -1
  262. package/dest/queue/bounded_serial_queue.d.ts +1 -1
  263. package/dest/queue/bounded_serial_queue.d.ts.map +1 -1
  264. package/dest/queue/fifo_memory_queue.d.ts +1 -1
  265. package/dest/queue/fifo_memory_queue.d.ts.map +1 -1
  266. package/dest/queue/index.d.ts +1 -1
  267. package/dest/queue/priority_memory_queue.d.ts +1 -1
  268. package/dest/queue/priority_memory_queue.d.ts.map +1 -1
  269. package/dest/queue/priority_queue.d.ts +1 -1
  270. package/dest/queue/priority_queue.d.ts.map +1 -1
  271. package/dest/queue/semaphore.d.ts +5 -1
  272. package/dest/queue/semaphore.d.ts.map +1 -1
  273. package/dest/queue/serial_queue.d.ts +1 -1
  274. package/dest/queue/serial_queue.d.ts.map +1 -1
  275. package/dest/retry/index.d.ts +11 -1
  276. package/dest/retry/index.d.ts.map +1 -1
  277. package/dest/retry/index.js +11 -0
  278. package/dest/running-promise/index.d.ts +1 -1
  279. package/dest/schemas/api.d.ts +1 -1
  280. package/dest/schemas/index.d.ts +1 -1
  281. package/dest/schemas/parse.d.ts +1 -1
  282. package/dest/schemas/schemas.d.ts +15 -17
  283. package/dest/schemas/schemas.d.ts.map +1 -1
  284. package/dest/schemas/schemas.js +14 -10
  285. package/dest/schemas/types.d.ts +31 -1
  286. package/dest/schemas/types.d.ts.map +1 -1
  287. package/dest/schemas/types.js +25 -1
  288. package/dest/schemas/utils.d.ts +4 -4
  289. package/dest/schemas/utils.d.ts.map +1 -1
  290. package/dest/schemas/utils.js +15 -5
  291. package/dest/serialize/buffer_reader.d.ts +14 -4
  292. package/dest/serialize/buffer_reader.d.ts.map +1 -1
  293. package/dest/serialize/buffer_reader.js +26 -4
  294. package/dest/serialize/field_reader.d.ts +2 -2
  295. package/dest/serialize/field_reader.d.ts.map +1 -1
  296. package/dest/serialize/field_reader.js +1 -1
  297. package/dest/serialize/free_funcs.d.ts +2 -2
  298. package/dest/serialize/free_funcs.d.ts.map +1 -1
  299. package/dest/serialize/free_funcs.js +1 -1
  300. package/dest/serialize/index.d.ts +1 -1
  301. package/dest/serialize/serialize.d.ts +20 -2
  302. package/dest/serialize/serialize.d.ts.map +1 -1
  303. package/dest/serialize/serialize.js +32 -1
  304. package/dest/serialize/type_registry.d.ts +1 -1
  305. package/dest/serialize/type_registry.d.ts.map +1 -1
  306. package/dest/serialize/types.d.ts +1 -1
  307. package/dest/sleep/index.d.ts +3 -1
  308. package/dest/sleep/index.d.ts.map +1 -1
  309. package/dest/sleep/index.js +4 -0
  310. package/dest/string/index.d.ts +5 -1
  311. package/dest/string/index.d.ts.map +1 -1
  312. package/dest/string/index.js +1 -1
  313. package/dest/testing/files/index.d.ts +2 -1
  314. package/dest/testing/files/index.d.ts.map +1 -1
  315. package/dest/testing/files/index.js +1 -1
  316. package/dest/testing/formatting.d.ts +1 -1
  317. package/dest/testing/index.d.ts +1 -1
  318. package/dest/testing/port_allocator.d.ts +1 -1
  319. package/dest/testing/snapshot_serializer.d.ts +1 -1
  320. package/dest/testing/test_data.d.ts +1 -1
  321. package/dest/timer/date.d.ts +26 -1
  322. package/dest/timer/date.d.ts.map +1 -1
  323. package/dest/timer/date.js +36 -0
  324. package/dest/timer/elapsed.d.ts +1 -1
  325. package/dest/timer/index.d.ts +1 -1
  326. package/dest/timer/timeout.d.ts +1 -1
  327. package/dest/timer/timeout.d.ts.map +1 -1
  328. package/dest/timer/timer.d.ts +1 -1
  329. package/dest/timer/timer.d.ts.map +1 -1
  330. package/dest/transport/dispatch/create_dispatch_fn.d.ts +1 -1
  331. package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +1 -1
  332. package/dest/transport/dispatch/create_dispatch_proxy.d.ts +1 -1
  333. package/dest/transport/dispatch/messages.d.ts +1 -1
  334. package/dest/transport/index.d.ts +1 -1
  335. package/dest/transport/interface/connector.d.ts +1 -1
  336. package/dest/transport/interface/listener.d.ts +1 -1
  337. package/dest/transport/interface/socket.d.ts +1 -1
  338. package/dest/transport/interface/transferable.d.ts +1 -1
  339. package/dest/transport/node/index.d.ts +1 -1
  340. package/dest/transport/node/node_connector.d.ts +1 -1
  341. package/dest/transport/node/node_connector.d.ts.map +1 -1
  342. package/dest/transport/node/node_connector_socket.d.ts +1 -1
  343. package/dest/transport/node/node_connector_socket.d.ts.map +1 -1
  344. package/dest/transport/node/node_listener.d.ts +1 -1
  345. package/dest/transport/node/node_listener.d.ts.map +1 -1
  346. package/dest/transport/node/node_listener_socket.d.ts +1 -1
  347. package/dest/transport/node/node_listener_socket.d.ts.map +1 -1
  348. package/dest/transport/transport_client.d.ts +1 -1
  349. package/dest/transport/transport_client.d.ts.map +1 -1
  350. package/dest/transport/transport_server.d.ts +1 -10
  351. package/dest/transport/transport_server.d.ts.map +1 -1
  352. package/dest/trees/balanced_merkle_tree_root.d.ts +17 -0
  353. package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -0
  354. package/dest/trees/balanced_merkle_tree_root.js +51 -0
  355. package/dest/trees/hasher.d.ts +3 -1
  356. package/dest/trees/hasher.d.ts.map +1 -1
  357. package/dest/trees/hasher.js +10 -5
  358. package/dest/trees/index.d.ts +4 -3
  359. package/dest/trees/index.d.ts.map +1 -1
  360. package/dest/trees/index.js +3 -2
  361. package/dest/trees/indexed_merkle_tree.d.ts +1 -1
  362. package/dest/trees/indexed_merkle_tree.d.ts.map +1 -1
  363. package/dest/trees/indexed_merkle_tree.js +1 -1
  364. package/dest/trees/indexed_merkle_tree_calculator.d.ts +1 -1
  365. package/dest/trees/indexed_merkle_tree_calculator.d.ts.map +1 -1
  366. package/dest/trees/indexed_tree_leaf.d.ts +1 -1
  367. package/dest/trees/membership_witness.d.ts +7 -18
  368. package/dest/trees/membership_witness.d.ts.map +1 -1
  369. package/dest/trees/membership_witness.js +10 -1
  370. package/dest/trees/merkle_tree.d.ts +1 -1
  371. package/dest/trees/merkle_tree.d.ts.map +1 -1
  372. package/dest/trees/merkle_tree_calculator.d.ts +2 -12
  373. package/dest/trees/merkle_tree_calculator.d.ts.map +1 -1
  374. package/dest/trees/merkle_tree_calculator.js +2 -32
  375. package/dest/trees/sibling_path.d.ts +5 -38
  376. package/dest/trees/sibling_path.d.ts.map +1 -1
  377. package/dest/trees/sibling_path.js +3 -3
  378. package/dest/trees/unbalanced_merkle_tree_calculator.d.ts +6 -6
  379. package/dest/trees/unbalanced_merkle_tree_calculator.d.ts.map +1 -1
  380. package/dest/trees/unbalanced_merkle_tree_calculator.js +34 -42
  381. package/dest/trees/unbalanced_merkle_tree_root.d.ts +28 -0
  382. package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -0
  383. package/dest/trees/unbalanced_merkle_tree_root.js +84 -0
  384. package/dest/trees/unbalanced_tree_store.d.ts +5 -1
  385. package/dest/trees/unbalanced_tree_store.d.ts.map +1 -1
  386. package/dest/trees/unbalanced_tree_store.js +49 -1
  387. package/dest/types/index.d.ts +6 -2
  388. package/dest/types/index.d.ts.map +1 -1
  389. package/dest/types/index.js +6 -0
  390. package/dest/url/index.d.ts +1 -1
  391. package/dest/validation/index.d.ts +1 -1
  392. package/package.json +52 -8
  393. package/src/array/array.ts +0 -16
  394. package/src/array/index.ts +1 -0
  395. package/src/array/sorted_array.ts +138 -0
  396. package/src/branded-types/block_number.ts +117 -0
  397. package/src/branded-types/checkpoint_number.ts +115 -0
  398. package/src/branded-types/epoch.ts +88 -0
  399. package/src/branded-types/index.ts +7 -0
  400. package/src/branded-types/index_within_checkpoint.ts +88 -0
  401. package/src/branded-types/slot.ts +88 -0
  402. package/src/branded-types/types.ts +2 -0
  403. package/src/buffer/buffer16.ts +2 -2
  404. package/src/buffer/buffer32.ts +2 -2
  405. package/src/collection/array.ts +67 -3
  406. package/src/config/env_var.ts +81 -15
  407. package/src/config/index.ts +39 -6
  408. package/src/config/network_config.ts +2 -0
  409. package/src/config/network_name.ts +4 -1
  410. package/src/config/parse-env.ts +4 -0
  411. package/src/crypto/aes128/index.ts +2 -2
  412. package/src/crypto/bls/bn254_keystore.ts +287 -0
  413. package/src/crypto/bls/index.ts +77 -0
  414. package/src/crypto/bn254/index.ts +50 -153
  415. package/src/crypto/ecdsa/index.ts +4 -4
  416. package/src/crypto/ecdsa/signature.ts +2 -2
  417. package/src/crypto/grumpkin/index.ts +8 -6
  418. package/src/crypto/keys/index.ts +2 -2
  419. package/src/crypto/pedersen/pedersen.wasm.ts +4 -4
  420. package/src/crypto/poseidon/index.ts +5 -15
  421. package/src/crypto/random/randomness_singleton.ts +6 -4
  422. package/src/crypto/schnorr/index.ts +5 -4
  423. package/src/crypto/schnorr/signature.ts +2 -2
  424. package/src/crypto/secp256k1/index.ts +3 -3
  425. package/src/crypto/secp256k1-signer/utils.ts +102 -10
  426. package/src/crypto/sha256/index.ts +1 -1
  427. package/src/crypto/sha512/index.ts +1 -1
  428. package/src/crypto/signature/index.ts +1 -1
  429. package/src/crypto/sync/index.ts +1 -1
  430. package/src/crypto/sync/pedersen/index.ts +1 -1
  431. package/src/crypto/sync/poseidon/index.ts +1 -10
  432. package/src/{fields/bls12_fields.ts → curves/bls12/field.ts} +6 -6
  433. package/src/curves/bls12/index.ts +2 -0
  434. package/src/{fields/bls12_point.ts → curves/bls12/point.ts} +7 -7
  435. package/src/{fields/fields.ts → curves/bn254/field.ts} +49 -56
  436. package/src/curves/bn254/index.ts +2 -0
  437. package/src/curves/bn254/point.ts +170 -0
  438. package/src/curves/grumpkin/index.ts +11 -0
  439. package/src/{fields → curves/grumpkin}/point.ts +26 -7
  440. package/src/eth-address/index.ts +27 -2
  441. package/src/iterator/filter.ts +11 -0
  442. package/src/iterator/index.ts +1 -0
  443. package/src/jest/setup.mjs +4 -1
  444. package/src/json-rpc/client/safe_json_rpc_client.ts +2 -0
  445. package/src/json-rpc/client/undici.ts +21 -3
  446. package/src/json-rpc/server/api_key_auth.ts +63 -0
  447. package/src/json-rpc/server/index.ts +1 -0
  448. package/src/json-rpc/server/safe_json_rpc_server.ts +1 -1
  449. package/src/log/bigint-utils.ts +22 -0
  450. package/src/log/gcloud-logger-config.ts +5 -0
  451. package/src/log/libp2p_logger.ts +12 -5
  452. package/src/log/log-filters.ts +29 -11
  453. package/src/log/pino-logger-server.ts +25 -0
  454. package/src/log/pino-logger.ts +144 -39
  455. package/src/queue/base_memory_queue.ts +1 -1
  456. package/src/queue/semaphore.ts +5 -0
  457. package/src/retry/index.ts +18 -0
  458. package/src/schemas/schemas.ts +24 -13
  459. package/src/schemas/types.ts +33 -0
  460. package/src/schemas/utils.ts +21 -8
  461. package/src/serialize/buffer_reader.ts +36 -9
  462. package/src/serialize/field_reader.ts +1 -1
  463. package/src/serialize/free_funcs.ts +1 -1
  464. package/src/serialize/serialize.ts +33 -1
  465. package/src/sleep/index.ts +6 -0
  466. package/src/string/index.ts +3 -0
  467. package/src/testing/files/index.ts +1 -1
  468. package/src/timer/date.ts +52 -0
  469. package/src/trees/balanced_merkle_tree_root.ts +68 -0
  470. package/src/trees/hasher.ts +9 -0
  471. package/src/trees/index.ts +3 -2
  472. package/src/trees/indexed_merkle_tree.ts +1 -1
  473. package/src/trees/membership_witness.ts +9 -1
  474. package/src/trees/merkle_tree_calculator.ts +2 -35
  475. package/src/trees/sibling_path.ts +3 -3
  476. package/src/trees/unbalanced_merkle_tree_calculator.ts +32 -50
  477. package/src/trees/unbalanced_merkle_tree_root.ts +115 -0
  478. package/src/trees/unbalanced_tree_store.ts +57 -2
  479. package/src/types/index.ts +11 -1
  480. package/dest/crypto/index.d.ts +0 -16
  481. package/dest/crypto/index.d.ts.map +0 -1
  482. package/dest/crypto/index.js +0 -15
  483. package/dest/fields/bls12_fields.d.ts.map +0 -1
  484. package/dest/fields/bls12_point.d.ts.map +0 -1
  485. package/dest/fields/coordinate.d.ts +0 -66
  486. package/dest/fields/coordinate.d.ts.map +0 -1
  487. package/dest/fields/coordinate.js +0 -96
  488. package/dest/fields/fields.d.ts.map +0 -1
  489. package/dest/fields/index.d.ts +0 -6
  490. package/dest/fields/index.d.ts.map +0 -1
  491. package/dest/fields/index.js +0 -5
  492. package/dest/fields/point.d.ts.map +0 -1
  493. package/dest/index.d.ts +0 -30
  494. package/dest/index.d.ts.map +0 -1
  495. package/dest/index.js +0 -30
  496. package/dest/json-rpc/server/telemetry.d.ts +0 -2
  497. package/dest/json-rpc/server/telemetry.d.ts.map +0 -1
  498. package/dest/json-rpc/server/telemetry.js +0 -0
  499. package/dest/trees/unbalanced_merkle_tree.d.ts +0 -28
  500. package/dest/trees/unbalanced_merkle_tree.d.ts.map +0 -1
  501. package/dest/trees/unbalanced_merkle_tree.js +0 -99
  502. package/src/crypto/index.ts +0 -15
  503. package/src/fields/coordinate.ts +0 -104
  504. package/src/fields/index.ts +0 -5
  505. package/src/index.ts +0 -30
  506. package/src/json-rpc/server/telemetry.ts +0 -0
  507. package/src/trees/unbalanced_merkle_tree.ts +0 -111
@@ -7,6 +7,27 @@ import { keccak256 } from '../keccak/index.js';
7
7
 
8
8
  const ETH_SIGN_PREFIX = '\x19Ethereum Signed Message:\n32';
9
9
 
10
+ /** Signature recovery options */
11
+ type RecoveryOpts = {
12
+ /**
13
+ * Whether to allow s-values in the high half of the curve (s >= CURVE.n/2).
14
+ * These are discouraged by EIP2 to prevent signature malleability, and outright
15
+ * rejected in OpenZeppelin's ECDSA recover, which we use in our Rollup contract.
16
+ */
17
+ allowMalleable?: boolean;
18
+ /**
19
+ * Whether to allow an y-parity 0-1 bit instead of the standard v value 27-28.
20
+ */
21
+ allowYParityAsV?: boolean;
22
+ };
23
+
24
+ export class Secp256k1Error extends Error {
25
+ constructor(message: string, opts?: { cause: unknown }) {
26
+ super(message, opts);
27
+ this.name = 'Secp256k1Error';
28
+ }
29
+ }
30
+
10
31
  // We just hash the message to make it easier to work with in the smart contract.
11
32
  export function makeEthSignDigest(message: Buffer32): Buffer32 {
12
33
  const prefix = Buffer.from(ETH_SIGN_PREFIX);
@@ -46,16 +67,18 @@ export function addressFromPrivateKey(privateKey: Buffer): EthAddress {
46
67
  * Recovers an address from a hash and a signature.
47
68
  * @param hash - The hash to recover the address from.
48
69
  * @param signature - The signature to recover the address from.
70
+ * @param opts - Recovery options.
49
71
  * @returns The address.
50
- * @throws Error if signature recovery fails.
72
+ * @throws Error if signature recovery fails or if signature is malleable and allowMalleable is false.
51
73
  */
52
- export function recoverAddress(hash: Buffer32, signature: Signature): EthAddress {
74
+ export function recoverAddress(hash: Buffer32, signature: Signature, opts?: RecoveryOpts): EthAddress {
53
75
  try {
54
- const publicKey = recoverPublicKey(hash, signature);
76
+ const publicKey = recoverPublicKey(hash, signature, opts);
55
77
  return publicKeyToAddress(publicKey);
56
- } catch (err) {
57
- throw new Error(
58
- `Error recovering Ethereum address from hash ${hash.toString()} and signature ${signature.toString()}: ${err}`,
78
+ } catch (err: unknown) {
79
+ throw new Secp256k1Error(
80
+ `Error recovering Ethereum address from hash ${hash.toString()} and signature ${signature.toString()}`,
81
+ { cause: err },
59
82
  );
60
83
  }
61
84
  }
@@ -64,11 +87,12 @@ export function recoverAddress(hash: Buffer32, signature: Signature): EthAddress
64
87
  * Safely attempts to recover an address from a hash and a signature.
65
88
  * @param hash - The hash to recover the address from.
66
89
  * @param signature - The signature to recover the address from.
90
+ * @param opts - Recovery options.
67
91
  * @returns The address if recovery succeeds, undefined otherwise.
68
92
  */
69
- export function tryRecoverAddress(hash: Buffer32, signature: Signature): EthAddress | undefined {
93
+ export function tryRecoverAddress(hash: Buffer32, signature: Signature, opts?: RecoveryOpts): EthAddress | undefined {
70
94
  try {
71
- const publicKey = recoverPublicKey(hash, signature);
95
+ const publicKey = recoverPublicKey(hash, signature, opts);
72
96
  return publicKeyToAddress(publicKey);
73
97
  } catch {
74
98
  return undefined;
@@ -91,7 +115,7 @@ export function toRecoveryBit(yParityOrV: number) {
91
115
  if (yParityOrV === 28) {
92
116
  return 1;
93
117
  }
94
- throw new Error('Invalid yParityOrV value');
118
+ throw new Secp256k1Error(`Invalid yParityOrV value ${yParityOrV}`);
95
119
  }
96
120
 
97
121
  /**
@@ -105,16 +129,84 @@ export function signMessage(message: Buffer32, privateKey: Buffer) {
105
129
  return new Signature(Buffer32.fromBigInt(r), Buffer32.fromBigInt(s), recovery ? 28 : 27);
106
130
  }
107
131
 
132
+ /**
133
+ * Flips an ECDSA signature.
134
+ * If the signature has a low s-value (s < CURVE.n/2), it flips it to high s-value (CURVE.n - s) and vice versa.
135
+ * Also flips the v value accordingly (27 <-> 28, or 0 <-> 1).
136
+ * This is useful for testing signature malleability handling.
137
+ * @param signature - The signature to flip.
138
+ * @returns A new signature with flipped s-value and v-value.
139
+ */
140
+ export function flipSignature(signature: Signature): Signature {
141
+ const { r, s, v } = signature;
142
+ const sig = new secp256k1.Signature(r.toBigInt(), s.toBigInt());
143
+ const flippedS = secp256k1.CURVE.n - sig.s;
144
+
145
+ return new Signature(r, Buffer32.fromBigInt(flippedS), flipV(v));
146
+ }
147
+
148
+ /**
149
+ * Normalizes an ECDSA signature.
150
+ * If the signature has a high s-value (s >= CURVE.n/2), it flips it to low s-value (CURVE.n - s), and flips v accordingly.
151
+ * If the signature uses a recovery bit of 0/1, it is converted to a v-value 27/28 for ecrecover.
152
+ * @remarks This does not handle post EIP155 tx signatures which embed the chain id in v. Use it only for feeding into ECRECOVER precompiles.
153
+ * @param signature - The signature to normalize.
154
+ */
155
+ export function normalizeSignature(signature: Signature): Signature {
156
+ const { r, s, v } = signature;
157
+ const sig = new secp256k1.Signature(r.toBigInt(), s.toBigInt());
158
+ if (sig.hasHighS()) {
159
+ const newV = flipV(v);
160
+ const newS = sig.normalizeS().s;
161
+ return new Signature(r, Buffer32.fromBigInt(newS), toVFromYParityOrV(newV));
162
+ }
163
+
164
+ return new Signature(r, s, toVFromYParityOrV(v));
165
+ }
166
+
167
+ /** Converts a yParityOrV value to a pre-EIP155 v-value 27-28. */
168
+ function toVFromYParityOrV(yParityOrV: number): number {
169
+ if (yParityOrV === 0 || yParityOrV === 1) {
170
+ return yParityOrV + 27;
171
+ } else if (yParityOrV === 27 || yParityOrV === 28) {
172
+ return yParityOrV;
173
+ } else {
174
+ throw new Secp256k1Error(`Invalid yParityOrV value ${yParityOrV}`);
175
+ }
176
+ }
177
+
178
+ /** Flips the recovery bit or v-value */
179
+ function flipV(v: number): number {
180
+ switch (v) {
181
+ case 27:
182
+ return 28;
183
+ case 28:
184
+ return 27;
185
+ case 0:
186
+ return 1;
187
+ case 1:
188
+ return 0;
189
+ default:
190
+ throw new Secp256k1Error(`Invalid v value ${v}`);
191
+ }
192
+ }
193
+
108
194
  /**
109
195
  * Recovers a public key from a hash and a signature.
110
196
  * @param hash - The hash to recover the public key from.
111
197
  * @param signature - The signature to recover the public key from.
112
198
  * @returns The public key.
113
199
  */
114
- export function recoverPublicKey(hash: Buffer32, signature: Signature): Buffer {
200
+ export function recoverPublicKey(hash: Buffer32, signature: Signature, opts: RecoveryOpts = {}): Buffer {
115
201
  const { r, s, v } = signature;
202
+ if (!opts.allowYParityAsV && v !== 27 && v !== 28) {
203
+ throw new Secp256k1Error(`Invalid v value ${v} (expected 27 or 28)`);
204
+ }
116
205
  const recoveryBit = toRecoveryBit(v);
117
206
  const sig = new secp256k1.Signature(r.toBigInt(), s.toBigInt()).addRecoveryBit(recoveryBit);
207
+ if (!opts.allowMalleable && sig.hasHighS()) {
208
+ throw new Secp256k1Error('Signature has high s-value (malleable signature)');
209
+ }
118
210
  const publicKey = sig.recoverPublicKey(hash.buffer).toHex(false);
119
211
  return Buffer.from(publicKey, 'hex');
120
212
  }
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable camelcase */
2
2
  import { default as hash } from 'hash.js';
3
3
 
4
- import { Fr } from '../../fields/fields.js';
4
+ import { Fr } from '../../curves/bn254/field.js';
5
5
  import { truncateAndPad } from '../../serialize/free_funcs.js';
6
6
  import { type Bufferable, serializeToBuffer } from '../../serialize/serialize.js';
7
7
  import type { Hasher } from '../../trees/hasher.js';
@@ -1,6 +1,6 @@
1
1
  import { default as hash } from 'hash.js';
2
2
 
3
- import { GrumpkinScalar } from '../../fields/fields.js';
3
+ import { GrumpkinScalar } from '../../curves/grumpkin/index.js';
4
4
  import { type Bufferable, serializeToBuffer } from '../../serialize/serialize.js';
5
5
 
6
6
  export const sha512 = (data: Buffer) => Buffer.from(hash.sha512().update(data).digest());
@@ -1,4 +1,4 @@
1
- import type { Fr } from '@aztec/foundation/fields';
1
+ import type { Fr } from '@aztec/foundation/curves/bn254';
2
2
 
3
3
  /**
4
4
  * Interface to represent a signature.
@@ -3,4 +3,4 @@ import { BarretenbergSync } from '@aztec/bb.js';
3
3
  export * from './poseidon/index.js';
4
4
  export * from './pedersen/index.js';
5
5
 
6
- await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
6
+ await BarretenbergSync.initSingleton();
@@ -1,6 +1,6 @@
1
1
  import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
- import { Fr } from '../../../fields/fields.js';
3
+ import { Fr } from '../../../curves/bn254/field.js';
4
4
  import { type Fieldable, serializeToFields } from '../../../serialize/serialize.js';
5
5
 
6
6
  /**
@@ -1,6 +1,6 @@
1
1
  import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
- import { Fr } from '../../../fields/fields.js';
3
+ import { Fr } from '../../../curves/bn254/field.js';
4
4
  import { type Fieldable, serializeToFields } from '../../../serialize/serialize.js';
5
5
 
6
6
  /**
@@ -34,15 +34,6 @@ export function poseidon2HashWithSeparator(input: Fieldable[], separator: number
34
34
  return Fr.fromBuffer(Buffer.from(response.hash));
35
35
  }
36
36
 
37
- export function poseidon2HashAccumulate(input: Fieldable[]): Fr {
38
- const inputFields = serializeToFields(input);
39
- const api = BarretenbergSync.getSingleton();
40
- const response = api.poseidon2HashAccumulate({
41
- inputs: inputFields.map(i => i.toBuffer()),
42
- });
43
- return Fr.fromBuffer(Buffer.from(response.hash));
44
- }
45
-
46
37
  /**
47
38
  * Runs a Poseidon2 permutation.
48
39
  * @param input the input state. Expected to be of size 4.
@@ -2,12 +2,12 @@
2
2
  import { bls12_381 } from '@noble/curves/bls12-381';
3
3
  import { inspect } from 'util';
4
4
 
5
- import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
6
- import { randomBytes } from '../crypto/random/index.js';
7
- import { hexSchemaFor } from '../schemas/utils.js';
8
- import { BufferReader } from '../serialize/buffer_reader.js';
9
- import { TypeRegistry } from '../serialize/type_registry.js';
10
- import { Fr } from './fields.js';
5
+ import { toBigIntBE, toBufferBE } from '../../bigint-buffer/index.js';
6
+ import { randomBytes } from '../../crypto/random/index.js';
7
+ import { hexSchemaFor } from '../../schemas/utils.js';
8
+ import { BufferReader } from '../../serialize/buffer_reader.js';
9
+ import { TypeRegistry } from '../../serialize/type_registry.js';
10
+ import { Fr } from '../bn254/field.js';
11
11
 
12
12
  /**
13
13
  * Represents a field derived from BLS12Field.
@@ -0,0 +1,2 @@
1
+ export * from './field.js';
2
+ export * from './point.js';
@@ -3,13 +3,13 @@ import type { ProjPointType } from '@noble/curves/abstract/weierstrass';
3
3
  import { bls12_381 } from '@noble/curves/bls12-381';
4
4
  import { inspect } from 'util';
5
5
 
6
- import { toBufferBE } from '../bigint-buffer/index.js';
7
- import { randomBoolean } from '../crypto/random/index.js';
8
- import { hexSchemaFor } from '../schemas/utils.js';
9
- import { BufferReader, serializeToBuffer } from '../serialize/index.js';
10
- import { bufferToHex, hexToBuffer } from '../string/index.js';
11
- import { BLS12Fq, BLS12Fr } from './bls12_fields.js';
12
- import { Fr } from './fields.js';
6
+ import { toBufferBE } from '../../bigint-buffer/index.js';
7
+ import { randomBoolean } from '../../crypto/random/index.js';
8
+ import { hexSchemaFor } from '../../schemas/utils.js';
9
+ import { BufferReader, serializeToBuffer } from '../../serialize/index.js';
10
+ import { bufferToHex, hexToBuffer } from '../../string/index.js';
11
+ import { Fr } from '../bn254/field.js';
12
+ import { BLS12Fq, BLS12Fr } from './field.js';
13
13
 
14
14
  /**
15
15
  * Represents a Point on an elliptic curve with x and y coordinates.
@@ -2,13 +2,11 @@ import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
3
  import { inspect } from 'util';
4
4
 
5
- import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
6
- import { randomBytes } from '../crypto/random/index.js';
7
- import { hexSchemaFor } from '../schemas/utils.js';
8
- import { BufferReader } from '../serialize/buffer_reader.js';
9
- import { TypeRegistry } from '../serialize/type_registry.js';
10
-
11
- const ZERO_BUFFER = Buffer.alloc(32);
5
+ import { toBigIntBE, toBufferBE } from '../../bigint-buffer/index.js';
6
+ import { randomBytes } from '../../crypto/random/index.js';
7
+ import { hexSchemaFor } from '../../schemas/utils.js';
8
+ import { BufferReader } from '../../serialize/buffer_reader.js';
9
+ import { TypeRegistry } from '../../serialize/type_registry.js';
12
10
 
13
11
  /* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
14
12
 
@@ -25,14 +23,12 @@ type DerivedField<T extends BaseField> = {
25
23
 
26
24
  /**
27
25
  * Base field class.
28
- * Conversions from Buffer to BigInt and vice-versa are not cheap.
29
- * We allow construction with either form and lazily convert to other as needed.
30
- * We only check we are within the field modulus when initializing with bigint.
26
+ * Uses bigint as the internal representation.
27
+ * Buffers are generated on demand from the bigint value.
31
28
  */
32
29
  abstract class BaseField {
33
30
  static SIZE_IN_BYTES = 32;
34
- private asBuffer?: Buffer;
35
- private asBigInt?: bigint;
31
+ private readonly asBigInt: bigint;
36
32
 
37
33
  /**
38
34
  * Return bigint representation.
@@ -52,53 +48,41 @@ abstract class BaseField {
52
48
  if (value.length > BaseField.SIZE_IN_BYTES) {
53
49
  throw new Error(`Value length ${value.length} exceeds ${BaseField.SIZE_IN_BYTES}`);
54
50
  }
55
- this.asBuffer =
56
- value.length === BaseField.SIZE_IN_BYTES
57
- ? value
58
- : Buffer.concat([Buffer.alloc(BaseField.SIZE_IN_BYTES - value.length), value]);
51
+ this.asBigInt = toBigIntBE(value);
59
52
  } else if (typeof value === 'bigint' || typeof value === 'number' || typeof value === 'boolean') {
60
53
  this.asBigInt = BigInt(value);
61
- if (this.asBigInt >= this.modulus()) {
62
- throw new Error(`Value 0x${this.asBigInt.toString(16)} is greater or equal to field modulus.`);
63
- } else if (this.asBigInt < 0n) {
64
- throw new Error(`Value 0x${this.asBigInt.toString(16)} is negative.`);
65
- }
66
54
  } else if (value instanceof BaseField) {
67
- this.asBuffer = value.asBuffer;
68
55
  this.asBigInt = value.asBigInt;
69
56
  } else {
70
57
  throw new Error(`Type '${typeof value}' with value '${value}' passed to BaseField ctor.`);
71
58
  }
59
+
60
+ if (this.asBigInt < 0n) {
61
+ throw new Error(`Value 0x${this.asBigInt.toString(16)} is negative.`);
62
+ } else if (this.asBigInt >= this.modulus()) {
63
+ throw new Error(`Value 0x${this.asBigInt.toString(16)} is greater or equal to field modulus.`);
64
+ }
72
65
  }
73
66
 
74
67
  protected abstract modulus(): bigint;
75
68
 
76
69
  /**
77
- * We return a copy of the Buffer to ensure this remains immutable.
70
+ * Converts the bigint to a Buffer.
78
71
  */
79
72
  toBuffer(): Buffer {
80
- if (!this.asBuffer) {
81
- this.asBuffer = toBufferBE(this.asBigInt!, 32);
82
- }
83
- return Buffer.from(this.asBuffer);
73
+ return toBufferBE(this.asBigInt, 32);
84
74
  }
85
75
 
86
76
  toString(): `0x${string}` {
87
- return `0x${this.toBuffer().toString('hex')}`;
77
+ return `0x${this.asBigInt.toString(16).padStart(64, '0')}`;
88
78
  }
89
79
 
90
80
  toBigInt(): bigint {
91
- if (this.asBigInt === undefined) {
92
- this.asBigInt = toBigIntBE(this.asBuffer!);
93
- if (this.asBigInt >= this.modulus()) {
94
- throw new Error(`Value 0x${this.asBigInt.toString(16)} is greater or equal to field modulus.`);
95
- }
96
- }
97
81
  return this.asBigInt;
98
82
  }
99
83
 
100
84
  toBool(): boolean {
101
- return Boolean(this.toBigInt());
85
+ return this.asBigInt !== 0n;
102
86
  }
103
87
 
104
88
  /**
@@ -106,11 +90,10 @@ abstract class BaseField {
106
90
  * Throws if the underlying value is greater than MAX_SAFE_INTEGER.
107
91
  */
108
92
  toNumber(): number {
109
- const value = this.toBigInt();
110
- if (value > Number.MAX_SAFE_INTEGER) {
111
- throw new Error(`Value ${value.toString(16)} greater than than max safe integer`);
93
+ if (this.asBigInt > Number.MAX_SAFE_INTEGER) {
94
+ throw new Error(`Value ${this.asBigInt.toString(16)} greater than than max safe integer`);
112
95
  }
113
- return Number(value);
96
+ return Number(this.asBigInt);
114
97
  }
115
98
 
116
99
  /**
@@ -118,8 +101,7 @@ abstract class BaseField {
118
101
  * May cause loss of precision if the underlying value is greater than MAX_SAFE_INTEGER.
119
102
  */
120
103
  toNumberUnsafe(): number {
121
- const value = this.toBigInt();
122
- return Number(value);
104
+ return Number(this.asBigInt);
123
105
  }
124
106
 
125
107
  toShortString(): string {
@@ -128,21 +110,24 @@ abstract class BaseField {
128
110
  }
129
111
 
130
112
  equals(rhs: BaseField): boolean {
131
- return this.toBuffer().equals(rhs.toBuffer());
113
+ return this.asBigInt === rhs.asBigInt;
132
114
  }
133
115
 
134
116
  lt(rhs: BaseField): boolean {
135
- return this.toBigInt() < rhs.toBigInt();
117
+ return this.asBigInt < rhs.asBigInt;
136
118
  }
137
119
 
138
120
  cmp(rhs: BaseField): -1 | 0 | 1 {
139
- const lhsBigInt = this.toBigInt();
140
- const rhsBigInt = rhs.toBigInt();
141
- return lhsBigInt === rhsBigInt ? 0 : lhsBigInt < rhsBigInt ? -1 : 1;
121
+ const rhsBigInt = rhs.asBigInt;
122
+ return this.asBigInt === rhsBigInt ? 0 : this.asBigInt < rhsBigInt ? -1 : 1;
123
+ }
124
+
125
+ static cmp(lhs: BaseField, rhs: BaseField): -1 | 0 | 1 {
126
+ return lhs.cmp(rhs);
142
127
  }
143
128
 
144
129
  isZero(): boolean {
145
- return this.toBuffer().equals(ZERO_BUFFER);
130
+ return this.asBigInt === 0n;
146
131
  }
147
132
 
148
133
  isEmpty(): boolean {
@@ -195,7 +180,7 @@ function fromHexString<T extends BaseField>(buf: string, f: DerivedField<T>) {
195
180
 
196
181
  const buffer = Buffer.from(checked.length % 2 === 1 ? '0' + checked : checked, 'hex');
197
182
 
198
- return new f(buffer);
183
+ return new f(toBigIntBE(buffer));
199
184
  }
200
185
 
201
186
  /** Branding to ensure fields are not interchangeable types. */
@@ -335,7 +320,23 @@ export class Fr extends BaseField {
335
320
  return this.toString();
336
321
  }
337
322
 
323
+ /**
324
+ * Creates an Fr instance from a plain object without Zod validation.
325
+ * This method is optimized for performance and skips validation, making it suitable
326
+ * for deserializing trusted data (e.g., from C++ via MessagePack).
327
+ * Handles buffers, strings, numbers, bigints, or existing instances.
328
+ * @param obj - Plain object, buffer, string, number, bigint, boolean, or Fr instance
329
+ * @returns An Fr instance
330
+ */
331
+ static fromPlainObject(obj: any): Fr {
332
+ if (obj instanceof Fr) {
333
+ return obj;
334
+ }
335
+ return new Fr(obj);
336
+ }
337
+
338
338
  static get schema() {
339
+ // Serialization from hex string.
339
340
  return hexSchemaFor(Fr);
340
341
  }
341
342
  }
@@ -495,14 +496,6 @@ function extendedEuclidean(a: bigint, modulus: bigint): [bigint, bigint, bigint]
495
496
  }
496
497
  }
497
498
 
498
- /**
499
- * GrumpkinScalar is an Fq.
500
- * @remarks Called GrumpkinScalar because it is used to represent elements in Grumpkin's scalar field as defined in
501
- * the Aztec Protocol Specs.
502
- */
503
- export type GrumpkinScalar = Fq;
504
- export const GrumpkinScalar = Fq;
505
-
506
499
  /** Wraps a function that returns a buffer so that all results are reduced into a field of the given type. */
507
500
  export function reduceFn<TInput, TField extends BaseField>(fn: (input: TInput) => Buffer, field: DerivedField<TField>) {
508
501
  return (input: TInput) => fromBufferReduce(fn(input), field);
@@ -0,0 +1,2 @@
1
+ export * from './field.js';
2
+ export * from './point.js';
@@ -0,0 +1,170 @@
1
+ import {
2
+ BN254_G1_GENERATOR,
3
+ BN254_G2_GENERATOR,
4
+ BarretenbergSync,
5
+ type Bn254G1Point as BbApiBn254G1Point,
6
+ type Bn254G2Point as BbApiBn254G2Point,
7
+ } from '@aztec/bb.js';
8
+
9
+ import { Fq, Fr } from './field.js';
10
+
11
+ /**
12
+ * BN254 G1 point using foundation field classes.
13
+ * Represents a point on the BN254 elliptic curve in affine coordinates.
14
+ */
15
+ export class Bn254G1Point {
16
+ constructor(
17
+ public readonly x: Fq,
18
+ public readonly y: Fq,
19
+ ) {}
20
+
21
+ private toBbApiPoint(): BbApiBn254G1Point {
22
+ return {
23
+ x: this.x.toBuffer(),
24
+ y: this.y.toBuffer(),
25
+ };
26
+ }
27
+
28
+ private static fromBbApiPoint(point: BbApiBn254G1Point): Bn254G1Point {
29
+ return new Bn254G1Point(Fq.fromBuffer(Buffer.from(point.x)), Fq.fromBuffer(Buffer.from(point.y)));
30
+ }
31
+
32
+ async isOnCurve(): Promise<boolean> {
33
+ await BarretenbergSync.initSingleton();
34
+ const api = BarretenbergSync.getSingleton();
35
+
36
+ const apiPoint = this.toBbApiPoint();
37
+ const response = api.bn254G1IsOnCurve({ point: apiPoint });
38
+ return response.isOnCurve;
39
+ }
40
+
41
+ /**
42
+ * Get the generator point for BN254 G1, or perform scalar multiplication.
43
+ * When called without arguments, returns the base generator point.
44
+ * When called with a scalar, returns scalar * generator (useful for public key derivation).
45
+ */
46
+ static async generator(scalar?: Fr): Promise<Bn254G1Point> {
47
+ if (!scalar) {
48
+ return new Bn254G1Point(
49
+ Fq.fromBuffer(Buffer.from(BN254_G1_GENERATOR.x)),
50
+ Fq.fromBuffer(Buffer.from(BN254_G1_GENERATOR.y)),
51
+ );
52
+ }
53
+
54
+ await BarretenbergSync.initSingleton();
55
+ const api = BarretenbergSync.getSingleton();
56
+
57
+ const response = api.bn254G1Mul({
58
+ point: BN254_G1_GENERATOR,
59
+ scalar: scalar.toBuffer(),
60
+ });
61
+
62
+ return Bn254G1Point.fromBbApiPoint(response.point);
63
+ }
64
+
65
+ /**
66
+ * Decompress a BN254 G1 point from compressed form (32 bytes).
67
+ * The compressed format encodes the x-coordinate and the sign bit of the y-coordinate
68
+ * in the most significant bit.
69
+ */
70
+ static async fromCompressed(compressed: Buffer): Promise<Bn254G1Point> {
71
+ if (compressed.length !== 32) {
72
+ throw new Error('Invalid compressed point length');
73
+ }
74
+ await BarretenbergSync.initSingleton();
75
+ const api = BarretenbergSync.getSingleton();
76
+
77
+ const response = api.bn254G1FromCompressed({
78
+ compressed: new Uint8Array(compressed),
79
+ });
80
+
81
+ return Bn254G1Point.fromBbApiPoint(response.point);
82
+ }
83
+
84
+ /**
85
+ * Compress this BN254 G1 point to 32 bytes.
86
+ * The compressed format encodes the x-coordinate and the sign bit of the y-coordinate
87
+ * in the most significant bit (bit 255).
88
+ */
89
+ compress(): Buffer {
90
+ const xBytes = this.x.toBuffer();
91
+ // Get the least significant bit of y to determine the sign
92
+ const yLsb = this.y.toBigInt() & 1n;
93
+ // If y is odd, set the most significant bit (bit 255) of the output
94
+ if (yLsb === 1n) {
95
+ xBytes[0] |= 0x80;
96
+ }
97
+ return xBytes;
98
+ }
99
+
100
+ equals(other: Bn254G1Point): boolean {
101
+ return this.x.equals(other.x) && this.y.equals(other.y);
102
+ }
103
+
104
+ toString(): string {
105
+ return `Bn254G1Point(x: ${this.x.toString()}, y: ${this.y.toString()})`;
106
+ }
107
+ }
108
+
109
+ /**
110
+ * BN254 G2 point using foundation field classes.
111
+ * Represents a point on the BN254 G2 curve (twist curve) in affine coordinates.
112
+ * G2 points use extension field coordinates (Fq2).
113
+ */
114
+ export class Bn254G2Point {
115
+ constructor(
116
+ public readonly x: [Fq, Fq],
117
+ public readonly y: [Fq, Fq],
118
+ ) {}
119
+
120
+ private toBbApiPoint(): BbApiBn254G2Point {
121
+ return {
122
+ x: [this.x[0].toBuffer(), this.x[1].toBuffer()],
123
+ y: [this.y[0].toBuffer(), this.y[1].toBuffer()],
124
+ };
125
+ }
126
+
127
+ private static fromBbApiPoint(point: BbApiBn254G2Point): Bn254G2Point {
128
+ return new Bn254G2Point(
129
+ [Fq.fromBuffer(Buffer.from(point.x[0])), Fq.fromBuffer(Buffer.from(point.x[1]))],
130
+ [Fq.fromBuffer(Buffer.from(point.y[0])), Fq.fromBuffer(Buffer.from(point.y[1]))],
131
+ );
132
+ }
133
+
134
+ /**
135
+ * Get the generator point for BN254 G2, or perform scalar multiplication.
136
+ * When called without arguments, returns the base generator point.
137
+ * When called with a scalar, returns scalar * generator.
138
+ */
139
+ static async generator(scalar?: Fr): Promise<Bn254G2Point> {
140
+ if (!scalar) {
141
+ return new Bn254G2Point(
142
+ [Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.x[0])), Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.x[1]))],
143
+ [Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.y[0])), Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.y[1]))],
144
+ );
145
+ }
146
+
147
+ await BarretenbergSync.initSingleton();
148
+ const api = BarretenbergSync.getSingleton();
149
+
150
+ const response = api.bn254G2Mul({
151
+ point: BN254_G2_GENERATOR as BbApiBn254G2Point,
152
+ scalar: scalar.toBuffer(),
153
+ });
154
+
155
+ return Bn254G2Point.fromBbApiPoint(response.point);
156
+ }
157
+
158
+ equals(other: Bn254G2Point): boolean {
159
+ return (
160
+ this.x[0].equals(other.x[0]) &&
161
+ this.x[1].equals(other.x[1]) &&
162
+ this.y[0].equals(other.y[0]) &&
163
+ this.y[1].equals(other.y[1])
164
+ );
165
+ }
166
+
167
+ toString(): string {
168
+ return `Bn254G2Point(x: (${this.x[0].toString()}, ${this.x[1].toString()}), y: (${this.y[0].toString()}, ${this.y[1].toString()}))`;
169
+ }
170
+ }
@@ -0,0 +1,11 @@
1
+ import { Fq } from '../bn254/field.js';
2
+
3
+ export * from './point.js';
4
+
5
+ /**
6
+ * GrumpkinScalar is an Fq.
7
+ * @remarks Called GrumpkinScalar because it is used to represent elements in Grumpkin's scalar field as defined in
8
+ * the Aztec Protocol Specs.
9
+ */
10
+ export type GrumpkinScalar = Fq;
11
+ export const GrumpkinScalar = Fq;