@noble/curves 1.9.1 → 1.9.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 (189) hide show
  1. package/README.md +56 -25
  2. package/_shortw_utils.d.ts +7 -5
  3. package/_shortw_utils.d.ts.map +1 -1
  4. package/_shortw_utils.js +2 -8
  5. package/_shortw_utils.js.map +1 -1
  6. package/abstract/bls.d.ts +60 -24
  7. package/abstract/bls.d.ts.map +1 -1
  8. package/abstract/bls.js +158 -109
  9. package/abstract/bls.js.map +1 -1
  10. package/abstract/curve.d.ts +44 -9
  11. package/abstract/curve.d.ts.map +1 -1
  12. package/abstract/curve.js +86 -7
  13. package/abstract/curve.js.map +1 -1
  14. package/abstract/edwards.d.ts +112 -25
  15. package/abstract/edwards.d.ts.map +1 -1
  16. package/abstract/edwards.js +138 -102
  17. package/abstract/edwards.js.map +1 -1
  18. package/abstract/fft.d.ts +12 -10
  19. package/abstract/fft.d.ts.map +1 -1
  20. package/abstract/fft.js +12 -13
  21. package/abstract/fft.js.map +1 -1
  22. package/abstract/hash-to-curve.d.ts +25 -11
  23. package/abstract/hash-to-curve.d.ts.map +1 -1
  24. package/abstract/hash-to-curve.js +17 -14
  25. package/abstract/hash-to-curve.js.map +1 -1
  26. package/abstract/modular.d.ts +24 -11
  27. package/abstract/modular.d.ts.map +1 -1
  28. package/abstract/modular.js +49 -20
  29. package/abstract/modular.js.map +1 -1
  30. package/abstract/montgomery.d.ts +1 -1
  31. package/abstract/montgomery.d.ts.map +1 -1
  32. package/abstract/montgomery.js +5 -4
  33. package/abstract/montgomery.js.map +1 -1
  34. package/abstract/poseidon.d.ts +5 -13
  35. package/abstract/poseidon.d.ts.map +1 -1
  36. package/abstract/poseidon.js +12 -7
  37. package/abstract/poseidon.js.map +1 -1
  38. package/abstract/tower.d.ts +20 -46
  39. package/abstract/tower.d.ts.map +1 -1
  40. package/abstract/tower.js +9 -3
  41. package/abstract/tower.js.map +1 -1
  42. package/abstract/utils.d.ts +1 -115
  43. package/abstract/utils.d.ts.map +1 -1
  44. package/abstract/utils.js +17 -371
  45. package/abstract/utils.js.map +1 -1
  46. package/abstract/weierstrass.d.ts +132 -76
  47. package/abstract/weierstrass.d.ts.map +1 -1
  48. package/abstract/weierstrass.js +462 -398
  49. package/abstract/weierstrass.js.map +1 -1
  50. package/bls12-381.d.ts +2 -0
  51. package/bls12-381.d.ts.map +1 -1
  52. package/bls12-381.js +504 -466
  53. package/bls12-381.js.map +1 -1
  54. package/bn254.d.ts +2 -0
  55. package/bn254.d.ts.map +1 -1
  56. package/bn254.js +44 -32
  57. package/bn254.js.map +1 -1
  58. package/ed25519.d.ts +8 -5
  59. package/ed25519.d.ts.map +1 -1
  60. package/ed25519.js +67 -54
  61. package/ed25519.js.map +1 -1
  62. package/ed448.d.ts +10 -6
  63. package/ed448.d.ts.map +1 -1
  64. package/ed448.js +80 -57
  65. package/ed448.js.map +1 -1
  66. package/esm/_shortw_utils.d.ts +7 -5
  67. package/esm/_shortw_utils.d.ts.map +1 -1
  68. package/esm/_shortw_utils.js +2 -8
  69. package/esm/_shortw_utils.js.map +1 -1
  70. package/esm/abstract/bls.d.ts +60 -24
  71. package/esm/abstract/bls.d.ts.map +1 -1
  72. package/esm/abstract/bls.js +158 -109
  73. package/esm/abstract/bls.js.map +1 -1
  74. package/esm/abstract/curve.d.ts +44 -9
  75. package/esm/abstract/curve.d.ts.map +1 -1
  76. package/esm/abstract/curve.js +83 -8
  77. package/esm/abstract/curve.js.map +1 -1
  78. package/esm/abstract/edwards.d.ts +112 -25
  79. package/esm/abstract/edwards.d.ts.map +1 -1
  80. package/esm/abstract/edwards.js +138 -104
  81. package/esm/abstract/edwards.js.map +1 -1
  82. package/esm/abstract/fft.d.ts +12 -10
  83. package/esm/abstract/fft.d.ts.map +1 -1
  84. package/esm/abstract/fft.js +10 -11
  85. package/esm/abstract/fft.js.map +1 -1
  86. package/esm/abstract/hash-to-curve.d.ts +25 -11
  87. package/esm/abstract/hash-to-curve.d.ts.map +1 -1
  88. package/esm/abstract/hash-to-curve.js +17 -14
  89. package/esm/abstract/hash-to-curve.js.map +1 -1
  90. package/esm/abstract/modular.d.ts +24 -11
  91. package/esm/abstract/modular.d.ts.map +1 -1
  92. package/esm/abstract/modular.js +48 -19
  93. package/esm/abstract/modular.js.map +1 -1
  94. package/esm/abstract/montgomery.d.ts +1 -1
  95. package/esm/abstract/montgomery.d.ts.map +1 -1
  96. package/esm/abstract/montgomery.js +5 -4
  97. package/esm/abstract/montgomery.js.map +1 -1
  98. package/esm/abstract/poseidon.d.ts +5 -13
  99. package/esm/abstract/poseidon.d.ts.map +1 -1
  100. package/esm/abstract/poseidon.js +12 -7
  101. package/esm/abstract/poseidon.js.map +1 -1
  102. package/esm/abstract/tower.d.ts +20 -46
  103. package/esm/abstract/tower.d.ts.map +1 -1
  104. package/esm/abstract/tower.js +9 -3
  105. package/esm/abstract/tower.js.map +1 -1
  106. package/esm/abstract/utils.d.ts +1 -115
  107. package/esm/abstract/utils.d.ts.map +1 -1
  108. package/esm/abstract/utils.js +3 -344
  109. package/esm/abstract/utils.js.map +1 -1
  110. package/esm/abstract/weierstrass.d.ts +132 -76
  111. package/esm/abstract/weierstrass.d.ts.map +1 -1
  112. package/esm/abstract/weierstrass.js +460 -400
  113. package/esm/abstract/weierstrass.js.map +1 -1
  114. package/esm/bls12-381.d.ts +2 -0
  115. package/esm/bls12-381.d.ts.map +1 -1
  116. package/esm/bls12-381.js +503 -465
  117. package/esm/bls12-381.js.map +1 -1
  118. package/esm/bn254.d.ts +2 -0
  119. package/esm/bn254.d.ts.map +1 -1
  120. package/esm/bn254.js +41 -29
  121. package/esm/bn254.js.map +1 -1
  122. package/esm/ed25519.d.ts +8 -5
  123. package/esm/ed25519.d.ts.map +1 -1
  124. package/esm/ed25519.js +62 -49
  125. package/esm/ed25519.js.map +1 -1
  126. package/esm/ed448.d.ts +10 -6
  127. package/esm/ed448.d.ts.map +1 -1
  128. package/esm/ed448.js +74 -51
  129. package/esm/ed448.js.map +1 -1
  130. package/esm/misc.d.ts.map +1 -1
  131. package/esm/misc.js +31 -26
  132. package/esm/misc.js.map +1 -1
  133. package/esm/nist.d.ts +7 -16
  134. package/esm/nist.d.ts.map +1 -1
  135. package/esm/nist.js +86 -97
  136. package/esm/nist.js.map +1 -1
  137. package/esm/p256.d.ts +3 -3
  138. package/esm/p384.d.ts +3 -3
  139. package/esm/p521.d.ts +3 -3
  140. package/esm/secp256k1.d.ts +6 -6
  141. package/esm/secp256k1.d.ts.map +1 -1
  142. package/esm/secp256k1.js +43 -40
  143. package/esm/secp256k1.js.map +1 -1
  144. package/esm/utils.d.ts +96 -0
  145. package/esm/utils.d.ts.map +1 -0
  146. package/esm/utils.js +279 -0
  147. package/esm/utils.js.map +1 -0
  148. package/misc.d.ts.map +1 -1
  149. package/misc.js +35 -30
  150. package/misc.js.map +1 -1
  151. package/nist.d.ts +7 -16
  152. package/nist.d.ts.map +1 -1
  153. package/nist.js +86 -97
  154. package/nist.js.map +1 -1
  155. package/p256.d.ts +3 -3
  156. package/p384.d.ts +3 -3
  157. package/p521.d.ts +3 -3
  158. package/package.json +14 -5
  159. package/secp256k1.d.ts +6 -6
  160. package/secp256k1.d.ts.map +1 -1
  161. package/secp256k1.js +46 -43
  162. package/secp256k1.js.map +1 -1
  163. package/src/_shortw_utils.ts +5 -15
  164. package/src/abstract/bls.ts +260 -145
  165. package/src/abstract/curve.ts +115 -13
  166. package/src/abstract/edwards.ts +279 -138
  167. package/src/abstract/fft.ts +30 -19
  168. package/src/abstract/hash-to-curve.ts +51 -27
  169. package/src/abstract/modular.ts +49 -28
  170. package/src/abstract/montgomery.ts +9 -7
  171. package/src/abstract/poseidon.ts +22 -18
  172. package/src/abstract/tower.ts +36 -67
  173. package/src/abstract/utils.ts +3 -378
  174. package/src/abstract/weierstrass.ts +700 -453
  175. package/src/bls12-381.ts +540 -489
  176. package/src/bn254.ts +47 -35
  177. package/src/ed25519.ts +80 -64
  178. package/src/ed448.ts +129 -92
  179. package/src/misc.ts +39 -34
  180. package/src/nist.ts +138 -127
  181. package/src/p256.ts +3 -3
  182. package/src/p384.ts +3 -3
  183. package/src/p521.ts +3 -3
  184. package/src/secp256k1.ts +58 -46
  185. package/src/utils.ts +328 -0
  186. package/utils.d.ts +96 -0
  187. package/utils.d.ts.map +1 -0
  188. package/utils.js +313 -0
  189. package/utils.js.map +1 -0
