@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,319 @@
1
+ /**
2
+ * Twisted Edwards curve. The formula is: ax² + y² = 1 + dx²y².
3
+ * For design rationale of types / exports, see weierstrass module documentation.
4
+ * Untwisted Edwards curves exist, but they aren't used in real-world protocols.
5
+ * @module
6
+ */
7
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
8
+ import { type FHash, type TArg, type TRet } from '../utils';
9
+ import { type AffinePoint, type CurveLengths, type CurvePoint, type CurvePointCons } from './curve';
10
+ import { type IField } from './modular';
11
+ /** Extended Edwards point with X/Y/Z/T coordinates. */
12
+ export interface EdwardsPoint extends CurvePoint<bigint, EdwardsPoint> {
13
+ /** extended X coordinate. Different from affine x. */
14
+ readonly X: bigint;
15
+ /** extended Y coordinate. Different from affine y. */
16
+ readonly Y: bigint;
17
+ /** extended Z coordinate */
18
+ readonly Z: bigint;
19
+ /** extended T coordinate */
20
+ readonly T: bigint;
21
+ }
22
+ /** Constructor and decoding helpers for extended Edwards points. */
23
+ export interface EdwardsPointCons extends CurvePointCons<EdwardsPoint> {
24
+ /** Create a point from extended X/Y/Z/T coordinates without validation. */
25
+ new (X: bigint, Y: bigint, Z: bigint, T: bigint): EdwardsPoint;
26
+ /**
27
+ * Return the curve parameters used by this point constructor.
28
+ * @returns Curve parameters.
29
+ */
30
+ CURVE(): EdwardsOpts;
31
+ /**
32
+ * Decode a point from bytes, optionally using ZIP-215 rules.
33
+ * @param bytes - Encoded point bytes.
34
+ * @param zip215 - Whether to accept ZIP-215 encodings.
35
+ * @returns Decoded Edwards point.
36
+ */
37
+ fromBytes(bytes: Uint8Array, zip215?: boolean): EdwardsPoint;
38
+ /**
39
+ * Decode a point from hex, optionally using ZIP-215 rules.
40
+ * @param hex - Encoded point hex.
41
+ * @param zip215 - Whether to accept ZIP-215 encodings.
42
+ * @returns Decoded Edwards point.
43
+ */
44
+ fromHex(hex: string, zip215?: boolean): EdwardsPoint;
45
+ }
46
+ /**
47
+ * Twisted Edwards curve options.
48
+ *
49
+ * * a: formula param
50
+ * * d: formula param
51
+ * * p: prime characteristic (order) of finite field, in which arithmetics is done
52
+ * * n: order of prime subgroup a.k.a total amount of valid curve points
53
+ * * h: cofactor. h*n is group order; n is subgroup order
54
+ * * Gx: x coordinate of generator point a.k.a. base point
55
+ * * Gy: y coordinate of generator point
56
+ */
57
+ export type EdwardsOpts = Readonly<{
58
+ /** Base-field modulus. */
59
+ p: bigint;
60
+ /** Prime subgroup order. */
61
+ n: bigint;
62
+ /** Curve cofactor. */
63
+ h: bigint;
64
+ /** Edwards curve parameter `a`. */
65
+ a: bigint;
66
+ /** Edwards curve parameter `d`. */
67
+ d: bigint;
68
+ /** Generator x coordinate. */
69
+ Gx: bigint;
70
+ /** Generator y coordinate. */
71
+ Gy: bigint;
72
+ }>;
73
+ /**
74
+ * Extra curve options for Twisted Edwards.
75
+ *
76
+ * * Fp: redefined Field over curve.p
77
+ * * Fn: redefined Field over curve.n
78
+ * * uvRatio: helper function for decompression, calculating √(u/v)
79
+ */
80
+ export type EdwardsExtraOpts = Partial<{
81
+ /** Optional base-field override. */
82
+ Fp: IField<bigint>;
83
+ /** Optional scalar-field override. */
84
+ Fn: IField<bigint>;
85
+ /** Whether field encodings are little-endian. */
86
+ FpFnLE: boolean;
87
+ /** Square-root ratio helper used during point decompression. */
88
+ uvRatio: (u: bigint, v: bigint) => {
89
+ isValid: boolean;
90
+ value: bigint;
91
+ };
92
+ }>;
93
+ /**
94
+ * EdDSA (Edwards Digital Signature algorithm) options.
95
+ *
96
+ * * hash: hash function used to hash secret keys and messages
97
+ * * adjustScalarBytes: clears bits to get valid field element
98
+ * * domain: Used for hashing
99
+ * * mapToCurve: for hash-to-curve standard
100
+ * * prehash: RFC 8032 pre-hashing of messages to sign() / verify()
101
+ * * randomBytes: function generating random bytes, used for randomSecretKey
102
+ */
103
+ export type EdDSAOpts = Partial<{
104
+ /** Clamp or otherwise normalize secret-scalar bytes before reducing mod `n`. */
105
+ adjustScalarBytes: (bytes: TArg<Uint8Array>) => TRet<Uint8Array>;
106
+ /** Domain-separation helper for contexts and prehash mode. */
107
+ domain: (data: TArg<Uint8Array>, ctx: TArg<Uint8Array>, phflag: boolean) => TRet<Uint8Array>;
108
+ /** Optional hash-to-curve mapper for protocols like Ristretto hash-to-group. */
109
+ mapToCurve: (scalar: bigint[]) => AffinePoint<bigint>;
110
+ /** Optional prehash function used before signing or verifying messages. */
111
+ prehash: FHash;
112
+ /** Default verification decoding policy. ZIP-215 is more permissive than RFC 8032 / NIST. */
113
+ zip215: boolean;
114
+ /** RNG override used by helper constructors. */
115
+ randomBytes: (bytesLength?: number) => TRet<Uint8Array>;
116
+ }>;
117
+ /**
118
+ * EdDSA (Edwards Digital Signature algorithm) helper namespace.
119
+ * Allows creating and verifying signatures, and deriving public keys.
120
+ */
121
+ export interface EdDSA {
122
+ /**
123
+ * Generate a secret/public key pair.
124
+ * @param seed - Optional seed material.
125
+ * @returns Secret/public key pair.
126
+ */
127
+ keygen: (seed?: TArg<Uint8Array>) => {
128
+ secretKey: TRet<Uint8Array>;
129
+ publicKey: TRet<Uint8Array>;
130
+ };
131
+ /**
132
+ * Derive the public key from a secret key.
133
+ * @param secretKey - Secret key bytes.
134
+ * @returns Encoded public key.
135
+ */
136
+ getPublicKey: (secretKey: TArg<Uint8Array>) => TRet<Uint8Array>;
137
+ /**
138
+ * Sign a message with an EdDSA secret key.
139
+ * @param message - Message bytes.
140
+ * @param secretKey - Secret key bytes.
141
+ * @param options - Optional signature tweaks:
142
+ * - `context` (optional): Domain-separation context for Ed25519ctx/Ed448.
143
+ * @returns Encoded signature bytes.
144
+ */
145
+ sign: (message: TArg<Uint8Array>, secretKey: TArg<Uint8Array>, options?: TArg<{
146
+ context?: Uint8Array;
147
+ }>) => TRet<Uint8Array>;
148
+ /**
149
+ * Verify a signature against a message and public key.
150
+ * @param sig - Encoded signature bytes.
151
+ * @param message - Message bytes.
152
+ * @param publicKey - Encoded public key.
153
+ * @param options - Optional verification tweaks:
154
+ * - `context` (optional): Domain-separation context for Ed25519ctx/Ed448.
155
+ * - `zip215` (optional): Whether to accept ZIP-215 encodings.
156
+ * @returns Whether the signature is valid.
157
+ */
158
+ verify: (sig: TArg<Uint8Array>, message: TArg<Uint8Array>, publicKey: TArg<Uint8Array>, options?: TArg<{
159
+ context?: Uint8Array;
160
+ zip215?: boolean;
161
+ }>) => boolean;
162
+ /** Point constructor used by this signature scheme. */
163
+ Point: EdwardsPointCons;
164
+ /** Helper utilities for key validation and Montgomery conversion. */
165
+ utils: {
166
+ /**
167
+ * Generate a valid random secret key.
168
+ * Optional seed bytes are only length-checked and returned unchanged.
169
+ */
170
+ randomSecretKey: (seed?: TArg<Uint8Array>) => TRet<Uint8Array>;
171
+ /** Check whether a secret key has the expected encoding. */
172
+ isValidSecretKey: (secretKey: TArg<Uint8Array>) => boolean;
173
+ /** Check whether a public key decodes to a valid point. */
174
+ isValidPublicKey: (publicKey: TArg<Uint8Array>, zip215?: boolean) => boolean;
175
+ /**
176
+ * Converts ed public key to x public key.
177
+ *
178
+ * There is NO `fromMontgomery`:
179
+ * - There are 2 valid ed25519 points for every x25519, with flipped coordinate
180
+ * - Sometimes there are 0 valid ed25519 points, because x25519 *additionally*
181
+ * accepts inputs on the quadratic twist, which can't be moved to ed25519
182
+ *
183
+ * @example
184
+ * Converts ed public key to x public key.
185
+ *
186
+ * ```js
187
+ * const someonesPub_ed = ed25519.getPublicKey(ed25519.utils.randomSecretKey());
188
+ * const someonesPub = ed25519.utils.toMontgomery(someonesPub);
189
+ * const aPriv = x25519.utils.randomSecretKey();
190
+ * const shared = x25519.getSharedSecret(aPriv, someonesPub)
191
+ * ```
192
+ */
193
+ toMontgomery: (publicKey: TArg<Uint8Array>) => TRet<Uint8Array>;
194
+ /**
195
+ * Converts ed secret key to x secret key.
196
+ * @example
197
+ * Converts ed secret key to x secret key.
198
+ *
199
+ * ```js
200
+ * const someonesPub = x25519.getPublicKey(x25519.utils.randomSecretKey());
201
+ * const aPriv_ed = ed25519.utils.randomSecretKey();
202
+ * const aPriv = ed25519.utils.toMontgomerySecret(aPriv_ed);
203
+ * const shared = x25519.getSharedSecret(aPriv, someonesPub)
204
+ * ```
205
+ */
206
+ toMontgomerySecret: (secretKey: TArg<Uint8Array>) => TRet<Uint8Array>;
207
+ /** Return the expanded private key components used by RFC8032 signing. */
208
+ getExtendedPublicKey: (key: TArg<Uint8Array>) => {
209
+ head: TRet<Uint8Array>;
210
+ prefix: TRet<Uint8Array>;
211
+ scalar: bigint;
212
+ point: EdwardsPoint;
213
+ pointBytes: TRet<Uint8Array>;
214
+ };
215
+ };
216
+ /** Byte lengths for keys and signatures exposed by this scheme. */
217
+ lengths: CurveLengths;
218
+ }
219
+ /**
220
+ * @param params - Curve parameters. See {@link EdwardsOpts}.
221
+ * @param extraOpts - Optional helpers and overrides. See {@link EdwardsExtraOpts}.
222
+ * @returns Edwards point constructor. Generator validation here only checks
223
+ * that `(Gx, Gy)` satisfies the affine Edwards equation.
224
+ * RFC 8032 base-point constraints like `B != (0,1)` and `[L]B = 0`
225
+ * are left to the caller's chosen parameters, since eager subgroup
226
+ * validation here adds about 10-15ms to heavyweight imports like ed448.
227
+ * The returned constructor also eagerly marks `Point.BASE` for W=8
228
+ * precompute caching. Some code paths still assume
229
+ * `Fp.BYTES === Fn.BYTES`, so mismatched byte lengths are not fully audited here.
230
+ * @throws If the curve parameters or Edwards overrides are invalid. {@link Error}
231
+ * @example
232
+ * ```ts
233
+ * import { edwards } from './edwards';
234
+ * import { jubjub } from '../misc';
235
+ * // Build a point constructor from explicit curve parameters, then use its base point.
236
+ * const Point = edwards(jubjub.Point.CURVE());
237
+ * Point.BASE.toHex();
238
+ * ```
239
+ */
240
+ export declare function edwards(params: TArg<EdwardsOpts>, extraOpts?: TArg<EdwardsExtraOpts>): EdwardsPointCons;
241
+ /**
242
+ * Base class for prime-order points like Ristretto255 and Decaf448.
243
+ * These points eliminate cofactor issues by representing equivalence classes
244
+ * of Edwards curve points. Multiple Edwards representatives can describe the
245
+ * same abstract wrapper element, so wrapper validity is not the same thing as
246
+ * the hidden representative being torsion-free.
247
+ * @param ep - Backing Edwards point.
248
+ * @example
249
+ * Base class for prime-order points like Ristretto255 and Decaf448.
250
+ *
251
+ * ```ts
252
+ * import { ristretto255 } from '../ed25519';
253
+ * const point = ristretto255.Point.BASE.multiply(2n);
254
+ * ```
255
+ */
256
+ export declare abstract class PrimeEdwardsPoint<T extends PrimeEdwardsPoint<T>> implements CurvePoint<bigint, T> {
257
+ static BASE: PrimeEdwardsPoint<any>;
258
+ static ZERO: PrimeEdwardsPoint<any>;
259
+ static Fp: IField<bigint>;
260
+ static Fn: IField<bigint>;
261
+ protected readonly ep: EdwardsPoint;
262
+ /**
263
+ * Wrap one internal Edwards representative directly.
264
+ * This is not a canonical encoding boundary: alternate Edwards
265
+ * representatives may still describe the same abstract wrapper element.
266
+ */
267
+ constructor(ep: EdwardsPoint);
268
+ abstract toBytes(): Uint8Array;
269
+ abstract equals(other: T): boolean;
270
+ static fromBytes(_bytes: Uint8Array): any;
271
+ static fromHex(_hex: string): any;
272
+ get x(): bigint;
273
+ get y(): bigint;
274
+ clearCofactor(): T;
275
+ assertValidity(): void;
276
+ /**
277
+ * Return affine coordinates of the current internal Edwards representative.
278
+ * This is a convenience helper, not a canonical Ristretto/Decaf encoding.
279
+ * Equal abstract elements may expose different `x` / `y`; use
280
+ * `toBytes()` / `fromBytes()` for canonical roundtrips.
281
+ */
282
+ toAffine(invertedZ?: bigint): AffinePoint<bigint>;
283
+ toHex(): string;
284
+ toString(): string;
285
+ isTorsionFree(): boolean;
286
+ isSmallOrder(): boolean;
287
+ add(other: T): T;
288
+ subtract(other: T): T;
289
+ multiply(scalar: bigint): T;
290
+ multiplyUnsafe(scalar: bigint): T;
291
+ double(): T;
292
+ negate(): T;
293
+ precompute(windowSize?: number, isLazy?: boolean): T;
294
+ abstract is0(): boolean;
295
+ protected abstract assertSame(other: T): void;
296
+ protected abstract init(ep: EdwardsPoint): T;
297
+ }
298
+ /**
299
+ * Initializes EdDSA signatures over given Edwards curve.
300
+ * @param Point - Edwards point constructor.
301
+ * @param cHash - Hash function.
302
+ * @param eddsaOpts - Optional signature helpers. See {@link EdDSAOpts}.
303
+ * @returns EdDSA helper namespace.
304
+ * @throws If the hash function, options, or derived point operations are invalid. {@link Error}
305
+ * @example
306
+ * Initializes EdDSA signatures over given Edwards curve.
307
+ *
308
+ * ```ts
309
+ * import { eddsa } from './edwards';
310
+ * import { jubjub } from '../misc';
311
+ * import { sha512 } from '@decryption/hashes/sha2';
312
+ * const sigs = eddsa(jubjub.Point, sha512);
313
+ * const { secretKey, publicKey } = sigs.keygen();
314
+ * const msg = new TextEncoder().encode('hello noble');
315
+ * const sig = sigs.sign(msg, secretKey);
316
+ * const isValid = sigs.verify(sig, msg, publicKey);
317
+ * ```
318
+ */
319
+ export declare function eddsa(Point: EdwardsPointCons, cHash: TArg<FHash>, eddsaOpts?: TArg<EdDSAOpts>): EdDSA;