@noble/curves 1.9.2 → 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 (179) hide show
  1. package/README.md +186 -206
  2. package/_shortw_utils.d.ts +1 -0
  3. package/_shortw_utils.d.ts.map +1 -1
  4. package/_shortw_utils.js +1 -0
  5. package/_shortw_utils.js.map +1 -1
  6. package/abstract/bls.d.ts +87 -62
  7. package/abstract/bls.d.ts.map +1 -1
  8. package/abstract/bls.js +170 -163
  9. package/abstract/bls.js.map +1 -1
  10. package/abstract/curve.d.ts +109 -23
  11. package/abstract/curve.d.ts.map +1 -1
  12. package/abstract/curve.js +158 -156
  13. package/abstract/curve.js.map +1 -1
  14. package/abstract/edwards.d.ts +124 -70
  15. package/abstract/edwards.d.ts.map +1 -1
  16. package/abstract/edwards.js +212 -62
  17. package/abstract/edwards.js.map +1 -1
  18. package/abstract/hash-to-curve.d.ts +8 -4
  19. package/abstract/hash-to-curve.d.ts.map +1 -1
  20. package/abstract/hash-to-curve.js +23 -11
  21. package/abstract/hash-to-curve.js.map +1 -1
  22. package/abstract/modular.d.ts +8 -3
  23. package/abstract/modular.d.ts.map +1 -1
  24. package/abstract/modular.js +79 -35
  25. package/abstract/modular.js.map +1 -1
  26. package/abstract/montgomery.d.ts +17 -4
  27. package/abstract/montgomery.d.ts.map +1 -1
  28. package/abstract/montgomery.js +19 -3
  29. package/abstract/montgomery.js.map +1 -1
  30. package/abstract/tower.d.ts +3 -3
  31. package/abstract/tower.d.ts.map +1 -1
  32. package/abstract/tower.js.map +1 -1
  33. package/abstract/weierstrass.d.ts +142 -116
  34. package/abstract/weierstrass.d.ts.map +1 -1
  35. package/abstract/weierstrass.js +414 -335
  36. package/abstract/weierstrass.js.map +1 -1
  37. package/bls12-381.d.ts.map +1 -1
  38. package/bls12-381.js +4 -4
  39. package/bls12-381.js.map +1 -1
  40. package/ed25519.d.ts +52 -66
  41. package/ed25519.d.ts.map +1 -1
  42. package/ed25519.js +128 -155
  43. package/ed25519.js.map +1 -1
  44. package/ed448.d.ts +57 -58
  45. package/ed448.d.ts.map +1 -1
  46. package/ed448.js +114 -131
  47. package/ed448.js.map +1 -1
  48. package/esm/_shortw_utils.d.ts +1 -0
  49. package/esm/_shortw_utils.d.ts.map +1 -1
  50. package/esm/_shortw_utils.js +1 -0
  51. package/esm/_shortw_utils.js.map +1 -1
  52. package/esm/abstract/bls.d.ts +87 -62
  53. package/esm/abstract/bls.d.ts.map +1 -1
  54. package/esm/abstract/bls.js +171 -164
  55. package/esm/abstract/bls.js.map +1 -1
  56. package/esm/abstract/curve.d.ts +109 -23
  57. package/esm/abstract/curve.d.ts.map +1 -1
  58. package/esm/abstract/curve.js +156 -155
  59. package/esm/abstract/curve.js.map +1 -1
  60. package/esm/abstract/edwards.d.ts +124 -70
  61. package/esm/abstract/edwards.d.ts.map +1 -1
  62. package/esm/abstract/edwards.js +210 -62
  63. package/esm/abstract/edwards.js.map +1 -1
  64. package/esm/abstract/hash-to-curve.d.ts +8 -4
  65. package/esm/abstract/hash-to-curve.d.ts.map +1 -1
  66. package/esm/abstract/hash-to-curve.js +22 -11
  67. package/esm/abstract/hash-to-curve.js.map +1 -1
  68. package/esm/abstract/modular.d.ts +8 -3
  69. package/esm/abstract/modular.d.ts.map +1 -1
  70. package/esm/abstract/modular.js +79 -35
  71. package/esm/abstract/modular.js.map +1 -1
  72. package/esm/abstract/montgomery.d.ts +17 -4
  73. package/esm/abstract/montgomery.d.ts.map +1 -1
  74. package/esm/abstract/montgomery.js +19 -3
  75. package/esm/abstract/montgomery.js.map +1 -1
  76. package/esm/abstract/tower.d.ts +3 -3
  77. package/esm/abstract/tower.d.ts.map +1 -1
  78. package/esm/abstract/tower.js.map +1 -1
  79. package/esm/abstract/weierstrass.d.ts +142 -116
  80. package/esm/abstract/weierstrass.d.ts.map +1 -1
  81. package/esm/abstract/weierstrass.js +411 -333
  82. package/esm/abstract/weierstrass.js.map +1 -1
  83. package/esm/bls12-381.d.ts.map +1 -1
  84. package/esm/bls12-381.js +4 -4
  85. package/esm/bls12-381.js.map +1 -1
  86. package/esm/ed25519.d.ts +52 -66
  87. package/esm/ed25519.d.ts.map +1 -1
  88. package/esm/ed25519.js +131 -157
  89. package/esm/ed25519.js.map +1 -1
  90. package/esm/ed448.d.ts +57 -58
  91. package/esm/ed448.d.ts.map +1 -1
  92. package/esm/ed448.js +116 -132
  93. package/esm/ed448.js.map +1 -1
  94. package/esm/index.js +7 -9
  95. package/esm/index.js.map +1 -1
  96. package/esm/jubjub.d.ts +3 -3
  97. package/esm/jubjub.d.ts.map +1 -1
  98. package/esm/jubjub.js +3 -3
  99. package/esm/jubjub.js.map +1 -1
  100. package/esm/misc.d.ts +3 -5
  101. package/esm/misc.d.ts.map +1 -1
  102. package/esm/misc.js +0 -3
  103. package/esm/misc.js.map +1 -1
  104. package/esm/nist.d.ts +0 -6
  105. package/esm/nist.d.ts.map +1 -1
  106. package/esm/nist.js +31 -15
  107. package/esm/nist.js.map +1 -1
  108. package/esm/p256.d.ts +4 -0
  109. package/esm/p256.d.ts.map +1 -1
  110. package/esm/p256.js +4 -0
  111. package/esm/p256.js.map +1 -1
  112. package/esm/p384.d.ts +4 -1
  113. package/esm/p384.d.ts.map +1 -1
  114. package/esm/p384.js +4 -1
  115. package/esm/p384.js.map +1 -1
  116. package/esm/p521.d.ts +4 -0
  117. package/esm/p521.d.ts.map +1 -1
  118. package/esm/p521.js +4 -0
  119. package/esm/p521.js.map +1 -1
  120. package/esm/secp256k1.d.ts +32 -15
  121. package/esm/secp256k1.d.ts.map +1 -1
  122. package/esm/secp256k1.js +72 -67
  123. package/esm/secp256k1.js.map +1 -1
  124. package/esm/utils.d.ts +1 -1
  125. package/esm/utils.js +1 -1
  126. package/index.js +7 -9
  127. package/index.js.map +1 -1
  128. package/jubjub.d.ts +3 -3
  129. package/jubjub.d.ts.map +1 -1
  130. package/jubjub.js +3 -3
  131. package/jubjub.js.map +1 -1
  132. package/misc.d.ts +3 -5
  133. package/misc.d.ts.map +1 -1
  134. package/misc.js +0 -3
  135. package/misc.js.map +1 -1
  136. package/nist.d.ts +0 -6
  137. package/nist.d.ts.map +1 -1
  138. package/nist.js +31 -15
  139. package/nist.js.map +1 -1
  140. package/p256.d.ts +4 -0
  141. package/p256.d.ts.map +1 -1
  142. package/p256.js +4 -0
  143. package/p256.js.map +1 -1
  144. package/p384.d.ts +4 -1
  145. package/p384.d.ts.map +1 -1
  146. package/p384.js +4 -1
  147. package/p384.js.map +1 -1
  148. package/p521.d.ts +4 -0
  149. package/p521.d.ts.map +1 -1
  150. package/p521.js +4 -0
  151. package/p521.js.map +1 -1
  152. package/package.json +4 -2
  153. package/secp256k1.d.ts +32 -15
  154. package/secp256k1.d.ts.map +1 -1
  155. package/secp256k1.js +70 -65
  156. package/secp256k1.js.map +1 -1
  157. package/src/_shortw_utils.ts +1 -0
  158. package/src/abstract/bls.ts +319 -257
  159. package/src/abstract/curve.ts +226 -170
  160. package/src/abstract/edwards.ts +350 -139
  161. package/src/abstract/hash-to-curve.ts +33 -16
  162. package/src/abstract/modular.ts +86 -35
  163. package/src/abstract/montgomery.ts +36 -9
  164. package/src/abstract/tower.ts +4 -4
  165. package/src/abstract/weierstrass.ts +567 -474
  166. package/src/bls12-381.ts +28 -20
  167. package/src/ed25519.ts +161 -179
  168. package/src/ed448.ts +150 -156
  169. package/src/index.ts +7 -9
  170. package/src/jubjub.ts +3 -3
  171. package/src/misc.ts +3 -7
  172. package/src/nist.ts +40 -16
  173. package/src/p256.ts +4 -0
  174. package/src/p384.ts +4 -2
  175. package/src/p521.ts +4 -0
  176. package/src/secp256k1.ts +91 -73
  177. package/src/utils.ts +1 -1
  178. package/utils.d.ts +1 -1
  179. package/utils.js +1 -1
