@noble/curves 1.8.1 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/README.md +305 -433
  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 +15 -16
  7. package/abstract/bls.js.map +1 -1
  8. package/abstract/curve.d.ts +11 -3
  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 +58 -71
  15. package/abstract/edwards.js.map +1 -1
  16. package/abstract/hash-to-curve.d.ts +15 -9
  17. package/abstract/hash-to-curve.d.ts.map +1 -1
  18. package/abstract/hash-to-curve.js +49 -39
  19. package/abstract/hash-to-curve.js.map +1 -1
  20. package/abstract/modular.d.ts +11 -8
  21. package/abstract/modular.d.ts.map +1 -1
  22. package/abstract/modular.js +79 -67
  23. package/abstract/modular.js.map +1 -1
  24. package/abstract/montgomery.d.ts.map +1 -1
  25. package/abstract/montgomery.js +13 -12
  26. package/abstract/montgomery.js.map +1 -1
  27. package/abstract/poseidon.d.ts +40 -3
  28. package/abstract/poseidon.d.ts.map +1 -1
  29. package/abstract/poseidon.js +186 -7
  30. package/abstract/poseidon.js.map +1 -1
  31. package/abstract/tower.d.ts +2 -2
  32. package/abstract/tower.d.ts.map +1 -1
  33. package/abstract/tower.js +16 -17
  34. package/abstract/tower.js.map +1 -1
  35. package/abstract/utils.d.ts +5 -2
  36. package/abstract/utils.d.ts.map +1 -1
  37. package/abstract/utils.js +27 -14
  38. package/abstract/utils.js.map +1 -1
  39. package/abstract/weierstrass.d.ts +21 -9
  40. package/abstract/weierstrass.d.ts.map +1 -1
  41. package/abstract/weierstrass.js +103 -86
  42. package/abstract/weierstrass.js.map +1 -1
  43. package/bls12-381.d.ts +1 -1
  44. package/bls12-381.js +41 -41
  45. package/bls12-381.js.map +1 -1
  46. package/bn254.d.ts +3 -2
  47. package/bn254.d.ts.map +1 -1
  48. package/bn254.js +39 -29
  49. package/bn254.js.map +1 -1
  50. package/ed25519.d.ts +9 -6
  51. package/ed25519.d.ts.map +1 -1
  52. package/ed25519.js +70 -71
  53. package/ed25519.js.map +1 -1
  54. package/ed448.d.ts +7 -6
  55. package/ed448.d.ts.map +1 -1
  56. package/ed448.js +54 -56
  57. package/ed448.js.map +1 -1
  58. package/esm/_shortw_utils.d.ts +2 -2
  59. package/esm/_shortw_utils.js +1 -1
  60. package/esm/abstract/bls.d.ts +5 -5
  61. package/esm/abstract/bls.d.ts.map +1 -1
  62. package/esm/abstract/bls.js +6 -7
  63. package/esm/abstract/bls.js.map +1 -1
  64. package/esm/abstract/curve.d.ts +11 -3
  65. package/esm/abstract/curve.d.ts.map +1 -1
  66. package/esm/abstract/curve.js +77 -74
  67. package/esm/abstract/curve.js.map +1 -1
  68. package/esm/abstract/edwards.d.ts +2 -2
  69. package/esm/abstract/edwards.d.ts.map +1 -1
  70. package/esm/abstract/edwards.js +39 -52
  71. package/esm/abstract/edwards.js.map +1 -1
  72. package/esm/abstract/hash-to-curve.d.ts +15 -9
  73. package/esm/abstract/hash-to-curve.d.ts.map +1 -1
  74. package/esm/abstract/hash-to-curve.js +33 -23
  75. package/esm/abstract/hash-to-curve.js.map +1 -1
  76. package/esm/abstract/modular.d.ts +11 -8
  77. package/esm/abstract/modular.d.ts.map +1 -1
  78. package/esm/abstract/modular.js +71 -59
  79. package/esm/abstract/modular.js.map +1 -1
  80. package/esm/abstract/montgomery.d.ts.map +1 -1
  81. package/esm/abstract/montgomery.js +4 -3
  82. package/esm/abstract/montgomery.js.map +1 -1
  83. package/esm/abstract/poseidon.d.ts +40 -3
  84. package/esm/abstract/poseidon.d.ts.map +1 -1
  85. package/esm/abstract/poseidon.js +180 -5
  86. package/esm/abstract/poseidon.js.map +1 -1
  87. package/esm/abstract/tower.d.ts +2 -2
  88. package/esm/abstract/tower.d.ts.map +1 -1
  89. package/esm/abstract/tower.js +8 -9
  90. package/esm/abstract/tower.js.map +1 -1
  91. package/esm/abstract/utils.d.ts +5 -2
  92. package/esm/abstract/utils.d.ts.map +1 -1
  93. package/esm/abstract/utils.js +26 -13
  94. package/esm/abstract/utils.js.map +1 -1
  95. package/esm/abstract/weierstrass.d.ts +21 -9
  96. package/esm/abstract/weierstrass.d.ts.map +1 -1
  97. package/esm/abstract/weierstrass.js +76 -59
  98. package/esm/abstract/weierstrass.js.map +1 -1
  99. package/esm/bls12-381.d.ts +1 -1
  100. package/esm/bls12-381.js +9 -9
  101. package/esm/bls12-381.js.map +1 -1
  102. package/esm/bn254.d.ts +3 -2
  103. package/esm/bn254.d.ts.map +1 -1
  104. package/esm/bn254.js +17 -7
  105. package/esm/bn254.js.map +1 -1
  106. package/esm/ed25519.d.ts +9 -6
  107. package/esm/ed25519.d.ts.map +1 -1
  108. package/esm/ed25519.js +25 -26
  109. package/esm/ed25519.js.map +1 -1
  110. package/esm/ed448.d.ts +7 -6
  111. package/esm/ed448.d.ts.map +1 -1
  112. package/esm/ed448.js +17 -19
  113. package/esm/ed448.js.map +1 -1
  114. package/esm/jubjub.d.ts +7 -4
  115. package/esm/jubjub.d.ts.map +1 -1
  116. package/esm/jubjub.js +7 -60
  117. package/esm/jubjub.js.map +1 -1
  118. package/esm/misc.d.ts +21 -0
  119. package/esm/misc.d.ts.map +1 -0
  120. package/esm/misc.js +107 -0
  121. package/esm/misc.js.map +1 -0
  122. package/esm/nist.d.ts +29 -0
  123. package/esm/nist.d.ts.map +1 -0
  124. package/esm/nist.js +120 -0
  125. package/esm/nist.js.map +1 -0
  126. package/esm/p256.d.ts +9 -8
  127. package/esm/p256.d.ts.map +1 -1
  128. package/esm/p256.js +6 -43
  129. package/esm/p256.js.map +1 -1
  130. package/esm/p384.d.ts +10 -8
  131. package/esm/p384.d.ts.map +1 -1
  132. package/esm/p384.js +7 -47
  133. package/esm/p384.js.map +1 -1
  134. package/esm/p521.d.ts +6 -6
  135. package/esm/p521.d.ts.map +1 -1
  136. package/esm/p521.js +6 -55
  137. package/esm/p521.js.map +1 -1
  138. package/esm/pasta.d.ts +5 -7
  139. package/esm/pasta.d.ts.map +1 -1
  140. package/esm/pasta.js +5 -33
  141. package/esm/pasta.js.map +1 -1
  142. package/esm/secp256k1.d.ts +15 -10
  143. package/esm/secp256k1.d.ts.map +1 -1
  144. package/esm/secp256k1.js +21 -18
  145. package/esm/secp256k1.js.map +1 -1
  146. package/jubjub.d.ts +7 -4
  147. package/jubjub.d.ts.map +1 -1
  148. package/jubjub.js +8 -63
  149. package/jubjub.js.map +1 -1
  150. package/misc.d.ts +21 -0
  151. package/misc.d.ts.map +1 -0
  152. package/misc.js +112 -0
  153. package/misc.js.map +1 -0
  154. package/nist.d.ts +29 -0
  155. package/nist.d.ts.map +1 -0
  156. package/nist.js +123 -0
  157. package/nist.js.map +1 -0
  158. package/p256.d.ts +9 -8
  159. package/p256.d.ts.map +1 -1
  160. package/p256.js +5 -48
  161. package/p256.js.map +1 -1
  162. package/p384.d.ts +10 -8
  163. package/p384.d.ts.map +1 -1
  164. package/p384.js +6 -52
  165. package/p384.js.map +1 -1
  166. package/p521.d.ts +6 -6
  167. package/p521.d.ts.map +1 -1
  168. package/p521.js +5 -60
  169. package/p521.js.map +1 -1
  170. package/package.json +116 -12
  171. package/pasta.d.ts +5 -7
  172. package/pasta.d.ts.map +1 -1
  173. package/pasta.js +6 -34
  174. package/pasta.js.map +1 -1
  175. package/secp256k1.d.ts +15 -10
  176. package/secp256k1.d.ts.map +1 -1
  177. package/secp256k1.js +60 -57
  178. package/secp256k1.js.map +1 -1
  179. package/src/_shortw_utils.ts +2 -2
  180. package/src/abstract/bls.ts +10 -10
  181. package/src/abstract/curve.ts +89 -80
  182. package/src/abstract/edwards.ts +56 -63
  183. package/src/abstract/hash-to-curve.ts +49 -39
  184. package/src/abstract/modular.ts +68 -59
  185. package/src/abstract/montgomery.ts +4 -3
  186. package/src/abstract/poseidon.ts +208 -13
  187. package/src/abstract/tower.ts +9 -10
  188. package/src/abstract/utils.ts +28 -15
  189. package/src/abstract/weierstrass.ts +105 -87
  190. package/src/bls12-381.ts +10 -10
  191. package/src/bn254.ts +18 -8
  192. package/src/ed25519.ts +31 -28
  193. package/src/ed448.ts +24 -21
  194. package/src/jubjub.ts +8 -63
  195. package/src/misc.ts +123 -0
  196. package/src/nist.ts +154 -0
  197. package/src/p256.ts +6 -49
  198. package/src/p384.ts +8 -53
  199. package/src/p521.ts +6 -70
  200. package/src/pasta.ts +5 -39
  201. package/src/secp256k1.ts +25 -20
