Weak RSA moduli: shared primes and close primes #
Two ways in which badly generated RSA moduli n = p * q are factored without solving a hard
problem, after [BS23], Section 16.4.2, pp. 698–699.
- Shared prime. Two moduli
p * q₁andp * q₂with a common primepand distinct primesq₁ ≠ q₂revealpas their gcd (gcd_mul_mul_of_prime_ne,RSADesc.gcd_modulus_eq_of_p_eq). The book observes this for keys generated on devices that have little entropy when they choosep. The attack isNat.gcd, whichTuring.MultiTapeTM.ComputableInPolyTime.binary_gcdcomputes in polynomial time. - Close primes (Section 16.4.2.1). If
|p - q| < 2 * n ^ (1 / 4), the bound in which the chain (16.11) ends, thenA = (p + q) / 2satisfies|A - √n| < 1 / 2(Lemma 16.4,abs_add_div_two_sub_sqrt_mul_lt_half). Since also√n ≤ A, whenp + qis evenAis an integer and thereforeA = ⌈√n⌉(ceilSqrt_mul_eq_add_div_two). Fromp + q = 2 * Aandp * q = nthe factors follow by solving a quadratic equation, which is whatfactorOfTotientfromRSAAttacks.leandoes givenn + 1 - (p + q) = (p - 1) * (q - 1).fermatFactor ncomposes the two steps, andfermatFactor_mul_eqshows that it returns(p, q). The book does not name this algorithm; the namefermatFactoris ours. It is the first step of Fermat's factoring method only: it triesA = ⌈√n⌉and does not search over largerA. It is computable in polynomial time (computableInPolyTime_fermatFactor), so moduli with close factors are factored by a polynomial-time function.
Main definitions #
Cslib.Crypto.fermatFactor: the close-prime attack,factorOfTotient n (n + 1 - 2 * ⌈√n⌉).
Main results #
Cslib.Crypto.gcd_mul_mul_of_prime_ne,Cslib.Crypto.RSADesc.gcd_modulus_eq_of_p_eq: moduli sharing one prime factor are factored by their gcd.Cslib.Crypto.abs_add_div_two_sub_sqrt_mul_lt_half: Lemma 16.4 overℝ.Cslib.Crypto.abs_sub_lt_two_mul_rpow_iff: the closeness bound|p - q| < 2 * (p * q) ^ (1 / 4)is the polynomial inequality(p - q) ^ 4 < 16 * (p * q), so it can be checked bynorm_num.Cslib.Crypto.ceilSqrt_mul_eq_add_div_two: step 1 of the attack,⌈√(p * q)⌉ = (p + q) / 2.Cslib.Crypto.fermatFactor_mul_eq,Cslib.Crypto.RSADesc.fermatFactor_eq:fermatFactorrecovers the factors of a modulus with close factors.Cslib.Crypto.computableInPolyTime_fermatFactor:fermatFactoris computable in polynomial time on binary naturals.
Implementation notes #
⌈√n⌉ is written as if n = 0 then 0 else (n - 1).sqrt + 1, the function on naturals that
Turing.MultiTapeTM.ComputableInPolyTime.binary_ceilSqrt certifies in polynomial time.
computableInPolyTime_fermatFactor reuses the certificate computableInPolyTime_factorOfTotient
from RSAAttacks.lean and certifies only its arguments n and n + 1 - 2 * ⌈√n⌉, from the
polynomial-time certificates for arithmetic, the test n = 0 and Nat.sqrt on binary naturals.
Unfolding factorOfTotient as well and running fun_prop on the whole term can exceed the default
heartbeat limit.
The book assumes that p and q are odd primes. The statements on naturals need only that
p + q is even: positivity of p and q follows from the closeness bound, and primality is not
used. RSADesc.fermatFactor_eq replaces evenness of p + q by p ≠ 2 and q ≠ 2. The closeness
hypothesis and the conclusion of Lemma 16.4 are stated over ℝ exactly as in the book; the book
leaves the proof of Lemma 16.4 as an exercise, and the proof here uses
(p + q) / 2 - √n = ((p - q) / 2) ^ 2 / ((p + q) / 2 + √n) and (p + q) / 2 ≥ √n.
References #
- D. Boneh, V. Shoup, A Graduate Course in Applied Cryptography, Version 0.6, Section 16.4.2, pp. 698–699 (shared primes on p. 698; Lemma 16.4 and the factoring algorithm of Section 16.4.2.1 on p. 699).
Shared primes #
Shared prime factor ([BS23], Section 16.4.2, p. 698): moduli p * q₁ and
p * q₂ with distinct primes q₁ ≠ q₂ have gcd p. The common factor p need not be prime.
The attack is Nat.gcd, which Turing.MultiTapeTM.ComputableInPolyTime.binary_gcd computes in
polynomial time on binary naturals.
Two RSA moduli with the same first prime and different second primes are both factored by
their gcd ([BS23], Section 16.4.2, p. 698), which
Turing.MultiTapeTM.ComputableInPolyTime.binary_gcd computes in polynomial time.
Close primes #
Lemma 16.4 of [BS23] (Section 16.4.2.1, p. 699): for positive reals p, q
with |p - q| < 2 * (p * q) ^ (1 / 4), the arithmetic mean A = (p + q) / 2 satisfies
|A - √(p * q)| < 1 / 2. This is the printed statement "if |p − q| < 2n^{1/4} then
|A − √n| < 1/2" with n = p * q substituted. The book states it for the factors of an RSA
modulus n = p * q; nothing beyond positivity is used.
The closeness bound of Lemma 16.4 and (16.11), |p - q| < 2 * (p * q) ^ (1 / 4), is the
polynomial inequality (p - q) ^ 4 < 16 * (p * q) (for 0 ≤ p * q). On concrete numbers the
right-hand side is decided by norm_num.
Step 1 of the close-prime attack ([BS23], Section 16.4.2.1, p. 699): if
p + q is even and |p - q| < 2 * (p * q) ^ (1 / 4), then ⌈√(p * q)⌉ = (p + q) / 2. The
ceiling square root is written as the natural-number function
if n = 0 then 0 else (n - 1).sqrt + 1 that binary_ceilSqrt certifies. The book assumes odd
primes p, q; neither primality nor oddness beyond the parity of p + q is needed (positivity
follows from the closeness bound), and without that parity the conclusion fails (p = 3,
q = 2).
The two steps of the close-prime attack of [BS23], Section 16.4.2.1, p. 699, on a
modulus n: compute A = ⌈√n⌉, written if n = 0 then 0 else (n - 1).sqrt + 1, and solve
p + q = 2 * A, p * q = n with factorOfTotient, which receives n + 1 - 2 * A in the place
of φ(n) = n + 1 - (p + q).
The book does not name the algorithm; the name fermatFactor is ours. It is a single step of
Fermat's factoring method, with no search over A: Fermat's method would go on to try
A = ⌈√n⌉ + 1, ⌈√n⌉ + 2, …, while fermatFactor stops at A = ⌈√n⌉ and factors n only when
its factors are close (fermatFactor_mul_eq).
The close-prime attack recovers close factors ([BS23], Section 16.4.2.1,
p. 699): for naturals q ≤ p with p + q even and |p - q| < 2 * (p * q) ^ (1 / 4),
fermatFactor (p * q) = (p, q). Primality of p and q is not needed, and their positivity
follows from the closeness bound.
The close-prime attack on an RSA modulus ([BS23], Section 16.4.2.1,
p. 699): if both primes are odd and |p - q| < 2 * (p * q) ^ (1 / 4),
fermatFactor returns the factorization (max p q, min p q).
Efficiency #
The single Fermat step fermatFactor is computable in polynomial time on binary naturals.
Together with fermatFactor_mul_eq, a modulus with close factors is factored by a polynomial-time
function. The proof unfolds only fermatFactor and applies the compositional certificate
computableInPolyTime_factorOfTotient, with fun_prop certifying its two arguments n and
n + 1 - 2 * ⌈√n⌉.