@noble/curves 0.4.0 → 0.5.1

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 (75) hide show
  1. package/README.md +254 -162
  2. package/lib/_shortw_utils.d.ts +75 -0
  3. package/lib/_shortw_utils.js +20 -0
  4. package/lib/{bls.d.ts → abstract/bls.d.ts} +2 -1
  5. package/lib/{bls.js → abstract/bls.js} +28 -27
  6. package/lib/{edwards.d.ts → abstract/edwards.d.ts} +17 -0
  7. package/lib/{edwards.js → abstract/edwards.js} +45 -4
  8. package/lib/{group.d.ts → abstract/group.d.ts} +2 -1
  9. package/lib/{group.js → abstract/group.js} +4 -3
  10. package/lib/abstract/hash-to-curve.d.ts +28 -0
  11. package/lib/{hashToCurve.js → abstract/hash-to-curve.js} +46 -11
  12. package/lib/{modular.d.ts → abstract/modular.d.ts} +13 -16
  13. package/lib/abstract/modular.js +337 -0
  14. package/lib/{montgomery.d.ts → abstract/montgomery.d.ts} +2 -1
  15. package/lib/{montgomery.js → abstract/montgomery.js} +17 -8
  16. package/lib/{utils.d.ts → abstract/utils.d.ts} +4 -2
  17. package/lib/{utils.js → abstract/utils.js} +1 -1
  18. package/lib/{weierstrass.d.ts → abstract/weierstrass.d.ts} +28 -16
  19. package/lib/{weierstrass.js → abstract/weierstrass.js} +261 -127
  20. package/lib/bls12-381.d.ts +66 -0
  21. package/lib/bls12-381.js +1132 -0
  22. package/lib/bn.d.ts +7 -0
  23. package/lib/bn.js +24 -0
  24. package/lib/ed25519.d.ts +48 -0
  25. package/lib/ed25519.js +385 -0
  26. package/lib/ed448.d.ts +3 -0
  27. package/lib/ed448.js +211 -0
  28. package/lib/esm/_shortw_utils.js +15 -0
  29. package/lib/esm/{bls.js → abstract/bls.js} +25 -24
  30. package/lib/esm/{edwards.js → abstract/edwards.js} +45 -4
  31. package/lib/esm/{group.js → abstract/group.js} +4 -3
  32. package/lib/esm/{hashToCurve.js → abstract/hash-to-curve.js} +43 -10
  33. package/lib/esm/abstract/modular.js +319 -0
  34. package/lib/esm/{montgomery.js → abstract/montgomery.js} +17 -8
  35. package/lib/esm/{utils.js → abstract/utils.js} +1 -1
  36. package/lib/esm/{weierstrass.js → abstract/weierstrass.js} +255 -123
  37. package/lib/esm/bls12-381.js +1129 -0
  38. package/lib/esm/bn.js +21 -0
  39. package/lib/esm/ed25519.js +381 -0
  40. package/lib/esm/ed448.js +208 -0
  41. package/lib/esm/index.js +2 -0
  42. package/lib/esm/jubjub.js +52 -0
  43. package/lib/esm/p192.js +21 -0
  44. package/lib/esm/p224.js +21 -0
  45. package/lib/esm/p256.js +39 -0
  46. package/lib/esm/p384.js +44 -0
  47. package/lib/esm/p521.js +58 -0
  48. package/lib/esm/pasta.js +29 -0
  49. package/lib/esm/secp256k1.js +293 -0
  50. package/lib/esm/stark.js +224 -0
  51. package/lib/index.d.ts +0 -0
  52. package/lib/index.js +2 -0
  53. package/lib/jubjub.d.ts +7 -0
  54. package/lib/jubjub.js +57 -0
  55. package/lib/p192.d.ts +130 -0
  56. package/lib/p192.js +24 -0
  57. package/lib/p224.d.ts +130 -0
  58. package/lib/p224.js +24 -0
  59. package/lib/p256.d.ts +130 -0
  60. package/lib/p256.js +42 -0
  61. package/lib/p384.d.ts +130 -0
  62. package/lib/p384.js +47 -0
  63. package/lib/p521.d.ts +131 -0
  64. package/lib/p521.js +61 -0
  65. package/lib/pasta.d.ts +4 -0
  66. package/lib/pasta.js +32 -0
  67. package/lib/secp256k1.d.ts +96 -0
  68. package/lib/secp256k1.js +297 -0
  69. package/lib/stark.d.ts +72 -0
  70. package/lib/stark.js +245 -0
  71. package/package.json +146 -50
  72. package/index.js +0 -1
  73. package/lib/esm/modular.js +0 -252
  74. package/lib/hashToCurve.d.ts +0 -13
  75. package/lib/modular.js +0 -267
