Diffie–Hellman and RSA both work, but to stay ahead of the best attacks their keys have grown large: 2048 to 3072 bits. Elliptic curve cryptography (ECC) gets the same security with keys about a tenth of that size. Smaller keys mean less computing, less battery and shorter messages, which is why phones, messaging apps and most HTTPS connections now use it.
NIST’s recommended key sizes (from NIST SP 800-57) show the difference. The security level means roughly “as hard as brute-forcing a symmetric key of this many bits.”
| Security level | Symmetric key | RSA or Diffie–Hellman mod | Elliptic curve |
|---|---|---|---|
| 112 bits | Triple DES (now retired) | 2048 bits | 224 bits |
| 128 bits | AES-128 | 3072 bits | 256 bits |
| 192 bits | AES-192 | 7680 bits | 384 bits |
| 256 bits | AES-256 | 15360 bits | 512 bits |
The plan for this section: what an elliptic curve is, how to “add” two points on one, why that addition works with integers mod , and then Diffie–Hellman rebuilt on a curve. You don’t need anything beyond the modular arithmetic you already know.
What Is an Elliptic Curve?¶
That rule rules out curves with a sharp point or a place where the curve crosses itself. Despite the name, an elliptic curve has nothing to do with ellipses. (The name comes from the history of calculating the lengths of curved arcs of ellipses.)
Here’s the curve :
Source
import numpy as np
import matplotlib.pyplot as plt
def plot_curve(a, b):
# y^2 = x^3 + ax + b: plot the top half and its mirror image
x = np.linspace(-2, 2.5, 2000)
right_side = x**3 + a * x + b
x = x[right_side >= 0]
y = np.sqrt(right_side[right_side >= 0])
plt.plot(x, y, color="tab:blue")
plt.plot(x, -y, color="tab:blue")
plt.axhline(0, color="gray", linewidth=0.5)
plt.axvline(0, color="gray", linewidth=0.5)
plt.xlabel("x")
plt.ylabel("y")
plot_curve(-1, 1)
plt.title("y² = x³ − x + 1")
plt.show()
Two features matter. The curve is symmetric about the -axis, because and give the same . And a straight line usually crosses it in exactly three points.
Adding Two Points¶
The key operation is a way to “add” two points and on the curve to get a third point on the curve:
For example, on , take and . The line through them is . Setting in the equation gives , so , and , 0 or 1. The third point is , and reflecting it gives .
Source
plot_curve(-1, 1)
plt.plot([-2, 2.5], [1, 1], color="tab:orange", linestyle="--")
plt.plot([1, 1], [1, -1], color="gray", linestyle=":")
for (x, y), label in [((-1, 1), "P"), ((0, 1), "Q"), ((1, 1), "R"), ((1, -1), "P + Q")]:
plt.plot(x, y, "o", color="black")
plt.annotate(label, (x, y), textcoords="offset points", xytext=(6, 6))
plt.title("Adding P = (-1, 1) and Q = (0, 1)")
plt.show()
It’s a strange definition, but it behaves like addition: , and . (The second fact is surprisingly hard to prove, but you can just use it.)
Adding a point to itself. To compute there’s only one point, so use the line that is tangent to the curve at instead. On at , the tangent line is . It crosses the curve again at , and reflecting gives .
The point at infinity. If and are reflections of each other, the line through them is vertical and never hits a third point. We say , the point at infinity. It acts like zero: . That’s why the curves below always count as one extra point.
The Formulas¶
The pictures turn into formulas using slopes, which is how a computer adds points. Let and . The slope of the line is
(the second one is the slope of the tangent line, from calculus). Then , already reflected, where
Check with the picture: and give , so and . That’s .
The important thing about these formulas is that they only add, subtract, multiply and divide. So they work just as well with integers mod .
Elliptic Curves Mod ¶
For cryptography, use the same equation with integers mod a prime . Instead of a smooth curve you get a scatter of points. Here are all the points on :
p, a, b = 29, 4, 20
points = [(x, y) for x in range(p) for y in range(p)
if (y * y - (x**3 + a * x + b)) % p == 0]
print(len(points), "points, plus the point at infinity")
print(points)36 points, plus the point at infinity
[(0, 7), (0, 22), (1, 5), (1, 24), (2, 6), (2, 23), (3, 1), (3, 28), (4, 10), (4, 19), (5, 7), (5, 22), (6, 12), (6, 17), (8, 10), (8, 19), (10, 4), (10, 25), (13, 6), (13, 23), (14, 6), (14, 23), (15, 2), (15, 27), (16, 2), (16, 27), (17, 10), (17, 19), (19, 13), (19, 16), (20, 3), (20, 26), (24, 7), (24, 22), (27, 2), (27, 27)]
Source
plt.figure(figsize=(6, 6))
plt.scatter([x for x, y in points], [y for x, y in points], color="tab:blue")
plt.axhline(p / 2, color="gray", linestyle=":")
plt.xlabel("x")
plt.ylabel("y")
plt.title("y² = x³ + 4x + 20 (mod 29)")
plt.show()
There are 36 points, plus , for 37 in all. You can check any of them by hand: for , . The mirror symmetry is still there, but now the reflection of is , shown by the dotted line at .
“Dividing” in the slope formula means multiplying by an inverse mod , exactly like ElGamal decryption and finding RSA’s . Python’s pow(k, -1, p) does it. Here are the formulas as Python functions, with None standing for the point at infinity:
def ec_add(P, Q, a, p):
# add two points on y^2 = x^3 + ax + b (mod p); None is the point at infinity
if P is None:
return Q
if Q is None:
return P
x1, y1 = P
x2, y2 = Q
if x1 == x2 and (y1 + y2) % p == 0: # P and Q are reflections
return None
if P == Q:
slope = (3 * x1**2 + a) * pow(2 * y1, -1, p) % p
else:
slope = (y2 - y1) * pow(x2 - x1, -1, p) % p
x3 = (slope**2 - x1 - x2) % p
y3 = (slope * (x1 - x3) - y1) % p
return (x3, y3)Example. Add and on .
The slope is . The inverse of 3 mod 29 is 10 (since ), so .
.
.
So . Check that it’s on the curve: , and .
Doubling uses the tangent slope , which gives .
ec_add((2, 6), (5, 7), 4, 29)(6, 12)ec_add((2, 6), (2, 6), 4, 29)(1, 5)Multiplying a Point: ¶
means added to itself times. Starting from , the multiples jump around the curve with no obvious pattern:
G = (1, 5)
point = G
for k in range(1, 9):
print(f"{k}G = {point}")
point = ec_add(point, G, 4, 29)1G = (1, 5)
2G = (4, 19)
3G = (20, 3)
4G = (15, 27)
5G = (6, 12)
6G = (17, 19)
7G = (24, 22)
8G = (8, 10)
Computing for a huge doesn’t mean doing additions. Use double and add, the same trick as square and multiply: to find , write , so , and get , and by doubling three times. For a 256-bit that’s about 256 doublings plus some additions, which is fast for a computer.
def ec_multiply(k, P, a, p):
# compute kP by double and add
result = None
doubled = P # P, 2P, 4P, 8P, ...
while k > 0:
if k % 2 == 1:
result = ec_add(result, doubled, a, p)
doubled = ec_add(doubled, doubled, a, p)
k = k // 2
return result
print("13G =", ec_multiply(13, G, 4, 29))
print("37G =", ec_multiply(37, G, 4, 29))13G = (16, 27)
37G = None
On this curve , so the multiples of cycle through all 37 points.
The Hard Problem¶
This is the elliptic curve version of the discrete log problem.
Easy direction: given and , compute with double and add.
Hard direction: given and a point , find the with .
On a 37-point curve you could just try every . On a real curve with about 2256 points, the best known methods take about 2128 steps, which is far out of reach. Compare the mod version from Diffie–Hellman: given and , find . The structure is identical; “multiply by ” became “add .”
Elliptic Curve Diffie–Hellman¶
Here’s Diffie–Hellman rebuilt on a curve (called ECDH). The curve and the point are public.
Alice picks a secret and sends .
Bob picks a secret and sends .
Alice computes and Bob computes . Both get , because .
Usually the -coordinate of the shared point becomes the key for a symmetric cipher like AES. Eve sees , and , and would need or , which is the hard problem.
a_secret, b_secret = 6, 10
A = ec_multiply(a_secret, G, 4, 29)
B = ec_multiply(b_secret, G, 4, 29)
print("Alice sends", A, " Bob sends", B)
print("Alice computes", ec_multiply(a_secret, B, 4, 29))
print("Bob computes ", ec_multiply(b_secret, A, 4, 29))Alice sends (17, 19) Bob sends (13, 23)
Alice computes (5, 7)
Bob computes (5, 7)
Everything from the man-in-the-middle section carries over, so ECDH still needs signatures to check whose public point you received.
Why Can the Keys Be So Small?¶
The answer is about the best known attacks. For RSA and mod Diffie–Hellman, the number field sieve and related methods are much faster than brute force, so the numbers have to be thousands of bits long. For a well-chosen elliptic curve, nobody has found anything much better than general methods that take about steps for a curve with points. So a 256-bit curve gives about 128 bits of security. “Well-chosen” matters: some special curves are weak, which is why everyone uses a few carefully studied standard curves.
Elliptic Curves in Real Life¶
ECC is now the default almost everywhere.
X25519 (Diffie–Hellman on a curve called Curve25519) is the most common key exchange in TLS 1.3. Signal and SSH use it too.
ECDSA on the NIST curve P-256 is widely used to sign web certificates, and Ed25519 is common for SSH keys and signing code. These are the elliptic curve versions of the RSA signatures from earlier in this chapter.
Bitcoin and Ethereum sign transactions with elliptic curve signatures on a curve called secp256k1.
What About Quantum Computers?¶
In 1994 Peter Shor found an algorithm that, on a large enough quantum computer, would factor and solve both kinds of discrete log problem efficiently. RSA, Diffie–Hellman and ECC would all break. (Symmetric ciphers like AES aren’t broken this way; they just need longer keys.) No quantum computer anywhere near that size exists, but an attacker could record encrypted traffic now and decrypt it later, so the switch has already started. In 2024 NIST published its first post-quantum standards: ML-KEM for sharing keys (FIPS 203) and ML-DSA for signatures (FIPS 204). They’re built on problems about lattices instead of factoring or curves. Many browsers and messaging apps already combine X25519 with ML-KEM, so a connection stays safe as long as either one holds up.