package/src/nist.ts CHANGED
@@ -4,152 +4,163 @@
4
4
  * @module
5
5
  */
6
6
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
7
- import { sha256, sha384, sha512 } from '@noble/hashes/sha2';
7
+ import { sha256, sha384, sha512 } from '@noble/hashes/sha2.js';
8
8
  import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
9
- import { createHasher, type Hasher } from './abstract/hash-to-curve.ts';
10
- import { Field } from './abstract/modular.ts';
11
- import { mapToCurveSimpleSWU } from './abstract/weierstrass.ts';
9
+ import { createHasher, type H2CHasher } from './abstract/hash-to-curve.ts';
10
+ import { Field, type IField } from './abstract/modular.ts';
11
+ import { mapToCurveSimpleSWU, type WeierstrassOpts } from './abstract/weierstrass.ts';
12
12
 
13
- const Fp256 = Field(BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'));
14
- const p256_a = Fp256.create(BigInt('-3'));
15
- const p256_b = BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b');
16
-
17
- /**
18
- * secp256r1 curve, ECDSA and ECDH methods.
19
- * Field: `2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n-1n`
20
- */
21
- // prettier-ignore
22
- export const p256: CurveFnWithCreate = createCurve({
23
- a: p256_a,
24
- b: p256_b,
25
- Fp: Fp256,
13
+ // p = 2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n - 1n
14
+ // a = Fp256.create(BigInt('-3'));
15
+ const p256_CURVE: WeierstrassOpts<bigint> = {
16
+ p: BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'),
26
17
  n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
18
+ h: BigInt(1),
19
+ a: BigInt('0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc'),
20
+ b: BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b'),
27
21
  Gx: BigInt('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'),
28
22
  Gy: BigInt('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'),
29
- h: BigInt(1),
30
- lowS: false
31
- } as const, sha256);
32
- /** Alias to p256. */
33
- export const secp256r1: CurveFnWithCreate = p256;
23
+ };
34
24
 
35
- const p256_mapSWU = /* @__PURE__ */ (() =>
36
- mapToCurveSimpleSWU(Fp256, {
37
- A: p256_a,
38
- B: p256_b,
39
- Z: Fp256.create(BigInt('-10')),
40
- }))();
41
-
42
- /** Hashing / encoding to p256 points / field. RFC 9380 methods. */
43
- export const p256_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
44
- createHasher(secp256r1.ProjectivePoint, (scalars: bigint[]) => p256_mapSWU(scalars[0]), {
45
- DST: 'P256_XMD:SHA-256_SSWU_RO_',
46
- encodeDST: 'P256_XMD:SHA-256_SSWU_NU_',
47
- p: Fp256.ORDER,
48
- m: 1,
49
- k: 128,
50
- expand: 'xmd',
51
- hash: sha256,
52
- }))();
53
-
54
- // Field over which we'll do calculations.
55
- const Fp384 = Field(
56
- BigInt(
25
+ // p = 2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n
26
+ const p384_CURVE: WeierstrassOpts<bigint> = {
27
+ p: BigInt(
57
28
  '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff'
58
- )
59
- );
60
- const p384_a = Fp384.create(BigInt('-3'));
61
- // prettier-ignore
62
- const p384_b = BigInt('0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef');
63
-
64
- /**
65
- * secp384r1 curve, ECDSA and ECDH methods.
66
- * Field: `2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n`.
67
- * */
68
- // prettier-ignore
69
- export const p384: CurveFnWithCreate = createCurve({
70
- a: p384_a,
71
- b: p384_b,
72
- Fp: Fp384,
73
- n: BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973'),
74
- Gx: BigInt('0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7'),
75
- Gy: BigInt('0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f'),
29
+ ),
30
+ n: BigInt(
31
+ '0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973'
32
+ ),
76
33
  h: BigInt(1),
77
- lowS: false
78
- } as const, sha384);
79
- /** Alias to p384. */
80
- export const secp384r1: CurveFnWithCreate = p384;
81
-
82
- const p384_mapSWU = /* @__PURE__ */ (() =>
83
- mapToCurveSimpleSWU(Fp384, {
84
- A: p384_a,
85
- B: p384_b,
86
- Z: Fp384.create(BigInt('-12')),
87
- }))();
88
-
89
- /** Hashing / encoding to p384 points / field. RFC 9380 methods. */
90
- export const p384_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
91
- createHasher(secp384r1.ProjectivePoint, (scalars: bigint[]) => p384_mapSWU(scalars[0]), {
92
- DST: 'P384_XMD:SHA-384_SSWU_RO_',
93
- encodeDST: 'P384_XMD:SHA-384_SSWU_NU_',
94
- p: Fp384.ORDER,
95
- m: 1,
96
- k: 192,
97
- expand: 'xmd',
98
- hash: sha384,
99
- }))();
34
+ a: BigInt(
35
+ '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc'
36
+ ),
37
+ b: BigInt(
38
+ '0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef'
39
+ ),
40
+ Gx: BigInt(
41
+ '0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7'
42
+ ),
43
+ Gy: BigInt(
44
+ '0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f'
45
+ ),
46
+ };
100
47
 
