3221 Words

🌀 Elliptic curves and ECDH: the mathematics in depth

Overview · Belongs to 1. The mathematics, section 5 · Previous: ← Diffie–Hellman · Next: Signatures in depth →

Part 1 showed the addition formulas and a table of points. This page derives them, so you see where every symbol comes from, and then follows the path all the way to X25519, the key exchange your SSH client used the last time you connected to GitHub. You need clock arithmetic (inverses) and the idea of Diffie–Hellman. Exercises have their answers hidden under Answer.


1. Why bother with curves?

Diffie–Hellman needs a group in which “repeat the operation $k$ times” is easy and “find $k$” is hard. Multiplication modulo a prime works, but attackers have a powerful shortcut there (index calculus, section 9), so the prime must be 3072 bits for 128-bit security. On a good elliptic curve, no such shortcut is known: 256 bits suffice. Smaller numbers mean faster calculations, shorter keys (32 bytes) and fewer bytes on the wire.


2. The equation

An elliptic curve (in short Weierstrass form) is the set of points $(x, y)$ with

$$y^2 = x^3 + ax + b$$

plus one extra point $\mathcal{O}$, “the point at infinity”. (It is not an ellipse; the name comes from elliptic integrals, which describe the length of an ellipse’s arc.)

Because of $y^2$, the curve is symmetric: if $(x, y)$ is on it, so is $(x, -y)$.

One condition: the curve must have no sharp point (cusp) or self-crossing. That is the case exactly when the discriminant is not zero:

$$4a^3 + 27b^2 \neq 0$$

Exercise 2.1. (a) Check the condition for part 1’s curve, $a = b = 2$, modulo 17. (b) Why is $y^2 = x^3$ not allowed?

Answer

(a) $4 \cdot 8 + 27 \cdot 4 = 32 + 108 = 140 = 8 \cdot 17 + 4 \equiv 4 \neq 0$ ✓

(b) $a = b = 0$ gives discriminant 0: the curve has a cusp at $(0, 0)$. The “addition” on such a curve turns out to be ordinary addition of numbers in disguise, so the discriminant check is not pedantry: on a singular curve, finding $k$ from $kG$ is as easy as a division.


3. Adding points: derive the formulas

The rule

Draw a line through $P$ and $Q$. It hits the curve in exactly one more point $R$. Then $P + Q = -R$, the mirror image of $R$. Put differently: three points on one line add up to zero, $P + Q + R = \mathcal{O}$.

Why exactly one more point? Because substituting a line into a cubic equation gives a cubic in $x$, and a cubic with two known roots has exactly one more.

Derivation for $P \ne Q$

Let $P = (x_1, y_1)$ and $Q = (x_2, y_2)$ with $x_1 \ne x_2$. The line through them is

$$y = \lambda x + \nu, \qquad \lambda = \frac{y_2 - y_1}{x_2 - x_1}, \qquad \nu = y_1 - \lambda x_1$$

Substitute it into the curve:

$$(\lambda x + \nu)^2 = x^3 + ax + b \quad\Longrightarrow\quad x^3 - \lambda^2 x^2 + (a - 2\lambda\nu)x + (b - \nu^2) = 0$$

This cubic has the three roots $x_1, x_2, x_3$. For any cubic $x^3 + c_2 x^2 + \dots$ the roots add up to $-c_2$ (Vieta’s formula; multiply out $(x - x_1)(x - x_2)(x - x_3)$ to see it). Here $c_2 = -\lambda^2$, so

$$x_1 + x_2 + x_3 = \lambda^2 \quad\Longrightarrow\quad \boxed{x_3 = \lambda^2 - x_1 - x_2}$$

The third point on the line has $y$-value $\lambda x_3 + \nu = \lambda(x_3 - x_1) + y_1$. Mirroring flips the sign:

$$\boxed{y_3 = \lambda(x_1 - x_3) - y_1}$$

These are exactly the formulas of part 1. Note that we never needed $b$: it is hidden in the fact that $P$ and $Q$ are on the curve.

Derivation for doubling, $P = Q$

