2497 Words

🔢 Prime numbers: the atoms of cryptography

Overview · Belongs to 1. The mathematics · Previous: ← Easy forward, hard backward · Next: RSA →

RSA multiplies two primes. Diffie–Hellman calculates modulo a prime. Curve25519 is named after the prime $2^{255} - 19$. This chapter explains what makes primes so special, proves the two theorems everything rests on, and shows how your computer finds a 1536-bit prime in a fraction of a second. Exercises have their answers hidden under Answer.


1. What is a prime?

A prime is a whole number greater than 1 that can only be divided by 1 and by itself: $2, 3, 5, 7, 11, 13, \dots$ A number greater than 1 that is not prime is composite: $12 = 2 \cdot 2 \cdot 3$.

  • 1 is not a prime. Otherwise the next rule would fail ($6 = 2 \cdot 3 = 1 \cdot 2 \cdot 3 = 1 \cdot 1 \cdot 2 \cdot 3 \dots$).
  • 2 is the only even prime. Every other even number is divisible by 2.

The fundamental theorem of arithmetic. Every whole number greater than 1 can be written as a product of primes in exactly one way (apart from the order).

Primes are the atoms; every number is a molecule made of them. RSA’s security is the claim that, for a molecule of two huge atoms, you cannot find the atoms back.

Exercise 1.1. Write as a product of primes: (a) 360 (b) 1001 (c) 2773 (d) 97

Answer

(a) $360 = 2^3 \cdot 3^2 \cdot 5$

(b) $1001 = 7 \cdot 11 \cdot 13$

(c) $2773 = 47 \cdot 59$ (the RSA modulus from part 1)

(d) $97$ is prime: it is not divisible by 2, 3, 5 or 7, and $11^2 = 121 > 97$, so there is nothing left to try.

Why only up to $\sqrt n$? If $n = a \cdot b$ with both $a$ and $b$ greater than $\sqrt n$, then $a \cdot b > n$. So one of the two factors is at most $\sqrt n$.


2. The sieve of Eratosthenes

The oldest way to list primes (about 240 BC): write down 2 to 100, then repeatedly take the first number that is not crossed out (it is prime) and cross out all its multiples. You only need to sieve with 2, 3, 5 and 7, because $11^2 = 121 > 100$.

The 25 primes below 100 are left:

 2   3   5   7  11  13  17  19  23  29
31  37  41  43  47  53  59  61  67  71
73  79  83  89  97

Exercise 2.1. (a) Find all twin primes below 100 (primes that differ by 2). (b) What is the biggest gap between two consecutive primes below 100?

Answer

(a) (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73). Whether there are infinitely many twin primes is still unknown.

(b) 8, between 89 and 97. Gaps grow slowly: between $n!+2$ and $n!+n$ every number is composite ($n!+k$ is divisible by $k$), so arbitrarily long gaps exist.


3. There are infinitely many primes

Euclid’s proof (about 300 BC), one of the most beautiful in mathematics:

  1. Suppose there are only finitely many primes: $p_1, p_2, \dots, p_k$.
  2. Make $N = p_1 \cdot p_2 \cdots p_k + 1$.
  3. Dividing $N$ by any $p_i$ leaves remainder 1. So none of them divides $N$.
  4. But $N$ has a prime factor (by the fundamental theorem). That prime is not on our list. Contradiction.

Exercise 3.1. $2 \cdot 3 \cdot 5 \cdot 7 \cdot 11 \cdot 13 + 1 = 30031$. Is 30031 prime? Does that break the proof?

Answer

$30031 = 59 \cdot 509$: not prime. That does not break the proof: the proof only says that $N$ has a prime factor outside the list, and 59 and 509 are indeed not among 2, 3, 5, 7, 11, 13.


4. How many primes are there?

The prime-counting function $\pi(N)$ is the number of primes up to $N$ (nothing to do with 3.14). The prime number theorem (Hadamard and de la Vallée Poussin, 1896) says:

$$\pi(N) \approx \frac{N}{\ln N}$$

So near a number $N$, roughly one in every $\ln N$ numbers is prime.

