Modular Arithmetic Basics

The math behind every asymmetric cryptosystem, starting from something you already use every day: the clock. Everything here is interactive, so change the numbers and see what happens.

1

Arithmetic on a clock

It's 9 o'clock. What time will it be in 5 hours? In the States, nobody says 14. You go round the clock and land on 2. That's modular arithmetic: counting on a loop instead of a line. The number of positions on the loop is the modulus; on a 12-hour clock it's 12.

On a 24-hour clock, used in much of the world and by the military, 9:00 plus 5 hours really is 14:00. That clock is a loop too, just a longer one, so the wrap-around comes later: 14:00 plus 15 hours is 29, which lands on 5:00, because 29 mod 24 = 5. Same idea, different modulus.

Mathematicians start their clocks at 0, so a 12-hour clock has the positions 0 to 11 (the 24-hour clock already starts at 00:00). Try different clocks below. With a modulus of 7, the positions are the days of the week; with 2, every number is just “even” (0) or “odd” (1).

2

What “mod” means

a mod n is the remainder when you divide a by n. 14 ÷ 12 is 1 with 2 left over, so 14 mod 12 = 2. The “1” counts the full laps of the clock, and the remainder is where you end up.

Every whole number can be written as a = q × n + r, with r between 0 and n − 1. The remainder r is the answer. Negative numbers work too: going back 3 hours from 0 lands on 9, so −3 mod 12 = 9. (Some calculators and programming languages answer −3 instead; on a clock that's the same position.)

When two numbers land on the same position, we say they're congruent and write 14 ≡ 2 (mod 12), read “14 is congruent to 2, mod 12”. Every ciphertext, message and shared secret in the worksheets is a remainder like this.

Check yourself
  1. What is 100 mod 7?
    Show answer

    2, because 100 = 14 × 7 + 2.

  2. What is −1 mod 12?
    Show answer

    11. One step back from 0 on a 12-position clock lands on 11.

3

Clocks, calendars and odometers

You already do modular arithmetic all the time:

  • Hours are mod 12 or mod 24: 22:00 plus 5 hours is 27 mod 24 = 3:00.
  • Days of the week are mod 7. Number them Sunday = 0 to Saturday = 6, and “100 days after a Tuesday” is (2 + 100) mod 7 = 4, a Thursday.
  • Months are mod 12: 5 months after November (month 11) is 16 mod 12 = 4, April.
  • Odometers are mod 1,000,000: one more mile after 999,999 rolls over to 000,000.
  • Compass bearings are mod 360: turning 90° right from 300° gives 390 mod 360 = 30°.
  • The last digit of any number is that number mod 10, and even or odd is mod 2.
4

Multiplying, and when you can divide

Adding, subtracting and multiplying all work on a clock, and you can take the remainder whenever you like: before, during or after. (17 × 23) mod 10 is the same as (7 × 3) mod 10 = 1, which is why a computer can multiply 45-digit numbers mod n without them ever getting bigger than n2.

Division is different. Dividing by a means multiplying by a number x with a × x ≡ 1, called the inverse of a. In the table below, a row with a 1 in it has an inverse. Look at mod 10: the rows for 2, 4, 5, 6 and 8 never reach 1, because those numbers share a factor with 10. Now switch to mod 7: every row (except 0) has a 1, because 7 is prime. That's why so many cryptosystems work mod a prime.

Check yourself
  1. Mod 9, which numbers from 1 to 8 have an inverse?
    Show answer

    1, 2, 4, 5, 7 and 8: the ones that share no factor with 9. (3 and 6 share the factor 3.)

  2. What is the inverse of 2, mod 9?
    Show answer

    5, because 2 × 5 = 10 = 9 + 1. Switch the table to mod 9 and look along row 2.

5

Powers that go round in circles

Keep multiplying by the same number and the remainders eventually repeat. Powers of 3 mod 7 go 3, 2, 6, 4, 5, 1, and then start again. The length of the loop is the order of 3. Here, 3 visits every non-zero position before repeating, so it's called a generator.

Going forwards is easy: 35 mod 7 = 5. Going backwards, from “5” to “which power?”, is the discrete logarithm. Here you can just read it off the list. For a 45-digit prime the loop can be about 1045 steps long, far too long to list. In general that's what makes ElGamal, Diffie–Hellman and their cousins secure. Puzzles get around it by choosing primes with a special weakness; section 11 shows what it is.

Check yourself
  1. What is the order of 2, mod 11? Is 2 a generator?
    Show answer

    The powers go 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, so the order is 10. That covers every number from 1 to 10, so yes, 2 is a generator mod 11.

  2. Using the same list, which power of 2 gives 9, mod 11?
    Show answer

    The 6th: 26 = 64 = 5 × 11 + 9. That backwards question is a discrete logarithm.

6

Fermat, Euler and φ

Fermat's little theorem. If p is prime and a isn't a multiple of p, then

a^(p−1) ≡ 1 (mod p) example: 3^6 = 729 = 104 × 7 + 1

So every loop of powers mod a prime closes within p − 1 steps, and its length divides p − 1. Try it in section 5: every order mod 7 is 1, 2, 3 or 6.

Euler's φ (“phi”). φ(n) counts the numbers from 1 to n that share no factor with n. For n = 15 they are 1, 2, 4, 7, 8, 11, 13 and 14, so φ(15) = 8. Two cases come up again and again:

φ(p) = p − 1 (p prime: every smaller number qualifies) φ(pq) = (p − 1)(q − 1) (p and q two different primes)

Why the second one: from 1 to pq, the numbers that do share a factor are the q multiples of p and the p multiples of q, and pq itself is counted in both lists. That leaves pq − q − p + 1 = (p − 1)(q − 1). For 15: 15 − 5 − 3 + 1 = 8.

Euler's theorem extends Fermat's to any modulus. If a shares no factor with n, then

a^φ(n) ≡ 1 (mod n) example: φ(15) = 8, and 2^8 = 256 = 17 × 15 + 1

Why RSA decryption works. RSA picks d so that e × d = 1 + k × φ(n) for some whole number k. Then

c^d = (m^e)^d = m^(1 + k·φ(n)) = m × (m^φ(n))^k ≡ m × 1^k = m (mod n)

Finding d needs φ(n), and finding φ(n) needs the primes p and q. That's why factoring breaks RSA.

Check yourself
  1. What is φ(21)? (21 = 3 × 7)
    Show answer

    (3 − 1)(7 − 1) = 12.

  2. Without multiplying it out, what is 512 mod 21?
    Show answer

    1, by Euler’s theorem: 5 shares no factor with 21, and 12 = φ(21).

7

Primes and factoring

A prime has no factors except 1 and itself: 2, 3, 5, 7, 11, 13, … Every whole number splits into primes in exactly one way, for example 360 = 23 × 32 × 5.

The greatest common divisor, gcd(a, b), is the biggest number that divides both. When it's 1, the numbers “share no factor” (they're coprime), and that's the condition behind inverses (section 4) and Euler's theorem (section 6). Euclid's algorithm finds it without factoring anything; section 8 shows the steps.