Now the line is the tangent at $P$. Its slope comes from differentiating both sides of $y^2 = x^3 + ax + b$ with respect to $x$ (implicit differentiation):

$$2y \frac{dy}{dx} = 3x^2 + a \quad\Longrightarrow\quad \boxed{\lambda = \frac{3x_1^2 + a}{2y_1}}$$

The tangent touches the curve twice at $P$ (a double root), so Vieta gives $2x_1 + x_3 = \lambda^2$: the same formulas with $x_2 = x_1$.

On a clock there is no “tangent” or “differentiating”, but the formulas are pure algebra (only $+$, $-$, $\cdot$ and division), so they work in any field, including arithmetic modulo a prime. That is why the modulus must be a prime: we need to divide.

The special cases

SituationResultWhy
$P + \mathcal{O}$$P$$\mathcal{O}$ is the zero
$P + (-P)$, with $-P = (x, -y)$$\mathcal{O}$the line is vertical; it “meets the curve at infinity”
doubling a point with $y = 0$$\mathcal{O}$the tangent is vertical (division by $2y = 0$)

With these rules, the points form a group: there is a zero, every point has an opposite, and the order of adding does not matter ($P + Q = Q + P$, because the line through them is the same). The hard property, associativity $(P + Q) + R = P + (Q + R)$, is true but surprisingly difficult to prove; it is usually done with a page of algebra or with more advanced geometry.


4. The curve on a clock: find every point by hand

Take part 1’s curve:

$$E: \; y^2 \equiv x^3 + 2x + 2 \pmod{17}$$

Step 1: which numbers are squares modulo 17? Square $y = 0 \dots 16$:

square012489131516
$y$01, 166, 112, 155, 123, 148, 97, 104, 13

Only 9 of the 17 numbers are squares (0 and half of the rest), and each non-zero square has two roots $y$ and $17 - y$: the mirror pair.

Step 2: calculate the right side $x^3 + 2x + 2$ for every $x$, and look it up:

$x$012345678910111213141516
right side251416192312123153716
square?✓––✓–✓✓✓–✓✓––✓––✓

Step 3: collect the points. Nine values of $x$ give two points each:

$$(0, 6), (0, 11), (3, 1), (3, 16), (5, 1), (5, 16), (6, 3), (6, 14), (7, 6), (7, 11),$$

$$(9, 1), (9, 16), (10, 6), (10, 11), (13, 7), (13, 10), (16, 4), (16, 13)$$

18 points, plus $\mathcal{O}$: 19.

Hasse’s theorem. The number of points $N$ on a curve modulo $p$ satisfies $|N - (p + 1)| \le 2\sqrt p$. Here: $p + 1 = 18$, $2\sqrt{17} \approx 8.2$, so $N$ is between 10 and 26, and 19 fits. Intuition: each $x$ gives on average one point (half the time two, half the time none), plus $\mathcal{O}$. For real curves, $N$ is computed with Schoof’s algorithm, because listing $2^{255}$ points is not an option.

Exercise 4.1. Is $(4, 5)$ on the curve? And $(13, 7)$?

Answer

$(4, 5)$: right side for $x = 4$ is 6, and $5^2 = 25 \equiv 8 \ne 6$: no (6 is not even a square).

$(13, 7)$: $13^3 + 26 + 2 = 2225 = 130 \cdot 17 + 15$, and $7^2 = 49 = 2 \cdot 17 + 15$ ✓: yes.


5. Calculating on the clock

Worked example: doubling $G = (5, 1)$.

$$\lambda = \frac{3 \cdot 5^2 + 2}{2 \cdot 1} = \frac{77}{2} \equiv \frac{9}{2} \equiv 9 \cdot 2^{-1} \equiv 9 \cdot 9 = 81 \equiv 13 \pmod{17}$$

($2^{-1} \equiv 9$, because $2 \cdot 9 = 18 \equiv 1$.)

$$x_3 = 13^2 - 2 \cdot 5 = 159 \equiv 6, \qquad y_3 = 13 \cdot (5 - 6) - 1 = -14 \equiv 3$$

