@noble/curves 1.9.5 → 2.0.0-beta.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 (211) hide show
  1. package/README.md +267 -421
  2. package/abstract/bls.d.ts +49 -111
  3. package/abstract/bls.d.ts.map +1 -1
  4. package/abstract/bls.js +108 -152
  5. package/abstract/bls.js.map +1 -1
  6. package/abstract/curve.d.ts +7 -48
  7. package/abstract/curve.d.ts.map +1 -1
  8. package/abstract/curve.js +22 -47
  9. package/abstract/curve.js.map +1 -1
  10. package/abstract/edwards.d.ts +17 -68
  11. package/abstract/edwards.d.ts.map +1 -1
  12. package/abstract/edwards.js +98 -175
  13. package/abstract/edwards.js.map +1 -1
  14. package/abstract/fft.js +14 -27
  15. package/abstract/fft.js.map +1 -1
  16. package/abstract/hash-to-curve.d.ts +11 -24
  17. package/abstract/hash-to-curve.d.ts.map +1 -1
  18. package/abstract/hash-to-curve.js +30 -35
  19. package/abstract/hash-to-curve.js.map +1 -1
  20. package/abstract/modular.d.ts +5 -17
  21. package/abstract/modular.d.ts.map +1 -1
  22. package/abstract/modular.js +166 -167
  23. package/abstract/modular.js.map +1 -1
  24. package/abstract/montgomery.d.ts +4 -9
  25. package/abstract/montgomery.d.ts.map +1 -1
  26. package/abstract/montgomery.js +17 -20
  27. package/abstract/montgomery.js.map +1 -1
  28. package/abstract/oprf.d.ts +282 -0
  29. package/abstract/oprf.d.ts.map +1 -0
  30. package/abstract/oprf.js +297 -0
  31. package/abstract/oprf.js.map +1 -0
  32. package/abstract/poseidon.js +20 -24
  33. package/abstract/poseidon.js.map +1 -1
  34. package/abstract/tower.d.ts +9 -7
  35. package/abstract/tower.d.ts.map +1 -1
  36. package/abstract/tower.js +600 -364
  37. package/abstract/tower.js.map +1 -1
  38. package/abstract/weierstrass.d.ts +12 -145
  39. package/abstract/weierstrass.d.ts.map +1 -1
  40. package/abstract/weierstrass.js +153 -377
  41. package/abstract/weierstrass.js.map +1 -1
  42. package/bls12-381.d.ts +2 -2
  43. package/bls12-381.d.ts.map +1 -1
  44. package/bls12-381.js +174 -216
  45. package/bls12-381.js.map +1 -1
  46. package/bn254.d.ts +58 -10
  47. package/bn254.d.ts.map +1 -1
  48. package/bn254.js +70 -130
  49. package/bn254.js.map +1 -1
  50. package/ed25519.d.ts +12 -31
  51. package/ed25519.d.ts.map +1 -1
  52. package/ed25519.js +104 -146
  53. package/ed25519.js.map +1 -1
  54. package/ed448.d.ts +14 -33
  55. package/ed448.d.ts.map +1 -1
  56. package/ed448.js +105 -132
  57. package/ed448.js.map +1 -1
  58. package/index.js +1 -1
  59. package/misc.d.ts +10 -14
  60. package/misc.d.ts.map +1 -1
  61. package/misc.js +51 -60
  62. package/misc.js.map +1 -1
  63. package/nist.d.ts +11 -14
  64. package/nist.d.ts.map +1 -1
  65. package/nist.js +46 -55
  66. package/nist.js.map +1 -1
  67. package/package.json +9 -224
  68. package/secp256k1.d.ts +7 -23
  69. package/secp256k1.d.ts.map +1 -1
  70. package/secp256k1.js +72 -83
  71. package/secp256k1.js.map +1 -1
  72. package/src/abstract/bls.ts +197 -344
  73. package/src/abstract/curve.ts +10 -83
  74. package/src/abstract/edwards.ts +96 -223
  75. package/src/abstract/hash-to-curve.ts +32 -45
  76. package/src/abstract/modular.ts +144 -130
  77. package/src/abstract/montgomery.ts +21 -22
  78. package/src/abstract/oprf.ts +600 -0
  79. package/src/abstract/tower.ts +627 -382
  80. package/src/abstract/weierstrass.ts +101 -482
  81. package/src/bls12-381.ts +148 -176
  82. package/src/bn254.ts +67 -122
  83. package/src/ed25519.ts +65 -118
  84. package/src/ed448.ts +63 -113
  85. package/src/index.ts +1 -1
  86. package/src/misc.ts +66 -49
  87. package/src/nist.ts +48 -57
  88. package/src/secp256k1.ts +56 -88
  89. package/src/utils.ts +41 -61
  90. package/src/webcrypto.ts +362 -0
  91. package/utils.d.ts +28 -19
  92. package/utils.d.ts.map +1 -1
  93. package/utils.js +45 -121
  94. package/utils.js.map +1 -1
  95. package/webcrypto.d.ts +47 -0
  96. package/webcrypto.d.ts.map +1 -0
  97. package/webcrypto.js +231 -0
  98. package/webcrypto.js.map +1 -0
  99. package/esm/_shortw_utils.d.ts +0 -19
  100. package/esm/_shortw_utils.d.ts.map +0 -1
  101. package/esm/_shortw_utils.js +0 -16
  102. package/esm/_shortw_utils.js.map +0 -1
  103. package/esm/abstract/bls.d.ts +0 -190
  104. package/esm/abstract/bls.d.ts.map +0 -1
  105. package/esm/abstract/bls.js +0 -408
  106. package/esm/abstract/bls.js.map +0 -1
  107. package/esm/abstract/curve.d.ts +0 -231
  108. package/esm/abstract/curve.d.ts.map +0 -1
  109. package/esm/abstract/curve.js +0 -465
  110. package/esm/abstract/curve.js.map +0 -1
  111. package/esm/abstract/edwards.d.ts +0 -237
  112. package/esm/abstract/edwards.d.ts.map +0 -1
  113. package/esm/abstract/edwards.js +0 -632
  114. package/esm/abstract/edwards.js.map +0 -1
  115. package/esm/abstract/fft.d.ts +0 -122
  116. package/esm/abstract/fft.d.ts.map +0 -1
  117. package/esm/abstract/fft.js +0 -425
  118. package/esm/abstract/fft.js.map +0 -1
  119. package/esm/abstract/hash-to-curve.d.ts +0 -102
  120. package/esm/abstract/hash-to-curve.d.ts.map +0 -1
  121. package/esm/abstract/hash-to-curve.js +0 -203
  122. package/esm/abstract/hash-to-curve.js.map +0 -1
  123. package/esm/abstract/modular.d.ts +0 -171
  124. package/esm/abstract/modular.d.ts.map +0 -1
  125. package/esm/abstract/modular.js +0 -530
  126. package/esm/abstract/modular.js.map +0 -1
  127. package/esm/abstract/montgomery.d.ts +0 -30
  128. package/esm/abstract/montgomery.d.ts.map +0 -1
  129. package/esm/abstract/montgomery.js +0 -157
  130. package/esm/abstract/montgomery.js.map +0 -1
  131. package/esm/abstract/poseidon.d.ts +0 -68
  132. package/esm/abstract/poseidon.d.ts.map +0 -1
  133. package/esm/abstract/poseidon.js +0 -296
  134. package/esm/abstract/poseidon.js.map +0 -1
  135. package/esm/abstract/tower.d.ts +0 -93
  136. package/esm/abstract/tower.d.ts.map +0 -1
  137. package/esm/abstract/tower.js +0 -502
  138. package/esm/abstract/tower.js.map +0 -1
  139. package/esm/abstract/utils.d.ts +0 -5
  140. package/esm/abstract/utils.d.ts.map +0 -1
  141. package/esm/abstract/utils.js +0 -7
  142. package/esm/abstract/utils.js.map +0 -1
  143. package/esm/abstract/weierstrass.d.ts +0 -412
  144. package/esm/abstract/weierstrass.d.ts.map +0 -1
  145. package/esm/abstract/weierstrass.js +0 -1428
  146. package/esm/abstract/weierstrass.js.map +0 -1
  147. package/esm/bls12-381.d.ts +0 -16
  148. package/esm/bls12-381.d.ts.map +0 -1
  149. package/esm/bls12-381.js +0 -738
  150. package/esm/bls12-381.js.map +0 -1
  151. package/esm/bn254.d.ts +0 -18
  152. package/esm/bn254.d.ts.map +0 -1
  153. package/esm/bn254.js +0 -246
  154. package/esm/bn254.js.map +0 -1
  155. package/esm/ed25519.d.ts +0 -106
  156. package/esm/ed25519.d.ts.map +0 -1
  157. package/esm/ed25519.js +0 -467
  158. package/esm/ed25519.js.map +0 -1
  159. package/esm/ed448.d.ts +0 -101
  160. package/esm/ed448.d.ts.map +0 -1
  161. package/esm/ed448.js +0 -448
  162. package/esm/ed448.js.map +0 -1
  163. package/esm/index.d.ts +0 -2
  164. package/esm/index.d.ts.map +0 -1
  165. package/esm/index.js +0 -17
  166. package/esm/index.js.map +0 -1
  167. package/esm/jubjub.d.ts +0 -12
  168. package/esm/jubjub.d.ts.map +0 -1
  169. package/esm/jubjub.js +0 -12
  170. package/esm/jubjub.js.map +0 -1
  171. package/esm/misc.d.ts +0 -19
  172. package/esm/misc.d.ts.map +0 -1
  173. package/esm/misc.js +0 -109
  174. package/esm/misc.js.map +0 -1
  175. package/esm/nist.d.ts +0 -21
  176. package/esm/nist.d.ts.map +0 -1
  177. package/esm/nist.js +0 -132
  178. package/esm/nist.js.map +0 -1
  179. package/esm/p256.d.ts +0 -16
  180. package/esm/p256.d.ts.map +0 -1
  181. package/esm/p256.js +0 -16
  182. package/esm/p256.js.map +0 -1
  183. package/esm/p384.d.ts +0 -16
  184. package/esm/p384.d.ts.map +0 -1
  185. package/esm/p384.js +0 -16
  186. package/esm/p384.js.map +0 -1
  187. package/esm/p521.d.ts +0 -16
  188. package/esm/p521.d.ts.map +0 -1
  189. package/esm/p521.js +0 -16
  190. package/esm/p521.js.map +0 -1
  191. package/esm/package.json +0 -4
  192. package/esm/pasta.d.ts +0 -10
  193. package/esm/pasta.d.ts.map +0 -1
  194. package/esm/pasta.js +0 -10
  195. package/esm/pasta.js.map +0 -1
  196. package/esm/secp256k1.d.ts +0 -89
  197. package/esm/secp256k1.d.ts.map +0 -1
  198. package/esm/secp256k1.js +0 -292
  199. package/esm/secp256k1.js.map +0 -1
  200. package/esm/utils.d.ts +0 -110
  201. package/esm/utils.d.ts.map +0 -1
  202. package/esm/utils.js +0 -322
  203. package/esm/utils.js.map +0 -1
  204. package/src/_shortw_utils.ts +0 -21
  205. package/src/abstract/utils.ts +0 -7
  206. package/src/jubjub.ts +0 -12
  207. package/src/p256.ts +0 -15
  208. package/src/p384.ts +0 -15
  209. package/src/p521.ts +0 -15
  210. package/src/package.json +0 -3
  211. package/src/pasta.ts +0 -9