101
- // Field over which we'll do calculations.
102
- const Fp521 = Field(
103
- BigInt(
48
+ // p = 2n**521n - 1n
49
+ const p521_CURVE: WeierstrassOpts<bigint> = {
50
+ p: BigInt(
104
51
  '0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
105
- )
106
- );
107
-
108
- const p521_a = Fp521.create(BigInt('-3'));
109
- const p521_b = BigInt(
110
- '0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00'
111
- );
112
-
113
- /**
114
- * NIST secp521r1 aka p521 curve, ECDSA and ECDH methods.
115
- * Field: `2n**521n - 1n`.
116
- */
117
- // prettier-ignore
118
- export const p521: CurveFnWithCreate = createCurve({
119
- a: p521_a,
120
- b: p521_b,
121
- Fp: Fp521,
52
+ ),
122
53
  n: BigInt(
123
54
  '0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409'
124
55
  ),
56
+ h: BigInt(1),
57
+ a: BigInt(
58
+ '0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc'
59
+ ),
60
+ b: BigInt(
61
+ '0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00'
62
+ ),
125
63
  Gx: BigInt(
126
64
  '0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66'
127
65
  ),
128
66
  Gy: BigInt(
129
67
  '0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'
130
68
  ),
131
- h: BigInt(1),
132
- lowS: false,
133
- allowedPrivateKeyLengths: [130, 131, 132] // P521 keys are variable-length. Normalize to 132b
134
- } as const, sha512);
135
- /** Alias to p521. */
136
- export const secp521r1: CurveFnWithCreate = p521;
69
+ };
137
70
 
