@ohos-ports/crypto 0.0.0-stage → 0.3.1-beta.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 (121) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +60 -2
  3. package/dist/bech32.d.ts +53 -0
  4. package/dist/bech32.js +187 -0
  5. package/dist/blake2b.d.ts +7 -0
  6. package/dist/blake2b.js +213 -0
  7. package/dist/bls12_318.d.ts +48 -0
  8. package/dist/bls12_318.js +268 -0
  9. package/dist/ed25519.d.ts +55 -0
  10. package/dist/ed25519.js +721 -0
  11. package/dist/hasGlobalWebCrypto.d.ts +1 -0
  12. package/dist/hasGlobalWebCrypto.js +21 -0
  13. package/dist/hmacSHA512.d.ts +6 -0
  14. package/dist/hmacSHA512.js +14 -0
  15. package/dist/index.d.ts +14 -0
  16. package/dist/index.js +30 -0
  17. package/dist/keccak.d.ts +4 -0
  18. package/dist/keccak.js +43 -0
  19. package/dist/kes/constants.d.ts +2 -0
  20. package/dist/kes/constants.js +5 -0
  21. package/dist/kes/index.d.ts +0 -0
  22. package/dist/kes/index.js +0 -0
  23. package/dist/kes/macro/sum_kes.d.ts +15 -0
  24. package/dist/kes/macro/sum_kes.js +9 -0
  25. package/dist/kes/singleKes.d.ts +0 -0
  26. package/dist/kes/singleKes.js +0 -0
  27. package/dist/noble/_assert.d.ts +23 -0
  28. package/dist/noble/_assert.js +55 -0
  29. package/dist/noble/_md.d.ts +29 -0
  30. package/dist/noble/_md.js +170 -0
  31. package/dist/noble/_shortw_utils.d.ts +61 -0
  32. package/dist/noble/_shortw_utils.js +63 -0
  33. package/dist/noble/_u64.d.ts +28 -0
  34. package/dist/noble/_u64.js +116 -0
  35. package/dist/noble/abstract/bls.d.ts +119 -0
  36. package/dist/noble/abstract/bls.js +334 -0
  37. package/dist/noble/abstract/curve.d.ts +83 -0
  38. package/dist/noble/abstract/curve.js +319 -0
  39. package/dist/noble/abstract/edwards.d.ts +85 -0
  40. package/dist/noble/abstract/edwards.js +508 -0
  41. package/dist/noble/abstract/hash-to-curve.d.ts +57 -0
  42. package/dist/noble/abstract/hash-to-curve.js +220 -0
  43. package/dist/noble/abstract/modular.d.ts +122 -0
  44. package/dist/noble/abstract/modular.js +455 -0
  45. package/dist/noble/abstract/montgomery.d.ts +28 -0
  46. package/dist/noble/abstract/montgomery.js +172 -0
  47. package/dist/noble/abstract/utils.d.ts +106 -0
  48. package/dist/noble/abstract/utils.js +442 -0
  49. package/dist/noble/bls12_381.d.ts +95 -0
  50. package/dist/noble/bls12_381.js +1434 -0
  51. package/dist/noble/crypto.d.ts +1 -0
  52. package/dist/noble/crypto.js +4 -0
  53. package/dist/noble/curves/curve.d.ts +69 -0
  54. package/dist/noble/curves/curve.js +167 -0
  55. package/dist/noble/curves/modular.d.ts +122 -0
  56. package/dist/noble/curves/modular.js +442 -0
  57. package/dist/noble/curves/utils.d.ts +93 -0
  58. package/dist/noble/curves/utils.js +385 -0
  59. package/dist/noble/curves/weierstrass.d.ts +240 -0
  60. package/dist/noble/curves/weierstrass.js +1178 -0
  61. package/dist/noble/ed25519.d.ts +100 -0
  62. package/dist/noble/ed25519.js +494 -0
  63. package/dist/noble/hmac.d.ts +25 -0
  64. package/dist/noble/hmac.js +101 -0
  65. package/dist/noble/index.d.ts +2 -0
  66. package/dist/noble/index.js +20 -0
  67. package/dist/noble/keccak.d.ts +58 -0
  68. package/dist/noble/keccak.js +289 -0
  69. package/dist/noble/ripemd_160.d.ts +20 -0
  70. package/dist/noble/ripemd_160.js +147 -0
  71. package/dist/noble/secp256k1.d.ts +98 -0
  72. package/dist/noble/secp256k1.js +309 -0
  73. package/dist/noble/sha256.d.ts +34 -0
  74. package/dist/noble/sha256.js +146 -0
  75. package/dist/noble/sha512.d.ts +66 -0
  76. package/dist/noble/sha512.js +301 -0
  77. package/dist/noble/utils.d.ts +90 -0
  78. package/dist/noble/utils.js +278 -0
  79. package/dist/ripemd_160.d.ts +1 -0
  80. package/dist/ripemd_160.js +23 -0
  81. package/dist/rust_vrf_reimpl/curves.d.ts +162 -0
  82. package/dist/rust_vrf_reimpl/curves.js +4225 -0
  83. package/dist/rust_vrf_reimpl/vrf03.d.ts +60 -0
  84. package/dist/rust_vrf_reimpl/vrf03.js +373 -0
  85. package/dist/rust_vrf_reimpl/vrf10.d.ts +73 -0
  86. package/dist/rust_vrf_reimpl/vrf10.js +468 -0
  87. package/dist/rust_vrf_reimpl/vrf10_batch.d.ts +53 -0
  88. package/dist/rust_vrf_reimpl/vrf10_batch.js +245 -0
  89. package/dist/secp256k1.d.ts +4 -0
  90. package/dist/secp256k1.js +33 -0
  91. package/dist/sha2_256.d.ts +16 -0
  92. package/dist/sha2_256.js +403 -0
  93. package/dist/sha2_512.d.ts +16 -0
  94. package/dist/sha2_512.js +256 -0
  95. package/dist/sha3.d.ts +17 -0
  96. package/dist/sha3.js +181 -0
  97. package/dist/types.d.ts +26 -0
  98. package/dist/types.js +120 -0
  99. package/dist/utils/assert.d.ts +3 -0
  100. package/dist/utils/assert.js +55 -0
  101. package/dist/utils/base32.d.ts +5 -0
  102. package/dist/utils/base32.js +52 -0
  103. package/dist/utils/bigintToBuffer.d.ts +1 -0
  104. package/dist/utils/bigintToBuffer.js +27 -0
  105. package/dist/utils/bytesToHex.d.ts +2 -0
  106. package/dist/utils/bytesToHex.js +15 -0
  107. package/dist/utils/getRandomValues.d.ts +4 -0
  108. package/dist/utils/getRandomValues.js +12 -0
  109. package/dist/utils/hexToBytes.d.ts +2 -0
  110. package/dist/utils/hexToBytes.js +14 -0
  111. package/dist/utils/index.d.ts +3 -0
  112. package/dist/utils/index.js +19 -0
  113. package/dist/utils/positiveMod.d.ts +1 -0
  114. package/dist/utils/positiveMod.js +11 -0
  115. package/dist/utils/promisifySync.d.ts +0 -0
  116. package/dist/utils/promisifySync.js +0 -0
  117. package/dist/utils/rotr32.d.ts +2 -0
  118. package/dist/utils/rotr32.js +7 -0
  119. package/dist/vrf.d.ts +74 -0
  120. package/dist/vrf.js +286 -0
  121. package/package.json +49 -4
