@noble/curves 0.8.3 → 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.
Files changed (108) hide show
  1. package/README.md +87 -62
  2. package/_shortw_utils.d.ts +2 -1
  3. package/_shortw_utils.d.ts.map +1 -1
  4. package/abstract/bls.d.ts +11 -11
  5. package/abstract/bls.d.ts.map +1 -1
  6. package/abstract/bls.js.map +1 -1
  7. package/abstract/curve.d.ts +5 -3
  8. package/abstract/curve.d.ts.map +1 -1
  9. package/abstract/curve.js +5 -1
  10. package/abstract/curve.js.map +1 -1
  11. package/abstract/edwards.d.ts +6 -1
  12. package/abstract/edwards.d.ts.map +1 -1
  13. package/abstract/edwards.js +8 -9
  14. package/abstract/edwards.js.map +1 -1
  15. package/abstract/hash-to-curve.d.ts +2 -2
  16. package/abstract/hash-to-curve.d.ts.map +1 -1
  17. package/abstract/hash-to-curve.js.map +1 -1
  18. package/abstract/modular.d.ts +24 -12
  19. package/abstract/modular.d.ts.map +1 -1
  20. package/abstract/modular.js +17 -4
  21. package/abstract/modular.js.map +1 -1
  22. package/abstract/poseidon.d.ts +3 -3
  23. package/abstract/poseidon.d.ts.map +1 -1
  24. package/abstract/poseidon.js.map +1 -1
  25. package/abstract/weierstrass.d.ts +29 -5
  26. package/abstract/weierstrass.d.ts.map +1 -1
  27. package/abstract/weierstrass.js +37 -23
  28. package/abstract/weierstrass.js.map +1 -1
  29. package/bls12-381.d.ts +4 -4
  30. package/bls12-381.d.ts.map +1 -1
  31. package/bls12-381.js +109 -106
  32. package/bls12-381.js.map +1 -1
  33. package/bn.js +1 -1
  34. package/bn.js.map +1 -1
  35. package/ed25519.js +2 -2
  36. package/ed25519.js.map +1 -1
  37. package/ed448.d.ts.map +1 -1
  38. package/ed448.js +4 -3
  39. package/ed448.js.map +1 -1
  40. package/esm/abstract/bls.js.map +1 -1
  41. package/esm/abstract/curve.js +5 -1
  42. package/esm/abstract/curve.js.map +1 -1
  43. package/esm/abstract/edwards.js +8 -9
  44. package/esm/abstract/edwards.js.map +1 -1
  45. package/esm/abstract/hash-to-curve.js.map +1 -1
  46. package/esm/abstract/modular.js +15 -2
  47. package/esm/abstract/modular.js.map +1 -1
  48. package/esm/abstract/poseidon.js.map +1 -1
  49. package/esm/abstract/weierstrass.js +37 -23
  50. package/esm/abstract/weierstrass.js.map +1 -1
  51. package/esm/bls12-381.js +109 -106
  52. package/esm/bls12-381.js.map +1 -1
  53. package/esm/bn.js +2 -2
  54. package/esm/bn.js.map +1 -1
  55. package/esm/ed25519.js +2 -2
  56. package/esm/ed25519.js.map +1 -1
  57. package/esm/ed448.js +4 -3
  58. package/esm/ed448.js.map +1 -1
  59. package/esm/jubjub.js +2 -2
  60. package/esm/jubjub.js.map +1 -1
  61. package/esm/p256.js +1 -1
  62. package/esm/p256.js.map +1 -1
  63. package/esm/p384.js +1 -1
  64. package/esm/p384.js.map +1 -1
  65. package/esm/p521.js +1 -1
  66. package/esm/p521.js.map +1 -1
  67. package/esm/pasta.js +2 -2
  68. package/esm/pasta.js.map +1 -1
  69. package/esm/secp256k1.js +2 -2
  70. package/esm/secp256k1.js.map +1 -1
  71. package/jubjub.js +1 -1
  72. package/jubjub.js.map +1 -1
  73. package/p256.d.ts +4 -2
  74. package/p256.d.ts.map +1 -1
  75. package/p256.js +1 -1
  76. package/p256.js.map +1 -1
  77. package/p384.d.ts +4 -2
  78. package/p384.d.ts.map +1 -1
  79. package/p384.js +1 -1
  80. package/p384.js.map +1 -1
  81. package/p521.d.ts +4 -2
  82. package/p521.d.ts.map +1 -1
  83. package/p521.js +1 -1
  84. package/p521.js.map +1 -1
  85. package/package.json +5 -5
  86. package/pasta.js +2 -2
  87. package/pasta.js.map +1 -1
  88. package/secp256k1.d.ts +2 -1
  89. package/secp256k1.d.ts.map +1 -1
  90. package/secp256k1.js +2 -2
  91. package/secp256k1.js.map +1 -1
  92. package/src/abstract/bls.ts +11 -11
  93. package/src/abstract/curve.ts +7 -3
  94. package/src/abstract/edwards.ts +12 -9
  95. package/src/abstract/hash-to-curve.ts +2 -2
  96. package/src/abstract/modular.ts +29 -20
  97. package/src/abstract/poseidon.ts +2 -2
  98. package/src/abstract/weierstrass.ts +45 -27
  99. package/src/bls12-381.ts +199 -114
  100. package/src/bn.ts +2 -2
  101. package/src/ed25519.ts +2 -2
  102. package/src/ed448.ts +4 -3
  103. package/src/jubjub.ts +2 -2
  104. package/src/p256.ts +1 -1
  105. package/src/p384.ts +1 -1
  106. package/src/p521.ts +1 -1
  107. package/src/pasta.ts +2 -2
  108. package/src/secp256k1.ts +3 -4