package/src/p256.ts CHANGED
@@ -1,54 +1,11 @@
1
1
  /**
2
2
  * NIST secp256r1 aka p256.
3
- * https://www.secg.org/sec2-v2.pdf, https://neuromancer.sk/std/nist/P-256
4
3
  * @module
5
4
  */
6
5
  /*! 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';
12
-
13
- const Fp256 = Field(BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'));
14
- const CURVE_A = Fp256.create(BigInt('-3'));
15
- const CURVE_B = BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b');
16
-
17
- /** secp256r1 curve, ECDSA and ECDH methods. */
18
- // prettier-ignore
19
- export const p256: CurveFnWithCreate = createCurve({
20
- a: CURVE_A, // Equation params: a, b
21
- 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
24
- n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
25
- // Base (generator) point (x, y)
26
- Gx: BigInt('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'),
27
- Gy: BigInt('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'),
28
- h: BigInt(1),
29
- lowS: false,
30
- } as const, sha256);
31
- /** Alias to p256. */
32
- export const secp256r1: CurveFnWithCreate = p256;
33
-
34
- const mapSWU = /* @__PURE__ */ (() =>
35
- mapToCurveSimpleSWU(Fp256, {
36
- A: CURVE_A,
37
- B: CURVE_B,
38
- Z: Fp256.create(BigInt('-10')),
39
- }))();
40
-
41
- const htf = /* @__PURE__ */ (() =>
42
- createHasher(secp256r1.ProjectivePoint, (scalars: bigint[]) => mapSWU(scalars[0]), {
43
- DST: 'P256_XMD:SHA-256_SSWU_RO_',
44
- encodeDST: 'P256_XMD:SHA-256_SSWU_NU_',
45
- p: Fp256.ORDER,
46
- m: 1,
47
- k: 128,
48
- expand: 'xmd',
49
- hash: sha256,
50
- }))();
51
- /** secp256r1 hash-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
52
- export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
53
- /** secp256r1 encode-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
54
- export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();
6
+ import { type HTFMethod } from './abstract/hash-to-curve.ts';
7
+ import { p256_hasher, p256 as p256n } from './nist.ts';
8
+ export const p256: typeof p256n = p256n;
9
+ export const secp256r1: typeof p256n = p256n;
10
+ export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => p256_hasher.hashToCurve)();
11
+ export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => p256_hasher.encodeToCurve)();
package/src/p384.ts CHANGED
@@ -1,58 +1,13 @@
1
1
  /**
2
2
  * NIST secp384r1 aka p384.
3
- * https://www.secg.org/sec2-v2.pdf, https://neuromancer.sk/std/nist/P-384
4
3
  * @module
5
4
  */
