Skip to content

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 aa, bb, and positive integer nn, we say aa is congruent to bb modulo nn, written as:

a \equiv b \pmod{n}

if and only if nn divides (ab)(a - b), which we write as n(ab)n \mid (a - b).

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 n

Examples:

  • 17 \equiv 5 \pmod{12} because 175=1217 - 5 = 12 and 121212 \mid 12
  • -3 \equiv 9 \pmod{12} because 39=12-3 - 9 = -12 and 12(12)12 \mid (-12)
  • 100 \equiv 4 \pmod{8} because 100=12×8+4100 = 12 \times 8 + 4

The Division Algorithm

The mathematical foundation of modular arithmetic rests on the Division Algorithm:

Theorem: For any integer aa and positive integer nn, there exist unique integers qq (quotient) and rr (remainder) such that:

a=qn+rwhere0r<na = qn + r \quad \text{where} \quad 0 \leq r < n

The remainder rr is denoted a \bmod n, and we have a \equiv r \pmod{n}.

Properties of Congruence Relations

Congruence modulo nn is an equivalence relation, satisfying three fundamental properties:

Reflexivity

\forall a \in \mathbb{Z}: \quad a \equiv a \pmod{n}

Proof: Since aa=0a - a = 0 and n0n \mid 0, we have a \equiv a \pmod{n}. \square

Symmetry

a \equiv b \pmod{n} \implies b \equiv a \pmod{n}

Proof: If n(ab)n \mid (a - b), then ab=kna - b = kn for some kZk \in \mathbb{Z}. Thus ba=kn=(k)nb - a = -kn = (-k)n, so n(ba)n \mid (b - a). \square

Transitivity

a \equiv b \pmod{n} \land b \equiv c \pmod{n} \implies a \equiv c \pmod{n}

Proof: If ab=k1na - b = k_1 n and bc=k2nb - c = k_2 n, then:

ac=(ab)+(bc)=k1n+k2n=(k1+k2)na - c = (a - b) + (b - c) = k_1 n + k_2 n = (k_1 + k_2)n

Therefore n(ac)n \mid (a - c). \square

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 n

Example:

(23 + 17) \bmod 10 = (3 + 7) \bmod 10 = 10 \bmod 10 = 0

Multiplication

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 a=a+k1na = a' + k_1 n and b=b+k2nb = b' + k_2 n. Then:

ab=(a+k1n)(b+k2n)=ab+n(ak2+bk1+k1k2n)ab = (a' + k_1 n)(b' + k_2 n) = a'b' + n(a'k_2 + b'k_1 + k_1 k_2 n)

Therefore ab \equiv a'b' \pmod{n}. \square

Exponentiation

Theorem: If a \equiv b \pmod{n}, then for any positive integer kk:

a^k \equiv b^k \pmod{n}

The Ring Structure: Z/nZ\mathbb{Z}/n\mathbb{Z}

Equivalence Classes

The congruence relation partitions Z\mathbb{Z} into nn equivalence classes:

[0]={,2n,n,0,n,2n,}[0] = \{\ldots, -2n, -n, 0, n, 2n, \ldots\}

[1]={,2n+1,n+1,1,n+1,2n+1,}[1] = \{\ldots, -2n+1, -n+1, 1, n+1, 2n+1, \ldots\}

\vdots

[n1]={,n1,1,n1,2n1,3n1,}[n-1] = \{\ldots, -n-1, -1, n-1, 2n-1, 3n-1, \ldots\}

The set Z/nZ={[0],[1],,[n1]}\mathbb{Z}/n\mathbb{Z} = \{[0], [1], \ldots, [n-1]\} forms a commutative ring with:

PropertyDefinition
Additive identity[0][0]
Multiplicative identity[1][1]
Additive inverse of [a][a][na][n-a]

Multiplicative Inverses

Definition and Existence

An element [a]Z/nZ[a] \in \mathbb{Z}/n\mathbb{Z} has a multiplicative inverse if there exists [b][b] such that:

[a] \cdot [b] = [1] \quad \text{i.e.,} \quad ab \equiv 1 \pmod{n}

Fundamental Theorem: The multiplicative inverse of aa modulo nn exists if and only if:

gcd(a,n)=1\gcd(a, n) = 1

