@noble/curves 0.8.0 → 0.8.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 +12 -17
- package/esm/_shortw_utils.js +17 -0
- package/esm/_shortw_utils.js.map +1 -0
- package/esm/abstract/bls.js +226 -0
- package/esm/abstract/bls.js.map +1 -0
- package/esm/abstract/curve.js +152 -0
- package/esm/abstract/curve.js.map +1 -0
- package/esm/abstract/edwards.js +409 -0
- package/esm/abstract/edwards.js.map +1 -0
- package/esm/abstract/hash-to-curve.js +166 -0
- package/esm/abstract/hash-to-curve.js.map +1 -0
- package/esm/abstract/modular.js +346 -0
- package/esm/abstract/modular.js.map +1 -0
- package/esm/abstract/montgomery.js +157 -0
- package/esm/abstract/montgomery.js.map +1 -0
- package/esm/abstract/poseidon.js +110 -0
- package/esm/abstract/poseidon.js.map +1 -0
- package/esm/abstract/utils.js +222 -0
- package/esm/abstract/utils.js.map +1 -0
- package/esm/abstract/weierstrass.js +1011 -0
- package/esm/abstract/weierstrass.js.map +1 -0
- package/esm/bls12-381.js +1173 -0
- package/esm/bls12-381.js.map +1 -0
- package/esm/bn.js +22 -0
- package/esm/bn.js.map +1 -0
- package/esm/ed25519.js +397 -0
- package/esm/ed25519.js.map +1 -0
- package/esm/ed448.js +213 -0
- package/esm/ed448.js.map +1 -0
- package/esm/index.js +3 -0
- package/esm/index.js.map +1 -0
- package/esm/jubjub.js +54 -0
- package/esm/jubjub.js.map +1 -0
- package/esm/p256.js +42 -0
- package/esm/p256.js.map +1 -0
- package/esm/p384.js +47 -0
- package/esm/p384.js.map +1 -0
- package/esm/p521.js +48 -0
- package/esm/p521.js.map +1 -0
- package/esm/package.json +7 -0
- package/esm/pasta.js +30 -0
- package/esm/pasta.js.map +1 -0
- package/esm/secp256k1.js +252 -0
- package/esm/secp256k1.js.map +1 -0
- package/package.json +27 -9
- package/src/stark.ts +0 -341
- package/stark.d.ts +0 -81
- package/stark.d.ts.map +0 -1
- package/stark.js +0 -277
- package/stark.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# noble-curves
|
|
2
2
|
|
|
3
|
-
Audited & minimal JS implementation of elliptic curve cryptography.
|
|
3
|
+
[Audited](#security) & minimal JS implementation of elliptic curve cryptography.
|
|
4
4
|
|
|
5
5
|
- **noble** family, zero dependencies
|
|
6
6
|
- Short Weierstrass, Edwards, Montgomery curves
|
|
@@ -56,7 +56,7 @@ Instead, you need to import specific primitives. This is done to ensure small si
|
|
|
56
56
|
Each curve can be used in the following way:
|
|
57
57
|
|
|
58
58
|
```ts
|
|
59
|
-
import { secp256k1 } from '@noble/curves/secp256k1'; // ECMAScript Modules (ESM)
|
|
59
|
+
import { secp256k1 } from '@noble/curves/secp256k1'; // ECMAScript Modules (ESM) and Common.js
|
|
60
60
|
// import { secp256k1 } from 'npm:@noble/curves@1.2.0/secp256k1'; // Deno
|
|
61
61
|
const priv = secp256k1.utils.randomPrivateKey();
|
|
62
62
|
const pub = secp256k1.getPublicKey(priv);
|
|
@@ -78,7 +78,6 @@ import { p256 } from '@noble/curves/p256';
|
|
|
78
78
|
import { p384 } from '@noble/curves/p384';
|
|
79
79
|
import { p521 } from '@noble/curves/p521';
|
|
80
80
|
import { pallas, vesta } from '@noble/curves/pasta';
|
|
81
|
-
import * as stark from '@noble/curves/stark';
|
|
82
81
|
import { bls12_381 } from '@noble/curves/bls12-381';
|
|
83
82
|
import { bn254 } from '@noble/curves/bn';
|
|
84
83
|
import { jubjub } from '@noble/curves/jubjub';
|
|
@@ -530,7 +529,7 @@ Implements [Poseidon](https://www.poseidon-hash.info) ZK-friendly hash.
|
|
|
530
529
|
|
|
531
530
|
There are many poseidon variants with different constants.
|
|
532
531
|
We don't provide them: you should construct them manually.
|
|
533
|
-
|
|
532
|
+
Check out [micro-starknet](https://github.com/paulmillr/micro-starknet) package for a proper example.
|
|
534
533
|
|
|
535
534
|
```ts
|
|
536
535
|
import { poseidon } from '@noble/curves/abstract/poseidon';
|
|
@@ -541,7 +540,7 @@ type PoseidonOpts = {
|
|
|
541
540
|
roundsFull: number;
|
|
542
541
|
roundsPartial: number;
|
|
543
542
|
sboxPower?: number;
|
|
544
|
-
reversePartialPowIdx?: boolean;
|
|
543
|
+
reversePartialPowIdx?: boolean;
|
|
545
544
|
mds: bigint[][];
|
|
546
545
|
roundConstants: bigint[][];
|
|
547
546
|
};
|
|
@@ -612,9 +611,11 @@ utils.equalBytes(Uint8Array.from([0xde]), Uint8Array.from([0xde]));
|
|
|
612
611
|
|
|
613
612
|
## Security
|
|
614
613
|
|
|
615
|
-
The library
|
|
616
|
-
|
|
617
|
-
|
|
614
|
+
1. The library has been audited during Jan-Feb 2023 by an independent security firm [Trail of Bits](https://www.trailofbits.com):
|
|
615
|
+
[PDF](https://github.com/trailofbits/publications/blob/master/reviews/2023-01-ryanshea-noblecurveslibrary-securityreview.pdf).
|
|
616
|
+
The audit has been funded by Ryan Shea. Audit scope was abstract modules `curve`, `hash-to-curve`, `modular`, `poseidon`, `utils`, `weierstrass`, and top-level modules `_shortw_utils` and `secp256k1`. See [changes since audit](https://github.com/paulmillr/noble-curves/compare/0.7.3..main).
|
|
617
|
+
2. The library has been fuzzed by [Guido Vranken's cryptofuzz](https://github.com/guidovranken/cryptofuzz). You can run the fuzzer by yourself to check it.
|
|
618
|
+
3. [Timing attack](https://en.wikipedia.org/wiki/Timing_attack) considerations: _JIT-compiler_ and _Garbage Collector_ make "constant time" extremely hard to achieve in a scripting language. Which means _any other JS library can't have constant-timeness_. Even statically typed Rust, a language without GC, [makes it harder to achieve constant-time](https://www.chosenplaintext.ca/open-source/rust-timing-shield/security) for some cases. If your goal is absolute security, don't use any JS lib — including bindings to native ones. Use low-level libraries & languages. Nonetheless we're targetting algorithmic constant time.
|
|
618
619
|
|
|
619
620
|
We consider infrastructure attacks like rogue NPM modules very important; that's why it's crucial to minimize the amount of 3rd-party dependencies & native bindings. If your app uses 500 dependencies, any dep could get hacked and you'll be downloading malware with every `npm install`. Our goal is to minimize this attack vector. As for devDependencies used by the library:
|
|
620
621
|
|
|
@@ -670,12 +671,6 @@ getPublicKey x 3,363 ops/sec @ 297μs/op
|
|
|
670
671
|
sign x 1,615 ops/sec @ 619μs/op
|
|
671
672
|
verify x 319 ops/sec @ 3ms/op
|
|
672
673
|
|
|
673
|
-
stark
|
|
674
|
-
init x 35 ops/sec @ 28ms/op
|
|
675
|
-
pedersen x 884 ops/sec @ 1ms/op
|
|
676
|
-
poseidon x 8,598 ops/sec @ 116μs/op
|
|
677
|
-
verify x 528 ops/sec @ 1ms/op
|
|
678
|
-
|
|
679
674
|
ecdh
|
|
680
675
|
├─x25519 x 1,337 ops/sec @ 747μs/op
|
|
681
676
|
├─secp256k1 x 461 ops/sec @ 2ms/op
|
|
@@ -713,8 +708,6 @@ hashToCurve
|
|
|
713
708
|
|
|
714
709
|
Article about some of library's features: [Learning fast elliptic-curve cryptography](https://paulmillr.com/posts/noble-secp256k1-fast-ecc/)
|
|
715
710
|
|
|
716
|
-
Demo: Elliptic curve calculator [paulmillr.com/ecc](https://paulmillr.com/ecc).
|
|
717
|
-
|
|
718
711
|
Projects using the library:
|
|
719
712
|
|
|
720
713
|
- secp256k1
|
|
@@ -725,7 +718,9 @@ Projects using the library:
|
|
|
725
718
|
- Check out `bls12-381.ts` for articles about the curve
|
|
726
719
|
- Threshold sigs demo [genthresh.com](https://genthresh.com)
|
|
727
720
|
- BBS signatures [github.com/Wind4Greg/BBS-Draft-Checks](https://github.com/Wind4Greg/BBS-Draft-Checks) following [draft-irtf-cfrg-bbs-signatures-latest](https://identity.foundation/bbs-signature/draft-irtf-cfrg-bbs-signatures.html)
|
|
728
|
-
|
|
721
|
+
- Others
|
|
722
|
+
- All curves demo: Elliptic curve calculator [paulmillr.com/ecc](https://paulmillr.com/ecc)
|
|
723
|
+
- [micro-starknet](https://github.com/paulmillr/micro-starknet) for stark-friendly elliptic curve.
|
|
729
724
|
## Upgrading
|
|
730
725
|
|
|
731
726
|
If you're coming from single-feature noble packages, the following changes need to be kept in mind:
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
2
|
+
import { hmac } from '@noble/hashes/hmac';
|
|
3
|
+
import { concatBytes, randomBytes } from '@noble/hashes/utils';
|
|
4
|
+
import { weierstrass } from './abstract/weierstrass.js';
|
|
5
|
+
// connects noble-curves to noble-hashes
|
|
6
|
+
export function getHash(hash) {
|
|
7
|
+
return {
|
|
8
|
+
hash,
|
|
9
|
+
hmac: (key, ...msgs) => hmac(hash, key, concatBytes(...msgs)),
|
|
10
|
+
randomBytes,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export function createCurve(curveDef, defHash) {
|
|
14
|
+
const create = (hash) => weierstrass({ ...curveDef, ...getHash(hash) });
|
|
15
|
+
return Object.freeze({ ...create(defHash), create });
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=_shortw_utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_shortw_utils.js","sourceRoot":"","sources":["../src/_shortw_utils.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAAE,WAAW,EAAa,MAAM,2BAA2B,CAAC;AAGnE,wCAAwC;AACxC,MAAM,UAAU,OAAO,CAAC,IAAW;IACjC,OAAO;QACL,IAAI;QACJ,IAAI,EAAE,CAAC,GAAe,EAAE,GAAG,IAAkB,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,WAAW,CAAC,GAAG,IAAI,CAAC,CAAC;QACvF,WAAW;KACZ,CAAC;AACJ,CAAC;AAGD,MAAM,UAAU,WAAW,CAAC,QAAkB,EAAE,OAAc;IAC5D,MAAM,MAAM,GAAG,CAAC,IAAW,EAAE,EAAE,CAAC,WAAW,CAAC,EAAE,GAAG,QAAQ,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC/E,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,MAAM,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;AACvD,CAAC"}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { hashToPrivateScalar } from './modular.js';
|
|
2
|
+
import { bitLen, bitGet, ensureBytes } from './utils.js';
|
|
3
|
+
import * as htf from './hash-to-curve.js';
|
|
4
|
+
import { weierstrassPoints, } from './weierstrass.js';
|
|
5
|
+
export function bls(CURVE) {
|
|
6
|
+
// Fields looks pretty specific for curve, so for now we need to pass them with opts
|
|
7
|
+
const { Fp, Fr, Fp2, Fp6, Fp12 } = CURVE;
|
|
8
|
+
const BLS_X_LEN = bitLen(CURVE.x);
|
|
9
|
+
const groupLen = 32; // TODO: calculate; hardcoded for now
|
|
10
|
+
// Pre-compute coefficients for sparse multiplication
|
|
11
|
+
// Point addition and point double calculations is reused for coefficients
|
|
12
|
+
function calcPairingPrecomputes(p) {
|
|
13
|
+
const { x, y } = p;
|
|
14
|
+
// prettier-ignore
|
|
15
|
+
const Qx = x, Qy = y, Qz = Fp2.ONE;
|
|
16
|
+
// prettier-ignore
|
|
17
|
+
let Rx = Qx, Ry = Qy, Rz = Qz;
|
|
18
|
+
let ell_coeff = [];
|
|
19
|
+
for (let i = BLS_X_LEN - 2; i >= 0; i--) {
|
|
20
|
+
// Double
|
|
21
|
+
let t0 = Fp2.sqr(Ry); // Ry²
|
|
22
|
+
let t1 = Fp2.sqr(Rz); // Rz²
|
|
23
|
+
let t2 = Fp2.multiplyByB(Fp2.mul(t1, 3n)); // 3 * T1 * B
|
|
24
|
+
let t3 = Fp2.mul(t2, 3n); // 3 * T2
|
|
25
|
+
let t4 = Fp2.sub(Fp2.sub(Fp2.sqr(Fp2.add(Ry, Rz)), t1), t0); // (Ry + Rz)² - T1 - T0
|
|
26
|
+
ell_coeff.push([
|
|
27
|
+
Fp2.sub(t2, t0),
|
|
28
|
+
Fp2.mul(Fp2.sqr(Rx), 3n),
|
|
29
|
+
Fp2.neg(t4), // -T4
|
|
30
|
+
]);
|
|
31
|
+
Rx = Fp2.div(Fp2.mul(Fp2.mul(Fp2.sub(t0, t3), Rx), Ry), 2n); // ((T0 - T3) * Rx * Ry) / 2
|
|
32
|
+
Ry = Fp2.sub(Fp2.sqr(Fp2.div(Fp2.add(t0, t3), 2n)), Fp2.mul(Fp2.sqr(t2), 3n)); // ((T0 + T3) / 2)² - 3 * T2²
|
|
33
|
+
Rz = Fp2.mul(t0, t4); // T0 * T4
|
|
34
|
+
if (bitGet(CURVE.x, i)) {
|
|
35
|
+
// Addition
|
|
36
|
+
let t0 = Fp2.sub(Ry, Fp2.mul(Qy, Rz)); // Ry - Qy * Rz
|
|
37
|
+
let t1 = Fp2.sub(Rx, Fp2.mul(Qx, Rz)); // Rx - Qx * Rz
|
|
38
|
+
ell_coeff.push([
|
|
39
|
+
Fp2.sub(Fp2.mul(t0, Qx), Fp2.mul(t1, Qy)),
|
|
40
|
+
Fp2.neg(t0),
|
|
41
|
+
t1, // T1
|
|
42
|
+
]);
|
|
43
|
+
let t2 = Fp2.sqr(t1); // T1²
|
|
44
|
+
let t3 = Fp2.mul(t2, t1); // T2 * T1
|
|
45
|
+
let t4 = Fp2.mul(t2, Rx); // T2 * Rx
|
|
46
|
+
let t5 = Fp2.add(Fp2.sub(t3, Fp2.mul(t4, 2n)), Fp2.mul(Fp2.sqr(t0), Rz)); // T3 - 2 * T4 + T0² * Rz
|
|
47
|
+
Rx = Fp2.mul(t1, t5); // T1 * T5
|
|
48
|
+
Ry = Fp2.sub(Fp2.mul(Fp2.sub(t4, t5), t0), Fp2.mul(t3, Ry)); // (T4 - T5) * T0 - T3 * Ry
|
|
49
|
+
Rz = Fp2.mul(Rz, t3); // Rz * T3
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return ell_coeff;
|
|
53
|
+
}
|
|
54
|
+
function millerLoop(ell, g1) {
|
|
55
|
+
const { x } = CURVE;
|
|
56
|
+
const Px = g1[0];
|
|
57
|
+
const Py = g1[1];
|
|
58
|
+
let f12 = Fp12.ONE;
|
|
59
|
+
for (let j = 0, i = BLS_X_LEN - 2; i >= 0; i--, j++) {
|
|
60
|
+
const E = ell[j];
|
|
61
|
+
f12 = Fp12.multiplyBy014(f12, E[0], Fp2.mul(E[1], Px), Fp2.mul(E[2], Py));
|
|
62
|
+
if (bitGet(x, i)) {
|
|
63
|
+
j += 1;
|
|
64
|
+
const F = ell[j];
|
|
65
|
+
f12 = Fp12.multiplyBy014(f12, F[0], Fp2.mul(F[1], Px), Fp2.mul(F[2], Py));
|
|
66
|
+
}
|
|
67
|
+
if (i !== 0)
|
|
68
|
+
f12 = Fp12.sqr(f12);
|
|
69
|
+
}
|
|
70
|
+
return Fp12.conjugate(f12);
|
|
71
|
+
}
|
|
72
|
+
const utils = {
|
|
73
|
+
randomPrivateKey: () => {
|
|
74
|
+
return Fr.toBytes(hashToPrivateScalar(CURVE.randomBytes(groupLen + 8), CURVE.r));
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
// Point on G1 curve: (x, y)
|
|
78
|
+
const G1_ = weierstrassPoints({ n: Fr.ORDER, ...CURVE.G1 });
|
|
79
|
+
const G1 = Object.assign(G1_, htf.createHasher(G1_.ProjectivePoint, CURVE.G1.mapToCurve, {
|
|
80
|
+
...CURVE.htfDefaults,
|
|
81
|
+
...CURVE.G1.htfDefaults,
|
|
82
|
+
}));
|
|
83
|
+
function pairingPrecomputes(point) {
|
|
84
|
+
const p = point;
|
|
85
|
+
if (p._PPRECOMPUTES)
|
|
86
|
+
return p._PPRECOMPUTES;
|
|
87
|
+
p._PPRECOMPUTES = calcPairingPrecomputes(point.toAffine());
|
|
88
|
+
return p._PPRECOMPUTES;
|
|
89
|
+
}
|
|
90
|
+
// TODO: export
|
|
91
|
+
// function clearPairingPrecomputes(point: G2) {
|
|
92
|
+
// const p = point as G2 & withPairingPrecomputes;
|
|
93
|
+
// p._PPRECOMPUTES = undefined;
|
|
94
|
+
// }
|
|
95
|
+
// Point on G2 curve (complex numbers): (x₁, x₂+i), (y₁, y₂+i)
|
|
96
|
+
const G2_ = weierstrassPoints({ n: Fr.ORDER, ...CURVE.G2 });
|
|
97
|
+
const G2 = Object.assign(G2_, htf.createHasher(G2_.ProjectivePoint, CURVE.G2.mapToCurve, {
|
|
98
|
+
...CURVE.htfDefaults,
|
|
99
|
+
...CURVE.G2.htfDefaults,
|
|
100
|
+
}));
|
|
101
|
+
const { Signature } = CURVE.G2;
|
|
102
|
+
// Calculates bilinear pairing
|
|
103
|
+
function pairing(Q, P, withFinalExponent = true) {
|
|
104
|
+
if (Q.equals(G1.ProjectivePoint.ZERO) || P.equals(G2.ProjectivePoint.ZERO))
|
|
105
|
+
throw new Error('pairing is not available for ZERO point');
|
|
106
|
+
Q.assertValidity();
|
|
107
|
+
P.assertValidity();
|
|
108
|
+
// Performance: 9ms for millerLoop and ~14ms for exp.
|
|
109
|
+
const Qa = Q.toAffine();
|
|
110
|
+
const looped = millerLoop(pairingPrecomputes(P), [Qa.x, Qa.y]);
|
|
111
|
+
return withFinalExponent ? Fp12.finalExponentiate(looped) : looped;
|
|
112
|
+
}
|
|
113
|
+
function normP1(point) {
|
|
114
|
+
return point instanceof G1.ProjectivePoint ? point : G1.ProjectivePoint.fromHex(point);
|
|
115
|
+
}
|
|
116
|
+
function normP2(point) {
|
|
117
|
+
return point instanceof G2.ProjectivePoint ? point : Signature.decode(point);
|
|
118
|
+
}
|
|
119
|
+
function normP2Hash(point, htfOpts) {
|
|
120
|
+
return point instanceof G2.ProjectivePoint
|
|
121
|
+
? point
|
|
122
|
+
: G2.hashToCurve(ensureBytes('point', point), htfOpts);
|
|
123
|
+
}
|
|
124
|
+
// Multiplies generator by private key.
|
|
125
|
+
// P = pk x G
|
|
126
|
+
function getPublicKey(privateKey) {
|
|
127
|
+
return G1.ProjectivePoint.fromPrivateKey(privateKey).toRawBytes(true);
|
|
128
|
+
}
|
|
129
|
+
function sign(message, privateKey, htfOpts) {
|
|
130
|
+
const msgPoint = normP2Hash(message, htfOpts);
|
|
131
|
+
msgPoint.assertValidity();
|
|
132
|
+
const sigPoint = msgPoint.multiply(G1.normPrivateKeyToScalar(privateKey));
|
|
133
|
+
if (message instanceof G2.ProjectivePoint)
|
|
134
|
+
return sigPoint;
|
|
135
|
+
return Signature.encode(sigPoint);
|
|
136
|
+
}
|
|
137
|
+
// Checks if pairing of public key & hash is equal to pairing of generator & signature.
|
|
138
|
+
// e(P, H(m)) == e(G, S)
|
|
139
|
+
function verify(signature, message, publicKey, htfOpts) {
|
|
140
|
+
const P = normP1(publicKey);
|
|
141
|
+
const Hm = normP2Hash(message, htfOpts);
|
|
142
|
+
const G = G1.ProjectivePoint.BASE;
|
|
143
|
+
const S = normP2(signature);
|
|
144
|
+
// Instead of doing 2 exponentiations, we use property of billinear maps
|
|
145
|
+
// and do one exp after multiplying 2 points.
|
|
146
|
+
const ePHm = pairing(P.negate(), Hm, false);
|
|
147
|
+
const eGS = pairing(G, S, false);
|
|
148
|
+
const exp = Fp12.finalExponentiate(Fp12.mul(eGS, ePHm));
|
|
149
|
+
return Fp12.eql(exp, Fp12.ONE);
|
|
150
|
+
}
|
|
151
|
+
function aggregatePublicKeys(publicKeys) {
|
|
152
|
+
if (!publicKeys.length)
|
|
153
|
+
throw new Error('Expected non-empty array');
|
|
154
|
+
const agg = publicKeys.map(normP1).reduce((sum, p) => sum.add(p), G1.ProjectivePoint.ZERO);
|
|
155
|
+
const aggAffine = agg; //.toAffine();
|
|
156
|
+
if (publicKeys[0] instanceof G1.ProjectivePoint) {
|
|
157
|
+
aggAffine.assertValidity();
|
|
158
|
+
return aggAffine;
|
|
159
|
+
}
|
|
160
|
+
// toRawBytes ensures point validity
|
|
161
|
+
return aggAffine.toRawBytes(true);
|
|
162
|
+
}
|
|
163
|
+
function aggregateSignatures(signatures) {
|
|
164
|
+
if (!signatures.length)
|
|
165
|
+
throw new Error('Expected non-empty array');
|
|
166
|
+
const agg = signatures.map(normP2).reduce((sum, s) => sum.add(s), G2.ProjectivePoint.ZERO);
|
|
167
|
+
const aggAffine = agg; //.toAffine();
|
|
168
|
+
if (signatures[0] instanceof G2.ProjectivePoint) {
|
|
169
|
+
aggAffine.assertValidity();
|
|
170
|
+
return aggAffine;
|
|
171
|
+
}
|
|
172
|
+
return Signature.encode(aggAffine);
|
|
173
|
+
}
|
|
174
|
+
// https://ethresear.ch/t/fast-verification-of-multiple-bls-signatures/5407
|
|
175
|
+
// e(G, S) = e(G, SUM(n)(Si)) = MUL(n)(e(G, Si))
|
|
176
|
+
function verifyBatch(signature, messages, publicKeys, htfOpts) {
|
|
177
|
+
// @ts-ignore
|
|
178
|
+
// console.log('verifyBatch', bytesToHex(signature as any), messages, publicKeys.map(bytesToHex));
|
|
179
|
+
if (!messages.length)
|
|
180
|
+
throw new Error('Expected non-empty messages array');
|
|
181
|
+
if (publicKeys.length !== messages.length)
|
|
182
|
+
throw new Error('Pubkey count should equal msg count');
|
|
183
|
+
const sig = normP2(signature);
|
|
184
|
+
const nMessages = messages.map((i) => normP2Hash(i, htfOpts));
|
|
185
|
+
const nPublicKeys = publicKeys.map(normP1);
|
|
186
|
+
try {
|
|
187
|
+
const paired = [];
|
|
188
|
+
for (const message of new Set(nMessages)) {
|
|
189
|
+
const groupPublicKey = nMessages.reduce((groupPublicKey, subMessage, i) => subMessage === message ? groupPublicKey.add(nPublicKeys[i]) : groupPublicKey, G1.ProjectivePoint.ZERO);
|
|
190
|
+
// const msg = message instanceof PointG2 ? message : await PointG2.hashToCurve(message);
|
|
191
|
+
// Possible to batch pairing for same msg with different groupPublicKey here
|
|
192
|
+
paired.push(pairing(groupPublicKey, message, false));
|
|
193
|
+
}
|
|
194
|
+
paired.push(pairing(G1.ProjectivePoint.BASE.negate(), sig, false));
|
|
195
|
+
const product = paired.reduce((a, b) => Fp12.mul(a, b), Fp12.ONE);
|
|
196
|
+
const exp = Fp12.finalExponentiate(product);
|
|
197
|
+
return Fp12.eql(exp, Fp12.ONE);
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
G1.ProjectivePoint.BASE._setWindowSize(4);
|
|
204
|
+
return {
|
|
205
|
+
CURVE,
|
|
206
|
+
Fr,
|
|
207
|
+
Fp,
|
|
208
|
+
Fp2,
|
|
209
|
+
Fp6,
|
|
210
|
+
Fp12,
|
|
211
|
+
G1,
|
|
212
|
+
G2,
|
|
213
|
+
Signature,
|
|
214
|
+
millerLoop,
|
|
215
|
+
calcPairingPrecomputes,
|
|
216
|
+
pairing,
|
|
217
|
+
getPublicKey,
|
|
218
|
+
sign,
|
|
219
|
+
verify,
|
|
220
|
+
aggregatePublicKeys,
|
|
221
|
+
aggregateSignatures,
|
|
222
|
+
verifyBatch,
|
|
223
|
+
utils,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
//# sourceMappingURL=bls.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bls.js","sourceRoot":"","sources":["../../src/abstract/bls.ts"],"names":[],"mappings":"AAcA,OAAO,EAAS,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,EAAuB,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAC9E,OAAO,KAAK,GAAG,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EAIL,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAiF1B,MAAM,UAAU,GAAG,CACjB,KAAoC;IAEpC,oFAAoF;IACpF,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;IACzC,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC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CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;YAClE,MAAM,GAAG,GAAG,IAAI,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;YAC5C,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;SAChC;QAAC,MAAM;YACN,OAAO,KAAK,CAAC;SACd;IACH,CAAC;IAED,EAAE,CAAC,eAAe,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;IAE1C,OAAO;QACL,KAAK;QACL,EAAE;QACF,EAAE;QACF,GAAG;QACH,GAAG;QACH,IAAI;QACJ,EAAE;QACF,EAAE;QACF,SAAS;QACT,UAAU;QACV,sBAAsB;QACtB,OAAO;QACP,YAAY;QACZ,IAAI;QACJ,MAAM;QACN,mBAAmB;QACnB,mBAAmB;QACnB,WAAW;QACX,KAAK;KACN,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
2
|
+
// Abelian group utilities
|
|
3
|
+
import { validateField, nLength } from './modular.js';
|
|
4
|
+
import { validateObject } from './utils.js';
|
|
5
|
+
const _0n = BigInt(0);
|
|
6
|
+
const _1n = BigInt(1);
|
|
7
|
+
// Elliptic curve multiplication of Point by scalar. Fragile.
|
|
8
|
+
// Scalars should always be less than curve order: this should be checked inside of a curve itself.
|
|
9
|
+
// Creates precomputation tables for fast multiplication:
|
|
10
|
+
// - private scalar is split by fixed size windows of W bits
|
|
11
|
+
// - every window point is collected from window's table & added to accumulator
|
|
12
|
+
// - since windows are different, same point inside tables won't be accessed more than once per calc
|
|
13
|
+
// - each multiplication is 'Math.ceil(CURVE_ORDER / 𝑊) + 1' point additions (fixed for any scalar)
|
|
14
|
+
// - +1 window is neccessary for wNAF
|
|
15
|
+
// - wNAF reduces table size: 2x less memory + 2x faster generation, but 10% slower multiplication
|
|
16
|
+
// TODO: Research returning 2d JS array of windows, instead of a single window. This would allow
|
|
17
|
+
// windows to be in different memory locations
|
|
18
|
+
export function wNAF(c, bits) {
|
|
19
|
+
const constTimeNegate = (condition, item) => {
|
|
20
|
+
const neg = item.negate();
|
|
21
|
+
return condition ? neg : item;
|
|
22
|
+
};
|
|
23
|
+
const opts = (W) => {
|
|
24
|
+
const windows = Math.ceil(bits / W) + 1; // +1, because
|
|
25
|
+
const windowSize = 2 ** (W - 1); // -1 because we skip zero
|
|
26
|
+
return { windows, windowSize };
|
|
27
|
+
};
|
|
28
|
+
return {
|
|
29
|
+
constTimeNegate,
|
|
30
|
+
// non-const time multiplication ladder
|
|
31
|
+
unsafeLadder(elm, n) {
|
|
32
|
+
let p = c.ZERO;
|
|
33
|
+
let d = elm;
|
|
34
|
+
while (n > _0n) {
|
|
35
|
+
if (n & _1n)
|
|
36
|
+
p = p.add(d);
|
|
37
|
+
d = d.double();
|
|
38
|
+
n >>= _1n;
|
|
39
|
+
}
|
|
40
|
+
return p;
|
|
41
|
+
},
|
|
42
|
+
/**
|
|
43
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
44
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
45
|
+
* Number of precomputed points depends on the curve size:
|
|
46
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
47
|
+
* - 𝑊 is the window size
|
|
48
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
49
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
50
|
+
* @returns precomputed point tables flattened to a single array
|
|
51
|
+
*/
|
|
52
|
+
precomputeWindow(elm, W) {
|
|
53
|
+
const { windows, windowSize } = opts(W);
|
|
54
|
+
const points = [];
|
|
55
|
+
let p = elm;
|
|
56
|
+
let base = p;
|
|
57
|
+
for (let window = 0; window < windows; window++) {
|
|
58
|
+
base = p;
|
|
59
|
+
points.push(base);
|
|
60
|
+
// =1, because we skip zero
|
|
61
|
+
for (let i = 1; i < windowSize; i++) {
|
|
62
|
+
base = base.add(p);
|
|
63
|
+
points.push(base);
|
|
64
|
+
}
|
|
65
|
+
p = base.double();
|
|
66
|
+
}
|
|
67
|
+
return points;
|
|
68
|
+
},
|
|
69
|
+
/**
|
|
70
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
71
|
+
* @param W window size
|
|
72
|
+
* @param precomputes precomputed tables
|
|
73
|
+
* @param n scalar (we don't check here, but should be less than curve order)
|
|
74
|
+
* @returns real and fake (for const-time) points
|
|
75
|
+
*/
|
|
76
|
+
wNAF(W, precomputes, n) {
|
|
77
|
+
// TODO: maybe check that scalar is less than group order? wNAF behavious is undefined otherwise
|
|
78
|
+
// But need to carefully remove other checks before wNAF. ORDER == bits here
|
|
79
|
+
const { windows, windowSize } = opts(W);
|
|
80
|
+
let p = c.ZERO;
|
|
81
|
+
let f = c.BASE;
|
|
82
|
+
const mask = BigInt(2 ** W - 1); // Create mask with W ones: 0b1111 for W=4 etc.
|
|
83
|
+
const maxNumber = 2 ** W;
|
|
84
|
+
const shiftBy = BigInt(W);
|
|
85
|
+
for (let window = 0; window < windows; window++) {
|
|
86
|
+
const offset = window * windowSize;
|
|
87
|
+
// Extract W bits.
|
|
88
|
+
let wbits = Number(n & mask);
|
|
89
|
+
// Shift number by W bits.
|
|
90
|
+
n >>= shiftBy;
|
|
91
|
+
// If the bits are bigger than max size, we'll split those.
|
|
92
|
+
// +224 => 256 - 32
|
|
93
|
+
if (wbits > windowSize) {
|
|
94
|
+
wbits -= maxNumber;
|
|
95
|
+
n += _1n;
|
|
96
|
+
}
|
|
97
|
+
// This code was first written with assumption that 'f' and 'p' will never be infinity point:
|
|
98
|
+
// since each addition is multiplied by 2 ** W, it cannot cancel each other. However,
|
|
99
|
+
// there is negate now: it is possible that negated element from low value
|
|
100
|
+
// would be the same as high element, which will create carry into next window.
|
|
101
|
+
// It's not obvious how this can fail, but still worth investigating later.
|
|
102
|
+
// Check if we're onto Zero point.
|
|
103
|
+
// Add random point inside current window to f.
|
|
104
|
+
const offset1 = offset;
|
|
105
|
+
const offset2 = offset + Math.abs(wbits) - 1; // -1 because we skip zero
|
|
106
|
+
const cond1 = window % 2 !== 0;
|
|
107
|
+
const cond2 = wbits < 0;
|
|
108
|
+
if (wbits === 0) {
|
|
109
|
+
// The most important part for const-time getPublicKey
|
|
110
|
+
f = f.add(constTimeNegate(cond1, precomputes[offset1]));
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
p = p.add(constTimeNegate(cond2, precomputes[offset2]));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// JIT-compiler should not eliminate f here, since it will later be used in normalizeZ()
|
|
117
|
+
// Even if the variable is still unused, there are some checks which will
|
|
118
|
+
// throw an exception, so compiler needs to prove they won't happen, which is hard.
|
|
119
|
+
// At this point there is a way to F be infinity-point even if p is not,
|
|
120
|
+
// which makes it less const-time: around 1 bigint multiply.
|
|
121
|
+
return { p, f };
|
|
122
|
+
},
|
|
123
|
+
wNAFCached(P, precomputesMap, n, transform) {
|
|
124
|
+
// @ts-ignore
|
|
125
|
+
const W = P._WINDOW_SIZE || 1;
|
|
126
|
+
// Calculate precomputes on a first run, reuse them after
|
|
127
|
+
let comp = precomputesMap.get(P);
|
|
128
|
+
if (!comp) {
|
|
129
|
+
comp = this.precomputeWindow(P, W);
|
|
130
|
+
if (W !== 1) {
|
|
131
|
+
precomputesMap.set(P, transform(comp));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return this.wNAF(W, comp, n);
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
export function validateBasic(curve) {
|
|
139
|
+
validateField(curve.Fp);
|
|
140
|
+
validateObject(curve, {
|
|
141
|
+
n: 'bigint',
|
|
142
|
+
h: 'bigint',
|
|
143
|
+
Gx: 'field',
|
|
144
|
+
Gy: 'field',
|
|
145
|
+
}, {
|
|
146
|
+
nBitLength: 'isSafeInteger',
|
|
147
|
+
nByteLength: 'isSafeInteger',
|
|
148
|
+
});
|
|
149
|
+
// Set defaults
|
|
150
|
+
return Object.freeze({ ...nLength(curve.n, curve.nBitLength), ...curve });
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=curve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"curve.js","sourceRoot":"","sources":["../../src/abstract/curve.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,0BAA0B;AAC1B,OAAO,EAAS,aAAa,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,MAAM,GAAG,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;AACtB,MAAM,GAAG,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;AAsBtB,6DAA6D;AAC7D,mGAAmG;AACnG,yDAAyD;AACzD,4DAA4D;AAC5D,+EAA+E;AAC/E,oGAAoG;AACpG,oGAAoG;AACpG,qCAAqC;AACrC,kGAAkG;AAClG,gGAAgG;AAChG,8CAA8C;AAC9C,MAAM,UAAU,IAAI,CAAqB,CAAsB,EAAE,IAAY;IAC3E,MAAM,eAAe,GAAG,CAAC,SAAkB,EAAE,IAAO,EAAK,EAAE;QACzD,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;QAC1B,OAAO,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC;IAChC,CAAC,CAAC;IACF,MAAM,IAAI,GAAG,CAAC,CAAS,EAAE,EAAE;QACzB,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,cAAc;QACvD,MAAM,UAAU,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,0BAA0B;QAC3D,OAAO,EA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