@noble/curves 1.9.1 → 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 (189) hide show
  1. package/README.md +56 -25
  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 +86 -7
  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 +138 -102
  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 +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 +24 -11
  27. package/abstract/modular.d.ts.map +1 -1
  28. package/abstract/modular.js +49 -20
  29. package/abstract/modular.js.map +1 -1
  30. package/abstract/montgomery.d.ts +1 -1
  31. package/abstract/montgomery.d.ts.map +1 -1
  32. package/abstract/montgomery.js +5 -4
  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 +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 +132 -76
  47. package/abstract/weierstrass.d.ts.map +1 -1
  48. package/abstract/weierstrass.js +462 -398
  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 +8 -5
  59. package/ed25519.d.ts.map +1 -1
  60. package/ed25519.js +67 -54
  61. package/ed25519.js.map +1 -1
  62. package/ed448.d.ts +10 -6
  63. package/ed448.d.ts.map +1 -1
  64. package/ed448.js +80 -57
  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 +83 -8
  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 +138 -104
  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 +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 +24 -11
  91. package/esm/abstract/modular.d.ts.map +1 -1
  92. package/esm/abstract/modular.js +48 -19
  93. package/esm/abstract/modular.js.map +1 -1
  94. package/esm/abstract/montgomery.d.ts +1 -1
  95. package/esm/abstract/montgomery.d.ts.map +1 -1
  96. package/esm/abstract/montgomery.js +5 -4
  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 +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 +132 -76
  111. package/esm/abstract/weierstrass.d.ts.map +1 -1
  112. package/esm/abstract/weierstrass.js +460 -400
  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 +8 -5
  123. package/esm/ed25519.d.ts.map +1 -1
  124. package/esm/ed25519.js +62 -49
  125. package/esm/ed25519.js.map +1 -1
  126. package/esm/ed448.d.ts +10 -6
  127. package/esm/ed448.d.ts.map +1 -1
  128. package/esm/ed448.js +74 -51
  129. package/esm/ed448.js.map +1 -1
  130. package/esm/misc.d.ts.map +1 -1
  131. package/esm/misc.js +31 -26
  132. package/esm/misc.js.map +1 -1
  133. package/esm/nist.d.ts +7 -16
  134. package/esm/nist.d.ts.map +1 -1
  135. package/esm/nist.js +86 -97
  136. package/esm/nist.js.map +1 -1
  137. package/esm/p256.d.ts +3 -3
  138. package/esm/p384.d.ts +3 -3
  139. package/esm/p521.d.ts +3 -3
  140. package/esm/secp256k1.d.ts +6 -6
  141. package/esm/secp256k1.d.ts.map +1 -1
  142. package/esm/secp256k1.js +43 -40
  143. package/esm/secp256k1.js.map +1 -1
  144. package/esm/utils.d.ts +96 -0
  145. package/esm/utils.d.ts.map +1 -0
  146. package/esm/utils.js +279 -0
  147. package/esm/utils.js.map +1 -0
  148. package/misc.d.ts.map +1 -1
  149. package/misc.js +35 -30
  150. package/misc.js.map +1 -1
  151. package/nist.d.ts +7 -16
  152. package/nist.d.ts.map +1 -1
  153. package/nist.js +86 -97
  154. package/nist.js.map +1 -1
  155. package/p256.d.ts +3 -3
  156. package/p384.d.ts +3 -3
  157. package/p521.d.ts +3 -3
  158. package/package.json +14 -5
  159. package/secp256k1.d.ts +6 -6
  160. package/secp256k1.d.ts.map +1 -1
  161. package/secp256k1.js +46 -43
  162. package/secp256k1.js.map +1 -1
  163. package/src/_shortw_utils.ts +5 -15
  164. package/src/abstract/bls.ts +260 -145
  165. package/src/abstract/curve.ts +115 -13
  166. package/src/abstract/edwards.ts +279 -138
  167. package/src/abstract/fft.ts +30 -19
  168. package/src/abstract/hash-to-curve.ts +51 -27
  169. package/src/abstract/modular.ts +49 -28
  170. package/src/abstract/montgomery.ts +9 -7
  171. package/src/abstract/poseidon.ts +22 -18
  172. package/src/abstract/tower.ts +36 -67
  173. package/src/abstract/utils.ts +3 -378
  174. package/src/abstract/weierstrass.ts +700 -453
  175. package/src/bls12-381.ts +540 -489
  176. package/src/bn254.ts +47 -35
  177. package/src/ed25519.ts +80 -64
  178. package/src/ed448.ts +129 -92
  179. package/src/misc.ts +39 -34
  180. package/src/nist.ts +138 -127
  181. package/src/p256.ts +3 -3
  182. package/src/p384.ts +3 -3
  183. package/src/p521.ts +3 -3
  184. package/src/secp256k1.ts +58 -46
  185. package/src/utils.ts +328 -0
  186. package/utils.d.ts +96 -0
  187. package/utils.d.ts.map +1 -0
  188. package/utils.js +313 -0
  189. 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,19 +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,