Proof (\Leftarrow): If gcd(a,n)=1\gcd(a, n) = 1, by Bézout's identity there exist x,yZx, y \in \mathbb{Z} such that:

ax+ny=1ax + ny = 1

This gives ax \equiv 1 \pmod{n}, so x \equiv a^{-1} \pmod{n}. \square

The Group of Units

The group of units modulo nn is:

(Z/nZ)={[a]Z/nZ:gcd(a,n)=1}(\mathbb{Z}/n\mathbb{Z})^* = \{[a] \in \mathbb{Z}/n\mathbb{Z} : \gcd(a, n) = 1\}

This forms a multiplicative group with order:

(Z/nZ)=φ(n)\left|(\mathbb{Z}/n\mathbb{Z})^*\right| = \varphi(n)

where φ\varphi is Euler's totient function.

Examples:

(Z/5Z)={[1],[2],[3],[4]},(Z/5Z)=4(\mathbb{Z}/5\mathbb{Z})^* = \{[1], [2], [3], [4]\}, \quad \left|(\mathbb{Z}/5\mathbb{Z})^*\right| = 4

(Z/8Z)={[1],[3],[5],[7]},(Z/8Z)=4(\mathbb{Z}/8\mathbb{Z})^* = \{[1], [3], [5], [7]\}, \quad \left|(\mathbb{Z}/8\mathbb{Z})^*\right| = 4

Computing Multiplicative Inverses

Extended Euclidean Algorithm

To find a^{-1} \bmod n, solve for xx in:

ax+ny=gcd(a,n)=1ax + ny = \gcd(a, n) = 1

python
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 % n

Example: Find 7^{-1} \bmod 11

Using the Extended Euclidean Algorithm:

11=1×7+411 = 1 \times 7 + 4

7=1×4+37 = 1 \times 4 + 3

4=1×3+14 = 1 \times 3 + 1

Back-substitution:

1=41×3=4(74)=2×47=2(117)7=2×113×71 = 4 - 1 \times 3 = 4 - (7 - 4) = 2 \times 4 - 7 = 2(11 - 7) - 7 = 2 \times 11 - 3 \times 7

Therefore: 7^{-1} \equiv -3 \equiv 8 \pmod{11}

Fermat's Little Theorem Method

For prime pp and gcd(a,p)=1\gcd(a, p) = 1:

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

Corollary:

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

Euler's Totient Function

Definition

For positive integer nn, Euler's totient function counts integers coprime to nn:

φ(n)={k:1kn,gcd(k,n)=1}\varphi(n) = \left|\{k : 1 \leq k \leq n, \gcd(k, n) = 1\}\right|

Key Properties

For prime pp:

φ(p)=p1\varphi(p) = p - 1

For prime power pkp^k:

φ(pk)=pkpk1=pk1(p1)\varphi(p^k) = p^k - p^{k-1} = p^{k-1}(p - 1)

Multiplicative property: If gcd(m,n)=1\gcd(m, n) = 1:

φ(mn)=φ(m)φ(n)\varphi(mn) = \varphi(m) \cdot \varphi(n)

General formula: For n=p1a1p2a2pkakn = p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k}:

φ(n)=ni=1k(11pi)\varphi(n) = n \prod_{i=1}^{k} \left(1 - \frac{1}{p_i}\right)

Example:

φ(12)=φ(223)=12(112)(113)=121223=4\varphi(12) = \varphi(2^2 \cdot 3) = 12 \cdot \left(1 - \frac{1}{2}\right) \cdot \left(1 - \frac{1}{3}\right) = 12 \cdot \frac{1}{2} \cdot \frac{2}{3} = 4

Euler's Theorem

Theorem: If gcd(a,n)=1\gcd(a, n) = 1, then:

a^{\varphi(n)} \equiv 1 \pmod{n}

Corollary:

a^{-1} \equiv a^{\varphi(n)-1} \pmod{n}

Proof sketch: The set S={r1,r2,,rφ(n)}S = \{r_1, r_2, \ldots, r_{\varphi(n)}\} of residues coprime to nn 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}. \square

Efficient Modular Exponentiation

Computing a^b \bmod n efficiently using binary exponentiation:

python
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 result

Complexity: O(logb)O(\log b) multiplications

Example: Compute 3^{13} \bmod 7

