Your teacher in Bhaktapur sets the class a game. Sita sits by the window and Ram by the door, and they have never planned anything together. By the end of the game they must both know the same secret number, one that nobody else in the room knows. The catch: they may only talk by calling out across the room, so every word either of them says, the whole class hears. No whispering, no notes, no meeting at the break.
It sounds impossible. Yet your phone wins this game every time it opens a website: before any message crosses the networks that anyone can listen to, the phone and the website agree on a secret key without ever sending it. Grade 9 Computing pictures this with padlocks. This topic opens the padlock and shows the arithmetic inside: remainders.
Guess first. Can Sita and Ram win the game?
Pick an answer to see why.
Try it: a sum that is hard to undo
Powers seem better. If Sita calls out “2 to the power of my number is 1,024”, the class has to find the power. But powers of 2 grow steadily, 2, 4, 8, 16, …, so the class can home in: 2¹⁰ = 1,024, done. That is what logarithms are for. A power is hard to undo only if its answers jump about, so that being close tells you nothing.
There is a way to make powers jump about: keep only the remainder. On a clock with 7 hours, numbered 0 to 6, a number lands on the hour it leaves over on dividing by 7. Take the powers of 3 and keep only the remainders on dividing by 7:
| Power | 3¹ | 3² | 3³ | 3⁴ | 3⁵ | 3⁶ |
|---|---|---|---|---|---|---|
| Value | 3 | 9 | 27 | 81 | 243 | 729 |
| Remainder on dividing by 7 | 3 | 2 | 6 | 4 | 5 | 1 |
The remainders jump about, 3, 2, 6, 4, 5, 1, visiting every hour from 1 to 6 once. Each comes from the last by multiplying by 3 and keeping the remainder (6 × 3 = 18 leaves 4), so you never need the big numbers.
Now try the slow direction.
On the 17-hour clock, which power of 3 lands on hour 2?
Pick an answer to see why.
That is the whole idea in small: forwards is a few multiplications, backwards is a walk round the clock.
Powers on a clock
Clock arithmetic
Counting round a clock and keeping only what is left over is modular arithmetic, and the number of hours is the modulus. In the Grades 6–8 topic What Day of the Week Will Your 100th Birthday Be?, the week is a clock of 7. Mathematicians write
and read it “3⁴ is congruent to 4, modulo 7”: 81 and 4 leave the same remainder on dividing by 7 (81 − 4 = 77 = 11 × 7).
Why you may take the remainder at every step
The table above threw the big numbers away. Is that safe? Take 23 × 19 = 437, which leaves 3 on dividing by 7. Now use only the remainders: 23 leaves 2, 19 leaves 5, and 2 × 5 = 10 leaves 3. The same.
It always works. Picture the product as a rectangle with sides (some sevens + 2) and (some sevens + 5), cut into four pieces. Three of them contain a whole number of sevens, so they land on hour 0; only the corner piece, 2 × 5, decides the remainder. The same holds for any clock, so in a chain of multiplications you may replace any number by its remainder at any step.
Big powers quickly: square and multiply
To find 3¹⁷ on a 31-hour clock, there is a faster way than multiplying by 3 again and again, from the laws of indices: squaring a power doubles it, since . Square again and again, taking the remainder each time:
| Power | 3¹ | 3² | 3⁴ | 3⁸ | 3¹⁶ |
|---|---|---|---|---|---|
| Remainder on dividing by 31 | 3 | 9 | 19 | 20 | 28 |
(9² = 81 leaves 19; 19² = 361 leaves 20; 20² = 400 leaves 28.) Then 3¹⁷ = 3¹⁶ × 3, so 28 × 3 = 84, which leaves 22. Four squarings and one multiplication, five in all, instead of sixteen.
This is square and multiply (or repeated squaring). Any power is a sum of 1, 2, 4, 8, 16, …, as in binary (22 = 16 + 4 + 2), so each binary digit of the power costs at most two multiplications. A power with 600 digits has about 2,000 binary digits: at most about 4,000 multiplications, a blink for a computer.
The slow way back
Going back, from 22 to “which power of 3?”, is called the discrete logarithm problem: a logarithm, because it asks for a power, and discrete, because on a clock the answers jump about instead of growing smoothly. The powers of 3 on the 31-hour clock go
3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21, 1
and 22 is the 17th. To find it, you walk the list. There are cleverer ways than walking every hop (below), but on a well-chosen clock even the best known is unimaginably slower than going forwards. Nobody has proved that no fast way exists: the whole internet bets on it.
On the 31-hour clock, 3¹⁶ leaves 28. What does 3³² leave?
Pick an answer to see why.
The trick: a secret nobody sent
First a picture people often use. Sita and Ram agree out loud on yellow paint. Each mixes in a secret color, red for Sita and blue for Ram, and holds up the mixture. Each then stirs their own secret color into the other’s mixture: both now have yellow + red + blue. The class saw yellow + red and yellow + blue, but paint can’t be unmixed. On a clock, mixing is raising to a power; unmixing is the discrete logarithm.
The game, with numbers
- Out loud, Sita and Ram agree on a clock, 31, and a base, 3.
- In secret, Sita picks a power, a = 17, and Ram picks b = 22.
- Out loud, Sita calls out , which on the 31-hour clock is 22. Ram calls out , which is 14.
- In secret, Sita raises Ram’s 14 to her own power 17. Ram raises Sita’s 22 to his own power 22. Both get 10.
The class, and any eavesdropper (Grade 9 Computing calls her Eve), heard 31, 3, 22 and 14. The secret 10 was never said. This is the Diffie–Hellman key exchange, published in 1976.
Why do both get the same number? Sita worked out and Ram . By the law of indices both are :
and since remainders can be taken at any step, the clock doesn’t change that. The class has and , but multiplying them gives , the wrong power. Nobody knows a way to make from them without first finding a or b.
How big is big enough?
On a 40-digit clock, Eve walking a billion powers a second would need around 3 × 10²³ years. Real clocks are far bigger, because Eve has better methods than walking. In 2015 the researchers behind an attack called Logjam prepared, in a week on several thousand computer cores, for one widely used 512-bit clock (about 155 digits), and could then find secret powers on it in about a minute each. They estimated that a 1,024-bit clock would cost around 100 million US dollars to prepare for, and recommended at least 2,048 bits, about 617 digits. Even then, Sita’s side is a few thousand multiplications.
Why the powers come back to 1
The widget showed something it didn’t explain: on a clock with a prime number of hours, every base comes back to 1, never later than one hop short of the number of hours. Why?
Take the 7-hour clock and the base 3. Multiply each hour from 1 to 6 by 3 and keep the remainder:
| Hour | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| × 3 lands on | 3 | 6 | 2 | 5 | 1 | 4 |
The same six hours, shuffled. They must be. None lands on 0, because 7 is prime and divides neither 3 nor any hour from 1 to 6, so it can’t divide their product. And if two hours x and y landed together, 7 would divide 3(x − y); since 7 doesn’t divide 3, it would have to divide x − y, impossible for two different hours from 1 to 6. (Both steps use one fact about primes: a prime that divides a product divides one of its factors, which follows from every number being built from primes in only one way, as in Is There a Biggest Prime Number?)
Multiply each row together: the bottom row is the same numbers reordered, so its product is the same:
The left side is , the right side 720. Their difference, , is a multiple of 7, and 7 doesn’t divide 720 = 1 × 2 × … × 6, so by the same fact it divides :
Nothing used anything special about 3 or 7, only that the clock is prime and the base isn’t a multiple of it. So for any prime p and any base a that isn’t a multiple of p:
This is Fermat’s little theorem, and it explains the widget. A base that first comes back after k hops repeats its cycle and must still be at 1 after p − 1 hops, so k divides p − 1. A base that visits every hour first is a generator (or primitive root) of the clock: 3 is one for the 31-hour clock, which is why Sita and Ram used it.
What does 3¹⁰⁰ leave on dividing by 7?
Pick an answer to see why.
From a shared secret to a padlock
Grade 9 Computing’s padlock, RSA (1977), lets anyone lock a message with Ram’s public numbers that only Ram can open. It rests on Fermat’s little theorem too. With toy primes:
Ram secretly picks two primes, 11 and 17, and multiplies them: 187. He works out (11 − 1) × (17 − 1) = 160, picks a locking power, 7, and an unlocking power that multiplies with it to one more than a multiple of 160: 7 × 23 = 161, so 23. He publishes 187 and 7, his open padlock, and keeps 23.
Sita’s message is the number 42. She locks it: on the 187-hour clock is 15, and she sends 15. Ram unlocks: on the same clock is 42.
Why? Ram has worked out . On the 11-hour clock, by Fermat, so . On the 17-hour clock, , so . A number 1 more than a multiple of 11 and of 17 is 1 more than a multiple of 187, since 11 and 17 share no factor. So , and : the message comes back.
To find 23, Eve needs 160, so the two primes. With 187 that takes a minute; a real clock is two primes each hundreds of digits long, and Grade 9 Computing’s The mathematics behind the padlock shows why finding them is hopeless. (Computers also use remainders on the two primes separately to make Ram’s side faster; How Many Eggs Were in the Basket? tells how.)
Where it breaks
A base that visits too few hours
On the 31-hour clock the base 2 visits only 5 hours, so whatever the secrets, the key is one of 5 numbers and Eve tries them all. Since a cycle divides p − 1, a clock where p − 1 is twice a prime has no short cycles except those of the bases 1 and p − 1 (lengths 1 and 2), which nobody would choose; every other base visits at least half the hours (the widget’s 40-digit clock is one).
The stranger in the middle
Suppose Eve can stop Sita’s and Ram’s calls and speak in their voices. She swaps keys with Sita, pretending to be Ram, and with Ram, pretending to be Sita, then reads everything and passes it on. The arithmetic is perfect; Diffie–Hellman alone just can’t tell who is calling. That is why a website also signs its numbers, and why your phone checks the signature against a certificate (Grade 9 Computing, HTTPS and certificates).
A new kind of computer
In 1994 the mathematician Peter Shor found a method by which a quantum computer, if one big enough could be built, would solve the discrete logarithm and find the primes behind RSA quickly. As of 2026 none comes close: the demonstrations so far have only handled tiny numbers, such as factoring 21, and even those were simplified using the answer known in advance. To be ready, in August 2024 the United States’ standards agency, NIST, published ML-KEM, a way for two parties to agree on a key over a public channel that rests on a different hard problem, and Google Chrome since version 131, with websites that support it, agrees its keys with ML-KEM and a Diffie–Hellman exchange (X25519, below) together.
Eve can intercept and replace Sita’s and Ram’s calls, not just listen. What protects them?
Pick an answer to see why.
A story from maths
Where this is used
Questions
★ What does 4³ leave on dividing by 7?
Pick an answer to see why.
Find the powers of 3 on the 11-hour clock, from 3¹ to 3¹⁰. When do they first come back to 1? Is 3 a generator of the 11-hour clock?
Answer
Sita and Ram agree in public on the 19-hour clock and the base 2. Sita’s secret power is 7, Ram’s is 11. What does each call out, and what key do they both get? Check that both sides agree.
Hint
Answer
★★ Eve hears that the clock is 13 and the base is 2, and Sita calls out 11. What is Sita’s secret power, between 1 and 12?
Pick an answer to see why.
Without a calculator, find the remainder when 2¹⁰⁰ is divided by 11, and when 5¹⁰⁰⁰ is divided by 13.
Hint
Answer
On the 31-hour clock, which bases visit only 5 hours before coming back to 1? Suppose Sita and Ram used one of them: how many different keys could they end up with? Why does that help Eve?
Hint
Answer
Check, by square and multiply, that Ram’s unlocking of Sita’s 15, 15²³ on the 187-hour clock, gives back 42.
Answer
Where to go next
- What Don’t Mathematicians Know About Prime Numbers? The primes that every padlock is built from, and the questions about them nobody can answer yet.
- How Many Eggs Were in the Basket? The Chinese remainder theorem: one number from its remainders on several clocks, and how it speeds up RSA.
- Can You Write a Message Only a Friend Can Read? (Grades 6–8) The old ciphers, where both friends must share a key first: the problem this topic solves.
- Is There a Biggest Prime Number? (Grades 6–8) Why primes never run out, and why every number is built from them in one way only.
- Grade 9 Computing, Secrets and Security: the padlock picture, digital signatures, HTTPS and end-to-end encryption, the uses of everything on this page.
- Grade 10 Math, Indices: the laws of indices that make .
Where these facts come from
- Diffie and Hellman’s 1976 paper (IEEE Transactions on Information Theory 22(6), pp. 644–654), Hellman’s view that it should be called Diffie–Hellman–Merkle, and their 2015 Turing Award: Wikipedia, “Martin Hellman”, read October 2026.
- The exchange as discrete logarithms, the paint picture, the man in the middle, Logjam (512-bit groups after a week on several thousand cores, then a minute each; about 100 million dollars for 1,024 bits; at least 2,048 bits recommended), Shor’s algorithm against it: Wikipedia, “Diffie–Hellman key exchange”, read October 2026.
- Merkle’s puzzles: the 1974 CS 244 project proposal at Berkeley and the 1978 Communications of the ACM paper: Wikipedia, “Merkle’s Puzzles”, read October 2026.
- Williamson’s exchange at GCHQ in 1974, declassified in 1997: Wikipedia, “Malcolm J. Williamson”, read October 2026.
- Fermat as a lawyer at the parlement of Toulouse (1601–1665; some sources say born 1607), his work sent in letters to friends: Wikipedia, “Pierre de Fermat”, read October 2026.
- Fermat’s letter of 18 October 1640 to Frénicle de Bessy, Euler’s proof of 1736, Leibniz’s unpublished one, the name: Wikipedia, “Fermat’s little theorem”, read October 2026. The shuffle proof is the standard one, written here in our own words.
- The history of square and multiply (Pingala c. 200 BCE as an antecedent, not a general method; al-Kashi’s Miftah al-Hisab presenting it as general and his own; al-Uqlidisi and al-Biruni using successive squaring in particular calculations): N. Aydin, M. K. Azarian, O. Khormali and G. Mtimet, “On the History of the Square and Multiply Algorithm”, arXiv:2606.00958 (2026), abstract read October 2026 (it calls Pingala’s binary counting “the earliest known antecedent” and says al-Kashi’s Miftah al-Hisab gives it “explicitly as a general computational method”). The Key of Arithmetic’s date, 1427 (another account says about 1425): Wikipedia, “Jamshīd al-Kāshī”, read October 2026.
- Shor’s algorithm (1994), and that as of 2026 demonstrations have handled only small numbers such as 21, compiled using the known answer: Wikipedia, “Shor’s algorithm”, read October 2026.
- ML-KEM, “used by two parties to establish a shared secret key over a public channel”, published 13 August 2024: NIST, FIPS 203, read October 2026.
- Chrome 131 switching its hybrid key exchange to ML-KEM768 + X25519: D. Adrian, D. Benjamin, B. Beck and D. O’Brien, “A new path for Kyber on the web”, Google Security Blog, 13 September 2024, read October 2026.
- TLS 1.3: “Static RSA and Diffie-Hellman cipher suites have been removed”, “all public-key based key exchange mechanisms now provide forward secrecy”: RFC 8446, section 1.2, read October 2026.
- X25519 in the Diffie–Hellman shape: RFC 7748, section 6.1, read October 2026.
- Every number on this page (the clock tables and cycles, the 31-hour exchange and its working, the widget’s three clocks, keys and multiplication counts, the 40-digit clock’s safe prime and generator, Eve’s 3 × 10²³ years, 617 digits for 2,048 bits, the RSA toy with 187, 160, 7, 23, 42 and 15, and every answer in the questions) was computed by a script (
checks/textbooks/math-further-9-12/09/tmp/answers.py).