package/src/bls12-381.ts CHANGED
@@ -69,16 +69,23 @@ import {
69
69
  } from './abstract/weierstrass.js';
70
70
  import { isogenyMap } from './abstract/hash-to-curve.js';
71
71
 
72
+ // Be friendly to bad ECMAScript parsers by not using bigint literals
73
+ // prettier-ignore
74
+ const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4);
75
+ const _8n = BigInt(8),
76
+ _16n = BigInt(16);
77
+
72
78
  // CURVE FIELDS
73
79
  // Finite field over p.
74
- const Fp =
75
- mod.Fp(
76
- 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn
77
- );
80
+ const Fp = mod.Field(
81
+ BigInt(
82
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab'
83
+ )
84
+ );
78
85
  type Fp = bigint;
79
86
  // Finite field over r.
80
87
  // This particular field is not used anywhere in bls12-381, but it is still useful.
81
- const Fr = mod.Fp(0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001n);
88
+ const Fr = mod.Field(BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001'));
82
89
 
83
90
  // Fp₂ over complex plane
84
91
  type BigintTuple = [bigint, bigint];
@@ -121,10 +128,11 @@ type Fp2Utils = {
121
128
  // h2q
122
129
  // NOTE: ORDER was wrong!
123
130
  const FP2_ORDER =
124
- 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn **
125
- 2n;
131
+ BigInt(
132
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab'
133
+ ) ** _2n;
126
134
 
127
- const Fp2: mod.Field<Fp2> & Fp2Utils = {
135
+ const Fp2: mod.IField<Fp2> & Fp2Utils = {
128
136
  ORDER: FP2_ORDER,
129
137
  BITS: bitLen(FP2_ORDER),
130
138
  BYTES: Math.ceil(bitLen(FP2_ORDER) / 8),
@@ -175,7 +183,7 @@ const Fp2: mod.Field<Fp2> & Fp2Utils = {
175
183
  // https://github.com/zkcrypto/bls12_381/blob/080eaa74ec0e394377caa1ba302c8c121df08b07/src/fp2.rs#L250
176
184
  // https://github.com/supranational/blst/blob/aae0c7d70b799ac269ff5edf29d8191dbd357876/src/exp2.c#L1
177
185
  // Inspired by https://github.com/dalek-cryptography/curve25519-dalek/blob/17698df9d4c834204f83a3574143abacb4fc81a5/src/field.rs#L99
178
- const candidateSqrt = Fp2.pow(num, (Fp2.ORDER + 8n) / 16n);
186
+ const candidateSqrt = Fp2.pow(num, (Fp2.ORDER + _8n) / _16n);
179
187
  const check = Fp2.div(Fp2.sqr(candidateSqrt), num); // candidateSqrt.square().div(this);
180
188
  const R = FP2_ROOTS_OF_UNITY;
181
189
  const divisor = [R[0], R[2], R[4], R[6]].find((r) => Fp2.eql(r, check));
@@ -193,10 +201,10 @@ const Fp2: mod.Field<Fp2> & Fp2Utils = {
193
201
  // Same as sgn0_fp2 in draft-irtf-cfrg-hash-to-curve-16
194
202
  isOdd: (x: Fp2) => {
195
203
  const { re: x0, im: x1 } = Fp2.reim(x);
196
- const sign_0 = x0 % 2n;
197
- const zero_0 = x0 === 0n;
198
- const sign_1 = x1 % 2n;
199
- return BigInt(sign_0 || (zero_0 && sign_1)) == 1n;
204
+ const sign_0 = x0 % _2n;
205
+ const zero_0 = x0 === _0n;
206
+ const sign_1 = x1 % _2n;
207
+ return BigInt(sign_0 || (zero_0 && sign_1)) == _1n;
200
208
  },
201
209
  // Bytes util
202
210
  fromBytes(b: Uint8Array): Fp2 {
@@ -216,8 +224,8 @@ const Fp2: mod.Field<Fp2> & Fp2Utils = {
216
224
  // multiply by u + 1
217
225
  mulByNonresidue: ({ c0, c1 }) => ({ c0: Fp.sub(c0, c1), c1: Fp.add(c0, c1) }),
218
226
  multiplyByB: ({ c0, c1 }) => {
219
- let t0 = Fp.mul(c0, 4n); // 4 * c0
220
- let t1 = Fp.mul(c1, 4n); // 4 * c1
227
+ let t0 = Fp.mul(c0, _4n); // 4 * c0
228
+ let t1 = Fp.mul(c1, _4n); // 4 * c1
221
229
  // (T0-T1) + (T0+T1)*i
222
230
  return { c0: Fp.sub(t0, t1), c1: Fp.add(t0, t1) };
223
231
  },
@@ -234,33 +242,36 @@ const Fp2: mod.Field<Fp2> & Fp2Utils = {
234
242
  // Finite extension field over irreducible polynominal.
235
243
  // Fp(u) / (u² - β) where β = -1
236
244
  const FP2_FROBENIUS_COEFFICIENTS = [
237
- 0x1n,
238
- 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaan,
245
+ BigInt('0x1'),
246
+ BigInt(
247
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'
248
+ ),
239
249
  ].map((item) => Fp.create(item));
240
250
 
241
251
  // For Fp2 roots of unity.
242
- const rv1 =
243
- 0x6af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09n;
252
+ const rv1 = BigInt(
253
+ '0x6af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09'
254
+ );
244
255
  // const ev1 =
245
- // 0x699be3b8c6870965e5bf892ad5d2cc7b0e85a117402dfd83b7f4a947e02d978498255a2aaec0ac627b5afbdf1bf1c90n;
256
+ // BigInt('0x699be3b8c6870965e5bf892ad5d2cc7b0e85a117402dfd83b7f4a947e02d978498255a2aaec0ac627b5afbdf1bf1c90');
246
257
  // const ev2 =
247
- // 0x8157cd83046453f5dd0972b6e3949e4288020b5b8a9cc99ca07e27089a2ce2436d965026adad3ef7baba37f2183e9b5n;
258
+ // BigInt('0x8157cd83046453f5dd0972b6e3949e4288020b5b8a9cc99ca07e27089a2ce2436d965026adad3ef7baba37f2183e9b5');
248
259
  // const ev3 =
249
- // 0xab1c2ffdd6c253ca155231eb3e71ba044fd562f6f72bc5bad5ec46a0b7a3b0247cf08ce6c6317f40edbc653a72dee17n;
260
+ // BigInt('0xab1c2ffdd6c253ca155231eb3e71ba044fd562f6f72bc5bad5ec46a0b7a3b0247cf08ce6c6317f40edbc653a72dee17');
250
261
  // const ev4 =
251
- // 0xaa404866706722864480885d68ad0ccac1967c7544b447873cc37e0181271e006df72162a3d3e0287bf597fbf7f8fc1n;
262
+ // BigInt('0xaa404866706722864480885d68ad0ccac1967c7544b447873cc37e0181271e006df72162a3d3e0287bf597fbf7f8fc1');
252
263
 
253
264
  // Eighth roots of unity, used for computing square roots in Fp2.
254
265
  // To verify or re-calculate:
255
266
  // Array(8).fill(new Fp2([1n, 1n])).map((fp2, k) => fp2.pow(Fp2.ORDER * BigInt(k) / 8n))
256
267
  const FP2_ROOTS_OF_UNITY = [
257
- [1n, 0n],
268
+ [_1n, _0n],
258
269
  [rv1, -rv1],
259
- [0n, 1n],
270
+ [_0n, _1n],
260
271
  [rv1, rv1],
261
- [-1n, 0n],
272
+ [-_1n, _0n],
262
273
  [-rv1, rv1],
263
- [0n, -1n],
274
+ [_0n, -_1n],
264
275
  [-rv1, -rv1],
265
276
  ].map((pair) => Fp2.fromBigTuple(pair));
266
277
  // eta values, used for computing sqrt(g(X1(t)))
@@ -314,8 +325,8 @@ const Fp6Multiply = ({ c0, c1, c2 }: Fp6, rhs: Fp6 | bigint) => {
314
325
  };
315
326
  const Fp6Square = ({ c0, c1, c2 }: Fp6) => {
316
327
  let t0 = Fp2.sqr(c0); // c0²
317
- let t1 = Fp2.mul(Fp2.mul(c0, c1), 2n); // 2 * c0 * c1
318
- let t3 = Fp2.mul(Fp2.mul(c1, c2), 2n); // 2 * c1 * c2
328
+ let t1 = Fp2.mul(Fp2.mul(c0, c1), _2n); // 2 * c0 * c1
329
+ let t3 = Fp2.mul(Fp2.mul(c1, c2), _2n); // 2 * c1 * c2
319
330
  let t4 = Fp2.sqr(c2); // c2²
320
331
  return {
321
332
  c0: Fp2.add(Fp2.mulByNonresidue(t3), t0), // T3 * (u + 1) + T0
@@ -333,7 +344,7 @@ type Fp6Utils = {
333
344
  multiplyByFp2(lhs: Fp6, rhs: Fp2): Fp6;
334
345
  };
335
346
 
336
- const Fp6: mod.Field<Fp6> & Fp6Utils = {
347
+ const Fp6: mod.IField<Fp6> & Fp6Utils = {
337
348
  ORDER: Fp2.ORDER, // TODO: unused, but need to verify
338
349
  BITS: 3 * Fp2.BITS,
339
350
  BYTES: 3 * Fp2.BYTES,
@@ -440,46 +451,64 @@ const Fp6: mod.Field<Fp6> & Fp6Utils = {
440
451
  };
441
452
 
442
453
  const FP6_FROBENIUS_COEFFICIENTS_1 = [
443
- [0x1n, 0x0n],
454
+ [BigInt('0x1'), BigInt('0x0')],
444
455
  [
445
- 0x0n,
446
- 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn,
456
+ BigInt('0x0'),
457
+ BigInt(
458
+ '0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'
459
+ ),
447
460
  ],
448
461
  [
449
- 0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen,
450
- 0x0n,
462
+ BigInt(
463
+ '0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'
464
+ ),
465
+ BigInt('0x0'),
451
466
  ],
452
- [0x0n, 0x1n],
467
+ [BigInt('0x0'), BigInt('0x1')],
453
468
  [
454
- 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn,
455
- 0x0n,
469
+ BigInt(
470
+ '0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'
471
+ ),
472
+ BigInt('0x0'),
456
473
  ],
457
474
  [
458
- 0x0n,
459
- 0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen,
475
+ BigInt('0x0'),
476
+ BigInt(
477
+ '0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'
478
+ ),
460
479
  ],
461
480
  ].map((pair) => Fp2.fromBigTuple(pair));
462
481
  const FP6_FROBENIUS_COEFFICIENTS_2 = [
463
- [0x1n, 0x0n],
482
+ [BigInt('0x1'), BigInt('0x0')],
464
483
  [
465
- 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaadn,
466
- 0x0n,
484
+ BigInt(
485
+ '0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaad'
486
+ ),
487
+ BigInt('0x0'),
467
488
  ],
468
489
  [
469
- 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn,
470
- 0x0n,
490
+ BigInt(
491
+ '0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'
492
+ ),
493
+ BigInt('0x0'),
471
494
  ],
472
495
  [
473
- 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaan,
474
- 0x0n,
496
+ BigInt(
497
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'
498
+ ),
499
+ BigInt('0x0'),
475
500
  ],
476
501
  [
477
- 0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen,
478
- 0x0n,
502
+ BigInt(
503
+ '0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'
504
+ ),
505
+ BigInt('0x0'),
479
506
  ],
480
507
  [
481
- 0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffffn,
482
- 0x0n,
508
+ BigInt(
509
+ '0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffff'
510
+ ),
511
+ BigInt('0x0'),
483
512
  ],
484
513
  ].map((pair) => Fp2.fromBigTuple(pair));
485
514
 
@@ -488,7 +517,7 @@ const FP6_FROBENIUS_COEFFICIENTS_2 = [
488
517
  // Fp₆(w) / (w² - γ) where γ = v
489
518
  type Fp12 = { c0: Fp6; c1: Fp6 };
490
519
  // The BLS parameter x for BLS12-381
491
- const BLS_X = 0xd201000000010000n;
520
+ const BLS_X = BigInt('0xd201000000010000');
492
521
  const BLS_X_LEN = bitLen(BLS_X);
493
522
 
494
523
  // prettier-ignore
@@ -545,7 +574,7 @@ type Fp12Utils = {
545
574
  _cyclotomicExp(num: Fp12, n: bigint): Fp12;
546
575
  };
547
576
 
548
- const Fp12: mod.Field<Fp12> & Fp12Utils = {
577
+ const Fp12: mod.IField<Fp12> & Fp12Utils = {
549
578
  ORDER: Fp2.ORDER, // TODO: unused, but need to verify
550
579
  BITS: 2 * Fp2.BITS,
551
580
  BYTES: 2 * Fp2.BYTES,
@@ -646,14 +675,14 @@ const Fp12: mod.Field<Fp12> & Fp12Utils = {
646
675
  let t9 = Fp2.mulByNonresidue(t8); // T8 * (u + 1)
647
676
  return {
648
677
  c0: Fp6.create({
649
- c0: Fp2.add(Fp2.mul(Fp2.sub(t3, c0c0), 2n), t3), // 2 * (T3 - c0c0) + T3
650
- c1: Fp2.add(Fp2.mul(Fp2.sub(t5, c0c1), 2n), t5), // 2 * (T5 - c0c1) + T5
651
- c2: Fp2.add(Fp2.mul(Fp2.sub(t7, c0c2), 2n), t7),
678
+ c0: Fp2.add(Fp2.mul(Fp2.sub(t3, c0c0), _2n), t3), // 2 * (T3 - c0c0) + T3
679
+ c1: Fp2.add(Fp2.mul(Fp2.sub(t5, c0c1), _2n), t5), // 2 * (T5 - c0c1) + T5
680
+ c2: Fp2.add(Fp2.mul(Fp2.sub(t7, c0c2), _2n), t7),
652
681
  }), // 2 * (T7 - c0c2) + T7
653
682
  c1: Fp6.create({
654
- c0: Fp2.add(Fp2.mul(Fp2.add(t9, c1c0), 2n), t9), // 2 * (T9 + c1c0) + T9
655
- c1: Fp2.add(Fp2.mul(Fp2.add(t4, c1c1), 2n), t4), // 2 * (T4 + c1c1) + T4
656
- c2: Fp2.add(Fp2.mul(Fp2.add(t6, c1c2), 2n), t6),
683
+ c0: Fp2.add(Fp2.mul(Fp2.add(t9, c1c0), _2n), t9), // 2 * (T9 + c1c0) + T9
684
+ c1: Fp2.add(Fp2.mul(Fp2.add(t4, c1c1), _2n), t4), // 2 * (T4 + c1c1) + T4
685
+ c2: Fp2.add(Fp2.mul(Fp2.add(t6, c1c2), _2n), t6),
657
686
  }),
658
687
  }; // 2 * (T6 + c1c2) + T6
659
688
  },
@@ -688,50 +717,84 @@ const Fp12: mod.Field<Fp12> & Fp12Utils = {
688
717
  },
689
718
  };
690
719
  const FP12_FROBENIUS_COEFFICIENTS = [
691
- [0x1n, 0x0n],
720
+ [BigInt('0x1'), BigInt('0x0')],
692
721
  [
693
- 0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8n,
694
- 0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3n,
722
+ BigInt(
723
+ '0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8'
724
+ ),
725
+ BigInt(
726
+ '0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3'
727
+ ),
695
728
  ],
696
729
  [
697
- 0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffffn,
698
- 0x0n,
730
+ BigInt(
731
+ '0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffff'
732
+ ),
733
+ BigInt('0x0'),
699
734
  ],
700
735
  [
701
- 0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2n,
702
- 0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09n,
736
+ BigInt(
737
+ '0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2'
738
+ ),
739
+ BigInt(
740
+ '0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09'
741
+ ),
703
742
  ],
704
743
  [
705
- 0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen,
706
- 0x0n,
744
+ BigInt(
745
+ '0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'
746
+ ),
747
+ BigInt('0x0'),
707
748
  ],
708
749
  [
709
- 0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995n,
710
- 0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116n,
750
+ BigInt(
751
+ '0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995'
752
+ ),
753
+ BigInt(
754
+ '0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116'
755
+ ),
711
756
  ],
712
757
  [
713
- 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaan,
714
- 0x0n,
758
+ BigInt(
759
+ '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'
760
+ ),
761
+ BigInt('0x0'),
715
762
  ],
716
763
  [
717
- 0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3n,
718
- 0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8n,
764
+ BigInt(
765
+ '0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3'
766
+ ),
767
+ BigInt(
768
+ '0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8'
769
+ ),
719
770
  ],
720
771
  [
721
- 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn,
722
- 0x0n,
772
+ BigInt(
773
+ '0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'
774
+ ),
775
+ BigInt('0x0'),
723
776
  ],
724
777
  [
725
- 0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09n,
726
- 0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2n,
778
+ BigInt(
779
+ '0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09'
780
+ ),
781
+ BigInt(
782
+ '0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2'
783
+ ),
727
784
  ],
728
785
  [
729
- 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaadn,
730
- 0x0n,
786
+ BigInt(
787
+ '0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaad'
788
+ ),
789
+ BigInt('0x0'),
731
790
  ],
732
791
  [
733
- 0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116n,
734
- 0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995n,
792
+ BigInt(
793
+ '0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116'
794
+ ),
795
+ BigInt(
796
+ '0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995'
797
+ ),
735
798
  ],
736
799
  ].map((n) => Fp2.fromBigTuple(n));
737
800
  // END OF CURVE FIELDS
@@ -887,17 +950,21 @@ const isogenyMapG1 = isogenyMap(
887
950
 
888
951
  // SWU Map - Fp2 to G2': y² = x³ + 240i * x + 1012 + 1012i
889
952
  const G2_SWU = mapToCurveSimpleSWU(Fp2, {
890
- A: Fp2.create({ c0: Fp.create(0n), c1: Fp.create(240n) }), // A' = 240 * I
953
+ A: Fp2.create({ c0: Fp.create(_0n), c1: Fp.create(240n) }), // A' = 240 * I
891
954
  B: Fp2.create({ c0: Fp.create(1012n), c1: Fp.create(1012n) }), // B' = 1012 * (1 + I)
892
955
  Z: Fp2.create({ c0: Fp.create(-2n), c1: Fp.create(-1n) }), // Z: -(2 + I)
893
956
  });
894
957
  // Optimized SWU Map - Fp to G1
895
958
  const G1_SWU = mapToCurveSimpleSWU(Fp, {
896
959
  A: Fp.create(
897
- 0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1dn
960
+ BigInt(
961
+ '0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1d'
962
+ )
898
963
  ),
899
964
  B: Fp.create(
900
- 0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0n
965
+ BigInt(
966
+ '0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0'
967
+ )
901
968
  ),
902
969
  Z: Fp.create(11n),
903
970
  });
@@ -922,8 +989,9 @@ function G2psi(c: ProjConstructor<Fp2>, P: ProjPointType<Fp2>) {
922
989
  }
923
990
  // Ψ²(P) endomorphism
924
991
  // 1 / F2(2)^((p-1)/3) in GF(p²)
925
- const PSI2_C1 =
926
- 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn;
992
+ const PSI2_C1 = BigInt(
993
+ '0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'
994
+ );
927
995
 
928
996
  function psi2(x: Fp2, y: Fp2): [Fp2, Fp2] {
929
997
  return [Fp2.mul(x, PSI2_C1), Fp2.neg(y)];
@@ -1000,14 +1068,18 @@ export const bls12_381: CurveFn<Fp, Fp2, Fp6, Fp12> = bls({
1000
1068
  G1: {
1001
1069
  Fp,
1002
1070
  // cofactor; (z - 1)²/3
1003
- h: 0x396c8c005555e1568c00aaab0000aaabn,
1071
+ h: BigInt('0x396c8c005555e1568c00aaab0000aaab'),
1004
1072
  // generator's coordinates
1005
1073
  // x = 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507
1006
1074
  // y = 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569
1007
- Gx: 0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bbn,
1008
- Gy: 0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1n,
1075
+ Gx: BigInt(
1076
+ '0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb'
1077
+ ),
1078
+ Gy: BigInt(
1079
+ '0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1'
1080
+ ),
1009
1081
  a: Fp.ZERO,
1010
- b: 4n,
1082
+ b: _4n,
1011
1083
  htfDefaults: { ...htfDefaults, m: 1 },
1012
1084
  wrapPrivateKey: true,
1013
1085
  allowInfinityPoint: true,
@@ -1017,8 +1089,9 @@ export const bls12_381: CurveFn<Fp, Fp2, Fp6, Fp12> = bls({
1017
1089
  // https://eprint.iacr.org/2021/1130.pdf
1018
1090
  isTorsionFree: (c, point): boolean => {
1019
1091
  // φ endomorphism
1020
- const cubicRootOfUnityModP =
1021
- 0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen;
1092
+ const cubicRootOfUnityModP = BigInt(
1093
+ '0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'
1094
+ );
1022
1095
  const phi = new c(Fp.mul(point.px, cubicRootOfUnityModP), point.py, point.pz);
1023
1096
 
1024
1097
  // todo: unroll
@@ -1028,7 +1101,7 @@ export const bls12_381: CurveFn<Fp, Fp2, Fp6, Fp12> = bls({
1028
1101
 
1029
1102
  // https://eprint.iacr.org/2019/814.pdf
1030
1103
  // (z² − 1)/3
1031
- // const c1 = 0x396c8c005555e1560000000055555555n;
1104
+ // const c1 = BigInt('0x396c8c005555e1560000000055555555');
1032
1105
  // const P = this;
1033
1106
  // const S = P.sigma();
1034
1107
  // const Q = S.double();
@@ -1054,13 +1127,13 @@ export const bls12_381: CurveFn<Fp, Fp2, Fp6, Fp12> = bls({
1054
1127
  const compressedValue = bytesToNumberBE(bytes);
1055
1128
  const bflag = bitGet(compressedValue, I_BIT_POS);
1056
1129
  // Zero
1057
- if (bflag === 1n) return { x: 0n, y: 0n };
1130
+ if (bflag === _1n) return { x: _0n, y: _0n };
1058
1131
  const x = Fp.create(compressedValue & Fp.MASK);
1059
- const right = Fp.add(Fp.pow(x, 3n), Fp.create(bls12_381.CURVE.G1.b)); // y² = x³ + b
1132
+ const right = Fp.add(Fp.pow(x, _3n), Fp.create(bls12_381.CURVE.G1.b)); // y² = x³ + b
1060
1133
  let y = Fp.sqrt(right);
1061
1134
  if (!y) throw new Error('Invalid compressed G1 point');
1062
1135
  const aflag = bitGet(compressedValue, C_BIT_POS);
1063
- if ((y * 2n) / P !== aflag) y = Fp.neg(y);
1136
+ if ((y * _2n) / P !== aflag) y = Fp.neg(y);
1064
1137
  return { x: Fp.create(x), y: Fp.create(y) };
1065
1138
  } else if (bytes.length === 96) {
1066
1139
  // Check if the infinity flag is set
@@ -1079,7 +1152,7 @@ export const bls12_381: CurveFn<Fp, Fp2, Fp6, Fp12> = bls({
1079
1152
  if (isZero) return COMPRESSED_ZERO.slice();
1080
1153
  const P = Fp.ORDER;
1081
1154
  let num;
1082
- num = bitSet(x, C_BIT_POS, Boolean((y * 2n) / P)); // set aflag
1155
+ num = bitSet(x, C_BIT_POS, Boolean((y * _2n) / P)); // set aflag
1083
1156
  num = bitSet(num, S_BIT_POS, true);
1084
1157
  return numberToBytesBE(num, Fp.BYTES);
1085
1158
  } else {
@@ -1100,21 +1173,33 @@ export const bls12_381: CurveFn<Fp, Fp2, Fp6, Fp12> = bls({
1100
1173
  G2: {
1101
1174
  Fp: Fp2,
1102
1175
  // cofactor
1103
- h: 0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5n,
1176
+ h: BigInt(
1177
+ '0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5'
1178
+ ),
1104
1179
  Gx: Fp2.fromBigTuple([
1105
- 0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8n,
1106
- 0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7en,
1180
+ BigInt(
1181
+ '0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8'
1182
+ ),
1183
+ BigInt(
1184
+ '0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e'
1185
+ ),
1107
1186
  ]),
1108
1187
  // y =
1109
1188
  // 927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582,
1110
1189
  // 1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905
1111
1190
  Gy: Fp2.fromBigTuple([
1112
- 0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801n,
1113
- 0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79ben,
1191
+ BigInt(
1192
+ '0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801'
1193
+ ),
1194
+ BigInt(
1195
+ '0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be'
1196
+ ),
1114
1197
  ]),
1115
1198
  a: Fp2.ZERO,
1116
- b: Fp2.fromBigTuple([4n, 4n]),
1117
- hEff: 0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551n,
1199
+ b: Fp2.fromBigTuple([4n, _4n]),
1200
+ hEff: BigInt(
1201
+ '0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551'
1202
+ ),
1118
1203
  htfDefaults: { ...htfDefaults },
1119
1204
  wrapPrivateKey: true,
1120
1205
  allowInfinityPoint: true,
@@ -1175,9 +1260,9 @@ export const bls12_381: CurveFn<Fp, Fp2, Fp6, Fp12> = bls({
1175
1260
  const x_1 = slc(bytes, 0, L);
1176
1261
  const x_0 = slc(bytes, L, 2 * L);
1177
1262
  const x = Fp2.create({ c0: Fp.create(x_0), c1: Fp.create(x_1) });
1178
- const right = Fp2.add(Fp2.pow(x, 3n), b); // y² = x³ + 4 * (u+1) = x³ + b
1263
+ const right = Fp2.add(Fp2.pow(x, _3n), b); // y² = x³ + 4 * (u+1) = x³ + b
1179
1264
  let y = Fp2.sqrt(right);
1180
- const Y_bit = y.c1 === 0n ? (y.c0 * 2n) / P : (y.c1 * 2n) / P ? 1n : 0n;
1265
+ const Y_bit = y.c1 === _0n ? (y.c0 * _2n) / P : (y.c1 * _2n) / P ? _1n : _0n;
1181
1266
  y = bitS > 0 && Y_bit > 0 ? y : Fp2.neg(y);
1182
1267
  return { x, y };
1183
1268
  } else if (bytes.length === 192 && !bitC) {
@@ -1200,7 +1285,7 @@ export const bls12_381: CurveFn<Fp, Fp2, Fp6, Fp12> = bls({
1200
1285
  if (isCompressed) {
1201
1286
  const P = Fp.ORDER;
1202
1287
  if (isZero) return concatB(COMPRESSED_ZERO, numberToBytesBE(0n, Fp.BYTES));
1203
- const flag = Boolean(y.c1 === 0n ? (y.c0 * 2n) / P : (y.c1 * 2n) / P);
1288
+ const flag = Boolean(y.c1 === _0n ? (y.c0 * _2n) / P : (y.c1 * _2n) / P);
1204
1289
  // set compressed & sign bits (looks like different offsets than for G1/Fp?)
1205
1290
  let x_1 = bitSet(x.c1, C_BIT_POS, flag);
1206
1291
  x_1 = bitSet(x_1, S_BIT_POS, true);
@@ -1229,12 +1314,12 @@ export const bls12_381: CurveFn<Fp, Fp2, Fp6, Fp12> = bls({
1229
1314
  const z2 = bytesToNumberBE(hex.slice(half));
1230
1315
  // Indicates the infinity point
1231
1316
  const bflag1 = bitGet(z1, I_BIT_POS);
1232
- if (bflag1 === 1n) return bls12_381.G2.ProjectivePoint.ZERO;
1317
+ if (bflag1 === _1n) return bls12_381.G2.ProjectivePoint.ZERO;
1233
1318
 
1234
1319
  const x1 = Fp.create(z1 & Fp.MASK);
1235
1320
  const x2 = Fp.create(z2);
1236
1321
  const x = Fp2.create({ c0: x2, c1: x1 });
1237
- const y2 = Fp2.add(Fp2.pow(x, 3n), bls12_381.CURVE.G2.b); // y² = x³ + 4
1322
+ const y2 = Fp2.add(Fp2.pow(x, _3n), bls12_381.CURVE.G2.b); // y² = x³ + 4
1238
1323
  // The slow part
1239
1324
  let y = Fp2.sqrt(y2);
1240
1325
  if (!y) throw new Error('Failed to find a square root');
@@ -1243,8 +1328,8 @@ export const bls12_381: CurveFn<Fp, Fp2, Fp6, Fp12> = bls({
1243
1328
  // If y1 happens to be zero, then use the bit of y0
1244
1329
  const { re: y0, im: y1 } = Fp2.reim(y);
1245
1330
  const aflag1 = bitGet(z1, 381);
1246
- const isGreater = y1 > 0n && (y1 * 2n) / P !== aflag1;
1247
- const isZero = y1 === 0n && (y0 * 2n) / P !== aflag1;
1331
+ const isGreater = y1 > _0n && (y1 * _2n) / P !== aflag1;
1332
+ const isZero = y1 === _0n && (y0 * _2n) / P !== aflag1;
1248
1333
  if (isGreater || isZero) y = Fp2.neg(y);
1249
1334
  const point = bls12_381.G2.ProjectivePoint.fromAffine({ x, y });
1250
1335
  point.assertValidity();
@@ -1258,8 +1343,8 @@ export const bls12_381: CurveFn<Fp, Fp2, Fp6, Fp12> = bls({
1258
1343
  const a = point.toAffine();
1259
1344
  const { re: x0, im: x1 } = Fp2.reim(a.x);
1260
1345
  const { re: y0, im: y1 } = Fp2.reim(a.y);
1261
- const tmp = y1 > 0n ? y1 * 2n : y0 * 2n;
1262
- const aflag1 = Boolean((tmp / Fp.ORDER) & 1n);
1346
+ const tmp = y1 > _0n ? y1 * _2n : y0 * _2n;
1347
+ const aflag1 = Boolean((tmp / Fp.ORDER) & _1n);
1263
1348
  const z1 = bitSet(bitSet(x1, 381, aflag1), S_BIT_POS, true);
1264
1349
  const z2 = x0;
1265
1350
  return concatB(numberToBytesBE(z1, Fp.BYTES), numberToBytesBE(z2, Fp.BYTES));
package/src/bn.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  import { sha256 } from '@noble/hashes/sha256';
3
3
  import { weierstrass } from './abstract/weierstrass.js';
4
4
  import { getHash } from './_shortw_utils.js';
5
- import { Fp } from './abstract/modular.js';
5
+ import { Field } from './abstract/modular.js';
6
6
  /**
7
7
  * bn254 pairing-friendly curve.
8
8
  * Previously known as alt_bn_128, when it had 128-bit security.
@@ -12,7 +12,7 @@ import { Fp } from './abstract/modular.js';
12
12
  export const bn254 = weierstrass({
13
13
  a: BigInt(0),
14
14
  b: BigInt(3),
15
- Fp: Fp(BigInt('0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47')),
15
+ Fp: Field(BigInt('0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47')),
16
16
  n: BigInt('0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001'),
17
17
  Gx: BigInt(1),
18
18
  Gy: BigInt(2),
package/src/ed25519.ts CHANGED
@@ -3,7 +3,7 @@ import { sha512 } from '@noble/hashes/sha512';
3
3
  import { concatBytes, randomBytes, utf8ToBytes } from '@noble/hashes/utils';
4
4
  import { twistedEdwards, ExtPointType } from './abstract/edwards.js';
5
5
  import { montgomery } from './abstract/montgomery.js';
6
- import { mod, pow2, isNegativeLE, Fp as Field, FpSqrtEven } from './abstract/modular.js';
6
+ import { mod, pow2, isNegativeLE, Field, FpSqrtEven } from './abstract/modular.js';
7
7
  import {
8
8
  equalBytes,
9
9
  bytesToHex,
@@ -204,7 +204,7 @@ function map_to_curve_elligator2_curve25519(u: bigint) {
204
204
  let y = Fp.cmov(y2, y1, e3); // 36. y = CMOV(y2, y1, e3) # If e3, y = y1, else y = y2
205
205
  let e4 = Fp.isOdd(y); // 37. e4 = sgn0(y) == 1 # Fix sign of y
206
206
  y = Fp.cmov(y, Fp.neg(y), e3 !== e4); // 38. y = CMOV(y, -y, e3 XOR e4)
207
- return { xMn: xn, xMd: xd, yMn: y, yMd: 1n }; // 39. return (xn, xd, y, 1)
207
+ return { xMn: xn, xMd: xd, yMn: y, yMd: _1n }; // 39. return (xn, xd, y, 1)
208
208
  }
209
209
 
210
210
  const ELL2_C1_EDWARDS = FpSqrtEven(Fp, Fp.neg(BigInt(486664))); // sgn0(c1) MUST equal 0
package/src/ed448.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  import { shake256 } from '@noble/hashes/sha3';
3
3
  import { concatBytes, randomBytes, utf8ToBytes, wrapConstructor } from '@noble/hashes/utils';
4
4
  import { twistedEdwards } from './abstract/edwards.js';
5
- import { mod, pow2, Fp as Field } from './abstract/modular.js';
5
+ import { mod, pow2, Field } from './abstract/modular.js';
6
6
  import { montgomery } from './abstract/montgomery.js';
7
7
  import * as htf from './abstract/hash-to-curve.js';
8
8
 
@@ -54,6 +54,7 @@ function adjustScalarBytes(bytes: Uint8Array): Uint8Array {
54
54
  }
55
55
 
56
56
  const Fp = Field(ed448P, 456, true);
57
+ const _4n = BigInt(4);
57
58
 
58
59
  const ED448_DEF = {
59
60
  // Param: a
@@ -195,10 +196,10 @@ function map_to_curve_elligator2_edwards448(u: bigint) {
195
196
  xEn = Fp.mul(xEn, xd2); // 9. xEn = xEn * xd2
196
197
  xEn = Fp.mul(xEn, yd); // 10. xEn = xEn * yd
197
198
  xEn = Fp.mul(xEn, yn); // 11. xEn = xEn * yn
198
- xEn = Fp.mul(xEn, 4n); // 12. xEn = xEn * 4
199
+ xEn = Fp.mul(xEn, _4n); // 12. xEn = xEn * 4
199
200
  tv2 = Fp.mul(tv2, xn2); // 13. tv2 = tv2 * xn2
200
201
  tv2 = Fp.mul(tv2, yd2); // 14. tv2 = tv2 * yd2
201
- let tv3 = Fp.mul(yn2, 4n); // 15. tv3 = 4 * yn2
202
+ let tv3 = Fp.mul(yn2, _4n); // 15. tv3 = 4 * yn2
202
203
  let tv1 = Fp.add(tv3, yd2); // 16. tv1 = tv3 + yd2
203
204
  tv1 = Fp.mul(tv1, xd4); // 17. tv1 = tv1 * xd4
204
205
  let xEd = Fp.add(tv1, tv2); // 18. xEd = tv1 + tv2