Topic · Foundations

Modular arithmetic

Every cipher you have ever used runs on the arithmetic of a clock. Numbers wrap around, information gets hidden, and reversing the wrap is where security begins.

1. Wrapping numbers around a clock

A twelve-hour clock is the friendliest piece of mathematics most people already know. Four hours after ten o'clock is two o'clock — not fourteen. The numbers ran off the end and wrapped back to the start.

That is all modular arithmetic is. We fix a modulus — the size of the clock face — and every number is replaced by its remainder after dividing by that modulus.

10 + 4 = 14  →  14 mod 12 = 2
25 mod 7 = 4   (because 25 = 3×7 + 4)
100 mod 9 = 1

The modulus can be any positive integer. Cryptography uses enormous ones — clock faces with hundreds of digits — but the rule never changes.

2. Congruence: when two numbers agree

Two numbers are congruent modulo n when they leave the same remainder. We write it with three bars:

17 ≡ 5 (mod 12)   both leave remainder 5
38 ≡ 3 (mod 7)   both leave remainder 3

Congruence behaves like equality in the ways that matter: you can add, subtract and multiply both sides freely and the relationship survives. That reliability is what lets us do serious algebra inside a finite world.

What you cannot do casually is divide — and that restriction turns out to be the door through which all of public-key cryptography walks.

3. Modular inverses — division, carefully

Dividing by 3 normally means multiplying by ⅓. In modular arithmetic there are no fractions, so instead we look for a whole number that undoes multiplication by 3. That number is the modular inverse.

3 × 4 = 12 ≡ 1 (mod 11)
so 4 is the inverse of 3 modulo 11

An inverse of a modulo n exists only when a and n share no common factor — that is, when gcd(a, n) = 1. The extended Euclidean algorithm finds it quickly.

This is not a curiosity. In RSA the private exponent d is precisely the modular inverse of the public exponent e. Finding that inverse is generating the private key.

4. Fermat's little theorem

One result does more work in cryptography than almost any other. If p is prime and a is not a multiple of it:

ap−1 ≡ 1 (mod p)

Test it: 2⁶ = 64, and 64 mod 7 = 1. ✓

This gives a fast way to detect composite numbers without factoring them — the foundation of the primality tests used to generate cryptographic primes. It also explains why RSA decryption returns the original message: the exponents are chosen so the round trip lands exactly on a power that reduces to 1.

5. Why cryptography needs the wrap

Ordinary arithmetic leaks information. Larger inputs give larger outputs, so an attacker can narrow things down by size alone.

Wrapping destroys that. Once numbers fold around a huge modulus, the output tells you almost nothing about the input. Modular exponentiation is easy to compute forwards and — as far as anyone knows — infeasible to reverse, which is exactly the asymmetry the discrete logarithm problem is built on.

See it move

The interactive clock in the Learn hub lets you set a modulus and watch numbers wrap in real time.

Open the interactive lesson →

Frequently asked questions

What is modular arithmetic in simple terms?
It is arithmetic on a clock face. You pick a modulus, and every number is replaced by its remainder after dividing by it, so numbers wrap around instead of growing forever.
What does mod mean in maths?
'a mod n' is the remainder when a is divided by n. For example 25 mod 7 = 4, because 25 = 3x7 + 4.
Why does cryptography use modular arithmetic?
Wrapping hides the size of the input, and it makes operations that are easy forwards but extremely hard to reverse. That one-way asymmetry is what public-key cryptography is built on.
What is a modular inverse?
A number that undoes multiplication, since modular arithmetic has no fractions. The inverse of a modulo n exists only when a and n share no common factor.

Keep going

Prime numbers

Why cryptography is obsessed with primes, and how enormous ones are found.

Read more →

Discrete logarithms

The one-way problem that secures key exchange.

Read more →

How RSA works

Modular arithmetic put to work in the most famous cipher of all.

Read more →