184
+ export const x25519: XCurveFn = /* @__PURE__ */ (() => {
185
+ const P = ed25519_CURVE.p;
186
+ return montgomery({
187
+ P,
182
188
  type: 'x25519',
183
189
  powPminus2: (x: bigint): bigint => {
184
- const P = ED25519_P;
185
190
  // x^(p-2) aka x^(2^255-21)
186
191
  const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
187
192
  return mod(pow2(pow_p_5_8, _3n, P) * b2, P);
188
193
  },
189
194
  adjustScalarBytes,
190
- randomBytes,
191
- }))();
195
+ });
196
+ })();
192
197
 
193
198
  /**
194
199
  * Converts ed25519 public key to x25519 public key. Uses formula:
@@ -200,7 +205,8 @@ export const x25519: XCurveFn = /* @__PURE__ */ (() =>
200
205
  * x25519.getSharedSecret(aPriv, edwardsToMontgomeryPub(someonesPub))
201
206
  */
202
207
  export function edwardsToMontgomeryPub(edwardsPub: Hex): Uint8Array {
203
- const { y } = ed25519.ExtendedPoint.fromHex(edwardsPub);
208
+ const bpub = ensureBytes('pub', edwardsPub);
209
+ const { y } = ed25519.Point.fromHex(bpub);
204
210
  const _1n = BigInt(1);
205
211
  return Fp.toBytes(Fp.create((_1n + y) * Fp.inv(_1n - y)));
206
212
  }
@@ -291,9 +297,9 @@ function map_to_curve_elligator2_edwards25519(u: bigint) {
291
297
  return { x: Fp.mul(xn, xd_inv), y: Fp.mul(yn, yd_inv) }; // 13. return (xn, xd, yn, yd)
292
298
  }
293
299
 
294
- export const ed25519_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
300
+ export const ed25519_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() =>
295
301
  createHasher(
296
- ed25519.ExtendedPoint,
302
+ ed25519.Point,
297
303
  (scalars: bigint[]) => map_to_curve_elligator2_edwards25519(scalars[0]),
298
304
  {
299
305
  DST: 'edwards25519_XMD:SHA-512_ELL2_RO_',
@@ -305,8 +311,8 @@ export const ed25519_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
305
311
  hash: sha512,
306
312
  }
307
313
  ))();
308
- export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => ed25519_hasher.hashToCurve)();
309
- export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() =>
314
+ export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => ed25519_hasher.hashToCurve)();
315
+ export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
310
316
  ed25519_hasher.encodeToCurve)();
311
317
 
