p521.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
  2. import { sha512 } from '@noble/hashes/sha512';
  3. import { createCurve } from './_shortw_utils.js';
  4. import { createHasher } from './abstract/hash-to-curve.js';
  5. import { Field } from './abstract/modular.js';
  6. import { mapToCurveSimpleSWU } from './abstract/weierstrass.js';
  7. // NIST secp521r1 aka p521
  8. // Note that it's 521, which differs from 512 of its hash function.
  9. // https://www.secg.org/sec2-v2.pdf, https://neuromancer.sk/std/nist/P-521
  10. // Field over which we'll do calculations.
  11. // prettier-ignore
  12. const P = BigInt('0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff');
  13. const Fp = Field(P);
  14. const CURVE = {
  15. a: Fp.create(BigInt('-3')),
  16. b: BigInt(
  17. '0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00'
  18. ),
  19. Fp,
  20. n: BigInt(
  21. '0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409'
  22. ),
  23. Gx: BigInt(
  24. '0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66'
  25. ),
  26. Gy: BigInt(
  27. '0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'
  28. ),
  29. h: BigInt(1),
  30. };
  31. // prettier-ignore
  32. export const p521 = createCurve({
  33. a: CURVE.a, // Equation params: a, b
  34. b: CURVE.b,
  35. Fp, // Field: 2n**521n - 1n
  36. // Curve order, total count of valid points in the field
  37. n: CURVE.n,
  38. Gx: CURVE.Gx, // Base point (x, y) aka generator point
  39. Gy: CURVE.Gy,
  40. h: CURVE.h,
  41. lowS: false,
  42. allowedPrivateKeyLengths: [130, 131, 132] // P521 keys are variable-length. Normalize to 132b
  43. } as const, sha512);
  44. export const secp521r1 = p521;
  45. const mapSWU = /* @__PURE__ */ (() =>
  46. mapToCurveSimpleSWU(Fp, {
  47. A: CURVE.a,
  48. B: CURVE.b,
  49. Z: Fp.create(BigInt('-4')),
  50. }))();
  51. const htf = /* @__PURE__ */ (() =>
  52. createHasher(secp521r1.ProjectivePoint, (scalars: bigint[]) => mapSWU(scalars[0]), {
  53. DST: 'P521_XMD:SHA-512_SSWU_RO_',
  54. encodeDST: 'P521_XMD:SHA-512_SSWU_NU_',
  55. p: Fp.ORDER,
  56. m: 1,
  57. k: 256,
  58. expand: 'xmd',
  59. hash: sha512,
  60. }))();
  61. export const hashToCurve = /* @__PURE__ */ (() => htf.hashToCurve)();
  62. export const encodeToCurve = /* @__PURE__ */ (() => htf.encodeToCurve)();