@noble/curves 1.8.2 → 1.9.0

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 (177) hide show
  1. package/README.md +27 -15
  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/edwards.d.ts.map +1 -1
  7. package/abstract/edwards.js +3 -2
  8. package/abstract/edwards.js.map +1 -1
  9. package/abstract/hash-to-curve.d.ts +10 -5
  10. package/abstract/hash-to-curve.d.ts.map +1 -1
  11. package/abstract/hash-to-curve.js +31 -23
  12. package/abstract/hash-to-curve.js.map +1 -1
  13. package/abstract/modular.d.ts +11 -8
  14. package/abstract/modular.d.ts.map +1 -1
  15. package/abstract/modular.js +70 -58
  16. package/abstract/modular.js.map +1 -1
  17. package/abstract/montgomery.d.ts.map +1 -1
  18. package/abstract/montgomery.js +2 -1
  19. package/abstract/montgomery.js.map +1 -1
  20. package/abstract/poseidon.d.ts +39 -2
  21. package/abstract/poseidon.d.ts.map +1 -1
  22. package/abstract/poseidon.js +183 -4
  23. package/abstract/poseidon.js.map +1 -1
  24. package/abstract/tower.d.ts.map +1 -1
  25. package/abstract/tower.js +3 -4
  26. package/abstract/tower.js.map +1 -1
  27. package/abstract/utils.d.ts +1 -0
  28. package/abstract/utils.d.ts.map +1 -1
  29. package/abstract/utils.js +2 -0
  30. package/abstract/utils.js.map +1 -1
  31. package/abstract/weierstrass.d.ts +4 -5
  32. package/abstract/weierstrass.d.ts.map +1 -1
  33. package/abstract/weierstrass.js +6 -6
  34. package/abstract/weierstrass.js.map +1 -1
  35. package/bn254.d.ts +1 -0
  36. package/bn254.d.ts.map +1 -1
  37. package/bn254.js +10 -0
  38. package/bn254.js.map +1 -1
  39. package/ed25519.d.ts +2 -1
  40. package/ed25519.d.ts.map +1 -1
  41. package/ed25519.js +6 -6
  42. package/ed25519.js.map +1 -1
  43. package/ed448.d.ts +2 -1
  44. package/ed448.d.ts.map +1 -1
  45. package/ed448.js +5 -5
  46. package/ed448.js.map +1 -1
  47. package/esm/abstract/bls.js +1 -1
  48. package/esm/abstract/bls.js.map +1 -1
  49. package/esm/abstract/curve.d.ts +1 -1
  50. package/esm/abstract/curve.d.ts.map +1 -1
  51. package/esm/abstract/edwards.d.ts.map +1 -1
  52. package/esm/abstract/edwards.js +5 -4
  53. package/esm/abstract/edwards.js.map +1 -1
  54. package/esm/abstract/hash-to-curve.d.ts +10 -5
  55. package/esm/abstract/hash-to-curve.d.ts.map +1 -1
  56. package/esm/abstract/hash-to-curve.js +32 -24
  57. package/esm/abstract/hash-to-curve.js.map +1 -1
  58. package/esm/abstract/modular.d.ts +11 -8
  59. package/esm/abstract/modular.d.ts.map +1 -1
  60. package/esm/abstract/modular.js +70 -58
  61. package/esm/abstract/modular.js.map +1 -1
  62. package/esm/abstract/montgomery.d.ts.map +1 -1
  63. package/esm/abstract/montgomery.js +3 -2
  64. package/esm/abstract/montgomery.js.map +1 -1
  65. package/esm/abstract/poseidon.d.ts +39 -2
  66. package/esm/abstract/poseidon.d.ts.map +1 -1
  67. package/esm/abstract/poseidon.js +180 -5
  68. package/esm/abstract/poseidon.js.map +1 -1
  69. package/esm/abstract/tower.d.ts.map +1 -1
  70. package/esm/abstract/tower.js +3 -4
  71. package/esm/abstract/tower.js.map +1 -1
  72. package/esm/abstract/utils.d.ts +1 -0
  73. package/esm/abstract/utils.d.ts.map +1 -1
  74. package/esm/abstract/utils.js +2 -0
  75. package/esm/abstract/utils.js.map +1 -1
  76. package/esm/abstract/weierstrass.d.ts +4 -5
  77. package/esm/abstract/weierstrass.d.ts.map +1 -1
  78. package/esm/abstract/weierstrass.js +8 -8
  79. package/esm/abstract/weierstrass.js.map +1 -1
  80. package/esm/bn254.d.ts +1 -0
  81. package/esm/bn254.d.ts.map +1 -1
  82. package/esm/bn254.js +10 -0
  83. package/esm/bn254.js.map +1 -1
  84. package/esm/ed25519.d.ts +2 -1
  85. package/esm/ed25519.d.ts.map +1 -1
  86. package/esm/ed25519.js +6 -6
  87. package/esm/ed25519.js.map +1 -1
  88. package/esm/ed448.d.ts +2 -1
  89. package/esm/ed448.d.ts.map +1 -1
  90. package/esm/ed448.js +5 -5
  91. package/esm/ed448.js.map +1 -1
  92. package/esm/jubjub.d.ts +7 -1
  93. package/esm/jubjub.d.ts.map +1 -1
  94. package/esm/jubjub.js +7 -1
  95. package/esm/jubjub.js.map +1 -1
  96. package/esm/misc.d.ts +8 -2
  97. package/esm/misc.d.ts.map +1 -1
  98. package/esm/misc.js +10 -4
  99. package/esm/misc.js.map +1 -1
  100. package/esm/nist.d.ts +29 -0
  101. package/esm/nist.d.ts.map +1 -0
  102. package/esm/nist.js +120 -0
  103. package/esm/nist.js.map +1 -0
  104. package/esm/p256.d.ts +7 -9
  105. package/esm/p256.d.ts.map +1 -1
  106. package/esm/p256.js +6 -44
  107. package/esm/p256.js.map +1 -1
  108. package/esm/p384.d.ts +9 -10
  109. package/esm/p384.d.ts.map +1 -1
  110. package/esm/p384.js +7 -46
  111. package/esm/p384.js.map +1 -1
  112. package/esm/p521.d.ts +7 -8
  113. package/esm/p521.d.ts.map +1 -1
  114. package/esm/p521.js +6 -46
  115. package/esm/p521.js.map +1 -1
  116. package/esm/pasta.d.ts +5 -1
  117. package/esm/pasta.d.ts.map +1 -1
  118. package/esm/pasta.js +5 -1
  119. package/esm/pasta.js.map +1 -1
  120. package/esm/secp256k1.d.ts +3 -3
  121. package/esm/secp256k1.d.ts.map +1 -1
  122. package/esm/secp256k1.js +5 -6
  123. package/esm/secp256k1.js.map +1 -1
  124. package/jubjub.d.ts +7 -1
  125. package/jubjub.d.ts.map +1 -1
  126. package/jubjub.js +8 -5
  127. package/jubjub.js.map +1 -1
  128. package/misc.d.ts +8 -2
  129. package/misc.d.ts.map +1 -1
  130. package/misc.js +11 -5
  131. package/misc.js.map +1 -1
  132. package/nist.d.ts +29 -0
  133. package/nist.d.ts.map +1 -0
  134. package/nist.js +123 -0
  135. package/nist.js.map +1 -0
  136. package/p256.d.ts +7 -9
  137. package/p256.d.ts.map +1 -1
  138. package/p256.js +5 -49
  139. package/p256.js.map +1 -1
  140. package/p384.d.ts +9 -10
  141. package/p384.d.ts.map +1 -1
  142. package/p384.js +6 -51
  143. package/p384.js.map +1 -1
  144. package/p521.d.ts +7 -8
  145. package/p521.d.ts.map +1 -1
  146. package/p521.js +5 -51
  147. package/p521.js.map +1 -1
  148. package/package.json +106 -6
  149. package/pasta.d.ts +5 -1
  150. package/pasta.d.ts.map +1 -1
  151. package/pasta.js +5 -3
  152. package/pasta.js.map +1 -1
  153. package/secp256k1.d.ts +3 -3
  154. package/secp256k1.d.ts.map +1 -1
  155. package/secp256k1.js +6 -7
  156. package/secp256k1.js.map +1 -1
  157. package/src/abstract/bls.ts +1 -1
  158. package/src/abstract/curve.ts +1 -1
  159. package/src/abstract/edwards.ts +11 -11
  160. package/src/abstract/hash-to-curve.ts +44 -36
  161. package/src/abstract/modular.ts +67 -58
  162. package/src/abstract/montgomery.ts +3 -2
  163. package/src/abstract/poseidon.ts +208 -13
  164. package/src/abstract/tower.ts +3 -4
  165. package/src/abstract/utils.ts +2 -0
  166. package/src/abstract/weierstrass.ts +19 -15
  167. package/src/bn254.ts +10 -0
  168. package/src/ed25519.ts +8 -7
  169. package/src/ed448.ts +7 -5
  170. package/src/jubjub.ts +8 -5
  171. package/src/misc.ts +10 -4
  172. package/src/nist.ts +154 -0
  173. package/src/p256.ts +6 -50
  174. package/src/p384.ts +8 -56
  175. package/src/p521.ts +6 -65
  176. package/src/pasta.ts +5 -1
  177. package/src/secp256k1.ts +9 -8
