@aztec/foundation 0.0.1-fake-ceab37513c → 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 (520) 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 +6 -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 +19 -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 +39 -0
  77. package/dest/crypto/bn254/index.d.ts.map +1 -0
  78. package/dest/crypto/bn254/index.js +56 -0
  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 +58 -48
  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 +10 -13
  86. package/dest/crypto/grumpkin/index.d.ts.map +1 -1
  87. package/dest/crypto/grumpkin/index.js +61 -55
  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 +8 -5
  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 +24 -14
  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 +27 -21
  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 +30 -38
  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 +23 -18
  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 -1
  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 +18 -11
  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 +23 -16
  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} +11 -11
  144. package/dest/curves/bls12/point.d.ts.map +1 -0
  145. package/dest/{fields/bls12_point.js → curves/bls12/point.js} +17 -15
  146. package/dest/curves/bn254/field.d.ts +180 -0
  147. package/dest/curves/bn254/field.d.ts.map +1 -0
  148. package/dest/{fields/fields.js → curves/bn254/field.js} +71 -59
  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 +10 -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 -30
  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 +2 -0
  253. package/dest/profiler/index.d.ts.map +1 -0
  254. package/dest/profiler/index.js +1 -0
  255. package/dest/profiler/profiler.d.ts +8 -0
  256. package/dest/profiler/profiler.d.ts.map +1 -0
  257. package/dest/profiler/profiler.js +97 -0
  258. package/dest/promise/index.d.ts +1 -1
  259. package/dest/promise/running-promise.d.ts +1 -1
  260. package/dest/promise/running-promise.d.ts.map +1 -1
  261. package/dest/promise/utils.d.ts +1 -1
  262. package/dest/queue/base_memory_queue.d.ts +2 -2
  263. package/dest/queue/base_memory_queue.d.ts.map +1 -1
  264. package/dest/queue/batch_queue.d.ts +1 -1
  265. package/dest/queue/batch_queue.d.ts.map +1 -1
  266. package/dest/queue/bounded_serial_queue.d.ts +1 -1
  267. package/dest/queue/bounded_serial_queue.d.ts.map +1 -1
  268. package/dest/queue/fifo_memory_queue.d.ts +1 -1
  269. package/dest/queue/fifo_memory_queue.d.ts.map +1 -1
  270. package/dest/queue/index.d.ts +1 -1
  271. package/dest/queue/priority_memory_queue.d.ts +1 -1
  272. package/dest/queue/priority_memory_queue.d.ts.map +1 -1
  273. package/dest/queue/priority_queue.d.ts +1 -1
  274. package/dest/queue/priority_queue.d.ts.map +1 -1
  275. package/dest/queue/semaphore.d.ts +5 -1
  276. package/dest/queue/semaphore.d.ts.map +1 -1
  277. package/dest/queue/serial_queue.d.ts +1 -1
  278. package/dest/queue/serial_queue.d.ts.map +1 -1
  279. package/dest/retry/index.d.ts +11 -1
  280. package/dest/retry/index.d.ts.map +1 -1
  281. package/dest/retry/index.js +11 -0
  282. package/dest/running-promise/index.d.ts +1 -1
  283. package/dest/schemas/api.d.ts +1 -1
  284. package/dest/schemas/index.d.ts +1 -1
  285. package/dest/schemas/parse.d.ts +1 -1
  286. package/dest/schemas/schemas.d.ts +15 -17
  287. package/dest/schemas/schemas.d.ts.map +1 -1
  288. package/dest/schemas/schemas.js +14 -10
  289. package/dest/schemas/types.d.ts +31 -1
  290. package/dest/schemas/types.d.ts.map +1 -1
  291. package/dest/schemas/types.js +25 -1
  292. package/dest/schemas/utils.d.ts +4 -4
  293. package/dest/schemas/utils.d.ts.map +1 -1
  294. package/dest/schemas/utils.js +15 -5
  295. package/dest/serialize/buffer_reader.d.ts +14 -4
  296. package/dest/serialize/buffer_reader.d.ts.map +1 -1
  297. package/dest/serialize/buffer_reader.js +26 -4
  298. package/dest/serialize/field_reader.d.ts +2 -2
  299. package/dest/serialize/field_reader.d.ts.map +1 -1
  300. package/dest/serialize/field_reader.js +1 -1
  301. package/dest/serialize/free_funcs.d.ts +2 -2
  302. package/dest/serialize/free_funcs.d.ts.map +1 -1
  303. package/dest/serialize/free_funcs.js +1 -1
  304. package/dest/serialize/index.d.ts +1 -1
  305. package/dest/serialize/serialize.d.ts +20 -2
  306. package/dest/serialize/serialize.d.ts.map +1 -1
  307. package/dest/serialize/serialize.js +32 -1
  308. package/dest/serialize/type_registry.d.ts +1 -1
  309. package/dest/serialize/type_registry.d.ts.map +1 -1
  310. package/dest/serialize/types.d.ts +1 -1
  311. package/dest/sleep/index.d.ts +3 -1
  312. package/dest/sleep/index.d.ts.map +1 -1
  313. package/dest/sleep/index.js +4 -0
  314. package/dest/string/index.d.ts +5 -1
  315. package/dest/string/index.d.ts.map +1 -1
  316. package/dest/string/index.js +1 -1
  317. package/dest/testing/files/index.d.ts +2 -1
  318. package/dest/testing/files/index.d.ts.map +1 -1
  319. package/dest/testing/files/index.js +1 -1
  320. package/dest/testing/formatting.d.ts +4 -0
  321. package/dest/testing/formatting.d.ts.map +1 -0
  322. package/dest/testing/formatting.js +3 -0
  323. package/dest/testing/index.d.ts +2 -1
  324. package/dest/testing/index.d.ts.map +1 -1
  325. package/dest/testing/index.js +1 -0
  326. package/dest/testing/port_allocator.d.ts +1 -1
  327. package/dest/testing/snapshot_serializer.d.ts +1 -1
  328. package/dest/testing/test_data.d.ts +1 -1
  329. package/dest/timer/date.d.ts +26 -1
  330. package/dest/timer/date.d.ts.map +1 -1
  331. package/dest/timer/date.js +36 -0
  332. package/dest/timer/elapsed.d.ts +1 -1
  333. package/dest/timer/index.d.ts +1 -1
  334. package/dest/timer/timeout.d.ts +1 -1
  335. package/dest/timer/timeout.d.ts.map +1 -1
  336. package/dest/timer/timer.d.ts +1 -1
  337. package/dest/timer/timer.d.ts.map +1 -1
  338. package/dest/transport/dispatch/create_dispatch_fn.d.ts +1 -1
  339. package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +1 -1
  340. package/dest/transport/dispatch/create_dispatch_proxy.d.ts +1 -1
  341. package/dest/transport/dispatch/messages.d.ts +1 -1
  342. package/dest/transport/index.d.ts +1 -1
  343. package/dest/transport/interface/connector.d.ts +1 -1
  344. package/dest/transport/interface/listener.d.ts +1 -1
  345. package/dest/transport/interface/socket.d.ts +1 -1
  346. package/dest/transport/interface/transferable.d.ts +1 -1
  347. package/dest/transport/node/index.d.ts +1 -1
  348. package/dest/transport/node/node_connector.d.ts +1 -1
  349. package/dest/transport/node/node_connector.d.ts.map +1 -1
  350. package/dest/transport/node/node_connector_socket.d.ts +1 -1
  351. package/dest/transport/node/node_connector_socket.d.ts.map +1 -1
  352. package/dest/transport/node/node_listener.d.ts +1 -1
  353. package/dest/transport/node/node_listener.d.ts.map +1 -1
  354. package/dest/transport/node/node_listener_socket.d.ts +1 -1
  355. package/dest/transport/node/node_listener_socket.d.ts.map +1 -1
  356. package/dest/transport/transport_client.d.ts +1 -1
  357. package/dest/transport/transport_client.d.ts.map +1 -1
  358. package/dest/transport/transport_server.d.ts +1 -10
  359. package/dest/transport/transport_server.d.ts.map +1 -1
  360. package/dest/trees/balanced_merkle_tree_root.d.ts +17 -0
  361. package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -0
  362. package/dest/trees/balanced_merkle_tree_root.js +51 -0
  363. package/dest/trees/hasher.d.ts +3 -1
  364. package/dest/trees/hasher.d.ts.map +1 -1
  365. package/dest/trees/hasher.js +10 -5
  366. package/dest/trees/index.d.ts +4 -3
  367. package/dest/trees/index.d.ts.map +1 -1
  368. package/dest/trees/index.js +3 -2
  369. package/dest/trees/indexed_merkle_tree.d.ts +1 -1
  370. package/dest/trees/indexed_merkle_tree.d.ts.map +1 -1
  371. package/dest/trees/indexed_merkle_tree.js +1 -1
  372. package/dest/trees/indexed_merkle_tree_calculator.d.ts +1 -1
  373. package/dest/trees/indexed_merkle_tree_calculator.d.ts.map +1 -1
  374. package/dest/trees/indexed_tree_leaf.d.ts +1 -1
  375. package/dest/trees/membership_witness.d.ts +7 -18
  376. package/dest/trees/membership_witness.d.ts.map +1 -1
  377. package/dest/trees/membership_witness.js +10 -1
  378. package/dest/trees/merkle_tree.d.ts +1 -1
  379. package/dest/trees/merkle_tree.d.ts.map +1 -1
  380. package/dest/trees/merkle_tree_calculator.d.ts +2 -12
  381. package/dest/trees/merkle_tree_calculator.d.ts.map +1 -1
  382. package/dest/trees/merkle_tree_calculator.js +2 -32
  383. package/dest/trees/sibling_path.d.ts +5 -38
  384. package/dest/trees/sibling_path.d.ts.map +1 -1
  385. package/dest/trees/sibling_path.js +3 -3
  386. package/dest/trees/unbalanced_merkle_tree_calculator.d.ts +26 -23
  387. package/dest/trees/unbalanced_merkle_tree_calculator.d.ts.map +1 -1
  388. package/dest/trees/unbalanced_merkle_tree_calculator.js +119 -97
  389. package/dest/trees/unbalanced_merkle_tree_root.d.ts +28 -0
  390. package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -0
  391. package/dest/trees/unbalanced_merkle_tree_root.js +84 -0
  392. package/dest/trees/unbalanced_tree_store.d.ts +6 -1
  393. package/dest/trees/unbalanced_tree_store.d.ts.map +1 -1
  394. package/dest/trees/unbalanced_tree_store.js +55 -1
  395. package/dest/types/index.d.ts +6 -2
  396. package/dest/types/index.d.ts.map +1 -1
  397. package/dest/types/index.js +6 -0
  398. package/dest/url/index.d.ts +1 -1
  399. package/dest/validation/index.d.ts +1 -1
  400. package/package.json +53 -8
  401. package/src/array/array.ts +0 -16
  402. package/src/array/index.ts +1 -0
  403. package/src/array/sorted_array.ts +138 -0
  404. package/src/branded-types/block_number.ts +117 -0
  405. package/src/branded-types/checkpoint_number.ts +115 -0
  406. package/src/branded-types/epoch.ts +88 -0
  407. package/src/branded-types/index.ts +7 -0
  408. package/src/branded-types/index_within_checkpoint.ts +88 -0
  409. package/src/branded-types/slot.ts +88 -0
  410. package/src/branded-types/types.ts +2 -0
  411. package/src/buffer/buffer16.ts +2 -2
  412. package/src/buffer/buffer32.ts +2 -2
  413. package/src/collection/array.ts +67 -3
  414. package/src/config/env_var.ts +82 -15
  415. package/src/config/index.ts +39 -6
  416. package/src/config/network_config.ts +2 -0
  417. package/src/config/network_name.ts +15 -1
  418. package/src/config/parse-env.ts +4 -0
  419. package/src/crypto/aes128/index.ts +19 -10
  420. package/src/crypto/bls/bn254_keystore.ts +287 -0
  421. package/src/crypto/bls/index.ts +77 -0
  422. package/src/crypto/bn254/index.ts +64 -0
  423. package/src/crypto/ecdsa/index.ts +40 -37
  424. package/src/crypto/ecdsa/signature.ts +2 -2
  425. package/src/crypto/grumpkin/index.ts +38 -46
  426. package/src/crypto/keys/index.ts +6 -6
  427. package/src/crypto/pedersen/pedersen.wasm.ts +23 -19
  428. package/src/crypto/poseidon/index.ts +27 -29
  429. package/src/crypto/random/randomness_singleton.ts +6 -4
  430. package/src/crypto/schnorr/index.ts +22 -18
  431. package/src/crypto/schnorr/signature.ts +2 -2
  432. package/src/crypto/secp256k1/index.ts +15 -11
  433. package/src/crypto/secp256k1-signer/utils.ts +102 -10
  434. package/src/crypto/sha256/index.ts +1 -1
  435. package/src/crypto/sha512/index.ts +1 -1
  436. package/src/crypto/signature/index.ts +1 -1
  437. package/src/crypto/sync/index.ts +1 -1
  438. package/src/crypto/sync/pedersen/index.ts +17 -16
  439. package/src/crypto/sync/poseidon/index.ts +23 -27
  440. package/src/{fields/bls12_fields.ts → curves/bls12/field.ts} +6 -6
  441. package/src/curves/bls12/index.ts +2 -0
  442. package/src/{fields/bls12_point.ts → curves/bls12/point.ts} +14 -14
  443. package/src/{fields/fields.ts → curves/bn254/field.ts} +69 -62
  444. package/src/curves/bn254/index.ts +2 -0
  445. package/src/curves/bn254/point.ts +170 -0
  446. package/src/curves/grumpkin/index.ts +11 -0
  447. package/src/{fields → curves/grumpkin}/point.ts +26 -7
  448. package/src/eth-address/index.ts +27 -2
  449. package/src/iterator/filter.ts +11 -0
  450. package/src/iterator/index.ts +1 -0
  451. package/src/jest/setup.mjs +4 -1
  452. package/src/json-rpc/client/safe_json_rpc_client.ts +11 -0
  453. package/src/json-rpc/client/undici.ts +21 -3
  454. package/src/json-rpc/server/api_key_auth.ts +63 -0
  455. package/src/json-rpc/server/index.ts +1 -0
  456. package/src/json-rpc/server/safe_json_rpc_server.ts +1 -1
  457. package/src/log/bigint-utils.ts +22 -0
  458. package/src/log/gcloud-logger-config.ts +5 -0
  459. package/src/log/libp2p_logger.ts +12 -5
  460. package/src/log/log-filters.ts +29 -11
  461. package/src/log/pino-logger-server.ts +25 -0
  462. package/src/log/pino-logger.ts +144 -40
  463. package/src/profiler/index.ts +1 -0
  464. package/src/profiler/profiler.ts +125 -0
  465. package/src/queue/base_memory_queue.ts +1 -1
  466. package/src/queue/semaphore.ts +5 -0
  467. package/src/retry/index.ts +18 -0
  468. package/src/schemas/schemas.ts +24 -13
  469. package/src/schemas/types.ts +33 -0
  470. package/src/schemas/utils.ts +21 -8
  471. package/src/serialize/buffer_reader.ts +36 -9
  472. package/src/serialize/field_reader.ts +1 -1
  473. package/src/serialize/free_funcs.ts +1 -1
  474. package/src/serialize/serialize.ts +33 -1
  475. package/src/sleep/index.ts +6 -0
  476. package/src/string/index.ts +3 -0
  477. package/src/testing/files/index.ts +1 -1
  478. package/src/testing/formatting.ts +3 -0
  479. package/src/testing/index.ts +1 -0
  480. package/src/timer/date.ts +52 -0
  481. package/src/trees/balanced_merkle_tree_root.ts +68 -0
  482. package/src/trees/hasher.ts +9 -0
  483. package/src/trees/index.ts +3 -2
  484. package/src/trees/indexed_merkle_tree.ts +1 -1
  485. package/src/trees/membership_witness.ts +9 -1
  486. package/src/trees/merkle_tree_calculator.ts +2 -35
  487. package/src/trees/sibling_path.ts +3 -3
  488. package/src/trees/unbalanced_merkle_tree_calculator.ts +123 -93
  489. package/src/trees/unbalanced_merkle_tree_root.ts +115 -0
  490. package/src/trees/unbalanced_tree_store.ts +62 -3
  491. package/src/types/index.ts +11 -1
  492. package/dest/crypto/index.d.ts +0 -15
  493. package/dest/crypto/index.d.ts.map +0 -1
  494. package/dest/crypto/index.js +0 -14
  495. package/dest/fields/bls12_fields.d.ts.map +0 -1
  496. package/dest/fields/bls12_point.d.ts.map +0 -1
  497. package/dest/fields/coordinate.d.ts +0 -66
  498. package/dest/fields/coordinate.d.ts.map +0 -1
  499. package/dest/fields/coordinate.js +0 -96
  500. package/dest/fields/fields.d.ts +0 -174
  501. package/dest/fields/fields.d.ts.map +0 -1
  502. package/dest/fields/index.d.ts +0 -6
  503. package/dest/fields/index.d.ts.map +0 -1
  504. package/dest/fields/index.js +0 -5
  505. package/dest/fields/point.d.ts.map +0 -1
  506. package/dest/index.d.ts +0 -29
  507. package/dest/index.d.ts.map +0 -1
  508. package/dest/index.js +0 -29
  509. package/dest/json-rpc/server/telemetry.d.ts +0 -2
  510. package/dest/json-rpc/server/telemetry.d.ts.map +0 -1
  511. package/dest/json-rpc/server/telemetry.js +0 -0
  512. package/dest/trees/unbalanced_merkle_tree.d.ts +0 -29
  513. package/dest/trees/unbalanced_merkle_tree.d.ts.map +0 -1
  514. package/dest/trees/unbalanced_merkle_tree.js +0 -99
  515. package/src/crypto/index.ts +0 -14
  516. package/src/fields/coordinate.ts +0 -104
  517. package/src/fields/index.ts +0 -5
  518. package/src/index.ts +0 -29
  519. package/src/json-rpc/server/telemetry.ts +0 -0
  520. package/src/trees/unbalanced_merkle_tree.ts +0 -111
