Documentation

CslibFrontier.Crypto.Computational.Factoring.LinearRelation

Linear-relation Fermat factoring #

Formalizes Fermat factoring under a known integer linear relation |a * p - b * q| < n ^ (1 / 4) (Boneh–Shoup Section 16.4.2.1, p. 699, and Exercise 16.14, p. 710). The book writes |a * p + b * q| < n ^ (1 / 4) with real parameters a and b; here a and b are natural numbers with the book's b negated, and non-integer real parameters (a, b) are out of scope.

Multiplying n = p * q by 4 * a * b pairs 2 * a * p and 2 * b * q, whose sum is always even, so fermatFactor (4 * a * b * n) recovers (2 * a * p, 2 * b * q) up to order without any parity side condition on a * p + b * q (by contrast, a = 3, b = 2 on odd p gives odd 3 * p + 2 * q, so ⌈√(6 * n)⌉ ≠ (3 * p + 2 * q) / 2). Taking gcds with n then recovers (p, q) or (q, p) provided Nat.Coprime (2 * a) q and Nat.Coprime (2 * b) p (equivalently ¬ q ∣ 2 * a and ¬ p ∣ 2 * b for primes p, q). Because fermatFactor computes a single closed-form integer square root with no gap search loop, complexity is certified directly as ComputableInPolyTime rather than as a step bound in terms of |a * p - b * q|.

Main definitions #

Main results #

References #

Factors n = p * q when a * p and b * q are close, by applying fermatFactor to 4 * a * b * n = (2 * a * p) * (2 * b * q) and taking gcds with n.

theorem Cslib.Crypto.fermatFactor_four_mul_eq {a b p q : ℕ} (hclose : |↑a * ↑p - ↑b * ↑q| < (4 * ↑a * ↑b * ↑p * ↑q) ^ (1 / 4)) :
fermatFactor (4 * a * b * (p * q)) = if b * q ≤ a * p then (2 * a * p, 2 * b * q) else (2 * b * q, 2 * a * p)

Under |(a : ℝ) * p - b * q| < (4 * a * b * p * q) ^ (1 / 4), fermatFactor on 4 * a * b * (p * q) returns (2 * a * p, 2 * b * q) when b * q ≤ a * p and the swapped pair otherwise.

theorem Cslib.Crypto.fermatFactorLinear_mul_eq {a b p q : ℕ} (hcop_a : (2 * a).Coprime q) (hcop_b : (2 * b).Coprime p) (hclose : |↑a * ↑p - ↑b * ↑q| < (4 * ↑a * ↑b * ↑p * ↑q) ^ (1 / 4)) :
fermatFactorLinear a b (p * q) = if b * q ≤ a * p then (p, q) else (q, p)

fermatFactorLinear a b (p * q) recovers (p, q) when b * q ≤ a * p and (q, p) otherwise, under Nat.Coprime (2 * a) q, Nat.Coprime (2 * b) p, and |(a : ℝ) * p - b * q| < (4 * a * b * p * q) ^ (1 / 4).

theorem Cslib.Crypto.fermatFactorLinear_mul_eq_of_lt_rpow {a b p q : ℕ} (ha : 0 < a) (hb : 0 < b) (hcop_a : (2 * a).Coprime q) (hcop_b : (2 * b).Coprime p) (hclose : |↑a * ↑p - ↑b * ↑q| < (↑p * ↑q) ^ (1 / 4)) :
fermatFactorLinear a b (p * q) = if b * q ≤ a * p then (p, q) else (q, p)

Corollary of fermatFactorLinear_mul_eq under the textbook bound |(a : ℝ) * p - b * q| < ((p : ℝ) * q) ^ (1 / 4 : ℝ) for positive integers a and b.

theorem Cslib.Crypto.RSADesc.fermatFactorLinear_eq_of_lt_rpow (desc : RSADesc) {a b : ℕ} (ha : ¬desc.q ∣ 2 * a) (hb : ¬desc.p ∣ 2 * b) (hclose : |↑a * ↑desc.p - ↑b * ↑desc.q| < ↑desc.modulus ^ (1 / 4)) :
fermatFactorLinear a b desc.modulus = if b * desc.q ≤ a * desc.p then (desc.p, desc.q) else (desc.q, desc.p)

Textbook-bound RSADesc wrapper: if ¬ desc.q ∣ 2 * a, ¬ desc.p ∣ 2 * b, and |a * desc.p - b * desc.q| < desc.modulus ^ (1 / 4), then fermatFactorLinear a b desc.modulus recovers (desc.p, desc.q) up to order.