1940 Words

✏️ Easy forward, hard backward: exercises

Overview · Belongs to 1. The mathematics, section 2 · Previous: ← Clock arithmetic · Next: Prime numbers →

Public-key cryptography needs a calculation that is fast one way and hopeless the other way. This page lets you feel both directions with your own hands: first how quick the forward direction is, then how slow going back gets, and finally the clever shortcuts attackers use (and why real keys are big enough to survive them). Answers are hidden under Answer.


1. Forward: square-and-multiply

To calculate $b^e \bmod n$, write the exponent $e$ in binary and read it from left to right, starting with $r = 1$:

  • for every bit: square, $r \leftarrow r^2$
  • if the bit is 1: also multiply, $r \leftarrow r \cdot b$

and take the remainder after every step.

Worked example. $3^{13} \bmod 17$. In binary $13 = 1101$.

bitsquaremultiply by 3?$r$
1$1^2 = 1$$1 \cdot 3 = 3$3
1$3^2 = 9$$9 \cdot 3 = 27 \equiv 10$10
0$10^2 = 100 \equiv 15$–15
1$15^2 = 225 \equiv 4$$4 \cdot 3 = 12$12

So $3^{13} \equiv 12 \pmod{17}$, in 4 squarings and 3 multiplications instead of 12 multiplications. Why it works: each square doubles the exponent built so far and each multiply adds 1, so the exponents go $1 \to 3 \to 6 \to 13$: exactly the binary prefixes 1, 11, 110, 1101.

Exercise 1.1. Calculate with square-and-multiply (write the table):

(a) $2^{10} \bmod 11$ (b) $7^{37} \bmod 13$ (c) $5^{13} \bmod 23$ (the example from part 1)

Answer (a)

$10 = 1010$: $r = 1 \to$ (1) $1, \cdot 2 = 2$ → (0) $4$ → (1) $16 \equiv 5$, $\cdot 2 = 10$ → (0) $100 \equiv$ 1. (Of course: Fermat says $2^{10} \equiv 1 \pmod{11}$.)

Answer (b)

$37 = 100101$.

bitsquaremultiply by 7?$r$
11$7$7
0$49 \equiv 10$–10
0$100 \equiv 9$–9
1$81 \equiv 3$$21 \equiv 8$8
0$64 \equiv 12$–12
1$144 \equiv 1$$7$7

So $7^{37} \equiv 7$. Shortcut check with Fermat: $37 \bmod 12 = 1$, so $7^{37} \equiv 7^1$ ✓

Answer (c)

$13 = 1101$: (1) $1 \to 5$ → (1) $25 \equiv 2$, $\cdot 5 = 10$ → (0) $100 \equiv 8$ → (1) $64 \equiv 18$, $\cdot 5 = 90 \equiv$ 21 ✓

Exercise 1.2. How many squarings does square-and-multiply need for a 2048-bit exponent, and how many multiplications at most? How many multiplications does the naive way need?

Answer

About 2048 squarings and at most 2048 multiplications (one per 1-bit, on average about 1024). The naive way needs $e - 1$ multiplications, about $2^{2048}$: a number with 617 digits. Forward is easy only because of square-and-multiply.


2. Backward: the discrete logarithm

The question “which $k$ gives $g^k \equiv h \pmod p$?” is the discrete logarithm. On a small clock you just make the table.

Exercise 2.1. The powers of 2 modulo 11 are:

$k$12345678910
$2^k \bmod 11$24851097361

Solve (a) $2^k \equiv 9$, (b) $2^k \equiv 3$, (c) $2^k \equiv 10 \pmod{11}$.

Answer

(a) 6 (b) 8 (c) 5. Note that $10 \equiv -1$ sits exactly halfway, at $k = (p-1)/2$. That is always the case for a generator.

Exercise 2.2. Make the table of $3^k \bmod 17$ yourself (multiply by 3 each time), and solve $3^k \equiv 13$ and $3^k \equiv 2$.

Answer
$k$12345678910111213141516
$3^k$39101351511161487412261

$3^k \equiv 13$: $k = 4$. $3^k \equiv 2$: $k = 14$. Notice how there is no pattern that lets you guess: 13 is at position 4, 12 at position 13, 14 at position 9.


