@noble/curves 1.9.0 → 1.9.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 (207) hide show
  1. package/README.md +78 -34
  2. package/_shortw_utils.d.ts +7 -5
  3. package/_shortw_utils.d.ts.map +1 -1
  4. package/_shortw_utils.js +2 -8
  5. package/_shortw_utils.js.map +1 -1
  6. package/abstract/bls.d.ts +60 -24
  7. package/abstract/bls.d.ts.map +1 -1
  8. package/abstract/bls.js +158 -109
  9. package/abstract/bls.js.map +1 -1
  10. package/abstract/curve.d.ts +44 -9
  11. package/abstract/curve.d.ts.map +1 -1
  12. package/abstract/curve.js +99 -11
  13. package/abstract/curve.js.map +1 -1
  14. package/abstract/edwards.d.ts +112 -25
  15. package/abstract/edwards.d.ts.map +1 -1
  16. package/abstract/edwards.js +141 -92
  17. package/abstract/edwards.js.map +1 -1
  18. package/abstract/fft.d.ts +122 -0
  19. package/abstract/fft.d.ts.map +1 -0
  20. package/abstract/fft.js +438 -0
  21. package/abstract/fft.js.map +1 -0
  22. package/abstract/hash-to-curve.d.ts +25 -11
  23. package/abstract/hash-to-curve.d.ts.map +1 -1
  24. package/abstract/hash-to-curve.js +17 -14
  25. package/abstract/hash-to-curve.js.map +1 -1
  26. package/abstract/modular.d.ts +28 -17
  27. package/abstract/modular.d.ts.map +1 -1
  28. package/abstract/modular.js +156 -139
  29. package/abstract/modular.js.map +1 -1
  30. package/abstract/montgomery.d.ts +3 -8
  31. package/abstract/montgomery.d.ts.map +1 -1
  32. package/abstract/montgomery.js +73 -93
  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 +20 -46
  39. package/abstract/tower.d.ts.map +1 -1
  40. package/abstract/tower.js +10 -4
  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 +152 -73
  47. package/abstract/weierstrass.d.ts.map +1 -1
  48. package/abstract/weierstrass.js +487 -404
  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 -480
  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 +25 -9
  59. package/ed25519.d.ts.map +1 -1
  60. package/ed25519.js +89 -65
  61. package/ed25519.js.map +1 -1
  62. package/ed448.d.ts +29 -10
  63. package/ed448.d.ts.map +1 -1
  64. package/ed448.js +116 -81
  65. package/ed448.js.map +1 -1
  66. package/esm/_shortw_utils.d.ts +7 -5
  67. package/esm/_shortw_utils.d.ts.map +1 -1
  68. package/esm/_shortw_utils.js +2 -8
  69. package/esm/_shortw_utils.js.map +1 -1
  70. package/esm/abstract/bls.d.ts +60 -24
  71. package/esm/abstract/bls.d.ts.map +1 -1
  72. package/esm/abstract/bls.js +158 -109
  73. package/esm/abstract/bls.js.map +1 -1
  74. package/esm/abstract/curve.d.ts +44 -9
  75. package/esm/abstract/curve.d.ts.map +1 -1
  76. package/esm/abstract/curve.js +96 -12
  77. package/esm/abstract/curve.js.map +1 -1
  78. package/esm/abstract/edwards.d.ts +112 -25
  79. package/esm/abstract/edwards.d.ts.map +1 -1
  80. package/esm/abstract/edwards.js +141 -94
  81. package/esm/abstract/edwards.js.map +1 -1
  82. package/esm/abstract/fft.d.ts +122 -0
  83. package/esm/abstract/fft.d.ts.map +1 -0
  84. package/esm/abstract/fft.js +425 -0
  85. package/esm/abstract/fft.js.map +1 -0
  86. package/esm/abstract/hash-to-curve.d.ts +25 -11
  87. package/esm/abstract/hash-to-curve.d.ts.map +1 -1
  88. package/esm/abstract/hash-to-curve.js +17 -14
  89. package/esm/abstract/hash-to-curve.js.map +1 -1
  90. package/esm/abstract/modular.d.ts +28 -17
  91. package/esm/abstract/modular.d.ts.map +1 -1
  92. package/esm/abstract/modular.js +155 -138
  93. package/esm/abstract/modular.js.map +1 -1
  94. package/esm/abstract/montgomery.d.ts +3 -8
  95. package/esm/abstract/montgomery.d.ts.map +1 -1
  96. package/esm/abstract/montgomery.js +74 -94
  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 +20 -46
  103. package/esm/abstract/tower.d.ts.map +1 -1
  104. package/esm/abstract/tower.js +10 -4
  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 +152 -73
  111. package/esm/abstract/weierstrass.d.ts.map +1 -1
  112. package/esm/abstract/weierstrass.js +485 -406
  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 -479
  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 +25 -9
  123. package/esm/ed25519.d.ts.map +1 -1
  124. package/esm/ed25519.js +84 -60
  125. package/esm/ed25519.js.map +1 -1
  126. package/esm/ed448.d.ts +29 -10
  127. package/esm/ed448.d.ts.map +1 -1
  128. package/esm/ed448.js +113 -78
  129. package/esm/ed448.js.map +1 -1
  130. package/esm/jubjub.d.ts +4 -0
  131. package/esm/jubjub.d.ts.map +1 -1
  132. package/esm/jubjub.js +4 -0
  133. package/esm/jubjub.js.map +1 -1
  134. package/esm/misc.d.ts.map +1 -1
  135. package/esm/misc.js +31 -26
  136. package/esm/misc.js.map +1 -1
  137. package/esm/nist.d.ts +8 -16
  138. package/esm/nist.d.ts.map +1 -1
  139. package/esm/nist.js +87 -97
  140. package/esm/nist.js.map +1 -1
  141. package/esm/p256.d.ts +3 -3
  142. package/esm/p384.d.ts +3 -3
  143. package/esm/p521.d.ts +3 -3
  144. package/esm/pasta.d.ts +4 -0
  145. package/esm/pasta.d.ts.map +1 -1
  146. package/esm/pasta.js +4 -0
  147. package/esm/pasta.js.map +1 -1
  148. package/esm/secp256k1.d.ts +6 -6
  149. package/esm/secp256k1.d.ts.map +1 -1
  150. package/esm/secp256k1.js +44 -41
  151. package/esm/secp256k1.js.map +1 -1
  152. package/esm/utils.d.ts +96 -0
  153. package/esm/utils.d.ts.map +1 -0
  154. package/esm/utils.js +279 -0
  155. package/esm/utils.js.map +1 -0
  156. package/jubjub.d.ts +4 -0
  157. package/jubjub.d.ts.map +1 -1
  158. package/jubjub.js +4 -0
  159. package/jubjub.js.map +1 -1
  160. package/misc.d.ts.map +1 -1
  161. package/misc.js +35 -30
  162. package/misc.js.map +1 -1
  163. package/nist.d.ts +8 -16
  164. package/nist.d.ts.map +1 -1
  165. package/nist.js +87 -97
  166. package/nist.js.map +1 -1
  167. package/p256.d.ts +3 -3
  168. package/p384.d.ts +3 -3
  169. package/p521.d.ts +3 -3
  170. package/package.json +26 -8
  171. package/pasta.d.ts +4 -0
  172. package/pasta.d.ts.map +1 -1
  173. package/pasta.js +4 -0
  174. package/pasta.js.map +1 -1
  175. package/secp256k1.d.ts +6 -6
  176. package/secp256k1.d.ts.map +1 -1
  177. package/secp256k1.js +47 -44
  178. package/secp256k1.js.map +1 -1
  179. package/src/_shortw_utils.ts +5 -15
  180. package/src/abstract/bls.ts +260 -145
  181. package/src/abstract/curve.ts +125 -18
  182. package/src/abstract/edwards.ts +282 -127
  183. package/src/abstract/fft.ts +519 -0
  184. package/src/abstract/hash-to-curve.ts +51 -27
  185. package/src/abstract/modular.ts +156 -143
  186. package/src/abstract/montgomery.ts +81 -111
  187. package/src/abstract/poseidon.ts +22 -18
  188. package/src/abstract/tower.ts +37 -68
  189. package/src/abstract/utils.ts +3 -378
  190. package/src/abstract/weierstrass.ts +752 -461
  191. package/src/bls12-381.ts +542 -507
  192. package/src/bn254.ts +47 -35
  193. package/src/ed25519.ts +104 -76
  194. package/src/ed448.ts +156 -105
  195. package/src/jubjub.ts +4 -0
  196. package/src/misc.ts +39 -34
  197. package/src/nist.ts +138 -126
  198. package/src/p256.ts +3 -3
  199. package/src/p384.ts +3 -3
  200. package/src/p521.ts +3 -3
  201. package/src/pasta.ts +5 -1
  202. package/src/secp256k1.ts +59 -47
  203. package/src/utils.ts +328 -0
  204. package/utils.d.ts +96 -0
  205. package/utils.d.ts.map +1 -0
  206. package/utils.js +313 -0
  207. package/utils.js.map +1 -0