package/src/nist.ts CHANGED
@@ -7,8 +7,12 @@
7
7
  import { sha256, sha384, sha512 } from '@noble/hashes/sha2.js';
8
8
  import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
9
9
  import { createHasher, type H2CHasher } from './abstract/hash-to-curve.ts';
10
- import { Field, type IField } from './abstract/modular.ts';
11
- import { mapToCurveSimpleSWU, type WeierstrassOpts } from './abstract/weierstrass.ts';
10
+ import { Field } from './abstract/modular.ts';
11
+ import {
12
+ mapToCurveSimpleSWU,
13
+ type WeierstrassOpts,
14
+ type WeierstrassPointCons,
15
+ } from './abstract/weierstrass.ts';
12
16
 
13
17
  // p = 2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n - 1n
14
18
  // a = Fp256.create(BigInt('-3'));
@@ -76,8 +80,8 @@ type SwuOpts = {
76
80
  B: bigint;
77
81
  Z: bigint;
78
82
  };
79
- function createSWU(field: IField<bigint>, opts: SwuOpts) {
80
- const map = mapToCurveSimpleSWU(field, opts);
83
+ function createSWU(Point: WeierstrassPointCons<bigint>, opts: SwuOpts) {
84
+ const map = mapToCurveSimpleSWU(Point.Fp, opts);
81
85
  return (scalars: bigint[]) => map(scalars[0]);
82
86
  }
83
87
 
@@ -86,16 +90,14 @@ export const p256: CurveFnWithCreate = createCurve(
86
90
  { ...p256_CURVE, Fp: Fp256, lowS: false },
87
91
  sha256
88
92
  );
89
- /** Alias to p256. */
90
- export const secp256r1: CurveFnWithCreate = p256;
91
93
  /** Hashing / encoding to p256 points / field. RFC 9380 methods. */
92
94
  export const p256_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
93
95
  return createHasher(
94
96
  p256.Point,
95
- createSWU(Fp256, {
97
+ createSWU(p256.Point, {
96
98
  A: p256_CURVE.a,
97
99
  B: p256_CURVE.b,
98
- Z: Fp256.create(BigInt('-10')),
100
+ Z: p256.Point.Fp.create(BigInt('-10')),
99
101
  }),
100
102
  {
101
103
  DST: 'P256_XMD:SHA-256_SSWU_RO_',
@@ -109,21 +111,27 @@ export const p256_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
109
111
  );
110
112
  })();
111
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
+
112
122
  /** NIST P384 (aka secp384r1) curve, ECDSA and ECDH methods. */
113
123
  export const p384: CurveFnWithCreate = createCurve(
114
124
  { ...p384_CURVE, Fp: Fp384, lowS: false },
115
125
  sha384
116
126
  );
117
- /** Alias to p384. */
118
- export const secp384r1: CurveFnWithCreate = p384;
119
127
  /** Hashing / encoding to p384 points / field. RFC 9380 methods. */
120
128
  export const p384_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
121
129
  return createHasher(
122
130
  p384.Point,
123
- createSWU(Fp384, {
131
+ createSWU(p384.Point, {
124
132
  A: p384_CURVE.a,
125
133
  B: p384_CURVE.b,
126
- Z: Fp384.create(BigInt('-12')),
134
+ Z: p384.Point.Fp.create(BigInt('-12')),
127
135
  }),
128
136
  {
129
137
  DST: 'P384_XMD:SHA-384_SSWU_RO_',
@@ -137,21 +145,29 @@ export const p384_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
137
145
  );
138
146
  })();
139
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] });
140
157
  /** NIST P521 (aka secp521r1) curve, ECDSA and ECDH methods. */
