@noble/curves 1.8.2 → 1.9.1

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 (199) hide show
  1. package/README.md +49 -24
  2. package/abstract/bls.js +1 -1
  3. package/abstract/bls.js.map +1 -1
  4. package/abstract/curve.d.ts +1 -1
  5. package/abstract/curve.d.ts.map +1 -1
  6. package/abstract/curve.js +13 -4
  7. package/abstract/curve.js.map +1 -1
  8. package/abstract/edwards.d.ts.map +1 -1
  9. package/abstract/edwards.js +17 -3
  10. package/abstract/edwards.js.map +1 -1
  11. package/abstract/fft.d.ts +120 -0
  12. package/abstract/fft.d.ts.map +1 -0
  13. package/abstract/fft.js +439 -0
  14. package/abstract/fft.js.map +1 -0
  15. package/abstract/hash-to-curve.d.ts +10 -5
  16. package/abstract/hash-to-curve.d.ts.map +1 -1
  17. package/abstract/hash-to-curve.js +31 -23
  18. package/abstract/hash-to-curve.js.map +1 -1
  19. package/abstract/modular.d.ts +13 -12
  20. package/abstract/modular.d.ts.map +1 -1
  21. package/abstract/modular.js +158 -158
  22. package/abstract/modular.js.map +1 -1
  23. package/abstract/montgomery.d.ts +4 -9
  24. package/abstract/montgomery.d.ts.map +1 -1
  25. package/abstract/montgomery.js +70 -90
  26. package/abstract/montgomery.js.map +1 -1
  27. package/abstract/poseidon.d.ts +39 -2
  28. package/abstract/poseidon.d.ts.map +1 -1
  29. package/abstract/poseidon.js +183 -4
  30. package/abstract/poseidon.js.map +1 -1
  31. package/abstract/tower.d.ts.map +1 -1
  32. package/abstract/tower.js +4 -5
  33. package/abstract/tower.js.map +1 -1
  34. package/abstract/utils.d.ts +1 -0
  35. package/abstract/utils.d.ts.map +1 -1
  36. package/abstract/utils.js +2 -0
  37. package/abstract/utils.js.map +1 -1
  38. package/abstract/weierstrass.d.ts +31 -9
  39. package/abstract/weierstrass.d.ts.map +1 -1
  40. package/abstract/weierstrass.js +67 -48
  41. package/abstract/weierstrass.js.map +1 -1
  42. package/bls12-381.d.ts.map +1 -1
  43. package/bls12-381.js +9 -23
  44. package/bls12-381.js.map +1 -1
  45. package/bn254.d.ts +1 -0
  46. package/bn254.d.ts.map +1 -1
  47. package/bn254.js +10 -0
  48. package/bn254.js.map +1 -1
  49. package/ed25519.d.ts +19 -5
  50. package/ed25519.d.ts.map +1 -1
  51. package/ed25519.js +29 -18
  52. package/ed25519.js.map +1 -1
  53. package/ed448.d.ts +21 -5
  54. package/ed448.d.ts.map +1 -1
  55. package/ed448.js +46 -34
  56. package/ed448.js.map +1 -1
  57. package/esm/abstract/bls.js +1 -1
  58. package/esm/abstract/bls.js.map +1 -1
  59. package/esm/abstract/curve.d.ts +1 -1
  60. package/esm/abstract/curve.d.ts.map +1 -1
  61. package/esm/abstract/curve.js +13 -4
  62. package/esm/abstract/curve.js.map +1 -1
  63. package/esm/abstract/edwards.d.ts.map +1 -1
  64. package/esm/abstract/edwards.js +19 -5
  65. package/esm/abstract/edwards.js.map +1 -1
  66. package/esm/abstract/fft.d.ts +120 -0
  67. package/esm/abstract/fft.d.ts.map +1 -0
  68. package/esm/abstract/fft.js +426 -0
  69. package/esm/abstract/fft.js.map +1 -0
  70. package/esm/abstract/hash-to-curve.d.ts +10 -5
  71. package/esm/abstract/hash-to-curve.d.ts.map +1 -1
  72. package/esm/abstract/hash-to-curve.js +32 -24
  73. package/esm/abstract/hash-to-curve.js.map +1 -1
  74. package/esm/abstract/modular.d.ts +13 -12
  75. package/esm/abstract/modular.d.ts.map +1 -1
  76. package/esm/abstract/modular.js +158 -158
  77. package/esm/abstract/modular.js.map +1 -1
  78. package/esm/abstract/montgomery.d.ts +4 -9
  79. package/esm/abstract/montgomery.d.ts.map +1 -1
  80. package/esm/abstract/montgomery.js +71 -91
  81. package/esm/abstract/montgomery.js.map +1 -1
  82. package/esm/abstract/poseidon.d.ts +39 -2
  83. package/esm/abstract/poseidon.d.ts.map +1 -1
  84. package/esm/abstract/poseidon.js +180 -5
  85. package/esm/abstract/poseidon.js.map +1 -1
  86. package/esm/abstract/tower.d.ts.map +1 -1
  87. package/esm/abstract/tower.js +4 -5
  88. package/esm/abstract/tower.js.map +1 -1
  89. package/esm/abstract/utils.d.ts +1 -0
  90. package/esm/abstract/utils.d.ts.map +1 -1
  91. package/esm/abstract/utils.js +2 -0
  92. package/esm/abstract/utils.js.map +1 -1
  93. package/esm/abstract/weierstrass.d.ts +31 -9
  94. package/esm/abstract/weierstrass.d.ts.map +1 -1
  95. package/esm/abstract/weierstrass.js +69 -50
  96. package/esm/abstract/weierstrass.js.map +1 -1
  97. package/esm/bls12-381.d.ts.map +1 -1
  98. package/esm/bls12-381.js +9 -23
  99. package/esm/bls12-381.js.map +1 -1
  100. package/esm/bn254.d.ts +1 -0
  101. package/esm/bn254.d.ts.map +1 -1
  102. package/esm/bn254.js +10 -0
  103. package/esm/bn254.js.map +1 -1
  104. package/esm/ed25519.d.ts +19 -5
  105. package/esm/ed25519.d.ts.map +1 -1
  106. package/esm/ed25519.js +29 -18
  107. package/esm/ed25519.js.map +1 -1
  108. package/esm/ed448.d.ts +21 -5
  109. package/esm/ed448.d.ts.map +1 -1
  110. package/esm/ed448.js +47 -35
  111. package/esm/ed448.js.map +1 -1
  112. package/esm/jubjub.d.ts +11 -1
  113. package/esm/jubjub.d.ts.map +1 -1
  114. package/esm/jubjub.js +11 -1
  115. package/esm/jubjub.js.map +1 -1
  116. package/esm/misc.d.ts +8 -2
  117. package/esm/misc.d.ts.map +1 -1
  118. package/esm/misc.js +10 -4
  119. package/esm/misc.js.map +1 -1
  120. package/esm/nist.d.ts +30 -0
  121. package/esm/nist.d.ts.map +1 -0
  122. package/esm/nist.js +121 -0
  123. package/esm/nist.js.map +1 -0
  124. package/esm/p256.d.ts +7 -9
  125. package/esm/p256.d.ts.map +1 -1
  126. package/esm/p256.js +6 -44
  127. package/esm/p256.js.map +1 -1
  128. package/esm/p384.d.ts +9 -10
  129. package/esm/p384.d.ts.map +1 -1
  130. package/esm/p384.js +7 -46
  131. package/esm/p384.js.map +1 -1
  132. package/esm/p521.d.ts +7 -8
  133. package/esm/p521.d.ts.map +1 -1
  134. package/esm/p521.js +6 -46
  135. package/esm/p521.js.map +1 -1
  136. package/esm/pasta.d.ts +9 -1
  137. package/esm/pasta.d.ts.map +1 -1
  138. package/esm/pasta.js +9 -1
  139. package/esm/pasta.js.map +1 -1
  140. package/esm/secp256k1.d.ts +3 -3
  141. package/esm/secp256k1.d.ts.map +1 -1
  142. package/esm/secp256k1.js +8 -9
  143. package/esm/secp256k1.js.map +1 -1
  144. package/jubjub.d.ts +11 -1
  145. package/jubjub.d.ts.map +1 -1
  146. package/jubjub.js +12 -5
  147. package/jubjub.js.map +1 -1
  148. package/misc.d.ts +8 -2
  149. package/misc.d.ts.map +1 -1
  150. package/misc.js +11 -5
  151. package/misc.js.map +1 -1
  152. package/nist.d.ts +30 -0
  153. package/nist.d.ts.map +1 -0
  154. package/nist.js +124 -0
  155. package/nist.js.map +1 -0
  156. package/p256.d.ts +7 -9
  157. package/p256.d.ts.map +1 -1
  158. package/p256.js +5 -49
  159. package/p256.js.map +1 -1
  160. package/p384.d.ts +9 -10
  161. package/p384.d.ts.map +1 -1
  162. package/p384.js +6 -51
  163. package/p384.js.map +1 -1
  164. package/p521.d.ts +7 -8
  165. package/p521.d.ts.map +1 -1
  166. package/p521.js +5 -51
  167. package/p521.js.map +1 -1
  168. package/package.json +117 -8
  169. package/pasta.d.ts +9 -1
  170. package/pasta.d.ts.map +1 -1
  171. package/pasta.js +9 -3
  172. package/pasta.js.map +1 -1
  173. package/secp256k1.d.ts +3 -3
  174. package/secp256k1.d.ts.map +1 -1
  175. package/secp256k1.js +9 -10
  176. package/secp256k1.js.map +1 -1
  177. package/src/abstract/bls.ts +1 -1
  178. package/src/abstract/curve.ts +11 -6
  179. package/src/abstract/edwards.ts +26 -12
  180. package/src/abstract/fft.ts +508 -0
  181. package/src/abstract/hash-to-curve.ts +44 -36
  182. package/src/abstract/modular.ts +154 -153
  183. package/src/abstract/montgomery.ts +78 -109
  184. package/src/abstract/poseidon.ts +208 -13
  185. package/src/abstract/tower.ts +4 -5
  186. package/src/abstract/utils.ts +2 -0
  187. package/src/abstract/weierstrass.ts +109 -61
  188. package/src/bls12-381.ts +11 -27
  189. package/src/bn254.ts +10 -0
  190. package/src/ed25519.ts +32 -19
  191. package/src/ed448.ts +91 -75
  192. package/src/jubjub.ts +12 -5
  193. package/src/misc.ts +10 -4
  194. package/src/nist.ts +155 -0
  195. package/src/p256.ts +6 -50
  196. package/src/p384.ts +8 -56
  197. package/src/p521.ts +6 -65
  198. package/src/pasta.ts +9 -1
  199. package/src/secp256k1.ts +12 -11
