@noble/curves 0.5.2 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +115 -41
- package/lib/_shortw_utils.d.ts +13 -24
- package/lib/abstract/bls.d.ts +39 -32
- package/lib/abstract/bls.js +74 -73
- package/lib/abstract/{group.d.ts → curve.d.ts} +30 -1
- package/lib/abstract/{group.js → curve.js} +33 -2
- package/lib/abstract/edwards.d.ts +30 -72
- package/lib/abstract/edwards.js +206 -389
- package/lib/abstract/hash-to-curve.d.ts +25 -6
- package/lib/abstract/hash-to-curve.js +40 -12
- package/lib/abstract/modular.d.ts +21 -8
- package/lib/abstract/modular.js +72 -48
- package/lib/abstract/montgomery.js +23 -68
- package/lib/abstract/poseidon.d.ts +29 -0
- package/lib/abstract/poseidon.js +115 -0
- package/lib/abstract/utils.d.ts +9 -37
- package/lib/abstract/utils.js +61 -87
- package/lib/abstract/weierstrass.d.ts +58 -81
- package/lib/abstract/weierstrass.js +485 -679
- package/lib/bls12-381.js +63 -58
- package/lib/bn.js +1 -1
- package/lib/ed25519.d.ts +7 -5
- package/lib/ed25519.js +82 -79
- package/lib/ed448.d.ts +3 -0
- package/lib/ed448.js +86 -83
- package/lib/esm/abstract/bls.js +75 -74
- package/lib/esm/abstract/{group.js → curve.js} +31 -1
- package/lib/esm/abstract/edwards.js +204 -387
- package/lib/esm/abstract/hash-to-curve.js +38 -11
- package/lib/esm/abstract/modular.js +69 -47
- package/lib/esm/abstract/montgomery.js +24 -69
- package/lib/esm/abstract/poseidon.js +109 -0
- package/lib/esm/abstract/utils.js +58 -82
- package/lib/esm/abstract/weierstrass.js +484 -678
- package/lib/esm/bls12-381.js +75 -70
- package/lib/esm/bn.js +1 -1
- package/lib/esm/ed25519.js +80 -78
- package/lib/esm/ed448.js +84 -82
- package/lib/esm/jubjub.js +1 -1
- package/lib/esm/p224.js +1 -1
- package/lib/esm/p256.js +11 -9
- package/lib/esm/p384.js +11 -9
- package/lib/esm/p521.js +12 -23
- package/lib/esm/secp256k1.js +124 -162
- package/lib/esm/stark.js +105 -41
- package/lib/jubjub.d.ts +2 -2
- package/lib/jubjub.js +1 -1
- package/lib/p192.d.ts +26 -48
- package/lib/p224.d.ts +26 -48
- package/lib/p224.js +1 -1
- package/lib/p256.d.ts +29 -48
- package/lib/p256.js +13 -10
- package/lib/p384.d.ts +29 -48
- package/lib/p384.js +13 -10
- package/lib/p521.d.ts +37 -57
- package/lib/p521.js +14 -24
- package/lib/secp256k1.d.ts +37 -46
- package/lib/secp256k1.js +124 -162
- package/lib/stark.d.ts +39 -22
- package/lib/stark.js +108 -41
- package/package.json +15 -10
package/lib/bls12-381.js
CHANGED
|
@@ -68,24 +68,24 @@ const Fp2 = {
|
|
|
68
68
|
ONE: { c0: Fp.ONE, c1: Fp.ZERO },
|
|
69
69
|
create: (num) => num,
|
|
70
70
|
isValid: ({ c0, c1 }) => typeof c0 === 'bigint' && typeof c1 === 'bigint',
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
is0: ({ c0, c1 }) => Fp.is0(c0) && Fp.is0(c1),
|
|
72
|
+
eql: ({ c0, c1 }, { c0: r0, c1: r1 }) => Fp.eql(c0, r0) && Fp.eql(c1, r1),
|
|
73
|
+
neg: ({ c0, c1 }) => ({ c0: Fp.neg(c0), c1: Fp.neg(c1) }),
|
|
74
74
|
pow: (num, power) => mod.FpPow(Fp2, num, power),
|
|
75
75
|
invertBatch: (nums) => mod.FpInvertBatch(Fp2, nums),
|
|
76
76
|
// Normalized
|
|
77
77
|
add: Fp2Add,
|
|
78
78
|
sub: Fp2Subtract,
|
|
79
79
|
mul: Fp2Multiply,
|
|
80
|
-
|
|
80
|
+
sqr: Fp2Square,
|
|
81
81
|
// NonNormalized stuff
|
|
82
82
|
addN: Fp2Add,
|
|
83
83
|
subN: Fp2Subtract,
|
|
84
84
|
mulN: Fp2Multiply,
|
|
85
|
-
|
|
85
|
+
sqrN: Fp2Square,
|
|
86
86
|
// Why inversion for bigint inside Fp instead of Fp2? it is even used in that context?
|
|
87
|
-
div: (lhs, rhs) => Fp2.mul(lhs, typeof rhs === 'bigint' ? Fp.
|
|
88
|
-
|
|
87
|
+
div: (lhs, rhs) => Fp2.mul(lhs, typeof rhs === 'bigint' ? Fp.inv(Fp.create(rhs)) : Fp2.inv(rhs)),
|
|
88
|
+
inv: ({ c0: a, c1: b }) => {
|
|
89
89
|
// We wish to find the multiplicative inverse of a nonzero
|
|
90
90
|
// element a + bu in Fp2. We leverage an identity
|
|
91
91
|
//
|
|
@@ -99,11 +99,11 @@ const Fp2 = {
|
|
|
99
99
|
// This gives that (a - bu)/(a² + b²) is the inverse
|
|
100
100
|
// of (a + bu). Importantly, this can be computing using
|
|
101
101
|
// only a single inversion in Fp.
|
|
102
|
-
const factor = Fp.
|
|
102
|
+
const factor = Fp.inv(Fp.create(a * a + b * b));
|
|
103
103
|
return { c0: Fp.mul(factor, Fp.create(a)), c1: Fp.mul(factor, Fp.create(-b)) };
|
|
104
104
|
},
|
|
105
105
|
sqrt: (num) => {
|
|
106
|
-
if (Fp2.
|
|
106
|
+
if (Fp2.eql(num, Fp2.ZERO))
|
|
107
107
|
return Fp2.ZERO; // Algo doesn't handles this case
|
|
108
108
|
// TODO: Optimize this line. It's extremely slow.
|
|
109
109
|
// Speeding this up would boost aggregateSignatures.
|
|
@@ -112,9 +112,9 @@ const Fp2 = {
|
|
|
112
112
|
// https://github.com/supranational/blst/blob/aae0c7d70b799ac269ff5edf29d8191dbd357876/src/exp2.c#L1
|
|
113
113
|
// Inspired by https://github.com/dalek-cryptography/curve25519-dalek/blob/17698df9d4c834204f83a3574143abacb4fc81a5/src/field.rs#L99
|
|
114
114
|
const candidateSqrt = Fp2.pow(num, (Fp2.ORDER + 8n) / 16n);
|
|
115
|
-
const check = Fp2.div(Fp2.
|
|
115
|
+
const check = Fp2.div(Fp2.sqr(candidateSqrt), num); // candidateSqrt.square().div(this);
|
|
116
116
|
const R = FP2_ROOTS_OF_UNITY;
|
|
117
|
-
const divisor = [R[0], R[2], R[4], R[6]].find((r) => Fp2.
|
|
117
|
+
const divisor = [R[0], R[2], R[4], R[6]].find((r) => Fp2.eql(r, check));
|
|
118
118
|
if (!divisor)
|
|
119
119
|
throw new Error('No root');
|
|
120
120
|
const index = R.indexOf(divisor);
|
|
@@ -122,7 +122,7 @@ const Fp2 = {
|
|
|
122
122
|
if (!root)
|
|
123
123
|
throw new Error('Invalid root');
|
|
124
124
|
const x1 = Fp2.div(candidateSqrt, root);
|
|
125
|
-
const x2 = Fp2.
|
|
125
|
+
const x2 = Fp2.neg(x1);
|
|
126
126
|
const { re: re1, im: im1 } = Fp2.reim(x1);
|
|
127
127
|
const { re: re2, im: im2 } = Fp2.reim(x2);
|
|
128
128
|
if (im1 > im2 || (im1 === im2 && re1 > re2))
|
|
@@ -233,15 +233,15 @@ const Fp6Multiply = ({ c0, c1, c2 }, rhs) => {
|
|
|
233
233
|
};
|
|
234
234
|
};
|
|
235
235
|
const Fp6Square = ({ c0, c1, c2 }) => {
|
|
236
|
-
let t0 = Fp2.
|
|
236
|
+
let t0 = Fp2.sqr(c0); // c0²
|
|
237
237
|
let t1 = Fp2.mul(Fp2.mul(c0, c1), 2n); // 2 * c0 * c1
|
|
238
238
|
let t3 = Fp2.mul(Fp2.mul(c1, c2), 2n); // 2 * c1 * c2
|
|
239
|
-
let t4 = Fp2.
|
|
239
|
+
let t4 = Fp2.sqr(c2); // c2²
|
|
240
240
|
return {
|
|
241
241
|
c0: Fp2.add(Fp2.mulByNonresidue(t3), t0),
|
|
242
242
|
c1: Fp2.add(Fp2.mulByNonresidue(t4), t1),
|
|
243
243
|
// T1 + (c0 - c1 + c2)² + T3 - T0 - T4
|
|
244
|
-
c2: Fp2.sub(Fp2.sub(Fp2.add(Fp2.add(t1, Fp2.
|
|
244
|
+
c2: Fp2.sub(Fp2.sub(Fp2.add(Fp2.add(t1, Fp2.sqr(Fp2.add(Fp2.sub(c0, c1), c2))), t3), t0), t4),
|
|
245
245
|
};
|
|
246
246
|
};
|
|
247
247
|
const Fp6 = {
|
|
@@ -253,32 +253,32 @@ const Fp6 = {
|
|
|
253
253
|
ONE: { c0: Fp2.ONE, c1: Fp2.ZERO, c2: Fp2.ZERO },
|
|
254
254
|
create: (num) => num,
|
|
255
255
|
isValid: ({ c0, c1, c2 }) => Fp2.isValid(c0) && Fp2.isValid(c1) && Fp2.isValid(c2),
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
256
|
+
is0: ({ c0, c1, c2 }) => Fp2.is0(c0) && Fp2.is0(c1) && Fp2.is0(c2),
|
|
257
|
+
neg: ({ c0, c1, c2 }) => ({ c0: Fp2.neg(c0), c1: Fp2.neg(c1), c2: Fp2.neg(c2) }),
|
|
258
|
+
eql: ({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }) => Fp2.eql(c0, r0) && Fp2.eql(c1, r1) && Fp2.eql(c2, r2),
|
|
259
259
|
sqrt: () => {
|
|
260
260
|
throw new Error('Not implemented');
|
|
261
261
|
},
|
|
262
262
|
// Do we need division by bigint at all? Should be done via order:
|
|
263
|
-
div: (lhs, rhs) => Fp6.mul(lhs, typeof rhs === 'bigint' ? Fp.
|
|
263
|
+
div: (lhs, rhs) => Fp6.mul(lhs, typeof rhs === 'bigint' ? Fp.inv(Fp.create(rhs)) : Fp6.inv(rhs)),
|
|
264
264
|
pow: (num, power) => mod.FpPow(Fp6, num, power),
|
|
265
265
|
invertBatch: (nums) => mod.FpInvertBatch(Fp6, nums),
|
|
266
266
|
// Normalized
|
|
267
267
|
add: Fp6Add,
|
|
268
268
|
sub: Fp6Subtract,
|
|
269
269
|
mul: Fp6Multiply,
|
|
270
|
-
|
|
270
|
+
sqr: Fp6Square,
|
|
271
271
|
// NonNormalized stuff
|
|
272
272
|
addN: Fp6Add,
|
|
273
273
|
subN: Fp6Subtract,
|
|
274
274
|
mulN: Fp6Multiply,
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
let t0 = Fp2.sub(Fp2.
|
|
278
|
-
let t1 = Fp2.sub(Fp2.mulByNonresidue(Fp2.
|
|
279
|
-
let t2 = Fp2.sub(Fp2.
|
|
275
|
+
sqrN: Fp6Square,
|
|
276
|
+
inv: ({ c0, c1, c2 }) => {
|
|
277
|
+
let t0 = Fp2.sub(Fp2.sqr(c0), Fp2.mulByNonresidue(Fp2.mul(c2, c1))); // c0² - c2 * c1 * (u + 1)
|
|
278
|
+
let t1 = Fp2.sub(Fp2.mulByNonresidue(Fp2.sqr(c2)), Fp2.mul(c0, c1)); // c2² * (u + 1) - c0 * c1
|
|
279
|
+
let t2 = Fp2.sub(Fp2.sqr(c1), Fp2.mul(c0, c2)); // c1² - c0 * c2
|
|
280
280
|
// 1/(((c2 * T1 + c1 * T2) * v) + c0 * T0)
|
|
281
|
-
let t4 = Fp2.
|
|
281
|
+
let t4 = Fp2.inv(Fp2.add(Fp2.mulByNonresidue(Fp2.add(Fp2.mul(c2, t1), Fp2.mul(c1, t2))), Fp2.mul(c0, t0)));
|
|
282
282
|
return { c0: Fp2.mul(t4, t0), c1: Fp2.mul(t4, t1), c2: Fp2.mul(t4, t2) };
|
|
283
283
|
},
|
|
284
284
|
// Bytes utils
|
|
@@ -419,11 +419,11 @@ const Fp12Square = ({ c0, c1 }) => {
|
|
|
419
419
|
}; // AB + AB
|
|
420
420
|
};
|
|
421
421
|
function Fp4Square(a, b) {
|
|
422
|
-
const a2 = Fp2.
|
|
423
|
-
const b2 = Fp2.
|
|
422
|
+
const a2 = Fp2.sqr(a);
|
|
423
|
+
const b2 = Fp2.sqr(b);
|
|
424
424
|
return {
|
|
425
425
|
first: Fp2.add(Fp2.mulByNonresidue(b2), a2),
|
|
426
|
-
second: Fp2.sub(Fp2.sub(Fp2.
|
|
426
|
+
second: Fp2.sub(Fp2.sub(Fp2.sqr(Fp2.add(a, b)), a2), b2), // (a + b)² - a² - b²
|
|
427
427
|
};
|
|
428
428
|
}
|
|
429
429
|
const Fp12 = {
|
|
@@ -435,29 +435,29 @@ const Fp12 = {
|
|
|
435
435
|
ONE: { c0: Fp6.ONE, c1: Fp6.ZERO },
|
|
436
436
|
create: (num) => num,
|
|
437
437
|
isValid: ({ c0, c1 }) => Fp6.isValid(c0) && Fp6.isValid(c1),
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
438
|
+
is0: ({ c0, c1 }) => Fp6.is0(c0) && Fp6.is0(c1),
|
|
439
|
+
neg: ({ c0, c1 }) => ({ c0: Fp6.neg(c0), c1: Fp6.neg(c1) }),
|
|
440
|
+
eql: ({ c0, c1 }, { c0: r0, c1: r1 }) => Fp6.eql(c0, r0) && Fp6.eql(c1, r1),
|
|
441
441
|
sqrt: () => {
|
|
442
442
|
throw new Error('Not implemented');
|
|
443
443
|
},
|
|
444
|
-
|
|
445
|
-
let t = Fp6.
|
|
446
|
-
return { c0: Fp6.mul(c0, t), c1: Fp6.
|
|
444
|
+
inv: ({ c0, c1 }) => {
|
|
445
|
+
let t = Fp6.inv(Fp6.sub(Fp6.sqr(c0), Fp6.mulByNonresidue(Fp6.sqr(c1)))); // 1 / (c0² - c1² * v)
|
|
446
|
+
return { c0: Fp6.mul(c0, t), c1: Fp6.neg(Fp6.mul(c1, t)) }; // ((C0 * T) * T) + (-C1 * T) * w
|
|
447
447
|
},
|
|
448
|
-
div: (lhs, rhs) => Fp12.mul(lhs, typeof rhs === 'bigint' ? Fp.
|
|
448
|
+
div: (lhs, rhs) => Fp12.mul(lhs, typeof rhs === 'bigint' ? Fp.inv(Fp.create(rhs)) : Fp12.inv(rhs)),
|
|
449
449
|
pow: (num, power) => mod.FpPow(Fp12, num, power),
|
|
450
450
|
invertBatch: (nums) => mod.FpInvertBatch(Fp12, nums),
|
|
451
451
|
// Normalized
|
|
452
452
|
add: Fp12Add,
|
|
453
453
|
sub: Fp12Subtract,
|
|
454
454
|
mul: Fp12Multiply,
|
|
455
|
-
|
|
455
|
+
sqr: Fp12Square,
|
|
456
456
|
// NonNormalized stuff
|
|
457
457
|
addN: Fp12Add,
|
|
458
458
|
subN: Fp12Subtract,
|
|
459
459
|
mulN: Fp12Multiply,
|
|
460
|
-
|
|
460
|
+
sqrN: Fp12Square,
|
|
461
461
|
// Bytes utils
|
|
462
462
|
fromBytes: (b) => {
|
|
463
463
|
if (b.length !== Fp12.BYTES)
|
|
@@ -511,7 +511,7 @@ const Fp12 = {
|
|
|
511
511
|
c0: Fp6.multiplyByFp2(c0, rhs),
|
|
512
512
|
c1: Fp6.multiplyByFp2(c1, rhs),
|
|
513
513
|
}),
|
|
514
|
-
conjugate: ({ c0, c1 }) => ({ c0, c1: Fp6.
|
|
514
|
+
conjugate: ({ c0, c1 }) => ({ c0, c1: Fp6.neg(c1) }),
|
|
515
515
|
// A cyclotomic group is a subgroup of Fp^n defined by
|
|
516
516
|
// GΦₙ(p) = {α ∈ Fpⁿ : α^Φₙ(p) = 1}
|
|
517
517
|
// The result of any pairing is in a cyclotomic subgroup
|
|
@@ -790,7 +790,7 @@ function G2psi(c, P) {
|
|
|
790
790
|
// 1 / F2(2)^((p-1)/3) in GF(p²)
|
|
791
791
|
const PSI2_C1 = 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn;
|
|
792
792
|
function psi2(x, y) {
|
|
793
|
-
return [Fp2.mul(x, PSI2_C1), Fp2.
|
|
793
|
+
return [Fp2.mul(x, PSI2_C1), Fp2.neg(y)];
|
|
794
794
|
}
|
|
795
795
|
function G2psi2(c, P) {
|
|
796
796
|
const affine = P.toAffine();
|
|
@@ -812,6 +812,7 @@ const htfDefaults = {
|
|
|
812
812
|
// defined in section 2.2.5
|
|
813
813
|
// Use utils.getDSTLabel(), utils.setDSTLabel(value)
|
|
814
814
|
DST: 'BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_',
|
|
815
|
+
encodeDST: 'BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_',
|
|
815
816
|
// p: the characteristic of F
|
|
816
817
|
// where F is a finite field of characteristic p and order q = p^m
|
|
817
818
|
p: Fp.ORDER,
|
|
@@ -878,7 +879,7 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
878
879
|
isTorsionFree: (c, point) => {
|
|
879
880
|
// φ endomorphism
|
|
880
881
|
const cubicRootOfUnityModP = 0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen;
|
|
881
|
-
const phi = new c(Fp.mul(point.
|
|
882
|
+
const phi = new c(Fp.mul(point.px, cubicRootOfUnityModP), point.py, point.pz);
|
|
882
883
|
// todo: unroll
|
|
883
884
|
const xP = point.multiplyUnsafe(exports.bls12_381.CURVE.x).negate(); // [x]P
|
|
884
885
|
const u2P = xP.multiplyUnsafe(exports.bls12_381.CURVE.x); // [u2]P
|
|
@@ -920,13 +921,13 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
920
921
|
throw new Error('Invalid compressed G1 point');
|
|
921
922
|
const aflag = (0, utils_js_1.bitGet)(compressedValue, C_BIT_POS);
|
|
922
923
|
if ((y * 2n) / P !== aflag)
|
|
923
|
-
y = Fp.
|
|
924
|
+
y = Fp.neg(y);
|
|
924
925
|
return { x: Fp.create(x), y: Fp.create(y) };
|
|
925
926
|
}
|
|
926
927
|
else if (bytes.length === 96) {
|
|
927
928
|
// Check if the infinity flag is set
|
|
928
929
|
if ((bytes[0] & (1 << 6)) !== 0)
|
|
929
|
-
return exports.bls12_381.G1.
|
|
930
|
+
return exports.bls12_381.G1.ProjectivePoint.ZERO.toAffine();
|
|
930
931
|
const x = (0, utils_js_1.bytesToNumberBE)(bytes.slice(0, Fp.BYTES));
|
|
931
932
|
const y = (0, utils_js_1.bytesToNumberBE)(bytes.slice(Fp.BYTES));
|
|
932
933
|
return { x: Fp.create(x), y: Fp.create(y) };
|
|
@@ -937,7 +938,7 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
937
938
|
},
|
|
938
939
|
toBytes: (c, point, isCompressed) => {
|
|
939
940
|
const isZero = point.equals(c.ZERO);
|
|
940
|
-
const { x, y } = point;
|
|
941
|
+
const { x, y } = point.toAffine();
|
|
941
942
|
if (isCompressed) {
|
|
942
943
|
if (isZero)
|
|
943
944
|
return COMPRESSED_ZERO.slice();
|
|
@@ -1024,6 +1025,8 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
1024
1025
|
const bitC = m_byte & 0x80; // compression bit
|
|
1025
1026
|
const bitI = m_byte & 0x40; // point at infinity bit
|
|
1026
1027
|
const bitS = m_byte & 0x20; // sign bit
|
|
1028
|
+
const L = Fp.BYTES;
|
|
1029
|
+
const slc = (b, from, to) => (0, utils_js_1.bytesToNumberBE)(b.slice(from, to));
|
|
1027
1030
|
if (bytes.length === 96 && bitC) {
|
|
1028
1031
|
const { b } = exports.bls12_381.CURVE.G2;
|
|
1029
1032
|
const P = Fp.ORDER;
|
|
@@ -1035,13 +1038,13 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
1035
1038
|
}
|
|
1036
1039
|
return { x: Fp2.ZERO, y: Fp2.ZERO };
|
|
1037
1040
|
}
|
|
1038
|
-
const x_1 = (
|
|
1039
|
-
const x_0 = (
|
|
1041
|
+
const x_1 = slc(bytes, 0, L);
|
|
1042
|
+
const x_0 = slc(bytes, L, 2 * L);
|
|
1040
1043
|
const x = Fp2.create({ c0: Fp.create(x_0), c1: Fp.create(x_1) });
|
|
1041
1044
|
const right = Fp2.add(Fp2.pow(x, 3n), b); // y² = x³ + 4 * (u+1) = x³ + b
|
|
1042
1045
|
let y = Fp2.sqrt(right);
|
|
1043
1046
|
const Y_bit = y.c1 === 0n ? (y.c0 * 2n) / P : (y.c1 * 2n) / P ? 1n : 0n;
|
|
1044
|
-
y = bitS > 0 && Y_bit > 0 ? y : Fp2.
|
|
1047
|
+
y = bitS > 0 && Y_bit > 0 ? y : Fp2.neg(y);
|
|
1045
1048
|
return { x, y };
|
|
1046
1049
|
}
|
|
1047
1050
|
else if (bytes.length === 192 && !bitC) {
|
|
@@ -1049,10 +1052,10 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
1049
1052
|
if ((bytes[0] & (1 << 6)) !== 0) {
|
|
1050
1053
|
return { x: Fp2.ZERO, y: Fp2.ZERO };
|
|
1051
1054
|
}
|
|
1052
|
-
const x1 = (
|
|
1053
|
-
const x0 = (
|
|
1054
|
-
const y1 = (
|
|
1055
|
-
const y0 = (
|
|
1055
|
+
const x1 = slc(bytes, 0, L);
|
|
1056
|
+
const x0 = slc(bytes, L, 2 * L);
|
|
1057
|
+
const y1 = slc(bytes, 2 * L, 3 * L);
|
|
1058
|
+
const y0 = slc(bytes, 3 * L, 4 * L);
|
|
1056
1059
|
return { x: Fp2.fromBigTuple([x0, x1]), y: Fp2.fromBigTuple([y0, y1]) };
|
|
1057
1060
|
}
|
|
1058
1061
|
else {
|
|
@@ -1061,7 +1064,7 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
1061
1064
|
},
|
|
1062
1065
|
toBytes: (c, point, isCompressed) => {
|
|
1063
1066
|
const isZero = point.equals(c.ZERO);
|
|
1064
|
-
const { x, y } = point;
|
|
1067
|
+
const { x, y } = point.toAffine();
|
|
1065
1068
|
if (isCompressed) {
|
|
1066
1069
|
const P = Fp.ORDER;
|
|
1067
1070
|
if (isZero)
|
|
@@ -1093,7 +1096,7 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
1093
1096
|
// Indicates the infinity point
|
|
1094
1097
|
const bflag1 = (0, utils_js_1.bitGet)(z1, I_BIT_POS);
|
|
1095
1098
|
if (bflag1 === 1n)
|
|
1096
|
-
return exports.bls12_381.G2.
|
|
1099
|
+
return exports.bls12_381.G2.ProjectivePoint.ZERO;
|
|
1097
1100
|
const x1 = Fp.create(z1 & Fp.MASK);
|
|
1098
1101
|
const x2 = Fp.create(z2);
|
|
1099
1102
|
const x = Fp2.create({ c0: x2, c1: x1 });
|
|
@@ -1109,18 +1112,20 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
1109
1112
|
const isGreater = y1 > 0n && (y1 * 2n) / P !== aflag1;
|
|
1110
1113
|
const isZero = y1 === 0n && (y0 * 2n) / P !== aflag1;
|
|
1111
1114
|
if (isGreater || isZero)
|
|
1112
|
-
y = Fp2.
|
|
1113
|
-
const point =
|
|
1115
|
+
y = Fp2.neg(y);
|
|
1116
|
+
const point = exports.bls12_381.G2.ProjectivePoint.fromAffine({ x, y });
|
|
1117
|
+
// console.log('Signature.decode', point);
|
|
1114
1118
|
point.assertValidity();
|
|
1115
1119
|
return point;
|
|
1116
1120
|
},
|
|
1117
1121
|
encode(point) {
|
|
1118
1122
|
// NOTE: by some reasons it was missed in bls12-381, looks like bug
|
|
1119
1123
|
point.assertValidity();
|
|
1120
|
-
if (point.equals(exports.bls12_381.G2.
|
|
1124
|
+
if (point.equals(exports.bls12_381.G2.ProjectivePoint.ZERO))
|
|
1121
1125
|
return (0, utils_js_1.concatBytes)(COMPRESSED_ZERO, (0, utils_js_1.numberToBytesBE)(0n, Fp.BYTES));
|
|
1122
|
-
const
|
|
1123
|
-
const { re:
|
|
1126
|
+
const a = point.toAffine();
|
|
1127
|
+
const { re: x0, im: x1 } = Fp2.reim(a.x);
|
|
1128
|
+
const { re: y0, im: y1 } = Fp2.reim(a.y);
|
|
1124
1129
|
const tmp = y1 > 0n ? y1 * 2n : y0 * 2n;
|
|
1125
1130
|
const aflag1 = Boolean((tmp / Fp.ORDER) & 1n);
|
|
1126
1131
|
const z1 = (0, utils_js_1.bitSet)((0, utils_js_1.bitSet)(x1, 381, aflag1), S_BIT_POS, true);
|
package/lib/bn.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.bn254 = void 0;
|
|
4
4
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
5
|
-
const weierstrass_js_1 = require("./abstract/weierstrass.js");
|
|
6
5
|
const sha256_1 = require("@noble/hashes/sha256");
|
|
6
|
+
const weierstrass_js_1 = require("./abstract/weierstrass.js");
|
|
7
7
|
const _shortw_utils_js_1 = require("./_shortw_utils.js");
|
|
8
8
|
const modular_js_1 = require("./abstract/modular.js");
|
|
9
9
|
/**
|
package/lib/ed25519.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ExtPointType } from './abstract/edwards.js';
|
|
2
2
|
import { Hex } from './abstract/utils.js';
|
|
3
|
+
import * as htf from './abstract/hash-to-curve.js';
|
|
3
4
|
export declare const ED25519_TORSION_SUBGROUP: string[];
|
|
4
5
|
export declare const ed25519: import("./abstract/edwards.js").CurveFn;
|
|
5
6
|
export declare const ed25519ctx: import("./abstract/edwards.js").CurveFn;
|
|
6
7
|
export declare const ed25519ph: import("./abstract/edwards.js").CurveFn;
|
|
7
8
|
export declare const x25519: import("./abstract/montgomery.js").CurveFn;
|
|
8
|
-
declare
|
|
9
|
+
declare const hashToCurve: (msg: Hex, options?: htf.htfBasicOpts | undefined) => htf.H2CPoint<bigint>, encodeToCurve: (msg: Hex, options?: htf.htfBasicOpts | undefined) => htf.H2CPoint<bigint>;
|
|
10
|
+
export { hashToCurve, encodeToCurve };
|
|
11
|
+
declare type ExtendedPoint = ExtPointType;
|
|
9
12
|
/**
|
|
10
13
|
* Each ed25519/ExtendedPoint has 8 different equivalent points. This can be
|
|
11
14
|
* a source of bugs for protocols like ring signatures. Ristretto was created to solve this.
|
|
@@ -42,7 +45,6 @@ export declare class RistrettoPoint {
|
|
|
42
45
|
equals(other: RistrettoPoint): boolean;
|
|
43
46
|
add(other: RistrettoPoint): RistrettoPoint;
|
|
44
47
|
subtract(other: RistrettoPoint): RistrettoPoint;
|
|
45
|
-
multiply(scalar:
|
|
46
|
-
multiplyUnsafe(scalar:
|
|
48
|
+
multiply(scalar: bigint): RistrettoPoint;
|
|
49
|
+
multiplyUnsafe(scalar: bigint): RistrettoPoint;
|
|
47
50
|
}
|
|
48
|
-
export {};
|
package/lib/ed25519.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RistrettoPoint = exports.x25519 = exports.ed25519ph = exports.ed25519ctx = exports.ed25519 = exports.ED25519_TORSION_SUBGROUP = void 0;
|
|
3
|
+
exports.RistrettoPoint = exports.encodeToCurve = exports.hashToCurve = exports.x25519 = exports.ed25519ph = exports.ed25519ctx = exports.ed25519 = exports.ED25519_TORSION_SUBGROUP = void 0;
|
|
4
4
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
5
5
|
const sha512_1 = require("@noble/hashes/sha512");
|
|
6
6
|
const utils_1 = require("@noble/hashes/utils");
|
|
@@ -8,6 +8,7 @@ const edwards_js_1 = require("./abstract/edwards.js");
|
|
|
8
8
|
const montgomery_js_1 = require("./abstract/montgomery.js");
|
|
9
9
|
const modular_js_1 = require("./abstract/modular.js");
|
|
10
10
|
const utils_js_1 = require("./abstract/utils.js");
|
|
11
|
+
const htf = require("./abstract/hash-to-curve.js");
|
|
11
12
|
/**
|
|
12
13
|
* ed25519 Twisted Edwards curve with following addons:
|
|
13
14
|
* - X25519 ECDH
|
|
@@ -82,57 +83,107 @@ exports.ED25519_TORSION_SUBGROUP = [
|
|
|
82
83
|
'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa',
|
|
83
84
|
];
|
|
84
85
|
const Fp = (0, modular_js_1.Fp)(ED25519_P, undefined, true);
|
|
86
|
+
const ED25519_DEF = {
|
|
87
|
+
// Param: a
|
|
88
|
+
a: BigInt(-1),
|
|
89
|
+
// Equal to -121665/121666 over finite field.
|
|
90
|
+
// Negative number is P - number, and division is invert(number, P)
|
|
91
|
+
d: BigInt('37095705934669439343138083508754565189542113879843219016388785533085940283555'),
|
|
92
|
+
// Finite field 𝔽p over which we'll do calculations; 2n ** 255n - 19n
|
|
93
|
+
Fp,
|
|
94
|
+
// Subgroup order: how many points ed25519 has
|
|
95
|
+
// 2n ** 252n + 27742317777372353535851937790883648493n;
|
|
96
|
+
n: BigInt('7237005577332262213973186563042994240857116359379907606001950938285454250989'),
|
|
97
|
+
// Cofactor
|
|
98
|
+
h: BigInt(8),
|
|
99
|
+
// Base point (x, y) aka generator point
|
|
100
|
+
Gx: BigInt('15112221349535400772501151409588531511454012693041857206046113283949847762202'),
|
|
101
|
+
Gy: BigInt('46316835694926478169428394003475163141307993866256225615783033603165251855960'),
|
|
102
|
+
hash: sha512_1.sha512,
|
|
103
|
+
randomBytes: utils_1.randomBytes,
|
|
104
|
+
adjustScalarBytes,
|
|
105
|
+
// dom2
|
|
106
|
+
// Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
|
|
107
|
+
// Constant-time, u/√v
|
|
108
|
+
uvRatio,
|
|
109
|
+
};
|
|
110
|
+
exports.ed25519 = (0, edwards_js_1.twistedEdwards)(ED25519_DEF);
|
|
111
|
+
function ed25519_domain(data, ctx, phflag) {
|
|
112
|
+
if (ctx.length > 255)
|
|
113
|
+
throw new Error('Context is too big');
|
|
114
|
+
return (0, utils_1.concatBytes)((0, utils_1.utf8ToBytes)('SigEd25519 no Ed25519 collisions'), new Uint8Array([phflag ? 1 : 0, ctx.length]), ctx, data);
|
|
115
|
+
}
|
|
116
|
+
exports.ed25519ctx = (0, edwards_js_1.twistedEdwards)({ ...ED25519_DEF, domain: ed25519_domain });
|
|
117
|
+
exports.ed25519ph = (0, edwards_js_1.twistedEdwards)({
|
|
118
|
+
...ED25519_DEF,
|
|
119
|
+
domain: ed25519_domain,
|
|
120
|
+
preHash: sha512_1.sha512,
|
|
121
|
+
});
|
|
122
|
+
exports.x25519 = (0, montgomery_js_1.montgomery)({
|
|
123
|
+
P: ED25519_P,
|
|
124
|
+
a24: BigInt('121665'),
|
|
125
|
+
montgomeryBits: 255,
|
|
126
|
+
nByteLength: 32,
|
|
127
|
+
Gu: '0900000000000000000000000000000000000000000000000000000000000000',
|
|
128
|
+
powPminus2: (x) => {
|
|
129
|
+
const P = ED25519_P;
|
|
130
|
+
// x^(p-2) aka x^(2^255-21)
|
|
131
|
+
const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
|
|
132
|
+
return (0, modular_js_1.mod)((0, modular_js_1.pow2)(pow_p_5_8, BigInt(3), P) * b2, P);
|
|
133
|
+
},
|
|
134
|
+
adjustScalarBytes,
|
|
135
|
+
});
|
|
85
136
|
// Hash To Curve Elligator2 Map (NOTE: different from ristretto255 elligator)
|
|
86
137
|
// NOTE: very important part is usage of FpSqrtEven for ELL2_C1_EDWARDS, since
|
|
87
138
|
// SageMath returns different root first and everything falls apart
|
|
88
139
|
const ELL2_C1 = (Fp.ORDER + BigInt(3)) / BigInt(8); // 1. c1 = (q + 3) / 8 # Integer arithmetic
|
|
89
140
|
const ELL2_C2 = Fp.pow(_2n, ELL2_C1); // 2. c2 = 2^c1
|
|
90
|
-
const ELL2_C3 = Fp.sqrt(Fp.
|
|
141
|
+
const ELL2_C3 = Fp.sqrt(Fp.neg(Fp.ONE)); // 3. c3 = sqrt(-1)
|
|
91
142
|
const ELL2_C4 = (Fp.ORDER - BigInt(5)) / BigInt(8); // 4. c4 = (q - 5) / 8 # Integer arithmetic
|
|
92
143
|
const ELL2_J = BigInt(486662);
|
|
93
144
|
// prettier-ignore
|
|
94
145
|
function map_to_curve_elligator2_curve25519(u) {
|
|
95
|
-
let tv1 = Fp.
|
|
146
|
+
let tv1 = Fp.sqr(u); // 1. tv1 = u^2
|
|
96
147
|
tv1 = Fp.mul(tv1, _2n); // 2. tv1 = 2 * tv1
|
|
97
148
|
let xd = Fp.add(tv1, Fp.ONE); // 3. xd = tv1 + 1 # Nonzero: -1 is square (mod p), tv1 is not
|
|
98
|
-
let x1n = Fp.
|
|
99
|
-
let tv2 = Fp.
|
|
149
|
+
let x1n = Fp.neg(ELL2_J); // 4. x1n = -J # x1 = x1n / xd = -J / (1 + 2 * u^2)
|
|
150
|
+
let tv2 = Fp.sqr(xd); // 5. tv2 = xd^2
|
|
100
151
|
let gxd = Fp.mul(tv2, xd); // 6. gxd = tv2 * xd # gxd = xd^3
|
|
101
152
|
let gx1 = Fp.mul(tv1, ELL2_J); // 7. gx1 = J * tv1 # x1n + J * xd
|
|
102
153
|
gx1 = Fp.mul(gx1, x1n); // 8. gx1 = gx1 * x1n # x1n^2 + J * x1n * xd
|
|
103
154
|
gx1 = Fp.add(gx1, tv2); // 9. gx1 = gx1 + tv2 # x1n^2 + J * x1n * xd + xd^2
|
|
104
155
|
gx1 = Fp.mul(gx1, x1n); // 10. gx1 = gx1 * x1n # x1n^3 + J * x1n^2 * xd + x1n * xd^2
|
|
105
|
-
let tv3 = Fp.
|
|
106
|
-
tv2 = Fp.
|
|
156
|
+
let tv3 = Fp.sqr(gxd); // 11. tv3 = gxd^2
|
|
157
|
+
tv2 = Fp.sqr(tv3); // 12. tv2 = tv3^2 # gxd^4
|
|
107
158
|
tv3 = Fp.mul(tv3, gxd); // 13. tv3 = tv3 * gxd # gxd^3
|
|
108
159
|
tv3 = Fp.mul(tv3, gx1); // 14. tv3 = tv3 * gx1 # gx1 * gxd^3
|
|
109
160
|
tv2 = Fp.mul(tv2, tv3); // 15. tv2 = tv2 * tv3 # gx1 * gxd^7
|
|
110
161
|
let y11 = Fp.pow(tv2, ELL2_C4); // 16. y11 = tv2^c4 # (gx1 * gxd^7)^((p - 5) / 8)
|
|
111
162
|
y11 = Fp.mul(y11, tv3); // 17. y11 = y11 * tv3 # gx1*gxd^3*(gx1*gxd^7)^((p-5)/8)
|
|
112
163
|
let y12 = Fp.mul(y11, ELL2_C3); // 18. y12 = y11 * c3
|
|
113
|
-
tv2 = Fp.
|
|
164
|
+
tv2 = Fp.sqr(y11); // 19. tv2 = y11^2
|
|
114
165
|
tv2 = Fp.mul(tv2, gxd); // 20. tv2 = tv2 * gxd
|
|
115
|
-
let e1 = Fp.
|
|
166
|
+
let e1 = Fp.eql(tv2, gx1); // 21. e1 = tv2 == gx1
|
|
116
167
|
let y1 = Fp.cmov(y12, y11, e1); // 22. y1 = CMOV(y12, y11, e1) # If g(x1) is square, this is its sqrt
|
|
117
168
|
let x2n = Fp.mul(x1n, tv1); // 23. x2n = x1n * tv1 # x2 = x2n / xd = 2 * u^2 * x1n / xd
|
|
118
169
|
let y21 = Fp.mul(y11, u); // 24. y21 = y11 * u
|
|
119
170
|
y21 = Fp.mul(y21, ELL2_C2); // 25. y21 = y21 * c2
|
|
120
171
|
let y22 = Fp.mul(y21, ELL2_C3); // 26. y22 = y21 * c3
|
|
121
172
|
let gx2 = Fp.mul(gx1, tv1); // 27. gx2 = gx1 * tv1 # g(x2) = gx2 / gxd = 2 * u^2 * g(x1)
|
|
122
|
-
tv2 = Fp.
|
|
173
|
+
tv2 = Fp.sqr(y21); // 28. tv2 = y21^2
|
|
123
174
|
tv2 = Fp.mul(tv2, gxd); // 29. tv2 = tv2 * gxd
|
|
124
|
-
let e2 = Fp.
|
|
175
|
+
let e2 = Fp.eql(tv2, gx2); // 30. e2 = tv2 == gx2
|
|
125
176
|
let y2 = Fp.cmov(y22, y21, e2); // 31. y2 = CMOV(y22, y21, e2) # If g(x2) is square, this is its sqrt
|
|
126
|
-
tv2 = Fp.
|
|
177
|
+
tv2 = Fp.sqr(y1); // 32. tv2 = y1^2
|
|
127
178
|
tv2 = Fp.mul(tv2, gxd); // 33. tv2 = tv2 * gxd
|
|
128
|
-
let e3 = Fp.
|
|
179
|
+
let e3 = Fp.eql(tv2, gx1); // 34. e3 = tv2 == gx1
|
|
129
180
|
let xn = Fp.cmov(x2n, x1n, e3); // 35. xn = CMOV(x2n, x1n, e3) # If e3, x = x1, else x = x2
|
|
130
181
|
let y = Fp.cmov(y2, y1, e3); // 36. y = CMOV(y2, y1, e3) # If e3, y = y1, else y = y2
|
|
131
182
|
let e4 = Fp.isOdd(y); // 37. e4 = sgn0(y) == 1 # Fix sign of y
|
|
132
|
-
y = Fp.cmov(y, Fp.
|
|
183
|
+
y = Fp.cmov(y, Fp.neg(y), e3 !== e4); // 38. y = CMOV(y, -y, e3 XOR e4)
|
|
133
184
|
return { xMn: xn, xMd: xd, yMn: y, yMd: 1n }; // 39. return (xn, xd, y, 1)
|
|
134
185
|
}
|
|
135
|
-
const ELL2_C1_EDWARDS = (0, modular_js_1.FpSqrtEven)(Fp, Fp.
|
|
186
|
+
const ELL2_C1_EDWARDS = (0, modular_js_1.FpSqrtEven)(Fp, Fp.neg(BigInt(486664))); // sgn0(c1) MUST equal 0
|
|
136
187
|
function map_to_curve_elligator2_edwards25519(u) {
|
|
137
188
|
const { xMn, xMd, yMn, yMd } = map_to_curve_elligator2_curve25519(u); // 1. (xMn, xMd, yMn, yMd) = map_to_curve_elligator2_curve25519(u)
|
|
138
189
|
let xn = Fp.mul(xMn, yMd); // 2. xn = xMn * yMd
|
|
@@ -141,7 +192,7 @@ function map_to_curve_elligator2_edwards25519(u) {
|
|
|
141
192
|
let yn = Fp.sub(xMn, xMd); // 5. yn = xMn - xMd
|
|
142
193
|
let yd = Fp.add(xMn, xMd); // 6. yd = xMn + xMd # (n / d - 1) / (n / d + 1) = (n - d) / (n + d)
|
|
143
194
|
let tv1 = Fp.mul(xd, yd); // 7. tv1 = xd * yd
|
|
144
|
-
let e = Fp.
|
|
195
|
+
let e = Fp.eql(tv1, Fp.ZERO); // 8. e = tv1 == 0
|
|
145
196
|
xn = Fp.cmov(xn, Fp.ZERO, e); // 9. xn = CMOV(xn, 0, e)
|
|
146
197
|
xd = Fp.cmov(xd, Fp.ONE, e); // 10. xd = CMOV(xd, 1, e)
|
|
147
198
|
yn = Fp.cmov(yn, Fp.ONE, e); // 11. yn = CMOV(yn, 1, e)
|
|
@@ -149,68 +200,20 @@ function map_to_curve_elligator2_edwards25519(u) {
|
|
|
149
200
|
const inv = Fp.invertBatch([xd, yd]); // batch division
|
|
150
201
|
return { x: Fp.mul(xn, inv[0]), y: Fp.mul(yn, inv[1]) }; // 13. return (xn, xd, yn, yd)
|
|
151
202
|
}
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
Fp,
|
|
160
|
-
// Subgroup order: how many points ed25519 has
|
|
161
|
-
// 2n ** 252n + 27742317777372353535851937790883648493n;
|
|
162
|
-
n: BigInt('7237005577332262213973186563042994240857116359379907606001950938285454250989'),
|
|
163
|
-
// Cofactor
|
|
164
|
-
h: BigInt(8),
|
|
165
|
-
// Base point (x, y) aka generator point
|
|
166
|
-
Gx: BigInt('15112221349535400772501151409588531511454012693041857206046113283949847762202'),
|
|
167
|
-
Gy: BigInt('46316835694926478169428394003475163141307993866256225615783033603165251855960'),
|
|
203
|
+
const { hashToCurve, encodeToCurve } = htf.hashToCurve(exports.ed25519.ExtendedPoint, (scalars) => map_to_curve_elligator2_edwards25519(scalars[0]), {
|
|
204
|
+
DST: 'edwards25519_XMD:SHA-512_ELL2_RO_',
|
|
205
|
+
encodeDST: 'edwards25519_XMD:SHA-512_ELL2_NU_',
|
|
206
|
+
p: Fp.ORDER,
|
|
207
|
+
m: 1,
|
|
208
|
+
k: 128,
|
|
209
|
+
expand: 'xmd',
|
|
168
210
|
hash: sha512_1.sha512,
|
|
169
|
-
randomBytes: utils_1.randomBytes,
|
|
170
|
-
adjustScalarBytes,
|
|
171
|
-
// dom2
|
|
172
|
-
// Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
|
|
173
|
-
// Constant-time, u/√v
|
|
174
|
-
uvRatio,
|
|
175
|
-
htfDefaults: {
|
|
176
|
-
DST: 'edwards25519_XMD:SHA-512_ELL2_RO_',
|
|
177
|
-
p: Fp.ORDER,
|
|
178
|
-
m: 1,
|
|
179
|
-
k: 128,
|
|
180
|
-
expand: 'xmd',
|
|
181
|
-
hash: sha512_1.sha512,
|
|
182
|
-
},
|
|
183
|
-
mapToCurve: (scalars) => map_to_curve_elligator2_edwards25519(scalars[0]),
|
|
184
|
-
};
|
|
185
|
-
exports.ed25519 = (0, edwards_js_1.twistedEdwards)(ED25519_DEF);
|
|
186
|
-
function ed25519_domain(data, ctx, phflag) {
|
|
187
|
-
if (ctx.length > 255)
|
|
188
|
-
throw new Error('Context is too big');
|
|
189
|
-
return (0, utils_1.concatBytes)((0, utils_1.utf8ToBytes)('SigEd25519 no Ed25519 collisions'), new Uint8Array([phflag ? 1 : 0, ctx.length]), ctx, data);
|
|
190
|
-
}
|
|
191
|
-
exports.ed25519ctx = (0, edwards_js_1.twistedEdwards)({ ...ED25519_DEF, domain: ed25519_domain });
|
|
192
|
-
exports.ed25519ph = (0, edwards_js_1.twistedEdwards)({
|
|
193
|
-
...ED25519_DEF,
|
|
194
|
-
domain: ed25519_domain,
|
|
195
|
-
preHash: sha512_1.sha512,
|
|
196
|
-
});
|
|
197
|
-
exports.x25519 = (0, montgomery_js_1.montgomery)({
|
|
198
|
-
P: ED25519_P,
|
|
199
|
-
a24: BigInt('121665'),
|
|
200
|
-
montgomeryBits: 255,
|
|
201
|
-
nByteLength: 32,
|
|
202
|
-
Gu: '0900000000000000000000000000000000000000000000000000000000000000',
|
|
203
|
-
powPminus2: (x) => {
|
|
204
|
-
const P = ED25519_P;
|
|
205
|
-
// x^(p-2) aka x^(2^255-21)
|
|
206
|
-
const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
|
|
207
|
-
return (0, modular_js_1.mod)((0, modular_js_1.pow2)(pow_p_5_8, BigInt(3), P) * b2, P);
|
|
208
|
-
},
|
|
209
|
-
adjustScalarBytes,
|
|
210
211
|
});
|
|
212
|
+
exports.hashToCurve = hashToCurve;
|
|
213
|
+
exports.encodeToCurve = encodeToCurve;
|
|
211
214
|
function assertRstPoint(other) {
|
|
212
215
|
if (!(other instanceof RistrettoPoint))
|
|
213
|
-
throw new
|
|
216
|
+
throw new Error('RistrettoPoint expected');
|
|
214
217
|
}
|
|
215
218
|
// √(-1) aka √(a) aka 2^((p-1)/4)
|
|
216
219
|
const SQRT_M1 = BigInt('19681161376707505956807079304988542015446066515923890162744021073123829784752');
|
|
@@ -319,7 +322,7 @@ class RistrettoPoint {
|
|
|
319
322
|
* https://ristretto.group/formulas/encoding.html
|
|
320
323
|
*/
|
|
321
324
|
toRawBytes() {
|
|
322
|
-
let { x, y, z, t } = this.ep;
|
|
325
|
+
let { ex: x, ey: y, ez: z, et: t } = this.ep;
|
|
323
326
|
const P = exports.ed25519.CURVE.Fp.ORDER;
|
|
324
327
|
const mod = exports.ed25519.CURVE.Fp.create;
|
|
325
328
|
const u1 = mod(mod(z + y) * mod(z - y)); // 1
|
|
@@ -357,12 +360,12 @@ class RistrettoPoint {
|
|
|
357
360
|
// Compare one point to another.
|
|
358
361
|
equals(other) {
|
|
359
362
|
assertRstPoint(other);
|
|
360
|
-
const
|
|
361
|
-
const
|
|
363
|
+
const { ex: X1, ey: Y1 } = this.ep;
|
|
364
|
+
const { ex: X2, ey: Y2 } = this.ep;
|
|
362
365
|
const mod = exports.ed25519.CURVE.Fp.create;
|
|
363
366
|
// (x1 * y2 == y1 * x2) | (y1 * y2 == x1 * x2)
|
|
364
|
-
const one = mod(
|
|
365
|
-
const two = mod(
|
|
367
|
+
const one = mod(X1 * Y2) === mod(Y1 * X2);
|
|
368
|
+
const two = mod(Y1 * Y2) === mod(X1 * X2);
|
|
366
369
|
return one || two;
|
|
367
370
|
}
|
|
368
371
|
add(other) {
|
package/lib/ed448.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import * as htf from './abstract/hash-to-curve.js';
|
|
1
2
|
export declare const ed448: import("./abstract/edwards.js").CurveFn;
|
|
2
3
|
export declare const ed448ph: import("./abstract/edwards.js").CurveFn;
|
|
3
4
|
export declare const x448: import("./abstract/montgomery.js").CurveFn;
|
|
5
|
+
declare const hashToCurve: (msg: import("./abstract/utils.js").Hex, options?: htf.htfBasicOpts | undefined) => htf.H2CPoint<bigint>, encodeToCurve: (msg: import("./abstract/utils.js").Hex, options?: htf.htfBasicOpts | undefined) => htf.H2CPoint<bigint>;
|
|
6
|
+
export { hashToCurve, encodeToCurve };
|