@noble/curves 1.8.1 → 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 (174) hide show
  1. package/README.md +278 -418
  2. package/_shortw_utils.d.ts +2 -2
  3. package/_shortw_utils.js +2 -2
  4. package/abstract/bls.d.ts +5 -5
  5. package/abstract/bls.d.ts.map +1 -1
  6. package/abstract/bls.js +14 -15
  7. package/abstract/bls.js.map +1 -1
  8. package/abstract/curve.d.ts +10 -2
  9. package/abstract/curve.d.ts.map +1 -1
  10. package/abstract/curve.js +81 -78
  11. package/abstract/curve.js.map +1 -1
  12. package/abstract/edwards.d.ts +2 -2
  13. package/abstract/edwards.d.ts.map +1 -1
  14. package/abstract/edwards.js +55 -69
  15. package/abstract/edwards.js.map +1 -1
  16. package/abstract/hash-to-curve.d.ts +5 -4
  17. package/abstract/hash-to-curve.d.ts.map +1 -1
  18. package/abstract/hash-to-curve.js +20 -18
  19. package/abstract/hash-to-curve.js.map +1 -1
  20. package/abstract/modular.d.ts.map +1 -1
  21. package/abstract/modular.js +9 -9
  22. package/abstract/montgomery.js +12 -12
  23. package/abstract/poseidon.d.ts +1 -1
  24. package/abstract/poseidon.js +3 -3
  25. package/abstract/tower.d.ts +2 -2
  26. package/abstract/tower.js +13 -13
  27. package/abstract/utils.d.ts +4 -2
  28. package/abstract/utils.d.ts.map +1 -1
  29. package/abstract/utils.js +25 -14
  30. package/abstract/utils.js.map +1 -1
  31. package/abstract/weierstrass.d.ts +17 -4
  32. package/abstract/weierstrass.d.ts.map +1 -1
  33. package/abstract/weierstrass.js +97 -80
  34. package/abstract/weierstrass.js.map +1 -1
  35. package/bls12-381.d.ts +1 -1
  36. package/bls12-381.js +41 -41
  37. package/bls12-381.js.map +1 -1
  38. package/bn254.d.ts +2 -2
  39. package/bn254.js +29 -29
  40. package/bn254.js.map +1 -1
  41. package/ed25519.d.ts +8 -6
  42. package/ed25519.d.ts.map +1 -1
  43. package/ed25519.js +65 -66
  44. package/ed25519.js.map +1 -1
  45. package/ed448.d.ts +6 -6
  46. package/ed448.d.ts.map +1 -1
  47. package/ed448.js +50 -52
  48. package/ed448.js.map +1 -1
  49. package/esm/_shortw_utils.d.ts +2 -2
  50. package/esm/_shortw_utils.js +1 -1
  51. package/esm/abstract/bls.d.ts +5 -5
  52. package/esm/abstract/bls.d.ts.map +1 -1
  53. package/esm/abstract/bls.js +5 -6
  54. package/esm/abstract/bls.js.map +1 -1
  55. package/esm/abstract/curve.d.ts +10 -2
  56. package/esm/abstract/curve.d.ts.map +1 -1
  57. package/esm/abstract/curve.js +77 -74
  58. package/esm/abstract/curve.js.map +1 -1
  59. package/esm/abstract/edwards.d.ts +2 -2
  60. package/esm/abstract/edwards.d.ts.map +1 -1
  61. package/esm/abstract/edwards.js +36 -50
  62. package/esm/abstract/edwards.js.map +1 -1
  63. package/esm/abstract/hash-to-curve.d.ts +5 -4
  64. package/esm/abstract/hash-to-curve.d.ts.map +1 -1
  65. package/esm/abstract/hash-to-curve.js +4 -2
  66. package/esm/abstract/hash-to-curve.js.map +1 -1
  67. package/esm/abstract/modular.d.ts.map +1 -1
  68. package/esm/abstract/modular.js +1 -1
  69. package/esm/abstract/montgomery.js +2 -2
  70. package/esm/abstract/poseidon.d.ts +1 -1
  71. package/esm/abstract/poseidon.js +1 -1
  72. package/esm/abstract/tower.d.ts +2 -2
  73. package/esm/abstract/tower.js +5 -5
  74. package/esm/abstract/utils.d.ts +4 -2
  75. package/esm/abstract/utils.d.ts.map +1 -1
  76. package/esm/abstract/utils.js +24 -13
  77. package/esm/abstract/utils.js.map +1 -1
  78. package/esm/abstract/weierstrass.d.ts +17 -4
  79. package/esm/abstract/weierstrass.d.ts.map +1 -1
  80. package/esm/abstract/weierstrass.js +70 -53
  81. package/esm/abstract/weierstrass.js.map +1 -1
  82. package/esm/bls12-381.d.ts +1 -1
  83. package/esm/bls12-381.js +9 -9
  84. package/esm/bls12-381.js.map +1 -1
  85. package/esm/bn254.d.ts +2 -2
  86. package/esm/bn254.js +7 -7
  87. package/esm/bn254.js.map +1 -1
  88. package/esm/ed25519.d.ts +8 -6
  89. package/esm/ed25519.d.ts.map +1 -1
  90. package/esm/ed25519.js +20 -21
  91. package/esm/ed25519.js.map +1 -1
  92. package/esm/ed448.d.ts +6 -6
  93. package/esm/ed448.d.ts.map +1 -1
  94. package/esm/ed448.js +13 -15
  95. package/esm/ed448.js.map +1 -1
  96. package/esm/jubjub.d.ts +1 -4
  97. package/esm/jubjub.d.ts.map +1 -1
  98. package/esm/jubjub.js +1 -60
  99. package/esm/jubjub.js.map +1 -1
  100. package/esm/misc.d.ts +15 -0
  101. package/esm/misc.d.ts.map +1 -0
  102. package/esm/misc.js +101 -0
  103. package/esm/misc.js.map +1 -0
  104. package/esm/p256.d.ts +8 -5
  105. package/esm/p256.d.ts.map +1 -1
  106. package/esm/p256.js +13 -12
  107. package/esm/p256.js.map +1 -1
  108. package/esm/p384.d.ts +8 -5
  109. package/esm/p384.d.ts.map +1 -1
  110. package/esm/p384.js +14 -15
  111. package/esm/p384.js.map +1 -1
  112. package/esm/p521.d.ts +6 -5
  113. package/esm/p521.d.ts.map +1 -1
  114. package/esm/p521.js +19 -28
  115. package/esm/p521.js.map +1 -1
  116. package/esm/pasta.d.ts +1 -7
  117. package/esm/pasta.d.ts.map +1 -1
  118. package/esm/pasta.js +1 -33
  119. package/esm/pasta.js.map +1 -1
  120. package/esm/secp256k1.d.ts +15 -10
  121. package/esm/secp256k1.d.ts.map +1 -1
  122. package/esm/secp256k1.js +18 -14
  123. package/esm/secp256k1.js.map +1 -1
  124. package/jubjub.d.ts +1 -4
  125. package/jubjub.d.ts.map +1 -1
  126. package/jubjub.js +5 -63
  127. package/jubjub.js.map +1 -1
  128. package/misc.d.ts +15 -0
  129. package/misc.d.ts.map +1 -0
  130. package/misc.js +106 -0
  131. package/misc.js.map +1 -0
  132. package/p256.d.ts +8 -5
  133. package/p256.d.ts.map +1 -1
  134. package/p256.js +19 -18
  135. package/p256.js.map +1 -1
  136. package/p384.d.ts +8 -5
  137. package/p384.d.ts.map +1 -1
  138. package/p384.js +19 -20
  139. package/p384.js.map +1 -1
  140. package/p521.d.ts +6 -5
  141. package/p521.d.ts.map +1 -1
  142. package/p521.js +23 -32
  143. package/p521.js.map +1 -1
  144. package/package.json +14 -10
  145. package/pasta.d.ts +1 -7
  146. package/pasta.d.ts.map +1 -1
  147. package/pasta.js +4 -34
  148. package/pasta.js.map +1 -1
  149. package/secp256k1.d.ts +15 -10
  150. package/secp256k1.d.ts.map +1 -1
  151. package/secp256k1.js +57 -53
  152. package/secp256k1.js.map +1 -1
  153. package/src/_shortw_utils.ts +2 -2
  154. package/src/abstract/bls.ts +9 -9
  155. package/src/abstract/curve.ts +88 -79
  156. package/src/abstract/edwards.ts +47 -54
  157. package/src/abstract/hash-to-curve.ts +7 -5
  158. package/src/abstract/modular.ts +1 -1
  159. package/src/abstract/montgomery.ts +2 -2
  160. package/src/abstract/poseidon.ts +1 -1
  161. package/src/abstract/tower.ts +6 -6
  162. package/src/abstract/utils.ts +26 -15
  163. package/src/abstract/weierstrass.ts +89 -75
  164. package/src/bls12-381.ts +10 -10
  165. package/src/bn254.ts +8 -8
  166. package/src/ed25519.ts +24 -22
  167. package/src/ed448.ts +18 -17
  168. package/src/jubjub.ts +5 -63
  169. package/src/misc.ts +117 -0
  170. package/src/p256.ts +13 -12
  171. package/src/p384.ts +18 -15
  172. package/src/p521.ts +27 -32
  173. package/src/pasta.ts +1 -39
  174. package/src/secp256k1.ts +19 -15
