@noble/curves 1.9.1 → 1.9.3

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 (223) hide show
  1. package/README.md +238 -227
  2. package/_shortw_utils.d.ts +8 -5
  3. package/_shortw_utils.d.ts.map +1 -1
  4. package/_shortw_utils.js +3 -8
  5. package/_shortw_utils.js.map +1 -1
  6. package/abstract/bls.d.ts +123 -62
  7. package/abstract/bls.d.ts.map +1 -1
  8. package/abstract/bls.js +219 -163
  9. package/abstract/bls.js.map +1 -1
  10. package/abstract/curve.d.ts +142 -21
  11. package/abstract/curve.d.ts.map +1 -1
  12. package/abstract/curve.js +224 -143
  13. package/abstract/curve.js.map +1 -1
  14. package/abstract/edwards.d.ts +190 -49
  15. package/abstract/edwards.d.ts.map +1 -1
  16. package/abstract/edwards.js +322 -136
  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 +31 -13
  23. package/abstract/hash-to-curve.d.ts.map +1 -1
  24. package/abstract/hash-to-curve.js +34 -19
  25. package/abstract/hash-to-curve.js.map +1 -1
  26. package/abstract/modular.d.ts +31 -13
  27. package/abstract/modular.d.ts.map +1 -1
  28. package/abstract/modular.js +125 -52
  29. package/abstract/modular.js.map +1 -1
  30. package/abstract/montgomery.d.ts +18 -5
  31. package/abstract/montgomery.d.ts.map +1 -1
  32. package/abstract/montgomery.js +23 -6
  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 +23 -49
  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 +206 -124
  47. package/abstract/weierstrass.d.ts.map +1 -1
  48. package/abstract/weierstrass.js +747 -604
  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 +55 -66
  59. package/ed25519.d.ts.map +1 -1
  60. package/ed25519.js +172 -186
  61. package/ed25519.js.map +1 -1
  62. package/ed448.d.ts +60 -57
  63. package/ed448.d.ts.map +1 -1
  64. package/ed448.js +172 -166
  65. package/ed448.js.map +1 -1
  66. package/esm/_shortw_utils.d.ts +8 -5
  67. package/esm/_shortw_utils.d.ts.map +1 -1
  68. package/esm/_shortw_utils.js +3 -8
  69. package/esm/_shortw_utils.js.map +1 -1
  70. package/esm/abstract/bls.d.ts +123 -62
  71. package/esm/abstract/bls.d.ts.map +1 -1
  72. package/esm/abstract/bls.js +220 -164
  73. package/esm/abstract/bls.js.map +1 -1
  74. package/esm/abstract/curve.d.ts +142 -21
  75. package/esm/abstract/curve.d.ts.map +1 -1
  76. package/esm/abstract/curve.js +219 -143
  77. package/esm/abstract/curve.js.map +1 -1
  78. package/esm/abstract/edwards.d.ts +190 -49
  79. package/esm/abstract/edwards.d.ts.map +1 -1
  80. package/esm/abstract/edwards.js +320 -138
  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 +31 -13
  87. package/esm/abstract/hash-to-curve.d.ts.map +1 -1
  88. package/esm/abstract/hash-to-curve.js +33 -19
  89. package/esm/abstract/hash-to-curve.js.map +1 -1
  90. package/esm/abstract/modular.d.ts +31 -13
  91. package/esm/abstract/modular.d.ts.map +1 -1
  92. package/esm/abstract/modular.js +124 -51
  93. package/esm/abstract/modular.js.map +1 -1
  94. package/esm/abstract/montgomery.d.ts +18 -5
  95. package/esm/abstract/montgomery.d.ts.map +1 -1
  96. package/esm/abstract/montgomery.js +23 -6
  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 +23 -49
  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 +206 -124
  111. package/esm/abstract/weierstrass.d.ts.map +1 -1
  112. package/esm/abstract/weierstrass.js +743 -605
  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 +55 -66
  123. package/esm/ed25519.d.ts.map +1 -1
  124. package/esm/ed25519.js +170 -183
  125. package/esm/ed25519.js.map +1 -1
  126. package/esm/ed448.d.ts +60 -57
  127. package/esm/ed448.d.ts.map +1 -1
  128. package/esm/ed448.js +169 -162
  129. package/esm/ed448.js.map +1 -1
  130. package/esm/index.js +7 -9
  131. package/esm/index.js.map +1 -1
  132. package/esm/jubjub.d.ts +3 -3
  133. package/esm/jubjub.d.ts.map +1 -1
  134. package/esm/jubjub.js +3 -3
  135. package/esm/jubjub.js.map +1 -1
  136. package/esm/misc.d.ts +3 -5
  137. package/esm/misc.d.ts.map +1 -1
  138. package/esm/misc.js +31 -29
  139. package/esm/misc.js.map +1 -1
  140. package/esm/nist.d.ts +7 -22
  141. package/esm/nist.d.ts.map +1 -1
  142. package/esm/nist.js +106 -101
  143. package/esm/nist.js.map +1 -1
  144. package/esm/p256.d.ts +7 -3
  145. package/esm/p256.d.ts.map +1 -1
  146. package/esm/p256.js +4 -0
  147. package/esm/p256.js.map +1 -1
  148. package/esm/p384.d.ts +7 -4
  149. package/esm/p384.d.ts.map +1 -1
  150. package/esm/p384.js +4 -1
  151. package/esm/p384.js.map +1 -1
  152. package/esm/p521.d.ts +7 -3
  153. package/esm/p521.d.ts.map +1 -1
  154. package/esm/p521.js +4 -0
  155. package/esm/p521.js.map +1 -1
  156. package/esm/secp256k1.d.ts +38 -21
  157. package/esm/secp256k1.d.ts.map +1 -1
  158. package/esm/secp256k1.js +112 -104
  159. package/esm/secp256k1.js.map +1 -1
  160. package/esm/utils.d.ts +96 -0
  161. package/esm/utils.d.ts.map +1 -0
  162. package/esm/utils.js +279 -0
  163. package/esm/utils.js.map +1 -0
  164. package/index.js +7 -9
  165. package/index.js.map +1 -1
  166. package/jubjub.d.ts +3 -3
  167. package/jubjub.d.ts.map +1 -1
  168. package/jubjub.js +3 -3
  169. package/jubjub.js.map +1 -1
  170. package/misc.d.ts +3 -5
  171. package/misc.d.ts.map +1 -1
  172. package/misc.js +35 -33
  173. package/misc.js.map +1 -1
  174. package/nist.d.ts +7 -22
  175. package/nist.d.ts.map +1 -1
  176. package/nist.js +106 -101
  177. package/nist.js.map +1 -1
  178. package/p256.d.ts +7 -3
  179. package/p256.d.ts.map +1 -1
  180. package/p256.js +4 -0
  181. package/p256.js.map +1 -1
  182. package/p384.d.ts +7 -4
  183. package/p384.d.ts.map +1 -1
  184. package/p384.js +4 -1
  185. package/p384.js.map +1 -1
  186. package/p521.d.ts +7 -3
  187. package/p521.d.ts.map +1 -1
  188. package/p521.js +4 -0
  189. package/p521.js.map +1 -1
  190. package/package.json +17 -6
  191. package/secp256k1.d.ts +38 -21
  192. package/secp256k1.d.ts.map +1 -1
  193. package/secp256k1.js +112 -104
  194. package/secp256k1.js.map +1 -1
  195. package/src/_shortw_utils.ts +6 -15
  196. package/src/abstract/bls.ts +428 -251
  197. package/src/abstract/curve.ts +307 -149
  198. package/src/abstract/edwards.ts +555 -203
  199. package/src/abstract/fft.ts +30 -19
  200. package/src/abstract/hash-to-curve.ts +75 -34
  201. package/src/abstract/modular.ts +131 -59
  202. package/src/abstract/montgomery.ts +44 -15
  203. package/src/abstract/poseidon.ts +22 -18
  204. package/src/abstract/tower.ts +40 -71
  205. package/src/abstract/utils.ts +3 -378
  206. package/src/abstract/weierstrass.ts +1086 -746
  207. package/src/bls12-381.ts +549 -490
  208. package/src/bn254.ts +47 -35
  209. package/src/ed25519.ts +214 -216
  210. package/src/ed448.ts +251 -220
  211. package/src/index.ts +7 -9
  212. package/src/jubjub.ts +3 -3
  213. package/src/misc.ts +41 -40
  214. package/src/nist.ts +161 -126
  215. package/src/p256.ts +7 -3
  216. package/src/p384.ts +7 -5
  217. package/src/p521.ts +7 -3
  218. package/src/secp256k1.ts +145 -115
  219. package/src/utils.ts +328 -0
  220. package/utils.d.ts +96 -0
  221. package/utils.d.ts.map +1 -0
  222. package/utils.js +313 -0
  223. package/utils.js.map +1 -0
