Modular Arithmetic & Cryptography Math
Modular Arithmetic & Cryptography Math
Modular arithmetic underpins computer science topics from hashing to cryptography. It works with remainders after division.
$$a \equiv b \pmod{m} \iff m \mid (a - b)$$
"$a$ is congruent to $b$ mod $m$" means $a$ and $b$ have the same remainder when divided by $m$.
If $a \equiv b$ and $c \equiv d$ (mod $m$), then:
- $(a + c) \equiv (b + d) \pmod{m}$
- $(a \cdot c) \equiv (b \cdot d) \pmod{m}$
- $a^n \equiv b^n \pmod{m}$
$$\gcd(a, b) = \gcd(b, a \bmod b)$$
Repeat until remainder is 0. The last non-zero remainder is the GCD.
Find $17 \bmod 5$.
$17 = 3 \times 5 + 2$, so $17 \bmod 5 = 2$.
Find $\gcd(252, 198)$ using the Euclidean algorithm.
$252 = 1 \times 198 + 54$
$198 = 3 \times 54 + 36$
$54 = 1 \times 36 + 18$
$36 = 2 \times 18 + 0$
$\gcd(252, 198) = 18$
Compute $7^{100} \bmod 10$ (find the last digit of $7^{100}$).
Powers of 7 mod 10 cycle: $7, 9, 3, 1, 7, 9, 3, 1, \ldots$ (period 4).
$100 \bmod 4 = 0$, so $7^{100} \equiv 7^0 \equiv 1 \pmod{10}$. Last digit is $1$.
Practice Problems
Show Answer Key
1. $29 = 4 \times 7 + 1$; answer: $1$
2. $45 - 15 = 30$; $10 \mid 30$ ✓ — yes, $45 \equiv 15 \pmod{10}$
3. $84 = 2 \times 36 + 12$; $36 = 3 \times 12 + 0$; $\gcd = 12$
4. $32 \bmod 7 = 4$
5. $56 \bmod 5 = 1$
6. $3^1=3, 3^2=9, 3^3=7, 3^4=1$ (mod 10, period 4); $50 \bmod 4 = 2$; last digit is $9$
7. $1071 = 2(462) + 147$; $462 = 3(147) + 21$; $147 = 7(21) + 0$; $\gcd = 21$
8. $n = 55$, $\phi = 40$
9. $\gcd(3, 40) = 1$; $e = 3$
10. $2^{10} = 1{,}024$; $1{,}024 \bmod 1000 = 24$
11. $3 \times 4 = 12 \equiv 1 \pmod{11}$; inverse is $4$
12. $h(47) = 47 \bmod 13 = 8$; $h(26) = 0$; $h(13) = 0$ (collision with 26)