@aztec/foundation 0.0.1-commit.03f7ef2 → 0.0.1-commit.04d373f

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 (367) hide show
  1. package/dest/array/index.d.ts +2 -1
  2. package/dest/array/index.d.ts.map +1 -1
  3. package/dest/array/index.js +1 -0
  4. package/dest/array/sorted_array.d.ts +15 -0
  5. package/dest/array/sorted_array.d.ts.map +1 -0
  6. package/dest/array/sorted_array.js +109 -0
  7. package/dest/bigint-buffer/index.d.ts +1 -1
  8. package/dest/bigint-buffer/index.d.ts.map +1 -1
  9. package/dest/bigint-buffer/index.js +6 -1
  10. package/dest/branded-types/block_number.d.ts +6 -3
  11. package/dest/branded-types/block_number.d.ts.map +1 -1
  12. package/dest/branded-types/block_number.js +18 -4
  13. package/dest/branded-types/buffer32_hash.d.ts +27 -0
  14. package/dest/branded-types/buffer32_hash.d.ts.map +1 -0
  15. package/dest/branded-types/buffer32_hash.js +12 -0
  16. package/dest/branded-types/checkpoint_number.d.ts +17 -3
  17. package/dest/branded-types/checkpoint_number.d.ts.map +1 -1
  18. package/dest/branded-types/checkpoint_number.js +28 -8
  19. package/dest/branded-types/epoch.d.ts +2 -2
  20. package/dest/branded-types/epoch.d.ts.map +1 -1
  21. package/dest/branded-types/index.d.ts +4 -2
  22. package/dest/branded-types/index.d.ts.map +1 -1
  23. package/dest/branded-types/index.js +3 -1
  24. package/dest/branded-types/index_within_checkpoint.d.ts +42 -0
  25. package/dest/branded-types/index_within_checkpoint.d.ts.map +1 -0
  26. package/dest/branded-types/index_within_checkpoint.js +59 -0
  27. package/dest/branded-types/slot.d.ts +5 -2
  28. package/dest/branded-types/slot.d.ts.map +1 -1
  29. package/dest/branded-types/slot.js +3 -0
  30. package/dest/buffer/buffer16.d.ts +6 -1
  31. package/dest/buffer/buffer16.d.ts.map +1 -1
  32. package/dest/buffer/buffer16.js +3 -1
  33. package/dest/buffer/buffer32.d.ts +28 -58
  34. package/dest/buffer/buffer32.d.ts.map +1 -1
  35. package/dest/buffer/buffer32.js +32 -64
  36. package/dest/buffer/index.d.ts +2 -1
  37. package/dest/buffer/index.d.ts.map +1 -1
  38. package/dest/buffer/index.js +1 -0
  39. package/dest/buffer/utils.d.ts +3 -0
  40. package/dest/buffer/utils.d.ts.map +1 -0
  41. package/dest/buffer/utils.js +7 -0
  42. package/dest/collection/array.d.ts +22 -1
  43. package/dest/collection/array.d.ts.map +1 -1
  44. package/dest/collection/array.js +84 -0
  45. package/dest/collection/index.d.ts +3 -1
  46. package/dest/collection/index.d.ts.map +1 -1
  47. package/dest/collection/index.js +2 -0
  48. package/dest/collection/lru_map.d.ts +41 -0
  49. package/dest/collection/lru_map.d.ts.map +1 -0
  50. package/dest/collection/lru_map.js +119 -0
  51. package/dest/collection/lru_set.d.ts +35 -0
  52. package/dest/collection/lru_set.d.ts.map +1 -0
  53. package/dest/collection/lru_set.js +95 -0
  54. package/dest/committable/committable.js +1 -1
  55. package/dest/config/env_var.d.ts +2 -2
  56. package/dest/config/env_var.d.ts.map +1 -1
  57. package/dest/config/index.d.ts +51 -31
  58. package/dest/config/index.d.ts.map +1 -1
  59. package/dest/config/index.js +59 -50
  60. package/dest/config/network_config.d.ts +15 -41
  61. package/dest/config/network_config.d.ts.map +1 -1
  62. package/dest/config/network_config.js +4 -1
  63. package/dest/config/network_name.d.ts +2 -2
  64. package/dest/config/network_name.d.ts.map +1 -1
  65. package/dest/config/network_name.js +2 -2
  66. package/dest/config/parse-env.d.ts +3 -0
  67. package/dest/config/parse-env.d.ts.map +1 -0
  68. package/dest/config/parse-env.js +7 -0
  69. package/dest/config/secret_value.d.ts +2 -2
  70. package/dest/config/secret_value.d.ts.map +1 -1
  71. package/dest/config/secret_value.js +3 -1
  72. package/dest/crypto/aes128/index.d.ts +2 -1
  73. package/dest/crypto/aes128/index.d.ts.map +1 -1
  74. package/dest/crypto/aes128/index.js +11 -2
  75. package/dest/crypto/bls/bn254_keystore.d.ts +9 -176
  76. package/dest/crypto/bls/bn254_keystore.d.ts.map +1 -1
  77. package/dest/crypto/bls/bn254_keystore.js +0 -17
  78. package/dest/crypto/ecdsa/signature.d.ts +10 -1
  79. package/dest/crypto/ecdsa/signature.d.ts.map +1 -1
  80. package/dest/crypto/poseidon/index.d.ts +1 -2
  81. package/dest/crypto/poseidon/index.d.ts.map +1 -1
  82. package/dest/crypto/poseidon/index.js +40 -42
  83. package/dest/crypto/random/randomness_singleton.d.ts +4 -3
  84. package/dest/crypto/random/randomness_singleton.d.ts.map +1 -1
  85. package/dest/crypto/random/randomness_singleton.js +5 -5
  86. package/dest/crypto/schnorr/index.d.ts +8 -7
  87. package/dest/crypto/schnorr/index.d.ts.map +1 -1
  88. package/dest/crypto/schnorr/index.js +6 -6
  89. package/dest/crypto/schnorr/signature.d.ts +9 -29
  90. package/dest/crypto/schnorr/signature.d.ts.map +1 -1
  91. package/dest/crypto/schnorr/signature.js +20 -36
  92. package/dest/crypto/secp256k1-signer/utils.d.ts +12 -1
  93. package/dest/crypto/secp256k1-signer/utils.d.ts.map +1 -1
  94. package/dest/crypto/secp256k1-signer/utils.js +26 -0
  95. package/dest/crypto/sync/poseidon/index.d.ts +1 -2
  96. package/dest/crypto/sync/poseidon/index.d.ts.map +1 -1
  97. package/dest/crypto/sync/poseidon/index.js +0 -8
  98. package/dest/curves/bls12/field.d.ts +3 -3
  99. package/dest/curves/bls12/field.d.ts.map +1 -1
  100. package/dest/curves/bls12/field.js +6 -8
  101. package/dest/curves/bls12/point.d.ts +11 -2
  102. package/dest/curves/bls12/point.d.ts.map +1 -1
  103. package/dest/curves/bls12/point.js +3 -1
  104. package/dest/curves/bn254/field.d.ts +25 -19
  105. package/dest/curves/bn254/field.d.ts.map +1 -1
  106. package/dest/curves/bn254/field.js +36 -21
  107. package/dest/curves/grumpkin/point.d.ts +21 -26
  108. package/dest/curves/grumpkin/point.d.ts.map +1 -1
  109. package/dest/curves/grumpkin/point.js +29 -43
  110. package/dest/error/index.d.ts +4 -4
  111. package/dest/error/index.d.ts.map +1 -1
  112. package/dest/eth-address/index.d.ts +2 -2
  113. package/dest/eth-address/index.d.ts.map +1 -1
  114. package/dest/eth-address/index.js +4 -5
  115. package/dest/eth-signature/eth_signature.d.ts +6 -2
  116. package/dest/eth-signature/eth_signature.d.ts.map +1 -1
  117. package/dest/eth-signature/eth_signature.js +7 -2
  118. package/dest/fifo/fifo_frame_reader.d.ts +41 -0
  119. package/dest/fifo/fifo_frame_reader.d.ts.map +1 -0
  120. package/dest/fifo/fifo_frame_reader.js +74 -0
  121. package/dest/fifo/index.d.ts +2 -0
  122. package/dest/fifo/index.d.ts.map +1 -0
  123. package/dest/fifo/index.js +1 -0
  124. package/dest/fifo_set/fifo_set.d.ts +15 -0
  125. package/dest/fifo_set/fifo_set.d.ts.map +1 -0
  126. package/dest/fifo_set/fifo_set.js +39 -0
  127. package/dest/fifo_set/index.d.ts +2 -0
  128. package/dest/fifo_set/index.d.ts.map +1 -0
  129. package/dest/fifo_set/index.js +1 -0
  130. package/dest/jest/setup.js +28 -1
  131. package/dest/json-rpc/client/fetch.d.ts +1 -1
  132. package/dest/json-rpc/client/fetch.d.ts.map +1 -1
  133. package/dest/json-rpc/client/fetch.js +4 -3
  134. package/dest/json-rpc/client/safe_json_rpc_client.d.ts +2 -1
  135. package/dest/json-rpc/client/safe_json_rpc_client.d.ts.map +1 -1
  136. package/dest/json-rpc/client/safe_json_rpc_client.js +3 -3
  137. package/dest/json-rpc/client/undici.d.ts +1 -1
  138. package/dest/json-rpc/client/undici.d.ts.map +1 -1
  139. package/dest/json-rpc/client/undici.js +23 -4
  140. package/dest/json-rpc/fixtures/class_a.d.ts +3 -3
  141. package/dest/json-rpc/fixtures/class_a.d.ts.map +1 -1
  142. package/dest/json-rpc/fixtures/class_b.d.ts +3 -3
  143. package/dest/json-rpc/fixtures/class_b.d.ts.map +1 -1
  144. package/dest/json-rpc/fixtures/test_state.d.ts +4 -8
  145. package/dest/json-rpc/fixtures/test_state.d.ts.map +1 -1
  146. package/dest/json-rpc/fixtures/test_state.js +57 -17
  147. package/dest/json-rpc/server/api_key_auth.d.ts +19 -0
  148. package/dest/json-rpc/server/api_key_auth.d.ts.map +1 -0
  149. package/dest/json-rpc/server/api_key_auth.js +57 -0
  150. package/dest/json-rpc/server/index.d.ts +2 -1
  151. package/dest/json-rpc/server/index.d.ts.map +1 -1
  152. package/dest/json-rpc/server/index.js +1 -0
  153. package/dest/json-rpc/server/safe_json_rpc_server.d.ts +8 -3
  154. package/dest/json-rpc/server/safe_json_rpc_server.d.ts.map +1 -1
  155. package/dest/json-rpc/server/safe_json_rpc_server.js +28 -14
  156. package/dest/log/bigint-utils.d.ts +5 -0
  157. package/dest/log/bigint-utils.d.ts.map +1 -0
  158. package/dest/log/bigint-utils.js +21 -0
  159. package/dest/log/gcloud-logger-config.d.ts +1 -1
  160. package/dest/log/gcloud-logger-config.d.ts.map +1 -1
  161. package/dest/log/gcloud-logger-config.js +3 -0
  162. package/dest/log/libp2p_logger.d.ts +5 -2
  163. package/dest/log/libp2p_logger.d.ts.map +1 -1
  164. package/dest/log/libp2p_logger.js +14 -4
  165. package/dest/log/log-filters.d.ts +17 -4
  166. package/dest/log/log-filters.d.ts.map +1 -1
  167. package/dest/log/log-filters.js +26 -12
  168. package/dest/log/pino-logger-server.d.ts +9 -0
  169. package/dest/log/pino-logger-server.d.ts.map +1 -0
  170. package/dest/log/pino-logger-server.js +18 -0
  171. package/dest/log/pino-logger.d.ts +37 -8
  172. package/dest/log/pino-logger.d.ts.map +1 -1
  173. package/dest/log/pino-logger.js +138 -30
  174. package/dest/noir/noir_package_config.d.ts +19 -104
  175. package/dest/noir/noir_package_config.d.ts.map +1 -1
  176. package/dest/noir/noir_package_config.js +1 -1
  177. package/dest/queue/base_memory_queue.d.ts +2 -2
  178. package/dest/queue/base_memory_queue.d.ts.map +1 -1
  179. package/dest/queue/batch_queue.d.ts +1 -1
  180. package/dest/queue/batch_queue.d.ts.map +1 -1
  181. package/dest/queue/batch_queue.js +1 -0
  182. package/dest/queue/semaphore.d.ts +5 -1
  183. package/dest/queue/semaphore.d.ts.map +1 -1
  184. package/dest/retry/index.d.ts +22 -1
  185. package/dest/retry/index.d.ts.map +1 -1
  186. package/dest/retry/index.js +32 -0
  187. package/dest/schemas/api.d.ts +12 -10
  188. package/dest/schemas/api.d.ts.map +1 -1
  189. package/dest/schemas/api.js +7 -1
  190. package/dest/schemas/parse.d.ts +4 -4
  191. package/dest/schemas/parse.d.ts.map +1 -1
  192. package/dest/schemas/parse.js +6 -5
  193. package/dest/schemas/schemas.d.ts +15 -15
  194. package/dest/schemas/types.d.ts +3 -3
  195. package/dest/schemas/types.d.ts.map +1 -1
  196. package/dest/schemas/utils.d.ts +7 -11
  197. package/dest/schemas/utils.d.ts.map +1 -1
  198. package/dest/schemas/utils.js +2 -18
  199. package/dest/serialize/buffer_reader.d.ts +14 -4
  200. package/dest/serialize/buffer_reader.d.ts.map +1 -1
  201. package/dest/serialize/buffer_reader.js +26 -4
  202. package/dest/serialize/buffer_sink.d.ts +134 -0
  203. package/dest/serialize/buffer_sink.d.ts.map +1 -0
  204. package/dest/serialize/buffer_sink.js +297 -0
  205. package/dest/serialize/index.d.ts +2 -1
  206. package/dest/serialize/index.d.ts.map +1 -1
  207. package/dest/serialize/index.js +1 -0
  208. package/dest/serialize/serialize.d.ts +19 -1
  209. package/dest/serialize/serialize.d.ts.map +1 -1
  210. package/dest/serialize/serialize.js +31 -0
  211. package/dest/sleep/index.d.ts +2 -1
  212. package/dest/sleep/index.d.ts.map +1 -1
  213. package/dest/sleep/index.js +10 -1
  214. package/dest/string/index.js +1 -1
  215. package/dest/timer/date.d.ts +25 -1
  216. package/dest/timer/date.d.ts.map +1 -1
  217. package/dest/timer/date.js +33 -0
  218. package/dest/timer/index.d.ts +2 -2
  219. package/dest/timer/index.d.ts.map +1 -1
  220. package/dest/timer/index.js +1 -1
  221. package/dest/timer/timeout.d.ts +3 -1
  222. package/dest/timer/timeout.d.ts.map +1 -1
  223. package/dest/timer/timeout.js +26 -0
  224. package/dest/transport/dispatch/create_dispatch_proxy.d.ts +11 -2
  225. package/dest/transport/dispatch/create_dispatch_proxy.d.ts.map +1 -1
  226. package/dest/transport/index.d.ts +1 -2
  227. package/dest/transport/index.d.ts.map +1 -1
  228. package/dest/transport/index.js +0 -1
  229. package/dest/transport/transport_client.js +2 -2
  230. package/dest/trees/balanced_merkle_tree_root.d.ts +16 -0
  231. package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -0
  232. package/dest/trees/{balanced_merkle_tree.js → balanced_merkle_tree_root.js} +3 -19
  233. package/dest/trees/hasher.d.ts +4 -1
  234. package/dest/trees/hasher.d.ts.map +1 -1
  235. package/dest/trees/hasher.js +10 -5
  236. package/dest/trees/index.d.ts +4 -4
  237. package/dest/trees/index.d.ts.map +1 -1
  238. package/dest/trees/index.js +3 -3
  239. package/dest/trees/indexed_merkle_tree_calculator.d.ts +1 -1
  240. package/dest/trees/indexed_merkle_tree_calculator.d.ts.map +1 -1
  241. package/dest/trees/indexed_merkle_tree_calculator.js +5 -1
  242. package/dest/trees/membership_witness.d.ts +12 -2
  243. package/dest/trees/membership_witness.d.ts.map +1 -1
  244. package/dest/trees/membership_witness.js +9 -0
  245. package/dest/trees/merkle_tree_calculator.d.ts +4 -2
  246. package/dest/trees/merkle_tree_calculator.d.ts.map +1 -1
  247. package/dest/trees/merkle_tree_calculator.js +1 -5
  248. package/dest/trees/sibling_path.d.ts +6 -4
  249. package/dest/trees/sibling_path.d.ts.map +1 -1
  250. package/dest/trees/sibling_path.js +10 -8
  251. package/dest/trees/unbalanced_merkle_tree_calculator.d.ts +2 -3
  252. package/dest/trees/unbalanced_merkle_tree_calculator.d.ts.map +1 -1
  253. package/dest/trees/unbalanced_merkle_tree_calculator.js +1 -5
  254. package/dest/trees/{unbalanced_merkle_tree.d.ts → unbalanced_merkle_tree_root.d.ts} +4 -7
  255. package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -0
  256. package/dest/trees/{unbalanced_merkle_tree.js → unbalanced_merkle_tree_root.js} +12 -51
  257. package/dest/trees/unbalanced_tree_store.d.ts +5 -1
  258. package/dest/trees/unbalanced_tree_store.d.ts.map +1 -1
  259. package/dest/trees/unbalanced_tree_store.js +49 -1
  260. package/dest/types/index.d.ts +12 -2
  261. package/dest/types/index.d.ts.map +1 -1
  262. package/dest/types/index.js +3 -0
  263. package/package.json +29 -6
  264. package/src/array/index.ts +1 -0
  265. package/src/array/sorted_array.ts +138 -0
  266. package/src/bigint-buffer/index.ts +6 -1
  267. package/src/branded-types/block_number.ts +25 -2
  268. package/src/branded-types/buffer32_hash.ts +38 -0
  269. package/src/branded-types/checkpoint_number.ts +37 -7
  270. package/src/branded-types/epoch.ts +1 -1
  271. package/src/branded-types/index.ts +3 -1
  272. package/src/branded-types/index_within_checkpoint.ts +88 -0
  273. package/src/branded-types/slot.ts +6 -1
  274. package/src/buffer/buffer16.ts +3 -0
  275. package/src/buffer/buffer32.ts +39 -68
  276. package/src/buffer/index.ts +1 -0
  277. package/src/buffer/utils.ts +8 -0
  278. package/src/collection/array.ts +86 -0
  279. package/src/collection/index.ts +2 -0
  280. package/src/collection/lru_map.ts +143 -0
  281. package/src/collection/lru_set.ts +115 -0
  282. package/src/committable/committable.ts +1 -1
  283. package/src/config/env_var.ts +96 -30
  284. package/src/config/index.ts +134 -99
  285. package/src/config/network_config.ts +3 -0
  286. package/src/config/network_name.ts +4 -4
  287. package/src/config/parse-env.ts +4 -0
  288. package/src/config/secret_value.ts +1 -1
  289. package/src/crypto/aes128/index.ts +11 -2
  290. package/src/crypto/bls/bn254_keystore.ts +0 -23
  291. package/src/crypto/poseidon/index.ts +42 -44
  292. package/src/crypto/random/randomness_singleton.ts +6 -4
  293. package/src/crypto/schnorr/index.ts +9 -8
  294. package/src/crypto/schnorr/signature.ts +17 -46
  295. package/src/crypto/secp256k1-signer/utils.ts +32 -0
  296. package/src/crypto/sync/poseidon/index.ts +0 -9
  297. package/src/curves/bls12/field.ts +0 -7
  298. package/src/curves/bn254/field.ts +45 -36
  299. package/src/curves/grumpkin/point.ts +23 -40
  300. package/src/eth-address/index.ts +1 -5
  301. package/src/eth-signature/eth_signature.ts +7 -1
  302. package/src/fifo/fifo_frame_reader.ts +98 -0
  303. package/src/fifo/index.ts +1 -0
  304. package/src/fifo_set/fifo_set.ts +52 -0
  305. package/src/fifo_set/index.ts +1 -0
  306. package/src/jest/setup.mjs +31 -1
  307. package/src/json-rpc/client/fetch.ts +4 -3
  308. package/src/json-rpc/client/safe_json_rpc_client.ts +4 -2
  309. package/src/json-rpc/client/undici.ts +23 -3
  310. package/src/json-rpc/fixtures/test_state.ts +10 -10
  311. package/src/json-rpc/server/api_key_auth.ts +63 -0
  312. package/src/json-rpc/server/index.ts +1 -0
  313. package/src/json-rpc/server/safe_json_rpc_server.ts +32 -14
  314. package/src/log/bigint-utils.ts +25 -0
  315. package/src/log/gcloud-logger-config.ts +5 -0
  316. package/src/log/libp2p_logger.ts +12 -5
  317. package/src/log/log-filters.ts +29 -11
  318. package/src/log/pino-logger-server.ts +25 -0
  319. package/src/log/pino-logger.ts +162 -40
  320. package/src/noir/noir_package_config.ts +32 -20
  321. package/src/queue/base_memory_queue.ts +1 -1
  322. package/src/queue/batch_queue.ts +1 -0
  323. package/src/queue/semaphore.ts +5 -0
  324. package/src/retry/index.ts +48 -0
  325. package/src/schemas/api.ts +25 -25
  326. package/src/schemas/parse.ts +9 -6
  327. package/src/schemas/schemas.ts +4 -4
  328. package/src/schemas/types.ts +2 -2
  329. package/src/schemas/utils.ts +4 -38
  330. package/src/serialize/buffer_reader.ts +36 -9
  331. package/src/serialize/buffer_sink.ts +350 -0
  332. package/src/serialize/index.ts +1 -0
  333. package/src/serialize/serialize.ts +32 -0
  334. package/src/sleep/index.ts +10 -1
  335. package/src/string/index.ts +1 -1
  336. package/src/timer/date.ts +48 -0
  337. package/src/timer/index.ts +1 -1
  338. package/src/timer/timeout.ts +31 -0
  339. package/src/transport/dispatch/create_dispatch_proxy.ts +11 -1
  340. package/src/transport/index.ts +0 -1
  341. package/src/transport/transport_client.ts +2 -2
  342. package/src/trees/{balanced_merkle_tree.ts → balanced_merkle_tree_root.ts} +3 -22
  343. package/src/trees/hasher.ts +12 -0
  344. package/src/trees/index.ts +3 -3
  345. package/src/trees/indexed_merkle_tree_calculator.ts +5 -1
  346. package/src/trees/membership_witness.ts +8 -0
  347. package/src/trees/merkle_tree_calculator.ts +4 -10
  348. package/src/trees/sibling_path.ts +11 -6
  349. package/src/trees/unbalanced_merkle_tree_calculator.ts +1 -12
  350. package/src/trees/{unbalanced_merkle_tree.ts → unbalanced_merkle_tree_root.ts} +17 -64
  351. package/src/trees/unbalanced_tree_store.ts +57 -2
  352. package/src/types/index.ts +20 -1
  353. package/dest/crypto/serialize.d.ts +0 -51
  354. package/dest/crypto/serialize.d.ts.map +0 -1
  355. package/dest/crypto/serialize.js +0 -68
  356. package/dest/json-rpc/server/telemetry.d.ts +0 -2
  357. package/dest/json-rpc/server/telemetry.d.ts.map +0 -1
  358. package/dest/json-rpc/server/telemetry.js +0 -0
  359. package/dest/transport/dispatch/create_dispatch_fn.d.ts +0 -25
  360. package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +0 -1
  361. package/dest/transport/dispatch/create_dispatch_fn.js +0 -17
  362. package/dest/trees/balanced_merkle_tree.d.ts +0 -22
  363. package/dest/trees/balanced_merkle_tree.d.ts.map +0 -1
  364. package/dest/trees/unbalanced_merkle_tree.d.ts.map +0 -1
  365. package/src/crypto/serialize.ts +0 -85
  366. package/src/json-rpc/server/telemetry.ts +0 -0
  367. package/src/transport/dispatch/create_dispatch_fn.ts +0 -35
