@noble/curves 1.9.6 → 2.0.0-beta.1

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 (210) hide show
  1. package/README.md +267 -421
  2. package/abstract/bls.d.ts +49 -111
  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 +4 -45
  7. package/abstract/curve.d.ts.map +1 -1
  8. package/abstract/curve.js +22 -47
  9. package/abstract/curve.js.map +1 -1
  10. package/abstract/edwards.d.ts +11 -68
  11. package/abstract/edwards.d.ts.map +1 -1
  12. package/abstract/edwards.js +62 -134
  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 +11 -24
  17. package/abstract/hash-to-curve.d.ts.map +1 -1
  18. package/abstract/hash-to-curve.js +30 -35
  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 +166 -167
  23. package/abstract/modular.js.map +1 -1
  24. package/abstract/montgomery.d.ts +4 -9
  25. package/abstract/montgomery.d.ts.map +1 -1
  26. package/abstract/montgomery.js +15 -18
  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.js +20 -24
  33. package/abstract/poseidon.js.map +1 -1
  34. package/abstract/tower.d.ts.map +1 -1
  35. package/abstract/tower.js +43 -19
  36. package/abstract/tower.js.map +1 -1
  37. package/abstract/weierstrass.d.ts +11 -145
  38. package/abstract/weierstrass.d.ts.map +1 -1
  39. package/abstract/weierstrass.js +122 -331
  40. package/abstract/weierstrass.js.map +1 -1
  41. package/bls12-381.d.ts +2 -2
  42. package/bls12-381.d.ts.map +1 -1
  43. package/bls12-381.js +171 -180
  44. package/bls12-381.js.map +1 -1
  45. package/bn254.d.ts +58 -10
  46. package/bn254.d.ts.map +1 -1
  47. package/bn254.js +69 -97
  48. package/bn254.js.map +1 -1
  49. package/ed25519.d.ts +12 -31
  50. package/ed25519.d.ts.map +1 -1
  51. package/ed25519.js +95 -137
  52. package/ed25519.js.map +1 -1
  53. package/ed448.d.ts +11 -29
  54. package/ed448.d.ts.map +1 -1
  55. package/ed448.js +85 -123
  56. package/ed448.js.map +1 -1
  57. package/index.js +1 -1
  58. package/misc.d.ts +10 -14
  59. package/misc.d.ts.map +1 -1
  60. package/misc.js +51 -60
  61. package/misc.js.map +1 -1
  62. package/nist.d.ts +11 -14
  63. package/nist.d.ts.map +1 -1
  64. package/nist.js +46 -55
  65. package/nist.js.map +1 -1
  66. package/package.json +8 -223
  67. package/secp256k1.d.ts +7 -23
  68. package/secp256k1.d.ts.map +1 -1
  69. package/secp256k1.js +47 -60
  70. package/secp256k1.js.map +1 -1
  71. package/src/abstract/bls.ts +197 -344
  72. package/src/abstract/curve.ts +7 -80
  73. package/src/abstract/edwards.ts +49 -171
  74. package/src/abstract/hash-to-curve.ts +32 -45
  75. package/src/abstract/modular.ts +144 -130
  76. package/src/abstract/montgomery.ts +19 -20
  77. package/src/abstract/oprf.ts +600 -0
  78. package/src/abstract/tower.ts +0 -3
  79. package/src/abstract/weierstrass.ts +69 -438
  80. package/src/bls12-381.ts +143 -138
  81. package/src/bn254.ts +65 -89
  82. package/src/ed25519.ts +53 -105
  83. package/src/ed448.ts +32 -77
  84. package/src/index.ts +1 -1
  85. package/src/misc.ts +66 -49
  86. package/src/nist.ts +48 -57
  87. package/src/secp256k1.ts +32 -65
  88. package/src/utils.ts +41 -61
  89. package/src/webcrypto.ts +362 -0
  90. package/utils.d.ts +28 -19
  91. package/utils.d.ts.map +1 -1
  92. package/utils.js +45 -121
  93. package/utils.js.map +1 -1
  94. package/webcrypto.d.ts +47 -0
  95. package/webcrypto.d.ts.map +1 -0
  96. package/webcrypto.js +231 -0
  97. package/webcrypto.js.map +1 -0
  98. package/esm/_shortw_utils.d.ts +0 -19
  99. package/esm/_shortw_utils.d.ts.map +0 -1
  100. package/esm/_shortw_utils.js +0 -16
  101. package/esm/_shortw_utils.js.map +0 -1
  102. package/esm/abstract/bls.d.ts +0 -190
  103. package/esm/abstract/bls.d.ts.map +0 -1
  104. package/esm/abstract/bls.js +0 -408
  105. package/esm/abstract/bls.js.map +0 -1
  106. package/esm/abstract/curve.d.ts +0 -231
  107. package/esm/abstract/curve.d.ts.map +0 -1
  108. package/esm/abstract/curve.js +0 -465
  109. package/esm/abstract/curve.js.map +0 -1
  110. package/esm/abstract/edwards.d.ts +0 -243
  111. package/esm/abstract/edwards.d.ts.map +0 -1
  112. package/esm/abstract/edwards.js +0 -627
  113. package/esm/abstract/edwards.js.map +0 -1
  114. package/esm/abstract/fft.d.ts +0 -122
  115. package/esm/abstract/fft.d.ts.map +0 -1
  116. package/esm/abstract/fft.js +0 -425
  117. package/esm/abstract/fft.js.map +0 -1
  118. package/esm/abstract/hash-to-curve.d.ts +0 -102
  119. package/esm/abstract/hash-to-curve.d.ts.map +0 -1
  120. package/esm/abstract/hash-to-curve.js +0 -203
  121. package/esm/abstract/hash-to-curve.js.map +0 -1
  122. package/esm/abstract/modular.d.ts +0 -171
  123. package/esm/abstract/modular.d.ts.map +0 -1
  124. package/esm/abstract/modular.js +0 -530
  125. package/esm/abstract/modular.js.map +0 -1
  126. package/esm/abstract/montgomery.d.ts +0 -30
  127. package/esm/abstract/montgomery.d.ts.map +0 -1
  128. package/esm/abstract/montgomery.js +0 -157
  129. package/esm/abstract/montgomery.js.map +0 -1
  130. package/esm/abstract/poseidon.d.ts +0 -68
  131. package/esm/abstract/poseidon.d.ts.map +0 -1
  132. package/esm/abstract/poseidon.js +0 -296
  133. package/esm/abstract/poseidon.js.map +0 -1
  134. package/esm/abstract/tower.d.ts +0 -95
  135. package/esm/abstract/tower.d.ts.map +0 -1
  136. package/esm/abstract/tower.js +0 -714
  137. package/esm/abstract/tower.js.map +0 -1
  138. package/esm/abstract/utils.d.ts +0 -5
  139. package/esm/abstract/utils.d.ts.map +0 -1
  140. package/esm/abstract/utils.js +0 -7
  141. package/esm/abstract/utils.js.map +0 -1
  142. package/esm/abstract/weierstrass.d.ts +0 -413
  143. package/esm/abstract/weierstrass.d.ts.map +0 -1
  144. package/esm/abstract/weierstrass.js +0 -1413
  145. package/esm/abstract/weierstrass.js.map +0 -1
  146. package/esm/bls12-381.d.ts +0 -16
  147. package/esm/bls12-381.d.ts.map +0 -1
  148. package/esm/bls12-381.js +0 -705
  149. package/esm/bls12-381.js.map +0 -1
  150. package/esm/bn254.d.ts +0 -18
  151. package/esm/bn254.d.ts.map +0 -1
  152. package/esm/bn254.js +0 -214
  153. package/esm/bn254.js.map +0 -1
  154. package/esm/ed25519.d.ts +0 -106
  155. package/esm/ed25519.d.ts.map +0 -1
  156. package/esm/ed25519.js +0 -467
  157. package/esm/ed25519.js.map +0 -1
  158. package/esm/ed448.d.ts +0 -100
  159. package/esm/ed448.d.ts.map +0 -1
  160. package/esm/ed448.js +0 -459
  161. package/esm/ed448.js.map +0 -1
  162. package/esm/index.d.ts +0 -2
  163. package/esm/index.d.ts.map +0 -1
  164. package/esm/index.js +0 -17
  165. package/esm/index.js.map +0 -1
  166. package/esm/jubjub.d.ts +0 -12
  167. package/esm/jubjub.d.ts.map +0 -1
  168. package/esm/jubjub.js +0 -12
  169. package/esm/jubjub.js.map +0 -1
  170. package/esm/misc.d.ts +0 -19
  171. package/esm/misc.d.ts.map +0 -1
  172. package/esm/misc.js +0 -109
  173. package/esm/misc.js.map +0 -1
  174. package/esm/nist.d.ts +0 -21
  175. package/esm/nist.d.ts.map +0 -1
  176. package/esm/nist.js +0 -132
  177. package/esm/nist.js.map +0 -1
  178. package/esm/p256.d.ts +0 -16
  179. package/esm/p256.d.ts.map +0 -1
  180. package/esm/p256.js +0 -16
  181. package/esm/p256.js.map +0 -1
  182. package/esm/p384.d.ts +0 -16
  183. package/esm/p384.d.ts.map +0 -1
  184. package/esm/p384.js +0 -16
  185. package/esm/p384.js.map +0 -1
  186. package/esm/p521.d.ts +0 -16
  187. package/esm/p521.d.ts.map +0 -1
  188. package/esm/p521.js +0 -16
  189. package/esm/p521.js.map +0 -1
  190. package/esm/package.json +0 -4
  191. package/esm/pasta.d.ts +0 -10
  192. package/esm/pasta.d.ts.map +0 -1
  193. package/esm/pasta.js +0 -10
  194. package/esm/pasta.js.map +0 -1
  195. package/esm/secp256k1.d.ts +0 -89
  196. package/esm/secp256k1.d.ts.map +0 -1
  197. package/esm/secp256k1.js +0 -294
  198. package/esm/secp256k1.js.map +0 -1
  199. package/esm/utils.d.ts +0 -110
  200. package/esm/utils.d.ts.map +0 -1
  201. package/esm/utils.js +0 -322
  202. package/esm/utils.js.map +0 -1
  203. package/src/_shortw_utils.ts +0 -21
  204. package/src/abstract/utils.ts +0 -7
  205. package/src/jubjub.ts +0 -12
  206. package/src/p256.ts +0 -15
  207. package/src/p384.ts +0 -15
  208. package/src/p521.ts +0 -15
  209. package/src/package.json +0 -3
  210. package/src/pasta.ts +0 -9
