Skip to content

Number Theory: The Mathematical Foundation of Cryptographic Security

Number theory is often called the "queen of mathematics" – a field of pure mathematical beauty that has found profound practical applications in our digital age. What makes number theory perfect for cryptography is its abundance of problems that are computationally easy in one direction but exponentially hard to reverse. Let's explore the mathematical structures that protect everything from your online banking to secure messaging.

The Fundamental Building Blocks

Divisibility: The Foundation of Everything

Divisibility is where number theory begins, and it's more subtle than it first appears. When we say integer a divides integer b (written a|b), we mean there exists an integer k such that b = ak.

Formal Definition: For integers a, b with a ≠ 0, we say a divides b (a|b) if there exists an integer k such that b = ak.

Examples:

  • 7|21 because 21 = 7 × 3
  • 5|0 because 0 = 5 × 0 (every nonzero integer divides 0)
  • 3∤10 because there's no integer k where 10 = 3k

Fundamental Properties of Divisibility:

  1. Reflexivity: a|a for all nonzero a
  2. Transitivity: If a|b and b|c, then a|c
  3. Linearity: If a|b and a|c, then a|(bx + cy) for any integers x, y
  4. Multiplicativity: If a|b, then ac|bc for any nonzero c
  5. Antisymmetry: If a|b and b|a, then a = ±b

Proof of Transitivity (Example): Given: a|b and b|c To prove: a|c

Since a|b, there exists integer k₁ such that b = ak₁ Since b|c, there exists integer k₂ such that c = bk₂ Substituting: c = (ak₁)k₂ = a(k₁k₂) Since k₁k₂ is an integer, we have a|c. ∎

The Division Algorithm: The Cornerstone Theorem

The Division Algorithm is fundamental to all of number theory and provides the mathematical foundation for modular arithmetic.

Theorem (Division Algorithm): For any integers a and b with b > 0, there exist unique integers q (quotient) and r (remainder) such that: a = bq + r, where 0 ≤ r < b

Proof Sketch:Existence: Consider the set S = {a - bk : k ∈ ℤ, a - bk ≥ 0}. This set is non-empty and bounded below by 0, so by the Well-Ordering Principle, it has a smallest element r = a - bq for some integer q. We can show that r < b.

Uniqueness: Suppose a = bq₁ + r₁ = bq₂ + r₂ with 0 ≤ r₁, r₂ < b. Then b(q₁ - q₂) = r₂ - r₁. Since |r₂ - r₁| < b, we must have q₁ = q₂ and r₁ = r₂. ∎

Cryptographic Significance: This theorem underlies all modular arithmetic operations used in cryptography. When we compute a mod n, we're finding the remainder r in the division algorithm.

Modular Arithmetic: Arithmetic in Finite Systems

Modular arithmetic creates finite number systems that are perfect for cryptographic applications.

Definition: We say a is congruent to b modulo n (written a ≡ b (mod n)) if n|(a - b).

Equivalent Characterizations:

  1. a ≡ b (mod n) ⟺ n|(a - b)
  2. a ≡ b (mod n) ⟺ a and b have the same remainder when divided by n
  3. a ≡ b (mod n) ⟺ a = b + kn for some integer k

Properties of Modular Congruence:

Reflexivity: a ≡ a (mod n) Symmetry: If a ≡ b (mod n), then b ≡ a (mod n) Transitivity: If a ≡ b (mod n) and b ≡ c (mod n), then a ≡ c (mod n)

Arithmetic Properties: If a ≡ b (mod n) and c ≡ d (mod n), then:

  • a + c ≡ b + d (mod n)
  • a - c ≡ b - d (mod n)
  • ac ≡ bd (mod n)
  • aᵏ ≡ bᵏ (mod n) for any positive integer k

The Ring ℤ/nℤ: The integers modulo n form a ring under addition and multiplication modulo n. This algebraic structure is fundamental to understanding cryptographic operations.

The Greatest Common Divisor: Measuring Commonality

The greatest common divisor is central to many cryptographic algorithms, particularly in key generation and modular arithmetic.

