Chapter 1Finite Fields and Polynomial Rings

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

Figure 1
Figure 1. 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.

The clock face has the numbers 11 through 1212 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 55, not 1717, because the hand has gone past 1212 and is now four steps further on.

A cleaner way to describe what is happening: take the ordinary integer answer (9+8=179 + 8 = 17) and subtract 1212 until the result lands in the range {1,2,…,12}\{1, 2, \ldots, 12\}. One subtraction is enough here, giving 55.

Mathematically, it is even cleaner to use the range {0,1,…,11}\{0, 1, \ldots, 11\} instead of {1,2,…,12}\{1, 2, \ldots, 12\}: it makes the arithmetic uniform, and matches what computers do. With that convention, "55" on a 1212-clock means "55 steps past noon" and "00" means "noon" or "midnight". The wrap-around point is the modulus 1212, which is identified with 00. This convention is what mathematicians use everywhere, and we will use it from here on.

02.The mod operator

For any integer aa and any positive integer nn, the expression

a mod na \bmod n

is the remainder you get when you divide aa by nn. The remainder is the unique number in the range {0,1,2,…,n−1}\{0, 1, 2, \ldots, n - 1\} that you can subtract from aa to land on a multiple of nn.

Some examples with n=5n = 5:

  • 7 mod 5=27 \bmod 5 = 2, because 7=1⋅5+27 = 1 \cdot 5 + 2.
  • 13 mod 5=313 \bmod 5 = 3, because 13=2⋅5+313 = 2 \cdot 5 + 3.
  • 20 mod 5=020 \bmod 5 = 0, because 20=4⋅5+020 = 4 \cdot 5 + 0.
  • 0 mod 5=00 \bmod 5 = 0, because 0=0⋅5+00 = 0 \cdot 5 + 0.

For negative integers, the rule is the same: the remainder must be non-negative and strictly less than nn. So you keep adding nn to the negative number until it falls into the right range.

  • −3 mod 5=2-3 \bmod 5 = 2, because −3+5=2-3 + 5 = 2, which is in {0,1,2,3,4}\{0, 1, 2, 3, 4\}.
  • −7 mod 5=3-7 \bmod 5 = 3, because −7+5+5=3-7 + 5 + 5 = 3.
  • −10 mod 5=0-10 \bmod 5 = 0, because −10+5+5=0-10 + 5 + 5 = 0.

It is worth doing a few of these by hand. The convention "remainder is in {0,1,…,n−1}\{0, 1, \ldots, n-1\}" is the only thing that distinguishes the mathematician's mod⁡\operatorname{mod} from what some programming languages do. Some languages return negative remainders for negative inputs. Whenever the math says a mod na \bmod n, take it to mean the non-negative remainder.

03.The five-clock, in full

Let n=5n = 5. Every integer falls into exactly one of five categories, depending on its remainder modulo 55:

RemainderIntegers in this class
00…,−10,−5,0,5,10,15,…\ldots, -10, -5, 0, 5, 10, 15, \ldots
11…,−9,−4,1,6,11,16,…\ldots, -9, -4, 1, 6, 11, 16, \ldots
22…,−8,−3,2,7,12,17,…\ldots, -8, -3, 2, 7, 12, 17, \ldots
33…,−7,−2,3,8,13,18,…\ldots, -7, -2, 3, 8, 13, 18, \ldots
44…,−6,−1,4,9,14,19,…\ldots, -6, -1, 4, 9, 14, 19, \ldots

Every integer is somewhere in this table. No integer appears in more than one row. The five rows partition Z\mathbb{Z} into five disjoint pieces, called residue classes modulo 55. The names for the classes are simply 0,1,2,3,40, 1, 2, 3, 4. Working "modulo 55" 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 ≡\equiv

Two integers aa and bb are congruent modulo nn, written

a≡b(modn),a \equiv b \pmod{n},

if they have the same remainder when divided by nn. Equivalently (and often more useful), a≡b(modn)a \equiv b \pmod{n} exactly when nn divides their difference, n∣(a−b)n \mid (a - b).

Examples with n=5n = 5:

  • 7≡2(mod5)7 \equiv 2 \pmod 5, because both 77 and 22 have remainder 22, or equivalently because 5∣(7−2)=55 \mid (7 - 2) = 5.
  • 13≡3(mod5)13 \equiv 3 \pmod 5, because 5∣(13−3)=105 \mid (13 - 3) = 10.
  • −3≡2(mod5)-3 \equiv 2 \pmod 5, because 5∣(−3−2)=−55 \mid (-3 - 2) = -5.
  • 7≡−3(mod5)7 \equiv -3 \pmod 5, because 5∣(7−(−3))=105 \mid (7 - (-3)) = 10.

