@aztec/foundation 0.0.0-test.1 → 0.0.1-commit.b655e406

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 (510) hide show
  1. package/dest/array/array.d.ts +5 -0
  2. package/dest/array/array.d.ts.map +1 -1
  3. package/dest/array/array.js +11 -0
  4. package/dest/async-pool/index.d.ts.map +1 -1
  5. package/dest/async-pool/index.js +1 -0
  6. package/dest/bigint/index.d.ts +7 -0
  7. package/dest/bigint/index.d.ts.map +1 -0
  8. package/dest/bigint/index.js +15 -0
  9. package/dest/bigint-buffer/index.d.ts +0 -2
  10. package/dest/bigint-buffer/index.d.ts.map +1 -1
  11. package/dest/buffer/buffer16.d.ts +80 -0
  12. package/dest/buffer/buffer16.d.ts.map +1 -0
  13. package/dest/buffer/buffer16.js +100 -0
  14. package/dest/buffer/buffer32.d.ts +3 -7
  15. package/dest/buffer/buffer32.d.ts.map +1 -1
  16. package/dest/buffer/buffer32.js +6 -6
  17. package/dest/buffer/index.d.ts +1 -0
  18. package/dest/buffer/index.d.ts.map +1 -1
  19. package/dest/buffer/index.js +1 -0
  20. package/dest/collection/array.d.ts +16 -3
  21. package/dest/collection/array.d.ts.map +1 -1
  22. package/dest/collection/array.js +57 -3
  23. package/dest/collection/object.d.ts +16 -0
  24. package/dest/collection/object.d.ts.map +1 -1
  25. package/dest/collection/object.js +25 -0
  26. package/dest/config/env_var.d.ts +1 -1
  27. package/dest/config/env_var.d.ts.map +1 -1
  28. package/dest/config/index.d.ts +48 -1
  29. package/dest/config/index.d.ts.map +1 -1
  30. package/dest/config/index.js +115 -7
  31. package/dest/config/network_config.d.ts +42 -0
  32. package/dest/config/network_config.d.ts.map +1 -0
  33. package/dest/config/network_config.js +9 -0
  34. package/dest/config/network_name.d.ts +3 -0
  35. package/dest/config/network_name.d.ts.map +1 -0
  36. package/dest/config/network_name.js +19 -0
  37. package/dest/config/secret_value.d.ts +28 -0
  38. package/dest/config/secret_value.d.ts.map +1 -0
  39. package/dest/config/secret_value.js +34 -0
  40. package/dest/crypto/aes128/index.d.ts +2 -4
  41. package/dest/crypto/aes128/index.d.ts.map +1 -1
  42. package/dest/crypto/aes128/index.js +23 -6
  43. package/dest/crypto/bls/bn254_keystore.d.ts +296 -0
  44. package/dest/crypto/bls/bn254_keystore.d.ts.map +1 -0
  45. package/dest/crypto/bls/bn254_keystore.js +218 -0
  46. package/dest/crypto/bls/index.d.ts +13 -0
  47. package/dest/crypto/bls/index.d.ts.map +1 -0
  48. package/dest/crypto/bls/index.js +87 -0
  49. package/dest/crypto/bn254/bn254_utils.d.ts +39 -0
  50. package/dest/crypto/bn254/bn254_utils.d.ts.map +1 -0
  51. package/dest/crypto/bn254/bn254_utils.js +56 -0
  52. package/dest/crypto/bn254/index.d.ts +55 -0
  53. package/dest/crypto/bn254/index.d.ts.map +1 -0
  54. package/dest/crypto/bn254/index.js +142 -0
  55. package/dest/crypto/ecdsa/index.d.ts +2 -2
  56. package/dest/crypto/ecdsa/index.d.ts.map +1 -1
  57. package/dest/crypto/ecdsa/index.js +70 -48
  58. package/dest/crypto/ecdsa/signature.d.ts +1 -3
  59. package/dest/crypto/ecdsa/signature.d.ts.map +1 -1
  60. package/dest/crypto/grumpkin/index.d.ts +6 -13
  61. package/dest/crypto/grumpkin/index.d.ts.map +1 -1
  62. package/dest/crypto/grumpkin/index.js +69 -54
  63. package/dest/crypto/index.d.ts +2 -0
  64. package/dest/crypto/index.d.ts.map +1 -1
  65. package/dest/crypto/index.js +2 -0
  66. package/dest/crypto/keccak/index.d.ts +2 -4
  67. package/dest/crypto/keccak/index.d.ts.map +1 -1
  68. package/dest/crypto/keys/index.d.ts +0 -2
  69. package/dest/crypto/keys/index.d.ts.map +1 -1
  70. package/dest/crypto/keys/index.js +9 -4
  71. package/dest/crypto/pedersen/pedersen.noble.d.ts +2 -4
  72. package/dest/crypto/pedersen/pedersen.noble.d.ts.map +1 -1
  73. package/dest/crypto/pedersen/pedersen.wasm.d.ts +2 -4
  74. package/dest/crypto/pedersen/pedersen.wasm.d.ts.map +1 -1
  75. package/dest/crypto/pedersen/pedersen.wasm.js +29 -13
  76. package/dest/crypto/poseidon/index.d.ts +0 -2
  77. package/dest/crypto/poseidon/index.d.ts.map +1 -1
  78. package/dest/crypto/poseidon/index.js +42 -17
  79. package/dest/crypto/random/index.d.ts +1 -3
  80. package/dest/crypto/random/index.d.ts.map +1 -1
  81. package/dest/crypto/random/index.js +2 -34
  82. package/dest/crypto/random/randomness_singleton.d.ts +0 -2
  83. package/dest/crypto/random/randomness_singleton.d.ts.map +1 -1
  84. package/dest/crypto/schnorr/index.d.ts.map +1 -1
  85. package/dest/crypto/schnorr/index.js +35 -37
  86. package/dest/crypto/schnorr/signature.d.ts +3 -5
  87. package/dest/crypto/schnorr/signature.d.ts.map +1 -1
  88. package/dest/crypto/secp256k1/index.d.ts +4 -6
  89. package/dest/crypto/secp256k1/index.d.ts.map +1 -1
  90. package/dest/crypto/secp256k1/index.js +29 -18
  91. package/dest/crypto/secp256k1-signer/utils.d.ts +55 -4
  92. package/dest/crypto/secp256k1-signer/utils.d.ts.map +1 -1
  93. package/dest/crypto/secp256k1-signer/utils.js +92 -6
  94. package/dest/crypto/serialize.d.ts +4 -6
  95. package/dest/crypto/serialize.d.ts.map +1 -1
  96. package/dest/crypto/sha256/index.d.ts +21 -4
  97. package/dest/crypto/sha256/index.d.ts.map +1 -1
  98. package/dest/crypto/sha256/index.js +42 -0
  99. package/dest/crypto/sha512/index.d.ts +1 -3
  100. package/dest/crypto/sha512/index.d.ts.map +1 -1
  101. package/dest/crypto/signature/index.d.ts +0 -2
  102. package/dest/crypto/signature/index.d.ts.map +1 -1
  103. package/dest/crypto/sync/index.js +3 -1
  104. package/dest/crypto/sync/pedersen/index.d.ts +2 -4
  105. package/dest/crypto/sync/pedersen/index.d.ts.map +1 -1
  106. package/dest/crypto/sync/pedersen/index.js +17 -10
  107. package/dest/crypto/sync/poseidon/index.d.ts +0 -2
  108. package/dest/crypto/sync/poseidon/index.d.ts.map +1 -1
  109. package/dest/crypto/sync/poseidon/index.js +27 -12
  110. package/dest/decorators/memoize.d.ts.map +1 -1
  111. package/dest/eth-address/index.d.ts +6 -7
  112. package/dest/eth-address/index.d.ts.map +1 -1
  113. package/dest/eth-address/index.js +5 -0
  114. package/dest/eth-signature/eth_signature.d.ts +16 -14
  115. package/dest/eth-signature/eth_signature.d.ts.map +1 -1
  116. package/dest/eth-signature/eth_signature.js +31 -17
  117. package/dest/fields/bls12_fields.d.ts +148 -0
  118. package/dest/fields/bls12_fields.d.ts.map +1 -0
  119. package/dest/fields/bls12_fields.js +359 -0
  120. package/dest/fields/bls12_point.d.ts +229 -0
  121. package/dest/fields/bls12_point.d.ts.map +1 -0
  122. package/dest/fields/bls12_point.js +398 -0
  123. package/dest/fields/coordinate.d.ts +0 -2
  124. package/dest/fields/coordinate.d.ts.map +1 -1
  125. package/dest/fields/fields.d.ts +6 -3
  126. package/dest/fields/fields.d.ts.map +1 -1
  127. package/dest/fields/fields.js +43 -15
  128. package/dest/fields/index.d.ts +2 -0
  129. package/dest/fields/index.d.ts.map +1 -1
  130. package/dest/fields/index.js +2 -0
  131. package/dest/fields/point.d.ts +2 -4
  132. package/dest/fields/point.d.ts.map +1 -1
  133. package/dest/fields/point.js +3 -3
  134. package/dest/fs/index.d.ts +1 -0
  135. package/dest/fs/index.d.ts.map +1 -1
  136. package/dest/fs/index.js +1 -0
  137. package/dest/fs/try_rm_dir.d.ts +3 -0
  138. package/dest/fs/try_rm_dir.d.ts.map +1 -0
  139. package/dest/fs/try_rm_dir.js +16 -0
  140. package/dest/index.d.ts +1 -2
  141. package/dest/index.d.ts.map +1 -1
  142. package/dest/index.js +1 -2
  143. package/dest/iterable/filter.js +1 -1
  144. package/dest/iterable/map.js +1 -1
  145. package/dest/jest/env.js +48 -0
  146. package/dest/jest/setupAfterEnv.js +3 -0
  147. package/dest/json-rpc/client/fetch.d.ts +2 -2
  148. package/dest/json-rpc/client/fetch.d.ts.map +1 -1
  149. package/dest/json-rpc/client/fetch.js +16 -30
  150. package/dest/json-rpc/client/safe_json_rpc_client.d.ts +9 -3
  151. package/dest/json-rpc/client/safe_json_rpc_client.d.ts.map +1 -1
  152. package/dest/json-rpc/client/safe_json_rpc_client.js +173 -13
  153. package/dest/json-rpc/client/undici.d.ts.map +1 -1
  154. package/dest/json-rpc/client/undici.js +6 -7
  155. package/dest/json-rpc/convert.d.ts +2 -9
  156. package/dest/json-rpc/convert.d.ts.map +1 -1
  157. package/dest/json-rpc/convert.js +1 -9
  158. package/dest/json-rpc/errors.d.ts +4 -0
  159. package/dest/json-rpc/errors.d.ts.map +1 -0
  160. package/dest/json-rpc/errors.js +6 -0
  161. package/dest/json-rpc/fixtures/class_a.d.ts +5 -3
  162. package/dest/json-rpc/fixtures/class_a.d.ts.map +1 -1
  163. package/dest/json-rpc/fixtures/class_b.d.ts +5 -3
  164. package/dest/json-rpc/fixtures/class_b.d.ts.map +1 -1
  165. package/dest/json-rpc/index.d.ts +2 -1
  166. package/dest/json-rpc/index.d.ts.map +1 -1
  167. package/dest/json-rpc/index.js +2 -1
  168. package/dest/json-rpc/js_utils.d.ts.map +1 -1
  169. package/dest/json-rpc/server/safe_json_rpc_server.d.ts +21 -22
  170. package/dest/json-rpc/server/safe_json_rpc_server.d.ts.map +1 -1
  171. package/dest/json-rpc/server/safe_json_rpc_server.js +161 -56
  172. package/dest/json-rpc/test/integration.d.ts +1 -2
  173. package/dest/json-rpc/test/integration.d.ts.map +1 -1
  174. package/dest/log/console.d.ts.map +1 -1
  175. package/dest/log/gcloud-logger-config.d.ts +0 -1
  176. package/dest/log/gcloud-logger-config.d.ts.map +1 -1
  177. package/dest/log/gcloud-logger-config.js +1 -9
  178. package/dest/log/index.d.ts +4 -4
  179. package/dest/log/index.d.ts.map +1 -1
  180. package/dest/log/index.js +4 -4
  181. package/dest/log/libp2p_logger.js +13 -2
  182. package/dest/log/log_fn.d.ts +1 -1
  183. package/dest/log/log_fn.d.ts.map +1 -1
  184. package/dest/log/noir_debug_log_util.d.ts +14 -0
  185. package/dest/log/noir_debug_log_util.d.ts.map +1 -0
  186. package/dest/log/noir_debug_log_util.js +14 -0
  187. package/dest/log/pino-logger.d.ts +2 -2
  188. package/dest/log/pino-logger.d.ts.map +1 -1
  189. package/dest/log/pino-logger.js +28 -2
  190. package/dest/message/index.d.ts.map +1 -1
  191. package/dest/mutex/mutex_database.d.ts.map +1 -1
  192. package/dest/noir/noir_package_config.d.ts +4 -4
  193. package/dest/number/index.d.ts +3 -0
  194. package/dest/number/index.d.ts.map +1 -0
  195. package/dest/number/index.js +12 -0
  196. package/dest/profiler/index.d.ts +2 -0
  197. package/dest/profiler/index.d.ts.map +1 -0
  198. package/dest/profiler/index.js +1 -0
  199. package/dest/profiler/profiler.d.ts +8 -0
  200. package/dest/profiler/profiler.d.ts.map +1 -0
  201. package/dest/profiler/profiler.js +97 -0
  202. package/dest/promise/running-promise.d.ts +1 -1
  203. package/dest/promise/running-promise.d.ts.map +1 -1
  204. package/dest/promise/running-promise.js +2 -2
  205. package/dest/queue/batch_queue.d.ts +1 -1
  206. package/dest/queue/batch_queue.js +1 -1
  207. package/dest/queue/bounded_serial_queue.d.ts.map +1 -1
  208. package/dest/queue/serial_queue.d.ts +2 -2
  209. package/dest/queue/serial_queue.d.ts.map +1 -1
  210. package/dest/queue/serial_queue.js +7 -5
  211. package/dest/retry/index.d.ts +3 -2
  212. package/dest/retry/index.d.ts.map +1 -1
  213. package/dest/retry/index.js +2 -2
  214. package/dest/schemas/api.d.ts.map +1 -1
  215. package/dest/schemas/parse.js +1 -1
  216. package/dest/schemas/schemas.d.ts +9 -6
  217. package/dest/schemas/schemas.d.ts.map +1 -1
  218. package/dest/schemas/schemas.js +15 -1
  219. package/dest/schemas/utils.d.ts +1 -3
  220. package/dest/schemas/utils.d.ts.map +1 -1
  221. package/dest/serialize/buffer_reader.d.ts +27 -2
  222. package/dest/serialize/buffer_reader.d.ts.map +1 -1
  223. package/dest/serialize/buffer_reader.js +44 -2
  224. package/dest/serialize/field_reader.d.ts +10 -1
  225. package/dest/serialize/field_reader.d.ts.map +1 -1
  226. package/dest/serialize/field_reader.js +18 -1
  227. package/dest/serialize/free_funcs.d.ts +23 -21
  228. package/dest/serialize/free_funcs.d.ts.map +1 -1
  229. package/dest/serialize/free_funcs.js +21 -27
  230. package/dest/serialize/serialize.d.ts +4 -5
  231. package/dest/serialize/serialize.d.ts.map +1 -1
  232. package/dest/string/index.d.ts +7 -2
  233. package/dest/string/index.d.ts.map +1 -1
  234. package/dest/string/index.js +27 -1
  235. package/dest/testing/files/index.d.ts +0 -2
  236. package/dest/testing/files/index.d.ts.map +1 -1
  237. package/dest/testing/files/index.js +2 -2
  238. package/dest/testing/formatting.d.ts +4 -0
  239. package/dest/testing/formatting.d.ts.map +1 -0
  240. package/dest/testing/formatting.js +3 -0
  241. package/dest/testing/index.d.ts +1 -0
  242. package/dest/testing/index.d.ts.map +1 -1
  243. package/dest/testing/index.js +1 -0
  244. package/dest/timer/date.d.ts +2 -1
  245. package/dest/timer/date.d.ts.map +1 -1
  246. package/dest/timer/date.js +5 -3
  247. package/dest/timer/index.d.ts +1 -1
  248. package/dest/timer/index.d.ts.map +1 -1
  249. package/dest/timer/index.js +1 -1
  250. package/dest/timer/timeout.d.ts +18 -5
  251. package/dest/timer/timeout.d.ts.map +1 -1
  252. package/dest/timer/timeout.js +43 -22
  253. package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +1 -1
  254. package/dest/transport/index.d.ts +0 -1
  255. package/dest/transport/index.d.ts.map +1 -1
  256. package/dest/transport/index.js +0 -1
  257. package/dest/transport/interface/connector.d.ts.map +1 -1
  258. package/dest/transport/interface/listener.d.ts +0 -1
  259. package/dest/transport/interface/listener.d.ts.map +1 -1
  260. package/dest/transport/interface/socket.d.ts.map +1 -1
  261. package/dest/transport/node/node_connector.d.ts +0 -1
  262. package/dest/transport/node/node_connector.d.ts.map +1 -1
  263. package/dest/transport/node/node_connector_socket.d.ts +0 -1
  264. package/dest/transport/node/node_connector_socket.d.ts.map +1 -1
  265. package/dest/transport/node/node_listener.d.ts +0 -1
  266. package/dest/transport/node/node_listener.d.ts.map +1 -1
  267. package/dest/transport/node/node_listener_socket.d.ts +0 -1
  268. package/dest/transport/node/node_listener_socket.d.ts.map +1 -1
  269. package/dest/transport/transport_client.d.ts +0 -1
  270. package/dest/transport/transport_client.d.ts.map +1 -1
  271. package/dest/transport/transport_server.d.ts.map +1 -1
  272. package/dest/trees/hasher.d.ts +4 -6
  273. package/dest/trees/hasher.d.ts.map +1 -1
  274. package/dest/trees/index.d.ts +1 -0
  275. package/dest/trees/index.d.ts.map +1 -1
  276. package/dest/trees/index.js +1 -0
  277. package/dest/trees/indexed_merkle_tree.d.ts +0 -2
  278. package/dest/trees/indexed_merkle_tree.d.ts.map +1 -1
  279. package/dest/trees/indexed_merkle_tree_calculator.d.ts +1 -3
  280. package/dest/trees/indexed_merkle_tree_calculator.d.ts.map +1 -1
  281. package/dest/trees/indexed_tree_leaf.d.ts +0 -2
  282. package/dest/trees/indexed_tree_leaf.d.ts.map +1 -1
  283. package/dest/trees/membership_witness.d.ts +19 -3
  284. package/dest/trees/membership_witness.d.ts.map +1 -1
  285. package/dest/trees/membership_witness.js +13 -0
  286. package/dest/trees/merkle_tree.d.ts +0 -2
  287. package/dest/trees/merkle_tree.d.ts.map +1 -1
  288. package/dest/trees/merkle_tree_calculator.d.ts +11 -3
  289. package/dest/trees/merkle_tree_calculator.d.ts.map +1 -1
  290. package/dest/trees/merkle_tree_calculator.js +31 -1
  291. package/dest/trees/sibling_path.d.ts +6 -8
  292. package/dest/trees/sibling_path.d.ts.map +1 -1
  293. package/dest/trees/unbalanced_merkle_tree.d.ts +19 -8
  294. package/dest/trees/unbalanced_merkle_tree.d.ts.map +1 -1
  295. package/dest/trees/unbalanced_merkle_tree.js +49 -50
  296. package/dest/trees/unbalanced_merkle_tree_calculator.d.ts +48 -0
  297. package/dest/trees/unbalanced_merkle_tree_calculator.d.ts.map +1 -0
  298. package/dest/trees/unbalanced_merkle_tree_calculator.js +162 -0
  299. package/dest/trees/unbalanced_tree_store.d.ts +1 -0
  300. package/dest/trees/unbalanced_tree_store.d.ts.map +1 -1
  301. package/dest/trees/unbalanced_tree_store.js +6 -0
  302. package/dest/types/index.d.ts +25 -2
  303. package/dest/types/index.d.ts.map +1 -1
  304. package/dest/url/index.d.ts.map +1 -1
  305. package/dest/url/index.js +1 -1
  306. package/package.json +35 -41
  307. package/src/array/array.ts +13 -0
  308. package/src/async-pool/index.ts +1 -0
  309. package/src/bigint/index.ts +20 -0
  310. package/src/buffer/buffer16.ts +133 -0
  311. package/src/buffer/buffer32.ts +8 -6
  312. package/src/buffer/index.ts +1 -0
  313. package/src/collection/array.ts +66 -4
  314. package/src/collection/object.ts +37 -0
  315. package/src/config/env_var.ts +142 -52
  316. package/src/config/index.ts +191 -8
  317. package/src/config/network_config.ts +16 -0
  318. package/src/config/network_name.ts +28 -0
  319. package/src/config/secret_value.ts +49 -0
  320. package/src/crypto/aes128/index.ts +19 -10
  321. package/src/crypto/bls/bn254_keystore.ts +287 -0
  322. package/src/crypto/bls/index.ts +77 -0
  323. package/src/crypto/bn254/bn254_utils.ts +64 -0
  324. package/src/crypto/bn254/index.ts +170 -0
  325. package/src/crypto/ecdsa/index.ts +41 -23
  326. package/src/crypto/grumpkin/index.ts +35 -45
  327. package/src/crypto/index.ts +2 -0
  328. package/src/crypto/keys/index.ts +5 -5
  329. package/src/crypto/pedersen/pedersen.wasm.ts +22 -18
  330. package/src/crypto/poseidon/index.ts +32 -24
  331. package/src/crypto/random/index.ts +2 -40
  332. package/src/crypto/schnorr/index.ts +20 -17
  333. package/src/crypto/secp256k1/index.ts +15 -11
  334. package/src/crypto/secp256k1-signer/utils.ts +120 -7
  335. package/src/crypto/sha256/index.ts +47 -0
  336. package/src/crypto/sync/index.ts +1 -1
  337. package/src/crypto/sync/pedersen/index.ts +16 -15
  338. package/src/crypto/sync/poseidon/index.ts +27 -22
  339. package/src/eth-address/index.ts +9 -2
  340. package/src/eth-signature/eth_signature.ts +44 -22
  341. package/src/fields/bls12_fields.ts +463 -0
  342. package/src/fields/bls12_point.ts +450 -0
  343. package/src/fields/fields.ts +37 -9
  344. package/src/fields/index.ts +2 -0
  345. package/src/fields/point.ts +1 -3
  346. package/src/fs/index.ts +1 -0
  347. package/src/fs/try_rm_dir.ts +15 -0
  348. package/src/index.ts +1 -2
  349. package/src/iterable/filter.ts +1 -1
  350. package/src/iterable/map.ts +1 -1
  351. package/src/jest/env.mjs +52 -0
  352. package/src/jest/setupAfterEnv.mjs +3 -0
  353. package/src/json-rpc/client/fetch.ts +14 -33
  354. package/src/json-rpc/client/safe_json_rpc_client.ts +226 -13
  355. package/src/json-rpc/client/undici.ts +8 -15
  356. package/src/json-rpc/convert.ts +3 -12
  357. package/src/json-rpc/errors.ts +6 -0
  358. package/src/json-rpc/fixtures/class_a.ts +4 -1
  359. package/src/json-rpc/fixtures/class_b.ts +4 -1
  360. package/src/json-rpc/index.ts +2 -1
  361. package/src/json-rpc/server/safe_json_rpc_server.ts +141 -48
  362. package/src/json-rpc/test/integration.ts +1 -1
  363. package/src/log/console.ts +4 -1
  364. package/src/log/gcloud-logger-config.ts +3 -7
  365. package/src/log/index.ts +4 -4
  366. package/src/log/libp2p_logger.ts +21 -2
  367. package/src/log/log_fn.ts +1 -1
  368. package/src/log/noir_debug_log_util.ts +21 -0
  369. package/src/log/pino-logger.ts +33 -2
  370. package/src/message/index.ts +5 -1
  371. package/src/mutex/mutex_database.ts +2 -3
  372. package/src/number/index.ts +14 -0
  373. package/src/profiler/index.ts +1 -0
  374. package/src/profiler/profiler.ts +125 -0
  375. package/src/promise/running-promise.ts +2 -2
  376. package/src/queue/batch_queue.ts +1 -1
  377. package/src/queue/bounded_serial_queue.ts +4 -1
  378. package/src/queue/serial_queue.ts +5 -5
  379. package/src/retry/index.ts +10 -5
  380. package/src/schemas/api.ts +4 -4
  381. package/src/schemas/parse.ts +1 -1
  382. package/src/schemas/schemas.ts +19 -1
  383. package/src/serialize/buffer_reader.ts +60 -3
  384. package/src/serialize/field_reader.ts +27 -3
  385. package/src/serialize/free_funcs.ts +25 -31
  386. package/src/serialize/serialize.ts +2 -0
  387. package/src/string/index.ts +36 -1
  388. package/src/testing/files/index.ts +6 -2
  389. package/src/testing/formatting.ts +3 -0
  390. package/src/testing/index.ts +1 -0
  391. package/src/timer/date.ts +9 -3
  392. package/src/timer/index.ts +1 -1
  393. package/src/timer/timeout.ts +54 -19
  394. package/src/transport/index.ts +0 -1
  395. package/src/transport/interface/connector.ts +0 -1
  396. package/src/transport/interface/listener.ts +2 -3
  397. package/src/transport/interface/socket.ts +2 -3
  398. package/src/transport/transport_client.ts +3 -4
  399. package/src/transport/transport_server.ts +4 -1
  400. package/src/trees/hasher.ts +4 -4
  401. package/src/trees/index.ts +1 -0
  402. package/src/trees/indexed_merkle_tree.ts +5 -1
  403. package/src/trees/indexed_merkle_tree_calculator.ts +2 -2
  404. package/src/trees/membership_witness.ts +13 -0
  405. package/src/trees/merkle_tree.ts +4 -1
  406. package/src/trees/merkle_tree_calculator.ts +43 -5
  407. package/src/trees/unbalanced_merkle_tree.ts +50 -42
  408. package/src/trees/unbalanced_merkle_tree_calculator.ts +194 -0
  409. package/src/trees/unbalanced_tree_store.ts +5 -1
  410. package/src/types/index.ts +25 -4
  411. package/src/url/index.ts +0 -1
  412. package/dest/crypto/pedersen/pedersen.elliptic.d.ts +0 -13
  413. package/dest/crypto/pedersen/pedersen.elliptic.d.ts.map +0 -1
  414. package/dest/crypto/pedersen/pedersen.elliptic.js +0 -582
  415. package/dest/log/debug.d.ts +0 -56
  416. package/dest/log/debug.d.ts.map +0 -1
  417. package/dest/log/debug.js +0 -75
  418. package/dest/log/log_history.d.ts +0 -31
  419. package/dest/log/log_history.d.ts.map +0 -1
  420. package/dest/log/log_history.js +0 -38
  421. package/dest/transport/browser/index.d.ts +0 -5
  422. package/dest/transport/browser/index.d.ts.map +0 -1
  423. package/dest/transport/browser/index.js +0 -4
  424. package/dest/transport/browser/message_port_socket.d.ts +0 -37
  425. package/dest/transport/browser/message_port_socket.d.ts.map +0 -1
  426. package/dest/transport/browser/message_port_socket.js +0 -42
  427. package/dest/transport/browser/shared_worker_connector.d.ts +0 -19
  428. package/dest/transport/browser/shared_worker_connector.d.ts.map +0 -1
  429. package/dest/transport/browser/shared_worker_connector.js +0 -19
  430. package/dest/transport/browser/shared_worker_listener.d.ts +0 -38
  431. package/dest/transport/browser/shared_worker_listener.d.ts.map +0 -1
  432. package/dest/transport/browser/shared_worker_listener.js +0 -33
  433. package/dest/transport/browser/worker_connector.d.ts +0 -26
  434. package/dest/transport/browser/worker_connector.d.ts.map +0 -1
  435. package/dest/transport/browser/worker_connector.js +0 -30
  436. package/dest/transport/browser/worker_listener.d.ts +0 -39
  437. package/dest/transport/browser/worker_listener.d.ts.map +0 -1
  438. package/dest/transport/browser/worker_listener.js +0 -35
  439. package/dest/wasm/empty_wasi_sdk.d.ts +0 -130
  440. package/dest/wasm/empty_wasi_sdk.d.ts.map +0 -1
  441. package/dest/wasm/empty_wasi_sdk.js +0 -148
  442. package/dest/wasm/index.d.ts +0 -2
  443. package/dest/wasm/index.d.ts.map +0 -1
  444. package/dest/wasm/index.js +0 -1
  445. package/dest/wasm/wasm_module.d.ts +0 -136
  446. package/dest/wasm/wasm_module.d.ts.map +0 -1
  447. package/dest/wasm/wasm_module.js +0 -196
  448. package/dest/worker/browser/index.d.ts +0 -3
  449. package/dest/worker/browser/index.d.ts.map +0 -1
  450. package/dest/worker/browser/index.js +0 -2
  451. package/dest/worker/browser/start_web_module.d.ts +0 -7
  452. package/dest/worker/browser/start_web_module.d.ts.map +0 -1
  453. package/dest/worker/browser/start_web_module.js +0 -27
  454. package/dest/worker/browser/web_data_store.d.ts +0 -24
  455. package/dest/worker/browser/web_data_store.d.ts.map +0 -1
  456. package/dest/worker/browser/web_data_store.js +0 -29
  457. package/dest/worker/browser/web_worker.d.ts +0 -10
  458. package/dest/worker/browser/web_worker.d.ts.map +0 -1
  459. package/dest/worker/browser/web_worker.js +0 -25
  460. package/dest/worker/data_store.d.ts +0 -21
  461. package/dest/worker/data_store.d.ts.map +0 -1
  462. package/dest/worker/data_store.js +0 -3
  463. package/dest/worker/index.d.ts +0 -3
  464. package/dest/worker/index.d.ts.map +0 -1
  465. package/dest/worker/index.js +0 -1
  466. package/dest/worker/node/index.d.ts +0 -3
  467. package/dest/worker/node/index.d.ts.map +0 -1
  468. package/dest/worker/node/index.js +0 -2
  469. package/dest/worker/node/node_data_store.d.ts +0 -13
  470. package/dest/worker/node/node_data_store.d.ts.map +0 -1
  471. package/dest/worker/node/node_data_store.js +0 -20
  472. package/dest/worker/node/node_worker.d.ts +0 -6
  473. package/dest/worker/node/node_worker.d.ts.map +0 -1
  474. package/dest/worker/node/node_worker.js +0 -22
  475. package/dest/worker/node/start_node_module.d.ts +0 -7
  476. package/dest/worker/node/start_node_module.d.ts.map +0 -1
  477. package/dest/worker/node/start_node_module.js +0 -31
  478. package/dest/worker/wasm_worker.d.ts +0 -9
  479. package/dest/worker/wasm_worker.d.ts.map +0 -1
  480. package/dest/worker/wasm_worker.js +0 -3
  481. package/dest/worker/worker_pool.d.ts +0 -40
  482. package/dest/worker/worker_pool.d.ts.map +0 -1
  483. package/dest/worker/worker_pool.js +0 -51
  484. package/src/crypto/pedersen/pedersen.elliptic.ts +0 -584
  485. package/src/log/debug.ts +0 -104
  486. package/src/log/log_history.ts +0 -44
  487. package/src/transport/browser/index.ts +0 -4
  488. package/src/transport/browser/message_port_socket.ts +0 -48
  489. package/src/transport/browser/shared_worker_connector.ts +0 -21
  490. package/src/transport/browser/shared_worker_listener.ts +0 -53
  491. package/src/transport/browser/worker_connector.ts +0 -30
  492. package/src/transport/browser/worker_listener.ts +0 -54
  493. package/src/wasm/README.md +0 -6
  494. package/src/wasm/empty_wasi_sdk.ts +0 -166
  495. package/src/wasm/fixtures/gcd.wasm +0 -0
  496. package/src/wasm/fixtures/gcd.wat +0 -27
  497. package/src/wasm/index.ts +0 -1
  498. package/src/wasm/wasm_module.ts +0 -260
  499. package/src/worker/browser/index.ts +0 -2
  500. package/src/worker/browser/start_web_module.ts +0 -24
  501. package/src/worker/browser/web_data_store.ts +0 -38
  502. package/src/worker/browser/web_worker.ts +0 -25
  503. package/src/worker/data_store.ts +0 -19
  504. package/src/worker/index.ts +0 -2
  505. package/src/worker/node/index.ts +0 -2
  506. package/src/worker/node/node_data_store.ts +0 -27
  507. package/src/worker/node/node_worker.ts +0 -23
  508. package/src/worker/node/start_node_module.ts +0 -30
  509. package/src/worker/wasm_worker.ts +0 -7
  510. package/src/worker/worker_pool.ts +0 -73
