28-01 โ€” Modular Arithmetic Review

Phase: 28 โ€” Cryptography Mathematics Subject: 28-01 Prerequisites: 00-08 (Basic Number Theory), 26-03 (Combinations โ€” for counting arguments) Next subject: 28-02 โ€” Classical Ciphers


Learning Objectives

By the end of this subject, you will be able to:

  1. Perform modular arithmetic operations fluently and understand the ring structure of Z_n
  2. Compute modular inverses using the Extended Euclidean Algorithm
  3. Apply Fermat's Little Theorem and Euler's Theorem for efficient modular exponentiation
  4. Solve systems of congruences using the Chinese Remainder Theorem
  5. Understand the mathematical foundations that make RSA and other cryptographic systems secure

Core Content

1. Modular Arithmetic Refresher

Modular arithmetic is arithmetic on remainders. For integers $a, b, n$ with $n > 0$:

$$a \equiv b \pmod{n} \iff n \mid (a - b)$$

The set $\mathbb{Z}_n = \{0, 1, 2, ..., n-1\}$ forms a ring under addition and multiplication mod $n$.

Key properties: - $(a + b) \mod n = [(a \mod n) + (b \mod n)] \mod n$ - $(a \cdot b) \mod n = [(a \mod n) \cdot (b \mod n)] \mod n$ - $a^k \mod n$ can be computed efficiently using modular exponentiation (square-and-multiply)

2. Modular Inverses

$a$ has a multiplicative inverse mod $n$ (written $a^{-1}$) if:

$$a \cdot a^{-1} \equiv 1 \pmod{n}$$

Existence condition: $\gcd(a, n) = 1$ (a and n are coprime).

Extended Euclidean Algorithm: Finds $x, y$ such that $ax + ny = \gcd(a, n)$. When $\gcd(a, n) = 1$, $x$ is the modular inverse of $a$ mod $n$.

3. Fermat's Little Theorem

If $p$ is prime and $p \nmid a$, then:

$$a^{p-1} \equiv 1 \pmod{p}$$

Corollary: $a^p \equiv a \pmod{p}$ for any integer $a$.

Use in cryptography: Enables fast computation of $a^b \mod p$ by reducing the exponent mod $p-1$: $$a^b \mod p = a^{b \mod (p-1)} \mod p$$

4. Euler's Theorem (Generalisation)

For any $n$ and $\gcd(a, n) = 1$:

$$a^{\phi(n)} \equiv 1 \pmod{n}$$

where $\phi(n)$ is Euler's totient function (count of integers $1 \leq k \leq n$ with $\gcd(k, n) = 1$).

For prime $p$: $\phi(p) = p - 1$, recovering Fermat's Little Theorem.

For $n = pq$ (product of two primes): $\phi(n) = (p-1)(q-1)$. This is the key to RSA security.

5. Chinese Remainder Theorem (CRT)

The CRT solves systems of congruences with coprime moduli.

Theorem: If $\gcd(m_1, m_2) = 1$, the system: $$x \equiv a_1 \pmod{m_1}$$ $$x \equiv a_2 \pmod{m_2}$$

has a unique solution mod $m_1 m_2$.

Construction: Find $M = m_1 m_2$, $M_1 = M/m_1$, $M_2 = M/m_2$. Compute $y_1 = M_1^{-1} \mod m_1$ and $y_2 = M_2^{-1} \mod m_2$. Then: $x = a_1 M_1 y_1 + a_2 M_2 y_2 \pmod{M}$.

CRT in RSA: Decrypting a ciphertext $c$ mod $n = pq$ can be done by computing $m_p = c^{d_p} \mod p$ and $m_q = c^{d_q} \mod q$, then combining via CRT. This is ~4x faster than computing $c^d \mod n$ directly.


Worked Examples

Example 1: Extended Euclidean Algorithm

Problem: Find the modular inverse of 17 mod 43.

Solution:

Apply the Extended Euclidean Algorithm to find $17x + 43y = \gcd(17, 43)$.

$43 = 2ยท17 + 9
17 = 1ยท9  + 8
 9 = 1ยท8  + 1
 8 = 8ยท1  + 0
$

Back-substitute:

$1 = 9 - 1ยท8
  = 9 - 1ยท(17 - 1ยท9) = 2ยท9 - 1ยท17
  = 2ยท(43 - 2ยท17) - 1ยท17 = 2ยท43 - 5ยท17
$

So $-5 \cdot 17 + 2 \cdot 43 = 1$, meaning $17^{-1} \equiv -5 \equiv 38 \pmod{43}$.

Answer: $17^{-1} \equiv 38 \pmod{43}$. Check: $17 \cdot 38 = 646$, $646 \div 43 = 15$ remainder $1$. โœ“


Example 2: Fermat's Little Theorem

Problem: Compute $3^{100} \mod 11$.

Solution:

Since 11 is prime and $3 \nmid 11$, Fermat's Little Theorem gives $3^{10} \equiv 1 \pmod{11}$.

