2271 Words

✏️ Clock arithmetic: exercises

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

Everything in public-key cryptography happens on a clock. If this page feels easy, the rest of the series will too. Every exercise has its answer hidden below it: work it out on paper first, then click Answer.


1. The remainder

$a \bmod n$ is the remainder after dividing $a$ by $n$. It is always one of $0, 1, \dots, n-1$.

Worked example. $38 \bmod 12$: $38 = 3 \cdot 12 + 2$, so the remainder is $2$. On a 12-hour clock, 38 hours after midnight the hand points at 2.

Exercise 1.1. Calculate: (a) $17 \bmod 5$, (b) $100 \bmod 7$, (c) $365 \bmod 7$, (d) $2026 \bmod 9$, (e) $1000 \bmod 13$.

Answer

(a) $17 = 3 \cdot 5 + 2$ → 2

(b) $100 = 14 \cdot 7 + 2$ → 2

(c) $365 = 52 \cdot 7 + 1$ → 1 (that is why your birthday moves one weekday per year, or two after 29 February)

(d) $2026 = 225 \cdot 9 + 1$ → 1. Shortcut: modulo 9 a number equals its digit sum: $2+0+2+6 = 10$, $1+0 = 1$.

(e) $1000 = 76 \cdot 13 + 12$ → 12

Negative numbers

The remainder is never negative. To get $-3 \bmod 12$, keep adding 12 until you are in $0 \dots 11$: $-3 + 12 = 9$. On the clock: 3 hours before midnight is 9 o’clock.

⚠️ Programming languages disagree here. Python’s -3 % 12 gives 9 (the mathematical answer); C, Java and JavaScript give -3. Crypto code that forgets this has real bugs.

Exercise 1.2. (a) $-3 \bmod 12$, (b) $-20 \bmod 7$, (c) $-1 \bmod 23$, (d) it is 9:00; what time was it 14 hours ago?

Answer

(a) 9 (b) $-20 + 21 = $ 1 (c) $-1 + 23 = $ 22. Remember this one: $-1$ and $n-1$ are the same number on the clock, which comes back in Diffie–Hellman.

(d) $(9 - 14) \bmod 12 = -5 \bmod 12 = $ 7 o’clock.


2. “Congruent”: the same place on the clock

$a \equiv b \pmod n$ means: $a$ and $b$ give the same remainder, or equivalently, $n$ divides $a - b$. That second form is usually the fastest check.

Exercise 2.1. True or false?

(a) $38 \equiv 14 \pmod{12}$ (b) $100 \equiv 1 \pmod 9$ (c) $50 \equiv 22 \pmod 7$ (d) $45 \equiv 12 \pmod{10}$

Answer

(a) $38 - 14 = 24 = 2 \cdot 12$ → true

(b) $100 - 1 = 99 = 11 \cdot 9$ → true

(c) $50 - 22 = 28 = 4 \cdot 7$ → true

(d) $45 - 12 = 33$, not a multiple of 10 → false (remainders 5 and 2)


3. Adding and multiplying: reduce whenever you like

You may take the remainder before, during or after a calculation; the answer is the same. That is what makes huge numbers manageable: you never need to write down anything bigger than $n^2$.

Worked example. $1234 \cdot 5678 \bmod 10$. Instead of $7{,}006{,}652$, take the last digits: $4 \cdot 8 = 32 \equiv 2$.

Worked example with negatives. $98 \cdot 99 \bmod 100$. Modulo 100, $98 \equiv -2$ and $99 \equiv -1$, so the product is $(-2)(-1) = 2$. No multiplication of large numbers needed.

Exercise 3.1. (a) $(23 + 19) \bmod 7$, (b) $13 \cdot 17 \bmod 11$, (c) $99 \cdot 99 \bmod 100$, (d) $7 \cdot 24 \bmod 7$.

Answer

(a) $23 \equiv 2$, $19 \equiv 5$, $2 + 5 = 7 \equiv$ 0

(b) $13 \equiv 2$, $17 \equiv 6$, $2 \cdot 6 = 12 \equiv$ 1 (check: $221 = 20 \cdot 11 + 1$)

(c) $(-1)(-1) = $ 1

(d) 0: any multiple of 7 is 0 on a 7-clock.

Multiplication tables on a clock

Modulo 5 (a prime):

×01234
000000
101234
202413
303142
404321

Every row except 0 contains every number exactly once, so every row contains a 1. Modulo 6 (not a prime) that goes wrong:

×012345
0000000
1012345
2024024
3030303
4042042
5054321

Here $2 \cdot 3 = 6 \equiv 0$: two numbers that are not zero multiply to zero. Rows 2, 3 and 4 have no 1 at all.

Exercise 3.2. Look at both tables. Which numbers have a 1 in their row modulo 5? And modulo 6? What do the “bad” numbers modulo 6 have in common with 6?

Answer

Modulo 5: 1, 2, 3, 4 (all of them; $2 \cdot 3 \equiv 1$, $4 \cdot 4 \equiv 1$).