package/src/ed448.ts CHANGED
@@ -9,17 +9,17 @@
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 type { AffinePoint, Group } from './abstract/curve.js';
13
- import { pippenger } from './abstract/curve.js';
14
- import { type CurveFn, type 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';
15
15
  import {
16
16
  createHasher,
17
17
  expand_message_xof,
18
18
  type htfBasicOpts,
19
19
  type HTFMethod,
20
- } from './abstract/hash-to-curve.js';
21
- import { Field, isNegativeLE, mod, pow2 } from './abstract/modular.js';
22
- import { montgomery, type CurveFn as XCurveFn } from './abstract/montgomery.js';
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,
@@ -27,7 +27,7 @@ import {
27
27
  equalBytes,
28
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/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 { type CurveFn, type 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, type CurveFnWithCreate } from './_shortw_utils.js';
9
- import { createHasher, type 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, type CurveFnWithCreate } from './_shortw_utils.js';
9
- import { createHasher, type 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, type CurveFnWithCreate } from './_shortw_utils.js';
10
- import { createHasher, type 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)();
package/src/pasta.ts CHANGED
@@ -1,39 +1 @@
1
- /**
2
- * Pasta curves. See [Spec](https://o1-labs.github.io/proof-systems/specs/pasta.html).
3
- * @module
4
- */
5
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
6
- import { sha256 } from '@noble/hashes/sha256';
7
- import { getHash } from './_shortw_utils.js';
8
- import { Field, mod } from './abstract/modular.js';
9
- import { type CurveFn, weierstrass } from './abstract/weierstrass.js';
10
-
11
- export const p: bigint = BigInt(
12
- '0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001'
13
- );
14
- export const q: bigint = BigInt(
15
- '0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001'
16
- );
17
-
18
- /** https://neuromancer.sk/std/other/Pallas */
19
- export const pallas: CurveFn = weierstrass({
20
- a: BigInt(0),
21
- b: BigInt(5),
22
- Fp: Field(p),
23
- n: q,
24
- Gx: mod(BigInt(-1), p),
25
- Gy: BigInt(2),
26
- h: BigInt(1),
27
- ...getHash(sha256),
28
- });
29
- /** https://neuromancer.sk/std/other/Vesta */
30
- export const vesta: CurveFn = weierstrass({
31
- a: BigInt(0),
32
- b: BigInt(5),
33
- Fp: Field(q),
34
- n: p,
35
- Gx: mod(BigInt(-1), q),
36
- Gy: BigInt(2),
37
- h: BigInt(1),
38
- ...getHash(sha256),
39
- });
1
+ export { pallas, vesta } from './misc.ts';
package/src/secp256k1.ts CHANGED
@@ -11,12 +11,12 @@
11
11
  * @module
12
12
  */
13
13
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
14
- import { sha256 } from '@noble/hashes/sha256';
14
+ import { sha256 } from '@noble/hashes/sha2';
15
15
  import { randomBytes } from '@noble/hashes/utils';
16
- import { createCurve, type CurveFnWithCreate } from './_shortw_utils.js';
17
- import { createHasher, type HTFMethod, isogenyMap } from './abstract/hash-to-curve.js';
18
- import { Field, mod, pow2 } from './abstract/modular.js';
19
- import type { Hex, PrivKey } from './abstract/utils.js';
16
+ import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
17
+ import { createHasher, type HTFMethod, isogenyMap } from './abstract/hash-to-curve.ts';
18
+ import { Field, mod, pow2 } from './abstract/modular.ts';
19
+ import type { Hex, PrivKey } from './abstract/utils.ts';
20
20
  import {
21
21
  aInRange,
22
22
  bytesToNumberBE,
@@ -24,8 +24,8 @@ import {
24
24
  ensureBytes,
25
25
  inRange,
26
26
  numberToBytesBE,
27
- } from './abstract/utils.js';
28
- import { mapToCurveSimpleSWU, type ProjPointType as PointType } from './abstract/weierstrass.js';
27
+ } from './abstract/utils.ts';
28
+ import { mapToCurveSimpleSWU, type ProjPointType as PointType } from './abstract/weierstrass.ts';
29
29
 
30
30
  const secp256k1P = BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f');
31
31
  const secp256k1N = BigInt('0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141');
@@ -64,24 +64,26 @@ function sqrtMod(y: bigint): bigint {
64
64
  const Fpk1 = Field(secp256k1P, undefined, undefined, { sqrt: sqrtMod });
65
65
 
66
66
  /**
67
- * secp256k1 short weierstrass curve and ECDSA signatures over it.
67
+ * secp256k1 curve, ECDSA and ECDH methods.
68
+ *
69
+ * Field: `2n**256n - 2n**32n - 2n**9n - 2n**8n - 2n**7n - 2n**6n - 2n**4n - 1n`
68
70
  *
69
71
  * @example
72
+ * ```js
70
73
  * import { secp256k1 } from '@noble/curves/secp256k1';
71
- *
72
74
  * const priv = secp256k1.utils.randomPrivateKey();
73
75
  * const pub = secp256k1.getPublicKey(priv);
74
76
  * const msg = new Uint8Array(32).fill(1); // message hash (not message) in ecdsa
75
77
  * const sig = secp256k1.sign(msg, priv); // `{prehash: true}` option is available
76
78
  * const isValid = secp256k1.verify(sig, msg, pub) === true;
79
+ * ```
77
80
  */
78
81
  export const secp256k1: CurveFnWithCreate = createCurve(
79
82
  {
80
- a: BigInt(0), // equation params: a, b
83
+ a: BigInt(0),
81
84
  b: BigInt(7),
82
- Fp: Fpk1, // Field's prime: 2n**256n - 2n**32n - 2n**9n - 2n**8n - 2n**7n - 2n**6n - 2n**4n - 1n
83
- n: secp256k1N, // Curve order, total count of valid points in the field
84
- // Base point (x, y) aka generator point
85
+ Fp: Fpk1,
86
+ n: secp256k1N,
85
87
  Gx: BigInt('55066263022277343669578718895168534326250603453777594175500187360389116729240'),
86
88
  Gy: BigInt('32670510020758816978083085130507043184471273380659243275938904335757337482424'),
87
89
  h: BigInt(1), // Cofactor
@@ -242,12 +244,14 @@ export type SecpSchnorr = {
242
244
  * Schnorr signatures over secp256k1.
243
245
  * https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
244
246
  * @example
247
+ * ```js
245
248
  * import { schnorr } from '@noble/curves/secp256k1';
246
249
  * const priv = schnorr.utils.randomPrivateKey();
247
250
  * const pub = schnorr.getPublicKey(priv);
248
251
  * const msg = new TextEncoder().encode('hello');
249
252
  * const sig = schnorr.sign(msg, priv);
250
253
  * const isValid = schnorr.verify(sig, msg, pub);
254
+ * ```
251
255
  */
252
256
  export const schnorr: SecpSchnorr = /* @__PURE__ */ (() => ({
253
257
  getPublicKey: schnorrGetPublicKey,
@@ -321,8 +325,8 @@ const htf = /* @__PURE__ */ (() =>
321
325
  }
322
326
  ))();
323
327
 
324
- /** secp256k1 hash-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
328
+ /** secp256k1 hash-to-curve from RFC 9380. */
325
329
  export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
326
330
 
327
- /** secp256k1 encode-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
331
+ /** secp256k1 encode-to-curve from RFC 9380. */
328
332
  export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();