Modular Arithmetic
Introduction
Modular arithmetic is the arithmetic of remainders. Instead of working with all integers, we work with a finite set of remainders when dividing by a fixed modulus. This creates a cyclical number system where arithmetic "wraps around."
Modular arithmetic is fundamental in cryptography (RSA, Diffie-Hellman), computer science (hash functions, checksums), and pure mathematics (studying prime numbers, solving Diophantine equations). The familiar 12-hour clock is an everyday example of arithmetic modulo 12.
Congruence
We say is congruent to modulo written:
if divides i.e., for some integer. Equivalently, and have the same remainder when divided by
Examples: (since ). Also .
Properties of Congruence
Congruence is an equivalence relation:
• Reflexive:
• Symmetric:
• Transitive: and
Arithmetic Operations
If and then:
We can add, subtract, and multiply congruences freely. Division is more delicate—we need multiplicative inverses.
Multiplicative Inverses
The multiplicative inverse of a modulo is an integer such that:
An inverse exists if and only if When it exists, it's unique modulo and can be found using the extended Euclidean algorithm.
Example: Find . We need . Testing: . So .
The Group ℤ/nℤ
The integers modulo form the set. Under addition, this is a group of order .
The units (elements with multiplicative inverses) form the group under multiplication.
Euler's Totient Function
counts integers from to that are coprime to . It equals . For prime :
For prime power:
Multiplicative property: If then
General formula:
Fermat's Little Theorem
If is prime and then:
Equivalently, for any
This gives an easy way to compute inverses:
Euler's Theorem
Generalizes Fermat's Little Theorem. If then:
For prime we get recovering Fermat's Little Theorem.
Chinese Remainder Theorem
If are pairwise coprime, then the system:
has a unique solution modulo
This means as rings.
Linear Congruences
The equation has a solution iff divides
If solvable, there are exactly solutions modulo. When there's a unique solution:
Modular Exponentiation
Computing efficiently uses repeated squaring (fast exponentiation):
• If is even:
• If is odd:
This requires only multiplications, essential for cryptography.
RSA Cryptography
RSA is a public-key encryption system based on modular arithmetic. Key generation:
Choose large primes and compute
Compute
Choose coprime to find
Public key: Private key:
Encryption: Decryption:
Security relies on the difficulty of factoring into and
Primality Testing
Fermat's test: If for some coprime to then is composite.
Passing the test doesn't guarantee primality (Carmichael numbers are exceptions). Miller-Rabin is a stronger probabilistic test used in practice.
Wilson's Theorem
is prime if and only if:
This is a theoretical characterization of primes (computing factorials is too slow for practical primality testing).
Quadratic Residues
is a quadratic residue mod if has a solution. Exactly half of the non-zero elements mod are quadratic residues.
Euler's criterion: is a QR iff
Summary
Modular arithmetic works with remainders: means Addition, subtraction, and multiplication preserve congruence. Division requires multiplicative inverses.
Euler's totient counts units; Euler's theorem says when The Chinese Remainder Theorem solves systems of congruences.
RSA cryptography is built on modular exponentiation and the difficulty of factoring. Modular arithmetic is essential in computer science, cryptography, and number theory.