Beta. Content is under active construction and has not been peer-reviewed. Report errors on GitHub.Disclaimer

Applied Math

Public-Key Cryptography

RSA, Diffie-Hellman, discrete logarithm, trapdoor one-way functions, and the number-theoretic assumptions underlying modern encryption. The mathematical bridge between algebra and practical security.

CoreTier 2Stable~45 min
0

Why This Matters

Before 1976, all cryptography required both parties to share a secret key in advance. Public-key cryptography solved this: two parties who have never communicated can establish a shared secret over a public channel. The mathematical mechanism relies on problems that are easy to set up but hard to reverse: multiplying large primes is fast, factoring their product is (believed to be) hard.

RSA and Diffie-Hellman are the two foundational constructions. RSA provides encryption and digital signatures. Diffie-Hellman provides key exchange. Both rely on number-theoretic hardness assumptions that have withstood decades of attack by the best mathematicians and computer scientists. Understanding the precise mathematical structure of these systems is necessary for reasoning about their security guarantees and failure modes.

Number-Theoretic Foundations

Definition

Modular Arithmetic

For integers aa, bb, and positive integer nn, we write ab(modn)a \equiv b \pmod{n} if nn divides aba - b. The set Z/nZ={0,1,,n1}\mathbb{Z}/n\mathbb{Z} = \{0, 1, \ldots, n-1\} forms a ring under addition and multiplication modulo nn.

Definition

Euler Totient Function