package/src/bn254.ts CHANGED
@@ -13,7 +13,7 @@ There are huge compatibility issues in the ecosystem:
13
13
  https://github.com/scipr-lab/libff/blob/a44f482e18b8ac04d034c193bd9d7df7817ad73f/libff/algebra/curves/bn128/bn128_init.cpp#L166-L169
14
14
  3. halo2curves bn256 is also incompatible and returns different outputs
15
15
 
16
- We don't implement Point methods toHex / toRawBytes.
16
+ We don't implement Point methods toHex / toBytes.
17
17
  To work around this limitation, has to initialize points on their own from BigInts.
18
18
  Reason it's not implemented is because [there is no standard](https://github.com/privacy-scaling-explorations/halo2curves/issues/109).
19
19
  Points of divergence:
@@ -54,32 +54,40 @@ Ate loop size: 6x+2
54
54
  * @module
55
55
  */
56
56
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
57
- import { sha256 } from '@noble/hashes/sha2';
58
- import { randomBytes } from '@noble/hashes/utils';
59
- import { getHash } from './_shortw_utils.ts';
57
+ import { sha256 } from '@noble/hashes/sha2.js';
60
58
  import {
61
59
  bls,
62
60
  type CurveFn as BLSCurveFn,
63
61
  type PostPrecomputeFn,
64
62
  type PostPrecomputePointAddFn,
65
63
  } from './abstract/bls.ts';