141
158
  export const p521: CurveFnWithCreate = createCurve(
142
159
  { ...p521_CURVE, Fp: Fp521, lowS: false, allowedPrivateKeyLengths: [130, 131, 132] },
143
160
  sha512
144
161
  );
145
- /** Alias to p521. */
146
- export const secp521r1: CurveFnWithCreate = p521;
162
+
147
163
  /** Hashing / encoding to p521 points / field. RFC 9380 methods. */
148
164
  export const p521_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
149
165
  return createHasher(
150
166
  p521.Point,
151
- createSWU(Fp521, {
167
+ createSWU(p521.Point, {
152
168
  A: p521_CURVE.a,
153
169
  B: p521_CURVE.b,
154
- Z: Fp521.create(BigInt('-4')),
170
+ Z: p521.Point.Fp.create(BigInt('-4')),
155
171
  }),
156
172
  {
157
173
  DST: 'P521_XMD:SHA-512_SSWU_RO_',
@@ -164,3 +180,11 @@ export const p521_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() => {
164
180
  }
165
181
  );
166
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
@@ -5,7 +5,11 @@
5
5
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
6
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;
12
+ /** @deprecated use `import { p256_hasher } from '@noble/curves/nist.js';` */
10
13
  export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p256_hasher.hashToCurve)();
14
+ /** @deprecated use `import { p256_hasher } from '@noble/curves/nist.js';` */
11
15
  export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p256_hasher.encodeToCurve)();
package/src/p384.ts CHANGED
@@ -5,9 +5,11 @@
5
5
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
6
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;
12
+ /** @deprecated use `import { p384_hasher } from '@noble/curves/nist.js';` */
10
13
  export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p384_hasher.hashToCurve)();