For a positive integer nn, Euler's totient function ϕ(n)\phi(n) counts the number of integers in {1,,n}\{1, \ldots, n\} that are coprime to nn. Key properties:

  • If pp is prime: ϕ(p)=p1\phi(p) = p - 1
  • If p,qp, q are distinct primes: ϕ(pq)=(p1)(q1)\phi(pq) = (p-1)(q-1)
  • The group of units (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times has order ϕ(n)\phi(n)
Definition

Trapdoor One-Way Function

A function ff is a one-way function if it is easy to compute f(x)f(x) from xx but computationally infeasible to find xx from f(x)f(x). A trapdoor one-way function additionally has a secret (the trapdoor) that makes inversion efficient. Without the trapdoor, inversion is hard. With it, inversion is easy. Public-key cryptography requires trapdoor one-way functions: the public key defines the easy direction, and the private key is the trapdoor.

Euler's Theorem

Theorem

Euler's Theorem

Statement

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

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

When n=pn = p is prime, this reduces to Fermat's little theorem: ap11(modp)a^{p-1} \equiv 1 \pmod{p} for a≢0(modp)a \not\equiv 0 \pmod{p}.

Intuition

The integers coprime to nn form a multiplicative group (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times of order ϕ(n)\phi(n). By Lagrange's theorem from group theory, the order of every element divides the group order. So aϕ(n)=1a^{\phi(n)} = 1 in this group.

Proof Sketch

Let {r1,,rϕ(n)}\{r_1, \ldots, r_{\phi(n)}\} be the elements of (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times. Since gcd(a,n)=1\gcd(a, n) = 1, multiplication by aa permutes these elements: {ar1,,arϕ(n)}\{ar_1, \ldots, ar_{\phi(n)}\} is the same set modulo nn. Therefore i(ari)iri(modn)\prod_i (ar_i) \equiv \prod_i r_i \pmod{n}, giving aϕ(n)iriiri(modn)a^{\phi(n)} \prod_i r_i \equiv \prod_i r_i \pmod{n}. Since each rir_i is coprime to nn, cancel ri\prod r_i to get aϕ(n)1(modn)a^{\phi(n)} \equiv 1 \pmod{n}.

Why It Matters

Euler's theorem is the engine behind RSA. The correctness of RSA decryption follows directly from the fact that akϕ(n)+1a(modn)a^{k\phi(n) + 1} \equiv a \pmod{n} when gcd(a,n)=1\gcd(a, n) = 1.

Failure Mode

The theorem requires gcd(a,n)=1\gcd(a, n) = 1. If aa shares a factor with nn, the conclusion fails. In RSA, where n=pqn = pq, the message mm must satisfy gcd(m,n)=1\gcd(m, n) = 1. If mm is a multiple of pp or qq, RSA still works by the Chinese Remainder Theorem, but the proof path changes.

RSA Cryptosystem

Key Generation

  1. Choose two large distinct primes pp and qq (typically 1024+ bits each)
  2. Compute n=pqn = pq and ϕ(n)=(p1)(q1)\phi(n) = (p-1)(q-1)
  3. Choose ee with 1<e<ϕ(n)1 < e < \phi(n) and gcd(e,ϕ(n))=1\gcd(e, \phi(n)) = 1 (common choice: e=65537e = 65537)
  4. Compute de1(modϕ(n))d \equiv e^{-1} \pmod{\phi(n)} using the extended Euclidean algorithm
  5. Public key: (n,e)(n, e). Private key: (n,d)(n, d). Discard pp, qq, and ϕ(n)\phi(n).

Encryption and Decryption

  • Encrypt: Given plaintext m{0,,n1}m \in \{0, \ldots, n-1\}, compute ciphertext cme(modn)c \equiv m^e \pmod{n}
  • Decrypt: Given ciphertext cc, compute mcd(modn)m \equiv c^d \pmod{n}
Theorem

RSA Correctness

Statement

For any message m{0,,n1}m \in \{0, \ldots, n-1\}:

medm(modn)m^{ed} \equiv m \pmod{n}

That is, decryption recovers the original message: (me)dm(modn)(m^e)^d \equiv m \pmod{n}.

Intuition

Since ed1(modϕ(n))ed \equiv 1 \pmod{\phi(n)}, write ed=1+kϕ(n)ed = 1 + k\phi(n) for some integer kk. Then med=m(mϕ(n))km^{ed} = m \cdot (m^{\phi(n)})^k. By Euler's theorem, mϕ(n)1(modn)m^{\phi(n)} \equiv 1 \pmod{n} when gcd(m,n)=1\gcd(m, n) = 1, so medm(modn)m^{ed} \equiv m \pmod{n}.

Proof Sketch

Write ed=1+k(p1)(q1)ed = 1 + k(p-1)(q-1).

Case 1: gcd(m,n)=1\gcd(m, n) = 1. By Euler's theorem, mϕ(n)1(modn)m^{\phi(n)} \equiv 1 \pmod{n}, so med=m(mϕ(n))km(modn)m^{ed} = m \cdot (m^{\phi(n)})^k \equiv m \pmod{n}.

Case 2: pmp | m but qmq \nmid m (i.e., gcd(m,n)=p\gcd(m, n) = p). Work modulo pp and qq separately. Modulo pp: m0m \equiv 0, so med0m(modp)m^{ed} \equiv 0 \equiv m \pmod{p}. Modulo qq: gcd(m,q)=1\gcd(m, q) = 1, so mq11(modq)m^{q-1} \equiv 1 \pmod{q} by Fermat's little theorem, hence med=m(m(p1)(q1))km(modq)m^{ed} = m \cdot (m^{(p-1)(q-1)})^k \equiv m \pmod{q}. By the Chinese Remainder Theorem, medm(modn)m^{ed} \equiv m \pmod{n}.

Case 3: nmn | m. Then m0(modn)m \equiv 0 \pmod{n} and med0m(modn)m^{ed} \equiv 0 \equiv m \pmod{n}.

Why It Matters

This theorem guarantees that RSA decryption always works. The proof covers all cases, including the edge case where mm shares a factor with nn. Without this completeness, RSA would be unreliable.

Failure Mode

RSA correctness holds regardless of the choice of mm. However, RSA security requires additional conditions: pp and qq must be large, random, and of similar size. If pp and qq are close together, Fermat factorization finds them quickly. If either is small, trial division succeeds. Textbook RSA (without padding) is also vulnerable to chosen-ciphertext attacks and lacks semantic security. In practice, RSA must be used with proper padding schemes like OAEP.

Example

Small RSA computation

Let p=61p = 61, q=53q = 53. Then n=3233n = 3233 and ϕ(n)=60×52=3120\phi(n) = 60 \times 52 = 3120.

Choose e=17e = 17 (coprime to 3120). Compute d171(mod3120)d \equiv 17^{-1} \pmod{3120}. Using the extended Euclidean algorithm: d=2753d = 2753 (since 17×2753=46801=15×3120+117 \times 2753 = 46801 = 15 \times 3120 + 1).

Encrypt m=65m = 65: c=6517mod3233=2790c = 65^{17} \bmod 3233 = 2790.

Decrypt: m=27902753mod3233=65m = 2790^{2753} \bmod 3233 = 65. The original message is recovered.

Diffie-Hellman Key Exchange

Definition

Diffie-Hellman Protocol

Let pp be a large prime and gg a generator of (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times.

  1. Alice chooses random a{2,,p2}a \in \{2, \ldots, p-2\}, sends A=gamodpA = g^a \bmod p to Bob
  2. Bob chooses random b{2,,p2}b \in \{2, \ldots, p-2\}, sends B=gbmodpB = g^b \bmod p to Alice
  3. Shared secret: Alice computes Ba=gabmodpB^a = g^{ab} \bmod p. Bob computes Ab=gabmodpA^b = g^{ab} \bmod p. Both obtain the same value K=gabmodpK = g^{ab} \bmod p.

An eavesdropper sees gg, pp, gag^a, and gbg^b, but must compute gabg^{ab} from these values. This is the Computational Diffie-Hellman (CDH) problem.

Definition

Discrete Logarithm Problem (DLP)

Given a prime pp, a generator gg of (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times, and a value h=gxmodph = g^x \bmod p, the discrete logarithm problem asks: find xx. The best known classical algorithms (general number field sieve for DLP, Pollard's rho) run in subexponential time Lp[1/3,c]L_p[1/3, c] for appropriate constants. No polynomial-time classical algorithm is known.

Security Assumptions

The security of RSA and Diffie-Hellman rests on computational hardness assumptions, not proven lower bounds.

Integer Factorization Assumption: Given n=pqn = pq for random primes p,qp, q of equal bit-length, no polynomial-time algorithm can find pp and qq with non-negligible probability.

Decisional Diffie-Hellman (DDH) Assumption: In a group GG of prime order qq with generator gg, the tuples (ga,gb,gab)(g^a, g^b, g^{ab}) and (ga,gb,gc)(g^a, g^b, g^c) are computationally indistinguishable for random a,b,ca, b, c.

Both assumptions are believed to be true but unproven. Breaking either would not immediately resolve P vs NP, since factoring and DLP might be in NP \cap co-NP (and thus unlikely to be NP-complete). However, both problems fall to Shor's algorithm on a quantum computer, running in polynomial time.

Post-Quantum Concerns

Shor's algorithm factors nn-bit integers in O(n3)O(n^3) time on a quantum computer and solves the discrete logarithm problem in comparable time. If large-scale quantum computers become practical, RSA and Diffie-Hellman are broken.

Post-quantum alternatives rely on different hardness assumptions:

  • Lattice-based: Learning With Errors (LWE), Ring-LWE
  • Code-based: McEliece cryptosystem
  • Hash-based: SPHINCS+ for signatures

NIST standardized lattice-based schemes (ML-KEM, ML-DSA) in 2024 as replacements.

Common Confusions

Watch Out

Public key does not always mean encryption key

In RSA, the public key can encrypt and the private key decrypts. But RSA also supports digital signatures, where the private key signs (encrypts the hash) and the public key verifies. In Diffie-Hellman, neither party has an "encryption key" at all: the protocol produces a shared secret, which is then used as a symmetric key. "Public key = encryption key" is an oversimplification that breaks down outside basic RSA encryption.

Watch Out

RSA security is not equivalent to factoring

Breaking RSA (recovering mm from c=memodnc = m^e \bmod n) is not known to be equivalent to factoring nn. It is possible in principle that one could decrypt without factoring. What is known: factoring nn breaks RSA (since knowing pp and qq lets you compute dd). The converse is open. In practice, all known attacks on RSA either factor nn or exploit implementation flaws (timing, padding oracles).

Watch Out

Large key size does not mean large security margin

RSA key sizes (2048 or 4096 bits) are much larger than symmetric key sizes (128 or 256 bits) because the best attacks on RSA (number field sieve) are subexponential, not exponential. A 2048-bit RSA key provides roughly 112 bits of security, comparable to a 112-bit symmetric key. The numbers are not directly comparable.

Exercises

ExerciseCore

Problem

In RSA with p=11p = 11 and q=13q = 13, compute the public and private keys using e=7e = 7. Then encrypt the message m=9m = 9 and verify that decryption recovers it.

ExerciseCore

Problem

In Diffie-Hellman with p=23p = 23 and g=5g = 5, Alice chooses a=6a = 6 and Bob chooses b=15b = 15. Compute the public values each sends and the shared secret.

ExerciseAdvanced

Problem

Prove that if you can compute ϕ(n)\phi(n) from n=pqn = pq (where p,qp, q are unknown primes), you can factor nn. That is, show that knowing ϕ(n)\phi(n) is equivalent to knowing the factorization.

ExerciseAdvanced

Problem

Why is textbook RSA (without padding) not semantically secure? Construct a concrete attack.

References

Canonical:

  • Katz & Lindell, Introduction to Modern Cryptography (2020), Chapters 8-13 for public-key encryption, digital signatures, and number-theoretic constructions
  • Shoup, A Computational Introduction to Number Theory and Algebra (2009), Chapters 10-12 for the mathematical foundations

Foundational papers:

  • Diffie & Hellman, "New Directions in Cryptography" (1976)
  • Rivest, Shamir, Adleman, "A Method for Obtaining Digital Signatures and Public-Key Cryptosystems" (1978)

Current:

  • Boneh & Shoup, A Graduate Course in Applied Cryptography (2023), Chapters 10-11, freely available online
  • NIST, "Post-Quantum Cryptography Standards" (2024), for ML-KEM and ML-DSA specifications

Next Topics

  • Zero-knowledge proofs: prove knowledge of a secret without revealing it, building on the hardness assumptions introduced here

Last reviewed: April 2026

Prerequisites

Foundations this topic depends on.

Builds on This

Next Topics