@@ -0,0 +1,1132 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bls12_381 = void 0;
4
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
5
+ const sha256_1 = require("@noble/hashes/sha256");
6
+ const utils_1 = require("@noble/hashes/utils");
7
+ const bls_js_1 = require("./abstract/bls.js");
8
+ const mod = require("./abstract/modular.js");
9
+ const utils_js_1 = require("./abstract/utils.js");
10
+ // Types
11
+ const weierstrass_js_1 = require("./abstract/weierstrass.js");
12
+ const hash_to_curve_js_1 = require("./abstract/hash-to-curve.js");
13
+ // Differences from bls12-381:
14
+ // - PointG1 -> G1.Point
15
+ // - PointG2 -> G2.Point
16
+ // - PointG2.fromSignature -> Signature.decode
17
+ // - PointG2.toSignature -> Signature.encode
18
+ // - Fixed Fp2 ORDER
19
+ // Points now have only two coordinates
20
+ // CURVE FIELDS
21
+ // Finite field over p.
22
+ const Fp = mod.Fp(0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn);
23
+ // Finite field over r.
24
+ // This particular field is not used anywhere in bls12-381, but it is still useful.
25
+ const Fr = mod.Fp(0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001n);
26
+ const Fp2Add = ({ c0, c1 }, { c0: r0, c1: r1 }) => ({
27
+ c0: Fp.add(c0, r0),
28
+ c1: Fp.add(c1, r1),
29
+ });
30
+ const Fp2Subtract = ({ c0, c1 }, { c0: r0, c1: r1 }) => ({
31
+ c0: Fp.sub(c0, r0),
32
+ c1: Fp.sub(c1, r1),
33
+ });
34
+ const Fp2Multiply = ({ c0, c1 }, rhs) => {
35
+ if (typeof rhs === 'bigint')
36
+ return { c0: Fp.mul(c0, rhs), c1: Fp.mul(c1, rhs) };
37
+ // (a+bi)(c+di) = (ac−bd) + (ad+bc)i
38
+ const { c0: r0, c1: r1 } = rhs;
39
+ let t1 = Fp.mul(c0, r0); // c0 * o0
40
+ let t2 = Fp.mul(c1, r1); // c1 * o1
41
+ // (T1 - T2) + ((c0 + c1) * (r0 + r1) - (T1 + T2))*i
42
+ const o0 = Fp.sub(t1, t2);
43
+ const o1 = Fp.sub(Fp.mul(Fp.add(c0, c1), Fp.add(r0, r1)), Fp.add(t1, t2));
44
+ return { c0: o0, c1: o1 };
45
+ };
46
+ const Fp2Square = ({ c0, c1 }) => {
47
+ const a = Fp.add(c0, c1);
48
+ const b = Fp.sub(c0, c1);
49
+ const c = Fp.add(c0, c0);
50
+ return { c0: Fp.mul(a, b), c1: Fp.mul(c, c1) };
51
+ };
52
+ // G2 is the order-q subgroup of E2(Fp²) : y² = x³+4(1+√−1),
53
+ // where Fp2 is Fp[√−1]/(x2+1). #E2(Fp2 ) = h2q, where
54
+ // G² - 1
55
+ // h2q
56
+ // NOTE: ORDER was wrong!
57
+ const FP2_ORDER = 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn **
58
+ 2n;
59
+ const Fp2 = {
60
+ ORDER: FP2_ORDER,
61
+ BITS: (0, utils_js_1.bitLen)(FP2_ORDER),
62
+ BYTES: Math.ceil((0, utils_js_1.bitLen)(FP2_ORDER) / 8),
63
+ MASK: (0, utils_js_1.bitMask)((0, utils_js_1.bitLen)(FP2_ORDER)),
64
+ ZERO: { c0: Fp.ZERO, c1: Fp.ZERO },
65
+ ONE: { c0: Fp.ONE, c1: Fp.ZERO },
66
+ create: (num) => num,
67
+ isValid: ({ c0, c1 }) => typeof c0 === 'bigint' && typeof c1 === 'bigint',
68
+ isZero: ({ c0, c1 }) => Fp.isZero(c0) && Fp.isZero(c1),
69
+ equals: ({ c0, c1 }, { c0: r0, c1: r1 }) => Fp.equals(c0, r0) && Fp.equals(c1, r1),
70
+ negate: ({ c0, c1 }) => ({ c0: Fp.negate(c0), c1: Fp.negate(c1) }),
71
+ pow: (num, power) => mod.FpPow(Fp2, num, power),
72
+ invertBatch: (nums) => mod.FpInvertBatch(Fp2, nums),
73
+ // Normalized
74
+ add: Fp2Add,
75
+ sub: Fp2Subtract,
76
+ mul: Fp2Multiply,
77
+ square: Fp2Square,
78
+ // NonNormalized stuff
79
+ addN: Fp2Add,
80
+ subN: Fp2Subtract,
81
+ mulN: Fp2Multiply,
82
+ squareN: Fp2Square,
83
+ // Why inversion for bigint inside Fp instead of Fp2? it is even used in that context?
84
+ div: (lhs, rhs) => Fp2.mul(lhs, typeof rhs === 'bigint' ? Fp.invert(Fp.create(rhs)) : Fp2.invert(rhs)),
85
+ invert: ({ c0: a, c1: b }) => {
86
+ // We wish to find the multiplicative inverse of a nonzero
87
+ // element a + bu in Fp2. We leverage an identity
88
+ //
89
+ // (a + bu)(a - bu) = a² + b²
90
+ //
91
+ // which holds because u² = -1. This can be rewritten as
92
+ //
93
+ // (a + bu)(a - bu)/(a² + b²) = 1
94
+ //
95
+ // because a² + b² = 0 has no nonzero solutions for (a, b).
96
+ // This gives that (a - bu)/(a² + b²) is the inverse
97
+ // of (a + bu). Importantly, this can be computing using
98
+ // only a single inversion in Fp.
99
+ const factor = Fp.invert(Fp.create(a * a + b * b));
100
+ return { c0: Fp.mul(factor, Fp.create(a)), c1: Fp.mul(factor, Fp.create(-b)) };
101
+ },
102
+ sqrt: (num) => {
103
+ // TODO: Optimize this line. It's extremely slow.
104
+ // Speeding this up would boost aggregateSignatures.
105
+ // https://eprint.iacr.org/2012/685.pdf applicable?
106
+ // https://github.com/zkcrypto/bls12_381/blob/080eaa74ec0e394377caa1ba302c8c121df08b07/src/fp2.rs#L250
107
+ // https://github.com/supranational/blst/blob/aae0c7d70b799ac269ff5edf29d8191dbd357876/src/exp2.c#L1
108
+ // Inspired by https://github.com/dalek-cryptography/curve25519-dalek/blob/17698df9d4c834204f83a3574143abacb4fc81a5/src/field.rs#L99
109
+ const candidateSqrt = Fp2.pow(num, (Fp2.ORDER + 8n) / 16n);
110
+ const check = Fp2.div(Fp2.square(candidateSqrt), num); // candidateSqrt.square().div(this);
111
+ const R = FP2_ROOTS_OF_UNITY;
112
+ const divisor = [R[0], R[2], R[4], R[6]].find((r) => Fp2.equals(r, check));
113
+ if (!divisor)
114
+ throw new Error('No root');
115
+ const index = R.indexOf(divisor);
116
+ const root = R[index / 2];
117
+ if (!root)
118
+ throw new Error('Invalid root');
119
+ const x1 = Fp2.div(candidateSqrt, root);
120
+ const x2 = Fp2.negate(x1);
121
+ const { re: re1, im: im1 } = Fp2.reim(x1);
122
+ const { re: re2, im: im2 } = Fp2.reim(x2);
123
+ if (im1 > im2 || (im1 === im2 && re1 > re2))
124
+ return x1;
125
+ return x2;
126
+ },
127
+ // Same as sgn0_fp2 in draft-irtf-cfrg-hash-to-curve-16
128
+ isOdd: (x) => {
129
+ const { re: x0, im: x1 } = Fp2.reim(x);
130
+ const sign_0 = x0 % 2n;
131
+ const zero_0 = x0 === 0n;
132
+ const sign_1 = x1 % 2n;
133
+ return BigInt(sign_0 || (zero_0 && sign_1)) == 1n;
134
+ },
135
+ // Bytes util
136
+ fromBytes(b) {
137
+ if (b.length !== Fp2.BYTES)
138
+ throw new Error(`fromBytes wrong length=${b.length}`);
139
+ return { c0: Fp.fromBytes(b.subarray(0, Fp.BYTES)), c1: Fp.fromBytes(b.subarray(Fp.BYTES)) };
140
+ },
141
+ toBytes: ({ c0, c1 }) => (0, utils_js_1.concatBytes)(Fp.toBytes(c0), Fp.toBytes(c1)),
142
+ cmov: ({ c0, c1 }, { c0: r0, c1: r1 }, c) => ({
143
+ c0: Fp.cmov(c0, r0, c),
144
+ c1: Fp.cmov(c1, r1, c),
145
+ }),
146
+ // Specific utils
147
+ // toString() {
148
+ // return `Fp2(${this.c0} + ${this.c1}×i)`;
149
+ // }
150
+ reim: ({ c0, c1 }) => ({ re: c0, im: c1 }),
151
+ // multiply by u + 1
152
+ mulByNonresidue: ({ c0, c1 }) => ({ c0: Fp.sub(c0, c1), c1: Fp.add(c0, c1) }),
153
+ multiplyByB: ({ c0, c1 }) => {
154
+ let t0 = Fp.mul(c0, 4n); // 4 * c0
155
+ let t1 = Fp.mul(c1, 4n); // 4 * c1
156
+ // (T0-T1) + (T0+T1)*i
157
+ return { c0: Fp.sub(t0, t1), c1: Fp.add(t0, t1) };
158
+ },
159
+ fromBigTuple: (tuple) => {
160
+ if (tuple.length !== 2)
161
+ throw new Error('Invalid tuple');
162
+ const fps = tuple.map((n) => Fp.create(n));
163
+ return { c0: fps[0], c1: fps[1] };
164
+ },
165
+ frobeniusMap: ({ c0, c1 }, power) => ({
166
+ c0,
167
+ c1: Fp.mul(c1, FP2_FROBENIUS_COEFFICIENTS[power % 2]),
168
+ }),
169
+ };
170
+ // Finite extension field over irreducible polynominal.
171
+ // Fp(u) / (u² - β) where β = -1
172
+ const FP2_FROBENIUS_COEFFICIENTS = [
173
+ 0x1n,
174
+ 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaan,
175
+ ].map((item) => Fp.create(item));
176
+ // For Fp2 roots of unity.
177
+ const rv1 = 0x6af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09n;
178
+ // const ev1 =
179
+ // 0x699be3b8c6870965e5bf892ad5d2cc7b0e85a117402dfd83b7f4a947e02d978498255a2aaec0ac627b5afbdf1bf1c90n;
180
+ // const ev2 =
181
+ // 0x8157cd83046453f5dd0972b6e3949e4288020b5b8a9cc99ca07e27089a2ce2436d965026adad3ef7baba37f2183e9b5n;
182
+ // const ev3 =
183
+ // 0xab1c2ffdd6c253ca155231eb3e71ba044fd562f6f72bc5bad5ec46a0b7a3b0247cf08ce6c6317f40edbc653a72dee17n;
184
+ // const ev4 =
185
+ // 0xaa404866706722864480885d68ad0ccac1967c7544b447873cc37e0181271e006df72162a3d3e0287bf597fbf7f8fc1n;
186
+ // Eighth roots of unity, used for computing square roots in Fp2.
187
+ // To verify or re-calculate:
188
+ // Array(8).fill(new Fp2([1n, 1n])).map((fp2, k) => fp2.pow(Fp2.ORDER * BigInt(k) / 8n))
189
+ const FP2_ROOTS_OF_UNITY = [
190
+ [1n, 0n],
191
+ [rv1, -rv1],
192
+ [0n, 1n],
193
+ [rv1, rv1],
194
+ [-1n, 0n],
195
+ [-rv1, rv1],
196
+ [0n, -1n],
197
+ [-rv1, -rv1],
198
+ ].map((pair) => Fp2.fromBigTuple(pair));
199
+ const Fp6Add = ({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }) => ({
200
+ c0: Fp2.add(c0, r0),
201
+ c1: Fp2.add(c1, r1),
202
+ c2: Fp2.add(c2, r2),
203
+ });
204
+ const Fp6Subtract = ({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }) => ({
205
+ c0: Fp2.sub(c0, r0),
206
+ c1: Fp2.sub(c1, r1),
207
+ c2: Fp2.sub(c2, r2),
208
+ });
209
+ const Fp6Multiply = ({ c0, c1, c2 }, rhs) => {
210
+ if (typeof rhs === 'bigint') {
211
+ return {
212
+ c0: Fp2.mul(c0, rhs),
213
+ c1: Fp2.mul(c1, rhs),
214
+ c2: Fp2.mul(c2, rhs),
215
+ };
216
+ }
217
+ const { c0: r0, c1: r1, c2: r2 } = rhs;
218
+ const t0 = Fp2.mul(c0, r0); // c0 * o0
219
+ const t1 = Fp2.mul(c1, r1); // c1 * o1
220
+ const t2 = Fp2.mul(c2, r2); // c2 * o2
221
+ return {
222
+ // t0 + (c1 + c2) * (r1 * r2) - (T1 + T2) * (u + 1)
223
+ c0: Fp2.add(t0, Fp2.mulByNonresidue(Fp2.sub(Fp2.mul(Fp2.add(c1, c2), Fp2.add(r1, r2)), Fp2.add(t1, t2)))),
224
+ // (c0 + c1) * (r0 + r1) - (T0 + T1) + T2 * (u + 1)
225
+ c1: Fp2.add(Fp2.sub(Fp2.mul(Fp2.add(c0, c1), Fp2.add(r0, r1)), Fp2.add(t0, t1)), Fp2.mulByNonresidue(t2)),
226
+ // T1 + (c0 + c2) * (r0 + r2) - T0 + T2
227
+ c2: Fp2.sub(Fp2.add(t1, Fp2.mul(Fp2.add(c0, c2), Fp2.add(r0, r2))), Fp2.add(t0, t2)),
228
+ };
229
+ };
230
+ const Fp6Square = ({ c0, c1, c2 }) => {
231
+ let t0 = Fp2.square(c0); // c0²
232
+ let t1 = Fp2.mul(Fp2.mul(c0, c1), 2n); // 2 * c0 * c1
233
+ let t3 = Fp2.mul(Fp2.mul(c1, c2), 2n); // 2 * c1 * c2
234
+ let t4 = Fp2.square(c2); // c2²
235
+ return {
236
+ c0: Fp2.add(Fp2.mulByNonresidue(t3), t0),
237
+ c1: Fp2.add(Fp2.mulByNonresidue(t4), t1),
238
+ // T1 + (c0 - c1 + c2)² + T3 - T0 - T4
239
+ c2: Fp2.sub(Fp2.sub(Fp2.add(Fp2.add(t1, Fp2.square(Fp2.add(Fp2.sub(c0, c1), c2))), t3), t0), t4),
240
+ };
241
+ };
242
+ const Fp6 = {
243
+ ORDER: Fp2.ORDER,
244
+ BITS: 3 * Fp2.BITS,
245
+ BYTES: 3 * Fp2.BYTES,
246
+ MASK: (0, utils_js_1.bitMask)(3 * Fp2.BITS),
247
+ ZERO: { c0: Fp2.ZERO, c1: Fp2.ZERO, c2: Fp2.ZERO },
248
+ ONE: { c0: Fp2.ONE, c1: Fp2.ZERO, c2: Fp2.ZERO },
249
+ create: (num) => num,
250
+ isValid: ({ c0, c1, c2 }) => Fp2.isValid(c0) && Fp2.isValid(c1) && Fp2.isValid(c2),
251
+ isZero: ({ c0, c1, c2 }) => Fp2.isZero(c0) && Fp2.isZero(c1) && Fp2.isZero(c2),
252
+ negate: ({ c0, c1, c2 }) => ({ c0: Fp2.negate(c0), c1: Fp2.negate(c1), c2: Fp2.negate(c2) }),
253
+ equals: ({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }) => Fp2.equals(c0, r0) && Fp2.equals(c1, r1) && Fp2.equals(c2, r2),
254
+ sqrt: () => {
255
+ throw new Error('Not implemented');
256
+ },
257
+ // Do we need division by bigint at all? Should be done via order:
258
+ div: (lhs, rhs) => Fp6.mul(lhs, typeof rhs === 'bigint' ? Fp.invert(Fp.create(rhs)) : Fp6.invert(rhs)),
259
+ pow: (num, power) => mod.FpPow(Fp6, num, power),
260
+ invertBatch: (nums) => mod.FpInvertBatch(Fp6, nums),
261
+ // Normalized
262
+ add: Fp6Add,
263
+ sub: Fp6Subtract,
264
+ mul: Fp6Multiply,
265
+ square: Fp6Square,
266
+ // NonNormalized stuff
267
+ addN: Fp6Add,
268
+ subN: Fp6Subtract,
269
+ mulN: Fp6Multiply,
270
+ squareN: Fp6Square,
271
+ invert: ({ c0, c1, c2 }) => {
272
+ let t0 = Fp2.sub(Fp2.square(c0), Fp2.mulByNonresidue(Fp2.mul(c2, c1))); // c0² - c2 * c1 * (u + 1)
273
+ let t1 = Fp2.sub(Fp2.mulByNonresidue(Fp2.square(c2)), Fp2.mul(c0, c1)); // c2² * (u + 1) - c0 * c1
274
+ let t2 = Fp2.sub(Fp2.square(c1), Fp2.mul(c0, c2)); // c1² - c0 * c2
275
+ // 1/(((c2 * T1 + c1 * T2) * v) + c0 * T0)
276
+ let t4 = Fp2.invert(Fp2.add(Fp2.mulByNonresidue(Fp2.add(Fp2.mul(c2, t1), Fp2.mul(c1, t2))), Fp2.mul(c0, t0)));
277
+ return { c0: Fp2.mul(t4, t0), c1: Fp2.mul(t4, t1), c2: Fp2.mul(t4, t2) };
278
+ },
279
+ // Bytes utils
280
+ fromBytes: (b) => {
281
+ if (b.length !== Fp6.BYTES)
282
+ throw new Error(`fromBytes wrong length=${b.length}`);
283
+ return {
284
+ c0: Fp2.fromBytes(b.subarray(0, Fp2.BYTES)),
285
+ c1: Fp2.fromBytes(b.subarray(Fp2.BYTES, 2 * Fp2.BYTES)),
286
+ c2: Fp2.fromBytes(b.subarray(2 * Fp2.BYTES)),
287
+ };
288
+ },
289
+ toBytes: ({ c0, c1, c2 }) => (0, utils_js_1.concatBytes)(Fp2.toBytes(c0), Fp2.toBytes(c1), Fp2.toBytes(c2)),
290
+ cmov: ({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }, c) => ({
291
+ c0: Fp2.cmov(c0, r0, c),
292
+ c1: Fp2.cmov(c1, r1, c),
293
+ c2: Fp2.cmov(c2, r2, c),
294
+ }),
295
+ // Utils
296
+ // fromTriple(triple: [Fp2, Fp2, Fp2]) {
297
+ // return new Fp6(...triple);
298
+ // }
299
+ // toString() {
300
+ // return `Fp6(${this.c0} + ${this.c1} * v, ${this.c2} * v^2)`;
301
+ // }
302
+ fromBigSix: (t) => {
303
+ if (!Array.isArray(t) || t.length !== 6)
304
+ throw new Error('Invalid Fp6 usage');
305
+ return {
306
+ c0: Fp2.fromBigTuple(t.slice(0, 2)),
307
+ c1: Fp2.fromBigTuple(t.slice(2, 4)),
308
+ c2: Fp2.fromBigTuple(t.slice(4, 6)),
309
+ };
310
+ },
311
+ frobeniusMap: ({ c0, c1, c2 }, power) => ({
312
+ c0: Fp2.frobeniusMap(c0, power),
313
+ c1: Fp2.mul(Fp2.frobeniusMap(c1, power), FP6_FROBENIUS_COEFFICIENTS_1[power % 6]),
314
+ c2: Fp2.mul(Fp2.frobeniusMap(c2, power), FP6_FROBENIUS_COEFFICIENTS_2[power % 6]),
315
+ }),
316
+ mulByNonresidue: ({ c0, c1, c2 }) => ({ c0: Fp2.mulByNonresidue(c2), c1: c0, c2: c1 }),
317
+ // Sparse multiplication
318
+ multiplyBy1: ({ c0, c1, c2 }, b1) => ({
319
+ c0: Fp2.mulByNonresidue(Fp2.mul(c2, b1)),
320
+ c1: Fp2.mul(c0, b1),
321
+ c2: Fp2.mul(c1, b1),
322
+ }),
323
+ // Sparse multiplication
324
+ multiplyBy01({ c0, c1, c2 }, b0, b1) {
325
+ let t0 = Fp2.mul(c0, b0); // c0 * b0
326
+ let t1 = Fp2.mul(c1, b1); // c1 * b1
327
+ return {
328
+ // ((c1 + c2) * b1 - T1) * (u + 1) + T0
329
+ c0: Fp2.add(Fp2.mulByNonresidue(Fp2.sub(Fp2.mul(Fp2.add(c1, c2), b1), t1)), t0),
330
+ // (b0 + b1) * (c0 + c1) - T0 - T1
331
+ c1: Fp2.sub(Fp2.sub(Fp2.mul(Fp2.add(b0, b1), Fp2.add(c0, c1)), t0), t1),
332
+ // (c0 + c2) * b0 - T0 + T1
333
+ c2: Fp2.add(Fp2.sub(Fp2.mul(Fp2.add(c0, c2), b0), t0), t1),
334
+ };
335
+ },
336
+ multiplyByFp2: ({ c0, c1, c2 }, rhs) => ({
337
+ c0: Fp2.mul(c0, rhs),
338
+ c1: Fp2.mul(c1, rhs),
339
+ c2: Fp2.mul(c2, rhs),
340
+ }),
341
+ };
342
+ const FP6_FROBENIUS_COEFFICIENTS_1 = [
343
+ [0x1n, 0x0n],
344
+ [
345
+ 0x0n,
346
+ 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn,
347
+ ],
348
+ [
349
+ 0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen,
350
+ 0x0n,
351
+ ],
352
+ [0x0n, 0x1n],
353
+ [
354
+ 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn,
355
+ 0x0n,
356
+ ],
357
+ [
358
+ 0x0n,
359
+ 0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen,
360
+ ],
361
+ ].map((pair) => Fp2.fromBigTuple(pair));
362
+ const FP6_FROBENIUS_COEFFICIENTS_2 = [
363
+ [0x1n, 0x0n],
364
+ [
365
+ 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaadn,
366
+ 0x0n,
367
+ ],
368
+ [
369
+ 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn,
370
+ 0x0n,
371
+ ],
372
+ [
373
+ 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaan,
374
+ 0x0n,
375
+ ],
376
+ [
377
+ 0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen,
378
+ 0x0n,
379
+ ],
380
+ [
381
+ 0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffffn,
382
+ 0x0n,
383
+ ],
384
+ ].map((pair) => Fp2.fromBigTuple(pair));
385
+ // The BLS parameter x for BLS12-381
386
+ const BLS_X = 0xd201000000010000n;
387
+ const BLS_X_LEN = (0, utils_js_1.bitLen)(BLS_X);
388
+ const Fp12Add = ({ c0, c1 }, { c0: r0, c1: r1 }) => ({
389
+ c0: Fp6.add(c0, r0),
390
+ c1: Fp6.add(c1, r1),
391
+ });
392
+ const Fp12Subtract = ({ c0, c1 }, { c0: r0, c1: r1 }) => ({
393
+ c0: Fp6.sub(c0, r0),
394
+ c1: Fp6.sub(c1, r1),
395
+ });
396
+ const Fp12Multiply = ({ c0, c1 }, rhs) => {
397
+ if (typeof rhs === 'bigint')
398
+ return { c0: Fp6.mul(c0, rhs), c1: Fp6.mul(c1, rhs) };
399
+ let { c0: r0, c1: r1 } = rhs;
400
+ let t1 = Fp6.mul(c0, r0); // c0 * r0
401
+ let t2 = Fp6.mul(c1, r1); // c1 * r1
402
+ return {
403
+ c0: Fp6.add(t1, Fp6.mulByNonresidue(t2)),
404
+ // (c0 + c1) * (r0 + r1) - (T1 + T2)
405
+ c1: Fp6.sub(Fp6.mul(Fp6.add(c0, c1), Fp6.add(r0, r1)), Fp6.add(t1, t2)),
406
+ };
407
+ };
408
+ const Fp12Square = ({ c0, c1 }) => {
409
+ let ab = Fp6.mul(c0, c1); // c0 * c1
410
+ return {
411
+ // (c1 * v + c0) * (c0 + c1) - AB - AB * v
412
+ c0: Fp6.sub(Fp6.sub(Fp6.mul(Fp6.add(Fp6.mulByNonresidue(c1), c0), Fp6.add(c0, c1)), ab), Fp6.mulByNonresidue(ab)),
413
+ c1: Fp6.add(ab, ab),
414
+ }; // AB + AB
415
+ };
416
+ function Fp4Square(a, b) {
417
+ const a2 = Fp2.square(a);
418
+ const b2 = Fp2.square(b);
419
+ return {
420
+ first: Fp2.add(Fp2.mulByNonresidue(b2), a2),
421
+ second: Fp2.sub(Fp2.sub(Fp2.square(Fp2.add(a, b)), a2), b2), // (a + b)² - a² - b²
422
+ };
423
+ }
424
+ const Fp12 = {
425
+ ORDER: Fp2.ORDER,
426
+ BITS: 2 * Fp2.BITS,
427
+ BYTES: 2 * Fp2.BYTES,
428
+ MASK: (0, utils_js_1.bitMask)(2 * Fp2.BITS),
429
+ ZERO: { c0: Fp6.ZERO, c1: Fp6.ZERO },
430
+ ONE: { c0: Fp6.ONE, c1: Fp6.ZERO },
431
+ create: (num) => num,
432
+ isValid: ({ c0, c1 }) => Fp6.isValid(c0) && Fp6.isValid(c1),
433
+ isZero: ({ c0, c1 }) => Fp6.isZero(c0) && Fp6.isZero(c1),
434
+ negate: ({ c0, c1 }) => ({ c0: Fp6.negate(c0), c1: Fp6.negate(c1) }),
435
+ equals: ({ c0, c1 }, { c0: r0, c1: r1 }) => Fp6.equals(c0, r0) && Fp6.equals(c1, r1),
436
+ sqrt: () => {
437
+ throw new Error('Not implemented');
438
+ },
439
+ invert: ({ c0, c1 }) => {
440
+ let t = Fp6.invert(Fp6.sub(Fp6.square(c0), Fp6.mulByNonresidue(Fp6.square(c1)))); // 1 / (c0² - c1² * v)
441
+ return { c0: Fp6.mul(c0, t), c1: Fp6.negate(Fp6.mul(c1, t)) }; // ((C0 * T) * T) + (-C1 * T) * w
442
+ },
443
+ div: (lhs, rhs) => Fp12.mul(lhs, typeof rhs === 'bigint' ? Fp.invert(Fp.create(rhs)) : Fp12.invert(rhs)),
444
+ pow: (num, power) => mod.FpPow(Fp12, num, power),
445
+ invertBatch: (nums) => mod.FpInvertBatch(Fp12, nums),
446
+ // Normalized
447
+ add: Fp12Add,
448
+ sub: Fp12Subtract,
449
+ mul: Fp12Multiply,
450
+ square: Fp12Square,
451
+ // NonNormalized stuff
452
+ addN: Fp12Add,
453
+ subN: Fp12Subtract,
454
+ mulN: Fp12Multiply,
455
+ squareN: Fp12Square,
456
+ // Bytes utils
457
+ fromBytes: (b) => {
458
+ if (b.length !== Fp12.BYTES)
459
+ throw new Error(`fromBytes wrong length=${b.length}`);
460
+ return {
461
+ c0: Fp6.fromBytes(b.subarray(0, Fp6.BYTES)),
462
+ c1: Fp6.fromBytes(b.subarray(Fp6.BYTES)),
463
+ };
464
+ },
465
+ toBytes: ({ c0, c1 }) => (0, utils_js_1.concatBytes)(Fp6.toBytes(c0), Fp6.toBytes(c1)),
466
+ cmov: ({ c0, c1 }, { c0: r0, c1: r1 }, c) => ({
467
+ c0: Fp6.cmov(c0, r0, c),
468
+ c1: Fp6.cmov(c1, r1, c),
469
+ }),
470
+ // Utils
471
+ // toString() {
472
+ // return `Fp12(${this.c0} + ${this.c1} * w)`;
473
+ // },
474
+ // fromTuple(c: [Fp6, Fp6]) {
475
+ // return new Fp12(...c);
476
+ // }
477
+ fromBigTwelve: (t) => ({
478
+ c0: Fp6.fromBigSix(t.slice(0, 6)),
479
+ c1: Fp6.fromBigSix(t.slice(6, 12)),
480
+ }),
481
+ // Raises to q**i -th power
482
+ frobeniusMap(lhs, power) {
483
+ const r0 = Fp6.frobeniusMap(lhs.c0, power);
484
+ const { c0, c1, c2 } = Fp6.frobeniusMap(lhs.c1, power);
485
+ const coeff = FP12_FROBENIUS_COEFFICIENTS[power % 12];
486
+ return {
487
+ c0: r0,
488
+ c1: Fp6.create({
489
+ c0: Fp2.mul(c0, coeff),
490
+ c1: Fp2.mul(c1, coeff),
491
+ c2: Fp2.mul(c2, coeff),
492
+ }),
493
+ };
494
+ },
495
+ // Sparse multiplication
496
+ multiplyBy014: ({ c0, c1 }, o0, o1, o4) => {
497
+ let t0 = Fp6.multiplyBy01(c0, o0, o1);
498
+ let t1 = Fp6.multiplyBy1(c1, o4);
499
+ return {
500
+ c0: Fp6.add(Fp6.mulByNonresidue(t1), t0),
501
+ // (c1 + c0) * [o0, o1+o4] - T0 - T1
502
+ c1: Fp6.sub(Fp6.sub(Fp6.multiplyBy01(Fp6.add(c1, c0), o0, Fp2.add(o1, o4)), t0), t1),
503
+ };
504
+ },
505
+ multiplyByFp2: ({ c0, c1 }, rhs) => ({
506
+ c0: Fp6.multiplyByFp2(c0, rhs),
507
+ c1: Fp6.multiplyByFp2(c1, rhs),
508
+ }),
509
+ conjugate: ({ c0, c1 }) => ({ c0, c1: Fp6.negate(c1) }),
510
+ // A cyclotomic group is a subgroup of Fp^n defined by
511
+ // GΦₙ(p) = {α ∈ Fpⁿ : α^Φₙ(p) = 1}
512
+ // The result of any pairing is in a cyclotomic subgroup
513
+ // https://eprint.iacr.org/2009/565.pdf
514
+ _cyclotomicSquare: ({ c0, c1 }) => {
515
+ const { c0: c0c0, c1: c0c1, c2: c0c2 } = c0;
516
+ const { c0: c1c0, c1: c1c1, c2: c1c2 } = c1;
517
+ const { first: t3, second: t4 } = Fp4Square(c0c0, c1c1);
518
+ const { first: t5, second: t6 } = Fp4Square(c1c0, c0c2);
519
+ const { first: t7, second: t8 } = Fp4Square(c0c1, c1c2);
520
+ let t9 = Fp2.mulByNonresidue(t8); // T8 * (u + 1)
521
+ return {
522
+ c0: Fp6.create({
523
+ c0: Fp2.add(Fp2.mul(Fp2.sub(t3, c0c0), 2n), t3),
524
+ c1: Fp2.add(Fp2.mul(Fp2.sub(t5, c0c1), 2n), t5),
525
+ c2: Fp2.add(Fp2.mul(Fp2.sub(t7, c0c2), 2n), t7),
526
+ }),
527
+ c1: Fp6.create({
528
+ c0: Fp2.add(Fp2.mul(Fp2.add(t9, c1c0), 2n), t9),
529
+ c1: Fp2.add(Fp2.mul(Fp2.add(t4, c1c1), 2n), t4),
530
+ c2: Fp2.add(Fp2.mul(Fp2.add(t6, c1c2), 2n), t6),
531
+ }),
532
+ }; // 2 * (T6 + c1c2) + T6
533
+ },
534
+ _cyclotomicExp(num, n) {
535
+ let z = Fp12.ONE;
536
+ for (let i = BLS_X_LEN - 1; i >= 0; i--) {
537
+ z = Fp12._cyclotomicSquare(z);
538
+ if ((0, utils_js_1.bitGet)(n, i))
539
+ z = Fp12.mul(z, num);
540
+ }
541
+ return z;
542
+ },
543
+ // https://eprint.iacr.org/2010/354.pdf
544
+ // https://eprint.iacr.org/2009/565.pdf
545
+ finalExponentiate: (num) => {
546
+ const x = BLS_X;
547
+ // this^(q⁶) / this
548
+ const t0 = Fp12.div(Fp12.frobeniusMap(num, 6), num);
549
+ // t0^(q²) * t0
550
+ const t1 = Fp12.mul(Fp12.frobeniusMap(t0, 2), t0);
551
+ const t2 = Fp12.conjugate(Fp12._cyclotomicExp(t1, x));
552
+ const t3 = Fp12.mul(Fp12.conjugate(Fp12._cyclotomicSquare(t1)), t2);
553
+ const t4 = Fp12.conjugate(Fp12._cyclotomicExp(t3, x));
554
+ const t5 = Fp12.conjugate(Fp12._cyclotomicExp(t4, x));
555
+ const t6 = Fp12.mul(Fp12.conjugate(Fp12._cyclotomicExp(t5, x)), Fp12._cyclotomicSquare(t2));
556
+ const t7 = Fp12.conjugate(Fp12._cyclotomicExp(t6, x));
557
+ const t2_t5_pow_q2 = Fp12.frobeniusMap(Fp12.mul(t2, t5), 2);
558
+ const t4_t1_pow_q3 = Fp12.frobeniusMap(Fp12.mul(t4, t1), 3);
559
+ const t6_t1c_pow_q1 = Fp12.frobeniusMap(Fp12.mul(t6, Fp12.conjugate(t1)), 1);
560
+ const t7_t3c_t1 = Fp12.mul(Fp12.mul(t7, Fp12.conjugate(t3)), t1);
561
+ // (t2 * t5)^(q²) * (t4 * t1)^(q³) * (t6 * t1.conj)^(q^1) * t7 * t3.conj * t1
562
+ return Fp12.mul(Fp12.mul(Fp12.mul(t2_t5_pow_q2, t4_t1_pow_q3), t6_t1c_pow_q1), t7_t3c_t1);
563
+ },
564
+ };
565
+ const FP12_FROBENIUS_COEFFICIENTS = [
566
+ [0x1n, 0x0n],
567
+ [
568
+ 0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8n,
569
+ 0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3n,
570
+ ],
571
+ [
572
+ 0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffffn,
573
+ 0x0n,
574
+ ],
575
+ [
576
+ 0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2n,
577
+ 0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09n,
578
+ ],
579
+ [
580
+ 0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen,
581
+ 0x0n,
582
+ ],
583
+ [
584
+ 0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995n,
585
+ 0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116n,
586
+ ],
587
+ [
588
+ 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaan,
589
+ 0x0n,
590
+ ],
591
+ [
592
+ 0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3n,
593
+ 0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8n,
594
+ ],
595
+ [
596
+ 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn,
597
+ 0x0n,
598
+ ],
599
+ [
600
+ 0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09n,
601
+ 0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2n,
602
+ ],
603
+ [
604
+ 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaadn,
605
+ 0x0n,
606
+ ],
607
+ [
608
+ 0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116n,
609
+ 0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995n,
610
+ ],
611
+ ].map((n) => Fp2.fromBigTuple(n));
612
+ // END OF CURVE FIELDS
613
+ // HashToCurve
614
+ // 3-isogeny map from E' to E
615
+ // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#appendix-E.3
616
+ const isogenyMapG2 = (0, hash_to_curve_js_1.isogenyMap)(Fp2, [
617
+ // xNum
618
+ [
619
+ [
620
+ '0x5c759507e8e333ebb5b7a9a47d7ed8532c52d39fd3a042a88b58423c50ae15d5c2638e343d9c71c6238aaaaaaaa97d6',
621
+ '0x5c759507e8e333ebb5b7a9a47d7ed8532c52d39fd3a042a88b58423c50ae15d5c2638e343d9c71c6238aaaaaaaa97d6',
622
+ ],
623
+ [
624
+ '0x0',
625
+ '0x11560bf17baa99bc32126fced787c88f984f87adf7ae0c7f9a208c6b4f20a4181472aaa9cb8d555526a9ffffffffc71a',
626
+ ],
627
+ [
628
+ '0x11560bf17baa99bc32126fced787c88f984f87adf7ae0c7f9a208c6b4f20a4181472aaa9cb8d555526a9ffffffffc71e',
629
+ '0x8ab05f8bdd54cde190937e76bc3e447cc27c3d6fbd7063fcd104635a790520c0a395554e5c6aaaa9354ffffffffe38d',
630
+ ],
631
+ [
632
+ '0x171d6541fa38ccfaed6dea691f5fb614cb14b4e7f4e810aa22d6108f142b85757098e38d0f671c7188e2aaaaaaaa5ed1',
633
+ '0x0',
634
+ ],
635
+ ],
636
+ // xDen
637
+ [
638
+ [
639
+ '0x0',
640
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa63',
641
+ ],
642
+ [
643
+ '0xc',
644
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa9f',
645
+ ],
646
+ ['0x1', '0x0'], // LAST 1
647
+ ],
648
+ // yNum
649
+ [
650
+ [
651
+ '0x1530477c7ab4113b59a4c18b076d11930f7da5d4a07f649bf54439d87d27e500fc8c25ebf8c92f6812cfc71c71c6d706',
652
+ '0x1530477c7ab4113b59a4c18b076d11930f7da5d4a07f649bf54439d87d27e500fc8c25ebf8c92f6812cfc71c71c6d706',
653
+ ],
654
+ [
655
+ '0x0',
656
+ '0x5c759507e8e333ebb5b7a9a47d7ed8532c52d39fd3a042a88b58423c50ae15d5c2638e343d9c71c6238aaaaaaaa97be',
657
+ ],
658
+ [
659
+ '0x11560bf17baa99bc32126fced787c88f984f87adf7ae0c7f9a208c6b4f20a4181472aaa9cb8d555526a9ffffffffc71c',
660
+ '0x8ab05f8bdd54cde190937e76bc3e447cc27c3d6fbd7063fcd104635a790520c0a395554e5c6aaaa9354ffffffffe38f',
661
+ ],
662
+ [
663
+ '0x124c9ad43b6cf79bfbf7043de3811ad0761b0f37a1e26286b0e977c69aa274524e79097a56dc4bd9e1b371c71c718b10',
664
+ '0x0',
665
+ ],
666
+ ],
667
+ // yDen
668
+ [
669
+ [
670
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffa8fb',
671
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffa8fb',
672
+ ],
673
+ [
674
+ '0x0',
675
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffa9d3',
676
+ ],
677
+ [
678
+ '0x12',
679
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa99',
680
+ ],
681
+ ['0x1', '0x0'], // LAST 1
682
+ ],
683
+ ].map((i) => i.map((pair) => Fp2.fromBigTuple(pair.map(BigInt)))));
684
+ // 11-isogeny map from E' to E
685
+ const isogenyMapG1 = (0, hash_to_curve_js_1.isogenyMap)(Fp, [
686
+ // xNum
687
+ [
688
+ '0x11a05f2b1e833340b809101dd99815856b303e88a2d7005ff2627b56cdb4e2c85610c2d5f2e62d6eaeac1662734649b7',
689
+ '0x17294ed3e943ab2f0588bab22147a81c7c17e75b2f6a8417f565e33c70d1e86b4838f2a6f318c356e834eef1b3cb83bb',
690
+ '0xd54005db97678ec1d1048c5d10a9a1bce032473295983e56878e501ec68e25c958c3e3d2a09729fe0179f9dac9edcb0',
691
+ '0x1778e7166fcc6db74e0609d307e55412d7f5e4656a8dbf25f1b33289f1b330835336e25ce3107193c5b388641d9b6861',
692
+ '0xe99726a3199f4436642b4b3e4118e5499db995a1257fb3f086eeb65982fac18985a286f301e77c451154ce9ac8895d9',
693
+ '0x1630c3250d7313ff01d1201bf7a74ab5db3cb17dd952799b9ed3ab9097e68f90a0870d2dcae73d19cd13c1c66f652983',
694
+ '0xd6ed6553fe44d296a3726c38ae652bfb11586264f0f8ce19008e218f9c86b2a8da25128c1052ecaddd7f225a139ed84',
695
+ '0x17b81e7701abdbe2e8743884d1117e53356de5ab275b4db1a682c62ef0f2753339b7c8f8c8f475af9ccb5618e3f0c88e',
696
+ '0x80d3cf1f9a78fc47b90b33563be990dc43b756ce79f5574a2c596c928c5d1de4fa295f296b74e956d71986a8497e317',
697
+ '0x169b1f8e1bcfa7c42e0c37515d138f22dd2ecb803a0c5c99676314baf4bb1b7fa3190b2edc0327797f241067be390c9e',
698
+ '0x10321da079ce07e272d8ec09d2565b0dfa7dccdde6787f96d50af36003b14866f69b771f8c285decca67df3f1605fb7b',
699
+ '0x6e08c248e260e70bd1e962381edee3d31d79d7e22c837bc23c0bf1bc24c6b68c24b1b80b64d391fa9c8ba2e8ba2d229',
700
+ ],
701
+ // xDen
702
+ [
703
+ '0x8ca8d548cff19ae18b2e62f4bd3fa6f01d5ef4ba35b48ba9c9588617fc8ac62b558d681be343df8993cf9fa40d21b1c',
704
+ '0x12561a5deb559c4348b4711298e536367041e8ca0cf0800c0126c2588c48bf5713daa8846cb026e9e5c8276ec82b3bff',
705
+ '0xb2962fe57a3225e8137e629bff2991f6f89416f5a718cd1fca64e00b11aceacd6a3d0967c94fedcfcc239ba5cb83e19',
706
+ '0x3425581a58ae2fec83aafef7c40eb545b08243f16b1655154cca8abc28d6fd04976d5243eecf5c4130de8938dc62cd8',
707
+ '0x13a8e162022914a80a6f1d5f43e7a07dffdfc759a12062bb8d6b44e833b306da9bd29ba81f35781d539d395b3532a21e',
708
+ '0xe7355f8e4e667b955390f7f0506c6e9395735e9ce9cad4d0a43bcef24b8982f7400d24bc4228f11c02df9a29f6304a5',
709
+ '0x772caacf16936190f3e0c63e0596721570f5799af53a1894e2e073062aede9cea73b3538f0de06cec2574496ee84a3a',
710
+ '0x14a7ac2a9d64a8b230b3f5b074cf01996e7f63c21bca68a81996e1cdf9822c580fa5b9489d11e2d311f7d99bbdcc5a5e',
711
+ '0xa10ecf6ada54f825e920b3dafc7a3cce07f8d1d7161366b74100da67f39883503826692abba43704776ec3a79a1d641',
712
+ '0x95fc13ab9e92ad4476d6e3eb3a56680f682b4ee96f7d03776df533978f31c1593174e4b4b7865002d6384d168ecdd0a',
713
+ '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001', // LAST 1
714
+ ],
715
+ // yNum
716
+ [
717
+ '0x90d97c81ba24ee0259d1f094980dcfa11ad138e48a869522b52af6c956543d3cd0c7aee9b3ba3c2be9845719707bb33',
718
+ '0x134996a104ee5811d51036d776fb46831223e96c254f383d0f906343eb67ad34d6c56711962fa8bfe097e75a2e41c696',
719
+ '0xcc786baa966e66f4a384c86a3b49942552e2d658a31ce2c344be4b91400da7d26d521628b00523b8dfe240c72de1f6',
720
+ '0x1f86376e8981c217898751ad8746757d42aa7b90eeb791c09e4a3ec03251cf9de405aba9ec61deca6355c77b0e5f4cb',
721
+ '0x8cc03fdefe0ff135caf4fe2a21529c4195536fbe3ce50b879833fd221351adc2ee7f8dc099040a841b6daecf2e8fedb',
722
+ '0x16603fca40634b6a2211e11db8f0a6a074a7d0d4afadb7bd76505c3d3ad5544e203f6326c95a807299b23ab13633a5f0',
723
+ '0x4ab0b9bcfac1bbcb2c977d027796b3ce75bb8ca2be184cb5231413c4d634f3747a87ac2460f415ec961f8855fe9d6f2',
724
+ '0x987c8d5333ab86fde9926bd2ca6c674170a05bfe3bdd81ffd038da6c26c842642f64550fedfe935a15e4ca31870fb29',
725
+ '0x9fc4018bd96684be88c9e221e4da1bb8f3abd16679dc26c1e8b6e6a1f20cabe69d65201c78607a360370e577bdba587',
726
+ '0xe1bba7a1186bdb5223abde7ada14a23c42a0ca7915af6fe06985e7ed1e4d43b9b3f7055dd4eba6f2bafaaebca731c30',
727
+ '0x19713e47937cd1be0dfd0b8f1d43fb93cd2fcbcb6caf493fd1183e416389e61031bf3a5cce3fbafce813711ad011c132',
728
+ '0x18b46a908f36f6deb918c143fed2edcc523559b8aaf0c2462e6bfe7f911f643249d9cdf41b44d606ce07c8a4d0074d8e',
729
+ '0xb182cac101b9399d155096004f53f447aa7b12a3426b08ec02710e807b4633f06c851c1919211f20d4c04f00b971ef8',
730
+ '0x245a394ad1eca9b72fc00ae7be315dc757b3b080d4c158013e6632d3c40659cc6cf90ad1c232a6442d9d3f5db980133',
731
+ '0x5c129645e44cf1102a159f748c4a3fc5e673d81d7e86568d9ab0f5d396a7ce46ba1049b6579afb7866b1e715475224b',
732
+ '0x15e6be4e990f03ce4ea50b3b42df2eb5cb181d8f84965a3957add4fa95af01b2b665027efec01c7704b456be69c8b604',
733
+ ],
734
+ // yDen
735
+ [
736
+ '0x16112c4c3a9c98b252181140fad0eae9601a6de578980be6eec3232b5be72e7a07f3688ef60c206d01479253b03663c1',
737
+ '0x1962d75c2381201e1a0cbd6c43c348b885c84ff731c4d59ca4a10356f453e01f78a4260763529e3532f6102c2e49a03d',
738
+ '0x58df3306640da276faaae7d6e8eb15778c4855551ae7f310c35a5dd279cd2eca6757cd636f96f891e2538b53dbf67f2',
739
+ '0x16b7d288798e5395f20d23bf89edb4d1d115c5dbddbcd30e123da489e726af41727364f2c28297ada8d26d98445f5416',
740
+ '0xbe0e079545f43e4b00cc912f8228ddcc6d19c9f0f69bbb0542eda0fc9dec916a20b15dc0fd2ededda39142311a5001d',
741
+ '0x8d9e5297186db2d9fb266eaac783182b70152c65550d881c5ecd87b6f0f5a6449f38db9dfa9cce202c6477faaf9b7ac',
742
+ '0x166007c08a99db2fc3ba8734ace9824b5eecfdfa8d0cf8ef5dd365bc400a0051d5fa9c01a58b1fb93d1a1399126a775c',
743
+ '0x16a3ef08be3ea7ea03bcddfabba6ff6ee5a4375efa1f4fd7feb34fd206357132b920f5b00801dee460ee415a15812ed9',
744
+ '0x1866c8ed336c61231a1be54fd1d74cc4f9fb0ce4c6af5920abc5750c4bf39b4852cfe2f7bb9248836b233d9d55535d4a',
745
+ '0x167a55cda70a6e1cea820597d94a84903216f763e13d87bb5308592e7ea7d4fbc7385ea3d529b35e346ef48bb8913f55',
746
+ '0x4d2f259eea405bd48f010a01ad2911d9c6dd039bb61a6290e591b36e636a5c871a5c29f4f83060400f8b49cba8f6aa8',
747
+ '0xaccbb67481d033ff5852c1e48c50c477f94ff8aefce42d28c0f9a88cea7913516f968986f7ebbea9684b529e2561092',
748
+ '0xad6b9514c767fe3c3613144b45f1496543346d98adf02267d5ceef9a00d9b8693000763e3b90ac11e99b138573345cc',
749
+ '0x2660400eb2e4f3b628bdd0d53cd76f2bf565b94e72927c1cb748df27942480e420517bd8714cc80d1fadc1326ed06f7',
750
+ '0xe0fa1d816ddc03e6b24255e0d7819c171c40f65e273b853324efcd6356caa205ca2f570f13497804415473a1d634b8f',
751
+ '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001', // LAST 1
752
+ ],
753
+ ].map((i) => i.map((j) => BigInt(j))));
754
+ // SWU Map - Fp2 to G2': y² = x³ + 240i * x + 1012 + 1012i
755
+ const G2_SWU = (0, weierstrass_js_1.mapToCurveSimpleSWU)(Fp2, {
756
+ A: Fp2.create({ c0: Fp.create(0n), c1: Fp.create(240n) }),
757
+ B: Fp2.create({ c0: Fp.create(1012n), c1: Fp.create(1012n) }),
758
+ Z: Fp2.create({ c0: Fp.create(-2n), c1: Fp.create(-1n) }), // Z: -(2 + I)
759
+ });
760
+ // Optimized SWU Map - Fp to G1
761
+ const G1_SWU = (0, weierstrass_js_1.mapToCurveSimpleSWU)(Fp, {
762
+ A: Fp.create(0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1dn),
763
+ B: Fp.create(0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0n),
764
+ Z: Fp.create(11n),
765
+ });
766
+ // Endomorphisms (for fast cofactor clearing)
767
+ // Ψ(P) endomorphism
768
+ const ut_root = Fp6.create({ c0: Fp2.ZERO, c1: Fp2.ONE, c2: Fp2.ZERO });
769
+ const wsq = Fp12.create({ c0: ut_root, c1: Fp6.ZERO });
770
+ const wcu = Fp12.create({ c0: Fp6.ZERO, c1: ut_root });
771
+ const [wsq_inv, wcu_inv] = Fp12.invertBatch([wsq, wcu]);
772
+ function psi(x, y) {
773
+ // Untwist Fp2->Fp12 && frobenius(1) && twist back
774
+ const x2 = Fp12.mul(Fp12.frobeniusMap(Fp12.multiplyByFp2(wsq_inv, x), 1), wsq).c0.c0;
775
+ const y2 = Fp12.mul(Fp12.frobeniusMap(Fp12.multiplyByFp2(wcu_inv, y), 1), wcu).c0.c0;
776
+ return [x2, y2];
777
+ }
778
+ // Ψ endomorphism
779
+ function G2psi(c, P) {
780
+ const affine = P.toAffine();
781
+ const p = psi(affine.x, affine.y);
782
+ return new c(p[0], p[1], Fp2.ONE);
783
+ }
784
+ // Ψ²(P) endomorphism
785
+ // 1 / F2(2)^((p-1)/3) in GF(p²)
786
+ const PSI2_C1 = 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn;
787
+ function psi2(x, y) {
788
+ return [Fp2.mul(x, PSI2_C1), Fp2.negate(y)];
789
+ }
790
+ function G2psi2(c, P) {
791
+ const affine = P.toAffine();
792
+ const p = psi2(affine.x, affine.y);
793
+ return new c(p[0], p[1], Fp2.ONE);
794
+ }
795
+ // Default hash_to_field options are for hash to G2.
796
+ //
797
+ // Parameter definitions are in section 5.3 of the spec unless otherwise noted.
798
+ // Parameter values come from section 8.8.2 of the spec.
799
+ // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-8.8.2
800
+ //
801
+ // Base field F is GF(p^m)
802
+ // p = 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab
803
+ // m = 2 (or 1 for G1 see section 8.8.1)
804
+ // k = 128
805
+ const htfDefaults = {
806
+ // DST: a domain separation tag
807
+ // defined in section 2.2.5
808
+ // Use utils.getDSTLabel(), utils.setDSTLabel(value)
809
+ DST: 'BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_',
810
+ // p: the characteristic of F
811
+ // where F is a finite field of characteristic p and order q = p^m
812
+ p: Fp.ORDER,
813
+ // m: the extension degree of F, m >= 1
814
+ // where F is a finite field of characteristic p and order q = p^m
815
+ m: 2,
816
+ // k: the target security level for the suite in bits
817
+ // defined in section 5.1
818
+ k: 128,
819
+ // option to use a message that has already been processed by
820
+ // expand_message_xmd
821
+ expand: 'xmd',
822
+ // Hash functions for: expand_message_xmd is appropriate for use with a
823
+ // wide range of hash functions, including SHA-2, SHA-3, BLAKE2, and others.
824
+ // BBS+ uses blake2: https://github.com/hyperledger/aries-framework-go/issues/2247
825
+ hash: sha256_1.sha256,
826
+ };
827
+ // Encoding utils
828
+ // Point on G1 curve: (x, y)
829
+ const C_BIT_POS = Fp.BITS; // C_bit, compression bit for serialization flag
830
+ const I_BIT_POS = Fp.BITS + 1; // I_bit, point-at-infinity bit for serialization flag
831
+ const S_BIT_POS = Fp.BITS + 2; // S_bit, sign bit for serialization flag
832
+ // Compressed point of infinity
833
+ const COMPRESSED_ZERO = Fp.toBytes((0, utils_js_1.bitSet)((0, utils_js_1.bitSet)(0n, I_BIT_POS, true), S_BIT_POS, true)); // set compressed & point-at-infinity bits
834
+ // To verify curve parameters, see pairing-friendly-curves spec:
835
+ // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-pairing-friendly-curves-09
836
+ // Basic math is done over finite fields over p.
837
+ // More complicated math is done over polynominal extension fields.
838
+ // To simplify calculations in Fp12, we construct extension tower:
839
+ // Fp₁₂ = Fp₆² => Fp₂³
840
+ // Fp(u) / (u² - β) where β = -1
841
+ // Fp₂(v) / (v³ - ξ) where ξ = u + 1
842
+ // Fp₆(w) / (w² - γ) where γ = v
843
+ // Here goes constants && point encoding format
844
+ exports.bls12_381 = (0, bls_js_1.bls)({
845
+ // Fields
846
+ Fr,
847
+ Fp,
848
+ Fp2,
849
+ Fp6,
850
+ Fp12,
851
+ // order; z⁴ − z² + 1
852
+ r: Fr.ORDER,
853
+ // G1 is the order-q subgroup of E1(Fp) : y² = x³ + 4, #E1(Fp) = h1q, where
854
+ // characteristic; z + (z⁴ - z² + 1)(z - 1)²/3
855
+ G1: {
856
+ Fp,
857
+ // cofactor; (z - 1)²/3
858
+ h: 0x396c8c005555e1568c00aaab0000aaabn,
859
+ // generator's coordinates
860
+ // x = 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507
861
+ // y = 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569
862
+ Gx: 0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bbn,
863
+ Gy: 0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1n,
864
+ a: Fp.ZERO,
865
+ b: 4n,
866
+ htfDefaults: { ...htfDefaults, m: 1 },
867
+ wrapPrivateKey: true,
868
+ allowInfinityPoint: true,
869
+ // Checks is the point resides in prime-order subgroup.
870
+ // point.isTorsionFree() should return true for valid points
871
+ // It returns false for shitty points.
872
+ // https://eprint.iacr.org/2021/1130.pdf
873
+ isTorsionFree: (c, point) => {
874
+ // φ endomorphism
875
+ const cubicRootOfUnityModP = 0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen;
876
+ const phi = new c(Fp.mul(point.x, cubicRootOfUnityModP), point.y, point.z);
877
+ // todo: unroll
878
+ const xP = point.multiplyUnsafe(exports.bls12_381.CURVE.x).negate(); // [x]P
879
+ const u2P = xP.multiplyUnsafe(exports.bls12_381.CURVE.x); // [u2]P
880
+ return u2P.equals(phi);
881
+ // https://eprint.iacr.org/2019/814.pdf
882
+ // (z² − 1)/3
883
+ // const c1 = 0x396c8c005555e1560000000055555555n;
884
+ // const P = this;
885
+ // const S = P.sigma();
886
+ // const Q = S.double();
887
+ // const S2 = S.sigma();
888
+ // // [(z² − 1)/3](2σ(P) − P − σ²(P)) − σ²(P) = O
889
+ // const left = Q.subtract(P).subtract(S2).multiplyUnsafe(c1);
890
+ // const C = left.subtract(S2);
891
+ // return C.isZero();
892
+ },
893
+ // Clear cofactor of G1
894
+ // https://eprint.iacr.org/2019/403
895
+ clearCofactor: (c, point) => {
896
+ // return this.multiplyUnsafe(CURVE.h);
897
+ return point.multiplyUnsafe(exports.bls12_381.CURVE.x).add(point); // x*P + P
898
+ },
899
+ mapToCurve: (scalars) => {
900
+ const { x, y } = G1_SWU(Fp.create(scalars[0]));
901
+ return isogenyMapG1(x, y);
902
+ },
903
+ fromBytes: (bytes) => {
904
+ if (bytes.length === 48) {
905
+ const P = Fp.ORDER;
906
+ const compressedValue = (0, utils_js_1.bytesToNumberBE)(bytes);
907
+ const bflag = (0, utils_js_1.bitGet)(compressedValue, I_BIT_POS);
908
+ // Zero
909
+ if (bflag === 1n)
910
+ return { x: 0n, y: 0n };
911
+ const x = Fp.create(compressedValue & Fp.MASK);
912
+ const right = Fp.add(Fp.pow(x, 3n), Fp.create(exports.bls12_381.CURVE.G1.b)); // y² = x³ + b
913
+ let y = Fp.sqrt(right);
914
+ if (!y)
915
+ throw new Error('Invalid compressed G1 point');
916
+ const aflag = (0, utils_js_1.bitGet)(compressedValue, C_BIT_POS);
917
+ if ((y * 2n) / P !== aflag)
918
+ y = Fp.negate(y);
919
+ return { x: Fp.create(x), y: Fp.create(y) };
920
+ }
921
+ else if (bytes.length === 96) {
922
+ // Check if the infinity flag is set
923
+ if ((bytes[0] & (1 << 6)) !== 0)
924
+ return exports.bls12_381.G1.Point.ZERO;
925
+ const x = (0, utils_js_1.bytesToNumberBE)(bytes.slice(0, Fp.BYTES));
926
+ const y = (0, utils_js_1.bytesToNumberBE)(bytes.slice(Fp.BYTES));
927
+ return { x: Fp.create(x), y: Fp.create(y) };
928
+ }
929
+ else {
930
+ throw new Error('Invalid point G1, expected 48/96 bytes');
931
+ }
932
+ },
933
+ toBytes: (c, point, isCompressed) => {
934
+ const isZero = point.equals(c.ZERO);
935
+ const { x, y } = point;
936
+ if (isCompressed) {
937
+ if (isZero)
938
+ return COMPRESSED_ZERO.slice();
939
+ const P = Fp.ORDER;
940
+ let num;
941
+ num = (0, utils_js_1.bitSet)(x, C_BIT_POS, Boolean((y * 2n) / P)); // set aflag
942
+ num = (0, utils_js_1.bitSet)(num, S_BIT_POS, true);
943
+ return (0, utils_js_1.numberToBytesBE)(num, Fp.BYTES);
944
+ }
945
+ else {
946
+ if (isZero) {
947
+ // 2x PUBLIC_KEY_LENGTH
948
+ const x = (0, utils_js_1.concatBytes)(new Uint8Array([0x40]), new Uint8Array(2 * Fp.BYTES - 1));
949
+ return x;
950
+ }
951
+ else {
952
+ return (0, utils_js_1.concatBytes)((0, utils_js_1.numberToBytesBE)(x, Fp.BYTES), (0, utils_js_1.numberToBytesBE)(y, Fp.BYTES));
953
+ }
954
+ }
955
+ },
956
+ },
957
+ // G2 is the order-q subgroup of E2(Fp²) : y² = x³+4(1+√−1),
958
+ // where Fp2 is Fp[√−1]/(x2+1). #E2(Fp2 ) = h2q, where
959
+ // G² - 1
960
+ // h2q
961
+ G2: {
962
+ Fp: Fp2,
963
+ // cofactor
964
+ h: 0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5n,
965
+ Gx: Fp2.fromBigTuple([
966
+ 0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8n,
967
+ 0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7en,
968
+ ]),
969
+ // y =
970
+ // 927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582,
971
+ // 1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905
972
+ Gy: Fp2.fromBigTuple([
973
+ 0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801n,
974
+ 0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79ben,
975
+ ]),
976
+ a: Fp2.ZERO,
977
+ b: Fp2.fromBigTuple([4n, 4n]),
978
+ hEff: 0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551n,
979
+ htfDefaults: { ...htfDefaults },
980
+ wrapPrivateKey: true,
981
+ allowInfinityPoint: true,
982
+ mapToCurve: (scalars) => {
983
+ const { x, y } = G2_SWU(Fp2.fromBigTuple(scalars));
984
+ return isogenyMapG2(x, y);
985
+ },
986
+ // Checks is the point resides in prime-order subgroup.
987
+ // point.isTorsionFree() should return true for valid points
988
+ // It returns false for shitty points.
989
+ // https://eprint.iacr.org/2021/1130.pdf
990
+ isTorsionFree: (c, P) => {
991
+ return P.multiplyUnsafe(exports.bls12_381.CURVE.x).negate().equals(G2psi(c, P)); // ψ(P) == [u](P)
992
+ // Older version: https://eprint.iacr.org/2019/814.pdf
993
+ // Ψ²(P) => Ψ³(P) => [z]Ψ³(P) where z = -x => [z]Ψ³(P) - Ψ²(P) + P == O
994
+ // return P.psi2().psi().mulNegX().subtract(psi2).add(P).isZero();
995
+ },
996
+ // Maps the point into the prime-order subgroup G2.
997
+ // clear_cofactor_bls12381_g2 from cfrg-hash-to-curve-11
998
+ // https://eprint.iacr.org/2017/419.pdf
999
+ // prettier-ignore
1000
+ clearCofactor: (c, P) => {
1001
+ const { x } = exports.bls12_381.CURVE;
1002
+ let t1 = P.multiplyUnsafe(x).negate(); // [-x]P
1003
+ let t2 = G2psi(c, P); // Ψ(P)
1004
+ let t3 = P.double(); // 2P
1005
+ t3 = G2psi2(c, t3); // Ψ²(2P)
1006
+ t3 = t3.subtract(t2); // Ψ²(2P) - Ψ(P)
1007
+ t2 = t1.add(t2); // [-x]P + Ψ(P)
1008
+ t2 = t2.multiplyUnsafe(x).negate(); // [x²]P - [x]Ψ(P)
1009
+ t3 = t3.add(t2); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P)
1010
+ t3 = t3.subtract(t1); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P) + [x]P
1011
+ const Q = t3.subtract(P); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P) + [x]P - 1P
1012
+ return Q; // [x²-x-1]P + [x-1]Ψ(P) + Ψ²(2P)
1013
+ },
1014
+ fromBytes: (bytes) => {
1015
+ const m_byte = bytes[0] & 0xe0;
1016
+ if (m_byte === 0x20 || m_byte === 0x60 || m_byte === 0xe0) {
1017
+ throw new Error('Invalid encoding flag: ' + m_byte);
1018
+ }
1019
+ const bitC = m_byte & 0x80; // compression bit
1020
+ const bitI = m_byte & 0x40; // point at infinity bit
1021
+ const bitS = m_byte & 0x20; // sign bit
1022
+ if (bytes.length === 96 && bitC) {
1023
+ const { b } = exports.bls12_381.CURVE.G2;
1024
+ const P = Fp.ORDER;
1025
+ bytes[0] = bytes[0] & 0x1f; // clear flags
1026
+ if (bitI) {
1027
+ // check that all bytes are 0
1028
+ if (bytes.reduce((p, c) => (p !== 0 ? c + 1 : c), 0) > 0) {
1029
+ throw new Error('Invalid compressed G2 point');
1030
+ }
1031
+ return { x: Fp2.ZERO, y: Fp2.ZERO };
1032
+ }
1033
+ const x_1 = (0, utils_js_1.bytesToNumberBE)(bytes.slice(0, Fp.BYTES));
1034
+ const x_0 = (0, utils_js_1.bytesToNumberBE)(bytes.slice(Fp.BYTES));
1035
+ const x = Fp2.create({ c0: Fp.create(x_0), c1: Fp.create(x_1) });
1036
+ const right = Fp2.add(Fp2.pow(x, 3n), b); // y² = x³ + 4 * (u+1) = x³ + b
1037
+ let y = Fp2.sqrt(right);
1038
+ const Y_bit = y.c1 === 0n ? (y.c0 * 2n) / P : (y.c1 * 2n) / P ? 1n : 0n;
1039
+ y = bitS > 0 && Y_bit > 0 ? y : Fp2.negate(y);
1040
+ return { x, y };
1041
+ }
1042
+ else if (bytes.length === 192 && !bitC) {
1043
+ // Check if the infinity flag is set
1044
+ if ((bytes[0] & (1 << 6)) !== 0) {
1045
+ return { x: Fp2.ZERO, y: Fp2.ZERO };
1046
+ }
1047
+ const x1 = (0, utils_js_1.bytesToNumberBE)(bytes.slice(0, Fp.BYTES));
1048
+ const x0 = (0, utils_js_1.bytesToNumberBE)(bytes.slice(Fp.BYTES, 2 * Fp.BYTES));
1049
+ const y1 = (0, utils_js_1.bytesToNumberBE)(bytes.slice(2 * Fp.BYTES, 3 * Fp.BYTES));
1050
+ const y0 = (0, utils_js_1.bytesToNumberBE)(bytes.slice(3 * Fp.BYTES));
1051
+ return { x: Fp2.fromBigTuple([x0, x1]), y: Fp2.fromBigTuple([y0, y1]) };
1052
+ }
1053
+ else {
1054
+ throw new Error('Invalid point G2, expected 96/192 bytes');
1055
+ }
1056
+ },
1057
+ toBytes: (c, point, isCompressed) => {
1058
+ const isZero = point.equals(c.ZERO);
1059
+ const { x, y } = point;
1060
+ if (isCompressed) {
1061
+ const P = Fp.ORDER;
1062
+ if (isZero)
1063
+ return (0, utils_js_1.concatBytes)(COMPRESSED_ZERO, (0, utils_js_1.numberToBytesBE)(0n, Fp.BYTES));
1064
+ const flag = Boolean(y.c1 === 0n ? (y.c0 * 2n) / P : (y.c1 * 2n) / P);
1065
+ // set compressed & sign bits (looks like different offsets than for G1/Fp?)
1066
+ let x_1 = (0, utils_js_1.bitSet)(x.c1, C_BIT_POS, flag);
1067
+ x_1 = (0, utils_js_1.bitSet)(x_1, S_BIT_POS, true);
1068
+ return (0, utils_js_1.concatBytes)((0, utils_js_1.numberToBytesBE)(x_1, Fp.BYTES), (0, utils_js_1.numberToBytesBE)(x.c0, Fp.BYTES));
1069
+ }
1070
+ else {
1071
+ if (isZero)
1072
+ return (0, utils_js_1.concatBytes)(new Uint8Array([0x40]), new Uint8Array(4 * Fp.BYTES - 1)); // bytes[0] |= 1 << 6;
1073
+ const { re: x0, im: x1 } = Fp2.reim(x);
1074
+ const { re: y0, im: y1 } = Fp2.reim(y);
1075
+ return (0, utils_js_1.concatBytes)((0, utils_js_1.numberToBytesBE)(x1, Fp.BYTES), (0, utils_js_1.numberToBytesBE)(x0, Fp.BYTES), (0, utils_js_1.numberToBytesBE)(y1, Fp.BYTES), (0, utils_js_1.numberToBytesBE)(y0, Fp.BYTES));
1076
+ }
1077
+ },
1078
+ Signature: {
1079
+ // TODO: Optimize, it's very slow because of sqrt.
1080
+ decode(hex) {
1081
+ hex = (0, utils_js_1.ensureBytes)(hex);
1082
+ const P = Fp.ORDER;
1083
+ const half = hex.length / 2;
1084
+ if (half !== 48 && half !== 96)
1085
+ throw new Error('Invalid compressed signature length, must be 96 or 192');
1086
+ const z1 = (0, utils_js_1.bytesToNumberBE)(hex.slice(0, half));
1087
+ const z2 = (0, utils_js_1.bytesToNumberBE)(hex.slice(half));
1088
+ // Indicates the infinity point
1089
+ const bflag1 = (0, utils_js_1.bitGet)(z1, I_BIT_POS);
1090
+ if (bflag1 === 1n)
1091
+ return exports.bls12_381.G2.Point.ZERO;
1092
+ const x1 = Fp.create(z1 & Fp.MASK);
1093
+ const x2 = Fp.create(z2);
1094
+ const x = Fp2.create({ c0: x2, c1: x1 });
1095
+ const y2 = Fp2.add(Fp2.pow(x, 3n), exports.bls12_381.CURVE.G2.b); // y² = x³ + 4
1096
+ // The slow part
1097
+ let y = Fp2.sqrt(y2);
1098
+ if (!y)
1099
+ throw new Error('Failed to find a square root');
1100
+ // Choose the y whose leftmost bit of the imaginary part is equal to the a_flag1
1101
+ // If y1 happens to be zero, then use the bit of y0
1102
+ const { re: y0, im: y1 } = Fp2.reim(y);
1103
+ const aflag1 = (0, utils_js_1.bitGet)(z1, 381);
1104
+ const isGreater = y1 > 0n && (y1 * 2n) / P !== aflag1;
1105
+ const isZero = y1 === 0n && (y0 * 2n) / P !== aflag1;
1106
+ if (isGreater || isZero)
1107
+ y = Fp2.negate(y);
1108
+ const point = new exports.bls12_381.G2.Point(x, y);
1109
+ point.assertValidity();
1110
+ return point;
1111
+ },
1112
+ encode(point) {
1113
+ // NOTE: by some reasons it was missed in bls12-381, looks like bug
1114
+ point.assertValidity();
1115
+ if (point.equals(exports.bls12_381.G2.Point.ZERO))
1116
+ return (0, utils_js_1.concatBytes)(COMPRESSED_ZERO, (0, utils_js_1.numberToBytesBE)(0n, Fp.BYTES));
1117
+ const { re: x0, im: x1 } = Fp2.reim(point.x);
1118
+ const { re: y0, im: y1 } = Fp2.reim(point.y);
1119
+ const tmp = y1 > 0n ? y1 * 2n : y0 * 2n;
1120
+ const aflag1 = Boolean((tmp / Fp.ORDER) & 1n);
1121
+ const z1 = (0, utils_js_1.bitSet)((0, utils_js_1.bitSet)(x1, 381, aflag1), S_BIT_POS, true);
1122
+ const z2 = x0;
1123
+ return (0, utils_js_1.concatBytes)((0, utils_js_1.numberToBytesBE)(z1, Fp.BYTES), (0, utils_js_1.numberToBytesBE)(z2, Fp.BYTES));
1124
+ },
1125
+ },
1126
+ },
1127
+ // The BLS parameter x for BLS12-381
1128
+ x: BLS_X,
1129
+ htfDefaults,
1130
+ hash: sha256_1.sha256,
1131
+ randomBytes: utils_1.randomBytes,
1132
+ });