p256.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
  2. import { sha256 } from '@noble/hashes/sha256';
  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 secp256r1 aka p256
  8. // https://www.secg.org/sec2-v2.pdf, https://neuromancer.sk/std/nist/P-256
  9. const Fp = Field(BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'));
  10. const CURVE_A = Fp.create(BigInt('-3'));
  11. const CURVE_B = BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b');
  12. // prettier-ignore
  13. export const p256 = createCurve({
  14. a: CURVE_A, // Equation params: a, b
  15. b: CURVE_B,
  16. Fp, // Field: 2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n-1n
  17. // Curve order, total count of valid points in the field
  18. n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
  19. // Base (generator) point (x, y)
  20. Gx: BigInt('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'),
  21. Gy: BigInt('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'),
  22. h: BigInt(1),
  23. lowS: false,
  24. } as const, sha256);
  25. export const secp256r1 = p256;
  26. const mapSWU = /* @__PURE__ */ (() =>
  27. mapToCurveSimpleSWU(Fp, {
  28. A: CURVE_A,
  29. B: CURVE_B,
  30. Z: Fp.create(BigInt('-10')),
  31. }))();
  32. const htf = /* @__PURE__ */ (() =>
  33. createHasher(secp256r1.ProjectivePoint, (scalars: bigint[]) => mapSWU(scalars[0]), {
  34. DST: 'P256_XMD:SHA-256_SSWU_RO_',
  35. encodeDST: 'P256_XMD:SHA-256_SSWU_NU_',
  36. p: Fp.ORDER,
  37. m: 1,
  38. k: 128,
  39. expand: 'xmd',
  40. hash: sha256,
  41. }))();
  42. export const hashToCurve = /* @__PURE__ */ (() => htf.hashToCurve)();
  43. export const encodeToCurve = /* @__PURE__ */ (() => htf.encodeToCurve)();