Modular Arithmetic

Introduction

Modular arithmetic is the arithmetic of remainders. Instead of working with all integers, we work with a finite set of remainders when dividing by a fixed modulus. This creates a cyclical number system where arithmetic "wraps around."

Modular arithmetic is fundamental in cryptography (RSA, Diffie-Hellman), computer science (hash functions, checksums), and pure mathematics (studying prime numbers, solving Diophantine equations). The familiar 12-hour clock is an everyday example of arithmetic modulo 12.

Congruence

We say a a is congruent to b b modulo n n written:

a≡b⁢(mod⁡(n)) a is identical to b times mod n

if n n divides a−b a minus b i.e., a−b=k⁢n a minus b equals k n for some integer. Equivalently, a a and b b have the same remainder when divided by n n

Examples: 17≡2⁢(mod⁡(5)) 17 is identical to 2 times mod 5 (since 17−2=15=3⋅5 17 minus 2 equals 15 equals 3 times 5). Also 3≡4⁢(mod⁡(7)) 3 is identical to 4 times mod 7.

Properties of Congruence

Congruence is an equivalence relation:

• Reflexive: a≡a⁢(mod⁡(n)) a is identical to a times mod n

• Symmetric: a≡b⇒b≡a a is identical to b right double arrow b is identical to a

• Transitive: a≡b a is identical to b and b≡c⇒a≡c b is identical to c right double arrow a is identical to c

Arithmetic Operations

If a≡b⁢(mod⁡(n)) a is identical to b times mod n and c≡d⁢(mod⁡(n)) c is identical to d times mod n then:

a+c≡b+d⁢(mod⁡(n)) a plus c is identical to b plus d times mod n

a−c≡b−d⁢(mod⁡(n)) a minus c is identical to b minus d times mod n

a⋅c≡b⋅d⁢(mod⁡(n)) a times c is identical to b times d times mod n

We can add, subtract, and multiply congruences freely. Division is more delicate—we need multiplicative inverses.

Multiplicative Inverses

The multiplicative inverse of a modulo n n is an integer a−1 a to the negative 1 power such that:

a⋅a−1≡1⁢(mod⁡(n)) a times a to the negative 1 power is identical to 1 times mod n

An inverse exists if and only if gcd⁡(a,n)=1 the gcd of open paren a comma n close paren equals 1 When it exists, it's unique modulo n n and can be found using the extended Euclidean algorithm.

Example: Find 3−1⁢(mod⁡(7)) 3 to the negative 1 power times mod 7. We need 3⁢x≡1⁢(mod⁡(7)) 3 x is identical to 1 times mod 7. Testing: 3⋅5=15=2⋅7+1≡1 3 times 5 equals 15 equals 2 times 7 plus 1 is identical to 1. So 3−1≡5⁢(mod⁡(7)) 3 to the negative 1 power is identical to 5 times mod 7.

The Group ℤ/nℤ

The integers modulo n n form the set. Under addition, this is a group of order n n.

The units (elements with multiplicative inverses) form the group (ℤ/n⁢ℤ)∗={a:gcd⁡(a,n)=1} open paren the integers divided by n the integers close paren raised to the asterisk power equals the set of all a such that the gcd of open paren a comma n close paren equals 1 under multiplication.

Euler's Totient Function

ϕ⁡(n) phi of n counts integers from 1 1 to n n that are coprime to n n. It equals |(ℤ/n⁢ℤ)∗| the absolute value of open paren the integers divided by n the integers close paren raised to the asterisk power. For prime p p: ϕ⁡(p)=p−1 phi of p equals p minus 1

For prime power: ϕ⁡(pk)=pk−pk−1=pk−1⁢(p−1) phi of open paren p to the k-th power close paren equals p to the k-th power minus p raised to the k minus 1 power equals p raised to the k minus 1 power times open paren p minus 1 close paren

Multiplicative property: If gcd⁡(m,n)=1 the gcd of open paren m comma n close paren equals 1 then ϕ⁢(m⁢n)=ϕ⁡(m)⁢ϕ⁡(n) phi times m n equals phi of m phi of n

General formula: ϕ⁡(n)=n⁢∏p⁢n∣(1−1p) phi of n equals n the product over p n vertical bar of open paren 1 minus 1 over p close paren

Fermat's Little Theorem

If p p is prime and gcd⁡(a,p)=1 the gcd of open paren a comma p close paren equals 1 then:

ap−1≡1⁢(mod⁡(p)) a raised to the p minus 1 power is identical to 1 times mod p

Equivalently, for any a a ap≡a⁢(mod⁡(p)) a to the p-th power is identical to a times mod p

This gives an easy way to compute inverses: a−1≡ap−2⁢(mod⁡(p)) a to the negative 1 power is identical to a raised to the p minus 2 power times mod p

Euler's Theorem