@@ -0,0 +1,1434 @@
1
+ "use strict";
2
+ /*
3
+ THIS IMPLEMENTATION WAS TAKEN FROM `@noble/hashes`
4
+
5
+ original source: https://github.com/paulmillr/noble-curves/blob/38a4ca1e6b0a80c60b11bbb6b3d71e3bfabd5bbb/src/bls12-381.ts#L1
6
+
7
+ it was modified and adapted to this repository
8
+
9
+ the main reason for copying over and not using as dependency is because of different target in the `tsconfig.json`
10
+
11
+ `@noble/hashes` targets es2020, which tends to cause problems with some other (older) packages sometimes
12
+
13
+ this repo targets ES5+
14
+ */
15
+ var __assign = (this && this.__assign) || function () {
16
+ __assign = Object.assign || function(t) {
17
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
18
+ s = arguments[i];
19
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20
+ t[p] = s[p];
21
+ }
22
+ return t;
23
+ };
24
+ return __assign.apply(this, arguments);
25
+ };
26
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
27
+ if (k2 === undefined) k2 = k;
28
+ var desc = Object.getOwnPropertyDescriptor(m, k);
29
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
30
+ desc = { enumerable: true, get: function() { return m[k]; } };
31
+ }
32
+ Object.defineProperty(o, k2, desc);
33
+ }) : (function(o, m, k, k2) {
34
+ if (k2 === undefined) k2 = k;
35
+ o[k2] = m[k];
36
+ }));
37
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
38
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
39
+ }) : function(o, v) {
40
+ o["default"] = v;
41
+ });
42
+ var __importStar = (this && this.__importStar) || function (mod) {
43
+ if (mod && mod.__esModule) return mod;
44
+ var result = {};
45
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
46
+ __setModuleDefault(result, mod);
47
+ return result;
48
+ };
49
+ var __read = (this && this.__read) || function (o, n) {
50
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
51
+ if (!m) return o;
52
+ var i = m.call(o), r, ar = [], e;
53
+ try {
54
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
55
+ }
56
+ catch (error) { e = { error: error }; }
57
+ finally {
58
+ try {
59
+ if (r && !r.done && (m = i["return"])) m.call(i);
60
+ }
61
+ finally { if (e) throw e.error; }
62
+ }
63
+ return ar;
64
+ };
65
+ Object.defineProperty(exports, "__esModule", { value: true });
66
+ exports.bls12_381 = exports.G2_mapToCurve = exports.G1_mapToCurve = exports.G1_formBytes = exports.parseMask = exports.Fp12 = exports.Fp12_ONE = exports.Fp12_eql = exports.Fp12_conjugate = exports.Fp12_finalExponentiate = exports.Fp12Multiply = exports.Fp2 = exports.Fp = void 0;
67
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
68
+ // bls12-381 is pairing-friendly Barreto-Lynn-Scott elliptic curve construction allowing to:
69
+ // - Construct zk-SNARKs at the 120-bit security
70
+ // - Efficiently verify N aggregate signatures with 1 pairing and N ec additions:
71
+ // the Boneh-Lynn-Shacham signature scheme is orders of magnitude more efficient than Schnorr
72
+ //
73
+ // ### Summary
74
+ // 1. BLS Relies on Bilinear Pairing (expensive)
75
+ // 2. Private Keys: 32 bytes
76
+ // 3. Public Keys: 48 bytes: 381 bit affine x coordinate, encoded into 48 big-endian bytes.
77
+ // 4. Signatures: 96 bytes: two 381 bit integers (affine x coordinate), encoded into two 48 big-endian byte arrays.
78
+ // - The signature is a point on the G2 subgroup, which is defined over a finite field
79
+ // with elements twice as big as the G1 curve (G2 is over Fp2 rather than Fp. Fp2 is analogous to the complex numbers).
80
+ // 5. The 12 stands for the Embedding degree.
81
+ //
82
+ // ### Formulas
83
+ // - `P = pk x G` - public keys
84
+ // - `S = pk x H(m)` - signing
85
+ // - `e(P, H(m)) == e(G, S)` - verification using pairings
86
+ // - `e(G, S) = e(G, SUM(n)(Si)) = MUL(n)(e(G, Si))` - signature aggregation
87
+ //
88
+ // ### Compatibility and notes
89
+ // 1. It is compatible with Algorand, Chia, Dfinity, Ethereum, Filecoin, ZEC
90
+ // Filecoin uses little endian byte arrays for private keys - make sure to reverse byte order.
91
+ // 2. Some projects use G2 for public keys and G1 for signatures. It's called "short signature"
92
+ // 3. Curve security level is about 120 bits as per Barbulescu-Duquesne 2017
93
+ // https://hal.science/hal-01534101/file/main.pdf
94
+ // 4. Compatible with specs:
95
+ // [cfrg-pairing-friendly-curves-11](https://tools.ietf.org/html/draft-irtf-cfrg-pairing-friendly-curves-11),
96
+ // [cfrg-bls-signature-05](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-bls-signature-05),
97
+ // [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380).
98
+ var bls_1 = require("./abstract/bls.js");
99
+ var mod = __importStar(require("./abstract/modular.js"));
100
+ var utils_1 = require("./abstract/utils.js");
101
+ // Types
102
+ var weierstrass_1 = require("./curves/weierstrass.js");
103
+ var hash_to_curve_1 = require("./abstract/hash-to-curve.js");
104
+ var sha256_1 = require("./sha256.js");
105
+ var utils_2 = require("./utils.js");
106
+ // Be friendly to bad ECMAScript parsers by not using bigint literals
107
+ // prettier-ignore
108
+ var _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4);
109
+ // prettier-ignore
110
+ var _8n = BigInt(8), _16n = BigInt(16);
111
+ // CURVE FIELDS
112
+ // Finite field over p.
113
+ var Fp_raw = BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab');
114
+ exports.Fp = mod.Field(Fp_raw);
115
+ // Finite field over r.
116
+ // This particular field is not used anywhere in bls12-381, but it is still useful.
117
+ var Fr = mod.Field(BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001'));
118
+ var Fp2Add = function (_a, _b) {
119
+ var c0 = _a.c0, c1 = _a.c1;
120
+ var r0 = _b.c0, r1 = _b.c1;
121
+ return ({
122
+ c0: exports.Fp.add(c0, r0),
123
+ c1: exports.Fp.add(c1, r1),
124
+ });
125
+ };
126
+ var Fp2Subtract = function (_a, _b) {
127
+ var c0 = _a.c0, c1 = _a.c1;
128
+ var r0 = _b.c0, r1 = _b.c1;
129
+ return ({
130
+ c0: exports.Fp.sub(c0, r0),
131
+ c1: exports.Fp.sub(c1, r1),
132
+ });
133
+ };
134
+ var Fp2Multiply = function (_a, rhs) {
135
+ var c0 = _a.c0, c1 = _a.c1;
136
+ if (typeof rhs === 'bigint')
137
+ return { c0: exports.Fp.mul(c0, rhs), c1: exports.Fp.mul(c1, rhs) };
138
+ // (a+bi)(c+di) = (ac−bd) + (ad+bc)i
139
+ var r0 = rhs.c0, r1 = rhs.c1;
140
+ var t1 = exports.Fp.mul(c0, r0); // c0 * o0
141
+ var t2 = exports.Fp.mul(c1, r1); // c1 * o1
142
+ // (T1 - T2) + ((c0 + c1) * (r0 + r1) - (T1 + T2))*i
143
+ var o0 = exports.Fp.sub(t1, t2);
144
+ var o1 = exports.Fp.sub(exports.Fp.mul(exports.Fp.add(c0, c1), exports.Fp.add(r0, r1)), exports.Fp.add(t1, t2));
145
+ return { c0: o0, c1: o1 };
146
+ };
147
+ var Fp2Square = function (_a) {
148
+ var c0 = _a.c0, c1 = _a.c1;
149
+ var a = exports.Fp.add(c0, c1);
150
+ var b = exports.Fp.sub(c0, c1);
151
+ var c = exports.Fp.add(c0, c0);
152
+ return { c0: exports.Fp.mul(a, b), c1: exports.Fp.mul(c, c1) };
153
+ };
154
+ // G2 is the order-q subgroup of E2(Fp²) : y² = x³+4(1+√−1),
155
+ // where Fp2 is Fp[√−1]/(x2+1). #E2(Fp2 ) = h2q, where
156
+ // G² - 1
157
+ // h2q
158
+ // NOTE: ORDER was wrong!
159
+ var FP2_ORDER = Fp_raw * Fp_raw;
160
+ exports.Fp2 = {
161
+ ORDER: FP2_ORDER,
162
+ BITS: (0, utils_1.bitLen)(FP2_ORDER),
163
+ BYTES: Math.ceil((0, utils_1.bitLen)(FP2_ORDER) / 8),
164
+ MASK: (0, utils_1.bitMask)((0, utils_1.bitLen)(FP2_ORDER)),
165
+ ZERO: { c0: exports.Fp.ZERO, c1: exports.Fp.ZERO },
166
+ ONE: { c0: exports.Fp.ONE, c1: exports.Fp.ZERO },
167
+ create: function (num) { return num; },
168
+ isValid: function (_a) {
169
+ var c0 = _a.c0, c1 = _a.c1;
170
+ return typeof c0 === 'bigint' && typeof c1 === 'bigint';
171
+ },
172
+ is0: function (_a) {
173
+ var c0 = _a.c0, c1 = _a.c1;
174
+ return exports.Fp.is0(c0) && exports.Fp.is0(c1);
175
+ },
176
+ eql: function (_a, _b) {
177
+ var c0 = _a.c0, c1 = _a.c1;
178
+ var r0 = _b.c0, r1 = _b.c1;
179
+ return exports.Fp.eql(c0, r0) && exports.Fp.eql(c1, r1);
180
+ },
181
+ neg: function (_a) {
182
+ var c0 = _a.c0, c1 = _a.c1;
183
+ return ({ c0: exports.Fp.neg(c0), c1: exports.Fp.neg(c1) });
184
+ },
185
+ pow: function (num, power) { return mod.FpPow(exports.Fp2, num, power); },
186
+ invertBatch: function (nums) { return mod.FpInvertBatch(exports.Fp2, nums); },
187
+ // Normalized
188
+ add: Fp2Add,
189
+ sub: Fp2Subtract,
190
+ mul: Fp2Multiply,
191
+ sqr: Fp2Square,
192
+ // NonNormalized stuff
193
+ addN: Fp2Add,
194
+ subN: Fp2Subtract,
195
+ mulN: Fp2Multiply,
196
+ sqrN: Fp2Square,
197
+ // Why inversion for bigint inside Fp instead of Fp2? it is even used in that context?
198
+ div: function (lhs, rhs) { return exports.Fp2.mul(lhs, typeof rhs === 'bigint' ? exports.Fp.inv(exports.Fp.create(rhs)) : exports.Fp2.inv(rhs)); },
199
+ inv: function (_a) {
200
+ var a = _a.c0, b = _a.c1;
201
+ // We wish to find the multiplicative inverse of a nonzero
202
+ // element a + bu in Fp2. We leverage an identity
203
+ //
204
+ // (a + bu)(a - bu) = a² + b²
205
+ //
206
+ // which holds because u² = -1. This can be rewritten as
207
+ //
208
+ // (a + bu)(a - bu)/(a² + b²) = 1
209
+ //
210
+ // because a² + b² = 0 has no nonzero solutions for (a, b).
211
+ // This gives that (a - bu)/(a² + b²) is the inverse
212
+ // of (a + bu). Importantly, this can be computing using
213
+ // only a single inversion in Fp.
214
+ var factor = exports.Fp.inv(exports.Fp.create(a * a + b * b));
215
+ return { c0: exports.Fp.mul(factor, exports.Fp.create(a)), c1: exports.Fp.mul(factor, exports.Fp.create(-b)) };
216
+ },
217
+ sqrt: function (num) {
218
+ if (exports.Fp2.eql(num, exports.Fp2.ZERO))
219
+ return exports.Fp2.ZERO; // Algo doesn't handles this case
220
+ // TODO: Optimize this line. It's extremely slow.
221
+ // Speeding this up would boost aggregateSignatures.
222
+ // https://eprint.iacr.org/2012/685.pdf applicable?
223
+ // https://github.com/zkcrypto/bls12_381/blob/080eaa74ec0e394377caa1ba302c8c121df08b07/src/fp2.rs#L250
224
+ // https://github.com/supranational/blst/blob/aae0c7d70b799ac269ff5edf29d8191dbd357876/src/exp2.c#L1
225
+ // Inspired by https://github.com/dalek-cryptography/curve25519-dalek/blob/17698df9d4c834204f83a3574143abacb4fc81a5/src/field.rs#L99
226
+ var candidateSqrt = exports.Fp2.pow(num, (exports.Fp2.ORDER + _8n) / _16n);
227
+ var check = exports.Fp2.div(exports.Fp2.sqr(candidateSqrt), num); // candidateSqrt.square().div(this);
228
+ var R = FP2_ROOTS_OF_UNITY;
229
+ var divisor = [R[0], R[2], R[4], R[6]].find(function (r) { return exports.Fp2.eql(r, check); });
230
+ if (!divisor)
231
+ throw new Error('No root');
232
+ var index = R.indexOf(divisor);
233
+ var root = R[index / 2];
234
+ if (!root)
235
+ throw new Error('Invalid root');
236
+ var x1 = exports.Fp2.div(candidateSqrt, root);
237
+ var x2 = exports.Fp2.neg(x1);
238
+ var _a = exports.Fp2.reim(x1), re1 = _a.re, im1 = _a.im;
239
+ var _b = exports.Fp2.reim(x2), re2 = _b.re, im2 = _b.im;
240
+ if (im1 > im2 || (im1 === im2 && re1 > re2))
241
+ return x1;
242
+ return x2;
243
+ },
244
+ // Same as sgn0_m_eq_2 in RFC 9380
245
+ isOdd: function (x) {
246
+ var _a = exports.Fp2.reim(x), x0 = _a.re, x1 = _a.im;
247
+ var sign_0 = x0 % _2n;
248
+ var zero_0 = x0 === _0n;
249
+ var sign_1 = x1 % _2n;
250
+ return BigInt(sign_0 || (zero_0 && sign_1)) == _1n;
251
+ },
252
+ // Bytes util
253
+ fromBytes: function (b) {
254
+ if (b.length !== exports.Fp2.BYTES)
255
+ throw new Error("fromBytes wrong length=".concat(b.length));
256
+ return { c0: exports.Fp.fromBytes(b.subarray(0, exports.Fp.BYTES)), c1: exports.Fp.fromBytes(b.subarray(exports.Fp.BYTES)) };
257
+ },
258
+ toBytes: function (_a) {
259
+ var c0 = _a.c0, c1 = _a.c1;
260
+ return (0, utils_1.concatBytes)(exports.Fp.toBytes(c0), exports.Fp.toBytes(c1));
261
+ },
262
+ cmov: function (_a, _b, c) {
263
+ var c0 = _a.c0, c1 = _a.c1;
264
+ var r0 = _b.c0, r1 = _b.c1;
265
+ return ({
266
+ c0: exports.Fp.cmov(c0, r0, c),
267
+ c1: exports.Fp.cmov(c1, r1, c),
268
+ });
269
+ },
270
+ // Specific utils
271
+ // toString() {
272
+ // return `Fp2(${this.c0} + ${this.c1}×i)`;
273
+ // }
274
+ reim: function (_a) {
275
+ var c0 = _a.c0, c1 = _a.c1;
276
+ return ({ re: c0, im: c1 });
277
+ },
278
+ // multiply by u + 1
279
+ mulByNonresidue: function (_a) {
280
+ var c0 = _a.c0, c1 = _a.c1;
281
+ return ({ c0: exports.Fp.sub(c0, c1), c1: exports.Fp.add(c0, c1) });
282
+ },
283
+ multiplyByB: function (_a) {
284
+ var c0 = _a.c0, c1 = _a.c1;
285
+ var t0 = exports.Fp.mul(c0, _4n); // 4 * c0
286
+ var t1 = exports.Fp.mul(c1, _4n); // 4 * c1
287
+ // (T0-T1) + (T0+T1)*i
288
+ return { c0: exports.Fp.sub(t0, t1), c1: exports.Fp.add(t0, t1) };
289
+ },
290
+ fromBigTuple: function (tuple) {
291
+ if (tuple.length !== 2)
292
+ throw new Error('Invalid tuple');
293
+ var fps = tuple.map(function (n) { return exports.Fp.create(n); });
294
+ return { c0: fps[0], c1: fps[1] };
295
+ },
296
+ frobeniusMap: function (_a, power) {
297
+ var c0 = _a.c0, c1 = _a.c1;
298
+ return ({
299
+ c0: c0,
300
+ c1: exports.Fp.mul(c1, FP2_FROBENIUS_COEFFICIENTS[power % 2]),
301
+ });
302
+ },
303
+ };
304
+ // Finite extension field over irreducible polynominal.
305
+ // Fp(u) / (u² - β) where β = -1
306
+ var FP2_FROBENIUS_COEFFICIENTS = [
307
+ BigInt('0x1'),
308
+ BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'),
309
+ ].map(function (item) { return exports.Fp.create(item); });
310
+ // For Fp2 roots of unity.
311
+ var rv1 = BigInt('0x6af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09');
312
+ // const ev1 =
313
+ // BigInt('0x699be3b8c6870965e5bf892ad5d2cc7b0e85a117402dfd83b7f4a947e02d978498255a2aaec0ac627b5afbdf1bf1c90');
314
+ // const ev2 =
315
+ // BigInt('0x8157cd83046453f5dd0972b6e3949e4288020b5b8a9cc99ca07e27089a2ce2436d965026adad3ef7baba37f2183e9b5');
316
+ // const ev3 =
317
+ // BigInt('0xab1c2ffdd6c253ca155231eb3e71ba044fd562f6f72bc5bad5ec46a0b7a3b0247cf08ce6c6317f40edbc653a72dee17');
318
+ // const ev4 =
319
+ // BigInt('0xaa404866706722864480885d68ad0ccac1967c7544b447873cc37e0181271e006df72162a3d3e0287bf597fbf7f8fc1');
320
+ // Eighth roots of unity, used for computing square roots in Fp2.
321
+ // To verify or re-calculate:
322
+ // Array(8).fill(new Fp2([1n, 1n])).map((fp2, k) => fp2.pow(Fp2.ORDER * BigInt(k) / 8n))
323
+ var FP2_ROOTS_OF_UNITY = [
324
+ [_1n, _0n],
325
+ [rv1, -rv1],
326
+ [_0n, _1n],
327
+ [rv1, rv1],
328
+ [-_1n, _0n],
329
+ [-rv1, rv1],
330
+ [_0n, -_1n],
331
+ [-rv1, -rv1],
332
+ ].map(function (pair) { return exports.Fp2.fromBigTuple(pair); });
333
+ var Fp6Add = function (_a, _b) {
334
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
335
+ var r0 = _b.c0, r1 = _b.c1, r2 = _b.c2;
336
+ return ({
337
+ c0: exports.Fp2.add(c0, r0),
338
+ c1: exports.Fp2.add(c1, r1),
339
+ c2: exports.Fp2.add(c2, r2),
340
+ });
341
+ };
342
+ var Fp6Subtract = function (_a, _b) {
343
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
344
+ var r0 = _b.c0, r1 = _b.c1, r2 = _b.c2;
345
+ return ({
346
+ c0: exports.Fp2.sub(c0, r0),
347
+ c1: exports.Fp2.sub(c1, r1),
348
+ c2: exports.Fp2.sub(c2, r2),
349
+ });
350
+ };
351
+ var Fp6Multiply = function (_a, rhs) {
352
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
353
+ if (typeof rhs === 'bigint') {
354
+ return {
355
+ c0: exports.Fp2.mul(c0, rhs),
356
+ c1: exports.Fp2.mul(c1, rhs),
357
+ c2: exports.Fp2.mul(c2, rhs),
358
+ };
359
+ }
360
+ var r0 = rhs.c0, r1 = rhs.c1, r2 = rhs.c2;
361
+ var t0 = exports.Fp2.mul(c0, r0); // c0 * o0
362
+ var t1 = exports.Fp2.mul(c1, r1); // c1 * o1
363
+ var t2 = exports.Fp2.mul(c2, r2); // c2 * o2
364
+ return {
365
+ // t0 + (c1 + c2) * (r1 * r2) - (T1 + T2) * (u + 1)
366
+ c0: exports.Fp2.add(t0, exports.Fp2.mulByNonresidue(exports.Fp2.sub(exports.Fp2.mul(exports.Fp2.add(c1, c2), exports.Fp2.add(r1, r2)), exports.Fp2.add(t1, t2)))),
367
+ // (c0 + c1) * (r0 + r1) - (T0 + T1) + T2 * (u + 1)
368
+ c1: exports.Fp2.add(exports.Fp2.sub(exports.Fp2.mul(exports.Fp2.add(c0, c1), exports.Fp2.add(r0, r1)), exports.Fp2.add(t0, t1)), exports.Fp2.mulByNonresidue(t2)),
369
+ // T1 + (c0 + c2) * (r0 + r2) - T0 + T2
370
+ c2: exports.Fp2.sub(exports.Fp2.add(t1, exports.Fp2.mul(exports.Fp2.add(c0, c2), exports.Fp2.add(r0, r2))), exports.Fp2.add(t0, t2)),
371
+ };
372
+ };
373
+ var Fp6Square = function (_a) {
374
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
375
+ var t0 = exports.Fp2.sqr(c0); // c0²
376
+ var t1 = exports.Fp2.mul(exports.Fp2.mul(c0, c1), _2n); // 2 * c0 * c1
377
+ var t3 = exports.Fp2.mul(exports.Fp2.mul(c1, c2), _2n); // 2 * c1 * c2
378
+ var t4 = exports.Fp2.sqr(c2); // c2²
379
+ return {
380
+ c0: exports.Fp2.add(exports.Fp2.mulByNonresidue(t3), t0),
381
+ c1: exports.Fp2.add(exports.Fp2.mulByNonresidue(t4), t1),
382
+ // T1 + (c0 - c1 + c2)² + T3 - T0 - T4
383
+ c2: exports.Fp2.sub(exports.Fp2.sub(exports.Fp2.add(exports.Fp2.add(t1, exports.Fp2.sqr(exports.Fp2.add(exports.Fp2.sub(c0, c1), c2))), t3), t0), t4),
384
+ };
385
+ };
386
+ var Fp6 = {
387
+ ORDER: exports.Fp2.ORDER,
388
+ BITS: 3 * exports.Fp2.BITS,
389
+ BYTES: 3 * exports.Fp2.BYTES,
390
+ MASK: (0, utils_1.bitMask)(3 * exports.Fp2.BITS),
391
+ ZERO: { c0: exports.Fp2.ZERO, c1: exports.Fp2.ZERO, c2: exports.Fp2.ZERO },
392
+ ONE: { c0: exports.Fp2.ONE, c1: exports.Fp2.ZERO, c2: exports.Fp2.ZERO },
393
+ create: function (num) { return num; },
394
+ isValid: function (_a) {
395
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
396
+ return exports.Fp2.isValid(c0) && exports.Fp2.isValid(c1) && exports.Fp2.isValid(c2);
397
+ },
398
+ is0: function (_a) {
399
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
400
+ return exports.Fp2.is0(c0) && exports.Fp2.is0(c1) && exports.Fp2.is0(c2);
401
+ },
402
+ neg: function (_a) {
403
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
404
+ return ({ c0: exports.Fp2.neg(c0), c1: exports.Fp2.neg(c1), c2: exports.Fp2.neg(c2) });
405
+ },
406
+ eql: function (_a, _b) {
407
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
408
+ var r0 = _b.c0, r1 = _b.c1, r2 = _b.c2;
409
+ return exports.Fp2.eql(c0, r0) && exports.Fp2.eql(c1, r1) && exports.Fp2.eql(c2, r2);
410
+ },
411
+ sqrt: function () {
412
+ throw new Error('Not implemented');
413
+ },
414
+ // Do we need division by bigint at all? Should be done via order:
415
+ div: function (lhs, rhs) { return Fp6.mul(lhs, typeof rhs === 'bigint' ? exports.Fp.inv(exports.Fp.create(rhs)) : Fp6.inv(rhs)); },
416
+ pow: function (num, power) { return mod.FpPow(Fp6, num, power); },
417
+ invertBatch: function (nums) { return mod.FpInvertBatch(Fp6, nums); },
418
+ // Normalized
419
+ add: Fp6Add,
420
+ sub: Fp6Subtract,
421
+ mul: Fp6Multiply,
422
+ sqr: Fp6Square,
423
+ // NonNormalized stuff
424
+ addN: Fp6Add,
425
+ subN: Fp6Subtract,
426
+ mulN: Fp6Multiply,
427
+ sqrN: Fp6Square,
428
+ inv: function (_a) {
429
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
430
+ var t0 = exports.Fp2.sub(exports.Fp2.sqr(c0), exports.Fp2.mulByNonresidue(exports.Fp2.mul(c2, c1))); // c0² - c2 * c1 * (u + 1)
431
+ var t1 = exports.Fp2.sub(exports.Fp2.mulByNonresidue(exports.Fp2.sqr(c2)), exports.Fp2.mul(c0, c1)); // c2² * (u + 1) - c0 * c1
432
+ var t2 = exports.Fp2.sub(exports.Fp2.sqr(c1), exports.Fp2.mul(c0, c2)); // c1² - c0 * c2
433
+ // 1/(((c2 * T1 + c1 * T2) * v) + c0 * T0)
434
+ var t4 = exports.Fp2.inv(exports.Fp2.add(exports.Fp2.mulByNonresidue(exports.Fp2.add(exports.Fp2.mul(c2, t1), exports.Fp2.mul(c1, t2))), exports.Fp2.mul(c0, t0)));
435
+ return { c0: exports.Fp2.mul(t4, t0), c1: exports.Fp2.mul(t4, t1), c2: exports.Fp2.mul(t4, t2) };
436
+ },
437
+ // Bytes utils
438
+ fromBytes: function (b) {
439
+ if (b.length !== Fp6.BYTES)
440
+ throw new Error("fromBytes wrong length=".concat(b.length));
441
+ return {
442
+ c0: exports.Fp2.fromBytes(b.subarray(0, exports.Fp2.BYTES)),
443
+ c1: exports.Fp2.fromBytes(b.subarray(exports.Fp2.BYTES, 2 * exports.Fp2.BYTES)),
444
+ c2: exports.Fp2.fromBytes(b.subarray(2 * exports.Fp2.BYTES)),
445
+ };
446
+ },
447
+ toBytes: function (_a) {
448
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
449
+ return (0, utils_1.concatBytes)(exports.Fp2.toBytes(c0), exports.Fp2.toBytes(c1), exports.Fp2.toBytes(c2));
450
+ },
451
+ cmov: function (_a, _b, c) {
452
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
453
+ var r0 = _b.c0, r1 = _b.c1, r2 = _b.c2;
454
+ return ({
455
+ c0: exports.Fp2.cmov(c0, r0, c),
456
+ c1: exports.Fp2.cmov(c1, r1, c),
457
+ c2: exports.Fp2.cmov(c2, r2, c),
458
+ });
459
+ },
460
+ // Utils
461
+ // fromTriple(triple: [Fp2, Fp2, Fp2]) {
462
+ // return new Fp6(...triple);
463
+ // }
464
+ // toString() {
465
+ // return `Fp6(${this.c0} + ${this.c1} * v, ${this.c2} * v^2)`;
466
+ // }
467
+ fromBigSix: function (t) {
468
+ if (!Array.isArray(t) || t.length !== 6)
469
+ throw new Error('Invalid Fp6 usage');
470
+ return {
471
+ c0: exports.Fp2.fromBigTuple(t.slice(0, 2)),
472
+ c1: exports.Fp2.fromBigTuple(t.slice(2, 4)),
473
+ c2: exports.Fp2.fromBigTuple(t.slice(4, 6)),
474
+ };
475
+ },
476
+ frobeniusMap: function (_a, power) {
477
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
478
+ return ({
479
+ c0: exports.Fp2.frobeniusMap(c0, power),
480
+ c1: exports.Fp2.mul(exports.Fp2.frobeniusMap(c1, power), FP6_FROBENIUS_COEFFICIENTS_1[power % 6]),
481
+ c2: exports.Fp2.mul(exports.Fp2.frobeniusMap(c2, power), FP6_FROBENIUS_COEFFICIENTS_2[power % 6]),
482
+ });
483
+ },
484
+ mulByNonresidue: function (_a) {
485
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
486
+ return ({ c0: exports.Fp2.mulByNonresidue(c2), c1: c0, c2: c1 });
487
+ },
488
+ // Sparse multiplication
489
+ multiplyBy1: function (_a, b1) {
490
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
491
+ return ({
492
+ c0: exports.Fp2.mulByNonresidue(exports.Fp2.mul(c2, b1)),
493
+ c1: exports.Fp2.mul(c0, b1),
494
+ c2: exports.Fp2.mul(c1, b1),
495
+ });
496
+ },
497
+ // Sparse multiplication
498
+ multiplyBy01: function (_a, b0, b1) {
499
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
500
+ var t0 = exports.Fp2.mul(c0, b0); // c0 * b0
501
+ var t1 = exports.Fp2.mul(c1, b1); // c1 * b1
502
+ return {
503
+ // ((c1 + c2) * b1 - T1) * (u + 1) + T0
504
+ c0: exports.Fp2.add(exports.Fp2.mulByNonresidue(exports.Fp2.sub(exports.Fp2.mul(exports.Fp2.add(c1, c2), b1), t1)), t0),
505
+ // (b0 + b1) * (c0 + c1) - T0 - T1
506
+ c1: exports.Fp2.sub(exports.Fp2.sub(exports.Fp2.mul(exports.Fp2.add(b0, b1), exports.Fp2.add(c0, c1)), t0), t1),
507
+ // (c0 + c2) * b0 - T0 + T1
508
+ c2: exports.Fp2.add(exports.Fp2.sub(exports.Fp2.mul(exports.Fp2.add(c0, c2), b0), t0), t1),
509
+ };
510
+ },
511
+ multiplyByFp2: function (_a, rhs) {
512
+ var c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
513
+ return ({
514
+ c0: exports.Fp2.mul(c0, rhs),
515
+ c1: exports.Fp2.mul(c1, rhs),
516
+ c2: exports.Fp2.mul(c2, rhs),
517
+ });
518
+ },
519
+ };
520
+ var FP6_FROBENIUS_COEFFICIENTS_1 = [
521
+ [BigInt('0x1'), BigInt('0x0')],
522
+ [
523
+ BigInt('0x0'),
524
+ BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'),
525
+ ],
526
+ [
527
+ BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'),
528
+ BigInt('0x0'),
529
+ ],
530
+ [BigInt('0x0'), BigInt('0x1')],
531
+ [
532
+ BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'),
533
+ BigInt('0x0'),
534
+ ],
535
+ [
536
+ BigInt('0x0'),
537
+ BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'),
538
+ ],
539
+ ].map(function (pair) { return exports.Fp2.fromBigTuple(pair); });
540
+ var FP6_FROBENIUS_COEFFICIENTS_2 = [
541
+ [BigInt('0x1'), BigInt('0x0')],
542
+ [
543
+ BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaad'),
544
+ BigInt('0x0'),
545
+ ],
546
+ [
547
+ BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'),
548
+ BigInt('0x0'),
549
+ ],
550
+ [
551
+ BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'),
552
+ BigInt('0x0'),
553
+ ],
554
+ [
555
+ BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'),
556
+ BigInt('0x0'),
557
+ ],
558
+ [
559
+ BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffff'),
560
+ BigInt('0x0'),
561
+ ],
562
+ ].map(function (pair) { return exports.Fp2.fromBigTuple(pair); });
563
+ // The BLS parameter x for BLS12-381
564
+ var BLS_X = BigInt('0xd201000000010000');
565
+ var BLS_X_LEN = (0, utils_1.bitLen)(BLS_X);
566
+ var Fp12Add = function (_a, _b) {
567
+ var c0 = _a.c0, c1 = _a.c1;
568
+ var r0 = _b.c0, r1 = _b.c1;
569
+ return ({
570
+ c0: Fp6.add(c0, r0),
571
+ c1: Fp6.add(c1, r1),
572
+ });
573
+ };
574
+ var Fp12Subtract = function (_a, _b) {
575
+ var c0 = _a.c0, c1 = _a.c1;
576
+ var r0 = _b.c0, r1 = _b.c1;
577
+ return ({
578
+ c0: Fp6.sub(c0, r0),
579
+ c1: Fp6.sub(c1, r1),
580
+ });
581
+ };
582
+ var Fp12Multiply = function (_a, rhs) {
583
+ var c0 = _a.c0, c1 = _a.c1;
584
+ if (typeof rhs === 'bigint')
585
+ return { c0: Fp6.mul(c0, rhs), c1: Fp6.mul(c1, rhs) };
586
+ var r0 = rhs.c0, r1 = rhs.c1;
587
+ var t1 = Fp6.mul(c0, r0); // c0 * r0
588
+ var t2 = Fp6.mul(c1, r1); // c1 * r1
589
+ return {
590
+ c0: Fp6.add(t1, Fp6.mulByNonresidue(t2)),
591
+ // (c0 + c1) * (r0 + r1) - (T1 + T2)
592
+ c1: Fp6.sub(Fp6.mul(Fp6.add(c0, c1), Fp6.add(r0, r1)), Fp6.add(t1, t2)),
593
+ };
594
+ };
595
+ exports.Fp12Multiply = Fp12Multiply;
596
+ var Fp12Square = function (_a) {
597
+ var c0 = _a.c0, c1 = _a.c1;
598
+ var ab = Fp6.mul(c0, c1); // c0 * c1
599
+ return {
600
+ // (c1 * v + c0) * (c0 + c1) - AB - AB * v
601
+ c0: Fp6.sub(Fp6.sub(Fp6.mul(Fp6.add(Fp6.mulByNonresidue(c1), c0), Fp6.add(c0, c1)), ab), Fp6.mulByNonresidue(ab)),
602
+ c1: Fp6.add(ab, ab),
603
+ }; // AB + AB
604
+ };
605
+ function Fp4Square(a, b) {
606
+ var a2 = exports.Fp2.sqr(a);
607
+ var b2 = exports.Fp2.sqr(b);
608
+ return {
609
+ first: exports.Fp2.add(exports.Fp2.mulByNonresidue(b2), a2),
610
+ second: exports.Fp2.sub(exports.Fp2.sub(exports.Fp2.sqr(exports.Fp2.add(a, b)), a2), b2), // (a + b)² - a² - b²
611
+ };
612
+ }
613
+ var Fp12_finalExponentiate = function (num) {
614
+ var x = BLS_X;
615
+ // this^(q⁶) / this
616
+ var t0 = exports.Fp12.div(exports.Fp12.frobeniusMap(num, 6), num);
617
+ // t0^(q²) * t0
618
+ var t1 = exports.Fp12.mul(exports.Fp12.frobeniusMap(t0, 2), t0);
619
+ var t2 = exports.Fp12.conjugate(exports.Fp12._cyclotomicExp(t1, x));
620
+ var t3 = exports.Fp12.mul(exports.Fp12.conjugate(exports.Fp12._cyclotomicSquare(t1)), t2);
621
+ var t4 = exports.Fp12.conjugate(exports.Fp12._cyclotomicExp(t3, x));
622
+ var t5 = exports.Fp12.conjugate(exports.Fp12._cyclotomicExp(t4, x));
623
+ var t6 = exports.Fp12.mul(exports.Fp12.conjugate(exports.Fp12._cyclotomicExp(t5, x)), exports.Fp12._cyclotomicSquare(t2));
624
+ var t7 = exports.Fp12.conjugate(exports.Fp12._cyclotomicExp(t6, x));
625
+ var t2_t5_pow_q2 = exports.Fp12.frobeniusMap(exports.Fp12.mul(t2, t5), 2);
626
+ var t4_t1_pow_q3 = exports.Fp12.frobeniusMap(exports.Fp12.mul(t4, t1), 3);
627
+ var t6_t1c_pow_q1 = exports.Fp12.frobeniusMap(exports.Fp12.mul(t6, exports.Fp12.conjugate(t1)), 1);
628
+ var t7_t3c_t1 = exports.Fp12.mul(exports.Fp12.mul(t7, exports.Fp12.conjugate(t3)), t1);
629
+ // (t2 * t5)^(q²) * (t4 * t1)^(q³) * (t6 * t1.conj)^(q^1) * t7 * t3.conj * t1
630
+ return exports.Fp12.mul(exports.Fp12.mul(exports.Fp12.mul(t2_t5_pow_q2, t4_t1_pow_q3), t6_t1c_pow_q1), t7_t3c_t1);
631
+ };
632
+ exports.Fp12_finalExponentiate = Fp12_finalExponentiate;
633
+ var Fp12_conjugate = function (_a) {
634
+ var c0 = _a.c0, c1 = _a.c1;
635
+ return ({ c0: c0, c1: Fp6.neg(c1) });
636
+ };
637
+ exports.Fp12_conjugate = Fp12_conjugate;
638
+ var Fp12_eql = function (_a, _b) {
639
+ var c0 = _a.c0, c1 = _a.c1;
640
+ var r0 = _b.c0, r1 = _b.c1;
641
+ return Fp6.eql(c0, r0) && Fp6.eql(c1, r1);
642
+ };
643
+ exports.Fp12_eql = Fp12_eql;
644
+ exports.Fp12_ONE = { c0: Fp6.ONE, c1: Fp6.ZERO };
645
+ exports.Fp12 = {
646
+ ORDER: exports.Fp2.ORDER,
647
+ BITS: 2 * exports.Fp2.BITS,
648
+ BYTES: 2 * exports.Fp2.BYTES,
649
+ MASK: (0, utils_1.bitMask)(2 * exports.Fp2.BITS),
650
+ ZERO: { c0: Fp6.ZERO, c1: Fp6.ZERO },
651
+ ONE: { c0: Fp6.ONE, c1: Fp6.ZERO },
652
+ create: function (num) { return num; },
653
+ isValid: function (_a) {
654
+ var c0 = _a.c0, c1 = _a.c1;
655
+ return Fp6.isValid(c0) && Fp6.isValid(c1);
656
+ },
657
+ is0: function (_a) {
658
+ var c0 = _a.c0, c1 = _a.c1;
659
+ return Fp6.is0(c0) && Fp6.is0(c1);
660
+ },
661
+ neg: function (_a) {
662
+ var c0 = _a.c0, c1 = _a.c1;
663
+ return ({ c0: Fp6.neg(c0), c1: Fp6.neg(c1) });
664
+ },
665
+ eql: exports.Fp12_eql,
666
+ sqrt: function () {
667
+ throw new Error('Not implemented');
668
+ },
669
+ inv: function (_a) {
670
+ var c0 = _a.c0, c1 = _a.c1;
671
+ var t = Fp6.inv(Fp6.sub(Fp6.sqr(c0), Fp6.mulByNonresidue(Fp6.sqr(c1)))); // 1 / (c0² - c1² * v)
672
+ return { c0: Fp6.mul(c0, t), c1: Fp6.neg(Fp6.mul(c1, t)) }; // ((C0 * T) * T) + (-C1 * T) * w
673
+ },
674
+ div: function (lhs, rhs) {
675
+ return exports.Fp12.mul(lhs, typeof rhs === 'bigint' ? exports.Fp.inv(exports.Fp.create(rhs)) : exports.Fp12.inv(rhs));
676
+ },
677
+ pow: function (num, power) { return mod.FpPow(exports.Fp12, num, power); },
678
+ invertBatch: function (nums) { return mod.FpInvertBatch(exports.Fp12, nums); },
679
+ // Normalized
680
+ add: Fp12Add,
681
+ sub: Fp12Subtract,
682
+ mul: exports.Fp12Multiply,
683
+ sqr: Fp12Square,
684
+ // NonNormalized stuff
685
+ addN: Fp12Add,
686
+ subN: Fp12Subtract,
687
+ mulN: exports.Fp12Multiply,
688
+ sqrN: Fp12Square,
689
+ // Bytes utils
690
+ fromBytes: function (b) {
691
+ if (b.length !== exports.Fp12.BYTES)
692
+ throw new Error("fromBytes wrong length=".concat(b.length));
693
+ return {
694
+ c0: Fp6.fromBytes(b.subarray(0, Fp6.BYTES)),
695
+ c1: Fp6.fromBytes(b.subarray(Fp6.BYTES)),
696
+ };
697
+ },
698
+ toBytes: function (_a) {
699
+ var c0 = _a.c0, c1 = _a.c1;
700
+ return (0, utils_1.concatBytes)(Fp6.toBytes(c0), Fp6.toBytes(c1));
701
+ },
702
+ cmov: function (_a, _b, c) {
703
+ var c0 = _a.c0, c1 = _a.c1;
704
+ var r0 = _b.c0, r1 = _b.c1;
705
+ return ({
706
+ c0: Fp6.cmov(c0, r0, c),
707
+ c1: Fp6.cmov(c1, r1, c),
708
+ });
709
+ },
710
+ // Utils
711
+ // toString() {
712
+ // return `Fp12(${this.c0} + ${this.c1} * w)`;
713
+ // },
714
+ // fromTuple(c: [Fp6, Fp6]) {
715
+ // return new Fp12(...c);
716
+ // }
717
+ fromBigTwelve: function (t) { return ({
718
+ c0: Fp6.fromBigSix(t.slice(0, 6)),
719
+ c1: Fp6.fromBigSix(t.slice(6, 12)),
720
+ }); },
721
+ // Raises to q**i -th power
722
+ frobeniusMap: function (lhs, power) {
723
+ var r0 = Fp6.frobeniusMap(lhs.c0, power);
724
+ var _a = Fp6.frobeniusMap(lhs.c1, power), c0 = _a.c0, c1 = _a.c1, c2 = _a.c2;
725
+ var coeff = FP12_FROBENIUS_COEFFICIENTS[power % 12];
726
+ return {
727
+ c0: r0,
728
+ c1: Fp6.create({
729
+ c0: exports.Fp2.mul(c0, coeff),
730
+ c1: exports.Fp2.mul(c1, coeff),
731
+ c2: exports.Fp2.mul(c2, coeff),
732
+ }),
733
+ };
734
+ },
735
+ // Sparse multiplication
736
+ multiplyBy014: function (_a, o0, o1, o4) {
737
+ var c0 = _a.c0, c1 = _a.c1;
738
+ var t0 = Fp6.multiplyBy01(c0, o0, o1);
739
+ var t1 = Fp6.multiplyBy1(c1, o4);
740
+ return {
741
+ c0: Fp6.add(Fp6.mulByNonresidue(t1), t0),
742
+ // (c1 + c0) * [o0, o1+o4] - T0 - T1
743
+ c1: Fp6.sub(Fp6.sub(Fp6.multiplyBy01(Fp6.add(c1, c0), o0, exports.Fp2.add(o1, o4)), t0), t1),
744
+ };
745
+ },
746
+ multiplyByFp2: function (_a, rhs) {
747
+ var c0 = _a.c0, c1 = _a.c1;
748
+ return ({
749
+ c0: Fp6.multiplyByFp2(c0, rhs),
750
+ c1: Fp6.multiplyByFp2(c1, rhs),
751
+ });
752
+ },
753
+ conjugate: exports.Fp12_conjugate,
754
+ // A cyclotomic group is a subgroup of Fp^n defined by
755
+ // GΦₙ(p) = {α ∈ Fpⁿ : α^Φₙ(p) = 1}
756
+ // The result of any pairing is in a cyclotomic subgroup
757
+ // https://eprint.iacr.org/2009/565.pdf
758
+ _cyclotomicSquare: function (_a) {
759
+ var c0 = _a.c0, c1 = _a.c1;
760
+ var c0c0 = c0.c0, c0c1 = c0.c1, c0c2 = c0.c2;
761
+ var c1c0 = c1.c0, c1c1 = c1.c1, c1c2 = c1.c2;
762
+ var _b = Fp4Square(c0c0, c1c1), t3 = _b.first, t4 = _b.second;
763
+ var _d = Fp4Square(c1c0, c0c2), t5 = _d.first, t6 = _d.second;
764
+ var _e = Fp4Square(c0c1, c1c2), t7 = _e.first, t8 = _e.second;
765
+ var t9 = exports.Fp2.mulByNonresidue(t8); // T8 * (u + 1)
766
+ return {
767
+ c0: Fp6.create({
768
+ c0: exports.Fp2.add(exports.Fp2.mul(exports.Fp2.sub(t3, c0c0), _2n), t3),
769
+ c1: exports.Fp2.add(exports.Fp2.mul(exports.Fp2.sub(t5, c0c1), _2n), t5),
770
+ c2: exports.Fp2.add(exports.Fp2.mul(exports.Fp2.sub(t7, c0c2), _2n), t7),
771
+ }),
772
+ c1: Fp6.create({
773
+ c0: exports.Fp2.add(exports.Fp2.mul(exports.Fp2.add(t9, c1c0), _2n), t9),
774
+ c1: exports.Fp2.add(exports.Fp2.mul(exports.Fp2.add(t4, c1c1), _2n), t4),
775
+ c2: exports.Fp2.add(exports.Fp2.mul(exports.Fp2.add(t6, c1c2), _2n), t6),
776
+ }),
777
+ }; // 2 * (T6 + c1c2) + T6
778
+ },
779
+ _cyclotomicExp: function (num, n) {
780
+ var z = exports.Fp12.ONE;
781
+ for (var i = BLS_X_LEN - 1; i >= 0; i--) {
782
+ z = exports.Fp12._cyclotomicSquare(z);
783
+ if ((0, utils_1.bitGet)(n, i))
784
+ z = exports.Fp12.mul(z, num);
785
+ }
786
+ return z;
787
+ },
788
+ // https://eprint.iacr.org/2010/354.pdf
789
+ // https://eprint.iacr.org/2009/565.pdf
790
+ finalExponentiate: exports.Fp12_finalExponentiate,
791
+ };
792
+ var FP12_FROBENIUS_COEFFICIENTS = [
793
+ [BigInt('0x1'), BigInt('0x0')],
794
+ [
795
+ BigInt('0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8'),
796
+ BigInt('0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3'),
797
+ ],
798
+ [
799
+ BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffff'),
800
+ BigInt('0x0'),
801
+ ],
802
+ [
803
+ BigInt('0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2'),
804
+ BigInt('0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09'),
805
+ ],
806
+ [
807
+ BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'),
808
+ BigInt('0x0'),
809
+ ],
810
+ [
811
+ BigInt('0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995'),
812
+ BigInt('0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116'),
813
+ ],
814
+ [
815
+ BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'),
816
+ BigInt('0x0'),
817
+ ],
818
+ [
819
+ BigInt('0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3'),
820
+ BigInt('0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8'),
821
+ ],
822
+ [
823
+ BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'),
824
+ BigInt('0x0'),
825
+ ],
826
+ [
827
+ BigInt('0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09'),
828
+ BigInt('0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2'),
829
+ ],
830
+ [
831
+ BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaad'),
832
+ BigInt('0x0'),
833
+ ],
834
+ [
835
+ BigInt('0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116'),
836
+ BigInt('0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995'),
837
+ ],
838
+ ].map(function (n) { return exports.Fp2.fromBigTuple(n); });
839
+ // END OF CURVE FIELDS
840
+ // HashToCurve
841
+ // 3-isogeny map from E' to E https://www.rfc-editor.org/rfc/rfc9380#appendix-E.3
842
+ var isogenyMapG2 = (0, hash_to_curve_1.isogenyMap)(exports.Fp2, [
843
+ // xNum
844
+ [
845
+ [
846
+ '0x5c759507e8e333ebb5b7a9a47d7ed8532c52d39fd3a042a88b58423c50ae15d5c2638e343d9c71c6238aaaaaaaa97d6',
847
+ '0x5c759507e8e333ebb5b7a9a47d7ed8532c52d39fd3a042a88b58423c50ae15d5c2638e343d9c71c6238aaaaaaaa97d6',
848
+ ],
849
+ [
850
+ '0x0',
851
+ '0x11560bf17baa99bc32126fced787c88f984f87adf7ae0c7f9a208c6b4f20a4181472aaa9cb8d555526a9ffffffffc71a',
852
+ ],
853
+ [
854
+ '0x11560bf17baa99bc32126fced787c88f984f87adf7ae0c7f9a208c6b4f20a4181472aaa9cb8d555526a9ffffffffc71e',
855
+ '0x8ab05f8bdd54cde190937e76bc3e447cc27c3d6fbd7063fcd104635a790520c0a395554e5c6aaaa9354ffffffffe38d',
856
+ ],
857
+ [
858
+ '0x171d6541fa38ccfaed6dea691f5fb614cb14b4e7f4e810aa22d6108f142b85757098e38d0f671c7188e2aaaaaaaa5ed1',
859
+ '0x0',
860
+ ],
861
+ ],
862
+ // xDen
863
+ [
864
+ [
865
+ '0x0',
866
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa63',
867
+ ],
868
+ [
869
+ '0xc',
870
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa9f',
871
+ ],
872
+ ['0x1', '0x0'], // LAST 1
873
+ ],
874
+ // yNum
875
+ [
876
+ [
877
+ '0x1530477c7ab4113b59a4c18b076d11930f7da5d4a07f649bf54439d87d27e500fc8c25ebf8c92f6812cfc71c71c6d706',
878
+ '0x1530477c7ab4113b59a4c18b076d11930f7da5d4a07f649bf54439d87d27e500fc8c25ebf8c92f6812cfc71c71c6d706',
879
+ ],
880
+ [
881
+ '0x0',
882
+ '0x5c759507e8e333ebb5b7a9a47d7ed8532c52d39fd3a042a88b58423c50ae15d5c2638e343d9c71c6238aaaaaaaa97be',
883
+ ],
884
+ [
885
+ '0x11560bf17baa99bc32126fced787c88f984f87adf7ae0c7f9a208c6b4f20a4181472aaa9cb8d555526a9ffffffffc71c',
886
+ '0x8ab05f8bdd54cde190937e76bc3e447cc27c3d6fbd7063fcd104635a790520c0a395554e5c6aaaa9354ffffffffe38f',
887
+ ],
888
+ [
889
+ '0x124c9ad43b6cf79bfbf7043de3811ad0761b0f37a1e26286b0e977c69aa274524e79097a56dc4bd9e1b371c71c718b10',
890
+ '0x0',
891
+ ],
892
+ ],
893
+ // yDen
894
+ [
895
+ [
896
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffa8fb',
897
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffa8fb',
898
+ ],
899
+ [
900
+ '0x0',
901
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffa9d3',
902
+ ],
903
+ [
904
+ '0x12',
905
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa99',
906
+ ],
907
+ ['0x1', '0x0'], // LAST 1
908
+ ],
909
+ ].map(function (i) { return i.map(function (pair) { return exports.Fp2.fromBigTuple(pair.map(BigInt)); }); }));
910
+ // 11-isogeny map from E' to E
911
+ var isogenyMapG1 = (0, hash_to_curve_1.isogenyMap)(exports.Fp, [
912
+ // xNum
913
+ [
914
+ '0x11a05f2b1e833340b809101dd99815856b303e88a2d7005ff2627b56cdb4e2c85610c2d5f2e62d6eaeac1662734649b7',
915
+ '0x17294ed3e943ab2f0588bab22147a81c7c17e75b2f6a8417f565e33c70d1e86b4838f2a6f318c356e834eef1b3cb83bb',
916
+ '0xd54005db97678ec1d1048c5d10a9a1bce032473295983e56878e501ec68e25c958c3e3d2a09729fe0179f9dac9edcb0',
917
+ '0x1778e7166fcc6db74e0609d307e55412d7f5e4656a8dbf25f1b33289f1b330835336e25ce3107193c5b388641d9b6861',
918
+ '0xe99726a3199f4436642b4b3e4118e5499db995a1257fb3f086eeb65982fac18985a286f301e77c451154ce9ac8895d9',
919
+ '0x1630c3250d7313ff01d1201bf7a74ab5db3cb17dd952799b9ed3ab9097e68f90a0870d2dcae73d19cd13c1c66f652983',
920
+ '0xd6ed6553fe44d296a3726c38ae652bfb11586264f0f8ce19008e218f9c86b2a8da25128c1052ecaddd7f225a139ed84',
921
+ '0x17b81e7701abdbe2e8743884d1117e53356de5ab275b4db1a682c62ef0f2753339b7c8f8c8f475af9ccb5618e3f0c88e',
922
+ '0x80d3cf1f9a78fc47b90b33563be990dc43b756ce79f5574a2c596c928c5d1de4fa295f296b74e956d71986a8497e317',
923
+ '0x169b1f8e1bcfa7c42e0c37515d138f22dd2ecb803a0c5c99676314baf4bb1b7fa3190b2edc0327797f241067be390c9e',
924
+ '0x10321da079ce07e272d8ec09d2565b0dfa7dccdde6787f96d50af36003b14866f69b771f8c285decca67df3f1605fb7b',
925
+ '0x6e08c248e260e70bd1e962381edee3d31d79d7e22c837bc23c0bf1bc24c6b68c24b1b80b64d391fa9c8ba2e8ba2d229',
926
+ ],
927
+ // xDen
928
+ [
929
+ '0x8ca8d548cff19ae18b2e62f4bd3fa6f01d5ef4ba35b48ba9c9588617fc8ac62b558d681be343df8993cf9fa40d21b1c',
930
+ '0x12561a5deb559c4348b4711298e536367041e8ca0cf0800c0126c2588c48bf5713daa8846cb026e9e5c8276ec82b3bff',
931
+ '0xb2962fe57a3225e8137e629bff2991f6f89416f5a718cd1fca64e00b11aceacd6a3d0967c94fedcfcc239ba5cb83e19',
932
+ '0x3425581a58ae2fec83aafef7c40eb545b08243f16b1655154cca8abc28d6fd04976d5243eecf5c4130de8938dc62cd8',
933
+ '0x13a8e162022914a80a6f1d5f43e7a07dffdfc759a12062bb8d6b44e833b306da9bd29ba81f35781d539d395b3532a21e',
934
+ '0xe7355f8e4e667b955390f7f0506c6e9395735e9ce9cad4d0a43bcef24b8982f7400d24bc4228f11c02df9a29f6304a5',
935
+ '0x772caacf16936190f3e0c63e0596721570f5799af53a1894e2e073062aede9cea73b3538f0de06cec2574496ee84a3a',
936
+ '0x14a7ac2a9d64a8b230b3f5b074cf01996e7f63c21bca68a81996e1cdf9822c580fa5b9489d11e2d311f7d99bbdcc5a5e',
937
+ '0xa10ecf6ada54f825e920b3dafc7a3cce07f8d1d7161366b74100da67f39883503826692abba43704776ec3a79a1d641',
938
+ '0x95fc13ab9e92ad4476d6e3eb3a56680f682b4ee96f7d03776df533978f31c1593174e4b4b7865002d6384d168ecdd0a',
939
+ '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001', // LAST 1
940
+ ],
941
+ // yNum
942
+ [
943
+ '0x90d97c81ba24ee0259d1f094980dcfa11ad138e48a869522b52af6c956543d3cd0c7aee9b3ba3c2be9845719707bb33',
944
+ '0x134996a104ee5811d51036d776fb46831223e96c254f383d0f906343eb67ad34d6c56711962fa8bfe097e75a2e41c696',
945
+ '0xcc786baa966e66f4a384c86a3b49942552e2d658a31ce2c344be4b91400da7d26d521628b00523b8dfe240c72de1f6',
946
+ '0x1f86376e8981c217898751ad8746757d42aa7b90eeb791c09e4a3ec03251cf9de405aba9ec61deca6355c77b0e5f4cb',
947
+ '0x8cc03fdefe0ff135caf4fe2a21529c4195536fbe3ce50b879833fd221351adc2ee7f8dc099040a841b6daecf2e8fedb',
948
+ '0x16603fca40634b6a2211e11db8f0a6a074a7d0d4afadb7bd76505c3d3ad5544e203f6326c95a807299b23ab13633a5f0',
949
+ '0x4ab0b9bcfac1bbcb2c977d027796b3ce75bb8ca2be184cb5231413c4d634f3747a87ac2460f415ec961f8855fe9d6f2',
950
+ '0x987c8d5333ab86fde9926bd2ca6c674170a05bfe3bdd81ffd038da6c26c842642f64550fedfe935a15e4ca31870fb29',
951
+ '0x9fc4018bd96684be88c9e221e4da1bb8f3abd16679dc26c1e8b6e6a1f20cabe69d65201c78607a360370e577bdba587',
952
+ '0xe1bba7a1186bdb5223abde7ada14a23c42a0ca7915af6fe06985e7ed1e4d43b9b3f7055dd4eba6f2bafaaebca731c30',
953
+ '0x19713e47937cd1be0dfd0b8f1d43fb93cd2fcbcb6caf493fd1183e416389e61031bf3a5cce3fbafce813711ad011c132',
954
+ '0x18b46a908f36f6deb918c143fed2edcc523559b8aaf0c2462e6bfe7f911f643249d9cdf41b44d606ce07c8a4d0074d8e',
955
+ '0xb182cac101b9399d155096004f53f447aa7b12a3426b08ec02710e807b4633f06c851c1919211f20d4c04f00b971ef8',
956
+ '0x245a394ad1eca9b72fc00ae7be315dc757b3b080d4c158013e6632d3c40659cc6cf90ad1c232a6442d9d3f5db980133',
957
+ '0x5c129645e44cf1102a159f748c4a3fc5e673d81d7e86568d9ab0f5d396a7ce46ba1049b6579afb7866b1e715475224b',
958
+ '0x15e6be4e990f03ce4ea50b3b42df2eb5cb181d8f84965a3957add4fa95af01b2b665027efec01c7704b456be69c8b604',
959
+ ],
960
+ // yDen
961
+ [
962
+ '0x16112c4c3a9c98b252181140fad0eae9601a6de578980be6eec3232b5be72e7a07f3688ef60c206d01479253b03663c1',
963
+ '0x1962d75c2381201e1a0cbd6c43c348b885c84ff731c4d59ca4a10356f453e01f78a4260763529e3532f6102c2e49a03d',
964
+ '0x58df3306640da276faaae7d6e8eb15778c4855551ae7f310c35a5dd279cd2eca6757cd636f96f891e2538b53dbf67f2',
965
+ '0x16b7d288798e5395f20d23bf89edb4d1d115c5dbddbcd30e123da489e726af41727364f2c28297ada8d26d98445f5416',
966
+ '0xbe0e079545f43e4b00cc912f8228ddcc6d19c9f0f69bbb0542eda0fc9dec916a20b15dc0fd2ededda39142311a5001d',
967
+ '0x8d9e5297186db2d9fb266eaac783182b70152c65550d881c5ecd87b6f0f5a6449f38db9dfa9cce202c6477faaf9b7ac',
968
+ '0x166007c08a99db2fc3ba8734ace9824b5eecfdfa8d0cf8ef5dd365bc400a0051d5fa9c01a58b1fb93d1a1399126a775c',
969
+ '0x16a3ef08be3ea7ea03bcddfabba6ff6ee5a4375efa1f4fd7feb34fd206357132b920f5b00801dee460ee415a15812ed9',
970
+ '0x1866c8ed336c61231a1be54fd1d74cc4f9fb0ce4c6af5920abc5750c4bf39b4852cfe2f7bb9248836b233d9d55535d4a',
971
+ '0x167a55cda70a6e1cea820597d94a84903216f763e13d87bb5308592e7ea7d4fbc7385ea3d529b35e346ef48bb8913f55',
972
+ '0x4d2f259eea405bd48f010a01ad2911d9c6dd039bb61a6290e591b36e636a5c871a5c29f4f83060400f8b49cba8f6aa8',
973
+ '0xaccbb67481d033ff5852c1e48c50c477f94ff8aefce42d28c0f9a88cea7913516f968986f7ebbea9684b529e2561092',
974
+ '0xad6b9514c767fe3c3613144b45f1496543346d98adf02267d5ceef9a00d9b8693000763e3b90ac11e99b138573345cc',
975
+ '0x2660400eb2e4f3b628bdd0d53cd76f2bf565b94e72927c1cb748df27942480e420517bd8714cc80d1fadc1326ed06f7',
976
+ '0xe0fa1d816ddc03e6b24255e0d7819c171c40f65e273b853324efcd6356caa205ca2f570f13497804415473a1d634b8f',
977
+ '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001', // LAST 1
978
+ ],
979
+ ].map(function (i) { return i.map(function (j) { return BigInt(j); }); }));
980
+ // SWU Map - Fp2 to G2': y² = x³ + 240i * x + 1012 + 1012i
981
+ var G2_SWU = (0, weierstrass_1.mapToCurveSimpleSWU)(exports.Fp2, {
982
+ A: exports.Fp2.create({ c0: exports.Fp.create(_0n), c1: exports.Fp.create(BigInt(240)) }),
983
+ B: exports.Fp2.create({ c0: exports.Fp.create(BigInt(1012)), c1: exports.Fp.create(BigInt(1012)) }),
984
+ Z: exports.Fp2.create({ c0: exports.Fp.create(BigInt(-2)), c1: exports.Fp.create(BigInt(-1)) }), // Z: -(2 + I)
985
+ });
986
+ // Optimized SWU Map - Fp to G1
987
+ var G1_SWU = (0, weierstrass_1.mapToCurveSimpleSWU)(exports.Fp, {
988
+ A: exports.Fp.create(BigInt('0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1d')),
989
+ B: exports.Fp.create(BigInt('0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0')),
990
+ Z: exports.Fp.create(BigInt(11)),
991
+ });
992
+ // Endomorphisms (for fast cofactor clearing)
993
+ // Ψ(P) endomorphism
994
+ var ut_root = Fp6.create({ c0: exports.Fp2.ZERO, c1: exports.Fp2.ONE, c2: exports.Fp2.ZERO });
995
+ var wsq = exports.Fp12.create({ c0: ut_root, c1: Fp6.ZERO });
996
+ var wcu = exports.Fp12.create({ c0: Fp6.ZERO, c1: ut_root });
997
+ var _a = __read(exports.Fp12.invertBatch([wsq, wcu]), 2), wsq_inv = _a[0], wcu_inv = _a[1];
998
+ function psi(x, y) {
999
+ // Untwist Fp2->Fp12 && frobenius(1) && twist back
1000
+ var x2 = exports.Fp12.mul(exports.Fp12.frobeniusMap(exports.Fp12.multiplyByFp2(wsq_inv, x), 1), wsq).c0.c0;
1001
+ var y2 = exports.Fp12.mul(exports.Fp12.frobeniusMap(exports.Fp12.multiplyByFp2(wcu_inv, y), 1), wcu).c0.c0;
1002
+ return [x2, y2];
1003
+ }
1004
+ // Ψ endomorphism
1005
+ function G2psi(c, P) {
1006
+ var affine = P.toAffine();
1007
+ var p = psi(affine.x, affine.y);
1008
+ return new c(p[0], p[1], exports.Fp2.ONE);
1009
+ }
1010
+ // Ψ²(P) endomorphism
1011
+ // 1 / F2(2)^((p-1)/3) in GF(p²)
1012
+ var PSI2_C1 = BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac');
1013
+ function psi2(x, y) {
1014
+ return [exports.Fp2.mul(x, PSI2_C1), exports.Fp2.neg(y)];
1015
+ }
1016
+ function G2psi2(c, P) {
1017
+ var affine = P.toAffine();
1018
+ var p = psi2(affine.x, affine.y);
1019
+ return new c(p[0], p[1], exports.Fp2.ONE);
1020
+ }
1021
+ // Default hash_to_field options are for hash to G2.
1022
+ //
1023
+ // Parameter definitions are in section 5.3 of the spec unless otherwise noted.
1024
+ // Parameter values come from section 8.8.2 of the spec.
1025
+ // https://www.rfc-editor.org/rfc/rfc9380#section-8.8.2
1026
+ //
1027
+ // Base field F is GF(p^m)
1028
+ // p = 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab
1029
+ // m = 2 (or 1 for G1 see section 8.8.1)
1030
+ // k = 128
1031
+ var htfDefaults = Object.freeze({
1032
+ // DST: a domain separation tag
1033
+ // defined in section 2.2.5
1034
+ // Use utils.getDSTLabel(), utils.setDSTLabel(value)
1035
+ DST: "",
1036
+ encodeDST: "",
1037
+ // p: the characteristic of F
1038
+ // where F is a finite field of characteristic p and order q = p^m
1039
+ p: exports.Fp.ORDER,
1040
+ // m: the extension degree of F, m >= 1
1041
+ // where F is a finite field of characteristic p and order q = p^m
1042
+ m: 2,
1043
+ // k: the target security level for the suite in bits
1044
+ // defined in section 5.1
1045
+ k: 128,
1046
+ // option to use a message that has already been processed by
1047
+ // expand_message_xmd
1048
+ expand: 'xmd',
1049
+ // Hash functions for: expand_message_xmd is appropriate for use with a
1050
+ // wide range of hash functions, including SHA-2, SHA-3, BLAKE2, and others.
1051
+ // BBS+ uses blake2: https://github.com/hyperledger/aries-framework-go/issues/2247
1052
+ hash: sha256_1.sha256,
1053
+ });
1054
+ // Encoding utils
1055
+ // Point on G1 curve: (x, y)
1056
+ // Compressed point of infinity
1057
+ var COMPRESSED_ZERO = setMask(exports.Fp.toBytes(_0n), { infinity: true, compressed: true }); // set compressed & point-at-infinity bits
1058
+ function parseMask(bytes) {
1059
+ // Copy, so we can remove mask data. It will be removed also later, when Fp.create will call modulo.
1060
+ // NOTE: must be a real copy even for Node Buffers (Buffer#slice returns a view, not a copy)
1061
+ bytes = Uint8Array.prototype.slice.call(bytes);
1062
+ var mask = bytes[0] & 224;
1063
+ var compressed = !!((mask >> 7) & 1); // compression bit (0b1000_0000)
1064
+ var infinity = !!((mask >> 6) & 1); // point at infinity bit (0b0100_0000)
1065
+ var sort = !!((mask >> 5) & 1); // sort bit (0b0010_0000)
1066
+ bytes[0] &= 31; // clear mask (zero first 3 bits)
1067
+ return { compressed: compressed, infinity: infinity, sort: sort, value: bytes };
1068
+ }
1069
+ exports.parseMask = parseMask;
1070
+ function setMask(bytes, mask) {
1071
+ if (bytes[0] & 224)
1072
+ throw new Error('setMask: non-empty mask');
1073
+ if (mask.compressed)
1074
+ bytes[0] |= 128;
1075
+ if (mask.infinity)
1076
+ bytes[0] |= 64;
1077
+ if (mask.sort)
1078
+ bytes[0] |= 32;
1079
+ return bytes;
1080
+ }
1081
+ function signatureG1ToRawBytes(point) {
1082
+ point.assertValidity();
1083
+ var isZero = point.equals(exports.bls12_381.G1.ProjectivePoint.ZERO);
1084
+ var _a = point.toAffine(), x = _a.x, y = _a.y;
1085
+ if (isZero)
1086
+ return COMPRESSED_ZERO.slice();
1087
+ var P = exports.Fp.ORDER;
1088
+ var sort = Boolean((y * _2n) / P);
1089
+ return setMask((0, utils_1.numberToBytesBE)(x, exports.Fp.BYTES), { compressed: true, sort: sort });
1090
+ }
1091
+ function signatureG2ToRawBytes(point) {
1092
+ // NOTE: by some reasons it was missed in bls12-381, looks like bug
1093
+ point.assertValidity();
1094
+ var len = exports.Fp.BYTES;
1095
+ if (point.equals(exports.bls12_381.G2.ProjectivePoint.ZERO))
1096
+ return (0, utils_1.concatBytes)(COMPRESSED_ZERO, (0, utils_1.numberToBytesBE)(_0n, len));
1097
+ var _a = point.toAffine(), x = _a.x, y = _a.y;
1098
+ var _b = exports.Fp2.reim(x), x0 = _b.re, x1 = _b.im;
1099
+ var _d = exports.Fp2.reim(y), y0 = _d.re, y1 = _d.im;
1100
+ var tmp = y1 > _0n ? y1 * _2n : y0 * _2n;
1101
+ var sort = Boolean((tmp / exports.Fp.ORDER) & _1n);
1102
+ var z2 = x0;
1103
+ return (0, utils_1.concatBytes)(setMask((0, utils_1.numberToBytesBE)(x1, len), { sort: sort, compressed: true }), (0, utils_1.numberToBytesBE)(z2, len));
1104
+ }
1105
+ var G1_formBytes = function (bytes) {
1106
+ var _a = parseMask(bytes), compressed = _a.compressed, infinity = _a.infinity, sort = _a.sort, value = _a.value;
1107
+ if (value.length === 48 && compressed) {
1108
+ // TODO: Fp.bytes
1109
+ var P = exports.Fp.ORDER;
1110
+ var compressedValue = (0, utils_1.bytesToNumberBE)(value);
1111
+ // Zero
1112
+ var x = exports.Fp.create(compressedValue & exports.Fp.MASK);
1113
+ if (infinity) {
1114
+ if (x !== _0n)
1115
+ throw new Error('G1: non-empty compressed point at infinity');
1116
+ return { x: _0n, y: _0n };
1117
+ }
1118
+ var right = exports.Fp.add(exports.Fp.pow(x, _3n), exports.Fp.create(exports.bls12_381.params.G1b)); // y² = x³ + b
1119
+ var y = exports.Fp.sqrt(right);
1120
+ if (!y)
1121
+ throw new Error('Invalid compressed G1 point');
1122
+ if ((y * _2n) / P !== BigInt(sort))
1123
+ y = exports.Fp.neg(y);
1124
+ return { x: exports.Fp.create(x), y: exports.Fp.create(y) };
1125
+ }
1126
+ else if (value.length === 96 && !compressed) {
1127
+ // Check if the infinity flag is set
1128
+ var x = (0, utils_1.bytesToNumberBE)(value.subarray(0, exports.Fp.BYTES));
1129
+ var y = (0, utils_1.bytesToNumberBE)(value.subarray(exports.Fp.BYTES));
1130
+ if (infinity) {
1131
+ if (x !== _0n || y !== _0n)
1132
+ throw new Error('G1: non-empty point at infinity');
1133
+ return exports.bls12_381.G1.ProjectivePoint.ZERO.toAffine();
1134
+ }
1135
+ return { x: exports.Fp.create(x), y: exports.Fp.create(y) };
1136
+ }
1137
+ else {
1138
+ throw new Error('Invalid point G1, expected 48/96 bytes');
1139
+ }
1140
+ };
1141
+ exports.G1_formBytes = G1_formBytes;
1142
+ var G1_mapToCurve = function (scalars) {
1143
+ var _a = G1_SWU(exports.Fp.create(scalars[0])), x = _a.x, y = _a.y;
1144
+ return isogenyMapG1(x, y);
1145
+ };
1146
+ exports.G1_mapToCurve = G1_mapToCurve;
1147
+ var G2_mapToCurve = function (scalars) {
1148
+ var _a = G2_SWU(exports.Fp2.fromBigTuple(scalars)), x = _a.x, y = _a.y;
1149
+ return isogenyMapG2(x, y);
1150
+ };
1151
+ exports.G2_mapToCurve = G2_mapToCurve;
1152
+ // To verify curve parameters, see pairing-friendly-curves spec:
1153
+ // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-pairing-friendly-curves-11
1154
+ // Basic math is done over finite fields over p.
1155
+ // More complicated math is done over polynominal extension fields.
1156
+ // To simplify calculations in Fp12, we construct extension tower:
1157
+ // Fp₁₂ = Fp₆² => Fp₂³
1158
+ // Fp(u) / (u² - β) where β = -1
1159
+ // Fp₂(v) / (v³ - ξ) where ξ = u + 1
1160
+ // Fp₆(w) / (w² - γ) where γ = v
1161
+ // Here goes constants && point encoding format
1162
+ exports.bls12_381 = (0, bls_1.bls)({
1163
+ // Fields
1164
+ fields: {
1165
+ Fp: exports.Fp,
1166
+ Fp2: exports.Fp2,
1167
+ Fp6: Fp6,
1168
+ Fp12: exports.Fp12,
1169
+ Fr: Fr,
1170
+ },
1171
+ // G1 is the order-q subgroup of E1(Fp) : y² = x³ + 4, #E1(Fp) = h1q, where
1172
+ // characteristic; z + (z⁴ - z² + 1)(z - 1)²/3
1173
+ G1: {
1174
+ Fp: exports.Fp,
1175
+ // cofactor; (z - 1)²/3
1176
+ h: BigInt('0x396c8c005555e1568c00aaab0000aaab'),
1177
+ // generator's coordinates
1178
+ // x = 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507
1179
+ // y = 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569
1180
+ Gx: BigInt('0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb'),
1181
+ Gy: BigInt('0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1'),
1182
+ a: exports.Fp.ZERO,
1183
+ b: _4n,
1184
+ htfDefaults: __assign(__assign({}, htfDefaults), { m: 1, DST: 'BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_' }),
1185
+ wrapPrivateKey: true,
1186
+ allowInfinityPoint: true,
1187
+ // Checks is the point resides in prime-order subgroup.
1188
+ // point.isTorsionFree() should return true for valid points
1189
+ // It returns false for shitty points.
1190
+ // https://eprint.iacr.org/2021/1130.pdf
1191
+ isTorsionFree: function (c, point) {
1192
+ // φ endomorphism
1193
+ var cubicRootOfUnityModP = BigInt('0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe');
1194
+ var phi = new c(exports.Fp.mul(point.px, cubicRootOfUnityModP), point.py, point.pz);
1195
+ // todo: unroll
1196
+ var xP = point.multiplyUnsafe(exports.bls12_381.params.x).negate(); // [x]P
1197
+ var u2P = xP.multiplyUnsafe(exports.bls12_381.params.x); // [u2]P
1198
+ return u2P.equals(phi);
1199
+ // https://eprint.iacr.org/2019/814.pdf
1200
+ // (z² − 1)/3
1201
+ // const c1 = BigInt('0x396c8c005555e1560000000055555555');
1202
+ // const P = this;
1203
+ // const S = P.sigma();
1204
+ // const Q = S.double();
1205
+ // const S2 = S.sigma();
1206
+ // // [(z² − 1)/3](2σ(P) − P − σ²(P)) − σ²(P) = O
1207
+ // const left = Q.subtract(P).subtract(S2).multiplyUnsafe(c1);
1208
+ // const C = left.subtract(S2);
1209
+ // return C.isZero();
1210
+ },
1211
+ // Clear cofactor of G1
1212
+ // https://eprint.iacr.org/2019/403
1213
+ clearCofactor: function (_c, point) {
1214
+ // return this.multiplyUnsafe(CURVE.h);
1215
+ return point.multiplyUnsafe(exports.bls12_381.params.x).add(point); // x*P + P
1216
+ },
1217
+ mapToCurve: exports.G1_mapToCurve,
1218
+ fromBytes: exports.G1_formBytes,
1219
+ toBytes: function (c, point, isCompressed) {
1220
+ var isZero = point.equals(c.ZERO);
1221
+ var _a = point.toAffine(), x = _a.x, y = _a.y;
1222
+ if (isCompressed) {
1223
+ if (isZero)
1224
+ return COMPRESSED_ZERO.slice();
1225
+ var P = exports.Fp.ORDER;
1226
+ var sort = Boolean((y * _2n) / P);
1227
+ return setMask((0, utils_1.numberToBytesBE)(x, exports.Fp.BYTES), { compressed: true, sort: sort });
1228
+ }
1229
+ else {
1230
+ if (isZero) {
1231
+ // 2x PUBLIC_KEY_LENGTH
1232
+ var x_2 = (0, utils_1.concatBytes)(new Uint8Array([0x40]), new Uint8Array(2 * exports.Fp.BYTES - 1));
1233
+ return x_2;
1234
+ }
1235
+ else {
1236
+ return (0, utils_1.concatBytes)((0, utils_1.numberToBytesBE)(x, exports.Fp.BYTES), (0, utils_1.numberToBytesBE)(y, exports.Fp.BYTES));
1237
+ }
1238
+ }
1239
+ },
1240
+ ShortSignature: {
1241
+ fromHex: function (hex) {
1242
+ var _a = parseMask((0, utils_1.ensureBytes)('signatureHex', hex, 48)), infinity = _a.infinity, sort = _a.sort, value = _a.value;
1243
+ var P = exports.Fp.ORDER;
1244
+ var compressedValue = (0, utils_1.bytesToNumberBE)(value);
1245
+ // Zero
1246
+ if (infinity)
1247
+ return exports.bls12_381.G1.ProjectivePoint.ZERO;
1248
+ var x = exports.Fp.create(compressedValue & exports.Fp.MASK);
1249
+ var right = exports.Fp.add(exports.Fp.pow(x, _3n), exports.Fp.create(exports.bls12_381.params.G1b)); // y² = x³ + b
1250
+ var y = exports.Fp.sqrt(right);
1251
+ if (!y)
1252
+ throw new Error('Invalid compressed G1 point');
1253
+ var aflag = BigInt(sort);
1254
+ if ((y * _2n) / P !== aflag)
1255
+ y = exports.Fp.neg(y);
1256
+ var point = exports.bls12_381.G1.ProjectivePoint.fromAffine({ x: x, y: y });
1257
+ point.assertValidity();
1258
+ return point;
1259
+ },
1260
+ toRawBytes: function (point) {
1261
+ return signatureG1ToRawBytes(point);
1262
+ },
1263
+ toHex: function (point) {
1264
+ return (0, utils_1.bytesToHex)(signatureG1ToRawBytes(point));
1265
+ },
1266
+ },
1267
+ },
1268
+ // G2 is the order-q subgroup of E2(Fp²) : y² = x³+4(1+√−1),
1269
+ // where Fp2 is Fp[√−1]/(x2+1). #E2(Fp2 ) = h2q, where
1270
+ // G² - 1
1271
+ // h2q
1272
+ G2: {
1273
+ Fp: exports.Fp2,
1274
+ // cofactor
1275
+ h: BigInt('0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5'),
1276
+ Gx: exports.Fp2.fromBigTuple([
1277
+ BigInt('0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8'),
1278
+ BigInt('0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e'),
1279
+ ]),
1280
+ // y =
1281
+ // 927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582,
1282
+ // 1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905
1283
+ Gy: exports.Fp2.fromBigTuple([
1284
+ BigInt('0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801'),
1285
+ BigInt('0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be'),
1286
+ ]),
1287
+ a: exports.Fp2.ZERO,
1288
+ b: exports.Fp2.fromBigTuple([_4n, _4n]),
1289
+ hEff: BigInt('0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551'),
1290
+ htfDefaults: __assign({}, htfDefaults),
1291
+ wrapPrivateKey: true,
1292
+ allowInfinityPoint: true,
1293
+ mapToCurve: exports.G2_mapToCurve,
1294
+ // Checks is the point resides in prime-order subgroup.
1295
+ // point.isTorsionFree() should return true for valid points
1296
+ // It returns false for shitty points.
1297
+ // https://eprint.iacr.org/2021/1130.pdf
1298
+ isTorsionFree: function (c, P) {
1299
+ return P.multiplyUnsafe(exports.bls12_381.params.x).negate().equals(G2psi(c, P)); // ψ(P) == [u](P)
1300
+ // Older version: https://eprint.iacr.org/2019/814.pdf
1301
+ // Ψ²(P) => Ψ³(P) => [z]Ψ³(P) where z = -x => [z]Ψ³(P) - Ψ²(P) + P == O
1302
+ // return P.psi2().psi().mulNegX().subtract(psi2).add(P).isZero();
1303
+ },
1304
+ // Maps the point into the prime-order subgroup G2.
1305
+ // clear_cofactor_bls12381_g2 from cfrg-hash-to-curve-11
1306
+ // https://eprint.iacr.org/2017/419.pdf
1307
+ // prettier-ignore
1308
+ clearCofactor: function (c, P) {
1309
+ var x = exports.bls12_381.params.x;
1310
+ var t1 = P.multiplyUnsafe(x).negate(); // [-x]P
1311
+ var t2 = G2psi(c, P); // Ψ(P)
1312
+ var t3 = P.double(); // 2P
1313
+ t3 = G2psi2(c, t3); // Ψ²(2P)
1314
+ t3 = t3.subtract(t2); // Ψ²(2P) - Ψ(P)
1315
+ t2 = t1.add(t2); // [-x]P + Ψ(P)
1316
+ t2 = t2.multiplyUnsafe(x).negate(); // [x²]P - [x]Ψ(P)
1317
+ t3 = t3.add(t2); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P)
1318
+ t3 = t3.subtract(t1); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P) + [x]P
1319
+ var Q = t3.subtract(P); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P) + [x]P - 1P
1320
+ return Q; // [x²-x-1]P + [x-1]Ψ(P) + Ψ²(2P)
1321
+ },
1322
+ fromBytes: function (bytes) {
1323
+ var _a = parseMask(bytes), compressed = _a.compressed, infinity = _a.infinity, sort = _a.sort, value = _a.value;
1324
+ if ((!compressed && !infinity && sort) || // 00100000
1325
+ (!compressed && infinity && sort) || // 01100000
1326
+ (sort && infinity && compressed) // 11100000
1327
+ ) {
1328
+ throw new Error('Invalid encoding flag: ' + (bytes[0] & 224));
1329
+ }
1330
+ var L = exports.Fp.BYTES;
1331
+ var slc = function (b, from, to) { return (0, utils_1.bytesToNumberBE)(b.slice(from, to)); };
1332
+ if (value.length === 96 && compressed) {
1333
+ var b = exports.bls12_381.params.G2b;
1334
+ var P = exports.Fp.ORDER;
1335
+ if (infinity) {
1336
+ // check that all bytes are 0
1337
+ if (value.reduce(function (p, c) { return (p !== 0 ? c + 1 : c); }, 0) > 0) {
1338
+ throw new Error('Invalid compressed G2 point');
1339
+ }
1340
+ return { x: exports.Fp2.ZERO, y: exports.Fp2.ZERO };
1341
+ }
1342
+ var x_1 = slc(value, 0, L);
1343
+ var x_0 = slc(value, L, 2 * L);
1344
+ var x = exports.Fp2.create({ c0: exports.Fp.create(x_0), c1: exports.Fp.create(x_1) });
1345
+ var right = exports.Fp2.add(exports.Fp2.pow(x, _3n), b); // y² = x³ + 4 * (u+1) = x³ + b
1346
+ var y = exports.Fp2.sqrt(right);
1347
+ var Y_bit = y.c1 === _0n ? (y.c0 * _2n) / P : (y.c1 * _2n) / P ? _1n : _0n;
1348
+ y = sort && Y_bit > 0 ? y : exports.Fp2.neg(y);
1349
+ return { x: x, y: y };
1350
+ }
1351
+ else if (value.length === 192 && !compressed) {
1352
+ if (infinity) {
1353
+ if (value.reduce(function (p, c) { return (p !== 0 ? c + 1 : c); }, 0) > 0) {
1354
+ throw new Error('Invalid uncompressed G2 point');
1355
+ }
1356
+ return { x: exports.Fp2.ZERO, y: exports.Fp2.ZERO };
1357
+ }
1358
+ var x1 = slc(value, 0, L);
1359
+ var x0 = slc(value, L, 2 * L);
1360
+ var y1 = slc(value, 2 * L, 3 * L);
1361
+ var y0 = slc(value, 3 * L, 4 * L);
1362
+ return { x: exports.Fp2.fromBigTuple([x0, x1]), y: exports.Fp2.fromBigTuple([y0, y1]) };
1363
+ }
1364
+ else {
1365
+ throw new Error('Invalid point G2, expected 96/192 bytes');
1366
+ }
1367
+ },
1368
+ toBytes: function (c, point, isCompressed) {
1369
+ var len = exports.Fp.BYTES, P = exports.Fp.ORDER;
1370
+ var isZero = point.equals(c.ZERO);
1371
+ var _a = point.toAffine(), x = _a.x, y = _a.y;
1372
+ if (isCompressed) {
1373
+ if (isZero)
1374
+ return (0, utils_1.concatBytes)(COMPRESSED_ZERO, (0, utils_1.numberToBytesBE)(_0n, len));
1375
+ var flag = Boolean(y.c1 === _0n ? (y.c0 * _2n) / P : (y.c1 * _2n) / P);
1376
+ return (0, utils_1.concatBytes)(setMask((0, utils_1.numberToBytesBE)(x.c1, len), { compressed: true, sort: flag }), (0, utils_1.numberToBytesBE)(x.c0, len));
1377
+ }
1378
+ else {
1379
+ if (isZero)
1380
+ return (0, utils_1.concatBytes)(new Uint8Array([0x40]), new Uint8Array(4 * len - 1)); // bytes[0] |= 1 << 6;
1381
+ var _b = exports.Fp2.reim(x), x0 = _b.re, x1 = _b.im;
1382
+ var _d = exports.Fp2.reim(y), y0 = _d.re, y1 = _d.im;
1383
+ return (0, utils_1.concatBytes)((0, utils_1.numberToBytesBE)(x1, len), (0, utils_1.numberToBytesBE)(x0, len), (0, utils_1.numberToBytesBE)(y1, len), (0, utils_1.numberToBytesBE)(y0, len));
1384
+ }
1385
+ },
1386
+ Signature: {
1387
+ // TODO: Optimize, it's very slow because of sqrt.
1388
+ fromHex: function (hex) {
1389
+ var _a = parseMask((0, utils_1.ensureBytes)('signatureHex', hex)), infinity = _a.infinity, sort = _a.sort, value = _a.value;
1390
+ var P = exports.Fp.ORDER;
1391
+ var half = hex.length / 2;
1392
+ if (half !== 48 && half !== 96)
1393
+ throw new Error('Invalid compressed signature length, must be 96 or 192');
1394
+ var z1 = (0, utils_1.bytesToNumberBE)(value.slice(0, half));
1395
+ var z2 = (0, utils_1.bytesToNumberBE)(value.slice(half));
1396
+ // Indicates the infinity point
1397
+ if (infinity)
1398
+ return exports.bls12_381.G2.ProjectivePoint.ZERO;
1399
+ var x1 = exports.Fp.create(z1 & exports.Fp.MASK);
1400
+ var x2 = exports.Fp.create(z2);
1401
+ var x = exports.Fp2.create({ c0: x2, c1: x1 });
1402
+ var y2 = exports.Fp2.add(exports.Fp2.pow(x, _3n), exports.bls12_381.params.G2b); // y² = x³ + 4
1403
+ // The slow part
1404
+ var y = exports.Fp2.sqrt(y2);
1405
+ if (!y)
1406
+ throw new Error('Failed to find a square root');
1407
+ // Choose the y whose leftmost bit of the imaginary part is equal to the a_flag1
1408
+ // If y1 happens to be zero, then use the bit of y0
1409
+ var _b = exports.Fp2.reim(y), y0 = _b.re, y1 = _b.im;
1410
+ var aflag1 = BigInt(sort);
1411
+ var isGreater = y1 > _0n && (y1 * _2n) / P !== aflag1;
1412
+ var isZero = y1 === _0n && (y0 * _2n) / P !== aflag1;
1413
+ if (isGreater || isZero)
1414
+ y = exports.Fp2.neg(y);
1415
+ var point = exports.bls12_381.G2.ProjectivePoint.fromAffine({ x: x, y: y });
1416
+ point.assertValidity();
1417
+ return point;
1418
+ },
1419
+ toRawBytes: function (point) {
1420
+ return signatureG2ToRawBytes(point);
1421
+ },
1422
+ toHex: function (point) {
1423
+ return (0, utils_1.bytesToHex)(signatureG2ToRawBytes(point));
1424
+ },
1425
+ },
1426
+ },
1427
+ params: {
1428
+ x: BLS_X,
1429
+ r: Fr.ORDER, // order; z⁴ − z² + 1; CURVE.n from other curves
1430
+ },
1431
+ htfDefaults: htfDefaults,
1432
+ hash: sha256_1.sha256,
1433
+ randomBytes: utils_2.randomBytes
1434
+ });