138
- const p521_mapSWU = /* @__PURE__ */ (() =>
139
- mapToCurveSimpleSWU(Fp521, {
140
- A: p521_a,
141
- B: p521_b,
142
- Z: Fp521.create(BigInt('-4')),
143
- }))();
71
+ const Fp256 = Field(p256_CURVE.p);
72
+ const Fp384 = Field(p384_CURVE.p);
73
+ const Fp521 = Field(p521_CURVE.p);
74
+ type SwuOpts = {
75
+ A: bigint;
76
+ B: bigint;
77
+ Z: bigint;
78
+ };
79
+ function createSWU(field: IField<bigint>, opts: SwuOpts) {
80
+ const map = mapToCurveSimpleSWU(field, opts);
81
+ return (scalars: bigint[]) => map(scalars[0]);
82
+ }
144
83
 
84
+ /** NIST P256 (aka secp256r1, prime256v1) curve, ECDSA and ECDH methods. */
85
+ export const p256: CurveFnWithCreate = createCurve(
86
+ { ...p256_CURVE, Fp: Fp256, lowS: false },
87
+ sha256
88
+ );
89
+ /** Alias to p256. */
90
+ export const secp256r1: CurveFnWithCreate = p256;
91
+ /** Hashing / encoding to p256 points / field. RFC 9380 methods. */
92
+ export const p256_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
93
+ return createHasher(
94
+ p256.Point,
95
+ createSWU(Fp256, {
96
+ A: p256_CURVE.a,
97
+ B: p256_CURVE.b,
98
+ Z: Fp256.create(BigInt('-10')),
99
+ }),
100
+ {
101
+ DST: 'P256_XMD:SHA-256_SSWU_RO_',
102
+ encodeDST: 'P256_XMD:SHA-256_SSWU_NU_',
103
+ p: p256_CURVE.p,
104
+ m: 1,
105
+ k: 128,
106
+ expand: 'xmd',
107
+ hash: sha256,
108
+ }
109
+ );
110
+ })();
111
+
112
+ /** NIST P384 (aka secp384r1) curve, ECDSA and ECDH methods. */
113
+ export const p384: CurveFnWithCreate = createCurve(
114
+ { ...p384_CURVE, Fp: Fp384, lowS: false },
115
+ sha384
116
+ );
117
+ /** Alias to p384. */
118
+ export const secp384r1: CurveFnWithCreate = p384;
119
+ /** Hashing / encoding to p384 points / field. RFC 9380 methods. */
120
+ export const p384_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
121
+ return createHasher(
122
+ p384.Point,
123
+ createSWU(Fp384, {
124
+ A: p384_CURVE.a,
125
+ B: p384_CURVE.b,
126
+ Z: Fp384.create(BigInt('-12')),
127
+ }),
128
+ {
129
+ DST: 'P384_XMD:SHA-384_SSWU_RO_',
130
+ encodeDST: 'P384_XMD:SHA-384_SSWU_NU_',
131
+ p: p384_CURVE.p,
132
+ m: 1,
133
+ k: 192,
134
+ expand: 'xmd',
135
+ hash: sha384,
136
+ }
137
+ );
138
+ })();
139
+
140
+ /** NIST P521 (aka secp521r1) curve, ECDSA and ECDH methods. */
141
+ export const p521: CurveFnWithCreate = createCurve(
142
+ { ...p521_CURVE, Fp: Fp521, lowS: false, allowedPrivateKeyLengths: [130, 131, 132] },
143
+ sha512
144
+ );
145
+ /** Alias to p521. */
146
+ export const secp521r1: CurveFnWithCreate = p521;
145
147
  /** Hashing / encoding to p521 points / field. RFC 9380 methods. */
