Modular Arithmetic: The Mathematics of Finite Systems
Modular arithmetic is the mathematical foundation that makes modern cryptography possible. Think of it as "clock arithmetic" – when you go past 12 on a clock, you wrap around to 1. This simple concept leads to profound mathematical structures that protect everything from your credit card transactions to secure messaging.
The Fundamental Concept: Congruence
Definition and Intuition
For integers , , and positive integer , we say is congruent to modulo , written as:
a \equiv b \pmod{n}if and only if divides , which we write as .
Equivalent characterizations:
a \equiv b \pmod{n} \iff \exists k \in \mathbb{Z} : a = b + kn a \equiv b \pmod{n} \iff a \bmod n = b \bmod nExamples:
- 17 \equiv 5 \pmod{12} because and
- -3 \equiv 9 \pmod{12} because and
- 100 \equiv 4 \pmod{8} because
The Division Algorithm
The mathematical foundation of modular arithmetic rests on the Division Algorithm:
Theorem: For any integer and positive integer , there exist unique integers (quotient) and (remainder) such that:
The remainder is denoted a \bmod n, and we have a \equiv r \pmod{n}.
Properties of Congruence Relations
Congruence modulo is an equivalence relation, satisfying three fundamental properties:
Reflexivity
\forall a \in \mathbb{Z}: \quad a \equiv a \pmod{n}Proof: Since and , we have a \equiv a \pmod{n}.
Symmetry
a \equiv b \pmod{n} \implies b \equiv a \pmod{n}Proof: If , then for some . Thus , so .
Transitivity
a \equiv b \pmod{n} \land b \equiv c \pmod{n} \implies a \equiv c \pmod{n}Proof: If and , then:
Therefore .
Arithmetic Operations
Addition and Subtraction
Theorem: If a \equiv a' \pmod{n} and b \equiv b' \pmod{n}, then:
a + b \equiv a' + b' \pmod{n} a - b \equiv a' - b' \pmod{n}Computational formula:
(a + b) \bmod n = \big((a \bmod n) + (b \bmod n)\big) \bmod nExample:
(23 + 17) \bmod 10 = (3 + 7) \bmod 10 = 10 \bmod 10 = 0Multiplication
Theorem: If a \equiv a' \pmod{n} and b \equiv b' \pmod{n}, then:
a \cdot b \equiv a' \cdot b' \pmod{n}Proof: Let and . Then:
Therefore ab \equiv a'b' \pmod{n}.
Exponentiation
Theorem: If a \equiv b \pmod{n}, then for any positive integer :
a^k \equiv b^k \pmod{n}The Ring Structure:
Equivalence Classes
The congruence relation partitions into equivalence classes:
The set forms a commutative ring with:
| Property | Definition |
|---|---|
| Additive identity | |
| Multiplicative identity | |
| Additive inverse of |
Multiplicative Inverses
Definition and Existence
An element has a multiplicative inverse if there exists such that:
[a] \cdot [b] = [1] \quad \text{i.e.,} \quad ab \equiv 1 \pmod{n}Fundamental Theorem: The multiplicative inverse of modulo exists if and only if:
Proof (): If , by Bézout's identity there exist such that:
This gives ax \equiv 1 \pmod{n}, so x \equiv a^{-1} \pmod{n}.
The Group of Units
The group of units modulo is:
This forms a multiplicative group with order:
where is Euler's totient function.
Examples:
Computing Multiplicative Inverses
Extended Euclidean Algorithm
To find a^{-1} \bmod n, solve for in:
def extended_gcd(a, b):
if b == 0:
return a, 1, 0
gcd, x1, y1 = extended_gcd(b, a % b)
x = y1
y = x1 - (a // b) * y1
return gcd, x, y
def mod_inverse(a, n):
gcd, x, _ = extended_gcd(a, n)
if gcd != 1:
raise ValueError("Inverse does not exist")
return x % nExample: Find 7^{-1} \bmod 11
Using the Extended Euclidean Algorithm:
Back-substitution:
Therefore: 7^{-1} \equiv -3 \equiv 8 \pmod{11}
Fermat's Little Theorem Method
For prime and :
a^{p-1} \equiv 1 \pmod{p}Corollary:
a^{-1} \equiv a^{p-2} \pmod{p}Euler's Totient Function
Definition
For positive integer , Euler's totient function counts integers coprime to :
Key Properties
For prime :
For prime power :
Multiplicative property: If :
General formula: For :
Example:
Euler's Theorem
Theorem: If , then:
a^{\varphi(n)} \equiv 1 \pmod{n}Corollary:
a^{-1} \equiv a^{\varphi(n)-1} \pmod{n}Proof sketch: The set of residues coprime to satisfies:
\prod_{i=1}^{\varphi(n)} r_i \equiv \prod_{i=1}^{\varphi(n)} (ar_i) \equiv a^{\varphi(n)} \prod_{i=1}^{\varphi(n)} r_i \pmod{n}Canceling gives a^{\varphi(n)} \equiv 1 \pmod{n}.
Efficient Modular Exponentiation
Computing a^b \bmod n efficiently using binary exponentiation:
def mod_exp(base, exp, mod):
result = 1
base = base % mod
while exp > 0:
if exp & 1:
result = (result * base) % mod
base = (base * base) % mod
exp >>= 1
return resultComplexity: multiplications
Example: Compute 3^{13} \bmod 7
Binary:
| Step | exp (binary) | Action | result | base |
|---|---|---|---|---|
| 1 | 1101 | multiply | ||
| 2 | 110 | square only | ||
| 3 | 11 | multiply | ||
| 4 | 1 | multiply | — |
Result: 3^{13} \equiv 3 \pmod{7}
Chinese Remainder Theorem
Statement
Let be pairwise coprime. The system:
$ \begin{cases} x \equiv a_1 \pmod{n_1} \\ x \equiv a_2 \pmod{n_2} \\ \vdots \\ x \equiv a_k \pmod{n_k} \end{cases} $has a unique solution modulo .
Construction
Let and find such that N_i M_i \equiv 1 \pmod{n_i}.
The solution is:
x \equiv \sum_{i=1}^{k} a_i N_i M_i \pmod{N}RSA Optimization
For RSA with , instead of computing m \equiv c^d \pmod{n}:
m_p \equiv c^{d \bmod (p-1)} \pmod{p} m_q \equiv c^{d \bmod (q-1)} \pmod{q}Combine using CRT for ~4× speedup.
Quadratic Residues
Definition
An integer is a quadratic residue modulo if:
\exists x \in \mathbb{Z}: \quad x^2 \equiv a \pmod{n}Example (mod 7):
| x^2 \bmod 7 | |
|---|---|
| 1 | 1 |
| 2 | 4 |
| 3 | 2 |
| 4 | 2 |
| 5 | 4 |
| 6 | 1 |
Quadratic residues: , Non-residues:
Legendre Symbol
For odd prime and :
$ \left(\frac{a}{p}\right) = \begin{cases} 1 & \text{if } a \text{ is a QR mod } p \\ -1 & \text{if } a \text{ is a QNR mod } p \end{cases} $Euler's Criterion:
\left(\frac{a}{p}\right) \equiv a^{\frac{p-1}{2}} \pmod{p}Quadratic Reciprocity
For distinct odd primes and :
Supplementary laws:
Primitive Roots
Definition
An integer is a primitive root modulo if:
where is the smallest positive such that g^k \equiv 1 \pmod{n}.
Existence
Primitive roots exist modulo if and only if:
where is an odd prime.
Properties
If is a primitive root modulo prime :
Number of primitive roots modulo :
The Discrete Logarithm Problem
Given , , and prime where is a primitive root, find such that:
g^x \equiv h \pmod{p}We write .
Cryptographic significance: The security of Diffie-Hellman, ElGamal, and DSA relies on the computational difficulty of this problem.
Cryptographic Applications
RSA Cryptosystem
Key generation:
- Choose large primes
- Compute and
- Choose with
- Compute d \equiv e^{-1} \pmod{\varphi(n)}
Operations:
\text{Encrypt: } c \equiv m^e \pmod{n} \text{Decrypt: } m \equiv c^d \pmod{n}Why it works: By Euler's theorem:
c^d = m^{ed} = m^{1 + k\varphi(n)} = m \cdot (m^{\varphi(n)})^k \equiv m \pmod{n}Diffie-Hellman Key Exchange
Public parameters: Prime , primitive root
Protocol:
\text{Alice: } A \equiv g^a \pmod{p} \text{Bob: } B \equiv g^b \pmod{p} \text{Shared secret: } K \equiv g^{ab} \equiv A^b \equiv B^a \pmod{p}ElGamal Encryption
Keys: Private , Public y \equiv g^x \pmod{p}
Encrypt message :
c_1 \equiv g^k \pmod{p}, \quad c_2 \equiv m \cdot y^k \pmod{p}Decrypt:
m \equiv c_2 \cdot (c_1^x)^{-1} \pmod{p}Summary of Key Formulas
| Concept | Formula |
|---|---|
| Congruence | a \equiv b \pmod{n} \iff n \mid (a-b) |
| Inverse exists | |
| Fermat's Little Theorem | a^{p-1} \equiv 1 \pmod{p} |
| Euler's Theorem | a^{\varphi(n)} \equiv 1 \pmod{n} |
| Totient (prime) | |
| Totient (general) | |
| Euler's Criterion | \left(\frac{a}{p}\right) \equiv a^{\frac{p-1}{2}} \pmod{p} |
Practice Problems
- Compute 17^{-1} \bmod 43 using the Extended Euclidean Algorithm
- Find all solutions to x^2 \equiv 2 \pmod{7}
- Solve the system: x \equiv 2 \pmod{3}, x \equiv 3 \pmod{5}, x \equiv 2 \pmod{7}
- Find a primitive root modulo
- Compute 2^{1000} \bmod 13 using Fermat's Little Theorem
The elegant mathematics of modular arithmetic forms the backbone of modern cryptography, transforming ancient number theory into the security infrastructure that protects our digital world.