@@ -1,4 +1,4 @@
1
- import { BarretenbergSync, Fr as FrBarretenberg } from '@aztec/bb.js';
1
+ import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
3
  import { Fr } from '../../fields/fields.js';
4
4
  import { type Fieldable, serializeToFields } from '../../serialize/serialize.js';
@@ -10,11 +10,12 @@ import { type Fieldable, serializeToFields } from '../../serialize/serialize.js'
10
10
  */
11
11
  export async function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
12
12
  const inputFields = serializeToFields(input);
13
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
14
- const hash = api.poseidon2Hash(
15
- inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
16
- );
17
- return Fr.fromBuffer(Buffer.from(hash.toBuffer()));
13
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
14
+ const api = BarretenbergSync.getSingleton();
15
+ const response = api.poseidon2Hash({
16
+ inputs: inputFields.map(i => i.toBuffer()),
17
+ });
18
+ return Fr.fromBuffer(Buffer.from(response.hash));
18
19
  }
19
20
 
20
21
  /**
@@ -26,19 +27,22 @@ export async function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
26
27
  export async function poseidon2HashWithSeparator(input: Fieldable[], separator: number): Promise<Fr> {
27
28
  const inputFields = serializeToFields(input);
28
29
  inputFields.unshift(new Fr(separator));
29
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
30
-
31
- const hash = api.poseidon2Hash(
32
- inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
33
- );
34
- return Fr.fromBuffer(Buffer.from(hash.toBuffer()));
30
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
31
+ const api = BarretenbergSync.getSingleton();
32
+ const response = api.poseidon2Hash({
33
+ inputs: inputFields.map(i => i.toBuffer()),
34
+ });
35
+ return Fr.fromBuffer(Buffer.from(response.hash));
35
36
  }
36
37
 
37
38
  export async function poseidon2HashAccumulate(input: Fieldable[]): Promise<Fr> {
38
39
  const inputFields = serializeToFields(input);
39
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
40
- const result = api.poseidon2HashAccumulate(inputFields.map(i => new FrBarretenberg(i.toBuffer())));
41
- return Fr.fromBuffer(Buffer.from(result.toBuffer()));
40
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
41
+ const api = BarretenbergSync.getSingleton();
42
+ const response = api.poseidon2HashAccumulate({
43
+ inputs: inputFields.map(i => i.toBuffer()),
44
+ });
45
+ return Fr.fromBuffer(Buffer.from(response.hash));
42
46
  }
43
47
 
44
48
  /**
@@ -50,11 +54,14 @@ export async function poseidon2Permutation(input: Fieldable[]): Promise<Fr[]> {
50
54
  const inputFields = serializeToFields(input);
51
55
  // We'd like this assertion but it's not possible to use it in the browser.
52
56
  // assert(input.length === 4, 'Input state must be of size 4');
53
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
54
- const res = api.poseidon2Permutation(inputFields.map(i => new FrBarretenberg(i.toBuffer())));
57
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
58
+ const api = BarretenbergSync.getSingleton();
59
+ const response = api.poseidon2Permutation({
60
+ inputs: inputFields.map(i => i.toBuffer()),
61
+ });
55
62
  // We'd like this assertion but it's not possible to use it in the browser.
56
- // assert(res.length === 4, 'Output state must be of size 4');
57
- return res.map(o => Fr.fromBuffer(Buffer.from(o.toBuffer())));
63
+ // assert(response.outputs.length === 4, 'Output state must be of size 4');
64
+ return response.outputs.map(o => Fr.fromBuffer(Buffer.from(o)));
58
65
  }
59
66
 
60
67
  export async function poseidon2HashBytes(input: Buffer): Promise<Fr> {
@@ -68,10 +75,11 @@ export async function poseidon2HashBytes(input: Buffer): Promise<Fr> {
68
75
  inputFields.push(Fr.fromBuffer(fieldBytes));
69
76
  }
70
77
 
71
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
72
- const res = api.poseidon2Hash(
73
- inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
74
- );
78
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
79
+ const api = BarretenbergSync.getSingleton();
80
+ const response = api.poseidon2Hash({
81
+ inputs: inputFields.map(i => i.toBuffer()),
82
+ });
75
83
 
76
- return Fr.fromBuffer(Buffer.from(res.toBuffer()));
84
+ return Fr.fromBuffer(Buffer.from(response.hash));
77
85
  }
@@ -1,52 +1,14 @@
1
- import nodeCrypto from 'crypto';
2
- import isNode from 'detect-node';
1
+ import { randomBytes as bbRandomBytes } from '@aztec/bb.js';
3
2
 
4
3
  import { RandomnessSingleton } from './randomness_singleton.js';
5
4
 
6
- // limit of Crypto.getRandomValues()
7
- // https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
8
- const MAX_BYTES = 65536;
9
-
10
- const getWebCrypto = () => {
11
- if (typeof window !== 'undefined' && window.crypto) {
12
- return window.crypto;
13
- }
14
- if (typeof self !== 'undefined' && self.crypto) {
15
- return self.crypto;
16
- }
17
- return undefined;
18
- };
19
-
20
5
  export const randomBytes = (len: number) => {
21
6
  const singleton = RandomnessSingleton.getInstance();
22
7
 
23
8
  if (singleton.isDeterministic()) {
24
9
  return singleton.getBytes(len);
25
10
  }
26
-
27
- if (isNode) {
28
- return nodeCrypto.randomBytes(len) as Buffer;
29
- }
30
-
31
- const crypto = getWebCrypto();
32
- if (!crypto) {
33
- throw new Error('randomBytes UnsupportedEnvironment');
34
- }
35
-
36
- const buf = Buffer.allocUnsafe(len);
37
- if (len > MAX_BYTES) {
38
- // this is the max bytes crypto.getRandomValues
39
- // can do at once see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
40
- for (let generated = 0; generated < len; generated += MAX_BYTES) {
41
- // buffer.slice automatically checks if the end is past the end of
42
- // the buffer so we don't have to here
43
- crypto.getRandomValues(buf.slice(generated, generated + MAX_BYTES));
44
- }
45
- } else {
46
- crypto.getRandomValues(buf);
47
- }
48
-
49
- return buf;
11
+ return Buffer.from(bbRandomBytes(len)) as Buffer<ArrayBuffer>;
50
12
  };
51
13
 
52
14
  /**
@@ -1,8 +1,6 @@
1
1
  import { BarretenbergSync } from '@aztec/bb.js';
2
2
  import { type GrumpkinScalar, Point } from '@aztec/foundation/fields';
3
- import { numToInt32BE } from '@aztec/foundation/serialize';
4
3
 
5
- import { concatenateUint8Arrays } from '../serialize.js';
6
4
  import { SchnorrSignature } from './signature.js';
7
5
 
8
6
  export * from './signature.js';
@@ -17,9 +15,10 @@ export class Schnorr {
17
15
  * @returns A grumpkin public key.
18
16
  */