146
- export const p521_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
147
- createHasher(secp521r1.ProjectivePoint, (scalars: bigint[]) => p521_mapSWU(scalars[0]), {
148
- DST: 'P521_XMD:SHA-512_SSWU_RO_',
149
- encodeDST: 'P521_XMD:SHA-512_SSWU_NU_',
150
- p: Fp521.ORDER,
151
- m: 1,
152
- k: 256,
153
- expand: 'xmd',
154
- hash: sha512,
155
- }))();
148
+ export const p521_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
149
+ return createHasher(
150
+ p521.Point,
151
+ createSWU(Fp521, {
152
+ A: p521_CURVE.a,
153
+ B: p521_CURVE.b,
154
+ Z: Fp521.create(BigInt('-4')),
155
+ }),
156
+ {
157
+ DST: 'P521_XMD:SHA-512_SSWU_RO_',
158
+ encodeDST: 'P521_XMD:SHA-512_SSWU_NU_',
159
+ p: p521_CURVE.p,
160
+ m: 1,
161
+ k: 256,
162
+ expand: 'xmd',
163
+ hash: sha512,
164
+ }
165
+ );
166
+ })();
package/src/p256.ts CHANGED
@@ -3,9 +3,9 @@
3
3
  * @module
4
4
  */
5
5
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
6
- import { type HTFMethod } from './abstract/hash-to-curve.ts';
6
+ import { type H2CMethod } from './abstract/hash-to-curve.ts';
7
7
  import { p256_hasher, p256 as p256n } from './nist.ts';
8
8
  export const p256: typeof p256n = p256n;
