1. The Clock Arithmetic of Computing
In digital computing, variables do not have unbounded size—integers overflow and wrap around fixed bit widths (e.g., unsigned 32-bit integers wrap modulo $2^{32}$).
Modular Arithmetic is the mathematics of cyclic structures and remainders. Beyond memory architectures, it forms the mathematical backbone of public-key cryptography (RSA, Diffie-Hellman, Elliptic Curves), hash table distribution, and cyclic redundancy checksums (CRC).
text
2. Divisibility & Congruence Relations
2.1 The Division Algorithm
For any integer $a$ and positive integer $d > 0$, there exist unique integers $q$ (quotient) and $r$ (remainder) such that:
$$a = q \cdot d + r, \quad \text{where } 0 \le r < d$$
2.2 Congruence Modulo $m$
Two integers $a$ and $b$ are congruent modulo $m$ (written $a \equiv b \pmod m$) if their difference is an integer multiple of $m$:
$$a \equiv b \pmod m \iff m \mid (a - b)$$
Fundamental Algebraic Properties:
If $a \equiv b \pmod m$ and $c \equiv d \pmod m$, then:
- Addition: $a + c \equiv b + d \pmod m$.
- Multiplication: $a \cdot c \equiv b \cdot d \pmod m$.
- Exponentiation: $a^k \equiv b^k \pmod m$ for any integer $k \ge 0$.
Cancellation Warning: You CANNOT divide freely in modular arithmetic! $ac \equiv bc \pmod m$ does NOT imply $a \equiv b \pmod m$ unless $\gcd(c, m) = 1$. For example, $2 \times 2 \equiv 2 \times 5 \pmod 6$ ($4 \equiv 10 \pmod 6$), but $2 \not\equiv 5 \pmod 6$!
3. The Euclidean & Extended Euclidean Algorithms
3.1 The Euclidean Algorithm for Greatest Common Divisor ($\gcd$)
Based on the identity: $\gcd(a, b) = \gcd(b, a \bmod b)$.
It terminates in $O(\log(\min(a, b)))$ steps (Lamé's Theorem).
text
3.2 Bézout's Identity & The Extended Euclidean Algorithm
For any non-zero integers $a$ and $b$, there exist integers $x$ and $y$ such that:
$$a x + b y = \gcd(a, b)$$
The Extended Euclidean Algorithm tracks the quotient steps in reverse to compute $x$ and $y$.
3.3 Modular Multiplicative Inverse
An integer $x$ is the modular inverse of $a$ modulo $m$ (denoted $a^{-1} \pmod m$) if:
$$a x \equiv 1 \pmod m$$
Theorem: The modular inverse $a^{-1} \pmod m$ exists if and only if $a$ and $m$ are coprime ($\gcd(a, m) = 1$).
Proof via Bézout:
If $\gcd(a, m) = 1$, by Bézout's identity:
$$a x + m y = 1$$
Taking this equation modulo $m$:
$$a x + 0 \equiv 1 \pmod m \implies a x \equiv 1 \pmod m$$
The Bézout coefficient $x$ is precisely the modular inverse!
4. Fermat's Little Theorem & Euler's Totient
4.1 Fermat's Little Theorem (FLT)
If $p$ is a prime number and $a$ is an integer not divisible by $p$:
$$a^{p-1} \equiv 1 \pmod p$$
Multiplying both sides by $a$: $a^p \equiv a \pmod p$ for all integers $a$.
4.2 Euler's Totient Function $\phi(n)$
Counts the number of positive integers up to $n$ that are relatively prime to $n$:
- If $p$ is prime: $\phi(p) = p - 1$.
- If $n = p \cdot q$ where $p, q$ are distinct primes: $$\phi(p \cdot q) = (p - 1)(q - 1)$$
4.3 Euler's Totient Theorem
For any modulus $n$ and integer $a$ coprime to $n$ ($\gcd(a, n) = 1$):
$$a^{\phi(n)} \equiv 1 \pmod n$$
This theorem is the mathematical engine that guarantees RSA decryption.
5. The Chinese Remainder Theorem (CRT)
Suppose a distributed database shards data by hashing IDs modulo $m_1 = 3$, $m_2 = 5$, and $m_3 = 7$. Given remainders from each shard, can we uniquely reconstruct the original ID?
Theorem (CRT)
Let $m_1, m_2, \dots, m_k$ be pairwise coprime positive integers ($\gcd(m_i, m_j) = 1$ for all $i \neq j$).
For any given integers $a_1, a_2, \dots, a_k$, the system of simultaneous congruences:
$$\begin{cases}
x \equiv a_1 \pmod{m_1} \
x \equiv a_2 \pmod{m_2} \
\vdots \
x \equiv a_k \pmod{m_k}
\end{cases}$$
has a unique solution modulo $M = m_1 \cdot m_2 \cdots m_k$.
Constructive Solution Formula:
- Compute $M = \prod_{i=1}^k m_i$.
- For each $i$, compute $M_i = M / m_i$.
- Compute modular inverse $y_i \equiv M_i^{-1} \pmod{m_i}$.
- The unique solution is: $$x = \left( \sum_{i=1}^k a_i M_i y_i \right) \bmod M$$
6. The RSA Public-Key Cryptosystem
RSA (Rivest, Shamir, Adleman, 1977) allows two parties to communicate securely over an untrusted channel without sharing a secret key in advance.
text
Mathematical Proof of Correctness:
We must prove that $c^d \equiv m \pmod n$:
$$c^d \equiv (m^e)^d \equiv m^{ed} \pmod n$$
By definition of modular inverse, $e \cdot d \equiv 1 \pmod{\phi(n)} \implies ed = 1 + k \cdot \phi(n)$ for some integer $k$.
$$m^{ed} = m^{1 + k \phi(n)} = m \cdot (m^{\phi(n)})^k$$
By Euler's Theorem, if $\gcd(m, n) = 1$, then $m^{\phi(n)} \equiv 1 \pmod n$:
$$m^{ed} \equiv m \cdot (1)^k \equiv m \pmod n \quad \blacksquare$$
7. Python Implementation: Extended Euclidean, Inverses & RSA
python
8. Key Takeaways
- Modular Inverses Require Coprimality: $a^{-1} \pmod m$ exists if and only if $\gcd(a, m) = 1$, computed in $O(\log m)$ via the Extended Euclidean algorithm.
- Euler's Totient Power: $a^{\phi(n)} \equiv 1 \pmod n$ enables trapdoor one-way functions by wrapping exponents modulo $\phi(n)$.
- RSA Security: Decrypting $c = m^e \bmod n$ requires computing $d = e^{-1} \bmod \phi(n)$, which is computationally impossible without factoring $n$ into $p \cdot q$.