Binary: 13=1101213 = 1101_2

Stepexp (binary)Actionresultbase
11101multiply333223^2 \equiv 2
2110square only332242^2 \equiv 4
311multiply3×453 \times 4 \equiv 54224^2 \equiv 2
41multiply5×235 \times 2 \equiv 3

Result: 3^{13} \equiv 3 \pmod{7}

Chinese Remainder Theorem

Statement

Let n1,n2,,nkn_1, n_2, \ldots, n_k 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 N=n1n2nkN = n_1 n_2 \cdots n_k.

Construction

Let Ni=NniN_i = \dfrac{N}{n_i} and find MiM_i 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 n=pqn = pq, 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 aa is a quadratic residue modulo nn if:

\exists x \in \mathbb{Z}: \quad x^2 \equiv a \pmod{n}

Example (mod 7):

xxx^2 \bmod 7
11
24
32
42
54
61

Quadratic residues: {1,2,4}\{1, 2, 4\}, Non-residues: {3,5,6}\{3, 5, 6\}

Legendre Symbol

For odd prime pp and gcd(a,p)=1\gcd(a, p) = 1:

$ \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 pp and qq:

(pq)(qp)=(1)(p1)(q1)4\left(\frac{p}{q}\right)\left(\frac{q}{p}\right) = (-1)^{\frac{(p-1)(q-1)}{4}}

Supplementary laws:

(1p)=(1)p12\left(\frac{-1}{p}\right) = (-1)^{\frac{p-1}{2}}

(2p)=(1)p218\left(\frac{2}{p}\right) = (-1)^{\frac{p^2-1}{8}}

Primitive Roots

Definition

An integer gg is a primitive root modulo nn if:

ordn(g)=φ(n)\text{ord}_n(g) = \varphi(n)

where ordn(g)\text{ord}_n(g) is the smallest positive kk such that g^k \equiv 1 \pmod{n}.

Existence

Primitive roots exist modulo nn if and only if:

n{1,2,4,pk,2pk}n \in \{1, 2, 4, p^k, 2p^k\}

where pp is an odd prime.

Properties

If gg is a primitive root modulo prime pp:

{g0,g1,g2,,gp2}=(Z/pZ)\{g^0, g^1, g^2, \ldots, g^{p-2}\} = (\mathbb{Z}/p\mathbb{Z})^*

Number of primitive roots modulo pp: φ(p1)\varphi(p-1)

The Discrete Logarithm Problem

Given gg, hh, and prime pp where gg is a primitive root, find xx such that:

g^x \equiv h \pmod{p}

We write x=logghx = \log_g h.

Cryptographic significance: The security of Diffie-Hellman, ElGamal, and DSA relies on the computational difficulty of this problem.

Cryptographic Applications

RSA Cryptosystem

Key generation:

  1. Choose large primes p,qp, q
  2. Compute n=pqn = pq and φ(n)=(p1)(q1)\varphi(n) = (p-1)(q-1)
  3. Choose ee with gcd(e,φ(n))=1\gcd(e, \varphi(n)) = 1
  4. 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 pp, primitive root gg

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 xx, Public y \equiv g^x \pmod{p}

Encrypt message mm:

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

ConceptFormula
Congruencea \equiv b \pmod{n} \iff n \mid (a-b)
Inverse existsgcd(a,n)=1\gcd(a,n) = 1
Fermat's Little Theorema^{p-1} \equiv 1 \pmod{p}
Euler's Theorema^{\varphi(n)} \equiv 1 \pmod{n}
Totient (prime)φ(p)=p1\varphi(p) = p-1
Totient (general)φ(n)=npn(11p)\varphi(n) = n\prod_{p \mid n}\left(1 - \frac{1}{p}\right)
Euler's Criterion\left(\frac{a}{p}\right) \equiv a^{\frac{p-1}{2}} \pmod{p}

Practice Problems

  1. Compute 17^{-1} \bmod 43 using the Extended Euclidean Algorithm
  2. Find all solutions to x^2 \equiv 2 \pmod{7}
  3. Solve the system: x \equiv 2 \pmod{3}, x \equiv 3 \pmod{5}, x \equiv 2 \pmod{7}
  4. Find a primitive root modulo 1313
  5. 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.

Released under the MIT License.