9
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)();
10
+ export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p256_hasher.hashToCurve)();
11
+ export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p256_hasher.encodeToCurve)();
package/src/p384.ts CHANGED
@@ -3,11 +3,11 @@
3
3
  * @module
4
4
  */
5
5
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
6
- import { type HTFMethod } from './abstract/hash-to-curve.ts';
6
+ import { type H2CMethod } from './abstract/hash-to-curve.ts';
7
7
  import { p384_hasher, p384 as p384n } from './nist.ts';
8
8
  export const p384: typeof p384n = p384n;
9
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)();
10
+ export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p384_hasher.hashToCurve)();
11
+ export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p384_hasher.encodeToCurve)();
12
12
 
13
13
  /** @deprecated Use `import { p384_hasher } from "@noble/curves/nist"` module. */
package/src/p521.ts CHANGED
@@ -3,9 +3,9 @@
3
3
  * @module
4
4
  */
5
5
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
6
- import { type HTFMethod } from './abstract/hash-to-curve.ts';
6
+ import { type H2CMethod } from './abstract/hash-to-curve.ts';
7
7
  import { p521_hasher, p521 as p521n } from './nist.ts';
8
8
  export const p521: typeof p521n = p521n;
9
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)();
10
+ export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p521_hasher.hashToCurve)();
11
+ export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p521_hasher.encodeToCurve)();
package/src/secp256k1.ts CHANGED
@@ -1,22 +1,28 @@
1
1
  /**
2
- * NIST secp256k1. See [pdf](https://www.secg.org/sec2-v2.pdf).
2
+ * SECG secp256k1. See [pdf](https://www.secg.org/sec2-v2.pdf).
3
3
  *
4
- * Seems to be rigid (not backdoored)
5
- * [as per discussion](https://bitcointalk.org/index.php?topic=289795.msg3183975#msg3183975).
6
- *
7
- * secp256k1 belongs to Koblitz curves: it has efficiently computable endomorphism.
8
- * Endomorphism uses 2x less RAM, speeds up precomputation by 2x and ECDH / key recovery by 20%.
9
- * For precomputed wNAF it trades off 1/2 init time & 1/3 ram for 20% perf hit.
10
- * [See explanation](https://gist.github.com/paulmillr/eb670806793e84df628a7c434a873066).
4
+ * Belongs to Koblitz curves: it has efficiently-computable GLV endomorphism ψ,
5
+ * check out {@link EndomorphismOpts}. Seems to be rigid (not backdoored).
11
6
  * @module
12
7
  */
13
8
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
14
- import { sha256 } from '@noble/hashes/sha2';
15
- import { randomBytes } from '@noble/hashes/utils';
9
+ import { sha256 } from '@noble/hashes/sha2.js';
10
+ import { randomBytes } from '@noble/hashes/utils.js';
16
11
  import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
17
- import { createHasher, type Hasher, type HTFMethod, isogenyMap } from './abstract/hash-to-curve.ts';
12
+ import {
13
+ createHasher,
14
+ type H2CHasher,
15
+ type H2CMethod,
16
+ isogenyMap,
17
+ } from './abstract/hash-to-curve.ts';
18
18
  import { Field, mod, pow2 } from './abstract/modular.ts';
19
- import type { Hex, PrivKey } from './abstract/utils.ts';
19
+ import {
20
+ type EndomorphismOpts,
21
+ mapToCurveSimpleSWU,
22
+ type ProjPointType as PointType,
23
+ type WeierstrassOpts,
24
+ } from './abstract/weierstrass.ts';
25
+ import type { Hex, PrivKey } from './utils.ts';
20
26
  import {
21
27
  aInRange,
22
28
  bytesToNumberBE,
@@ -24,11 +30,20 @@ import {
24
30
  ensureBytes,
25
31
  inRange,
26
32
  numberToBytesBE,
27
- } from './abstract/utils.ts';
28
- import { mapToCurveSimpleSWU, type ProjPointType as PointType } from './abstract/weierstrass.ts';
33
+ } from './utils.ts';
29
34
 
30
- const secp256k1P = BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f');
31
- const secp256k1N = BigInt('0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141');
35
+ // Seems like generator was produced from some seed:
36
+ // `Point.BASE.multiply(Point.Fn.inv(2n, N)).toAffine().x`
37
+ // // gives short x 0x3b78ce563f89a0ed9414f5aa28ad0d96d6795f9c63n
38
+ const secp256k1_CURVE: WeierstrassOpts<bigint> = {
39
+ p: BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f'),
40
+ n: BigInt('0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141'),
41
+ h: BigInt(1),
42
+ a: BigInt(0),
43
+ b: BigInt(7),
44
+ Gx: BigInt('0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'),
45
+ Gy: BigInt('0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'),
46
+ };
32
47
  const _0n = BigInt(0);
