@noble/curves 1.8.0 → 1.8.2

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 (191) hide show
  1. package/README.md +282 -419
  2. package/_shortw_utils.d.ts +2 -2
  3. package/_shortw_utils.d.ts.map +1 -1
  4. package/_shortw_utils.js +2 -2
  5. package/_shortw_utils.js.map +1 -1
  6. package/abstract/bls.d.ts +5 -5
  7. package/abstract/bls.d.ts.map +1 -1
  8. package/abstract/bls.js +14 -15
  9. package/abstract/bls.js.map +1 -1
  10. package/abstract/curve.d.ts +10 -2
  11. package/abstract/curve.d.ts.map +1 -1
  12. package/abstract/curve.js +81 -78
  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 +55 -69
  17. package/abstract/edwards.js.map +1 -1
  18. package/abstract/hash-to-curve.d.ts +5 -4
  19. package/abstract/hash-to-curve.d.ts.map +1 -1
  20. package/abstract/hash-to-curve.js +20 -18
  21. package/abstract/hash-to-curve.js.map +1 -1
  22. package/abstract/modular.d.ts.map +1 -1
  23. package/abstract/modular.js +9 -9
  24. package/abstract/montgomery.js +12 -12
  25. package/abstract/poseidon.d.ts +1 -1
  26. package/abstract/poseidon.d.ts.map +1 -1
  27. package/abstract/poseidon.js +3 -3
  28. package/abstract/poseidon.js.map +1 -1
  29. package/abstract/tower.d.ts +2 -2
  30. package/abstract/tower.js +13 -13
  31. package/abstract/utils.d.ts +4 -2
  32. package/abstract/utils.d.ts.map +1 -1
  33. package/abstract/utils.js +25 -14
  34. package/abstract/utils.js.map +1 -1
  35. package/abstract/weierstrass.d.ts +19 -6
  36. package/abstract/weierstrass.d.ts.map +1 -1
  37. package/abstract/weierstrass.js +97 -80
  38. package/abstract/weierstrass.js.map +1 -1
  39. package/bls12-381.d.ts +1 -1
  40. package/bls12-381.d.ts.map +1 -1
  41. package/bls12-381.js +48 -49
  42. package/bls12-381.js.map +1 -1
  43. package/bn254.d.ts +2 -2
  44. package/bn254.d.ts.map +1 -1
  45. package/bn254.js +29 -30
  46. package/bn254.js.map +1 -1
  47. package/ed25519.d.ts +8 -6
  48. package/ed25519.d.ts.map +1 -1
  49. package/ed25519.js +65 -66
  50. package/ed25519.js.map +1 -1
  51. package/ed448.d.ts +6 -6
  52. package/ed448.d.ts.map +1 -1
  53. package/ed448.js +50 -52
  54. package/ed448.js.map +1 -1
  55. package/esm/_shortw_utils.d.ts +2 -2
  56. package/esm/_shortw_utils.d.ts.map +1 -1
  57. package/esm/_shortw_utils.js +1 -1
  58. package/esm/_shortw_utils.js.map +1 -1
  59. package/esm/abstract/bls.d.ts +5 -5
  60. package/esm/abstract/bls.d.ts.map +1 -1
  61. package/esm/abstract/bls.js +5 -6
  62. package/esm/abstract/bls.js.map +1 -1
  63. package/esm/abstract/curve.d.ts +10 -2
  64. package/esm/abstract/curve.d.ts.map +1 -1
  65. package/esm/abstract/curve.js +77 -74
  66. package/esm/abstract/curve.js.map +1 -1
  67. package/esm/abstract/edwards.d.ts +2 -2
  68. package/esm/abstract/edwards.d.ts.map +1 -1
  69. package/esm/abstract/edwards.js +36 -50
  70. package/esm/abstract/edwards.js.map +1 -1
  71. package/esm/abstract/hash-to-curve.d.ts +5 -4
  72. package/esm/abstract/hash-to-curve.d.ts.map +1 -1
  73. package/esm/abstract/hash-to-curve.js +4 -2
  74. package/esm/abstract/hash-to-curve.js.map +1 -1
  75. package/esm/abstract/modular.d.ts.map +1 -1
  76. package/esm/abstract/modular.js +1 -1
  77. package/esm/abstract/montgomery.js +2 -2
  78. package/esm/abstract/poseidon.d.ts +1 -1
  79. package/esm/abstract/poseidon.d.ts.map +1 -1
  80. package/esm/abstract/poseidon.js +1 -1
  81. package/esm/abstract/poseidon.js.map +1 -1
  82. package/esm/abstract/tower.d.ts +2 -2
  83. package/esm/abstract/tower.js +5 -5
  84. package/esm/abstract/utils.d.ts +4 -2
  85. package/esm/abstract/utils.d.ts.map +1 -1
  86. package/esm/abstract/utils.js +24 -13
  87. package/esm/abstract/utils.js.map +1 -1
  88. package/esm/abstract/weierstrass.d.ts +19 -6
  89. package/esm/abstract/weierstrass.d.ts.map +1 -1
  90. package/esm/abstract/weierstrass.js +77 -60
  91. package/esm/abstract/weierstrass.js.map +1 -1
  92. package/esm/bls12-381.d.ts +1 -1
  93. package/esm/bls12-381.d.ts.map +1 -1
  94. package/esm/bls12-381.js +16 -17
  95. package/esm/bls12-381.js.map +1 -1
  96. package/esm/bn254.d.ts +2 -2
  97. package/esm/bn254.d.ts.map +1 -1
  98. package/esm/bn254.js +7 -8
  99. package/esm/bn254.js.map +1 -1
  100. package/esm/ed25519.d.ts +8 -6
  101. package/esm/ed25519.d.ts.map +1 -1
  102. package/esm/ed25519.js +20 -21
  103. package/esm/ed25519.js.map +1 -1
  104. package/esm/ed448.d.ts +6 -6
  105. package/esm/ed448.d.ts.map +1 -1
  106. package/esm/ed448.js +13 -15
  107. package/esm/ed448.js.map +1 -1
  108. package/esm/index.js +13 -1
  109. package/esm/index.js.map +1 -1
  110. package/esm/jubjub.d.ts +1 -4
  111. package/esm/jubjub.d.ts.map +1 -1
  112. package/esm/jubjub.js +1 -60
  113. package/esm/jubjub.js.map +1 -1
  114. package/esm/misc.d.ts +15 -0
  115. package/esm/misc.d.ts.map +1 -0
  116. package/esm/misc.js +101 -0
  117. package/esm/misc.js.map +1 -0
  118. package/esm/p256.d.ts +8 -5
  119. package/esm/p256.d.ts.map +1 -1
  120. package/esm/p256.js +13 -12
  121. package/esm/p256.js.map +1 -1
  122. package/esm/p384.d.ts +8 -5
  123. package/esm/p384.d.ts.map +1 -1
  124. package/esm/p384.js +14 -15
  125. package/esm/p384.js.map +1 -1
  126. package/esm/p521.d.ts +6 -5
  127. package/esm/p521.d.ts.map +1 -1
  128. package/esm/p521.js +19 -28
  129. package/esm/p521.js.map +1 -1
  130. package/esm/pasta.d.ts +1 -7
  131. package/esm/pasta.d.ts.map +1 -1
  132. package/esm/pasta.js +1 -33
  133. package/esm/pasta.js.map +1 -1
  134. package/esm/secp256k1.d.ts +15 -10
  135. package/esm/secp256k1.d.ts.map +1 -1
  136. package/esm/secp256k1.js +18 -14
  137. package/esm/secp256k1.js.map +1 -1
  138. package/index.js +13 -1
  139. package/index.js.map +1 -1
  140. package/jubjub.d.ts +1 -4
  141. package/jubjub.d.ts.map +1 -1
  142. package/jubjub.js +5 -63
  143. package/jubjub.js.map +1 -1
  144. package/misc.d.ts +15 -0
  145. package/misc.d.ts.map +1 -0
  146. package/misc.js +106 -0
  147. package/misc.js.map +1 -0
  148. package/p256.d.ts +8 -5
  149. package/p256.d.ts.map +1 -1
  150. package/p256.js +19 -18
  151. package/p256.js.map +1 -1
  152. package/p384.d.ts +8 -5
  153. package/p384.d.ts.map +1 -1
  154. package/p384.js +19 -20
  155. package/p384.js.map +1 -1
  156. package/p521.d.ts +6 -5
  157. package/p521.d.ts.map +1 -1
  158. package/p521.js +23 -32
  159. package/p521.js.map +1 -1
  160. package/package.json +21 -16
  161. package/pasta.d.ts +1 -7
  162. package/pasta.d.ts.map +1 -1
  163. package/pasta.js +4 -34
  164. package/pasta.js.map +1 -1
  165. package/secp256k1.d.ts +15 -10
  166. package/secp256k1.d.ts.map +1 -1
  167. package/secp256k1.js +57 -53
  168. package/secp256k1.js.map +1 -1
  169. package/src/_shortw_utils.ts +2 -2
  170. package/src/abstract/bls.ts +14 -12
  171. package/src/abstract/curve.ts +88 -79
  172. package/src/abstract/edwards.ts +52 -59
  173. package/src/abstract/hash-to-curve.ts +7 -5
  174. package/src/abstract/modular.ts +1 -1
  175. package/src/abstract/montgomery.ts +2 -2
  176. package/src/abstract/poseidon.ts +1 -1
  177. package/src/abstract/tower.ts +6 -6
  178. package/src/abstract/utils.ts +26 -15
  179. package/src/abstract/weierstrass.ts +99 -77
  180. package/src/bls12-381.ts +30 -28
  181. package/src/bn254.ts +11 -13
  182. package/src/ed25519.ts +27 -26
  183. package/src/ed448.ts +21 -20
  184. package/src/index.ts +13 -1
  185. package/src/jubjub.ts +5 -63
  186. package/src/misc.ts +117 -0
  187. package/src/p256.ts +13 -12
  188. package/src/p384.ts +18 -15
  189. package/src/p521.ts +27 -32
  190. package/src/pasta.ts +1 -39
  191. package/src/secp256k1.ts +20 -16