package/src/misc.ts CHANGED
@@ -4,75 +4,78 @@
4
4
  * @module
5
5
  */
6
6
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
7
- import { blake256 } from '@noble/hashes/blake1';
8
- import { blake2s } from '@noble/hashes/blake2';
9
- import { sha256, sha512 } from '@noble/hashes/sha2';
10
- import { concatBytes, randomBytes, utf8ToBytes } from '@noble/hashes/utils';
11
- import { getHash } from './_shortw_utils.ts';
12
- import { type CurveFn, type ExtPointType, twistedEdwards } from './abstract/edwards.ts';
7
+ import { blake256 } from '@noble/hashes/blake1.js';
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';
11
+ import {
12
+ twistedEdwards,
13
+ type CurveFn,
14
+ type EdwardsOpts,
15
+ type EdwardsPoint,
16
+ } from './abstract/edwards.ts';
13
17
  import { Field, mod } from './abstract/modular.ts';
14
- import { type CurveFn as WCurveFn, weierstrass } from './abstract/weierstrass.ts';
18
+ import { weierstrass, type CurveFn as WCurveFn } from './abstract/weierstrass.ts';
19
+ import { bls12_381_Fr } from './bls12-381.ts';
20
+ import { bn254_Fr } from './bn254.ts';
15
21
 
