Modular Arithmetic
August 25, 20269 min readbeginner
If you add eight hours to nine o'clock, you get five o'clock, not seventeen o'clock. The clock face has only twelve numbers on it, so anything that goes past twelve "wraps around"…
If you add eight hours to nine o'clock, you get five o'clock, not seventeen o'clock. The clock face has only twelve numbers on it, so anything that goes past twelve "wraps around" and starts again from one. That kind of arithmetic, where the answer cycles back to the start once it gets too big, is called modular arithmetic. It is the engine that runs almost every modern cryptosystem. This note builds it up properly.
01.The clock face
The clock face has the numbers through arranged in a circle. Walking around the circle and counting hours has a built-in wrap-around: every twelve hours brings you back to where you started. So the answer to "what time is it eight hours after nine?" is , not , because the hand has gone past and is now four steps further on.
A cleaner way to describe what is happening: take the ordinary integer answer () and subtract until the result lands in the range . One subtraction is enough here, giving .
Mathematically, it is even cleaner to use the range instead of : it makes the arithmetic uniform, and matches what computers do. With that convention, "" on a -clock means " steps past noon" and "" means "noon" or "midnight". The wrap-around point is the modulus , which is identified with . This convention is what mathematicians use everywhere, and we will use it from here on.
02.The mod operator
For any integer and any positive integer , the expression
is the remainder you get when you divide by . The remainder is the unique number in the range that you can subtract from to land on a multiple of .
Some examples with :
- , because .
- , because .
- , because .
- , because .
For negative integers, the rule is the same: the remainder must be non-negative and strictly less than . So you keep adding to the negative number until it falls into the right range.
- , because , which is in .
- , because .
- , because .
It is worth doing a few of these by hand. The convention "remainder is in " is the only thing that distinguishes the mathematician's from what some programming languages do. Some languages return negative remainders for negative inputs. Whenever the math says , take it to mean the non-negative remainder.
03.The five-clock, in full
Let . Every integer falls into exactly one of five categories, depending on its remainder modulo :
| Remainder | Integers in this class |
|---|---|
Every integer is somewhere in this table. No integer appears in more than one row. The five rows partition into five disjoint pieces, called residue classes modulo . The names for the classes are simply . Working "modulo " means treating each whole row as a single thing, and only caring about which row a number belongs to.
This is the right picture: not five numbers, but five classes of numbers, each class containing infinitely many integers that all behave identically once we agree to ignore everything except the remainder.
04.Congruence: the symbol
Two integers and are congruent modulo , written
if they have the same remainder when divided by . Equivalently (and often more useful), exactly when divides their difference, .
Examples with :
- , because both and have remainder , or equivalently because .
- , because .
- , because .
- , because .
The relation is the right way to write "is in the same residue class as". It behaves like equality in many ways: it is reflexive (), symmetric (), and transitive ( and imply ). For that reason it is called an equivalence relation. The residue classes are exactly its equivalence classes.
The notation "" sits at the end of the line because it modifies the entire equation. It is not part of either side. The line is read as " and are in the same residue class, where the residue is taken modulo ".
05.Reduction commutes with arithmetic
This is the most important fact in the whole note, and it is what makes modular arithmetic computationally cheap. The fact is:
If and , then
Said in plain language: if you replace any number in an arithmetic expression by another number in the same residue class, the answer is in the same residue class as before. So when you only care about the answer modulo , you can reduce your inputs modulo at any point, in any order, without changing the result.
A worked example with . Compute .
The slow, honest way: , then , then .
The fast way, using the commute-with-reduction fact: reduce each input modulo first.
- ,
- ,
- .
So .
Same answer. Far smaller intermediate values. In hardware, this is the difference between a -bit multiplier and a much narrower one. In ML-KEM, and the natural data path is bits wide, even though the polynomial coefficients in intermediate calculations could in principle grow much larger.
The proof of the commute-with-reduction property is short and clean. If , then for some integer . Similarly, for some integer . Then
For multiplication,
and the last three terms are all multiples of , so they vanish modulo . So . That is the entire argument.
Tables for
Because the residues mod are just , we can write the entire addition and multiplication tables explicitly.
Addition modulo :
Multiplication modulo :
Read entries off the tables to convince yourself the answers match the rules. For instance, : the ordinary product is , and , which matches the entry in row , column . The bottom-right of the multiplication table is , again matching.
A small thing worth noticing in the multiplication table: every row past the zero row contains every non-zero residue exactly once. That is a property special to prime moduli, and it is the property that makes a field rather than only a ring.
07.What you carry forward
Three things from this note will appear constantly.
The mod operator produces the remainder in the range . The congruence symbol says "in the same residue class". And the commute-with-reduction property lets you reduce inputs modulo at any stage of an arithmetic computation without changing the final answer modulo .
The next note packages all of this into a named structure: , the ring of integers modulo . The tables you have just looked at are the addition and multiplication tables of .