@decryption/curves 0.2.0

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 (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +75 -0
  3. package/abstract/bls.d.ts +410 -0
  4. package/abstract/bls.js +433 -0
  5. package/abstract/curve.d.ts +441 -0
  6. package/abstract/curve.js +622 -0
  7. package/abstract/edwards.d.ts +319 -0
  8. package/abstract/edwards.js +697 -0
  9. package/abstract/fft.d.ts +438 -0
  10. package/abstract/fft.js +580 -0
  11. package/abstract/frost.d.ts +292 -0
  12. package/abstract/frost.js +705 -0
  13. package/abstract/hash-to-curve.d.ts +238 -0
  14. package/abstract/hash-to-curve.js +353 -0
  15. package/abstract/modular.d.ts +550 -0
  16. package/abstract/modular.js +872 -0
  17. package/abstract/montgomery.d.ts +95 -0
  18. package/abstract/montgomery.js +180 -0
  19. package/abstract/oprf.d.ts +354 -0
  20. package/abstract/oprf.js +358 -0
  21. package/abstract/poseidon.d.ts +198 -0
  22. package/abstract/poseidon.js +471 -0
  23. package/abstract/tower.d.ts +213 -0
  24. package/abstract/tower.js +963 -0
  25. package/abstract/weierstrass.d.ts +587 -0
  26. package/abstract/weierstrass.js +1421 -0
  27. package/bls12-381.d.ts +23 -0
  28. package/bls12-381.js +680 -0
  29. package/bn254.d.ts +74 -0
  30. package/bn254.js +248 -0
  31. package/ed25519.d.ts +211 -0
  32. package/ed25519.js +626 -0
  33. package/ed448.d.ts +184 -0
  34. package/ed448.js +596 -0
  35. package/esm/abstract/bls.js +429 -0
  36. package/esm/abstract/curve.js +610 -0
  37. package/esm/abstract/edwards.js +691 -0
  38. package/esm/abstract/fft.js +567 -0
  39. package/esm/abstract/frost.js +702 -0
  40. package/esm/abstract/hash-to-curve.js +345 -0
  41. package/esm/abstract/modular.js +849 -0
  42. package/esm/abstract/montgomery.js +177 -0
  43. package/esm/abstract/oprf.js +355 -0
  44. package/esm/abstract/poseidon.js +462 -0
  45. package/esm/abstract/tower.js +925 -0
  46. package/esm/abstract/weierstrass.js +1411 -0
  47. package/esm/bls12-381.js +677 -0
  48. package/esm/bn254.js +244 -0
  49. package/esm/ed25519.js +622 -0
  50. package/esm/ed448.js +593 -0
  51. package/esm/index.js +38 -0
  52. package/esm/misc.js +211 -0
  53. package/esm/nist.js +266 -0
  54. package/esm/secp256k1.js +465 -0
  55. package/esm/utils.js +597 -0
  56. package/esm/webcrypto.js +362 -0
  57. package/index.d.ts +0 -0
  58. package/index.js +38 -0
  59. package/misc.d.ts +113 -0
  60. package/misc.js +216 -0
  61. package/nist.d.ts +136 -0
  62. package/nist.js +269 -0
  63. package/package.json +46 -0
  64. package/secp256k1.d.ts +152 -0
  65. package/secp256k1.js +468 -0
  66. package/utils.d.ts +535 -0
  67. package/utils.js +626 -0
  68. package/webcrypto.d.ts +240 -0
  69. package/webcrypto.js +365 -0
package/bls12-381.js ADDED
@@ -0,0 +1,680 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bls12_381 = exports.bls12_381_Fr = void 0;
4
+ /**
5
+ * bls12-381 is pairing-friendly Barreto-Lynn-Scott elliptic curve construction allowing to:
6
+
7
+ * Construct zk-SNARKs at the ~120-bit security, as per [Barbulescu-Duquesne 2017](https://hal.science/hal-01534101/file/main.pdf)
8
+ * Efficiently verify N aggregate signatures with 1 pairing and N ec additions:
9
+ the Boneh-Lynn-Shacham signature scheme is orders of magnitude more efficient than Schnorr
10
+
11
+ BLS can mean 2 different things:
12
+
13
+ * Barreto-Lynn-Scott: BLS12, a Pairing Friendly Elliptic Curve
14
+ * Boneh-Lynn-Shacham: A Signature Scheme.
15
+
16
+ ### Summary
17
+
18
+ 1. BLS Relies on expensive bilinear pairing
19
+ 2. Secret Keys: 32 bytes
20
+ 3. Public Keys: 48 OR 96 bytes - big-endian x coordinate of point on G1 OR G2 curve
21
+ 4. Signatures: 96 OR 48 bytes - big-endian x coordinate of point on G2 OR G1 curve
22
+ 5. The 12 stands for the Embedding degree.
23
+
24
+ Modes of operation:
25
+
26
+ * Long signatures: 48-byte keys + 96-byte sigs (G1 keys + G2 sigs).
27
+ * Short signatures: 96-byte keys + 48-byte sigs (G2 keys + G1 sigs).
28
+
29
+ ### Formulas
30
+
31
+ - `P = pk x G` - public keys
32
+ - `S = pk x H(m)` - signing, uses hash-to-curve on m
33
+ - `e(P, H(m)) == e(G, S)` - verification using pairings
34
+ - `e(G, S) = e(G, SUM(n)(Si)) = MUL(n)(e(G, Si))` - signature aggregation
35
+
36
+ ### Curves
37
+
38
+ G1 is ordinary elliptic curve. G2 is extension field curve, think "over complex numbers".
39
+
40
+ - G1: y² = x³ + 4
41
+ - G2: y² = x³ + 4(u + 1) where u = √−1; r-order subgroup of E'(Fp²), M-type twist
42
+
43
+ ### Towers
44
+
45
+ Pairing G1 + G2 produces element in Fp₁₂, 12-degree polynomial.
46
+ Fp₁₂ is usually implemented using tower of lower-degree polynomials for speed.
47
+
48
+ - Fp₁₂ = Fp₆² => Fp₂³
49
+ - Fp(u) / (u² - β) where β = -1
50
+ - Fp₂(v) / (v³ - ξ) where ξ = u + 1
51
+ - Fp₆(w) / (w² - γ) where γ = v
52
+ - Fp²[u] = Fp/u²+1
53
+ - Fp⁶[v] = Fp²/v³-1-u
54
+ - Fp¹²[w] = Fp⁶/w²-v
55
+
56
+ ### Params
57
+
58
+ * Embedding degree (k): 12
59
+ * Seed is sometimes named x or t
60
+ * t = -15132376222941642752
61
+ * p = (t-1)² * (t⁴-t²+1)/3 + t
62
+ * r = t⁴-t²+1
63
+ * Ate loop size: X
64
+
65
+ To verify curve parameters, see
66
+ [pairing-friendly-curves spec](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-pairing-friendly-curves-11).
67
+ Basic math is done over finite fields over p.
68
+ More complicated math is done over polynominal extension fields.
69
+
70
+ ### Compatibility and notes
71
+ 1. It is compatible with Algorand, Chia, Dfinity, Ethereum, Filecoin, ZEC.
72
+ Filecoin uses little endian byte arrays for secret keys - make sure to reverse byte order.
73
+ 2. Make sure to correctly select mode: "long signature" or "short signature".
74
+ 3. Compatible with specs:
75
+ RFC 9380,
76
+ [cfrg-pairing-friendly-curves-11](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-pairing-friendly-curves-11),
77
+ [cfrg-bls-signature-05](https://datatracker.ietf.org/doc/draft-irtf-cfrg-bls-signature/).
78
+
79
+ *
80
+ * @module
81
+ */
82
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
83
+ const sha2_1 = require("@decryption/hashes/sha2");
84
+ const bls_1 = require("./abstract/bls");
85
+ const modular_1 = require("./abstract/modular");
86
+ const utils_1 = require("./utils");
87
+ // Types
88
+ const hash_to_curve_1 = require("./abstract/hash-to-curve");
89
+ const tower_1 = require("./abstract/tower");
90
+ const weierstrass_1 = require("./abstract/weierstrass");
91
+ // Be friendly to bad ECMAScript parsers by not using bigint literals
92
+ // prettier-ignore
93
+ const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4);
94
+ // To verify math:
95
+ // https://tools.ietf.org/html/draft-irtf-cfrg-pairing-friendly-curves-11
96
+ // The BLS parameter x (seed) for BLS12-381. The stored constant is `|x|`; call
97
+ // sites that need the signed parameter apply the minus sign themselves.
98
+ // x = -2^63 - 2^62 - 2^60 - 2^57 - 2^48 - 2^16
99
+ const BLS_X = BigInt('0xd201000000010000');
100
+ // t = x (called differently in different places)
101
+ // const t = -BLS_X;
102
+ const BLS_X_LEN = (0, utils_1.bitLen)(BLS_X);
103
+ // a=0, b=4
104
+ // P is characteristic of field Fp, in which curve calculations are done.
105
+ // p = (t-1)² * (t⁴-t²+1)/3 + t
106
+ // bls12_381_Fp = (t-1n)**2n * (t**4n - t**2n + 1n) / 3n + t
107
+ // r*h is curve order, amount of points on curve,
108
+ // where r is order of prime subgroup and h is cofactor.
109
+ // r = t⁴-t²+1
110
+ // r = (t**4n - t**2n + 1n)
111
+ // cofactor h of G1: (t - 1)²/3, with the signed convention `t = -x`
112
+ // cofactorG1 = (t-1n)**2n/3n
113
+ // x = 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507
114
+ // y = 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569
115
+ const bls12_381_CURVE_G1 = {
116
+ p: BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab'),
117
+ n: BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001'),
118
+ h: BigInt('0x396c8c005555e1568c00aaab0000aaab'),
119
+ a: _0n,
120
+ b: _4n,
121
+ Gx: BigInt('0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb'),
122
+ Gy: BigInt('0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1'),
123
+ };
124
+ // CURVE FIELDS
125
+ // r = z⁴ − z² + 1; CURVE.n from other curves
126
+ /**
127
+ * bls12-381 Fr (Fn) field.
128
+ * `fromBytes()` reduces modulo `q` instead of rejecting non-canonical encodings.
129
+ */
130
+ exports.bls12_381_Fr = (0, modular_1.Field)(bls12_381_CURVE_G1.n, {
131
+ modFromBytes: true,
132
+ });
133
+ const { Fp, Fp2, Fp6, Fp12 } = (0, tower_1.tower12)({
134
+ ORDER: bls12_381_CURVE_G1.p,
135
+ X_LEN: BLS_X_LEN,
136
+ // Finite extension field over irreducible polynominal.
137
+ // Fp(u) / (u² - β) where β = -1
138
+ // Public `Fp2.NONRESIDUE` below is the sextic-tower value `(1, 1) = u + 1`;
139
+ // the quadratic non-residue for the base Fp2 construction is still `-1`.
140
+ FP2_NONRESIDUE: [_1n, _1n],
141
+ Fp2mulByB: ({ c0, c1 }) => {
142
+ const t0 = Fp.mul(c0, _4n); // 4 * c0
143
+ const t1 = Fp.mul(c1, _4n); // 4 * c1
144
+ // (T0-T1) + (T0+T1)*i
145
+ return { c0: Fp.sub(t0, t1), c1: Fp.add(t0, t1) };
146
+ },
147
+ Fp12finalExponentiate: (num) => {
148
+ const x = BLS_X;
149
+ // this^(q⁶) / this
150
+ const t0 = Fp12.div(Fp12.frobeniusMap(num, 6), num);
151
+ // t0^(q²) * t0
152
+ const t1 = Fp12.mul(Fp12.frobeniusMap(t0, 2), t0);
153
+ const t2 = Fp12.conjugate(Fp12._cyclotomicExp(t1, x));
154
+ const t3 = Fp12.mul(Fp12.conjugate(Fp12._cyclotomicSquare(t1)), t2);
155
+ const t4 = Fp12.conjugate(Fp12._cyclotomicExp(t3, x));
156
+ const t5 = Fp12.conjugate(Fp12._cyclotomicExp(t4, x));
157
+ const t6 = Fp12.mul(Fp12.conjugate(Fp12._cyclotomicExp(t5, x)), Fp12._cyclotomicSquare(t2));
158
+ const t7 = Fp12.conjugate(Fp12._cyclotomicExp(t6, x));
159
+ const t2_t5_pow_q2 = Fp12.frobeniusMap(Fp12.mul(t2, t5), 2);
160
+ const t4_t1_pow_q3 = Fp12.frobeniusMap(Fp12.mul(t4, t1), 3);
161
+ const t6_t1c_pow_q1 = Fp12.frobeniusMap(Fp12.mul(t6, Fp12.conjugate(t1)), 1);
162
+ const t7_t3c_t1 = Fp12.mul(Fp12.mul(t7, Fp12.conjugate(t3)), t1);
163
+ // (t2 * t5)^(q²) * (t4 * t1)^(q³) * (t6 * t1.conj)^(q^1) * t7 * t3.conj * t1
164
+ return Fp12.mul(Fp12.mul(Fp12.mul(t2_t5_pow_q2, t4_t1_pow_q3), t6_t1c_pow_q1), t7_t3c_t1);
165
+ },
166
+ });
167
+ // GLV endomorphism Ψ(P), for fast cofactor clearing. `Fp2.NONRESIDUE` here is
168
+ // the tower value `u + 1`, so the Frobenius base passed to psiFrobenius is
169
+ // `1 / (u + 1)`, and psi2 derives the published `1 / 2^((p - 1) / 3)` constant internally.
170
+ let frob;
171
+ const getFrob = () => frob || (frob = (0, tower_1.psiFrobenius)(Fp, Fp2, Fp2.div(Fp2.ONE, Fp2.NONRESIDUE)));
172
+ // Eager psiFrobenius setup now dominates `bls12-381.js` import, so defer it to
173
+ // first use. After that these locals are rewritten to the direct helper refs.
174
+ let G2psi = (c, P) => {
175
+ const fn = getFrob().G2psi;
176
+ G2psi = fn;
177
+ return fn(c, P);
178
+ };
179
+ let G2psi2 = (c, P) => {
180
+ const fn = getFrob().G2psi2;
181
+ G2psi2 = fn;
182
+ return fn(c, P);
183
+ };
184
+ /**
185
+ * Default hash_to_field / hash-to-curve for BLS.
186
+ * m: 1 for G1, 2 for G2
187
+ * k: target security level in bits
188
+ * hash: any function, e.g. BBS+ uses BLAKE2: see [github](https://github.com/hyperledger/aries-framework-go/issues/2247).
189
+ * Field/hash parameters come from [section 8.8.2 of RFC 9380](https://www.rfc-editor.org/rfc/rfc9380#section-8.8.2),
190
+ * but the `DST` / `encodeDST` strings below are the BLS-signature-suite override.
191
+ */
192
+ const hasher_opts = Object.freeze({
193
+ DST: 'BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_',
194
+ encodeDST: 'BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_',
195
+ p: Fp.ORDER,
196
+ m: 2,
197
+ k: 128,
198
+ expand: 'xmd',
199
+ hash: sha2_1.sha256,
200
+ });
201
+ // a=0, b=4
202
+ // cofactor h of G2, derived with the signed convention `t = -x`
203
+ // (t^8 - 4t^7 + 5t^6 - 4t^4 + 6t^3 - 4t^2 - 4t + 13)/9
204
+ // cofactorG2 = (t**8n - 4n*t**7n + 5n*t**6n - 4n*t**4n + 6n*t**3n - 4n*t**2n - 4n*t+13n)/9n
205
+ // x = 3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758*u + 352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160
206
+ // y = 927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582*u + 1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905
207
+ const bls12_381_CURVE_G2 = {
208
+ p: Fp2.ORDER,
209
+ n: bls12_381_CURVE_G1.n,
210
+ h: BigInt('0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5'),
211
+ a: Fp2.ZERO,
212
+ b: Fp2.fromBigTuple([_4n, _4n]),
213
+ Gx: Fp2.fromBigTuple([
214
+ BigInt('0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8'),
215
+ BigInt('0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e'),
216
+ ]),
217
+ Gy: Fp2.fromBigTuple([
218
+ BigInt('0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801'),
219
+ BigInt('0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be'),
220
+ ]),
221
+ };
222
+ // Encoding utils
223
+ const sortBit = (parts, p) => {
224
+ for (const part of parts) {
225
+ if (part !== _0n)
226
+ return Boolean((part * _2n) / p);
227
+ }
228
+ return false;
229
+ };
230
+ const fp2 = {
231
+ // Generic tower bytes use `c0 || c1`, but the BLS12-381 G2 point/signature wire encoding uses
232
+ // `c1 || c0`, so keep this local wrapper instead of changing generic field serialization.
233
+ encode({ c0, c1 }) {
234
+ const { BYTES: L } = Fp;
235
+ return (0, utils_1.concatBytes)((0, utils_1.numberToBytesBE)(c1, L), (0, utils_1.numberToBytesBE)(c0, L));
236
+ },
237
+ decode(bytes) {
238
+ const { BYTES: L } = Fp;
239
+ return Fp2.create({
240
+ c0: Fp.create((0, utils_1.bytesToNumberBE)(bytes.subarray(L))),
241
+ c1: Fp.create((0, utils_1.bytesToNumberBE)(bytes.subarray(0, L))),
242
+ });
243
+ },
244
+ };
245
+ const BaseFp = Fp;
246
+ // Keep BLS12-381 point/signature codecs on one control-flow skeleton: the G1/G2
247
+ // and point/signature variants differ only in field packing, subgroup bytes, and
248
+ // whether uncompressed form is allowed. Copy-paste decoders were diverging.
249
+ const coder = (name, Fp, b, encode, decode, yparts) => {
250
+ const F = Fp;
251
+ const enc = encode;
252
+ const dec = decode;
253
+ const W = F.BYTES;
254
+ return (allowUncompressed) => ({
255
+ encode(point, compressed = true) {
256
+ if (!compressed && !allowUncompressed)
257
+ throw new Error('invalid signature: expected compressed encoding');
258
+ const infinity = point.is0();
259
+ const { x, y } = point.toAffine();
260
+ const bytes = compressed ? enc(x) : (0, utils_1.concatBytes)(enc(x), enc(y));
261
+ let sort;
262
+ if (compressed && !infinity)
263
+ sort = sortBit(yparts(y), BaseFp.ORDER);
264
+ return setMask(bytes, { compressed, infinity, sort });
265
+ },
266
+ decode(bytes) {
267
+ const raw = allowUncompressed
268
+ ? (0, utils_1.abytes)(bytes, undefined, 'point')
269
+ : (0, utils_1.abytes)(bytes, W, 'signature');
270
+ const { compressed, infinity, sort, value } = parseMask(raw);
271
+ if (!allowUncompressed && !compressed)
272
+ throw new Error('invalid signature: expected compressed encoding');
273
+ const len = compressed ? W : 2 * W;
274
+ if (value.length !== len)
275
+ throw new Error(`invalid ${name} point: expected ${len} bytes`);
276
+ if (infinity) {
277
+ // Infinity canonicality has to be checked on raw bytes before decode()
278
+ // reduces coordinates modulo p and turns non-empty payloads into zero.
279
+ for (const b of value) {
280
+ if (b)
281
+ throw new Error(`invalid ${name} point: non-canonical zero`);
282
+ }
283
+ return { x: F.ZERO, y: F.ZERO };
284
+ }
285
+ const x = dec(compressed ? value : value.subarray(0, W));
286
+ let y;
287
+ if (compressed) {
288
+ y = F.sqrt(F.add(F.pow(x, _3n), b));
289
+ if (!y)
290
+ throw new Error(`invalid ${name} point: compressed`);
291
+ if (sortBit(yparts(y), BaseFp.ORDER) !== sort)
292
+ y = F.neg(y);
293
+ }
294
+ else {
295
+ y = dec(value.subarray(W));
296
+ }
297
+ // Noble keeps the permissive coordinate reduction path here, but an
298
+ // omitted infinity flag must not still decode to ZERO afterwards.
299
+ if (!compressed && F.is0(x) && F.is0(y))
300
+ throw new Error(`invalid ${name} point: uncompressed`);
301
+ return { x, y };
302
+ },
303
+ });
304
+ };
305
+ // Internal helper only: it copies before clearing the top flag bits. The
306
+ // pairing-friendly-curves draft C.2 step 1 rejects 0x20 / 0x60 / 0xe0 because
307
+ // S_bit must be zero for infinity and for all uncompressed encodings.
308
+ function validateMask({ compressed, infinity, sort }) {
309
+ if ((!compressed && !infinity && sort) || // 0010_0000 = 0x20
310
+ (!compressed && infinity && sort) || // 0110_0000 = 0x60
311
+ (compressed && infinity && sort) // 1110_0000 = 0xe0
312
+ )
313
+ throw new Error('invalid encoding flag');
314
+ }
315
+ function parseMask(bytes) {
316
+ // Copy, so we can remove mask data.
317
+ // It will be removed also later, when Fp.create will call modulo.
318
+ bytes = (0, utils_1.copyBytes)(bytes);
319
+ const mask = bytes[0] & 0b1110_0000;
320
+ const compressed = !!((mask >> 7) & 1); // compression bit (0b1000_0000)
321
+ const infinity = !!((mask >> 6) & 1); // point at infinity bit (0b0100_0000)
322
+ const sort = !!((mask >> 5) & 1); // sort bit (0b0010_0000)
323
+ validateMask({ compressed, infinity, sort });
324
+ bytes[0] &= 0b0001_1111; // clear mask (zero first 3 bits)
325
+ return { compressed, infinity, sort, value: bytes };
326
+ }
327
+ // Internal helper only: mutates a non-empty fresh buffer in place and just
328
+ // sets bits. Keep the same invalid-flag guard as parseMask() so encoders cannot
329
+ // manufacture states that decoders already reject.
330
+ function setMask(bytes, mask) {
331
+ if (bytes[0] & 0b1110_0000)
332
+ throw new Error('setMask: non-empty mask');
333
+ validateMask({ compressed: !!mask.compressed, infinity: !!mask.infinity, sort: !!mask.sort });
334
+ if (mask.compressed)
335
+ bytes[0] |= 0b1000_0000;
336
+ if (mask.infinity)
337
+ bytes[0] |= 0b0100_0000;
338
+ if (mask.sort)
339
+ bytes[0] |= 0b0010_0000;
340
+ return bytes;
341
+ }
342
+ const g1coder = coder('G1', Fp, Fp.create(bls12_381_CURVE_G1.b), (x) => (0, utils_1.numberToBytesBE)(x, Fp.BYTES), (bytes) => Fp.create((0, utils_1.bytesToNumberBE)(bytes) & (0, utils_1.bitMask)(Fp.BITS)), (y) => [y]);
343
+ const g1 = { point: g1coder(true), sig: g1coder(false) };
344
+ const signatureG1ToBytes = (point) => {
345
+ point.assertValidity();
346
+ return g1.sig.encode(point);
347
+ };
348
+ function signatureG1FromBytes(bytes) {
349
+ const Point = exports.bls12_381.G1.Point;
350
+ const point = Point.fromAffine(g1.sig.decode(bytes));
351
+ point.assertValidity();
352
+ return point;
353
+ }
354
+ const g2coder = coder('G2', Fp2, bls12_381_CURVE_G2.b, fp2.encode, fp2.decode, (y) => [
355
+ y.c1,
356
+ y.c0,
357
+ ]);
358
+ const g2 = { point: g2coder(true), sig: g2coder(false) };
359
+ const signatureG2ToBytes = (point) => {
360
+ point.assertValidity();
361
+ return g2.sig.encode(point);
362
+ };
363
+ function signatureG2FromBytes(bytes) {
364
+ const Point = exports.bls12_381.G2.Point;
365
+ const point = Point.fromAffine(g2.sig.decode(bytes));
366
+ point.assertValidity();
367
+ return point;
368
+ }
369
+ const signatureCoders = {
370
+ ShortSignature: {
371
+ fromBytes(bytes) {
372
+ return signatureG1FromBytes((0, utils_1.abytes)(bytes));
373
+ },
374
+ fromHex(hex) {
375
+ return signatureG1FromBytes((0, utils_1.hexToBytes)(hex));
376
+ },
377
+ toBytes(point) {
378
+ return signatureG1ToBytes(point);
379
+ },
380
+ // Historical alias: BLS signatures have a single compressed byte format here.
381
+ toRawBytes(point) {
382
+ return signatureG1ToBytes(point);
383
+ },
384
+ toHex(point) {
385
+ return (0, utils_1.bytesToHex)(signatureG1ToBytes(point));
386
+ },
387
+ },
388
+ LongSignature: {
389
+ fromBytes(bytes) {
390
+ return signatureG2FromBytes((0, utils_1.abytes)(bytes));
391
+ },
392
+ fromHex(hex) {
393
+ return signatureG2FromBytes((0, utils_1.hexToBytes)(hex));
394
+ },
395
+ toBytes(point) {
396
+ return signatureG2ToBytes(point);
397
+ },
398
+ // Historical alias: BLS signatures have a single compressed byte format here.
399
+ toRawBytes(point) {
400
+ return signatureG2ToBytes(point);
401
+ },
402
+ toHex(point) {
403
+ return (0, utils_1.bytesToHex)(signatureG2ToBytes(point));
404
+ },
405
+ },
406
+ };
407
+ const fields = {
408
+ Fp,
409
+ Fp2,
410
+ Fp6,
411
+ Fp12,
412
+ Fr: exports.bls12_381_Fr,
413
+ };
414
+ const G1_Point = (0, weierstrass_1.weierstrass)(bls12_381_CURVE_G1, {
415
+ // Public point APIs still accept infinity, even though the Zcash proof
416
+ // encoding rules cited above only define nonzero point encodings.
417
+ allowInfinityPoint: true,
418
+ Fn: exports.bls12_381_Fr,
419
+ fromBytes: g1.point.decode,
420
+ toBytes: (_c, point, isComp) => g1.point.encode(point, isComp),
421
+ // Checks is the point resides in prime-order subgroup.
422
+ // point.isTorsionFree() should return true for valid points
423
+ // It returns false for shitty points.
424
+ // https://eprint.iacr.org/2021/1130.pdf
425
+ isTorsionFree: (c, point) => {
426
+ // GLV endomorphism ψ(P)
427
+ const beta = BigInt('0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe');
428
+ const phi = new c(Fp.mul(point.X, beta), point.Y, point.Z);
429
+ // TODO: unroll
430
+ const xP = point.multiplyUnsafe(BLS_X).negate(); // [x]P
431
+ const u2P = xP.multiplyUnsafe(BLS_X); // [u2]P
432
+ return u2P.equals(phi);
433
+ },
434
+ // Clear cofactor of G1
435
+ // https://eprint.iacr.org/2019/403
436
+ clearCofactor: (_c, point) => {
437
+ // return this.multiplyUnsafe(CURVE.h);
438
+ return point.multiplyUnsafe(BLS_X).add(point); // x*P + P
439
+ },
440
+ });
441
+ const G2_Point = (0, weierstrass_1.weierstrass)(bls12_381_CURVE_G2, {
442
+ Fp: Fp2,
443
+ // Public point APIs still accept infinity, even though the Zcash proof
444
+ // encoding rules cited above only define nonzero point encodings.
445
+ allowInfinityPoint: true,
446
+ Fn: exports.bls12_381_Fr,
447
+ fromBytes: g2.point.decode,
448
+ toBytes: (_c, point, isComp) => g2.point.encode(point, isComp),
449
+ // https://eprint.iacr.org/2021/1130.pdf
450
+ // Older version: https://eprint.iacr.org/2019/814.pdf
451
+ isTorsionFree: (c, P) => {
452
+ return P.multiplyUnsafe(BLS_X).negate().equals(G2psi(c, P)); // ψ(P) == [u](P)
453
+ },
454
+ // clear_cofactor_bls12381_g2 from RFC 9380.
455
+ // https://eprint.iacr.org/2017/419.pdf
456
+ // prettier-ignore
457
+ clearCofactor: (c, P) => {
458
+ const x = BLS_X;
459
+ let t1 = P.multiplyUnsafe(x).negate(); // [-x]P
460
+ let t2 = G2psi(c, P); // Ψ(P)
461
+ let t3 = P.double(); // 2P
462
+ t3 = G2psi2(c, t3); // Ψ²(2P)
463
+ t3 = t3.subtract(t2); // Ψ²(2P) - Ψ(P)
464
+ t2 = t1.add(t2); // [-x]P + Ψ(P)
465
+ t2 = t2.multiplyUnsafe(x).negate(); // [x²]P - [x]Ψ(P)
466
+ t3 = t3.add(t2); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P)
467
+ t3 = t3.subtract(t1); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P) + [x]P
468
+ const Q = t3.subtract(P); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P) + [x]P - 1P
469
+ return Q; // [x²-x-1]P + [x-1]Ψ(P) + Ψ²(2P)
470
+ },
471
+ });
472
+ const bls12_hasher_opts = {
473
+ mapToG1: mapToG1,
474
+ mapToG2: mapToG2,
475
+ hasherOpts: hasher_opts,
476
+ // RFC 9380 Appendix J defines distinct G1/G2 RO and NU suite IDs, and
477
+ // draft-irtf-cfrg-bls-signature-06 §4.2.1 gives separate G1/G2 `_NUL_` DSTs.
478
+ // Keep G1 encode-to-curve on the G1 domain instead of inheriting G2's `encodeDST`.
479
+ hasherOptsG1: {
480
+ ...hasher_opts,
481
+ m: 1,
482
+ DST: 'BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_',
483
+ encodeDST: 'BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_',
484
+ },
485
+ hasherOptsG2: { ...hasher_opts },
486
+ };
487
+ const bls12_params = {
488
+ ateLoopSize: BLS_X, // The BLS parameter x for BLS12-381
489
+ xNegative: true,
490
+ twistType: 'multiplicative',
491
+ randomBytes: utils_1.randomBytes,
492
+ };
493
+ /**
494
+ * bls12-381 pairing-friendly curve construction.
495
+ * Provides both longSignatures and shortSignatures.
496
+ * @example
497
+ * bls12-381 pairing-friendly curve construction.
498
+ *
499
+ * ```ts
500
+ * const bls = bls12_381.longSignatures;
501
+ * const { secretKey, publicKey } = bls.keygen();
502
+ * const msg = bls.hash(new TextEncoder().encode('hello noble'));
503
+ * const sig = bls.sign(msg, secretKey);
504
+ * const isValid = bls.verify(sig, msg, publicKey);
505
+ * ```
506
+ */
507
+ exports.bls12_381 = (0, bls_1.bls)(fields, G1_Point, G2_Point, bls12_params, bls12_hasher_opts, signatureCoders);
508
+ // 3-isogeny map from E' to E https://www.rfc-editor.org/rfc/rfc9380#appendix-E.3
509
+ // Coefficients stay in ascending `k_(?,0)`..`k_(?,d)` order; isogenyMap()
510
+ // reverses them internally for Horner evaluation.
511
+ const isogenyMapG2 = (0, hash_to_curve_1.isogenyMap)(Fp2, [
512
+ // xNum
513
+ [
514
+ [
515
+ '0x5c759507e8e333ebb5b7a9a47d7ed8532c52d39fd3a042a88b58423c50ae15d5c2638e343d9c71c6238aaaaaaaa97d6',
516
+ '0x5c759507e8e333ebb5b7a9a47d7ed8532c52d39fd3a042a88b58423c50ae15d5c2638e343d9c71c6238aaaaaaaa97d6',
517
+ ],
518
+ [
519
+ '0x0',
520
+ '0x11560bf17baa99bc32126fced787c88f984f87adf7ae0c7f9a208c6b4f20a4181472aaa9cb8d555526a9ffffffffc71a',
521
+ ],
522
+ [
523
+ '0x11560bf17baa99bc32126fced787c88f984f87adf7ae0c7f9a208c6b4f20a4181472aaa9cb8d555526a9ffffffffc71e',
524
+ '0x8ab05f8bdd54cde190937e76bc3e447cc27c3d6fbd7063fcd104635a790520c0a395554e5c6aaaa9354ffffffffe38d',
525
+ ],
526
+ [
527
+ '0x171d6541fa38ccfaed6dea691f5fb614cb14b4e7f4e810aa22d6108f142b85757098e38d0f671c7188e2aaaaaaaa5ed1',
528
+ '0x0',
529
+ ],
530
+ ],
531
+ // xDen
532
+ [
533
+ [
534
+ '0x0',
535
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa63',
536
+ ],
537
+ [
538
+ '0xc',
539
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa9f',
540
+ ],
541
+ ['0x1', '0x0'], // LAST 1
542
+ ],
543
+ // yNum
544
+ [
545
+ [
546
+ '0x1530477c7ab4113b59a4c18b076d11930f7da5d4a07f649bf54439d87d27e500fc8c25ebf8c92f6812cfc71c71c6d706',
547
+ '0x1530477c7ab4113b59a4c18b076d11930f7da5d4a07f649bf54439d87d27e500fc8c25ebf8c92f6812cfc71c71c6d706',
548
+ ],
549
+ [
550
+ '0x0',
551
+ '0x5c759507e8e333ebb5b7a9a47d7ed8532c52d39fd3a042a88b58423c50ae15d5c2638e343d9c71c6238aaaaaaaa97be',
552
+ ],
553
+ [
554
+ '0x11560bf17baa99bc32126fced787c88f984f87adf7ae0c7f9a208c6b4f20a4181472aaa9cb8d555526a9ffffffffc71c',
555
+ '0x8ab05f8bdd54cde190937e76bc3e447cc27c3d6fbd7063fcd104635a790520c0a395554e5c6aaaa9354ffffffffe38f',
556
+ ],
557
+ [
558
+ '0x124c9ad43b6cf79bfbf7043de3811ad0761b0f37a1e26286b0e977c69aa274524e79097a56dc4bd9e1b371c71c718b10',
559
+ '0x0',
560
+ ],
561
+ ],
562
+ // yDen
563
+ [
564
+ [
565
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffa8fb',
566
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffa8fb',
567
+ ],
568
+ [
569
+ '0x0',
570
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffa9d3',
571
+ ],
572
+ [
573
+ '0x12',
574
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa99',
575
+ ],
576
+ ['0x1', '0x0'], // LAST 1
577
+ ],
578
+ ].map((i) => i.map((pair) => Fp2.fromBigTuple(pair.map(BigInt)))));
579
+ // 11-isogeny map from E' to E. Coefficients stay in ascending
580
+ // `k_(?,0)`..`k_(?,d)` order; isogenyMap() reverses them for Horner evaluation.
581
+ const isogenyMapG1 = (0, hash_to_curve_1.isogenyMap)(Fp, [
582
+ // xNum
583
+ [
584
+ '0x11a05f2b1e833340b809101dd99815856b303e88a2d7005ff2627b56cdb4e2c85610c2d5f2e62d6eaeac1662734649b7',
585
+ '0x17294ed3e943ab2f0588bab22147a81c7c17e75b2f6a8417f565e33c70d1e86b4838f2a6f318c356e834eef1b3cb83bb',
586
+ '0xd54005db97678ec1d1048c5d10a9a1bce032473295983e56878e501ec68e25c958c3e3d2a09729fe0179f9dac9edcb0',
587
+ '0x1778e7166fcc6db74e0609d307e55412d7f5e4656a8dbf25f1b33289f1b330835336e25ce3107193c5b388641d9b6861',
588
+ '0xe99726a3199f4436642b4b3e4118e5499db995a1257fb3f086eeb65982fac18985a286f301e77c451154ce9ac8895d9',
589
+ '0x1630c3250d7313ff01d1201bf7a74ab5db3cb17dd952799b9ed3ab9097e68f90a0870d2dcae73d19cd13c1c66f652983',
590
+ '0xd6ed6553fe44d296a3726c38ae652bfb11586264f0f8ce19008e218f9c86b2a8da25128c1052ecaddd7f225a139ed84',
591
+ '0x17b81e7701abdbe2e8743884d1117e53356de5ab275b4db1a682c62ef0f2753339b7c8f8c8f475af9ccb5618e3f0c88e',
592
+ '0x80d3cf1f9a78fc47b90b33563be990dc43b756ce79f5574a2c596c928c5d1de4fa295f296b74e956d71986a8497e317',
593
+ '0x169b1f8e1bcfa7c42e0c37515d138f22dd2ecb803a0c5c99676314baf4bb1b7fa3190b2edc0327797f241067be390c9e',
594
+ '0x10321da079ce07e272d8ec09d2565b0dfa7dccdde6787f96d50af36003b14866f69b771f8c285decca67df3f1605fb7b',
595
+ '0x6e08c248e260e70bd1e962381edee3d31d79d7e22c837bc23c0bf1bc24c6b68c24b1b80b64d391fa9c8ba2e8ba2d229',
596
+ ],
597
+ // xDen
598
+ [
599
+ '0x8ca8d548cff19ae18b2e62f4bd3fa6f01d5ef4ba35b48ba9c9588617fc8ac62b558d681be343df8993cf9fa40d21b1c',
600
+ '0x12561a5deb559c4348b4711298e536367041e8ca0cf0800c0126c2588c48bf5713daa8846cb026e9e5c8276ec82b3bff',
601
+ '0xb2962fe57a3225e8137e629bff2991f6f89416f5a718cd1fca64e00b11aceacd6a3d0967c94fedcfcc239ba5cb83e19',
602
+ '0x3425581a58ae2fec83aafef7c40eb545b08243f16b1655154cca8abc28d6fd04976d5243eecf5c4130de8938dc62cd8',
603
+ '0x13a8e162022914a80a6f1d5f43e7a07dffdfc759a12062bb8d6b44e833b306da9bd29ba81f35781d539d395b3532a21e',
604
+ '0xe7355f8e4e667b955390f7f0506c6e9395735e9ce9cad4d0a43bcef24b8982f7400d24bc4228f11c02df9a29f6304a5',
605
+ '0x772caacf16936190f3e0c63e0596721570f5799af53a1894e2e073062aede9cea73b3538f0de06cec2574496ee84a3a',
606
+ '0x14a7ac2a9d64a8b230b3f5b074cf01996e7f63c21bca68a81996e1cdf9822c580fa5b9489d11e2d311f7d99bbdcc5a5e',
607
+ '0xa10ecf6ada54f825e920b3dafc7a3cce07f8d1d7161366b74100da67f39883503826692abba43704776ec3a79a1d641',
608
+ '0x95fc13ab9e92ad4476d6e3eb3a56680f682b4ee96f7d03776df533978f31c1593174e4b4b7865002d6384d168ecdd0a',
609
+ '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001', // LAST 1
610
+ ],
611
+ // yNum
612
+ [
613
+ '0x90d97c81ba24ee0259d1f094980dcfa11ad138e48a869522b52af6c956543d3cd0c7aee9b3ba3c2be9845719707bb33',
614
+ '0x134996a104ee5811d51036d776fb46831223e96c254f383d0f906343eb67ad34d6c56711962fa8bfe097e75a2e41c696',
615
+ '0xcc786baa966e66f4a384c86a3b49942552e2d658a31ce2c344be4b91400da7d26d521628b00523b8dfe240c72de1f6',
616
+ '0x1f86376e8981c217898751ad8746757d42aa7b90eeb791c09e4a3ec03251cf9de405aba9ec61deca6355c77b0e5f4cb',
617
+ '0x8cc03fdefe0ff135caf4fe2a21529c4195536fbe3ce50b879833fd221351adc2ee7f8dc099040a841b6daecf2e8fedb',
618
+ '0x16603fca40634b6a2211e11db8f0a6a074a7d0d4afadb7bd76505c3d3ad5544e203f6326c95a807299b23ab13633a5f0',
619
+ '0x4ab0b9bcfac1bbcb2c977d027796b3ce75bb8ca2be184cb5231413c4d634f3747a87ac2460f415ec961f8855fe9d6f2',
620
+ '0x987c8d5333ab86fde9926bd2ca6c674170a05bfe3bdd81ffd038da6c26c842642f64550fedfe935a15e4ca31870fb29',
621
+ '0x9fc4018bd96684be88c9e221e4da1bb8f3abd16679dc26c1e8b6e6a1f20cabe69d65201c78607a360370e577bdba587',
622
+ '0xe1bba7a1186bdb5223abde7ada14a23c42a0ca7915af6fe06985e7ed1e4d43b9b3f7055dd4eba6f2bafaaebca731c30',
623
+ '0x19713e47937cd1be0dfd0b8f1d43fb93cd2fcbcb6caf493fd1183e416389e61031bf3a5cce3fbafce813711ad011c132',
624
+ '0x18b46a908f36f6deb918c143fed2edcc523559b8aaf0c2462e6bfe7f911f643249d9cdf41b44d606ce07c8a4d0074d8e',
625
+ '0xb182cac101b9399d155096004f53f447aa7b12a3426b08ec02710e807b4633f06c851c1919211f20d4c04f00b971ef8',
626
+ '0x245a394ad1eca9b72fc00ae7be315dc757b3b080d4c158013e6632d3c40659cc6cf90ad1c232a6442d9d3f5db980133',
627
+ '0x5c129645e44cf1102a159f748c4a3fc5e673d81d7e86568d9ab0f5d396a7ce46ba1049b6579afb7866b1e715475224b',
628
+ '0x15e6be4e990f03ce4ea50b3b42df2eb5cb181d8f84965a3957add4fa95af01b2b665027efec01c7704b456be69c8b604',
629
+ ],
630
+ // yDen
631
+ [
632
+ '0x16112c4c3a9c98b252181140fad0eae9601a6de578980be6eec3232b5be72e7a07f3688ef60c206d01479253b03663c1',
633
+ '0x1962d75c2381201e1a0cbd6c43c348b885c84ff731c4d59ca4a10356f453e01f78a4260763529e3532f6102c2e49a03d',
634
+ '0x58df3306640da276faaae7d6e8eb15778c4855551ae7f310c35a5dd279cd2eca6757cd636f96f891e2538b53dbf67f2',
635
+ '0x16b7d288798e5395f20d23bf89edb4d1d115c5dbddbcd30e123da489e726af41727364f2c28297ada8d26d98445f5416',
636
+ '0xbe0e079545f43e4b00cc912f8228ddcc6d19c9f0f69bbb0542eda0fc9dec916a20b15dc0fd2ededda39142311a5001d',
637
+ '0x8d9e5297186db2d9fb266eaac783182b70152c65550d881c5ecd87b6f0f5a6449f38db9dfa9cce202c6477faaf9b7ac',
638
+ '0x166007c08a99db2fc3ba8734ace9824b5eecfdfa8d0cf8ef5dd365bc400a0051d5fa9c01a58b1fb93d1a1399126a775c',
639
+ '0x16a3ef08be3ea7ea03bcddfabba6ff6ee5a4375efa1f4fd7feb34fd206357132b920f5b00801dee460ee415a15812ed9',
640
+ '0x1866c8ed336c61231a1be54fd1d74cc4f9fb0ce4c6af5920abc5750c4bf39b4852cfe2f7bb9248836b233d9d55535d4a',
641
+ '0x167a55cda70a6e1cea820597d94a84903216f763e13d87bb5308592e7ea7d4fbc7385ea3d529b35e346ef48bb8913f55',
642
+ '0x4d2f259eea405bd48f010a01ad2911d9c6dd039bb61a6290e591b36e636a5c871a5c29f4f83060400f8b49cba8f6aa8',
643
+ '0xaccbb67481d033ff5852c1e48c50c477f94ff8aefce42d28c0f9a88cea7913516f968986f7ebbea9684b529e2561092',
644
+ '0xad6b9514c767fe3c3613144b45f1496543346d98adf02267d5ceef9a00d9b8693000763e3b90ac11e99b138573345cc',
645
+ '0x2660400eb2e4f3b628bdd0d53cd76f2bf565b94e72927c1cb748df27942480e420517bd8714cc80d1fadc1326ed06f7',
646
+ '0xe0fa1d816ddc03e6b24255e0d7819c171c40f65e273b853324efcd6356caa205ca2f570f13497804415473a1d634b8f',
647
+ '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001', // LAST 1
648
+ ],
649
+ ].map((i) => i.map((j) => BigInt(j))));
650
+ let G1_SWU;
651
+ let G2_SWU;
652
+ // SWU setup validates the pre-isogeny curve parameters and builds sqrt-ratio helpers.
653
+ // Doing that eagerly adds about 10ms to `bls12-381.js` import here, so keep it lazy; after the
654
+ // first map call the cached mapper is reused directly.
655
+ const getG1_SWU = () => G1_SWU ||
656
+ (G1_SWU = (0, weierstrass_1.mapToCurveSimpleSWU)(Fp, {
657
+ A: Fp.create(BigInt('0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1d')),
658
+ B: Fp.create(BigInt('0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0')),
659
+ Z: Fp.create(BigInt(11)),
660
+ }));
661
+ const getG2_SWU = () => G2_SWU ||
662
+ (G2_SWU = (0, weierstrass_1.mapToCurveSimpleSWU)(Fp2, {
663
+ // SWU map for the RFC 9380 §8.8.2 pre-isogeny G2 curve E':
664
+ // y² = x³ + 240i * x + 1012 + 1012i
665
+ A: Fp2.create({ c0: Fp.create(_0n), c1: Fp.create(BigInt(240)) }), // A' = 240 * I
666
+ B: Fp2.create({ c0: Fp.create(BigInt(1012)), c1: Fp.create(BigInt(1012)) }), // B' = 1012 * (1 + I)
667
+ Z: Fp2.create({ c0: Fp.create(BigInt(-2)), c1: Fp.create(BigInt(-1)) }), // Z: -(2 + I)
668
+ }));
669
+ // Internal hash-to-curve step: G1 uses `m = 1`, so only `scalars[0]` is read,
670
+ // and the result is the isogeny image on E before the subgroup clear.
671
+ function mapToG1(scalars) {
672
+ const { x, y } = getG1_SWU()(Fp.create(scalars[0]));
673
+ return isogenyMapG1(x, y);
674
+ }
675
+ // Internal hash-to-curve step: G2 expects the RFC `m = 2` pair, and the result
676
+ // is the isogeny image on E before the subgroup clear.
677
+ function mapToG2(scalars) {
678
+ const { x, y } = getG2_SWU()(Fp2.fromBigTuple(scalars));
679
+ return isogenyMapG2(x, y);
680
+ }