3. Generators and the order of a number

Not every base visits every number. The powers of each number modulo 7:

$g$$g^1$$g^2$$g^3$$g^4$$g^5$$g^6$order
111
22413
33264516
44213
55462316
6612

The order of $g$ is the first $k$ with $g^k \equiv 1$. A number whose order is $p - 1$ visits everything: a generator (or primitive root). Modulo 7 those are 3 and 5.

Lagrange’s rule: the order always divides $p - 1$. Modulo 7 the possible orders are 1, 2, 3 and 6, never 4 or 5.

Exercise 3.1. Which of the numbers 2 to 10 are generators modulo 11? (Hint: the order must divide 10, so it is 1, 2, 5 or 10. You only need to check that $g^2 \not\equiv 1$ and $g^5 \not\equiv 1$.)

Answer

$g^5 \bmod 11$ for $g = 2 \dots 10$: $10, 1, 1, 1, 10, 10, 10, 1, 10$. The ones with $g^5 \equiv 10 \equiv -1$ (and $g^2 \neq 1$) are generators: 2, 6, 7, 8. The others (3, 4, 5, 9) have order 5, and 10 has order 2.

Exercise 3.2. Modulo 23 (the Diffie–Hellman example in part 1), $p - 1 = 22 = 2 \cdot 11$. What orders are possible? Part 1 used $g = 5$. Is 5 a generator?

Answer

Orders divide 22: 1, 2, 11 or 22. Check 5: $5^2 = 25 \equiv 2 \ne 1$ and $5^{11} \equiv 22 \ne 1$ (see the table in part 1), so the order is 22: yes, a generator. Of the 22 numbers, exactly 10 are generators (5, 7, 10, 11, 14, 15, 17, 19, 20, 21), ten have order 11, and $22 \equiv -1$ has order 2. Why that matters for security: Diffie–Hellman in depth.


4. Why backward is slow, and how attackers speed it up

Brute force tries $k = 1, 2, 3, \dots$: on average $p/2$ attempts. For a 2048-bit $p$ that is about $2^{2047}$: hopeless.

Baby-step giant-step (Shanks, 1971) is a smarter attack with about $\sqrt p$ steps. Write the unknown as $k = i \cdot m + j$ with $m \approx \sqrt p$. Then $g^k = h$ becomes:

$$g^j = h \cdot (g^{-m})^i$$

Make a list of all $g^j$ (the baby steps), then keep multiplying $h$ by $g^{-m}$ (the giant steps) until you land in the list.

Worked example. Solve $5^k \equiv 21 \pmod{23}$ with $m = 5$.

Baby steps $5^j$ for $j = 0 \dots 4$: $\quad 1, 5, 2, 10, 4$.

Giant step factor: $5^5 \equiv 20$, and $20^{-1} \equiv 15$ (because $20 \cdot 15 = 300 = 13 \cdot 23 + 1$).

Giant steps $21 \cdot 15^i$:

$i$valuein the baby list?
021no
1$21 \cdot 15 = 315 \equiv 16$no
2$16 \cdot 15 = 240 \equiv 10$yes: $j = 3$

So $k = 2 \cdot 5 + 3 = $ 13, and indeed $5^{13} \equiv 21$ (exercise 1.1c). We used 5 + 3 steps instead of up to 22.

Exercise 4.1. Baby-step giant-step needs about $\sqrt p$ steps. Why doesn’t that break a 2048-bit Diffie–Hellman? And why are 256-bit elliptic curves still fine?

Answer

$\sqrt{2^{2048}} = 2^{1024}$ steps (and memory): still far beyond reach. For elliptic curves, the best known attacks (Pollard’s rho, the memory-free cousin of baby-step giant-step) are also about $\sqrt n$: $\sqrt{2^{256}} = 2^{128}$. That is the “128-bit security” of Curve25519. For classic Diffie–Hellman there exist even faster attacks (the number field sieve), which is why it needs 2048 to 3072 bits while elliptic curves manage with 256. See Elliptic curves in depth.


5. Factoring: the other one-way function