@@ -1,5 +1,5 @@
1
1
  import { toBufferBE } from '../bigint-buffer/index.js';
2
- import { Fr } from '../fields/fields.js';
2
+ import { Fr } from '../curves/bn254/field.js';
3
3
  import type { Tuple } from './types.js';
4
4
 
5
5
  /**
@@ -1,5 +1,5 @@
1
1
  import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
2
- import { Fr } from '../fields/fields.js';
2
+ import { Fr } from '../curves/bn254/field.js';
3
3
  import { numToUInt32BE } from './free_funcs.js';
4
4
 
5
5
  /**
@@ -269,6 +269,22 @@ export function serializeBigInt(n: bigint, width = 32) {
269
269
  return toBufferBE(n, width);
270
270
  }
271
271
 
272
+ /**
273
+ * Serialize a signed BigInt value into a Buffer of specified width using two's complement.
274
+ * @param n - The signed BigInt value to be serialized.
275
+ * @param width - The width (in bytes) of the output Buffer, optional with default value 32.
276
+ * @returns A Buffer containing the serialized signed BigInt value in big-endian format.
277
+ */
278
+ export function serializeSignedBigInt(n: bigint, width = 32) {
279
+ const widthBits = BigInt(width * 8);
280
+ const max = 1n << (widthBits - 1n);
281
+ if (n < -max || n >= max) {
282
+ throw new Error(`Signed BigInt ${n.toString()} does not fit into ${width} bytes`);
283
+ }
284
+ const unsigned = n < 0n ? (1n << widthBits) + n : n;
285
+ return toBufferBE(unsigned, width);
286
+ }
287
+
272
288
  /**
273
289
  * Deserialize a big integer from a buffer, given an offset and width.
274
290
  * Reads the specified number of bytes from the buffer starting at the offset, converts it to a big integer, and returns the deserialized result along with the number of bytes read (advanced).
@@ -282,6 +298,22 @@ export function deserializeBigInt(buf: Buffer, offset = 0, width = 32) {
282
298
  return { elem: toBigIntBE(buf.subarray(offset, offset + width)), adv: width };
283
299
  }
284
300
 
301
+ /**
302
+ * Deserialize a signed BigInt from a buffer (two's complement).
303
+ * @param buf - The buffer containing the signed big integer to be deserialized.
304
+ * @param offset - The position in the buffer where the integer starts. Defaults to 0.
305
+ * @param width - The number of bytes to read from the buffer for the integer. Defaults to 32.
306
+ * @returns An object containing the deserialized signed bigint value ('elem') and bytes advanced ('adv').
307
+ */
308
+ export function deserializeSignedBigInt(buf: Buffer, offset = 0, width = 32) {
309
+ const { elem, adv } = deserializeBigInt(buf, offset, width);
310
+ const widthBits = BigInt(width * 8);
311
+ const signBit = 1n << (widthBits - 1n);
312
+ const fullRange = 1n << widthBits;
313
+ const signed = elem >= signBit ? elem - fullRange : elem;
314
+ return { elem: signed, adv };
315
+ }
316
+
285
317
  /**
286
318
  * Serializes a Date object into a Buffer containing its timestamp as a big integer value.
287
319
  * The resulting Buffer has a fixed width of 8 bytes, representing a 64-bit big-endian integer.
@@ -73,3 +73,9 @@ export class InterruptibleSleep {
73
73
  export function sleep<T>(ms: number, returnValue?: T): Promise<T> {
74
74
  return new Promise(resolve => setTimeout(() => resolve(returnValue as T), ms));
75
75
  }
76
+
77
+ /** Sleeps until the target date */
78
+ export function sleepUntil<T>(target: Date, now: Date, returnValue?: T): Promise<T> {
79
+ const ms = target.getTime() - now.getTime();
80
+ return sleep(ms, returnValue);
81
+ }
@@ -1,3 +1,6 @@
1
+ /** Parameterized hex string type for specific byte lengths */
2
+ export type Hex<TByteLength extends number> = `0x${string}` & { readonly _length: TByteLength };
3
+
1
4
  export function hasHexPrefix(str: string): str is `0x${string}` {
2
5
  return str.startsWith('0x');
3
6
  }