66
- import { Field } from './abstract/modular.ts';
64
+ import { Field, type IField } from './abstract/modular.ts';
67
65
  import type { Fp, Fp12, Fp2, Fp6 } from './abstract/tower.ts';
68
66
  import { psiFrobenius, tower12 } from './abstract/tower.ts';
69
- import { bitGet, bitLen, notImplemented } from './abstract/utils.ts';
70
- import { type CurveFn, weierstrass } from './abstract/weierstrass.ts';
67
+ import { type CurveFn, weierstrass, type WeierstrassOpts } from './abstract/weierstrass.ts';
68
+ import { bitGet, bitLen, notImplemented } from './utils.ts';
71
69
  // prettier-ignore
72
- const _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
70
+ const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
73
71
  const _6n = BigInt(6);
74
72
 
75
73
  const BN_X = BigInt('4965661367192848881');
76
74
  const BN_X_LEN = bitLen(BN_X);
77
75
  const SIX_X_SQUARED = _6n * BN_X ** _2n;
78
76
 
77
+ const bn254_G1_CURVE: WeierstrassOpts<bigint> = {
78
+ p: BigInt('0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47'),
79
+ n: BigInt('0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001'),
80
+ h: _1n,
81
+ a: _0n,
82
+ b: _3n,
83
+ Gx: _1n,
84
+ Gy: BigInt(2),
85
+ };
86
+
87
+ // r == n
79
88
  // Finite field over r. It's for convenience and is not used in the code below.
80
- const Fr = Field(
81
- BigInt('21888242871839275222246405745257275088548364400416034343698204186575808495617')
82
- );
89
+ export const bn254_Fr: IField<bigint> = Field(bn254_G1_CURVE.n);
90
+
83
91
  // Fp2.div(Fp2.mul(Fp2.ONE, _3n), Fp2.NONRESIDUE)