19
17
  public async computePublicKey(privateKey: GrumpkinScalar): Promise<Point> {
20
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
21
- const [result] = api.getWasm().callWasmExport('schnorr_compute_public_key', [privateKey.toBuffer()], [64]);
22
- return Point.fromBuffer(Buffer.from(result));
18
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
19
+ const api = BarretenbergSync.getSingleton();
20
+ const response = api.schnorrComputePublicKey({ privateKey: privateKey.toBuffer() });
21
+ return Point.fromBuffer(Buffer.concat([Buffer.from(response.publicKey.x), Buffer.from(response.publicKey.y)]));
23
22
  }
24
23
 
25
24
  /**
@@ -29,12 +28,13 @@ export class Schnorr {
29
28
  * @returns A Schnorr signature of the form (s, e).
30
29
  */
31
30
  public async constructSignature(msg: Uint8Array, privateKey: GrumpkinScalar) {
32
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
33
- const messageArray = concatenateUint8Arrays([numToInt32BE(msg.length), msg]);
34
- const [s, e] = api
35
- .getWasm()
36
- .callWasmExport('schnorr_construct_signature', [messageArray, privateKey.toBuffer()], [32, 32]);
37
- return new SchnorrSignature(Buffer.from([...s, ...e]));
31
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
32
+ const api = BarretenbergSync.getSingleton();
33
+ const response = api.schnorrConstructSignature({
34
+ message: msg,
35
+ privateKey: privateKey.toBuffer(),
36
+ });
37
+ return new SchnorrSignature(Buffer.from([...response.s, ...response.e]));
38
38
  }