@@ -63,7 +63,7 @@ export function updateProtocolCircuitSampleInputs(circuitName: string, value: st
63
63
  logger(`Updated test data in ${targetFile} for ${circuitName}`);
64
64
  }
65
65
 
66
- function getPathToFile(targetFileFromRepoRoot: string) {
66
+ export function getPathToFile(targetFileFromRepoRoot: string) {
67
67
  const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), '../../../../../');
68
68
  if (!existsSync(join(repoRoot, 'CODEOWNERS'))) {
69
69
  throw new Error(`Path to repo root is incorrect (got ${repoRoot})`);
@@ -0,0 +1,3 @@
1
+ export function toInlineStrArray(arr: { toString: () => string }[]): string {
2
+ return `[${arr.map(f => f.toString()).join(',')}]`;
3
+ }
@@ -1,3 +1,4 @@
1
+ export * from './formatting.js';
1
2
  export * from './snapshot_serializer.js';
2
3
  export * from './port_allocator.js';
3
4
  export * from './test_data.js';
package/src/timer/date.ts CHANGED
@@ -9,6 +9,10 @@ export class DateProvider {
9
9
  public nowInSeconds(): number {
10
10
  return Math.floor(this.now() / 1000);
11
11
  }
12
+
13
+ public nowAsDate(): Date {
14
+ return new Date(this.now());
15
+ }
12
16
  }
13
17
 
14
18
  /** Returns current datetime and allows to override it. */
@@ -27,4 +31,52 @@ export class TestDateProvider extends DateProvider {
27
31
  this.offset = timeMs - Date.now();
28
32
  this.logger.warn(`Time set to ${new Date(timeMs).toISOString()}`, { offset: this.offset, timeMs });
29
33
  }
34
+
35
+ /** Resets the time back to real time (offset = 0). */
36
+ public reset() {
37
+ this.offset = 0;
38
+ this.logger.warn('Time reset to real time');
39
+ }
40
+
41
+ /** Advances the time by the given number of seconds. */
42
+ public advanceTime(seconds: number) {
43
+ this.offset += seconds * 1000;
44
+ }
45
+ }
46
+
47
+ /**
48
+ * A date provider for tests that only advances time via explicit advanceTime() calls.
49
+ * Unlike TestDateProvider, this does NOT track real time progression - time is completely
50
+ * frozen until explicitly advanced. This eliminates flakiness from tests taking
51
+ * varying amounts of real time to execute.
52
+ */
53
+ export class ManualDateProvider extends DateProvider {
54
+ private currentTimeMs: number;
55
+
56
+ /**
57
+ * @param initialTimeMs - Initial time in milliseconds. Defaults to a round timestamp for easy visualization.
58
+ */
59
+ constructor(initialTimeMs: number = Date.UTC(2025, 0, 1, 0, 0, 0)) {
60
+ super();
61
+ this.currentTimeMs = initialTimeMs;
62
+ }
63
+
64
+ public override now(): number {
65
+ return this.currentTimeMs;
66
+ }
67
+
68
+ /** Sets the current time to the given timestamp in milliseconds. */
69
+ public setTime(timeMs: number) {
70
+ this.currentTimeMs = timeMs;
71
+ }
72
+
73
+ /** Advances the time by the given number of seconds. */
74
+ public advanceTime(seconds: number) {
75
+ this.currentTimeMs += seconds * 1000;
76
+ }
77
+
78
+ /** Advances the time by the given number of milliseconds. */
79
+ public advanceTimeMs(ms: number) {
80
+ this.currentTimeMs += ms;
81
+ }
30
82
  }