φ from the factors. Once you know the primes, φ is quick: multiply n by (1 − 1/p) for each different prime p. For 360: 360 × ½ × ⅔ × ⅘ = 96. With two primes this is the (p − 1)(q − 1) from section 6.

Why factoring is hard. The simplest method tries dividing by every number up to √n. That's instant for the 15-digit numbers below, but a 45-digit number would need about 1022 tries. Cleverer methods, like the quadratic sieve the worksheets use, split 45 digits in about a second. They still slow down steeply: the largest RSA challenge number factored so far, 250 digits, took about 2,700 CPU-core-years in 2020, and real keys are around 600 digits.

8

Two tools: fast powers and inverses

Square and multiply

Working out 7560 mod 561 by multiplying 7 by itself 559 times is slow, and a 45-digit exponent would take forever. Instead, square repeatedly (7, 72, 74, 78, …, taking the remainder each time), and multiply together the squares matching the 1s in the exponent written in binary. A 45-digit exponent needs only about 150 squarings. Big numbers work in these boxes.

Show every step

The extended Euclidean algorithm

To find an inverse, run Euclid's algorithm for the greatest common divisor: divide, keep the remainder, repeat. If the remainders reach 1, the inverse exists. Alongside each remainder the table keeps a multiplier, chosen so that

