@aztec/foundation 0.0.1-commit.fce3e4f → 0.0.1-commit.ffe5b04ea

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 (335) 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/branded-types/block_number.d.ts +56 -0
  8. package/dest/branded-types/block_number.d.ts.map +1 -0
  9. package/dest/branded-types/block_number.js +78 -0
  10. package/dest/branded-types/checkpoint_number.d.ts +54 -0
  11. package/dest/branded-types/checkpoint_number.d.ts.map +1 -0
  12. package/dest/branded-types/checkpoint_number.js +77 -0
  13. package/dest/branded-types/index.d.ts +4 -1
  14. package/dest/branded-types/index.d.ts.map +1 -1
  15. package/dest/branded-types/index.js +3 -0
  16. package/dest/branded-types/index_within_checkpoint.d.ts +42 -0
  17. package/dest/branded-types/index_within_checkpoint.d.ts.map +1 -0
  18. package/dest/branded-types/index_within_checkpoint.js +59 -0
  19. package/dest/buffer/buffer16.d.ts +5 -2
  20. package/dest/buffer/buffer16.d.ts.map +1 -1
  21. package/dest/buffer/buffer16.js +4 -2
  22. package/dest/buffer/buffer32.d.ts +5 -2
  23. package/dest/buffer/buffer32.d.ts.map +1 -1
  24. package/dest/buffer/buffer32.js +4 -2
  25. package/dest/collection/array.d.ts +12 -1
  26. package/dest/collection/array.d.ts.map +1 -1
  27. package/dest/collection/array.js +51 -0
  28. package/dest/config/env_var.d.ts +2 -2
  29. package/dest/config/env_var.d.ts.map +1 -1
  30. package/dest/config/index.d.ts +15 -4
  31. package/dest/config/index.d.ts.map +1 -1
  32. package/dest/config/index.js +47 -9
  33. package/dest/config/network_config.d.ts +25 -1
  34. package/dest/config/network_config.d.ts.map +1 -1
  35. package/dest/config/network_config.js +5 -1
  36. package/dest/config/network_name.d.ts +2 -2
  37. package/dest/config/network_name.d.ts.map +1 -1
  38. package/dest/config/network_name.js +2 -0
  39. package/dest/config/parse-env.d.ts +3 -0
  40. package/dest/config/parse-env.d.ts.map +1 -0
  41. package/dest/config/parse-env.js +7 -0
  42. package/dest/config/secret_value.js +3 -1
  43. package/dest/crypto/bls/bn254_keystore.js +1 -1
  44. package/dest/crypto/bls/index.js +1 -1
  45. package/dest/crypto/bn254/index.d.ts +35 -51
  46. package/dest/crypto/bn254/index.d.ts.map +1 -1
  47. package/dest/crypto/bn254/index.js +51 -140
  48. package/dest/crypto/ecdsa/signature.d.ts +11 -2
  49. package/dest/crypto/ecdsa/signature.d.ts.map +1 -1
  50. package/dest/crypto/ecdsa/signature.js +2 -2
  51. package/dest/crypto/grumpkin/index.d.ts +4 -2
  52. package/dest/crypto/grumpkin/index.d.ts.map +1 -1
  53. package/dest/crypto/grumpkin/index.js +2 -1
  54. package/dest/crypto/keys/index.d.ts +2 -2
  55. package/dest/crypto/keys/index.d.ts.map +1 -1
  56. package/dest/crypto/keys/index.js +1 -1
  57. package/dest/crypto/pedersen/pedersen.wasm.d.ts +2 -2
  58. package/dest/crypto/pedersen/pedersen.wasm.d.ts.map +1 -1
  59. package/dest/crypto/pedersen/pedersen.wasm.js +1 -1
  60. package/dest/crypto/poseidon/index.d.ts +2 -3
  61. package/dest/crypto/poseidon/index.d.ts.map +1 -1
  62. package/dest/crypto/poseidon/index.js +14 -23
  63. package/dest/crypto/random/randomness_singleton.d.ts +4 -3
  64. package/dest/crypto/random/randomness_singleton.d.ts.map +1 -1
  65. package/dest/crypto/random/randomness_singleton.js +5 -5
  66. package/dest/crypto/schnorr/index.d.ts +3 -2
  67. package/dest/crypto/schnorr/index.d.ts.map +1 -1
  68. package/dest/crypto/schnorr/index.js +1 -1
  69. package/dest/crypto/schnorr/signature.d.ts +2 -2
  70. package/dest/crypto/schnorr/signature.d.ts.map +1 -1
  71. package/dest/crypto/schnorr/signature.js +2 -2
  72. package/dest/crypto/secp256k1-signer/utils.d.ts +12 -1
  73. package/dest/crypto/secp256k1-signer/utils.d.ts.map +1 -1
  74. package/dest/crypto/secp256k1-signer/utils.js +26 -0
  75. package/dest/crypto/sha256/index.d.ts +2 -2
  76. package/dest/crypto/sha256/index.d.ts.map +1 -1
  77. package/dest/crypto/sha256/index.js +1 -1
  78. package/dest/crypto/sha512/index.d.ts +2 -2
  79. package/dest/crypto/sha512/index.d.ts.map +1 -1
  80. package/dest/crypto/sha512/index.js +1 -1
  81. package/dest/crypto/signature/index.d.ts +2 -2
  82. package/dest/crypto/signature/index.d.ts.map +1 -1
  83. package/dest/crypto/sync/pedersen/index.d.ts +2 -2
  84. package/dest/crypto/sync/pedersen/index.d.ts.map +1 -1
  85. package/dest/crypto/sync/pedersen/index.js +1 -1
  86. package/dest/crypto/sync/poseidon/index.d.ts +2 -3
  87. package/dest/crypto/sync/poseidon/index.d.ts.map +1 -1
  88. package/dest/crypto/sync/poseidon/index.js +1 -9
  89. package/dest/{fields/bls12_fields.d.ts → curves/bls12/field.d.ts} +3 -3
  90. package/dest/curves/bls12/field.d.ts.map +1 -0
  91. package/dest/{fields/bls12_fields.js → curves/bls12/field.js} +12 -9
  92. package/dest/curves/bls12/index.d.ts +3 -0
  93. package/dest/curves/bls12/index.d.ts.map +1 -0
  94. package/dest/curves/bls12/index.js +2 -0
  95. package/dest/{fields/bls12_point.d.ts → curves/bls12/point.d.ts} +13 -4
  96. package/dest/curves/bls12/point.d.ts.map +1 -0
  97. package/dest/{fields/bls12_point.js → curves/bls12/point.js} +10 -8
  98. package/dest/{fields/fields.d.ts → curves/bn254/field.d.ts} +8 -15
  99. package/dest/curves/bn254/field.d.ts.map +1 -0
  100. package/dest/{fields/fields.js → curves/bn254/field.js} +38 -49
  101. package/dest/curves/bn254/index.d.ts +3 -0
  102. package/dest/curves/bn254/index.d.ts.map +1 -0
  103. package/dest/curves/bn254/index.js +2 -0
  104. package/dest/curves/bn254/point.d.ts +54 -0
  105. package/dest/curves/bn254/point.d.ts.map +1 -0
  106. package/dest/curves/bn254/point.js +143 -0
  107. package/dest/curves/grumpkin/index.d.ts +10 -0
  108. package/dest/curves/grumpkin/index.d.ts.map +1 -0
  109. package/dest/curves/grumpkin/index.js +3 -0
  110. package/dest/{fields → curves/grumpkin}/point.d.ts +13 -4
  111. package/dest/curves/grumpkin/point.d.ts.map +1 -0
  112. package/dest/{fields → curves/grumpkin}/point.js +7 -7
  113. package/dest/error/index.d.ts +4 -4
  114. package/dest/error/index.d.ts.map +1 -1
  115. package/dest/eth-address/index.d.ts +3 -2
  116. package/dest/eth-address/index.d.ts.map +1 -1
  117. package/dest/eth-address/index.js +10 -3
  118. package/dest/eth-signature/eth_signature.d.ts +5 -1
  119. package/dest/eth-signature/eth_signature.d.ts.map +1 -1
  120. package/dest/eth-signature/eth_signature.js +7 -2
  121. package/dest/iterator/filter.d.ts +3 -0
  122. package/dest/iterator/filter.d.ts.map +1 -0
  123. package/dest/iterator/filter.js +7 -0
  124. package/dest/iterator/index.d.ts +2 -0
  125. package/dest/iterator/index.d.ts.map +1 -0
  126. package/dest/iterator/index.js +1 -0
  127. package/dest/jest/setup.js +28 -1
  128. package/dest/json-rpc/client/safe_json_rpc_client.d.ts +2 -1
  129. package/dest/json-rpc/client/safe_json_rpc_client.d.ts.map +1 -1
  130. package/dest/json-rpc/client/safe_json_rpc_client.js +1 -1
  131. package/dest/json-rpc/client/undici.d.ts +1 -1
  132. package/dest/json-rpc/client/undici.d.ts.map +1 -1
  133. package/dest/json-rpc/client/undici.js +21 -4
  134. package/dest/json-rpc/fixtures/class_a.d.ts +3 -3
  135. package/dest/json-rpc/fixtures/class_a.d.ts.map +1 -1
  136. package/dest/json-rpc/fixtures/class_b.d.ts +3 -3
  137. package/dest/json-rpc/fixtures/class_b.d.ts.map +1 -1
  138. package/dest/json-rpc/server/api_key_auth.d.ts +19 -0
  139. package/dest/json-rpc/server/api_key_auth.d.ts.map +1 -0
  140. package/dest/json-rpc/server/api_key_auth.js +57 -0
  141. package/dest/json-rpc/server/index.d.ts +2 -1
  142. package/dest/json-rpc/server/index.d.ts.map +1 -1
  143. package/dest/json-rpc/server/index.js +1 -0
  144. package/dest/json-rpc/server/safe_json_rpc_server.js +1 -1
  145. package/dest/log/bigint-utils.d.ts +5 -0
  146. package/dest/log/bigint-utils.d.ts.map +1 -0
  147. package/dest/log/bigint-utils.js +21 -0
  148. package/dest/log/gcloud-logger-config.d.ts +1 -1
  149. package/dest/log/gcloud-logger-config.d.ts.map +1 -1
  150. package/dest/log/gcloud-logger-config.js +3 -0
  151. package/dest/log/libp2p_logger.d.ts +5 -2
  152. package/dest/log/libp2p_logger.d.ts.map +1 -1
  153. package/dest/log/libp2p_logger.js +14 -4
  154. package/dest/log/log-filters.d.ts +17 -4
  155. package/dest/log/log-filters.d.ts.map +1 -1
  156. package/dest/log/log-filters.js +26 -12
  157. package/dest/log/pino-logger-server.d.ts +9 -0
  158. package/dest/log/pino-logger-server.d.ts.map +1 -0
  159. package/dest/log/pino-logger-server.js +18 -0
  160. package/dest/log/pino-logger.d.ts +37 -8
  161. package/dest/log/pino-logger.d.ts.map +1 -1
  162. package/dest/log/pino-logger.js +122 -29
  163. package/dest/queue/base_memory_queue.d.ts +2 -2
  164. package/dest/queue/base_memory_queue.d.ts.map +1 -1
  165. package/dest/queue/semaphore.d.ts +5 -1
  166. package/dest/queue/semaphore.d.ts.map +1 -1
  167. package/dest/retry/index.d.ts +11 -1
  168. package/dest/retry/index.d.ts.map +1 -1
  169. package/dest/retry/index.js +11 -0
  170. package/dest/schemas/schemas.d.ts +7 -3
  171. package/dest/schemas/schemas.d.ts.map +1 -1
  172. package/dest/schemas/schemas.js +9 -3
  173. package/dest/schemas/types.d.ts +31 -1
  174. package/dest/schemas/types.d.ts.map +1 -1
  175. package/dest/schemas/types.js +25 -1
  176. package/dest/serialize/buffer_reader.d.ts +14 -4
  177. package/dest/serialize/buffer_reader.d.ts.map +1 -1
  178. package/dest/serialize/buffer_reader.js +26 -4
  179. package/dest/serialize/field_reader.d.ts +2 -2
  180. package/dest/serialize/field_reader.d.ts.map +1 -1
  181. package/dest/serialize/field_reader.js +1 -1
  182. package/dest/serialize/free_funcs.d.ts +2 -2
  183. package/dest/serialize/free_funcs.d.ts.map +1 -1
  184. package/dest/serialize/free_funcs.js +1 -1
  185. package/dest/serialize/serialize.d.ts +20 -2
  186. package/dest/serialize/serialize.d.ts.map +1 -1
  187. package/dest/serialize/serialize.js +32 -1
  188. package/dest/sleep/index.d.ts +4 -1
  189. package/dest/sleep/index.d.ts.map +1 -1
  190. package/dest/sleep/index.js +14 -1
  191. package/dest/timer/date.d.ts +26 -1
  192. package/dest/timer/date.d.ts.map +1 -1
  193. package/dest/timer/date.js +36 -0
  194. package/dest/transport/transport_client.js +2 -2
  195. package/dest/trees/balanced_merkle_tree_root.d.ts +17 -0
  196. package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -0
  197. package/dest/trees/{balanced_merkle_tree.js → balanced_merkle_tree_root.js} +2 -15
  198. package/dest/trees/hasher.d.ts +3 -1
  199. package/dest/trees/hasher.d.ts.map +1 -1
  200. package/dest/trees/hasher.js +10 -5
  201. package/dest/trees/index.d.ts +4 -4
  202. package/dest/trees/index.d.ts.map +1 -1
  203. package/dest/trees/index.js +3 -3
  204. package/dest/trees/indexed_merkle_tree.js +1 -1
  205. package/dest/trees/membership_witness.d.ts +12 -2
  206. package/dest/trees/membership_witness.d.ts.map +1 -1
  207. package/dest/trees/membership_witness.js +10 -1
  208. package/dest/trees/merkle_tree_calculator.d.ts +1 -1
  209. package/dest/trees/merkle_tree_calculator.d.ts.map +1 -1
  210. package/dest/trees/merkle_tree_calculator.js +2 -2
  211. package/dest/trees/sibling_path.d.ts +3 -2
  212. package/dest/trees/sibling_path.d.ts.map +1 -1
  213. package/dest/trees/sibling_path.js +3 -3
  214. package/dest/trees/unbalanced_merkle_tree_calculator.d.ts +2 -3
  215. package/dest/trees/unbalanced_merkle_tree_calculator.d.ts.map +1 -1
  216. package/dest/trees/unbalanced_merkle_tree_calculator.js +1 -5
  217. package/dest/trees/{unbalanced_merkle_tree.d.ts → unbalanced_merkle_tree_root.d.ts} +3 -5
  218. package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -0
  219. package/dest/trees/{unbalanced_merkle_tree.js → unbalanced_merkle_tree_root.js} +11 -49
  220. package/dest/trees/unbalanced_tree_store.d.ts +5 -1
  221. package/dest/trees/unbalanced_tree_store.d.ts.map +1 -1
  222. package/dest/trees/unbalanced_tree_store.js +49 -1
  223. package/dest/types/index.d.ts +6 -2
  224. package/dest/types/index.d.ts.map +1 -1
  225. package/dest/types/index.js +6 -0
  226. package/package.json +48 -9
  227. package/src/array/index.ts +1 -0
  228. package/src/array/sorted_array.ts +138 -0
  229. package/src/branded-types/block_number.ts +117 -0
  230. package/src/branded-types/checkpoint_number.ts +115 -0
  231. package/src/branded-types/index.ts +3 -0
  232. package/src/branded-types/index_within_checkpoint.ts +88 -0
  233. package/src/buffer/buffer16.ts +2 -2
  234. package/src/buffer/buffer32.ts +2 -2
  235. package/src/collection/array.ts +52 -0
  236. package/src/config/env_var.ts +81 -17
  237. package/src/config/index.ts +54 -6
  238. package/src/config/network_config.ts +4 -0
  239. package/src/config/network_name.ts +4 -1
  240. package/src/config/parse-env.ts +4 -0
  241. package/src/crypto/bls/bn254_keystore.ts +1 -1
  242. package/src/crypto/bls/index.ts +1 -1
  243. package/src/crypto/bn254/index.ts +50 -159
  244. package/src/crypto/ecdsa/signature.ts +2 -2
  245. package/src/crypto/grumpkin/index.ts +3 -1
  246. package/src/crypto/keys/index.ts +1 -1
  247. package/src/crypto/pedersen/pedersen.wasm.ts +1 -1
  248. package/src/crypto/poseidon/index.ts +14 -24
  249. package/src/crypto/random/randomness_singleton.ts +6 -4
  250. package/src/crypto/schnorr/index.ts +2 -1
  251. package/src/crypto/schnorr/signature.ts +2 -2
  252. package/src/crypto/secp256k1-signer/utils.ts +32 -0
  253. package/src/crypto/sha256/index.ts +1 -1
  254. package/src/crypto/sha512/index.ts +1 -1
  255. package/src/crypto/signature/index.ts +1 -1
  256. package/src/crypto/sync/pedersen/index.ts +1 -1
  257. package/src/crypto/sync/poseidon/index.ts +1 -10
  258. package/src/{fields/bls12_fields.ts → curves/bls12/field.ts} +6 -6
  259. package/src/curves/bls12/index.ts +2 -0
  260. package/src/{fields/bls12_point.ts → curves/bls12/point.ts} +7 -7
  261. package/src/{fields/fields.ts → curves/bn254/field.ts} +37 -56
  262. package/src/curves/bn254/index.ts +2 -0
  263. package/src/curves/bn254/point.ts +170 -0
  264. package/src/curves/grumpkin/index.ts +11 -0
  265. package/src/{fields → curves/grumpkin}/point.ts +7 -7
  266. package/src/eth-address/index.ts +8 -2
  267. package/src/eth-signature/eth_signature.ts +7 -1
  268. package/src/iterator/filter.ts +11 -0
  269. package/src/iterator/index.ts +1 -0
  270. package/src/jest/setup.mjs +31 -1
  271. package/src/json-rpc/client/safe_json_rpc_client.ts +2 -0
  272. package/src/json-rpc/client/undici.ts +21 -3
  273. package/src/json-rpc/server/api_key_auth.ts +63 -0
  274. package/src/json-rpc/server/index.ts +1 -0
  275. package/src/json-rpc/server/safe_json_rpc_server.ts +1 -1
  276. package/src/log/bigint-utils.ts +25 -0
  277. package/src/log/gcloud-logger-config.ts +5 -0
  278. package/src/log/libp2p_logger.ts +12 -5
  279. package/src/log/log-filters.ts +29 -11
  280. package/src/log/pino-logger-server.ts +25 -0
  281. package/src/log/pino-logger.ts +144 -39
  282. package/src/queue/base_memory_queue.ts +1 -1
  283. package/src/queue/semaphore.ts +5 -0
  284. package/src/retry/index.ts +18 -0
  285. package/src/schemas/schemas.ts +14 -3
  286. package/src/schemas/types.ts +33 -0
  287. package/src/serialize/buffer_reader.ts +36 -9
  288. package/src/serialize/field_reader.ts +1 -1
  289. package/src/serialize/free_funcs.ts +1 -1
  290. package/src/serialize/serialize.ts +33 -1
  291. package/src/sleep/index.ts +16 -1
  292. package/src/timer/date.ts +52 -0
  293. package/src/transport/transport_client.ts +2 -2
  294. package/src/trees/{balanced_merkle_tree.ts → balanced_merkle_tree_root.ts} +2 -16
  295. package/src/trees/hasher.ts +9 -0
  296. package/src/trees/index.ts +3 -3
  297. package/src/trees/indexed_merkle_tree.ts +1 -1
  298. package/src/trees/membership_witness.ts +9 -1
  299. package/src/trees/merkle_tree_calculator.ts +2 -2
  300. package/src/trees/sibling_path.ts +3 -3
  301. package/src/trees/unbalanced_merkle_tree_calculator.ts +1 -12
  302. package/src/trees/{unbalanced_merkle_tree.ts → unbalanced_merkle_tree_root.ts} +17 -61
  303. package/src/trees/unbalanced_tree_store.ts +57 -2
  304. package/src/types/index.ts +11 -1
  305. package/dest/crypto/bn254/bn254_utils.d.ts +0 -39
  306. package/dest/crypto/bn254/bn254_utils.d.ts.map +0 -1
  307. package/dest/crypto/bn254/bn254_utils.js +0 -56
  308. package/dest/crypto/index.d.ts +0 -17
  309. package/dest/crypto/index.d.ts.map +0 -1
  310. package/dest/crypto/index.js +0 -16
  311. package/dest/fields/bls12_fields.d.ts.map +0 -1
  312. package/dest/fields/bls12_point.d.ts.map +0 -1
  313. package/dest/fields/coordinate.d.ts +0 -63
  314. package/dest/fields/coordinate.d.ts.map +0 -1
  315. package/dest/fields/coordinate.js +0 -96
  316. package/dest/fields/fields.d.ts.map +0 -1
  317. package/dest/fields/index.d.ts +0 -6
  318. package/dest/fields/index.d.ts.map +0 -1
  319. package/dest/fields/index.js +0 -5
  320. package/dest/fields/point.d.ts.map +0 -1
  321. package/dest/index.d.ts +0 -31
  322. package/dest/index.d.ts.map +0 -1
  323. package/dest/index.js +0 -31
  324. package/dest/json-rpc/server/telemetry.d.ts +0 -2
  325. package/dest/json-rpc/server/telemetry.d.ts.map +0 -1
  326. package/dest/json-rpc/server/telemetry.js +0 -0
  327. package/dest/trees/balanced_merkle_tree.d.ts +0 -22
  328. package/dest/trees/balanced_merkle_tree.d.ts.map +0 -1
  329. package/dest/trees/unbalanced_merkle_tree.d.ts.map +0 -1
  330. package/src/crypto/bn254/bn254_utils.ts +0 -64
  331. package/src/crypto/index.ts +0 -16
  332. package/src/fields/coordinate.ts +0 -104
  333. package/src/fields/index.ts +0 -5
  334. package/src/index.ts +0 -31
  335. package/src/json-rpc/server/telemetry.ts +0 -0