39
39
 
40
40
  /**
@@ -45,11 +45,14 @@ export class Schnorr {
45
45
  * @returns True or false.
46
46
  */
47
47
  public async verifySignature(msg: Uint8Array, pubKey: Point, sig: SchnorrSignature) {
48
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
49
- const messageArray = concatenateUint8Arrays([numToInt32BE(msg.length), msg]);
50
- const [result] = api
51
- .getWasm()
52
- .callWasmExport('schnorr_verify_signature', [messageArray, pubKey.toBuffer(), sig.s, sig.e], [1]);
53
- return result[0] === 1;
48
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
49
+ const api = BarretenbergSync.getSingleton();
50
+ const response = api.schnorrVerifySignature({
51
+ message: msg,
52
+ publicKey: { x: pubKey.x.toBuffer(), y: pubKey.y.toBuffer() },
53
+ s: sig.s,
54
+ e: sig.e,
55
+ });
56
+ return response.verified;
54
57
  }
55
58
  }
@@ -27,9 +27,13 @@ export class Secp256k1 {
27
27
  * @returns Result of the multiplication.
28
28
  */
29
29
  public async mul(point: Uint8Array, scalar: Uint8Array) {
30
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
31
- const [result] = api.getWasm().callWasmExport('ecc_secp256k1__mul', [point, scalar], [64]);
32
- return Buffer.from(result);
30
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
31
+ const api = BarretenbergSync.getSingleton();
32
+ const response = api.secp256k1Mul({
33
+ point: { x: point.subarray(0, 32), y: point.subarray(32, 64) },
34
+ scalar,
35
+ });
36
+ return Buffer.concat([Buffer.from(response.point.x), Buffer.from(response.point.y)]);
33
37
  }
34
38
 
35
39
  /**
@@ -37,9 +41,10 @@ export class Secp256k1 {
37
41
  * @returns Random field element.
38
42
  */
39
43
  public async getRandomFr() {
40
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
41
- const [result] = api.getWasm().callWasmExport('ecc_secp256k1__get_random_scalar_mod_circuit_modulus', [], [32]);
42
- return Buffer.from(result);
44
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
45
+ const api = BarretenbergSync.getSingleton();
46
+ const response = api.secp256k1GetRandomFr({ dummy: 0 });
47
+ return Buffer.from(response.value);
43
48
  }
44
49
 
45
50
  /**
@@ -48,10 +53,9 @@ export class Secp256k1 {
48
53
  * @returns Buffer representation of the field element.
49
54
  */
50
55
  public async reduce512BufferToFr(uint512Buf: Buffer) {
51
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
52
- const [result] = api
53
- .getWasm()
54
- .callWasmExport('ecc_secp256k1__reduce512_buffer_mod_circuit_modulus', [uint512Buf], [32]);
55
- return Buffer.from(result);
56
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
57
+ const api = BarretenbergSync.getSingleton();
58
+ const response = api.secp256k1Reduce512({ input: uint512Buf });
59
+ return Buffer.from(response.value);
56
60
  }
57
61
  }