package/src/ed448.ts CHANGED
@@ -8,19 +8,13 @@
8
8
  */
9
9
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
10
10
  import { shake256 } from '@noble/hashes/sha3.js';
11
- import {
12
- abytes,
13
- concatBytes,
14
- utf8ToBytes,
15
- createHasher as wrapConstructor,
16
- } from '@noble/hashes/utils.js';
11
+ import { concatBytes, hexToBytes, createHasher as wrapConstructor } from '@noble/hashes/utils.js';
17
12
  import type { AffinePoint } from './abstract/curve.ts';
18
- import { pippenger } from './abstract/curve.ts';
19
13
  import {
14
+ eddsa,
20
15
  edwards,
21
16
  PrimeEdwardsPoint,
22
- twistedEdwards,
23
- type CurveFn,
17
+ type EdDSA,
24
18
  type EdwardsOpts,
25
19
  type EdwardsPoint,
26
20
  type EdwardsPointCons,
@@ -31,12 +25,12 @@ import {
31
25
  expand_message_xof,
32
26
  type H2CHasher,
33
27
  type H2CHasherBase,
34
- type H2CMethod,
35
28
  type htfBasicOpts,
36
29
  } from './abstract/hash-to-curve.ts';
37
30
  import { Field, FpInvertBatch, isNegativeLE, mod, pow2, type IField } from './abstract/modular.ts';
38
- import { montgomery, type MontgomeryECDH as XCurveFn } from './abstract/montgomery.ts';
39
- import { bytesToNumberLE, ensureBytes, equalBytes, numberToBytesLE, type Hex } from './utils.ts';
31
+ import { montgomery, type MontgomeryECDH } from './abstract/montgomery.ts';
32
+ import { createORPF, type OPRF } from './abstract/oprf.ts';
33
+ import { abytes, asciiToBytes, bytesToNumberLE, equalBytes } from './utils.ts';
40
34
 
41
35
  // edwards448 curve
42
36
  // a = 1n
@@ -140,33 +134,27 @@ function uvRatio(u: bigint, v: bigint): { isValid: boolean; value: bigint } {
140
134
  }
141
135
 
142
136
  // Finite field 2n**448n - 2n**224n - 1n
137
+ // The value fits in 448 bits, but we use 456-bit (57-byte) elements because of bitflags.
138
+ // - ed25519 fits in 255 bits, allowing using last 1 byte for specifying bit flag of point negation.
139
+ // - ed448 fits in 448 bits. We can't use last 1 byte: we can only use a bit 224 in the middle.
143
140
  const Fp = /* @__PURE__ */ (() => Field(ed448_CURVE.p, { BITS: 456, isLE: true }))();
144
- // RFC 7748 has 56-byte keys, RFC 8032 has 57-byte keys
145
- const Fn = /* @__PURE__ */ (() => Field(ed448_CURVE.n, { BITS: 448, isLE: true }))();
146
- // Fn456 has BITS: 456
141
+ const Fn = /* @__PURE__ */ (() => Field(ed448_CURVE.n, { BITS: 456, isLE: true }))();
142
+ // decaf448 uses 448-bit (56-byte) keys
143
+ const Fp448 = /* @__PURE__ */ (() => Field(ed448_CURVE.p, { BITS: 448, isLE: true }))();
144
+ const Fn448 = /* @__PURE__ */ (() => Field(ed448_CURVE.n, { BITS: 448, isLE: true }))();
147
145
 
148
146
  // SHAKE256(dom4(phflag,context)||x, 114)
149
147
  function dom4(data: Uint8Array, ctx: Uint8Array, phflag: boolean) {
150
148
  if (ctx.length > 255) throw new Error('context must be smaller than 255, got: ' + ctx.length);
151
149
  return concatBytes(
152
- utf8ToBytes('SigEd448'),
150
+ asciiToBytes('SigEd448'),
153
151
  new Uint8Array([phflag ? 1 : 0, ctx.length]),
154
152
  ctx,
155
153
  data
156
154
  );
157
155
  }
158
- // const ed448_eddsa_opts = { adjustScalarBytes, domain: dom4 };
159
- // const ed448_Point = edwards(ed448_CURVE, { Fp, Fn, uvRatio });
160
-
161
- const ED448_DEF = /* @__PURE__ */ (() => ({
162
- ...ed448_CURVE,
163
- Fp,
164
- Fn,
165
- hash: shake256_114,
166
- adjustScalarBytes,
167
- domain: dom4,
168
- uvRatio,
169
- }))();
156
+ const ed448_eddsa_opts = { adjustScalarBytes, domain: dom4 };
157
+ const ed448_Point = edwards(ed448_CURVE, { Fp, Fn, uvRatio });
170
158
 
171
159
  /**
172
160
  * ed448 EdDSA curve and methods.
@@ -177,15 +165,14 @@ const ED448_DEF = /* @__PURE__ */ (() => ({
177
165
  * const sig = ed448.sign(msg, secretKey);
178
166
  * const isValid = ed448.verify(sig, msg, publicKey);
179
167
  */
180
- export const ed448: CurveFn = twistedEdwards(ED448_DEF);
168
+ export const ed448: EdDSA = eddsa(ed448_Point, shake256_114, ed448_eddsa_opts);
181
169
 
182
170
  // There is no ed448ctx, since ed448 supports ctx by default
183
171
  /** Prehashed version of ed448. Accepts already-hashed messages in sign() and verify(). */
184
- export const ed448ph: CurveFn = /* @__PURE__ */ (() =>
185
- twistedEdwards({
186
- ...ED448_DEF,
187
- prehash: shake256_64,
188
- }))();
172
+ export const ed448ph: EdDSA = /* @__PURE__ */ eddsa(ed448_Point, shake256_114, {
173
+ ...ed448_eddsa_opts,
174
+ prehash: shake256_64,
175
+ });
189
176
 
190
177
  /**
191
178
  * E448 curve, defined by NIST.
@@ -199,7 +186,7 @@ export const E448: EdwardsPointCons = edwards(E448_CURVE);
199
186
  * x448 has 56-byte keys as per RFC 7748, while
200
187
  * ed448 has 57-byte keys as per RFC 8032.
201
188
  */
202
- export const x448: XCurveFn = /* @__PURE__ */ (() => {
189
+ export const x448: MontgomeryECDH = /* @__PURE__ */ (() => {
203
190
  const P = ed448_CURVE.p;
204
191
  return montgomery({
205
192
  P,
@@ -291,8 +278,8 @@ function map_to_curve_elligator2_edwards448(u: bigint) {
291
278
  }
292
279
 
293
280
  /** Hashing / encoding to ed448 points / field. RFC 9380 methods. */
294
- export const ed448_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() =>
295
- createHasher(ed448.Point, (scalars: bigint[]) => map_to_curve_elligator2_edwards448(scalars[0]), {
281
+ export const ed448_hasher: H2CHasher<EdwardsPointCons> = /* @__PURE__ */ (() =>
282
+ createHasher(ed448_Point, (scalars: bigint[]) => map_to_curve_elligator2_edwards448(scalars[0]), {
296
283
  DST: 'edwards448_XOF:SHAKE256_ELL2_RO_',
297
284
  encodeDST: 'edwards448_XOF:SHAKE256_ELL2_NU_',
298
285
  p: Fp.ORDER,
@@ -317,19 +304,12 @@ const INVSQRT_MINUS_D = /* @__PURE__ */ BigInt(
317
304
  // Calculates 1/√(number)
318
305
  const invertSqrt = (number: bigint) => uvRatio(_1n, number);
319
306
 
320
- const MAX_448B = /* @__PURE__ */ BigInt(
321
- '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
322
- );
323
- const bytes448ToNumberLE = (bytes: Uint8Array) => Fp.create(bytesToNumberLE(bytes) & MAX_448B);
324
-
325
- type ExtendedPoint = EdwardsPoint;
326
-
327
307
  /**
328
308
  * Elligator map for hash-to-curve of decaf448.
329
309
  * Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-C)
330
310
  * and [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-element-derivation-2).
331
311
  */
332
- function calcElligatorDecafMap(r0: bigint): ExtendedPoint {
312
+ function calcElligatorDecafMap(r0: bigint): EdwardsPoint {
333
313
  const { d } = ed448_CURVE;
334
314
  const P = Fp.ORDER;
335
315
  const mod = (n: bigint) => Fp.create(n);
@@ -355,22 +335,26 @@ function calcElligatorDecafMap(r0: bigint): ExtendedPoint {
355
335
  const W1 = mod(s2 + _1n); // 9
356
336
  const W2 = mod(s2 - _1n); // 10
357
337
  const W3 = mod(v_prime * s * (r - _1n) * ONE_MINUS_TWO_D + sgn); // 11
358
- return new ed448.Point(mod(W0 * W3), mod(W2 * W1), mod(W1 * W3), mod(W0 * W2));
338
+ return new ed448_Point(mod(W0 * W3), mod(W2 * W1), mod(W1 * W3), mod(W0 * W2));
359
339
  }
360
340
 
361
341
  function decaf448_map(bytes: Uint8Array): _DecafPoint {
362
342
  abytes(bytes, 112);
363
- const r1 = bytes448ToNumberLE(bytes.slice(0, 56));
343
+ const skipValidation = true;
344
+ // Note: Similar to the field element decoding described in
345
+ // [RFC7748], and unlike the field element decoding described in
346
+ // Section 5.3.1, non-canonical values are accepted.
347
+ const r1 = Fp448.create(Fp448.fromBytes(bytes.subarray(0, 56), skipValidation));
364
348
  const R1 = calcElligatorDecafMap(r1);
365
- const r2 = bytes448ToNumberLE(bytes.slice(56, 112));
349
+ const r2 = Fp448.create(Fp448.fromBytes(bytes.subarray(56, 112), skipValidation));
366
350
  const R2 = calcElligatorDecafMap(r2);
367
351
  return new _DecafPoint(R1.add(R2));
368
352
  }
369
353
 
370
354
  /**
371
- * Each ed448/ExtendedPoint has 4 different equivalent points. This can be
355
+ * Each ed448/EdwardsPoint has 4 different equivalent points. This can be
372
356
  * a source of bugs for protocols like ring signatures. Decaf was created to solve this.
373
- * Decaf point operates in X:Y:Z:T extended coordinates like ExtendedPoint,
357
+ * Decaf point operates in X:Y:Z:T extended coordinates like EdwardsPoint,
374
358
  * but it should work in its own namespace: do not combine those two.
375
359
  * See [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
376
360
  */
@@ -378,23 +362,23 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
378
362
  // The following gymnastics is done because typescript strips comments otherwise
379
363
  // prettier-ignore
380
364
  static BASE: _DecafPoint =
381
- /* @__PURE__ */ (() => new _DecafPoint(ed448.Point.BASE).multiplyUnsafe(_2n))();
365
+ /* @__PURE__ */ (() => new _DecafPoint(ed448_Point.BASE).multiplyUnsafe(_2n))();
382
366
  // prettier-ignore
383
367
  static ZERO: _DecafPoint =
384
- /* @__PURE__ */ (() => new _DecafPoint(ed448.Point.ZERO))();
368
+ /* @__PURE__ */ (() => new _DecafPoint(ed448_Point.ZERO))();
385
369
  // prettier-ignore
386
370
  static Fp: IField<bigint> =
387
- /* @__PURE__ */ (() => Fp)();
371
+ /* @__PURE__ */ (() => Fp448)();
388
372
  // prettier-ignore
389
373
  static Fn: IField<bigint> =
390
- /* @__PURE__ */ (() => Fn)();
374
+ /* @__PURE__ */ (() => Fn448)();
391
375
 
392
- constructor(ep: ExtendedPoint) {
376
+ constructor(ep: EdwardsPoint) {
393
377
  super(ep);
394
378
  }
395
379
 
396
380
  static fromAffine(ap: AffinePoint<bigint>): _DecafPoint {
397
- return new _DecafPoint(ed448.Point.fromAffine(ap));
381
+ return new _DecafPoint(ed448_Point.fromAffine(ap));
398
382
  }
399
383
 
400
384
  protected assertSame(other: _DecafPoint): void {
@@ -405,21 +389,16 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
405
389
  return new _DecafPoint(ep);
406
390
  }
407
391
 
408
- /** @deprecated use `import { decaf448_hasher } from '@noble/curves/ed448.js';` */
409
- static hashToCurve(hex: Hex): _DecafPoint {
410
- return decaf448_map(ensureBytes('decafHash', hex, 112));
411
- }
412
-
413
392
  static fromBytes(bytes: Uint8Array): _DecafPoint {
414
393
  abytes(bytes, 56);
415
394
  const { d } = ed448_CURVE;
416
395
  const P = Fp.ORDER;
417
- const mod = (n: bigint) => Fp.create(n);
418
- const s = bytes448ToNumberLE(bytes);
396
+ const mod = (n: bigint) => Fp448.create(n);
397
+ const s = Fp448.fromBytes(bytes);
419
398
 
420
399
  // 1. Check that s_bytes is the canonical encoding of a field element, or else abort.
421
400
  // 2. Check that s is non-negative, or else abort
422
- if (!equalBytes(numberToBytesLE(s, 56), bytes) || isNegativeLE(s, P))
401
+ if (!equalBytes(Fn448.toBytes(s), bytes) || isNegativeLE(s, P))
423
402
  throw new Error('invalid decaf448 encoding 1');
424
403
 
425
404
  const s2 = mod(s * s); // 1
@@ -437,7 +416,7 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
437
416
  const t = mod(x * y); // 8
438
417
 
439
418
  if (!isValid) throw new Error('invalid decaf448 encoding 2');
440
- return new _DecafPoint(new ed448.Point(x, y, _1n, t));
419
+ return new _DecafPoint(new ed448_Point(x, y, _1n, t));
441
420
  }
442
421
 
443
422
  /**
@@ -445,13 +424,8 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
445
424
  * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode-2).
446
425
  * @param hex Decaf-encoded 56 bytes. Not every 56-byte string is valid decaf encoding
447
426
  */
448
- static fromHex(hex: Hex): _DecafPoint {
449
- return _DecafPoint.fromBytes(ensureBytes('decafHex', hex, 56));
450
- }
451
-
452
- /** @deprecated use `import { pippenger } from '@noble/curves/abstract/curve.js';` */
453
- static msm(points: _DecafPoint[], scalars: bigint[]): _DecafPoint {
454
- return pippenger(_DecafPoint, Fn, points, scalars);
427
+ static fromHex(hex: string): _DecafPoint {
428
+ return _DecafPoint.fromBytes(hexToBytes(hex));
455
429
  }
456
430
 
457
431
  /**
@@ -462,20 +436,15 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
462
436
  const { X, Z, T } = this.ep;
463
437
  const P = Fp.ORDER;
464
438
  const mod = (n: bigint) => Fp.create(n);
465
-
466
439
  const u1 = mod(mod(X + T) * mod(X - T)); // 1
467
440
  const x2 = mod(X * X);
468
441
  const { value: invsqrt } = invertSqrt(mod(u1 * ONE_MINUS_D * x2)); // 2
469
-
470
442
  let ratio = mod(invsqrt * u1 * SQRT_MINUS_D); // 3
471
443
  if (isNegativeLE(ratio, P)) ratio = mod(-ratio);
472
-
473
444
  const u2 = mod(INVSQRT_MINUS_D * ratio * Z - T); // 4
474
-
475
445
  let s = mod(ONE_MINUS_D * invsqrt * X * u2); // 5
476
446
  if (isNegativeLE(s, P)) s = mod(-s);
477
-
478
- return numberToBytesLE(s, 56);
447
+ return Fn448.toBytes(s);
479
448
  }
480
449
 
481
450
  /**
@@ -486,9 +455,8 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
486
455
  this.assertSame(other);
487
456
  const { X: X1, Y: Y1 } = this.ep;
488
457
  const { X: X2, Y: Y2 } = other.ep;
489
- const mod = (n: bigint) => Fp.create(n);
490
458
  // (x1 * y2 == y1 * x2)
491
- return mod(X1 * Y2) === mod(Y1 * X2);
459
+ return Fp.create(X1 * Y2) === Fp.create(Y1 * X2);
492
460
  }
493
461
 
494
462
  is0(): boolean {
@@ -501,23 +469,29 @@ export const decaf448: {
501
469
  } = { Point: _DecafPoint };
502
470
 
503
471
  /** Hashing to decaf448 points / field. RFC 9380 methods. */
504
- export const decaf448_hasher: H2CHasherBase<bigint> = {
472
+ export const decaf448_hasher: H2CHasherBase<_DecafPoint> = {
505
473
  hashToCurve(msg: Uint8Array, options?: htfBasicOpts): _DecafPoint {
506
474
  const DST = options?.DST || 'decaf448_XOF:SHAKE256_D448MAP_RO_';
507
475
  return decaf448_map(expand_message_xof(msg, DST, 112, 224, shake256));
508
476
  },
477
+ // Warning: has big modulo bias of 2^-64.
478
+ // RFC is invalid. RFC says "use 64-byte xof", while for 2^-112 bias
479
+ // it must use 84-byte xof (56+56/2), not 64.
509
480
  hashToScalar(msg: Uint8Array, options: htfBasicOpts = { DST: _DST_scalar }) {
510
- return Fn.create(bytesToNumberLE(expand_message_xof(msg, options.DST, 64, 256, shake256)));
481
+ // Can't use `Fn448.fromBytes()`. 64-byte input => 56-byte field element
482
+ const xof = expand_message_xof(msg, options.DST, 64, 256, shake256);
483
+ return Fn448.create(bytesToNumberLE(xof));
511
484
  },
512
485
  };
513
486
 
514
- // export const decaf448_oprf: OPRF = createORPF({
515
- // name: 'decaf448-SHAKE256',
516
- // Point: DecafPoint,
517
- // hash: (msg: Uint8Array) => shake256(msg, { dkLen: 64 }),
518
- // hashToGroup: decaf448_hasher.hashToCurve,
519
- // hashToScalar: decaf448_hasher.hashToScalar,
520
- // });
487
+ export const decaf448_oprf: OPRF = /* @__PURE__ */ (() =>
488
+ createORPF({
489
+ name: 'decaf448-SHAKE256',
490
+ Point: _DecafPoint,
491
+ hash: (msg: Uint8Array) => shake256(msg, { dkLen: 64 }),
492
+ hashToGroup: decaf448_hasher.hashToCurve,
493
+ hashToScalar: decaf448_hasher.hashToScalar,
494
+ }))();
521
495
 
522
496
  /**
523
497
  * Weird / bogus points, useful for debugging.
@@ -531,27 +505,3 @@ export const ED448_TORSION_SUBGROUP: string[] = [
531
505
  '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
532
506
  '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080',
533
507
  ];
534
-
535
- type DcfHasher = (msg: Uint8Array, options: htfBasicOpts) => _DecafPoint;
536
-
537
- /** @deprecated use `decaf448.Point` */
538
- export const DecafPoint: typeof _DecafPoint = _DecafPoint;
539
- /** @deprecated use `import { ed448_hasher } from '@noble/curves/ed448.js';` */
540
- export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => ed448_hasher.hashToCurve)();
541
- /** @deprecated use `import { ed448_hasher } from '@noble/curves/ed448.js';` */
542
- export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
543
- ed448_hasher.encodeToCurve)();
544
- /** @deprecated use `import { decaf448_hasher } from '@noble/curves/ed448.js';` */
545
- export const hashToDecaf448: DcfHasher = /* @__PURE__ */ (() =>
546
- decaf448_hasher.hashToCurve as DcfHasher)();
547
- /** @deprecated use `import { decaf448_hasher } from '@noble/curves/ed448.js';` */
548
- export const hash_to_decaf448: DcfHasher = /* @__PURE__ */ (() =>
549
- decaf448_hasher.hashToCurve as DcfHasher)();
550
-
551
- /** @deprecated use `ed448.utils.toMontgomery` */
552
- export function edwardsToMontgomeryPub(edwardsPub: string | Uint8Array): Uint8Array {
553
- return ed448.utils.toMontgomery(ensureBytes('pub', edwardsPub));
554
- }
555
-
556
- /** @deprecated use `ed448.utils.toMontgomery` */
557
- export const edwardsToMontgomery: typeof edwardsToMontgomeryPub = edwardsToMontgomeryPub;
package/src/index.ts CHANGED
@@ -9,7 +9,7 @@ import { ed448, ed448ph, ed448ctx, x448 } from '@noble/curves/ed448.js';
9
9
  import { p256, p384, p521 } from '@noble/curves/nist.js';
10
10
  import { bls12_381 } from '@noble/curves/bls12-381.js';
11
11
  import { bn254 } from '@noble/curves/bn254.js';
12
- import { bytesToHex, hexToBytes, concatBytes, utf8ToBytes } from '@noble/curves/abstract/utils.js';
12
+ import { bytesToHex, hexToBytes, concatBytes, utf8ToBytes } from '@noble/curves/utils.js';
13
13
  ```
14
14
  */
15
15
  throw new Error('root module cannot be imported: import submodules instead. Check out README');
package/src/misc.ts CHANGED
@@ -6,18 +6,19 @@
6
6
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
7
7
  import { blake256 } from '@noble/hashes/blake1.js';
8
8
  import { blake2s } from '@noble/hashes/blake2.js';
9
- import { sha256, sha512 } from '@noble/hashes/sha2.js';
10
- import { concatBytes, utf8ToBytes } from '@noble/hashes/utils.js';
9
+ import { sha256, sha384, sha512 } from '@noble/hashes/sha2.js';
10
+ import { concatBytes } from '@noble/hashes/utils.js';
11
11
  import {
12
- twistedEdwards,
13
- type CurveFn,
12
+ eddsa,
13
+ edwards,
14
+ type EdDSA,
14
15
  type EdwardsOpts,
15
16
  type EdwardsPoint,
16
17
  } from './abstract/edwards.ts';
17
- import { Field, mod } from './abstract/modular.ts';
18
- import { weierstrass, type CurveFn as WCurveFn } from './abstract/weierstrass.ts';
18
+ import { ecdsa, weierstrass, type ECDSA, type WeierstrassOpts } from './abstract/weierstrass.ts';
19
19
  import { bls12_381_Fr } from './bls12-381.ts';
20
20
  import { bn254_Fr } from './bn254.ts';
21
+ import { asciiToBytes } from './utils.ts';
21
22
 
22
23
  // Jubjub curves have 𝔽p over scalar fields of other curves. They are friendly to ZK proofs.
23
24
  // jubjub Fp = bls n. babyjubjub Fp = bn254 n.
@@ -32,11 +33,7 @@ const jubjub_CURVE: EdwardsOpts = {
32
33
  Gy: BigInt('0x1d523cf1ddab1a1793132e78c866c0c33e26ba5cc220fed7cc3f870e59d292aa'),
33
34
  };
34
35
  /** Curve over scalar field of bls12-381. jubjub Fp = bls n */
35
- export const jubjub: CurveFn = /* @__PURE__ */ twistedEdwards({
36
- ...jubjub_CURVE,
37
- Fp: bls12_381_Fr,
38
- hash: sha512,
39
- });
36
+ export const jubjub: EdDSA = /* @__PURE__ */ eddsa(edwards(jubjub_CURVE), sha512);
40
37
 
41
38
  const babyjubjub_CURVE: EdwardsOpts = {
42
39
  p: bn254_Fr.ORDER,
@@ -48,13 +45,9 @@ const babyjubjub_CURVE: EdwardsOpts = {
48
45
  Gy: BigInt('0xc19139cb84c680a6e14116da06056174a0cfa121e6e5c2450f87d64fc000001'),
49
46
  };
50
47
  /** Curve over scalar field of bn254. babyjubjub Fp = bn254 n */
51
- export const babyjubjub: CurveFn = /* @__PURE__ */ twistedEdwards({
52
- ...babyjubjub_CURVE,
53
- Fp: bn254_Fr,
54
- hash: blake256,
55
- });
48
+ export const babyjubjub: EdDSA = /* @__PURE__ */ eddsa(edwards(babyjubjub_CURVE), blake256);
56
49
 
57
- const jubjub_gh_first_block = utf8ToBytes(
50
+ const jubjub_gh_first_block = asciiToBytes(
58
51
  '096b36a5804bfacef1691e173c366a47ff5ba84a44f26ddd7e8d9f79d5b42df0'
59
52
  );
60
53
 
@@ -87,38 +80,62 @@ export function jubjub_findGroupHash(m: Uint8Array, personalization: Uint8Array)
87
80
  return hashes[0];
88
81
  }
89
82
 
90
- // Pasta curves. See [Spec](https://o1-labs.github.io/proof-systems/specs/pasta.html).
91
-
92
- export const pasta_p: bigint = BigInt(
93
- '0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001'
94
- );
95
- export const pasta_q: bigint = BigInt(
96
- '0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001'
97
- );
83
+ const brainpoolP256r1_CURVE: WeierstrassOpts<bigint> = {
84
+ p: BigInt('0xa9fb57dba1eea9bc3e660a909d838d726e3bf623d52620282013481d1f6e5377'),
85
+ a: BigInt('0x7d5a0975fc2c3057eef67530417affe7fb8055c126dc5c6ce94a4b44f330b5d9'),
86
+ b: BigInt('0x26dc5c6ce94a4b44f330b5d9bbd77cbf958416295cf7e1ce6bccdc18ff8c07b6'),
87
+ n: BigInt('0xa9fb57dba1eea9bc3e660a909d838d718c397aa3b561a6f7901e0e82974856a7'),
88
+ Gx: BigInt('0x8bd2aeb9cb7e57cb2c4b482ffc81b7afb9de27e1e3bd23c23a4453bd9ace3262'),
89
+ Gy: BigInt('0x547ef835c3dac4fd97f8461a14611dc9c27745132ded8e545c1d54c72f046997'),
90
+ h: BigInt(1),
91
+ };
92
+ /** Brainpool P256r1 with sha256, from RFC 5639. */
93
+ export const brainpoolP256r1: ECDSA = ecdsa(weierstrass(brainpoolP256r1_CURVE), sha256);
98
94
 
99
- /**
100
- * @deprecated
101
- */
102
- export const pallas: WCurveFn = weierstrass({
103
- a: BigInt(0),
104
- b: BigInt(5),
105
- Fp: Field(pasta_p),
106
- n: pasta_q,
107
- Gx: mod(BigInt(-1), pasta_p),
108
- Gy: BigInt(2),
95
+ const brainpoolP384r1_CURVE: WeierstrassOpts<bigint> = {
96
+ p: BigInt(
97
+ '0x8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b412b1da197fb71123acd3a729901d1a71874700133107ec53'
98
+ ),
99
+ a: BigInt(
100
+ '0x7bc382c63d8c150c3c72080ace05afa0c2bea28e4fb22787139165efba91f90f8aa5814a503ad4eb04a8c7dd22ce2826'
101
+ ),
102
+ b: BigInt(
103
+ '0x04a8c7dd22ce28268b39b55416f0447c2fb77de107dcd2a62e880ea53eeb62d57cb4390295dbc9943ab78696fa504c11'
104
+ ),
105
+ n: BigInt(
106
+ '0x8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b31f166e6cac0425a7cf3ab6af6b7fc3103b883202e9046565'
107
+ ),
108
+ Gx: BigInt(
109
+ '0x1d1c64f068cf45ffa2a63a81b7c13f6b8847a3e77ef14fe3db7fcafe0cbd10e8e826e03436d646aaef87b2e247d4af1e'
110
+ ),
111
+ Gy: BigInt(
112
+ '0x8abe1d7520f9c2a45cb1eb8e95cfd55262b70b29feec5864e19c054ff99129280e4646217791811142820341263c5315'
113
+ ),
109
114
  h: BigInt(1),
110
- hash: sha256,
111
- });
112
- /**
113
- * @deprecated
114
- */
115
- export const vesta: WCurveFn = weierstrass({
116
- a: BigInt(0),
117
- b: BigInt(5),
118
- Fp: Field(pasta_q),
119
- n: pasta_p,
120
- Gx: mod(BigInt(-1), pasta_q),
121
- Gy: BigInt(2),
115
+ };
116
+ /** Brainpool P384r1 with sha384, from RFC 5639. */
117
+ export const brainpoolP384r1: ECDSA = ecdsa(weierstrass(brainpoolP384r1_CURVE), sha384);
118
+
119
+ const brainpoolP512r1_CURVE: WeierstrassOpts<bigint> = {
120
+ p: BigInt(
121
+ '0xaadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca703308717d4d9b009bc66842aecda12ae6a380e62881ff2f2d82c68528aa6056583a48f3'
122
+ ),
123
+ a: BigInt(
124
+ '0x7830a3318b603b89e2327145ac234cc594cbdd8d3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94ca'
125
+ ),
126
+ b: BigInt(
127
+ '0x3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94cadc083e67984050b75ebae5dd2809bd638016f723'
128
+ ),
129
+ n: BigInt(
130
+ '0xaadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca70330870553e5c414ca92619418661197fac10471db1d381085ddaddb58796829ca90069'
131
+ ),
132
+ Gx: BigInt(
133
+ '0x81aee4bdd82ed9645a21322e9c4c6a9385ed9f70b5d916c1b43b62eef4d0098eff3b1f78e2d0d48d50d1687b93b97d5f7c6d5047406a5e688b352209bcb9f822'
134
+ ),
135
+ Gy: BigInt(
136
+ '0x7dde385d566332ecc0eabfa9cf7822fdf209f70024a57b1aa000c55b881f8111b2dcde494a5f485e5bca4bd88a2763aed1ca2b2fa8f0540678cd1e0f3ad80892'
137
+ ),
122
138
  h: BigInt(1),
123
- hash: sha256,
124
- });
139
+ };
140
+ /** Brainpool P521r1 with sha512, from RFC 5639. */
141
+ export const brainpoolP512r1: ECDSA = ecdsa(weierstrass(brainpoolP512r1_CURVE), sha512);