Leandro Junes

Elliptic-Curve Diffie–Hellman

Explore elliptic-curve key exchange and shared-secret computation.

Discover how Alice and Bob use elliptic-curve points to establish the same shared secret.

Curve and base point

public

Small curves are for learning, not security.

A prime from 5 to 251, any whole coefficients, and a point of the curve for G.

  • G the base point
  • QA Alice’s public point
  • QB Bob’s public point
  • S the shared point (thicker ring)
  • R, T a multiplication in progress (dashed), when its steps are open below
  • □ the point being inspected

Alice and Bob

Alice

Public point QA public —

Bob

Public point QB public —

  • Alice sendsQA = —to Bob
  • Alice receivesQB = —from Bob

Only points travel. The scalars dA and dB are never sent. The curve and G are public.

Shared point

Alice computes dA · QB —

Bob computes dB · QA —

Explore further

Explore scalar multiplication

Multiplying a point by k means adding it to itself k times. Double-and-add does it in a few steps by reading k in binary. Step through one of the four multiplications; the plot above follows.

Show point-addition mathematics

Adding two points is done with formulas, not with a ruler. They come from the chord-and-tangent rule for curves over the real numbers, but over a finite field there is no chord to draw: only the algebra remains, with a modular inverse where a division would be.

Can Eve recover Alice’s scalar?

Finding d from G and Q = d · G is the elliptic-curve discrete logarithm problem. On a curve this small it is no problem at all.

Understanding ECDH

What is an elliptic curve over a finite field?

The set of pairs (x, y) of residues modulo a prime p with y² ≡ x³ + ax + b, together with one extra point O. The coefficients must satisfy 4a³ + 27b² ≢ 0; otherwise the curve is singular and what follows fails. It is a finite set of points, not a line that can be drawn.

What is the point at infinity?

O is the identity of the group: P + O = P for every point. It has no coordinates, and it is not the pair (0, 0), which may or may not be a point of the curve like any other pair.

What is point addition, and why do the points form a group?

Two points are added by the formulas shown under “Show point-addition mathematics”. The inverse of (x, y) is (x, −y), and a point plus its inverse is O. The sum of two points of the curve is again a point of the curve, the order of adding does not matter, and (P + Q) + R = P + (Q + R). That last fact is far from obvious from the formulas; this page’s tests check it for every triple of points on several small curves.

What is scalar multiplication?

kP = P + P + … + P, with k terms. The order of G is the smallest n ≥ 1 with nG = O; the multiples of G are then n different points, which need not be all the points of the curve. This page asks for a G of prime order, as real systems do.

Why do Alice and Bob get the same point?

dA · QB = dA(dB G) = (dAdB)G = dB(dA G) = dB · QA

Adding G to itself dAdB times gives the same point however the additions are grouped. It is the same idea as classical Diffie–Hellman, where (gb)a = (ga)b, in a different group:

Classical Diffie–Hellman

  • Public value: ga mod p
  • Shared secret: gab mod p
  • Operation: multiplying residues

Elliptic-curve Diffie–Hellman

  • Public point: aG
  • Shared point: abG
  • Operation: adding points

What is the elliptic-curve discrete logarithm problem?

Given G and QA = dA · G, find dA. Computing QA takes a few dozen point operations even for enormous scalars; no practical way back is known for properly chosen curves with a subgroup of large prime order.

Why are toy curves insecure?

The default curve has 19 points, so Eve tries at most 18 scalars. Any curve on this page has fewer than 300. A somewhat larger prime would not help: real curves are standardized, with primes of 256 bits or more and many other properties checked by experts. Use the Modern ECDH view to see one at work.

Why does ECDH require authentication?

The exchange shows that two parties computed the same point, not who they are. Someone in the middle can swap each public point for her own and share one secret with Alice and another with Bob. Real protocols authenticate the public keys, for example with signatures or certificates.

  • Diffie–Hellman: the same key agreement in the multiplicative group modulo a prime.
  • ElGamal: public-key encryption built on that same group mathematics.
  • RSA: public-key encryption of another kind, with RSA-OAEP in the browser.