84
92
  const Fp2B = {
85
93
  c0: BigInt('19485874751759354771024239261021720505790618469301721065564631296452457478373'),
@@ -87,7 +95,7 @@ const Fp2B = {
87
95
  };
88
96
 
89
97
  const { Fp, Fp2, Fp6, Fp4Square, Fp12 } = tower12({
90
- ORDER: BigInt('21888242871839275222246405745257275088696311157297823662689037894645226208583'),
98
+ ORDER: bn254_G1_CURVE.p,
91
99
  FP2_NONRESIDUE: [BigInt(9), _1n],
92
100
  Fp2mulByB: (num) => Fp2.mul(num, Fp2B),
93
101
  // The result of any pairing is in a cyclotomic subgroup
@@ -161,7 +169,7 @@ const htfDefaults = Object.freeze({
161
169
  k: 128,
162
170
  expand: 'xmd',
163
171
  hash: sha256,
164
- } as const);
172
+ });
165
173
 
166
174
  export const _postPrecompute: PostPrecomputeFn = (
167
175
  Rx: Fp2,
@@ -177,20 +185,33 @@ export const _postPrecompute: PostPrecomputeFn = (
177
185
  pointAdd(Rx, Ry, Rz, q2[0], Fp2.neg(q2[1]));
178
186
  };
179
187
 
188
+ // cofactor: (36 * X^4) + (36 * X^3) + (30 * X^2) + 6*X + 1
189
+ const bn254_G2_CURVE: WeierstrassOpts<Fp2> = {
190
+ p: Fp2.ORDER,
191
+ n: bn254_G1_CURVE.n,
192
+ h: BigInt('0x30644e72e131a029b85045b68181585e06ceecda572a2489345f2299c0f9fa8d'),
193
+ a: Fp2.ZERO,
194
+ b: Fp2B,
195
+ Gx: Fp2.fromBigTuple([
196
+ BigInt('10857046999023057135944570762232829481370756359578518086990519993285655852781'),
197
+ BigInt('11559732032986387107991004021392285783925812861821192530917403151452391805634'),
198
+ ]),
199
+ Gy: Fp2.fromBigTuple([
200
+ BigInt('8495653923123431417604973247489272438418190587263600148770280649306958101930'),
201
+ BigInt('4082367875863433681332203403145435568316851327593401208105741076214120093531'),
202
+ ]),
203
+ };
204
+
180
205
  /**
181
206
  * bn254 (a.k.a. alt_bn128) pairing-friendly curve.
182
207
  * Contains G1 / G2 operations and pairings.
183
208
  */
184
209
  export const bn254: BLSCurveFn = bls({
185
210
  // Fields
186
- fields: { Fp, Fp2, Fp6, Fp12, Fr },
211
+ fields: { Fp, Fp2, Fp6, Fp12, Fr: bn254_Fr },
187
212
  G1: {
213
+ ...bn254_G1_CURVE,
188
214
  Fp,
189
- h: BigInt(1),
190
- Gx: BigInt(1),
191
- Gy: BigInt(2),
192
- a: Fp.ZERO,
193
- b: _3n,
194
215
  htfDefaults: { ...htfDefaults, m: 1, DST: 'BN254G2_XMD:SHA-256_SVDW_RO_' },
195
216
  wrapPrivateKey: true,
196
217
  allowInfinityPoint: true,
@@ -198,25 +219,16 @@ export const bn254: BLSCurveFn = bls({
198
219
  fromBytes: notImplemented,
199
220
  toBytes: notImplemented,
200
221
  ShortSignature: {
222
+ fromBytes: notImplemented,
201
223
  fromHex: notImplemented,
224
+ toBytes: notImplemented,
202
225
  toRawBytes: notImplemented,
203
226
  toHex: notImplemented,
204
227
  },
205
228
  },
206
229
  G2: {
230
+ ...bn254_G2_CURVE,
207
231
  Fp: Fp2,
208
- // cofactor: (36 * X^4) + (36 * X^3) + (30 * X^2) + 6*X + 1
209
- h: BigInt('21888242871839275222246405745257275088844257914179612981679871602714643921549'),
210
- Gx: Fp2.fromBigTuple([
211
- BigInt('10857046999023057135944570762232829481370756359578518086990519993285655852781'),
212
- BigInt('11559732032986387107991004021392285783925812861821192530917403151452391805634'),
213
- ]),
214
- Gy: Fp2.fromBigTuple([
215
- BigInt('8495653923123431417604973247489272438418190587263600148770280649306958101930'),
216
- BigInt('4082367875863433681332203403145435568316851327593401208105741076214120093531'),
217
- ]),
218
- a: Fp2.ZERO,
219
- b: Fp2B,
220
232
  hEff: BigInt('21888242871839275222246405745257275088844257914179612981679871602714643921549'),
221
233
  htfDefaults: { ...htfDefaults },
222
234
  wrapPrivateKey: true,
@@ -226,21 +238,21 @@ export const bn254: BLSCurveFn = bls({
226
238
  fromBytes: notImplemented,
227
239
  toBytes: notImplemented,
228
240
  Signature: {
241
+ fromBytes: notImplemented,
229
242
  fromHex: notImplemented,
243
+ toBytes: notImplemented,
230
244
  toRawBytes: notImplemented,
231
245
  toHex: notImplemented,
232
246
  },
233
247
  },
234
248
  params: {
235
249
  ateLoopSize: BN_X * _6n + _2n,
236
- r: Fr.ORDER,
250
+ r: bn254_Fr.ORDER,
237
251
  xNegative: false,
238
252
  twistType: 'divisive',
239
253
  },
240
254
  htfDefaults,
241
255
  hash: sha256,
242
- randomBytes,
243
-
244
256
  postPrecompute: _postPrecompute,
245
257
  });
246
258
 
@@ -258,5 +270,5 @@ export const bn254_weierstrass: CurveFn = weierstrass({
258
270
  Gx: BigInt(1),
259
271
  Gy: BigInt(2),
260
272
  h: BigInt(1),
261
- ...getHash(sha256),
273
+ hash: sha256,
262
274
  });
package/src/ed25519.ts CHANGED
@@ -6,16 +6,21 @@
6
6
  * @module
7
7
  */
8
8
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
9
- import { sha512 } from '@noble/hashes/sha2';
10
- import { concatBytes, randomBytes, utf8ToBytes } from '@noble/hashes/utils';
9
+ import { sha512 } from '@noble/hashes/sha2.js';
10
+ import { abytes, concatBytes, utf8ToBytes } from '@noble/hashes/utils.js';
11
11
  import { type AffinePoint, type Group, pippenger } from './abstract/curve.ts';
12
- import { type CurveFn, type ExtPointType, twistedEdwards } from './abstract/edwards.ts';
12
+ import {
13
+ type CurveFn,
14
+ type EdwardsOpts,
15
+ type ExtPointType,
16
+ twistedEdwards,
17
+ } from './abstract/edwards.ts';
13
18
  import {
14
19
  createHasher,
15
20
  expand_message_xmd,
16
- type Hasher,
21
+ type H2CHasher,
22
+ type H2CMethod,
17
23
  type htfBasicOpts,
18
- type HTFMethod,
19
24
  } from './abstract/hash-to-curve.ts';
20
25
  import { Field, FpInvertBatch, FpSqrtEven, isNegativeLE, mod, pow2 } from './abstract/modular.ts';
21
26
  import { montgomery, type CurveFn as XCurveFn } from './abstract/montgomery.ts';
@@ -26,27 +31,33 @@ import {
26
31
  equalBytes,
27
32
  type Hex,
28
33
  numberToBytesLE,
29
- } from './abstract/utils.ts';
30
-
31
- // 2n**255n - 19n
32
- const ED25519_P = BigInt(
33
- '57896044618658097711785492504343953926634992332820282019728792003956564819949'
34
- );
35
- // √(-1) aka √(a) aka 2^((p-1)/4)
36
- // Fp.sqrt(Fp.neg(1))
37
- const ED25519_SQRT_M1 = /* @__PURE__ */ BigInt(
38
- '19681161376707505956807079304988542015446066515923890162744021073123829784752'
39
- );
34
+ } from './utils.ts';
40
35
 
41
36
  // prettier-ignore
42
37
  const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
43
38
  // prettier-ignore
44
39
  const _5n = BigInt(5), _8n = BigInt(8);
45
40
 
41
+ // 2n**255n - 19n
42
+ // Removing Fp.create() will still work, and is 10% faster on sign
43
+ // a: Fp.create(BigInt(-1)),
44
+ // d is -121665/121666 a.k.a. Fp.neg(121665 * Fp.inv(121666))
45
+ // Finite field 2n**255n - 19n
46
+ // Subgroup order 2n**252n + 27742317777372353535851937790883648493n;
47
+ const ed25519_CURVE: EdwardsOpts = {
48
+ p: BigInt('0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed'),
49
+ n: BigInt('0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed'),
50
+ h: _8n,
51
+ a: BigInt('0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec'),
52
+ d: BigInt('0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3'),
53
+ Gx: BigInt('0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a'),
54
+ Gy: BigInt('0x6666666666666666666666666666666666666666666666666666666666666658'),
55
+ };
56
+
46
57
  function ed25519_pow_2_252_3(x: bigint) {
47
58
  // prettier-ignore
48
59
  const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
49
- const P = ED25519_P;
60
+ const P = ed25519_CURVE.p;
50
61
  const x2 = (x * x) % P;
51
62
  const b2 = (x2 * x) % P; // x^3, 11
52
63
  const b4 = (pow2(b2, _2n, P) * b2) % P; // x^15, 1111
@@ -74,9 +85,14 @@ function adjustScalarBytes(bytes: Uint8Array): Uint8Array {
74
85
  return bytes;
75
86
  }
76
87
 
88
+ // √(-1) aka √(a) aka 2^((p-1)/4)
89
+ // Fp.sqrt(Fp.neg(1))
90
+ const ED25519_SQRT_M1 = /* @__PURE__ */ BigInt(
91
+ '19681161376707505956807079304988542015446066515923890162744021073123829784752'
92
+ );
77
93
  // sqrt(u/v)
78
94
  function uvRatio(u: bigint, v: bigint): { isValid: boolean; value: bigint } {
79
- const P = ED25519_P;
95
+ const P = ed25519_CURVE.p;
80
96
  const v3 = mod(v * v * v, P); // v³
81
97
  const v7 = mod(v3 * v3 * v, P); // v⁷
82
98
  // (p+3)/8 and (p-5)/8
@@ -106,29 +122,18 @@ export const ED25519_TORSION_SUBGROUP: string[] = [
106
122
  'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa',
107
123
  ];
108
124
 
109
- const Fp = /* @__PURE__ */ (() => Field(ED25519_P, undefined, true))();
110
-
111
- const ed25519Defaults = /* @__PURE__ */ (() =>
112
- ({
113
- // Removing Fp.create() will still work, and is 10% faster on sign
114
- a: Fp.create(BigInt(-1)),
115
- // d is -121665/121666 a.k.a. Fp.neg(121665 * Fp.inv(121666))
116
- d: BigInt('37095705934669439343138083508754565189542113879843219016388785533085940283555'),
117
- // Finite field 2n**255n - 19n
118
- Fp,
119
- // Subgroup order 2n**252n + 27742317777372353535851937790883648493n;
120
- n: BigInt('7237005577332262213973186563042994240857116359379907606001950938285454250989'),
121
- h: _8n,
122
- Gx: BigInt('15112221349535400772501151409588531511454012693041857206046113283949847762202'),
123
- Gy: BigInt('46316835694926478169428394003475163141307993866256225615783033603165251855960'),
124
- hash: sha512,
125
- randomBytes,
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
- }) as const)();
125
+ const Fp = /* @__PURE__ */ (() => Field(ed25519_CURVE.p, undefined, true))();
126
+
127
+ const ed25519Defaults = /* @__PURE__ */ (() => ({
128
+ ...ed25519_CURVE,
129
+ Fp,
130
+ hash: sha512,
131
+ adjustScalarBytes,
132
+ // dom2
133
+ // Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
134
+ // Constant-time, u/√v
135
+ uvRatio,
136
+ }))();
132
137
 
133
138
  /**
134
139
  * ed25519 curve with EdDSA signatures.
@@ -176,22 +181,19 @@ export const ed25519ph: CurveFn = /* @__PURE__ */ (() =>
176
181
  * x25519.getPublicKey(priv) === x25519.scalarMultBase(priv);
177
182
  * x25519.getPublicKey(x25519.utils.randomPrivateKey());
178
183
  */
179
- export const x25519: XCurveFn = /* @__PURE__ */ (() =>
180
- montgomery({
181
- P: ED25519_P,
182
- a: BigInt(486662),
183
- montgomeryBits: 255, // n is 253 bits
184
- nByteLength: 32,
185
- Gu: BigInt(9),
184
+ export const x25519: XCurveFn = /* @__PURE__ */ (() => {
185
+ const P = ed25519_CURVE.p;
186
+ return montgomery({
187
+ P,
188
+ type: 'x25519',
186
189
  powPminus2: (x: bigint): bigint => {
187
- const P = ED25519_P;
188
190
  // x^(p-2) aka x^(2^255-21)
189
191
  const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
190
192
  return mod(pow2(pow_p_5_8, _3n, P) * b2, P);
191
193
  },
192
194
  adjustScalarBytes,
193
- randomBytes,
194
- }))();
195
+ });
196
+ })();
195
197
 
196
198
  /**
197
199
  * Converts ed25519 public key to x25519 public key. Uses formula:
@@ -203,7 +205,8 @@ export const x25519: XCurveFn = /* @__PURE__ */ (() =>
203
205
  * x25519.getSharedSecret(aPriv, edwardsToMontgomeryPub(someonesPub))
204
206
  */
205
207
  export function edwardsToMontgomeryPub(edwardsPub: Hex): Uint8Array {
206
- const { y } = ed25519.ExtendedPoint.fromHex(edwardsPub);
208
+ const bpub = ensureBytes('pub', edwardsPub);
209
+ const { y } = ed25519.Point.fromHex(bpub);
207
210
  const _1n = BigInt(1);
208
211
  return Fp.toBytes(Fp.create((_1n + y) * Fp.inv(_1n - y)));
209
212
  }
@@ -294,9 +297,9 @@ function map_to_curve_elligator2_edwards25519(u: bigint) {
294
297
  return { x: Fp.mul(xn, xd_inv), y: Fp.mul(yn, yd_inv) }; // 13. return (xn, xd, yn, yd)
295
298
  }
296
299
 
297
- export const ed25519_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
300
+ export const ed25519_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() =>
298
301
  createHasher(
299
- ed25519.ExtendedPoint,
302
+ ed25519.Point,
300
303
  (scalars: bigint[]) => map_to_curve_elligator2_edwards25519(scalars[0]),
301
304
  {
302
305
  DST: 'edwards25519_XMD:SHA-512_ELL2_RO_',
@@ -308,8 +311,8 @@ export const ed25519_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
308
311
  hash: sha512,
309
312
  }
310
313
  ))();
311
- export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => ed25519_hasher.hashToCurve)();
312
- export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() =>
314
+ export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => ed25519_hasher.hashToCurve)();
315
+ export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
313
316
  ed25519_hasher.encodeToCurve)();
314
317
 
315
318
  function aristp(other: unknown) {
@@ -345,8 +348,11 @@ const bytes255ToNumberLE = (bytes: Uint8Array) =>
345
348
 
346
349
  type ExtendedPoint = ExtPointType;
347
350
 
348
- // Computes Elligator map for Ristretto
349
- // https://ristretto.group/formulas/elligator.html
351
+ /**
352
+ * Computes Elligator map for Ristretto255.
353
+ * Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-B) and on
354
+ * the [website](https://ristretto.group/formulas/elligator.html).
355
+ */
350
356
  function calcElligatorRistrettoMap(r0: bigint): ExtendedPoint {
351
357
  const { d } = ed25519.CURVE;
352
358
  const P = ed25519.CURVE.Fp.ORDER;
@@ -366,7 +372,7 @@ function calcElligatorRistrettoMap(r0: bigint): ExtendedPoint {
366
372
  const W1 = mod(Nt * SQRT_AD_MINUS_ONE); // 11
367
373
  const W2 = mod(_1n - s2); // 12
368
374
  const W3 = mod(_1n + s2); // 13
369
- return new ed25519.ExtendedPoint(mod(W0 * W3), mod(W2 * W1), mod(W1 * W3), mod(W0 * W2));
375
+ return new ed25519.Point(mod(W0 * W3), mod(W2 * W1), mod(W1 * W3), mod(W0 * W2));
370
376
  }
371
377
 
372
378
  /**
@@ -374,7 +380,7 @@ function calcElligatorRistrettoMap(r0: bigint): ExtendedPoint {
374
380
  * a source of bugs for protocols like ring signatures. Ristretto was created to solve this.
375
381
  * Ristretto point operates in X:Y:Z:T extended coordinates like ExtendedPoint,
376
382
  * but it should work in its own namespace: do not combine those two.
377
- * https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-ristretto255-decaf448
383
+ * See [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
378
384
  */
379
385
  class RistPoint implements Group<RistPoint> {
380
386
  static BASE: RistPoint;
@@ -387,14 +393,15 @@ class RistPoint implements Group<RistPoint> {
387
393
  }
388
394
 
389
395
  static fromAffine(ap: AffinePoint<bigint>): RistPoint {
390
- return new RistPoint(ed25519.ExtendedPoint.fromAffine(ap));
396
+ return new RistPoint(ed25519.Point.fromAffine(ap));
391
397
  }
392
398
 
393
399
  /**
394
400
  * Takes uniform output of 64-byte hash function like sha512 and converts it to `RistrettoPoint`.
395
401
  * The hash-to-group operation applies Elligator twice and adds the results.
396
402
  * **Note:** this is one-way map, there is no conversion from point to hash.
397
- * https://ristretto.group/formulas/elligator.html
403
+ * Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-B) and on
404
+ * the [website](https://ristretto.group/formulas/elligator.html).
398
405
  * @param hex 64-byte output of a hash function
399
406
  */
400
407
  static hashToCurve(hex: Hex): RistPoint {
@@ -406,16 +413,21 @@ class RistPoint implements Group<RistPoint> {
406
413
  return new RistPoint(R1.add(R2));
407
414
  }
408
415
 
416
+ static fromBytes(bytes: Uint8Array): RistPoint {
417
+ abytes(bytes);
418
+ return this.fromHex(bytes);
419
+ }
420
+
409
421
  /**
410
422
  * Converts ristretto-encoded string to ristretto point.
411
- * https://ristretto.group/formulas/decoding.html
423
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
412
424
  * @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
413
425
  */
414
426
  static fromHex(hex: Hex): RistPoint {
415
427
  hex = ensureBytes('ristrettoHex', hex, 32);
416
428
  const { a, d } = ed25519.CURVE;
417
- const P = ed25519.CURVE.Fp.ORDER;
418
- const mod = ed25519.CURVE.Fp.create;
429
+ const P = Fp.ORDER;
430
+ const mod = Fp.create;
419
431
  const emsg = 'RistrettoPoint.fromHex: the hex is not valid encoding of RistrettoPoint';
420
432
  const s = bytes255ToNumberLE(hex);
421
433
  // 1. Check that s_bytes is the canonical encoding of a field element, or else abort.
@@ -435,7 +447,7 @@ class RistPoint implements Group<RistPoint> {
435
447
  const y = mod(u1 * Dy); // 11
436
448
  const t = mod(x * y); // 12
437
449
  if (!isValid || isNegativeLE(t, P) || y === _0n) throw new Error(emsg);
438
- return new RistPoint(new ed25519.ExtendedPoint(x, y, _1n, t));
450
+ return new RistPoint(new ed25519.Point(x, y, _1n, t));
439
451
  }
440
452
 
441
453
  static msm(points: RistPoint[], scalars: bigint[]): RistPoint {
@@ -445,12 +457,12 @@ class RistPoint implements Group<RistPoint> {
445
457
 
446
458
  /**
447
459
  * Encodes ristretto point to Uint8Array.
448
- * https://ristretto.group/formulas/encoding.html
460
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
449
461
  */
450
- toRawBytes(): Uint8Array {
462
+ toBytes(): Uint8Array {
451
463
  let { ex: x, ey: y, ez: z, et: t } = this.ep;
452
- const P = ed25519.CURVE.Fp.ORDER;
453
- const mod = ed25519.CURVE.Fp.create;
464
+ const P = Fp.ORDER;
465
+ const mod = Fp.create;
454
466
  const u1 = mod(mod(z + y) * mod(z - y)); // 1
455
467
  const u2 = mod(x * y); // 2
456
468
  // Square root always exists
@@ -475,20 +487,28 @@ class RistPoint implements Group<RistPoint> {
475
487
  return numberToBytesLE(s, 32); // 11
476
488
  }
477
489
 
490
+ /** @deprecated use `toBytes` */
491
+ toRawBytes(): Uint8Array {
492
+ return this.toBytes();
493
+ }
494
+
478
495
  toHex(): string {
479
- return bytesToHex(this.toRawBytes());
496
+ return bytesToHex(this.toBytes());
480
497
  }
481
498
 
482
499
  toString(): string {
483
500
  return this.toHex();
484
501
  }
485
502
 
486
- // Compare one point to another.
503
+ /**
504
+ * Compares two Ristretto points.
505
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
506
+ */
487
507
  equals(other: RistPoint): boolean {
488
508
  aristp(other);
489
509
  const { ex: X1, ey: Y1 } = this.ep;
490
510
  const { ex: X2, ey: Y2 } = other.ep;
491
- const mod = ed25519.CURVE.Fp.create;
511
+ const mod = Fp.create;
492
512
  // (x1 * y2 == y1 * x2) | (y1 * y2 == x1 * x2)
493
513
  const one = mod(X1 * Y2) === mod(Y1 * X2);
494
514
  const two = mod(Y1 * Y2) === mod(X1 * X2);
@@ -521,13 +541,21 @@ class RistPoint implements Group<RistPoint> {
521
541
  return new RistPoint(this.ep.negate());
522
542
  }
523
543
  }
544
+
545
+ /**
546
+ * Wrapper over Edwards Point for ristretto255 from
547
+ * [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
548
+ */
524
549
  export const RistrettoPoint: typeof RistPoint = /* @__PURE__ */ (() => {
525
- if (!RistPoint.BASE) RistPoint.BASE = new RistPoint(ed25519.ExtendedPoint.BASE);
526
- if (!RistPoint.ZERO) RistPoint.ZERO = new RistPoint(ed25519.ExtendedPoint.ZERO);
550
+ if (!RistPoint.BASE) RistPoint.BASE = new RistPoint(ed25519.Point.BASE);
551
+ if (!RistPoint.ZERO) RistPoint.ZERO = new RistPoint(ed25519.Point.ZERO);
527
552
  return RistPoint;
528
553
  })();
529
554
 
530
- // Hashing to ristretto255. https://www.rfc-editor.org/rfc/rfc9380#appendix-B
555
+ /**
556
+ * hash-to-curve for ristretto255.
557
+ * Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-B).
558
+ */
531
559
  export const hashToRistretto255 = (msg: Uint8Array, options: htfBasicOpts): RistPoint => {
532
560
  const d = options.DST;
533
561
  const DST = typeof d === 'string' ? utf8ToBytes(d) : d;