package/src/ed25519.ts CHANGED
@@ -6,32 +6,33 @@
6
6
  * @module
7
7
  */
8
8
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
9
- import { sha512 } from '@noble/hashes/sha512';
9
+ import { sha512 } from '@noble/hashes/sha2';
10
10
  import { concatBytes, randomBytes, utf8ToBytes } from '@noble/hashes/utils';
11
- import { AffinePoint, Group } from './abstract/curve.js';
12
- import { CurveFn, ExtPointType, twistedEdwards } from './abstract/edwards.js';
11
+ import { type AffinePoint, type Group, pippenger } from './abstract/curve.ts';
12
+ import { type CurveFn, type ExtPointType, twistedEdwards } from './abstract/edwards.ts';
13
13
  import {
14
14
  createHasher,
15
15
  expand_message_xmd,
16
- htfBasicOpts,
17
- HTFMethod,
18
- } from './abstract/hash-to-curve.js';
19
- import { Field, FpSqrtEven, isNegativeLE, mod, pow2 } from './abstract/modular.js';
20
- import { CurveFn as XCurveFn, montgomery } from './abstract/montgomery.js';
21
- import { pippenger } from './abstract/curve.js';
16
+ type htfBasicOpts,
17
+ type HTFMethod,
18
+ } from './abstract/hash-to-curve.ts';
19
+ import { Field, FpSqrtEven, isNegativeLE, mod, pow2 } from './abstract/modular.ts';
20
+ import { montgomery, type CurveFn as XCurveFn } from './abstract/montgomery.ts';
22
21
  import {
23
22
  bytesToHex,
24
23
  bytesToNumberLE,
25
24
  ensureBytes,
26
25
  equalBytes,
27
- Hex,
26
+ type Hex,
28
27
  numberToBytesLE,
29
- } from './abstract/utils.js';
28
+ } from './abstract/utils.ts';
30
29
 
