@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
@@ -0,0 +1,963 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.__TEST = void 0;
37
+ exports.psiFrobenius = psiFrobenius;
38
+ exports.tower12 = tower12;
39
+ /**
40
+ * Towered extension fields.
41
+ * Rather than implementing a massive 12th-degree extension directly, it is more efficient
42
+ * to build it up from smaller extensions: a tower of extensions.
43
+ *
44
+ * For BLS12-381, the Fp12 field is implemented as a quadratic (degree two) extension,
45
+ * on top of a cubic (degree three) extension, on top of a quadratic extension of Fp.
46
+ *
47
+ * For more info: "Pairings for beginners" by Costello, section 7.3.
48
+ * @module
49
+ */
50
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
51
+ const utils_1 = require("../utils");
52
+ const mod = __importStar(require("./modular"));
53
+ // Be friendly to bad ECMAScript parsers by not using bigint literals
54
+ // prettier-ignore
55
+ const _0n = /* @__PURE__ */ BigInt(0), _1n = /* @__PURE__ */ BigInt(1), _2n = /* @__PURE__ */ BigInt(2), _3n = /* @__PURE__ */ BigInt(3), _6n = /* @__PURE__ */ BigInt(6), _12n = /* @__PURE__ */ BigInt(12);
56
+ const isObj = (value) => !!value && typeof value === 'object';
57
+ function calcFrobeniusCoefficients(Fp, nonResidue, modulus, degree, num = 1, divisor) {
58
+ (0, utils_1.asafenumber)(num, 'num');
59
+ const F = Fp;
60
+ // Generic callers can hit empty / fractional row counts through `__TEST`; fail closed instead of
61
+ // silently returning `[]` or deriving extra Frobenius rows from a truncated loop bound.
62
+ if (num <= 0)
63
+ throw new Error('calcFrobeniusCoefficients: expected positive row count, got ' + num);
64
+ const _divisor = BigInt(divisor === undefined ? degree : divisor);
65
+ const towerModulus = modulus ** BigInt(degree);
66
+ const res = [];
67
+ // Derive tower-basis multipliers for the `p^k` Frobenius action. The
68
+ // divisions below are expected to be exact for the chosen tower parameters.
69
+ for (let i = 0; i < num; i++) {
70
+ const a = BigInt(i + 1);
71
+ const powers = [];
72
+ for (let j = 0, qPower = _1n; j < degree; j++) {
73
+ const numer = a * qPower - a;
74
+ // Shipped towers divide cleanly here, but generic callers can pick bad
75
+ // params. Bigint division would floor and derive the wrong Frobenius table.
76
+ if (numer % _divisor)
77
+ throw new Error('calcFrobeniusCoefficients: inexact tower exponent');
78
+ const power = (numer / _divisor) % towerModulus;
79
+ powers.push(F.pow(nonResidue, power));
80
+ qPower *= modulus;
81
+ }
82
+ res.push(powers);
83
+ }
84
+ return res;
85
+ }
86
+ exports.__TEST =
87
+ /* @__PURE__ */ Object.freeze({
88
+ calcFrobeniusCoefficients,
89
+ });
90
+ // This works same at least for bls12-381, bn254 and bls12-377
91
+ /**
92
+ * @param Fp - Base field implementation.
93
+ * @param Fp2 - Quadratic extension field.
94
+ * @param base - Twist-specific Frobenius base whose powers yield the `c1` / `c2` constants.
95
+ * BLS12-381 uses `1 / NONRESIDUE`; BN254 uses `NONRESIDUE`.
96
+ * @returns Frobenius endomorphism helpers.
97
+ * @throws If the derived Frobenius constants are inconsistent for the tower. {@link Error}
98
+ * @example
99
+ * Build Frobenius endomorphism helpers for a BLS extension tower.
100
+ *
101
+ * ```ts
102
+ * import { psiFrobenius } from './tower';
103
+ * import { bls12_381 } from '../bls12-381';
104
+ * const Fp = bls12_381.fields.Fp;
105
+ * const Fp2 = bls12_381.fields.Fp2;
106
+ * const frob = psiFrobenius(Fp, Fp2, Fp2.div(Fp2.ONE, Fp2.NONRESIDUE));
107
+ * const point = frob.G2psi(bls12_381.G2.Point, bls12_381.G2.Point.BASE);
108
+ * ```
109
+ */
110
+ function psiFrobenius(Fp, Fp2, base) {
111
+ // GLV endomorphism Ψ(P)
112
+ const PSI_X = Fp2.pow(base, (Fp.ORDER - _1n) / _3n); // u^((p-1)/3)
113
+ const PSI_Y = Fp2.pow(base, (Fp.ORDER - _1n) / _2n); // u^((p-1)/2)
114
+ function psi(x, y) {
115
+ // This x10 faster than previous version in bls12-381
116
+ const x2 = Fp2.mul(Fp2.frobeniusMap(x, 1), PSI_X);
117
+ const y2 = Fp2.mul(Fp2.frobeniusMap(y, 1), PSI_Y);
118
+ return [x2, y2];
119
+ }
120
+ // Ψ²(P) endomorphism (psi2(x) = psi(psi(x)))
121
+ const PSI2_X = Fp2.pow(base, (Fp.ORDER ** _2n - _1n) / _3n); // u^((p^2 - 1)/3)
122
+ // Current towers rely on this landing on `-1`, which lets psi2 map `y` with
123
+ // one negation instead of carrying a separate Frobenius multiplier.
124
+ const PSI2_Y = Fp2.pow(base, (Fp.ORDER ** _2n - _1n) / _2n); // u^((p^2 - 1)/2)
125
+ if (!Fp2.eql(PSI2_Y, Fp2.neg(Fp2.ONE)))
126
+ throw new Error('psiFrobenius: PSI2_Y!==-1');
127
+ function psi2(x, y) {
128
+ return [Fp2.mul(x, PSI2_X), Fp2.neg(y)];
129
+ }
130
+ // Map points
131
+ const mapAffine = (fn) => (c, P) => {
132
+ const affine = P.toAffine();
133
+ const p = fn(affine.x, affine.y);
134
+ return c.fromAffine({ x: p[0], y: p[1] });
135
+ };
136
+ const G2psi = mapAffine(psi);
137
+ const G2psi2 = mapAffine(psi2);
138
+ return { psi, psi2, G2psi, G2psi2, PSI_X, PSI_Y, PSI2_X, PSI2_Y };
139
+ }
140
+ class _Field2 {
141
+ ORDER;
142
+ BITS;
143
+ BYTES;
144
+ isLE;
145
+ ZERO;
146
+ ONE;
147
+ Fp;
148
+ NONRESIDUE;
149
+ mulByB;
150
+ Fp_NONRESIDUE;
151
+ Fp_div2;
152
+ FROBENIUS_COEFFICIENTS;
153
+ constructor(Fp, opts = {}) {
154
+ const { NONRESIDUE = BigInt(-1), FP2_NONRESIDUE, Fp2mulByB } = opts;
155
+ const ORDER = Fp.ORDER;
156
+ const FP2_ORDER = ORDER * ORDER;
157
+ this.Fp = Fp;
158
+ this.ORDER = FP2_ORDER;
159
+ this.BITS = (0, utils_1.bitLen)(FP2_ORDER);
160
+ this.BYTES = Math.ceil((0, utils_1.bitLen)(FP2_ORDER) / 8);
161
+ this.isLE = Fp.isLE;
162
+ this.ZERO = this.create({ c0: Fp.ZERO, c1: Fp.ZERO });
163
+ this.ONE = this.create({ c0: Fp.ONE, c1: Fp.ZERO });
164
+ // These knobs only swap constants for the shipped quadratic tower shape:
165
+ // arithmetic below assumes `u^2 = -1`, and bytes are handled as two adjacent
166
+ // `Fp` limbs (`fromBytes` / `toBytes` expect the shipped `2 * Fp.BYTES` layout).
167
+ this.Fp_NONRESIDUE = Fp.create(NONRESIDUE);
168
+ this.Fp_div2 = Fp.div(Fp.ONE, _2n); // 1/2
169
+ this.NONRESIDUE = this.create({ c0: FP2_NONRESIDUE[0], c1: FP2_NONRESIDUE[1] });
170
+ // const Fp2Nonresidue = this.create({ c0: FP2_NONRESIDUE![0], c1: FP2_NONRESIDUE![1] });
171
+ this.FROBENIUS_COEFFICIENTS = Object.freeze(calcFrobeniusCoefficients(Fp, this.Fp_NONRESIDUE, Fp.ORDER, 2)[0]);
172
+ this.mulByB = (num) => {
173
+ // This config hook is trusted to return a canonical Fp2 value already.
174
+ // Copy+freeze it to keep the tower immutability invariant without mutating caller objects.
175
+ const { c0, c1 } = Fp2mulByB(num);
176
+ return Object.freeze({ c0, c1 });
177
+ };
178
+ Object.freeze(this);
179
+ }
180
+ fromBigTuple(tuple) {
181
+ if (!Array.isArray(tuple) || tuple.length !== 2)
182
+ throw new Error('invalid Fp2.fromBigTuple');
183
+ const [c0, c1] = tuple;
184
+ if (typeof c0 !== 'bigint' || typeof c1 !== 'bigint')
185
+ throw new Error('invalid Fp2.fromBigTuple');
186
+ return this.create({ c0, c1 });
187
+ }
188
+ create(num) {
189
+ const { Fp } = this;
190
+ const c0 = Fp.create(num.c0);
191
+ const c1 = Fp.create(num.c1);
192
+ // Bigint field elements are immutable values, and higher-level code relies on
193
+ // that invariant. Copy+freeze tower values too without mutating caller-owned objects.
194
+ return Object.freeze({ c0, c1 });
195
+ }
196
+ isValid(num) {
197
+ if (!isObj(num))
198
+ throw new TypeError('invalid field element: expected object, got ' + typeof num);
199
+ const { c0, c1 } = num;
200
+ const { Fp } = this;
201
+ // Match base-field `isValid(...)`: malformed coordinate types are errors, not a `false`
202
+ // predicate result.
203
+ return Fp.isValid(c0) && Fp.isValid(c1);
204
+ }
205
+ is0(num) {
206
+ if (!isObj(num))
207
+ return false;
208
+ const { c0, c1 } = num;
209
+ const { Fp } = this;
210
+ return Fp.is0(c0) && Fp.is0(c1);
211
+ }
212
+ isValidNot0(num) {
213
+ return !this.is0(num) && this.isValid(num);
214
+ }
215
+ eql({ c0, c1 }, { c0: r0, c1: r1 }) {
216
+ const { Fp } = this;
217
+ return Fp.eql(c0, r0) && Fp.eql(c1, r1);
218
+ }
219
+ neg({ c0, c1 }) {
220
+ const { Fp } = this;
221
+ return Object.freeze({ c0: Fp.neg(c0), c1: Fp.neg(c1) });
222
+ }
223
+ pow(num, power) {
224
+ return mod.FpPow(this, num, power);
225
+ }
226
+ invertBatch(nums) {
227
+ return mod.FpInvertBatch(this, nums);
228
+ }
229
+ // Normalized
230
+ add(f1, f2) {
231
+ const { Fp } = this;
232
+ const { c0, c1 } = f1;
233
+ const { c0: r0, c1: r1 } = f2;
234
+ return Object.freeze({
235
+ c0: Fp.add(c0, r0),
236
+ c1: Fp.add(c1, r1),
237
+ });
238
+ }
239
+ sub({ c0, c1 }, { c0: r0, c1: r1 }) {
240
+ const { Fp } = this;
241
+ return Object.freeze({
242
+ c0: Fp.sub(c0, r0),
243
+ c1: Fp.sub(c1, r1),
244
+ });
245
+ }
246
+ mul({ c0, c1 }, rhs) {
247
+ const { Fp } = this;
248
+ if (typeof rhs === 'bigint')
249
+ return Object.freeze({ c0: Fp.mul(c0, rhs), c1: Fp.mul(c1, rhs) });
250
+ // (a+bi)(c+di) = (ac−bd) + (ad+bc)i
251
+ const { c0: r0, c1: r1 } = rhs;
252
+ let t1 = Fp.mul(c0, r0); // c0 * o0
253
+ let t2 = Fp.mul(c1, r1); // c1 * o1
254
+ // (T1 - T2) + ((c0 + c1) * (r0 + r1) - (T1 + T2))*i
255
+ const o0 = Fp.sub(t1, t2);
256
+ const o1 = Fp.sub(Fp.mul(Fp.add(c0, c1), Fp.add(r0, r1)), Fp.add(t1, t2));
257
+ return Object.freeze({ c0: o0, c1: o1 });
258
+ }
259
+ sqr({ c0, c1 }) {
260
+ const { Fp } = this;
261
+ const a = Fp.add(c0, c1);
262
+ const b = Fp.sub(c0, c1);
263
+ const c = Fp.add(c0, c0);
264
+ return Object.freeze({ c0: Fp.mul(a, b), c1: Fp.mul(c, c1) });
265
+ }
266
+ // NonNormalized stuff
267
+ addN(a, b) {
268
+ return this.add(a, b);
269
+ }
270
+ subN(a, b) {
271
+ return this.sub(a, b);
272
+ }
273
+ mulN(a, b) {
274
+ return this.mul(a, b);
275
+ }
276
+ sqrN(a) {
277
+ return this.sqr(a);
278
+ }
279
+ // Why inversion for bigint inside Fp instead of Fp2? it is even used in that context?
280
+ div(lhs, rhs) {
281
+ const { Fp } = this;
282
+ // @ts-ignore
283
+ return this.mul(lhs, typeof rhs === 'bigint' ? Fp.inv(Fp.create(rhs)) : this.inv(rhs));
284
+ }
285
+ inv({ c0: a, c1: b }) {
286
+ // We wish to find the multiplicative inverse of a nonzero
287
+ // element a + bu in Fp2. We leverage an identity
288
+ //
289
+ // (a + bu)(a - bu) = a² + b²
290
+ //
291
+ // which holds because u² = -1. This can be rewritten as
292
+ //
293
+ // (a + bu)(a - bu)/(a² + b²) = 1
294
+ //
295
+ // because a² + b² = 0 has no nonzero solutions for (a, b).
296
+ // This gives that (a - bu)/(a² + b²) is the inverse
297
+ // of (a + bu). Importantly, this can be computing using
298
+ // only a single inversion in Fp.
299
+ const { Fp } = this;
300
+ const factor = Fp.inv(Fp.create(a * a + b * b));
301
+ return Object.freeze({ c0: Fp.mul(factor, Fp.create(a)), c1: Fp.mul(factor, Fp.create(-b)) });
302
+ }
303
+ sqrt(num) {
304
+ // This is generic for all quadratic extensions (Fp2)
305
+ const { Fp } = this;
306
+ const Fp2 = this;
307
+ const { c0, c1 } = num;
308
+ if (Fp.is0(c1)) {
309
+ // if c0 is quadratic residue
310
+ if (mod.FpLegendre(Fp, c0) === 1)
311
+ return Fp2.create({ c0: Fp.sqrt(c0), c1: Fp.ZERO });
312
+ else
313
+ return Fp2.create({ c0: Fp.ZERO, c1: Fp.sqrt(Fp.div(c0, this.Fp_NONRESIDUE)) });
314
+ }
315
+ const a = Fp.sqrt(Fp.sub(Fp.sqr(c0), Fp.mul(Fp.sqr(c1), this.Fp_NONRESIDUE)));
316
+ let d = Fp.mul(Fp.add(a, c0), this.Fp_div2);
317
+ const legendre = mod.FpLegendre(Fp, d);
318
+ // -1, Quadratic non residue
319
+ if (legendre === -1)
320
+ d = Fp.sub(d, a);
321
+ const a0 = Fp.sqrt(d);
322
+ const candidateSqrt = Fp2.create({ c0: a0, c1: Fp.div(Fp.mul(c1, this.Fp_div2), a0) });
323
+ if (!Fp2.eql(Fp2.sqr(candidateSqrt), num))
324
+ throw new Error('Cannot find square root');
325
+ // Normalize root: at this point candidateSqrt ** 2 = num, but also -candidateSqrt ** 2 = num
326
+ const x1 = candidateSqrt;
327
+ const x2 = Fp2.neg(x1);
328
+ const { re: re1, im: im1 } = Fp2.reim(x1);
329
+ const { re: re2, im: im2 } = Fp2.reim(x2);
330
+ if (im1 > im2 || (im1 === im2 && re1 > re2))
331
+ return x1;
332
+ return x2;
333
+ }
334
+ // Same as sgn0_m_eq_2 in RFC 9380
335
+ isOdd(x) {
336
+ const { re: x0, im: x1 } = this.reim(x);
337
+ const sign_0 = x0 % _2n;
338
+ const zero_0 = x0 === _0n;
339
+ const sign_1 = x1 % _2n;
340
+ return BigInt(sign_0 || (zero_0 && sign_1)) == _1n;
341
+ }
342
+ // Bytes util
343
+ fromBytes(b) {
344
+ const { Fp } = this;
345
+ (0, utils_1.abytes)(b);
346
+ if (b.length !== this.BYTES)
347
+ throw new Error('fromBytes invalid length=' + b.length);
348
+ return this.create({
349
+ c0: Fp.fromBytes(b.subarray(0, Fp.BYTES)),
350
+ c1: Fp.fromBytes(b.subarray(Fp.BYTES)),
351
+ });
352
+ }
353
+ toBytes({ c0, c1 }) {
354
+ return (0, utils_1.concatBytes)(this.Fp.toBytes(c0), this.Fp.toBytes(c1));
355
+ }
356
+ cmov({ c0, c1 }, { c0: r0, c1: r1 }, c) {
357
+ const { Fp } = this;
358
+ return this.create({
359
+ c0: Fp.cmov(c0, r0, c),
360
+ c1: Fp.cmov(c1, r1, c),
361
+ });
362
+ }
363
+ reim({ c0, c1 }) {
364
+ return { re: c0, im: c1 };
365
+ }
366
+ Fp4Square(a, b) {
367
+ const Fp2 = this;
368
+ const a2 = Fp2.sqr(a);
369
+ const b2 = Fp2.sqr(b);
370
+ return {
371
+ first: Fp2.add(Fp2.mulByNonresidue(b2), a2), // b² * Nonresidue + a²
372
+ second: Fp2.sub(Fp2.sub(Fp2.sqr(Fp2.add(a, b)), a2), b2), // (a + b)² - a² - b²
373
+ };
374
+ }
375
+ // multiply by u + 1
376
+ mulByNonresidue({ c0, c1 }) {
377
+ return this.mul({ c0, c1 }, this.NONRESIDUE);
378
+ }
379
+ frobeniusMap({ c0, c1 }, power) {
380
+ return Object.freeze({
381
+ c0,
382
+ c1: this.Fp.mul(c1, this.FROBENIUS_COEFFICIENTS[power % 2]),
383
+ });
384
+ }
385
+ }
386
+ class _Field6 {
387
+ ORDER;
388
+ BITS;
389
+ BYTES;
390
+ isLE;
391
+ ZERO;
392
+ ONE;
393
+ Fp2;
394
+ constructor(Fp2) {
395
+ this.Fp2 = Fp2;
396
+ // `IField.ORDER` is the field cardinality `q`; for sextic extensions that is `p^6`.
397
+ // Generic helpers like Frobenius-style `x^q = x` checks rely on the literal field size here.
398
+ this.ORDER = Fp2.Fp.ORDER ** _6n;
399
+ this.BITS = 3 * Fp2.BITS;
400
+ this.BYTES = 3 * Fp2.BYTES;
401
+ this.isLE = Fp2.isLE;
402
+ this.ZERO = this.create({ c0: Fp2.ZERO, c1: Fp2.ZERO, c2: Fp2.ZERO });
403
+ this.ONE = this.create({ c0: Fp2.ONE, c1: Fp2.ZERO, c2: Fp2.ZERO });
404
+ Object.freeze(this);
405
+ }
406
+ // Most callers never touch Frobenius maps, so keep the sextic tables lazy:
407
+ // eagerly deriving them dominates `bls12-381.js` / `bn254.js` import time.
408
+ get FROBENIUS_COEFFICIENTS_1() {
409
+ const frob = _FROBENIUS_COEFFICIENTS_6.get(this);
410
+ if (frob)
411
+ return frob[0];
412
+ const { Fp2 } = this;
413
+ const { Fp } = Fp2;
414
+ const rows = calcFrobeniusCoefficients(Fp2, Fp2.NONRESIDUE, Fp.ORDER, 6, 2, 3);
415
+ const cache = [Object.freeze(rows[0]), Object.freeze(rows[1])];
416
+ _FROBENIUS_COEFFICIENTS_6.set(this, cache);
417
+ return cache[0];
418
+ }
419
+ get FROBENIUS_COEFFICIENTS_2() {
420
+ const frob = _FROBENIUS_COEFFICIENTS_6.get(this);
421
+ if (frob)
422
+ return frob[1];
423
+ void this.FROBENIUS_COEFFICIENTS_1;
424
+ return _FROBENIUS_COEFFICIENTS_6.get(this)[1];
425
+ }
426
+ add({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }) {
427
+ const { Fp2 } = this;
428
+ return Object.freeze({
429
+ c0: Fp2.add(c0, r0),
430
+ c1: Fp2.add(c1, r1),
431
+ c2: Fp2.add(c2, r2),
432
+ });
433
+ }
434
+ sub({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }) {
435
+ const { Fp2 } = this;
436
+ return Object.freeze({
437
+ c0: Fp2.sub(c0, r0),
438
+ c1: Fp2.sub(c1, r1),
439
+ c2: Fp2.sub(c2, r2),
440
+ });
441
+ }
442
+ mul({ c0, c1, c2 }, rhs) {
443
+ const { Fp2 } = this;
444
+ if (typeof rhs === 'bigint') {
445
+ return Object.freeze({
446
+ c0: Fp2.mul(c0, rhs),
447
+ c1: Fp2.mul(c1, rhs),
448
+ c2: Fp2.mul(c2, rhs),
449
+ });
450
+ }
451
+ const { c0: r0, c1: r1, c2: r2 } = rhs;
452
+ const t0 = Fp2.mul(c0, r0); // c0 * o0
453
+ const t1 = Fp2.mul(c1, r1); // c1 * o1
454
+ const t2 = Fp2.mul(c2, r2); // c2 * o2
455
+ return Object.freeze({
456
+ // t0 + (c1 + c2) * (r1 * r2) - (T1 + T2) * (u + 1)
457
+ c0: Fp2.add(t0, Fp2.mulByNonresidue(Fp2.sub(Fp2.mul(Fp2.add(c1, c2), Fp2.add(r1, r2)), Fp2.add(t1, t2)))),
458
+ // (c0 + c1) * (r0 + r1) - (T0 + T1) + T2 * (u + 1)
459
+ c1: Fp2.add(Fp2.sub(Fp2.mul(Fp2.add(c0, c1), Fp2.add(r0, r1)), Fp2.add(t0, t1)), Fp2.mulByNonresidue(t2)),
460
+ // T1 + (c0 + c2) * (r0 + r2) - T0 + T2
461
+ c2: Fp2.sub(Fp2.add(t1, Fp2.mul(Fp2.add(c0, c2), Fp2.add(r0, r2))), Fp2.add(t0, t2)),
462
+ });
463
+ }
464
+ sqr({ c0, c1, c2 }) {
465
+ const { Fp2 } = this;
466
+ let t0 = Fp2.sqr(c0); // c0²
467
+ let t1 = Fp2.mul(Fp2.mul(c0, c1), _2n); // 2 * c0 * c1
468
+ let t3 = Fp2.mul(Fp2.mul(c1, c2), _2n); // 2 * c1 * c2
469
+ let t4 = Fp2.sqr(c2); // c2²
470
+ return Object.freeze({
471
+ c0: Fp2.add(Fp2.mulByNonresidue(t3), t0), // T3 * (u + 1) + T0
472
+ c1: Fp2.add(Fp2.mulByNonresidue(t4), t1), // T4 * (u + 1) + T1
473
+ // T1 + (c0 - c1 + c2)² + T3 - T0 - T4
474
+ c2: Fp2.sub(Fp2.sub(Fp2.add(Fp2.add(t1, Fp2.sqr(Fp2.add(Fp2.sub(c0, c1), c2))), t3), t0), t4),
475
+ });
476
+ }
477
+ addN(a, b) {
478
+ return this.add(a, b);
479
+ }
480
+ subN(a, b) {
481
+ return this.sub(a, b);
482
+ }
483
+ mulN(a, b) {
484
+ return this.mul(a, b);
485
+ }
486
+ sqrN(a) {
487
+ return this.sqr(a);
488
+ }
489
+ create(num) {
490
+ const { Fp2 } = this;
491
+ const c0 = Fp2.create(num.c0);
492
+ const c1 = Fp2.create(num.c1);
493
+ const c2 = Fp2.create(num.c2);
494
+ return Object.freeze({ c0, c1, c2 });
495
+ }
496
+ isValid(num) {
497
+ if (!isObj(num))
498
+ throw new TypeError('invalid field element: expected object, got ' + typeof num);
499
+ const { c0, c1, c2 } = num;
500
+ const { Fp2 } = this;
501
+ return Fp2.isValid(c0) && Fp2.isValid(c1) && Fp2.isValid(c2);
502
+ }
503
+ is0(num) {
504
+ if (!isObj(num))
505
+ return false;
506
+ const { c0, c1, c2 } = num;
507
+ const { Fp2 } = this;
508
+ return Fp2.is0(c0) && Fp2.is0(c1) && Fp2.is0(c2);
509
+ }
510
+ isValidNot0(num) {
511
+ return !this.is0(num) && this.isValid(num);
512
+ }
513
+ neg({ c0, c1, c2 }) {
514
+ const { Fp2 } = this;
515
+ return Object.freeze({ c0: Fp2.neg(c0), c1: Fp2.neg(c1), c2: Fp2.neg(c2) });
516
+ }
517
+ eql({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }) {
518
+ const { Fp2 } = this;
519
+ return Fp2.eql(c0, r0) && Fp2.eql(c1, r1) && Fp2.eql(c2, r2);
520
+ }
521
+ sqrt(_) {
522
+ // Sextic extensions can use generic odd-field Tonelli-Shanks, but the helper must work
523
+ // over `IField<T>` with a quadratic non-residue from Fp6 itself. The current
524
+ // `mod.tonelliShanks(P)` precomputation only searches integer residues in the base field.
525
+ return (0, utils_1.notImplemented)();
526
+ }
527
+ // Do we need division by bigint at all? Should be done via order:
528
+ div(lhs, rhs) {
529
+ const { Fp2 } = this;
530
+ const { Fp } = Fp2;
531
+ return this.mul(lhs, typeof rhs === 'bigint' ? Fp.inv(Fp.create(rhs)) : this.inv(rhs));
532
+ }
533
+ pow(num, power) {
534
+ return mod.FpPow(this, num, power);
535
+ }
536
+ invertBatch(nums) {
537
+ return mod.FpInvertBatch(this, nums);
538
+ }
539
+ inv({ c0, c1, c2 }) {
540
+ const { Fp2 } = this;
541
+ let t0 = Fp2.sub(Fp2.sqr(c0), Fp2.mulByNonresidue(Fp2.mul(c2, c1))); // c0² - c2 * c1 * (u + 1)
542
+ let t1 = Fp2.sub(Fp2.mulByNonresidue(Fp2.sqr(c2)), Fp2.mul(c0, c1)); // c2² * (u + 1) - c0 * c1
543
+ let t2 = Fp2.sub(Fp2.sqr(c1), Fp2.mul(c0, c2)); // c1² - c0 * c2
544
+ // 1/(((c2 * T1 + c1 * T2) * v) + c0 * T0)
545
+ let t4 = Fp2.inv(Fp2.add(Fp2.mulByNonresidue(Fp2.add(Fp2.mul(c2, t1), Fp2.mul(c1, t2))), Fp2.mul(c0, t0)));
546
+ return Object.freeze({ c0: Fp2.mul(t4, t0), c1: Fp2.mul(t4, t1), c2: Fp2.mul(t4, t2) });
547
+ }
548
+ // Bytes utils
549
+ fromBytes(b) {
550
+ const { Fp2 } = this;
551
+ (0, utils_1.abytes)(b);
552
+ if (b.length !== this.BYTES)
553
+ throw new Error('fromBytes invalid length=' + b.length);
554
+ const B2 = Fp2.BYTES;
555
+ return this.create({
556
+ c0: Fp2.fromBytes(b.subarray(0, B2)),
557
+ c1: Fp2.fromBytes(b.subarray(B2, B2 * 2)),
558
+ c2: Fp2.fromBytes(b.subarray(2 * B2)),
559
+ });
560
+ }
561
+ toBytes({ c0, c1, c2 }) {
562
+ const { Fp2 } = this;
563
+ return (0, utils_1.concatBytes)(Fp2.toBytes(c0), Fp2.toBytes(c1), Fp2.toBytes(c2));
564
+ }
565
+ cmov({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }, c) {
566
+ const { Fp2 } = this;
567
+ return this.create({
568
+ c0: Fp2.cmov(c0, r0, c),
569
+ c1: Fp2.cmov(c1, r1, c),
570
+ c2: Fp2.cmov(c2, r2, c),
571
+ });
572
+ }
573
+ fromBigSix(tuple) {
574
+ const { Fp2 } = this;
575
+ if (!Array.isArray(tuple) || tuple.length !== 6)
576
+ throw new Error('invalid Fp6.fromBigSix');
577
+ for (let i = 0; i < 6; i++)
578
+ if (typeof tuple[i] !== 'bigint')
579
+ throw new Error('invalid Fp6.fromBigSix');
580
+ const t = tuple;
581
+ return this.create({
582
+ c0: Fp2.fromBigTuple(t.slice(0, 2)),
583
+ c1: Fp2.fromBigTuple(t.slice(2, 4)),
584
+ c2: Fp2.fromBigTuple(t.slice(4, 6)),
585
+ });
586
+ }
587
+ frobeniusMap({ c0, c1, c2 }, power) {
588
+ const { Fp2 } = this;
589
+ return Object.freeze({
590
+ c0: Fp2.frobeniusMap(c0, power),
591
+ c1: Fp2.mul(Fp2.frobeniusMap(c1, power), this.FROBENIUS_COEFFICIENTS_1[power % 6]),
592
+ c2: Fp2.mul(Fp2.frobeniusMap(c2, power), this.FROBENIUS_COEFFICIENTS_2[power % 6]),
593
+ });
594
+ }
595
+ mulByFp2({ c0, c1, c2 }, rhs) {
596
+ const { Fp2 } = this;
597
+ return Object.freeze({
598
+ c0: Fp2.mul(c0, rhs),
599
+ c1: Fp2.mul(c1, rhs),
600
+ c2: Fp2.mul(c2, rhs),
601
+ });
602
+ }
603
+ mulByNonresidue({ c0, c1, c2 }) {
604
+ const { Fp2 } = this;
605
+ return Object.freeze({ c0: Fp2.mulByNonresidue(c2), c1: c0, c2: c1 });
606
+ }
607
+ // Sparse multiplication
608
+ mul1({ c0, c1, c2 }, b1) {
609
+ const { Fp2 } = this;
610
+ return Object.freeze({
611
+ c0: Fp2.mulByNonresidue(Fp2.mul(c2, b1)),
612
+ c1: Fp2.mul(c0, b1),
613
+ c2: Fp2.mul(c1, b1),
614
+ });
615
+ }
616
+ // Sparse multiplication
617
+ mul01({ c0, c1, c2 }, b0, b1) {
618
+ const { Fp2 } = this;
619
+ let t0 = Fp2.mul(c0, b0); // c0 * b0
620
+ let t1 = Fp2.mul(c1, b1); // c1 * b1
621
+ return Object.freeze({
622
+ // ((c1 + c2) * b1 - T1) * (u + 1) + T0
623
+ c0: Fp2.add(Fp2.mulByNonresidue(Fp2.sub(Fp2.mul(Fp2.add(c1, c2), b1), t1)), t0),
624
+ // (b0 + b1) * (c0 + c1) - T0 - T1
625
+ c1: Fp2.sub(Fp2.sub(Fp2.mul(Fp2.add(b0, b1), Fp2.add(c0, c1)), t0), t1),
626
+ // (c0 + c2) * b0 - T0 + T1
627
+ c2: Fp2.add(Fp2.sub(Fp2.mul(Fp2.add(c0, c2), b0), t0), t1),
628
+ });
629
+ }
630
+ }
631
+ // Keep lazy tower caches off-object: field instances stay frozen, and debugger output
632
+ // stays readable without JS private slots while second/subsequent lookups still hit cache.
633
+ const _FROBENIUS_COEFFICIENTS_6 = new WeakMap();
634
+ class _Field12 {
635
+ ORDER;
636
+ BITS;
637
+ BYTES;
638
+ isLE;
639
+ ZERO;
640
+ ONE;
641
+ Fp6;
642
+ X_LEN;
643
+ finalExponentiate;
644
+ constructor(Fp6, opts) {
645
+ const { X_LEN, Fp12finalExponentiate } = opts;
646
+ const { Fp2 } = Fp6;
647
+ const { Fp } = Fp2;
648
+ this.Fp6 = Fp6;
649
+ // `IField.ORDER` is the field cardinality `q`; for degree-12 extensions that is `p^12`.
650
+ // Keeping `p^2` here breaks generic field identities like `x^q = x` on Fp12.
651
+ this.ORDER = Fp.ORDER ** _12n;
652
+ this.BITS = 2 * Fp6.BITS;
653
+ this.BYTES = 2 * Fp6.BYTES;
654
+ this.isLE = Fp6.isLE;
655
+ // Returned tower values are frozen, so larger constants can safely reuse
656
+ // already-frozen child coefficients instead of cloning them.
657
+ this.ZERO = this.create({ c0: Fp6.ZERO, c1: Fp6.ZERO });
658
+ this.ONE = this.create({ c0: Fp6.ONE, c1: Fp6.ZERO });
659
+ this.X_LEN = X_LEN;
660
+ this.finalExponentiate = (num) => {
661
+ const copy2 = ({ c0, c1 }) => Object.freeze({ c0, c1 });
662
+ const copy6 = ({ c0, c1, c2 }) => Object.freeze({ c0: copy2(c0), c1: copy2(c1), c2: copy2(c2) });
663
+ // This config hook is trusted to return a canonical Fp12 value already.
664
+ // Copy+freeze it to keep the tower immutability invariant without mutating caller objects.
665
+ const res = Fp12finalExponentiate(num);
666
+ return Object.freeze({ c0: copy6(res.c0), c1: copy6(res.c1) });
667
+ };
668
+ Object.freeze(this);
669
+ }
670
+ // Keep the degree-12 Frobenius row lazy too; after the first lookup the cached
671
+ // array is reused exactly like the old eager table.
672
+ get FROBENIUS_COEFFICIENTS() {
673
+ const frob = _FROBENIUS_COEFFICIENTS_12.get(this);
674
+ if (frob)
675
+ return frob;
676
+ const { Fp2 } = this.Fp6;
677
+ const { Fp } = Fp2;
678
+ const cache = Object.freeze(calcFrobeniusCoefficients(Fp2, Fp2.NONRESIDUE, Fp.ORDER, 12, 1, 6)[0]);
679
+ _FROBENIUS_COEFFICIENTS_12.set(this, cache);
680
+ return cache;
681
+ }
682
+ create(num) {
683
+ const { Fp6 } = this;
684
+ const c0 = Fp6.create(num.c0);
685
+ const c1 = Fp6.create(num.c1);
686
+ return Object.freeze({ c0, c1 });
687
+ }
688
+ isValid(num) {
689
+ if (!isObj(num))
690
+ throw new TypeError('invalid field element: expected object, got ' + typeof num);
691
+ const { c0, c1 } = num;
692
+ const { Fp6 } = this;
693
+ return Fp6.isValid(c0) && Fp6.isValid(c1);
694
+ }
695
+ is0(num) {
696
+ if (!isObj(num))
697
+ return false;
698
+ const { c0, c1 } = num;
699
+ const { Fp6 } = this;
700
+ return Fp6.is0(c0) && Fp6.is0(c1);
701
+ }
702
+ isValidNot0(num) {
703
+ return !this.is0(num) && this.isValid(num);
704
+ }
705
+ neg({ c0, c1 }) {
706
+ const { Fp6 } = this;
707
+ return Object.freeze({ c0: Fp6.neg(c0), c1: Fp6.neg(c1) });
708
+ }
709
+ eql({ c0, c1 }, { c0: r0, c1: r1 }) {
710
+ const { Fp6 } = this;
711
+ return Fp6.eql(c0, r0) && Fp6.eql(c1, r1);
712
+ }
713
+ sqrt(_) {
714
+ // Fp12 is quadratic over Fp6, so a dedicated quadratic-extension sqrt is possible here
715
+ // once Fp6.sqrt() exists. Without that lower-level sqrt, only a field-generic
716
+ // Tonelli-Shanks path over Fp12 itself would work.
717
+ return (0, utils_1.notImplemented)();
718
+ }
719
+ inv({ c0, c1 }) {
720
+ const { Fp6 } = this;
721
+ let t = Fp6.inv(Fp6.sub(Fp6.sqr(c0), Fp6.mulByNonresidue(Fp6.sqr(c1)))); // 1 / (c0² - c1² * v)
722
+ // ((C0 * T) * T) + (-C1 * T) * w
723
+ return Object.freeze({ c0: Fp6.mul(c0, t), c1: Fp6.neg(Fp6.mul(c1, t)) });
724
+ }
725
+ div(lhs, rhs) {
726
+ const { Fp6 } = this;
727
+ const { Fp2 } = Fp6;
728
+ const { Fp } = Fp2;
729
+ return this.mul(lhs, typeof rhs === 'bigint' ? Fp.inv(Fp.create(rhs)) : this.inv(rhs));
730
+ }
731
+ pow(num, power) {
732
+ return mod.FpPow(this, num, power);
733
+ }
734
+ invertBatch(nums) {
735
+ return mod.FpInvertBatch(this, nums);
736
+ }
737
+ // Normalized
738
+ add({ c0, c1 }, { c0: r0, c1: r1 }) {
739
+ const { Fp6 } = this;
740
+ return Object.freeze({
741
+ c0: Fp6.add(c0, r0),
742
+ c1: Fp6.add(c1, r1),
743
+ });
744
+ }
745
+ sub({ c0, c1 }, { c0: r0, c1: r1 }) {
746
+ const { Fp6 } = this;
747
+ return Object.freeze({
748
+ c0: Fp6.sub(c0, r0),
749
+ c1: Fp6.sub(c1, r1),
750
+ });
751
+ }
752
+ mul({ c0, c1 }, rhs) {
753
+ const { Fp6 } = this;
754
+ if (typeof rhs === 'bigint')
755
+ return Object.freeze({ c0: Fp6.mul(c0, rhs), c1: Fp6.mul(c1, rhs) });
756
+ let { c0: r0, c1: r1 } = rhs;
757
+ let t1 = Fp6.mul(c0, r0); // c0 * r0
758
+ let t2 = Fp6.mul(c1, r1); // c1 * r1
759
+ return Object.freeze({
760
+ c0: Fp6.add(t1, Fp6.mulByNonresidue(t2)), // T1 + T2 * v
761
+ // (c0 + c1) * (r0 + r1) - (T1 + T2)
762
+ c1: Fp6.sub(Fp6.mul(Fp6.add(c0, c1), Fp6.add(r0, r1)), Fp6.add(t1, t2)),
763
+ });
764
+ }
765
+ sqr({ c0, c1 }) {
766
+ const { Fp6 } = this;
767
+ let ab = Fp6.mul(c0, c1); // c0 * c1
768
+ return Object.freeze({
769
+ // (c1 * v + c0) * (c0 + c1) - AB - AB * v
770
+ c0: Fp6.sub(Fp6.sub(Fp6.mul(Fp6.add(Fp6.mulByNonresidue(c1), c0), Fp6.add(c0, c1)), ab), Fp6.mulByNonresidue(ab)),
771
+ c1: Fp6.add(ab, ab),
772
+ }); // AB + AB
773
+ }
774
+ // NonNormalized stuff
775
+ addN(a, b) {
776
+ return this.add(a, b);
777
+ }
778
+ subN(a, b) {
779
+ return this.sub(a, b);
780
+ }
781
+ mulN(a, b) {
782
+ return this.mul(a, b);
783
+ }
784
+ sqrN(a) {
785
+ return this.sqr(a);
786
+ }
787
+ // Bytes utils
788
+ fromBytes(b) {
789
+ const { Fp6 } = this;
790
+ (0, utils_1.abytes)(b);
791
+ if (b.length !== this.BYTES)
792
+ throw new Error('fromBytes invalid length=' + b.length);
793
+ return this.create({
794
+ c0: Fp6.fromBytes(b.subarray(0, Fp6.BYTES)),
795
+ c1: Fp6.fromBytes(b.subarray(Fp6.BYTES)),
796
+ });
797
+ }
798
+ toBytes({ c0, c1 }) {
799
+ const { Fp6 } = this;
800
+ return (0, utils_1.concatBytes)(Fp6.toBytes(c0), Fp6.toBytes(c1));
801
+ }
802
+ cmov({ c0, c1 }, { c0: r0, c1: r1 }, c) {
803
+ const { Fp6 } = this;
804
+ return this.create({
805
+ c0: Fp6.cmov(c0, r0, c),
806
+ c1: Fp6.cmov(c1, r1, c),
807
+ });
808
+ }
809
+ // Utils
810
+ // toString() {
811
+ // return '' + 'Fp12(' + this.c0 + this.c1 + '* w');
812
+ // },
813
+ // fromTuple(c: [Fp6, Fp6]) {
814
+ // return new Fp12(...c);
815
+ // }
816
+ fromBigTwelve(tuple) {
817
+ const { Fp6 } = this;
818
+ if (!Array.isArray(tuple) || tuple.length !== 12)
819
+ throw new Error('invalid Fp12.fromBigTwelve');
820
+ for (let i = 0; i < 12; i++)
821
+ if (typeof tuple[i] !== 'bigint')
822
+ throw new Error('invalid Fp12.fromBigTwelve');
823
+ const t = tuple;
824
+ return this.create({
825
+ c0: Fp6.fromBigSix(t.slice(0, 6)),
826
+ c1: Fp6.fromBigSix(t.slice(6, 12)),
827
+ });
828
+ }
829
+ // Raises to q**i -th power
830
+ frobeniusMap(lhs, power) {
831
+ const { Fp6 } = this;
832
+ const { Fp2 } = Fp6;
833
+ const { c0, c1, c2 } = Fp6.frobeniusMap(lhs.c1, power);
834
+ const coeff = this.FROBENIUS_COEFFICIENTS[power % 12];
835
+ return Object.freeze({
836
+ c0: Fp6.frobeniusMap(lhs.c0, power),
837
+ c1: Object.freeze({
838
+ c0: Fp2.mul(c0, coeff),
839
+ c1: Fp2.mul(c1, coeff),
840
+ c2: Fp2.mul(c2, coeff),
841
+ }),
842
+ });
843
+ }
844
+ mulByFp2({ c0, c1 }, rhs) {
845
+ const { Fp6 } = this;
846
+ return Object.freeze({
847
+ c0: Fp6.mulByFp2(c0, rhs),
848
+ c1: Fp6.mulByFp2(c1, rhs),
849
+ });
850
+ }
851
+ conjugate({ c0, c1 }) {
852
+ // Reuse `c0` by reference and only negate the `w` coefficient.
853
+ return Object.freeze({ c0, c1: this.Fp6.neg(c1) });
854
+ }
855
+ // Sparse multiplication
856
+ mul014({ c0, c1 }, o0, o1, o4) {
857
+ const { Fp6 } = this;
858
+ const { Fp2 } = Fp6;
859
+ let t0 = Fp6.mul01(c0, o0, o1);
860
+ let t1 = Fp6.mul1(c1, o4);
861
+ return Object.freeze({
862
+ c0: Fp6.add(Fp6.mulByNonresidue(t1), t0), // T1 * v + T0
863
+ // (c1 + c0) * [o0, o1+o4] - T0 - T1
864
+ c1: Fp6.sub(Fp6.sub(Fp6.mul01(Fp6.add(c1, c0), o0, Fp2.add(o1, o4)), t0), t1),
865
+ });
866
+ }
867
+ mul034({ c0, c1 }, o0, o3, o4) {
868
+ const { Fp6 } = this;
869
+ const { Fp2 } = Fp6;
870
+ const a = Object.freeze({
871
+ c0: Fp2.mul(c0.c0, o0),
872
+ c1: Fp2.mul(c0.c1, o0),
873
+ c2: Fp2.mul(c0.c2, o0),
874
+ });
875
+ const b = Fp6.mul01(c1, o3, o4);
876
+ const e = Fp6.mul01(Fp6.add(c0, c1), Fp2.add(o0, o3), o4);
877
+ return Object.freeze({
878
+ c0: Fp6.add(Fp6.mulByNonresidue(b), a),
879
+ c1: Fp6.sub(e, Fp6.add(a, b)),
880
+ });
881
+ }
882
+ // A cyclotomic group is a subgroup of Fp^n defined by
883
+ // GΦₙ(p) = {α ∈ Fpⁿ : α^Φₙ(p) = 1}
884
+ // The result of any pairing is in a cyclotomic subgroup
885
+ // https://eprint.iacr.org/2009/565.pdf
886
+ // https://eprint.iacr.org/2010/354.pdf
887
+ _cyclotomicSquare({ c0, c1 }) {
888
+ const { Fp6 } = this;
889
+ const { Fp2 } = Fp6;
890
+ const { c0: c0c0, c1: c0c1, c2: c0c2 } = c0;
891
+ const { c0: c1c0, c1: c1c1, c2: c1c2 } = c1;
892
+ const { first: t3, second: t4 } = Fp2.Fp4Square(c0c0, c1c1);
893
+ const { first: t5, second: t6 } = Fp2.Fp4Square(c1c0, c0c2);
894
+ const { first: t7, second: t8 } = Fp2.Fp4Square(c0c1, c1c2);
895
+ const t9 = Fp2.mulByNonresidue(t8); // T8 * (u + 1)
896
+ return Object.freeze({
897
+ c0: Object.freeze({
898
+ c0: Fp2.add(Fp2.mul(Fp2.sub(t3, c0c0), _2n), t3), // 2 * (T3 - c0c0) + T3
899
+ c1: Fp2.add(Fp2.mul(Fp2.sub(t5, c0c1), _2n), t5), // 2 * (T5 - c0c1) + T5
900
+ c2: Fp2.add(Fp2.mul(Fp2.sub(t7, c0c2), _2n), t7),
901
+ }), // 2 * (T7 - c0c2) + T7
902
+ c1: Object.freeze({
903
+ c0: Fp2.add(Fp2.mul(Fp2.add(t9, c1c0), _2n), t9), // 2 * (T9 + c1c0) + T9
904
+ c1: Fp2.add(Fp2.mul(Fp2.add(t4, c1c1), _2n), t4), // 2 * (T4 + c1c1) + T4
905
+ c2: Fp2.add(Fp2.mul(Fp2.add(t6, c1c2), _2n), t6),
906
+ }),
907
+ }); // 2 * (T6 + c1c2) + T6
908
+ }
909
+ // https://eprint.iacr.org/2009/565.pdf
910
+ _cyclotomicExp(num, n) {
911
+ // The loop only consumes `X_LEN` bits, so out-of-range exponents would otherwise get silently
912
+ // truncated (or sign-extended for negatives) instead of matching the caller's requested power.
913
+ (0, utils_1.aInRange)('cyclotomic exponent', n, _0n, _1n << BigInt(this.X_LEN));
914
+ let z = this.ONE;
915
+ for (let i = this.X_LEN - 1; i >= 0; i--) {
916
+ z = this._cyclotomicSquare(z);
917
+ if ((0, utils_1.bitGet)(n, i))
918
+ z = this.mul(z, num);
919
+ }
920
+ return z;
921
+ }
922
+ }
923
+ const _FROBENIUS_COEFFICIENTS_12 = new WeakMap();
924
+ /**
925
+ * @param opts - Tower construction options. See {@link Tower12Opts}.
926
+ * @returns BLS tower fields.
927
+ * @throws If the tower options or derived Frobenius helpers are invalid. {@link Error}
928
+ * @example
929
+ * Construct the Fp2/Fp6/Fp12 tower used by a pairing-friendly curve.
930
+ *
931
+ * ```ts
932
+ * const fields = tower12({
933
+ * ORDER: 17n,
934
+ * X_LEN: 4,
935
+ * FP2_NONRESIDUE: [1n, 1n],
936
+ * Fp2mulByB: (num) => num,
937
+ * Fp12finalExponentiate: (num) => num,
938
+ * });
939
+ * const fp12 = fields.Fp12.ONE;
940
+ * ```
941
+ */
942
+ function tower12(opts) {
943
+ (0, utils_1.validateObject)(opts, {
944
+ ORDER: 'bigint',
945
+ X_LEN: 'number',
946
+ FP2_NONRESIDUE: 'object',
947
+ Fp2mulByB: 'function',
948
+ Fp12finalExponentiate: 'function',
949
+ }, { NONRESIDUE: 'bigint' });
950
+ (0, utils_1.asafenumber)(opts.X_LEN, 'X_LEN');
951
+ if (opts.X_LEN < 1)
952
+ throw new Error('invalid X_LEN');
953
+ const nonresidue = opts.FP2_NONRESIDUE;
954
+ if (!Array.isArray(nonresidue) || nonresidue.length !== 2)
955
+ throw new Error('invalid FP2_NONRESIDUE');
956
+ if (typeof nonresidue[0] !== 'bigint' || typeof nonresidue[1] !== 'bigint')
957
+ throw new Error('invalid FP2_NONRESIDUE');
958
+ const Fp = mod.Field(opts.ORDER);
959
+ const Fp2 = new _Field2(Fp, opts);
960
+ const Fp6 = new _Field6(Fp2);
961
+ const Fp12 = new _Field12(Fp6, opts);
962
+ return { Fp, Fp2, Fp6, Fp12 };
963
+ }