6
5
  /*! 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';
12
-
13
- // Field over which we'll do calculations.
14
- // prettier-ignore
15
- const P = BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff');
16
- const Fp384 = Field(P);
17
- const CURVE_A = Fp384.create(BigInt('-3'));
18
- // prettier-ignore
19
- const CURVE_B = BigInt('0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef');
20
-
21
- /** secp384r1 curve, ECDSA and ECDH methods. */
22
- // prettier-ignore
23
- export const p384: CurveFnWithCreate = createCurve({
24
- a: CURVE_A, // Equation params: a, b
25
- 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.
28
- n: BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973'),
29
- // Base (generator) point (x, y)
30
- Gx: BigInt('0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7'),
31
- Gy: BigInt('0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f'),
32
- h: BigInt(1),
33
- lowS: false,
34
- } as const, sha384);
35
- /** Alias to p384. */
36
- export const secp384r1: CurveFnWithCreate = p384;
37
-
38
- const mapSWU = /* @__PURE__ */ (() =>
39
- mapToCurveSimpleSWU(Fp384, {
40
- A: CURVE_A,
41
- B: CURVE_B,
42
- Z: Fp384.create(BigInt('-12')),
43
- }))();
44
-
45
- const htf = /* @__PURE__ */ (() =>
46
- createHasher(secp384r1.ProjectivePoint, (scalars: bigint[]) => mapSWU(scalars[0]), {
47
- DST: 'P384_XMD:SHA-384_SSWU_RO_',
48
- encodeDST: 'P384_XMD:SHA-384_SSWU_NU_',
49
- p: Fp384.ORDER,
50
- m: 1,
51
- k: 192,
52
- expand: 'xmd',
53
- hash: sha384,
54
- }))();
55
- /** secp384r1 hash-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
56
- export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
57
- /** secp384r1 encode-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
58
- export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();
6
+ import { type HTFMethod } from './abstract/hash-to-curve.ts';
7
+ import { p384_hasher, p384 as p384n } from './nist.ts';
8
+ export const p384: typeof p384n = p384n;
9
+ export const secp384r1: typeof p384n = p384n;
10
+ export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => p384_hasher.hashToCurve)();
11
+ export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => p384_hasher.encodeToCurve)();
12
+
13
+ /** @deprecated Use `import { p384_hasher } from "@noble/curves/nist"` module. */
package/src/p521.ts CHANGED
@@ -1,75 +1,11 @@
1
1
  /**
2
2
  * NIST secp521r1 aka p521.
3
- * Note that it's 521, which differs from 512 of its hash function.
4
- * https://www.secg.org/sec2-v2.pdf, https://neuromancer.sk/std/nist/P-521
5
3
  * @module
6
4
  */