$N$$\pi(N)$$N / \ln N$
1002522
1,000168145
10,0001,2291,086
1,000,00078,49872,382

Exercise 4.1. An RSA-3072 key needs two primes of 1536 bits, so near $2^{1536}$. (a) How many random numbers of that size do you have to try on average to hit a prime? (b) And if you only try odd numbers? (c) Is there any risk that two people get the same prime?

Answer

(a) $\ln 2^{1536} = 1536 \cdot \ln 2 \approx 1065$, so about 1 in 1065. (Why 1536 bits, that is 463 digits per prime: Why 77 to 925 digits?)

(b) Skipping even numbers doubles your chances: about 1 in 532. That is a few hundred quick tests: a fraction of a second.

(c) There are about $2^{1536}/1065 \approx 2^{1526}$ primes of that size, a number with 460 digits. Two honest random choices practically never collide. Collisions in the wild (the gcd attack) came from broken random generators, not from a shortage of primes.


5. Greatest common divisor and Euler’s φ

The greatest common divisor $\gcd(a, b)$ is the biggest number that divides both. Two numbers are coprime when $\gcd(a, b) = 1$. You calculate it with Euclid’s algorithm: replace the pair $(a, b)$ by $(b, a \bmod b)$ until the remainder is 0.

$$\begin{aligned} 1071 &= 2 \cdot 462 + 147 \\ 462 &= 3 \cdot 147 + 21 \\ 147 &= 7 \cdot 21 + 0 \end{aligned} \qquad\Rightarrow\qquad \gcd(1071, 462) = 21$$

It is very fast: the numbers at least halve every two steps, so even for 600-digit numbers it takes only a few thousand steps. Clock arithmetic shows the extended version, which also gives the inverse.

Euler’s totient function $\varphi(n)$ counts how many numbers from 1 to $n$ are coprime to $n$: exactly the numbers that have an inverse modulo $n$.

  • For a prime: $\varphi(p) = p - 1$ (everything except 0, or $p$ itself).
  • For two different primes: $\varphi(pq) = (p-1)(q-1)$.

Why the second rule? Of the $pq$ numbers, the multiples of $p$ ($q$ of them) and of $q$ ($p$ of them) are not coprime, and $pq$ itself was counted twice: $pq - q - p + 1 = (p-1)(q-1)$.

Exercise 5.1. Calculate (a) $\varphi(7)$, (b) $\varphi(10)$, (c) $\varphi(12)$, (d) $\varphi(15)$, (e) $\varphi(143)$, (f) $\varphi(2773)$.

Answer

(a) 6 (b) $\varphi(2 \cdot 5) = 1 \cdot 4 = $ 4: the numbers 1, 3, 7, 9 (c) 4: 1, 5, 7, 11 (12 has a repeated factor, $2^2 \cdot 3$, so the simple product rule does not apply; the general rule is $\varphi(n) = n \prod (1 - 1/p)$ over its primes, $12 \cdot \tfrac12 \cdot \tfrac23 = 4$) (d) $2 \cdot 4 = $ 8 (e) $143 = 11 \cdot 13$, $10 \cdot 12 = $ 120 (f) $46 \cdot 58 = $ 2668

The point of (f): with the factors it is one multiplication. Without them you would have to count up to 2773, or factor first. This is RSA’s trapdoor.

Exercise 5.2. Show that knowing $\varphi(n)$ is just as good as knowing the factors: for $n = 2773$ and $\varphi(n) = 2668$, find $p$ and $q$.

Answer

$\varphi(n) = (p-1)(q-1) = pq - (p + q) + 1 = n - (p + q) + 1$, so $p + q = n - \varphi(n) + 1 = 2773 - 2668 + 1 = 106$. And $pq = 2773$. So $p$ and $q$ are the roots of $x^2 - 106x + 2773 = 0$:

$$x = \frac{106 \pm \sqrt{106^2 - 4 \cdot 2773}}{2} = \frac{106 \pm \sqrt{144}}{2} = \frac{106 \pm 12}{2} = 59 \text{ or } 47$$

