TExcelTAD
Home › Number Theory & Cryptography › The Integers Modulo n

The Integers Modulo n

Treat each congruence class as a single object and you get a finite number system, Z/nZ\mathbb{Z}/n\mathbb{Z}, with nn elements, addition and multiplication. When nn is prime it is a field, where every nonzero element can be divided by: the setting of Diffie-Hellman, DSA and elliptic-curve cryptography.

The ring Z/nZ\mathbb{Z}/n\mathbb{Z}

The elements are the classes 0ˉ,1ˉ,…,n−1‾\bar 0, \bar 1, \ldots, \overline{n - 1}, usually written as their representatives 0,1,…,n−10, 1, \ldots, n - 1. Addition and multiplication are "compute, then reduce mod nn", which is well defined by the rules of Congruences. The usual laws hold (associative, commutative, distributive, 00 and 11 are identities), so Z/nZ\mathbb{Z}/n\mathbb{Z} is a commutative ring.

python · try it yourself
n = 6
print("+ |", *range(n))
for a in range(n):
    print(a, "|", *[(a + b) % n for b in range(n)])
print()
print("* |", *range(n))
for a in range(n):
    print(a, "|", *[(a * b) % n for b in range(n)])
+ | 0 1 2 3 4 5
0 | 0 1 2 3 4 5
1 | 1 2 3 4 5 0
2 | 2 3 4 5 0 1
3 | 3 4 5 0 1 2
4 | 4 5 0 1 2 3
5 | 5 0 1 2 3 4

* | 0 1 2 3 4 5
0 | 0 0 0 0 0 0
1 | 0 1 2 3 4 5
2 | 0 2 4 0 2 4
3 | 0 3 0 3 0 3
4 | 0 4 2 0 4 2
5 | 0 5 4 3 2 1

Look at the multiplication table modulo 6: the rows of 1 and 5 contain every value once, but 2⋅3=02 \cdot 3 = 0. Elements like 2 and 3, nonzero but multiplying to zero, are zero divisors, and they have no inverses.

Units

An element aa is a unit if some bb satisfies ab≡1(modn)ab \equiv 1 \pmod{n}. By Bézout, aa is a unit exactly when gcd⁡(a,n)=1\gcd(a, n) = 1. The units form a group under multiplication, written (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times, with φ(n)\varphi(n) elements (Euler's Totient Function).

python · try it yourself
from math import gcd

for n in (6, 7, 12, 15):
    units = [a for a in range(1, n) if gcd(a, n) == 1]
    print(f"n={n:2}  units={units}  count={len(units)}")
n= 6  units=[1, 5]  count=2
n= 7  units=[1, 2, 3, 4, 5, 6]  count=6
n=12  units=[1, 5, 7, 11]  count=4
n=15  units=[1, 2, 4, 7, 8, 11, 13, 14]  count=8

Prime modulus: a field

When n=pn = p is prime, every a∈{1,…,p−1}a \in \{1, \ldots, p - 1\} is coprime to pp, so every nonzero element is a unit and Z/pZ\mathbb{Z}/p\mathbb{Z} is a field, written Fp\mathbb{F}_p or GF(p)GF(p). You can solve linear equations, invert matrices and do polynomial algebra exactly as over the rationals.

python · try it yourself
p = 7
inv = {a: b for a in range(1, p) for b in range(1, p) if a * b % p == 1}
print("inverses mod 7:", inv)
# Solve 3x + 4 = 2 in F_7: x = (2 - 4) * 3^{-1}
x = (2 - 4) * inv[3] % p
print("x =", x, " check:", (3 * x + 4) % p)
inverses mod 7: {1: 1, 2: 4, 3: 5, 4: 2, 5: 3, 6: 6}
x = 4  check: 2

Fast reduction in practice

Cryptographic code multiplies 256- to 4096-bit numbers modulo nn millions of times. Two standard tricks avoid slow division: special moduli like p=2255−19p = 2^{255} - 19 (Curve25519), where reduction uses 2255≡192^{255} \equiv 19, and Montgomery multiplication, which replaces division by nn with shifts. Here is the special-modulus trick:

python · try it yourself
p = 2**255 - 19
a, b = 3**160, 5**109             # two numbers below p
t = a * b                          # up to 510 bits
hi, lo = t >> 255, t & (2**255 - 1)
r = lo + 19 * hi                   # since 2^255 = 19 (mod p); r < 20 * 2^255
while r >= p:                      # a few subtractions finish the job
    r -= p
print(r == t % p, t.bit_length(), r.bit_length())
True 507 251

In software

  • uint32_t and uint64_t arithmetic in C, Rust and Go is Z/232Z\mathbb{Z}/2^{32}\mathbb{Z} and Z/264Z\mathbb{Z}/2^{64}\mathbb{Z}; 2 is a zero divisor there (2⋅263=02 \cdot 2^{63} = 0), so only odd numbers have inverses.
  • Curve25519, P-256 and Ed448 use primes chosen to make reduction cheap; CRC checksums use arithmetic of polynomials modulo 2 (Finite Fields GF(2^n)).

Exercises

Exercise 1: Zero divisors

List all zero divisors in Z/12Z\mathbb{Z}/12\mathbb{Z}.

Answer

2, 3, 4, 6, 8, 9, 10.

Solution

A nonzero aa is a zero divisor exactly when gcd⁡(a,12)>1\gcd(a, 12) > 1 (then a⋅12gcd⁡(a,12)≡0a \cdot \frac{12}{\gcd(a, 12)} \equiv 0). The units are 1, 5, 7, 11; all other nonzero elements are zero divisors.

Exercise 2: Solve in F11\mathbb{F}_{11}

Solve 5x+7≡3(mod11)5x + 7 \equiv 3 \pmod{11}.

Answer

x=8x = 8.

Solution

5x≡−4≡75x \equiv -4 \equiv 7. The inverse of 5 modulo 11 is 9 (45=44+145 = 44 + 1). So x≡7⋅9=63≡8x \equiv 7 \cdot 9 = 63 \equiv 8. Check: 5⋅8+7=47=44+35 \cdot 8 + 7 = 47 = 44 + 3.

Exercise 3: Not a field (code)

Show in code that Z/10Z\mathbb{Z}/10\mathbb{Z} is not a field by listing elements without an inverse, and that Z/11Z\mathbb{Z}/11\mathbb{Z} is one.

Answer

Modulo 10: 2, 4, 5, 6, 8 have no inverse. Modulo 11: all of 1 to 10 do.

Solution
python · try it yourself
for n in (10, 11):
    no_inv = [a for a in range(1, n) if not any(a * b % n == 1 for b in range(n))]
    print(n, no_inv)
10 [2, 4, 5, 6, 8]
11 []

Test yourself

When is Z/nZ\mathbb{Z}/n\mathbb{Z} a field?

  1. when nn is even
  2. when nn is prime
  3. when nn is a power of 2
  4. always

In Z/nZ\mathbb{Z}/n\mathbb{Z}, aa has a multiplicative inverse exactly when…

  1. a<n/2a < n/2
  2. gcd⁡(a,n)=1\gcd(a, n) = 1
  3. aa is prime
  4. aa is odd

Which of these is a zero divisor modulo 15?

  1. 2
  2. 4
  3. 7
  4. 5