@@ -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,11 +67,36 @@ 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.
72
+ * @throws Error if signature recovery fails or if signature is malleable and allowMalleable is false.
50
73
  */
51
- export function recoverAddress(hash: Buffer32, signature: Signature): EthAddress {
52
- const publicKey = recoverPublicKey(hash, signature);
53
- return publicKeyToAddress(publicKey);
74
+ export function recoverAddress(hash: Buffer32, signature: Signature, opts?: RecoveryOpts): EthAddress {
75
+ try {
76
+ const publicKey = recoverPublicKey(hash, signature, opts);
77
+ return publicKeyToAddress(publicKey);
78
+ } catch (err: unknown) {
79
+ throw new Secp256k1Error(
80
+ `Error recovering Ethereum address from hash ${hash.toString()} and signature ${signature.toString()}`,
81
+ { cause: err },
82
+ );
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Safely attempts to recover an address from a hash and a signature.
88
+ * @param hash - The hash to recover the address from.
89
+ * @param signature - The signature to recover the address from.
90
+ * @param opts - Recovery options.
91
+ * @returns The address if recovery succeeds, undefined otherwise.
92
+ */
93
+ export function tryRecoverAddress(hash: Buffer32, signature: Signature, opts?: RecoveryOpts): EthAddress | undefined {
94
+ try {
95
+ const publicKey = recoverPublicKey(hash, signature, opts);
96
+ return publicKeyToAddress(publicKey);
97
+ } catch {
98
+ return undefined;
99
+ }
54
100
  }
55
101
 
56
102
  /**
@@ -59,7 +105,7 @@ export function recoverAddress(hash: Buffer32, signature: Signature): EthAddress
59
105
  * @param yParityOrV - The yParityOrV value to convert.
60
106
  * @returns The recovery bit.
61
107
  */
62
- function toRecoveryBit(yParityOrV: number) {
108
+ export function toRecoveryBit(yParityOrV: number) {
63
109
  if (yParityOrV === 0 || yParityOrV === 1) {
64
110
  return yParityOrV;
65
111
  }
@@ -69,7 +115,7 @@ function toRecoveryBit(yParityOrV: number) {
69
115
  if (yParityOrV === 28) {
70
116
  return 1;
71
117
  }
72
- throw new Error('Invalid yParityOrV value');
118
+ throw new Secp256k1Error(`Invalid yParityOrV value ${yParityOrV}`);
73
119
  }
74
120
 
75
121
  /**
@@ -83,17 +129,84 @@ export function signMessage(message: Buffer32, privateKey: Buffer) {
83
129
  return new Signature(Buffer32.fromBigInt(r), Buffer32.fromBigInt(s), recovery ? 28 : 27);
84
130
  }
85
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
+
86
194
  /**
87
195
  * Recovers a public key from a hash and a signature.
88
196
  * @param hash - The hash to recover the public key from.
89
197
  * @param signature - The signature to recover the public key from.
90
198
  * @returns The public key.
91
199
  */
92
- export function recoverPublicKey(hash: Buffer32, signature: Signature): Buffer {
200
+ export function recoverPublicKey(hash: Buffer32, signature: Signature, opts: RecoveryOpts = {}): Buffer {
93
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
+ }
94
205
  const recoveryBit = toRecoveryBit(v);
95
206
  const sig = new secp256k1.Signature(r.toBigInt(), s.toBigInt()).addRecoveryBit(recoveryBit);
96
-
207
+ if (!opts.allowMalleable && sig.hasHighS()) {
208
+ throw new Secp256k1Error('Signature has high s-value (malleable signature)');
209
+ }
97
210
  const publicKey = sig.recoverPublicKey(hash.buffer).toHex(false);
98
211
  return Buffer.from(publicKey, 'hex');
99
212
  }
@@ -4,6 +4,7 @@ import { default as hash } from 'hash.js';
4
4
  import { Fr } from '../../fields/fields.js';
5
5
  import { truncateAndPad } from '../../serialize/free_funcs.js';
6
6
  import { type Bufferable, serializeToBuffer } from '../../serialize/serialize.js';
7
+ import type { Hasher } from '../../trees/hasher.js';
7
8
 
8
9
  export function sha256(data: Buffer) {
9
10
  return Buffer.from(hash.sha256().update(data).digest());
@@ -145,3 +146,49 @@ function g0_256(x: number) {
145
146
  function g1_256(x: number) {
146
147
  return rotr32(x, 17) ^ rotr32(x, 19) ^ (x >>> 10);
147
148
  }
149
+
150
+ /**
151
+ * A helper class encapsulating SHA256 hash functionality.
152
+ * @deprecated Don't call SHA256 directly in production code. Instead, create suitably-named functions for specific
153
+ * purposes.
154
+ */
155
+ export class SHA256 implements Hasher {
156
+ /*
157
+ * @deprecated Don't call SHA256 directly in production code. Instead, create suitably-named functions for specific
158
+ * purposes.
159
+ */
160
+ public hash(lhs: Uint8Array, rhs: Uint8Array) {
161
+ return sha256(Buffer.concat([Buffer.from(lhs), Buffer.from(rhs)])) as Buffer<ArrayBuffer>;
162
+ }
163
+
164
+ /*
165
+ * @deprecated Don't call SHA256 directly in production code. Instead, create suitably-named functions for specific
166
+ * purposes.
167
+ */
168
+ public hashInputs(inputs: Buffer[]) {
169
+ return sha256(Buffer.concat(inputs)) as Buffer<ArrayBuffer>;
170
+ }
171
+ }
172
+
173
+ /**
174
+ * A helper class encapsulating truncated SHA256 hash functionality.
175
+ * @deprecated Don't call SHA256 directly in production code. Instead, create suitably-named functions for specific
176
+ * purposes.
177
+ */
178
+ export class SHA256Trunc implements Hasher {
179
+ /*
180
+ * @deprecated Don't call SHA256 directly in production code. Instead, create suitably-named functions for specific
181
+ * purposes.
182
+ */
183
+ public hash(lhs: Uint8Array, rhs: Uint8Array) {
184
+ return truncateAndPad(sha256(Buffer.concat([Buffer.from(lhs), Buffer.from(rhs)]))) as Buffer<ArrayBuffer>;
185
+ }
186
+
187
+ /*
188
+ * @deprecated Don't call SHA256 directly in production code. Instead, create suitably-named functions for specific
189
+ * purposes.
190
+ */
191
+ public hashInputs(inputs: Buffer[]) {
192
+ return truncateAndPad(sha256(Buffer.concat(inputs))) as Buffer<ArrayBuffer>;
193
+ }
194
+ }
@@ -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(process.env.BB_WASM_PATH);
6
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
@@ -1,4 +1,4 @@
1
- import { BarretenbergSync, Fr as FrBarretenberg } from '@aztec/bb.js';
1
+ import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
3
  import { Fr } from '../../../fields/fields.js';
4
4
  import { type Fieldable, serializeToFields } from '../../../serialize/serialize.js';
@@ -12,13 +12,11 @@ export function pedersenCommit(input: Buffer[], offset = 0) {
12
12
  throw new Error('All Pedersen Commit input buffers must be <= 32 bytes.');
13
13
  }
14
14
  input = input.map(i => (i.length < 32 ? Buffer.concat([Buffer.alloc(32 - i.length, 0), i]) : i));
15
- const point = BarretenbergSync.getSingleton().pedersenCommit(
16
- input.map(i => new FrBarretenberg(i)),
17
- offset,
18
- );
19
- // toBuffer returns Uint8Arrays (browser/worker-boundary friendly).
20
- // TODO: rename toTypedArray()?
21
- return [Buffer.from(point.x.toBuffer()), Buffer.from(point.y.toBuffer())];
15
+ const response = BarretenbergSync.getSingleton().pedersenCommit({
16
+ inputs: input,
17
+ hashIndex: offset,
18
+ });
19
+ return [Buffer.from(response.point.x), Buffer.from(response.point.y)];
22
20
  }
23
21
 
24
22
  /**
@@ -29,17 +27,20 @@ export function pedersenCommit(input: Buffer[], offset = 0) {
29
27
  */
30
28
  export function pedersenHash(input: Fieldable[], index = 0): Fr {
31
29
  const inputFields = serializeToFields(input);
32
- const hash = BarretenbergSync.getSingleton().pedersenHash(
33
- inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
34
- index,
35
- );
36
- return Fr.fromBuffer(Buffer.from(hash.toBuffer()));
30
+ const response = BarretenbergSync.getSingleton().pedersenHash({
31
+ inputs: inputFields.map(i => i.toBuffer()),
32
+ hashIndex: index,
33
+ });
34
+ return Fr.fromBuffer(Buffer.from(response.hash));
37
35
  }
38
36
 
39
37
  /**
40
38
  * Create a pedersen hash from an arbitrary length buffer.
41
39
  */
42
40
  export function pedersenHashBuffer(input: Buffer, index = 0) {
43
- const result = BarretenbergSync.getSingleton().pedersenHashBuffer(input, index);
44
- return Buffer.from(result.toBuffer());
41
+ const response = BarretenbergSync.getSingleton().pedersenHashBuffer({
42
+ input,
43
+ hashIndex: index,
44
+ });
45
+ return Buffer.from(response.hash);
45
46
  }
@@ -1,4 +1,4 @@
1
- import { BarretenbergSync, Fr as FrBarretenberg } from '@aztec/bb.js';
1
+ import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
3
  import { Fr } from '../../../fields/fields.js';
4
4
  import { type Fieldable, serializeToFields } from '../../../serialize/serialize.js';
@@ -10,10 +10,11 @@ import { type Fieldable, serializeToFields } from '../../../serialize/serialize.
10
10
  */
11
11
  export function poseidon2Hash(input: Fieldable[]): Fr {
12
12
  const inputFields = serializeToFields(input);
13
- const hash = BarretenbergSync.getSingleton().poseidon2Hash(
14
- inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
15
- );
16
- return Fr.fromBuffer(Buffer.from(hash.toBuffer()));
13
+ const api = BarretenbergSync.getSingleton();
14
+ const response = api.poseidon2Hash({
15
+ inputs: inputFields.map(i => i.toBuffer()),
16
+ });
17
+ return Fr.fromBuffer(Buffer.from(response.hash));
17
18
  }
18
19
 
19
20
  /**
@@ -26,18 +27,20 @@ export function poseidon2HashWithSeparator(input: Fieldable[], separator: number
26
27
  const inputFields = serializeToFields(input);
27
28
  inputFields.unshift(new Fr(separator));
28
29
 
29
- const hash = BarretenbergSync.getSingleton().poseidon2Hash(
30
- inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
31
- );
32
- return Fr.fromBuffer(Buffer.from(hash.toBuffer()));
30
+ const api = BarretenbergSync.getSingleton();
31
+ const response = api.poseidon2Hash({
32
+ inputs: inputFields.map(i => i.toBuffer()),
33
+ });
34
+ return Fr.fromBuffer(Buffer.from(response.hash));
33
35
  }
34
36
 
35
37
  export function poseidon2HashAccumulate(input: Fieldable[]): Fr {
36
38
  const inputFields = serializeToFields(input);
37
- const result = BarretenbergSync.getSingleton().poseidon2HashAccumulate(
38
- inputFields.map(i => new FrBarretenberg(i.toBuffer())),
39
- );
40
- return Fr.fromBuffer(Buffer.from(result.toBuffer()));
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));
41
44
  }
42
45
 
43
46
  /**
@@ -49,12 +52,13 @@ export function poseidon2Permutation(input: Fieldable[]): Fr[] {
49
52
  const inputFields = serializeToFields(input);
50
53
  // We'd like this assertion but it's not possible to use it in the browser.
51
54
  // assert(input.length === 4, 'Input state must be of size 4');
52
- const res = BarretenbergSync.getSingleton().poseidon2Permutation(
53
- inputFields.map(i => new FrBarretenberg(i.toBuffer())),
54
- );
55
+ const api = BarretenbergSync.getSingleton();
56
+ const response = api.poseidon2Permutation({
57
+ inputs: inputFields.map(i => i.toBuffer()),
58
+ });
55
59
  // We'd like this assertion but it's not possible to use it in the browser.
56
- // assert(res.length === 4, 'Output state must be of size 4');
57
- return res.map(o => Fr.fromBuffer(Buffer.from(o.toBuffer())));
60
+ // assert(response.outputs.length === 4, 'Output state must be of size 4');
61
+ return response.outputs.map(o => Fr.fromBuffer(Buffer.from(o)));
58
62
  }
59
63
 
60
64
  export function poseidon2HashBytes(input: Buffer): Fr {
@@ -68,9 +72,10 @@ export function poseidon2HashBytes(input: Buffer): Fr {
68
72
  inputFields.push(Fr.fromBuffer(fieldBytes));
69
73
  }
70
74
 
71
- const res = BarretenbergSync.getSingleton().poseidon2Hash(
72
- inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
73
- );
75
+ const api = BarretenbergSync.getSingleton();
76
+ const response = api.poseidon2Hash({
77
+ inputs: inputFields.map(i => i.toBuffer()),
78
+ });
74
79
 
75
- return Fr.fromBuffer(Buffer.from(res.toBuffer()));
80
+ return Fr.fromBuffer(Buffer.from(response.hash));
76
81
  }
@@ -117,14 +117,14 @@ export class EthAddress {
117
117
  * @param address - The Ethereum address as a hex-encoded string.
118
118
  * @returns The Ethereum address in its checksum format.
119
119
  */
120
- public static toChecksumAddress(address: string) {
120
+ public static toChecksumAddress(address: string): `0x${string}` {
121
121
  if (!EthAddress.isAddress(address)) {
122
122
  throw new Error('Invalid address string.');
123
123
  }
124
124
 
125
125
  address = address.toLowerCase().replace(/^0x/i, '');
126
126
  const addressHash = keccak256String(address);
127
- let checksumAddress = '0x';
127
+ let checksumAddress: `0x${string}` = '0x';
128
128
 
129
129
  for (let i = 0; i < address.length; i++) {
130
130
  // If ith character is 9 to f then make it uppercase.
@@ -228,6 +228,13 @@ export class EthAddress {
228
228
  return new EthAddress(reader.readBytes(EthAddress.SIZE_IN_BYTES));
229
229
  }
230
230
 
231
+ /** Converts a number into an address. Useful for testing. */
232
+ static fromNumber(num: bigint | number): EthAddress {
233
+ const buffer = Buffer.alloc(EthAddress.SIZE_IN_BYTES);
234
+ buffer.writeBigUInt64BE(BigInt(num), 0);
235
+ return new EthAddress(buffer);
236
+ }
237
+
231
238
  toJSON() {
232
239
  return this.toString();
233
240
  }