@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/ed25519.ts CHANGED
@@ -7,26 +7,27 @@
7
7
  */
8
8
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
9
9
  import { sha512 } from '@noble/hashes/sha2.js';
10
- import { abytes, concatBytes, utf8ToBytes } from '@noble/hashes/utils.js';
11
- import { pippenger, type AffinePoint } from './abstract/curve.ts';
10
+ import { abytes, concatBytes, hexToBytes } from '@noble/hashes/utils.js';
11
+ import { type AffinePoint } from './abstract/curve.ts';
12
12
  import {
13
+ eddsa,
14
+ edwards,
13
15
  PrimeEdwardsPoint,
14
- twistedEdwards,
15
- type CurveFn,
16
+ type EdDSA,
17
+ type EdDSAOpts,
16
18
  type EdwardsOpts,
17
19
  type EdwardsPoint,
20
+ type EdwardsPointCons,
18
21
  } from './abstract/edwards.ts';
19
22
  import {
20
23
  _DST_scalar,
21
24
  createHasher,
22
25
  expand_message_xmd,
26
+ type H2CDSTOpts,
23
27
  type H2CHasher,
24
28
  type H2CHasherBase,
25
- type H2CMethod,
26
- type htfBasicOpts,
27
29
  } from './abstract/hash-to-curve.ts';
