Documentation

CslibFrontier.Crypto.Computational.Factoring.WeakModuli

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.

Main definitions #

Main results #

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 #

Shared primes #

theorem Cslib.Crypto.gcd_mul_mul_of_prime_ne {p q₁ q₂ : ℕ} (hq₁ : Nat.Prime q₁) (hq₂ : Nat.Prime q₂) (hne : q₁ ≠ q₂) :
(p * q₁).gcd (p * q₂) = p

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.

theorem Cslib.Crypto.RSADesc.gcd_modulus_eq_of_p_eq (desc₁ desc₂ : RSADesc) (hp : desc₁.p = desc₂.p) (hq : desc₁.q ≠ desc₂.q) :
desc₁.modulus.gcd desc₂.modulus = desc₁.p

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 #

theorem Cslib.Crypto.abs_add_div_two_sub_sqrt_mul_lt_half {p q : ℝ} (hp : 0 < p) (hq : 0 < q) (h : |p - q| < 2 * (p * q) ^ (1 / 4)) :
|(p + q) / 2 - √(p * q)| < 1 / 2

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.

theorem Cslib.Crypto.abs_sub_lt_two_mul_rpow_iff {p q : ℝ} (h : 0 ≤ p * q) :
|p - q| < 2 * (p * q) ^ (1 / 4) ↔ (p - q) ^ 4 < 16 * (p * q)

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.

theorem Cslib.Crypto.ceilSqrt_mul_eq_add_div_two {p q : ℕ} (hpq : Even (p + q)) (hclose : |↑p - ↑q| < 2 * (↑p * ↑q) ^ (1 / 4)) :
(if p * q = 0 then 0 else (p * q - 1).sqrt + 1) = (p + q) / 2

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).

theorem Cslib.Crypto.fermatFactor_mul_eq {p q : ℕ} (hqp : q ≤ p) (hpq : Even (p + q)) (hclose : |↑p - ↑q| < 2 * (↑p * ↑q) ^ (1 / 4)) :
fermatFactor (p * q) = (p, q)

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.

theorem Cslib.Crypto.RSADesc.fermatFactor_eq (desc : RSADesc) (hp : desc.p ≠ 2) (hq : desc.q ≠ 2) (hclose : |↑desc.p - ↑desc.q| < 2 * (↑desc.p * ↑desc.q) ^ (1 / 4)) :
fermatFactor desc.modulus = (max desc.p desc.q, min desc.p desc.q)

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⌉.