16
22
  // Jubjub curves have 𝔽p over scalar fields of other curves. They are friendly to ZK proofs.
17
23
  // jubjub Fp = bls n. babyjubjub Fp = bn254 n.
18
24
  // verify manually, check bls12-381.ts and bn254.ts.
19
- // https://neuromancer.sk/std/other/JubJub
20
-
21
- const bls12_381_Fr = Field(
22
- BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001')
23
- );
24
- const bn254_Fr = Field(
25
- BigInt('21888242871839275222246405745257275088548364400416034343698204186575808495617')
26
- );
27
-
28
- /** Curve over scalar field of bls12-381. jubjub Fp = bls n */
29
- export const jubjub: CurveFn = /* @__PURE__ */ twistedEdwards({
30
- a: BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000'),
31
- d: BigInt('0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1'),
32
- Fp: bls12_381_Fr,
25
+ const jubjub_CURVE: EdwardsOpts = {
26
+ p: bls12_381_Fr.ORDER,
33
27
  n: BigInt('0xe7db4ea6533afa906673b0101343b00a6682093ccc81082d0970e5ed6f72cb7'),
34
28
  h: BigInt(8),
29
+ a: BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000'),
30
+ d: BigInt('0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1'),
35
31
  Gx: BigInt('0x11dafe5d23e1218086a365b99fbf3d3be72f6afd7d1f72623e6b071492d1122b'),
36
32
  Gy: BigInt('0x1d523cf1ddab1a1793132e78c866c0c33e26ba5cc220fed7cc3f870e59d292aa'),
33
+ };
34
+ /** 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,
37
38
  hash: sha512,
38
- randomBytes,
39
- } as const);
39
+ });
40
40
 
41
+ const babyjubjub_CURVE: EdwardsOpts = {
42
+ p: bn254_Fr.ORDER,
43
+ n: BigInt('0x30644e72e131a029b85045b68181585d59f76dc1c90770533b94bee1c9093788'),
44
+ h: BigInt(8),
45
+ a: BigInt('168700'),
46
+ d: BigInt('168696'),
47
+ Gx: BigInt('0x23343e3445b673d38bcba38f25645adb494b1255b1162bb40f41a59f4d4b45e'),
48
+ Gy: BigInt('0xc19139cb84c680a6e14116da06056174a0cfa121e6e5c2450f87d64fc000001'),
49
+ };
41
50
  /** Curve over scalar field of bn254. babyjubjub Fp = bn254 n */
42
51
  export const babyjubjub: CurveFn = /* @__PURE__ */ twistedEdwards({
43
- a: BigInt(168700),
44
- d: BigInt(168696),
52
+ ...babyjubjub_CURVE,
45
53
  Fp: bn254_Fr,
46
- n: BigInt('21888242871839275222246405745257275088614511777268538073601725287587578984328'),
47
- h: BigInt(8),
48
- Gx: BigInt('995203441582195749578291179787384436505546430278305826713579947235728471134'),
49
- Gy: BigInt('5472060717959818805561601436314318772137091100104008585924551046643952123905'),
50
54
  hash: blake256,
51
- randomBytes,
52
- } as const);
55
+ });
53
56
 