package/src/misc.ts CHANGED
@@ -6,18 +6,19 @@
6
6
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
7
7
  import { blake256 } from '@noble/hashes/blake1.js';
8
8
  import { blake2s } from '@noble/hashes/blake2.js';
9
- import { sha256, sha512 } from '@noble/hashes/sha2.js';
10
- import { concatBytes, utf8ToBytes } from '@noble/hashes/utils.js';
9
+ import { sha256, sha384, sha512 } from '@noble/hashes/sha2.js';
10
+ import { concatBytes } from '@noble/hashes/utils.js';
11
11
  import {
12
- twistedEdwards,
13
- type CurveFn,
12
+ eddsa,
13
+ edwards,
14
+ type EdDSA,
14
15
  type EdwardsOpts,
15
16
  type EdwardsPoint,
16
17
  } from './abstract/edwards.ts';
17
- import { Field, mod } from './abstract/modular.ts';
18
- import { weierstrass, type CurveFn as WCurveFn } from './abstract/weierstrass.ts';
18
+ import { ecdsa, weierstrass, type ECDSA, type WeierstrassOpts } from './abstract/weierstrass.ts';
19
19
  import { bls12_381_Fr } from './bls12-381.ts';
20
20
  import { bn254_Fr } from './bn254.ts';
21
+ import { asciiToBytes } from './utils.ts';
21
22
 