312
318
  function aristp(other: unknown) {
@@ -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
  /**
@@ -387,7 +393,7 @@ 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
  /**
@@ -407,6 +413,11 @@ class RistPoint implements Group<RistPoint> {
407
413
  return new RistPoint(R1.add(R2));
408
414
  }
409
415
 
416
+ static fromBytes(bytes: Uint8Array): RistPoint {
417
+ abytes(bytes);
418
+ return this.fromHex(bytes);
419
+ }
420
+
410
421
  /**
411
422
  * Converts ristretto-encoded string to ristretto point.
412
423
  * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
@@ -415,8 +426,8 @@ class RistPoint implements Group<RistPoint> {
415
426
  static fromHex(hex: Hex): RistPoint {
416
427
  hex = ensureBytes('ristrettoHex', hex, 32);
417
428
  const { a, d } = ed25519.CURVE;
418
- const P = ed25519.CURVE.Fp.ORDER;
419
- const mod = ed25519.CURVE.Fp.create;
429
+ const P = Fp.ORDER;
430
+ const mod = Fp.create;
420
431
  const emsg = 'RistrettoPoint.fromHex: the hex is not valid encoding of RistrettoPoint';
421
432
  const s = bytes255ToNumberLE(hex);
422
433
  // 1. Check that s_bytes is the canonical encoding of a field element, or else abort.
@@ -436,7 +447,7 @@ class RistPoint implements Group<RistPoint> {
436
447
  const y = mod(u1 * Dy); // 11
437
448
  const t = mod(x * y); // 12
438
449
  if (!isValid || isNegativeLE(t, P) || y === _0n) throw new Error(emsg);
439
- return new RistPoint(new ed25519.ExtendedPoint(x, y, _1n, t));
450
+ return new RistPoint(new ed25519.Point(x, y, _1n, t));
440
451
  }
441
452
 
442
453
  static msm(points: RistPoint[], scalars: bigint[]): RistPoint {
@@ -448,10 +459,10 @@ class RistPoint implements Group<RistPoint> {
448
459
  * Encodes ristretto point to Uint8Array.
449
460
  * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
450
461
  */
451
- toRawBytes(): Uint8Array {
462
+ toBytes(): Uint8Array {
452
463
  let { ex: x, ey: y, ez: z, et: t } = this.ep;
453
- const P = ed25519.CURVE.Fp.ORDER;
454
- const mod = ed25519.CURVE.Fp.create;
464
+ const P = Fp.ORDER;
465
+ const mod = Fp.create;
455
466
  const u1 = mod(mod(z + y) * mod(z - y)); // 1
456
467
  const u2 = mod(x * y); // 2
457
468
  // Square root always exists
@@ -476,8 +487,13 @@ class RistPoint implements Group<RistPoint> {
476
487
  return numberToBytesLE(s, 32); // 11
477
488
  }
478
489
 
490
+ /** @deprecated use `toBytes` */
491
+ toRawBytes(): Uint8Array {
492
+ return this.toBytes();
493
+ }
494
+
479
495
  toHex(): string {
480
- return bytesToHex(this.toRawBytes());
496
+ return bytesToHex(this.toBytes());
481
497
  }
482
498
 
483
499
  toString(): string {
@@ -492,7 +508,7 @@ class RistPoint implements Group<RistPoint> {
492
508
  aristp(other);
493
509
  const { ex: X1, ey: Y1 } = this.ep;
494
510
  const { ex: X2, ey: Y2 } = other.ep;
495
- const mod = ed25519.CURVE.Fp.create;
511
+ const mod = Fp.create;
496
512
  // (x1 * y2 == y1 * x2) | (y1 * y2 == x1 * x2)
497
513
  const one = mod(X1 * Y2) === mod(Y1 * X2);
498
514
  const two = mod(Y1 * Y2) === mod(X1 * X2);
@@ -531,8 +547,8 @@ class RistPoint implements Group<RistPoint> {
531
547
  * [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
532
548
  */
533
549
  export const RistrettoPoint: typeof RistPoint = /* @__PURE__ */ (() => {
534
- if (!RistPoint.BASE) RistPoint.BASE = new RistPoint(ed25519.ExtendedPoint.BASE);
535
- 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);
536
552
  return RistPoint;
537
553
  })();
538
554