Elliptic curves over a small field
Points you can add
An elliptic curve is the set of points (x, y) satisfying y² = x³ + ax + b, plus one extra point: the point at infinity O. In cryptography x and y are numbers modulo a prime p (see the Modulo p station), so instead of a smooth line the curve is a cloud of points on a p × p grid. The cloud is symmetric: together with P = (x, y) the curve holds (x, p − y), the opposite point −P. Two points are added by the chord and tangent rule: the line through P and Q meets the curve in a third point, and its mirror image is P + Q; when P = Q, the tangent takes the place of the chord. Over the real numbers this is a drawing; modulo p the same formulas multiply by a modular inverse instead of dividing. With this addition the points form a group in which O plays the role of zero: P + O = P and P + (−P) = O.
Multiplying a point by a number k means adding P k times, but it is computed by the double-and-add shortcut: bit by bit of k the result is doubled, and when the bit is 1, P is added as well — exactly like square-and-multiply in exponentiation. The multiples of P eventually come back to O, and the number of steps, the order of P, divides the number of elements of the group. Curves for cryptography have h · n elements, where n is a large prime and the cofactor h is small: P-256 has h = 1 (NIST SP 800-186, section 3.2.1.3), Curve25519 has h = 8 (RFC 7748). In the toy the curve modulo 23 has 28 = 4 · 7 elements, so you will find points of order 2, 4, 7, 14 and 28 on it.
Curve points on a grid
y² = x³ + 2x + 2 (mod 17)
Points on the grid: 18. With the point at infinity O the group has 19 elements.
Step 4 of 4 · Reflecting −R = (x₃, y) → (x₃, 17 − y) gives the result: R = P + Q = (10, 6).
The dots are all the points of the curve modulo 17, the small squares the line through P and Q, which wraps around the edges of the grid. The dashed line is the mirror axis: (x, y) and (x, 17 − y) lie on the curve together.
Undo the point multiplication
A private key on a curve is a number d, and the public key is the point Q = d · G, where G is a fixed base point of order n. Double-and-add computes Q for a 256-bit d in a few hundred point operations. The other way round — recovering d from Q — is the elliptic-curve discrete logarithm problem (ECDLP). On a well-chosen curve the best known attacks are generic ones, such as baby-step giant-step or Pollard’s rho, which need on the order of √n operations. That is why NIST puts a curve’s strength at half the bit length of n, that is 128 bits for P-256 (SP 800-186, section 3, table 1). For curves over a prime field no counterpart of the number field sieve, which speeds up the discrete logarithm modulo p, is known — hence much shorter keys than in Diffie–Hellman modulo p (compared in the ECDH station). In the “Break it” mode of the toy below you break 12–40-bit curves on your own device: by search and by baby-step giant-step. The times for P-256 are an extrapolation from your measurement, not a computation. Public records are far from 256 bits: in 2009 a discrete logarithm was solved on a curve over a 112-bit prime field, computed on a cluster of more than 200 PlayStation 3 consoles.
Toy implementation — not for production useCurves of 12–40 bits fall in seconds or minutes, and BigInt arithmetic in affine coordinates does not run in constant time. Real keys: P-256 from WebCrypto or X25519 from an audited library.
Discrete logarithm on a curve
Drawing a private key…
Real size: the curve P-256 (NIST SP 800-186). WebCrypto draws a key pair, and the same toy arithmetic computes the public key Q = d · G from the private key d alone. The key is for show — the page uses it for nothing.
Classically P-256 is resistant, but Shor’s algorithm on a quantum computer solves the discrete logarithm on a curve in polynomial time. Roetteler, Naehrig, Svore and Lauter estimated that P-256 needs 2330 logical qubits and about 1.26 · 10¹¹ Toffoli gates, while factoring a 3072-bit RSA modulus needs 6146 qubits (ASIACRYPT 2017) — breaking the curve takes fewer resources than RSA of comparable classical strength. That is why the map marks the curve “quantum-threatened”; the algorithm itself is shown by the Shor’s algorithm station.
Where curves are computed
- P-256 (NIST)The curve y² = x³ − 3x + b modulo p = 2²⁵⁶ − 2²²⁴ + 2¹⁹² + 2⁹⁶ − 1 with cofactor 1, so its number of points is prime — just like the 12–40-bit curves in the toy. NIST SP 800-186 allows it for ECDSA and key establishment; it is the curve on which the toy checks itself against WebCrypto.Source (P-256 (NIST))
- Curve25519 and X25519The Montgomery curve v² = u³ + 486662u² + u modulo p = 2²⁵⁵ − 19 with cofactor 8. The X25519 function multiplies a point on it so that every key goes through the same sequence of operations.Source (Curve25519 and X25519)
- Ed25519EdDSA signatures on the curve edwards25519 over the same field 2²⁵⁵ − 19. RFC 7748 calls it birationally equivalent to Curve25519: the same curve written in a different form.Source (Ed25519)
- Bitcoin — secp256k1Bitcoin authenticates transactions with ECDSA signatures on the curve secp256k1: y² = x³ + 7 modulo p = 2²⁵⁶ − 2³² − 977. BIP 340 defines Schnorr signatures on the same curve.Source (Bitcoin — secp256k1)
Key exchange on a curve, and why you must check that a point lies on the curve, is shown by the ECDH station. ECDSA and Ed25519 signatures, and how a repeated nonce gives the private key away, are shown by the Digital signatures station.
Take curves from a library
Use named curves from a mature implementation: P-256 from WebCrypto, or X25519 and Ed25519 from an audited library. Let the library draw the private key and keep it — in WebCrypto it can be non-extractable (the WebCrypto station). Import the other side’s public key with a function that checks that the point lies on the curve (the ECDH station).
// a P-256 key pair; the private key d never leaves WebCrypto
const { privateKey, publicKey } = await crypto.subtle.generateKey(
{ name: 'ECDSA', namedCurve: 'P-256' }, false, ['sign', 'verify']);
// the public key Q = d · G: 65 B, 0x04 || x || y
const q = new Uint8Array(await crypto.subtle.exportKey('raw', publicKey));using var key = ECDsa.Create(ECCurve.NamedCurves.nistP256);
// the public key only: Q.X and Q.Y, without d
ECParameters pub = key.ExportParameters(includePrivateParameters: false);Do not compute keys with your own curve arithmetic, like this toy does: double-and-add adds P only for bits equal to 1, so the time a multiplication takes gives away the bits of the private key. In X25519, RFC 7748 runs the same sequence of operations for every key and recommends that even swapping variables takes constant time. Do not invent your own curves or parameters: when the number of points has no large prime factor, the discrete logarithm falls apart into small subgroups (Pohlig–Hellman, as in the Modulo p station).
// ✕ the time depends on the bits of the private key d
let R = null;
for (const bit of d.toString(2)) {
R = add(R, R);
if (bit === '1') R = add(R, G); // adds only for a 1 bit
}