@noble/curves 1.9.7 → 2.0.0-beta.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 (242) hide show
  1. package/README.md +520 -505
  2. package/abstract/bls.d.ts +58 -120
  3. package/abstract/bls.d.ts.map +1 -1
  4. package/abstract/bls.js +108 -152
  5. package/abstract/bls.js.map +1 -1
  6. package/abstract/curve.d.ts +18 -54
  7. package/abstract/curve.d.ts.map +1 -1
  8. package/abstract/curve.js +30 -49
  9. package/abstract/curve.js.map +1 -1
  10. package/abstract/edwards.d.ts +18 -77
  11. package/abstract/edwards.d.ts.map +1 -1
  12. package/abstract/edwards.js +68 -144
  13. package/abstract/edwards.js.map +1 -1
  14. package/abstract/fft.js +14 -27
  15. package/abstract/fft.js.map +1 -1
  16. package/abstract/hash-to-curve.d.ts +35 -47
  17. package/abstract/hash-to-curve.d.ts.map +1 -1
  18. package/abstract/hash-to-curve.js +42 -46
  19. package/abstract/hash-to-curve.js.map +1 -1
  20. package/abstract/modular.d.ts +5 -17
  21. package/abstract/modular.d.ts.map +1 -1
  22. package/abstract/modular.js +170 -169
  23. package/abstract/modular.js.map +1 -1
  24. package/abstract/montgomery.d.ts +7 -12
  25. package/abstract/montgomery.d.ts.map +1 -1
  26. package/abstract/montgomery.js +22 -29
  27. package/abstract/montgomery.js.map +1 -1
  28. package/abstract/oprf.d.ts +282 -0
  29. package/abstract/oprf.d.ts.map +1 -0
  30. package/abstract/oprf.js +297 -0
  31. package/abstract/oprf.js.map +1 -0
  32. package/abstract/poseidon.d.ts.map +1 -1
  33. package/abstract/poseidon.js +26 -31
  34. package/abstract/poseidon.js.map +1 -1
  35. package/abstract/tower.d.ts.map +1 -1
  36. package/abstract/tower.js +43 -19
  37. package/abstract/tower.js.map +1 -1
  38. package/abstract/weierstrass.d.ts +77 -168
  39. package/abstract/weierstrass.d.ts.map +1 -1
  40. package/abstract/weierstrass.js +184 -389
  41. package/abstract/weierstrass.js.map +1 -1
  42. package/bls12-381.d.ts +5 -11
  43. package/bls12-381.d.ts.map +1 -1
  44. package/bls12-381.js +161 -181
  45. package/bls12-381.js.map +1 -1
  46. package/bn254.d.ts +59 -11
  47. package/bn254.d.ts.map +1 -1
  48. package/bn254.js +69 -97
  49. package/bn254.js.map +1 -1
  50. package/ed25519.d.ts +33 -48
  51. package/ed25519.d.ts.map +1 -1
  52. package/ed25519.js +147 -161
  53. package/ed25519.js.map +1 -1
  54. package/ed448.d.ts +27 -36
  55. package/ed448.d.ts.map +1 -1
  56. package/ed448.js +143 -164
  57. package/ed448.js.map +1 -1
  58. package/index.d.ts +1 -0
  59. package/index.js +20 -4
  60. package/index.js.map +1 -1
  61. package/misc.d.ts +10 -14
  62. package/misc.d.ts.map +1 -1
  63. package/misc.js +53 -62
  64. package/misc.js.map +1 -1
  65. package/nist.d.ts +31 -16
  66. package/nist.d.ts.map +1 -1
  67. package/nist.js +75 -64
  68. package/nist.js.map +1 -1
  69. package/package.json +20 -234
  70. package/secp256k1.d.ts +17 -30
  71. package/secp256k1.d.ts.map +1 -1
  72. package/secp256k1.js +59 -73
  73. package/secp256k1.js.map +1 -1
  74. package/src/abstract/bls.ts +207 -354
  75. package/src/abstract/curve.ts +25 -84
  76. package/src/abstract/edwards.ts +68 -193
  77. package/src/abstract/hash-to-curve.ts +71 -85
  78. package/src/abstract/modular.ts +150 -134
  79. package/src/abstract/montgomery.ts +28 -35
  80. package/src/abstract/oprf.ts +600 -0
  81. package/src/abstract/poseidon.ts +6 -8
  82. package/src/abstract/tower.ts +0 -3
  83. package/src/abstract/weierstrass.ts +203 -525
  84. package/src/bls12-381.ts +133 -139
  85. package/src/bn254.ts +69 -93
  86. package/src/ed25519.ts +106 -133
  87. package/src/ed448.ts +111 -138
  88. package/src/index.ts +19 -3
  89. package/src/misc.ts +68 -51
  90. package/src/nist.ts +77 -70
  91. package/src/secp256k1.ts +46 -81
  92. package/src/utils.ts +67 -137
  93. package/src/webcrypto.ts +403 -0
  94. package/utils.d.ts +31 -38
  95. package/utils.d.ts.map +1 -1
  96. package/utils.js +66 -185
  97. package/utils.js.map +1 -1
  98. package/webcrypto.d.ts +99 -0
  99. package/webcrypto.d.ts.map +1 -0
  100. package/webcrypto.js +256 -0
  101. package/webcrypto.js.map +1 -0
  102. package/_shortw_utils.d.ts +0 -19
  103. package/_shortw_utils.d.ts.map +0 -1
  104. package/_shortw_utils.js +0 -20
  105. package/_shortw_utils.js.map +0 -1
  106. package/abstract/utils.d.ts +0 -78
  107. package/abstract/utils.d.ts.map +0 -1
  108. package/abstract/utils.js +0 -73
  109. package/abstract/utils.js.map +0 -1
  110. package/esm/_shortw_utils.d.ts +0 -19
  111. package/esm/_shortw_utils.d.ts.map +0 -1
  112. package/esm/_shortw_utils.js +0 -16
  113. package/esm/_shortw_utils.js.map +0 -1
  114. package/esm/abstract/bls.d.ts +0 -190
  115. package/esm/abstract/bls.d.ts.map +0 -1
  116. package/esm/abstract/bls.js +0 -408
  117. package/esm/abstract/bls.js.map +0 -1
  118. package/esm/abstract/curve.d.ts +0 -231
  119. package/esm/abstract/curve.d.ts.map +0 -1
  120. package/esm/abstract/curve.js +0 -465
  121. package/esm/abstract/curve.js.map +0 -1
  122. package/esm/abstract/edwards.d.ts +0 -243
  123. package/esm/abstract/edwards.d.ts.map +0 -1
  124. package/esm/abstract/edwards.js +0 -627
  125. package/esm/abstract/edwards.js.map +0 -1
  126. package/esm/abstract/fft.d.ts +0 -122
  127. package/esm/abstract/fft.d.ts.map +0 -1
  128. package/esm/abstract/fft.js +0 -425
  129. package/esm/abstract/fft.js.map +0 -1
  130. package/esm/abstract/hash-to-curve.d.ts +0 -102
  131. package/esm/abstract/hash-to-curve.d.ts.map +0 -1
  132. package/esm/abstract/hash-to-curve.js +0 -203
  133. package/esm/abstract/hash-to-curve.js.map +0 -1
  134. package/esm/abstract/modular.d.ts +0 -171
  135. package/esm/abstract/modular.d.ts.map +0 -1
  136. package/esm/abstract/modular.js +0 -530
  137. package/esm/abstract/modular.js.map +0 -1
  138. package/esm/abstract/montgomery.d.ts +0 -30
  139. package/esm/abstract/montgomery.d.ts.map +0 -1
  140. package/esm/abstract/montgomery.js +0 -157
  141. package/esm/abstract/montgomery.js.map +0 -1
  142. package/esm/abstract/poseidon.d.ts +0 -68
  143. package/esm/abstract/poseidon.d.ts.map +0 -1
  144. package/esm/abstract/poseidon.js +0 -296
  145. package/esm/abstract/poseidon.js.map +0 -1
  146. package/esm/abstract/tower.d.ts +0 -95
  147. package/esm/abstract/tower.d.ts.map +0 -1
  148. package/esm/abstract/tower.js +0 -714
  149. package/esm/abstract/tower.js.map +0 -1
  150. package/esm/abstract/utils.d.ts +0 -78
  151. package/esm/abstract/utils.d.ts.map +0 -1
  152. package/esm/abstract/utils.js +0 -70
  153. package/esm/abstract/utils.js.map +0 -1
  154. package/esm/abstract/weierstrass.d.ts +0 -416
  155. package/esm/abstract/weierstrass.d.ts.map +0 -1
  156. package/esm/abstract/weierstrass.js +0 -1413
  157. package/esm/abstract/weierstrass.js.map +0 -1
  158. package/esm/bls12-381.d.ts +0 -16
  159. package/esm/bls12-381.d.ts.map +0 -1
  160. package/esm/bls12-381.js +0 -705
  161. package/esm/bls12-381.js.map +0 -1
  162. package/esm/bn254.d.ts +0 -18
  163. package/esm/bn254.d.ts.map +0 -1
  164. package/esm/bn254.js +0 -214
  165. package/esm/bn254.js.map +0 -1
  166. package/esm/ed25519.d.ts +0 -106
  167. package/esm/ed25519.d.ts.map +0 -1
  168. package/esm/ed25519.js +0 -467
  169. package/esm/ed25519.js.map +0 -1
  170. package/esm/ed448.d.ts +0 -100
  171. package/esm/ed448.d.ts.map +0 -1
  172. package/esm/ed448.js +0 -459
  173. package/esm/ed448.js.map +0 -1
  174. package/esm/index.d.ts +0 -2
  175. package/esm/index.d.ts.map +0 -1
  176. package/esm/index.js +0 -17
  177. package/esm/index.js.map +0 -1
  178. package/esm/jubjub.d.ts +0 -12
  179. package/esm/jubjub.d.ts.map +0 -1
  180. package/esm/jubjub.js +0 -12
  181. package/esm/jubjub.js.map +0 -1
  182. package/esm/misc.d.ts +0 -19
  183. package/esm/misc.d.ts.map +0 -1
  184. package/esm/misc.js +0 -109
  185. package/esm/misc.js.map +0 -1
  186. package/esm/nist.d.ts +0 -21
  187. package/esm/nist.d.ts.map +0 -1
  188. package/esm/nist.js +0 -132
  189. package/esm/nist.js.map +0 -1
  190. package/esm/p256.d.ts +0 -16
  191. package/esm/p256.d.ts.map +0 -1
  192. package/esm/p256.js +0 -16
  193. package/esm/p256.js.map +0 -1
  194. package/esm/p384.d.ts +0 -16
  195. package/esm/p384.d.ts.map +0 -1
  196. package/esm/p384.js +0 -16
  197. package/esm/p384.js.map +0 -1
  198. package/esm/p521.d.ts +0 -16
  199. package/esm/p521.d.ts.map +0 -1
  200. package/esm/p521.js +0 -16
  201. package/esm/p521.js.map +0 -1
  202. package/esm/package.json +0 -4
  203. package/esm/pasta.d.ts +0 -10
  204. package/esm/pasta.d.ts.map +0 -1
  205. package/esm/pasta.js +0 -10
  206. package/esm/pasta.js.map +0 -1
  207. package/esm/secp256k1.d.ts +0 -89
  208. package/esm/secp256k1.d.ts.map +0 -1
  209. package/esm/secp256k1.js +0 -294
  210. package/esm/secp256k1.js.map +0 -1
  211. package/esm/utils.d.ts +0 -110
  212. package/esm/utils.d.ts.map +0 -1
  213. package/esm/utils.js +0 -322
  214. package/esm/utils.js.map +0 -1
  215. package/jubjub.d.ts +0 -12
  216. package/jubjub.d.ts.map +0 -1
  217. package/jubjub.js +0 -15
  218. package/jubjub.js.map +0 -1
  219. package/p256.d.ts +0 -16
  220. package/p256.d.ts.map +0 -1
  221. package/p256.js +0 -13
  222. package/p256.js.map +0 -1
  223. package/p384.d.ts +0 -16
  224. package/p384.d.ts.map +0 -1
  225. package/p384.js +0 -13
  226. package/p384.js.map +0 -1
  227. package/p521.d.ts +0 -16
  228. package/p521.d.ts.map +0 -1
  229. package/p521.js +0 -13
  230. package/p521.js.map +0 -1
  231. package/pasta.d.ts +0 -10
  232. package/pasta.d.ts.map +0 -1
  233. package/pasta.js +0 -13
  234. package/pasta.js.map +0 -1
  235. package/src/_shortw_utils.ts +0 -21
  236. package/src/abstract/utils.ts +0 -80
  237. package/src/jubjub.ts +0 -12
  238. package/src/p256.ts +0 -15
  239. package/src/p384.ts +0 -15
  240. package/src/p521.ts +0 -15
  241. package/src/package.json +0 -3
  242. package/src/pasta.ts +0 -9
