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