@@ -1,6 +1,6 @@
1
1
  import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
- import { Fr } from '../../fields/fields.js';
3
+ import { Fr } from '../../curves/bn254/field.js';
4
4
 
5
5
  export async function vkAsFieldsMegaHonk(input: Buffer): Promise<Fr[]> {
6
6
  await BarretenbergSync.initSingleton();
@@ -1,6 +1,6 @@
1
1
  import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
- import { Fr } from '../../fields/fields.js';
3
+ import { Fr } from '../../curves/bn254/field.js';
4
4
  import { type Fieldable, serializeToFields } from '../../serialize/serialize.js';
5
5
 
6
6
  /**
@@ -1,6 +1,6 @@
1
- import { BarretenbergSync } from '@aztec/bb.js';
1
+ import { Barretenberg } from '@aztec/bb.js';
2
2
 
3
- import { Fr } from '../../fields/fields.js';
3
+ import { Fr } from '../../curves/bn254/field.js';
4
4
  import { type Fieldable, serializeToFields } from '../../serialize/serialize.js';
5
5
 
6
6
  /**
@@ -10,9 +10,9 @@ import { type Fieldable, serializeToFields } from '../../serialize/serialize.js'
10
10
  */
11
11
  export async function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
12
12
  const inputFields = serializeToFields(input);
13
- await BarretenbergSync.initSingleton();
14
- const api = BarretenbergSync.getSingleton();
15
- const response = api.poseidon2Hash({
13
+ await Barretenberg.initSingleton();
14
+ const api = Barretenberg.getSingleton();
15
+ const response = await api.poseidon2Hash({
16
16
  inputs: inputFields.map(i => i.toBuffer()),
17
17
  });
18
18
  return Fr.fromBuffer(Buffer.from(response.hash));
@@ -27,19 +27,9 @@ export async function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
27
27
  export async function poseidon2HashWithSeparator(input: Fieldable[], separator: number): Promise<Fr> {
28
28
  const inputFields = serializeToFields(input);
29
29
  inputFields.unshift(new Fr(separator));
30
- await BarretenbergSync.initSingleton();
31
- const api = BarretenbergSync.getSingleton();
32
- const response = api.poseidon2Hash({
33
- inputs: inputFields.map(i => i.toBuffer()),
34
- });
35
- return Fr.fromBuffer(Buffer.from(response.hash));
36
- }
37
-
38
- export async function poseidon2HashAccumulate(input: Fieldable[]): Promise<Fr> {
39
- const inputFields = serializeToFields(input);
40
- await BarretenbergSync.initSingleton();
41
- const api = BarretenbergSync.getSingleton();
42
- const response = api.poseidon2HashAccumulate({
30
+ await Barretenberg.initSingleton();
31
+ const api = Barretenberg.getSingleton();
32
+ const response = await api.poseidon2Hash({
43
33
  inputs: inputFields.map(i => i.toBuffer()),
44
34
  });
45
35
  return Fr.fromBuffer(Buffer.from(response.hash));
@@ -54,9 +44,9 @@ export async function poseidon2Permutation(input: Fieldable[]): Promise<Fr[]> {
54
44
  const inputFields = serializeToFields(input);
55
45
  // We'd like this assertion but it's not possible to use it in the browser.
56
46
  // assert(input.length === 4, 'Input state must be of size 4');
57
- await BarretenbergSync.initSingleton();
58
- const api = BarretenbergSync.getSingleton();
59
- const response = api.poseidon2Permutation({
47
+ await Barretenberg.initSingleton();
48
+ const api = Barretenberg.getSingleton();
49
+ const response = await api.poseidon2Permutation({
60
50
  inputs: inputFields.map(i => i.toBuffer()),
61
51
  });
62
52
  // We'd like this assertion but it's not possible to use it in the browser.
@@ -75,9 +65,9 @@ export async function poseidon2HashBytes(input: Buffer): Promise<Fr> {
75
65
  inputFields.push(Fr.fromBuffer(fieldBytes));
76
66
  }
77
67
 
78
- await BarretenbergSync.initSingleton();
79
- const api = BarretenbergSync.getSingleton();
80
- const response = api.poseidon2Hash({
68
+ await Barretenberg.initSingleton();
69
+ const api = Barretenberg.getSingleton();
70
+ const response = await api.poseidon2Hash({
81
71
  inputs: inputFields.map(i => i.toBuffer()),
82
72
  });
83
73
 
@@ -1,4 +1,4 @@
1
- import { createLogger } from '../../log/pino-logger.js';
1
+ import { type Logger, type LoggerBindings, createLogger } from '../../log/pino-logger.js';
2
2
 
3
3
  /**
4
4
  * A number generator which is used as a source of randomness in the system. If the SEED env variable is set, the
@@ -12,11 +12,13 @@ export class RandomnessSingleton {
12
12
  private static instance: RandomnessSingleton;
13
13
 
14
14
  private counter = 0;
15
+ private log: Logger;
15
16
 
16
17
  private constructor(
17
18
  private readonly seed?: number,
18
- private readonly log = createLogger('foundation:randomness_singleton'),
19
+ bindings?: LoggerBindings,
19
20
  ) {
21
+ this.log = createLogger('foundation:randomness_singleton', bindings);
20
22
  if (seed !== undefined) {
21
23
  this.log.debug(`Using pseudo-randomness with seed: ${seed}`);
22
24
  this.counter = seed;
@@ -25,10 +27,10 @@ export class RandomnessSingleton {
25
27
  }
26
28
  }
27
29
 
28
- public static getInstance(): RandomnessSingleton {
30
+ public static getInstance(bindings?: LoggerBindings): RandomnessSingleton {
29
31
  if (!RandomnessSingleton.instance) {
30
32
  const seed = process.env.SEED ? Number(process.env.SEED) : undefined;
31
- RandomnessSingleton.instance = new RandomnessSingleton(seed);
33
+ RandomnessSingleton.instance = new RandomnessSingleton(seed, bindings);
32
34
  }
33
35
 
34
36
  return RandomnessSingleton.instance;
@@ -1,5 +1,6 @@
1
1
  import { BarretenbergSync } from '@aztec/bb.js';
2
- import { type GrumpkinScalar, Point } from '@aztec/foundation/fields';
2
+ import type { GrumpkinScalar } from '@aztec/foundation/curves/grumpkin';
3
+ import { Point } from '@aztec/foundation/curves/grumpkin';
3
4
 
4
5
  import { SchnorrSignature } from './signature.js';
5
6
 
@@ -1,5 +1,5 @@
1
- import { randomBytes } from '@aztec/foundation/crypto';
2
- import { Fr } from '@aztec/foundation/fields';
1
+ import { randomBytes } from '@aztec/foundation/crypto/random';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
3
  import { BufferReader, mapTuple } from '@aztec/foundation/serialize';
4
4
 
5
5
  import type { Signature } from '../signature/index.js';
@@ -210,3 +210,35 @@ export function recoverPublicKey(hash: Buffer32, signature: Signature, opts: Rec
210
210
  const publicKey = sig.recoverPublicKey(hash.buffer).toHex(false);
211
211
  return Buffer.from(publicKey, 'hex');
212
212
  }
213
+
214
+ /** Arbitrary hash used for testing signature recoverability. */
215
+ const PROBE_HASH = Buffer32.fromBuffer(keccak256(Buffer.from('signature-recoverability-probe')));
216
+
217
+ /**
218
+ * Generates a random valid ECDSA signature that is recoverable to some address.
219
+ * Since Signature.random() produces real signatures via secp256k1 signing, the result is always
220
+ * recoverable, but we verify defensively by checking tryRecoverAddress.
221
+ */
222
+ export function generateRecoverableSignature(): Signature {
223
+ for (let i = 0; i < 100; i++) {
224
+ const sig = Signature.random();
225
+ if (tryRecoverAddress(PROBE_HASH, sig) !== undefined) {
226
+ return sig;
227
+ }
228
+ }
229
+ throw new Secp256k1Error('Failed to generate a recoverable signature after 100 attempts');
230
+ }
231
+
232
+ /**
233
+ * Generates a random signature where ECDSA address recovery fails.
234
+ * Uses random r/s values (not from real signing) so that r is unlikely to be a valid secp256k1 x-coordinate.
235
+ */
236
+ export function generateUnrecoverableSignature(): Signature {
237
+ for (let i = 0; i < 100; i++) {
238
+ const sig = new Signature(Buffer32.random(), Buffer32.random(), 27);
239
+ if (tryRecoverAddress(PROBE_HASH, sig) === undefined) {
240
+ return sig;
241
+ }
242
+ }
243
+ throw new Secp256k1Error('Failed to generate an unrecoverable signature after 100 attempts');
244
+ }
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable camelcase */
2
2
  import { default as hash } from 'hash.js';
3
3
 
4
- import { Fr } from '../../fields/fields.js';
4
+ import { Fr } from '../../curves/bn254/field.js';
5
5
  import { truncateAndPad } from '../../serialize/free_funcs.js';
6
6
  import { type Bufferable, serializeToBuffer } from '../../serialize/serialize.js';
7
7
  import type { Hasher } from '../../trees/hasher.js';
@@ -1,6 +1,6 @@
1
1
  import { default as hash } from 'hash.js';
2
2
 
3
- import { GrumpkinScalar } from '../../fields/fields.js';
3
+ import { GrumpkinScalar } from '../../curves/grumpkin/index.js';
4
4
  import { type Bufferable, serializeToBuffer } from '../../serialize/serialize.js';
5
5
 
6
6
  export const sha512 = (data: Buffer) => Buffer.from(hash.sha512().update(data).digest());
@@ -1,4 +1,4 @@
1
- import type { Fr } from '@aztec/foundation/fields';
1
+ import type { Fr } from '@aztec/foundation/curves/bn254';
2
2
 
3
3
  /**
4
4
  * Interface to represent a signature.
@@ -1,6 +1,6 @@
1
1
  import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
- import { Fr } from '../../../fields/fields.js';
3
+ import { Fr } from '../../../curves/bn254/field.js';
4
4
  import { type Fieldable, serializeToFields } from '../../../serialize/serialize.js';
5
5
 
6
6
  /**
@@ -1,6 +1,6 @@
1
1
  import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
- import { Fr } from '../../../fields/fields.js';
3
+ import { Fr } from '../../../curves/bn254/field.js';
4
4
  import { type Fieldable, serializeToFields } from '../../../serialize/serialize.js';
5
5
 
6
6
  /**
@@ -34,15 +34,6 @@ export function poseidon2HashWithSeparator(input: Fieldable[], separator: number
34
34
  return Fr.fromBuffer(Buffer.from(response.hash));
35
35
  }
36
36
 
37
- export function poseidon2HashAccumulate(input: Fieldable[]): Fr {
38
- const inputFields = serializeToFields(input);
39
- const api = BarretenbergSync.getSingleton();
40
- const response = api.poseidon2HashAccumulate({
41
- inputs: inputFields.map(i => i.toBuffer()),
42
- });
43
- return Fr.fromBuffer(Buffer.from(response.hash));
44
- }
45
-
46
37
  /**
47
38
  * Runs a Poseidon2 permutation.
48
39
  * @param input the input state. Expected to be of size 4.
@@ -2,12 +2,12 @@
2
2
  import { bls12_381 } from '@noble/curves/bls12-381';
3
3
  import { inspect } from 'util';
4
4
 
5
- import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
6
- import { randomBytes } from '../crypto/random/index.js';
7
- import { hexSchemaFor } from '../schemas/utils.js';
8
- import { BufferReader } from '../serialize/buffer_reader.js';
9
- import { TypeRegistry } from '../serialize/type_registry.js';
10
- import { Fr } from './fields.js';
5
+ import { toBigIntBE, toBufferBE } from '../../bigint-buffer/index.js';
6
+ import { randomBytes } from '../../crypto/random/index.js';
7
+ import { hexSchemaFor } from '../../schemas/utils.js';
8
+ import { BufferReader } from '../../serialize/buffer_reader.js';
9
+ import { TypeRegistry } from '../../serialize/type_registry.js';
10
+ import { Fr } from '../bn254/field.js';
11
11
 
12
12
  /**
13
13
  * Represents a field derived from BLS12Field.
@@ -0,0 +1,2 @@
1
+ export * from './field.js';
2
+ export * from './point.js';
@@ -3,13 +3,13 @@ import type { ProjPointType } from '@noble/curves/abstract/weierstrass';
3
3
  import { bls12_381 } from '@noble/curves/bls12-381';
4
4
  import { inspect } from 'util';
5
5
 
6
- import { toBufferBE } from '../bigint-buffer/index.js';
7
- import { randomBoolean } from '../crypto/random/index.js';
8
- import { hexSchemaFor } from '../schemas/utils.js';
9
- import { BufferReader, serializeToBuffer } from '../serialize/index.js';
10
- import { bufferToHex, hexToBuffer } from '../string/index.js';
11
- import { BLS12Fq, BLS12Fr } from './bls12_fields.js';
12
- import { Fr } from './fields.js';
6
+ import { toBufferBE } from '../../bigint-buffer/index.js';
7
+ import { randomBoolean } from '../../crypto/random/index.js';
8
+ import { hexSchemaFor } from '../../schemas/utils.js';
9
+ import { BufferReader, serializeToBuffer } from '../../serialize/index.js';
10
+ import { bufferToHex, hexToBuffer } from '../../string/index.js';
11
+ import { Fr } from '../bn254/field.js';
12
+ import { BLS12Fq, BLS12Fr } from './field.js';
13
13
 
14
14
  /**
15
15
  * Represents a Point on an elliptic curve with x and y coordinates.
@@ -2,13 +2,11 @@ import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
3
  import { inspect } from 'util';
4
4
 
5
- import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
6
- import { randomBytes } from '../crypto/random/index.js';
7
- import { hexSchemaFor } from '../schemas/utils.js';
8
- import { BufferReader } from '../serialize/buffer_reader.js';
9
- import { TypeRegistry } from '../serialize/type_registry.js';
10
-
11
- const ZERO_BUFFER = Buffer.alloc(32);
5
+ import { toBigIntBE, toBufferBE } from '../../bigint-buffer/index.js';
6
+ import { randomBytes } from '../../crypto/random/index.js';
7
+ import { hexSchemaFor } from '../../schemas/utils.js';
8
+ import { BufferReader } from '../../serialize/buffer_reader.js';
9
+ import { TypeRegistry } from '../../serialize/type_registry.js';
12
10
 
13
11
  /* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
14
12
 
@@ -25,14 +23,12 @@ type DerivedField<T extends BaseField> = {
25
23
 
26
24
  /**
27
25
  * Base field class.
28
- * Conversions from Buffer to BigInt and vice-versa are not cheap.
29
- * We allow construction with either form and lazily convert to other as needed.
30
- * We only check we are within the field modulus when initializing with bigint.
26
+ * Uses bigint as the internal representation.
27
+ * Buffers are generated on demand from the bigint value.
31
28
  */
32
29
  abstract class BaseField {
33
30
  static SIZE_IN_BYTES = 32;
34
- private asBuffer?: Buffer;
35
- private asBigInt?: bigint;
31
+ private readonly asBigInt: bigint;
36
32
 
37
33
  /**
38
34
  * Return bigint representation.
@@ -52,53 +48,41 @@ abstract class BaseField {
52
48
  if (value.length > BaseField.SIZE_IN_BYTES) {
53
49
  throw new Error(`Value length ${value.length} exceeds ${BaseField.SIZE_IN_BYTES}`);
54
50
  }
55
- this.asBuffer =
56
- value.length === BaseField.SIZE_IN_BYTES
57
- ? value
58
- : Buffer.concat([Buffer.alloc(BaseField.SIZE_IN_BYTES - value.length), value]);
51
+ this.asBigInt = toBigIntBE(value);
59
52
  } else if (typeof value === 'bigint' || typeof value === 'number' || typeof value === 'boolean') {
60
53
  this.asBigInt = BigInt(value);
61
- if (this.asBigInt >= this.modulus()) {
62
- throw new Error(`Value 0x${this.asBigInt.toString(16)} is greater or equal to field modulus.`);
63
- } else if (this.asBigInt < 0n) {
64
- throw new Error(`Value 0x${this.asBigInt.toString(16)} is negative.`);
65
- }
66
54
  } else if (value instanceof BaseField) {
67
- this.asBuffer = value.asBuffer;
68
55
  this.asBigInt = value.asBigInt;
69
56
  } else {
70
57
  throw new Error(`Type '${typeof value}' with value '${value}' passed to BaseField ctor.`);
71
58
  }
59
+
60
+ if (this.asBigInt < 0n) {
61
+ throw new Error(`Value 0x${this.asBigInt.toString(16)} is negative.`);
62
+ } else if (this.asBigInt >= this.modulus()) {
63
+ throw new Error(`Value 0x${this.asBigInt.toString(16)} is greater or equal to field modulus.`);
64
+ }
72
65
  }
73
66
 
74
67
  protected abstract modulus(): bigint;
75
68
 
76
69
  /**
77
- * We return a copy of the Buffer to ensure this remains immutable.
70
+ * Converts the bigint to a Buffer.
78
71
  */
79
72
  toBuffer(): Buffer {
80
- if (!this.asBuffer) {
81
- this.asBuffer = toBufferBE(this.asBigInt!, 32);
82
- }
83
- return Buffer.from(this.asBuffer);
73
+ return toBufferBE(this.asBigInt, 32);
84
74
  }
85
75
 
86
76
  toString(): `0x${string}` {
87
- return `0x${this.toBuffer().toString('hex')}`;
77
+ return `0x${this.asBigInt.toString(16).padStart(64, '0')}`;
88
78
  }
89
79
 
90
80
  toBigInt(): bigint {
91
- if (this.asBigInt === undefined) {
92
- this.asBigInt = toBigIntBE(this.asBuffer!);
93
- if (this.asBigInt >= this.modulus()) {
94
- throw new Error(`Value 0x${this.asBigInt.toString(16)} is greater or equal to field modulus.`);
95
- }
96
- }
97
81
  return this.asBigInt;
98
82
  }
99
83
 
100
84
  toBool(): boolean {
101
- return Boolean(this.toBigInt());
85
+ return this.asBigInt !== 0n;
102
86
  }
103
87
 
104
88
  /**
@@ -106,11 +90,10 @@ abstract class BaseField {
106
90
  * Throws if the underlying value is greater than MAX_SAFE_INTEGER.
107
91
  */
108
92
  toNumber(): number {
109
- const value = this.toBigInt();
110
- if (value > Number.MAX_SAFE_INTEGER) {
111
- throw new Error(`Value ${value.toString(16)} greater than than max safe integer`);
93
+ if (this.asBigInt > Number.MAX_SAFE_INTEGER) {
94
+ throw new Error(`Value ${this.asBigInt.toString(16)} greater than than max safe integer`);
112
95
  }
113
- return Number(value);
96
+ return Number(this.asBigInt);
114
97
  }
115
98
 
116
99
  /**
@@ -118,8 +101,7 @@ abstract class BaseField {
118
101
  * May cause loss of precision if the underlying value is greater than MAX_SAFE_INTEGER.
119
102
  */
120
103
  toNumberUnsafe(): number {
121
- const value = this.toBigInt();
122
- return Number(value);
104
+ return Number(this.asBigInt);
123
105
  }
124
106
 
125
107
  toShortString(): string {
@@ -128,21 +110,28 @@ abstract class BaseField {
128
110
  }
129
111
 
130
112
  equals(rhs: BaseField): boolean {
131
- return this.toBuffer().equals(rhs.toBuffer());
113
+ return this.asBigInt === rhs.asBigInt;
132
114
  }
133
115
 
134
116
  lt(rhs: BaseField): boolean {
135
- return this.toBigInt() < rhs.toBigInt();
117
+ return this.asBigInt < rhs.asBigInt;
136
118
  }
137
119
 
138
120
  cmp(rhs: BaseField): -1 | 0 | 1 {
139
- const lhsBigInt = this.toBigInt();
140
- const rhsBigInt = rhs.toBigInt();
141
- return lhsBigInt === rhsBigInt ? 0 : lhsBigInt < rhsBigInt ? -1 : 1;
121
+ return BaseField.cmpAsBigInt(this.asBigInt, rhs.asBigInt);
122
+ }
123
+
124
+ static cmp(lhs: BaseField, rhs: BaseField): -1 | 0 | 1 {
125
+ return lhs.cmp(rhs);
126
+ }
127
+
128
+ // Actual bigint comparison. Arguments must have been validated previously.
129
+ static cmpAsBigInt(lhs: bigint, rhs: bigint): -1 | 0 | 1 {
130
+ return lhs === rhs ? 0 : lhs < rhs ? -1 : 1;
142
131
  }
143
132
 
144
133
  isZero(): boolean {
145
- return this.toBuffer().equals(ZERO_BUFFER);
134
+ return this.asBigInt === 0n;
146
135
  }
147
136
 
148
137
  isEmpty(): boolean {
@@ -195,7 +184,7 @@ function fromHexString<T extends BaseField>(buf: string, f: DerivedField<T>) {
195
184
 
196
185
  const buffer = Buffer.from(checked.length % 2 === 1 ? '0' + checked : checked, 'hex');
197
186
 
198
- return new f(buffer);
187
+ return new f(toBigIntBE(buffer));
199
188
  }
200
189
 
201
190
  /** Branding to ensure fields are not interchangeable types. */
@@ -511,14 +500,6 @@ function extendedEuclidean(a: bigint, modulus: bigint): [bigint, bigint, bigint]
511
500
  }
512
501
  }
513
502
 
514
- /**
515
- * GrumpkinScalar is an Fq.
516
- * @remarks Called GrumpkinScalar because it is used to represent elements in Grumpkin's scalar field as defined in
517
- * the Aztec Protocol Specs.
518
- */
519
- export type GrumpkinScalar = Fq;
520
- export const GrumpkinScalar = Fq;
521
-
522
503
  /** Wraps a function that returns a buffer so that all results are reduced into a field of the given type. */
523
504
  export function reduceFn<TInput, TField extends BaseField>(fn: (input: TInput) => Buffer, field: DerivedField<TField>) {
524
505
  return (input: TInput) => fromBufferReduce(fn(input), field);
@@ -0,0 +1,2 @@
1
+ export * from './field.js';
2
+ export * from './point.js';
@@ -0,0 +1,170 @@
1
+ import {
2
+ BN254_G1_GENERATOR,
3
+ BN254_G2_GENERATOR,
4
+ BarretenbergSync,
5
+ type Bn254G1Point as BbApiBn254G1Point,
6
+ type Bn254G2Point as BbApiBn254G2Point,
7
+ } from '@aztec/bb.js';
8
+
9
+ import { Fq, Fr } from './field.js';
10
+
11
+ /**
12
+ * BN254 G1 point using foundation field classes.
13
+ * Represents a point on the BN254 elliptic curve in affine coordinates.
14
+ */
15
+ export class Bn254G1Point {
16
+ constructor(
17
+ public readonly x: Fq,
18
+ public readonly y: Fq,
19
+ ) {}
20
+
21
+ private toBbApiPoint(): BbApiBn254G1Point {
22
+ return {
23
+ x: this.x.toBuffer(),
24
+ y: this.y.toBuffer(),
25
+ };
26
+ }
27
+
28
+ private static fromBbApiPoint(point: BbApiBn254G1Point): Bn254G1Point {
29
+ return new Bn254G1Point(Fq.fromBuffer(Buffer.from(point.x)), Fq.fromBuffer(Buffer.from(point.y)));
30
+ }
31
+
32
+ async isOnCurve(): Promise<boolean> {
33
+ await BarretenbergSync.initSingleton();
34
+ const api = BarretenbergSync.getSingleton();
35
+
36
+ const apiPoint = this.toBbApiPoint();
37
+ const response = api.bn254G1IsOnCurve({ point: apiPoint });
38
+ return response.isOnCurve;
39
+ }
40
+
41
+ /**
42
+ * Get the generator point for BN254 G1, or perform scalar multiplication.
43
+ * When called without arguments, returns the base generator point.
44
+ * When called with a scalar, returns scalar * generator (useful for public key derivation).
45
+ */
46
+ static async generator(scalar?: Fr): Promise<Bn254G1Point> {
47
+ if (!scalar) {
48
+ return new Bn254G1Point(
49
+ Fq.fromBuffer(Buffer.from(BN254_G1_GENERATOR.x)),
50
+ Fq.fromBuffer(Buffer.from(BN254_G1_GENERATOR.y)),
51
+ );
52
+ }
53
+
54
+ await BarretenbergSync.initSingleton();
55
+ const api = BarretenbergSync.getSingleton();
56
+
57
+ const response = api.bn254G1Mul({
58
+ point: BN254_G1_GENERATOR,
59
+ scalar: scalar.toBuffer(),
60
+ });
61
+
62
+ return Bn254G1Point.fromBbApiPoint(response.point);
63
+ }
64
+
65
+ /**
66
+ * Decompress a BN254 G1 point from compressed form (32 bytes).
67
+ * The compressed format encodes the x-coordinate and the sign bit of the y-coordinate
68
+ * in the most significant bit.
69
+ */
70
+ static async fromCompressed(compressed: Buffer): Promise<Bn254G1Point> {
71
+ if (compressed.length !== 32) {
72
+ throw new Error('Invalid compressed point length');
73
+ }
74
+ await BarretenbergSync.initSingleton();
75
+ const api = BarretenbergSync.getSingleton();
76
+
77
+ const response = api.bn254G1FromCompressed({
78
+ compressed: new Uint8Array(compressed),
79
+ });
80
+
81
+ return Bn254G1Point.fromBbApiPoint(response.point);
82
+ }
83
+
84
+ /**
85
+ * Compress this BN254 G1 point to 32 bytes.
86
+ * The compressed format encodes the x-coordinate and the sign bit of the y-coordinate
87
+ * in the most significant bit (bit 255).
88
+ */
89
+ compress(): Buffer {
90
+ const xBytes = this.x.toBuffer();
91
+ // Get the least significant bit of y to determine the sign
92
+ const yLsb = this.y.toBigInt() & 1n;
93
+ // If y is odd, set the most significant bit (bit 255) of the output
94
+ if (yLsb === 1n) {
95
+ xBytes[0] |= 0x80;
96
+ }
97
+ return xBytes;
98
+ }
99
+
100
+ equals(other: Bn254G1Point): boolean {
101
+ return this.x.equals(other.x) && this.y.equals(other.y);
102
+ }
103
+
104
+ toString(): string {
105
+ return `Bn254G1Point(x: ${this.x.toString()}, y: ${this.y.toString()})`;
106
+ }
107
+ }
108
+
109
+ /**
110
+ * BN254 G2 point using foundation field classes.
111
+ * Represents a point on the BN254 G2 curve (twist curve) in affine coordinates.
112
+ * G2 points use extension field coordinates (Fq2).
113
+ */
114
+ export class Bn254G2Point {
115
+ constructor(
116
+ public readonly x: [Fq, Fq],
117
+ public readonly y: [Fq, Fq],
118
+ ) {}
119
+
120
+ private toBbApiPoint(): BbApiBn254G2Point {
121
+ return {
122
+ x: [this.x[0].toBuffer(), this.x[1].toBuffer()],
123
+ y: [this.y[0].toBuffer(), this.y[1].toBuffer()],
124
+ };
125
+ }
126
+
127
+ private static fromBbApiPoint(point: BbApiBn254G2Point): Bn254G2Point {
128
+ return new Bn254G2Point(
129
+ [Fq.fromBuffer(Buffer.from(point.x[0])), Fq.fromBuffer(Buffer.from(point.x[1]))],
130
+ [Fq.fromBuffer(Buffer.from(point.y[0])), Fq.fromBuffer(Buffer.from(point.y[1]))],
131
+ );
132
+ }
133
+
134
+ /**
135
+ * Get the generator point for BN254 G2, or perform scalar multiplication.
136
+ * When called without arguments, returns the base generator point.
137
+ * When called with a scalar, returns scalar * generator.
138
+ */
139
+ static async generator(scalar?: Fr): Promise<Bn254G2Point> {
140
+ if (!scalar) {
141
+ return new Bn254G2Point(
142
+ [Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.x[0])), Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.x[1]))],
143
+ [Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.y[0])), Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.y[1]))],
144
+ );
145
+ }
146
+
147
+ await BarretenbergSync.initSingleton();
148
+ const api = BarretenbergSync.getSingleton();
149
+
150
+ const response = api.bn254G2Mul({
151
+ point: BN254_G2_GENERATOR as BbApiBn254G2Point,
152
+ scalar: scalar.toBuffer(),
153
+ });
154
+
155
+ return Bn254G2Point.fromBbApiPoint(response.point);
156
+ }
157
+
158
+ equals(other: Bn254G2Point): boolean {
159
+ return (
160
+ this.x[0].equals(other.x[0]) &&
161
+ this.x[1].equals(other.x[1]) &&
162
+ this.y[0].equals(other.y[0]) &&
163
+ this.y[1].equals(other.y[1])
164
+ );
165
+ }
166
+
167
+ toString(): string {
168
+ return `Bn254G2Point(x: (${this.x[0].toString()}, ${this.x[1].toString()}), y: (${this.y[0].toString()}, ${this.y[1].toString()}))`;
169
+ }
170
+ }
@@ -0,0 +1,11 @@
1
+ import { Fq } from '../bn254/field.js';
2
+
3
+ export * from './point.js';
4
+
5
+ /**
6
+ * GrumpkinScalar is an Fq.
7
+ * @remarks Called GrumpkinScalar because it is used to represent elements in Grumpkin's scalar field as defined in
8
+ * the Aztec Protocol Specs.
9
+ */
10
+ export type GrumpkinScalar = Fq;
11
+ export const GrumpkinScalar = Fq;