Math

Overview

What the package computes. Short Weierstrass curves over a prime you pick and secp256k1 and P-256 and P-384 and P-521 and three Brainpool curves and sr25519 and ed25519 and Ed448 on their own

Two kinds of Weierstrass curve

Most of the package works on the short Weierstrass form, y² = x³ + ax + b, over the integers mod a prime p. That covers the toy curves of a CTF and secp256k1 alike. The difference is in what you hand over.

Curves you definesecp256k1P-256, P-384, P-521, brainpoolP256r1, brainpoolP384r1 and brainpoolP512r1
Import@agntn/curves@agntn/curves/secp256k1@agntn/curves/p256, @agntn/curves/p384, @agntn/curves/p521, @agntn/curves/brainpoolp256r1, @agntn/curves/brainpoolp384r1, @agntn/curves/brainpoolp512r1
CurvedefineCurve({ a, b, p })fixed, from SEC 2fixed, from SEC 2 and RFC 5639
Points{ x, y } as bigints, null for infinitySEC1 hex, infinity refusedSEC1 hex, infinity refused
Scalarsany bigint1 to n - 1, hex or bigint1 to n - 1, hex or bigint
Extralifting x, counting, listing, orders, logsscalars mod n, lifting xlifting x, ECDSA signing and verification
Agent toolcurves_computecurves_secp256k1_computecurves_p256_compute, curves_p384_compute, curves_p521_compute, curves_brainpoolp256r1_compute, curves_brainpoolp384r1_compute, curves_brainpoolp512r1_compute

Why two? A toy curve is about the group. Count the points, find an order, take a log. secp256k1 is about keys. Nobody counts its points, and a log on it is not happening.

P-256, or secp256r1, uses SEC1 points, lifts x and signs and verifies ECDSA. Its signatures use deterministic RFC 6979 nonces. One letter separates secp256k1 from secp256r1. Now both can sign.

P-384 gives each coordinate 48 bytes, so its points take up more room than P-256's. It uses the same SEC 1 code and the same a = -3 formulas as P-256. The math doesn't change, only the ruler does.

P-521 fits each coordinate into 66 bytes, with only one bit used in the top byte. And yes, it's 521, not 512. That's not a typo.

Brainpool skips shortcut primes. Its a is neither 0 nor -3, so it needs formulas for any a. Two of its curves look like P-256 and P-384 by point length. brainpoolP512r1's 64-byte coordinates match nothing else here.

And three that aren't Weierstrass

Polkadot went another way. Its accounts sign with sr25519. That's Schnorr over ristretto255, a group that sits on edwards25519. Not a Weierstrass curve at all, so it gets entries of its own. @agntn/curves/sr25519 has keys, signatures and HDKD. @agntn/curves/ristretto255 has the points underneath. And the agent tool? curves_sr25519_compute.

Monero sits on the same curve and skips the ristretto layer. Its keys are plain ed25519 points, small order ones included, so @agntn/curves/ed25519 keeps the cofactor in view. No agent tool for that one yet, just the library.

Ed448 is edwards448 with a cofactor of 4. @agntn/curves/ed448 verifies RFC 8032 signatures, and curves_ed448_compute brings verification to agents. Context is part of the signature, even when it's empty. Ed448's bigger bytes don't come with signing here.

Pages

  • Curves and points: define a curve, add and multiply points, check one, lift an x.
  • Orders and logs: count, list, orders, discrete logs, and how far each goes.
  • Modular arithmetic: mod, powers, inverses, square roots and congruences over BigInt, refused when the input can't work.
  • Linear algebra: rank, nullspaces and systems over a prime field, with a proof when nothing solves them.
  • secp256k1: SEC1 points, scalars mod n, lifting x, ECDSA and BIP340 signatures.
  • sr25519: Polkadot keys, signatures, hard and soft derivation, ristretto255.
  • ed25519: RFC 8032 points, scalars, Monero subgroup checks and Ed25519 signatures.
  • Ed448: RFC 8032 signatures on edwards448, checked under their context.
  • P-256: SEC1 points, point math, lifting x, ECDSA signing, verification and recovery.
  • P-384: the same at 48 bytes a coordinate, for ES384 and certificates.
  • P-521: 66 bytes a coordinate and a single bit in the top one, for ES512 and OpenPGP keys.
  • brainpoolP256r1: P-256 sized points with different parameters and a tricky resemblance.
  • brainpoolP384r1: 48-byte coordinates, like P-384, but a different curve.
  • brainpoolP512r1: 64-byte coordinates stand alone here.