28
30
  import {
29
- Field,
30
31
  FpInvertBatch,
31
32
  FpSqrtEven,
32
33
  isNegativeLE,
@@ -34,19 +35,19 @@ import {
34
35
  pow2,
35
36
  type IField,
36
37
  } from './abstract/modular.ts';
37
- import { montgomery, type MontgomeryECDH as XCurveFn } from './abstract/montgomery.ts';
38
- import { bytesToNumberLE, ensureBytes, equalBytes, type Hex } from './utils.ts';
38
+ import { montgomery, type MontgomeryECDH } from './abstract/montgomery.ts';
39
+ import { createORPF, type OPRF } from './abstract/oprf.ts';
40
+ import { asciiToBytes, bytesToNumberLE, equalBytes } from './utils.ts';
39
41
 
40
42
  // prettier-ignore
41
- const _0n = /* @__PURE__ */ BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
43
+ const _0n = /* @__PURE__ */ BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = /* @__PURE__ */ BigInt(3);
42
44
  // prettier-ignore
43
45
  const _5n = BigInt(5), _8n = BigInt(8);
44
46
 
45
- // P = 2n**255n-19n
47
+ // P = 2n**255n - 19n
46
48
  const ed25519_CURVE_p = BigInt(
47
49
  '0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed'
48
50
  );
49
-
50
51
  // N = 2n**252n + 27742317777372353535851937790883648493n
51
52
  // a = Fp.create(BigInt(-1))
52
53
  // d = -121665/121666 a.k.a. Fp.neg(121665 * Fp.inv(121666))
@@ -116,70 +117,56 @@ function uvRatio(u: bigint, v: bigint): { isValid: boolean; value: bigint } {
116
117
  return { isValid: useRoot1 || useRoot2, value: x };
117
118
  }
118
119
 
119
- const Fp = /* @__PURE__ */ (() => Field(ed25519_CURVE.p, { isLE: true }))();
120
- const Fn = /* @__PURE__ */ (() => Field(ed25519_CURVE.n, { isLE: true }))();
121
-
122
- const ed25519Defaults = /* @__PURE__ */ (() => ({
123
- ...ed25519_CURVE,
124
- Fp,
125
- hash: sha512,
126
- adjustScalarBytes,
127
- // dom2
128
- // Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
129
- // Constant-time, u/√v
130
- uvRatio,
131
- }))();
132
-
133
- /**
134
- * ed25519 curve with EdDSA signatures.
135
- * @example
136
- * import { ed25519 } from '@noble/curves/ed25519';
137
- * const { secretKey, publicKey } = ed25519.keygen();
138
- * const msg = new TextEncoder().encode('hello');
139
- * const sig = ed25519.sign(msg, priv);
140
- * ed25519.verify(sig, msg, pub); // Default mode: follows ZIP215
141
- * ed25519.verify(sig, msg, pub, { zip215: false }); // RFC8032 / FIPS 186-5
142
- */
143
- export const ed25519: CurveFn = /* @__PURE__ */ (() => twistedEdwards(ed25519Defaults))();
120
+ const ed25519_Point = /* @__PURE__ */ edwards(ed25519_CURVE, { uvRatio });
121
+ const Fp = /* @__PURE__ */ (() => ed25519_Point.Fp)();
122
+ const Fn = /* @__PURE__ */ (() => ed25519_Point.Fn)();
144
123
 
145
124
  function ed25519_domain(data: Uint8Array, ctx: Uint8Array, phflag: boolean) {
146
125
  if (ctx.length > 255) throw new Error('Context is too big');
147
126
  return concatBytes(
148
- utf8ToBytes('SigEd25519 no Ed25519 collisions'),
127
+ asciiToBytes('SigEd25519 no Ed25519 collisions'),
149
128
  new Uint8Array([phflag ? 1 : 0, ctx.length]),
150
129
  ctx,
151
130
  data
152
131
  );
153
132
  }
154
133
 
155
- /** Context of ed25519. Uses context for domain separation. */
156
- export const ed25519ctx: CurveFn = /* @__PURE__ */ (() =>
157
- twistedEdwards({
158
- ...ed25519Defaults,
159
- domain: ed25519_domain,
160
- }))();
134
+ function ed(opts: EdDSAOpts) {
135
+ return eddsa(ed25519_Point, sha512, Object.assign({ adjustScalarBytes }, opts));
136
+ }
161
137
 
162
- /** Prehashed version of ed25519. Accepts already-hashed messages in sign() and verify(). */
163
- export const ed25519ph: CurveFn = /* @__PURE__ */ (() =>
164
- twistedEdwards(
165
- Object.assign({}, ed25519Defaults, {
166
- domain: ed25519_domain,
167
- prehash: sha512,
168
- })
169
- ))();
138
+ /**
139
+ * ed25519 curve with EdDSA signatures.
140
+ * @example
141
+ * ```js
142
+ * import { ed25519 } from '@noble/curves/ed25519.js';
143
+ * const { secretKey, publicKey } = ed25519.keygen();
144
+ * // const publicKey = ed25519.getPublicKey(secretKey);
145
+ * const msg = new TextEncoder().encode('hello noble');
146
+ * const sig = ed25519.sign(msg, secretKey);
147
+ * const isValid = ed25519.verify(sig, msg, pub); // ZIP215
148
+ * // RFC8032 / FIPS 186-5
149
+ * const isValid2 = ed25519.verify(sig, msg, pub, { zip215: false });
150
+ * ```
151
+ */
152
+ export const ed25519: EdDSA = /* @__PURE__ */ ed({});
153
+ /** Context version of ed25519 (ctx for domain separation). See {@link ed25519} */
154
+ export const ed25519ctx: EdDSA = /* @__PURE__ */ ed({ domain: ed25519_domain });
155
+ /** Prehashed version of ed25519. See {@link ed25519} */
156
+ export const ed25519ph: EdDSA = /* @__PURE__ */ ed({ domain: ed25519_domain, prehash: sha512 });
170
157
 
171
158
  /**
172
159
  * ECDH using curve25519 aka x25519.
173
160
  * @example
174
- * import { x25519 } from '@noble/curves/ed25519';
175
- * const priv = 'a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4';
176
- * const pub = 'e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c';
177
- * x25519.getSharedSecret(priv, pub) === x25519.scalarMult(priv, pub); // aliases
178
- * x25519.getPublicKey(priv) === x25519.scalarMultBase(priv);
179
- * x25519.getPublicKey(x25519.utils.randomSecretKey());
161
+ * ```js
162
+ * import { x25519 } from '@noble/curves/ed25519.js';
163
+ * const alice = x25519.keygen();
164
+ * const bob = x25519.keygen();
165
+ * const shared = x25519.getSharedSecret(alice.secretKey, bob.publicKey);
166
+ * ```
180
167
  */
181
- export const x25519: XCurveFn = /* @__PURE__ */ (() => {
182
- const P = Fp.ORDER;
168
+ export const x25519: MontgomeryECDH = /* @__PURE__ */ (() => {
169
+ const P = ed25519_CURVE_p;
183
170
  return montgomery({
184
171
  P,
185
172
  type: 'x25519',
@@ -265,9 +252,9 @@ function map_to_curve_elligator2_edwards25519(u: bigint) {
265
252
  }
266
253
 
267
254
  /** Hashing to ed25519 points / field. RFC 9380 methods. */
268
- export const ed25519_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() =>
255
+ export const ed25519_hasher: H2CHasher<EdwardsPointCons> = /* @__PURE__ */ (() =>
269
256
  createHasher(
270
- ed25519.Point,
257
+ ed25519_Point,
271
258
  (scalars: bigint[]) => map_to_curve_elligator2_edwards25519(scalars[0]),
272
259
  {
273
260
  DST: 'edwards25519_XMD:SHA-512_ELL2_RO_',
@@ -304,17 +291,14 @@ const invertSqrt = (number: bigint) => uvRatio(_1n, number);
304
291
  const MAX_255B = /* @__PURE__ */ BigInt(
305
292
  '0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
306
293
  );
307
- const bytes255ToNumberLE = (bytes: Uint8Array) =>
308
- ed25519.Point.Fp.create(bytesToNumberLE(bytes) & MAX_255B);
309
-
310
- type ExtendedPoint = EdwardsPoint;
294
+ const bytes255ToNumberLE = (bytes: Uint8Array) => Fp.create(bytesToNumberLE(bytes) & MAX_255B);
311
295
 
312
296
  /**
313
297
  * Computes Elligator map for Ristretto255.
314
298
  * Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-B) and on
315
299
  * the [website](https://ristretto.group/formulas/elligator.html).
316
300
  */
317
- function calcElligatorRistrettoMap(r0: bigint): ExtendedPoint {
301
+ function calcElligatorRistrettoMap(r0: bigint): EdwardsPoint {
318
302
  const { d } = ed25519_CURVE;
319
303
  const P = ed25519_CURVE_p;
320
304
  const mod = (n: bigint) => Fp.create(n);
@@ -333,24 +317,15 @@ function calcElligatorRistrettoMap(r0: bigint): ExtendedPoint {
333
317
  const W1 = mod(Nt * SQRT_AD_MINUS_ONE); // 11
334
318
  const W2 = mod(_1n - s2); // 12
335
319
  const W3 = mod(_1n + s2); // 13
336
- return new ed25519.Point(mod(W0 * W3), mod(W2 * W1), mod(W1 * W3), mod(W0 * W2));
337
- }
338
-
339
- function ristretto255_map(bytes: Uint8Array): _RistrettoPoint {
340
- abytes(bytes, 64);
341
- const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));
342
- const R1 = calcElligatorRistrettoMap(r1);
343
- const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));
344
- const R2 = calcElligatorRistrettoMap(r2);
345
- return new _RistrettoPoint(R1.add(R2));
320
+ return new ed25519_Point(mod(W0 * W3), mod(W2 * W1), mod(W1 * W3), mod(W0 * W2));
346
321
  }
347
322
 
348
323
  /**
349
324
  * Wrapper over Edwards Point for ristretto255.
350
325
  *
351
- * Each ed25519/ExtendedPoint has 8 different equivalent points. This can be
326
+ * Each ed25519/EdwardsPoint has 8 different equivalent points. This can be
352
327
  * a source of bugs for protocols like ring signatures. Ristretto was created to solve this.
353
- * Ristretto point operates in X:Y:Z:T extended coordinates like ExtendedPoint,
328
+ * Ristretto point operates in X:Y:Z:T extended coordinates like EdwardsPoint,
354
329
  * but it should work in its own namespace: do not combine those two.
355
330
  * See [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
356
331
  */
@@ -359,10 +334,10 @@ class _RistrettoPoint extends PrimeEdwardsPoint<_RistrettoPoint> {
359
334
  // because typescript strips comments, which makes bundlers disable tree-shaking.
360
335
  // prettier-ignore
361
336
  static BASE: _RistrettoPoint =
362
- /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.BASE))();
337
+ /* @__PURE__ */ (() => new _RistrettoPoint(ed25519_Point.BASE))();
363
338
  // prettier-ignore
364
339
  static ZERO: _RistrettoPoint =
365
- /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.ZERO))();
340
+ /* @__PURE__ */ (() => new _RistrettoPoint(ed25519_Point.ZERO))();
366
341
  // prettier-ignore
367
342
  static Fp: IField<bigint> =
368
343
  /* @__PURE__ */ (() => Fp)();
@@ -370,12 +345,12 @@ class _RistrettoPoint extends PrimeEdwardsPoint<_RistrettoPoint> {
370
345
  static Fn: IField<bigint> =
371
346
  /* @__PURE__ */ (() => Fn)();
372
347
 
373
- constructor(ep: ExtendedPoint) {
348
+ constructor(ep: EdwardsPoint) {
374
349
  super(ep);
375
350
  }
376
351
 
377
352
  static fromAffine(ap: AffinePoint<bigint>): _RistrettoPoint {
378
- return new _RistrettoPoint(ed25519.Point.fromAffine(ap));
353
+ return new _RistrettoPoint(ed25519_Point.fromAffine(ap));
379
354
  }
380
355
 
381
356
  protected assertSame(other: _RistrettoPoint): void {
@@ -386,11 +361,6 @@ class _RistrettoPoint extends PrimeEdwardsPoint<_RistrettoPoint> {
386
361
  return new _RistrettoPoint(ep);
387
362
  }
388
363
 
389
- /** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */
390
- static hashToCurve(hex: Hex): _RistrettoPoint {
391
- return ristretto255_map(ensureBytes('ristrettoHash', hex, 64));
392
- }
393
-
394
364
  static fromBytes(bytes: Uint8Array): _RistrettoPoint {
395
365
  abytes(bytes, 32);
396
366
  const { a, d } = ed25519_CURVE;
@@ -416,7 +386,7 @@ class _RistrettoPoint extends PrimeEdwardsPoint<_RistrettoPoint> {
416
386
  const t = mod(x * y); // 12
417
387
  if (!isValid || isNegativeLE(t, P) || y === _0n)
418
388
  throw new Error('invalid ristretto255 encoding 2');
419
- return new _RistrettoPoint(new ed25519.Point(x, y, _1n, t));
389
+ return new _RistrettoPoint(new ed25519_Point(x, y, _1n, t));
420
390
  }
421
391
 
422
392
  /**
@@ -424,12 +394,8 @@ class _RistrettoPoint extends PrimeEdwardsPoint<_RistrettoPoint> {
424
394
  * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
425
395
  * @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
426
396
  */
427
- static fromHex(hex: Hex): _RistrettoPoint {
428
- return _RistrettoPoint.fromBytes(ensureBytes('ristrettoHex', hex, 32));
429
- }
430
-
431
- static msm(points: _RistrettoPoint[], scalars: bigint[]): _RistrettoPoint {
432
- return pippenger(_RistrettoPoint, ed25519.Point.Fn, points, scalars);
397
+ static fromHex(hex: string): _RistrettoPoint {
398
+ return _RistrettoPoint.fromBytes(hexToBytes(hex));
433
399
  }
434
400
 
435
401
  /**
@@ -489,25 +455,59 @@ export const ristretto255: {
489
455
  } = { Point: _RistrettoPoint };
490
456
 
491
457
  /** Hashing to ristretto255 points / field. RFC 9380 methods. */
492
- export const ristretto255_hasher: H2CHasherBase<bigint> = {
493
- hashToCurve(msg: Uint8Array, options?: htfBasicOpts): _RistrettoPoint {
458
+ export const ristretto255_hasher: H2CHasherBase<typeof _RistrettoPoint> = {
459
+ Point: _RistrettoPoint,
460
+ /**
461
+ * Spec: https://www.rfc-editor.org/rfc/rfc9380.html#name-hashing-to-ristretto255. Caveats:
462
+ * * There are no test vectors
463
+ * * encodeToCurve / mapToCurve is undefined
464
+ * * mapToCurve would be `calcElligatorRistrettoMap(scalars[0])`, not ristretto255_map!
465
+ * * hashToScalar is undefined too, so we just use OPRF implementation
466
+ * * We cannot re-use 'createHasher', because ristretto255_map is different algorithm/RFC
467
+ (os2ip -> bytes255ToNumberLE)
468
+ * * mapToCurve == calcElligatorRistrettoMap, hashToCurve == ristretto255_map
469
+ * * hashToScalar is undefined in RFC9380 for ristretto, we are using version from OPRF here, using bytes255ToNumblerLE will create different result if we use bytes255ToNumberLE as os2ip
470
+ * * current version is closest to spec.
471
+ */
472
+ hashToCurve(msg: Uint8Array, options?: H2CDSTOpts): _RistrettoPoint {
473
+ // == 'hash_to_ristretto255'
494
474
  const DST = options?.DST || 'ristretto255_XMD:SHA-512_R255MAP_RO_';
495
475
  const xmd = expand_message_xmd(msg, DST, 64, sha512);
496
- return ristretto255_map(xmd);
476
+ // NOTE: RFC 9380 incorrectly calls this function 'ristretto255_map', in RFC 9496 map was function inside (per point)
477
+ // That also lead to confustion that ristretto255_map is mapToCurve (it is not! it is old hashToCurve)
478
+ return ristretto255_hasher.deriveToCurve!(xmd);
497
479
  },
498
- hashToScalar(msg: Uint8Array, options: htfBasicOpts = { DST: _DST_scalar }) {
480
+ hashToScalar(msg: Uint8Array, options: H2CDSTOpts = { DST: _DST_scalar }) {
499
481
  const xmd = expand_message_xmd(msg, options.DST, 64, sha512);
500
482
  return Fn.create(bytesToNumberLE(xmd));
501
483
  },
484
+ /**
485
+ * HashToCurve-like construction based on RFC 9496 (Element Derivation).
486
+ * Converts 64 uniform random bytes into a curve point.
487
+ *
488
+ * WARNING: This represents an older hash-to-curve construction, preceding the finalization of RFC 9380.
489
+ * It was later reused as a component in the newer `hash_to_ristretto255` function defined in RFC 9380.
490
+ */
491
+ deriveToCurve(bytes: Uint8Array): _RistrettoPoint {
492
+ // https://www.rfc-editor.org/rfc/rfc9496.html#name-element-derivation
493
+ abytes(bytes, 64);
494
+ const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));
495
+ const R1 = calcElligatorRistrettoMap(r1);
496
+ const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));
497
+ const R2 = calcElligatorRistrettoMap(r2);
498
+ return new _RistrettoPoint(R1.add(R2));
499
+ },
502
500
  };
503
501
 
504
- // export const ristretto255_oprf: OPRF = createORPF({
505
- // name: 'ristretto255-SHA512',
506
- // Point: RistrettoPoint,
507
- // hash: sha512,
508
- // hashToGroup: ristretto255_hasher.hashToCurve,
509
- // hashToScalar: ristretto255_hasher.hashToScalar,
510
- // });
502
+ /** ristretto255 OPRF, defined in RFC 9497. */
503
+ export const ristretto255_oprf: OPRF = /* @__PURE__ */ (() =>
504
+ createORPF({
505
+ name: 'ristretto255-SHA512',
506
+ Point: _RistrettoPoint,
507
+ hash: sha512,
508
+ hashToGroup: ristretto255_hasher.hashToCurve,
509
+ hashToScalar: ristretto255_hasher.hashToScalar,
510
+ }))();
511
511
 
512
512
  /**
513
513
  * Weird / bogus points, useful for debugging.
@@ -525,30 +525,3 @@ export const ED25519_TORSION_SUBGROUP: string[] = [
525
525
  '0000000000000000000000000000000000000000000000000000000000000000',
526
526
  'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa',
527
527
  ];
528
-
529
- /** @deprecated use `ed25519.utils.toMontgomery` */
530
- export function edwardsToMontgomeryPub(edwardsPub: Hex): Uint8Array {
531
- return ed25519.utils.toMontgomery(ensureBytes('pub', edwardsPub));
532
- }
533
- /** @deprecated use `ed25519.utils.toMontgomery` */
534
- export const edwardsToMontgomery: typeof edwardsToMontgomeryPub = edwardsToMontgomeryPub;
535
-
536
- /** @deprecated use `ed25519.utils.toMontgomerySecret` */
537
- export function edwardsToMontgomeryPriv(edwardsPriv: Uint8Array): Uint8Array {
538
- return ed25519.utils.toMontgomerySecret(ensureBytes('pub', edwardsPriv));
539
- }
540
-
541
- /** @deprecated use `ristretto255.Point` */
542
- export const RistrettoPoint: typeof _RistrettoPoint = _RistrettoPoint;
543
- /** @deprecated use `import { ed25519_hasher } from '@noble/curves/ed25519.js';` */
544
- export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => ed25519_hasher.hashToCurve)();
545
- /** @deprecated use `import { ed25519_hasher } from '@noble/curves/ed25519.js';` */
546
- export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
547
- ed25519_hasher.encodeToCurve)();
548
- type RistHasher = (msg: Uint8Array, options: htfBasicOpts) => _RistrettoPoint;
549
- /** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */
550
- export const hashToRistretto255: RistHasher = /* @__PURE__ */ (() =>
551
- ristretto255_hasher.hashToCurve as RistHasher)();
552
- /** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */
553
- export const hash_to_ristretto255: RistHasher = /* @__PURE__ */ (() =>
554
- ristretto255_hasher.hashToCurve as RistHasher)();