33
48
  const _1n = BigInt(1);
34
49
  const _2n = BigInt(2);
@@ -39,7 +54,7 @@ const divNearest = (a: bigint, b: bigint) => (a + b / _2n) / b;
39
54
  * (P+1n/4n).toString(2) would produce bits [223x 1, 0, 22x 1, 4x 0, 11, 00]
40
55
  */
41
56
  function sqrtMod(y: bigint): bigint {
42
- const P = secp256k1P;
57
+ const P = secp256k1_CURVE.p;
43
58
  // prettier-ignore
44
59
  const _3n = BigInt(3), _6n = BigInt(6), _11n = BigInt(11), _22n = BigInt(22);
45
60
  // prettier-ignore
@@ -62,7 +77,7 @@ function sqrtMod(y: bigint): bigint {
62
77
  return root;
63
78
  }
64
79
 
65
- const Fpk1 = Field(secp256k1P, undefined, undefined, { sqrt: sqrtMod });
80
+ const Fpk1 = Field(secp256k1_CURVE.p, undefined, undefined, { sqrt: sqrtMod });
66
81
 
67
82
  /**
68
83
  * secp256k1 curve, ECDSA and ECDH methods.
@@ -81,19 +96,14 @@ const Fpk1 = Field(secp256k1P, undefined, undefined, { sqrt: sqrtMod });
81
96
  */
82
97
  export const secp256k1: CurveFnWithCreate = createCurve(
83
98
  {
84
- a: _0n,
85
- b: BigInt(7),
99
+ ...secp256k1_CURVE,
86
100
  Fp: Fpk1,
87
- n: secp256k1N,
88
- Gx: BigInt('55066263022277343669578718895168534326250603453777594175500187360389116729240'),
89
- Gy: BigInt('32670510020758816978083085130507043184471273380659243275938904335757337482424'),
90
- h: BigInt(1),
91
101
  lowS: true, // Allow only low-S signatures by default in sign() and verify()
92
102
  endo: {
93
103
  // Endomorphism, see above
94
104
  beta: BigInt('0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee'),
95
105
  splitScalar: (k: bigint) => {
96
- const n = secp256k1N;
106
+ const n = secp256k1_CURVE.n;
97
107
  const a1 = BigInt('0x3086d221a7d46bcde86c90e49284eb15');
98
108
  const b1 = -_1n * BigInt('0xe4437ed6010e88286f547fa90abfe4c3');
99
109
  const a2 = BigInt('0x114ca50f7a8e2f3f657c1108d9d44cfd8');
@@ -113,7 +123,7 @@ export const secp256k1: CurveFnWithCreate = createCurve(
113
123
  }
114
124
  return { k1neg, k1, k2neg, k2 };
115
125
  },
116
- },
126
+ } satisfies EndomorphismOpts,
117
127
  },
118
128
  sha256
119
129
  );
@@ -133,19 +143,18 @@ function taggedHash(tag: string, ...messages: Uint8Array[]): Uint8Array {
133
143
  }
134
144
 
135
145
  // ECDSA compact points are 33-byte. Schnorr is 32: we strip first byte 0x02 or 0x03
136
- const pointToBytes = (point: PointType<bigint>) => point.toRawBytes(true).slice(1);
146
+ const pointToBytes = (point: PointType<bigint>) => point.toBytes(true).slice(1);
137
147
  const numTo32b = (n: bigint) => numberToBytesBE(n, 32);
138
- const modP = (x: bigint) => mod(x, secp256k1P);
139
- const modN = (x: bigint) => mod(x, secp256k1N);
140
- const Point = /* @__PURE__ */ (() => secp256k1.ProjectivePoint)();
141
- const GmulAdd = (Q: PointType<bigint>, a: bigint, b: bigint) =>
142
- Point.BASE.multiplyAndAddUnsafe(Q, a, b);
148
+ const modP = (x: bigint) => mod(x, secp256k1_CURVE.p);
149
+ const modN = (x: bigint) => mod(x, secp256k1_CURVE.n);
150
+ const Point = /* @__PURE__ */ (() => secp256k1.Point)();
151
+ const hasEven = (y: bigint) => y % _2n === _0n;
143
152
 
144
153
  // Calculate point, scalar and bytes