Multiplying is easy: $47 \cdot 59 = 2773$. Going back, the simplest method is trial division: try every prime up to $\sqrt n$. For 2773, $\sqrt{2773} \approx 52.7$, so you try 2, 3, 5, 7, …, 47 and find $2773 = 47 \cdot 59$.

Exercise 5.1. Factor by trial division: (a) 221 (b) 1001 (c) 8633

Answer

(a) $\sqrt{221} \approx 14.9$; 2, 3, 5, 7, 11 fail; $221 = $ 13 · 17

(b) $1001 = $ 7 · 11 · 13 (a nice one to remember: $1001 \cdot 123 = 123123$)

(c) $\sqrt{8633} \approx 92.9$; the first prime that divides it is 89: $8633 = $ 89 · 97. That took 24 primes to try.

For a 3072-bit RSA modulus, trial division would need about $2^{1536}/1064$ attempts. The best real method (the general number field sieve) is much faster than that, but still far too slow: the record is a 829-bit number (RSA-250, 2020), which took about 2,700 CPU-core years. Why exactly 3072 bits (925 digits) is enough: Why 77 to 925 digits?

Fermat’s trick: when the primes are close together

If $p$ and $q$ are close, then $n = pq$ is almost a square. Write $n = a^2 - b^2 = (a - b)(a + b)$: start at $a = \lceil\sqrt n\rceil$ and check whether $a^2 - n$ is a perfect square.

Worked example. $n = 10403$. $\sqrt{10403} \approx 101.99$, so $a = 102$: $102^2 - 10403 = 10404 - 10403 = 1 = 1^2$. So $10403 = (102 - 1)(102 + 1) = $ 101 · 103, in one step.

Exercise 5.2. Use Fermat’s trick on (a) 9991 and (b) 2773 (our RSA modulus from part 1!).

Answer

(a) $a = 100$: $10000 - 9991 = 9 = 3^2$ → $9991 = 97 \cdot 103$

(b) $a = 53$: $2809 - 2773 = 36 = 6^2$ → $2773 = 47 \cdot 59$, again in one step.

Lesson: the two RSA primes must not be close together. Real key generators pick them independently at random, and the standard (FIPS 186-5) requires them to differ in their first 100 bits. In 2022 printers from Fujifilm and Canon were found making RSA keys with primes so close that this exact trick broke them in seconds (CVE-2022-26320).

A shared prime: gcd breaks two keys at once

Computing a greatest common divisor is fast, even for huge numbers (Euclid’s algorithm). So if two RSA keys accidentally share a prime, anybody can find it.

Exercise 5.3. Two devices made the RSA moduli $n_1 = 2773$ and $n_2 = 2491$. Calculate $\gcd(2773, 2491)$ with Euclid’s algorithm and factor both.

Answer$$\begin{aligned} 2773 &= 1 \cdot 2491 + 282 \\ 2491 &= 8 \cdot 282 + 235 \\ 282 &= 1 \cdot 235 + 47 \\ 235 &= 5 \cdot 47 + 0 \end{aligned}$$

$\gcd = $ 47, so $2773 = 47 \cdot 59$ and $2491 = 47 \cdot 53$. In 2012, researchers did this on millions of public keys found on the internet and could recover the private keys of 0.5 % of all TLS servers they scanned (“Mining your Ps and Qs”, Heninger et al.), mostly from routers that generated keys right after booting, before their random generator had any randomness.


6. Summary

forward (easy)backward (hard)best known attack
Exponentiation$g^k \bmod p$, square-and-multiply, $\log_2 k$ stepsdiscrete logarithmnumber field sieve (sub-exponential)
Multiplication$p \cdot q$factoringnumber field sieve (sub-exponential)
Point multiplication$kG$, double-and-addelliptic-curve discrete logarithmPollard’s rho, $\sqrt n$ steps

Check yourself in Python

pow(3, 13, 17)                      # 12
[pow(2, k, 11) for k in range(1, 11)]   # the table of exercise 2.1
next(k for k in range(1, 23) if pow(5, k, 23) == 21)   # 13, brute force
import math
math.isqrt(10403) + 1               # 102, the start of Fermat's trick
math.gcd(2773, 2491)                # 47

Next: Prime numbers →