X25519 and elliptic-curve Diffie–Hellman
Two parties each pick a secret number, exchange one curve point each, and arrive at the same shared secret, which an eavesdropper cannot compute without solving a discrete logarithm. X25519 is the version almost everyone uses. Its design choices make it hard to implement wrongly.
- Rests on
- The elliptic-curve Diffie–Hellman problem, which as far as anyone knows is as hard as the ECDLP
- Standards
- RFC 7748 (X25519 and X448); NIST SP 800-56A with the curves of SP 800-186
- Sizes
- 32-byte public keys and shared secret
- Group
- Curve25519 over Fp, p = 2255 − 19; order 8·L with L a 253-bit prime
- Quantum
- Broken by Shor's algorithm; deployments are moving to hybrids with ML-KEM
The exchange
Diffie–Hellman needs a group in which exponentiation is easy and its inverse, the discrete logarithm, is hard. On an elliptic curve with base point G, Alice picks a secret scalar a and publishes aG; Bob picks b and publishes bG. Each multiplies the other's point by their own secret, and both arrive at abG. An eavesdropper sees aG and bG, and getting abG from those is the Diffie–Hellman problem, for which no method is known that is better than recovering a or b outright.
X25519, specified in RFC 7748, is that exchange on Bernstein's Curve25519 with a particular encoding. Keys and points are 32-byte strings, and only the x-coordinate (u in RFC 7748) is ever sent. The code that builds this page implements it and reproduces the RFC's test vectors exactly:
a = 77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a X25519(a, 9) = 8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a b = 5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb X25519(b, 9) = de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f shared K = 4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742
RFC 7748 section 6.1, recomputed at build time: 9 is the base point's u-coordinate
X25519(a, X25519(b, 9)) and X25519(b, X25519(a, 9)) both give K, and both match the RFC.
Design choices that remove mistakes
Most elliptic-curve Diffie–Hellman failures have been implementation failures: invalid-curve points, timing leaks, missing checks. Curve25519 was designed so that the simplest correct implementation is also the natural one.
- The Montgomery ladder. Scalar multiplication uses only x-coordinates and performs the same operations for every bit of the scalar, swapping two values conditionally. There is no branch on secret data. The implementation here is the RFC's pseudocode line for line.
- Clamping. Before use, the three lowest bits of the scalar are cleared, the top bit is cleared and bit 254 is set. The build checks this on Alice's key from the RFC: after clamping it is a multiple of 8 with bit 254 set. Clearing the low bits makes the scalar a multiple of the cofactor 8. Fixing the top bit gives every scalar the same length, so the ladder always runs the same number of steps.
- Twist security. A received u-coordinate that is not on Curve25519 is on its quadratic twist instead, and the ladder computes there without complaint. That is safe only if the twist's group is also hard. The build computes it: the curve has order 8·L and the twist 4·L′, with L and L′ primes of 253 and 253 bits. An invalid point therefore gives an attacker nothing useful, so X25519 needs no point validation.
Low-order points
Clamping has one visible consequence. The curve has a few points of order dividing 8, among them u = 0 and u = 1, and multiplying any of them by a multiple of 8 gives the identity. A peer who sends one of these as its public key forces the shared secret to all zeros, whatever the other side's key. The build checks this for u = 0 and u = 1 against 5 different private keys, and every result is 32 zero bytes.
That does not reveal anything about the private key. It does let a malicious peer make two parties' shared secrets predictable, which matters in protocols that assume the secret is contributory. RFC 7748 allows implementations to reject an all-zero output, and TLS 1.3 requires it: implementations MUST check for the all-zero secret and abort.
Security and the quantum transition
Against classical computers X25519 offers about 2126 security: Pollard's rho on a group of prime order L ≈ 2252, less a little for the negation map. Shor's algorithm breaks it completely, and because recorded traffic can be decrypted later, key exchange is the first thing to migrate. The deployed answer is a hybrid: run X25519 and ML-KEM-768 side by side and combine both secrets, so the result is secure while either holds. Hybrid X25519 and ML-KEM key exchange has been enabled by default in major browsers and in OpenSSH.