That is why $\varphi(n)$ must stay as secret as the primes themselves.


6. Fermat’s little theorem

Fermat’s little theorem (1640). If $p$ is prime and $p$ does not divide $a$, then

$$a^{p-1} \equiv 1 \pmod p$$

Proof. Take the numbers $1, 2, \dots, p-1$ and multiply each by $a$:

$$a \cdot 1,\; a \cdot 2,\; \dots,\; a \cdot (p-1) \pmod p$$

These are all different (if $ai \equiv aj$, multiply by $a^{-1}$: $i \equiv j$) and none is 0. So they are the same $p - 1$ numbers, in a different order (look at a row in the multiplication table modulo 5 in Clock arithmetic). Multiply them all together:

$$a^{p-1} \cdot (p-1)! \equiv (p-1)! \pmod p$$

and since $(p-1)!$ has an inverse, it cancels: $a^{p-1} \equiv 1$. ∎

Euler’s generalisation works for any modulus $n$: if $\gcd(a, n) = 1$, then $a^{\varphi(n)} \equiv 1 \pmod n$. Same proof, with only the $\varphi(n)$ numbers that have an inverse. This is exactly the step that makes RSA decryption work.

Exercise 6.1. Check Fermat for (a) $a = 2, p = 7$ (b) $a = 3, p = 5$. Then use it to calculate (c) $7^{222} \bmod 11$ (d) $1234^{2668} \bmod 2773$ (Euler).

Answer

(a) $2^6 = 64 = 9 \cdot 7 + 1$ ✓ (b) $3^4 = 81 = 16 \cdot 5 + 1$ ✓

(c) $222 = 22 \cdot 10 + 2$, so $7^{222} \equiv 7^2 = 49 \equiv$ 5

(d) $2668 = \varphi(2773)$ and $\gcd(1234, 2773) = 1$, so 1.


7. Testing whether a big number is prime

Trial division up to $\sqrt n$ is impossible for a 1536-bit $n$ ($2^{768}$ divisions). Instead, computers use Fermat’s theorem the other way around.

The Fermat test

If $a^{n-1} \not\equiv 1 \pmod n$, then $n$ is certainly not prime. Example: $3^{340} \bmod 341 = 56 \ne 1$, so 341 is composite ($341 = 11 \cdot 31$), and we found that out without finding a factor.

But the converse is not true: $2^{340} \equiv 1 \pmod{341}$, even though 341 is composite. 341 “fools” base 2; it is a pseudoprime. Worse, some composite numbers fool every base that is coprime to them: the Carmichael numbers $561 = 3 \cdot 11 \cdot 17$, $1105$, $1729$, $2465$, $2821$, …

The Miller–Rabin test

Modulo a prime, the only square roots of 1 are $1$ and $-1$ (because $x^2 - 1 = (x-1)(x+1) \equiv 0$ forces one of the factors to be 0). Miller–Rabin checks exactly that:

  1. Write $n - 1 = 2^s \cdot d$ with $d$ odd.
  2. Pick a base $a$ and calculate $x = a^d \bmod n$.
  3. Square $x$ up to $s$ times. If $n$ is prime, this sequence is either $1$ from the start, or it reaches $-1$ (that is, $n - 1$) before it reaches 1.
  4. If a number other than $\pm 1$ squares to 1, then $n$ is certainly composite.

Worked example: the Carmichael number 561. $560 = 2^4 \cdot 35$. With $a = 2$:

$$2^{35} \equiv 263,\quad 263^2 \equiv 166,\quad 166^2 \equiv 67,\quad 67^2 \equiv 1 \pmod{561}$$

$67^2 \equiv 1$ but $67 \ne \pm 1$: composite, while the Fermat test was fooled.

Worked example: the prime 97. $96 = 2^5 \cdot 3$. With $a = 5$:

$$5^3 \equiv 28,\quad 28^2 \equiv 8,\quad 8^2 \equiv 64,\quad 64^2 \equiv 22,\quad 22^2 \equiv 96 = -1 \;✓$$

