Elliptic-Curve Diffie-Hellman
ECDH is Diffie-Hellman with points instead of powers: Alice sends , Bob sends , and both compute . With 32-byte keys and fast arithmetic, X25519 ECDH is the default key exchange of TLS 1.3, SSH, Signal and WireGuard.
The protocol
Public: a curve, a base point of prime order .
- Alice picks and sends .
- Bob picks and sends .
- Alice computes , Bob computes ; both equal . The shared secret is (a hash of) its -coordinate.
import hashlib
p, a_coef, N, G = 10007, -3, 10193, (2, 3) # toy curve y^2 = x^3 - 3x + 7, prime order
def add(P, Q):
if P is None: return Q
if Q is None: return P
(x1, y1), (x2, y2) = P, Q
if x1 == x2 and (y1 + y2) % p == 0: return None
lam = ((3 * x1 * x1 + a_coef) * pow(2 * y1, -1, p) if P == Q else (y2 - y1) * pow(x2 - x1, -1, p)) % p
x3 = (lam * lam - x1 - x2) % p
return (x3, (lam * (x1 - x3) - y1) % p)
def mul(k, P):
R = None
for bit in bin(k)[2:]:
R = add(R, R)
if bit == "1": R = add(R, P)
return R
a, b = 3141, 5926 # private keys (normally from secrets.randbelow)
A, B = mul(a, G), mul(b, G)
S_alice, S_bob = mul(a, B), mul(b, A)
print("public keys:", A, B)
print("shared point:", S_alice, S_alice == S_bob)
print("session key:", hashlib.sha256(S_alice[0].to_bytes(2, "big")).hexdigest()[:32])public keys: (6063, 1158) (6902, 7611)
shared point: (9622, 6854) True
session key: 7c6b7de5a330d8722c23c4d91501ea44
Validate the peer's point
An attacker can send a point that is not on the curve. The addition formulas never use , so the victim silently computes on a different curve whose group may have small subgroups, and leaks its secret modulo small primes (the invalid-curve attack, which broke several TLS libraries in 2015 and Bluetooth pairing in 2018). The defence: check that the received point satisfies the curve equation and is not .
p, a_coef, b_coef = 10007, -3, 7
def on_curve(P):
return P is not None and (P[1] ** 2 - (P[0] ** 3 + a_coef * P[0] + b_coef)) % p == 0
for P in ((2, 3), (2, 4), (5, 1)):
print(P, "accept" if on_curve(P) else "REJECT")(2, 3) accept
(2, 4) REJECT
(5, 1) REJECT
With a cofactor (Curve25519 has ), the received point might also lie in a small subgroup; X25519 handles it by clearing the low 3 bits of the scalar ("clamping"), so ignores any small-order component.
X25519 in one paragraph
X25519 uses the Montgomery curve over and works only with -coordinates through a Montgomery ladder. Private keys are 32 random bytes, clamped; public keys are 32 bytes. Every 32-byte string is accepted as a public key, which removes most validation pitfalls by design (RFC 7748). Here is its field arithmetic on the real prime, just to show the sizes:
p = 2**255 - 19
print("field bits:", p.bit_length(), " p mod 4:", p % 4, " p mod 8:", p % 8)
A24 = (486662 - 2) // 4
print("ladder constant a24:", A24)
print("486662^2 - 4 is a non-square:", pow(486662**2 - 4, (p - 1) // 2, p) == p - 1)field bits: 255 p mod 4: 1 p mod 8: 5
ladder constant a24: 121665
486662^2 - 4 is a non-square: True
In software
- Python:
X25519PrivateKey.generate(),.exchange(peer_public_key); Go:crypto/ecdh; libsodium:crypto_scalarmult. - TLS 1.3 key shares, WireGuard handshakes and Signal's X3DH all run X25519 (Signal also adds a post-quantum KEM, see Post-Quantum Cryptography).
Exercises
Exercise 1: Key sizes
Compare the public-key sizes of X25519 and 3072-bit finite-field DH at the same security level.
Answer
32 bytes versus 384 bytes.
Solution
X25519 sends one 255-bit -coordinate (32 bytes). Finite-field DH at 128-bit security sends a 3072-bit number, 384 bytes: twelve times larger.
Exercise 2: What the eavesdropper sees
In ECDH on the toy curve, an eavesdropper sees and . What would she need to compute to get the shared point, and how hard is that here?
Answer
(or ) via an ECDLP, about steps on the toy curve.
Solution
Recovering from is an elliptic-curve discrete log; baby-step giant-step takes about 101 steps here, and then is the secret. On a 256-bit curve the same attack takes about steps.
Exercise 3: Break the toy (code)
Recover Alice's private key from on the toy curve with a brute-force search, then compute the shared secret.
Answer
and the same shared point as Alice and Bob.
Solution
p, a_coef, G = 10007, -3, (2, 3)
def add(P, Q):
if P is None: return Q
if Q is None: return P
(x1, y1), (x2, y2) = P, Q
if x1 == x2 and (y1 + y2) % p == 0: return None
lam = ((3 * x1 * x1 + a_coef) * pow(2 * y1, -1, p) if P == Q else (y2 - y1) * pow(x2 - x1, -1, p)) % p
x3 = (lam * lam - x1 - x2) % p
return (x3, (lam * (x1 - x3) - y1) % p)
def mul(k, P):
R = None
for bit in bin(k)[2:]:
R = add(R, R)
if bit == "1": R = add(R, P)
return R
A, B = mul(3141, G), mul(5926, G)
R, k = G, 1
while R != A:
R, k = add(R, G), k + 1
print("a =", k, " shared:", mul(k, B))a = 3141 shared: (9622, 6854)
Go further: X25519 and Forward Secrecy checks the RFC test keys and explains forward secrecy and "harvest now, decrypt later".
Test yourself
In ECDH, the shared secret is…
The invalid-curve attack works when the receiver…
- uses a large curve
- does not check that the received point is on the curve
- hashes the secret
- uses X25519
X25519 public keys are…
- 256 bytes
- 32 bytes
- 64 bytes
- 3072 bits