@noble/curves
Advanced tools
Comparing version 0.9.0 to 0.9.1
@@ -47,2 +47,4 @@ import * as ut from './utils.js'; | ||
readonly et: bigint; | ||
get x(): bigint; | ||
get y(): bigint; | ||
assertValidity(): void; | ||
@@ -55,2 +57,4 @@ multiply(scalar: bigint): ExtPointType; | ||
toAffine(iz?: bigint): AffinePoint<bigint>; | ||
toRawBytes(isCompressed?: boolean): Uint8Array; | ||
toHex(isCompressed?: boolean): string; | ||
} | ||
@@ -57,0 +61,0 @@ export interface ExtPointConstructor extends GroupConstructor<ExtPointType> { |
@@ -10,7 +10,5 @@ "use strict"; | ||
const curve_js_1 = require("./curve.js"); | ||
// Be friendly to bad ECMAScript parsers by not using bigint literals like 123n | ||
const _0n = BigInt(0); | ||
const _1n = BigInt(1); | ||
const _2n = BigInt(2); | ||
const _8n = BigInt(8); | ||
// Be friendly to bad ECMAScript parsers by not using bigint literals | ||
// prettier-ignore | ||
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _8n = BigInt(8); | ||
function validateOpts(curve) { | ||
@@ -55,3 +53,3 @@ const opts = (0, curve_js_1.validateBasic)(curve); | ||
}); // NOOP | ||
const inBig = (n) => typeof n === 'bigint' && 0n < n; // n in [1..] | ||
const inBig = (n) => typeof n === 'bigint' && _0n < n; // n in [1..] | ||
const inRange = (n, max) => inBig(n) && inBig(max) && n < max; // n in [1..max-1] | ||
@@ -234,4 +232,5 @@ const in0MaskRange = (n) => n === _0n || inRange(n, MASK); // n in [0..MASK-1] | ||
// an exposed private key e.g. sig verification. | ||
// Does NOT allow scalars higher than CURVE.n. | ||
multiplyUnsafe(scalar) { | ||
let n = assertGE0(scalar); | ||
let n = assertGE0(scalar); // 0 <= scalar < CURVE.n | ||
if (n === _0n) | ||
@@ -382,4 +381,4 @@ return I; | ||
const R = Point.fromHex(sig.slice(0, len), false); // 0 <= R < 2^256: ZIP215 R can be >= P | ||
const s = ut.bytesToNumberLE(sig.slice(len, 2 * len)); // 0 <= s < l | ||
const SB = G.multiplyUnsafe(s); | ||
const s = ut.bytesToNumberLE(sig.slice(len, 2 * len)); | ||
const SB = G.multiplyUnsafe(s); // 0 <= s < l is done inside | ||
const k = hashDomainToScalar(context, R.toRawBytes(), A.toRawBytes(), msg); | ||
@@ -386,0 +385,0 @@ const RkA = R.add(A.multiplyUnsafe(k)); |
@@ -235,3 +235,3 @@ "use strict"; | ||
d = f.sqr(d); | ||
power >>= 1n; | ||
power >>= _1n; | ||
} | ||
@@ -238,0 +238,0 @@ return p; |
@@ -61,2 +61,4 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
readonly pz: T; | ||
get x(): T; | ||
get y(): T; | ||
multiply(scalar: bigint): ProjPointType<T>; | ||
@@ -63,0 +65,0 @@ toAffine(iz?: T): AffinePoint<T>; |
@@ -91,5 +91,5 @@ "use strict"; | ||
}; | ||
// Be friendly to bad ECMAScript parsers by not using bigint literals like 123n | ||
const _0n = BigInt(0); | ||
const _1n = BigInt(1); | ||
// Be friendly to bad ECMAScript parsers by not using bigint literals | ||
// prettier-ignore | ||
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4); | ||
function weierstrassPoints(opts) { | ||
@@ -276,3 +276,3 @@ const CURVE = validatePointOpts(opts); | ||
const { a, b } = CURVE; | ||
const b3 = Fp.mul(b, 3n); | ||
const b3 = Fp.mul(b, _3n); | ||
const { px: X1, py: Y1, pz: Z1 } = this; | ||
@@ -323,3 +323,3 @@ let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO; // prettier-ignore | ||
const a = CURVE.a; | ||
const b3 = Fp.mul(CURVE.b, 3n); | ||
const b3 = Fp.mul(CURVE.b, _3n); | ||
let t0 = Fp.mul(X1, X2); // step 1 | ||
@@ -931,12 +931,12 @@ let t1 = Fp.mul(Y1, Y2); | ||
const q = Fp.ORDER; | ||
let l = 0n; | ||
for (let o = q - 1n; o % 2n === 0n; o /= 2n) | ||
l += 1n; | ||
let l = _0n; | ||
for (let o = q - _1n; o % _2n === _0n; o /= _2n) | ||
l += _1n; | ||
const c1 = l; // 1. c1, the largest integer such that 2^c1 divides q - 1. | ||
const c2 = (q - 1n) / 2n ** c1; // 2. c2 = (q - 1) / (2^c1) # Integer arithmetic | ||
const c3 = (c2 - 1n) / 2n; // 3. c3 = (c2 - 1) / 2 # Integer arithmetic | ||
const c4 = 2n ** c1 - 1n; // 4. c4 = 2^c1 - 1 # Integer arithmetic | ||
const c5 = 2n ** (c1 - 1n); // 5. c5 = 2^(c1 - 1) # Integer arithmetic | ||
const c2 = (q - _1n) / _2n ** c1; // 2. c2 = (q - 1) / (2^c1) # Integer arithmetic | ||
const c3 = (c2 - _1n) / _2n; // 3. c3 = (c2 - 1) / 2 # Integer arithmetic | ||
const c4 = _2n ** c1 - _1n; // 4. c4 = 2^c1 - 1 # Integer arithmetic | ||
const c5 = _2n ** (c1 - _1n); // 5. c5 = 2^(c1 - 1) # Integer arithmetic | ||
const c6 = Fp.pow(Z, c2); // 6. c6 = Z^c2 | ||
const c7 = Fp.pow(Z, (c2 + 1n) / 2n); // 7. c7 = Z^((c2 + 1) / 2) | ||
const c7 = Fp.pow(Z, (c2 + _1n) / _2n); // 7. c7 = Z^((c2 + 1) / 2) | ||
let sqrtRatio = (u, v) => { | ||
@@ -961,3 +961,3 @@ let tv1 = c6; // 1. tv1 = c6 | ||
for (let i = c1; i > 1; i--) { | ||
let tv5 = 2n ** (i - 2n); // 18. tv5 = i - 2; 19. tv5 = 2^tv5 | ||
let tv5 = _2n ** (i - _2n); // 18. tv5 = i - 2; 19. tv5 = 2^tv5 | ||
let tvv5 = Fp.pow(tv4, tv5); // 20. tv5 = tv4^tv5 | ||
@@ -973,5 +973,5 @@ const e1 = Fp.eql(tvv5, Fp.ONE); // 21. e1 = tv5 == 1 | ||
}; | ||
if (Fp.ORDER % 4n === 3n) { | ||
if (Fp.ORDER % _4n === _3n) { | ||
// sqrt_ratio_3mod4(u, v) | ||
const c1 = (Fp.ORDER - 3n) / 4n; // 1. c1 = (q - 3) / 4 # Integer arithmetic | ||
const c1 = (Fp.ORDER - _3n) / _4n; // 1. c1 = (q - 3) / 4 # Integer arithmetic | ||
const c2 = Fp.sqrt(Fp.neg(Z)); // 2. c2 = sqrt(-Z) | ||
@@ -992,3 +992,3 @@ sqrtRatio = (u, v) => { | ||
// No curves uses that | ||
// if (Fp.ORDER % 8n === 5n) // sqrt_ratio_5mod8 | ||
// if (Fp.ORDER % _8n === _5n) // sqrt_ratio_5mod8 | ||
return sqrtRatio; | ||
@@ -995,0 +995,0 @@ } |
215
bls12-381.js
@@ -57,8 +57,12 @@ "use strict"; | ||
const hash_to_curve_js_1 = require("./abstract/hash-to-curve.js"); | ||
// Be friendly to bad ECMAScript parsers by not using bigint literals | ||
// prettier-ignore | ||
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4); | ||
const _8n = BigInt(8), _16n = BigInt(16); | ||
// CURVE FIELDS | ||
// Finite field over p. | ||
const Fp = mod.Field(0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn); | ||
const Fp = mod.Field(BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab')); | ||
// Finite field over r. | ||
// This particular field is not used anywhere in bls12-381, but it is still useful. | ||
const Fr = mod.Field(0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001n); | ||
const Fr = mod.Field(BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001')); | ||
const Fp2Add = ({ c0, c1 }, { c0: r0, c1: r1 }) => ({ | ||
@@ -95,4 +99,3 @@ c0: Fp.add(c0, r0), | ||
// NOTE: ORDER was wrong! | ||
const FP2_ORDER = 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn ** | ||
2n; | ||
const FP2_ORDER = BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab') ** _2n; | ||
const Fp2 = { | ||
@@ -150,3 +153,3 @@ ORDER: FP2_ORDER, | ||
// Inspired by https://github.com/dalek-cryptography/curve25519-dalek/blob/17698df9d4c834204f83a3574143abacb4fc81a5/src/field.rs#L99 | ||
const candidateSqrt = Fp2.pow(num, (Fp2.ORDER + 8n) / 16n); | ||
const candidateSqrt = Fp2.pow(num, (Fp2.ORDER + _8n) / _16n); | ||
const check = Fp2.div(Fp2.sqr(candidateSqrt), num); // candidateSqrt.square().div(this); | ||
@@ -172,6 +175,6 @@ const R = FP2_ROOTS_OF_UNITY; | ||
const { re: x0, im: x1 } = Fp2.reim(x); | ||
const sign_0 = x0 % 2n; | ||
const zero_0 = x0 === 0n; | ||
const sign_1 = x1 % 2n; | ||
return BigInt(sign_0 || (zero_0 && sign_1)) == 1n; | ||
const sign_0 = x0 % _2n; | ||
const zero_0 = x0 === _0n; | ||
const sign_1 = x1 % _2n; | ||
return BigInt(sign_0 || (zero_0 && sign_1)) == _1n; | ||
}, | ||
@@ -197,4 +200,4 @@ // Bytes util | ||
multiplyByB: ({ c0, c1 }) => { | ||
let t0 = Fp.mul(c0, 4n); // 4 * c0 | ||
let t1 = Fp.mul(c1, 4n); // 4 * c1 | ||
let t0 = Fp.mul(c0, _4n); // 4 * c0 | ||
let t1 = Fp.mul(c1, _4n); // 4 * c1 | ||
// (T0-T1) + (T0+T1)*i | ||
@@ -217,15 +220,15 @@ return { c0: Fp.sub(t0, t1), c1: Fp.add(t0, t1) }; | ||
const FP2_FROBENIUS_COEFFICIENTS = [ | ||
0x1n, | ||
0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaan, | ||
BigInt('0x1'), | ||
BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'), | ||
].map((item) => Fp.create(item)); | ||
// For Fp2 roots of unity. | ||
const rv1 = 0x6af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09n; | ||
const rv1 = BigInt('0x6af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09'); | ||
// const ev1 = | ||
// 0x699be3b8c6870965e5bf892ad5d2cc7b0e85a117402dfd83b7f4a947e02d978498255a2aaec0ac627b5afbdf1bf1c90n; | ||
// BigInt('0x699be3b8c6870965e5bf892ad5d2cc7b0e85a117402dfd83b7f4a947e02d978498255a2aaec0ac627b5afbdf1bf1c90'); | ||
// const ev2 = | ||
// 0x8157cd83046453f5dd0972b6e3949e4288020b5b8a9cc99ca07e27089a2ce2436d965026adad3ef7baba37f2183e9b5n; | ||
// BigInt('0x8157cd83046453f5dd0972b6e3949e4288020b5b8a9cc99ca07e27089a2ce2436d965026adad3ef7baba37f2183e9b5'); | ||
// const ev3 = | ||
// 0xab1c2ffdd6c253ca155231eb3e71ba044fd562f6f72bc5bad5ec46a0b7a3b0247cf08ce6c6317f40edbc653a72dee17n; | ||
// BigInt('0xab1c2ffdd6c253ca155231eb3e71ba044fd562f6f72bc5bad5ec46a0b7a3b0247cf08ce6c6317f40edbc653a72dee17'); | ||
// const ev4 = | ||
// 0xaa404866706722864480885d68ad0ccac1967c7544b447873cc37e0181271e006df72162a3d3e0287bf597fbf7f8fc1n; | ||
// BigInt('0xaa404866706722864480885d68ad0ccac1967c7544b447873cc37e0181271e006df72162a3d3e0287bf597fbf7f8fc1'); | ||
// Eighth roots of unity, used for computing square roots in Fp2. | ||
@@ -235,9 +238,9 @@ // To verify or re-calculate: | ||
const FP2_ROOTS_OF_UNITY = [ | ||
[1n, 0n], | ||
[_1n, _0n], | ||
[rv1, -rv1], | ||
[0n, 1n], | ||
[_0n, _1n], | ||
[rv1, rv1], | ||
[-1n, 0n], | ||
[-_1n, _0n], | ||
[-rv1, rv1], | ||
[0n, -1n], | ||
[_0n, -_1n], | ||
[-rv1, -rv1], | ||
@@ -278,4 +281,4 @@ ].map((pair) => Fp2.fromBigTuple(pair)); | ||
let t0 = Fp2.sqr(c0); // c0² | ||
let t1 = Fp2.mul(Fp2.mul(c0, c1), 2n); // 2 * c0 * c1 | ||
let t3 = Fp2.mul(Fp2.mul(c1, c2), 2n); // 2 * c1 * c2 | ||
let t1 = Fp2.mul(Fp2.mul(c0, c1), _2n); // 2 * c0 * c1 | ||
let t3 = Fp2.mul(Fp2.mul(c1, c2), _2n); // 2 * c1 * c2 | ||
let t4 = Fp2.sqr(c2); // c2² | ||
@@ -390,46 +393,46 @@ return { | ||
const FP6_FROBENIUS_COEFFICIENTS_1 = [ | ||
[0x1n, 0x0n], | ||
[BigInt('0x1'), BigInt('0x0')], | ||
[ | ||
0x0n, | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn, | ||
BigInt('0x0'), | ||
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'), | ||
], | ||
[ | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen, | ||
0x0n, | ||
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'), | ||
BigInt('0x0'), | ||
], | ||
[0x0n, 0x1n], | ||
[BigInt('0x0'), BigInt('0x1')], | ||
[ | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn, | ||
0x0n, | ||
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x0n, | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen, | ||
BigInt('0x0'), | ||
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'), | ||
], | ||
].map((pair) => Fp2.fromBigTuple(pair)); | ||
const FP6_FROBENIUS_COEFFICIENTS_2 = [ | ||
[0x1n, 0x0n], | ||
[BigInt('0x1'), BigInt('0x0')], | ||
[ | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaadn, | ||
0x0n, | ||
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaad'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn, | ||
0x0n, | ||
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaan, | ||
0x0n, | ||
BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen, | ||
0x0n, | ||
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffffn, | ||
0x0n, | ||
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffff'), | ||
BigInt('0x0'), | ||
], | ||
].map((pair) => Fp2.fromBigTuple(pair)); | ||
// The BLS parameter x for BLS12-381 | ||
const BLS_X = 0xd201000000010000n; | ||
const BLS_X = BigInt('0xd201000000010000'); | ||
const BLS_X_LEN = (0, utils_js_1.bitLen)(BLS_X); | ||
@@ -571,10 +574,10 @@ const Fp12Add = ({ c0, c1 }, { c0: r0, c1: r1 }) => ({ | ||
c0: Fp6.create({ | ||
c0: Fp2.add(Fp2.mul(Fp2.sub(t3, c0c0), 2n), t3), | ||
c1: Fp2.add(Fp2.mul(Fp2.sub(t5, c0c1), 2n), t5), | ||
c2: Fp2.add(Fp2.mul(Fp2.sub(t7, c0c2), 2n), t7), | ||
c0: Fp2.add(Fp2.mul(Fp2.sub(t3, c0c0), _2n), t3), | ||
c1: Fp2.add(Fp2.mul(Fp2.sub(t5, c0c1), _2n), t5), | ||
c2: Fp2.add(Fp2.mul(Fp2.sub(t7, c0c2), _2n), t7), | ||
}), | ||
c1: Fp6.create({ | ||
c0: Fp2.add(Fp2.mul(Fp2.add(t9, c1c0), 2n), t9), | ||
c1: Fp2.add(Fp2.mul(Fp2.add(t4, c1c1), 2n), t4), | ||
c2: Fp2.add(Fp2.mul(Fp2.add(t6, c1c2), 2n), t6), | ||
c0: Fp2.add(Fp2.mul(Fp2.add(t9, c1c0), _2n), t9), | ||
c1: Fp2.add(Fp2.mul(Fp2.add(t4, c1c1), _2n), t4), | ||
c2: Fp2.add(Fp2.mul(Fp2.add(t6, c1c2), _2n), t6), | ||
}), | ||
@@ -615,46 +618,46 @@ }; // 2 * (T6 + c1c2) + T6 | ||
const FP12_FROBENIUS_COEFFICIENTS = [ | ||
[0x1n, 0x0n], | ||
[BigInt('0x1'), BigInt('0x0')], | ||
[ | ||
0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8n, | ||
0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3n, | ||
BigInt('0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8'), | ||
BigInt('0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3'), | ||
], | ||
[ | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffffn, | ||
0x0n, | ||
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffff'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2n, | ||
0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09n, | ||
BigInt('0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2'), | ||
BigInt('0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09'), | ||
], | ||
[ | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen, | ||
0x0n, | ||
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995n, | ||
0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116n, | ||
BigInt('0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995'), | ||
BigInt('0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116'), | ||
], | ||
[ | ||
0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaan, | ||
0x0n, | ||
BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3n, | ||
0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8n, | ||
BigInt('0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3'), | ||
BigInt('0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8'), | ||
], | ||
[ | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn, | ||
0x0n, | ||
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09n, | ||
0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2n, | ||
BigInt('0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09'), | ||
BigInt('0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2'), | ||
], | ||
[ | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaadn, | ||
0x0n, | ||
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaad'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116n, | ||
0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995n, | ||
BigInt('0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116'), | ||
BigInt('0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995'), | ||
], | ||
@@ -806,3 +809,3 @@ ].map((n) => Fp2.fromBigTuple(n)); | ||
const G2_SWU = (0, weierstrass_js_1.mapToCurveSimpleSWU)(Fp2, { | ||
A: Fp2.create({ c0: Fp.create(0n), c1: Fp.create(240n) }), | ||
A: Fp2.create({ c0: Fp.create(_0n), c1: Fp.create(240n) }), | ||
B: Fp2.create({ c0: Fp.create(1012n), c1: Fp.create(1012n) }), | ||
@@ -813,4 +816,4 @@ Z: Fp2.create({ c0: Fp.create(-2n), c1: Fp.create(-1n) }), // Z: -(2 + I) | ||
const G1_SWU = (0, weierstrass_js_1.mapToCurveSimpleSWU)(Fp, { | ||
A: Fp.create(0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1dn), | ||
B: Fp.create(0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0n), | ||
A: Fp.create(BigInt('0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1d')), | ||
B: Fp.create(BigInt('0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0')), | ||
Z: Fp.create(11n), | ||
@@ -838,3 +841,3 @@ }); | ||
// 1 / F2(2)^((p-1)/3) in GF(p²) | ||
const PSI2_C1 = 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn; | ||
const PSI2_C1 = BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'); | ||
function psi2(x, y) { | ||
@@ -912,10 +915,10 @@ return [Fp2.mul(x, PSI2_C1), Fp2.neg(y)]; | ||
// cofactor; (z - 1)²/3 | ||
h: 0x396c8c005555e1568c00aaab0000aaabn, | ||
h: BigInt('0x396c8c005555e1568c00aaab0000aaab'), | ||
// generator's coordinates | ||
// x = 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507 | ||
// y = 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569 | ||
Gx: 0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bbn, | ||
Gy: 0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1n, | ||
Gx: BigInt('0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb'), | ||
Gy: BigInt('0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1'), | ||
a: Fp.ZERO, | ||
b: 4n, | ||
b: _4n, | ||
htfDefaults: { ...htfDefaults, m: 1 }, | ||
@@ -930,3 +933,3 @@ wrapPrivateKey: true, | ||
// φ endomorphism | ||
const cubicRootOfUnityModP = 0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen; | ||
const cubicRootOfUnityModP = BigInt('0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'); | ||
const phi = new c(Fp.mul(point.px, cubicRootOfUnityModP), point.py, point.pz); | ||
@@ -939,3 +942,3 @@ // todo: unroll | ||
// (z² − 1)/3 | ||
// const c1 = 0x396c8c005555e1560000000055555555n; | ||
// const c1 = BigInt('0x396c8c005555e1560000000055555555'); | ||
// const P = this; | ||
@@ -966,6 +969,6 @@ // const S = P.sigma(); | ||
// Zero | ||
if (bflag === 1n) | ||
return { x: 0n, y: 0n }; | ||
if (bflag === _1n) | ||
return { x: _0n, y: _0n }; | ||
const x = Fp.create(compressedValue & Fp.MASK); | ||
const right = Fp.add(Fp.pow(x, 3n), Fp.create(exports.bls12_381.CURVE.G1.b)); // y² = x³ + b | ||
const right = Fp.add(Fp.pow(x, _3n), Fp.create(exports.bls12_381.CURVE.G1.b)); // y² = x³ + b | ||
let y = Fp.sqrt(right); | ||
@@ -975,3 +978,3 @@ if (!y) | ||
const aflag = (0, utils_js_1.bitGet)(compressedValue, C_BIT_POS); | ||
if ((y * 2n) / P !== aflag) | ||
if ((y * _2n) / P !== aflag) | ||
y = Fp.neg(y); | ||
@@ -1000,3 +1003,3 @@ return { x: Fp.create(x), y: Fp.create(y) }; | ||
let num; | ||
num = (0, utils_js_1.bitSet)(x, C_BIT_POS, Boolean((y * 2n) / P)); // set aflag | ||
num = (0, utils_js_1.bitSet)(x, C_BIT_POS, Boolean((y * _2n) / P)); // set aflag | ||
num = (0, utils_js_1.bitSet)(num, S_BIT_POS, true); | ||
@@ -1024,6 +1027,6 @@ return (0, utils_js_1.numberToBytesBE)(num, Fp.BYTES); | ||
// cofactor | ||
h: 0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5n, | ||
h: BigInt('0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5'), | ||
Gx: Fp2.fromBigTuple([ | ||
0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8n, | ||
0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7en, | ||
BigInt('0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8'), | ||
BigInt('0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e'), | ||
]), | ||
@@ -1034,8 +1037,8 @@ // y = | ||
Gy: Fp2.fromBigTuple([ | ||
0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801n, | ||
0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79ben, | ||
BigInt('0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801'), | ||
BigInt('0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be'), | ||
]), | ||
a: Fp2.ZERO, | ||
b: Fp2.fromBigTuple([4n, 4n]), | ||
hEff: 0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551n, | ||
b: Fp2.fromBigTuple([4n, _4n]), | ||
hEff: BigInt('0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551'), | ||
htfDefaults: { ...htfDefaults }, | ||
@@ -1100,5 +1103,5 @@ wrapPrivateKey: true, | ||
const x = Fp2.create({ c0: Fp.create(x_0), c1: Fp.create(x_1) }); | ||
const right = Fp2.add(Fp2.pow(x, 3n), b); // y² = x³ + 4 * (u+1) = x³ + b | ||
const right = Fp2.add(Fp2.pow(x, _3n), b); // y² = x³ + 4 * (u+1) = x³ + b | ||
let y = Fp2.sqrt(right); | ||
const Y_bit = y.c1 === 0n ? (y.c0 * 2n) / P : (y.c1 * 2n) / P ? 1n : 0n; | ||
const Y_bit = y.c1 === _0n ? (y.c0 * _2n) / P : (y.c1 * _2n) / P ? _1n : _0n; | ||
y = bitS > 0 && Y_bit > 0 ? y : Fp2.neg(y); | ||
@@ -1129,3 +1132,3 @@ return { x, y }; | ||
return (0, utils_js_1.concatBytes)(COMPRESSED_ZERO, (0, utils_js_1.numberToBytesBE)(0n, Fp.BYTES)); | ||
const flag = Boolean(y.c1 === 0n ? (y.c0 * 2n) / P : (y.c1 * 2n) / P); | ||
const flag = Boolean(y.c1 === _0n ? (y.c0 * _2n) / P : (y.c1 * _2n) / P); | ||
// set compressed & sign bits (looks like different offsets than for G1/Fp?) | ||
@@ -1156,3 +1159,3 @@ let x_1 = (0, utils_js_1.bitSet)(x.c1, C_BIT_POS, flag); | ||
const bflag1 = (0, utils_js_1.bitGet)(z1, I_BIT_POS); | ||
if (bflag1 === 1n) | ||
if (bflag1 === _1n) | ||
return exports.bls12_381.G2.ProjectivePoint.ZERO; | ||
@@ -1162,3 +1165,3 @@ const x1 = Fp.create(z1 & Fp.MASK); | ||
const x = Fp2.create({ c0: x2, c1: x1 }); | ||
const y2 = Fp2.add(Fp2.pow(x, 3n), exports.bls12_381.CURVE.G2.b); // y² = x³ + 4 | ||
const y2 = Fp2.add(Fp2.pow(x, _3n), exports.bls12_381.CURVE.G2.b); // y² = x³ + 4 | ||
// The slow part | ||
@@ -1172,4 +1175,4 @@ let y = Fp2.sqrt(y2); | ||
const aflag1 = (0, utils_js_1.bitGet)(z1, 381); | ||
const isGreater = y1 > 0n && (y1 * 2n) / P !== aflag1; | ||
const isZero = y1 === 0n && (y0 * 2n) / P !== aflag1; | ||
const isGreater = y1 > _0n && (y1 * _2n) / P !== aflag1; | ||
const isZero = y1 === _0n && (y0 * _2n) / P !== aflag1; | ||
if (isGreater || isZero) | ||
@@ -1189,4 +1192,4 @@ y = Fp2.neg(y); | ||
const { re: y0, im: y1 } = Fp2.reim(a.y); | ||
const tmp = y1 > 0n ? y1 * 2n : y0 * 2n; | ||
const aflag1 = Boolean((tmp / Fp.ORDER) & 1n); | ||
const tmp = y1 > _0n ? y1 * _2n : y0 * _2n; | ||
const aflag1 = Boolean((tmp / Fp.ORDER) & _1n); | ||
const z1 = (0, utils_js_1.bitSet)((0, utils_js_1.bitSet)(x1, 381, aflag1), S_BIT_POS, true); | ||
@@ -1193,0 +1196,0 @@ const z2 = x0; |
@@ -185,3 +185,3 @@ "use strict"; | ||
y = Fp.cmov(y, Fp.neg(y), e3 !== e4); // 38. y = CMOV(y, -y, e3 XOR e4) | ||
return { xMn: xn, xMd: xd, yMn: y, yMd: 1n }; // 39. return (xn, xd, y, 1) | ||
return { xMn: xn, xMd: xd, yMn: y, yMd: _1n }; // 39. return (xn, xd, y, 1) | ||
} | ||
@@ -188,0 +188,0 @@ const ELL2_C1_EDWARDS = (0, modular_js_1.FpSqrtEven)(Fp, Fp.neg(BigInt(486664))); // sgn0(c1) MUST equal 0 |
@@ -53,2 +53,3 @@ "use strict"; | ||
const Fp = (0, modular_js_1.Field)(ed448P, 456, true); | ||
const _4n = BigInt(4); | ||
const ED448_DEF = { | ||
@@ -178,6 +179,6 @@ // Param: a | ||
xEn = Fp.mul(xEn, yn); // 11. xEn = xEn * yn | ||
xEn = Fp.mul(xEn, 4n); // 12. xEn = xEn * 4 | ||
xEn = Fp.mul(xEn, _4n); // 12. xEn = xEn * 4 | ||
tv2 = Fp.mul(tv2, xn2); // 13. tv2 = tv2 * xn2 | ||
tv2 = Fp.mul(tv2, yd2); // 14. tv2 = tv2 * yd2 | ||
let tv3 = Fp.mul(yn2, 4n); // 15. tv3 = 4 * yn2 | ||
let tv3 = Fp.mul(yn2, _4n); // 15. tv3 = 4 * yn2 | ||
let tv1 = Fp.add(tv3, yd2); // 16. tv1 = tv3 + yd2 | ||
@@ -184,0 +185,0 @@ tv1 = Fp.mul(tv1, xd4); // 17. tv1 = tv1 * xd4 |
@@ -7,7 +7,5 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
import { wNAF, validateBasic } from './curve.js'; | ||
// Be friendly to bad ECMAScript parsers by not using bigint literals like 123n | ||
const _0n = BigInt(0); | ||
const _1n = BigInt(1); | ||
const _2n = BigInt(2); | ||
const _8n = BigInt(8); | ||
// Be friendly to bad ECMAScript parsers by not using bigint literals | ||
// prettier-ignore | ||
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _8n = BigInt(8); | ||
function validateOpts(curve) { | ||
@@ -52,3 +50,3 @@ const opts = validateBasic(curve); | ||
}); // NOOP | ||
const inBig = (n) => typeof n === 'bigint' && 0n < n; // n in [1..] | ||
const inBig = (n) => typeof n === 'bigint' && _0n < n; // n in [1..] | ||
const inRange = (n, max) => inBig(n) && inBig(max) && n < max; // n in [1..max-1] | ||
@@ -231,4 +229,5 @@ const in0MaskRange = (n) => n === _0n || inRange(n, MASK); // n in [0..MASK-1] | ||
// an exposed private key e.g. sig verification. | ||
// Does NOT allow scalars higher than CURVE.n. | ||
multiplyUnsafe(scalar) { | ||
let n = assertGE0(scalar); | ||
let n = assertGE0(scalar); // 0 <= scalar < CURVE.n | ||
if (n === _0n) | ||
@@ -379,4 +378,4 @@ return I; | ||
const R = Point.fromHex(sig.slice(0, len), false); // 0 <= R < 2^256: ZIP215 R can be >= P | ||
const s = ut.bytesToNumberLE(sig.slice(len, 2 * len)); // 0 <= s < l | ||
const SB = G.multiplyUnsafe(s); | ||
const s = ut.bytesToNumberLE(sig.slice(len, 2 * len)); | ||
const SB = G.multiplyUnsafe(s); // 0 <= s < l is done inside | ||
const k = hashDomainToScalar(context, R.toRawBytes(), A.toRawBytes(), msg); | ||
@@ -383,0 +382,0 @@ const RkA = R.add(A.multiplyUnsafe(k)); |
@@ -224,3 +224,3 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
d = f.sqr(d); | ||
power >>= 1n; | ||
power >>= _1n; | ||
} | ||
@@ -227,0 +227,0 @@ return p; |
@@ -88,5 +88,5 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
}; | ||
// Be friendly to bad ECMAScript parsers by not using bigint literals like 123n | ||
const _0n = BigInt(0); | ||
const _1n = BigInt(1); | ||
// Be friendly to bad ECMAScript parsers by not using bigint literals | ||
// prettier-ignore | ||
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4); | ||
export function weierstrassPoints(opts) { | ||
@@ -273,3 +273,3 @@ const CURVE = validatePointOpts(opts); | ||
const { a, b } = CURVE; | ||
const b3 = Fp.mul(b, 3n); | ||
const b3 = Fp.mul(b, _3n); | ||
const { px: X1, py: Y1, pz: Z1 } = this; | ||
@@ -320,3 +320,3 @@ let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO; // prettier-ignore | ||
const a = CURVE.a; | ||
const b3 = Fp.mul(CURVE.b, 3n); | ||
const b3 = Fp.mul(CURVE.b, _3n); | ||
let t0 = Fp.mul(X1, X2); // step 1 | ||
@@ -926,12 +926,12 @@ let t1 = Fp.mul(Y1, Y2); | ||
const q = Fp.ORDER; | ||
let l = 0n; | ||
for (let o = q - 1n; o % 2n === 0n; o /= 2n) | ||
l += 1n; | ||
let l = _0n; | ||
for (let o = q - _1n; o % _2n === _0n; o /= _2n) | ||
l += _1n; | ||
const c1 = l; // 1. c1, the largest integer such that 2^c1 divides q - 1. | ||
const c2 = (q - 1n) / 2n ** c1; // 2. c2 = (q - 1) / (2^c1) # Integer arithmetic | ||
const c3 = (c2 - 1n) / 2n; // 3. c3 = (c2 - 1) / 2 # Integer arithmetic | ||
const c4 = 2n ** c1 - 1n; // 4. c4 = 2^c1 - 1 # Integer arithmetic | ||
const c5 = 2n ** (c1 - 1n); // 5. c5 = 2^(c1 - 1) # Integer arithmetic | ||
const c2 = (q - _1n) / _2n ** c1; // 2. c2 = (q - 1) / (2^c1) # Integer arithmetic | ||
const c3 = (c2 - _1n) / _2n; // 3. c3 = (c2 - 1) / 2 # Integer arithmetic | ||
const c4 = _2n ** c1 - _1n; // 4. c4 = 2^c1 - 1 # Integer arithmetic | ||
const c5 = _2n ** (c1 - _1n); // 5. c5 = 2^(c1 - 1) # Integer arithmetic | ||
const c6 = Fp.pow(Z, c2); // 6. c6 = Z^c2 | ||
const c7 = Fp.pow(Z, (c2 + 1n) / 2n); // 7. c7 = Z^((c2 + 1) / 2) | ||
const c7 = Fp.pow(Z, (c2 + _1n) / _2n); // 7. c7 = Z^((c2 + 1) / 2) | ||
let sqrtRatio = (u, v) => { | ||
@@ -956,3 +956,3 @@ let tv1 = c6; // 1. tv1 = c6 | ||
for (let i = c1; i > 1; i--) { | ||
let tv5 = 2n ** (i - 2n); // 18. tv5 = i - 2; 19. tv5 = 2^tv5 | ||
let tv5 = _2n ** (i - _2n); // 18. tv5 = i - 2; 19. tv5 = 2^tv5 | ||
let tvv5 = Fp.pow(tv4, tv5); // 20. tv5 = tv4^tv5 | ||
@@ -968,5 +968,5 @@ const e1 = Fp.eql(tvv5, Fp.ONE); // 21. e1 = tv5 == 1 | ||
}; | ||
if (Fp.ORDER % 4n === 3n) { | ||
if (Fp.ORDER % _4n === _3n) { | ||
// sqrt_ratio_3mod4(u, v) | ||
const c1 = (Fp.ORDER - 3n) / 4n; // 1. c1 = (q - 3) / 4 # Integer arithmetic | ||
const c1 = (Fp.ORDER - _3n) / _4n; // 1. c1 = (q - 3) / 4 # Integer arithmetic | ||
const c2 = Fp.sqrt(Fp.neg(Z)); // 2. c2 = sqrt(-Z) | ||
@@ -987,3 +987,3 @@ sqrtRatio = (u, v) => { | ||
// No curves uses that | ||
// if (Fp.ORDER % 8n === 5n) // sqrt_ratio_5mod8 | ||
// if (Fp.ORDER % _8n === _5n) // sqrt_ratio_5mod8 | ||
return sqrtRatio; | ||
@@ -990,0 +990,0 @@ } |
@@ -54,8 +54,12 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
import { isogenyMap } from './abstract/hash-to-curve.js'; | ||
// Be friendly to bad ECMAScript parsers by not using bigint literals | ||
// prettier-ignore | ||
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4); | ||
const _8n = BigInt(8), _16n = BigInt(16); | ||
// CURVE FIELDS | ||
// Finite field over p. | ||
const Fp = mod.Field(0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn); | ||
const Fp = mod.Field(BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab')); | ||
// Finite field over r. | ||
// This particular field is not used anywhere in bls12-381, but it is still useful. | ||
const Fr = mod.Field(0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001n); | ||
const Fr = mod.Field(BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001')); | ||
const Fp2Add = ({ c0, c1 }, { c0: r0, c1: r1 }) => ({ | ||
@@ -92,4 +96,3 @@ c0: Fp.add(c0, r0), | ||
// NOTE: ORDER was wrong! | ||
const FP2_ORDER = 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn ** | ||
2n; | ||
const FP2_ORDER = BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab') ** _2n; | ||
const Fp2 = { | ||
@@ -147,3 +150,3 @@ ORDER: FP2_ORDER, | ||
// Inspired by https://github.com/dalek-cryptography/curve25519-dalek/blob/17698df9d4c834204f83a3574143abacb4fc81a5/src/field.rs#L99 | ||
const candidateSqrt = Fp2.pow(num, (Fp2.ORDER + 8n) / 16n); | ||
const candidateSqrt = Fp2.pow(num, (Fp2.ORDER + _8n) / _16n); | ||
const check = Fp2.div(Fp2.sqr(candidateSqrt), num); // candidateSqrt.square().div(this); | ||
@@ -169,6 +172,6 @@ const R = FP2_ROOTS_OF_UNITY; | ||
const { re: x0, im: x1 } = Fp2.reim(x); | ||
const sign_0 = x0 % 2n; | ||
const zero_0 = x0 === 0n; | ||
const sign_1 = x1 % 2n; | ||
return BigInt(sign_0 || (zero_0 && sign_1)) == 1n; | ||
const sign_0 = x0 % _2n; | ||
const zero_0 = x0 === _0n; | ||
const sign_1 = x1 % _2n; | ||
return BigInt(sign_0 || (zero_0 && sign_1)) == _1n; | ||
}, | ||
@@ -194,4 +197,4 @@ // Bytes util | ||
multiplyByB: ({ c0, c1 }) => { | ||
let t0 = Fp.mul(c0, 4n); // 4 * c0 | ||
let t1 = Fp.mul(c1, 4n); // 4 * c1 | ||
let t0 = Fp.mul(c0, _4n); // 4 * c0 | ||
let t1 = Fp.mul(c1, _4n); // 4 * c1 | ||
// (T0-T1) + (T0+T1)*i | ||
@@ -214,15 +217,15 @@ return { c0: Fp.sub(t0, t1), c1: Fp.add(t0, t1) }; | ||
const FP2_FROBENIUS_COEFFICIENTS = [ | ||
0x1n, | ||
0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaan, | ||
BigInt('0x1'), | ||
BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'), | ||
].map((item) => Fp.create(item)); | ||
// For Fp2 roots of unity. | ||
const rv1 = 0x6af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09n; | ||
const rv1 = BigInt('0x6af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09'); | ||
// const ev1 = | ||
// 0x699be3b8c6870965e5bf892ad5d2cc7b0e85a117402dfd83b7f4a947e02d978498255a2aaec0ac627b5afbdf1bf1c90n; | ||
// BigInt('0x699be3b8c6870965e5bf892ad5d2cc7b0e85a117402dfd83b7f4a947e02d978498255a2aaec0ac627b5afbdf1bf1c90'); | ||
// const ev2 = | ||
// 0x8157cd83046453f5dd0972b6e3949e4288020b5b8a9cc99ca07e27089a2ce2436d965026adad3ef7baba37f2183e9b5n; | ||
// BigInt('0x8157cd83046453f5dd0972b6e3949e4288020b5b8a9cc99ca07e27089a2ce2436d965026adad3ef7baba37f2183e9b5'); | ||
// const ev3 = | ||
// 0xab1c2ffdd6c253ca155231eb3e71ba044fd562f6f72bc5bad5ec46a0b7a3b0247cf08ce6c6317f40edbc653a72dee17n; | ||
// BigInt('0xab1c2ffdd6c253ca155231eb3e71ba044fd562f6f72bc5bad5ec46a0b7a3b0247cf08ce6c6317f40edbc653a72dee17'); | ||
// const ev4 = | ||
// 0xaa404866706722864480885d68ad0ccac1967c7544b447873cc37e0181271e006df72162a3d3e0287bf597fbf7f8fc1n; | ||
// BigInt('0xaa404866706722864480885d68ad0ccac1967c7544b447873cc37e0181271e006df72162a3d3e0287bf597fbf7f8fc1'); | ||
// Eighth roots of unity, used for computing square roots in Fp2. | ||
@@ -232,9 +235,9 @@ // To verify or re-calculate: | ||
const FP2_ROOTS_OF_UNITY = [ | ||
[1n, 0n], | ||
[_1n, _0n], | ||
[rv1, -rv1], | ||
[0n, 1n], | ||
[_0n, _1n], | ||
[rv1, rv1], | ||
[-1n, 0n], | ||
[-_1n, _0n], | ||
[-rv1, rv1], | ||
[0n, -1n], | ||
[_0n, -_1n], | ||
[-rv1, -rv1], | ||
@@ -275,4 +278,4 @@ ].map((pair) => Fp2.fromBigTuple(pair)); | ||
let t0 = Fp2.sqr(c0); // c0² | ||
let t1 = Fp2.mul(Fp2.mul(c0, c1), 2n); // 2 * c0 * c1 | ||
let t3 = Fp2.mul(Fp2.mul(c1, c2), 2n); // 2 * c1 * c2 | ||
let t1 = Fp2.mul(Fp2.mul(c0, c1), _2n); // 2 * c0 * c1 | ||
let t3 = Fp2.mul(Fp2.mul(c1, c2), _2n); // 2 * c1 * c2 | ||
let t4 = Fp2.sqr(c2); // c2² | ||
@@ -387,46 +390,46 @@ return { | ||
const FP6_FROBENIUS_COEFFICIENTS_1 = [ | ||
[0x1n, 0x0n], | ||
[BigInt('0x1'), BigInt('0x0')], | ||
[ | ||
0x0n, | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn, | ||
BigInt('0x0'), | ||
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'), | ||
], | ||
[ | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen, | ||
0x0n, | ||
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'), | ||
BigInt('0x0'), | ||
], | ||
[0x0n, 0x1n], | ||
[BigInt('0x0'), BigInt('0x1')], | ||
[ | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn, | ||
0x0n, | ||
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x0n, | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen, | ||
BigInt('0x0'), | ||
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'), | ||
], | ||
].map((pair) => Fp2.fromBigTuple(pair)); | ||
const FP6_FROBENIUS_COEFFICIENTS_2 = [ | ||
[0x1n, 0x0n], | ||
[BigInt('0x1'), BigInt('0x0')], | ||
[ | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaadn, | ||
0x0n, | ||
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaad'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn, | ||
0x0n, | ||
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaan, | ||
0x0n, | ||
BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen, | ||
0x0n, | ||
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffffn, | ||
0x0n, | ||
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffff'), | ||
BigInt('0x0'), | ||
], | ||
].map((pair) => Fp2.fromBigTuple(pair)); | ||
// The BLS parameter x for BLS12-381 | ||
const BLS_X = 0xd201000000010000n; | ||
const BLS_X = BigInt('0xd201000000010000'); | ||
const BLS_X_LEN = bitLen(BLS_X); | ||
@@ -568,10 +571,10 @@ const Fp12Add = ({ c0, c1 }, { c0: r0, c1: r1 }) => ({ | ||
c0: Fp6.create({ | ||
c0: Fp2.add(Fp2.mul(Fp2.sub(t3, c0c0), 2n), t3), | ||
c1: Fp2.add(Fp2.mul(Fp2.sub(t5, c0c1), 2n), t5), | ||
c2: Fp2.add(Fp2.mul(Fp2.sub(t7, c0c2), 2n), t7), | ||
c0: Fp2.add(Fp2.mul(Fp2.sub(t3, c0c0), _2n), t3), | ||
c1: Fp2.add(Fp2.mul(Fp2.sub(t5, c0c1), _2n), t5), | ||
c2: Fp2.add(Fp2.mul(Fp2.sub(t7, c0c2), _2n), t7), | ||
}), | ||
c1: Fp6.create({ | ||
c0: Fp2.add(Fp2.mul(Fp2.add(t9, c1c0), 2n), t9), | ||
c1: Fp2.add(Fp2.mul(Fp2.add(t4, c1c1), 2n), t4), | ||
c2: Fp2.add(Fp2.mul(Fp2.add(t6, c1c2), 2n), t6), | ||
c0: Fp2.add(Fp2.mul(Fp2.add(t9, c1c0), _2n), t9), | ||
c1: Fp2.add(Fp2.mul(Fp2.add(t4, c1c1), _2n), t4), | ||
c2: Fp2.add(Fp2.mul(Fp2.add(t6, c1c2), _2n), t6), | ||
}), | ||
@@ -612,46 +615,46 @@ }; // 2 * (T6 + c1c2) + T6 | ||
const FP12_FROBENIUS_COEFFICIENTS = [ | ||
[0x1n, 0x0n], | ||
[BigInt('0x1'), BigInt('0x0')], | ||
[ | ||
0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8n, | ||
0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3n, | ||
BigInt('0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8'), | ||
BigInt('0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3'), | ||
], | ||
[ | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffffn, | ||
0x0n, | ||
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffff'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2n, | ||
0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09n, | ||
BigInt('0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2'), | ||
BigInt('0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09'), | ||
], | ||
[ | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen, | ||
0x0n, | ||
BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995n, | ||
0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116n, | ||
BigInt('0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995'), | ||
BigInt('0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116'), | ||
], | ||
[ | ||
0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaan, | ||
0x0n, | ||
BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3n, | ||
0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8n, | ||
BigInt('0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3'), | ||
BigInt('0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8'), | ||
], | ||
[ | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn, | ||
0x0n, | ||
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09n, | ||
0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2n, | ||
BigInt('0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09'), | ||
BigInt('0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2'), | ||
], | ||
[ | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaadn, | ||
0x0n, | ||
BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaad'), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116n, | ||
0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995n, | ||
BigInt('0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116'), | ||
BigInt('0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995'), | ||
], | ||
@@ -803,3 +806,3 @@ ].map((n) => Fp2.fromBigTuple(n)); | ||
const G2_SWU = mapToCurveSimpleSWU(Fp2, { | ||
A: Fp2.create({ c0: Fp.create(0n), c1: Fp.create(240n) }), | ||
A: Fp2.create({ c0: Fp.create(_0n), c1: Fp.create(240n) }), | ||
B: Fp2.create({ c0: Fp.create(1012n), c1: Fp.create(1012n) }), | ||
@@ -810,4 +813,4 @@ Z: Fp2.create({ c0: Fp.create(-2n), c1: Fp.create(-1n) }), // Z: -(2 + I) | ||
const G1_SWU = mapToCurveSimpleSWU(Fp, { | ||
A: Fp.create(0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1dn), | ||
B: Fp.create(0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0n), | ||
A: Fp.create(BigInt('0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1d')), | ||
B: Fp.create(BigInt('0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0')), | ||
Z: Fp.create(11n), | ||
@@ -835,3 +838,3 @@ }); | ||
// 1 / F2(2)^((p-1)/3) in GF(p²) | ||
const PSI2_C1 = 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn; | ||
const PSI2_C1 = BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'); | ||
function psi2(x, y) { | ||
@@ -909,10 +912,10 @@ return [Fp2.mul(x, PSI2_C1), Fp2.neg(y)]; | ||
// cofactor; (z - 1)²/3 | ||
h: 0x396c8c005555e1568c00aaab0000aaabn, | ||
h: BigInt('0x396c8c005555e1568c00aaab0000aaab'), | ||
// generator's coordinates | ||
// x = 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507 | ||
// y = 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569 | ||
Gx: 0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bbn, | ||
Gy: 0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1n, | ||
Gx: BigInt('0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb'), | ||
Gy: BigInt('0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1'), | ||
a: Fp.ZERO, | ||
b: 4n, | ||
b: _4n, | ||
htfDefaults: { ...htfDefaults, m: 1 }, | ||
@@ -927,3 +930,3 @@ wrapPrivateKey: true, | ||
// φ endomorphism | ||
const cubicRootOfUnityModP = 0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen; | ||
const cubicRootOfUnityModP = BigInt('0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'); | ||
const phi = new c(Fp.mul(point.px, cubicRootOfUnityModP), point.py, point.pz); | ||
@@ -936,3 +939,3 @@ // todo: unroll | ||
// (z² − 1)/3 | ||
// const c1 = 0x396c8c005555e1560000000055555555n; | ||
// const c1 = BigInt('0x396c8c005555e1560000000055555555'); | ||
// const P = this; | ||
@@ -963,6 +966,6 @@ // const S = P.sigma(); | ||
// Zero | ||
if (bflag === 1n) | ||
return { x: 0n, y: 0n }; | ||
if (bflag === _1n) | ||
return { x: _0n, y: _0n }; | ||
const x = Fp.create(compressedValue & Fp.MASK); | ||
const right = Fp.add(Fp.pow(x, 3n), Fp.create(bls12_381.CURVE.G1.b)); // y² = x³ + b | ||
const right = Fp.add(Fp.pow(x, _3n), Fp.create(bls12_381.CURVE.G1.b)); // y² = x³ + b | ||
let y = Fp.sqrt(right); | ||
@@ -972,3 +975,3 @@ if (!y) | ||
const aflag = bitGet(compressedValue, C_BIT_POS); | ||
if ((y * 2n) / P !== aflag) | ||
if ((y * _2n) / P !== aflag) | ||
y = Fp.neg(y); | ||
@@ -997,3 +1000,3 @@ return { x: Fp.create(x), y: Fp.create(y) }; | ||
let num; | ||
num = bitSet(x, C_BIT_POS, Boolean((y * 2n) / P)); // set aflag | ||
num = bitSet(x, C_BIT_POS, Boolean((y * _2n) / P)); // set aflag | ||
num = bitSet(num, S_BIT_POS, true); | ||
@@ -1021,6 +1024,6 @@ return numberToBytesBE(num, Fp.BYTES); | ||
// cofactor | ||
h: 0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5n, | ||
h: BigInt('0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5'), | ||
Gx: Fp2.fromBigTuple([ | ||
0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8n, | ||
0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7en, | ||
BigInt('0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8'), | ||
BigInt('0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e'), | ||
]), | ||
@@ -1031,8 +1034,8 @@ // y = | ||
Gy: Fp2.fromBigTuple([ | ||
0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801n, | ||
0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79ben, | ||
BigInt('0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801'), | ||
BigInt('0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be'), | ||
]), | ||
a: Fp2.ZERO, | ||
b: Fp2.fromBigTuple([4n, 4n]), | ||
hEff: 0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551n, | ||
b: Fp2.fromBigTuple([4n, _4n]), | ||
hEff: BigInt('0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551'), | ||
htfDefaults: { ...htfDefaults }, | ||
@@ -1097,5 +1100,5 @@ wrapPrivateKey: true, | ||
const x = Fp2.create({ c0: Fp.create(x_0), c1: Fp.create(x_1) }); | ||
const right = Fp2.add(Fp2.pow(x, 3n), b); // y² = x³ + 4 * (u+1) = x³ + b | ||
const right = Fp2.add(Fp2.pow(x, _3n), b); // y² = x³ + 4 * (u+1) = x³ + b | ||
let y = Fp2.sqrt(right); | ||
const Y_bit = y.c1 === 0n ? (y.c0 * 2n) / P : (y.c1 * 2n) / P ? 1n : 0n; | ||
const Y_bit = y.c1 === _0n ? (y.c0 * _2n) / P : (y.c1 * _2n) / P ? _1n : _0n; | ||
y = bitS > 0 && Y_bit > 0 ? y : Fp2.neg(y); | ||
@@ -1126,3 +1129,3 @@ return { x, y }; | ||
return concatB(COMPRESSED_ZERO, numberToBytesBE(0n, Fp.BYTES)); | ||
const flag = Boolean(y.c1 === 0n ? (y.c0 * 2n) / P : (y.c1 * 2n) / P); | ||
const flag = Boolean(y.c1 === _0n ? (y.c0 * _2n) / P : (y.c1 * _2n) / P); | ||
// set compressed & sign bits (looks like different offsets than for G1/Fp?) | ||
@@ -1153,3 +1156,3 @@ let x_1 = bitSet(x.c1, C_BIT_POS, flag); | ||
const bflag1 = bitGet(z1, I_BIT_POS); | ||
if (bflag1 === 1n) | ||
if (bflag1 === _1n) | ||
return bls12_381.G2.ProjectivePoint.ZERO; | ||
@@ -1159,3 +1162,3 @@ const x1 = Fp.create(z1 & Fp.MASK); | ||
const x = Fp2.create({ c0: x2, c1: x1 }); | ||
const y2 = Fp2.add(Fp2.pow(x, 3n), bls12_381.CURVE.G2.b); // y² = x³ + 4 | ||
const y2 = Fp2.add(Fp2.pow(x, _3n), bls12_381.CURVE.G2.b); // y² = x³ + 4 | ||
// The slow part | ||
@@ -1169,4 +1172,4 @@ let y = Fp2.sqrt(y2); | ||
const aflag1 = bitGet(z1, 381); | ||
const isGreater = y1 > 0n && (y1 * 2n) / P !== aflag1; | ||
const isZero = y1 === 0n && (y0 * 2n) / P !== aflag1; | ||
const isGreater = y1 > _0n && (y1 * _2n) / P !== aflag1; | ||
const isZero = y1 === _0n && (y0 * _2n) / P !== aflag1; | ||
if (isGreater || isZero) | ||
@@ -1186,4 +1189,4 @@ y = Fp2.neg(y); | ||
const { re: y0, im: y1 } = Fp2.reim(a.y); | ||
const tmp = y1 > 0n ? y1 * 2n : y0 * 2n; | ||
const aflag1 = Boolean((tmp / Fp.ORDER) & 1n); | ||
const tmp = y1 > _0n ? y1 * _2n : y0 * _2n; | ||
const aflag1 = Boolean((tmp / Fp.ORDER) & _1n); | ||
const z1 = bitSet(bitSet(x1, 381, aflag1), S_BIT_POS, true); | ||
@@ -1190,0 +1193,0 @@ const z2 = x0; |
@@ -182,3 +182,3 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
y = Fp.cmov(y, Fp.neg(y), e3 !== e4); // 38. y = CMOV(y, -y, e3 XOR e4) | ||
return { xMn: xn, xMd: xd, yMn: y, yMd: 1n }; // 39. return (xn, xd, y, 1) | ||
return { xMn: xn, xMd: xd, yMn: y, yMd: _1n }; // 39. return (xn, xd, y, 1) | ||
} | ||
@@ -185,0 +185,0 @@ const ELL2_C1_EDWARDS = FpSqrtEven(Fp, Fp.neg(BigInt(486664))); // sgn0(c1) MUST equal 0 |
@@ -50,2 +50,3 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
const Fp = Field(ed448P, 456, true); | ||
const _4n = BigInt(4); | ||
const ED448_DEF = { | ||
@@ -175,6 +176,6 @@ // Param: a | ||
xEn = Fp.mul(xEn, yn); // 11. xEn = xEn * yn | ||
xEn = Fp.mul(xEn, 4n); // 12. xEn = xEn * 4 | ||
xEn = Fp.mul(xEn, _4n); // 12. xEn = xEn * 4 | ||
tv2 = Fp.mul(tv2, xn2); // 13. tv2 = tv2 * xn2 | ||
tv2 = Fp.mul(tv2, yd2); // 14. tv2 = tv2 * yd2 | ||
let tv3 = Fp.mul(yn2, 4n); // 15. tv3 = 4 * yn2 | ||
let tv3 = Fp.mul(yn2, _4n); // 15. tv3 = 4 * yn2 | ||
let tv1 = Fp.add(tv3, yd2); // 16. tv1 = tv3 + yd2 | ||
@@ -181,0 +182,0 @@ tv1 = Fp.mul(tv1, xd4); // 17. tv1 = tv1 * xd4 |
@@ -125,3 +125,3 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
let y = sqrtMod(c); // Let y = c^(p+1)/4 mod p. | ||
if (y % 2n !== 0n) | ||
if (y % _2n !== _0n) | ||
y = modP(-y); // Return the unique point P such that x(P) = x and | ||
@@ -128,0 +128,0 @@ const p = new Point(x, y, _1n); // y(P) = y if y mod 2 = 0 or y(P) = p-y otherwise. |
{ | ||
"name": "@noble/curves", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"description": "Audited & minimal JS implementation of elliptic curve cryptography", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -129,3 +129,3 @@ "use strict"; | ||
let y = sqrtMod(c); // Let y = c^(p+1)/4 mod p. | ||
if (y % 2n !== 0n) | ||
if (y % _2n !== _0n) | ||
y = modP(-y); // Return the unique point P such that x(P) = x and | ||
@@ -132,0 +132,0 @@ const p = new Point(x, y, _1n); // y(P) = y if y mod 2 = 0 or y(P) = p-y otherwise. |
@@ -8,7 +8,5 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
// Be friendly to bad ECMAScript parsers by not using bigint literals like 123n | ||
const _0n = BigInt(0); | ||
const _1n = BigInt(1); | ||
const _2n = BigInt(2); | ||
const _8n = BigInt(8); | ||
// Be friendly to bad ECMAScript parsers by not using bigint literals | ||
// prettier-ignore | ||
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _8n = BigInt(8); | ||
@@ -55,2 +53,4 @@ // Edwards curves must declare params a & d. | ||
readonly et: bigint; | ||
get x(): bigint; | ||
get y(): bigint; | ||
assertValidity(): void; | ||
@@ -63,2 +63,4 @@ multiply(scalar: bigint): ExtPointType; | ||
toAffine(iz?: bigint): AffinePoint<bigint>; | ||
toRawBytes(isCompressed?: boolean): Uint8Array; | ||
toHex(isCompressed?: boolean): string; | ||
} | ||
@@ -115,3 +117,3 @@ // Static methods of Extended Point with coordinates in X, Y, Z, T | ||
}); // NOOP | ||
const inBig = (n: bigint) => typeof n === 'bigint' && 0n < n; // n in [1..] | ||
const inBig = (n: bigint) => typeof n === 'bigint' && _0n < n; // n in [1..] | ||
const inRange = (n: bigint, max: bigint) => inBig(n) && inBig(max) && n < max; // n in [1..max-1] | ||
@@ -304,4 +306,5 @@ const in0MaskRange = (n: bigint) => n === _0n || inRange(n, MASK); // n in [0..MASK-1] | ||
// an exposed private key e.g. sig verification. | ||
// Does NOT allow scalars higher than CURVE.n. | ||
multiplyUnsafe(scalar: bigint): Point { | ||
let n = assertGE0(scalar); | ||
let n = assertGE0(scalar); // 0 <= scalar < CURVE.n | ||
if (n === _0n) return I; | ||
@@ -448,4 +451,4 @@ if (this.equals(I) || n === _1n) return this; | ||
const R = Point.fromHex(sig.slice(0, len), false); // 0 <= R < 2^256: ZIP215 R can be >= P | ||
const s = ut.bytesToNumberLE(sig.slice(len, 2 * len)); // 0 <= s < l | ||
const SB = G.multiplyUnsafe(s); | ||
const s = ut.bytesToNumberLE(sig.slice(len, 2 * len)); | ||
const SB = G.multiplyUnsafe(s); // 0 <= s < l is done inside | ||
const k = hashDomainToScalar(context, R.toRawBytes(), A.toRawBytes(), msg); | ||
@@ -452,0 +455,0 @@ const RkA = R.add(A.multiplyUnsafe(k)); |
@@ -278,3 +278,3 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
d = f.sqr(d); | ||
power >>= 1n; | ||
power >>= _1n; | ||
} | ||
@@ -281,0 +281,0 @@ return p; |
@@ -61,2 +61,4 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
readonly pz: T; | ||
get x(): T; | ||
get y(): T; | ||
multiply(scalar: bigint): ProjPointType<T>; | ||
@@ -180,5 +182,5 @@ toAffine(iz?: T): AffinePoint<T>; | ||
// Be friendly to bad ECMAScript parsers by not using bigint literals like 123n | ||
const _0n = BigInt(0); | ||
const _1n = BigInt(1); | ||
// Be friendly to bad ECMAScript parsers by not using bigint literals | ||
// prettier-ignore | ||
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4); | ||
@@ -370,3 +372,3 @@ export function weierstrassPoints<T>(opts: CurvePointsType<T>) { | ||
const { a, b } = CURVE; | ||
const b3 = Fp.mul(b, 3n); | ||
const b3 = Fp.mul(b, _3n); | ||
const { px: X1, py: Y1, pz: Z1 } = this; | ||
@@ -418,3 +420,3 @@ let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO; // prettier-ignore | ||
const a = CURVE.a; | ||
const b3 = Fp.mul(CURVE.b, 3n); | ||
const b3 = Fp.mul(CURVE.b, _3n); | ||
let t0 = Fp.mul(X1, X2); // step 1 | ||
@@ -1085,11 +1087,11 @@ let t1 = Fp.mul(Y1, Y2); | ||
const q = Fp.ORDER; | ||
let l = 0n; | ||
for (let o = q - 1n; o % 2n === 0n; o /= 2n) l += 1n; | ||
let l = _0n; | ||
for (let o = q - _1n; o % _2n === _0n; o /= _2n) l += _1n; | ||
const c1 = l; // 1. c1, the largest integer such that 2^c1 divides q - 1. | ||
const c2 = (q - 1n) / 2n ** c1; // 2. c2 = (q - 1) / (2^c1) # Integer arithmetic | ||
const c3 = (c2 - 1n) / 2n; // 3. c3 = (c2 - 1) / 2 # Integer arithmetic | ||
const c4 = 2n ** c1 - 1n; // 4. c4 = 2^c1 - 1 # Integer arithmetic | ||
const c5 = 2n ** (c1 - 1n); // 5. c5 = 2^(c1 - 1) # Integer arithmetic | ||
const c2 = (q - _1n) / _2n ** c1; // 2. c2 = (q - 1) / (2^c1) # Integer arithmetic | ||
const c3 = (c2 - _1n) / _2n; // 3. c3 = (c2 - 1) / 2 # Integer arithmetic | ||
const c4 = _2n ** c1 - _1n; // 4. c4 = 2^c1 - 1 # Integer arithmetic | ||
const c5 = _2n ** (c1 - _1n); // 5. c5 = 2^(c1 - 1) # Integer arithmetic | ||
const c6 = Fp.pow(Z, c2); // 6. c6 = Z^c2 | ||
const c7 = Fp.pow(Z, (c2 + 1n) / 2n); // 7. c7 = Z^((c2 + 1) / 2) | ||
const c7 = Fp.pow(Z, (c2 + _1n) / _2n); // 7. c7 = Z^((c2 + 1) / 2) | ||
let sqrtRatio = (u: T, v: T): { isValid: boolean; value: T } => { | ||
@@ -1114,3 +1116,3 @@ let tv1 = c6; // 1. tv1 = c6 | ||
for (let i = c1; i > 1; i--) { | ||
let tv5 = 2n ** (i - 2n); // 18. tv5 = i - 2; 19. tv5 = 2^tv5 | ||
let tv5 = _2n ** (i - _2n); // 18. tv5 = i - 2; 19. tv5 = 2^tv5 | ||
let tvv5 = Fp.pow(tv4, tv5); // 20. tv5 = tv4^tv5 | ||
@@ -1126,5 +1128,5 @@ const e1 = Fp.eql(tvv5, Fp.ONE); // 21. e1 = tv5 == 1 | ||
}; | ||
if (Fp.ORDER % 4n === 3n) { | ||
if (Fp.ORDER % _4n === _3n) { | ||
// sqrt_ratio_3mod4(u, v) | ||
const c1 = (Fp.ORDER - 3n) / 4n; // 1. c1 = (q - 3) / 4 # Integer arithmetic | ||
const c1 = (Fp.ORDER - _3n) / _4n; // 1. c1 = (q - 3) / 4 # Integer arithmetic | ||
const c2 = Fp.sqrt(Fp.neg(Z)); // 2. c2 = sqrt(-Z) | ||
@@ -1145,3 +1147,3 @@ sqrtRatio = (u: T, v: T) => { | ||
// No curves uses that | ||
// if (Fp.ORDER % 8n === 5n) // sqrt_ratio_5mod8 | ||
// if (Fp.ORDER % _8n === _5n) // sqrt_ratio_5mod8 | ||
return sqrtRatio; | ||
@@ -1148,0 +1150,0 @@ } |
@@ -72,12 +72,19 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
// Be friendly to bad ECMAScript parsers by not using bigint literals | ||
// prettier-ignore | ||
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4); | ||
const _8n = BigInt(8), | ||
_16n = BigInt(16); | ||
// CURVE FIELDS | ||
// Finite field over p. | ||
const Fp = | ||
mod.Field( | ||
0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn | ||
); | ||
const Fp = mod.Field( | ||
BigInt( | ||
'0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab' | ||
) | ||
); | ||
type Fp = bigint; | ||
// Finite field over r. | ||
// This particular field is not used anywhere in bls12-381, but it is still useful. | ||
const Fr = mod.Field(0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001n); | ||
const Fr = mod.Field(BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001')); | ||
@@ -125,4 +132,5 @@ // Fp₂ over complex plane | ||
const FP2_ORDER = | ||
0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn ** | ||
2n; | ||
BigInt( | ||
'0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab' | ||
) ** _2n; | ||
@@ -180,3 +188,3 @@ const Fp2: mod.IField<Fp2> & Fp2Utils = { | ||
// Inspired by https://github.com/dalek-cryptography/curve25519-dalek/blob/17698df9d4c834204f83a3574143abacb4fc81a5/src/field.rs#L99 | ||
const candidateSqrt = Fp2.pow(num, (Fp2.ORDER + 8n) / 16n); | ||
const candidateSqrt = Fp2.pow(num, (Fp2.ORDER + _8n) / _16n); | ||
const check = Fp2.div(Fp2.sqr(candidateSqrt), num); // candidateSqrt.square().div(this); | ||
@@ -199,6 +207,6 @@ const R = FP2_ROOTS_OF_UNITY; | ||
const { re: x0, im: x1 } = Fp2.reim(x); | ||
const sign_0 = x0 % 2n; | ||
const zero_0 = x0 === 0n; | ||
const sign_1 = x1 % 2n; | ||
return BigInt(sign_0 || (zero_0 && sign_1)) == 1n; | ||
const sign_0 = x0 % _2n; | ||
const zero_0 = x0 === _0n; | ||
const sign_1 = x1 % _2n; | ||
return BigInt(sign_0 || (zero_0 && sign_1)) == _1n; | ||
}, | ||
@@ -223,4 +231,4 @@ // Bytes util | ||
multiplyByB: ({ c0, c1 }) => { | ||
let t0 = Fp.mul(c0, 4n); // 4 * c0 | ||
let t1 = Fp.mul(c1, 4n); // 4 * c1 | ||
let t0 = Fp.mul(c0, _4n); // 4 * c0 | ||
let t1 = Fp.mul(c1, _4n); // 4 * c1 | ||
// (T0-T1) + (T0+T1)*i | ||
@@ -242,17 +250,20 @@ return { c0: Fp.sub(t0, t1), c1: Fp.add(t0, t1) }; | ||
const FP2_FROBENIUS_COEFFICIENTS = [ | ||
0x1n, | ||
0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaan, | ||
BigInt('0x1'), | ||
BigInt( | ||
'0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa' | ||
), | ||
].map((item) => Fp.create(item)); | ||
// For Fp2 roots of unity. | ||
const rv1 = | ||
0x6af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09n; | ||
const rv1 = BigInt( | ||
'0x6af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09' | ||
); | ||
// const ev1 = | ||
// 0x699be3b8c6870965e5bf892ad5d2cc7b0e85a117402dfd83b7f4a947e02d978498255a2aaec0ac627b5afbdf1bf1c90n; | ||
// BigInt('0x699be3b8c6870965e5bf892ad5d2cc7b0e85a117402dfd83b7f4a947e02d978498255a2aaec0ac627b5afbdf1bf1c90'); | ||
// const ev2 = | ||
// 0x8157cd83046453f5dd0972b6e3949e4288020b5b8a9cc99ca07e27089a2ce2436d965026adad3ef7baba37f2183e9b5n; | ||
// BigInt('0x8157cd83046453f5dd0972b6e3949e4288020b5b8a9cc99ca07e27089a2ce2436d965026adad3ef7baba37f2183e9b5'); | ||
// const ev3 = | ||
// 0xab1c2ffdd6c253ca155231eb3e71ba044fd562f6f72bc5bad5ec46a0b7a3b0247cf08ce6c6317f40edbc653a72dee17n; | ||
// BigInt('0xab1c2ffdd6c253ca155231eb3e71ba044fd562f6f72bc5bad5ec46a0b7a3b0247cf08ce6c6317f40edbc653a72dee17'); | ||
// const ev4 = | ||
// 0xaa404866706722864480885d68ad0ccac1967c7544b447873cc37e0181271e006df72162a3d3e0287bf597fbf7f8fc1n; | ||
// BigInt('0xaa404866706722864480885d68ad0ccac1967c7544b447873cc37e0181271e006df72162a3d3e0287bf597fbf7f8fc1'); | ||
@@ -263,9 +274,9 @@ // Eighth roots of unity, used for computing square roots in Fp2. | ||
const FP2_ROOTS_OF_UNITY = [ | ||
[1n, 0n], | ||
[_1n, _0n], | ||
[rv1, -rv1], | ||
[0n, 1n], | ||
[_0n, _1n], | ||
[rv1, rv1], | ||
[-1n, 0n], | ||
[-_1n, _0n], | ||
[-rv1, rv1], | ||
[0n, -1n], | ||
[_0n, -_1n], | ||
[-rv1, -rv1], | ||
@@ -324,4 +335,4 @@ ].map((pair) => Fp2.fromBigTuple(pair)); | ||
let t0 = Fp2.sqr(c0); // c0² | ||
let t1 = Fp2.mul(Fp2.mul(c0, c1), 2n); // 2 * c0 * c1 | ||
let t3 = Fp2.mul(Fp2.mul(c1, c2), 2n); // 2 * c1 * c2 | ||
let t1 = Fp2.mul(Fp2.mul(c0, c1), _2n); // 2 * c0 * c1 | ||
let t3 = Fp2.mul(Fp2.mul(c1, c2), _2n); // 2 * c1 * c2 | ||
let t4 = Fp2.sqr(c2); // c2² | ||
@@ -451,42 +462,60 @@ return { | ||
const FP6_FROBENIUS_COEFFICIENTS_1 = [ | ||
[0x1n, 0x0n], | ||
[BigInt('0x1'), BigInt('0x0')], | ||
[ | ||
0x0n, | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn, | ||
BigInt('0x0'), | ||
BigInt( | ||
'0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac' | ||
), | ||
], | ||
[ | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen, | ||
0x0n, | ||
BigInt( | ||
'0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe' | ||
), | ||
BigInt('0x0'), | ||
], | ||
[0x0n, 0x1n], | ||
[BigInt('0x0'), BigInt('0x1')], | ||
[ | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn, | ||
0x0n, | ||
BigInt( | ||
'0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac' | ||
), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x0n, | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen, | ||
BigInt('0x0'), | ||
BigInt( | ||
'0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe' | ||
), | ||
], | ||
].map((pair) => Fp2.fromBigTuple(pair)); | ||
const FP6_FROBENIUS_COEFFICIENTS_2 = [ | ||
[0x1n, 0x0n], | ||
[BigInt('0x1'), BigInt('0x0')], | ||
[ | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaadn, | ||
0x0n, | ||
BigInt( | ||
'0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaad' | ||
), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn, | ||
0x0n, | ||
BigInt( | ||
'0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac' | ||
), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaan, | ||
0x0n, | ||
BigInt( | ||
'0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa' | ||
), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen, | ||
0x0n, | ||
BigInt( | ||
'0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe' | ||
), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffffn, | ||
0x0n, | ||
BigInt( | ||
'0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffff' | ||
), | ||
BigInt('0x0'), | ||
], | ||
@@ -500,3 +529,3 @@ ].map((pair) => Fp2.fromBigTuple(pair)); | ||
// The BLS parameter x for BLS12-381 | ||
const BLS_X = 0xd201000000010000n; | ||
const BLS_X = BigInt('0xd201000000010000'); | ||
const BLS_X_LEN = bitLen(BLS_X); | ||
@@ -659,10 +688,10 @@ | ||
c0: Fp6.create({ | ||
c0: Fp2.add(Fp2.mul(Fp2.sub(t3, c0c0), 2n), t3), // 2 * (T3 - c0c0) + T3 | ||
c1: Fp2.add(Fp2.mul(Fp2.sub(t5, c0c1), 2n), t5), // 2 * (T5 - c0c1) + T5 | ||
c2: Fp2.add(Fp2.mul(Fp2.sub(t7, c0c2), 2n), t7), | ||
c0: Fp2.add(Fp2.mul(Fp2.sub(t3, c0c0), _2n), t3), // 2 * (T3 - c0c0) + T3 | ||
c1: Fp2.add(Fp2.mul(Fp2.sub(t5, c0c1), _2n), t5), // 2 * (T5 - c0c1) + T5 | ||
c2: Fp2.add(Fp2.mul(Fp2.sub(t7, c0c2), _2n), t7), | ||
}), // 2 * (T7 - c0c2) + T7 | ||
c1: Fp6.create({ | ||
c0: Fp2.add(Fp2.mul(Fp2.add(t9, c1c0), 2n), t9), // 2 * (T9 + c1c0) + T9 | ||
c1: Fp2.add(Fp2.mul(Fp2.add(t4, c1c1), 2n), t4), // 2 * (T4 + c1c1) + T4 | ||
c2: Fp2.add(Fp2.mul(Fp2.add(t6, c1c2), 2n), t6), | ||
c0: Fp2.add(Fp2.mul(Fp2.add(t9, c1c0), _2n), t9), // 2 * (T9 + c1c0) + T9 | ||
c1: Fp2.add(Fp2.mul(Fp2.add(t4, c1c1), _2n), t4), // 2 * (T4 + c1c1) + T4 | ||
c2: Fp2.add(Fp2.mul(Fp2.add(t6, c1c2), _2n), t6), | ||
}), | ||
@@ -702,46 +731,80 @@ }; // 2 * (T6 + c1c2) + T6 | ||
const FP12_FROBENIUS_COEFFICIENTS = [ | ||
[0x1n, 0x0n], | ||
[BigInt('0x1'), BigInt('0x0')], | ||
[ | ||
0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8n, | ||
0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3n, | ||
BigInt( | ||
'0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8' | ||
), | ||
BigInt( | ||
'0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3' | ||
), | ||
], | ||
[ | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffffn, | ||
0x0n, | ||
BigInt( | ||
'0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffff' | ||
), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2n, | ||
0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09n, | ||
BigInt( | ||
'0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2' | ||
), | ||
BigInt( | ||
'0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09' | ||
), | ||
], | ||
[ | ||
0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen, | ||
0x0n, | ||
BigInt( | ||
'0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe' | ||
), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995n, | ||
0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116n, | ||
BigInt( | ||
'0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995' | ||
), | ||
BigInt( | ||
'0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116' | ||
), | ||
], | ||
[ | ||
0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaan, | ||
0x0n, | ||
BigInt( | ||
'0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa' | ||
), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3n, | ||
0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8n, | ||
BigInt( | ||
'0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3' | ||
), | ||
BigInt( | ||
'0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8' | ||
), | ||
], | ||
[ | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn, | ||
0x0n, | ||
BigInt( | ||
'0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac' | ||
), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09n, | ||
0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2n, | ||
BigInt( | ||
'0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09' | ||
), | ||
BigInt( | ||
'0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2' | ||
), | ||
], | ||
[ | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaadn, | ||
0x0n, | ||
BigInt( | ||
'0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaad' | ||
), | ||
BigInt('0x0'), | ||
], | ||
[ | ||
0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116n, | ||
0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995n, | ||
BigInt( | ||
'0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116' | ||
), | ||
BigInt( | ||
'0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995' | ||
), | ||
], | ||
@@ -902,3 +965,3 @@ ].map((n) => Fp2.fromBigTuple(n)); | ||
const G2_SWU = mapToCurveSimpleSWU(Fp2, { | ||
A: Fp2.create({ c0: Fp.create(0n), c1: Fp.create(240n) }), // A' = 240 * I | ||
A: Fp2.create({ c0: Fp.create(_0n), c1: Fp.create(240n) }), // A' = 240 * I | ||
B: Fp2.create({ c0: Fp.create(1012n), c1: Fp.create(1012n) }), // B' = 1012 * (1 + I) | ||
@@ -910,6 +973,10 @@ Z: Fp2.create({ c0: Fp.create(-2n), c1: Fp.create(-1n) }), // Z: -(2 + I) | ||
A: Fp.create( | ||
0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1dn | ||
BigInt( | ||
'0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1d' | ||
) | ||
), | ||
B: Fp.create( | ||
0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0n | ||
BigInt( | ||
'0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0' | ||
) | ||
), | ||
@@ -939,4 +1006,5 @@ Z: Fp.create(11n), | ||
// 1 / F2(2)^((p-1)/3) in GF(p²) | ||
const PSI2_C1 = | ||
0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn; | ||
const PSI2_C1 = BigInt( | ||
'0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac' | ||
); | ||
@@ -1018,10 +1086,14 @@ function psi2(x: Fp2, y: Fp2): [Fp2, Fp2] { | ||
// cofactor; (z - 1)²/3 | ||
h: 0x396c8c005555e1568c00aaab0000aaabn, | ||
h: BigInt('0x396c8c005555e1568c00aaab0000aaab'), | ||
// generator's coordinates | ||
// x = 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507 | ||
// y = 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569 | ||
Gx: 0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bbn, | ||
Gy: 0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1n, | ||
Gx: BigInt( | ||
'0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb' | ||
), | ||
Gy: BigInt( | ||
'0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1' | ||
), | ||
a: Fp.ZERO, | ||
b: 4n, | ||
b: _4n, | ||
htfDefaults: { ...htfDefaults, m: 1 }, | ||
@@ -1036,4 +1108,5 @@ wrapPrivateKey: true, | ||
// φ endomorphism | ||
const cubicRootOfUnityModP = | ||
0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen; | ||
const cubicRootOfUnityModP = BigInt( | ||
'0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe' | ||
); | ||
const phi = new c(Fp.mul(point.px, cubicRootOfUnityModP), point.py, point.pz); | ||
@@ -1048,3 +1121,3 @@ | ||
// (z² − 1)/3 | ||
// const c1 = 0x396c8c005555e1560000000055555555n; | ||
// const c1 = BigInt('0x396c8c005555e1560000000055555555'); | ||
// const P = this; | ||
@@ -1075,9 +1148,9 @@ // const S = P.sigma(); | ||
// Zero | ||
if (bflag === 1n) return { x: 0n, y: 0n }; | ||
if (bflag === _1n) return { x: _0n, y: _0n }; | ||
const x = Fp.create(compressedValue & Fp.MASK); | ||
const right = Fp.add(Fp.pow(x, 3n), Fp.create(bls12_381.CURVE.G1.b)); // y² = x³ + b | ||
const right = Fp.add(Fp.pow(x, _3n), Fp.create(bls12_381.CURVE.G1.b)); // y² = x³ + b | ||
let y = Fp.sqrt(right); | ||
if (!y) throw new Error('Invalid compressed G1 point'); | ||
const aflag = bitGet(compressedValue, C_BIT_POS); | ||
if ((y * 2n) / P !== aflag) y = Fp.neg(y); | ||
if ((y * _2n) / P !== aflag) y = Fp.neg(y); | ||
return { x: Fp.create(x), y: Fp.create(y) }; | ||
@@ -1101,3 +1174,3 @@ } else if (bytes.length === 96) { | ||
let num; | ||
num = bitSet(x, C_BIT_POS, Boolean((y * 2n) / P)); // set aflag | ||
num = bitSet(x, C_BIT_POS, Boolean((y * _2n) / P)); // set aflag | ||
num = bitSet(num, S_BIT_POS, true); | ||
@@ -1123,6 +1196,12 @@ return numberToBytesBE(num, Fp.BYTES); | ||
// cofactor | ||
h: 0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5n, | ||
h: BigInt( | ||
'0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5' | ||
), | ||
Gx: Fp2.fromBigTuple([ | ||
0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8n, | ||
0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7en, | ||
BigInt( | ||
'0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8' | ||
), | ||
BigInt( | ||
'0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e' | ||
), | ||
]), | ||
@@ -1133,8 +1212,14 @@ // y = | ||
Gy: Fp2.fromBigTuple([ | ||
0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801n, | ||
0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79ben, | ||
BigInt( | ||
'0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801' | ||
), | ||
BigInt( | ||
'0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be' | ||
), | ||
]), | ||
a: Fp2.ZERO, | ||
b: Fp2.fromBigTuple([4n, 4n]), | ||
hEff: 0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551n, | ||
b: Fp2.fromBigTuple([4n, _4n]), | ||
hEff: BigInt( | ||
'0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551' | ||
), | ||
htfDefaults: { ...htfDefaults }, | ||
@@ -1200,5 +1285,5 @@ wrapPrivateKey: true, | ||
const x = Fp2.create({ c0: Fp.create(x_0), c1: Fp.create(x_1) }); | ||
const right = Fp2.add(Fp2.pow(x, 3n), b); // y² = x³ + 4 * (u+1) = x³ + b | ||
const right = Fp2.add(Fp2.pow(x, _3n), b); // y² = x³ + 4 * (u+1) = x³ + b | ||
let y = Fp2.sqrt(right); | ||
const Y_bit = y.c1 === 0n ? (y.c0 * 2n) / P : (y.c1 * 2n) / P ? 1n : 0n; | ||
const Y_bit = y.c1 === _0n ? (y.c0 * _2n) / P : (y.c1 * _2n) / P ? _1n : _0n; | ||
y = bitS > 0 && Y_bit > 0 ? y : Fp2.neg(y); | ||
@@ -1226,3 +1311,3 @@ return { x, y }; | ||
if (isZero) return concatB(COMPRESSED_ZERO, numberToBytesBE(0n, Fp.BYTES)); | ||
const flag = Boolean(y.c1 === 0n ? (y.c0 * 2n) / P : (y.c1 * 2n) / P); | ||
const flag = Boolean(y.c1 === _0n ? (y.c0 * _2n) / P : (y.c1 * _2n) / P); | ||
// set compressed & sign bits (looks like different offsets than for G1/Fp?) | ||
@@ -1256,3 +1341,3 @@ let x_1 = bitSet(x.c1, C_BIT_POS, flag); | ||
const bflag1 = bitGet(z1, I_BIT_POS); | ||
if (bflag1 === 1n) return bls12_381.G2.ProjectivePoint.ZERO; | ||
if (bflag1 === _1n) return bls12_381.G2.ProjectivePoint.ZERO; | ||
@@ -1262,3 +1347,3 @@ const x1 = Fp.create(z1 & Fp.MASK); | ||
const x = Fp2.create({ c0: x2, c1: x1 }); | ||
const y2 = Fp2.add(Fp2.pow(x, 3n), bls12_381.CURVE.G2.b); // y² = x³ + 4 | ||
const y2 = Fp2.add(Fp2.pow(x, _3n), bls12_381.CURVE.G2.b); // y² = x³ + 4 | ||
// The slow part | ||
@@ -1272,4 +1357,4 @@ let y = Fp2.sqrt(y2); | ||
const aflag1 = bitGet(z1, 381); | ||
const isGreater = y1 > 0n && (y1 * 2n) / P !== aflag1; | ||
const isZero = y1 === 0n && (y0 * 2n) / P !== aflag1; | ||
const isGreater = y1 > _0n && (y1 * _2n) / P !== aflag1; | ||
const isZero = y1 === _0n && (y0 * _2n) / P !== aflag1; | ||
if (isGreater || isZero) y = Fp2.neg(y); | ||
@@ -1288,4 +1373,4 @@ const point = bls12_381.G2.ProjectivePoint.fromAffine({ x, y }); | ||
const { re: y0, im: y1 } = Fp2.reim(a.y); | ||
const tmp = y1 > 0n ? y1 * 2n : y0 * 2n; | ||
const aflag1 = Boolean((tmp / Fp.ORDER) & 1n); | ||
const tmp = y1 > _0n ? y1 * _2n : y0 * _2n; | ||
const aflag1 = Boolean((tmp / Fp.ORDER) & _1n); | ||
const z1 = bitSet(bitSet(x1, 381, aflag1), S_BIT_POS, true); | ||
@@ -1292,0 +1377,0 @@ const z2 = x0; |
@@ -207,3 +207,3 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
y = Fp.cmov(y, Fp.neg(y), e3 !== e4); // 38. y = CMOV(y, -y, e3 XOR e4) | ||
return { xMn: xn, xMd: xd, yMn: y, yMd: 1n }; // 39. return (xn, xd, y, 1) | ||
return { xMn: xn, xMd: xd, yMn: y, yMd: _1n }; // 39. return (xn, xd, y, 1) | ||
} | ||
@@ -210,0 +210,0 @@ |
@@ -57,2 +57,3 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
const Fp = Field(ed448P, 456, true); | ||
const _4n = BigInt(4); | ||
@@ -199,6 +200,6 @@ const ED448_DEF = { | ||
xEn = Fp.mul(xEn, yn); // 11. xEn = xEn * yn | ||
xEn = Fp.mul(xEn, 4n); // 12. xEn = xEn * 4 | ||
xEn = Fp.mul(xEn, _4n); // 12. xEn = xEn * 4 | ||
tv2 = Fp.mul(tv2, xn2); // 13. tv2 = tv2 * xn2 | ||
tv2 = Fp.mul(tv2, yd2); // 14. tv2 = tv2 * yd2 | ||
let tv3 = Fp.mul(yn2, 4n); // 15. tv3 = 4 * yn2 | ||
let tv3 = Fp.mul(yn2, _4n); // 15. tv3 = 4 * yn2 | ||
let tv1 = Fp.add(tv3, yd2); // 16. tv1 = tv3 + yd2 | ||
@@ -205,0 +206,0 @@ tv1 = Fp.mul(tv1, xd4); // 17. tv1 = tv1 * xd4 |
@@ -134,3 +134,3 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
let y = sqrtMod(c); // Let y = c^(p+1)/4 mod p. | ||
if (y % 2n !== 0n) y = modP(-y); // Return the unique point P such that x(P) = x and | ||
if (y % _2n !== _0n) y = modP(-y); // Return the unique point P such that x(P) = x and | ||
const p = new Point(x, y, _1n); // y(P) = y if y mod 2 = 0 or y(P) = p-y otherwise. | ||
@@ -137,0 +137,0 @@ p.assertValidity(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1262509
17368