@@ -32,6 +32,7 @@ export function abool(title: string, value: boolean): void {
32
32
  if (typeof value !== 'boolean') throw new Error(title + ' boolean expected, got ' + value);
33
33
  }
34
34
 
35
+ // Used in weierstrass, der
35
36
  export function numberToHexUnpadded(num: number | bigint): string {
36
37
  const hex = num.toString(16);
37
38
  return hex.length & 1 ? '0' + hex : hex;
@@ -217,6 +218,7 @@ export function aInRange(title: string, n: bigint, min: bigint, max: bigint): vo
217
218
  /**
218
219
  * Calculates amount of bits in a bigint.
219
220
  * Same as `n.toString(2).length`
221
+ * TODO: merge with nLength in modular
220
222
  */
221
223
  export function bitLen(n: bigint): number {
222
224
  let len;
@@ -41,11 +41,13 @@
41
41
  // prettier-ignore
42
42
  import {
43
43
  type AffinePoint, type BasicCurve, type Group, type GroupConstructor,
44
- pippenger, validateBasic, wNAF,
44
+ pippenger, validateBasic, wNAF
45
45
  } from './curve.ts';
46
46
  // prettier-ignore
47
47
  import {
48
- Field, type IField, getMinHashLength, invert, mapHashToField, mod, validateField,
48
+ Field,
49
+ FpInvertBatch,
50
+ type IField, getMinHashLength, invert, mapHashToField, mod, validateField
49
51
  } from './modular.ts';
50
52
  // prettier-ignore
51
53
  import {
@@ -94,17 +96,16 @@ export interface ProjPointType<T> extends Group<ProjPointType<T>> {
94
96
  readonly pz: T;
95
97
  get x(): T;
96
98
  get y(): T;
97
- multiply(scalar: bigint): ProjPointType<T>;
98
99
  toAffine(iz?: T): AffinePoint<T>;
99
- isTorsionFree(): boolean;
100
- clearCofactor(): ProjPointType<T>;
101
- assertValidity(): void;
102
- hasEvenY(): boolean;
103
- toRawBytes(isCompressed?: boolean): Uint8Array;
104
100
  toHex(isCompressed?: boolean): string;
101
+ toRawBytes(isCompressed?: boolean): Uint8Array;
105
102
 
103
+ assertValidity(): void;
104
+ hasEvenY(): boolean;
106
105
  multiplyUnsafe(scalar: bigint): ProjPointType<T>;
107
106
  multiplyAndAddUnsafe(Q: ProjPointType<T>, a: bigint, b: bigint): ProjPointType<T> | undefined;
107
+ isTorsionFree(): boolean;
108
+ clearCofactor(): ProjPointType<T>;
108
109
  _setWindowSize(windowSize: number): void;
109
110
  }
110
111
  // Static methods for 3d XYZ points
@@ -413,14 +414,14 @@ export function weierstrassPoints<T>(opts: CurvePointsType<T>): CurvePointsRes<T
413
414
  */
414
415
  class Point implements ProjPointType<T> {
415
416
  static readonly BASE = new Point(CURVE.Gx, CURVE.Gy, Fp.ONE);
416
- static readonly ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ZERO);
417
+ static readonly ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ZERO); // 0, 1, 0
417
418
  readonly px: T;
418
419
  readonly py: T;
419
420
  readonly pz: T;
420
421
 
421
422
  constructor(px: T, py: T, pz: T) {
422
423
  if (px == null || !Fp.isValid(px)) throw new Error('x required');
423
- if (py == null || !Fp.isValid(py)) throw new Error('y required');
424
+ if (py == null || !Fp.isValid(py) || Fp.is0(py)) throw new Error('y required');
424
425
  if (pz == null || !Fp.isValid(pz)) throw new Error('z required');
425
426
  this.px = px;
426
427
  this.py = py;
@@ -454,7 +455,10 @@ export function weierstrassPoints<T>(opts: CurvePointsType<T>): CurvePointsRes<T
454
455
  * Optimization: converts a list of projective points to a list of identical points with Z=1.
455
456
  */
456
457
  static normalizeZ(points: Point[]): Point[] {
457
- const toInv = Fp.invertBatch(points.map((p) => p.pz));
458
+ const toInv = FpInvertBatch(
459
+ Fp,
460
+ points.map((p) => p.pz)
461
+ );
458
462
  return points.map((p, i) => p.toAffine(toInv[i])).map(Point.fromAffine);
459
463
  }
460
464
 
@@ -617,6 +621,7 @@ export function weierstrassPoints<T>(opts: CurvePointsType<T>): CurvePointsRes<T
617
621
  is0() {
618
622
  return this.equals(Point.ZERO);
619
623
  }
624
+
620
625
  private wNAF(n: bigint): { p: Point; f: Point } {
621
626
  return wnaf.wNAFCached(this, n, Point.normalizeZ);
622
627
  }
@@ -734,7 +739,6 @@ export function weierstrassPoints<T>(opts: CurvePointsType<T>): CurvePointsRes<T
734
739
  }
735
740
  const _bits = CURVE.nBitLength;
736
741
  const wnaf = wNAF(Point, CURVE.endo ? Math.ceil(_bits / 2) : _bits);
737
- // Validate if generator point is on curve
738
742
  return {
739
743
  CURVE,
740
744
  ProjectivePoint: Point as ProjConstructor<T>,
@@ -756,7 +760,6 @@ export interface SignatureType {
756
760
  recoverPublicKey(msgHash: Hex): ProjPointType<bigint>;
757
761
  toCompactRawBytes(): Uint8Array;
758
762
  toCompactHex(): string;
759
- // DER-encoded
760
763
  toDERRawBytes(isCompressed?: boolean): Uint8Array;
761
764
  toDERHex(isCompressed?: boolean): string;
762
765
  }
@@ -975,7 +978,7 @@ export function weierstrass(curveDef: CurveType): CurveFn {
975
978
  return hexToBytes(this.toDERHex());
976
979
  }
977
980
  toDERHex() {
978
- return DER.hexFromSig({ r: this.r, s: this.s });
981
+ return DER.hexFromSig(this);
979
982
  }
980
983
 
981
984
  // padded bytes of r, then padded bytes of s
@@ -1383,7 +1386,8 @@ export function mapToCurveSimpleSWU<T>(
1383
1386
  y = Fp.cmov(y, value, isValid); // 22. y = CMOV(y, y1, is_gx1_square)
1384
1387
  const e1 = Fp.isOdd!(u) === Fp.isOdd!(y); // 23. e1 = sgn0(u) == sgn0(y)
1385
1388
  y = Fp.cmov(Fp.neg(y), y, e1); // 24. y = CMOV(-y, y, e1)
1386
- x = Fp.div(x, tv4); // 25. x = x / tv4
1389
+ const tv4_inv = FpInvertBatch(Fp, [tv4], true)[0];
1390
+ x = Fp.mul(x, tv4_inv); // 25. x = x / tv4
1387
1391
  return { x, y };
1388
1392
  };
1389
1393
  }
package/src/bn254.ts CHANGED
@@ -13,6 +13,15 @@ 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.
17
+ To work around this limitation, has to initialize points on their own from BigInts.
18
+ Reason it's not implemented is because [there is no standard](https://github.com/privacy-scaling-explorations/halo2curves/issues/109).
19
+ Points of divergence:
20
+
21
+ - Endianness: LE vs BE (byte-swapped)
22
+ - Flags as first hex bits (similar to BLS) vs no-flags
23
+ - Imaginary part last in G2 vs first (c0, c1 vs c1, c0)
24
+
16
25
  The goal of our implementation is to support "Ethereum" variant of the curve,
17
26
  because it at least has specs:
18
27
 
@@ -239,6 +248,7 @@ export const bn254: BLSCurveFn = bls({
239
248
  * bn254 weierstrass curve with ECDSA.
240
249
  * This is very rare and probably not used anywhere.
241
250
  * Instead, you should use G1 / G2, defined above.
251
+ * @deprecated
242
252
  */
243
253
  export const bn254_weierstrass: CurveFn = weierstrass({
244
254
  a: BigInt(0),
package/src/ed25519.ts CHANGED
@@ -13,10 +13,11 @@ import { type CurveFn, type ExtPointType, twistedEdwards } from './abstract/edwa
13
13
  import {
14
14
  createHasher,
15
15
  expand_message_xmd,
16
+ type Hasher,
16
17
  type htfBasicOpts,
17
18
  type HTFMethod,
18
19
  } from './abstract/hash-to-curve.ts';
19
- import { Field, FpSqrtEven, isNegativeLE, mod, pow2 } from './abstract/modular.ts';
20
+ import { Field, FpInvertBatch, FpSqrtEven, isNegativeLE, mod, pow2 } from './abstract/modular.ts';
20
21
  import { montgomery, type CurveFn as XCurveFn } from './abstract/montgomery.ts';
21
22
  import {
22
23
  bytesToHex,
@@ -289,12 +290,11 @@ function map_to_curve_elligator2_edwards25519(u: bigint) {
289
290
  xd = Fp.cmov(xd, Fp.ONE, e); // 10. xd = CMOV(xd, 1, e)
290
291
  yn = Fp.cmov(yn, Fp.ONE, e); // 11. yn = CMOV(yn, 1, e)
291
292
  yd = Fp.cmov(yd, Fp.ONE, e); // 12. yd = CMOV(yd, 1, e)
292
-
293
- const inv = Fp.invertBatch([xd, yd]); // batch division
294
- return { x: Fp.mul(xn, inv[0]), y: Fp.mul(yn, inv[1]) }; // 13. return (xn, xd, yn, yd)
293
+ const [xd_inv, yd_inv] = FpInvertBatch(Fp, [xd, yd], true); // batch division
294
+ return { x: Fp.mul(xn, xd_inv), y: Fp.mul(yn, yd_inv) }; // 13. return (xn, xd, yn, yd)
295
295
  }
296
296
 
297
- const htf = /* @__PURE__ */ (() =>
297
+ export const ed25519_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
298
298
  createHasher(
299
299
  ed25519.ExtendedPoint,
300
300
  (scalars: bigint[]) => map_to_curve_elligator2_edwards25519(scalars[0]),
@@ -308,8 +308,9 @@ const htf = /* @__PURE__ */ (() =>
308
308
  hash: sha512,
309
309
  }
310
310
  ))();
311
- export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
312
- export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();
311
+ export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => ed25519_hasher.hashToCurve)();
312
+ export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() =>
313
+ ed25519_hasher.encodeToCurve)();
313
314
 
314
315
  function aristp(other: unknown) {
315
316
  if (!(other instanceof RistPoint)) throw new Error('RistrettoPoint expected');
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,
@@ -263,11 +264,11 @@ function map_to_curve_elligator2_edwards448(u: bigint) {
263
264
  yEn = Fp.cmov(yEn, Fp.ONE, e); // 36. yEn = CMOV(yEn, 1, e)
264
265
  yEd = Fp.cmov(yEd, Fp.ONE, e); // 37. yEd = CMOV(yEd, 1, e)
265
266
 
266
- const inv = Fp.invertBatch([xEd, yEd]); // batch division
267
+ const inv = FpInvertBatch(Fp, [xEd, yEd], true); // batch division
267
268
  return { x: Fp.mul(xEn, inv[0]), y: Fp.mul(yEn, inv[1]) }; // 38. return (xEn, xEd, yEn, yEd)
268
269
  }
269
270
 
270
- const htf = /* @__PURE__ */ (() =>
271
+ export const ed448_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
271
272
  createHasher(
272
273
  ed448.ExtendedPoint,
273
274
  (scalars: bigint[]) => map_to_curve_elligator2_edwards448(scalars[0]),
@@ -281,8 +282,9 @@ const htf = /* @__PURE__ */ (() =>
281
282
  hash: shake256,
282
283
  }
283
284
  ))();
284
- export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
285
- export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();
285
+ export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => ed448_hasher.hashToCurve)();
286
+ export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() =>
287
+ ed448_hasher.encodeToCurve)();
286
288
 
287
289
  function adecafp(other: unknown) {
288
290
  if (!(other instanceof DcfPoint)) throw new Error('DecafPoint expected');
package/src/jubjub.ts CHANGED
@@ -1,5 +1,8 @@
1
- export {
2
- jubjub_findGroupHash as findGroupHash,
3
- jubjub_groupHash as groupHash,
4
- jubjub,
5
- } from './misc.ts';
1
+ import { jubjub_findGroupHash, jubjub_groupHash, jubjub as jubjubn } from './misc.ts';
2
+
3
+ /** @deprecated Use `@noble/curves/misc` module directly. */
4
+ export const jubjub: typeof jubjubn = jubjubn;
5
+ /** @deprecated Use `@noble/curves/misc` module directly. */
6
+ export const findGroupHash: typeof jubjub_findGroupHash = jubjub_findGroupHash;
7
+ /** @deprecated Use `@noble/curves/misc` module directly. */
8
+ 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,154 @@
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
+ export const secp521r1: CurveFnWithCreate = p521;
136
+
137
+ const p521_mapSWU = /* @__PURE__ */ (() =>
138
+ mapToCurveSimpleSWU(Fp521, {
139
+ A: p521_a,
140
+ B: p521_b,
141
+ Z: Fp521.create(BigInt('-4')),
142
+ }))();
143
+
144
+ /** Hashing / encoding to p521 points / field. RFC 9380 methods. */
145
+ export const p521_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
146
+ createHasher(secp521r1.ProjectivePoint, (scalars: bigint[]) => p521_mapSWU(scalars[0]), {
147
+ DST: 'P521_XMD:SHA-512_SSWU_RO_',
148
+ encodeDST: 'P521_XMD:SHA-512_SSWU_NU_',
149
+ p: Fp521.ORDER,
150
+ m: 1,
151
+ k: 256,
152
+ expand: 'xmd',
153
+ hash: sha512,
154
+ }))();
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. */
package/src/p521.ts CHANGED
@@ -1,70 +1,11 @@
1
1
  /**
2
2
  * NIST secp521r1 aka p521.
3
- * Note that it's 521, which differs from 512 of its hash function.
4
- * https://www.secg.org/sec2-v2.pdf, https://neuromancer.sk/std/nist/P-521
5
3
  * @module
6
4
  */
7
5
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
8
- import { sha512 } from '@noble/hashes/sha2';
9
- import { createCurve, type CurveFnWithCreate } from './_shortw_utils.ts';
10
- import { createHasher, type HTFMethod } from './abstract/hash-to-curve.ts';
11
- import { Field } from './abstract/modular.ts';
12
- import { mapToCurveSimpleSWU } from './abstract/weierstrass.ts';
13
-
14
- // Field over which we'll do calculations.
15
- const Fp521 = Field(
16
- BigInt(
17
- '0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
18
- )
19
- );
20
-
21
- const CURVE_A = Fp521.create(BigInt('-3'));
22
- const CURVE_B = BigInt(
23
- '0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00'
24
- );
25
-
26
- /**
27
- * NIST secp521r1 aka p521 curve, ECDSA and ECDH methods.
28
- * Field: `2n**521n - 1n`.
29
- */
30
- // prettier-ignore
31
- export const p521: CurveFnWithCreate = createCurve({
32
- a: CURVE_A,
33
- b: CURVE_B,
34
- Fp: Fp521,
35
- n: BigInt(
36
- '0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409'
37
- ),
38
- Gx: BigInt(
39
- '0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66'
40
- ),
41
- Gy: BigInt(
42
- '0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'
43
- ),
44
- h: BigInt(1),
45
- lowS: false,
46
- allowedPrivateKeyLengths: [130, 131, 132] // P521 keys are variable-length. Normalize to 132b
47
- } as const, sha512);
48
- export const secp521r1: CurveFnWithCreate = p521;
49
-
50
- const mapSWU = /* @__PURE__ */ (() =>
51
- mapToCurveSimpleSWU(Fp521, {
52
- A: CURVE_A,
53
- B: CURVE_B,
54
- Z: Fp521.create(BigInt('-4')),
55
- }))();
56
-
57
- const htf = /* @__PURE__ */ (() =>
58
- createHasher(secp521r1.ProjectivePoint, (scalars: bigint[]) => mapSWU(scalars[0]), {
59
- DST: 'P521_XMD:SHA-512_SSWU_RO_',
60
- encodeDST: 'P521_XMD:SHA-512_SSWU_NU_',
61
- p: Fp521.ORDER,
62
- m: 1,
63
- k: 256,
64
- expand: 'xmd',
65
- hash: sha512,
66
- }))();
67
- /** secp521r1 hash-to-curve from RFC 9380. */
68
- export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.hashToCurve)();
69
- /** secp521r1 encode-to-curve from RFC 9380. */
70
- export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => htf.encodeToCurve)();
6
+ import { type HTFMethod } from './abstract/hash-to-curve.ts';
7
+ import { p521_hasher, p521 as p521n } from './nist.ts';
8
+ export const p521: typeof p521n = p521n;
9
+ export const secp521r1: typeof p521n = p521n;
10
+ export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => p521_hasher.hashToCurve)();
11
+ export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => p521_hasher.encodeToCurve)();
package/src/pasta.ts CHANGED
@@ -1 +1,5 @@
1
- export { pallas, vesta } from './misc.ts';
1
+ import { pallas as pn, vesta as vn } from './misc.ts';
2
+ /** @deprecated */
3
+ export const pallas: typeof pn = pn;
4
+ /** @deprecated */
5
+ export const vesta: typeof vn = vn;