Definition: For integers a and b (not both zero), gcd(a,b) is the largest positive integer that divides both a and b.

Properties of GCD:

  1. gcd(a,b) = gcd(b,a) (symmetry)
  2. gcd(a,b) = gcd(|a|,|b|) (sign doesn't matter)
  3. gcd(a,0) = |a| for a ≠ 0
  4. gcd(a,b) = gcd(a-kb,b) for any integer k
  5. If d = gcd(a,b), then gcd(a/d, b/d) = 1

The Euclidean Algorithm: Ancient Efficiency

The Euclidean Algorithm, dating to around 300 BCE, remains the most efficient method for computing GCDs.

Algorithm:

gcd(a,b):
    while b ≠ 0:
        temp = b
        b = a mod b
        a = temp
    return a

Mathematical Analysis: The algorithm terminates because the sequence of remainders is strictly decreasing and non-negative. The number of steps is O(log min(a,b)).

Proof of Correctness: At each step, we have gcd(a,b) = gcd(b, a mod b). This follows from the fact that any common divisor of a and b is also a common divisor of b and a mod b, and vice versa.

Example Trace: gcd(252, 105)

252 = 2 × 105 + 42    gcd(252,105) = gcd(105,42)
105 = 2 × 42 + 21     gcd(105,42) = gcd(42,21)
42 = 2 × 21 + 0       gcd(42,21) = gcd(21,0) = 21

The Extended Euclidean Algorithm: Finding Linear Combinations

The Extended Euclidean Algorithm not only finds gcd(a,b) but also finds integers x and y such that ax + by = gcd(a,b).

Bézout's Identity: For any integers a and b, there exist integers x and y such that ax + by = gcd(a,b).

Algorithm:

python
def extended_gcd(a, b):
    if b == 0:
        return a, 1, 0
    else:
        gcd, x1, y1 = extended_gcd(b, a % b)
        x = y1
        y = x1 - (a // b) * y1
        return gcd, x, y

Cryptographic Application: This algorithm is essential for finding multiplicative inverses in modular arithmetic, which is crucial for RSA key generation.

Modular Arithmetic: The Heart of Cryptographic Operations

Multiplicative Inverses and the Group (ℤ/nℤ)*

Definition: The multiplicative inverse of a modulo n is an integer x such that ax ≡ 1 (mod n).

Existence Theorem: The multiplicative inverse of a modulo n exists if and only if gcd(a,n) = 1.

Proof: (⟹) If ax ≡ 1 (mod n), then ax = 1 + kn for some integer k, so ax - kn = 1. Any common divisor of a and n must divide 1, so gcd(a,n) = 1.

(⟸) If gcd(a,n) = 1, then by Bézout's identity, there exist integers x and y such that ax + ny = 1. This means ax ≡ 1 (mod n), so x is the multiplicative inverse of a modulo n. ∎

Computing Inverses: Use the Extended Euclidean Algorithm to find x such that ax + ny = 1, then x ≡ a⁻¹ (mod n).

The Group (ℤ/nℤ)*: The set of integers modulo n that are coprime to n forms a group under multiplication modulo n. This group has φ(n) elements, where φ is Euler's totient function.

Euler's Totient Function: Counting Coprime Integers

Definition: For a positive integer n, Euler's totient function φ(n) counts the number of integers from 1 to n that are coprime to n.

Examples:

  • φ(1) = 1
  • φ(p) = p - 1 for prime p
  • φ(pᵏ) = pᵏ - pᵏ⁻¹ = pᵏ⁻¹(p - 1) for prime p
  • φ(mn) = φ(m)φ(n) if gcd(m,n) = 1 (multiplicative property)

General Formula: For n = p₁^a₁ × p₂^a₂ × ... × pₖ^aₖ, φ(n) = n × ∏(1 - 1/pᵢ) = n × ∏((pᵢ - 1)/pᵢ)

Proof of Multiplicativity: If gcd(m,n) = 1, then by the Chinese Remainder Theorem, there's a bijection between ℤ/mnℤ and ℤ/mℤ × ℤ/nℤ that preserves coprimality. Therefore, φ(mn) = φ(m)φ(n). ∎

Cryptographic Significance: In RSA, φ(n) where n = pq determines the relationship between public and private exponents.

Euler's Theorem and Fermat's Little Theorem

Euler's Theorem: If gcd(a,n) = 1, then a^φ(n) ≡ 1 (mod n).

Proof Sketch: Consider the set S = {a₁, a₂, ..., aφ(n)} of all integers from 1 to n that are coprime to n. The set aS = {aa₁, aa₂, ..., aaφ(n)} (mod n) is a permutation of S. Therefore: ∏aᵢ ≡ ∏(aaᵢ) ≡ a^φ(n) ∏aᵢ (mod n)

Since gcd(∏aᵢ, n) = 1, we can cancel to get a^φ(n) ≡ 1 (mod n). ∎

Fermat's Little Theorem: If p is prime and gcd(a,p) = 1, then a^(p-1) ≡ 1 (mod p).

This is a special case of Euler's theorem since φ(p) = p - 1 for prime p.

Applications:

  1. Modular exponentiation: Computing a^(-1) ≡ a^(φ(n)-1) (mod n)
  2. RSA decryption: The relationship ed ≡ 1 (mod φ(n)) ensures that (m^e)^d ≡ m (mod n)
  3. Primality testing: Fermat's theorem forms the basis for probabilistic primality tests

Advanced Number Theoretic Concepts

The Chinese Remainder Theorem: Parallel Computation

Theorem: Let n₁, n₂, ..., nₖ be pairwise coprime positive integers, and let n = n₁n₂...nₖ. Then the system of congruences: x ≡ a₁ (mod n₁) x ≡ a₂ (mod n₂) ... x ≡ aₖ (mod nₖ)

has a unique solution modulo n.

Constructive Proof: Let Nᵢ = n/nᵢ for each i. Since gcd(Nᵢ, nᵢ) = 1, there exists Mᵢ such that NᵢMᵢ ≡ 1 (mod nᵢ).

The solution is: x ≡ ∑aᵢNᵢMᵢ (mod n)

Cryptographic Applications:

  1. RSA optimization: CRT speeds up RSA decryption by factor of 4
  2. Secret sharing: Splitting secrets across multiple moduli
  3. Parallel computation: Breaking large modular arithmetic into smaller problems

RSA-CRT Implementation: Instead of computing m ≡ c^d (mod n), compute:

  • m₁ ≡ c^(d mod (p-1)) (mod p)
  • m₂ ≡ c^(d mod (q-1)) (mod q)
  • Combine using CRT to get m

Quadratic Residues: Square Roots Modulo Primes

Definition: An integer a is a quadratic residue modulo n if there exists an integer x such that x² ≡ a (mod n).

Legendre Symbol: For odd prime p and integer a not divisible by p: (a/p) = { 1 if a is a quadratic residue mod p {-1 if a is a quadratic non-residue mod p

Properties of Legendre Symbol:

  1. (a/p) ≡ a^((p-1)/2) (mod p) (Euler's criterion)
  2. (ab/p) = (a/p)(b/p) (multiplicativity)
  3. (a²/p) = 1 for gcd(a,p) = 1
  4. (-1/p) = (-1)^((p-1)/2)
  5. (2/p) = (-1)^((p²-1)/8)

Quadratic Reciprocity Law: For distinct odd primes p and q: (p/q)(q/p) = (-1)^((p-1)(q-1)/4)

This beautiful theorem, called the "gem of number theory" by Gauss, provides a way to compute Legendre symbols efficiently.

Cryptographic Applications:

  1. Rabin cryptosystem: Based on difficulty of computing square roots modulo n
  2. Goldwasser-Micali encryption: Uses quadratic residuosity for semantic security
  3. Zero-knowledge proofs: Quadratic residuosity is used in some ZK protocols

Primitive Roots and Discrete Logarithms

Definition: An integer g is a primitive root modulo n if the order of g modulo n is φ(n).

Existence Theorem: Primitive roots modulo n exist if and only if n = 1, 2, 4, pᵏ, or 2pᵏ where p is an odd prime and k ≥ 1.

Properties:

  1. If g is a primitive root mod p, then {g⁰, g¹, g², ..., g^(p-2)} generates all non-zero elements mod p
  2. The number of primitive roots mod p is φ(φ(p)) = φ(p-1)

The Discrete Logarithm Problem: Given g, h, and p where g is a primitive root mod p, find x such that g^x ≡ h (mod p).

Cryptographic Significance:

  1. Diffie-Hellman: Security based on discrete logarithm problem
  2. ElGamal: Encryption and signatures based on discrete logs
  3. DSA: Digital Signature Algorithm uses discrete logarithms

Computing Discrete Logarithms:

  • Baby-step Giant-step: O(√p) time and space
  • Pollard's rho: O(√p) expected time, constant space
  • Index calculus: Sub-exponential for large primes
  • Pohlig-Hellman: Reduces to prime power subgroups

Continued Fractions and Cryptanalysis

Definition: A continued fraction representation of a real number α is: α = a₀ + 1/(a₁ + 1/(a₂ + 1/(a₃ + ...)))

Written as α = [a₀; a₁, a₂, a₃, ...]

Convergents: The rational approximations pₖ/qₖ obtained by truncating the continued fraction.

Properties:

  1. Convergents provide the best rational approximations
  2. |α - pₖ/qₖ| < 1/(qₖqₖ₊₁)
  3. If |α - p/q| < 1/(2q²), then p/q is a convergent of α

Cryptanalytic Applications:

Wiener's Attack on RSA: If the private exponent d is small (d < N^(1/4)/3), then d can be recovered using continued fractions applied to e/N.

Algorithm:

  1. Compute continued fraction expansion of e/N
  2. For each convergent k/d, check if φ(N) = (ed-1)/k is an integer
  3. If so, solve N - φ(N) + 1 = 0 to find p and q

Boneh-Durfee Attack: Extends Wiener's attack to d < N^0.292 using lattice methods.

Computational Number Theory

Efficient Modular Exponentiation

Computing a^b mod n efficiently is crucial for cryptographic operations.

Binary Exponentiation (Square-and-Multiply):

python
def mod_exp(base, exp, mod):
    result = 1
    base = base % mod
    while exp > 0:
        if exp % 2 == 1:
            result = (result * base) % mod
        exp = exp >> 1
        base = (base * base) % mod
    return result

Complexity: O(log b) multiplications, each taking O(log² n) time.

Montgomery Multiplication: Optimizes modular multiplication by avoiding division.

Sliding Window Methods: Precompute powers to reduce the number of multiplications.

Primality Testing: Distinguishing Primes from Composites

Deterministic Tests:

  • Trial Division: O(√n) - only practical for small numbers
  • AKS Test: O(log⁶ n) - polynomial time but impractical

Probabilistic Tests:

  • Fermat Test: Based on Fermat's Little Theorem
  • Miller-Rabin: Most widely used in practice
  • Solovay-Strassen: Based on Jacobi symbols

Miller-Rabin Analysis: For odd composite n, at least 3/4 of the bases a ∈ {1, 2, ..., n-1} are witnesses to n's compositeness. Therefore, k rounds of Miller-Rabin give error probability ≤ (1/4)^k.

Integer Factorization: The Hard Problem

Classical Methods:

  • Trial Division: O(√n)
  • Pollard's rho: O(n^(1/4)) expected time
  • Pollard's p-1: Effective when p-1 has only small prime factors

Modern Methods:

  • Quadratic Sieve: O(exp(√(ln n ln ln n))) for numbers up to ~100 digits
  • General Number Field Sieve: O(exp((64/9)^(1/3)(ln n)^(1/3)(ln ln n)^(2/3))) - best known classical algorithm

Quantum Algorithm:

  • Shor's Algorithm: O((log n)³) on quantum computer - polynomial time!

Algebraic Number Theory in Cryptography

Cyclotomic Fields and Lattice Cryptography

Cyclotomic Polynomials: Φₙ(x) is the minimal polynomial of primitive nth roots of unity.

Ring of Integers: In the cyclotomic field ℚ(ζₙ), the ring of integers has special structure useful for lattice-based cryptography.

Applications:

  1. NTRU: Uses polynomial rings related to cyclotomic fields
  2. Ring-LWE: Learning With Errors over polynomial rings
  3. Post-quantum cryptography: Many candidates use algebraic structures

Elliptic Curves: Geometry Meets Number Theory

Weierstrass Form: y² = x³ + ax + b over a field K

Group Law: Points on an elliptic curve form an abelian group under a geometric addition operation.

Point Addition:

  • Point at infinity O: Identity element
  • Inverse: P + (-P) = O where -P = (x, -y) for P = (x, y)
  • Addition formula: For P₁ = (x₁, y₁) and P₂ = (x₂, y₂):
    • If x₁ ≠ x₂: λ = (y₂ - y₁)/(x₂ - x₁), x₃ = λ² - x₁ - x₂, y₃ = λ(x₁ - x₃) - y₁
    • If P₁ = P₂: λ = (3x₁² + a)/(2y₁), x₃ = λ² - 2x₁, y₃ = λ(x₁ - x₃) - y₁

Elliptic Curve Discrete Logarithm Problem (ECDLP): Given points P and Q on an elliptic curve, find integer k such that Q = kP.

Advantages of ECC:

  1. Smaller key sizes: 256-bit ECC ≈ 3072-bit RSA security
  2. Faster operations: Especially on constrained devices
  3. Lower bandwidth: Smaller signatures and keys

Pairings and Advanced Cryptography

Bilinear Pairings: Functions e: G₁ × G₂ → Gₜ with special properties:

  1. Bilinearity: e(aP, bQ) = e(P, Q)^(ab)
  2. Non-degeneracy: e(P, Q) ≠ 1 for some P, Q
  3. Computability: e can be computed efficiently

Applications:

  1. Identity-based encryption: Boneh-Franklin scheme
  2. Short signatures: BLS signatures
  3. Zero-knowledge proofs: zk-SNARKs use pairings extensively

Analytic Number Theory and Cryptography

The Riemann Zeta Function and Prime Distribution

Riemann Zeta Function: ζ(s) = ∑(n=1 to ∞) 1/n^s for Re(s) > 1

Euler Product Formula: ζ(s) = ∏(p prime) 1/(1 - p^(-s))

This connects the zeta function to prime numbers, showing the deep relationship between analysis and number theory.

Prime Number Theorem: π(x) ~ x/ln(x) as x → ∞, where π(x) counts primes ≤ x.

Riemann Hypothesis: All non-trivial zeros of ζ(s) have real part 1/2.

Cryptographic Implications:

  1. Prime generation: Estimates how many candidates to test
  2. Security analysis: Understanding prime gaps and distribution
  3. Quantum algorithms: Some quantum algorithms use number-theoretic functions

L-functions and Cryptanalysis

Dirichlet L-functions: Generalizations of the Riemann zeta function associated with characters modulo n.

Applications:

  1. Primality proving: Using L-functions to prove primality
  2. Factoring algorithms: Some methods use analytic number theory
  3. Cryptanalysis: Understanding the distribution of cryptographic parameters

Computational Complexity and Cryptographic Hardness

Complexity Classes and Cryptographic Assumptions

P: Problems solvable in polynomial time NP: Problems verifiable in polynomial time
BPP: Problems solvable in polynomial time with bounded error probability

Cryptographic Hardness Assumptions:

  1. Integer Factorization: No polynomial-time algorithm known
  2. Discrete Logarithm: Hard in certain groups
  3. RSA Problem: Computing eth roots modulo n
  4. Diffie-Hellman Problems: Various formulations of DH hardness

Reductions: Showing that breaking one cryptographic scheme implies solving a hard mathematical problem.

Average-Case vs. Worst-Case Hardness

Worst-case hardness: Problem is hard for some inputs Average-case hardness: Problem is hard for random inputs

Lattice-based cryptography: Some schemes have worst-case to average-case reductions, providing strong security guarantees.

Applications to Modern Cryptographic Systems

RSA: Number Theory in Practice

Key Generation:

  1. Choose large primes p, q
  2. Compute n = pq, φ(n) = (p-1)(q-1)
  3. Choose e with gcd(e, φ(n)) = 1
  4. Compute d ≡ e^(-1) (mod φ(n))

Security Analysis:

  • Factoring attacks: If n can be factored, RSA is broken
  • Low exponent attacks: Small e or d can be vulnerable
  • Side-channel attacks: Implementation must protect against timing/power analysis

Elliptic Curve Cryptography: Geometric Number Theory

ECDSA (Elliptic Curve Digital Signature Algorithm):

  1. Key generation: Private key d, public key Q = dP
  2. Signing: Choose random k, compute r = (kP)ₓ mod n, s = k^(-1)(H(m) + dr) mod n
  3. Verification: Check if r = ((H(m)s^(-1))P + (rs^(-1))Q)ₓ mod n

Security: Based on ECDLP hardness and proper implementation.

Post-Quantum Cryptography: Beyond Classical Number Theory

Lattice-based schemes: Use hard problems in high-dimensional lattices Code-based schemes: Based on error-correcting codes Multivariate schemes: Solving systems of polynomial equations Hash-based signatures: Based on one-way hash functions

The Beauty and Depth of Cryptographic Number Theory

Number theory in cryptography represents one of the most beautiful applications of pure mathematics to practical problems. The field connects:

  • Ancient mathematics: Euclid's algorithm (300 BCE) still optimizes modern cryptography
  • Classical results: Fermat's and Euler's theorems enable RSA
  • Modern discoveries: Elliptic curves provide efficient cryptography
  • Future challenges: Post-quantum cryptography requires new mathematical foundations

Unsolved Problems with Cryptographic Relevance

  1. Integer factorization: Is there a polynomial-time classical algorithm?
  2. Discrete logarithm: Hardness in various groups
  3. Elliptic curve discrete logarithm: Security of ECC
  4. Lattice problems: Foundation of post-quantum cryptography
  5. Isogeny problems: Basis for some post-quantum schemes

The Continuing Evolution

As quantum computers threaten current cryptographic systems, number theorists and cryptographers are developing new mathematical foundations:

  • Lattice theory: High-dimensional geometry for security
  • Coding theory: Error-correcting codes for cryptography
  • Multivariate algebra: Polynomial systems for signatures
  • Hash functions: Information-theoretic security

The mathematical beauty of number theory ensures its continued central role in cryptography, whether in defending against quantum computers or enabling new cryptographic capabilities we can barely imagine today.

Practical Exploration and Further Study

Programming Exercises

  1. Implement the Extended Euclidean Algorithm and use it to compute modular inverses
  2. Build a complete RSA system from prime generation to encryption/decryption
  3. Explore quadratic residues by implementing the Legendre symbol and Tonelli-Shanks algorithm
  4. Implement elliptic curve operations and build ECDSA
  5. Study lattice reduction with the LLL algorithm

Mathematical Investigations

  1. Verify number-theoretic theorems with computational experiments
  2. Analyze the distribution of quadratic residues modulo various primes
  3. Explore continued fraction attacks on RSA with small private exponents
  4. Investigate the structure of multiplicative groups modulo composite numbers
  5. Study the efficiency of various factoring algorithms on different types of numbers

Advanced Topics for Further Study

  1. Algebraic number theory: Class field theory and its cryptographic applications
  2. Analytic number theory: L-functions and their role in cryptanalysis
  3. Computational number theory: Advanced algorithms for number-theoretic problems
  4. Arithmetic geometry: Elliptic curves, abelian varieties, and higher-dimensional analogues
  5. Post-quantum mathematics: Lattices, codes, and multivariate systems

The journey through cryptographic number theory is one of continuous discovery, where ancient mathematical insights illuminate modern security challenges, and where the pursuit of mathematical beauty leads to practical solutions for protecting our digital world.

Released under the MIT License.