@@ -0,0 +1,68 @@
1
+ import { poseidonMerkleHash, shaMerkleHash } from './hasher.js';
2
+
3
+ export const computeBalancedShaRoot = (leaves: Buffer[]) => computeBalancedMerkleTreeRoot(leaves);
4
+
5
+ export const computeBalancedPoseidonRoot = async (leaves: Buffer[]) =>
6
+ await computeBalancedMerkleTreeRootAsync(leaves, poseidonMerkleHash);
7
+
8
+ /**
9
+ * Computes the Merkle root with the provided leaves **synchronously**.
10
+ * This method uses a synchronous hash function (defaults to `sha256Trunc`).
11
+ *
12
+ * @throws If the number of leaves is not a power of two.
13
+ */
14
+ export function computeBalancedMerkleTreeRoot(leaves: Buffer[], hasher = shaMerkleHash): Buffer {
15
+ const height = getTreeHeight(leaves);
16
+ let nodes = leaves.slice();
17
+
18
+ for (let i = 0; i < height; ++i) {
19
+ let j = 0;
20
+ for (; j < nodes.length / 2; ++j) {
21
+ const l = nodes[j * 2];
22
+ const r = nodes[j * 2 + 1];
23
+ nodes[j] = hasher(l, r);
24
+ }
25
+ nodes = nodes.slice(0, j);
26
+ }
27
+
28
+ return nodes[0];
29
+ }
30
+
31
+ /**
32
+ * Computes the Merkle root with the provided leaves **asynchronously**.
33
+ * This method uses an asynchronous hash function (defaults to `poseidon2Hash`).
34
+ *
35
+ * @throws If the number of leaves is not a power of two.
36
+ */
37
+ export async function computeBalancedMerkleTreeRootAsync(
38
+ leaves: Buffer[],
39
+ hasher = poseidonMerkleHash,
40
+ ): Promise<Buffer> {
41
+ const height = getTreeHeight(leaves);
42
+ let nodes = leaves.slice();
43
+
44
+ for (let i = 0; i < height; ++i) {
45
+ let j = 0;
46
+ for (; j < nodes.length / 2; ++j) {
47
+ const l = nodes[j * 2];
48
+ const r = nodes[j * 2 + 1];
49
+ nodes[j] = await hasher(l, r);
50
+ }
51
+ nodes = nodes.slice(0, j);
52
+ }
53
+
54
+ return nodes[0];
55
+ }
56
+
57
+ function getTreeHeight(leaves: Buffer[]) {
58
+ if (leaves.length === 0) {
59
+ throw new Error('Cannot compute a Merkle root with no leaves');
60
+ }
61
+
62
+ const height = Math.log2(leaves.length);
63
+ if (!Number.isInteger(height)) {
64
+ throw new Error('Cannot compute a Merkle root with a non-power-of-two number of leaves');
65
+ }
66
+
67
+ return height;
68
+ }
@@ -1,3 +1,6 @@
1
+ import { poseidon2Hash } from '../crypto/poseidon/index.js';
2
+ import { sha256Trunc } from '../crypto/sha256/index.js';
3
+
1
4
  /**
2
5
  * Defines hasher interface used by Merkle trees.
3
6
  */
