The Integers Modulo n
Treat each congruence class as a single object and you get a finite number system, , with elements, addition and multiplication. When 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
The elements are the classes , usually written as their representatives . Addition and multiplication are "compute, then reduce mod ", which is well defined by the rules of Congruences. The usual laws hold (associative, commutative, distributive, and are identities), so is a commutative ring.
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 . Elements like 2 and 3, nonzero but multiplying to zero, are zero divisors, and they have no inverses.
Units
An element is a unit if some satisfies . By Bézout, is a unit exactly when . The units form a group under multiplication, written , with elements (Euler's Totient Function).
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 is prime, every is coprime to , so every nonzero element is a unit and is a field, written or . You can solve linear equations, invert matrices and do polynomial algebra exactly as over the rationals.
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 millions of times. Two standard tricks avoid slow division: special moduli like (Curve25519), where reduction uses , and Montgomery multiplication, which replaces division by with shifts. Here is the special-modulus trick:
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_tanduint64_tarithmetic in C, Rust and Go is and ; 2 is a zero divisor there (), 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 .
Answer
2, 3, 4, 6, 8, 9, 10.
Solution
A nonzero is a zero divisor exactly when (then ). The units are 1, 5, 7, 11; all other nonzero elements are zero divisors.
Exercise 2: Solve in
Solve .
Answer
.
Solution
. The inverse of 5 modulo 11 is 9 (). So . Check: .
Exercise 3: Not a field (code)
Show in code that is not a field by listing elements without an inverse, and that is one.
Answer
Modulo 10: 2, 4, 5, 6, 8 have no inverse. Modulo 11: all of 1 to 10 do.
Solution
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 a field?
- when is even
- when is prime
- when is a power of 2
- always
In , has a multiplicative inverse exactly when…
- is prime
- is odd
Which of these is a zero divisor modulo 15?
- 2
- 4
- 7
- 5