package/src/ed448.ts CHANGED
@@ -15,10 +15,11 @@ import { type CurveFn, type ExtPointType, twistedEdwards } from './abstract/edwa
15
15
  import {
16
16
  createHasher,
17
17
  expand_message_xof,
18
+ type Hasher,
18
19
  type htfBasicOpts,
19
20
  type HTFMethod,
20
21
  } from './abstract/hash-to-curve.ts';
21
- import { Field, isNegativeLE, mod, pow2 } from './abstract/modular.ts';
22
+ import { Field, FpInvertBatch, isNegativeLE, mod, pow2 } from './abstract/modular.ts';
22
23
  import { montgomery, type CurveFn as XCurveFn } from './abstract/montgomery.ts';
23
24
  import {
24
25
  bytesToHex,
@@ -29,8 +30,8 @@ import {
29
30
  numberToBytesLE,
30
31
  } from './abstract/utils.ts';
31
32
 
32
- const shake256_114 = wrapConstructor(() => shake256.create({ dkLen: 114 }));
33
- const shake256_64 = wrapConstructor(() => shake256.create({ dkLen: 64 }));
33
+ const shake256_114 = /* @__PURE__ */ wrapConstructor(() => shake256.create({ dkLen: 114 }));
34
+ const shake256_64 = /* @__PURE__ */ wrapConstructor(() => shake256.create({ dkLen: 64 }));
34
35
  const ed448P = BigInt(
35
36
  '726838724295606890549323807888004534353641360687318060281490199180612328166730772686396383698676545930088884461843637361053498018365439'
36
37
  );
@@ -93,47 +94,49 @@ function uvRatio(u: bigint, v: bigint): { isValid: boolean; value: bigint } {
93
94
  return { isValid: mod(x2 * v, P) === u, value: x };
94
95
  }
95
96
 
96
- const Fp = Field(ed448P, 456, true);
97
-
98
- const ED448_DEF = {
99
- // Param: a
100
- a: BigInt(1),
101
- // -39081 a.k.a. Fp.neg(39081)
102
- d: BigInt(
103
- '726838724295606890549323807888004534353641360687318060281490199180612328166730772686396383698676545930088884461843637361053498018326358'
104
- ),
105
- // Finite field 2n**448n - 2n**224n - 1n
106
- Fp,
107
- // Subgroup order
108
- // 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n
109
- n: BigInt(
110
- '181709681073901722637330951972001133588410340171829515070372549795146003961539585716195755291692375963310293709091662304773755859649779'
111
- ),
112
- // RFC 7748 has 56-byte keys, RFC 8032 has 57-byte keys
113
- nBitLength: 456,
114
- h: BigInt(4),
115
- Gx: BigInt(
116
- '224580040295924300187604334099896036246789641632564134246125461686950415467406032909029192869357953282578032075146446173674602635247710'
117
- ),
118
- Gy: BigInt(
119
- '298819210078481492676017930443930673437544040154080242095928241372331506189835876003536878655418784733982303233503462500531545062832660'
120
- ),
121
- // SHAKE256(dom4(phflag,context)||x, 114)
122
- hash: shake256_114,
123
- randomBytes,
124
- adjustScalarBytes,
125
- // dom4
126
- domain: (data: Uint8Array, ctx: Uint8Array, phflag: boolean) => {
127
- if (ctx.length > 255) throw new Error('context must be smaller than 255, got: ' + ctx.length);
128
- return concatBytes(
129
- utf8ToBytes('SigEd448'),
130
- new Uint8Array([phflag ? 1 : 0, ctx.length]),
131
- ctx,
132
- data
133
- );
134
- },
135
- uvRatio,
136
- } as const;
97
+ // Finite field 2n**448n - 2n**224n - 1n
98
+ const Fp = /* @__PURE__ */ (() => Field(ed448P, 456, true))();
99
+
100
+ const ED448_DEF = /* @__PURE__ */ (() =>
101
+ ({
102
+ // Param: a
103
+ a: BigInt(1),
104
+ // -39081 a.k.a. Fp.neg(39081)
105
+ d: BigInt(
106
+ '726838724295606890549323807888004534353641360687318060281490199180612328166730772686396383698676545930088884461843637361053498018326358'
107
+ ),
108
+ // Finite field 2n**448n - 2n**224n - 1n
109
+ Fp,
110
+ // Subgroup order
111
+ // 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n
112
+ n: BigInt(
113
+ '181709681073901722637330951972001133588410340171829515070372549795146003961539585716195755291692375963310293709091662304773755859649779'
114
+ ),
115
+ // RFC 7748 has 56-byte keys, RFC 8032 has 57-byte keys
116
+ nBitLength: 456,
117
+ h: BigInt(4),
118
+ Gx: BigInt(
119
+ '224580040295924300187604334099896036246789641632564134246125461686950415467406032909029192869357953282578032075146446173674602635247710'
120
+ ),
121
+ Gy: BigInt(
122
+ '298819210078481492676017930443930673437544040154080242095928241372331506189835876003536878655418784733982303233503462500531545062832660'
123
+ ),
124
+ // SHAKE256(dom4(phflag,context)||x, 114)
125
+ hash: shake256_114,
126
+ randomBytes,
127
+ adjustScalarBytes,
128
+ // dom4
129
+ domain: (data: Uint8Array, ctx: Uint8Array, phflag: boolean) => {
130
+ if (ctx.length > 255) throw new Error('context must be smaller than 255, got: ' + ctx.length);
131
+ return concatBytes(
132
+ utf8ToBytes('SigEd448'),
133
+ new Uint8Array([phflag ? 1 : 0, ctx.length]),
134
+ ctx,
135
+ data
136
+ );
137
+ },
138
+ uvRatio,
139
+ }) as const)();
137
140
 
138
141
  /**
139
142
  * ed448 EdDSA curve and methods.
@@ -145,28 +148,27 @@ const ED448_DEF = {
145
148
  * const sig = ed448.sign(msg, priv);
146
149
  * ed448.verify(sig, msg, pub);
147
150
  */
148
- export const ed448: CurveFn = /* @__PURE__ */ twistedEdwards(ED448_DEF);
151
+ export const ed448: CurveFn = twistedEdwards(ED448_DEF);
149
152
  // NOTE: there is no ed448ctx, since ed448 supports ctx by default
150
- export const ed448ph: CurveFn = /* @__PURE__ */ twistedEdwards({
151
- ...ED448_DEF,
152
- prehash: shake256_64,
153
- });
153
+ export const ed448ph: CurveFn = /* @__PURE__ */ (() =>
154
+ twistedEdwards({
155
+ ...ED448_DEF,
156
+ prehash: shake256_64,
157
+ }))();
154
158
 
155
159
  /**
156
160
  * ECDH using curve448 aka x448.
161
+ * x448 has 56-byte keys as per RFC 7748, while
162
+ * ed448 has 57-byte keys as per RFC 8032.
157
163
  */
158
164
  export const x448: XCurveFn = /* @__PURE__ */ (() =>
159
165
  montgomery({
160
- a: BigInt(156326),
161
- // RFC 7748 has 56-byte keys, RFC 8032 has 57-byte keys
162
- montgomeryBits: 448,
163
- nByteLength: 56,
164
166
  P: ed448P,
165
- Gu: BigInt(5),
167
+ type: 'x448',
166
168
  powPminus2: (x: bigint): bigint => {
167
169
  const P = ed448P;
168
170
  const Pminus3div4 = ed448_pow_Pminus3div4(x);
169
- const Pminus3 = pow2(Pminus3div4, BigInt(2), P);
171
+ const Pminus3 = pow2(Pminus3div4, _2n, P);
170
172
  return mod(Pminus3 * x, P); // Pminus3 * x = Pminus2
171
173
  },
172
174
  adjustScalarBytes,
@@ -191,8 +193,8 @@ export const edwardsToMontgomery: typeof edwardsToMontgomeryPub = edwardsToMontg
191
193
  // TODO: add edwardsToMontgomeryPriv, similar to ed25519 version
192
194
 
193
195
  // Hash To Curve Elligator2 Map
194
- const ELL2_C1 = (Fp.ORDER - BigInt(3)) / BigInt(4); // 1. c1 = (q - 3) / 4 # Integer arithmetic
195
- const ELL2_J = BigInt(156326);
196
+ const ELL2_C1 = /* @__PURE__ */ (() => (Fp.ORDER - BigInt(3)) / BigInt(4))(); // 1. c1 = (q - 3) / 4 # Integer arithmetic
197
+ const ELL2_J = /* @__PURE__ */ BigInt(156326);
196
198
 
197
199
  function map_to_curve_elligator2_curve448(u: bigint) {
198
200
  let tv1 = Fp.sqr(u); // 1. tv1 = u^2
@@ -263,11 +265,11 @@ function map_to_curve_elligator2_edwards448(u: bigint) {
263
265
  yEn = Fp.cmov(yEn, Fp.ONE, e); // 36. yEn = CMOV(yEn, 1, e)
264
266
  yEd = Fp.cmov(yEd, Fp.ONE, e); // 37. yEd = CMOV(yEd, 1, e)
265
267
 
266
- const inv = Fp.invertBatch([xEd, yEd]); // batch division
268
+ const inv = FpInvertBatch(Fp, [xEd, yEd], true); // batch division
267
269
  return { x: Fp.mul(xEn, inv[0]), y: Fp.mul(yEn, inv[1]) }; // 38. return (xEn, xEd, yEn, yEd)
268
270
  }
269
271
 
270
- const htf = /* @__PURE__ */ (() =>
272
+ export const ed448_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
271
273
  createHasher(
272
274
  ed448.ExtendedPoint,
273
275
  (scalars: bigint[]) => map_to_curve_elligator2_edwards448(scalars[0]),
@@ -281,29 +283,30 @@ const htf = /* @__PURE__ */ (() =>
281
283
  hash: shake256,
282
284
  }
283
285
  ))();
284
- export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
285
- export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();
286
+ export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => ed448_hasher.hashToCurve)();
287
+ export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() =>
288
+ ed448_hasher.encodeToCurve)();
286
289
 
287
290
  function adecafp(other: unknown) {
288
291
  if (!(other instanceof DcfPoint)) throw new Error('DecafPoint expected');
289
292
  }
290
293
 
291
294
  // 1-d
292
- const ONE_MINUS_D = BigInt('39082');
295
+ const ONE_MINUS_D = /* @__PURE__ */ BigInt('39082');
293
296
  // 1-2d
294
- const ONE_MINUS_TWO_D = BigInt('78163');
297
+ const ONE_MINUS_TWO_D = /* @__PURE__ */ BigInt('78163');
295
298
  // √(-d)
296
- const SQRT_MINUS_D = BigInt(
299
+ const SQRT_MINUS_D = /* @__PURE__ */ BigInt(
297
300
  '98944233647732219769177004876929019128417576295529901074099889598043702116001257856802131563896515373927712232092845883226922417596214'
298
301
  );
299
302
  // 1 / √(-d)
300
- const INVSQRT_MINUS_D = BigInt(
303
+ const INVSQRT_MINUS_D = /* @__PURE__ */ BigInt(
301
304
  '315019913931389607337177038330951043522456072897266928557328499619017160722351061360252776265186336876723201881398623946864393857820716'
302
305
  );
303
306
  // Calculates 1/√(number)
304
307
  const invertSqrt = (number: bigint) => uvRatio(_1n, number);
305
308
 
306
- const MAX_448B = BigInt(
309
+ const MAX_448B = /* @__PURE__ */ BigInt(
307
310
  '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
308
311
  );
309
312
  const bytes448ToNumberLE = (bytes: Uint8Array) =>
@@ -311,8 +314,11 @@ const bytes448ToNumberLE = (bytes: Uint8Array) =>
311
314
 
312
315
  type ExtendedPoint = ExtPointType;
313
316
 
314
- // Computes Elligator map for Decaf
315
- // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-ristretto255-decaf448-07#name-element-derivation-2
317
+ /**
318
+ * Elligator map for hash-to-curve of decaf448.
319
+ * Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-C)
320
+ * and [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-element-derivation-2).
321
+ */
316
322
  function calcElligatorDecafMap(r0: bigint): ExtendedPoint {
317
323
  const { d } = ed448.CURVE;
318
324
  const P = ed448.CURVE.Fp.ORDER;
@@ -347,7 +353,7 @@ function calcElligatorDecafMap(r0: bigint): ExtendedPoint {
347
353
  * a source of bugs for protocols like ring signatures. Decaf was created to solve this.
348
354
  * Decaf point operates in X:Y:Z:T extended coordinates like ExtendedPoint,
349
355
  * but it should work in its own namespace: do not combine those two.
350
- * https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-ristretto255-decaf448
356
+ * See [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
351
357
  */
352
358
  class DcfPoint implements Group<DcfPoint> {
353
359
  static BASE: DcfPoint;
@@ -367,7 +373,8 @@ class DcfPoint implements Group<DcfPoint> {
367
373
  * Takes uniform output of 112-byte hash function like shake256 and converts it to `DecafPoint`.
368
374
  * The hash-to-group operation applies Elligator twice and adds the results.
369
375
  * **Note:** this is one-way map, there is no conversion from point to hash.
370
- * https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-ristretto255-decaf448-07#name-element-derivation-2
376
+ * Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-C)
377
+ * and [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-element-derivation-2).
371
378
  * @param hex 112-byte output of a hash function
372
379
  */
373
380
  static hashToCurve(hex: Hex): DcfPoint {
@@ -381,7 +388,7 @@ class DcfPoint implements Group<DcfPoint> {
381
388
 
382
389
  /**
383
390
  * Converts decaf-encoded string to decaf point.
384
- * https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-ristretto255-decaf448-07#name-decode-2
391
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode-2).
385
392
  * @param hex Decaf-encoded 56 bytes. Not every 56-byte string is valid decaf encoding
386
393
  */
387
394
  static fromHex(hex: Hex): DcfPoint {
@@ -421,7 +428,7 @@ class DcfPoint implements Group<DcfPoint> {
421
428
 
422
429
  /**
423
430
  * Encodes decaf point to Uint8Array.
424
- * https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-ristretto255-decaf448-07#name-encode-2
431
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode-2).
425
432
  */
426
433
  toRawBytes(): Uint8Array {
427
434
  let { ex: x, ey: _y, ez: z, et: t } = this.ep;
@@ -451,8 +458,10 @@ class DcfPoint implements Group<DcfPoint> {
451
458
  return this.toHex();
452
459
  }
453
460
 
454
- // Compare one point to another.
455
- // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-ristretto255-decaf448-07#name-equals-2
461
+ /**
462
+ * Compare one point to another.
463
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals-2).
464
+ */
456
465
  equals(other: DcfPoint): boolean {
457
466
  adecafp(other);
458
467
  const { ex: X1, ey: Y1 } = this.ep;
@@ -489,6 +498,10 @@ class DcfPoint implements Group<DcfPoint> {
489
498
  }
490
499
  }
491
500
 
501
+ /**
502
+ * Wrapper over Edwards Point for decaf448 from
503
+ * [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
504
+ */
492
505
  export const DecafPoint: typeof DcfPoint = /* @__PURE__ */ (() => {
493
506
  // decaf448 base point is ed448 base x 2
494
507
  // https://github.com/dalek-cryptography/curve25519-dalek/blob/59837c6ecff02b77b9d5ff84dbc239d0cf33ef90/vendor/ristretto.sage#L699
@@ -497,7 +510,10 @@ export const DecafPoint: typeof DcfPoint = /* @__PURE__ */ (() => {
497
510
  return DcfPoint;
498
511
  })();
499
512
 
500
- // Hashing to decaf448. https://www.rfc-editor.org/rfc/rfc9380#appendix-C
513
+ /**
514
+ * hash-to-curve for decaf448.
515
+ * Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-C).
516
+ */
501
517
  export const hashToDecaf448 = (msg: Uint8Array, options: htfBasicOpts): DcfPoint => {
502
518
  const d = options.DST;
503
519
  const DST = typeof d === 'string' ? utf8ToBytes(d) : d;
package/src/jubjub.ts CHANGED
@@ -1,5 +1,12 @@
1
- export {
2
- jubjub_findGroupHash as findGroupHash,
3
- jubjub_groupHash as groupHash,
4
- jubjub,
5
- } from './misc.ts';
1
+ /**
2
+ * @deprecated
3
+ * @module
4
+ */
5
+ import { jubjub_findGroupHash, jubjub_groupHash, jubjub as jubjubn } from './misc.ts';
6
+
7
+ /** @deprecated Use `@noble/curves/misc` module directly. */
8
+ export const jubjub: typeof jubjubn = jubjubn;
9
+ /** @deprecated Use `@noble/curves/misc` module directly. */
10
+ export const findGroupHash: typeof jubjub_findGroupHash = jubjub_findGroupHash;
11
+ /** @deprecated Use `@noble/curves/misc` module directly. */
12
+ export const groupHash: typeof jubjub_groupHash = jubjub_groupHash;
package/src/misc.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  */
6
6
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
7
7
  import { blake256 } from '@noble/hashes/blake1';
8
- import { blake2s } from '@noble/hashes/blake2s';
8
+ import { blake2s } from '@noble/hashes/blake2';
9
9
  import { sha256, sha512 } from '@noble/hashes/sha2';
10
10
  import { concatBytes, randomBytes, utf8ToBytes } from '@noble/hashes/utils';
11
11
  import { getHash } from './_shortw_utils.ts';
@@ -70,7 +70,7 @@ export function jubjub_groupHash(tag: Uint8Array, personalization: Uint8Array):
70
70
 
71
71
  // No secret data is leaked here at all.
72
72
  // It operates over public data:
73
- // const G_SPEND = jubjub.findGroupHash(new Uint8Array(), utf8ToBytes('Item_G_'));
73
+ // const G_SPEND = jubjub.findGroupHash(Uint8Array.of(), utf8ToBytes('Item_G_'));
74
74
  export function jubjub_findGroupHash(m: Uint8Array, personalization: Uint8Array): ExtPointType {
75
75
  const tag = concatBytes(m, new Uint8Array([0]));
76
76
  const hashes = [];
@@ -93,7 +93,10 @@ export const pasta_q: bigint = BigInt(
93
93
  '0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001'
94
94
  );
95
95
 
96
- /** https://neuromancer.sk/std/other/Pallas */
96
+ /**
97
+ * https://neuromancer.sk/std/other/Pallas
98
+ * @deprecated
99
+ */
97
100
  export const pallas: WCurveFn = weierstrass({
98
101
  a: BigInt(0),
99
102
  b: BigInt(5),
@@ -104,7 +107,10 @@ export const pallas: WCurveFn = weierstrass({
104
107
  h: BigInt(1),
105
108
  ...getHash(sha256),
106
109
  });
107
- /** https://neuromancer.sk/std/other/Vesta */
110
+ /**
111
+ * https://neuromancer.sk/std/other/Vesta
112
+ * @deprecated
113
+ */
108
114
  export const vesta: WCurveFn = weierstrass({
109
115
  a: BigInt(0),
110
116
  b: BigInt(5),
package/src/nist.ts ADDED
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Internal module for NIST P256, P384, P521 curves.
3
+ * Do not use for now.
4
+ * @module
5
+ */
6
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
7
+ import { sha256, sha384, sha512 } from '@noble/hashes/sha2';
8
+ import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
9
+ import { createHasher, type Hasher } from './abstract/hash-to-curve.ts';
10
+ import { Field } from './abstract/modular.ts';
11
+ import { mapToCurveSimpleSWU } from './abstract/weierstrass.ts';
12
+
13
+ const Fp256 = Field(BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'));
14
+ const p256_a = Fp256.create(BigInt('-3'));
15
+ const p256_b = BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b');
16
+
17
+ /**
18
+ * secp256r1 curve, ECDSA and ECDH methods.
19
+ * Field: `2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n-1n`
20
+ */
21
+ // prettier-ignore
22
+ export const p256: CurveFnWithCreate = createCurve({
23
+ a: p256_a,
24
+ b: p256_b,
25
+ Fp: Fp256,
26
+ n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
27
+ Gx: BigInt('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'),
28
+ Gy: BigInt('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'),
29
+ h: BigInt(1),
30
+ lowS: false
31
+ } as const, sha256);
32
+ /** Alias to p256. */
33
+ export const secp256r1: CurveFnWithCreate = p256;
34
+
35
+ const p256_mapSWU = /* @__PURE__ */ (() =>
36
+ mapToCurveSimpleSWU(Fp256, {
37
+ A: p256_a,
38
+ B: p256_b,
39
+ Z: Fp256.create(BigInt('-10')),
40
+ }))();
41
+
42
+ /** Hashing / encoding to p256 points / field. RFC 9380 methods. */
43
+ export const p256_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
44
+ createHasher(secp256r1.ProjectivePoint, (scalars: bigint[]) => p256_mapSWU(scalars[0]), {
45
+ DST: 'P256_XMD:SHA-256_SSWU_RO_',
46
+ encodeDST: 'P256_XMD:SHA-256_SSWU_NU_',
47
+ p: Fp256.ORDER,
48
+ m: 1,
49
+ k: 128,
50
+ expand: 'xmd',
51
+ hash: sha256,
52
+ }))();
53
+
54
+ // Field over which we'll do calculations.
55
+ const Fp384 = Field(
56
+ BigInt(
57
+ '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff'
58
+ )
59
+ );
60
+ const p384_a = Fp384.create(BigInt('-3'));
61
+ // prettier-ignore
62
+ const p384_b = BigInt('0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef');
63
+
64
+ /**
65
+ * secp384r1 curve, ECDSA and ECDH methods.
66
+ * Field: `2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n`.
67
+ * */
68
+ // prettier-ignore
69
+ export const p384: CurveFnWithCreate = createCurve({
70
+ a: p384_a,
71
+ b: p384_b,
72
+ Fp: Fp384,
73
+ n: BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973'),
74
+ Gx: BigInt('0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7'),
75
+ Gy: BigInt('0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f'),
76
+ h: BigInt(1),
77
+ lowS: false
78
+ } as const, sha384);
79
+ /** Alias to p384. */
80
+ export const secp384r1: CurveFnWithCreate = p384;
81
+
82
+ const p384_mapSWU = /* @__PURE__ */ (() =>
83
+ mapToCurveSimpleSWU(Fp384, {
84
+ A: p384_a,
85
+ B: p384_b,
86
+ Z: Fp384.create(BigInt('-12')),
87
+ }))();
88
+
89
+ /** Hashing / encoding to p384 points / field. RFC 9380 methods. */
90
+ export const p384_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
91
+ createHasher(secp384r1.ProjectivePoint, (scalars: bigint[]) => p384_mapSWU(scalars[0]), {
92
+ DST: 'P384_XMD:SHA-384_SSWU_RO_',
93
+ encodeDST: 'P384_XMD:SHA-384_SSWU_NU_',
94
+ p: Fp384.ORDER,
95
+ m: 1,
96
+ k: 192,
97
+ expand: 'xmd',
98
+ hash: sha384,
99
+ }))();
100
+
101
+ // Field over which we'll do calculations.
102
+ const Fp521 = Field(
103
+ BigInt(
104
+ '0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
105
+ )
106
+ );
107
+
108
+ const p521_a = Fp521.create(BigInt('-3'));
109
+ const p521_b = BigInt(
110
+ '0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00'
111
+ );
112
+
113
+ /**
114
+ * NIST secp521r1 aka p521 curve, ECDSA and ECDH methods.
115
+ * Field: `2n**521n - 1n`.
116
+ */
117
+ // prettier-ignore
118
+ export const p521: CurveFnWithCreate = createCurve({
119
+ a: p521_a,
120
+ b: p521_b,
121
+ Fp: Fp521,
122
+ n: BigInt(
123
+ '0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409'
124
+ ),
125
+ Gx: BigInt(
126
+ '0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66'
127
+ ),
128
+ Gy: BigInt(
129
+ '0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'
130
+ ),
131
+ h: BigInt(1),
132
+ lowS: false,
133
+ allowedPrivateKeyLengths: [130, 131, 132] // P521 keys are variable-length. Normalize to 132b
134
+ } as const, sha512);
135
+ /** Alias to p521. */
136
+ export const secp521r1: CurveFnWithCreate = p521;
137
+
138
+ const p521_mapSWU = /* @__PURE__ */ (() =>
139
+ mapToCurveSimpleSWU(Fp521, {
140
+ A: p521_a,
141
+ B: p521_b,
142
+ Z: Fp521.create(BigInt('-4')),
143
+ }))();
144
+
145
+ /** Hashing / encoding to p521 points / field. RFC 9380 methods. */
146
+ export const p521_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
147
+ createHasher(secp521r1.ProjectivePoint, (scalars: bigint[]) => p521_mapSWU(scalars[0]), {
148
+ DST: 'P521_XMD:SHA-512_SSWU_RO_',
149
+ encodeDST: 'P521_XMD:SHA-512_SSWU_NU_',
150
+ p: Fp521.ORDER,
151
+ m: 1,
152
+ k: 256,
153
+ expand: 'xmd',
154
+ hash: sha512,
155
+ }))();
package/src/p256.ts CHANGED
@@ -1,55 +1,11 @@
1
1
  /**
2
2
  * NIST secp256r1 aka p256.
3
- * https://www.secg.org/sec2-v2.pdf, https://neuromancer.sk/std/nist/P-256
4
3
  * @module
5
4
  */
6
5
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
7
- import { sha256 } from '@noble/hashes/sha2';
8
- import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
9
- import { createHasher, type HTFMethod } from './abstract/hash-to-curve.ts';
10
- import { Field } from './abstract/modular.ts';
11
- import { mapToCurveSimpleSWU } from './abstract/weierstrass.ts';
12
-
13
- const Fp256 = Field(BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'));
14
- const CURVE_A = Fp256.create(BigInt('-3'));
15
- const CURVE_B = BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b');
16
-
17
- /**
18
- * secp256r1 curve, ECDSA and ECDH methods.
19
- * Field: `2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n-1n`
20
- */
21
- // prettier-ignore
22
- export const p256: CurveFnWithCreate = createCurve({
23
- a: CURVE_A,
24
- b: CURVE_B,
25
- Fp: Fp256,
26
- n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
27
- Gx: BigInt('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'),
28
- Gy: BigInt('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'),
29
- h: BigInt(1),
30
- lowS: false,
31
- } as const, sha256);
32
- /** Alias to p256. */
33
- export const secp256r1: CurveFnWithCreate = p256;
34
-
35
- const mapSWU = /* @__PURE__ */ (() =>
36
- mapToCurveSimpleSWU(Fp256, {
37
- A: CURVE_A,
38
- B: CURVE_B,
39
- Z: Fp256.create(BigInt('-10')),
40
- }))();
41
-
42
- const htf = /* @__PURE__ */ (() =>
43
- createHasher(secp256r1.ProjectivePoint, (scalars: bigint[]) => mapSWU(scalars[0]), {
44
- DST: 'P256_XMD:SHA-256_SSWU_RO_',
45
- encodeDST: 'P256_XMD:SHA-256_SSWU_NU_',
46
- p: Fp256.ORDER,
47
- m: 1,
48
- k: 128,
49
- expand: 'xmd',
50
- hash: sha256,
51
- }))();
52
- /** secp256r1 hash-to-curve from RFC 9380. */
53
- export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
54
- /** secp256r1 encode-to-curve from RFC 9380. */
55
- export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();
6
+ import { type HTFMethod } from './abstract/hash-to-curve.ts';
7
+ import { p256_hasher, p256 as p256n } from './nist.ts';
8
+ export const p256: typeof p256n = p256n;
9
+ export const secp256r1: typeof p256n = p256n;
10
+ export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => p256_hasher.hashToCurve)();
11
+ export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => p256_hasher.encodeToCurve)();
package/src/p384.ts CHANGED
@@ -1,61 +1,13 @@
1
1
  /**
2
2
  * NIST secp384r1 aka p384.
3
- * https://www.secg.org/sec2-v2.pdf, https://neuromancer.sk/std/nist/P-384
4
3
  * @module
5
4
  */
6
5
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
7
- import { sha384 } from '@noble/hashes/sha2';
8
- import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
9
- import { createHasher, type HTFMethod } from './abstract/hash-to-curve.ts';
10
- import { Field } from './abstract/modular.ts';
11
- import { mapToCurveSimpleSWU } from './abstract/weierstrass.ts';
12
-
13
- // Field over which we'll do calculations.
14
- const Fp384 = Field(
15
- BigInt(
16
- '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff'
17
- )
18
- );
19
- const CURVE_A = Fp384.create(BigInt('-3'));
20
- // prettier-ignore
21
- const CURVE_B = BigInt('0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef');
22
-
23
- /**
24
- * secp384r1 curve, ECDSA and ECDH methods.
25
- * Field: `2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n`.
26
- * */
27
- // prettier-ignore
28
- export const p384: CurveFnWithCreate = createCurve({
29
- a: CURVE_A,
30
- b: CURVE_B,
31
- Fp: Fp384,
32
- n: BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973'),
33
- Gx: BigInt('0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7'),
34
- Gy: BigInt('0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f'),
35
- h: BigInt(1),
36
- lowS: false,
37
- } as const, sha384);
38
- /** Alias to p384. */
39
- export const secp384r1: CurveFnWithCreate = p384;
40
-
41
- const mapSWU = /* @__PURE__ */ (() =>
42
- mapToCurveSimpleSWU(Fp384, {
43
- A: CURVE_A,
44
- B: CURVE_B,
45
- Z: Fp384.create(BigInt('-12')),
46
- }))();
47
-
48
- const htf = /* @__PURE__ */ (() =>
49
- createHasher(secp384r1.ProjectivePoint, (scalars: bigint[]) => mapSWU(scalars[0]), {
50
- DST: 'P384_XMD:SHA-384_SSWU_RO_',
51
- encodeDST: 'P384_XMD:SHA-384_SSWU_NU_',
52
- p: Fp384.ORDER,
53
- m: 1,
54
- k: 192,
55
- expand: 'xmd',
56
- hash: sha384,
57
- }))();
58
- /** secp384r1 hash-to-curve from RFC 9380. */
59
- export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
60
- /** secp384r1 encode-to-curve from RFC 9380. */
61
- export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();
6
+ import { type HTFMethod } from './abstract/hash-to-curve.ts';
7
+ import { p384_hasher, p384 as p384n } from './nist.ts';
8
+ export const p384: typeof p384n = p384n;
9
+ export const secp384r1: typeof p384n = p384n;
10
+ export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => p384_hasher.hashToCurve)();
11
+ export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => p384_hasher.encodeToCurve)();
12
+
13
+ /** @deprecated Use `import { p384_hasher } from "@noble/curves/nist"` module. */