The relation ≡\equiv is the right way to write "is in the same residue class as". It behaves like equality in many ways: it is reflexive (a≡aa \equiv a), symmetric (a≡b⇒b≡aa \equiv b \Rightarrow b \equiv a), and transitive (a≡ba \equiv b and b≡cb \equiv c imply a≡ca \equiv c). For that reason it is called an equivalence relation. The residue classes are exactly its equivalence classes.

The notation " mod n\bmod n" sits at the end of the line because it modifies the entire equation. It is not part of either side. The line a≡b(modn)a \equiv b \pmod n is read as "aa and bb are in the same residue class, where the residue is taken modulo nn".

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 a≡a′(modn)a \equiv a' \pmod n and b≡b′(modn)b \equiv b' \pmod n, then

a+b  ≡  a′+b′(modn),a⋅b  ≡  a′⋅b′(modn).a + b \;\equiv\; a' + b' \pmod{n}, \qquad a \cdot b \;\equiv\; a' \cdot b' \pmod{n}.

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 nn, you can reduce your inputs modulo nn at any point, in any order, without changing the result.

A worked example with n=5n = 5. Compute (17+38)⋅23 mod 5(17 + 38) \cdot 23 \bmod 5.

The slow, honest way: 17+38=5517 + 38 = 55, then 55⋅23=126555 \cdot 23 = 1265, then 1265 mod 5=01265 \bmod 5 = 0.

The fast way, using the commute-with-reduction fact: reduce each input modulo 55 first.

  • 17 mod 5=217 \bmod 5 = 2,
  • 38 mod 5=338 \bmod 5 = 3,
  • 23 mod 5=323 \bmod 5 = 3.

So (17+38)⋅23≡(2+3)⋅3≡5⋅3≡15≡0(mod5)(17 + 38) \cdot 23 \equiv (2 + 3) \cdot 3 \equiv 5 \cdot 3 \equiv 15 \equiv 0 \pmod 5.

Same answer. Far smaller intermediate values. In hardware, this is the difference between a 3232-bit multiplier and a much narrower one. In ML-KEM, q=3329q = 3329 and the natural data path is 1212 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 a≡a′(modn)a \equiv a' \pmod n, then a=a′+k⋅na = a' + k \cdot n for some integer kk. Similarly, b=b′+ℓ⋅nb = b' + \ell \cdot n for some integer ℓ\ell. Then

a+b  =  (a′+b′)+(k+ℓ)⋅n  ≡  a′+b′(modn).a + b \;=\; (a' + b') + (k + \ell) \cdot n \;\equiv\; a' + b' \pmod n.

For multiplication,

a⋅b  =  (a′+kn)⋅(b′+ℓn)  =  a′b′+a′ℓn+b′kn+kℓn2,a \cdot b \;=\; (a' + kn) \cdot (b' + \ell n) \;=\; a' b' + a' \ell n + b' k n + k \ell n^2,

and the last three terms are all multiples of nn, so they vanish modulo nn. So a⋅b≡a′⋅b′(modn)a \cdot b \equiv a' \cdot b' \pmod n. That is the entire argument.

Tables for n=5n = 5

Because the residues mod 55 are just {0,1,2,3,4}\{0, 1, 2, 3, 4\}, we can write the entire addition and multiplication tables explicitly.

Addition modulo 55:

++0011223344
000011223344
111122334400
222233440011
333344001122
444400112233

Multiplication modulo 55:

⋅\cdot0011223344
000000000000
110011223344
220022441133
330033114422
440044332211

Read entries off the tables to convince yourself the answers match the rules. For instance, 3⋅4 mod 53 \cdot 4 \bmod 5: the ordinary product is 1212, and 12 mod 5=212 \bmod 5 = 2, which matches the entry in row 33, column 44. The bottom-right of the multiplication table is 4⋅4 mod 5=16 mod 5=14 \cdot 4 \bmod 5 = 16 \bmod 5 = 1, 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 Z5\mathbb{Z}_5 a field rather than only a ring.

07.What you carry forward

Three things from this note will appear constantly.

The mod operator a mod na \bmod n produces the remainder in the range {0,1,…,n−1}\{0, 1, \ldots, n - 1\}. The congruence symbol ≡\equiv says "in the same residue class". And the commute-with-reduction property lets you reduce inputs modulo nn at any stage of an arithmetic computation without changing the final answer modulo nn.

The next note packages all of this into a named structure: Zn\mathbb{Z}_n, the ring of integers modulo nn. The n=5n = 5 tables you have just looked at are the addition and multiplication tables of Z5\mathbb{Z}_5.

FeedbackBook mode
post-quantum-cryptographycryptographymathematics