54
57
  const jubjub_gh_first_block = utf8ToBytes(
55
58
  '096b36a5804bfacef1691e173c366a47ff5ba84a44f26ddd7e8d9f79d5b42df0'
56
59
  );
57
60
 
58
61
  // Returns point at JubJub curve which is prime order and not zero
59
- export function jubjub_groupHash(tag: Uint8Array, personalization: Uint8Array): ExtPointType {
62
+ export function jubjub_groupHash(tag: Uint8Array, personalization: Uint8Array): EdwardsPoint {
60
63
  const h = blake2s.create({ personalization, dkLen: 32 });
61
64
  h.update(jubjub_gh_first_block);
62
65
  h.update(tag);
63
66
  // NOTE: returns ExtendedPoint, in case it will be multiplied later
64
- let p = jubjub.ExtendedPoint.fromHex(h.digest());
67
+ let p = jubjub.Point.fromHex(h.digest());
65
68
  // NOTE: cannot replace with isSmallOrder, returns Point*8
66
69
  p = p.multiply(jubjub.CURVE.h);
67
- if (p.equals(jubjub.ExtendedPoint.ZERO)) throw new Error('Point has small order');
70
+ if (p.equals(jubjub.Point.ZERO)) throw new Error('Point has small order');
68
71
  return p;
69
72
  }
70
73
 
71
74
  // No secret data is leaked here at all.
72
75
  // It operates over public data:
73
76
  // const G_SPEND = jubjub.findGroupHash(Uint8Array.of(), utf8ToBytes('Item_G_'));
74
- export function jubjub_findGroupHash(m: Uint8Array, personalization: Uint8Array): ExtPointType {
75
- const tag = concatBytes(m, new Uint8Array([0]));
77
+ export function jubjub_findGroupHash(m: Uint8Array, personalization: Uint8Array): EdwardsPoint {
78
+ const tag = concatBytes(m, Uint8Array.of(0));
76
79
  const hashes = [];
77
80
  for (let i = 0; i < 256; i++) {
78
81
  tag[tag.length - 1] = i;
@@ -94,7 +97,6 @@ export const pasta_q: bigint = BigInt(
94
97
  );
95
98
 
96
99
  /**
97
- * https://neuromancer.sk/std/other/Pallas
98
100
  * @deprecated
99
101
  */
100
102
  export const pallas: WCurveFn = weierstrass({
@@ -105,10 +107,9 @@ export const pallas: WCurveFn = weierstrass({
105
107
  Gx: mod(BigInt(-1), pasta_p),
106
108
  Gy: BigInt(2),
107
109
  h: BigInt(1),
108
- ...getHash(sha256),
110
+ hash: sha256,
109
111
  });
110
112
  /**
111
- * https://neuromancer.sk/std/other/Vesta
112
113
  * @deprecated
113
114
  */
114
115
  export const vesta: WCurveFn = weierstrass({
@@ -119,5 +120,5 @@ export const vesta: WCurveFn = weierstrass({
119
120
  Gx: mod(BigInt(-1), pasta_q),
120
121
  Gy: BigInt(2),
121
122
  h: BigInt(1),
122
- ...getHash(sha256),
123
+ hash: sha256,
123
124
  });
package/src/nist.ts CHANGED
@@ -4,152 +4,187 @@
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';
9
+ import { createHasher, type H2CHasher } from './abstract/hash-to-curve.ts';
10
10
  import { Field } from './abstract/modular.ts';
11
- import { mapToCurveSimpleSWU } from './abstract/weierstrass.ts';
11
+ import {
12
+ mapToCurveSimpleSWU,
13
+ type WeierstrassOpts,
14
+ type WeierstrassPointCons,
15
+ } from './abstract/weierstrass.ts';
12
16
 
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,
17
+ // p = 2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n - 1n
18
+ // a = Fp256.create(BigInt('-3'));
19
+ const p256_CURVE: WeierstrassOpts<bigint> = {
20
+ p: BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'),
26
21
  n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
22
+ h: BigInt(1),
23
+ a: BigInt('0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc'),
24
+ b: BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b'),
27
25
  Gx: BigInt('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'),
28
26
  Gy: BigInt('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'),
29
- h: BigInt(1),
30
- lowS: false
31
- } as const, sha256);
32
- /** Alias to p256. */
33
- export const secp256r1: CurveFnWithCreate = p256;
34
-
35
- const p256_mapSWU = /* @__PURE__ */ (() =>
36
- mapToCurveSimpleSWU(Fp256, {
37
- A: p256_a,
38
- B: p256_b,
39
- Z: Fp256.create(BigInt('-10')),
40
- }))();
27
+ };
41
28
 
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(
29
+ // p = 2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n
30
+ const p384_CURVE: WeierstrassOpts<bigint> = {
31
+ p: BigInt(
57
32
  '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'),
33
+ ),
34
+ n: BigInt(
35
+ '0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973'
36
+ ),
76
37
  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
- }))();
38
+ a: BigInt(
39
+ '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc'
40
+ ),
41
+ b: BigInt(
42
+ '0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef'
43
+ ),
44
+ Gx: BigInt(
45
+ '0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7'
46
+ ),
47
+ Gy: BigInt(
48
+ '0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f'
49
+ ),
50
+ };
100
51
 
101
- // Field over which we'll do calculations.
102
- const Fp521 = Field(
103
- BigInt(
52
+ // p = 2n**521n - 1n
53
+ const p521_CURVE: WeierstrassOpts<bigint> = {
54
+ p: BigInt(
104
55
  '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,
56
+ ),
122
57
  n: BigInt(
123
58
  '0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409'
124
59
  ),
60
+ h: BigInt(1),
61
+ a: BigInt(
62
+ '0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc'
63
+ ),
64
+ b: BigInt(
65
+ '0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00'
66
+ ),
125
67
  Gx: BigInt(
126
68
  '0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66'
127
69
  ),
128
70
  Gy: BigInt(
129
71
  '0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'
130
72
  ),
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;
73
+ };
137
74
 
138
- const p521_mapSWU = /* @__PURE__ */ (() =>
139
- mapToCurveSimpleSWU(Fp521, {
140
- A: p521_a,
141
- B: p521_b,
142
- Z: Fp521.create(BigInt('-4')),
143
- }))();
75
+ const Fp256 = Field(p256_CURVE.p);
76
+ const Fp384 = Field(p384_CURVE.p);
77
+ const Fp521 = Field(p521_CURVE.p);
78
+ type SwuOpts = {
79
+ A: bigint;
80
+ B: bigint;
81
+ Z: bigint;
82
+ };
83
+ function createSWU(Point: WeierstrassPointCons<bigint>, opts: SwuOpts) {
84
+ const map = mapToCurveSimpleSWU(Point.Fp, opts);
85
+ return (scalars: bigint[]) => map(scalars[0]);
86
+ }
87
+
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
+ );
93
+ /** Hashing / encoding to p256 points / field. RFC 9380 methods. */
94
+ export const p256_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
95
+ return createHasher(
96
+ p256.Point,
97
+ createSWU(p256.Point, {
98
+ A: p256_CURVE.a,
99
+ B: p256_CURVE.b,
100
+ Z: p256.Point.Fp.create(BigInt('-10')),
101
+ }),
102
+ {
103
+ DST: 'P256_XMD:SHA-256_SSWU_RO_',
104
+ encodeDST: 'P256_XMD:SHA-256_SSWU_NU_',
105
+ p: p256_CURVE.p,
106
+ m: 1,
107
+ k: 128,
108
+ expand: 'xmd',
109
+ hash: sha256,
110
+ }
111
+ );
112
+ })();
113
+
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
+ );
127
+ /** Hashing / encoding to p384 points / field. RFC 9380 methods. */
128
+ export const p384_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
129
+ return createHasher(
130
+ p384.Point,
131
+ createSWU(p384.Point, {
132
+ A: p384_CURVE.a,
133
+ B: p384_CURVE.b,
134
+ Z: p384.Point.Fp.create(BigInt('-12')),
135
+ }),
136
+ {
137
+ DST: 'P384_XMD:SHA-384_SSWU_RO_',
138
+ encodeDST: 'P384_XMD:SHA-384_SSWU_NU_',
139
+ p: p384_CURVE.p,
140
+ m: 1,
141
+ k: 192,
142
+ expand: 'xmd',
143
+ hash: sha384,
144
+ }
145
+ );
146
+ })();
147
+
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
+ );
144
162
 