So $2G = (6, 3)$ ✓ (compare the table in part 1).

Exercise 5.1. Calculate (a) $G + 2G = (5, 1) + (6, 3)$, (b) $2 \cdot (6, 3)$, (c) $(5, 1) + (5, 16)$.

Answer (a)

$\lambda = \dfrac{3 - 1}{6 - 5} = 2$. $x_3 = 4 - 5 - 6 = -7 \equiv 10$. $y_3 = 2(5 - 10) - 1 = -11 \equiv 6$. So $3G = $ (10, 6) ✓

Answer (b)

$\lambda = \dfrac{3 \cdot 36 + 2}{2 \cdot 3} = \dfrac{110}{6} \equiv \dfrac{8}{6}$. $6^{-1} \equiv 3$ ($18 \equiv 1$), so $\lambda \equiv 24 \equiv 7$.

$x_3 = 49 - 12 = 37 \equiv 3$, $y_3 = 7(6 - 3) - 3 = 18 \equiv 1$. So $4G = $ (3, 1) ✓

Answer (c)

Same $x$, opposite $y$ ($16 \equiv -1$): this is $G + (-G) = $ $\mathcal{O}$. Indeed $(5, 16) = 18G = -G$ in the table.

Exercise 5.2. Every addition needs one inverse modulo $p$, which is the slowest operation. How do real implementations avoid it?

Answer

They use projective coordinates: a point is stored as $(X : Y : Z)$ meaning $x = X/Z$, $y = Y/Z$ (or a variant). The divisions are postponed by multiplying them into $Z$, and only at the very end is there one single inversion. An inversion can itself be done as $a^{p-2} \bmod p$ (by Fermat), which avoids timing differences.


6. Multiplying a point: double-and-add

$kG$ is computed exactly like square-and-multiply, with “double” instead of “square” and “add $G$” instead of “multiply by $g$”.

Worked example: $13G$. $13 = 1101$ in binary.

bitdoubleadd $G$?result
1$\mathcal{O}$$+ G$$G = (5, 1)$
1$2G = (6, 3)$$+ G$$3G = (10, 6)$
0$6G = (16, 13)$–$6G$
1$12G = (0, 11)$$+ G$$13G = $ (16, 4)

For a 255-bit $k$: about 255 doublings and 128 additions.

⚠️ Timing leaks. If “add” only happens for 1-bits, the time taken (or the power used) reveals the bits of the secret $k$. Real implementations always do the same operations regardless of the bits: see the Montgomery ladder in section 8.

The order of the group

After $19G = \mathcal{O}$ the multiples repeat, so $G$ has order 19. By Lagrange’s rule (the same as for numbers modulo $p$), the order of every point divides the number of points. Since 19 is prime, every point except $\mathcal{O}$ has order 19: every point is a generator. That is exactly what you want (Pohlig–Hellman cannot split the problem).

On real curves the number of points is often $h \cdot n$ with $n$ a big prime and $h$ a small cofactor: $h = 1$ for P-256, $h = 8$ for Curve25519. Then you work in the subgroup of order $n$ and must deal with the small part separately (section 8).

Exercise 6.1. Calculate with the table from part 1: (a) $5G + 7G$, (b) $15G + 10G$, (c) $-(9G)$.

Answer

(a) $12G = $ (0, 11) (b) $25G = 25 - 19 = 6G = $ (16, 13) (c) $-9G = 10G$, and indeed $9G = (7, 6)$ and $10G = (7, 11)$: the mirror image ✓. Multiples count modulo 19, just as exponents counted modulo $p - 1$.


7. ECDH: exercises

Alice picks a secret number $a$ and publishes $A = aG$; Bob picks $b$ and publishes $B = bG$. Alice calculates $aB$, Bob $bA$, and both get $abG$.

Exercise 7.1. On part 1’s curve with $G = (5, 1)$: (a) $a = 5$, $b = 7$; (b) $a = 2$, $b = 13$. Give $A$, $B$ and the shared point.

Answer