$100 = 10 \cdot 10$, so $3^{100} = (3^{10})^{10} \equiv 1^{10} \equiv 1 \pmod{11}$.

Answer: $3^{100} \equiv 1 \pmod{11}$.


Example 3: Chinese Remainder Theorem

Problem: Find $x$ such that $x \equiv 2 \pmod{3}$ and $x \equiv 3 \pmod{5}$.

Solution:

$m_1 = 3, a_1 = 2$; $m_2 = 5, a_2 = 3$. $M = 15$. $M_1 = 5$, need $5y_1 \equiv 1 \pmod{3}$: $5 \equiv 2 \pmod{3}$, $2 \cdot 2 = 4 \equiv 1$, so $y_1 = 2$. $M_2 = 3$, need $3y_2 \equiv 1 \pmod{5}$: $3 \cdot 2 = 6 \equiv 1$, so $y_2 = 2$.

$x = a_1 M_1 y_1 + a_2 M_2 y_2 = 2 \cdot 5 \cdot 2 + 3 \cdot 3 \cdot 2 = 20 + 18 = 38 \equiv 8 \pmod{15}$.

Answer: $x \equiv 8 \pmod{15}$. Check: $8 \equiv 2 \pmod{3}$ โœ“, $8 \equiv 3 \pmod{5}$ โœ“.


Practice Problems

Problem 1: Find the modular inverse of 13 mod 47 using the Extended Euclidean Algorithm.

Problem 2: Compute $7^{50} \mod 13$ using Fermat's Little Theorem.

Problem 3: For $n = 91 = 7 \cdot 13$, compute $\phi(91)$.

Problem 4: Solve: $x \equiv 1 \pmod{4}$, $x \equiv 2 \pmod{7}$, $x \equiv 3 \pmod{9}$ (note: $\gcd(4,7)=1$, $\gcd(4,9)=1$, $\gcd(7,9)=1$).

Problem 5: Show that if $a^k \equiv 1 \pmod{n}$ and $a^m \equiv 1 \pmod{n}$, then $a^{\gcd(k,m)} \equiv 1 \pmod{n}$.


Summary

  1. Modular arithmetic operates on remainders; $\mathbb{Z}_n$ forms a ring with efficient modular multiplication and exponentiation
  2. Modular inverses exist when $\gcd(a, n) = 1$; found via Extended Euclidean Algorithm
  3. Fermat's Little Theorem ($a^{p-1} \equiv 1 \pmod{p}$) enables fast modular exponentiation for prime moduli
  4. Euler's Theorem generalises to $a^{\phi(n)} \equiv 1 \pmod{n}$; $\phi(pq) = (p-1)(q-1)$ is the RSA key insight
  5. Chinese Remainder Theorem solves systems of congruences and enables faster RSA decryption

Pitfalls


Key Terms


Quiz

Q1: The modular inverse of $a$ mod $n$ exists if and only if:

A) $a < n$ B) $\gcd(a, n) = 1$ C) $a$ is even D) $n$ is prime

Correct: B)


Q2: Fermat's Little Theorem states that for prime $p$ and $p \nmid a$:

A) $a^p \equiv a \pmod{p}$ B) $a^{p-1} \equiv 1 \pmod{p}$ C) $a^{p} \equiv 1 \pmod{p}$ D) $a^{p-1} \equiv 0 \pmod{p}$

Correct: B)


Q3: Euler's totient function $\phi(15)$ equals:

A) 8 B) 6 C) 4 D) 15

Correct: A)


Q4: The Chinese Remainder Theorem guarantees:

A) A solution exists for any system of congruences B) A unique solution mod $m_1 m_2$ when $\gcd(m_1, m_2) = 1$ C) A solution only when all moduli are prime D) No solution when moduli overlap

Correct: B)


Q5: For $n = pq$ where $p, q$ are large primes, $\phi(n)$ equals:

A) $p + q - 1$ B) $(p-1)(q-1)$ C) $pq$ D) $p + q$

Correct: B)


Q6: To compute $a^b \mod n$ efficiently, we use:

A) Compute $a^b$ first, then take mod B) Square-and-multiply (modular exponentiation), reducing mod $n$ at each step C) Use a calculator D) Approximate with floating point

Correct: B)


Q7: In RSA, the security relies on the difficulty of:

A) Computing $\phi(n)$ without knowing the prime factors of $n$ B) Computing modular inverses C) Adding large numbers D) Finding $a^{b} \mod n$

Correct: A)


Q8: CRT speeds up RSA decryption because:

A) It avoids modular exponentiation entirely B) It computes two smaller exponentiations ($c^{d_p} \mod p$ and $c^{d_q} \mod q$) and combines them, which is ~4x faster than one large exponentiation C) It reduces the key size D) It eliminates the need for private keys

Correct: B)


Next Steps

Continue to 28-02 โ€” Classical Ciphers to explore the historical foundations of cryptography before diving into modern symmetric and asymmetric encryption.