@noble/curves 0.8.1 → 0.8.3

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 (68) hide show
  1. package/README.md +162 -131
  2. package/_shortw_utils.d.ts +1 -1
  3. package/_shortw_utils.d.ts.map +1 -1
  4. package/_shortw_utils.js +13 -8
  5. package/_shortw_utils.js.map +1 -1
  6. package/abstract/bls.d.ts +4 -4
  7. package/abstract/bls.d.ts.map +1 -1
  8. package/abstract/bls.js +16 -12
  9. package/abstract/bls.js.map +1 -1
  10. package/abstract/curve.d.ts +4 -4
  11. package/abstract/curve.d.ts.map +1 -1
  12. package/abstract/curve.js +12 -7
  13. package/abstract/curve.js.map +1 -1
  14. package/abstract/edwards.d.ts +2 -2
  15. package/abstract/edwards.d.ts.map +1 -1
  16. package/abstract/edwards.js +20 -16
  17. package/abstract/edwards.js.map +1 -1
  18. package/abstract/hash-to-curve.d.ts +4 -4
  19. package/abstract/hash-to-curve.d.ts.map +1 -1
  20. package/abstract/hash-to-curve.js +26 -18
  21. package/abstract/hash-to-curve.js.map +1 -1
  22. package/abstract/modular.d.ts +1 -1
  23. package/abstract/modular.d.ts.map +1 -1
  24. package/abstract/modular.js +44 -24
  25. package/abstract/modular.js.map +1 -1
  26. package/abstract/montgomery.d.ts +3 -3
  27. package/abstract/montgomery.d.ts.map +1 -1
  28. package/abstract/montgomery.js +15 -11
  29. package/abstract/montgomery.js.map +1 -1
  30. package/abstract/poseidon.d.ts +1 -1
  31. package/abstract/poseidon.d.ts.map +1 -1
  32. package/abstract/poseidon.js +12 -6
  33. package/abstract/poseidon.js.map +1 -1
  34. package/abstract/utils.d.ts +7 -7
  35. package/abstract/utils.d.ts.map +1 -1
  36. package/abstract/utils.js +41 -19
  37. package/abstract/utils.js.map +1 -1
  38. package/abstract/weierstrass.d.ts +13 -13
  39. package/abstract/weierstrass.d.ts.map +1 -1
  40. package/abstract/weierstrass.js +28 -21
  41. package/abstract/weierstrass.js.map +1 -1
  42. package/bls12-381.d.ts +10 -10
  43. package/bls12-381.d.ts.map +1 -1
  44. package/bls12-381.js +66 -63
  45. package/bls12-381.js.map +1 -1
  46. package/bn.js +10 -7
  47. package/bn.js.map +1 -1
  48. package/ed25519.d.ts +1 -1
  49. package/ed25519.d.ts.map +1 -1
  50. package/ed25519.js +78 -72
  51. package/ed25519.js.map +1 -1
  52. package/ed448.js +41 -37
  53. package/ed448.js.map +1 -1
  54. package/esm/ed25519.js +2 -1
  55. package/esm/ed25519.js.map +1 -1
  56. package/jubjub.js +22 -17
  57. package/jubjub.js.map +1 -1
  58. package/p256.js +17 -13
  59. package/p256.js.map +1 -1
  60. package/p384.js +17 -13
  61. package/p384.js.map +1 -1
  62. package/p521.js +17 -13
  63. package/p521.js.map +1 -1
  64. package/package.json +5 -5
  65. package/pasta.js +19 -16
  66. package/pasta.js.map +1 -1
  67. package/secp256k1.js +57 -53
  68. package/secp256k1.js.map +1 -1
package/README.md CHANGED
@@ -9,20 +9,27 @@
9
9
  for encoding or hashing an arbitrary string to an elliptic curve point