The sequence reaches $-1$, as it should for a prime: 97 passes.

A composite number passes one random Miller–Rabin round with a chance of at most 1 in 4. After 40 rounds: at most $4^{-40} = 2^{-80}$. OpenSSL and GnuPG use enough rounds that the chance of a mistake is far smaller than the chance of a hardware error.

Exercise 7.1. Use Miller–Rabin with $a = 2$ on $n = 341$ ($340 = 2^2 \cdot 85$, and $2^{85} \equiv 32 \pmod{341}$). Composite or probably prime?

Answer

$x = 32$. Square: $32^2 = 1024 = 3 \cdot 341 + 1 \equiv 1$. So 32 is a square root of 1 that is not $\pm 1$: composite. Miller–Rabin catches 341 with base 2, where the Fermat test did not.

How a key generator finds a prime

ssh-keygen -t rsa or openssl genrsa does roughly this:

  1. Take a random 1536-bit number; set the top bit (so it really has 1536 bits) and the bottom bit (so it is odd).
  2. Quickly throw it away if it is divisible by one of the first few thousand small primes (most candidates die here).
  3. Run Miller–Rabin rounds on the survivors.
  4. Not prime? Try the next candidate. On average a few hundred candidates.

You can watch it:

openssl prime 2773                    # AD5 (2773) is not prime   (hex first, then decimal)
openssl prime 97                      # 61 (97) is prime
openssl prime -generate -bits 64      # a random 64-bit prime, e.g. 14108925313122120907

8. Special primes in real cryptography

KindDefinitionWhere
Safe prime$p = 2q + 1$ with $q$ also primeClassic Diffie–Hellman groups (RFC 3526, RFC 7919): the group then has no small subgroups to abuse. Our toy prime $23 = 2 \cdot 11 + 1$ is one.
Pseudo-Mersenne primevery close to a power of 2$2^{255} - 19$ (Curve25519, 77 digits), $2^{448} - 2^{224} - 1$ (Curve448). Reducing modulo such a number is very fast.
Mersenne prime$2^k - 1$$2^{127} - 1$; the largest known primes are Mersenne primes. Not used directly in common protocols.
Prime group orderthe number of points on an elliptic curve (or a factor of it) is primeEvery curve used in practice: Curve25519 has $8 \cdot \ell$ points with $\ell = 2^{252} + 2774\dots8493$ prime.

Exercise 8.1. (a) Which primes below 100 are safe primes? (b) $2^{11} - 1 = 2047$. Is it prime? (Hint: try 23.)

Answer

(a) 5, 7, 11, 23, 47, 59, 83 ($= 2q+1$ with $q = 2, 3, 5, 11, 23, 29, 41$). Funny detail: both RSA primes from part 1, 47 and 59, are safe primes.

(b) $2047 = 23 \cdot 89$: not prime, even though 11 is. $2^k - 1$ can only be prime when $k$ is prime, but that is not enough.


9. Why primes, really?

Everything comes together in one property: modulo a prime, every number except 0 has an inverse. That means you can divide, equations $ax \equiv b$ have exactly one answer, and the numbers $1 \dots p-1$ form a neat cycle under multiplication (there is always a generator). Mathematicians call such a structure a field. Elliptic-curve formulas need division (the slope $\lambda$), so they need a field: arithmetic modulo a prime.

RSA uses a modulus that is deliberately not prime, $n = pq$. There, a few numbers have no inverse (multiples of $p$ or $q$), and $\varphi(n)$ can only be calculated by someone who knows the factorisation. That asymmetry is the trapdoor.

Check yourself in Python

import math
math.gcd(1071, 462)          # 21
pow(3, 340, 341)             # 56  → 341 is composite
pow(2, 340, 341)             # 1   → base 2 is fooled
pow(2, 560, 561)             # 1   → Carmichael number fools Fermat
[pow(2, 35 * 2**i, 561) for i in range(5)]   # [263, 166, 67, 1, 1]
sum(1 for k in range(1, 2774) if math.gcd(k, 2773) == 1)   # 2668

Next: RSA: exercises and in depth →