7
5
  /*! 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';
13
-
14
- // Field over which we'll do calculations.
15
- // prettier-ignore
16
- const P = BigInt('0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff');
17
- const Fp521 = Field(P);
18
-
19
- const CURVE = {
20
- a: Fp521.create(BigInt('-3')),
21
- b: BigInt(
22
- '0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00'
23
- ),
24
- Fp: Fp521,
25
- n: BigInt(
26
- '0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409'
27
- ),
28
- Gx: BigInt(
29
- '0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66'
30
- ),
31
- Gy: BigInt(
32
- '0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'
33
- ),
34
- 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
- lowS: false,
51
- allowedPrivateKeyLengths: [130, 131, 132] // P521 keys are variable-length. Normalize to 132b
52
- } as const, sha512);
53
- export const secp521r1: CurveFnWithCreate = p521;
54
-
55
- const mapSWU = /* @__PURE__ */ (() =>
56
- mapToCurveSimpleSWU(Fp521, {
57
- A: CURVE.a,
58
- B: CURVE.b,
59
- Z: Fp521.create(BigInt('-4')),
60
- }))();
61
-
62
- const htf = /* @__PURE__ */ (() =>
63
- createHasher(secp521r1.ProjectivePoint, (scalars: bigint[]) => mapSWU(scalars[0]), {
64
- DST: 'P521_XMD:SHA-512_SSWU_RO_',
65
- encodeDST: 'P521_XMD:SHA-512_SSWU_NU_',
66
- p: Fp521.ORDER,
67
- m: 1,
68
- k: 256,
69
- expand: 'xmd',
70
- hash: sha512,
71
- }))();
72
- /** secp521r1 hash-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
73
- export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
74
- /** secp521r1 encode-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
75
- export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();
6
+ import { type HTFMethod } from './abstract/hash-to-curve.ts';
7
+ import { p521_hasher, p521 as p521n } from './nist.ts';
8
+ export const p521: typeof p521n = p521n;
9
+ export const secp521r1: typeof p521n = p521n;
10
+ export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => p521_hasher.hashToCurve)();
11
+ export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => p521_hasher.encodeToCurve)();
package/src/pasta.ts CHANGED
@@ -1,39 +1,5 @@
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
+ import { pallas as pn, vesta as vn } from './misc.ts';
2
+ /** @deprecated */
3
+ export const pallas: typeof pn = pn;
4
+ /** @deprecated */
5
+ export const vesta: typeof vn = vn;
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 Hasher, 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,27 +64,29 @@ 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
- h: BigInt(1), // Cofactor
89
+ h: BigInt(1),
88
90
  lowS: true, // Allow only low-S signatures by default in sign() and verify()
89
91
  endo: {
90
92
  // Endomorphism, see above
@@ -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,
@@ -303,7 +307,8 @@ const mapSWU = /* @__PURE__ */ (() =>
303
307
  B: BigInt('1771'),
304
308
  Z: Fpk1.create(BigInt('-11')),
305
309
  }))();
306
- const htf = /* @__PURE__ */ (() =>
310
+ /** Hashing / encoding to secp256k1 points / field. RFC 9380 methods. */
311
+ export const secp256k1_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
307
312
  createHasher(
308
313
  secp256k1.ProjectivePoint,
309
314
  (scalars: bigint[]) => {
@@ -318,11 +323,11 @@ const htf = /* @__PURE__ */ (() =>
318
323
  k: 128,
319
324
  expand: 'xmd',
320
325
  hash: sha256,
321
- }
326
+ } as const
322
327
  ))();
323
328
 
324
- /** secp256k1 hash-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
325
- export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
329
+ export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() =>
330
+ secp256k1_hasher.hashToCurve)();
326
331
 
327
- /** secp256k1 encode-to-curve from [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380). */
328
- export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();
332
+ export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() =>
333
+ secp256k1_hasher.encodeToCurve)();