@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,587 @@
1
+ import { type CHash, type HmacFn, type TArg, type TRet } from '../utils';
2
+ import { type AffinePoint, type CurveLengths, type CurvePoint, type CurvePointCons } from './curve';
3
+ import { type IField } from './modular';
4
+ /** Shared affine point shape used by Weierstrass helpers. */
5
+ export type { AffinePoint };
6
+ type EndoBasis = [[bigint, bigint], [bigint, bigint]];
7
+ /**
8
+ * When Weierstrass curve has `a=0`, it becomes Koblitz curve.
9
+ * Koblitz curves allow using **efficiently-computable GLV endomorphism ψ**.
10
+ * Endomorphism uses 2x less RAM, speeds up precomputation by 2x and ECDH / key recovery by 20%.
11
+ * For precomputed wNAF it trades off 1/2 init time & 1/3 ram for 20% perf hit.
12
+ *
13
+ * Endomorphism consists of beta, lambda and splitScalar:
14
+ *
15
+ * 1. GLV endomorphism ψ transforms a point: `P = (x, y) ↦ ψ(P) = (β·x mod p, y)`
16
+ * 2. GLV scalar decomposition transforms a scalar: `k ≡ k₁ + k₂·λ (mod n)`
17
+ * 3. Then these are combined: `k·P = k₁·P + k₂·ψ(P)`
18
+ * 4. Two 128-bit point-by-scalar multiplications + one point addition is faster than
19
+ * one 256-bit multiplication.
20
+ *
21
+ * where
22
+ * * beta: β ∈ Fₚ with β³ = 1, β ≠ 1
23
+ * * lambda: λ ∈ Fₙ with λ³ = 1, λ ≠ 1
24
+ * * splitScalar decomposes k ↦ k₁, k₂, by using reduced basis vectors.
25
+ * Gauss lattice reduction calculates them from initial basis vectors `(n, 0), (-λ, 0)`
26
+ *
27
+ * Check out `test/misc/endomorphism.js` and
28
+ * {@link https://gist.github.com/paulmillr/eb670806793e84df628a7c434a873066 | this endomorphism gist}.
29
+ */
30
+ export type EndomorphismOpts = {
31
+ /** Cube root of unity used by the GLV endomorphism. */
32
+ beta: bigint;
33
+ /** Reduced lattice basis used for scalar splitting. */
34
+ basises?: EndoBasis;
35
+ /**
36
+ * Optional custom scalar-splitting helper.
37
+ * Receives one scalar and returns two half-sized scalar components.
38
+ */
39
+ splitScalar?: (k: bigint) => {
40
+ k1neg: boolean;
41
+ k1: bigint;
42
+ k2neg: boolean;
43
+ k2: bigint;
44
+ };
45
+ };
46
+ /** Two half-sized scalar components returned by endomorphism splitting. */
47
+ export type ScalarEndoParts = {
48
+ /** Whether the first split scalar should be negated. */
49
+ k1neg: boolean;
50
+ /** Absolute value of the first split scalar. */
51
+ k1: bigint;
52
+ /** Whether the second split scalar should be negated. */
53
+ k2neg: boolean;
54
+ /** Absolute value of the second split scalar. */
55
+ k2: bigint;
56
+ };
57
+ /** Splits scalar for GLV endomorphism. */
58
+ export declare function _splitEndoScalar(k: bigint, basis: EndoBasis, n: bigint): ScalarEndoParts;
59
+ /**
60
+ * Option to enable hedged signatures with improved security.
61
+ *
62
+ * * Randomly generated k is bad, because broken CSPRNG would leak private keys.
63
+ * * Deterministic k (RFC6979) is better; but is suspectible to fault attacks.
64
+ *
65
+ * We allow using technique described in RFC6979 3.6: additional k', a.k.a. adding randomness
66
+ * to deterministic sig. If CSPRNG is broken & randomness is weak, it would STILL be as secure
67
+ * as ordinary sig without ExtraEntropy.
68
+ *
69
+ * * `true` means "fetch data, from CSPRNG, incorporate it into k generation"
70
+ * * `false` means "disable extra entropy, use purely deterministic k"
71
+ * * `Uint8Array` passed means "incorporate following data into k generation"
72
+ *
73
+ * See {@link https://paulmillr.com/posts/deterministic-signatures/ | deterministic signatures}.
74
+ */
75
+ export type ECDSAExtraEntropy = boolean | Uint8Array;
76
+ /**
77
+ * - `compact` is the default format
78
+ * - `recovered` is the same as compact, but with an extra byte indicating recovery byte
79
+ * - `der` is ASN.1 DER encoding
80
+ */
81
+ export type ECDSASignatureFormat = 'compact' | 'recovered' | 'der';
82
+ /**
83
+ * - `prehash`: (default: true) indicates whether to do sha256(message).
84
+ * When a custom hash is used, it must be set to `false`.
85
+ */
86
+ export type ECDSARecoverOpts = {
87
+ /** Whether to hash the message before signature recovery. */
88
+ prehash?: boolean;
89
+ };
90
+ /**
91
+ * - `prehash`: (default: true) indicates whether to do sha256(message).
92
+ * When a custom hash is used, it must be set to `false`.
93
+ * - `lowS`: (default: true) prohibits signatures with `sig.s >= CURVE.n/2n`.
94
+ * Compatible with BTC/ETH. Setting `lowS: false` allows to create malleable signatures,
95
+ * which is default openssl behavior.
96
+ * Non-malleable signatures can still be successfully verified in openssl.
97
+ * - `format`: (default: 'compact') 'compact' or 'recovered' with recovery byte
98
+ */
99
+ export type ECDSAVerifyOpts = {
100
+ /** Whether to hash the message before verification. */
101
+ prehash?: boolean;
102
+ /** Whether to reject high-S signatures. */
103
+ lowS?: boolean;
104
+ /** Signature encoding to accept. */
105
+ format?: ECDSASignatureFormat;
106
+ };
107
+ /**
108
+ * - `prehash`: (default: true) indicates whether to do sha256(message).
109
+ * When a custom hash is used, it must be set to `false`.
110
+ * - `lowS`: (default: true) prohibits signatures with `sig.s >= CURVE.n/2n`.
111
+ * Compatible with BTC/ETH. Setting `lowS: false` allows to create malleable signatures,
112
+ * which is default openssl behavior.
113
+ * Non-malleable signatures can still be successfully verified in openssl.
114
+ * - `format`: (default: 'compact') 'compact' or 'recovered' with recovery byte
115
+ * - `extraEntropy`: (default: false) creates signatures with increased
116
+ * security, see {@link ECDSAExtraEntropy}
117
+ */
118
+ export type ECDSASignOpts = {
119
+ /** Whether to hash the message before signing. */
120
+ prehash?: boolean;
121
+ /** Whether to normalize signatures into the low-S half-order. */
122
+ lowS?: boolean;
123
+ /** Signature encoding to produce. */
124
+ format?: ECDSASignatureFormat;
125
+ /** Optional hedging input for deterministic k generation. */
126
+ extraEntropy?: ECDSAExtraEntropy;
127
+ };
128
+ /** Projective XYZ point used by short Weierstrass curves. */
129
+ export interface WeierstrassPoint<T> extends CurvePoint<T, WeierstrassPoint<T>> {
130
+ /** projective X coordinate. Different from affine x. */
131
+ readonly X: T;
132
+ /** projective Y coordinate. Different from affine y. */
133
+ readonly Y: T;
134
+ /** projective z coordinate */
135
+ readonly Z: T;
136
+ /** affine x coordinate. Different from projective X. */
137
+ get x(): T;
138
+ /** affine y coordinate. Different from projective Y. */
139
+ get y(): T;
140
+ /**
141
+ * Encode the point into compressed or uncompressed SEC1 bytes.
142
+ * @param isCompressed - Whether to use the compressed form.
143
+ * @returns Encoded point bytes.
144
+ */
145
+ toBytes(isCompressed?: boolean): TRet<Uint8Array>;
146
+ /**
147
+ * Encode the point into compressed or uncompressed SEC1 hex.
148
+ * @param isCompressed - Whether to use the compressed form.
149
+ * @returns Encoded point hex.
150
+ */
151
+ toHex(isCompressed?: boolean): string;
152
+ }
153
+ /** Constructor and metadata helpers for Weierstrass points. */
154
+ export interface WeierstrassPointCons<T> extends CurvePointCons<WeierstrassPoint<T>> {
155
+ /** Does NOT validate if the point is valid. Use `.assertValidity()`. */
156
+ new (X: T, Y: T, Z: T): WeierstrassPoint<T>;
157
+ /**
158
+ * Return the curve parameters captured by this point constructor.
159
+ * @returns Curve parameters.
160
+ */
161
+ CURVE(): WeierstrassOpts<T>;
162
+ }
163
+ /**
164
+ * Weierstrass curve options.
165
+ *
166
+ * * p: prime characteristic (order) of finite field, in which arithmetics is done
167
+ * * n: order of prime subgroup a.k.a total amount of valid curve points
168
+ * * h: cofactor, usually 1. h*n is group order; n is subgroup order
169
+ * * a: formula param, must be in field of p
170
+ * * b: formula param, must be in field of p
171
+ * * Gx: x coordinate of generator point a.k.a. base point
172
+ * * Gy: y coordinate of generator point
173
+ */
174
+ export type WeierstrassOpts<T> = Readonly<{
175
+ /** Base-field modulus. */
176
+ p: bigint;
177
+ /** Prime subgroup order. */
178
+ n: bigint;
179
+ /** Curve cofactor. */
180
+ h: bigint;
181
+ /** Weierstrass curve parameter `a`. */
182
+ a: T;
183
+ /** Weierstrass curve parameter `b`. */
184
+ b: T;
185
+ /** Generator x coordinate. */
186
+ Gx: T;
187
+ /** Generator y coordinate. */
188
+ Gy: T;
189
+ }>;
190
+ /**
191
+ * Optional helpers and overrides for a Weierstrass point constructor.
192
+ *
193
+ * When a cofactor != 1, there can be effective methods to:
194
+ * 1. Determine whether a point is torsion-free
195
+ * 2. Clear torsion component
196
+ */
197
+ export type WeierstrassExtraOpts<T> = Partial<{
198
+ /** Optional base-field override. */
199
+ Fp: IField<T>;
200
+ /** Optional scalar-field override. */
201
+ Fn: IField<bigint>;
202
+ /** Whether the point constructor accepts infinity points. */
203
+ allowInfinityPoint: boolean;
204
+ /** Optional GLV endomorphism data. */
205
+ endo: EndomorphismOpts;
206
+ /** Optional torsion-check override. */
207
+ isTorsionFree: (c: WeierstrassPointCons<T>, point: WeierstrassPoint<T>) => boolean;
208
+ /** Optional cofactor-clearing override. */
209
+ clearCofactor: (c: WeierstrassPointCons<T>, point: WeierstrassPoint<T>) => WeierstrassPoint<T>;
210
+ /** Optional custom point decoder. */
211
+ fromBytes: (bytes: TArg<Uint8Array>) => AffinePoint<T>;
212
+ /** Optional custom point encoder. */
213
+ toBytes: (c: WeierstrassPointCons<T>, point: WeierstrassPoint<T>, isCompressed: boolean) => TRet<Uint8Array>;
214
+ }>;
215
+ /**
216
+ * Options for ECDSA signatures over a Weierstrass curve.
217
+ *
218
+ * * lowS: (default: true) whether produced or verified signatures occupy the
219
+ * low half of `ecdsaOpts.n`. Prevents malleability.
220
+ * * hmac: (default: noble-hashes hmac) function, would be used to init hmac-drbg for k generation.
221
+ * * randomBytes: (default: webcrypto os-level CSPRNG) custom method for fetching secure randomness.
222
+ * * bits2int, bits2int_modN: used in sigs, sometimes overridden by curves. Custom hooks are
223
+ * treated as pure functions over validated bytes and MUST NOT mutate caller-owned buffers or
224
+ * closure-captured option bags. `bits2int_modN` must also return a canonical scalar in
225
+ * `[0..Point.Fn.ORDER-1]`.
226
+ */
227
+ export type ECDSAOpts = Partial<{
228
+ /** Default low-S policy for this ECDSA instance. */
229
+ lowS: boolean;
230
+ /** HMAC implementation used by RFC6979 DRBG. */
231
+ hmac: HmacFn;
232
+ /** RNG override used by helper constructors. */
233
+ randomBytes: (bytesLength?: number) => TRet<Uint8Array>;
234
+ /** Hash-to-integer conversion override. */
235
+ bits2int: (bytes: TArg<Uint8Array>) => bigint;
236
+ /** Hash-to-integer-mod-n conversion override. Returns a canonical scalar in `[0..Fn.ORDER-1]`. */
237
+ bits2int_modN: (bytes: TArg<Uint8Array>) => bigint;
238
+ }>;
239
+ /** Elliptic Curve Diffie-Hellman helper namespace. */
240
+ export interface ECDH {
241
+ /**
242
+ * Generate a secret/public key pair.
243
+ * @param seed - Optional seed material.
244
+ * @returns Secret/public key pair.
245
+ */
246
+ keygen: (seed?: TArg<Uint8Array>) => {
247
+ secretKey: TRet<Uint8Array>;
248
+ publicKey: TRet<Uint8Array>;
249
+ };
250
+ /**
251
+ * Derive the public key from a secret key.
252
+ * @param secretKey - Secret key bytes.
253
+ * @param isCompressed - Whether to emit compressed SEC1 bytes.
254
+ * @returns Encoded public key.
255
+ */
256
+ getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
257
+ /**
258
+ * Compute the shared secret point from a secret key and peer public key.
259
+ * @param secretKeyA - Local secret key bytes.
260
+ * @param publicKeyB - Peer public key bytes.
261
+ * @param isCompressed - Whether to emit compressed SEC1 bytes.
262
+ * @returns Encoded shared point.
263
+ */
264
+ getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
265
+ /** Point constructor used by this ECDH instance. */
266
+ Point: WeierstrassPointCons<bigint>;
267
+ /** Validation and random-key helpers. */
268
+ utils: {
269
+ /** Check whether a secret key has the expected encoding. */
270
+ isValidSecretKey: (secretKey: TArg<Uint8Array>) => boolean;
271
+ /** Check whether a public key decodes to a valid point. */
272
+ isValidPublicKey: (publicKey: TArg<Uint8Array>, isCompressed?: boolean) => boolean;
273
+ /** Generate a valid random secret key. */
274
+ randomSecretKey: (seed?: TArg<Uint8Array>) => TRet<Uint8Array>;
275
+ };
276
+ /** Byte lengths for keys and signatures exposed by this curve. */
277
+ lengths: CurveLengths;
278
+ }
279
+ /**
280
+ * ECDSA interface.
281
+ * Only supported for prime fields, not Fp2 (extension fields).
282
+ */
283
+ export interface ECDSA extends ECDH {
284
+ /**
285
+ * Sign a message with the given secret key.
286
+ * @param message - Message bytes.
287
+ * @param secretKey - Secret key bytes.
288
+ * @param opts - Optional signing tweaks. See {@link ECDSASignOpts}.
289
+ * @returns Encoded signature bytes.
290
+ */
291
+ sign: (message: TArg<Uint8Array>, secretKey: TArg<Uint8Array>, opts?: TArg<ECDSASignOpts>) => TRet<Uint8Array>;
292
+ /**
293
+ * Verify a signature against a message and public key.
294
+ * @param signature - Encoded signature bytes.
295
+ * @param message - Message bytes.
296
+ * @param publicKey - Encoded public key.
297
+ * @param opts - Optional verification tweaks. See {@link ECDSAVerifyOpts}.
298
+ * @returns Whether the signature is valid.
299
+ */
300
+ verify: (signature: TArg<Uint8Array>, message: TArg<Uint8Array>, publicKey: TArg<Uint8Array>, opts?: TArg<ECDSAVerifyOpts>) => boolean;
301
+ /**
302
+ * Recover the public key encoded into a recoverable signature.
303
+ * @param signature - Recoverable signature bytes.
304
+ * @param message - Message bytes.
305
+ * @param opts - Optional recovery tweaks. See {@link ECDSARecoverOpts}.
306
+ * @returns Encoded recovered public key.
307
+ */
308
+ recoverPublicKey(signature: TArg<Uint8Array>, message: TArg<Uint8Array>, opts?: TArg<ECDSARecoverOpts>): TRet<Uint8Array>;
309
+ /** Signature constructor and parser helpers. */
310
+ Signature: ECDSASignatureCons;
311
+ }
312
+ /**
313
+ * @param m - Error message.
314
+ * @example
315
+ * Throw a DER-specific error when signature parsing encounters invalid bytes.
316
+ *
317
+ * ```ts
318
+ * new DERErr('bad der');
319
+ * ```
320
+ */
321
+ export declare class DERErr extends Error {
322
+ constructor(m?: string);
323
+ }
324
+ /** DER helper namespace used by ECDSA signature parsing and encoding. */
325
+ export type IDER = {
326
+ /**
327
+ * DER-specific error constructor.
328
+ * @param m - Error message.
329
+ * @returns DER-specific error instance.
330
+ */
331
+ Err: typeof DERErr;
332
+ /** Low-level tag-length-value helpers used by DER encoders. */
333
+ _tlv: {
334
+ /**
335
+ * Encode one TLV record.
336
+ * @param tag - ASN.1 tag byte.
337
+ * @param data - Hex-encoded value payload.
338
+ * @returns Encoded TLV string.
339
+ */
340
+ encode: (tag: number, data: string) => string;
341
+ /**
342
+ * Decode one TLV record and return the value plus leftover bytes.
343
+ * @param tag - Expected ASN.1 tag byte.
344
+ * @param data - Remaining DER bytes.
345
+ * @returns Parsed value plus leftover bytes.
346
+ */
347
+ decode(tag: number, data: TArg<Uint8Array>): TRet<{
348
+ v: Uint8Array;
349
+ l: Uint8Array;
350
+ }>;
351
+ };
352
+ /** Positive-integer DER helpers used by ECDSA signature encoding. */
353
+ _int: {
354
+ /**
355
+ * Encode one positive bigint as a DER INTEGER.
356
+ * @param num - Positive integer to encode.
357
+ * @returns Encoded DER INTEGER.
358
+ */
359
+ encode(num: bigint): string;
360
+ /**
361
+ * Decode one DER INTEGER into a bigint.
362
+ * @param data - DER INTEGER bytes.
363
+ * @returns Decoded bigint.
364
+ */
365
+ decode(data: TArg<Uint8Array>): bigint;
366
+ };
367
+ /**
368
+ * Parse a DER signature into `{ r, s }`.
369
+ * @param bytes - DER signature bytes.
370
+ * @returns Parsed signature components.
371
+ */
372
+ toSig(bytes: TArg<Uint8Array>): {
373
+ r: bigint;
374
+ s: bigint;
375
+ };
376
+ /**
377
+ * Encode `{ r, s }` as a DER signature.
378
+ * @param sig - Signature components.
379
+ * @returns DER-encoded signature hex.
380
+ */
381
+ hexFromSig(sig: {
382
+ r: bigint;
383
+ s: bigint;
384
+ }): string;
385
+ };
386
+ /**
387
+ * ASN.1 DER encoding utilities. ASN is very complex & fragile. Format:
388
+ *
389
+ * [0x30 (SEQUENCE), bytelength, 0x02 (INTEGER), intLength, R, 0x02 (INTEGER), intLength, S]
390
+ *
391
+ * Docs: {@link https://letsencrypt.org/docs/a-warm-welcome-to-asn1-and-der/ | Let's Encrypt ASN.1 guide} and
392
+ * {@link https://luca.ntop.org/Teaching/Appunti/asn1.html | Luca Deri's ASN.1 notes}.
393
+ * @example
394
+ * ASN.1 DER encoding utilities.
395
+ *
396
+ * ```ts
397
+ * const der = DER.hexFromSig({ r: 1n, s: 2n });
398
+ * ```
399
+ */
400
+ export declare const DER: IDER;
401
+ /**
402
+ * Creates weierstrass Point constructor, based on specified curve options.
403
+ *
404
+ * See {@link WeierstrassOpts}.
405
+ * @param params - Curve parameters. See {@link WeierstrassOpts}.
406
+ * @param extraOpts - Optional helpers and overrides. See {@link WeierstrassExtraOpts}.
407
+ * @returns Weierstrass point constructor.
408
+ * @throws If the curve parameters, overrides, or point codecs are invalid. {@link Error}
409
+ *
410
+ * @example
411
+ * Construct a point type from explicit Weierstrass curve parameters.
412
+ *
413
+ * ```js
414
+ * const opts = {
415
+ * p: 0xfffffffffffffffffffffffffffffffeffffac73n,
416
+ * n: 0x100000000000000000001b8fa16dfab9aca16b6b3n,
417
+ * h: 1n,
418
+ * a: 0n,
419
+ * b: 7n,
420
+ * Gx: 0x3b4c382ce37aa192a4019e763036f4f5dd4d7ebbn,
421
+ * Gy: 0x938cf935318fdced6bc28286531733c3f03c4feen,
422
+ * };
423
+ * const secp160k1_Point = weierstrass(opts);
424
+ * ```
425
+ */
426
+ export declare function weierstrass<T>(params: WeierstrassOpts<T>, extraOpts?: WeierstrassExtraOpts<T>): WeierstrassPointCons<T>;
427
+ /** Parsed ECDSA signature with helpers for recovery and re-encoding. */
428
+ export interface ECDSASignature {
429
+ /** Signature component `r`. */
430
+ readonly r: bigint;
431
+ /** Signature component `s`. */
432
+ readonly s: bigint;
433
+ /** Optional recovery bit for recoverable signatures. */
434
+ readonly recovery?: number;
435
+ /**
436
+ * Return a copy of the signature with a recovery bit attached.
437
+ * @param recovery - Recovery bit to attach.
438
+ * @returns Signature with an attached recovery bit.
439
+ */
440
+ addRecoveryBit(recovery: number): ECDSASignature & {
441
+ readonly recovery: number;
442
+ };
443
+ /**
444
+ * Check whether the signature uses the high-S half-order.
445
+ * @returns Whether the signature uses the high-S half-order.
446
+ */
447
+ hasHighS(): boolean;
448
+ /**
449
+ * Recover the public key from the hashed message and recovery bit.
450
+ * @param messageHash - Hashed message bytes.
451
+ * @returns Recovered public-key point.
452
+ */
453
+ recoverPublicKey(messageHash: TArg<Uint8Array>): WeierstrassPoint<bigint>;
454
+ /**
455
+ * Encode the signature into bytes.
456
+ * @param format - Signature encoding to produce.
457
+ * @returns Encoded signature bytes.
458
+ */
459
+ toBytes(format?: string): TRet<Uint8Array>;
460
+ /**
461
+ * Encode the signature into hex.
462
+ * @param format - Signature encoding to produce.
463
+ * @returns Encoded signature hex.
464
+ */
465
+ toHex(format?: string): string;
466
+ }
467
+ /** Constructor and decoding helpers for ECDSA signatures. */
468
+ export type ECDSASignatureCons = {
469
+ /** Create a signature from `r`, `s`, and an optional recovery bit. */
470
+ new (r: bigint, s: bigint, recovery?: number): ECDSASignature;
471
+ /**
472
+ * Decode a signature from bytes.
473
+ * @param bytes - Encoded signature bytes.
474
+ * @param format - Signature encoding to parse.
475
+ * @returns Parsed signature.
476
+ */
477
+ fromBytes(bytes: TArg<Uint8Array>, format?: ECDSASignatureFormat): ECDSASignature;
478
+ /**
479
+ * Decode a signature from hex.
480
+ * @param hex - Encoded signature hex.
481
+ * @param format - Signature encoding to parse.
482
+ * @returns Parsed signature.
483
+ */
484
+ fromHex(hex: string, format?: ECDSASignatureFormat): ECDSASignature;
485
+ };
486
+ /**
487
+ * Implementation of the Shallue and van de Woestijne method for any weierstrass curve.
488
+ * TODO: check if there is a way to merge this with uvRatio in Edwards; move to modular.
489
+ * b = True and y = sqrt(u / v) if (u / v) is square in F, and
490
+ * b = False and y = sqrt(Z * (u / v)) otherwise.
491
+ * RFC 9380 expects callers to provide `v != 0`; this helper does not enforce it.
492
+ * @param Fp - Field implementation.
493
+ * @param Z - Simplified SWU map parameter.
494
+ * @returns Square-root ratio helper.
495
+ * @example
496
+ * Build the square-root ratio helper used by SWU map implementations.
497
+ *
498
+ * ```ts
499
+ * import { SWUFpSqrtRatio } from './weierstrass';
500
+ * import { Field } from './modular';
501
+ * const Fp = Field(17n);
502
+ * const sqrtRatio = SWUFpSqrtRatio(Fp, 3n);
503
+ * const out = sqrtRatio(4n, 1n);
504
+ * ```
505
+ */
506
+ export declare function SWUFpSqrtRatio<T>(Fp: TArg<IField<T>>, Z: T): (u: T, v: T) => {
507
+ isValid: boolean;
508
+ value: T;
509
+ };
510
+ /**
511
+ * Simplified Shallue-van de Woestijne-Ulas Method
512
+ * See {@link https://www.rfc-editor.org/rfc/rfc9380#section-6.6.2 | RFC 9380 section 6.6.2}.
513
+ * @param Fp - Field implementation.
514
+ * @param opts - SWU parameters:
515
+ * - `A`: Curve parameter `A`.
516
+ * - `B`: Curve parameter `B`.
517
+ * - `Z`: Simplified SWU map parameter.
518
+ * @returns Deterministic map-to-curve function.
519
+ * @throws If the SWU parameters are invalid or the field lacks the required helpers. {@link Error}
520
+ * @example
521
+ * Map one field element to a Weierstrass curve point with the SWU recipe.
522
+ *
523
+ * ```ts
524
+ * import { mapToCurveSimpleSWU } from './weierstrass';
525
+ * import { Field } from './modular';
526
+ * const Fp = Field(17n);
527
+ * const map = mapToCurveSimpleSWU(Fp, { A: 1n, B: 2n, Z: 3n });
528
+ * const point = map(5n);
529
+ * ```
530
+ */
531
+ export declare function mapToCurveSimpleSWU<T>(Fp: TArg<IField<T>>, opts: {
532
+ A: T;
533
+ B: T;
534
+ Z: T;
535
+ }): (u: T) => {
536
+ x: T;
537
+ y: T;
538
+ };
539
+ /**
540
+ * Sometimes users only need getPublicKey, getSharedSecret, and secret key handling.
541
+ * This helper ensures no signature functionality is present. Less code, smaller bundle size.
542
+ * @param Point - Weierstrass point constructor.
543
+ * @param ecdhOpts - Optional randomness helpers:
544
+ * - `randomBytes` (optional): Optional RNG override.
545
+ * @returns ECDH helper namespace.
546
+ * @example
547
+ * Sometimes users only need getPublicKey, getSharedSecret, and secret key handling.
548
+ *
549
+ * ```ts
550
+ * import { ecdh } from './weierstrass';
551
+ * import { p256 } from '../nist';
552
+ * const dh = ecdh(p256.Point);
553
+ * const alice = dh.keygen();
554
+ * const shared = dh.getSharedSecret(alice.secretKey, alice.publicKey);
555
+ * ```
556
+ */
557
+ export declare function ecdh(Point: WeierstrassPointCons<bigint>, ecdhOpts?: TArg<{
558
+ randomBytes?: (bytesLength?: number) => TRet<Uint8Array>;
559
+ }>): ECDH;
560
+ /**
561
+ * Creates ECDSA signing interface for given elliptic curve `Point` and `hash` function.
562
+ *
563
+ * @param Point - created using {@link weierstrass} function
564
+ * @param hash - used for 1) message prehash-ing 2) k generation in `sign`, using hmac_drbg(hash)
565
+ * @param ecdsaOpts - rarely needed, see {@link ECDSAOpts}:
566
+ * - `lowS`: Default low-S policy.
567
+ * - `hmac`: HMAC implementation used by RFC6979 DRBG.
568
+ * - `randomBytes`: Optional RNG override.
569
+ * - `bits2int`: Optional hash-to-int conversion override.
570
+ * - `bits2int_modN`: Optional hash-to-int-mod-n conversion override.
571
+ *
572
+ * @returns ECDSA helper namespace.
573
+ * @example
574
+ * Create an ECDSA signer/verifier bundle for one curve implementation.
575
+ *
576
+ * ```ts
577
+ * import { ecdsa } from './weierstrass';
578
+ * import { p256 } from '../nist';
579
+ * import { sha256 } from '@decryption/hashes/sha2';
580
+ * const p256ecdsa = ecdsa(p256.Point, sha256);
581
+ * const { secretKey, publicKey } = p256ecdsa.keygen();
582
+ * const msg = new TextEncoder().encode('hello noble');
583
+ * const sig = p256ecdsa.sign(msg, secretKey);
584
+ * const isValid = p256ecdsa.verify(sig, msg, publicKey);
585
+ * ```
586
+ */
587
+ export declare function ecdsa(Point: WeierstrassPointCons<bigint>, hash: TArg<CHash>, ecdsaOpts?: TArg<ECDSAOpts>): ECDSA;