10
10
  - 🧜‍♂️ [Poseidon](https://www.poseidon-hash.info) ZK-friendly hash
11
11
  - 🏎 [Ultra-fast](#speed), hand-optimized for caveats of JS engines
12
- - 🔍 Unique tests ensure correctness with Wycheproof vectors and [cryptofuzz](https://github.com/guidovranken/cryptofuzz) differential fuzzing
12
+ - 🔍 Unique tests ensure correctness with Wycheproof vectors and
13
+ [cryptofuzz](https://github.com/guidovranken/cryptofuzz) differential fuzzing
13
14
  - 🔻 Tree-shaking-friendly: there is no entry point, which ensures small size of your app
14
15
 
15
16
  Package consists of two parts:
16
17
 
17
18
  1. [Abstract](#abstract-api), zero-dependency EC algorithms
18
- 2. [Implementations](#implementations), utilizing one dependency `@noble/hashes`, providing ready-to-use:
19
+ 2. [Implementations](#implementations), utilizing one dependency `@noble/hashes`,
20
+ providing ready-to-use:
19
21
  - NIST curves secp256r1/P256, secp384r1/P384, secp521r1/P521
20
22
  - SECG curve secp256k1
21
- - ed25519/curve25519/x25519/ristretto255, edwards448/curve448/x448 [RFC7748](https://www.rfc-editor.org/rfc/rfc7748) / [RFC8032](https://www.rfc-editor.org/rfc/rfc8032) / [ZIP215](https://zips.z.cash/zip-0215) stuff
23
+ - ed25519/curve25519/x25519/ristretto255, edwards448/curve448/x448
24
+ implementing
25
+ [RFC7748](https://www.rfc-editor.org/rfc/rfc7748) /
26
+ [RFC8032](https://www.rfc-editor.org/rfc/rfc8032) /
27
+ [ZIP215](https://zips.z.cash/zip-0215) standards
22
28
  - pairing-friendly curves bls12-381, bn254
23
29
 
24
- Check out [Upgrading](#upgrading) if you've previously used single-feature noble packages
25
- ([secp256k1](https://github.com/paulmillr/noble-secp256k1), [ed25519](https://github.com/paulmillr/noble-ed25519)).
30
+ Check out [Upgrading](#upgrading) if you've previously used single-feature noble
31
+ packages ([secp256k1](https://github.com/paulmillr/noble-secp256k1),
32
+ [ed25519](https://github.com/paulmillr/noble-ed25519)).
26
33
  See [Resources](#resources) for articles and real-world software that uses curves.
27
34
 
28
35
  ### This library belongs to _noble_ crypto
@@ -41,22 +48,25 @@ See [Resources](#resources) for articles and real-world software that uses curve
41
48
 
42
49
  ## Usage
43
50
 
44
- Use NPM for browser / node.js:
51
+ Browser, deno and node.js are supported:
45
52
 
46
53
  > npm install @noble/curves
47
54
 
48
- For [Deno](https://deno.land), use it with [npm specifier](https://deno.land/manual@v1.28.0/node/npm_specifiers). In browser, you could also include the single file from
55
+ For [Deno](https://deno.land), use it with
56
+ [npm specifier](https://deno.land/manual@v1.28.0/node/npm_specifiers).
57
+ In browser, you could also include the single file from
49
58
  [GitHub's releases page](https://github.com/paulmillr/noble-curves/releases).
50
59
 
51
- The library is tree-shaking-friendly and does not expose root entry point as `import * from '@noble/curves'`.
52
- Instead, you need to import specific primitives. This is done to ensure small size of your apps.
60
+ The library is tree-shaking-friendly and does not expose root entry point as
61
+ `import * from '@noble/curves'`. Instead, you need to import specific primitives.
62
+ This is done to ensure small size of your apps.
53
63
 
54
64
  ### Implementations
55
65
 
56
66
  Each curve can be used in the following way:
57
67
 
58
68
  ```ts
59
- import { secp256k1 } from '@noble/curves/secp256k1'; // ECMAScript Modules (ESM) and Common.js
69
+ import { secp256k1 } from '@noble/curves/secp256k1'; // ESM and Common.js
60
70
  // import { secp256k1 } from 'npm:@noble/curves@1.2.0/secp256k1'; // Deno
61
71
  const priv = secp256k1.utils.randomPrivateKey();
62
72
  const pub = secp256k1.getPublicKey(priv);
@@ -64,8 +74,9 @@ const msg = new Uint8Array(32).fill(1);
64
74
  const sig = secp256k1.sign(msg, priv);
65
75
  secp256k1.verify(sig, msg, pub) === true;
66
76
 
77
+ // hex strings are also supported besides Uint8Arrays:
67
78
  const privHex = '46c930bc7bb4db7f55da20798697421b98c4175a52c630294d75a84b9c126236';
68
- const pub2 = secp256k1.getPublicKey(privHex); // keys & other inputs can be Uint8Array-s or hex strings
79
+ const pub2 = secp256k1.getPublicKey(privHex);
69
80
  ```
70
81
 
71
82
  All curves:
@@ -90,7 +101,7 @@ Weierstrass curves feature recovering public keys from signatures and ECDH key a
90
101
  const sigImprovedSecurity = secp256k1.sign(msg, priv, { extraEntropy: true });
91
102
  sig.recoverPublicKey(msg) === pub; // public key recovery
92
103
  const someonesPub = secp256k1.getPublicKey(secp256k1.utils.randomPrivateKey());
93
- const shared = secp256k1.getSharedSecret(priv, someonesPub); // ECDH (elliptic curve diffie-hellman)
104
+ const shared = secp256k1.getSharedSecret(priv, someonesPub); // ECDH
94
105
  ```
95
106
 
96
107
  secp256k1 has schnorr signature implementation which follows
@@ -103,7 +114,6 @@ const pub = schnorr.getPublicKey(priv);
103
114
  const msg = new TextEncoder().encode('hello');
104
115
  const sig = schnorr.sign(msg, priv);
105
116
  const isValid = schnorr.verify(sig, msg, pub);
106
- console.log(isValid);
107
117
  ```
108
118
 
109
119
  ed25519 module has ed25519ctx / ed25519ph variants,
@@ -134,18 +144,20 @@ RistrettoPoint.hashToCurve('Ristretto is traditionally a short shot of espresso
134
144
  // also has add(), equals(), multiply(), toRawBytes() methods
135
145
  ```
136
146
 
137
- ed448 module is basically the same:
147
+ ed448 is similar:
138
148
 
139
149
  ```ts
140
150
  import { ed448, ed448ph, ed448ctx, x448 } from '@noble/curves/ed448';
141
151
  import { hashToCurve, encodeToCurve } from '@noble/curves/ed448';
152
+ ed448.getPublicKey(ed448.utils.randomPrivateKey());
142
153
  ```
143
154
 
144
155
  BLS12-381 pairing-friendly Barreto-Lynn-Scott elliptic curve construction allows to
145
156
  construct [zk-SNARKs](https://z.cash/technology/zksnarks/) at the 128-bit security
146
157
  and use aggregated, batch-verifiable
147
158
  [threshold signatures](https://medium.com/snigirev.stepan/bls-signatures-better-than-schnorr-5a7fe30ea716),
148
- using Boneh-Lynn-Shacham signature scheme.
159
+ using Boneh-Lynn-Shacham signature scheme. Compatible with ETH and others,
160
+ just make sure to provide correct DST (domain separation tag argument).
149
161
 
150
162
  ```ts
151
163
  import { bls12_381 as bls } from '@noble/curves/bls12-381';
@@ -182,10 +194,13 @@ console.log({ publicKeys, signatures3, aggSignature3, isValid3 });
182
194
 
183
195
  ## Abstract API
184
196
 
185
- Abstract API allows to define custom curves. All arithmetics is done with JS bigints over finite fields,
186
- which is defined from `modular` sub-module. For scalar multiplication, we use [precomputed tables with w-ary non-adjacent form (wNAF)](https://paulmillr.com/posts/noble-secp256k1-fast-ecc/).
187
- Precomputes are enabled for weierstrass and edwards BASE points of a curve. You could precompute any
188
- other point (e.g. for ECDH) using `utils.precompute()` method: check out examples.
197
+ Abstract API allows to define custom curves. All arithmetics is done with JS
198
+ bigints over finite fields, which is defined from `modular` sub-module. For
199
+ scalar multiplication, we use
200
+ [precomputed tables with w-ary non-adjacent form (wNAF)](https://paulmillr.com/posts/noble-secp256k1-fast-ecc/).
201
+ Precomputes are enabled for weierstrass and edwards BASE points of a curve. You
202
+ could precompute any other point (e.g. for ECDH) using `utils.precompute()`
203
+ method: check out examples.
189
204
 
190
205
  There are following zero-dependency algorithms:
191
206
 
@@ -201,14 +216,34 @@ There are following zero-dependency algorithms:
201
216
 
202
217
  ```ts
203
218
  import { weierstrass } from '@noble/curves/abstract/weierstrass';
219
+ import { Fp } from '@noble/curves/abstract/modular'; // finite field for mod arithmetics
220
+ import { sha256 } from '@noble/hashes/sha256'; // 3rd-party sha256() of type utils.CHash
221
+ import { hmac } from '@noble/hashes/hmac'; // 3rd-party hmac() that will accept sha256()
222
+ import { concatBytes, randomBytes } from '@noble/hashes/utils'; // 3rd-party utilities
223
+ const secq256k1 = weierstrass({
224
+ // secq256k1: cycle of secp256k1 with Fp/N flipped.
225
+ // https://personaelabs.org/posts/spartan-ecdsa
226
+ // https://zcash.github.io/halo2/background/curves.html#cycles-of-curves
227
+ a: 0n,
228
+ b: 7n,
229
+ Fp: Fp(2n ** 256n - 432420386565659656852420866394968145599n),
230
+ n: 2n ** 256n - 2n ** 32n - 2n ** 9n - 2n ** 8n - 2n ** 7n - 2n ** 6n - 2n ** 4n - 1n,
231
+ Gx: 55066263022277343669578718895168534326250603453777594175500187360389116729240n,
232
+ Gy: 32670510020758816978083085130507043184471273380659243275938904335757337482424n,
233
+ hash: sha256,
234
+ hmac: (key: Uint8Array, ...msgs: Uint8Array[]) => hmac(sha256, key, concatBytes(...msgs)),
235
+ randomBytes,
236
+ });
204
237
  ```
205
238
 
206
- Short Weierstrass curve's formula is `y² = x³ + ax + b`. `weierstrass` expects arguments `a`, `b`, field `Fp`, curve order `n`, cofactor `h`
239
+ Short Weierstrass curve's formula is `y² = x³ + ax + b`. `weierstrass`
240
+ expects arguments `a`, `b`, field `Fp`, curve order `n`, cofactor `h`
207
241
  and coordinates `Gx`, `Gy` of generator point.
208
242
 
209
- **`k` generation** is done deterministically, following [RFC6979](https://www.rfc-editor.org/rfc/rfc6979).
210
- For this you will need `hmac` & `hash`, which in our implementations is provided by noble-hashes.
211
- If you're using different hashing library, make sure to wrap it in the following interface:
243
+ **`k` generation** is done deterministically, following
244
+ [RFC6979](https://www.rfc-editor.org/rfc/rfc6979). For this you will need
245
+ `hmac` & `hash`, which in our implementations is provided by noble-hashes. If
246
+ you're using different hashing library, make sure to wrap it in the following interface:
212
247
 
213
248
  ```ts
214
249
  type CHash = {
@@ -224,11 +259,35 @@ type CHash = {
224
259
  1. Exported as `ProjectivePoint`
225
260
  2. Represented in projective (homogeneous) coordinates: (x, y, z) ∋ (x=x/z, y=y/z)
226
261
  3. Use complete exception-free formulas for addition and doubling
227
- 4. Can be decoded/encoded from/to Uint8Array / hex strings using `ProjectivePoint.fromHex` and `ProjectivePoint#toRawBytes()`
262
+ 4. Can be decoded/encoded from/to Uint8Array / hex strings using
263
+ `ProjectivePoint.fromHex` and `ProjectivePoint#toRawBytes()`
228
264
  5. Have `assertValidity()` which checks for being on-curve
229
265
  6. Have `toAffine()` and `x` / `y` getters which convert to 2d xy affine coordinates
230
266
 
231
267
  ```ts
268
+ // `weierstrass()` returns `CurveFn`
269
+ type SignOpts = { lowS?: boolean; prehash?: boolean; extraEntropy: boolean | Uint8Array };
270
+ type CurveFn = {
271
+ CURVE: ReturnType<typeof validateOpts>;
272
+ getPublicKey: (privateKey: PrivKey, isCompressed?: boolean) => Uint8Array;
273
+ getSharedSecret: (privateA: PrivKey, publicB: Hex, isCompressed?: boolean) => Uint8Array;
274
+ sign: (msgHash: Hex, privKey: PrivKey, opts?: SignOpts) => SignatureType;
275
+ verify: (
276
+ signature: Hex | SignatureType,
277
+ msgHash: Hex,
278
+ publicKey: Hex,
279
+ opts?: { lowS?: boolean; prehash?: boolean }
280
+ ) => boolean;
281
+ ProjectivePoint: ProjectivePointConstructor;
282
+ Signature: SignatureConstructor;
283
+ utils: {
284
+ normPrivateKeyToScalar: (key: PrivKey) => bigint;
285
+ isValidPrivateKey(key: PrivKey): boolean;
286
+ randomPrivateKey: () => Uint8Array;
287
+ precompute: (windowSize?: number, point?: ProjPointType<bigint>) => ProjPointType<bigint>;
288
+ };
289
+ };
290
+
232
291
  // T is usually bigint, but can be something else like complex numbers in BLS curves
233
292
  interface ProjPointType<T> extends Group<ProjPointType<T>> {
234
293
  readonly px: T;
@@ -254,7 +313,8 @@ interface ProjConstructor<T> extends GroupConstructor<ProjPointType<T>> {
254
313
  }
255
314
  ```
256
315
 
257
- **ECDSA signatures** are represented by `Signature` instances and can be described by the interface:
316
+ **ECDSA signatures** are represented by `Signature` instances and can be
317
+ described by the interface:
258
318
 
259
319
  ```ts
260
320
  interface SignatureType {
@@ -279,28 +339,9 @@ type SignatureConstructor = {
279
339
  };
280
340
  ```
281
341
 
282
- Example implementing [secq256k1](https://personaelabs.org/posts/spartan-ecdsa) (NOT secp256k1)
283
- [cycle](https://zcash.github.io/halo2/background/curves.html#cycles-of-curves) of secp256k1 with Fp/N flipped.
342
+ More examples:
284
343
 
285
344
  ```typescript
286
- import { weierstrass } from '@noble/curves/abstract/weierstrass';
287
- import { Field } from '@noble/curves/abstract/modular'; // finite field, mod arithmetics done over it
288
- import { sha256 } from '@noble/hashes/sha256'; // 3rd-party sha256() of type utils.CHash, with blockLen/outputLen
289
- import { hmac } from '@noble/hashes/hmac'; // 3rd-party hmac() that will accept sha256()
290
- import { concatBytes, randomBytes } from '@noble/hashes/utils'; // 3rd-party utilities
291
- const secq256k1 = weierstrass({
292
- // secq256k1: cycle of secp256k1 with Fp/N flipped.
293
- a: 0n,
294
- b: 7n,
295
- Fp: Field(2n ** 256n - 432420386565659656852420866394968145599n),
296
- n: 2n ** 256n - 2n ** 32n - 2n ** 9n - 2n ** 8n - 2n ** 7n - 2n ** 6n - 2n ** 4n - 1n,
297
- Gx: 55066263022277343669578718895168534326250603453777594175500187360389116729240n,
298
- Gy: 32670510020758816978083085130507043184471273380659243275938904335757337482424n,
299
- hash: sha256,
300
- hmac: (key: Uint8Array, ...msgs: Uint8Array[]) => hmac(sha256, key, concatBytes(...msgs)),
301
- randomBytes,
302
- });
303
-
304
345
  // All curves expose same generic interface.
305
346
  const priv = secq256k1.utils.randomPrivateKey();
306
347
  secq256k1.getPublicKey(priv); // Convert private key to public.
@@ -326,84 +367,19 @@ const fast = secq256k1.utils.precompute(8, Point.fromHex(someonesPubKey));
326
367
  fast.multiply(privKey); // much faster ECDH now
327
368
  ```
328
369
 
329
- `weierstrass()` returns `CurveFn`:
330
-
331
- ```ts
332
- type SignOpts = { lowS?: boolean; prehash?: boolean; extraEntropy: boolean | Uint8Array };
333
- type CurveFn = {
334
- CURVE: ReturnType<typeof validateOpts>;
335
- getPublicKey: (privateKey: PrivKey, isCompressed?: boolean) => Uint8Array;
336
- getSharedSecret: (privateA: PrivKey, publicB: Hex, isCompressed?: boolean) => Uint8Array;
337
- sign: (msgHash: Hex, privKey: PrivKey, opts?: SignOpts) => SignatureType;
338
- verify: (
339
- signature: Hex | SignatureType,
340
- msgHash: Hex,
341
- publicKey: Hex,
342
- opts?: { lowS?: boolean; prehash?: boolean }
343
- ) => boolean;
344
- ProjectivePoint: ProjectivePointConstructor;
345
- Signature: SignatureConstructor;
346
- utils: {
347
- normPrivateKeyToScalar: (key: PrivKey) => bigint;
348
- isValidPrivateKey(key: PrivKey): boolean;
349
- randomPrivateKey: () => Uint8Array;
350
- precompute: (windowSize?: number, point?: ProjPointType<bigint>) => ProjPointType<bigint>;
351
- };
352
- };
353
- ```
354
-
355
370
  ### abstract/edwards: Twisted Edwards curve
356
371
 
357
- Twisted Edwards curve's formula is `ax² + y² = 1 + dx²y²`. You must specify `a`, `d`, field `Fp`, order `n`, cofactor `h`
358
- and coordinates `Gx`, `Gy` of generator point.
359
-
360
- For EdDSA signatures, `hash` param required. `adjustScalarBytes` which instructs how to change private scalars could be specified.
361
-
362
- **Edwards points:**
363
-
364
- 1. Exported as `ExtendedPoint`
365
- 2. Represented in extended coordinates: (x, y, z, t) ∋ (x=x/z, y=y/z)
366
- 3. Use complete exception-free formulas for addition and doubling
367
- 4. Can be decoded/encoded from/to Uint8Array / hex strings using `ExtendedPoint.fromHex` and `ExtendedPoint#toRawBytes()`
368
- 5. Have `assertValidity()` which checks for being on-curve
369
- 6. Have `toAffine()` and `x` / `y` getters which convert to 2d xy affine coordinates
370
- 7. Have `isTorsionFree()`, `clearCofactor()` and `isSmallOrder()` utilities to handle torsions
371
-
372
- ```ts
373
- interface ExtPointType extends Group<ExtPointType> {
374
- readonly ex: bigint;
375
- readonly ey: bigint;
376
- readonly ez: bigint;
377
- readonly et: bigint;
378
- assertValidity(): void;
379
- multiply(scalar: bigint): ExtPointType;
380
- multiplyUnsafe(scalar: bigint): ExtPointType;
381
- isSmallOrder(): boolean;
382
- isTorsionFree(): boolean;
383
- clearCofactor(): ExtPointType;
384
- toAffine(iz?: bigint): AffinePoint<bigint>;
385
- }
386
- // Static methods of Extended Point with coordinates in X, Y, Z, T
387
- interface ExtPointConstructor extends GroupConstructor<ExtPointType> {
388
- new (x: bigint, y: bigint, z: bigint, t: bigint): ExtPointType;
389
- fromAffine(p: AffinePoint<bigint>): ExtPointType;
390
- fromHex(hex: Hex): ExtPointType;
391
- fromPrivateKey(privateKey: Hex): ExtPointType;
392
- }
393
- ```
394
-
395
- Example implementing edwards25519:
396
-
397
372
  ```ts
398
373
  import { twistedEdwards } from '@noble/curves/abstract/edwards';
399
- import { Field, div } from '@noble/curves/abstract/modular';
374
+ import { Fp } from '@noble/curves/abstract/modular';
400
375
  import { sha512 } from '@noble/hashes/sha512';
376
+ import { randomBytes } from '@noble/hashes/utils';
401
377
 
402
- const Fp = Field(2n ** 255n - 19n);
378
+ const fp = Fp(2n ** 255n - 19n);
403
379
  const ed25519 = twistedEdwards({
404
380
  a: -1n,
405
- d: Fp.div(-121665n, 121666n), // -121665n/121666n mod p
406
- Fp,
381
+ d: fp.div(-121665n, 121666n), // -121665n/121666n mod p
382
+ Fp: fp,
407
383
  n: 2n ** 252n + 27742317777372353535851937790883648493n,
408
384
  h: 8n,
409
385
  Gx: 15112221349535400772501151409588531511454012693041857206046113283949847762202n,
@@ -420,9 +396,23 @@ const ed25519 = twistedEdwards({
420
396
  } as const);
421
397
  ```
422
398
 
423
- `twistedEdwards()` returns `CurveFn` of following type:
399
+ Twisted Edwards curve's formula is `ax² + y² = 1 + dx²y²`. You must specify `a`, `d`, field `Fp`, order `n`, cofactor `h`
400
+ and coordinates `Gx`, `Gy` of generator point.
401
+
402
+ For EdDSA signatures, `hash` param required. `adjustScalarBytes` which instructs how to change private scalars could be specified.
403
+
404
+ **Edwards points:**
405
+
406
+ 1. Exported as `ExtendedPoint`
407
+ 2. Represented in extended coordinates: (x, y, z, t) ∋ (x=x/z, y=y/z)
408
+ 3. Use complete exception-free formulas for addition and doubling
409
+ 4. Can be decoded/encoded from/to Uint8Array / hex strings using `ExtendedPoint.fromHex` and `ExtendedPoint#toRawBytes()`
410
+ 5. Have `assertValidity()` which checks for being on-curve
411
+ 6. Have `toAffine()` and `x` / `y` getters which convert to 2d xy affine coordinates
412
+ 7. Have `isTorsionFree()`, `clearCofactor()` and `isSmallOrder()` utilities to handle torsions
424
413
 
425
414
  ```ts
415
+ // `twistedEdwards()` returns `CurveFn` of following type:
426
416
  type CurveFn = {
427
417
  CURVE: ReturnType<typeof validateOpts>;
428
418
  getPublicKey: (privateKey: Hex) => Uint8Array;
@@ -440,19 +430,37 @@ type CurveFn = {
440
430
  };
441
431
  };
442
432
  };
433
+
434
+ interface ExtPointType extends Group<ExtPointType> {
435
+ readonly ex: bigint;
436
+ readonly ey: bigint;
437
+ readonly ez: bigint;
438
+ readonly et: bigint;
439
+ assertValidity(): void;
440
+ multiply(scalar: bigint): ExtPointType;
441
+ multiplyUnsafe(scalar: bigint): ExtPointType;
442
+ isSmallOrder(): boolean;
443
+ isTorsionFree(): boolean;
444
+ clearCofactor(): ExtPointType;
445
+ toAffine(iz?: bigint): AffinePoint<bigint>;
446
+ }
447
+ // Static methods of Extended Point with coordinates in X, Y, Z, T
448
+ interface ExtPointConstructor extends GroupConstructor<ExtPointType> {
449
+ new (x: bigint, y: bigint, z: bigint, t: bigint): ExtPointType;
450
+ fromAffine(p: AffinePoint<bigint>): ExtPointType;
451
+ fromHex(hex: Hex): ExtPointType;
452
+ fromPrivateKey(privateKey: Hex): ExtPointType;
453
+ }
443
454
  ```
444
455
 
445
456
  ### abstract/montgomery: Montgomery curve
446
457
 
447
- The module contains methods for x-only ECDH on Curve25519 / Curve448 from RFC7748. Proper Elliptic Curve Points are not implemented yet.
448
-
449
- You must specify curve params `Fp`, `a`, `Gu` coordinate of u, `montgomeryBits` and `nByteLength`.
450
-
451
458
  ```typescript
452
459
  import { montgomery } from '@noble/curves/abstract/montgomery';
460
+ import { Fp } from '@noble/curves/abstract/modular';
453
461
 
454
462
  const x25519 = montgomery({
455
- Fp: Field(2n ** 255n - 19n),
463
+ Fp: Fp(2n ** 255n - 19n),
456
464
  a: 486662n,
457
465
  Gu: 9n,
458
466
  montgomeryBits: 255,
@@ -467,6 +475,11 @@ const x25519 = montgomery({
467
475
  });
468
476
  ```
469
477
 
478
+ The module contains methods for x-only ECDH on Curve25519 / Curve448 from RFC7748.
479
+ Proper Elliptic Curve Points are not implemented yet.
480
+
481
+ You must specify curve params `Fp`, `a`, `Gu` coordinate of u, `montgomeryBits` and `nByteLength`.
482
+
470
483
  ### abstract/hash-to-curve: Hashing strings to curve points
471
484
 
472
485
  The module allows to hash arbitrary strings to elliptic curve points. Implements [hash-to-curve v16](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-16).
@@ -507,19 +520,37 @@ function expand_message_xof(
507
520
  ): Uint8Array;
508
521
  ```
509
522
 
510
- `hash_to_field(msg, count, options)` [(spec)](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-5.3)
523
+ `hash_to_field(msg, count, options)`
524
+ [(spec)](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-5.3)
511
525
  hashes arbitrary-length byte strings to a list of one or more elements of a finite field F.
512
526
 
513
- - `msg` a byte string containing the message to hash
514
- - `count` the number of elements of F to output
515
- - `options` `{DST: string, p: bigint, m: number, k: number, expand: 'xmd' | 'xof', hash: H}`.
516
- - `p` is field prime, m=field extension (1 for prime fields)
517
- - `k` is security target in bits (e.g. 128).
518
- - `expand` should be `xmd` for SHA2, SHA3, BLAKE; `xof` for SHAKE, BLAKE-XOF
519
- - `hash` conforming to `utils.CHash` interface, with `outputLen` / `blockLen` props
520
- - Returns `[u_0, ..., u_(count - 1)]`, a list of field elements.
521
-
522
527
  ```ts
528
+ /**
529
+ * * `DST` is a domain separation tag, defined in section 2.2.5
530
+ * * `p` characteristic of F, where F is a finite field of characteristic p and order q = p^m
531
+ * * `m` is extension degree (1 for prime fields)
532
+ * * `k` is the target security target in bits (e.g. 128), from section 5.1
533
+ * * `expand` is `xmd` (SHA2, SHA3, BLAKE) or `xof` (SHAKE, BLAKE-XOF)
534
+ * * `hash` conforming to `utils.CHash` interface, with `outputLen` / `blockLen` props
535
+ */
536
+ type UnicodeOrBytes = string | Uint8Array;
537
+ type Opts = {
538
+ DST: UnicodeOrBytes;
539
+ p: bigint;
540
+ m: number;
541
+ k: number;
542
+ expand?: 'xmd' | 'xof';
543
+ hash: CHash;
544
+ };
545
+
546
+ /**
547
+ * Hashes arbitrary-length byte strings to a list of one or more elements of a finite field F
548
+ * https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-5.3
549
+ * @param msg a byte string containing the message to hash
550
+ * @param count the number of elements of F to output
551
+ * @param options `{DST: string, p: bigint, m: number, k: number, expand: 'xmd' | 'xof', hash: H}`, see above
552
+ * @returns [u_0, ..., u_(count - 1)], a list of field elements.
553
+ */
523
554
  function hash_to_field(msg: Uint8Array, count: number, options: Opts): bigint[][];
524
555
  ```
525
556
 
@@ -6,7 +6,7 @@ export declare function getHash(hash: CHash): {
6
6
  hmac: (key: Uint8Array, ...msgs: Uint8Array[]) => Uint8Array;
7
7
  randomBytes: typeof randomBytes;
8
8
  };
9
- declare type CurveDef = Readonly<Omit<CurveType, 'hash' | 'hmac' | 'randomBytes'>>;
9
+ type CurveDef = Readonly<Omit<CurveType, 'hash' | 'hmac' | 'randomBytes'>>;
10
10
  export declare function createCurve(curveDef: CurveDef, defHash: CHash): Readonly<{
11
11
  create: (hash: CHash) => import("./abstract/weierstrass.js").CurveFn;
12
12
  CURVE: Readonly<{
@@ -1 +1 @@
1
- {"version":3,"file":"_shortw_utils.d.ts","sourceRoot":"","sources":["src/_shortw_utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAe,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAAe,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACnE,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAG5C,wBAAgB,OAAO,CAAC,IAAI,EAAE,KAAK;;gBAGnB,UAAU,WAAW,UAAU,EAAE;;EAGhD;AAED,aAAK,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,aAAa,CAAC,CAAC,CAAC;AAC3E,wBAAgB,WAAW,CAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK;mBACtC,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAE5B"}
1
+ {"version":3,"file":"_shortw_utils.d.ts","sourceRoot":"","sources":["src/_shortw_utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAe,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAAe,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACnE,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAG5C,wBAAgB,OAAO,CAAC,IAAI,EAAE,KAAK;;gBAGnB,UAAU,WAAW,UAAU,EAAE;;EAGhD;AAED,KAAK,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,aAAa,CAAC,CAAC,CAAC;AAC3E,wBAAgB,WAAW,CAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK;mBACtC,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAE5B"}
package/_shortw_utils.js CHANGED
@@ -1,17 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createCurve = exports.getHash = void 0;
1
4
  /*! 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
+ const hmac_1 = require("@noble/hashes/hmac");
6
+ const utils_1 = require("@noble/hashes/utils");
7
+ const weierstrass_js_1 = require("./abstract/weierstrass.js");
5
8
  // connects noble-curves to noble-hashes
6
- export function getHash(hash) {
9
+ function getHash(hash) {
7
10
  return {
8
11
  hash,
9
- hmac: (key, ...msgs) => hmac(hash, key, concatBytes(...msgs)),
10
- randomBytes,
12
+ hmac: (key, ...msgs) => (0, hmac_1.hmac)(hash, key, (0, utils_1.concatBytes)(...msgs)),
13
+ randomBytes: utils_1.randomBytes,
11
14
  };
12
15
  }
13
- export function createCurve(curveDef, defHash) {
14
- const create = (hash) => weierstrass({ ...curveDef, ...getHash(hash) });
16
+ exports.getHash = getHash;
17
+ function createCurve(curveDef, defHash) {
18
+ const create = (hash) => (0, weierstrass_js_1.weierstrass)({ ...curveDef, ...getHash(hash) });
15
19
  return Object.freeze({ ...create(defHash), create });
16
20
  }
21
+ exports.createCurve = createCurve;
17
22
  //# sourceMappingURL=_shortw_utils.js.map
@@ -1 +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;AACx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MAAM,EAAE,CAAC,CAAC;AACvD,CAAC;AAHD,kCAGC"}
package/abstract/bls.d.ts CHANGED
@@ -16,12 +16,12 @@ import { Field } from './modular.js';
16
16
  import { Hex, PrivKey, CHash } from './utils.js';
17
17
  import * as htf from './hash-to-curve.js';
18
18
  import { CurvePointsType, ProjPointType as ProjPointType, CurvePointsRes } from './weierstrass.js';
19
- declare type Fp = bigint;
20
- export declare type SignatureCoder<Fp2> = {
19
+ type Fp = bigint;
20
+ export type SignatureCoder<Fp2> = {
21
21
  decode(hex: Hex): ProjPointType<Fp2>;
22
22
  encode(point: ProjPointType<Fp2>): Uint8Array;
23
23
  };
24
- export declare type CurveType<Fp, Fp2, Fp6, Fp12> = {
24
+ export type CurveType<Fp, Fp2, Fp6, Fp12> = {
25
25
  r: bigint;
26
26
  G1: Omit<CurvePointsType<Fp>, 'n'> & {
27
27
  mapToCurve: htf.MapToCurve<Fp>;
@@ -54,7 +54,7 @@ export declare type CurveType<Fp, Fp2, Fp6, Fp12> = {
54
54
  hash: CHash;
55
55
  randomBytes: (bytesLength?: number) => Uint8Array;
56
56
  };
57
- export declare type CurveFn<Fp, Fp2, Fp6, Fp12> = {
57
+ export type CurveFn<Fp, Fp2, Fp6, Fp12> = {
58
58
  CURVE: CurveType<Fp, Fp2, Fp6, Fp12>;
59
59
  Fr: Field<bigint>;
60
60
  Fp: Field<Fp>;
@@ -1 +1 @@
1
- {"version":3,"file":"bls.d.ts","sourceRoot":"","sources":["../src/abstract/bls.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,KAAK,EAAuB,MAAM,cAAc,CAAC;AAC1D,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,KAAK,EAA+B,MAAM,YAAY,CAAC;AAC9E,OAAO,KAAK,GAAG,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EACL,eAAe,EACf,aAAa,I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AE,CAAC,CAAC,EAAE,KACpC,OAAO,CAAC;IACb,KAAK,EAAE;QACL,gBAAgB,EAAE,MAAM,UAAU,CAAC;KACpC,CAAC;CACH,CAAC;AAEF,wBAAgB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAChC,KAAK,EAAE,SAAS,CAAC,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,GACnC,OAAO,CAAC,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CA8Q7B"}
package/abstract/bls.js CHANGED
@@ -1,11 +1,14 @@
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) {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bls = void 0;
4
+ const modular_js_1 = require("./modular.js");
5
+ const utils_js_1 = require("./utils.js");
6
+ const htf = require("./hash-to-curve.js");
7
+ const weierstrass_js_1 = require("./weierstrass.js");
8
+ function bls(CURVE) {
6
9
  // Fields looks pretty specific for curve, so for now we need to pass them with opts
7
10
  const { Fp, Fr, Fp2, Fp6, Fp12 } = CURVE;
8
- const BLS_X_LEN = bitLen(CURVE.x);
11
+ const BLS_X_LEN = (0, utils_js_1.bitLen)(CURVE.x);
9
12
  const groupLen = 32; // TODO: calculate; hardcoded for now
10
13
  // Pre-compute coefficients for sparse multiplication
11
14
  // Point addition and point double calculations is reused for coefficients
@@ -31,7 +34,7 @@ export function bls(CURVE) {
31
34
  Rx = Fp2.div(Fp2.mul(Fp2.mul(Fp2.sub(t0, t3), Rx), Ry), 2n); // ((T0 - T3) * Rx * Ry) / 2
32
35
  Ry = Fp2.sub(Fp2.sqr(Fp2.div(Fp2.add(t0, t3), 2n)), Fp2.mul(Fp2.sqr(t2), 3n)); // ((T0 + T3) / 2)² - 3 * T2²
33
36
  Rz = Fp2.mul(t0, t4); // T0 * T4
34
- if (bitGet(CURVE.x, i)) {
37
+ if ((0, utils_js_1.bitGet)(CURVE.x, i)) {
35
38
  // Addition
36
39
  let t0 = Fp2.sub(Ry, Fp2.mul(Qy, Rz)); // Ry - Qy * Rz
37
40
  let t1 = Fp2.sub(Rx, Fp2.mul(Qx, Rz)); // Rx - Qx * Rz
@@ -59,7 +62,7 @@ export function bls(CURVE) {
59
62
  for (let j = 0, i = BLS_X_LEN - 2; i >= 0; i--, j++) {
60
63
  const E = ell[j];
61
64
  f12 = Fp12.multiplyBy014(f12, E[0], Fp2.mul(E[1], Px), Fp2.mul(E[2], Py));
62
- if (bitGet(x, i)) {
65
+ if ((0, utils_js_1.bitGet)(x, i)) {
63
66
  j += 1;
64
67
  const F = ell[j];
65
68
  f12 = Fp12.multiplyBy014(f12, F[0], Fp2.mul(F[1], Px), Fp2.mul(F[2], Py));
@@ -71,11 +74,11 @@ export function bls(CURVE) {
71
74
  }
72
75
  const utils = {
73
76
  randomPrivateKey: () => {
74
- return Fr.toBytes(hashToPrivateScalar(CURVE.randomBytes(groupLen + 8), CURVE.r));
77
+ return Fr.toBytes((0, modular_js_1.hashToPrivateScalar)(CURVE.randomBytes(groupLen + 8), CURVE.r));
75
78
  },
76
79
  };
77
80
  // Point on G1 curve: (x, y)
78
- const G1_ = weierstrassPoints({ n: Fr.ORDER, ...CURVE.G1 });
81
+ const G1_ = (0, weierstrass_js_1.weierstrassPoints)({ n: Fr.ORDER, ...CURVE.G1 });
79
82
  const G1 = Object.assign(G1_, htf.createHasher(G1_.ProjectivePoint, CURVE.G1.mapToCurve, {
80
83
  ...CURVE.htfDefaults,
81
84
  ...CURVE.G1.htfDefaults,
@@ -93,7 +96,7 @@ export function bls(CURVE) {
93
96
  // p._PPRECOMPUTES = undefined;
94
97
  // }
95
98
  // Point on G2 curve (complex numbers): (x₁, x₂+i), (y₁, y₂+i)
96
- const G2_ = weierstrassPoints({ n: Fr.ORDER, ...CURVE.G2 });
99
+ const G2_ = (0, weierstrass_js_1.weierstrassPoints)({ n: Fr.ORDER, ...CURVE.G2 });
97
100
  const G2 = Object.assign(G2_, htf.createHasher(G2_.ProjectivePoint, CURVE.G2.mapToCurve, {
98
101
  ...CURVE.htfDefaults,
99
102
  ...CURVE.G2.htfDefaults,
@@ -119,7 +122,7 @@ export function bls(CURVE) {
119
122
  function normP2Hash(point, htfOpts) {
120
123
  return point instanceof G2.ProjectivePoint
121
124
  ? point
122
- : G2.hashToCurve(ensureBytes('point', point), htfOpts);
125
+ : G2.hashToCurve((0, utils_js_1.ensureBytes)('point', point), htfOpts);
123
126
  }
124
127
  // Multiplies generator by private key.
125
128
  // P = pk x G
@@ -223,4 +226,5 @@ export function bls(CURVE) {
223
226
  utils,
224
227
  };
225
228
  }
229
+ exports.bls = bls;
226
230
  //# sourceMappingURL=bls.js.map