@noble/curves 0.9.0 → 0.9.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.
- package/abstract/edwards.d.ts +4 -0
- package/abstract/edwards.d.ts.map +1 -1
- package/abstract/edwards.js +8 -9
- package/abstract/edwards.js.map +1 -1
- package/abstract/modular.js +1 -1
- package/abstract/modular.js.map +1 -1
- package/abstract/weierstrass.d.ts +2 -0
- package/abstract/weierstrass.d.ts.map +1 -1
- package/abstract/weierstrass.js +17 -17
- package/abstract/weierstrass.js.map +1 -1
- package/bls12-381.d.ts.map +1 -1
- package/bls12-381.js +109 -106
- package/bls12-381.js.map +1 -1
- package/ed25519.js +1 -1
- package/ed25519.js.map +1 -1
- package/ed448.d.ts.map +1 -1
- package/ed448.js +3 -2
- package/ed448.js.map +1 -1
- package/esm/abstract/edwards.js +8 -9
- package/esm/abstract/edwards.js.map +1 -1
- package/esm/abstract/modular.js +1 -1
- package/esm/abstract/modular.js.map +1 -1
- package/esm/abstract/weierstrass.js +17 -17
- package/esm/abstract/weierstrass.js.map +1 -1
- package/esm/bls12-381.js +109 -106
- package/esm/bls12-381.js.map +1 -1
- package/esm/ed25519.js +1 -1
- package/esm/ed25519.js.map +1 -1
- package/esm/ed448.js +3 -2
- package/esm/ed448.js.map +1 -1
- package/esm/secp256k1.js +1 -1
- package/esm/secp256k1.js.map +1 -1
- package/package.json +1 -1
- package/secp256k1.js +1 -1
- package/secp256k1.js.map +1 -1
- package/src/abstract/edwards.ts +12 -9
- package/src/abstract/modular.ts +1 -1
- package/src/abstract/weierstrass.ts +18 -16
- package/src/bls12-381.ts +196 -111
- package/src/ed25519.ts +1 -1
- package/src/ed448.ts +3 -2
- package/src/secp256k1.ts +1 -1
package/esm/bls12-381.js
CHANGED
|
@@ -51,12 +51,16 @@ import { concatBytes as concatB, ensureBytes, numberToBytesBE, bytesToNumberBE,
|
|
|
51
51
|
// Types
|
|
52
52
|
import { mapToCurveSimpleSWU, } from './abstract/weierstrass.js';
|
|
53
53
|
import { isogenyMap } from './abstract/hash-to-curve.js';
|
|
54
|
+
// Be friendly to bad ECMAScript parsers by not using bigint literals
|
|
55
|
+
// prettier-ignore
|
|
56
|
+
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4);
|
|
57
|
+
const _8n = BigInt(8), _16n = BigInt(16);
|
|
54
58
|
// CURVE FIELDS
|
|
55
59
|
// Finite field over p.
|
|
56
|
-
const Fp = mod.Field(
|
|
60
|
+
const Fp = mod.Field(BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab'));
|
|
57
61
|
// Finite field over r.
|
|
58
62
|
// This particular field is not used anywhere in bls12-381, but it is still useful.
|
|
59
|
-
const Fr = mod.Field(
|
|
63
|
+
const Fr = mod.Field(BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001'));
|
|
60
64
|
const Fp2Add = ({ c0, c1 }, { c0: r0, c1: r1 }) => ({
|
|
61
65
|
c0: Fp.add(c0, r0),
|
|
62
66
|
c1: Fp.add(c1, r1),
|
|
@@ -88,8 +92,7 @@ const Fp2Square = ({ c0, c1 }) => {
|
|
|
88
92
|
// G² - 1
|
|
89
93
|
// h2q
|
|
90
94
|
// NOTE: ORDER was wrong!
|
|
91
|
-
const FP2_ORDER =
|
|
92
|
-
2n;
|
|
95
|
+
const FP2_ORDER = BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab') ** _2n;
|
|
93
96
|
const Fp2 = {
|
|
94
97
|
ORDER: FP2_ORDER,
|
|
95
98
|
BITS: bitLen(FP2_ORDER),
|
|
@@ -142,7 +145,7 @@ const Fp2 = {
|
|
|
142
145
|
// https://github.com/zkcrypto/bls12_381/blob/080eaa74ec0e394377caa1ba302c8c121df08b07/src/fp2.rs#L250
|
|
143
146
|
// https://github.com/supranational/blst/blob/aae0c7d70b799ac269ff5edf29d8191dbd357876/src/exp2.c#L1
|
|
144
147
|
// Inspired by https://github.com/dalek-cryptography/curve25519-dalek/blob/17698df9d4c834204f83a3574143abacb4fc81a5/src/field.rs#L99
|
|
145
|
-
const candidateSqrt = Fp2.pow(num, (Fp2.ORDER +
|
|
148
|
+
const candidateSqrt = Fp2.pow(num, (Fp2.ORDER + _8n) / _16n);
|
|
146
149
|
const check = Fp2.div(Fp2.sqr(candidateSqrt), num); // candidateSqrt.square().div(this);
|
|
147
150
|
const R = FP2_ROOTS_OF_UNITY;
|
|
148
151
|
const divisor = [R[0], R[2], R[4], R[6]].find((r) => Fp2.eql(r, check));
|
|
@@ -163,10 +166,10 @@ const Fp2 = {
|
|
|
163
166
|
// Same as sgn0_fp2 in draft-irtf-cfrg-hash-to-curve-16
|
|
164
167
|
isOdd: (x) => {
|
|
165
168
|
const { re: x0, im: x1 } = Fp2.reim(x);
|
|
166
|
-
const sign_0 = x0 %
|
|
167
|
-
const zero_0 = x0 ===
|
|
168
|
-
const sign_1 = x1 %
|
|
169
|
-
return BigInt(sign_0 || (zero_0 && sign_1)) ==
|
|
169
|
+
const sign_0 = x0 % _2n;
|
|
170
|
+
const zero_0 = x0 === _0n;
|
|
171
|
+
const sign_1 = x1 % _2n;
|
|
172
|
+
return BigInt(sign_0 || (zero_0 && sign_1)) == _1n;
|
|
170
173
|
},
|
|
171
174
|
// Bytes util
|
|
172
175
|
fromBytes(b) {
|
|
@@ -187,8 +190,8 @@ const Fp2 = {
|
|
|
187
190
|
// multiply by u + 1
|
|
188
191
|
mulByNonresidue: ({ c0, c1 }) => ({ c0: Fp.sub(c0, c1), c1: Fp.add(c0, c1) }),
|
|
189
192
|
multiplyByB: ({ c0, c1 }) => {
|
|
190
|
-
let t0 = Fp.mul(c0,
|
|
191
|
-
let t1 = Fp.mul(c1,
|
|
193
|
+
let t0 = Fp.mul(c0, _4n); // 4 * c0
|
|
194
|
+
let t1 = Fp.mul(c1, _4n); // 4 * c1
|
|
192
195
|
// (T0-T1) + (T0+T1)*i
|
|
193
196
|
return { c0: Fp.sub(t0, t1), c1: Fp.add(t0, t1) };
|
|
194
197
|
},
|
|
@@ -206,30 +209,30 @@ const Fp2 = {
|
|
|
206
209
|
// Finite extension field over irreducible polynominal.
|
|
207
210
|
// Fp(u) / (u² - β) where β = -1
|
|
208
211
|
const FP2_FROBENIUS_COEFFICIENTS = [
|
|
209
|
-
|
|
210
|
-
|
|
212
|
+
BigInt('0x1'),
|
|
213
|
+
BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'),
|
|
211
214
|
].map((item) => Fp.create(item));
|
|
212
215
|
// For Fp2 roots of unity.
|
|
213
|
-
const rv1 =
|
|
216
|
+
const rv1 = BigInt('0x6af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09');
|
|
214
217
|
// const ev1 =
|
|
215
|
-
//
|
|
218
|
+
// BigInt('0x699be3b8c6870965e5bf892ad5d2cc7b0e85a117402dfd83b7f4a947e02d978498255a2aaec0ac627b5afbdf1bf1c90');
|
|
216
219
|
// const ev2 =
|
|
217
|
-
//
|
|
220
|
+
// BigInt('0x8157cd83046453f5dd0972b6e3949e4288020b5b8a9cc99ca07e27089a2ce2436d965026adad3ef7baba37f2183e9b5');
|
|
218
221
|
// const ev3 =
|
|
219
|
-
//
|
|
222
|
+
// BigInt('0xab1c2ffdd6c253ca155231eb3e71ba044fd562f6f72bc5bad5ec46a0b7a3b0247cf08ce6c6317f40edbc653a72dee17');
|
|
220
223
|
// const ev4 =
|
|
221
|
-
//
|
|
224
|
+
// BigInt('0xaa404866706722864480885d68ad0ccac1967c7544b447873cc37e0181271e006df72162a3d3e0287bf597fbf7f8fc1');
|
|
222
225
|
// Eighth roots of unity, used for computing square roots in Fp2.
|
|
223
226
|
// To verify or re-calculate:
|
|
224
227
|
// Array(8).fill(new Fp2([1n, 1n])).map((fp2, k) => fp2.pow(Fp2.ORDER * BigInt(k) / 8n))
|
|
225
228
|
const FP2_ROOTS_OF_UNITY = [
|
|
226
|
-
[
|
|
229
|
+
[_1n, _0n],
|
|
227
230
|
[rv1, -rv1],
|
|
228
|
-
[
|
|
231
|
+
[_0n, _1n],
|
|
229
232
|
[rv1, rv1],
|
|
230
|
-
[-
|
|
233
|
+
[-_1n, _0n],
|
|
231
234
|
[-rv1, rv1],
|
|
232
|
-
[
|
|
235
|
+
[_0n, -_1n],
|
|
233
236
|
[-rv1, -rv1],
|
|
234
237
|
].map((pair) => Fp2.fromBigTuple(pair));
|
|
235
238
|
const Fp6Add = ({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }) => ({
|
|
@@ -265,8 +268,8 @@ const Fp6Multiply = ({ c0, c1, c2 }, rhs) => {
|
|
|
265
268
|
};
|
|
266
269
|
const Fp6Square = ({ c0, c1, c2 }) => {
|
|
267
270
|
let t0 = Fp2.sqr(c0); // c0²
|
|
268
|
-
let t1 = Fp2.mul(Fp2.mul(c0, c1),
|
|
269
|
-
let t3 = Fp2.mul(Fp2.mul(c1, c2),
|
|
271
|
+
let t1 = Fp2.mul(Fp2.mul(c0, c1), _2n); // 2 * c0 * c1
|
|
272
|
+
let t3 = Fp2.mul(Fp2.mul(c1, c2), _2n); // 2 * c1 * c2
|
|
270
273
|
let t4 = Fp2.sqr(c2); // c2²
|
|
271
274
|
return {
|
|
272
275
|
c0: Fp2.add(Fp2.mulByNonresidue(t3), t0),
|
|
@@ -376,50 +379,50 @@ const Fp6 = {
|
|
|
376
379
|
}),
|
|
377
380
|
};
|
|
378
381
|
const FP6_FROBENIUS_COEFFICIENTS_1 = [
|
|
379
|
-
[
|
|
382
|
+
[BigInt('0x1'), BigInt('0x0')],
|
|
380
383
|
[
|
|
381
|
-
|
|
382
|
-
|
|
384
|
+
BigInt('0x0'),
|
|
385
|
+
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'),
|
|
383
386
|
],
|
|
384
387
|
[
|
|
385
|
-
|
|
386
|
-
|
|
388
|
+
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'),
|
|
389
|
+
BigInt('0x0'),
|
|
387
390
|
],
|
|
388
|
-
[
|
|
391
|
+
[BigInt('0x0'), BigInt('0x1')],
|
|
389
392
|
[
|
|
390
|
-
|
|
391
|
-
|
|
393
|
+
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'),
|
|
394
|
+
BigInt('0x0'),
|
|
392
395
|
],
|
|
393
396
|
[
|
|
394
|
-
|
|
395
|
-
|
|
397
|
+
BigInt('0x0'),
|
|
398
|
+
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'),
|
|
396
399
|
],
|
|
397
400
|
].map((pair) => Fp2.fromBigTuple(pair));
|
|
398
401
|
const FP6_FROBENIUS_COEFFICIENTS_2 = [
|
|
399
|
-
[
|
|
402
|
+
[BigInt('0x1'), BigInt('0x0')],
|
|
400
403
|
[
|
|
401
|
-
|
|
402
|
-
|
|
404
|
+
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaad'),
|
|
405
|
+
BigInt('0x0'),
|
|
403
406
|
],
|
|
404
407
|
[
|
|
405
|
-
|
|
406
|
-
|
|
408
|
+
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'),
|
|
409
|
+
BigInt('0x0'),
|
|
407
410
|
],
|
|
408
411
|
[
|
|
409
|
-
|
|
410
|
-
|
|
412
|
+
BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'),
|
|
413
|
+
BigInt('0x0'),
|
|
411
414
|
],
|
|
412
415
|
[
|
|
413
|
-
|
|
414
|
-
|
|
416
|
+
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'),
|
|
417
|
+
BigInt('0x0'),
|
|
415
418
|
],
|
|
416
419
|
[
|
|
417
|
-
|
|
418
|
-
|
|
420
|
+
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffff'),
|
|
421
|
+
BigInt('0x0'),
|
|
419
422
|
],
|
|
420
423
|
].map((pair) => Fp2.fromBigTuple(pair));
|
|
421
424
|
// The BLS parameter x for BLS12-381
|
|
422
|
-
const BLS_X =
|
|
425
|
+
const BLS_X = BigInt('0xd201000000010000');
|
|
423
426
|
const BLS_X_LEN = bitLen(BLS_X);
|
|
424
427
|
const Fp12Add = ({ c0, c1 }, { c0: r0, c1: r1 }) => ({
|
|
425
428
|
c0: Fp6.add(c0, r0),
|
|
@@ -556,14 +559,14 @@ const Fp12 = {
|
|
|
556
559
|
let t9 = Fp2.mulByNonresidue(t8); // T8 * (u + 1)
|
|
557
560
|
return {
|
|
558
561
|
c0: Fp6.create({
|
|
559
|
-
c0: Fp2.add(Fp2.mul(Fp2.sub(t3, c0c0),
|
|
560
|
-
c1: Fp2.add(Fp2.mul(Fp2.sub(t5, c0c1),
|
|
561
|
-
c2: Fp2.add(Fp2.mul(Fp2.sub(t7, c0c2),
|
|
562
|
+
c0: Fp2.add(Fp2.mul(Fp2.sub(t3, c0c0), _2n), t3),
|
|
563
|
+
c1: Fp2.add(Fp2.mul(Fp2.sub(t5, c0c1), _2n), t5),
|
|
564
|
+
c2: Fp2.add(Fp2.mul(Fp2.sub(t7, c0c2), _2n), t7),
|
|
562
565
|
}),
|
|
563
566
|
c1: Fp6.create({
|
|
564
|
-
c0: Fp2.add(Fp2.mul(Fp2.add(t9, c1c0),
|
|
565
|
-
c1: Fp2.add(Fp2.mul(Fp2.add(t4, c1c1),
|
|
566
|
-
c2: Fp2.add(Fp2.mul(Fp2.add(t6, c1c2),
|
|
567
|
+
c0: Fp2.add(Fp2.mul(Fp2.add(t9, c1c0), _2n), t9),
|
|
568
|
+
c1: Fp2.add(Fp2.mul(Fp2.add(t4, c1c1), _2n), t4),
|
|
569
|
+
c2: Fp2.add(Fp2.mul(Fp2.add(t6, c1c2), _2n), t6),
|
|
567
570
|
}),
|
|
568
571
|
}; // 2 * (T6 + c1c2) + T6
|
|
569
572
|
},
|
|
@@ -599,50 +602,50 @@ const Fp12 = {
|
|
|
599
602
|
},
|
|
600
603
|
};
|
|
601
604
|
const FP12_FROBENIUS_COEFFICIENTS = [
|
|
602
|
-
[
|
|
605
|
+
[BigInt('0x1'), BigInt('0x0')],
|
|
603
606
|
[
|
|
604
|
-
|
|
605
|
-
|
|
607
|
+
BigInt('0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8'),
|
|
608
|
+
BigInt('0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3'),
|
|
606
609
|
],
|
|
607
610
|
[
|
|
608
|
-
|
|
609
|
-
|
|
611
|
+
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffff'),
|
|
612
|
+
BigInt('0x0'),
|
|
610
613
|
],
|
|
611
614
|
[
|
|
612
|
-
|
|
613
|
-
|
|
615
|
+
BigInt('0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2'),
|
|
616
|
+
BigInt('0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09'),
|
|
614
617
|
],
|
|
615
618
|
[
|
|
616
|
-
|
|
617
|
-
|
|
619
|
+
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'),
|
|
620
|
+
BigInt('0x0'),
|
|
618
621
|
],
|
|
619
622
|
[
|
|
620
|
-
|
|
621
|
-
|
|
623
|
+
BigInt('0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995'),
|
|
624
|
+
BigInt('0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116'),
|
|
622
625
|
],
|
|
623
626
|
[
|
|
624
|
-
|
|
625
|
-
|
|
627
|
+
BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'),
|
|
628
|
+
BigInt('0x0'),
|
|
626
629
|
],
|
|
627
630
|
[
|
|
628
|
-
|
|
629
|
-
|
|
631
|
+
BigInt('0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3'),
|
|
632
|
+
BigInt('0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8'),
|
|
630
633
|
],
|
|
631
634
|
[
|
|
632
|
-
|
|
633
|
-
|
|
635
|
+
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'),
|
|
636
|
+
BigInt('0x0'),
|
|
634
637
|
],
|
|
635
638
|
[
|
|
636
|
-
|
|
637
|
-
|
|
639
|
+
BigInt('0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09'),
|
|
640
|
+
BigInt('0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2'),
|
|
638
641
|
],
|
|
639
642
|
[
|
|
640
|
-
|
|
641
|
-
|
|
643
|
+
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaad'),
|
|
644
|
+
BigInt('0x0'),
|
|
642
645
|
],
|
|
643
646
|
[
|
|
644
|
-
|
|
645
|
-
|
|
647
|
+
BigInt('0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116'),
|
|
648
|
+
BigInt('0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995'),
|
|
646
649
|
],
|
|
647
650
|
].map((n) => Fp2.fromBigTuple(n));
|
|
648
651
|
// END OF CURVE FIELDS
|
|
@@ -789,14 +792,14 @@ const isogenyMapG1 = isogenyMap(Fp, [
|
|
|
789
792
|
].map((i) => i.map((j) => BigInt(j))));
|
|
790
793
|
// SWU Map - Fp2 to G2': y² = x³ + 240i * x + 1012 + 1012i
|
|
791
794
|
const G2_SWU = mapToCurveSimpleSWU(Fp2, {
|
|
792
|
-
A: Fp2.create({ c0: Fp.create(
|
|
795
|
+
A: Fp2.create({ c0: Fp.create(_0n), c1: Fp.create(240n) }),
|
|
793
796
|
B: Fp2.create({ c0: Fp.create(1012n), c1: Fp.create(1012n) }),
|
|
794
797
|
Z: Fp2.create({ c0: Fp.create(-2n), c1: Fp.create(-1n) }), // Z: -(2 + I)
|
|
795
798
|
});
|
|
796
799
|
// Optimized SWU Map - Fp to G1
|
|
797
800
|
const G1_SWU = mapToCurveSimpleSWU(Fp, {
|
|
798
|
-
A: Fp.create(
|
|
799
|
-
B: Fp.create(
|
|
801
|
+
A: Fp.create(BigInt('0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1d')),
|
|
802
|
+
B: Fp.create(BigInt('0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0')),
|
|
800
803
|
Z: Fp.create(11n),
|
|
801
804
|
});
|
|
802
805
|
// Endomorphisms (for fast cofactor clearing)
|
|
@@ -819,7 +822,7 @@ function G2psi(c, P) {
|
|
|
819
822
|
}
|
|
820
823
|
// Ψ²(P) endomorphism
|
|
821
824
|
// 1 / F2(2)^((p-1)/3) in GF(p²)
|
|
822
|
-
const PSI2_C1 =
|
|
825
|
+
const PSI2_C1 = BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac');
|
|
823
826
|
function psi2(x, y) {
|
|
824
827
|
return [Fp2.mul(x, PSI2_C1), Fp2.neg(y)];
|
|
825
828
|
}
|
|
@@ -892,14 +895,14 @@ export const bls12_381 = bls({
|
|
|
892
895
|
G1: {
|
|
893
896
|
Fp,
|
|
894
897
|
// cofactor; (z - 1)²/3
|
|
895
|
-
h:
|
|
898
|
+
h: BigInt('0x396c8c005555e1568c00aaab0000aaab'),
|
|
896
899
|
// generator's coordinates
|
|
897
900
|
// x = 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507
|
|
898
901
|
// y = 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569
|
|
899
|
-
Gx:
|
|
900
|
-
Gy:
|
|
902
|
+
Gx: BigInt('0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb'),
|
|
903
|
+
Gy: BigInt('0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1'),
|
|
901
904
|
a: Fp.ZERO,
|
|
902
|
-
b:
|
|
905
|
+
b: _4n,
|
|
903
906
|
htfDefaults: { ...htfDefaults, m: 1 },
|
|
904
907
|
wrapPrivateKey: true,
|
|
905
908
|
allowInfinityPoint: true,
|
|
@@ -909,7 +912,7 @@ export const bls12_381 = bls({
|
|
|
909
912
|
// https://eprint.iacr.org/2021/1130.pdf
|
|
910
913
|
isTorsionFree: (c, point) => {
|
|
911
914
|
// φ endomorphism
|
|
912
|
-
const cubicRootOfUnityModP =
|
|
915
|
+
const cubicRootOfUnityModP = BigInt('0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe');
|
|
913
916
|
const phi = new c(Fp.mul(point.px, cubicRootOfUnityModP), point.py, point.pz);
|
|
914
917
|
// todo: unroll
|
|
915
918
|
const xP = point.multiplyUnsafe(bls12_381.CURVE.x).negate(); // [x]P
|
|
@@ -917,7 +920,7 @@ export const bls12_381 = bls({
|
|
|
917
920
|
return u2P.equals(phi);
|
|
918
921
|
// https://eprint.iacr.org/2019/814.pdf
|
|
919
922
|
// (z² − 1)/3
|
|
920
|
-
// const c1 =
|
|
923
|
+
// const c1 = BigInt('0x396c8c005555e1560000000055555555');
|
|
921
924
|
// const P = this;
|
|
922
925
|
// const S = P.sigma();
|
|
923
926
|
// const Q = S.double();
|
|
@@ -943,15 +946,15 @@ export const bls12_381 = bls({
|
|
|
943
946
|
const compressedValue = bytesToNumberBE(bytes);
|
|
944
947
|
const bflag = bitGet(compressedValue, I_BIT_POS);
|
|
945
948
|
// Zero
|
|
946
|
-
if (bflag ===
|
|
947
|
-
return { x:
|
|
949
|
+
if (bflag === _1n)
|
|
950
|
+
return { x: _0n, y: _0n };
|
|
948
951
|
const x = Fp.create(compressedValue & Fp.MASK);
|
|
949
|
-
const right = Fp.add(Fp.pow(x,
|
|
952
|
+
const right = Fp.add(Fp.pow(x, _3n), Fp.create(bls12_381.CURVE.G1.b)); // y² = x³ + b
|
|
950
953
|
let y = Fp.sqrt(right);
|
|
951
954
|
if (!y)
|
|
952
955
|
throw new Error('Invalid compressed G1 point');
|
|
953
956
|
const aflag = bitGet(compressedValue, C_BIT_POS);
|
|
954
|
-
if ((y *
|
|
957
|
+
if ((y * _2n) / P !== aflag)
|
|
955
958
|
y = Fp.neg(y);
|
|
956
959
|
return { x: Fp.create(x), y: Fp.create(y) };
|
|
957
960
|
}
|
|
@@ -975,7 +978,7 @@ export const bls12_381 = bls({
|
|
|
975
978
|
return COMPRESSED_ZERO.slice();
|
|
976
979
|
const P = Fp.ORDER;
|
|
977
980
|
let num;
|
|
978
|
-
num = bitSet(x, C_BIT_POS, Boolean((y *
|
|
981
|
+
num = bitSet(x, C_BIT_POS, Boolean((y * _2n) / P)); // set aflag
|
|
979
982
|
num = bitSet(num, S_BIT_POS, true);
|
|
980
983
|
return numberToBytesBE(num, Fp.BYTES);
|
|
981
984
|
}
|
|
@@ -998,21 +1001,21 @@ export const bls12_381 = bls({
|
|
|
998
1001
|
G2: {
|
|
999
1002
|
Fp: Fp2,
|
|
1000
1003
|
// cofactor
|
|
1001
|
-
h:
|
|
1004
|
+
h: BigInt('0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5'),
|
|
1002
1005
|
Gx: Fp2.fromBigTuple([
|
|
1003
|
-
|
|
1004
|
-
|
|
1006
|
+
BigInt('0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8'),
|
|
1007
|
+
BigInt('0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e'),
|
|
1005
1008
|
]),
|
|
1006
1009
|
// y =
|
|
1007
1010
|
// 927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582,
|
|
1008
1011
|
// 1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905
|
|
1009
1012
|
Gy: Fp2.fromBigTuple([
|
|
1010
|
-
|
|
1011
|
-
|
|
1013
|
+
BigInt('0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801'),
|
|
1014
|
+
BigInt('0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be'),
|
|
1012
1015
|
]),
|
|
1013
1016
|
a: Fp2.ZERO,
|
|
1014
|
-
b: Fp2.fromBigTuple([4n,
|
|
1015
|
-
hEff:
|
|
1017
|
+
b: Fp2.fromBigTuple([4n, _4n]),
|
|
1018
|
+
hEff: BigInt('0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551'),
|
|
1016
1019
|
htfDefaults: { ...htfDefaults },
|
|
1017
1020
|
wrapPrivateKey: true,
|
|
1018
1021
|
allowInfinityPoint: true,
|
|
@@ -1072,9 +1075,9 @@ export const bls12_381 = bls({
|
|
|
1072
1075
|
const x_1 = slc(bytes, 0, L);
|
|
1073
1076
|
const x_0 = slc(bytes, L, 2 * L);
|
|
1074
1077
|
const x = Fp2.create({ c0: Fp.create(x_0), c1: Fp.create(x_1) });
|
|
1075
|
-
const right = Fp2.add(Fp2.pow(x,
|
|
1078
|
+
const right = Fp2.add(Fp2.pow(x, _3n), b); // y² = x³ + 4 * (u+1) = x³ + b
|
|
1076
1079
|
let y = Fp2.sqrt(right);
|
|
1077
|
-
const Y_bit = y.c1 ===
|
|
1080
|
+
const Y_bit = y.c1 === _0n ? (y.c0 * _2n) / P : (y.c1 * _2n) / P ? _1n : _0n;
|
|
1078
1081
|
y = bitS > 0 && Y_bit > 0 ? y : Fp2.neg(y);
|
|
1079
1082
|
return { x, y };
|
|
1080
1083
|
}
|
|
@@ -1100,7 +1103,7 @@ export const bls12_381 = bls({
|
|
|
1100
1103
|
const P = Fp.ORDER;
|
|
1101
1104
|
if (isZero)
|
|
1102
1105
|
return concatB(COMPRESSED_ZERO, numberToBytesBE(0n, Fp.BYTES));
|
|
1103
|
-
const flag = Boolean(y.c1 ===
|
|
1106
|
+
const flag = Boolean(y.c1 === _0n ? (y.c0 * _2n) / P : (y.c1 * _2n) / P);
|
|
1104
1107
|
// set compressed & sign bits (looks like different offsets than for G1/Fp?)
|
|
1105
1108
|
let x_1 = bitSet(x.c1, C_BIT_POS, flag);
|
|
1106
1109
|
x_1 = bitSet(x_1, S_BIT_POS, true);
|
|
@@ -1126,12 +1129,12 @@ export const bls12_381 = bls({
|
|
|
1126
1129
|
const z2 = bytesToNumberBE(hex.slice(half));
|
|
1127
1130
|
// Indicates the infinity point
|
|
1128
1131
|
const bflag1 = bitGet(z1, I_BIT_POS);
|
|
1129
|
-
if (bflag1 ===
|
|
1132
|
+
if (bflag1 === _1n)
|
|
1130
1133
|
return bls12_381.G2.ProjectivePoint.ZERO;
|
|
1131
1134
|
const x1 = Fp.create(z1 & Fp.MASK);
|
|
1132
1135
|
const x2 = Fp.create(z2);
|
|
1133
1136
|
const x = Fp2.create({ c0: x2, c1: x1 });
|
|
1134
|
-
const y2 = Fp2.add(Fp2.pow(x,
|
|
1137
|
+
const y2 = Fp2.add(Fp2.pow(x, _3n), bls12_381.CURVE.G2.b); // y² = x³ + 4
|
|
1135
1138
|
// The slow part
|
|
1136
1139
|
let y = Fp2.sqrt(y2);
|
|
1137
1140
|
if (!y)
|
|
@@ -1140,8 +1143,8 @@ export const bls12_381 = bls({
|
|
|
1140
1143
|
// If y1 happens to be zero, then use the bit of y0
|
|
1141
1144
|
const { re: y0, im: y1 } = Fp2.reim(y);
|
|
1142
1145
|
const aflag1 = bitGet(z1, 381);
|
|
1143
|
-
const isGreater = y1 >
|
|
1144
|
-
const isZero = y1 ===
|
|
1146
|
+
const isGreater = y1 > _0n && (y1 * _2n) / P !== aflag1;
|
|
1147
|
+
const isZero = y1 === _0n && (y0 * _2n) / P !== aflag1;
|
|
1145
1148
|
if (isGreater || isZero)
|
|
1146
1149
|
y = Fp2.neg(y);
|
|
1147
1150
|
const point = bls12_381.G2.ProjectivePoint.fromAffine({ x, y });
|
|
@@ -1156,8 +1159,8 @@ export const bls12_381 = bls({
|
|
|
1156
1159
|
const a = point.toAffine();
|
|
1157
1160
|
const { re: x0, im: x1 } = Fp2.reim(a.x);
|
|
1158
1161
|
const { re: y0, im: y1 } = Fp2.reim(a.y);
|
|
1159
|
-
const tmp = y1 >
|
|
1160
|
-
const aflag1 = Boolean((tmp / Fp.ORDER) &
|
|
1162
|
+
const tmp = y1 > _0n ? y1 * _2n : y0 * _2n;
|
|
1163
|
+
const aflag1 = Boolean((tmp / Fp.ORDER) & _1n);
|
|
1161
1164
|
const z1 = bitSet(bitSet(x1, 381, aflag1), S_BIT_POS, true);
|
|
1162
1165
|
const z2 = x0;
|
|
1163
1166
|
return concatB(numberToBytesBE(z1, Fp.BYTES), numberToBytesBE(z2, Fp.BYTES));
|