145
154
  function schnorrGetExtPubKey(priv: PrivKey) {
146
155
  let d_ = secp256k1.utils.normPrivateKeyToScalar(priv); // same method executed in fromPrivateKey
147
156
  let p = Point.fromPrivateKey(d_); // P = d'⋅G; 0 < d' < n check is done inside
148
- const scalar = p.hasEvenY() ? d_ : modN(-d_);
157
+ const scalar = hasEven(p.y) ? d_ : modN(-d_);
149
158
  return { scalar: scalar, bytes: pointToBytes(p) };
150
159
  }
151
160
  /**
@@ -153,12 +162,12 @@ function schnorrGetExtPubKey(priv: PrivKey) {
153
162
  * @returns valid point checked for being on-curve
154
163
  */
155
164
  function lift_x(x: bigint): PointType<bigint> {
156
- aInRange('x', x, _1n, secp256k1P); // Fail if x ≥ p.
165
+ aInRange('x', x, _1n, secp256k1_CURVE.p); // Fail if x ≥ p.
157
166
  const xx = modP(x * x);
158
167
  const c = modP(xx * x + BigInt(7)); // Let c = x³ + 7 mod p.
159
168
  let y = sqrtMod(c); // Let y = c^(p+1)/4 mod p.
160
- if (y % _2n !== _0n) y = modP(-y); // Return the unique point P such that x(P) = x and
161
- const p = new Point(x, y, _1n); // y(P) = y if y mod 2 = 0 or y(P) = p-y otherwise.
169
+ if (!hasEven(y)) y = modP(-y); // Return the unique point P such that x(P) = x and
170
+ const p = Point.fromAffine({ x, y }); // y(P) = y if y mod 2 = 0 or y(P) = p-y otherwise.
162
171
  p.assertValidity();
163
172
  return p;
164
173
  }
@@ -214,13 +223,16 @@ function schnorrVerify(signature: Hex, message: Hex, publicKey: Hex): boolean {
214
223
  try {
215
224
  const P = lift_x(num(pub)); // P = lift_x(int(pk)); fail if that fails
216
225
  const r = num(sig.subarray(0, 32)); // Let r = int(sig[0:32]); fail if r ≥ p.
217
- if (!inRange(r, _1n, secp256k1P)) return false;
226
+ if (!inRange(r, _1n, secp256k1_CURVE.p)) return false;
218
227
  const s = num(sig.subarray(32, 64)); // Let s = int(sig[32:64]); fail if s ≥ n.
219
- if (!inRange(s, _1n, secp256k1N)) return false;
228
+ if (!inRange(s, _1n, secp256k1_CURVE.n)) return false;
220
229
  const e = challenge(numTo32b(r), pointToBytes(P), m); // int(challenge(bytes(r)||bytes(P)||m))%n
221
- const R = GmulAdd(P, s, modN(-e)); // R = s⋅G - eP
222
- if (!R || !R.hasEvenY() || R.toAffine().x !== r) return false; // -eP == (n-e)P
223
- return true; // Fail if is_infinite(R) / not has_even_y(R) / x(R) ≠ r.
230
+ // R = s⋅G - e⋅P, where -eP == (n-e)P
231
+ const R = Point.BASE.multiplyUnsafe(s).add(P.multiplyUnsafe(modN(-e)));
232
+ const { x, y } = R.toAffine();
233
+ // Fail if is_infinite(R) / not has_even_y(R) / x(R) ≠ r.
234
+ if (R.is0() || !hasEven(y) || x !== r) return false;
235
+ return true;
224
236
  } catch (error) {
225
237
  return false;
226
238
  }
@@ -308,9 +320,9 @@ const mapSWU = /* @__PURE__ */ (() =>
308
320
  Z: Fpk1.create(BigInt('-11')),
309
321
  }))();
310
322
  /** Hashing / encoding to secp256k1 points / field. RFC 9380 methods. */
311
- export const secp256k1_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
323
+ export const secp256k1_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() =>
312
324
  createHasher(
313
- secp256k1.ProjectivePoint,
325
+ secp256k1.Point,
314
326
  (scalars: bigint[]) => {
315
327
  const { x, y } = mapSWU(Fpk1.create(scalars[0]));
316
328
  return isoMap(x, y);
@@ -323,11 +335,11 @@ export const secp256k1_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
323
335
  k: 128,
324
336
  expand: 'xmd',
325
337
  hash: sha256,
326
- } as const
338
+ }
327
339
  ))();
328
340
 
329
- export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() =>
341
+ export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
330
342
  secp256k1_hasher.hashToCurve)();
331
343
 
332
- export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() =>
344
+ export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
333
345
  secp256k1_hasher.encodeToCurve)();