Generalizes Fermat's Little Theorem. If gcd⁡(a,n)=1 the gcd of open paren a comma n close paren equals 1 then:

aϕ⁡(n)≡1⁢(mod⁡(n)) a raised to the phi of n power is identical to 1 times mod n

For prime n=p n equals p we get ϕ⁡(p)=p−1 phi of p equals p minus 1 recovering Fermat's Little Theorem.

Chinese Remainder Theorem

If n1,n2,…,nk n sub 1 comma n sub 2 comma dot dot dot comma n sub k are pairwise coprime, then the system:

x≡a1⁢(mod⁡(n1)),x≡a2⁢(mod⁡(n2)),…,x≡ak⁢(mod⁡(nk)) x is identical to a sub 1 times open paren the mod of open paren n sub 1 close paren close paren comma x is identical to a sub 2 times open paren the mod of open paren n sub 2 close paren close paren comma dot dot dot comma x is identical to a sub k times open paren the mod of open paren n sub k close paren close paren

has a unique solution modulo N=n1⁢n2⋯nk N equals n sub 1 n sub 2 dot dot dot n sub k

This means ℤ/N⁢ℤ≅ℤ/n1⁢ℤ×⋯×ℤ/nk⁢ℤ the integers divided by N the integers approximately equals the integers divided by n sub 1 the integers times dot dot dot times the integers divided by n sub k the integers as rings.

Linear Congruences

The equation a⁢x≡b⁢(mod⁡(n)) a x is identical to b times mod n has a solution iff d=gcd⁡(a,n) d equals the gcd of open paren a comma n close paren divides b b

If solvable, there are exactly d d solutions modulo. When gcd⁡(a,n)=1 the gcd of open paren a comma n close paren equals 1 there's a unique solution: x≡a−1⁢b⁢(mod⁡(n)) x is identical to a to the negative 1 power b times mod n

Modular Exponentiation

Computing ak⁢mod⁡(n) a to the k-th power mod n efficiently uses repeated squaring (fast exponentiation):

• If k k is even: ak=(ak/2)2 a to the k-th power equals a raised to the k divided by 2 power squared

• If k k is odd: ak=a⋅ak−1 a to the k-th power equals a times a raised to the k minus 1 power

This requires only O⁢(log⁡k) O times log k multiplications, essential for cryptography.

RSA Cryptography

RSA is a public-key encryption system based on modular arithmetic. Key generation:

  1. Choose large primes p p and q q compute n=p⁢q n equals p q

  2. Compute ϕ⁡(n)=(p−1)⁢(q−1) phi of n equals open paren p minus 1 close paren times open paren q minus 1 close paren

  3. Choose e e coprime to ϕ⁡(n) phi of n find d≡e−1⁢(mod⁡(ϕ⁡(n))) d is identical to e to the negative 1 power times mod phi of n

Public key: (n,e) open paren n comma e close paren Private key: d d

Encryption: c≡me⁢(mod⁡(n)) c is identical to m to the e-th power times mod n Decryption: m≡cd⁢(mod⁡(n)) m is identical to c to the d-th power times mod n

Security relies on the difficulty of factoring n n into p p and q q

Primality Testing

Fermat's test: If an−1≢1⁢(mod⁡(n)) a raised to the n minus 1 power is not identical to 1 times mod n for some a a coprime to n n then n n is composite.

Passing the test doesn't guarantee primality (Carmichael numbers are exceptions). Miller-Rabin is a stronger probabilistic test used in practice.

Wilson's Theorem

p p is prime if and only if:

(p−1)!≡−1⁢(mod⁡(p)) open paren p minus 1 close paren factorial is identical to negative 1 times mod p

This is a theoretical characterization of primes (computing factorials is too slow for practical primality testing).

Quadratic Residues

a a is a quadratic residue mod p p if x2≡a⁢(mod⁡(p)) x squared is identical to a times mod p has a solution. Exactly half of the non-zero elements mod p p are quadratic residues.

Euler's criterion: a a is a QR iff a(p−1)/2≡1⁢(mod⁡(p)) a raised to the open paren p minus 1 close paren divided by 2 power is identical to 1 times mod p

Summary

Modular arithmetic works with remainders: a≡b⁢(mod⁡(n)) a is identical to b times mod n means n|(a−b) n divides open paren a minus b close paren Addition, subtraction, and multiplication preserve congruence. Division requires multiplicative inverses.

Euler's totient ϕ⁡(n) phi of n counts units; Euler's theorem says aϕ⁡(n)≡1 a raised to the phi of n power is identical to 1 when gcd⁡(a,n)=1 the gcd of open paren a comma n close paren equals 1 The Chinese Remainder Theorem solves systems of congruences.

RSA cryptography is built on modular exponentiation and the difficulty of factoring. Modular arithmetic is essential in computer science, cryptography, and number theory.