@noble/curves 0.5.1 → 0.6.0
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 +49 -8
- package/lib/_shortw_utils.d.ts +11 -26
- package/lib/abstract/bls.d.ts +51 -35
- package/lib/abstract/bls.js +77 -139
- package/lib/abstract/{group.d.ts → curve.d.ts} +31 -1
- package/lib/abstract/{group.js → curve.js} +39 -2
- package/lib/abstract/edwards.d.ts +30 -81
- package/lib/abstract/edwards.js +225 -420
- 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 +20 -7
- package/lib/abstract/modular.js +80 -51
- package/lib/abstract/montgomery.js +3 -4
- package/lib/abstract/poseidon.d.ts +29 -0
- package/lib/abstract/poseidon.js +115 -0
- package/lib/abstract/utils.d.ts +5 -34
- package/lib/abstract/utils.js +23 -63
- package/lib/abstract/weierstrass.d.ts +56 -79
- package/lib/abstract/weierstrass.js +509 -641
- package/lib/bls12-381.d.ts +1 -0
- package/lib/bls12-381.js +75 -65
- package/lib/bn.js +1 -1
- package/lib/ed25519.d.ts +7 -5
- package/lib/ed25519.js +87 -84
- package/lib/ed448.d.ts +3 -0
- package/lib/ed448.js +88 -84
- package/lib/esm/abstract/bls.js +77 -139
- package/lib/esm/abstract/{group.js → curve.js} +37 -1
- package/lib/esm/abstract/edwards.js +223 -418
- package/lib/esm/abstract/hash-to-curve.js +38 -11
- package/lib/esm/abstract/modular.js +77 -50
- package/lib/esm/abstract/montgomery.js +4 -7
- package/lib/esm/abstract/poseidon.js +109 -0
- package/lib/esm/abstract/utils.js +21 -59
- package/lib/esm/abstract/weierstrass.js +508 -640
- package/lib/esm/bls12-381.js +86 -76
- package/lib/esm/bn.js +1 -1
- package/lib/esm/ed25519.js +85 -83
- package/lib/esm/ed448.js +86 -83
- package/lib/esm/jubjub.js +6 -5
- package/lib/esm/p256.js +11 -9
- package/lib/esm/p384.js +11 -9
- package/lib/esm/p521.js +13 -12
- package/lib/esm/secp256k1.js +118 -157
- package/lib/esm/stark.js +104 -39
- package/lib/jubjub.d.ts +3 -2
- package/lib/jubjub.js +6 -5
- package/lib/p192.d.ts +22 -52
- package/lib/p224.d.ts +22 -52
- package/lib/p256.d.ts +25 -52
- package/lib/p256.js +13 -10
- package/lib/p384.d.ts +25 -52
- package/lib/p384.js +13 -10
- package/lib/p521.d.ts +25 -52
- package/lib/p521.js +15 -13
- package/lib/secp256k1.d.ts +26 -42
- package/lib/secp256k1.js +118 -157
- package/lib/stark.d.ts +36 -21
- package/lib/stark.js +107 -39
- package/package.json +14 -9
package/lib/bls12-381.d.ts
CHANGED
package/lib/bls12-381.js
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
4
|
exports.bls12_381 = void 0;
|
|
4
|
-
|
|
5
|
+
// The pairing-friendly Barreto-Lynn-Scott elliptic curve construction allows to:
|
|
6
|
+
// - Construct zk-SNARKs at the 128-bit security
|
|
7
|
+
// - Use threshold signatures, which allows a user to sign lots of messages with one signature and verify them swiftly in a batch, using Boneh-Lynn-Shacham signature scheme.
|
|
8
|
+
// Differences from @noble/bls12-381 1.4:
|
|
9
|
+
// - PointG1 -> G1.Point
|
|
10
|
+
// - PointG2 -> G2.Point
|
|
11
|
+
// - PointG2.fromSignature -> Signature.decode
|
|
12
|
+
// - PointG2.toSignature -> Signature.encode
|
|
13
|
+
// - Fixed Fp2 ORDER
|
|
14
|
+
// - Points now have only two coordinates
|
|
5
15
|
const sha256_1 = require("@noble/hashes/sha256");
|
|
6
16
|
const utils_1 = require("@noble/hashes/utils");
|
|
7
17
|
const bls_js_1 = require("./abstract/bls.js");
|
|
@@ -10,13 +20,6 @@ const utils_js_1 = require("./abstract/utils.js");
|
|
|
10
20
|
// Types
|
|
11
21
|
const weierstrass_js_1 = require("./abstract/weierstrass.js");
|
|
12
22
|
const hash_to_curve_js_1 = require("./abstract/hash-to-curve.js");
|
|
13
|
-
// Differences from bls12-381:
|
|
14
|
-
// - PointG1 -> G1.Point
|
|
15
|
-
// - PointG2 -> G2.Point
|
|
16
|
-
// - PointG2.fromSignature -> Signature.decode
|
|
17
|
-
// - PointG2.toSignature -> Signature.encode
|
|
18
|
-
// - Fixed Fp2 ORDER
|
|
19
|
-
// Points now have only two coordinates
|
|
20
23
|
// CURVE FIELDS
|
|
21
24
|
// Finite field over p.
|
|
22
25
|
const Fp = mod.Fp(0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn);
|
|
@@ -65,24 +68,24 @@ const Fp2 = {
|
|
|
65
68
|
ONE: { c0: Fp.ONE, c1: Fp.ZERO },
|
|
66
69
|
create: (num) => num,
|
|
67
70
|
isValid: ({ c0, c1 }) => typeof c0 === 'bigint' && typeof c1 === 'bigint',
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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) }),
|
|
71
74
|
pow: (num, power) => mod.FpPow(Fp2, num, power),
|
|
72
75
|
invertBatch: (nums) => mod.FpInvertBatch(Fp2, nums),
|
|
73
76
|
// Normalized
|
|
74
77
|
add: Fp2Add,
|
|
75
78
|
sub: Fp2Subtract,
|
|
76
79
|
mul: Fp2Multiply,
|
|
77
|
-
|
|
80
|
+
sqr: Fp2Square,
|
|
78
81
|
// NonNormalized stuff
|
|
79
82
|
addN: Fp2Add,
|
|
80
83
|
subN: Fp2Subtract,
|
|
81
84
|
mulN: Fp2Multiply,
|
|
82
|
-
|
|
85
|
+
sqrN: Fp2Square,
|
|
83
86
|
// Why inversion for bigint inside Fp instead of Fp2? it is even used in that context?
|
|
84
|
-
div: (lhs, rhs) => Fp2.mul(lhs, typeof rhs === 'bigint' ? Fp.
|
|
85
|
-
|
|
87
|
+
div: (lhs, rhs) => Fp2.mul(lhs, typeof rhs === 'bigint' ? Fp.inv(Fp.create(rhs)) : Fp2.inv(rhs)),
|
|
88
|
+
inv: ({ c0: a, c1: b }) => {
|
|
86
89
|
// We wish to find the multiplicative inverse of a nonzero
|
|
87
90
|
// element a + bu in Fp2. We leverage an identity
|
|
88
91
|
//
|
|
@@ -96,10 +99,12 @@ const Fp2 = {
|
|
|
96
99
|
// This gives that (a - bu)/(a² + b²) is the inverse
|
|
97
100
|
// of (a + bu). Importantly, this can be computing using
|
|
98
101
|
// only a single inversion in Fp.
|
|
99
|
-
const factor = Fp.
|
|
102
|
+
const factor = Fp.inv(Fp.create(a * a + b * b));
|
|
100
103
|
return { c0: Fp.mul(factor, Fp.create(a)), c1: Fp.mul(factor, Fp.create(-b)) };
|
|
101
104
|
},
|
|
102
105
|
sqrt: (num) => {
|
|
106
|
+
if (Fp2.eql(num, Fp2.ZERO))
|
|
107
|
+
return Fp2.ZERO; // Algo doesn't handles this case
|
|
103
108
|
// TODO: Optimize this line. It's extremely slow.
|
|
104
109
|
// Speeding this up would boost aggregateSignatures.
|
|
105
110
|
// https://eprint.iacr.org/2012/685.pdf applicable?
|
|
@@ -107,9 +112,9 @@ const Fp2 = {
|
|
|
107
112
|
// https://github.com/supranational/blst/blob/aae0c7d70b799ac269ff5edf29d8191dbd357876/src/exp2.c#L1
|
|
108
113
|
// Inspired by https://github.com/dalek-cryptography/curve25519-dalek/blob/17698df9d4c834204f83a3574143abacb4fc81a5/src/field.rs#L99
|
|
109
114
|
const candidateSqrt = Fp2.pow(num, (Fp2.ORDER + 8n) / 16n);
|
|
110
|
-
const check = Fp2.div(Fp2.
|
|
115
|
+
const check = Fp2.div(Fp2.sqr(candidateSqrt), num); // candidateSqrt.square().div(this);
|
|
111
116
|
const R = FP2_ROOTS_OF_UNITY;
|
|
112
|
-
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));
|
|
113
118
|
if (!divisor)
|
|
114
119
|
throw new Error('No root');
|
|
115
120
|
const index = R.indexOf(divisor);
|
|
@@ -117,7 +122,7 @@ const Fp2 = {
|
|
|
117
122
|
if (!root)
|
|
118
123
|
throw new Error('Invalid root');
|
|
119
124
|
const x1 = Fp2.div(candidateSqrt, root);
|
|
120
|
-
const x2 = Fp2.
|
|
125
|
+
const x2 = Fp2.neg(x1);
|
|
121
126
|
const { re: re1, im: im1 } = Fp2.reim(x1);
|
|
122
127
|
const { re: re2, im: im2 } = Fp2.reim(x2);
|
|
123
128
|
if (im1 > im2 || (im1 === im2 && re1 > re2))
|
|
@@ -228,15 +233,15 @@ const Fp6Multiply = ({ c0, c1, c2 }, rhs) => {
|
|
|
228
233
|
};
|
|
229
234
|
};
|
|
230
235
|
const Fp6Square = ({ c0, c1, c2 }) => {
|
|
231
|
-
let t0 = Fp2.
|
|
236
|
+
let t0 = Fp2.sqr(c0); // c0²
|
|
232
237
|
let t1 = Fp2.mul(Fp2.mul(c0, c1), 2n); // 2 * c0 * c1
|
|
233
238
|
let t3 = Fp2.mul(Fp2.mul(c1, c2), 2n); // 2 * c1 * c2
|
|
234
|
-
let t4 = Fp2.
|
|
239
|
+
let t4 = Fp2.sqr(c2); // c2²
|
|
235
240
|
return {
|
|
236
241
|
c0: Fp2.add(Fp2.mulByNonresidue(t3), t0),
|
|
237
242
|
c1: Fp2.add(Fp2.mulByNonresidue(t4), t1),
|
|
238
243
|
// T1 + (c0 - c1 + c2)² + T3 - T0 - T4
|
|
239
|
-
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),
|
|
240
245
|
};
|
|
241
246
|
};
|
|
242
247
|
const Fp6 = {
|
|
@@ -248,32 +253,32 @@ const Fp6 = {
|
|
|
248
253
|
ONE: { c0: Fp2.ONE, c1: Fp2.ZERO, c2: Fp2.ZERO },
|
|
249
254
|
create: (num) => num,
|
|
250
255
|
isValid: ({ c0, c1, c2 }) => Fp2.isValid(c0) && Fp2.isValid(c1) && Fp2.isValid(c2),
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
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),
|
|
254
259
|
sqrt: () => {
|
|
255
260
|
throw new Error('Not implemented');
|
|
256
261
|
},
|
|
257
262
|
// Do we need division by bigint at all? Should be done via order:
|
|
258
|
-
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)),
|
|
259
264
|
pow: (num, power) => mod.FpPow(Fp6, num, power),
|
|
260
265
|
invertBatch: (nums) => mod.FpInvertBatch(Fp6, nums),
|
|
261
266
|
// Normalized
|
|
262
267
|
add: Fp6Add,
|
|
263
268
|
sub: Fp6Subtract,
|
|
264
269
|
mul: Fp6Multiply,
|
|
265
|
-
|
|
270
|
+
sqr: Fp6Square,
|
|
266
271
|
// NonNormalized stuff
|
|
267
272
|
addN: Fp6Add,
|
|
268
273
|
subN: Fp6Subtract,
|
|
269
274
|
mulN: Fp6Multiply,
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
let t0 = Fp2.sub(Fp2.
|
|
273
|
-
let t1 = Fp2.sub(Fp2.mulByNonresidue(Fp2.
|
|
274
|
-
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
|
|
275
280
|
// 1/(((c2 * T1 + c1 * T2) * v) + c0 * T0)
|
|
276
|
-
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)));
|
|
277
282
|
return { c0: Fp2.mul(t4, t0), c1: Fp2.mul(t4, t1), c2: Fp2.mul(t4, t2) };
|
|
278
283
|
},
|
|
279
284
|
// Bytes utils
|
|
@@ -414,11 +419,11 @@ const Fp12Square = ({ c0, c1 }) => {
|
|
|
414
419
|
}; // AB + AB
|
|
415
420
|
};
|
|
416
421
|
function Fp4Square(a, b) {
|
|
417
|
-
const a2 = Fp2.
|
|
418
|
-
const b2 = Fp2.
|
|
422
|
+
const a2 = Fp2.sqr(a);
|
|
423
|
+
const b2 = Fp2.sqr(b);
|
|
419
424
|
return {
|
|
420
425
|
first: Fp2.add(Fp2.mulByNonresidue(b2), a2),
|
|
421
|
-
second: Fp2.sub(Fp2.sub(Fp2.
|
|
426
|
+
second: Fp2.sub(Fp2.sub(Fp2.sqr(Fp2.add(a, b)), a2), b2), // (a + b)² - a² - b²
|
|
422
427
|
};
|
|
423
428
|
}
|
|
424
429
|
const Fp12 = {
|
|
@@ -430,29 +435,29 @@ const Fp12 = {
|
|
|
430
435
|
ONE: { c0: Fp6.ONE, c1: Fp6.ZERO },
|
|
431
436
|
create: (num) => num,
|
|
432
437
|
isValid: ({ c0, c1 }) => Fp6.isValid(c0) && Fp6.isValid(c1),
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
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),
|
|
436
441
|
sqrt: () => {
|
|
437
442
|
throw new Error('Not implemented');
|
|
438
443
|
},
|
|
439
|
-
|
|
440
|
-
let t = Fp6.
|
|
441
|
-
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
|
|
442
447
|
},
|
|
443
|
-
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)),
|
|
444
449
|
pow: (num, power) => mod.FpPow(Fp12, num, power),
|
|
445
450
|
invertBatch: (nums) => mod.FpInvertBatch(Fp12, nums),
|
|
446
451
|
// Normalized
|
|
447
452
|
add: Fp12Add,
|
|
448
453
|
sub: Fp12Subtract,
|
|
449
454
|
mul: Fp12Multiply,
|
|
450
|
-
|
|
455
|
+
sqr: Fp12Square,
|
|
451
456
|
// NonNormalized stuff
|
|
452
457
|
addN: Fp12Add,
|
|
453
458
|
subN: Fp12Subtract,
|
|
454
459
|
mulN: Fp12Multiply,
|
|
455
|
-
|
|
460
|
+
sqrN: Fp12Square,
|
|
456
461
|
// Bytes utils
|
|
457
462
|
fromBytes: (b) => {
|
|
458
463
|
if (b.length !== Fp12.BYTES)
|
|
@@ -506,7 +511,7 @@ const Fp12 = {
|
|
|
506
511
|
c0: Fp6.multiplyByFp2(c0, rhs),
|
|
507
512
|
c1: Fp6.multiplyByFp2(c1, rhs),
|
|
508
513
|
}),
|
|
509
|
-
conjugate: ({ c0, c1 }) => ({ c0, c1: Fp6.
|
|
514
|
+
conjugate: ({ c0, c1 }) => ({ c0, c1: Fp6.neg(c1) }),
|
|
510
515
|
// A cyclotomic group is a subgroup of Fp^n defined by
|
|
511
516
|
// GΦₙ(p) = {α ∈ Fpⁿ : α^Φₙ(p) = 1}
|
|
512
517
|
// The result of any pairing is in a cyclotomic subgroup
|
|
@@ -785,7 +790,7 @@ function G2psi(c, P) {
|
|
|
785
790
|
// 1 / F2(2)^((p-1)/3) in GF(p²)
|
|
786
791
|
const PSI2_C1 = 0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaacn;
|
|
787
792
|
function psi2(x, y) {
|
|
788
|
-
return [Fp2.mul(x, PSI2_C1), Fp2.
|
|
793
|
+
return [Fp2.mul(x, PSI2_C1), Fp2.neg(y)];
|
|
789
794
|
}
|
|
790
795
|
function G2psi2(c, P) {
|
|
791
796
|
const affine = P.toAffine();
|
|
@@ -807,6 +812,7 @@ const htfDefaults = {
|
|
|
807
812
|
// defined in section 2.2.5
|
|
808
813
|
// Use utils.getDSTLabel(), utils.setDSTLabel(value)
|
|
809
814
|
DST: 'BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_',
|
|
815
|
+
encodeDST: 'BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_',
|
|
810
816
|
// p: the characteristic of F
|
|
811
817
|
// where F is a finite field of characteristic p and order q = p^m
|
|
812
818
|
p: Fp.ORDER,
|
|
@@ -873,7 +879,7 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
873
879
|
isTorsionFree: (c, point) => {
|
|
874
880
|
// φ endomorphism
|
|
875
881
|
const cubicRootOfUnityModP = 0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffen;
|
|
876
|
-
const phi = new c(Fp.mul(point.
|
|
882
|
+
const phi = new c(Fp.mul(point.px, cubicRootOfUnityModP), point.py, point.pz);
|
|
877
883
|
// todo: unroll
|
|
878
884
|
const xP = point.multiplyUnsafe(exports.bls12_381.CURVE.x).negate(); // [x]P
|
|
879
885
|
const u2P = xP.multiplyUnsafe(exports.bls12_381.CURVE.x); // [u2]P
|
|
@@ -915,13 +921,13 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
915
921
|
throw new Error('Invalid compressed G1 point');
|
|
916
922
|
const aflag = (0, utils_js_1.bitGet)(compressedValue, C_BIT_POS);
|
|
917
923
|
if ((y * 2n) / P !== aflag)
|
|
918
|
-
y = Fp.
|
|
924
|
+
y = Fp.neg(y);
|
|
919
925
|
return { x: Fp.create(x), y: Fp.create(y) };
|
|
920
926
|
}
|
|
921
927
|
else if (bytes.length === 96) {
|
|
922
928
|
// Check if the infinity flag is set
|
|
923
929
|
if ((bytes[0] & (1 << 6)) !== 0)
|
|
924
|
-
return exports.bls12_381.G1.
|
|
930
|
+
return exports.bls12_381.G1.ProjectivePoint.ZERO.toAffine();
|
|
925
931
|
const x = (0, utils_js_1.bytesToNumberBE)(bytes.slice(0, Fp.BYTES));
|
|
926
932
|
const y = (0, utils_js_1.bytesToNumberBE)(bytes.slice(Fp.BYTES));
|
|
927
933
|
return { x: Fp.create(x), y: Fp.create(y) };
|
|
@@ -932,7 +938,7 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
932
938
|
},
|
|
933
939
|
toBytes: (c, point, isCompressed) => {
|
|
934
940
|
const isZero = point.equals(c.ZERO);
|
|
935
|
-
const { x, y } = point;
|
|
941
|
+
const { x, y } = point.toAffine();
|
|
936
942
|
if (isCompressed) {
|
|
937
943
|
if (isZero)
|
|
938
944
|
return COMPRESSED_ZERO.slice();
|
|
@@ -1019,6 +1025,8 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
1019
1025
|
const bitC = m_byte & 0x80; // compression bit
|
|
1020
1026
|
const bitI = m_byte & 0x40; // point at infinity bit
|
|
1021
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));
|
|
1022
1030
|
if (bytes.length === 96 && bitC) {
|
|
1023
1031
|
const { b } = exports.bls12_381.CURVE.G2;
|
|
1024
1032
|
const P = Fp.ORDER;
|
|
@@ -1030,13 +1038,13 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
1030
1038
|
}
|
|
1031
1039
|
return { x: Fp2.ZERO, y: Fp2.ZERO };
|
|
1032
1040
|
}
|
|
1033
|
-
const x_1 = (
|
|
1034
|
-
const x_0 = (
|
|
1041
|
+
const x_1 = slc(bytes, 0, L);
|
|
1042
|
+
const x_0 = slc(bytes, L, 2 * L);
|
|
1035
1043
|
const x = Fp2.create({ c0: Fp.create(x_0), c1: Fp.create(x_1) });
|
|
1036
1044
|
const right = Fp2.add(Fp2.pow(x, 3n), b); // y² = x³ + 4 * (u+1) = x³ + b
|
|
1037
1045
|
let y = Fp2.sqrt(right);
|
|
1038
1046
|
const Y_bit = y.c1 === 0n ? (y.c0 * 2n) / P : (y.c1 * 2n) / P ? 1n : 0n;
|
|
1039
|
-
y = bitS > 0 && Y_bit > 0 ? y : Fp2.
|
|
1047
|
+
y = bitS > 0 && Y_bit > 0 ? y : Fp2.neg(y);
|
|
1040
1048
|
return { x, y };
|
|
1041
1049
|
}
|
|
1042
1050
|
else if (bytes.length === 192 && !bitC) {
|
|
@@ -1044,10 +1052,10 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
1044
1052
|
if ((bytes[0] & (1 << 6)) !== 0) {
|
|
1045
1053
|
return { x: Fp2.ZERO, y: Fp2.ZERO };
|
|
1046
1054
|
}
|
|
1047
|
-
const x1 = (
|
|
1048
|
-
const x0 = (
|
|
1049
|
-
const y1 = (
|
|
1050
|
-
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);
|
|
1051
1059
|
return { x: Fp2.fromBigTuple([x0, x1]), y: Fp2.fromBigTuple([y0, y1]) };
|
|
1052
1060
|
}
|
|
1053
1061
|
else {
|
|
@@ -1056,7 +1064,7 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
1056
1064
|
},
|
|
1057
1065
|
toBytes: (c, point, isCompressed) => {
|
|
1058
1066
|
const isZero = point.equals(c.ZERO);
|
|
1059
|
-
const { x, y } = point;
|
|
1067
|
+
const { x, y } = point.toAffine();
|
|
1060
1068
|
if (isCompressed) {
|
|
1061
1069
|
const P = Fp.ORDER;
|
|
1062
1070
|
if (isZero)
|
|
@@ -1088,7 +1096,7 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
1088
1096
|
// Indicates the infinity point
|
|
1089
1097
|
const bflag1 = (0, utils_js_1.bitGet)(z1, I_BIT_POS);
|
|
1090
1098
|
if (bflag1 === 1n)
|
|
1091
|
-
return exports.bls12_381.G2.
|
|
1099
|
+
return exports.bls12_381.G2.ProjectivePoint.ZERO;
|
|
1092
1100
|
const x1 = Fp.create(z1 & Fp.MASK);
|
|
1093
1101
|
const x2 = Fp.create(z2);
|
|
1094
1102
|
const x = Fp2.create({ c0: x2, c1: x1 });
|
|
@@ -1104,18 +1112,20 @@ exports.bls12_381 = (0, bls_js_1.bls)({
|
|
|
1104
1112
|
const isGreater = y1 > 0n && (y1 * 2n) / P !== aflag1;
|
|
1105
1113
|
const isZero = y1 === 0n && (y0 * 2n) / P !== aflag1;
|
|
1106
1114
|
if (isGreater || isZero)
|
|
1107
|
-
y = Fp2.
|
|
1108
|
-
const point =
|
|
1115
|
+
y = Fp2.neg(y);
|
|
1116
|
+
const point = exports.bls12_381.G2.ProjectivePoint.fromAffine({ x, y });
|
|
1117
|
+
// console.log('Signature.decode', point);
|
|
1109
1118
|
point.assertValidity();
|
|
1110
1119
|
return point;
|
|
1111
1120
|
},
|
|
1112
1121
|
encode(point) {
|
|
1113
1122
|
// NOTE: by some reasons it was missed in bls12-381, looks like bug
|
|
1114
1123
|
point.assertValidity();
|
|
1115
|
-
if (point.equals(exports.bls12_381.G2.
|
|
1124
|
+
if (point.equals(exports.bls12_381.G2.ProjectivePoint.ZERO))
|
|
1116
1125
|
return (0, utils_js_1.concatBytes)(COMPRESSED_ZERO, (0, utils_js_1.numberToBytesBE)(0n, Fp.BYTES));
|
|
1117
|
-
const
|
|
1118
|
-
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);
|
|
1119
1129
|
const tmp = y1 > 0n ? y1 * 2n : y0 * 2n;
|
|
1120
1130
|
const aflag1 = Boolean((tmp / Fp.ORDER) & 1n);
|
|
1121
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 {};
|