Path: blob/main/foundations/04-number-theory-rsa/sage/04e-rsa-key-generation.ipynb
483 views
Primality Testing
Module 04 | 04-number-theory-rsa | Notebook 04e
Trial division, Fermat test, Carmichael numbers, Miller-Rabin, is_prime(), random_prime()
Objectives
By the end of this notebook you will be able to:
Explain why finding large primes is essential for RSA key generation.
Implement trial division and understand its computational limits.
Describe the Fermat primality test and identify its weakness (Carmichael numbers).
Implement and analyze the Miller-Rabin probabilistic primality test.
Use SageMath's
is_prime(),is_pseudoprime(), andrandom_prime()for cryptographic prime generation.
Prerequisites
Completion of The Chinese Remainder Theorem.
Fermat's Little Theorem from notebook 04c: if is prime and , then .
Modular exponentiation via
power_mod(a, e, n).
Motivation: Why Primality Testing Matters
The Question: Is prime?
Trial division would require testing divisors up to . At a billion divisions per second, that is roughly 7 hours. For a 1024-bit number (the size RSA actually uses), trial division would take longer than the age of the universe. Is there a faster way?
Bridge from 04c: In notebook 04c we proved Fermat's Little Theorem: for prime . We used it as a structural fact about primes. Now we flip the logic and use it as a test: if , then cannot be prime. This simple contrapositive is the foundation of every fast primality test.
Crypto foreshadowing: RSA key generation needs two large primes and , each typically 1024 bits. The standard algorithm is: generate a random odd number, test primality, repeat. By the Prime Number Theorem, roughly 1 in every 710 odd 1024-bit numbers is prime, so efficient testing is critical.
1. Trial Division
The simplest primality test: to check whether is prime, test whether any integer with divides .
Why suffices: If with , then . So if no divisor up to exists, is prime.
Complexity: divisions. For an -bit number, that is , exponential in the bit-length.
2. The Fermat Primality Test
Idea (from 04c): Fermat's Little Theorem says: if is prime and , then
Contrapositive: If for some with , then is definitely composite.
Algorithm:
Pick a random base with .
Compute .
If the result is not 1, output "composite." Otherwise, output "probably prime."
Repeat times with different random bases for higher confidence.
The Fermat test is blazingly fast: power_mod uses repeated squaring, so it runs in time, polynomial in the bit-length! Compare that to trial division's exponential cost.
But there is a trap...
3. Carmichael Numbers: When Fermat Fails
Misconception alert: "If for some base , then is probably prime."
This is dangerously wrong. Carmichael numbers are composites where for every base with . The Fermat test will say "probably prime" no matter how many rounds you run!
Definition: A composite number is a Carmichael number if for all with .
Korselt's criterion: is a Carmichael number if and only if is square-free and, for every prime dividing , we have .
The smallest Carmichael number is .
Note: Carmichael numbers are rare but they are infinite (Alford-Granville-Pomerance, 1994). The first few are: 561, 1105, 1729, 2465, 2821, 6601, 8911, ...
For cryptography, "rare but infinite" is not good enough. We need a test with a provable error bound.
4. The Miller-Rabin Primality Test
Miller-Rabin strengthens the Fermat test by exploiting an additional property of primes.
Key insight: If is an odd prime and , then . That is, the only square roots of 1 modulo a prime are and . (This follows because is a field.)
Setup: Write where is odd. Then:
We compute the sequence:
Each term is the square of the previous one. If is prime, then:
Either , or
for some .
If neither condition holds, is definitely composite.
Error bound: If is composite, at least 3/4 of all bases will witness this. So each round has error probability , and rounds give error probability .
Miller-Rabin catches Carmichael numbers
Checkpoint: Before running the cell below, predict: will Miller-Rabin correctly identify 561 as composite? (Recall that Fermat was fooled every time.)
5. Probabilistic vs Deterministic Testing
| Test | Type | Complexity | Weakness |
|---|---|---|---|
| Trial division | Deterministic | Way too slow for large | |
| Fermat test | Probabilistic | Carmichael numbers | |
| Miller-Rabin | Probabilistic | Error (no known weakness) | |
| AKS (2002) | Deterministic | Polynomial, but too slow in practice |
In practice: Miller-Rabin with rounds gives error probability . This is far smaller than the probability of a hardware error corrupting the computation. Cryptographic libraries universally use Miller-Rabin.
SageMath's approach:
is_pseudoprime(n): Uses Miller-Rabin. Fast, probabilistic.is_prime(n): Uses a combination of methods to give a proven result (for numbers within practical range). Slower but deterministic.
6. Generating RSA Primes
Crypto foreshadowing: Here is how RSA key generation actually works:
Pick a random odd number of the desired bit-length (e.g., 1024 bits).
Test if is prime (using Miller-Rabin).
If not, try , , ... (or just pick a new random number).
Repeat until a prime is found.
By the Prime Number Theorem, the density of primes near is about . For 1024-bit numbers, , so on average we test about 355 random odd numbers before finding a prime.
Exercises
Exercise 1: Trace Miller-Rabin by Hand (Fully Worked)
Problem: Apply the Miller-Rabin test to with base .
Solution:
Step 1: Write , so and .
Step 2: Compute .
Exercise 2: Fermat vs Miller-Rabin on Carmichael Numbers (Guided)
Problem: The number is a Carmichael number (also famous as the Hardy-Ramanujan "taxicab" number). Your tasks:
Verify Korselt's criterion: check that for each prime factor .
Show that the Fermat test is fooled by testing 20 random bases.
Show that Miller-Rabin catches it: find a witness base.
Exercise 3: Error Probability Experiment (Independent)
Problem: Miller-Rabin theory says: for a composite , at most of bases in are non-witnesses (i.e., they fail to detect that is composite).
Pick the composite number (a base-2 Fermat pseudoprime).
For every base from 2 to 339, run
miller_rabin_single(341, a)and count how many returnTrue(non-witnesses).Compute the fraction of non-witnesses. Is it ?
Repeat for . Is the bound satisfied?
No starter code provided. Write your solution from scratch.
Summary
| Concept | Key idea |
|---|---|
| Trial division | Simple and correct, but exponential complexity in bit-length makes it useless for cryptographic sizes |
| Fermat test | Uses the contrapositive of Fermat's Little Theorem: if , then is composite. Fast, but fatally flawed by Carmichael numbers |
| Carmichael numbers | Composites (like 561, 1105, 1729) that fool the Fermat test for every coprime base |
| Miller-Rabin test | Strengthens Fermat by checking for non-trivial square roots of 1. Each round catches a composite with probability , so rounds give error |
| SageMath tools | is_prime() (deterministic), is_pseudoprime() (Miller-Rabin), and random_prime() for generating primes |
| RSA prime generation | Pick random odd numbers and test with Miller-Rabin until a prime is found. The Prime Number Theorem guarantees this happens quickly |
Next: RSA Encryption and Decryption, we put primes to work, building the full RSA cryptosystem.