@@ -0,0 +1,138 @@
1
+ type Cmp<T> = (a: T, b: T) => -1 | 0 | 1;
2
+
3
+ export function dedupeSortedArray<T>(arr: T[], cmp: Cmp<T>): void {
4
+ for (let i = 0; i < arr.length; i++) {
5
+ let j = i + 1;
6
+ for (; j < arr.length; j++) {
7
+ const res = cmp(arr[i], arr[j]);
8
+ if (res === 0) {
9
+ continue;
10
+ } else if (res < 0) {
11
+ break;
12
+ } else {
13
+ throw new Error('Array not sorted');
14
+ }
15
+ }
16
+
17
+ if (j - i > 1) {
18
+ arr.splice(i + 1, j - i - 1);
19
+ }
20
+ }
21
+ }
22
+
23
+ export function insertIntoSortedArray<T>(arr: T[], item: T, cmp: Cmp<T>, allowDuplicates = true): boolean {
24
+ const index = findInsertionIndexInSortedArray(arr, item, cmp);
25
+
26
+ if (!allowDuplicates) {
27
+ // Check element before insertion point (upper bound returns index after equal elements)
28
+ if (index > 0 && cmp(arr[index - 1], item) === 0) {
29
+ return false;
30
+ }
31
+ }
32
+
33
+ arr.splice(index, 0, item);
34
+ return true;
35
+ }
36
+
37
+ /**
38
+ * Finds the index where needle would be inserted to maintain sorted order.
39
+ * Returns the count of elements less than or equal to needle.
40
+ */
41
+ export function findInsertionIndexInSortedArray<T, N>(values: T[], needle: N, cmp: (a: T, b: N) => number): number {
42
+ let start = 0;
43
+ let end = values.length;
44
+
45
+ while (start < end) {
46
+ const mid = start + (((end - start) / 2) | 0);
47
+ const comparison = cmp(values[mid], needle);
48
+
49
+ if (comparison <= 0) {
50
+ start = mid + 1;
51
+ } else {
52
+ end = mid;
53
+ }
54
+ }
55
+
56
+ return start;
57
+ }
58
+
59
+ export function findIndexInSortedArray<T, N>(values: T[], needle: N, cmp: (a: T, b: N) => number): number {
60
+ let start = 0;
61
+ let end = values.length - 1;
62
+
63
+ while (start <= end) {
64
+ const mid = start + (((end - start) / 2) | 0);
65
+ const comparison = cmp(values[mid], needle);
66
+
67
+ if (comparison === 0) {
68
+ return mid;
69
+ }
70
+
71
+ if (comparison > 0) {
72
+ end = mid - 1;
73
+ } else {
74
+ start = mid + 1;
75
+ }
76
+ }
77
+
78
+ return -1;
79
+ }
80
+
81
+ export function findInSortedArray<T, N>(values: T[], needle: N, cmp: (a: T, b: N) => number): T | undefined {
82
+ const idx = findIndexInSortedArray(values, needle, cmp);
83
+ return idx > -1 ? values[idx] : undefined;
84
+ }
85
+
86
+ export function removeAnyOf<T, N>(arr: T[], vals: N[], cmp: (a: T, b: N) => -1 | 0 | 1): void {
87
+ let writeIdx = 0;
88
+ let readIdx = 0;
89
+ let valIdx = 0;
90
+
91
+ while (readIdx < arr.length && valIdx < vals.length) {
92
+ const comparison = cmp(arr[readIdx], vals[valIdx]);
93
+
94
+ if (comparison < 0) {
95
+ arr[writeIdx++] = arr[readIdx++];
96
+ } else if (comparison > 0) {
97
+ valIdx++;
98
+ } else {
99
+ readIdx++;
100
+ }
101
+ }
102
+
103
+ while (readIdx < arr.length) {
104
+ arr[writeIdx++] = arr[readIdx++];
105
+ }
106
+
107
+ arr.length = writeIdx;
108
+ }
109
+
110
+ export function removeFromSortedArray<T, N>(arr: T[], val: N, cmp: (a: T, b: N) => -1 | 0 | 1) {
111
+ const idx = findIndexInSortedArray(arr, val, cmp);
112
+ if (idx > -1) {
113
+ arr.splice(idx, 1);
114
+ }
115
+ }
116
+
117
+ export function merge<T>(arr: T[], toInsert: T[], cmp: (a: T, b: T) => -1 | 0 | 1): void {
118
+ const result = new Array<T>(arr.length + toInsert.length);
119
+ let i = 0,
120
+ j = 0,
121
+ k = 0;
122
+
123
+ while (i < arr.length && j < toInsert.length) {
124
+ result[k++] = cmp(arr[i], toInsert[j]) <= 0 ? arr[i++] : toInsert[j++];
125
+ }
126
+
127
+ while (i < arr.length) {
128
+ result[k++] = arr[i++];
129
+ }
130
+ while (j < toInsert.length) {
131
+ result[k++] = toInsert[j++];
132
+ }
133
+
134
+ for (i = 0; i < result.length; i++) {
135
+ arr[i] = result[i];
136
+ }
137
+ arr.length = result.length;
138
+ }
@@ -49,9 +49,14 @@ export function toBufferLE(num: bigint, width: number): Buffer {
49
49
  * @returns A big-endian buffer representation of num.
50
50
  */
51
51
  export function toBufferBE(num: bigint, width: number): Buffer {
52
- if (num < BigInt(0)) {
52
+ if (num < 0n) {
53
53
  throw new Error(`Cannot convert negative bigint ${num.toString()} to buffer with toBufferBE.`);
54
54
  }
55
+ // The values serialized in the hot paths are overwhelmingly zero (field elements are mostly
56
+ // zero-padding in protocol structs), and the hex round-trip is wasteful for them.
57
+ if (num === 0n) {
58
+ return Buffer.alloc(width);
59
+ }
55
60
  const hex = num.toString(16);
56
61
  const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
57
62
  if (buffer.length > width) {
@@ -87,15 +87,38 @@ BlockNumber.isValid = function (value: unknown): value is BlockNumber {
87
87
  return typeof value === 'number' && Number.isInteger(value) && value >= 0;
88
88
  };
89
89
 
90
+ /** Increments a BlockNumber by a given value. */
91
+ BlockNumber.add = function (bn: BlockNumber, increment: number): BlockNumber {
92
+ return BlockNumber(bn + increment);
93
+ };
94
+
90
95
  /**
91
96
  * The zero block value (genesis block).
92
97
  */
93
98
  BlockNumber.ZERO = BlockNumber(0);
94
99
 
95
100
  function makeBlockNumberSchema(minValue: number) {
101
+ const max = Number.MAX_SAFE_INTEGER;
96
102
  return z
97
- .union([z.number(), z.bigint(), z.string()])
98
- .pipe(z.coerce.number().int().min(minValue))
103
+ .union([
104
+ z.number().int().min(minValue).max(max),
105
+ z
106
+ .bigint()
107
+ .min(BigInt(minValue))
108
+ .max(BigInt(max))
109
+ .transform(v => Number(v)),
110
+ z
111
+ .string()
112
+ .regex(/^\d+$/, 'BlockNumber string must be a non-negative decimal integer')
113
+ .transform((v, ctx) => {
114
+ const parsed = Number(v);
115
+ if (!Number.isInteger(parsed) || parsed > max || parsed < minValue) {
116
+ ctx.addIssue({ code: z.ZodIssueCode.custom, message: `BlockNumber out of range: ${v}` });
117
+ return z.NEVER;
118
+ }
119
+ return parsed;
120
+ }),
121
+ ])
99
122
  .transform(value => BlockNumber(value));
100
123
  }
101
124
 
@@ -0,0 +1,38 @@
1
+ import type { Branded } from './types.js';
2
+
3
+ /**
4
+ * A branded `0x`-prefixed hex string representing a checkpoint proposal payload hash.
5
+ *
6
+ * A `CheckpointProposal` and the matching `CheckpointAttestation` sign the same
7
+ * `ConsensusPayload`, so they share this hash type. Used by the p2p attestation
8
+ * pool to dedup signed payloads and detect equivocations.
9
+ */
10
+ export type CheckpointProposalHash = Branded<`0x${string}`, 'CheckpointProposalHash'>;
11
+
12
+ /** Brands a `0x`-prefixed hex string as a CheckpointProposalHash. */
13
+ export function CheckpointProposalHash(s: `0x${string}`): CheckpointProposalHash {
14
+ return s as CheckpointProposalHash;
15
+ }
16
+
17
+ /** Constructs a CheckpointProposalHash from a raw 32-byte hash buffer. */
18
+ CheckpointProposalHash.fromBuffer = function (buf: Buffer): CheckpointProposalHash {
19
+ return `0x${buf.toString('hex')}` as CheckpointProposalHash;
20
+ };
21
+
22
+ /**
23
+ * A branded `0x`-prefixed hex string representing a block proposal payload hash.
24
+ *
25
+ * Used by the p2p attestation pool to dedup signed payloads at a given
26
+ * `(slot, indexWithinCheckpoint)` and detect equivocations.
27
+ */
28
+ export type BlockProposalHash = Branded<`0x${string}`, 'BlockProposalHash'>;
29
+
30
+ /** Brands a `0x`-prefixed hex string as a BlockProposalHash. */
31
+ export function BlockProposalHash(s: `0x${string}`): BlockProposalHash {
32
+ return s as BlockProposalHash;
33
+ }
34
+
35
+ /** Constructs a BlockProposalHash from a raw 32-byte hash buffer. */
36
+ BlockProposalHash.fromBuffer = function (buf: Buffer): BlockProposalHash {
37
+ return `0x${buf.toString('hex')}` as BlockProposalHash;
38
+ };
@@ -1,5 +1,6 @@
1
1
  import { z } from 'zod';
2
2
 
3
+ import { isDefined } from '../types/index.js';
3
4
  import type { BlockNumber } from './block_number.js';
4
5
  import type { Branded } from './types.js';
5
6
 
@@ -85,16 +86,45 @@ CheckpointNumber.isValid = function (value: unknown): value is CheckpointNumber
85
86
  return typeof value === 'number' && Number.isInteger(value) && value >= 0;
86
87
  };
87
88
 
88
- /**
89
- * The zero checkpoint value.
90
- */
89
+ /** Increments a CheckpointNumber by a given value. */
90
+ CheckpointNumber.add = function (n: CheckpointNumber, increment: number): CheckpointNumber {
91
+ return CheckpointNumber(n + increment);
92
+ };
93
+
94
+ /** Computes max of a set of checkpoint numbers, ignoring undefined values. */
95
+ CheckpointNumber.max = function (...values: (CheckpointNumber | undefined)[]): CheckpointNumber | undefined {
96
+ const filtered = values.filter(isDefined);
97
+ if (filtered.length === 0) {
98
+ return undefined;
99
+ }
100
+ return CheckpointNumber(Math.max(...filtered));
101
+ };
102
+
103
+ /** The zero checkpoint value. */
91
104
  CheckpointNumber.ZERO = CheckpointNumber(0);
92
105
 
106
+ /** Initial checkpoint. */
107
+ CheckpointNumber.INITIAL = CheckpointNumber(1);
108
+
93
109
  /**
94
110
  * Zod schema for parsing and validating CheckpointNumber values.
95
111
  * Accepts numbers, bigints, or strings and coerces them to CheckpointNumber.
96
112
  */
97
- export const CheckpointNumberSchema = z
98
- .union([z.number(), z.bigint(), z.string()])
99
- .pipe(z.coerce.number().int().min(0))
100
- .transform(value => CheckpointNumber(value));
113
+ function makeCheckpointNumberSchema(minValue: number) {
114
+ return z
115
+ .union([z.number(), z.bigint(), z.string()])
116
+ .pipe(z.coerce.number<string | number | bigint>().int().min(minValue))
117
+ .transform(value => CheckpointNumber(value));
118
+ }
119
+
120
+ /**
121
+ * Zod schema for parsing and validating Checkpoint values.
122
+ * Accepts numbers, bigints, or strings and coerces them to CheckpointNumber.
123
+ */
124
+ export const CheckpointNumberSchema = makeCheckpointNumberSchema(0);
125
+
126
+ /**
127
+ * Zod schema for parsing and validating CheckpointNumber values that are strictly positive.
128
+ * Accepts numbers, bigints, or strings and coerces them to CheckpointNumber.
129
+ */
130
+ export const CheckpointNumberPositiveSchema = makeCheckpointNumberSchema(1);
@@ -84,5 +84,5 @@ EpochNumber.ZERO = EpochNumber(0);
84
84
  */
85
85
  export const EpochNumberSchema = z
86
86
  .union([z.number(), z.bigint(), z.string()])
87
- .pipe(z.coerce.number().int().min(0))
87
+ .pipe(z.coerce.number<string | number | bigint>().int().min(0))
88
88
  .transform(value => EpochNumber(value));
@@ -1,6 +1,8 @@
1
1
  export { BlockNumber, BlockNumberSchema, BlockNumberPositiveSchema } from './block_number.js';
2
- export { CheckpointNumber, CheckpointNumberSchema } from './checkpoint_number.js';
2
+ export { BlockProposalHash, CheckpointProposalHash } from './buffer32_hash.js';
3
+ export { CheckpointNumber, CheckpointNumberSchema, CheckpointNumberPositiveSchema } from './checkpoint_number.js';
3
4
  export { EpochNumber, EpochNumberSchema } from './epoch.js';
5
+ export { IndexWithinCheckpoint, IndexWithinCheckpointSchema } from './index_within_checkpoint.js';
4
6
  export { SlotNumber, SlotNumberSchema } from './slot.js';
5
7
 
6
8
  export type { Branded } from './types.js';
@@ -0,0 +1,88 @@
1
+ import { z } from 'zod';
2
+
3
+ import type { Branded } from './types.js';
4
+
5
+ /**
6
+ * A branded type representing the index of a block within a checkpoint.
7
+ * The first block in a checkpoint has index 0, the second has index 1, etc.
8
+ *
9
+ * This is a nominal type backed by a number, but TypeScript will treat it as
10
+ * incompatible with plain numbers and other branded numeric types.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const index = IndexWithinCheckpoint(0);
15
+ * const plainNumber: number = index; // OK - IndexWithinCheckpoint is assignable to number
16
+ * const indexFromNumber: IndexWithinCheckpoint = 0; // ERROR - number is not assignable to IndexWithinCheckpoint
17
+ * ```
18
+ */
19
+ export type IndexWithinCheckpoint = Branded<number, 'IndexWithinCheckpoint'>;
20
+
21
+ /**
22
+ * Creates an IndexWithinCheckpoint from a number.
23
+ * @param value - The index (must be a non-negative integer)
24
+ * @returns The branded IndexWithinCheckpoint value
25
+ * @throws If the value is negative or not an integer
26
+ */
27
+ export function IndexWithinCheckpoint(value: number): IndexWithinCheckpoint {
28
+ if (!Number.isInteger(value)) {
29
+ throw new Error(`IndexWithinCheckpoint must be an integer, got ${value}`);
30
+ }
31
+ if (value < 0) {
32
+ throw new Error(`IndexWithinCheckpoint must be non-negative, got ${value}`);
33
+ }
34
+ return value as IndexWithinCheckpoint;
35
+ }
36
+
37
+ /**
38
+ * Creates an IndexWithinCheckpoint from a bigint.
39
+ * @param value - The index as bigint (must be a non-negative integer within safe integer range)
40
+ * @returns The branded IndexWithinCheckpoint value
41
+ * @throws If the value is negative or exceeds Number.MAX_SAFE_INTEGER
42
+ */
43
+ IndexWithinCheckpoint.fromBigInt = function (value: bigint): IndexWithinCheckpoint {
44
+ if (value < 0n) {
45
+ throw new Error(`IndexWithinCheckpoint must be non-negative, got ${value}`);
46
+ }
47
+ if (value > BigInt(Number.MAX_SAFE_INTEGER)) {
48
+ throw new Error(`IndexWithinCheckpoint ${value} exceeds MAX_SAFE_INTEGER`);
49
+ }
50
+ return Number(value) as IndexWithinCheckpoint;
51
+ };
52
+
53
+ /**
54
+ * Creates an IndexWithinCheckpoint from a string.
55
+ * @param value - The index as a string
56
+ * @returns The branded IndexWithinCheckpoint value
57
+ * @throws If the string cannot be parsed as a valid index
58
+ */
59
+ IndexWithinCheckpoint.fromString = function (value: string): IndexWithinCheckpoint {
60
+ const parsed = parseInt(value, 10);
61
+ if (isNaN(parsed)) {
62
+ throw new Error(`Cannot parse IndexWithinCheckpoint from string: ${value}`);
63
+ }
64
+ return IndexWithinCheckpoint(parsed);
65
+ };
66
+
67
+ /**
68
+ * Type guard to check if a value is a valid IndexWithinCheckpoint.
69
+ * Note: At runtime, an IndexWithinCheckpoint is just a number, so this checks if the value
70
+ * is a non-negative integer.
71
+ */
72
+ IndexWithinCheckpoint.isValid = function (value: unknown): value is IndexWithinCheckpoint {
73
+ return typeof value === 'number' && Number.isInteger(value) && value >= 0;
74
+ };
75
+
76
+ /**
77
+ * The zero index value (first block in checkpoint).
78
+ */
79
+ IndexWithinCheckpoint.ZERO = IndexWithinCheckpoint(0);
80
+
81
+ /**
82
+ * Zod schema for parsing and validating IndexWithinCheckpoint values.
83
+ * Accepts numbers, bigints, or strings and coerces them to IndexWithinCheckpoint.
84
+ */
85
+ export const IndexWithinCheckpointSchema = z
86
+ .union([z.number(), z.bigint(), z.string()])
87
+ .pipe(z.coerce.number<string | number | bigint>().int().min(0))
88
+ .transform(value => IndexWithinCheckpoint(value));
@@ -73,6 +73,11 @@ SlotNumber.isValid = function (value: unknown): value is SlotNumber {
73
73
  return typeof value === 'number' && Number.isInteger(value) && value >= 0;
74
74
  };
75
75
 
76
+ /** Increments a SlotNumber by a given value. */
77
+ SlotNumber.add = function (sn: SlotNumber, increment: number): SlotNumber {
78
+ return SlotNumber(sn + increment);
79
+ };
80
+
76
81
  /**
77
82
  * The zero slot value.
78
83
  */
@@ -84,5 +89,5 @@ SlotNumber.ZERO = SlotNumber(0);
84
89
  */
85
90
  export const SlotNumberSchema = z
86
91
  .union([z.number(), z.bigint(), z.string()])
87
- .pipe(z.coerce.number().int().min(0))
92
+ .pipe(z.coerce.number<string | number | bigint>().int().min(0))
88
93
  .transform(value => SlotNumber(value));
@@ -10,6 +10,9 @@ import { bufferToHex } from '../string/index.js';
10
10
  * A class representing a 16 byte Buffer.
11
11
  */
12
12
  export class Buffer16 {
13
+ /** Branding for nominal typing. */
14
+ declare private readonly _branding: 'Buffer16';
15
+
13
16
  /**
14
17
  * The size of the hash in bytes.
15
18
  */
@@ -7,69 +7,39 @@ import { inspect } from 'util';
7
7
  import { bufferToHex } from '../string/index.js';
8
8
 
9
9
  /**
10
- * A class representing a 32 byte Buffer.
10
+ * Abstract unbranded base class for 32-byte buffers.
11
+ * Extend this instead of Buffer32 when defining a branded subtype
12
+ * to avoid inheriting Buffer32's `_branding`.
11
13
  */
12
- export class Buffer32 {
13
- /**
14
- * The size of the hash in bytes.
15
- */
14
+ export abstract class BaseBuffer32 {
15
+ /** The size of the buffer in bytes. */
16
16
  public static SIZE = 32;
17
17
 
18
- /**
19
- * Buffer32 with value zero.
20
- */
21
- public static ZERO = new Buffer32(Buffer.alloc(Buffer32.SIZE));
22
-
23
18
  constructor(
24
- /**
25
- * The buffer containing the hash.
26
- */
19
+ /** The buffer containing the data. */
27
20
  public buffer: Buffer,
28
21
  ) {
29
- if (buffer.length !== Buffer32.SIZE) {
30
- throw new Error(`Expected buffer to have length ${Buffer32.SIZE} but was ${buffer.length}`);
22
+ if (buffer.length !== BaseBuffer32.SIZE) {
23
+ throw new Error(`Expected buffer to have length ${BaseBuffer32.SIZE} but was ${buffer.length}`);
31
24
  }
32
25
  }
33
26
 
34
- /**
35
- * Returns the raw buffer of the hash.
36
- * @returns The buffer containing the hash.
37
- */
27
+ /** Returns the raw buffer. */
38
28
  public toBuffer() {
39
29
  return this.buffer;
40
30
  }
41
31
 
42
- /**
43
- * Creates a Buffer32 from a buffer.
44
- * @param buffer - The buffer to create from.
45
- * @returns A new Buffer32 object.
46
- */
47
- public static fromBuffer(buffer: Buffer | BufferReader) {
48
- const reader = BufferReader.asReader(buffer);
49
- return new Buffer32(reader.readBytes(Buffer32.SIZE));
50
- }
51
-
52
- /**
53
- * Checks if this hash and another hash are equal.
54
- * @param hash - A hash to compare with.
55
- * @returns True if the hashes are equal, false otherwise.
56
- */
57
- public equals(hash: Buffer32): boolean {
32
+ /** Checks if this and another buffer are equal. */
33
+ public equals(hash: BaseBuffer32): boolean {
58
34
  return this.buffer.equals(hash.buffer);
59
35
  }
60
36
 
61
- /**
62
- * Returns true if this hash is zero.
63
- * @returns True if this hash is zero.
64
- */
37
+ /** Returns true if all bytes are zero. */
65
38
  public isZero(): boolean {
66
39
  return this.buffer.equals(Buffer.alloc(32, 0));
67
40
  }
68
41
 
69
- /**
70
- * Convert this hash to a hex string.
71
- * @returns The hex string.
72
- */
42
+ /** Convert to a hex string. */
73
43
  public toString() {
74
44
  return bufferToHex(this.buffer);
75
45
  }
@@ -82,16 +52,27 @@ export class Buffer32 {
82
52
  return this.toString();
83
53
  }
84
54
 
85
- /**
86
- * Convert this hash to a big int.
87
- * @returns The big int.
88
- */
55
+ /** Convert to a big int. */
89
56
  public toBigInt() {
90
- return deserializeBigInt(this.buffer, 0, Buffer32.SIZE).elem;
57
+ return deserializeBigInt(this.buffer, 0, BaseBuffer32.SIZE).elem;
91
58
  }
92
- /**
93
- * Creates a Buffer32 from a bigint.
94
- */
59
+ }
60
+
61
+ /** A branded 32-byte buffer. */
62
+ export class Buffer32 extends BaseBuffer32 {
63
+ /** Branding for nominal typing. */
64
+ declare private readonly _branding: 'Buffer32';
65
+
66
+ /** Buffer32 with value zero. */
67
+ public static ZERO = new Buffer32(Buffer.alloc(Buffer32.SIZE));
68
+
69
+ /** Creates a Buffer32 from a buffer. */
70
+ public static fromBuffer(buffer: Buffer | BufferReader) {
71
+ const reader = BufferReader.asReader(buffer);
72
+ return new Buffer32(reader.readBytes(Buffer32.SIZE));
73
+ }
74
+
75
+ /** Creates a Buffer32 from a bigint. */
95
76
  public static fromBigInt(hash: bigint) {
96
77
  return new Buffer32(serializeBigInt(hash, Buffer32.SIZE));
97
78
  }
@@ -101,8 +82,7 @@ export class Buffer32 {
101
82
  }
102
83
 
103
84
  /**
104
- * Converts this hash from a buffer of 28 bytes.
105
- * Verifies the input is 28 bytes.
85
+ * Converts from a buffer of 28 bytes.
106
86
  * @param buffer - The 28 byte buffer to construct from.
107
87
  * @returns A Buffer32 created from the input buffer with 4 bytes 0 padding at the front.
108
88
  */
@@ -110,36 +90,27 @@ export class Buffer32 {
110
90
  if (buffer.length != 28) {
111
91
  throw new Error(`Expected Buffer32 input buffer to be 28 bytes`);
112
92
  }
113
- const padded = Buffer.concat([Buffer.alloc(this.SIZE - 28), buffer]);
93
+ const padded = Buffer.concat([Buffer.alloc(Buffer32.SIZE - 28), buffer]);
114
94
  return new Buffer32(padded);
115
95
  }
116
96
 
117
- /**
118
- * Converts a string into a Buffer32 object.
119
- */
97
+ /** Converts a string into a Buffer32 object. */
120
98
  public static fromString(str: string): Buffer32 {
121
99
  if (str.startsWith('0x')) {
122
100
  str = str.slice(2);
123
101
  }
124
- if (str.length !== this.SIZE * 2) {
125
- throw new Error(`Expected string to be ${this.SIZE * 2} characters long, but was ${str.length}`);
102
+ if (str.length !== Buffer32.SIZE * 2) {
103
+ throw new Error(`Expected string to be ${Buffer32.SIZE * 2} characters long, but was ${str.length}`);
126
104
  }
127
105
  return new Buffer32(Buffer.from(str, 'hex'));
128
106
  }
129
107
 
130
- /**
131
- * Converts a number into a Buffer32 object.
132
- * @param num - The number to convert.
133
- * @returns A new Buffer32 object.
134
- */
108
+ /** Converts a number into a Buffer32 object. */
135
109
  public static fromNumber(num: number): Buffer32 {
136
110
  return new Buffer32(serializeBigInt(BigInt(num), Buffer32.SIZE));
137
111
  }
138
112
 
139
- /**
140
- * Generates a random Buffer32.
141
- * @returns A new Buffer32 object.
142
- */
113
+ /** Generates a random Buffer32. */
143
114
  public static random(): Buffer32 {
144
115
  return new Buffer32(Buffer.from(randomBytes(Buffer32.SIZE)));
145
116
  }
@@ -1,2 +1,3 @@
1
1
  export * from './buffer32.js';
2
2
  export * from './buffer16.js';
3
+ export * from './utils.js';
@@ -0,0 +1,8 @@
1
+ /** Returns the number of bytes in the buffer, excluding any trailing zero bytes. */
2
+ export function trimmedBytesLength(buf: Uint8Array): number {
3
+ let end = buf.length;
4
+ while (end > 0 && buf[end - 1] === 0) {
5
+ end--;
6
+ }
7
+ return end;
8
+ }