← Back to section · Foundations of Cryptography

An ECDH Key Exchange Walkthrough: From Exchanged Public Keys to a Shared Secret

Dissect elliptic-curve Diffie–Hellman step by step: both parties generate key pairs, exchange public keys, each performs one scalar multiplication to arrive at the same point, and a KDF turns it into a session key; then see why small-subgroup and invalid-curve attacks force implementations to validate points.

Published 22 November 2026 · 6 min read · PQ Frontier

An ECDH Key Exchange Walkthrough: From Exchanged Public Keys to a Shared Secret
AI-generated site artwork
Video explainer · Elliptic curve cryptography: points and groups

The idea of Diffie–Hellman already appeared in the number-theory foundations article: over a public channel, the two parties exchange material and each computes a shared secret that only they can obtain. Replace the carrier group — the multiplicative group modulo a prime — with an elliptic-curve group and you get ECDH, the actual workhorse of the handshake phase in protocols such as TLS 1.3, WireGuard and Signal. This article does not settle for a one-sentence principle; it walks through one complete exchange in message order and accounts, item by item, for where each step can be attacked.

Walkthrough: one exchange in four steps

Suppose the two parties have agreed on a curve and a base point GG (of prime order nn). Step one, generate key pairs: Alice draws a private key a∈[1,n−1]a \in [1, n-1] at random and computes the public key A=aGA = aG; Bob likewise obtains bb and B=bGB = bG. The randomness of the private key is the lifeline of the whole protocol and must come from a cryptographically secure source; a large share of historical key-compromise incidents trace back to random-number quality at exactly this step. Step two, exchange public keys: AA and BB are sent to each other over the public channel, fully visible to an eavesdropper. Step three, each computes the shared point: Alice computes S=aBS = aB, Bob computes S=bAS = bA. That the two agree is no coincidence — aB=a(bG)=(ab)G=b(aG)=bAaB = a(bG) = (ab)G = b(aG) = bA; the associativity of scalar multiplication and the group law guarantees it. Step four, derive the key: take the x-coordinate of SS (it cannot be used as a key directly) and feed it, together with the handshake context, into a key derivation function to obtain the session key.

The eavesdropper holds GG, AA and BB; to compute SS they must first recover aa from AA or bb from BB — that is, solve ECDLP — or compute (ab)G(ab)G directly from AA and BB, the computational Diffie–Hellman problem, considered equally infeasible. Note that the x-coordinate of the shared point SS is not uniformly distributed and carries structure; using it directly as a symmetric key would leave exploitable bias, so the KDF in step four is not a formality but the necessary process that washes an “algebraic object” into a “uniform key”.

Static versus ephemeral: the dividing line of forward secrecy

The walkthrough above is the skeleton; the key deployment choice is the lifetime of the public keys. If both parties use fixed key pairs over the long term (static ECDH), then the later compromise of either private key lets an eavesdropper decrypt all recorded historical traffic. If each session generates a fresh, disposable ephemeral key pair (ephemeral ECDH, ECDHE), the session key is destroyed together with the ephemeral private key, and the long-term private key is used only to sign and authenticate identity, never taking part in the actual key agreement — this is forward secrecy. TLS 1.3 mandates ephemeral mode only, and static ECDH has essentially exited mainstream protocols. The price is two extra scalar multiplications per session, negligible on modern hardware; there is no reason not to pay this bill.

The validation problem: a point is not usable just because it arrived

Step three of the walkthrough hides ECDH’s most important implementation lesson: before multiplying the received point B=bGB = bG by your private scalar, you must confirm that the point really lies in the intended large prime-order subgroup of the agreed curve. If Alice skips validation, an attacker can hand her a carefully crafted point: for example, a point in some small-order subgroup of the curve, in which case S=aBS = aB can take only a few possible values, and Alice’s subsequent behaviour — encrypting with SS or computing message authentication codes — leaks the value of aa modulo that small order; trying several different small-order points in turn and stitching the results together with the Chinese remainder theorem extracts a substantial part of the private key. This is the small-subgroup attack. The invalid-curve attack goes one step further and does not even use the same curve: the attacker sends a point on a “different but isomorphic-looking and much weaker” curve, and if the implementation’s addition formulas do not depend on the coefficient bb (the short Weierstrass formulas indeed do not), the arithmetic proceeds happily on the wrong curve, whose group order is smooth and from which private-key information is far easier to squeeze out.

The defences form a system: check that the point satisfies the curve equation, check that it lies in the correct subgroup (or multiply by the cofactor during validation to clear small-subgroup components), and reject the point at infinity and malformed encodings. The trouble is that every validation step costs money and code, and different curves have different safety boundaries; several major libraries have historically had bugs exactly in their validation logic.

X25519: designing the validation problem away

X25519’s (RFC 7748) approach is not to perform validation in full but to make most of it unnecessary. The scalar is first “clamped”: certain bits are fixed so that the scalar becomes a multiple of the cofactor 8, so any small-subgroup component is automatically annihilated in the scalar multiplication and the small-subgroup attack loses its grip; only x-coordinates are transmitted and only the Montgomery ladder is used, the formulas do not involve the parts of the structure depending on the point’s y-coordinate or the curve coefficient bb, and together with the twist security of Curve25519 itself, invalid-curve attacks are also kept outside the door; an all-zero output is declared a failure, sealing the last degenerate case. The checks left for the protocol designer are compressed to nearly zero — this is the concrete meaning of “born for implementation security” from the overview article, and the reason X25519 became the default choice of modern protocols.

What ECDH does not provide: authentication of identity

The walkthrough must end by drawing a boundary: ECDH only solves “sharing a secret with whoever holds the corresponding private key”; it does not solve “who the other party actually is”. If an attacker sits in the middle of the channel and runs a separate ECDH with Alice and with Bob, both sides obtain a “successful” shared secret while the content is relayed and tampered with in between — the classic man-in-the-middle attack, against which ECDH by itself has no resistance at all. In real protocols, ephemeral public keys must therefore be signed by a long-term identity key (the TLS 1.3 approach) or verified through an out-of-band channel (the safety-number comparison approach); key exchange and identity authentication are two independent processes. Likewise, ECDH provides no integrity, no non-repudiation and no replay protection; the upper layers must supply these with authenticated encryption keyed from the derived key and with the protocol state machine. Treating ECDH as a synonym for “secure channel” is the most common beginner’s misreading.

Summary

The mathematics of ECDH is one line: a(bG)=b(aG)a(bG) = b(aG). But between the principle and a deployable protocol lie three layers of engineering: a KDF washes the shared point into a uniform key, ephemeral keys buy forward secrecy, and point validation (or X25519’s design-level dissolution of the problem) seals off subgroup and invalid-curve attacks. The next article enters the signature domain and compares the two most common elliptic-curve signature schemes: the veteran ECDSA and the later-arriving EdDSA.

Tags: ECDHKey ExchangeX25519