(a) $A = 5G = (9, 16)$, $B = 7G = (0, 6)$. Shared: $35G = 16G = $ (10, 11).

(b) $A = 2G = (6, 3)$, $B = 13G = (16, 4)$. Shared: $26G = 7G = $ (0, 6).

In real ECDH only the x-coordinate of the shared point is used (and then hashed into keys): 10 and 0 here.

A second curve, all by yourself

$$E_2: \; y^2 \equiv x^3 + x + 6 \pmod{11}, \qquad H = (2, 7)$$

Exercise 7.2. (a) Find all points. (Squares modulo 11 are 0, 1, 3, 4, 5, 9.) (b) Calculate $2H$. (c) The multiples of $H$ are given below. What is the order? (d) ECDH with $a = 4$, $b = 6$.

$k$12345678910111213
$kH$(2,7)(5,2)(8,3)(10,2)(3,6)(7,9)(7,2)(3,5)(10,9)(8,8)(5,9)(2,4)$\mathcal{O}$
Answer

(a) Right sides for $x = 0 \dots 10$: $6, 8, 5, 3, 8, 4, 8, 4, 9, 7, 4$. Squares among them: $x = 2$ (5), 3 (3), 5 (4), 7 (4), 8 (9), 10 (4). Points: $(2, 4), (2, 7), (3, 5), (3, 6), (5, 2), (5, 9), (7, 2), (7, 9), (8, 3), (8, 8), (10, 2), (10, 9)$, plus $\mathcal{O}$: 13 points. Hasse: $12 \pm 6.6$ ✓

(b) $\lambda = \dfrac{3 \cdot 4 + 1}{2 \cdot 7} = \dfrac{13}{14} \equiv \dfrac{2}{3} \equiv 2 \cdot 4 = 8$ (since $3^{-1} \equiv 4$). $x_3 = 64 - 4 = 60 \equiv 5$, $y_3 = 8(2 - 5) - 7 = -31 \equiv 2$. So $2H = $ (5, 2) ✓

(c) 13, a prime, so again every point is a generator.

(d) $A = 4H = (10, 2)$, $B = 6H = (7, 9)$, shared: $24H = 11H = $ (5, 9).


8. Curve25519 and X25519

Daniel J. Bernstein’s Curve25519 (2006) is the curve in X25519 (key exchange in SSH, TLS, WireGuard, Signal) and, in another shape, Ed25519 (signatures). It is written in Montgomery form:

$$y^2 = x^3 + 486662\,x^2 + x \pmod{2^{255} - 19}$$
  • Field prime $p = 2^{255} - 19$: the name. Being so close to a power of 2 makes “mod $p$” cheap: $2^{255} \equiv 19$, so the top part of a number can be folded back by multiplying by 19.
  • Number of points $8 \cdot \ell$, with $\ell = 2^{252} + 27742317777372353535851937790883648493$ prime. Cofactor 8.
  • Base point $x = 9$ (the smallest $x$ that gives a point of order $\ell$).
  • $486662$ is the smallest $A$ that meets Bernstein’s published security criteria, so there was no room to hide anything in the choice.

Only the x-coordinate

On a Montgomery curve you can compute $x(kP)$ from $x(P)$ alone, never touching $y$. That halves the data (a public key is just 32 bytes of $x$) and removes the whole class of “is this point valid?” bugs: every 32-byte string is accepted as some $x$.

The Montgomery ladder

Keep two points, $R_0$ and $R_1$, always with $R_1 - R_0 = P$. Start with $R_0 = \mathcal{O}$, $R_1 = P$, and go through the bits of $k$ from the top:

  • bit 0: $R_1 \leftarrow R_0 + R_1$, $R_0 \leftarrow 2R_0$
  • bit 1: $R_0 \leftarrow R_0 + R_1$, $R_1 \leftarrow 2R_1$

At the end $R_0 = kP$. Every bit does exactly one addition and one doubling, so the timing reveals nothing about $k$. (Because the difference is always the known point $P$, the addition can be done with only x-coordinates.)

