TExcelTAD
Home › Number Theory & Cryptography › Elliptic-Curve Diffie-Hellman

Elliptic-Curve Diffie-Hellman

ECDH is Diffie-Hellman with points instead of powers: Alice sends aGaG, Bob sends bGbG, and both compute abGabG. 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 GG of prime order nn.

  1. Alice picks a∈[1,n−1]a \in [1, n - 1] and sends A=aGA = aG.
  2. Bob picks bb and sends B=bGB = bG.
  3. Alice computes aBaB, Bob computes bAbA; both equal abGabG. The shared secret is (a hash of) its xx-coordinate.
python · try it yourself
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 bb, so the victim silently computes on a different curve y2=x3+ax+b′y^2 = x^3 + ax + b' 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 O\mathcal{O}.

python · try it yourself
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 h>1h > 1 (Curve25519 has h=8h = 8), the received point might also lie in a small subgroup; X25519 handles it by clearing the low 3 bits of the scalar ("clamping"), so abGabG ignores any small-order component.

X25519 in one paragraph

X25519 uses the Montgomery curve y2=x3+486662x2+xy^2 = x^3 + 486662x^2 + x over F2255−19\mathbb{F}_{2^{255} - 19} and works only with xx-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:

python · try it yourself
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 xx-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 AA and BB. What would she need to compute to get the shared point, and how hard is that here?

Answer

aa (or bb) via an ECDLP, about 10193≈100\sqrt{10193} \approx 100 steps on the toy curve.

Solution

Recovering aa from A=aGA = aG is an elliptic-curve discrete log; baby-step giant-step takes about 101 steps here, and then aBaB is the secret. On a 256-bit curve the same attack takes about 21282^{128} steps.

Exercise 3: Break the toy (code)

Recover Alice's private key from AA on the toy curve with a brute-force search, then compute the shared secret.

Answer

a=3141a = 3141 and the same shared point as Alice and Bob.

Solution
python · try it yourself
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…

  1. aG+bGaG + bG
  2. abGabG
  3. (a+b)G(a + b)G
  4. GG

The invalid-curve attack works when the receiver…

  1. uses a large curve
  2. does not check that the received point is on the curve
  3. hashes the secret
  4. uses X25519

X25519 public keys are…

  1. 256 bytes
  2. 32 bytes
  3. 64 bytes
  4. 3072 bits