14
+ /** @deprecated use `import { p384_hasher } from '@noble/curves/nist.js';` */
11
15
  export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p384_hasher.encodeToCurve)();
12
-
13
- /** @deprecated Use `import { p384_hasher } from "@noble/curves/nist"` module. */
package/src/p521.ts CHANGED
@@ -5,7 +5,11 @@
5
5
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
6
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;
12
+ /** @deprecated use `import { p521_hasher } from '@noble/curves/nist.js';` */
10
13
  export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p521_hasher.hashToCurve)();
14
+ /** @deprecated use `import { p521_hasher } from '@noble/curves/nist.js';` */
11
15
  export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => p521_hasher.encodeToCurve)();
package/src/secp256k1.ts CHANGED
@@ -9,18 +9,21 @@
9
9
  import { sha256 } from '@noble/hashes/sha2.js';
10
10
  import { randomBytes } from '@noble/hashes/utils.js';
11
11
  import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
12
+ import type { CurveInfo } from './abstract/curve.ts';
12
13
  import {
13
14
  createHasher,
14
15
  type H2CHasher,
15
16
  type H2CMethod,
16
17
  isogenyMap,
17
18
  } from './abstract/hash-to-curve.ts';
18
- import { Field, mod, pow2 } from './abstract/modular.ts';
19
+ import { Field, mapHashToField, mod, pow2 } from './abstract/modular.ts';
19
20
  import {
21
+ _normFnElement,
20
22
  type EndomorphismOpts,
21
23
  mapToCurveSimpleSWU,
22
- type ProjPointType as PointType,
24
+ type WeierstrassPoint as PointType,
23
25
  type WeierstrassOpts,
26
+ type WeierstrassPointCons,
24
27
  } from './abstract/weierstrass.ts';