Exercise 8.1. Run the ladder on part 1’s curve for $k = 13 = 1101$ with $P = G$. Write down $(R_0, R_1)$ as multiples of $G$.

Answer

Start $(0, 1)$. Bit 1: $(1, 2)$. Bit 1: $(3, 4)$. Bit 0: $(6, 7)$. Bit 1: $(13, 14)$. So $R_0 = 13G = (16, 4)$ ✓, and the difference $R_1 - R_0$ is always $1 \cdot G$.

Clamping the secret

An X25519 private key is 32 random bytes, adjusted before use:

  1. Clear the lowest 3 bits → $k$ is a multiple of 8. Multiplying by 8 kills any part of a point in the small subgroup of order 8 (the small subgroup attack of Diffie–Hellman, defused by construction).
  2. Clear bit 255, set bit 254 → every key has the same length, so the ladder always runs the same number of steps (constant time again).

Twist security

Because any 32-byte $x$ is accepted, an attacker can send an $x$ that is not on the curve. Such an $x$ lies on the quadratic twist, a sister curve. Bernstein chose Curve25519 so that the twist also has a huge prime factor in its order, so even those points do not help an attacker. Many older curves do not have this property, which is why their implementations must check every received point (and some forgot: the 2018 Bluetooth “invalid curve” attack).

Edwards form: Ed25519

The same curve can be rewritten as a twisted Edwards curve:

$$-x^2 + y^2 = 1 + d\,x^2 y^2, \qquad d = -\frac{121665}{121666}$$

whose addition formula has no special cases at all: the same formula works for $P + Q$, doubling, and $\mathcal{O}$ (which is now the ordinary point $(0, 1)$):

$$x_3 = \frac{x_1 y_2 + y_1 x_2}{1 + d\,x_1 x_2 y_1 y_2}, \qquad y_3 = \frac{y_1 y_2 + x_1 x_2}{1 - d\,x_1 x_2 y_1 y_2}$$

No “if this is a doubling” branch means nothing to forget and nothing to leak. Ed25519 uses this form for signatures.


9. Why elliptic curves are harder to attack

For numbers modulo $p$, index calculus works: pick a factor base of small primes, find many $g^k$ that factor into those small primes, and combine them with linear algebra into discrete logarithms. That makes the attack sub-exponential, and is why classic Diffie–Hellman needs 3072 bits.

On an elliptic curve there is no notion of a “small” point and no factoring of points into smaller ones. The best generic attack, Pollard’s rho, takes about $\sqrt{\pi n / 4}$ steps: for Curve25519 that is about $2^{126}$. That is why 256 bits on a curve match 3072 bits modulo a prime (in digits: 77 against 925, see Why 77 to 925 digits?).

There are weak curves, and standards rule them out:

Weak curveAttack
number of points = $p$ (anomalous)Smart’s attack: discrete log in linear time
small embedding degree (e.g. supersingular curves)MOV attack: moves the problem to numbers modulo $p^k$, where index calculus works
group order with only small prime factorsPohlig–Hellman
singular curve (discriminant 0)becomes ordinary arithmetic (exercise 2.1)

Quantum computers are different: Shor’s algorithm solves the elliptic-curve discrete log just as easily as the classic one. That is why SSH and browsers now combine X25519 with ML-KEM.


10. Build it yourself in Python

A few lines that reproduce every number on this page:

def add(P, Q, a=2, p=17):
    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                                   # P + (-P) = O
    if P == Q: lam = (3*x1*x1 + a) * pow(2*y1, -1, p) % p
    else:      lam = (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, a=2, p=17):                              # double-and-add
    R = None
    for bit in bin(k)[2:]:
        R = add(R, R, a, p)
        if bit == '1': R = add(R, P, a, p)
    return R

G = (5, 1)
[mul(k, G) for k in range(1, 20)]                     # the table from part 1
mul(5, mul(7, G)), mul(7, mul(5, G))                  # ECDH: both (10, 11)
mul(4, (2, 7), a=1, p=11)                             # second curve: (10, 2)

Next: Signatures in depth →