@@ -37,3 +40,9 @@ export interface AsyncHasher {
37
40
  */
38
41
  hashInputs(inputs: Buffer[]): Promise<Buffer<ArrayBuffer>>;
39
42
  }
43
+
44
+ export const shaMerkleHash: Hasher['hash'] = (left: Buffer, right: Buffer) =>
45
+ sha256Trunc(Buffer.concat([left, right])) as Buffer<ArrayBuffer>;
46
+
47
+ export const poseidonMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
48
+ (await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>;
@@ -1,5 +1,4 @@
1
- export * from './unbalanced_merkle_tree.js';
2
- export * from './unbalanced_tree_store.js';
1
+ export * from './balanced_merkle_tree_root.js';
3
2
  export * from './merkle_tree_calculator.js';
4
3
  export * from './merkle_tree.js';
5
4
  export * from './indexed_merkle_tree_calculator.js';
@@ -9,3 +8,5 @@ export * from './membership_witness.js';
9
8
  export * from './hasher.js';
10
9
  export * from './indexed_tree_leaf.js';
11
10
  export * from './unbalanced_merkle_tree_calculator.js';
11
+ export * from './unbalanced_merkle_tree_root.js';
12
+ export * from './unbalanced_tree_store.js';
@@ -1,4 +1,4 @@
1
- import { Fr } from '../fields/index.js';
1
+ import { Fr } from '../curves/bn254/index.js';
2
2
  import { assertLength } from '../serialize/index.js';
3
3
  import type { IndexedTreeLeafPreimage } from './indexed_tree_leaf.js';
4
4
  import { MembershipWitness } from './membership_witness.js';
@@ -1,6 +1,6 @@
1
1
  import { assertMemberLength } from '../array/array.js';
2
2
  import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
3
- import { Fr } from '../fields/fields.js';
3
+ import { Fr } from '../curves/bn254/field.js';
4
4
  import { schemas } from '../schemas/schemas.js';
5
5
  import { BufferReader, type Tuple, serializeToBuffer } from '../serialize/index.js';
6
6
  import type { SiblingPath } from './sibling_path.js';
@@ -38,6 +38,14 @@ export class MembershipWitness<N extends number> {
38
38
  return [new Fr(this.leafIndex), ...this.siblingPath];
39
39
  }
40
40
 
41
+ /**
42
+ * Returns a representation of the membership witness as expected by intrinsic Noir deserialization.
43
+ */
44
+ public toNoirRepresentation(): (string | string[])[] {
45
+ // TODO(#12874): remove the stupid as string conversion by modifying ForeignCallOutput type in acvm.js
46
+ return [new Fr(this.leafIndex).toString() as string, this.siblingPath.map(fr => fr.toString()) as string[]];
47
+ }
48
+
41
49
  static schemaFor<N extends number>(size: N) {
42
50
  return schemas.Buffer.transform(b => MembershipWitness.fromBuffer(b, size));
43
51
  }
@@ -1,4 +1,4 @@
1
- import { pedersenHash, sha256Trunc } from '@aztec/foundation/crypto';
1
+ import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';
2
2
 
3
3
  import type { AsyncHasher } from './hasher.js';
4
4
  import { MerkleTree } from './merkle_tree.js';
@@ -19,7 +19,7 @@ export class MerkleTreeCalculator {
19
19
  height: number,
20
20
  zeroLeaf: Buffer = Buffer.alloc(32),
21
21
  hasher = async (left: Buffer, right: Buffer) =>
22
- (await pedersenHash([left, right])).toBuffer() as Buffer<ArrayBuffer>,
22
+ (await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>,
23
23
  ) {
24
24
  const zeroHashes = [zeroLeaf];
25
25
  for (let i = 0; i < height; i++) {
@@ -69,37 +69,4 @@ export class MerkleTreeCalculator {
69
69
 
70
70
  return leaves[0];
71
71
  }
72
-
73
- /**
74
- * Computes the Merkle root with the provided leaves **synchronously**.
75
- *
76
- * This method uses a synchronous hash function (defaults to `sha256Trunc`) and **does not** allow for padding.
77
- * If the number of leaves is not a power of two, it throws an error.
78
- * This contrasts with the above non-static async method `computeTreeRoot`, which can handle any number of leaves by
79
- * padding with zero hashes.
80
- */
81
- static computeTreeRootSync(leaves: Buffer[], hasher = sha256Trunc): Buffer {
82
- if (leaves.length === 0) {
83
- throw new Error('Cannot compute a Merkle root with no leaves');
84
- }
85
-
86
- const height = Math.log2(leaves.length);
87
- if (!Number.isInteger(height)) {
88
- throw new Error('Cannot compute a Merkle root with a non-power-of-two number of leaves');
89
- }
90
-
91
- let nodes = leaves.slice();
92
-
93
- for (let i = 0; i < height; ++i) {
94
- let j = 0;
95
- for (; j < nodes.length / 2; ++j) {
96
- const l = nodes[j * 2];
97
- const r = nodes[j * 2 + 1];
98
- nodes[j] = hasher(Buffer.concat([l, r]));
99
- }
100
- nodes = nodes.slice(0, j);
101
- }
102
-
103
- return nodes[0];
104
- }
105
72
  }
@@ -1,6 +1,6 @@
1
1
  import { makeTuple } from '../array/array.js';
2
- import { pedersenHash } from '../crypto/index.js';
3
- import { Fr } from '../fields/index.js';
2
+ import { poseidon2Hash } from '../crypto/poseidon/index.js';
3
+ import { Fr } from '../curves/bn254/index.js';
4
4
  import { schemas } from '../schemas/index.js';
5
5
  import {
6
6
  type Tuple,
@@ -172,7 +172,7 @@ export async function computeRootFromSiblingPath(
172
172
  leaf: Buffer,
173
173
  siblingPath: Buffer[],
174
174
  index: number,
175
- hasher = async (left: Buffer, right: Buffer) => (await pedersenHash([left, right])).toBuffer(),
175
+ hasher = async (left: Buffer, right: Buffer) => (await poseidon2Hash([left, right])).toBuffer(),
176
176
  ) {
177
177
  let result = leaf;
178
178
  for (const sibling of siblingPath) {
@@ -1,37 +1,41 @@
1
- import { type Bufferable, serializeToBuffer } from '@aztec/foundation/serialize';
2
- import type { AsyncHasher } from '@aztec/foundation/trees';
3
- import { SiblingPath } from '@aztec/foundation/trees';
1
+ import { type Hasher, shaMerkleHash } from './hasher.js';
2
+ import { SiblingPath } from './sibling_path.js';
3
+ import { type TreeNodeLocation, UnbalancedTreeStore } from './unbalanced_tree_store.js';
4
4
 
5
- import { sha256Trunc } from '../crypto/index.js';
6
-
7
- const indexToKeyHash = (level: number, index: bigint) => `${level}:${index}`;
5
+ interface TreeNode {
6
+ value: Buffer;
7
+ leafIndex?: number;
8
+ }
8
9
 
9
10
  /**
10
11
  * An ephemeral unbalanced Merkle tree implementation.
11
12
  * Follows the rollup implementation which greedily hashes pairs of nodes up the tree.
12
13
  * Remaining rightmost nodes are shifted up until they can be paired.
14
+ * The values that match the `valueToCompress` are skipped and the sibling of the compressed leaf are shifted up until
15
+ * they can be paired.
13
16
  * If there is only one leaf, the root is the leaf.
14
17
  */
15
18
  export class UnbalancedMerkleTreeCalculator {
16
- // This map stores index and depth -> value
17
- private cache: { [key: string]: Buffer } = {};
18
- // This map stores value -> index and depth, since we have variable depth
19
- private valueCache: { [key: string]: string } = {};
20
- protected size: bigint = 0n;
21
-
22
- root: Buffer = Buffer.alloc(32);
19
+ private store: UnbalancedTreeStore<TreeNode>;
20
+ private leafLocations: TreeNodeLocation[] = [];
23
21
 
24
22
  public constructor(
25
- private maxDepth: number,
26
- private hasher: AsyncHasher['hash'],
27
- ) {}
23
+ private readonly leaves: Buffer[],
24
+ private readonly valueToCompress: Buffer,
25
+ private readonly emptyRoot: Buffer,
26
+ private readonly hasher: Hasher['hash'],
27
+ ) {
28
+ this.store = new UnbalancedTreeStore(leaves.length);
29
+ this.buildTree();
30
+ }
28
31
 
29
32
  static create(
30
- height: number,
31
- hasher = (left: Buffer, right: Buffer) =>
32
- Promise.resolve(sha256Trunc(Buffer.concat([left, right])) as Buffer<ArrayBuffer>),
33
+ leaves: Buffer[],
34
+ valueToCompress = Buffer.alloc(0),
35
+ emptyRoot = Buffer.alloc(32),
36
+ hasher = shaMerkleHash,
33
37
  ) {
34
- return new UnbalancedMerkleTreeCalculator(height, hasher);
38
+ return new UnbalancedMerkleTreeCalculator(leaves, valueToCompress, emptyRoot, hasher);
35
39
  }
36
40
 
37
41
  /**
@@ -39,108 +43,134 @@ export class UnbalancedMerkleTreeCalculator {
39
43
  * @returns The root of the tree.
40
44
  */
41
45
  public getRoot(): Buffer {
42
- return this.root;
46
+ return this.store.getRoot()!.value;
43
47
  }
44
48
 
45
49
  /**
46
- * Returns a sibling path for the element at the given index.
50
+ * Returns a sibling path for the element.
47
51
  * @param value - The value of the element.
48
52
  * @returns A sibling path for the element.
49
53
  * Note: The sibling path is an array of sibling hashes, with the lowest hash (leaf hash) first, and the highest hash last.
50
54
  */
51
- public getSiblingPath<N extends number>(value: Bufferable): Promise<SiblingPath<N>> {
52
- if (this.size === 1n) {
53
- return Promise.resolve(new SiblingPath<N>(0 as N, []));
55
+ public getSiblingPath<N extends number>(value: Buffer): SiblingPath<N> {
56
+ const leafIndex = this.leaves.findIndex(leaf => leaf.equals(value));
57
+ if (leafIndex === -1) {
58
+ throw Error(`Leaf value ${value.toString('hex')} not found in tree.`);
54
59
  }
55
60
 
56
- const path: Buffer[] = [];
57
- const [depth, _index] = this.valueCache[serializeToBuffer(value).toString('hex')].split(':');
58
- let level = parseInt(depth, 10);
59
- let index = BigInt(_index);
60
- while (level > 0) {
61
- const isRight = index & 0x01n;
62
- const key = indexToKeyHash(level, isRight ? index - 1n : index + 1n);
63
- const sibling = this.cache[key];
64
- path.push(sibling);
65
- level -= 1;
66
- index >>= 1n;
67
- }
68
- return Promise.resolve(new SiblingPath<N>(parseInt(depth, 10) as N, path));
61
+ return this.getSiblingPathByLeafIndex(leafIndex);
69
62
  }
70
63
 
71
64
  /**
72
- * Appends the given leaves to the tree.
73
- * @param leaves - The leaves to append.
74
- * @returns Empty promise.
65
+ * Returns a sibling path for the leaf at the given index.
66
+ * @param leafIndex - The index of the leaf.
67
+ * @returns A sibling path for the leaf.
75
68
  */
76
- public async appendLeaves(leaves: Buffer[]): Promise<void> {
77
- if (this.size != BigInt(0)) {
78
- throw Error(`Can't re-append to an unbalanced tree. Current has ${this.size} leaves.`);
69
+ public getSiblingPathByLeafIndex<N extends number>(leafIndex: number): SiblingPath<N> {
70
+ if (leafIndex >= this.leaves.length) {
71
+ throw Error(`Leaf index ${leafIndex} out of bounds. Tree has ${this.leaves.length} leaves.`);
79
72
  }
80
- if (leaves.length === 0) {
81
- throw Error(`Can't append 0 leaves to an unbalanced tree.`);
73
+
74
+ const leaf = this.leaves[leafIndex];
75
+ if (leaf.equals(this.valueToCompress)) {
76
+ throw Error(`Leaf at index ${leafIndex} has been compressed.`);
82
77
  }
83
78
 
84
- if (leaves.length === 1) {
85
- this.root = leaves[0];
86
- } else {
87
- this.root = await this.batchInsert(leaves);
79
+ const path: Buffer[] = [];
80
+ let location = this.leafLocations[leafIndex];
81
+ while (location.level > 0) {
82
+ const sibling = this.store.getSibling(location)!;
83
+ path.push(sibling.value);
84
+ location = this.store.getParentLocation(location);
88
85
  }
89
86
 
90
- this.size = BigInt(leaves.length);
87
+ return new SiblingPath<N>(path.length as N, path);
88
+ }
91
89
 
92
- return Promise.resolve();
90
+ public getLeafLocation(leafIndex: number) {
91
+ return this.leafLocations[leafIndex];
93
92
  }
94
93
 
95
94
  /**
96
- * Calculates root while adding leaves and nodes to the cache.
97
- * @param leaves - The leaves to append.
98
- * @returns Resulting root of the tree.
95
+ * Adds leaves and nodes to the store. Updates the leafLocations.
96
+ * @param leaves - The leaves of the tree.
99
97
  */
100
- private async batchInsert(_leaves: Buffer[]): Promise<Buffer> {
101
- // If we have an even number of leaves, hash them all in pairs
102
- // Otherwise, store the final leaf to be shifted up to the next odd sized level
103
- let [layerWidth, nodeToShift] =
104
- _leaves.length & 1
105
- ? [_leaves.length - 1, serializeToBuffer(_leaves[_leaves.length - 1])]
106
- : [_leaves.length, Buffer.alloc(0)];
107
- // Allocate this layer's leaves and init the next layer up
108
- let thisLayer = _leaves.slice(0, layerWidth).map(l => serializeToBuffer(l));
109
- let nextLayer = [];
110
- // Store the bottom level leaves
111
- thisLayer.forEach((leaf, i) => this.storeNode(leaf, this.maxDepth, BigInt(i)));
112
- for (let i = 0; i < this.maxDepth; i++) {
113
- for (let j = 0; j < layerWidth; j += 2) {
114
- // Store the hash of each pair one layer up
115
- nextLayer[j / 2] = await this.hasher(serializeToBuffer(thisLayer[j]), serializeToBuffer(thisLayer[j + 1]));
116
- this.storeNode(nextLayer[j / 2], this.maxDepth - i - 1, BigInt(j >> 1));
117
- }
118
- layerWidth /= 2;
119
- if (layerWidth & 1) {
120
- if (nodeToShift.length) {
121
- // If the next layer has odd length, and we have a node that needs to be shifted up, add it here
122
- nextLayer.push(serializeToBuffer(nodeToShift));
123
- this.storeNode(nodeToShift, this.maxDepth - i - 1, BigInt((layerWidth * 2) >> 1));
124
- layerWidth += 1;
125
- nodeToShift = Buffer.alloc(0);
98
+ private buildTree() {
99
+ this.leafLocations = this.leaves.map((value, i) => this.store.setLeaf(i, { value, leafIndex: i }));
100
+
101
+ // Start with the leaves that are not compressed.
102
+ let toProcess = this.leafLocations.filter((_, i) => !this.leaves[i].equals(this.valueToCompress));
103
+ if (!toProcess.length) {
104
+ // All leaves are compressed. Set empty root to the root.
105
+ this.store.setNode({ level: 0, index: 0 }, { value: this.emptyRoot });
106
+ return;
107
+ }
108
+
109
+ const level = toProcess[0].level;
110
+ for (let i = level; i > 0; i--) {
111
+ const toProcessNext = [];
112
+ for (const location of toProcess) {
113
+ if (location.level !== i) {
114
+ toProcessNext.push(location);
115
+ continue;
116
+ }
117
+
118
+ const parentLocation = this.store.getParentLocation(location);
119
+ if (this.store.getNode(parentLocation)) {
120
+ // Parent has been updated by its (left) sibling.
121
+ continue;
122
+ }
123
+
124
+ const sibling = this.store.getSibling(location);
125
+ // If sibling is undefined, all its children are compressed.
126
+ const shouldShiftUp = !sibling || sibling.value.equals(this.valueToCompress);
127
+ if (shouldShiftUp) {
128
+ // The node becomes the parent if the sibling is a compressed leaf.
129
+ this.shiftNodeUp(location, parentLocation);
126
130
  } else {
127
- // If we don't have a node waiting to be shifted, store the next layer's final node to be shifted
128
- layerWidth -= 1;
129
- nodeToShift = nextLayer[layerWidth];
131
+ // Hash the value with the (right) sibling and update the parent node.
132
+ const node = this.store.getNode(location)!;
133
+ const parentValue = this.hasher(node.value, sibling.value);
134
+ this.store.setNode(parentLocation, { value: parentValue });
130
135
  }
136
+
137
+ // Add the parent location to be processed next.
138
+ toProcessNext.push(parentLocation);
131
139
  }
132
- // reset the layers
133
- thisLayer = nextLayer;
134
- nextLayer = [];
140
+
141
+ toProcess = toProcessNext;
135
142
  }
143
+ }
136
144
 
137
- // return the root
138
- return thisLayer[0];
145
+ private shiftNodeUp(from: TreeNodeLocation, to: TreeNodeLocation) {
146
+ // Collect all nodes that need to shift.
147
+ const nodesToShift = this.collectNodesToShift(from, to);
148
+ // Move all nodes to their new locations.
149
+ for (const { node, newLocation } of nodesToShift) {
150
+ this.store.setNode(newLocation, node);
151
+ if (node.leafIndex !== undefined) {
152
+ this.leafLocations[node.leafIndex] = newLocation;
153
+ }
154
+ }
139
155
  }
140
156
 
141
- private storeNode(value: Buffer, depth: number, index: bigint) {
142
- const key = indexToKeyHash(depth, index);
143
- this.cache[key] = value;
144
- this.valueCache[value.toString('hex')] = key;
157
+ private collectNodesToShift(from: TreeNodeLocation, to: TreeNodeLocation) {
158
+ const node = this.store.getNode(from);
159
+ if (!node) {
160
+ return [];
161
+ }
162
+
163
+ let result = [{ node, newLocation: to }];
164
+
165
+ // If not a leaf, collect its children.
166
+ if (node.leafIndex === undefined) {
167
+ const [leftChild, rightChild] = this.store.getChildLocations(from);
168
+ const [leftChildNew, rightChildNew] = this.store.getChildLocations(to);
169
+ result = result
170
+ .concat(this.collectNodesToShift(leftChild, leftChildNew))
171
+ .concat(this.collectNodesToShift(rightChild, rightChildNew));
172
+ }
173
+
174
+ return result;
145
175
  }
146
176
  }