Path: blob/main/foundations/06-elliptic-curves/connect/ecdh-x25519-tls13.ipynb
483 views
Connect: ECDH (X25519) in TLS 1.3
Module 06 | Real-World Connections
The elliptic curve Diffie-Hellman you learned in Notebook 06f is the default key exchange in every modern HTTPS connection.
Introduction
When you visit https:// anything, your browser performs a TLS 1.3 handshake with the server. The most common key exchange in TLS 1.3 is X25519: elliptic curve Diffie-Hellman on Curve25519.
This notebook traces how the abstract concepts from Module 06 --- scalar multiplication, the ECDLP, curve choice --- become the concrete key exchange that secures the internet.
We will:
Understand the Montgomery form of Curve25519
See why -coordinate-only arithmetic (the Montgomery ladder) is elegant and secure
Simulate an ECDH key exchange on a small Montgomery curve
Understand why X25519 was chosen over NIST curves
Curve25519: A Montgomery Curve
Curve25519 is defined by the Montgomery form:
with , , over where .
This is a different representation from the short Weierstrass form we used in Module 06, but it is still an elliptic curve with the same group structure. The Montgomery form enables a special trick: -coordinate-only scalar multiplication.
The X25519 Function: -Coordinate Only
The key innovation of X25519 is that the entire Diffie-Hellman computation uses only -coordinates. The -coordinate is never computed.
For Montgomery curves, there is a formula for the -coordinate of that depends only on , , and . This is called the Montgomery ladder, and it has a beautiful property: every step performs the same operations regardless of the scalar bit, making it naturally constant-time.
Let's demonstrate on a small Montgomery curve.
Toy ECDH Key Exchange (Montgomery Style)
Let's simulate a full X25519-style key exchange on our small Montgomery curve. This is exactly what happens during a TLS 1.3 handshake.
Why X25519 Over NIST Curves?
TLS 1.3 supports both NIST P-256 and X25519, but X25519 is the overwhelmingly preferred choice. Here is why:
| Property | X25519 (Curve25519) | P-256 (NIST) |
|---|---|---|
| Constant-time | By design (Montgomery ladder) | Requires careful implementation |
| Twist-secure | Yes (twist cofactor = 4) | No (twist is vulnerable) |
| Input validation | Minimal (any 32 bytes is valid) | Must check point on curve |
| Side-channel resistance | Built into the design | Implementation-dependent |
| Speed | Very fast (special prime ) | Moderate |
| Specification clarity | Complete, single document | Complex, multiple standards |
| Trust | Transparent design by Bernstein | "Nothing up my sleeve" concerns |
The key advantage: X25519 is hard to implement wrong. The Montgomery ladder is naturally constant-time, twist security means input validation is minimal, and the function accepts any 32-byte string as a valid private key (after clamping).
Concept Map: Module 06 Concepts in TLS 1.3
| Module 06 Concept | TLS 1.3 Application |
|---|---|
| Scalar multiplication | X25519 function: compute shared secret |
| ECDLP hardness | Security of the key exchange |
| Curve choice (Montgomery form) | Enables -only arithmetic, constant-time |
| Group order and cofactor | Key clamping clears cofactor bits |
| Twist security (Break notebook) | Curve25519 is twist-secure by design |
| Point validation | X25519 needs minimal validation (twist-secure) |
Summary
| Concept | Key idea |
|---|---|
| Curve25519 | A Montgomery curve over , designed for speed and safety. |
| X25519 (x-only DH) | Scalar multiplication uses only x-coordinates via the Montgomery ladder, so the y-coordinate is never computed. |
| Constant-time by design | The Montgomery ladder performs the same operations for every scalar bit, providing natural side-channel resistance. |
| Twist security | Curve25519 is twist-secure, so implementations do not need to validate whether points are on the curve or the twist. |
| Key clamping | The scalar is forced to be a multiple of the cofactor (8), killing small-subgroup attacks. |
| Fast arithmetic | The prime enables especially efficient modular reduction. |
Module 06 gave you the foundations: scalar multiplication, ECDLP hardness, curve group structure. X25519 is those foundations engineered into a protocol that is fast, secure, and hard to misimplement.
Back to Module 06: Elliptic Curves