25
28
  import type { Hex, PrivKey } from './utils.ts';
26
29
  import {
@@ -44,10 +47,18 @@ const secp256k1_CURVE: WeierstrassOpts<bigint> = {
44
47
  Gx: BigInt('0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'),
45
48
  Gy: BigInt('0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'),
46
49
  };
47
- const _0n = BigInt(0);
48
- const _1n = BigInt(1);
49
- const _2n = BigInt(2);
50
- const divNearest = (a: bigint, b: bigint) => (a + b / _2n) / b;
50
+
51
+ const secp256k1_ENDO: EndomorphismOpts = {
52
+ beta: BigInt('0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee'),
53
+ basises: [
54
+ [BigInt('0x3086d221a7d46bcde86c90e49284eb15'), -BigInt('0xe4437ed6010e88286f547fa90abfe4c3')],
55
+ [BigInt('0x114ca50f7a8e2f3f657c1108d9d44cfd8'), BigInt('0x3086d221a7d46bcde86c90e49284eb15')],
56
+ ],
57
+ };
58
+
59
+ const _0n = /* @__PURE__ */ BigInt(0);
60
+ const _1n = /* @__PURE__ */ BigInt(1);
61
+ const _2n = /* @__PURE__ */ BigInt(2);
51
62
 
52
63
  /**
53
64
  * √n = n^((p+1)/4) for fields p = 3 mod 4. We unwrap the loop and multiply bit-by-bit.
@@ -87,44 +98,14 @@ const Fpk1 = Field(secp256k1_CURVE.p, undefined, undefined, { sqrt: sqrtMod });
87
98
  * @example
88
99
  * ```js
89
100
  * import { secp256k1 } from '@noble/curves/secp256k1';
90
- * const priv = secp256k1.utils.randomPrivateKey();
91
- * const pub = secp256k1.getPublicKey(priv);
92
- * const msg = new Uint8Array(32).fill(1); // message hash (not message) in ecdsa
93
- * const sig = secp256k1.sign(msg, priv); // `{prehash: true}` option is available
94
- * const isValid = secp256k1.verify(sig, msg, pub) === true;
101
+ * const { secretKey, publicKey } = secp256k1.keygen();
102
+ * const msg = new TextEncoder().encode('hello');
103
+ * const sig = secp256k1.sign(msg, secretKey);
104
+ * const isValid = secp256k1.verify(sig, msg, publicKey) === true;
95
105
  * ```
96
106
  */
97
107
  export const secp256k1: CurveFnWithCreate = createCurve(
98
- {
99
- ...secp256k1_CURVE,
100
- Fp: Fpk1,
101
- lowS: true, // Allow only low-S signatures by default in sign() and verify()
102
- endo: {
103
- // Endomorphism, see above
104
- beta: BigInt('0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee'),
105
- splitScalar: (k: bigint) => {
106
- const n = secp256k1_CURVE.n;
107
- const a1 = BigInt('0x3086d221a7d46bcde86c90e49284eb15');
108
- const b1 = -_1n * BigInt('0xe4437ed6010e88286f547fa90abfe4c3');
109
- const a2 = BigInt('0x114ca50f7a8e2f3f657c1108d9d44cfd8');
110
- const b2 = a1;
111
- const POW_2_128 = BigInt('0x100000000000000000000000000000000'); // (2n**128n).toString(16)
112
-
113
- const c1 = divNearest(b2 * k, n);
114
- const c2 = divNearest(-b1 * k, n);
115
- let k1 = mod(k - c1 * a1 - c2 * a2, n);
116
- let k2 = mod(-c1 * b1 - c2 * b2, n);
117
- const k1neg = k1 > POW_2_128;
118
- const k2neg = k2 > POW_2_128;
119
- if (k1neg) k1 = n - k1;
120
- if (k2neg) k2 = n - k2;
121
- if (k1 > POW_2_128 || k2 > POW_2_128) {
122
- throw new Error('splitScalar: Endomorphism failed, k=' + k);
123
- }
124
- return { k1neg, k1, k2neg, k2 };
125
- },
126
- } satisfies EndomorphismOpts,
127
- },
108
+ { ...secp256k1_CURVE, Fp: Fpk1, lowS: true, endo: secp256k1_ENDO },
128
109
  sha256
129
110
  );
130
111
 
@@ -152,10 +133,11 @@ const hasEven = (y: bigint) => y % _2n === _0n;
152
133
 
153
134
  // Calculate point, scalar and bytes
154
135
  function schnorrGetExtPubKey(priv: PrivKey) {
155
- let d_ = secp256k1.utils.normPrivateKeyToScalar(priv); // same method executed in fromPrivateKey
156
- let p = Point.fromPrivateKey(d_); // P = d'⋅G; 0 < d' < n check is done inside
136
+ // TODO: replace with Point.Fn.fromBytes(priv)
137
+ let d_ = _normFnElement(Point.Fn, priv);
138
+ let p = Point.BASE.multiply(d_); // P = d'⋅G; 0 < d' < n check is done inside
157
139
  const scalar = hasEven(p.y) ? d_ : modN(-d_);
158
- return { scalar: scalar, bytes: pointToBytes(p) };
140
+ return { scalar, bytes: pointToBytes(p) };
159
141
  }
160
142
  /**
161
143
  * lift_x from BIP340. Convert 32-byte x coordinate to elliptic curve point.
@@ -182,21 +164,17 @@ function challenge(...args: Uint8Array[]): bigint {
182
164
  /**
183
165
  * Schnorr public key is just `x` coordinate of Point as per BIP340.
184
166
  */
185
- function schnorrGetPublicKey(privateKey: Hex): Uint8Array {
186
- return schnorrGetExtPubKey(privateKey).bytes; // d'=int(sk). Fail if d'=0 or d'≥n. Ret bytes(d'⋅G)
167
+ function schnorrGetPublicKey(secretKey: Hex): Uint8Array {
168
+ return schnorrGetExtPubKey(secretKey).bytes; // d'=int(sk). Fail if d'=0 or d'≥n. Ret bytes(d'⋅G)
187
169
  }
188
170
 
189
171
  /**
190
172
  * Creates Schnorr signature as per BIP340. Verifies itself before returning anything.
191
173
  * auxRand is optional and is not the sole source of k generation: bad CSPRNG won't be dangerous.
192
174
  */
193
- function schnorrSign(
194
- message: Hex,
195
- privateKey: PrivKey,
196
- auxRand: Hex = randomBytes(32)
197
- ): Uint8Array {
175
+ function schnorrSign(message: Hex, secretKey: PrivKey, auxRand: Hex = randomBytes(32)): Uint8Array {
198
176
  const m = ensureBytes('message', message);
199
- const { bytes: px, scalar: d } = schnorrGetExtPubKey(privateKey); // checks for isWithinCurveOrder
177
+ const { bytes: px, scalar: d } = schnorrGetExtPubKey(secretKey); // checks for isWithinCurveOrder
200
178
  const a = ensureBytes('auxRand', auxRand, 32); // Auxiliary random data a: a 32-byte array
201
179
  const t = numTo32b(d ^ num(taggedHash('BIP0340/aux', a))); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
202
180
  const rand = taggedHash('BIP0340/nonce', t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
@@ -239,18 +217,27 @@ function schnorrVerify(signature: Hex, message: Hex, publicKey: Hex): boolean {
239
217
  }
240
218
 
241
219
  export type SecpSchnorr = {
220
+ keygen: (seed?: Uint8Array) => { secretKey: Uint8Array; publicKey: Uint8Array };
242
221
  getPublicKey: typeof schnorrGetPublicKey;
243
222
  sign: typeof schnorrSign;
244
223
  verify: typeof schnorrVerify;
224
+ Point: WeierstrassPointCons<bigint>;
245
225
  utils: {
246
- randomPrivateKey: () => Uint8Array;
247
- lift_x: typeof lift_x;
226
+ randomSecretKey: (seed?: Uint8Array) => Uint8Array;
248
227
  pointToBytes: (point: PointType<bigint>) => Uint8Array;
228
+ lift_x: typeof lift_x;
229
+ taggedHash: typeof taggedHash;
230
+
231
+ /** @deprecated use `randomSecretKey` */
232
+ randomPrivateKey: (seed?: Uint8Array) => Uint8Array;
233
+ /** @deprecated use `utils` */
249
234
  numberToBytesBE: typeof numberToBytesBE;
235
+ /** @deprecated use `utils` */
250
236
  bytesToNumberBE: typeof bytesToNumberBE;
251
- taggedHash: typeof taggedHash;
237
+ /** @deprecated use `modular` */
252
238
  mod: typeof mod;
253
239
  };
240
+ info: { type: 'weierstrass'; publicKeyHasPrefix: false; lengths: CurveInfo['lengths'] };
254
241
  };
255
242
  /**
256
243
  * Schnorr signatures over secp256k1.
@@ -258,27 +245,55 @@ export type SecpSchnorr = {
258
245
  * @example
259
246
  * ```js
260
247
  * import { schnorr } from '@noble/curves/secp256k1';
261
- * const priv = schnorr.utils.randomPrivateKey();
262
- * const pub = schnorr.getPublicKey(priv);
248
+ * const { secretKey, publicKey } = schnorr.keygen();
249
+ * // const publicKey = schnorr.getPublicKey(secretKey);
263
250
  * const msg = new TextEncoder().encode('hello');
264
- * const sig = schnorr.sign(msg, priv);
265
- * const isValid = schnorr.verify(sig, msg, pub);
251
+ * const sig = schnorr.sign(msg, secretKey);
252
+ * const isValid = schnorr.verify(sig, msg, publicKey);
266
253
  * ```
267
254
  */
268
- export const schnorr: SecpSchnorr = /* @__PURE__ */ (() => ({
269
- getPublicKey: schnorrGetPublicKey,
270
- sign: schnorrSign,
271
- verify: schnorrVerify,
272
- utils: {
273
- randomPrivateKey: secp256k1.utils.randomPrivateKey,
274
- lift_x,
275
- pointToBytes,
276
- numberToBytesBE,
277
- bytesToNumberBE,
278
- taggedHash,
279
- mod,
280
- },
281
- }))();
255
+ export const schnorr: SecpSchnorr = /* @__PURE__ */ (() => {
256
+ const size = 32;
257
+ const seedLength = 48;
258
+ const randomSecretKey = (seed = randomBytes(seedLength)): Uint8Array => {
259
+ return mapHashToField(seed, secp256k1_CURVE.n);
260
+ };
261
+ // TODO: remove
262
+ secp256k1.utils.randomSecretKey;
263
+ function keygen(seed?: Uint8Array) {
264
+ const secretKey = randomSecretKey(seed);
265
+ return { secretKey, publicKey: schnorrGetPublicKey(secretKey) };
266
+ }
267
+ return {
268
+ keygen,
269
+ getPublicKey: schnorrGetPublicKey,
270
+ sign: schnorrSign,
271
+ verify: schnorrVerify,
272
+ Point,
273
+ utils: {
274
+ randomSecretKey: randomSecretKey,
275
+ randomPrivateKey: randomSecretKey,
276
+ taggedHash,
277
+
278
+ // TODO: remove
279
+ lift_x,
280
+ pointToBytes,
281
+ numberToBytesBE,
282
+ bytesToNumberBE,
283
+ mod,
284
+ },
285
+ info: {
286
+ type: 'weierstrass',
287
+ publicKeyHasPrefix: false,
288
+ lengths: {
289
+ secret: size,
290
+ public: size,
291
+ signature: size * 2,
292
+ seed: seedLength,
293
+ },
294
+ },
295
+ };
296
+ })();
282
297
 
283
298
  const isoMap = /* @__PURE__ */ (() =>
284
299
  isogenyMap(
@@ -319,6 +334,7 @@ const mapSWU = /* @__PURE__ */ (() =>
319
334
  B: BigInt('1771'),
320
335
  Z: Fpk1.create(BigInt('-11')),
321
336
  }))();
337
+
322
338
  /** Hashing / encoding to secp256k1 points / field. RFC 9380 methods. */
323
339
  export const secp256k1_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() =>
324
340
  createHasher(
@@ -338,8 +354,10 @@ export const secp256k1_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() =>
338
354
  }
339
355
  ))();
340
356
 
357
+ /** @deprecated use `import { secp256k1_hasher } from '@noble/curves/secp256k1.js';` */
341
358
  export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
342
359
  secp256k1_hasher.hashToCurve)();
343
360
 
361
+ /** @deprecated use `import { secp256k1_hasher } from '@noble/curves/secp256k1.js';` */
344
362
  export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
345
363
  secp256k1_hasher.encodeToCurve)();
package/src/utils.ts CHANGED
@@ -72,7 +72,7 @@ export function numberToVarBytesBE(n: number | bigint): Uint8Array {
72
72
  * Takes hex string or Uint8Array, converts to Uint8Array.
73
73
  * Validates output length.
74
74
  * Will throw error for other types.
75
- * @param title descriptive title for an error e.g. 'private key'
75
+ * @param title descriptive title for an error e.g. 'secret key'
76
76
  * @param hex hex string or Uint8Array
77
77
  * @param expectedLength optional, will compare to result array's length
78
78
  * @returns
package/utils.d.ts CHANGED
@@ -22,7 +22,7 @@ export declare function numberToVarBytesBE(n: number | bigint): Uint8Array;
22
22
  * Takes hex string or Uint8Array, converts to Uint8Array.
23
23
  * Validates output length.
24
24
  * Will throw error for other types.
25
- * @param title descriptive title for an error e.g. 'private key'
25
+ * @param title descriptive title for an error e.g. 'secret key'
26
26
  * @param hex hex string or Uint8Array
27
27
  * @param expectedLength optional, will compare to result array's length
28
28
  * @returns
package/utils.js CHANGED
@@ -75,7 +75,7 @@ function numberToVarBytesBE(n) {
75
75
  * Takes hex string or Uint8Array, converts to Uint8Array.
76
76
  * Validates output length.
77
77
  * Will throw error for other types.
78
- * @param title descriptive title for an error e.g. 'private key'
78
+ * @param title descriptive title for an error e.g. 'secret key'
79
79
  * @param hex hex string or Uint8Array
80
80
  * @param expectedLength optional, will compare to result array's length
81
81
  * @returns