remainder ≡ multiplier × a (mod n)

Each new multiplier is the one from two rows up, minus “goes in” times the one from the row above. Start with 0 for n and 1 for a. On the row where the remainder is 1, the multiplier is the inverse. If it's negative, add n: for 7 mod 40 that row says −17, and −17 + 40 = 23. This is how RSA's private exponent d is found from e.

9

Two clocks at once

Picture a 3-hour clock and a 5-hour clock ticking together. Which count shows 2 on the first and 3 on the second? Counting up: 8 (8 mod 3 = 2, 8 mod 5 = 3). Because 3 and 5 share no factor, the pair of positions repeats only every 3 × 5 = 15 ticks, so 8 is the only answer between 0 and 14.

That's the Chinese Remainder Theorem (CRT): knowing a number modulo several moduli that share no factors pins it down modulo their product. Cryptosystems use it to split one big calculation into smaller ones, mod p and mod q, and glue the answers back together.

Check yourself
  1. Which x from 0 to 35 has x mod 4 = 3 and x mod 9 = 5?
    Show answer

    23: 23 = 5 × 4 + 3 and 23 = 2 × 9 + 5. Put 4, 3, 9, 5 in the boxes above to check.

10

Square roots on a clock

Which numbers x have x2 ≡ a (mod n)? Mod 7, squaring 1 to 6 gives 1, 4, 2, 2, 4, 1, so only 1, 2 and 4 have square roots. Those are called quadratic residues (squares, for short). Mod an odd prime, exactly half the non-zero numbers are squares, and each has two roots, x and p − x (on the clock, x and its mirror image).

Euler's criterion tells squares from non-squares in one power:

a^((p−1)/2) mod p = 1 if a is a square a^((p−1)/2) mod p = p − 1 if it isn't (that's −1) mod 7: 2^3 = 8 ≡ 1 so 2 is a square 3^3 = 27 ≡ 6 ≡ −1 so 3 isn't

The shortcut for primes that leave remainder 3 when divided by 4 (like 7, 11, 19, 23):

x = a^((p+1)/4) mod p √2 mod 7: 2^((7+1)/4) = 2^2 = 4, and 4^2 = 16 ≡ 2 ✓

It works because x2 = a(p+1)/2 = a × a(p−1)/2 ≡ a × 1 = a, using Euler's criterion. Other primes need a longer method (Tonelli–Shanks), which the worksheets do for you.

Mod a product of two primes, a square has four roots. Solve mod each prime, then glue the pieces together with the CRT (section 9). For x2 ≡ 1 (mod 15): mod 3 the roots are ±1, mod 5 they're ±1, and the four combinations are:

mod 3mod 5x mod 15
111
144
2111
2414

Check: 42 = 16, 112 = 121 and 142 = 196 all leave remainder 1 mod 15. Anyone can square, but finding all four roots needs the two primes. A system that encrypts by squaring rests on exactly that.

Check yourself
  1. Which numbers are squares mod 11?
    Show answer

    1, 3, 4, 5 and 9 (from 12, 52, 22, 42, 32, and their mirror images).

  2. Use the shortcut to find a square root of 3 mod 11.
    Show answer

    11 leaves remainder 3 when divided by 4, so x = 3(11+1)/4 = 33 = 27 ≡ 5. Check: 52 = 25 = 2 × 11 + 3. The other root is 11 − 5 = 6.