145
163
  /** 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
- }))();
164
+ export const p521_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
165
+ return createHasher(
166
+ p521.Point,
167
+ createSWU(p521.Point, {
168
+ A: p521_CURVE.a,
169
+ B: p521_CURVE.b,
170
+ Z: p521.Point.Fp.create(BigInt('-4')),
171
+ }),
172
+ {
173
+ DST: 'P521_XMD:SHA-512_SSWU_RO_',
174
+ encodeDST: 'P521_XMD:SHA-512_SSWU_NU_',
175
+ p: p521_CURVE.p,
176
+ m: 1,
177
+ k: 256,
178
+ expand: 'xmd',
179
+ hash: sha512,
180
+ }
181
+ );
182
+ })();
183
+
184
+ // export const p521_oprf: OPRF = createORPF({
185
+ // name: 'P521-SHA512',
186
+ // Point: p521.Point,
187
+ // hash: sha512,
188
+ // hashToGroup: p521_hasher.hashToCurve,
189
+ // hashToScalar: p521_hasher.hashToScalar, // produces L=98 just like in RFC
190
+ // });
package/src/p256.ts CHANGED
@@ -3,9 +3,13 @@
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
+ /** @deprecated use `import { p256 } from '@noble/curves/nist.js';` */
8
9
  export const p256: typeof p256n = p256n;
