After completing this tutorial, you should be able to:
Modern cryptography is built upon a foundation of pure mathematics—specifically, number theory. While classical ciphers required only basic arithmetic, the cryptographic algorithms that secure today's digital world—RSA, Diffie-Hellman, Elliptic Curve Cryptography, and AES—are deeply rooted in the properties of integers, modular arithmetic, prime numbers, and finite fields.
This tutorial provides the mathematical infrastructure necessary for understanding the cryptographic algorithms covered in subsequent tutorials. We will develop the number theory concepts essential to public-key cryptography (RSA, Diffie-Hellman, ECC) and symmetric cryptography (AES, which operates in finite fields).
The journey begins with the fundamental building blocks: divisibility, modular arithmetic, and prime numbers. We then explore the Euclidean algorithm (one of the oldest known algorithms) and its extended version, which is essential for computing modular inverses—a critical operation in RSA. Euler's and Fermat's theorems provide the mathematical justification for the RSA algorithm's correctness, and finite fields form the algebraic structure in which AES and many other modern ciphers operate.
This tutorial is mathematically rigorous but designed to be accessible to undergraduate students. We provide extensive worked examples, proofs of key theorems, and practical applications that connect the mathematics to cryptographic practice.
The security of modern cryptosystems rests on the presumed difficulty of certain mathematical problems:
Understanding these problems and the number theory behind them is essential for understanding why these cryptosystems are secure and how they are implemented.
In Tutorial 2.2, we explored classical cryptography and its historical context. The classical ciphers used simple arithmetic; modern cryptography requires deep number theory. This tutorial bridges that gap by providing the mathematical tools that will be used throughout the rest of Unit 2. Tutorials 2.4–2.10 (symmetric cryptography) will rely on finite fields; Tutorials 2.11–2.14 (public-key cryptography) will rely on modular arithmetic, prime numbers, and Euler's theorem.
We begin with the basic arithmetic of integers—the foundation upon which all number theory is built.
If d does not divide n, we write d ∤ n.
Properties of Divisibility:
n = p1e1 · p2e2 · … · pkek
where the pi are distinct primes and the ei are positive integers.Worked Example: Factor 360.
360 = 36 · 10 = (6²) · (2·5) = (2·3)² · 2·5 = 2² · 3² · 2 · 5 = 2³ · 3² · 5.
Thus 360 = 2³ · 3² · 5¹.
Modular arithmetic is arithmetic on a finite set of integers, {0, 1, 2, …, m – 1}, with operations defined modulo m. This is the arithmetic used in virtually all modern cryptosystems.
The integer m is called the modulus.
Basic Operations:
Properties of Modular Arithmetic:
Modular exponentiation—computing ae mod m—is a fundamental operation in public-key cryptography. Efficient algorithms (such as square-and-multiply) compute this in O(log e) steps.
Prime numbers are the building blocks of integers and form the foundation of many cryptographic systems. The distribution of primes, their density, and methods for finding them are of central importance.
The Prime Number Theorem states that the number of primes less than or equal to x, denoted π(x), satisfies:
π(x) ~ x / ln(x)
For example, there are approximately 1012 / ln(1012) ≈ 1012 / 27.6 ≈ 3.6 × 1010 primes less than 1012.
For cryptographic purposes, we need primes of specific sizes (e.g., 1024-bit, 2048-bit, or 3072-bit primes for RSA). The density of primes near n is about 1/ln(n), so for a 2048-bit number (≈ 10616), the probability that a randomly chosen number in that range is prime is about 1/1418. This means we can find primes efficiently by random sampling and testing.
Two integers a and b are relatively prime (or coprime) if gcd(a, b) = 1. The probability that two random integers are relatively prime is 6/π² ≈ 0.6079. This arises from the fact that the probability they share a prime factor p is 1/p², and the product over all primes of (1 – 1/p²) = 6/π².
The Euclidean algorithm, one of the oldest known algorithms (Euclid, c. 300 BCE), computes the GCD efficiently without factoring the numbers.
The extended Euclidean algorithm not only computes gcd(a, b) but also finds integers x and y such that:
a·x + b·y = gcd(a, b)
These coefficients are essential for computing modular inverses. When gcd(a, b) = 1, the equation becomes:
a·x + b·y = 1
Taking both sides modulo b, we get a·x ≡ 1 (mod b), so x is the modular inverse of a modulo b.
These two theorems provide the mathematical basis for RSA and other public-key cryptosystems. They relate exponentiation modulo n to the totient function.
ap−1 ≡ 1 (mod p)
Equivalently, for any integer a (not necessarily coprime to p), ap ≡ a (mod p).aφ(n) ≡ 1 (mod n)
where φ(n) is Euler's totient function (the number of integers 1 ≤ k ≤ n with gcd(k, n) = 1).Euler's theorem is a generalization of Fermat's Little Theorem, which is the special case where n = p is prime (φ(p) = p – 1).
Properties:
A field is a set equipped with two operations (addition and multiplication) that satisfy the usual algebraic properties: associativity, commutativity, distributivity, existence of additive and multiplicative identities (0 and 1), and existence of additive inverses and multiplicative inverses for all non-zero elements.
A finite field (or Galois field) is a field with a finite number of elements. Finite fields are denoted GF(q) where q = pm is a prime power.
There are two types of finite fields used in cryptography:
In GF(p), elements are integers modulo p. Addition and multiplication are performed modulo p. Every non-zero element has a multiplicative inverse because p is prime.
GF(2m) is the field of polynomials over GF(2) modulo an irreducible polynomial of degree m. Elements are represented as binary polynomials:
a(x) = am−1xm−1 + … + a1x + a0, where ai ∈ {0, 1}
Addition is bitwise XOR (polynomial addition). Multiplication is polynomial multiplication modulo the irreducible polynomial.
Why GF(2m) is important: AES uses the field GF(2⁸) with the irreducible polynomial x⁸ + x⁴ + x³ + x + 1 (0x11B). The S-box and MixColumns operations in AES are defined using arithmetic in this field.
In the context of modular arithmetic, the discrete logarithm problem (DLP) is:
Given a prime p, a generator g of Zp∗, and an element h ∈ Zp∗, find x such that gx ≡ h (mod p).
Why it's hard: Computing gx mod p (modular exponentiation) is easy (polynomial time). But reversing the operation—finding x from g, h, and p—is believed to be hard (subexponential time with current algorithms). This asymmetry is the foundation of the Diffie-Hellman key exchange and the Digital Signature Algorithm (DSA).
The ECDLP is the analog of the discrete logarithm problem in the group of points on an elliptic curve over a finite field. Given points P and Q on an elliptic curve, find k such that Q = k·P (scalar multiplication). The ECDLP is believed to be harder than the DLP in finite fields, allowing for smaller key sizes for equivalent security.
Generating large prime numbers for cryptographic key generation requires efficient primality testing. The goal is to determine whether a given large integer is prime (or, more practically, is prime with very high probability).
The simplest method: test divisibility by all primes ≤ √n. This is infeasible for large n (e.g., 1024-bit numbers).
Based on Fermat's Little Theorem: if n is prime and 1 ≤ a < n, then an−1 ≡ 1 (mod n). If we find an a such that this fails, n is composite. However, there are composite numbers (Carmichael numbers) that pass the test for all a coprime to n.
The Miller-Rabin test is a probabilistic test that is widely used in practice. It is based on the fact that if n is prime, the only square roots of 1 modulo n are ±1.
The Miller-Rabin test has a false positive rate of at most 4−k for k iterations. With k = 40, the error probability is negligible (≈ 10−24).
The AKS primality test (2002) is a deterministic polynomial-time algorithm, but it is too slow in practice for large numbers. For cryptographic key generation, probabilistic tests with multiple iterations are standard.
| Number Theory Concept | Cryptographic Application | Example Algorithm |
|---|---|---|
| Prime numbers, factorization | RSA security relies on difficulty of factoring n = p·q | RSA key generation |
| Modular arithmetic, Euler's theorem | RSA encryption/decryption, correctness proof | RSA |
| Discrete logarithms | Security of Diffie-Hellman and DSA | Diffie-Hellman, DSA |
| Finite fields (GF(p) and GF(2m)) | Elliptic curve cryptography, AES | ECC, AES |
| Extended Euclidean algorithm | Computing modular inverses for RSA and ECC | RSA key generation, ECC |
| Primality testing | Generating secure prime numbers for keys | Miller-Rabin |
| Euler's totient function | RSA key generation: φ(n) = (p–1)(q–1) | RSA |
| Chinese Remainder Theorem | Efficient RSA decryption (CRT optimization) | RSA-CRT |
Factor the following numbers into prime factors:
Compute the following:
Compute gcd(a, b) for the following pairs using the Euclidean algorithm:
Find the modular inverse of the following numbers modulo the given modulus:
Compute φ(n) for the following values of n:
Verify Euler's theorem for the following cases:
Suppose you choose p = 17 and q = 23 for RSA.
This exercise previews RSA key generation—you will use these same mathematical operations in Tutorial 2.12.
a. n = 17·23 = 391
b. φ(n) = (17–1)·(23–1) = 16·22 = 352
c. Choose e = 3. Check gcd(3,352) = 1 (since 352 mod 3 = 1). ✓
d. Find d such that 3·d ≡ 1 (mod 352). Extended Euclid: 352 = 117·3 + 1; 3 = 3·1 + 0. Back-substitute: 1 = 352 – 117·3 → 3·(−117) + 352·1 = 1, so d ≡ −117 ≡ 235 (mod 352).
e. 3·235 = 705; 705 mod 352 = 705 – 704 = 1. ✓
AES uses GF(2⁸) with irreducible polynomial x⁸ + x⁴ + x³ + x + 1 (0x11B).
a. Addition is XOR: (x⁷ + x⁵ + x² + 1) + (x⁶ + x⁵ + x³ + x) = x⁷ + x⁶ + (x⁵ + x⁵) + x³ + x² + x + 1 = x⁷ + x⁶ + x³ + x² + x + 1.
b. (x+1)·(x⁷ + x⁶ + x³ + x) = x·(x⁷+x⁶+x³+x) + 1·(x⁷+x⁶+x³+x) = x⁸+x⁷+x⁴+x² + x⁷+x⁶+x³+x = x⁸ + (x⁷+x⁷) + x⁶ + x⁴ + x³ + x² + x = x⁸ + x⁶ + x⁴ + x³ + x² + x. Now reduce modulo x⁸+x⁴+x³+x+1: x⁸ ≡ x⁴+x³+x+1 (since x⁸ = x⁸, subtract to get 0). So result = (x⁴+x³+x+1) + x⁶+x⁴+x³+x²+x = x⁶ + (x⁴+x⁴) + (x³+x³) + x² + (x+x) + 1 = x⁶ + x² + 1.
Let p = 17 and g = 3 (a primitive root modulo 17).
a. Powers of 3 mod 17: 3⁰=1, 3¹=3, 3²=9, 3³=27≡10, 3⁴=30≡13, 3⁵=39≡5, 3⁶=15, 3⁷=45≡11, 3⁸=33≡16, 3⁹=48≡14, 3¹⁰=42≡8, 3¹¹=24≡7, 3¹²=21≡4, 3¹³=12, 3¹⁴=36≡2, 3¹⁵=6. All non-zero elements {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16} appear, so 3 is primitive.
b. From the table, 3⁴ ≡ 13 (mod 17), so log₃(13) = 4.
c. From the table, 3⁵ ≡ 5 (mod 17), so log₃(5) = 5.
Apply the Miller-Rabin test to n = 221 with a = 174. Show all steps and determine whether 221 is prime or composite.
Hint: n−1 = 220 = 2²·55, so s=2 and d=55.
n = 221, n−1 = 220 = 2²·55 → s=2, d=55.
a = 174.
x = ad mod n = 174⁵⁵ mod 221.
Compute using modular exponentiation: 174² = 30276; 30276 mod 221: 221·137 = 30277, so 174² ≡ −1 ≡ 220 (mod 221).
Then 174⁵⁵ = 174²·²⁷·¹ = (174²)²⁷·174. Since 174² ≡ −1, (174²)²⁷ ≡ (−1)²⁷ = −1 ≡ 220. So x = 220·174 = 38280; 38280 mod 221: 221·173 = 38233, remainder 47. So x = 47.
Since x ≠ 1 and x ≠ n−1 (i.e., 220), we compute x² mod n: 47²=2209; 2209 mod 221: 221·9 = 1989, remainder 220. So x² ≡ 220 = n−1.
Since we found a square root of 1 (x² ≡ −1), this is a "likely prime" result for this test.
However, 221 = 13·17, so 221 is composite. Miller-Rabin with a single base did not catch it (it's a strong pseudoprime to base 174). Additional bases would catch it.
Research the AKS primality test (Agrawal–Kayal–Saxena, 2002). Write a 300-word summary that addresses:
A complete answer would explain that AKS was the first deterministic polynomial-time primality test (O(log¹² n) time). The key idea is based on the identity (x + a)ⁿ ≡ xⁿ + a (mod n) for all a when n is prime. The test checks this identity modulo (xʳ – 1) for carefully chosen r. Although theoretically significant, AKS is not practical for the large numbers used in cryptography (it is slower than Miller-Rabin). For cryptographic applications, probabilistic tests like Miller-Rabin remain the standard because they are fast and provide negligible error probability. However, AKS provides a theoretical guarantee that the problem of primality testing is in P, which has important theoretical implications.
This tutorial has provided the mathematical foundation necessary for understanding modern cryptographic algorithms. We began with the basic concepts of integer arithmetic, divisibility, and prime numbers, establishing the Fundamental Theorem of Arithmetic as the cornerstone.
We explored modular arithmetic in detail, including the concept of modular inverses and the conditions under which they exist. The Euclidean algorithm and its extended version provide efficient methods for computing GCDs and modular inverses—operations that are used repeatedly in cryptographic implementations.
Fermat's Little Theorem and Euler's Theorem provide the mathematical basis for RSA and other public-key systems. We saw how Euler's totient function φ(n) enables the RSA key generation process, and how Euler's theorem guarantees that encryption followed by decryption returns the original message.
Finite fields—particularly GF(p) and GF(2m)—provide the algebraic structures used in AES, ECC, and many other algorithms. The discrete logarithm problem, both in finite fields and on elliptic curves, is the basis for Diffie-Hellman and ECC security.
Finally, we examined primality testing, an essential component of cryptographic key generation. The Miller-Rabin test provides an efficient probabilistic method for generating large primes, which is used in practice for RSA and other public-key systems.
With this mathematical foundation in place, we are now prepared to explore cryptographic algorithms in depth—starting with symmetric encryption in the next tutorial.
In Tutorial 2.4: Symmetric Encryption Fundamentals, we will apply the concepts of finite fields and modular arithmetic to understand the design and operation of symmetric key ciphers. We will explore the principles of confusion and diffusion (introduced in Tutorial 2.1) and see how they are implemented using the mathematical structures developed here.