package/src/nist.ts CHANGED
@@ -5,18 +5,21 @@
5
5
  */
6
6
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
7
7
  import { sha256, sha384, sha512 } from '@noble/hashes/sha2.js';
8
- import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
9
8
  import { createHasher, type H2CHasher } from './abstract/hash-to-curve.ts';
10
9
  import { Field } from './abstract/modular.ts';
10
+ import { createORPF, type OPRF } from './abstract/oprf.ts';
11
11
  import {
12
+ ecdsa,
12
13
  mapToCurveSimpleSWU,
14
+ weierstrass,
15
+ type ECDSA,
13
16
  type WeierstrassOpts,
14
17
  type WeierstrassPointCons,
15
18
  } from './abstract/weierstrass.ts';
16
19
 
17
20
  // p = 2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n - 1n
18
21
  // a = Fp256.create(BigInt('-3'));
19
- const p256_CURVE: WeierstrassOpts<bigint> = {
22
+ const p256_CURVE: WeierstrassOpts<bigint> = /* @__PURE__ */ (() => ({
20
23
  p: BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'),
21
24
  n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
22
25
  h: BigInt(1),
@@ -24,10 +27,10 @@ const p256_CURVE: WeierstrassOpts<bigint> = {
24
27
  b: BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b'),
25
28
  Gx: BigInt('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'),
26
29
  Gy: BigInt('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'),
27
- };
30
+ }))();
28
31
 
29
32
  // p = 2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n
30
- const p384_CURVE: WeierstrassOpts<bigint> = {
33
+ const p384_CURVE: WeierstrassOpts<bigint> = /* @__PURE__ */ (() => ({
31
34
  p: BigInt(
32
35
  '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff'
33
36
  ),
@@ -47,10 +50,10 @@ const p384_CURVE: WeierstrassOpts<bigint> = {
47
50
  Gy: BigInt(
48
51
  '0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f'
49
52
  ),
50
- };
53
+ }))();
51
54
 
52
55
  // p = 2n**521n - 1n
53
- const p521_CURVE: WeierstrassOpts<bigint> = {
56
+ const p521_CURVE: WeierstrassOpts<bigint> = /* @__PURE__ */ (() => ({
54
57
  p: BigInt(
55
58
  '0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
56
59
  ),
@@ -70,34 +73,45 @@ const p521_CURVE: WeierstrassOpts<bigint> = {
70
73
  Gy: BigInt(
71
74
  '0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'
72
75
  ),
73
- };
76
+ }))();
74
77
 
75
- const Fp256 = Field(p256_CURVE.p);
76
- const Fp384 = Field(p384_CURVE.p);
77
- const Fp521 = Field(p521_CURVE.p);
78
78
  type SwuOpts = {
79
79
  A: bigint;
80
80
  B: bigint;
81
81
  Z: bigint;
82
82
  };
83
+
83
84
  function createSWU(Point: WeierstrassPointCons<bigint>, opts: SwuOpts) {
84
85
  const map = mapToCurveSimpleSWU(Point.Fp, opts);
85
86
  return (scalars: bigint[]) => map(scalars[0]);
86
87
  }
87
88
 
88
- /** NIST P256 (aka secp256r1, prime256v1) curve, ECDSA and ECDH methods. */
89
- export const p256: CurveFnWithCreate = createCurve(
90
- { ...p256_CURVE, Fp: Fp256, lowS: false },
91
- sha256
92
- );
89
+ // NIST P256
90
+ const p256_Point = /* @__PURE__ */ weierstrass(p256_CURVE);
91
+ /**
92
+ * NIST P256 (aka secp256r1, prime256v1) curve, ECDSA and ECDH methods.
93
+ * Hashes inputs with sha256 by default.
94
+ *
95
+ * @example
96
+ * ```js
97
+ * import { p256 } from '@noble/curves/nist.js';
98
+ * const { secretKey, publicKey } = p256.keygen();
99
+ * // const publicKey = p256.getPublicKey(secretKey);
100
+ * const msg = new TextEncoder().encode('hello noble');
101
+ * const sig = p256.sign(msg, secretKey);
102
+ * const isValid = p256.verify(sig, msg, publicKey);
103
+ * // const sigKeccak = p256.sign(keccak256(msg), secretKey, { prehash: false });
104
+ * ```
105
+ */
106
+ export const p256: ECDSA = /* @__PURE__ */ ecdsa(p256_Point, sha256);
93
107
  /** Hashing / encoding to p256 points / field. RFC 9380 methods. */
94
- export const p256_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
108
+ export const p256_hasher: H2CHasher<WeierstrassPointCons<bigint>> = /* @__PURE__ */ (() => {
95
109
  return createHasher(
96
- p256.Point,
97
- createSWU(p256.Point, {
110
+ p256_Point,
111
+ createSWU(p256_Point, {
98
112
  A: p256_CURVE.a,
99
113
  B: p256_CURVE.b,
100
- Z: p256.Point.Fp.create(BigInt('-10')),
114
+ Z: p256_Point.Fp.create(BigInt('-10')),
101
115
  }),
102
116
  {
103
117
  DST: 'P256_XMD:SHA-256_SSWU_RO_',
@@ -110,28 +124,28 @@ export const p256_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
110
124
  }
111
125
  );
112
126
  })();
127
+ /** p256 OPRF, defined in RFC 9497. */
128
+ export const p256_oprf: OPRF = /* @__PURE__ */ (() =>
129
+ createORPF({
130
+ name: 'P256-SHA256',
131
+ Point: p256_Point,
132
+ hash: sha256,
133
+ hashToGroup: p256_hasher.hashToCurve,
134
+ hashToScalar: p256_hasher.hashToScalar,
135
+ }))();
113
136
 
114
- // export const p256_oprf: OPRF = createORPF({
115
- // name: 'P256-SHA256',
116
- // Point: p256.Point,
117
- // hash: sha256,
118
- // hashToGroup: p256_hasher.hashToCurve,
119
- // hashToScalar: p256_hasher.hashToScalar,
120
- // });
121
-
122
- /** NIST P384 (aka secp384r1) curve, ECDSA and ECDH methods. */
123
- export const p384: CurveFnWithCreate = createCurve(
124
- { ...p384_CURVE, Fp: Fp384, lowS: false },
125
- sha384
126
- );
137
+ // NIST P384
138
+ const p384_Point = /* @__PURE__ */ weierstrass(p384_CURVE);
139
+ /** NIST P384 (aka secp384r1) curve, ECDSA and ECDH methods. Hashes inputs with sha384 by default. */
140
+ export const p384: ECDSA = /* @__PURE__ */ ecdsa(p384_Point, sha384);
127
141
  /** Hashing / encoding to p384 points / field. RFC 9380 methods. */
128
- export const p384_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
142
+ export const p384_hasher: H2CHasher<WeierstrassPointCons<bigint>> = /* @__PURE__ */ (() => {
129
143
  return createHasher(
130
- p384.Point,
131
- createSWU(p384.Point, {
144
+ p384_Point,
145
+ createSWU(p384_Point, {
132
146
  A: p384_CURVE.a,
133
147
  B: p384_CURVE.b,
134
- Z: p384.Point.Fp.create(BigInt('-12')),
148
+ Z: p384_Point.Fp.create(BigInt('-12')),
135
149
  }),
136
150
  {
137
151
  DST: 'P384_XMD:SHA-384_SSWU_RO_',
@@ -144,37 +158,29 @@ export const p384_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
144
158
  }
145
159
  );
146
160
  })();
161
+ /** p384 OPRF, defined in RFC 9497. */
162
+ export const p384_oprf: OPRF = /* @__PURE__ */ (() =>
163
+ createORPF({
164
+ name: 'P384-SHA384',
165
+ Point: p384_Point,
166
+ hash: sha384,
167
+ hashToGroup: p384_hasher.hashToCurve,
168
+ hashToScalar: p384_hasher.hashToScalar,
169
+ }))();
147
170
 
148
- // export const p384_oprf: OPRF = createORPF({
149
- // name: 'P384-SHA384',
150
- // Point: p384.Point,
151
- // hash: sha384,
152
- // hashToGroup: p384_hasher.hashToCurve,
153
- // hashToScalar: p384_hasher.hashToScalar,
154
- // });
155
-
156
- // const Fn521 = Field(p521_CURVE.n, { allowedScalarLengths: [65, 66] });
157
- /** NIST P521 (aka secp521r1) curve, ECDSA and ECDH methods. */
158
- export const p521: CurveFnWithCreate = createCurve(
159
- { ...p521_CURVE, Fp: Fp521, lowS: false, allowedPrivateKeyLengths: [130, 131, 132] },
160
- sha512
161
- );
162
-
163
- /** @deprecated use `p256` for consistency with `p256_hasher` */
164
- export const secp256r1: typeof p256 = p256;
165
- /** @deprecated use `p384` for consistency with `p384_hasher` */
166
- export const secp384r1: typeof p384 = p384;
167
- /** @deprecated use `p521` for consistency with `p521_hasher` */
168
- export const secp521r1: typeof p521 = p521;
169
-
171
+ // NIST P521
172
+ const Fn521 = /* @__PURE__ */ (() => Field(p521_CURVE.n, { allowedLengths: [65, 66] }))();
173
+ const p521_Point = /* @__PURE__ */ weierstrass(p521_CURVE, { Fn: Fn521 });
174
+ /** NIST P521 (aka secp521r1) curve, ECDSA and ECDH methods. Hashes inputs with sha512 by default. */
175
+ export const p521: ECDSA = /* @__PURE__ */ ecdsa(p521_Point, sha512);
170
176
  /** Hashing / encoding to p521 points / field. RFC 9380 methods. */
171
- export const p521_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
177
+ export const p521_hasher: H2CHasher<WeierstrassPointCons<bigint>> = /* @__PURE__ */ (() => {
172
178
  return createHasher(
173
- p521.Point,
174
- createSWU(p521.Point, {
179
+ p521_Point,
180
+ createSWU(p521_Point, {
175
181
  A: p521_CURVE.a,
176
182
  B: p521_CURVE.b,
177
- Z: p521.Point.Fp.create(BigInt('-4')),
183
+ Z: p521_Point.Fp.create(BigInt('-4')),
178
184
  }),
179
185
  {
180
186
  DST: 'P521_XMD:SHA-512_SSWU_RO_',
@@ -187,11 +193,12 @@ export const p521_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
187
193
  }
188
194
  );
189
195
  })();
190
-
191
- // export const p521_oprf: OPRF = createORPF({
192
- // name: 'P521-SHA512',
193
- // Point: p521.Point,
194
- // hash: sha512,
195
- // hashToGroup: p521_hasher.hashToCurve,
196
- // hashToScalar: p521_hasher.hashToScalar, // produces L=98 just like in RFC
197
- // });
196
+ /** p521 OPRF, defined in RFC 9497. */
197
+ export const p521_oprf: OPRF = /* @__PURE__ */ (() =>
198
+ createORPF({
199
+ name: 'P521-SHA512',
200
+ Point: p521_Point,
201
+ hash: sha512,
202
+ hashToGroup: p521_hasher.hashToCurve,
203
+ hashToScalar: p521_hasher.hashToScalar, // produces L=98 just like in RFC
204
+ }))();
package/src/secp256k1.ts CHANGED
@@ -8,35 +8,23 @@
8
8
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
9
9
  import { sha256 } from '@noble/hashes/sha2.js';
10
10
  import { randomBytes } from '@noble/hashes/utils.js';
11
- import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
12
- import type { CurveLengths } from './abstract/curve.ts';
11
+ import { createKeygen, type CurveLengths } from './abstract/curve.ts';
12
+ import { createHasher, type H2CHasher, isogenyMap } from './abstract/hash-to-curve.ts';
13
+ import { Field, mapHashToField, pow2 } from './abstract/modular.ts';
13
14
  import {
14
- createHasher,
15
- type H2CHasher,
16
- type H2CMethod,
17
- isogenyMap,
18
- } from './abstract/hash-to-curve.ts';
19
- import { Field, mapHashToField, mod, pow2 } from './abstract/modular.ts';
20
- import {
21
- _normFnElement,
15
+ type ECDSA,
16
+ ecdsa,
22
17
  type EndomorphismOpts,
23
18
  mapToCurveSimpleSWU,
24
19
  type WeierstrassPoint as PointType,
20
+ weierstrass,
25
21
  type WeierstrassOpts,
26
22
  type WeierstrassPointCons,
27
23
  } from './abstract/weierstrass.ts';
28
- import type { Hex, PrivKey } from './utils.ts';
29
- import {
30
- bytesToNumberBE,
31
- concatBytes,
32
- ensureBytes,
33
- inRange,
34
- numberToBytesBE,
35
- utf8ToBytes,
36
- } from './utils.ts';
24
+ import { abytes, asciiToBytes, bytesToNumberBE, concatBytes } from './utils.ts';
37
25
 
38
26
  // Seems like generator was produced from some seed:
39
- // `Point.BASE.multiply(Point.Fn.inv(2n, N)).toAffine().x`
27
+ // `Pointk1.BASE.multiply(Pointk1.Fn.inv(2n, N)).toAffine().x`
40
28
  // // gives short x 0x3b78ce563f89a0ed9414f5aa28ad0d96d6795f9c63n
41
29
  const secp256k1_CURVE: WeierstrassOpts<bigint> = {
42
30
  p: BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f'),
@@ -57,7 +45,6 @@ const secp256k1_ENDO: EndomorphismOpts = {
57
45
  };
58
46
 
59
47
  const _0n = /* @__PURE__ */ BigInt(0);
60
- const _1n = /* @__PURE__ */ BigInt(1);
61
48
  const _2n = /* @__PURE__ */ BigInt(2);
62
49
 
63
50
  /**
@@ -89,25 +76,29 @@ function sqrtMod(y: bigint): bigint {
89
76
  }
90
77
 
91
78
  const Fpk1 = Field(secp256k1_CURVE.p, { sqrt: sqrtMod });
79
+ const Pointk1 = /* @__PURE__ */ weierstrass(secp256k1_CURVE, {
80
+ Fp: Fpk1,
81
+ endo: secp256k1_ENDO,
82
+ });
92
83
 
93
84
  /**
94
- * secp256k1 curve, ECDSA and ECDH methods.
85
+ * secp256k1 curve: ECDSA and ECDH methods.
95
86
  *
96
- * Field: `2n**256n - 2n**32n - 2n**9n - 2n**8n - 2n**7n - 2n**6n - 2n**4n - 1n`
87
+ * Uses sha256 to hash messages. To use a different hash,
88
+ * pass `{ prehash: false }` to sign / verify.
97
89
  *
98
90
  * @example
99
91
  * ```js
100
- * import { secp256k1 } from '@noble/curves/secp256k1';
92
+ * import { secp256k1 } from '@noble/curves/secp256k1.js';
101
93
  * const { secretKey, publicKey } = secp256k1.keygen();
102
- * const msg = new TextEncoder().encode('hello');
94
+ * // const publicKey = secp256k1.getPublicKey(secretKey);
95
+ * const msg = new TextEncoder().encode('hello noble');
103
96
  * const sig = secp256k1.sign(msg, secretKey);
104
- * const isValid = secp256k1.verify(sig, msg, publicKey) === true;
97
+ * const isValid = secp256k1.verify(sig, msg, publicKey);
98
+ * // const sigKeccak = secp256k1.sign(keccak256(msg), secretKey, { prehash: false });
105
99
  * ```
106
100
  */
107
- export const secp256k1: CurveFnWithCreate = createCurve(
108
- { ...secp256k1_CURVE, Fp: Fpk1, lowS: true, endo: secp256k1_ENDO },
109
- sha256
110
- );
101
+ export const secp256k1: ECDSA = /* @__PURE__ */ ecdsa(Pointk1, sha256);
111
102
 
112
103
  // Schnorr signatures are superior to ECDSA from above. Below is Schnorr-specific BIP0340 code.
113
104
  // https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
@@ -116,7 +107,7 @@ const TAGGED_HASH_PREFIXES: { [tag: string]: Uint8Array } = {};
116
107
  function taggedHash(tag: string, ...messages: Uint8Array[]): Uint8Array {
117
108
  let tagP = TAGGED_HASH_PREFIXES[tag];
118
109
  if (tagP === undefined) {
119
- const tagH = sha256(utf8ToBytes(tag));
110
+ const tagH = sha256(asciiToBytes(tag));
120
111
  tagP = concatBytes(tagH, tagH);
121
112
  TAGGED_HASH_PREFIXES[tag] = tagP;
122
113
  }
@@ -125,13 +116,12 @@ function taggedHash(tag: string, ...messages: Uint8Array[]): Uint8Array {
125
116
 
126
117
  // ECDSA compact points are 33-byte. Schnorr is 32: we strip first byte 0x02 or 0x03
127
118
  const pointToBytes = (point: PointType<bigint>) => point.toBytes(true).slice(1);
128
- const Pointk1 = /* @__PURE__ */ (() => secp256k1.Point)();
129
119
  const hasEven = (y: bigint) => y % _2n === _0n;
130
120
 
131
121
  // Calculate point, scalar and bytes
132
- function schnorrGetExtPubKey(priv: PrivKey) {
122
+ function schnorrGetExtPubKey(priv: Uint8Array) {
133
123
  const { Fn, BASE } = Pointk1;
134
- const d_ = _normFnElement(Fn, priv);
124
+ const d_ = Fn.fromBytes(priv);
135
125
  const p = BASE.multiply(d_); // P = d'⋅G; 0 < d' < n check is done inside
136
126
  const scalar = hasEven(p.y) ? d_ : Fn.neg(d_);
137
127
  return { scalar, bytes: pointToBytes(p) };
@@ -164,7 +154,7 @@ function challenge(...args: Uint8Array[]): bigint {
164
154
  /**
165
155
  * Schnorr public key is just `x` coordinate of Point as per BIP340.
166
156
  */
167
- function schnorrGetPublicKey(secretKey: Hex): Uint8Array {
157
+ function schnorrGetPublicKey(secretKey: Uint8Array): Uint8Array {
168
158
  return schnorrGetExtPubKey(secretKey).bytes; // d'=int(sk). Fail if d'=0 or d'≥n. Ret bytes(d'⋅G)
169
159
  }
170
160
 
@@ -172,11 +162,15 @@ function schnorrGetPublicKey(secretKey: Hex): Uint8Array {
172
162
  * Creates Schnorr signature as per BIP340. Verifies itself before returning anything.
173
163
  * auxRand is optional and is not the sole source of k generation: bad CSPRNG won't be dangerous.
174
164
  */
175
- function schnorrSign(message: Hex, secretKey: PrivKey, auxRand: Hex = randomBytes(32)): Uint8Array {
165
+ function schnorrSign(
166
+ message: Uint8Array,
167
+ secretKey: Uint8Array,
168
+ auxRand: Uint8Array = randomBytes(32)
169
+ ): Uint8Array {
176
170
  const { Fn } = Pointk1;
177
- const m = ensureBytes('message', message);
171
+ const m = abytes(message, undefined, 'message');
178
172
  const { bytes: px, scalar: d } = schnorrGetExtPubKey(secretKey); // checks for isWithinCurveOrder
179
- const a = ensureBytes('auxRand', auxRand, 32); // Auxiliary random data a: a 32-byte array
173
+ const a = abytes(auxRand, 32, 'auxRand'); // Auxiliary random data a: a 32-byte array
180
174
  const t = Fn.toBytes(d ^ num(taggedHash('BIP0340/aux', a))); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
181
175
  const rand = taggedHash('BIP0340/nonce', t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
182
176
  // Let k' = int(rand) mod n. Fail if k' = 0. Let R = k'⋅G
@@ -194,19 +188,19 @@ function schnorrSign(message: Hex, secretKey: PrivKey, auxRand: Hex = randomByte
194
188
  * Verifies Schnorr signature.
195
189
  * Will swallow errors & return false except for initial type validation of arguments.
196
190
  */
197
- function schnorrVerify(signature: Hex, message: Hex, publicKey: Hex): boolean {
198
- const { Fn, BASE } = Pointk1;
199
- const sig = ensureBytes('signature', signature, 64);
200
- const m = ensureBytes('message', message);
201
- const pub = ensureBytes('publicKey', publicKey, 32);
191
+ function schnorrVerify(signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array): boolean {
192
+ const { Fp, Fn, BASE } = Pointk1;
193
+ const sig = abytes(signature, 64, 'signature');
194
+ const m = abytes(message, undefined, 'message');
195
+ const pub = abytes(publicKey, 32, 'publicKey');
202
196
  try {
203
197
  const P = lift_x(num(pub)); // P = lift_x(int(pk)); fail if that fails
204
198
  const r = num(sig.subarray(0, 32)); // Let r = int(sig[0:32]); fail if r ≥ p.
205
- if (!inRange(r, _1n, secp256k1_CURVE.p)) return false;
199
+ if (!Fp.isValidNot0(r)) return false;
206
200
  const s = num(sig.subarray(32, 64)); // Let s = int(sig[32:64]); fail if s ≥ n.
207
- if (!inRange(s, _1n, secp256k1_CURVE.n)) return false;
208
- // int(challenge(bytes(r)||bytes(P)||m))%n
209
- const e = challenge(Fn.toBytes(r), pointToBytes(P), m);
201
+ if (!Fn.isValidNot0(s)) return false;
202
+
203
+ const e = challenge(Fn.toBytes(r), pointToBytes(P), m); // int(challenge(bytes(r)||bytes(P)||m))%n
210
204
  // R = s⋅G - e⋅P, where -eP == (n-e)P
211
205
  const R = BASE.multiplyUnsafe(s).add(P.multiplyUnsafe(Fn.neg(e)));
212
206
  const { x, y } = R.toAffine();
@@ -229,15 +223,6 @@ export type SecpSchnorr = {
229
223
  pointToBytes: (point: PointType<bigint>) => Uint8Array;
230
224
  lift_x: typeof lift_x;
231
225
  taggedHash: typeof taggedHash;
232
-
233
- /** @deprecated use `randomSecretKey` */
234
- randomPrivateKey: (seed?: Uint8Array) => Uint8Array;
235
- /** @deprecated use `utils` */
236
- numberToBytesBE: typeof numberToBytesBE;
237
- /** @deprecated use `utils` */
238
- bytesToNumberBE: typeof bytesToNumberBE;
239
- /** @deprecated use `modular` */
240
- mod: typeof mod;
241
226
  };
242
227
  lengths: CurveLengths;
243
228
  };
@@ -246,7 +231,7 @@ export type SecpSchnorr = {
246
231
  * https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
247
232
  * @example
248
233
  * ```js
249
- * import { schnorr } from '@noble/curves/secp256k1';
234
+ * import { schnorr } from '@noble/curves/secp256k1.js';
250
235
  * const { secretKey, publicKey } = schnorr.keygen();
251
236
  * // const publicKey = schnorr.getPublicKey(secretKey);
252
237
  * const msg = new TextEncoder().encode('hello');
@@ -260,29 +245,17 @@ export const schnorr: SecpSchnorr = /* @__PURE__ */ (() => {
260
245
  const randomSecretKey = (seed = randomBytes(seedLength)): Uint8Array => {
261
246
  return mapHashToField(seed, secp256k1_CURVE.n);
262
247
  };
263
- // TODO: remove
264
- secp256k1.utils.randomSecretKey;
265
- function keygen(seed?: Uint8Array) {
266
- const secretKey = randomSecretKey(seed);
267
- return { secretKey, publicKey: schnorrGetPublicKey(secretKey) };
268
- }
269
248
  return {
270
- keygen,
249
+ keygen: createKeygen(randomSecretKey, schnorrGetPublicKey),
271
250
  getPublicKey: schnorrGetPublicKey,
272
251
  sign: schnorrSign,
273
252
  verify: schnorrVerify,
274
253
  Point: Pointk1,
275
254
  utils: {
276
- randomSecretKey: randomSecretKey,
277
- randomPrivateKey: randomSecretKey,
255
+ randomSecretKey,
278
256
  taggedHash,
279
-
280
- // TODO: remove
281
257
  lift_x,
282
258
  pointToBytes,
283
- numberToBytesBE,
284
- bytesToNumberBE,
285
- mod,
286
259
  },
287
260
  lengths: {
288
261
  secretKey: size,
@@ -335,9 +308,9 @@ const mapSWU = /* @__PURE__ */ (() =>
335
308
  }))();
336
309
 
337
310
  /** Hashing / encoding to secp256k1 points / field. RFC 9380 methods. */
338
- export const secp256k1_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() =>
311
+ export const secp256k1_hasher: H2CHasher<WeierstrassPointCons<bigint>> = /* @__PURE__ */ (() =>
339
312
  createHasher(
340
- secp256k1.Point,
313
+ Pointk1,
341
314
  (scalars: bigint[]) => {
342
315
  const { x, y } = mapSWU(Fpk1.create(scalars[0]));
343
316
  return isoMap(x, y);
@@ -352,11 +325,3 @@ export const secp256k1_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() =>
352
325
  hash: sha256,
353
326
  }
354
327
  ))();
355
-
356
- /** @deprecated use `import { secp256k1_hasher } from '@noble/curves/secp256k1.js';` */
357
- export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
358
- secp256k1_hasher.hashToCurve)();
359
-
360
- /** @deprecated use `import { secp256k1_hasher } from '@noble/curves/secp256k1.js';` */
361
- export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
362
- secp256k1_hasher.encodeToCurve)();