Path: blob/main/foundations/04-number-theory-rsa/break/fermat-factorization-close-primes.ipynb
483 views
Break: Fermat Factorization When Primes Are Close
Module 04 | Breaking Weak Parameters
Factor when is small using Fermat's method.
Why This Matters
RSA's security rests on the difficulty of factoring . But how and are chosen matters enormously.
If and are close together --- say, chosen from the same narrow range, or generated by a buggy random number generator --- then can be factored almost instantly using a method Pierre de Fermat described in 1643.
This is not a theoretical curiosity. Real-world RSA keys have been broken this way:
ROCA vulnerability (2017): Infineon smart cards generated primes from a restricted set, making them factorable.
Poorly seeded PRNGs in embedded devices can produce near-identical primes.
The Scenario: Close Primes
We generate where and are deliberately chosen to be close together. The attacker sees only and must factor it.
Step 1: Fermat's Idea
Fermat observed that any odd composite can be written as a difference of two squares:
where and (assuming ).
The algorithm:
Start with
Compute
If is a perfect square, then and we're done
Otherwise, increment and repeat
When , we have , so the algorithm terminates after very few iterations.
Step 2: Implement Fermat's Factorization
Step 3: How Many Iterations?
The number of iterations Fermat's method needs is approximately:
When is small, this is tiny. When (as it should be for secure RSA), the number of iterations is , which is as hard as trial division.
The Fix: Ensure Is Large
The FIPS 186-4 standard for RSA key generation requires:
where is the bit length of . For a 2048-bit RSA key, this means:
This makes Fermat's method require approximately iterations --- completely infeasible.
In practice, generating and independently and uniformly from the right-sized range almost always satisfies this bound. The primes are only dangerously close if the random number generator is broken.
Exercises
Vary the gap: Generate keys with gaps of for . Plot the number of iterations. Does it match the predicted quadratic relationship?
Larger keys: Try Fermat factorization on 256-bit and 512-bit RSA keys with close primes. How does the constant factor scale with key size?
Comparison with trial division: For a given with close primes, compare the speed of Fermat's method vs. trial division (trying all primes up to ). When is each method faster?
Summary
| Component | Role in the Attack |
|---|---|
| Fermat's difference-of-squares representation | |
| Starting point, close to when primes are close | |
| Perfect square test | Check if is a perfect square at each step |
| Close primes | Make the starting very close to the true |
Key takeaways:
Fermat's factorization method (1643) factors in steps.
When is small, factoring is trivial --- a few iterations suffice.
FIPS 186-4 mandates to prevent this.
Independent, uniform prime generation naturally produces primes that are far apart.
The lesson: RSA security depends not just on key size, but on how the primes are generated.
Back to Module 04: Number Theory and RSA