30
+ // 2n**255n - 19n
31
31
  const ED25519_P = BigInt(
32
32
  '57896044618658097711785492504343953926634992332820282019728792003956564819949'
33
33
  );
34
34
  // √(-1) aka √(a) aka 2^((p-1)/4)
35
+ // Fp.sqrt(Fp.neg(1))
35
36
  const ED25519_SQRT_M1 = /* @__PURE__ */ BigInt(
36
37
  '19681161376707505956807079304988542015446066515923890162744021073123829784752'
37
38
  );
@@ -92,7 +93,7 @@ function uvRatio(u: bigint, v: bigint): { isValid: boolean; value: bigint } {
92
93
  return { isValid: useRoot1 || useRoot2, value: x };
93
94
  }
94
95
 
95
- // Just in case
96
+ /** Weird / bogus points, useful for debugging. */
96
97
  export const ED25519_TORSION_SUBGROUP: string[] = [
97
98
  '0100000000000000000000000000000000000000000000000000000000000000',
98
99
  'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a',
@@ -108,19 +109,15 @@ const Fp = /* @__PURE__ */ (() => Field(ED25519_P, undefined, true))();
108
109
 
109
110
  const ed25519Defaults = /* @__PURE__ */ (() =>
110
111
  ({
111
- // Param: a
112
- a: BigInt(-1), // Fp.create(-1) is proper; our way still works and is faster
113
- // d is equal to -121665/121666 over finite field.
114
- // Negative number is P - number, and division is invert(number, P)
112
+ // Removing Fp.create() will still work, and is 10% faster on sign
113
+ a: Fp.create(BigInt(-1)),
114
+ // d is -121665/121666 a.k.a. Fp.neg(121665 * Fp.inv(121666))
115
115
  d: BigInt('37095705934669439343138083508754565189542113879843219016388785533085940283555'),
116
- // Finite field 𝔽p over which we'll do calculations; 2n**255n - 19n
116
+ // Finite field 2n**255n - 19n
117
117
  Fp,
118
- // Subgroup order: how many points curve has
119
- // 2n**252n + 27742317777372353535851937790883648493n;
118
+ // Subgroup order 2n**252n + 27742317777372353535851937790883648493n;
120
119
  n: BigInt('7237005577332262213973186563042994240857116359379907606001950938285454250989'),
121
- // Cofactor
122
120
  h: _8n,
123
- // Base point (x, y) aka generator point
124
121
  Gx: BigInt('15112221349535400772501151409588531511454012693041857206046113283949847762202'),
125
122
  Gy: BigInt('46316835694926478169428394003475163141307993866256225615783033603165251855960'),
126
123
  hash: sha512,
@@ -314,7 +311,7 @@ const htf = /* @__PURE__ */ (() =>
314
311
  export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
315
312
  export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();
316
313
 
317
- function assertRstPoint(other: unknown) {
314
+ function aristp(other: unknown) {
318
315
  if (!(other instanceof RistPoint)) throw new Error('RistrettoPoint expected');
319
316
  }
320
317
 
@@ -381,9 +378,12 @@ function calcElligatorRistrettoMap(r0: bigint): ExtendedPoint {
381
378
  class RistPoint implements Group<RistPoint> {
382
379
  static BASE: RistPoint;
383
380
  static ZERO: RistPoint;
381
+ private readonly ep: ExtendedPoint;
384
382
  // Private property to discourage combining ExtendedPoint + RistrettoPoint
385
383
  // Always use Ristretto encoding/decoding instead.
386
- constructor(private readonly ep: ExtendedPoint) {}
384
+ constructor(ep: ExtendedPoint) {
385
+ this.ep = ep;
386
+ }
387
387
 
388
388
  static fromAffine(ap: AffinePoint<bigint>): RistPoint {
389
389
  return new RistPoint(ed25519.ExtendedPoint.fromAffine(ap));
@@ -484,7 +484,7 @@ class RistPoint implements Group<RistPoint> {
484
484
 
485
485
  // Compare one point to another.
486
486
  equals(other: RistPoint): boolean {
487
- assertRstPoint(other);
487
+ aristp(other);
488
488
  const { ex: X1, ey: Y1 } = this.ep;
489
489
  const { ex: X2, ey: Y2 } = other.ep;
490
490
  const mod = ed25519.CURVE.Fp.create;
@@ -495,12 +495,12 @@ class RistPoint implements Group<RistPoint> {
495
495
  }
496
496
 
497
497
  add(other: RistPoint): RistPoint {
498
- assertRstPoint(other);
498
+ aristp(other);
499
499
  return new RistPoint(this.ep.add(other.ep));
500
500
  }
501
501
 
502
502
  subtract(other: RistPoint): RistPoint {
503
- assertRstPoint(other);
503
+ aristp(other);
504
504
  return new RistPoint(this.ep.subtract(other.ep));
505
505
  }
506
506
 
@@ -534,5 +534,6 @@ export const hashToRistretto255 = (msg: Uint8Array, options: htfBasicOpts): Rist
534
534
  const P = RistPoint.hashToCurve(uniform_bytes);
535
535
  return P;
536
536
  };
537
+ /** @deprecated */
537
538
  export const hash_to_ristretto255: (msg: Uint8Array, options: htfBasicOpts) => RistPoint =
538
539
  hashToRistretto255; // legacy
package/src/ed448.ts CHANGED
@@ -9,25 +9,25 @@
9
9
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
10
10
  import { shake256 } from '@noble/hashes/sha3';
11
11
  import { concatBytes, randomBytes, utf8ToBytes, wrapConstructor } from '@noble/hashes/utils';
12
- import { AffinePoint, Group } from './abstract/curve.js';
13
- import { CurveFn, ExtPointType, twistedEdwards } from './abstract/edwards.js';
12
+ import type { AffinePoint, Group } from './abstract/curve.ts';
13
+ import { pippenger } from './abstract/curve.ts';
14
+ import { type CurveFn, type ExtPointType, twistedEdwards } from './abstract/edwards.ts';
14
15
  import {
15
16
  createHasher,
16
17
  expand_message_xof,
17
- htfBasicOpts,
18
- HTFMethod,
19
- } from './abstract/hash-to-curve.js';
20
- import { Field, isNegativeLE, mod, pow2 } from './abstract/modular.js';
21
- import { CurveFn as XCurveFn, montgomery } from './abstract/montgomery.js';
22
- import { pippenger } from './abstract/curve.js';
18
+ type htfBasicOpts,
19
+ type HTFMethod,
20
+ } from './abstract/hash-to-curve.ts';
21
+ import { Field, isNegativeLE, mod, pow2 } from './abstract/modular.ts';
22
+ import { montgomery, type CurveFn as XCurveFn } from './abstract/montgomery.ts';
23
23
  import {
24
24
  bytesToHex,
25
25
  bytesToNumberLE,
26
26
  ensureBytes,
27
27
  equalBytes,
28
- Hex,
28
+ type Hex,
29
29
  numberToBytesLE,
30
- } from './abstract/utils.js';
30
+ } from './abstract/utils.ts';
31
31
 
32
32
  const shake256_114 = wrapConstructor(() => shake256.create({ dkLen: 114 }));
33
33
  const shake256_64 = wrapConstructor(() => shake256.create({ dkLen: 64 }));
@@ -98,22 +98,20 @@ const Fp = Field(ed448P, 456, true);
98
98
  const ED448_DEF = {
99
99
  // Param: a
100
100
  a: BigInt(1),
101
- // -39081. Negative number is P - number
101
+ // -39081 a.k.a. Fp.neg(39081)
102
102
  d: BigInt(
103
103
  '726838724295606890549323807888004534353641360687318060281490199180612328166730772686396383698676545930088884461843637361053498018326358'
104
104
  ),
105
- // Finite field 𝔽p over which we'll do calculations; 2n**448n - 2n**224n - 1n
105
+ // Finite field 2n**448n - 2n**224n - 1n
106
106
  Fp,
107
- // Subgroup order: how many points curve has;
107
+ // Subgroup order
108
108
  // 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n
109
109
  n: BigInt(
110
110
  '181709681073901722637330951972001133588410340171829515070372549795146003961539585716195755291692375963310293709091662304773755859649779'
111
111
  ),
112
112
  // RFC 7748 has 56-byte keys, RFC 8032 has 57-byte keys
113
113
  nBitLength: 456,
114
- // Cofactor
115
114
  h: BigInt(4),
116
- // Base point (x, y) aka generator point
117
115
  Gx: BigInt(
118
116
  '224580040295924300187604334099896036246789641632564134246125461686950415467406032909029192869357953282578032075146446173674602635247710'
119
117
  ),
@@ -286,7 +284,7 @@ const htf = /* @__PURE__ */ (() =>
286
284
  export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
287
285
  export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();
288
286
 
289
- function assertDcfPoint(other: unknown) {
287
+ function adecafp(other: unknown) {
290
288
  if (!(other instanceof DcfPoint)) throw new Error('DecafPoint expected');
291
289
  }
292
290
 
@@ -354,9 +352,12 @@ function calcElligatorDecafMap(r0: bigint): ExtendedPoint {
354
352
  class DcfPoint implements Group<DcfPoint> {
355
353
  static BASE: DcfPoint;
356
354
  static ZERO: DcfPoint;
355
+ private readonly ep: ExtendedPoint;
357
356
  // Private property to discourage combining ExtendedPoint + DecafPoint
358
357
  // Always use Decaf encoding/decoding instead.
359
- constructor(private readonly ep: ExtendedPoint) {}
358
+ constructor(ep: ExtendedPoint) {
359
+ this.ep = ep;
360
+ }
360
361
 
361
362
  static fromAffine(ap: AffinePoint<bigint>): DcfPoint {
362
363
  return new DcfPoint(ed448.ExtendedPoint.fromAffine(ap));
@@ -453,7 +454,7 @@ class DcfPoint implements Group<DcfPoint> {
453
454
  // Compare one point to another.
454
455
  // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-ristretto255-decaf448-07#name-equals-2
455
456
  equals(other: DcfPoint): boolean {
456
- assertDcfPoint(other);
457
+ adecafp(other);
457
458
  const { ex: X1, ey: Y1 } = this.ep;
458
459
  const { ex: X2, ey: Y2 } = other.ep;
459
460
  const mod = ed448.CURVE.Fp.create;
@@ -462,12 +463,12 @@ class DcfPoint implements Group<DcfPoint> {
462
463
  }
463
464
 
464
465
  add(other: DcfPoint): DcfPoint {
465
- assertDcfPoint(other);
466
+ adecafp(other);
466
467
  return new DcfPoint(this.ep.add(other.ep));
467
468
  }
468
469
 
469
470
  subtract(other: DcfPoint): DcfPoint {
470
- assertDcfPoint(other);
471
+ adecafp(other);
471
472
  return new DcfPoint(this.ep.subtract(other.ep));
472
473
  }
473
474
 
package/src/index.ts CHANGED
@@ -1,5 +1,17 @@
1
1
  /**
2
- * Audited & minimal JS implementation of elliptic curve cryptography. Check out individual modules.
2
+ * Audited & minimal JS implementation of elliptic curve cryptography.
3
3
  * @module
4
+ * @example
5
+ ```js
6
+ import { secp256k1, schnorr } from '@noble/curves/secp256k1';
7
+ import { ed25519, ed25519ph, ed25519ctx, x25519, RistrettoPoint } from '@noble/curves/ed25519';
8
+ import { ed448, ed448ph, ed448ctx, x448 } from '@noble/curves/ed448';
9
+ import { p256 } from '@noble/curves/p256';
10
+ import { p384 } from '@noble/curves/p384';
11
+ import { p521 } from '@noble/curves/p521';
12
+ import { bls12_381 } from '@noble/curves/bls12-381';
13
+ import { bn254 } from '@noble/curves/bn254';
14
+ import { bytesToHex, hexToBytes, concatBytes, utf8ToBytes } from '@noble/curves/abstract/utils';
15
+ ```
4
16
  */
5
17
  throw new Error('root module cannot be imported: import submodules instead. Check out README');
package/src/jubjub.ts CHANGED
@@ -1,63 +1,5 @@
1
- /**
2
- * jubjub Twisted Edwards curve.
3
- * https://neuromancer.sk/std/other/JubJub
4
- * jubjub does not use EdDSA, so `hash`/sha512 params are passed because interface expects them.
5
- * @module
6
- */
7
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
8
- import { blake2s } from '@noble/hashes/blake2s';
9
- import { sha512 } from '@noble/hashes/sha512';
10
- import { concatBytes, randomBytes, utf8ToBytes } from '@noble/hashes/utils';
11
- import { CurveFn, ExtPointType, twistedEdwards } from './abstract/edwards.js';
12
- import { Field } from './abstract/modular.js';
13
-
14
- export const jubjub: CurveFn = /* @__PURE__ */ twistedEdwards({
15
- // Params: a, d
16
- a: BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000'),
17
- d: BigInt('0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1'),
18
- // Finite field 𝔽p over which we'll do calculations
19
- // Same value as bls12-381 Fr (not Fp)
20
- Fp: Field(BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001')),
21
- // Subgroup order: how many points curve has
22
- n: BigInt('0xe7db4ea6533afa906673b0101343b00a6682093ccc81082d0970e5ed6f72cb7'),
23
- // Cofactor
24
- h: BigInt(8),
25
- // Base point (x, y) aka generator point
26
- Gx: BigInt('0x11dafe5d23e1218086a365b99fbf3d3be72f6afd7d1f72623e6b071492d1122b'),
27
- Gy: BigInt('0x1d523cf1ddab1a1793132e78c866c0c33e26ba5cc220fed7cc3f870e59d292aa'),
28
- hash: sha512,
29
- randomBytes,
30
- } as const);
31
-
32
- const GH_FIRST_BLOCK = utf8ToBytes(
33
- '096b36a5804bfacef1691e173c366a47ff5ba84a44f26ddd7e8d9f79d5b42df0'
34
- );
35
-
36
- // Returns point at JubJub curve which is prime order and not zero
37
- export function groupHash(tag: Uint8Array, personalization: Uint8Array): ExtPointType {
38
- const h = blake2s.create({ personalization, dkLen: 32 });
39
- h.update(GH_FIRST_BLOCK);
40
- h.update(tag);
41
- // NOTE: returns ExtendedPoint, in case it will be multiplied later
42
- let p = jubjub.ExtendedPoint.fromHex(h.digest());
43
- // NOTE: cannot replace with isSmallOrder, returns Point*8
44
- p = p.multiply(jubjub.CURVE.h);
45
- if (p.equals(jubjub.ExtendedPoint.ZERO)) throw new Error('Point has small order');
46
- return p;
47
- }
48
-
49
- // No secret data is leaked here at all.
50
- // It operates over public data:
51
- // const G_SPEND = jubjub.findGroupHash(new Uint8Array(), utf8ToBytes('Item_G_'));
52
- export function findGroupHash(m: Uint8Array, personalization: Uint8Array): ExtPointType {
53
- const tag = concatBytes(m, new Uint8Array([0]));
54
- const hashes = [];
55
- for (let i = 0; i < 256; i++) {
56
- tag[tag.length - 1] = i;
57
- try {
58
- hashes.push(groupHash(tag, personalization));
59
- } catch (e) {}
60
- }
61
- if (!hashes.length) throw new Error('findGroupHash tag overflow');
62
- return hashes[0];
63
- }
1
+ export {
2
+ jubjub_findGroupHash as findGroupHash,
3
+ jubjub_groupHash as groupHash,
4
+ jubjub,
5
+ } from './misc.ts';
package/src/misc.ts ADDED
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Miscellaneous, rarely used curves.
3
+ * jubjub, babyjubjub, pallas, vesta.
4
+ * @module
5
+ */
6
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
7
+ import { blake256 } from '@noble/hashes/blake1';
8
+ import { blake2s } from '@noble/hashes/blake2s';
9
+ import { sha256, sha512 } from '@noble/hashes/sha2';
10
+ import { concatBytes, randomBytes, utf8ToBytes } from '@noble/hashes/utils';
11
+ import { getHash } from './_shortw_utils.ts';
12
+ import { type CurveFn, type ExtPointType, twistedEdwards } from './abstract/edwards.ts';
13
+ import { Field, mod } from './abstract/modular.ts';
14
+ import { type CurveFn as WCurveFn, weierstrass } from './abstract/weierstrass.ts';
15
+
16
+ // Jubjub curves have 𝔽p over scalar fields of other curves. They are friendly to ZK proofs.
17
+ // jubjub Fp = bls n. babyjubjub Fp = bn254 n.
18
+ // verify manually, check bls12-381.ts and bn254.ts.
19
+ // https://neuromancer.sk/std/other/JubJub
20
+
21
+ const bls12_381_Fr = Field(
22
+ BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001')
23
+ );
24
+ const bn254_Fr = Field(
25
+ BigInt('21888242871839275222246405745257275088548364400416034343698204186575808495617')
26
+ );
27
+
28
+ /** Curve over scalar field of bls12-381. jubjub Fp = bls n */
29
+ export const jubjub: CurveFn = /* @__PURE__ */ twistedEdwards({
30
+ a: BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000'),
31
+ d: BigInt('0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1'),
32
+ Fp: bls12_381_Fr,
33
+ n: BigInt('0xe7db4ea6533afa906673b0101343b00a6682093ccc81082d0970e5ed6f72cb7'),
34
+ h: BigInt(8),
35
+ Gx: BigInt('0x11dafe5d23e1218086a365b99fbf3d3be72f6afd7d1f72623e6b071492d1122b'),
36
+ Gy: BigInt('0x1d523cf1ddab1a1793132e78c866c0c33e26ba5cc220fed7cc3f870e59d292aa'),
37
+ hash: sha512,
38
+ randomBytes,
39
+ } as const);
40
+
41
+ /** Curve over scalar field of bn254. babyjubjub Fp = bn254 n */
42
+ export const babyjubjub: CurveFn = /* @__PURE__ */ twistedEdwards({
43
+ a: BigInt(168700),
44
+ d: BigInt(168696),
45
+ Fp: bn254_Fr,
46
+ n: BigInt('21888242871839275222246405745257275088614511777268538073601725287587578984328'),
47
+ h: BigInt(8),
48
+ Gx: BigInt('995203441582195749578291179787384436505546430278305826713579947235728471134'),
49
+ Gy: BigInt('5472060717959818805561601436314318772137091100104008585924551046643952123905'),
50
+ hash: blake256,
51
+ randomBytes,
52
+ } as const);
53
+
54
+ const jubjub_gh_first_block = utf8ToBytes(
55
+ '096b36a5804bfacef1691e173c366a47ff5ba84a44f26ddd7e8d9f79d5b42df0'
56
+ );
57
+
58
+ // Returns point at JubJub curve which is prime order and not zero
59
+ export function jubjub_groupHash(tag: Uint8Array, personalization: Uint8Array): ExtPointType {
60
+ const h = blake2s.create({ personalization, dkLen: 32 });
61
+ h.update(jubjub_gh_first_block);
62
+ h.update(tag);
63
+ // NOTE: returns ExtendedPoint, in case it will be multiplied later
64
+ let p = jubjub.ExtendedPoint.fromHex(h.digest());
65
+ // NOTE: cannot replace with isSmallOrder, returns Point*8
66
+ p = p.multiply(jubjub.CURVE.h);
67
+ if (p.equals(jubjub.ExtendedPoint.ZERO)) throw new Error('Point has small order');
68
+ return p;
69
+ }
70
+
71
+ // No secret data is leaked here at all.
72
+ // It operates over public data:
73
+ // const G_SPEND = jubjub.findGroupHash(new Uint8Array(), utf8ToBytes('Item_G_'));
74
+ export function jubjub_findGroupHash(m: Uint8Array, personalization: Uint8Array): ExtPointType {
75
+ const tag = concatBytes(m, new Uint8Array([0]));
76
+ const hashes = [];
77
+ for (let i = 0; i < 256; i++) {
78
+ tag[tag.length - 1] = i;
79
+ try {
80
+ hashes.push(jubjub_groupHash(tag, personalization));
81
+ } catch (e) {}
82
+ }
83
+ if (!hashes.length) throw new Error('findGroupHash tag overflow');
84
+ return hashes[0];
85
+ }
86
+
87
+ // Pasta curves. See [Spec](https://o1-labs.github.io/proof-systems/specs/pasta.html).
88
+
89
+ export const pasta_p: bigint = BigInt(
90
+ '0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001'
91
+ );
92
+ export const pasta_q: bigint = BigInt(
93
+ '0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001'
94
+ );
95
+
96
+ /** https://neuromancer.sk/std/other/Pallas */
97
+ export const pallas: WCurveFn = weierstrass({
98
+ a: BigInt(0),
99
+ b: BigInt(5),
100
+ Fp: Field(pasta_p),
101
+ n: pasta_q,
102
+ Gx: mod(BigInt(-1), pasta_p),
103
+ Gy: BigInt(2),
104
+ h: BigInt(1),
105
+ ...getHash(sha256),
106
+ });
107
+ /** https://neuromancer.sk/std/other/Vesta */
108
+ export const vesta: WCurveFn = weierstrass({
109
+ a: BigInt(0),
110
+ b: BigInt(5),
111
+ Fp: Field(pasta_q),
112
+ n: pasta_p,
113
+ Gx: mod(BigInt(-1), pasta_q),
114
+ Gy: BigInt(2),
115
+ h: BigInt(1),
116
+ ...getHash(sha256),
117
+ });
package/src/p256.ts CHANGED
@@ -4,25 +4,26 @@
4
4
  * @module
5
5
  */
6
6
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
7
- import { sha256 } from '@noble/hashes/sha256';
8
- import { createCurve, CurveFnWithCreate } from './_shortw_utils.js';
9
- import { createHasher, HTFMethod } from './abstract/hash-to-curve.js';
10
- import { Field } from './abstract/modular.js';
11
- import { mapToCurveSimpleSWU } from './abstract/weierstrass.js';
7
+ import { sha256 } from '@noble/hashes/sha2';
8
+ import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
9
+ import { createHasher, type HTFMethod } from './abstract/hash-to-curve.ts';
10
+ import { Field } from './abstract/modular.ts';
11
+ import { mapToCurveSimpleSWU } from './abstract/weierstrass.ts';
12
12
 
13
13
  const Fp256 = Field(BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'));
14
14
  const CURVE_A = Fp256.create(BigInt('-3'));
15
15
  const CURVE_B = BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b');
16
16
 
17
- /** secp256r1 curve, ECDSA and ECDH methods. */
17
+ /**
18
+ * secp256r1 curve, ECDSA and ECDH methods.
19
+ * Field: `2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n-1n`
20
+ */
18
21
  // prettier-ignore
19
22
  export const p256: CurveFnWithCreate = createCurve({
20
- a: CURVE_A, // Equation params: a, b
23
+ a: CURVE_A,
21
24
  b: CURVE_B,
22
- Fp: Fp256, // Field: 2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n-1n
23
- // Curve order, total count of valid points in the field
25
+ Fp: Fp256,
24
26
  n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
25
- // Base (generator) point (x, y)
26
27
  Gx: BigInt('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'),
27
28
  Gy: BigInt('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'),
28
29
  h: BigInt(1),
@@ -48,7 +49,7 @@ const htf = /* @__PURE__ */ (() =>
48
49
  expand: 'xmd',
49
50
  hash: sha256,
50
51
  }))();
51
- /** secp256r1 hash-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
52
+ /** secp256r1 hash-to-curve from RFC 9380. */
52
53
  export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
53
- /** secp256r1 encode-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
54
+ /** secp256r1 encode-to-curve from RFC 9380. */
54
55
  export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();
package/src/p384.ts CHANGED
@@ -4,29 +4,32 @@
4
4
  * @module
5
5
  */
6
6
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
7
- import { sha384 } from '@noble/hashes/sha512';
8
- import { createCurve, CurveFnWithCreate } from './_shortw_utils.js';
9
- import { createHasher, HTFMethod } from './abstract/hash-to-curve.js';
10
- import { Field } from './abstract/modular.js';
11
- import { mapToCurveSimpleSWU } from './abstract/weierstrass.js';
7
+ import { sha384 } from '@noble/hashes/sha2';
8
+ import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
9
+ import { createHasher, type HTFMethod } from './abstract/hash-to-curve.ts';
10
+ import { Field } from './abstract/modular.ts';
11
+ import { mapToCurveSimpleSWU } from './abstract/weierstrass.ts';
12
12
 
13
13
  // Field over which we'll do calculations.
14
- // prettier-ignore
15
- const P = BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff');
16
- const Fp384 = Field(P);
14
+ const Fp384 = Field(
15
+ BigInt(
16
+ '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff'
17
+ )
18
+ );
17
19
  const CURVE_A = Fp384.create(BigInt('-3'));
18
20
  // prettier-ignore
19
21
  const CURVE_B = BigInt('0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef');
20
22
 
21
- /** secp384r1 curve, ECDSA and ECDH methods. */
23
+ /**
24
+ * secp384r1 curve, ECDSA and ECDH methods.
25
+ * Field: `2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n`.
26
+ * */
22
27
  // prettier-ignore
23
28
  export const p384: CurveFnWithCreate = createCurve({
24
- a: CURVE_A, // Equation params: a, b
29
+ a: CURVE_A,
25
30
  b: CURVE_B,
26
- Fp: Fp384, // Field: 2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n
27
- // Curve order, total count of valid points in the field.
31
+ Fp: Fp384,
28
32
  n: BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973'),
29
- // Base (generator) point (x, y)
30
33
  Gx: BigInt('0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7'),
31
34
  Gy: BigInt('0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f'),
32
35
  h: BigInt(1),
@@ -52,7 +55,7 @@ const htf = /* @__PURE__ */ (() =>
52
55
  expand: 'xmd',
53
56
  hash: sha384,
54
57
  }))();
55
- /** secp384r1 hash-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
58
+ /** secp384r1 hash-to-curve from RFC 9380. */
56
59
  export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
57
- /** secp384r1 encode-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
60
+ /** secp384r1 encode-to-curve from RFC 9380. */
58
61
  export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();
package/src/p521.ts CHANGED
@@ -5,22 +5,32 @@
5
5
  * @module
6
6
  */
7
7
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
8
- import { sha512 } from '@noble/hashes/sha512';
9
- import { createCurve, CurveFnWithCreate } from './_shortw_utils.js';
10
- import { createHasher, HTFMethod } from './abstract/hash-to-curve.js';
11
- import { Field } from './abstract/modular.js';
12
- import { mapToCurveSimpleSWU } from './abstract/weierstrass.js';
8
+ import { sha512 } from '@noble/hashes/sha2';
9
+ import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
10
+ import { createHasher, type HTFMethod } from './abstract/hash-to-curve.ts';
11
+ import { Field } from './abstract/modular.ts';
12
+ import { mapToCurveSimpleSWU } from './abstract/weierstrass.ts';
13
13
 
14
14
  // Field over which we'll do calculations.
15
- // prettier-ignore
16
- const P = BigInt('0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff');
17
- const Fp521 = Field(P);
15
+ const Fp521 = Field(
16
+ BigInt(
17
+ '0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
18
+ )
19
+ );
18
20
 
19
- const CURVE = {
20
- a: Fp521.create(BigInt('-3')),
21
- b: BigInt(
22
- '0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00'
23
- ),
21
+ const CURVE_A = Fp521.create(BigInt('-3'));
22
+ const CURVE_B = BigInt(
23
+ '0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00'
24
+ );
25
+
26
+ /**
27
+ * NIST secp521r1 aka p521 curve, ECDSA and ECDH methods.
28
+ * Field: `2n**521n - 1n`.
29
+ */
30
+ // prettier-ignore
31
+ export const p521: CurveFnWithCreate = createCurve({
32
+ a: CURVE_A,
33
+ b: CURVE_B,
24
34
  Fp: Fp521,
25
35
  n: BigInt(
26
36
  '0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409'
@@ -32,21 +42,6 @@ const CURVE = {
32
42
  '0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'
33
43
  ),
34
44
  h: BigInt(1),
35
- };
36
-
37
- /**
38
- * NIST secp521r1 aka p521.
39
- */
40
- // prettier-ignore
41
- export const p521: CurveFnWithCreate = createCurve({
42
- a: CURVE.a, // Equation params: a, b
43
- b: CURVE.b,
44
- Fp: Fp521, // Field: 2n**521n - 1n
45
- // Curve order, total count of valid points in the field
46
- n: CURVE.n,
47
- Gx: CURVE.Gx, // Base point (x, y) aka generator point
48
- Gy: CURVE.Gy,
49
- h: CURVE.h,
50
45
  lowS: false,
51
46
  allowedPrivateKeyLengths: [130, 131, 132] // P521 keys are variable-length. Normalize to 132b
52
47
  } as const, sha512);
@@ -54,8 +49,8 @@ export const secp521r1: CurveFnWithCreate = p521;
54
49
 
55
50
  const mapSWU = /* @__PURE__ */ (() =>
56
51
  mapToCurveSimpleSWU(Fp521, {
57
- A: CURVE.a,
58
- B: CURVE.b,
52
+ A: CURVE_A,
53
+ B: CURVE_B,
59
54
  Z: Fp521.create(BigInt('-4')),
60
55
  }))();
61
56
 
@@ -69,7 +64,7 @@ const htf = /* @__PURE__ */ (() =>
69
64
  expand: 'xmd',
70
65
  hash: sha512,
71
66
  }))();
72
- /** secp521r1 hash-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
67
+ /** secp521r1 hash-to-curve from RFC 9380. */
73
68
  export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
74
- /** secp521r1 encode-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
69
+ /** secp521r1 encode-to-curve from RFC 9380. */
75
70
  export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();