@decryption/curves 0.2.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/LICENSE +21 -0
- package/README.md +75 -0
- package/abstract/bls.d.ts +410 -0
- package/abstract/bls.js +433 -0
- package/abstract/curve.d.ts +441 -0
- package/abstract/curve.js +622 -0
- package/abstract/edwards.d.ts +319 -0
- package/abstract/edwards.js +697 -0
- package/abstract/fft.d.ts +438 -0
- package/abstract/fft.js +580 -0
- package/abstract/frost.d.ts +292 -0
- package/abstract/frost.js +705 -0
- package/abstract/hash-to-curve.d.ts +238 -0
- package/abstract/hash-to-curve.js +353 -0
- package/abstract/modular.d.ts +550 -0
- package/abstract/modular.js +872 -0
- package/abstract/montgomery.d.ts +95 -0
- package/abstract/montgomery.js +180 -0
- package/abstract/oprf.d.ts +354 -0
- package/abstract/oprf.js +358 -0
- package/abstract/poseidon.d.ts +198 -0
- package/abstract/poseidon.js +471 -0
- package/abstract/tower.d.ts +213 -0
- package/abstract/tower.js +963 -0
- package/abstract/weierstrass.d.ts +587 -0
- package/abstract/weierstrass.js +1421 -0
- package/bls12-381.d.ts +23 -0
- package/bls12-381.js +680 -0
- package/bn254.d.ts +74 -0
- package/bn254.js +248 -0
- package/ed25519.d.ts +211 -0
- package/ed25519.js +626 -0
- package/ed448.d.ts +184 -0
- package/ed448.js +596 -0
- package/esm/abstract/bls.js +429 -0
- package/esm/abstract/curve.js +610 -0
- package/esm/abstract/edwards.js +691 -0
- package/esm/abstract/fft.js +567 -0
- package/esm/abstract/frost.js +702 -0
- package/esm/abstract/hash-to-curve.js +345 -0
- package/esm/abstract/modular.js +849 -0
- package/esm/abstract/montgomery.js +177 -0
- package/esm/abstract/oprf.js +355 -0
- package/esm/abstract/poseidon.js +462 -0
- package/esm/abstract/tower.js +925 -0
- package/esm/abstract/weierstrass.js +1411 -0
- package/esm/bls12-381.js +677 -0
- package/esm/bn254.js +244 -0
- package/esm/ed25519.js +622 -0
- package/esm/ed448.js +593 -0
- package/esm/index.js +38 -0
- package/esm/misc.js +211 -0
- package/esm/nist.js +266 -0
- package/esm/secp256k1.js +465 -0
- package/esm/utils.js +597 -0
- package/esm/webcrypto.js +362 -0
- package/index.d.ts +0 -0
- package/index.js +38 -0
- package/misc.d.ts +113 -0
- package/misc.js +216 -0
- package/nist.d.ts +136 -0
- package/nist.js +269 -0
- package/package.json +46 -0
- package/secp256k1.d.ts +152 -0
- package/secp256k1.js +468 -0
- package/utils.d.ts +535 -0
- package/utils.js +626 -0
- package/webcrypto.d.ts +240 -0
- package/webcrypto.js +365 -0
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BLS != BLS.
|
|
3
|
+
* The file implements BLS (Boneh-Lynn-Shacham) signatures.
|
|
4
|
+
* Used in both BLS (Barreto-Lynn-Scott) and BN (Barreto-Naehrig)
|
|
5
|
+
* families of pairing-friendly curves.
|
|
6
|
+
* Consists of two curves: G1 and G2:
|
|
7
|
+
* - G1 is a subgroup of (x, y) E(Fq) over y² = x³ + 4.
|
|
8
|
+
* - G2 is a subgroup of ((x₁, x₂+i), (y₁, y₂+i)) E(Fq²) over y² = x³ + 4(1 + i) where i is √-1
|
|
9
|
+
* - Gt, created by bilinear (ate) pairing e(G1, G2), consists of p-th roots of unity in
|
|
10
|
+
* Fq^k where k is embedding degree. Only degree 12 is currently supported, 24 is not.
|
|
11
|
+
* Pairing is used to aggregate and verify signatures.
|
|
12
|
+
* There are two modes of operation:
|
|
13
|
+
* - Long signatures: X-byte keys + 2X-byte sigs (G1 keys + G2 sigs).
|
|
14
|
+
* - Short signatures: 2X-byte keys + X-byte sigs (G2 keys + G1 sigs).
|
|
15
|
+
* @module
|
|
16
|
+
**/
|
|
17
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
18
|
+
import { abytes, notImplemented, randomBytes } from '../utils';
|
|
19
|
+
import { createHasher, } from './hash-to-curve';
|
|
20
|
+
import { getMinHashLength, mapHashToField } from './modular';
|
|
21
|
+
// prettier-ignore
|
|
22
|
+
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
|
|
23
|
+
// Signed non-adjacent decomposition of the spec-defined Miller-loop parameter.
|
|
24
|
+
// BN254 benefits most because `6x+2` has multiple adjacent `11` runs, but BLS12-381's
|
|
25
|
+
// stored `|x|` still starts with `11`, so the Miller loop must also handle one `-1` digit there.
|
|
26
|
+
function NAfDecomposition(a) {
|
|
27
|
+
const res = [];
|
|
28
|
+
// a>1 because of marker bit
|
|
29
|
+
for (; a > _1n; a >>= _1n) {
|
|
30
|
+
if ((a & _1n) === _0n)
|
|
31
|
+
res.unshift(0);
|
|
32
|
+
else if ((a & _3n) === _3n) {
|
|
33
|
+
res.unshift(-1);
|
|
34
|
+
a += _1n;
|
|
35
|
+
}
|
|
36
|
+
else
|
|
37
|
+
res.unshift(1);
|
|
38
|
+
}
|
|
39
|
+
return res;
|
|
40
|
+
}
|
|
41
|
+
function aNonEmpty(arr) {
|
|
42
|
+
// Aggregate helpers use this to reject empty variable-length inputs consistently.
|
|
43
|
+
// Without the guard, each caller would fall through into a different empty-input / identity
|
|
44
|
+
// case and hide missing inputs behind outputs that still look structurally valid.
|
|
45
|
+
if (!Array.isArray(arr) || arr.length === 0)
|
|
46
|
+
throw new Error('expected non-empty array');
|
|
47
|
+
}
|
|
48
|
+
// This should be enough for bn254, no need to export full stuff?
|
|
49
|
+
function createBlsPairing(fields, G1, G2, params) {
|
|
50
|
+
const { Fr, Fp2, Fp12 } = fields;
|
|
51
|
+
const { twistType, ateLoopSize, xNegative, postPrecompute } = params;
|
|
52
|
+
// Applies sparse multiplication as line function
|
|
53
|
+
let lineFunction;
|
|
54
|
+
if (twistType === 'multiplicative') {
|
|
55
|
+
lineFunction = (c0, c1, c2, f, Px, Py) => Fp12.mul014(f, c0, Fp2.mul(c1, Px), Fp2.mul(c2, Py));
|
|
56
|
+
}
|
|
57
|
+
else if (twistType === 'divisive') {
|
|
58
|
+
// NOTE: it should be [c0, c1, c2], but we use different order here to reduce complexity of
|
|
59
|
+
// precompute calculations.
|
|
60
|
+
lineFunction = (c0, c1, c2, f, Px, Py) => Fp12.mul034(f, Fp2.mul(c2, Py), Fp2.mul(c1, Px), c0);
|
|
61
|
+
}
|
|
62
|
+
else
|
|
63
|
+
throw new Error('bls: unknown twist type');
|
|
64
|
+
const Fp2div2 = Fp2.div(Fp2.ONE, Fp2.mul(Fp2.ONE, _2n));
|
|
65
|
+
function pointDouble(ell, Rx, Ry, Rz) {
|
|
66
|
+
const t0 = Fp2.sqr(Ry); // Ry²
|
|
67
|
+
const t1 = Fp2.sqr(Rz); // Rz²
|
|
68
|
+
const t2 = Fp2.mulByB(Fp2.mul(t1, _3n)); // 3 * T1 * B
|
|
69
|
+
const t3 = Fp2.mul(t2, _3n); // 3 * T2
|
|
70
|
+
const t4 = Fp2.sub(Fp2.sub(Fp2.sqr(Fp2.add(Ry, Rz)), t1), t0); // (Ry + Rz)² - T1 - T0
|
|
71
|
+
const c0 = Fp2.sub(t2, t0); // T2 - T0 (i)
|
|
72
|
+
const c1 = Fp2.mul(Fp2.sqr(Rx), _3n); // 3 * Rx²
|
|
73
|
+
const c2 = Fp2.neg(t4); // -T4 (-h)
|
|
74
|
+
ell.push([c0, c1, c2]);
|
|
75
|
+
Rx = Fp2.mul(Fp2.mul(Fp2.mul(Fp2.sub(t0, t3), Rx), Ry), Fp2div2); // ((T0 - T3) * Rx * Ry) / 2
|
|
76
|
+
// ((T0 + T3) / 2)² - 3 * T2²
|
|
77
|
+
Ry = Fp2.sub(Fp2.sqr(Fp2.mul(Fp2.add(t0, t3), Fp2div2)), Fp2.mul(Fp2.sqr(t2), _3n));
|
|
78
|
+
Rz = Fp2.mul(t0, t4); // T0 * T4
|
|
79
|
+
return { Rx, Ry, Rz };
|
|
80
|
+
}
|
|
81
|
+
function pointAdd(ell, Rx, Ry, Rz, Qx, Qy) {
|
|
82
|
+
// Addition
|
|
83
|
+
const t0 = Fp2.sub(Ry, Fp2.mul(Qy, Rz)); // Ry - Qy * Rz
|
|
84
|
+
const t1 = Fp2.sub(Rx, Fp2.mul(Qx, Rz)); // Rx - Qx * Rz
|
|
85
|
+
const c0 = Fp2.sub(Fp2.mul(t0, Qx), Fp2.mul(t1, Qy)); // T0 * Qx - T1 * Qy == Ry * Qx - Rx * Qy
|
|
86
|
+
const c1 = Fp2.neg(t0); // -T0 == Qy * Rz - Ry
|
|
87
|
+
const c2 = t1; // == Rx - Qx * Rz
|
|
88
|
+
ell.push([c0, c1, c2]);
|
|
89
|
+
const t2 = Fp2.sqr(t1); // T1²
|
|
90
|
+
const t3 = Fp2.mul(t2, t1); // T2 * T1
|
|
91
|
+
const t4 = Fp2.mul(t2, Rx); // T2 * Rx
|
|
92
|
+
// T3 - 2 * T4 + T0² * Rz
|
|
93
|
+
const t5 = Fp2.add(Fp2.sub(t3, Fp2.mul(t4, _2n)), Fp2.mul(Fp2.sqr(t0), Rz));
|
|
94
|
+
Rx = Fp2.mul(t1, t5); // T1 * T5
|
|
95
|
+
Ry = Fp2.sub(Fp2.mul(Fp2.sub(t4, t5), t0), Fp2.mul(t3, Ry)); // (T4 - T5) * T0 - T3 * Ry
|
|
96
|
+
Rz = Fp2.mul(Rz, t3); // Rz * T3
|
|
97
|
+
return { Rx, Ry, Rz };
|
|
98
|
+
}
|
|
99
|
+
// Pre-compute coefficients for sparse multiplication
|
|
100
|
+
// Point addition and point double calculations is reused for coefficients
|
|
101
|
+
// pointAdd happens only if bit set, so wNAF is reasonable. Unfortunately we cannot combine
|
|
102
|
+
// add + double in windowed precomputes here, otherwise it would be single op (since X is static)
|
|
103
|
+
const ATE_NAF = NAfDecomposition(ateLoopSize);
|
|
104
|
+
const calcPairingPrecomputes = (point) => {
|
|
105
|
+
const p = point;
|
|
106
|
+
const { x, y } = p.toAffine();
|
|
107
|
+
// prettier-ignore
|
|
108
|
+
const Qx = x, Qy = y, negQy = Fp2.neg(y);
|
|
109
|
+
// prettier-ignore
|
|
110
|
+
let Rx = Qx, Ry = Qy, Rz = Fp2.ONE;
|
|
111
|
+
const ell = [];
|
|
112
|
+
for (const bit of ATE_NAF) {
|
|
113
|
+
const cur = [];
|
|
114
|
+
({ Rx, Ry, Rz } = pointDouble(cur, Rx, Ry, Rz));
|
|
115
|
+
if (bit)
|
|
116
|
+
({ Rx, Ry, Rz } = pointAdd(cur, Rx, Ry, Rz, Qx, bit === -1 ? negQy : Qy));
|
|
117
|
+
ell.push(cur);
|
|
118
|
+
}
|
|
119
|
+
if (postPrecompute) {
|
|
120
|
+
const last = ell[ell.length - 1];
|
|
121
|
+
postPrecompute(Rx, Ry, Rz, Qx, Qy, pointAdd.bind(null, last));
|
|
122
|
+
}
|
|
123
|
+
return ell;
|
|
124
|
+
};
|
|
125
|
+
function millerLoopBatch(pairs, withFinalExponent = false) {
|
|
126
|
+
let f12 = Fp12.ONE;
|
|
127
|
+
if (pairs.length) {
|
|
128
|
+
const ellLen = pairs[0][0].length;
|
|
129
|
+
for (let i = 0; i < ellLen; i++) {
|
|
130
|
+
f12 = Fp12.sqr(f12); // This allows us to do sqr only one time for all pairings
|
|
131
|
+
// NOTE: we apply multiple pairings in parallel here
|
|
132
|
+
for (const [ell, Px, Py] of pairs) {
|
|
133
|
+
for (const [c0, c1, c2] of ell[i])
|
|
134
|
+
f12 = lineFunction(c0, c1, c2, f12, Px, Py);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (xNegative)
|
|
139
|
+
f12 = Fp12.conjugate(f12);
|
|
140
|
+
return withFinalExponent ? Fp12.finalExponentiate(f12) : f12;
|
|
141
|
+
}
|
|
142
|
+
// Calculates product of multiple pairings
|
|
143
|
+
// This up to x2 faster than just `map(({g1, g2})=>pairing({g1,g2}))`
|
|
144
|
+
function pairingBatch(pairs, withFinalExponent = true) {
|
|
145
|
+
const res = [];
|
|
146
|
+
for (const { g1, g2 } of pairs) {
|
|
147
|
+
// Mathematically, a zero pairing term contributes GT.ONE. We still reject it here because
|
|
148
|
+
// this API mainly backs BLS verification, where ZERO inputs usually mean broken hash /
|
|
149
|
+
// wiring. Silently skipping them would turn those failures into a neutral pairing product.
|
|
150
|
+
// Callers that want the algebraic neutral-element behavior can filter ZERO terms first.
|
|
151
|
+
if (g1.is0() || g2.is0())
|
|
152
|
+
throw new Error('pairing is not available for ZERO point');
|
|
153
|
+
// This uses toAffine inside
|
|
154
|
+
g1.assertValidity();
|
|
155
|
+
g2.assertValidity();
|
|
156
|
+
const Qa = g1.toAffine();
|
|
157
|
+
res.push([calcPairingPrecomputes(g2), Qa.x, Qa.y]);
|
|
158
|
+
}
|
|
159
|
+
return millerLoopBatch(res, withFinalExponent);
|
|
160
|
+
}
|
|
161
|
+
// Calculates bilinear pairing
|
|
162
|
+
function pairing(Q, P, withFinalExponent = true) {
|
|
163
|
+
return pairingBatch([{ g1: Q, g2: P }], withFinalExponent);
|
|
164
|
+
}
|
|
165
|
+
const lengths = {
|
|
166
|
+
seed: getMinHashLength(Fr.ORDER),
|
|
167
|
+
};
|
|
168
|
+
const rand = params.randomBytes === undefined ? randomBytes : params.randomBytes;
|
|
169
|
+
// Seeded calls deterministically reduce exactly `lengths.seed` bytes into `1..Fr.ORDER-1`;
|
|
170
|
+
// omitting `seed` just fills that input buffer from the configured RNG first.
|
|
171
|
+
const randomSecretKey = (seed) => {
|
|
172
|
+
seed = seed === undefined ? rand(lengths.seed) : seed;
|
|
173
|
+
abytes(seed, lengths.seed, 'seed');
|
|
174
|
+
return mapHashToField(seed, Fr.ORDER);
|
|
175
|
+
};
|
|
176
|
+
Object.freeze(lengths);
|
|
177
|
+
return {
|
|
178
|
+
lengths,
|
|
179
|
+
Fr,
|
|
180
|
+
Fp12, // NOTE: we re-export Fp12 here because pairing results are Fp12!
|
|
181
|
+
millerLoopBatch,
|
|
182
|
+
pairing,
|
|
183
|
+
pairingBatch,
|
|
184
|
+
calcPairingPrecomputes,
|
|
185
|
+
randomSecretKey,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function createBlsSig(blsPairing, PubPoint, SigPoint, isSigG1, hashToSigCurve, SignatureCoder) {
|
|
189
|
+
const { Fr, Fp12, pairingBatch, randomSecretKey, lengths } = blsPairing;
|
|
190
|
+
if (!SignatureCoder) {
|
|
191
|
+
SignatureCoder = {
|
|
192
|
+
fromBytes: notImplemented,
|
|
193
|
+
fromHex: notImplemented,
|
|
194
|
+
toBytes: notImplemented,
|
|
195
|
+
toHex: notImplemented,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
function normPub(point) {
|
|
199
|
+
return point instanceof PubPoint ? point : PubPoint.fromBytes(point);
|
|
200
|
+
}
|
|
201
|
+
function normSig(point) {
|
|
202
|
+
return point instanceof SigPoint ? point : SigPoint.fromBytes(point);
|
|
203
|
+
}
|
|
204
|
+
// Sign/verify here take points already hashed onto the signature subgroup.
|
|
205
|
+
// Raw bytes and points from the other subgroup must fail this constructor-brand
|
|
206
|
+
// check before later validity checks run.
|
|
207
|
+
function amsg(m) {
|
|
208
|
+
if (!(m instanceof SigPoint))
|
|
209
|
+
throw new Error(`expected valid message hashed to ${!isSigG1 ? 'G2' : 'G1'} curve`);
|
|
210
|
+
return m;
|
|
211
|
+
}
|
|
212
|
+
// What matters here is what point pairing API accepts as G1 or G2, not actual size or names
|
|
213
|
+
const pair = !isSigG1
|
|
214
|
+
? (a, b) => ({ g1: a, g2: b })
|
|
215
|
+
: (a, b) => ({ g1: b, g2: a });
|
|
216
|
+
return Object.freeze({
|
|
217
|
+
lengths: Object.freeze({ ...lengths, secretKey: Fr.BYTES }),
|
|
218
|
+
keygen(seed) {
|
|
219
|
+
const secretKey = randomSecretKey(seed);
|
|
220
|
+
const publicKey = this.getPublicKey(secretKey);
|
|
221
|
+
return { secretKey, publicKey };
|
|
222
|
+
},
|
|
223
|
+
// P = pk x G
|
|
224
|
+
getPublicKey(secretKey) {
|
|
225
|
+
let sec;
|
|
226
|
+
try {
|
|
227
|
+
sec = PubPoint.Fn.fromBytes(secretKey);
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
// @ts-ignore
|
|
231
|
+
throw new Error('invalid private key: ' + typeof secretKey, { cause: error });
|
|
232
|
+
}
|
|
233
|
+
return PubPoint.BASE.multiply(sec);
|
|
234
|
+
},
|
|
235
|
+
// S = pk x H(m)
|
|
236
|
+
sign(message, secretKey, unusedArg) {
|
|
237
|
+
if (unusedArg != null)
|
|
238
|
+
throw new Error('sign() expects 2 arguments');
|
|
239
|
+
const sec = PubPoint.Fn.fromBytes(secretKey);
|
|
240
|
+
amsg(message).assertValidity();
|
|
241
|
+
return message.multiply(sec);
|
|
242
|
+
},
|
|
243
|
+
// Checks if pairing of public key & hash is equal to pairing of generator & signature.
|
|
244
|
+
// e(P, H(m)) == e(G, S)
|
|
245
|
+
// e(S, G) == e(H(m), P)
|
|
246
|
+
verify(signature, message, publicKey, unusedArg) {
|
|
247
|
+
if (unusedArg != null)
|
|
248
|
+
throw new Error('verify() expects 3 arguments');
|
|
249
|
+
signature = normSig(signature);
|
|
250
|
+
publicKey = normPub(publicKey);
|
|
251
|
+
const P = publicKey.negate();
|
|
252
|
+
const G = PubPoint.BASE;
|
|
253
|
+
const Hm = amsg(message);
|
|
254
|
+
const S = signature;
|
|
255
|
+
// This code was changed in 1.9.x:
|
|
256
|
+
// Before it was G.negate() in G2, now it's always pubKey.negate
|
|
257
|
+
// e(P, -Q)===e(-P, Q)==e(P, Q)^-1. Negate can be done anywhere (as long it is done once per pair).
|
|
258
|
+
// We just moving sign, but since pairing is multiplicative, we doing X * X^-1 = 1
|
|
259
|
+
try {
|
|
260
|
+
const exp = pairingBatch([pair(P, Hm), pair(G, S)]);
|
|
261
|
+
return Fp12.eql(exp, Fp12.ONE);
|
|
262
|
+
}
|
|
263
|
+
catch {
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
// https://ethresear.ch/t/fast-verification-of-multiple-bls-signatures/5407
|
|
268
|
+
// e(G, S) = e(G, SUM(n)(Si)) = MUL(n)(e(G, Si))
|
|
269
|
+
// TODO: maybe `{message: G2Hex, publicKey: G1Hex}[]` instead?
|
|
270
|
+
verifyBatch(signature, items) {
|
|
271
|
+
aNonEmpty(items);
|
|
272
|
+
const sig = normSig(signature);
|
|
273
|
+
const nMessages = items.map((i) => i.message);
|
|
274
|
+
const nPublicKeys = items.map((i) => normPub(i.publicKey));
|
|
275
|
+
// NOTE: this works only for exact same object
|
|
276
|
+
const messagePubKeyMap = new Map();
|
|
277
|
+
for (let i = 0; i < nPublicKeys.length; i++) {
|
|
278
|
+
const pub = nPublicKeys[i];
|
|
279
|
+
const msg = nMessages[i];
|
|
280
|
+
let keys = messagePubKeyMap.get(msg);
|
|
281
|
+
if (keys === undefined) {
|
|
282
|
+
keys = [];
|
|
283
|
+
messagePubKeyMap.set(msg, keys);
|
|
284
|
+
}
|
|
285
|
+
keys.push(pub);
|
|
286
|
+
}
|
|
287
|
+
const paired = [];
|
|
288
|
+
const G = PubPoint.BASE;
|
|
289
|
+
try {
|
|
290
|
+
for (const [msg, keys] of messagePubKeyMap) {
|
|
291
|
+
const groupPublicKey = keys.reduce((acc, msg) => acc.add(msg));
|
|
292
|
+
paired.push(pair(groupPublicKey, msg));
|
|
293
|
+
}
|
|
294
|
+
paired.push(pair(G.negate(), sig));
|
|
295
|
+
return Fp12.eql(pairingBatch(paired), Fp12.ONE);
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
// Adds a bunch of public key points together.
|
|
302
|
+
// pk1 + pk2 + pk3 = pkA
|
|
303
|
+
aggregatePublicKeys(publicKeys) {
|
|
304
|
+
aNonEmpty(publicKeys);
|
|
305
|
+
publicKeys = publicKeys.map((pub) => normPub(pub));
|
|
306
|
+
const agg = publicKeys.reduce((sum, p) => sum.add(p), PubPoint.ZERO);
|
|
307
|
+
agg.assertValidity();
|
|
308
|
+
return agg;
|
|
309
|
+
},
|
|
310
|
+
// Adds a bunch of signature points together.
|
|
311
|
+
// pk1 + pk2 + pk3 = pkA
|
|
312
|
+
aggregateSignatures(signatures) {
|
|
313
|
+
aNonEmpty(signatures);
|
|
314
|
+
signatures = signatures.map((sig) => normSig(sig));
|
|
315
|
+
const agg = signatures.reduce((sum, s) => sum.add(s), SigPoint.ZERO);
|
|
316
|
+
agg.assertValidity();
|
|
317
|
+
return agg;
|
|
318
|
+
},
|
|
319
|
+
hash(messageBytes, DST) {
|
|
320
|
+
abytes(messageBytes);
|
|
321
|
+
const opts = DST ? { DST } : undefined;
|
|
322
|
+
return hashToSigCurve(messageBytes, opts);
|
|
323
|
+
},
|
|
324
|
+
Signature: Object.freeze({ ...SignatureCoder }),
|
|
325
|
+
}) /*satisfies Signer */;
|
|
326
|
+
}
|
|
327
|
+
// NOTE: separate function instead of function override, so we don't depend on hasher in bn254.
|
|
328
|
+
/**
|
|
329
|
+
* @param fields - Tower field implementations.
|
|
330
|
+
* @param G1_Point - G1 point constructor.
|
|
331
|
+
* @param G2_Point - G2 point constructor.
|
|
332
|
+
* @param params - Pairing parameters. See {@link BlsPairingParams}.
|
|
333
|
+
* @returns Pairing-only BLS helpers. The returned pairing surface rejects infinity inputs, while
|
|
334
|
+
* empty `pairingBatch(...)` calls return the multiplicative identity in GT. This keeps the
|
|
335
|
+
* low-level pairing API fail-closed for BLS-style callers, where identity points usually signal
|
|
336
|
+
* broken hash / wiring instead of an intentionally neutral pairing term. This also eagerly
|
|
337
|
+
* precomputes the G1 base-point table as a performance side effect.
|
|
338
|
+
* @throws If the pairing parameters or underlying curve helpers are inconsistent. {@link Error}
|
|
339
|
+
* @example
|
|
340
|
+
* ```ts
|
|
341
|
+
* import { blsBasic } from './bls';
|
|
342
|
+
* import { bn254 } from '../bn254';
|
|
343
|
+
* // Pair a G1 point with a G2 point without the higher-level signer helpers.
|
|
344
|
+
* const gt = bn254.pairing(bn254.G1.Point.BASE, bn254.G2.Point.BASE);
|
|
345
|
+
* ```
|
|
346
|
+
*/
|
|
347
|
+
export function blsBasic(fields, G1_Point, G2_Point, params) {
|
|
348
|
+
// Fields are specific for curve, so for now we'll need to pass them with opts
|
|
349
|
+
const { Fp, Fr, Fp2, Fp6, Fp12 } = fields;
|
|
350
|
+
// Point on G1 curve: (x, y)
|
|
351
|
+
// const G1_Point = weierstrass(CURVE.G1, { Fn: Fr });
|
|
352
|
+
const G1 = { Point: G1_Point };
|
|
353
|
+
// Point on G2 curve (complex numbers): (x₁, x₂+i), (y₁, y₂+i)
|
|
354
|
+
const G2 = { Point: G2_Point };
|
|
355
|
+
const pairingRes = createBlsPairing(fields, G1_Point, G2_Point, params);
|
|
356
|
+
const { millerLoopBatch, pairing, pairingBatch, calcPairingPrecomputes, randomSecretKey, lengths, } = pairingRes;
|
|
357
|
+
G1.Point.BASE.precompute(4);
|
|
358
|
+
Object.freeze(G1);
|
|
359
|
+
Object.freeze(G2);
|
|
360
|
+
return Object.freeze({
|
|
361
|
+
lengths: Object.freeze(lengths),
|
|
362
|
+
millerLoopBatch,
|
|
363
|
+
pairing,
|
|
364
|
+
pairingBatch,
|
|
365
|
+
G1,
|
|
366
|
+
G2,
|
|
367
|
+
fields: Object.freeze({ Fr, Fp, Fp2, Fp6, Fp12 }),
|
|
368
|
+
params: Object.freeze({
|
|
369
|
+
ateLoopSize: params.ateLoopSize,
|
|
370
|
+
twistType: params.twistType,
|
|
371
|
+
}),
|
|
372
|
+
utils: Object.freeze({
|
|
373
|
+
randomSecretKey,
|
|
374
|
+
calcPairingPrecomputes,
|
|
375
|
+
}),
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
// We can export this too, but seems there is not much reasons for now? If user wants hasher, they can just create hasher.
|
|
379
|
+
function blsHashers(fields, G1_Point, G2_Point, params, hasherParams) {
|
|
380
|
+
const base = blsBasic(fields, G1_Point, G2_Point, params);
|
|
381
|
+
// Missing map hooks intentionally fail closed via notImplemented on first hash use.
|
|
382
|
+
const G1Hasher = createHasher(G1_Point, hasherParams.mapToG1 === undefined ? notImplemented : hasherParams.mapToG1, {
|
|
383
|
+
...hasherParams.hasherOpts,
|
|
384
|
+
...hasherParams.hasherOptsG1,
|
|
385
|
+
});
|
|
386
|
+
const G2Hasher = createHasher(G2_Point, hasherParams.mapToG2 === undefined ? notImplemented : hasherParams.mapToG2, {
|
|
387
|
+
...hasherParams.hasherOpts,
|
|
388
|
+
...hasherParams.hasherOptsG2,
|
|
389
|
+
});
|
|
390
|
+
return Object.freeze({ ...base, G1: G1Hasher, G2: G2Hasher });
|
|
391
|
+
}
|
|
392
|
+
// G1_Point: ProjConstructor<bigint>, G2_Point: ProjConstructor<Fp2>,
|
|
393
|
+
// Rename to blsSignatures?
|
|
394
|
+
/**
|
|
395
|
+
* @param fields - Tower field implementations.
|
|
396
|
+
* @param G1_Point - G1 point constructor.
|
|
397
|
+
* @param G2_Point - G2 point constructor.
|
|
398
|
+
* @param params - Pairing parameters. See {@link BlsPairingParams}.
|
|
399
|
+
* @param hasherParams - Hash-to-curve configuration. See {@link BlsHasherParams}.
|
|
400
|
+
* @param signatureCoders - Signature codecs.
|
|
401
|
+
* @returns BLS helpers with signers. The inherited pairing surface still rejects infinity inputs,
|
|
402
|
+
* and empty `pairingBatch(...)` calls still return the multiplicative identity in GT. Aggregate
|
|
403
|
+
* verification still requires proof of possession or another rogue-key defense from the caller.
|
|
404
|
+
* @throws If the pairing, hashing, or signature helpers are configured inconsistently. {@link Error}
|
|
405
|
+
* @example
|
|
406
|
+
* ```ts
|
|
407
|
+
* import { bls } from './bls';
|
|
408
|
+
* import { bls12_381 } from '../bls12-381';
|
|
409
|
+
* const sigs = bls12_381.longSignatures;
|
|
410
|
+
* // Use the full BLS helper set when you need hashing, keygen, signing, and verification.
|
|
411
|
+
* const { secretKey, publicKey } = sigs.keygen();
|
|
412
|
+
* const msg = sigs.hash(new TextEncoder().encode('hello noble'));
|
|
413
|
+
* const sig = sigs.sign(msg, secretKey);
|
|
414
|
+
* const isValid = sigs.verify(sig, msg, publicKey);
|
|
415
|
+
* ```
|
|
416
|
+
*/
|
|
417
|
+
export function bls(fields, G1_Point, G2_Point, params, hasherParams, signatureCoders) {
|
|
418
|
+
const base = blsHashers(fields, G1_Point, G2_Point, params, hasherParams);
|
|
419
|
+
const pairingRes = {
|
|
420
|
+
...base,
|
|
421
|
+
Fr: base.fields.Fr,
|
|
422
|
+
Fp12: base.fields.Fp12,
|
|
423
|
+
calcPairingPrecomputes: base.utils.calcPairingPrecomputes,
|
|
424
|
+
randomSecretKey: base.utils.randomSecretKey,
|
|
425
|
+
};
|
|
426
|
+
const longSignatures = createBlsSig(pairingRes, G1_Point, G2_Point, false, base.G2.hashToCurve, signatureCoders?.LongSignature);
|
|
427
|
+
const shortSignatures = createBlsSig(pairingRes, G2_Point, G1_Point, true, base.G1.hashToCurve, signatureCoders?.ShortSignature);
|
|
428
|
+
return Object.freeze({ ...base, longSignatures, shortSignatures });
|
|
429
|
+
}
|