Modulo 6: only 1 and 5. The bad numbers 2, 3, 4 share a factor with 6 (2 or 3). That is the rule of the next section.


4. Dividing = multiplying by the inverse

The inverse of $a$ modulo $n$, written $a^{-1}$, is the number with $a \cdot a^{-1} \equiv 1 \pmod n$. It exists exactly when $\gcd(a, n) = 1$ (they share no factor). When $n$ is a prime, every number from 1 to $n - 1$ has one, which is why cryptography loves primes. More in Prime numbers.

For a small modulus you can just try: the inverse of 3 modulo 7? $3 \cdot 1 = 3$, $3 \cdot 2 = 6$, $3 \cdot 3 = 9 \equiv 2$, $3 \cdot 4 = 12 \equiv 5$, $3 \cdot 5 = 15 \equiv 1$. Found: 5.

Exercise 4.1. Find by trying (or say why it does not exist):

(a) $2^{-1} \bmod 7$ (b) $3^{-1} \bmod 11$ (c) $5^{-1} \bmod 12$ (d) $4^{-1} \bmod 9$ (e) $6^{-1} \bmod 9$ (f) $3^{-1} \bmod 10$

Answer

(a) 4 ($2 \cdot 4 = 8 \equiv 1$)

(b) 4 ($3 \cdot 4 = 12 \equiv 1$)

(c) 5 ($5 \cdot 5 = 25 = 2 \cdot 12 + 1$). A number can be its own inverse.

(d) 7 ($4 \cdot 7 = 28 = 3 \cdot 9 + 1$)

(e) does not exist: $\gcd(6, 9) = 3$. Multiples of 6 modulo 9 are only 0, 6, 3.

(f) 7 ($3 \cdot 7 = 21 \equiv 1$)

Exercise 4.2. Write down all inverses modulo 7. Do you see a pattern?

Answer
$a$123456
$a^{-1}$145236

They come in pairs (2↔4, 3↔5), and only $1$ and $6 \equiv -1$ are their own inverse. Modulo a prime that is always the case.

The extended Euclidean algorithm

Trying does not work for 600-digit numbers. The extended Euclidean algorithm finds the inverse in a few steps, even then. Here is the one from RSA in part 1: the inverse of $17$ modulo $2668$.

Step 1: divide repeatedly (each time: the divisor becomes the new number, the remainder the new divisor):

$$\begin{aligned} 2668 &= 156 \cdot 17 + 16 \\ 17 &= 1 \cdot 16 + 1 \\ 16 &= 16 \cdot 1 + 0 \end{aligned}$$

The last non-zero remainder is $1$, so $\gcd(2668, 17) = 1$ and the inverse exists.

Step 2: walk back up, writing 1 as a combination of 2668 and 17:

$$\begin{aligned} 1 &= 17 - 1 \cdot 16 \\ &= 17 - 1 \cdot (2668 - 156 \cdot 17) \\ &= 157 \cdot 17 - 1 \cdot 2668 \end{aligned}$$

Modulo 2668 the last term vanishes: $157 \cdot 17 \equiv 1$. So $17^{-1} = $ 157, the private RSA exponent from part 1.

Exercise 4.3. Use the algorithm to find (a) $7^{-1} \bmod 40$, (b) $11^{-1} \bmod 26$, (c) $10^{-1} \bmod 17$ (used in the elliptic-curve example of part 1).

Answer (a)$$\begin{aligned} 40 &= 5 \cdot 7 + 5 \\ 7 &= 1 \cdot 5 + 2 \\ 5 &= 2 \cdot 2 + 1 \end{aligned}$$

Back up:

$$\begin{aligned} 1 &= 5 - 2 \cdot 2 = 5 - 2(7 - 5) = 3 \cdot 5 - 2 \cdot 7 \\ &= 3(40 - 5 \cdot 7) - 2 \cdot 7 = 3 \cdot 40 - 17 \cdot 7 \end{aligned}$$

So $-17 \cdot 7 \equiv 1$, and $-17 \bmod 40 = $ 23. Check: $7 \cdot 23 = 161 = 4 \cdot 40 + 1$ ✓

Answer (b)

$26 = 2 \cdot 11 + 4$, $11 = 2 \cdot 4 + 3$, $4 = 1 \cdot 3 + 1$.

$1 = 4 - 3 = 4 - (11 - 2 \cdot 4) = 3 \cdot 4 - 11 = 3(26 - 2 \cdot 11) - 11 = 3 \cdot 26 - 7 \cdot 11$.

So $11^{-1} \equiv -7 \equiv$ 19. Check: $11 \cdot 19 = 209 = 8 \cdot 26 + 1$ ✓

Answer (c)

$17 = 1 \cdot 10 + 7$, $10 = 1 \cdot 7 + 3$, $7 = 2 \cdot 3 + 1$.