22
23
  // Jubjub curves have 𝔽p over scalar fields of other curves. They are friendly to ZK proofs.
23
24
  // jubjub Fp = bls n. babyjubjub Fp = bn254 n.
@@ -32,11 +33,7 @@ const jubjub_CURVE: EdwardsOpts = {
32
33
  Gy: BigInt('0x1d523cf1ddab1a1793132e78c866c0c33e26ba5cc220fed7cc3f870e59d292aa'),
33
34
  };
34
35
  /** Curve over scalar field of bls12-381. jubjub Fp = bls n */
35
- export const jubjub: CurveFn = /* @__PURE__ */ twistedEdwards({
36
- ...jubjub_CURVE,
37
- Fp: bls12_381_Fr,
38
- hash: sha512,
39
- });
36
+ export const jubjub: EdDSA = /* @__PURE__ */ eddsa(edwards(jubjub_CURVE), sha512);
40
37
 
41
38
  const babyjubjub_CURVE: EdwardsOpts = {
42
39
  p: bn254_Fr.ORDER,
@@ -48,13 +45,9 @@ const babyjubjub_CURVE: EdwardsOpts = {
48
45
  Gy: BigInt('0xc19139cb84c680a6e14116da06056174a0cfa121e6e5c2450f87d64fc000001'),
49
46
  };
50
47
  /** Curve over scalar field of bn254. babyjubjub Fp = bn254 n */
51
- export const babyjubjub: CurveFn = /* @__PURE__ */ twistedEdwards({
52
- ...babyjubjub_CURVE,
53
- Fp: bn254_Fr,
54
- hash: blake256,
55
- });
48
+ export const babyjubjub: EdDSA = /* @__PURE__ */ eddsa(edwards(babyjubjub_CURVE), blake256);
56
49
 
57
- const jubjub_gh_first_block = utf8ToBytes(
50
+ const jubjub_gh_first_block = asciiToBytes(
58
51
  '096b36a5804bfacef1691e173c366a47ff5ba84a44f26ddd7e8d9f79d5b42df0'
59
52
  );
60
53
 
@@ -87,38 +80,62 @@ export function jubjub_findGroupHash(m: Uint8Array, personalization: Uint8Array)
87
80
  return hashes[0];
88
81
  }
89
82
 
90
- // Pasta curves. See [Spec](https://o1-labs.github.io/proof-systems/specs/pasta.html).
91
-
92
- export const pasta_p: bigint = BigInt(
93
- '0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001'
94
- );
95
- export const pasta_q: bigint = BigInt(
96
- '0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001'
97
- );
83
+ const brainpoolP256r1_CURVE: WeierstrassOpts<bigint> = {
84
+ p: BigInt('0xa9fb57dba1eea9bc3e660a909d838d726e3bf623d52620282013481d1f6e5377'),
85
+ a: BigInt('0x7d5a0975fc2c3057eef67530417affe7fb8055c126dc5c6ce94a4b44f330b5d9'),
86
+ b: BigInt('0x26dc5c6ce94a4b44f330b5d9bbd77cbf958416295cf7e1ce6bccdc18ff8c07b6'),
87
+ n: BigInt('0xa9fb57dba1eea9bc3e660a909d838d718c397aa3b561a6f7901e0e82974856a7'),
88
+ Gx: BigInt('0x8bd2aeb9cb7e57cb2c4b482ffc81b7afb9de27e1e3bd23c23a4453bd9ace3262'),
89
+ Gy: BigInt('0x547ef835c3dac4fd97f8461a14611dc9c27745132ded8e545c1d54c72f046997'),
90
+ h: BigInt(1),
91
+ };
92
+ /** Brainpool P256r1 with sha256, from RFC 5639. */
93
+ export const brainpoolP256r1: ECDSA = ecdsa(weierstrass(brainpoolP256r1_CURVE), sha256);
98
94
 
99
- /**
100
- * @deprecated
101
- */
102
- export const pallas: WCurveFn = weierstrass({
103
- a: BigInt(0),
104
- b: BigInt(5),
105
- Fp: Field(pasta_p),
106
- n: pasta_q,
107
- Gx: mod(BigInt(-1), pasta_p),
108
- Gy: BigInt(2),
95
+ const brainpoolP384r1_CURVE: WeierstrassOpts<bigint> = {
96
+ p: BigInt(
97
+ '0x8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b412b1da197fb71123acd3a729901d1a71874700133107ec53'
98
+ ),
99
+ a: BigInt(
100
+ '0x7bc382c63d8c150c3c72080ace05afa0c2bea28e4fb22787139165efba91f90f8aa5814a503ad4eb04a8c7dd22ce2826'
101
+ ),
102
+ b: BigInt(
103
+ '0x04a8c7dd22ce28268b39b55416f0447c2fb77de107dcd2a62e880ea53eeb62d57cb4390295dbc9943ab78696fa504c11'
104
+ ),
105
+ n: BigInt(
106
+ '0x8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b31f166e6cac0425a7cf3ab6af6b7fc3103b883202e9046565'
107
+ ),
108
+ Gx: BigInt(
109
+ '0x1d1c64f068cf45ffa2a63a81b7c13f6b8847a3e77ef14fe3db7fcafe0cbd10e8e826e03436d646aaef87b2e247d4af1e'
110
+ ),
111
+ Gy: BigInt(
112
+ '0x8abe1d7520f9c2a45cb1eb8e95cfd55262b70b29feec5864e19c054ff99129280e4646217791811142820341263c5315'
113
+ ),
109
114
  h: BigInt(1),
110
- hash: sha256,
111
- });
112
- /**
113
- * @deprecated
114
- */
115
- export const vesta: WCurveFn = weierstrass({
116
- a: BigInt(0),
117
- b: BigInt(5),
118
- Fp: Field(pasta_q),
119
- n: pasta_p,
120
- Gx: mod(BigInt(-1), pasta_q),
121
- Gy: BigInt(2),
115
+ };
116
+ /** Brainpool P384r1 with sha384, from RFC 5639. */
117
+ export const brainpoolP384r1: ECDSA = ecdsa(weierstrass(brainpoolP384r1_CURVE), sha384);
118
+
119
+ const brainpoolP512r1_CURVE: WeierstrassOpts<bigint> = {
120
+ p: BigInt(
121
+ '0xaadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca703308717d4d9b009bc66842aecda12ae6a380e62881ff2f2d82c68528aa6056583a48f3'
122
+ ),
123
+ a: BigInt(
124
+ '0x7830a3318b603b89e2327145ac234cc594cbdd8d3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94ca'
125
+ ),
126
+ b: BigInt(
127
+ '0x3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94cadc083e67984050b75ebae5dd2809bd638016f723'
128
+ ),
129
+ n: BigInt(
130
+ '0xaadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca70330870553e5c414ca92619418661197fac10471db1d381085ddaddb58796829ca90069'
131
+ ),
132
+ Gx: BigInt(
133
+ '0x81aee4bdd82ed9645a21322e9c4c6a9385ed9f70b5d916c1b43b62eef4d0098eff3b1f78e2d0d48d50d1687b93b97d5f7c6d5047406a5e688b352209bcb9f822'
134
+ ),
135
+ Gy: BigInt(
136
+ '0x7dde385d566332ecc0eabfa9cf7822fdf209f70024a57b1aa000c55b881f8111b2dcde494a5f485e5bca4bd88a2763aed1ca2b2fa8f0540678cd1e0f3ad80892'
137
+ ),
122
138
  h: BigInt(1),
123
- hash: sha256,
124
- });
139
+ };
140
+ /** Brainpool P521r1 with sha512, from RFC 5639. */
141
+ export const brainpoolP512r1: ECDSA = ecdsa(weierstrass(brainpoolP512r1_CURVE), sha512);
package/src/nist.ts CHANGED
@@ -5,11 +5,14 @@
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';
@@ -72,9 +75,6 @@ const p521_CURVE: WeierstrassOpts<bigint> = {
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;
@@ -86,18 +86,17 @@ function createSWU(Point: WeierstrassPointCons<bigint>, opts: SwuOpts) {
86
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
+
90
+ const p256_Point = /* @__PURE__ */ weierstrass(p256_CURVE);
91
+ export const p256: ECDSA = /* @__PURE__ */ ecdsa(p256_Point, sha256);
93
92
  /** Hashing / encoding to p256 points / field. RFC 9380 methods. */
94
- export const p256_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
93
+ export const p256_hasher: H2CHasher<WeierstrassPointCons<bigint>> = /* @__PURE__ */ (() => {
95
94
  return createHasher(
96
- p256.Point,
97
- createSWU(p256.Point, {
95
+ p256_Point,
96
+ createSWU(p256_Point, {
98
97
  A: p256_CURVE.a,
99
98
  B: p256_CURVE.b,
100
- Z: p256.Point.Fp.create(BigInt('-10')),
99
+ Z: p256_Point.Fp.create(BigInt('-10')),
101
100
  }),
102
101
  {
103
102
  DST: 'P256_XMD:SHA-256_SSWU_RO_',
@@ -111,27 +110,26 @@ export const p256_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
111
110
  );
112
111
  })();
113
112
 
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
- // });
113
+ export const p256_oprf: OPRF = /* @__PURE__ */ (() =>
114
+ createORPF({
115
+ name: 'P256-SHA256',
116
+ Point: p256_Point,
117
+ hash: sha256,
118
+ hashToGroup: p256_hasher.hashToCurve,
119
+ hashToScalar: p256_hasher.hashToScalar,
120
+ }))();
121
121
 
122
+ const p384_Point = /* @__PURE__ */ weierstrass(p384_CURVE);
122
123
  /** 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
- );
124
+ export const p384: ECDSA = /* @__PURE__ */ ecdsa(p384_Point, sha384);
127
125
  /** Hashing / encoding to p384 points / field. RFC 9380 methods. */
128
- export const p384_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
126
+ export const p384_hasher: H2CHasher<WeierstrassPointCons<bigint>> = /* @__PURE__ */ (() => {
129
127
  return createHasher(
130
- p384.Point,
131
- createSWU(p384.Point, {
128
+ p384_Point,
129
+ createSWU(p384_Point, {
132
130
  A: p384_CURVE.a,
133
131
  B: p384_CURVE.b,
134
- Z: p384.Point.Fp.create(BigInt('-12')),
132
+ Z: p384_Point.Fp.create(BigInt('-12')),
135
133
  }),
136
134
  {
137
135
  DST: 'P384_XMD:SHA-384_SSWU_RO_',
@@ -145,36 +143,28 @@ export const p384_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
145
143
  );
146
144
  })();
147
145
 
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
- // });
146
+ export const p384_oprf: OPRF = /* @__PURE__ */ (() =>
147
+ createORPF({
148
+ name: 'P384-SHA384',
149
+ Point: p384_Point,
150
+ hash: sha384,
151
+ hashToGroup: p384_hasher.hashToCurve,
152
+ hashToScalar: p384_hasher.hashToScalar,
153
+ }))();
155
154
 
156
- // const Fn521 = Field(p521_CURVE.n, { allowedScalarLengths: [65, 66] });
155
+ const Fn521 = /* @__PURE__ */ Field(p521_CURVE.n, { allowedLengths: [65, 66] });
156
+ const p521_Point = /* @__PURE__ */ weierstrass(p521_CURVE, { Fn: Fn521 });
157
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;
158
+ export const p521: ECDSA = /* @__PURE__ */ ecdsa(p521_Point, sha512);
169
159
 
170
160
  /** Hashing / encoding to p521 points / field. RFC 9380 methods. */
171
- export const p521_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
161
+ export const p521_hasher: H2CHasher<WeierstrassPointCons<bigint>> = /* @__PURE__ */ (() => {
172
162
  return createHasher(
173
- p521.Point,
174
- createSWU(p521.Point, {
163
+ p521_Point,
164
+ createSWU(p521_Point, {
175
165
  A: p521_CURVE.a,
176
166
  B: p521_CURVE.b,
177
- Z: p521.Point.Fp.create(BigInt('-4')),
167
+ Z: p521_Point.Fp.create(BigInt('-4')),
178
168
  }),
179
169
  {
180
170
  DST: 'P521_XMD:SHA-512_SSWU_RO_',
@@ -188,10 +178,11 @@ export const p521_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
188
178
  );
189
179
  })();
190
180
 
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
- // });
181
+ export const p521_oprf: OPRF = /* @__PURE__ */ (() =>
182
+ createORPF({
183
+ name: 'P521-SHA512',
184
+ Point: p521_Point,
185
+ hash: sha512,
186
+ hashToGroup: p521_hasher.hashToCurve,
187
+ hashToScalar: p521_hasher.hashToScalar, // produces L=98 just like in RFC
188
+ }))();
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
11
  import 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, inRange } 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'),
@@ -89,6 +77,10 @@ function sqrtMod(y: bigint): bigint {
89
77
  }
90
78
 
91
79
  const Fpk1 = Field(secp256k1_CURVE.p, { sqrt: sqrtMod });
80
+ const Pointk1 = /* @__PURE__ */ weierstrass(secp256k1_CURVE, {
81
+ Fp: Fpk1,
82
+ endo: secp256k1_ENDO,
83
+ });
92
84
 
93
85
  /**
94
86
  * secp256k1 curve, ECDSA and ECDH methods.
@@ -104,10 +96,8 @@ const Fpk1 = Field(secp256k1_CURVE.p, { sqrt: sqrtMod });
104
96
  * const isValid = secp256k1.verify(sig, msg, publicKey) === true;
105
97
  * ```
106
98
  */
107
- export const secp256k1: CurveFnWithCreate = createCurve(
108
- { ...secp256k1_CURVE, Fp: Fpk1, lowS: true, endo: secp256k1_ENDO },
109
- sha256
110
- );
99
+
100
+ export const secp256k1: ECDSA = /* @__PURE__ */ ecdsa(Pointk1, sha256);
111
101
 
112
102
  // Schnorr signatures are superior to ECDSA from above. Below is Schnorr-specific BIP0340 code.
113
103
  // https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
@@ -116,7 +106,7 @@ const TAGGED_HASH_PREFIXES: { [tag: string]: Uint8Array } = {};
116
106
  function taggedHash(tag: string, ...messages: Uint8Array[]): Uint8Array {
117
107
  let tagP = TAGGED_HASH_PREFIXES[tag];
118
108
  if (tagP === undefined) {
119
- const tagH = sha256(utf8ToBytes(tag));
109
+ const tagH = sha256(asciiToBytes(tag));
120
110
  tagP = concatBytes(tagH, tagH);
121
111
  TAGGED_HASH_PREFIXES[tag] = tagP;
122
112
  }
@@ -125,13 +115,12 @@ function taggedHash(tag: string, ...messages: Uint8Array[]): Uint8Array {
125
115
 
126
116
  // ECDSA compact points are 33-byte. Schnorr is 32: we strip first byte 0x02 or 0x03
127
117
  const pointToBytes = (point: PointType<bigint>) => point.toBytes(true).slice(1);
128
- const Pointk1 = /* @__PURE__ */ (() => secp256k1.Point)();
129
118
  const hasEven = (y: bigint) => y % _2n === _0n;
130
119
 
131
120
  // Calculate point, scalar and bytes
132
- function schnorrGetExtPubKey(priv: PrivKey) {
121
+ function schnorrGetExtPubKey(priv: Uint8Array) {
133
122
  const { Fn, BASE } = Pointk1;
134
- const d_ = _normFnElement(Fn, priv);
123
+ const d_ = Fn.fromBytes(priv);
135
124
  const p = BASE.multiply(d_); // P = d'⋅G; 0 < d' < n check is done inside
136
125
  const scalar = hasEven(p.y) ? d_ : Fn.neg(d_);
137
126
  return { scalar, bytes: pointToBytes(p) };
@@ -164,7 +153,7 @@ function challenge(...args: Uint8Array[]): bigint {
164
153
  /**
165
154
  * Schnorr public key is just `x` coordinate of Point as per BIP340.
166
155
  */
167
- function schnorrGetPublicKey(secretKey: Hex): Uint8Array {
156
+ function schnorrGetPublicKey(secretKey: Uint8Array): Uint8Array {
168
157
  return schnorrGetExtPubKey(secretKey).bytes; // d'=int(sk). Fail if d'=0 or d'≥n. Ret bytes(d'⋅G)
169
158
  }
170
159
 
@@ -172,11 +161,15 @@ function schnorrGetPublicKey(secretKey: Hex): Uint8Array {
172
161
  * Creates Schnorr signature as per BIP340. Verifies itself before returning anything.
173
162
  * auxRand is optional and is not the sole source of k generation: bad CSPRNG won't be dangerous.
174
163
  */
175
- function schnorrSign(message: Hex, secretKey: PrivKey, auxRand: Hex = randomBytes(32)): Uint8Array {
164
+ function schnorrSign(
165
+ message: Uint8Array,
166
+ secretKey: Uint8Array,
167
+ auxRand: Uint8Array = randomBytes(32)
168
+ ): Uint8Array {
176
169
  const { Fn } = Pointk1;
177
- const m = ensureBytes('message', message);
170
+ const m = abytes(message, undefined, 'message');
178
171
  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
172
+ const a = abytes(auxRand, 32, 'auxRand'); // Auxiliary random data a: a 32-byte array
180
173
  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
174
  const rand = taggedHash('BIP0340/nonce', t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
182
175
  // Let k' = int(rand) mod n. Fail if k' = 0. Let R = k'⋅G
@@ -194,19 +187,18 @@ function schnorrSign(message: Hex, secretKey: PrivKey, auxRand: Hex = randomByte
194
187
  * Verifies Schnorr signature.
195
188
  * Will swallow errors & return false except for initial type validation of arguments.
196
189
  */
197
- function schnorrVerify(signature: Hex, message: Hex, publicKey: Hex): boolean {
190
+ function schnorrVerify(signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array): boolean {
198
191
  const { Fn, BASE } = Pointk1;
199
- const sig = ensureBytes('signature', signature, 64);
200
- const m = ensureBytes('message', message);
201
- const pub = ensureBytes('publicKey', publicKey, 32);
192
+ const sig = abytes(signature, 64, 'signature');
193
+ const m = abytes(message, undefined, 'message');
194
+ const pub = abytes(publicKey, 32, 'publicKey');
202
195
  try {
203
196
  const P = lift_x(num(pub)); // P = lift_x(int(pk)); fail if that fails
204
197
  const r = num(sig.subarray(0, 32)); // Let r = int(sig[0:32]); fail if r ≥ p.
205
198
  if (!inRange(r, _1n, secp256k1_CURVE.p)) return false;
206
199
  const s = num(sig.subarray(32, 64)); // Let s = int(sig[32:64]); fail if s ≥ n.
207
200
  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
+ const e = challenge(Fn.toBytes(r), pointToBytes(P), m); // int(challenge(bytes(r)||bytes(P)||m))%n
210
202
  // R = s⋅G - e⋅P, where -eP == (n-e)P
211
203
  const R = BASE.multiplyUnsafe(s).add(P.multiplyUnsafe(Fn.neg(e)));
212
204
  const { x, y } = R.toAffine();
@@ -229,15 +221,6 @@ export type SecpSchnorr = {
229
221
  pointToBytes: (point: PointType<bigint>) => Uint8Array;
230
222
  lift_x: typeof lift_x;
231
223
  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
224
  };
242
225
  lengths: CurveLengths;
243
226
  };
@@ -260,8 +243,6 @@ export const schnorr: SecpSchnorr = /* @__PURE__ */ (() => {
260
243
  const randomSecretKey = (seed = randomBytes(seedLength)): Uint8Array => {
261
244
  return mapHashToField(seed, secp256k1_CURVE.n);
262
245
  };
263
- // TODO: remove
264
- secp256k1.utils.randomSecretKey;
265
246
  function keygen(seed?: Uint8Array) {
266
247
  const secretKey = randomSecretKey(seed);
267
248
  return { secretKey, publicKey: schnorrGetPublicKey(secretKey) };
@@ -273,16 +254,10 @@ export const schnorr: SecpSchnorr = /* @__PURE__ */ (() => {
273
254
  verify: schnorrVerify,
274
255
  Point: Pointk1,
275
256
  utils: {
276
- randomSecretKey: randomSecretKey,
277
- randomPrivateKey: randomSecretKey,
257
+ randomSecretKey,
278
258
  taggedHash,
279
-
280
- // TODO: remove
281
259
  lift_x,
282
260
  pointToBytes,
283
- numberToBytesBE,
284
- bytesToNumberBE,
285
- mod,
286
261
  },
287
262
  lengths: {
288
263
  secretKey: size,
@@ -335,9 +310,9 @@ const mapSWU = /* @__PURE__ */ (() =>
335
310
  }))();
336
311
 
337
312
  /** Hashing / encoding to secp256k1 points / field. RFC 9380 methods. */
338
- export const secp256k1_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() =>
313
+ export const secp256k1_hasher: H2CHasher<WeierstrassPointCons<bigint>> = /* @__PURE__ */ (() =>
339
314
  createHasher(
340
- secp256k1.Point,
315
+ Pointk1,
341
316
  (scalars: bigint[]) => {
342
317
  const { x, y } = mapSWU(Fpk1.create(scalars[0]));
343
318
  return isoMap(x, y);
@@ -352,11 +327,3 @@ export const secp256k1_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() =>
352
327
  hash: sha256,
353
328
  }
354
329
  ))();
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)();