11

Why these discrete logs break

Finding x with gx ≡ h (mod p) is hard in general: the best general methods take about √p steps, around 1022 for a 45-digit prime. But the loop of powers of g has a length that divides p − 1 (section 6), and when p − 1 splits into small primes, the Pohlig–Hellman method solves the problem one small prime at a time:

  1. For each prime q dividing p − 1, raise both g and h to the power (p − 1)/q. That pushes both into a loop only q long, where only x mod q matters.
  2. Find x mod q by searching that short loop.
  3. Glue the answers together with the CRT (section 9).

Worked example. Find x with 3x ≡ 22 (mod 31). Here p − 1 = 30 = 2 × 3 × 5.

q330/q mod 312230/q mod 31The short loopx mod q
230301, 301
32551, 25, 52
51681, 16, 8, 4, 22

Read each answer from the loop's position: 2215 ≡ 30 is step 1 of the first loop, 5 is step 2 of the second, 8 is step 2 of the third. Then the CRT: x ≡ 1 (mod 2), x ≡ 2 (mod 3), x ≡ 2 (mod 5) gives x = 17, and indeed 317 mod 31 = 22.

What decides the difficulty is the largest prime factor of p − 1, not the size of p. A 45-digit prime whose p − 1 is built from primes of 13 digits or fewer needs only a few million steps per piece. Real systems avoid this by using “safe primes” p = 2q + 1 with q prime, so one piece is almost as big as p and the trick gains nothing. The worksheets show the pieces for any prime you give them.

12

Put it together: a tiny RSA

Every step of RSA, small enough to check by hand. Work each one out (the widgets above help), then open the answer.

Make the keys, then send a message
  1. Take the primes p = 11 and q = 23. What is n = p × q?
    Show answer

    253.

  2. What is φ(n) = (p − 1)(q − 1)? (Section 6.)
    Show answer

    10 × 22 = 220.

  3. Choose e = 3. Does it share a factor with 220? (Section 7's gcd box.)
    Show answer

    No: gcd(3, 220) = 1, so e = 3 works.

  4. Find d = 3−1 mod 220. (Section 8's inverse box.)
    Show answer

    147, because 3 × 147 = 441 = 2 × 220 + 1.

  5. Encrypt m = 100: c = 1003 mod 253. (Section 8's power box.)
    Show answer

    144.

  6. Decrypt: what is 144147 mod 253?
    Show answer

    100, the message again.

  7. Now be the eavesdropper. You only know n = 253, e = 3 and c = 144. What do you do first?
    Show answer

    Factor 253 (section 7's box): 11 × 23. From there, steps 2, 4 and 6 give you the message. That's everything the RSA worksheet does, with bigger numbers.

13

How the worksheets use all this

  • Remainders (sections 1–3): every ciphertext, message and shared secret is a remainder after dividing by the public modulus.
  • Inverses (sections 4 and 8): private exponents, and “dividing out” a shared secret.
  • Powers, Fermat and Euler (sections 5, 6 and 8): encrypting and decrypting, and why decryption undoes encryption.
  • Factoring and φ (sections 6 and 7): the hard problem behind the systems built on a product of secret primes.
  • The CRT (section 9): splitting work into mod p and mod q, then gluing it back together.
  • Square roots (section 10): the systems that encrypt by squaring.
  • Discrete logarithms (sections 5 and 11): the hard problem behind the systems built on powers of a base, and why puzzle-sized ones break.
  • Turning numbers into text: see From Letters to Numbers.

Which system a puzzle uses, and which of these ideas it leans on, is for you to work out. Back to the worksheets.

Bonus

The calendar is a clock too

Days of the week are arithmetic mod 7, and John Conway turned that into a trick for finding the day of the week of any date in your head: the Doomsday rule. It has its own page, with the formulas, why each one works, a calculator and a practice game: Day of the Week.