$1 = 7 - 2 \cdot 3 = 7 - 2(10 - 7) = 3 \cdot 7 - 2 \cdot 10 = 3(17 - 10) - 2 \cdot 10 = 3 \cdot 17 - 5 \cdot 10$.

So $10^{-1} \equiv -5 \equiv$ 12. Check: $120 = 7 \cdot 17 + 1$ ✓


5. Solving equations on a clock

$3x \equiv 4 \pmod 7$: “dividing by 3” means multiplying both sides by $3^{-1} = 5$:

$$x \equiv 4 \cdot 5 = 20 \equiv 6 \pmod 7 \qquad\text{check: } 3 \cdot 6 = 18 \equiv 4 \;✓$$

Exercise 5.1. Solve:

(a) $5x + 2 \equiv 9 \pmod{11}$

(b) $4x \equiv 6 \pmod{10}$

(c) $2x \equiv 3 \pmod 6$

Answer

(a) $5x \equiv 7$. $5^{-1} \bmod 11 = 9$ ($45 = 4 \cdot 11 + 1$). $x \equiv 7 \cdot 9 = 63 \equiv$ 8. Check: $5 \cdot 8 + 2 = 42 = 3 \cdot 11 + 9$ ✓

(b) 4 has no inverse modulo 10, yet there are solutions: $x = 4$ and $x = 9$ ($16 \equiv 6$, $36 \equiv 6$). Without an inverse an equation can have several solutions…

(c) …or none: $2x$ is always even, and on a 6-clock even stays even. No solution. This is why cryptography works modulo primes, where every equation $ax \equiv b$ with $a \not\equiv 0$ has exactly one answer.


6. Powers on a clock and Fermat’s little theorem

Powers repeat on a clock. Look at the powers of 3 modulo 7:

$k$12345678
$3^k \bmod 7$32645132

At $k = 6$ it hits 1, and from there it repeats. That is no coincidence:

Fermat’s little theorem. If $p$ is a prime and $a$ is not a multiple of $p$, then $a^{p-1} \equiv 1 \pmod p$.

So exponents can be reduced modulo $p - 1$ (not modulo $p$!). This is the heart of RSA; the proof is in Prime numbers.

Worked example. $3^{100} \bmod 7$: $100 = 16 \cdot 6 + 4$, so $3^{100} = (3^6)^{16} \cdot 3^4 \equiv 1 \cdot 81 \equiv 4$.

Exercise 6.1. (a) $2^{1000} \bmod 13$ (b) $5^{22} \bmod 23$ (c) $10^{6} \bmod 7$

Answer

(a) $p - 1 = 12$, $1000 = 83 \cdot 12 + 4$, so $2^{1000} \equiv 2^4 = 16 \equiv$ 3

(b) $22 = p - 1$, so 1 without calculating anything. (In the table of part 1, $5^{22}$ is indeed 1.)

(c) 1, again Fermat with $p = 7$.


7. You already use this every day

Weekdays. 29 November 2026 is a Sunday. Which day is it 100 days later?

Answer

$100 \bmod 7 = 2$, so two days after Sunday: Tuesday (9 March 2027).

Hours. It is 15:00. What time is it 50 hours later?

Answer

$(15 + 50) \bmod 24 = 65 \bmod 24 = $ 17:00.

IBAN. A bank account number is checked with clock arithmetic modulo 97. Move the first four characters to the end, replace letters by numbers (A = 10, B = 11, …, Z = 35), and the result must be $\equiv 1 \pmod{97}$. For the standard example GB82 WEST 1234 5698 7654 32:

WEST12345698765432GB82
→ 3214282912345698765432161182

$3214282912345698765432161182 \bmod 97 = 1$ ✓. A typo in one digit always changes the remainder, so your bank app catches it before any money moves. The check digits “82” are calculated as $98 - (\text{the number with } 00 \text{ in their place} \bmod 97)$.

ISBN-10. The digits $d_1 \dots d_{10}$ of an old book number satisfy $10 d_1 + 9 d_2 + \dots + 1 \cdot d_{10} \equiv 0 \pmod{11}$. The first nine digits of one book are 0-306-40615-?. What is the last digit?

Answer

$10 \cdot 0 + 9 \cdot 3 + 8 \cdot 0 + 7 \cdot 6 + 6 \cdot 4 + 5 \cdot 0 + 4 \cdot 6 + 3 \cdot 1 + 2 \cdot 5 = 130$, and $130 \bmod 11 = 9$. We need $9 + d_{10} \equiv 0$, so $d_{10} = $ 2: ISBN 0-306-40615-2. (When the answer is 10 the book gets an “X”.)


Check yourself in Python

17 % 5                 # 2
-3 % 12                # 9   (Python gives the mathematical remainder)
pow(17, -1, 2668)      # 157 the inverse
pow(6, -1, 9)          # ValueError: base is not invertible
pow(2, 1000, 13)       # 3
import math; math.gcd(6, 9)   # 3
int("3214282912345698765432161182") % 97   # 1 (IBAN)

Next: Easy forward, hard backward: exercises →