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.
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
Modular Arithmetic
For integers , , and positive integer , we write if divides . The set forms a ring under addition and multiplication modulo .
Euler Totient Function
For a positive integer , Euler's totient function counts the number of integers in that are coprime to . Key properties:
- If is prime:
- If are distinct primes:
- The group of units has order
Trapdoor One-Way Function
A function is a one-way function if it is easy to compute from but computationally infeasible to find from . 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
Euler's Theorem
Statement
If , then:
When is prime, this reduces to Fermat's little theorem: for .
Intuition
The integers coprime to form a multiplicative group of order . By Lagrange's theorem from group theory, the order of every element divides the group order. So in this group.
Proof Sketch
Let be the elements of . Since , multiplication by permutes these elements: is the same set modulo . Therefore , giving . Since each is coprime to , cancel to get .
Why It Matters
Euler's theorem is the engine behind RSA. The correctness of RSA decryption follows directly from the fact that when .
Failure Mode
The theorem requires . If shares a factor with , the conclusion fails. In RSA, where , the message must satisfy . If is a multiple of or , RSA still works by the Chinese Remainder Theorem, but the proof path changes.
RSA Cryptosystem
Key Generation
- Choose two large distinct primes and (typically 1024+ bits each)
- Compute and
- Choose with and (common choice: )
- Compute using the extended Euclidean algorithm
- Public key: . Private key: . Discard , , and .
Encryption and Decryption
- Encrypt: Given plaintext , compute ciphertext
- Decrypt: Given ciphertext , compute
RSA Correctness
Statement
For any message :
That is, decryption recovers the original message: .
Intuition
Since , write for some integer . Then . By Euler's theorem, when , so .
Proof Sketch
Write .
Case 1: . By Euler's theorem, , so .
Case 2: but (i.e., ). Work modulo and separately. Modulo : , so . Modulo : , so by Fermat's little theorem, hence . By the Chinese Remainder Theorem, .
Case 3: . Then and .
Why It Matters
This theorem guarantees that RSA decryption always works. The proof covers all cases, including the edge case where shares a factor with . Without this completeness, RSA would be unreliable.
Failure Mode
RSA correctness holds regardless of the choice of . However, RSA security requires additional conditions: and must be large, random, and of similar size. If and 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.
Small RSA computation
Let , . Then and .
Choose (coprime to 3120). Compute . Using the extended Euclidean algorithm: (since ).
Encrypt : .
Decrypt: . The original message is recovered.
Diffie-Hellman Key Exchange
Diffie-Hellman Protocol
Let be a large prime and a generator of .
- Alice chooses random , sends to Bob
- Bob chooses random , sends to Alice
- Shared secret: Alice computes . Bob computes . Both obtain the same value .
An eavesdropper sees , , , and , but must compute from these values. This is the Computational Diffie-Hellman (CDH) problem.
Discrete Logarithm Problem (DLP)
Given a prime , a generator of , and a value , the discrete logarithm problem asks: find . The best known classical algorithms (general number field sieve for DLP, Pollard's rho) run in subexponential time 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 for random primes of equal bit-length, no polynomial-time algorithm can find and with non-negligible probability.
Decisional Diffie-Hellman (DDH) Assumption: In a group of prime order with generator , the tuples and are computationally indistinguishable for random .
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 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 -bit integers in 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
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.
RSA security is not equivalent to factoring
Breaking RSA (recovering from ) is not known to be equivalent to factoring . It is possible in principle that one could decrypt without factoring. What is known: factoring breaks RSA (since knowing and lets you compute ). The converse is open. In practice, all known attacks on RSA either factor or exploit implementation flaws (timing, padding oracles).
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
Problem
In RSA with and , compute the public and private keys using . Then encrypt the message and verify that decryption recovers it.
Problem
In Diffie-Hellman with and , Alice chooses and Bob chooses . Compute the public values each sends and the shared secret.
Problem
Prove that if you can compute from (where are unknown primes), you can factor . That is, show that knowing is equivalent to knowing the factorization.
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.
- Sets, Functions, and RelationsLayer 0A
- Basic Logic and Proof TechniquesLayer 0A
- Common Probability DistributionsLayer 0A
Builds on This
- Zero-Knowledge ProofsLayer 3