10
+ /** @deprecated use `import { p256 } from '@noble/curves/nist.js';` */
9
11
  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)();
12
+ /** @deprecated use `import { p256_hasher } from '@noble/curves/nist.js';` */
13
+ export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p256_hasher.hashToCurve)();
14
+ /** @deprecated use `import { p256_hasher } from '@noble/curves/nist.js';` */
15
+ export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p256_hasher.encodeToCurve)();
package/src/p384.ts CHANGED
@@ -3,11 +3,13 @@
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
+ /** @deprecated use `import { p384 } from '@noble/curves/nist.js';` */
8
9
  export const p384: typeof p384n = p384n;
10
+ /** @deprecated use `import { p384 } from '@noble/curves/nist.js';` */
9
11
  export const secp384r1: typeof p384n = p384n;
10
- export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => p384_hasher.hashToCurve)();
11
- export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => p384_hasher.encodeToCurve)();
12
-
13
- /** @deprecated Use `import { p384_hasher } from "@noble/curves/nist"` module. */
12
+ /** @deprecated use `import { p384_hasher } from '@noble/curves/nist.js';` */
13
+ export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p384_hasher.hashToCurve)();
14
+ /** @deprecated use `import { p384_hasher } from '@noble/curves/nist.js';` */
15
+ export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p384_hasher.encodeToCurve)();
package/src/p521.ts CHANGED
@@ -3,9 +3,13 @@
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
+ /** @deprecated use `import { p521 } from '@noble/curves/nist.js';` */
8
9
  export const p521: typeof p521n = p521n;
10
+ /** @deprecated use `import { p521 } from '@noble/curves/nist.js';` */
9
11
  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)();
12
+ /** @deprecated use `import { p521_hasher } from '@noble/curves/nist.js';` */
13
+ export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p521_hasher.hashToCurve)();
14
+ /** @deprecated use `import { p521_hasher } from '@noble/curves/nist.js';` */
15
+ export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p521_hasher.encodeToCurve)();