Imports
import CLRSLean.Chapter_31.Section_31_1_Elementary_Number_Theory
import CLRSLean.Chapter_31.Section_31_2_Greatest_Common_Divisor
import CLRSLean.Chapter_31.Section_31_3_Modular_Arithmetic
import CLRSLean.Chapter_31.Section_31_4_Solving_Modular_Linear_Equations
import CLRSLean.Chapter_31.Section_31_5_Chinese_Remainder_Theorem
import CLRSLean.Chapter_31.Section_31_6_Powers_Of_An_Element
import CLRSLean.Chapter_31.Section_31_7_RSA
import CLRSLean.Chapter_31.Section_31_8_Primality_Testing
import CLRSLean.Chapter_31.Section_31_9_Integer_FactorizationChapter 31 — Number-Theoretic Algorithms
Chapter 31 of CLRS covers algorithms for number theory: divisibility, the greatest common divisor, modular arithmetic, primality testing, and cryptography. This chapter formalizes all nine sections with the division theorem, Bezout's identity, the EUCLID / EXTENDED-EUCLID algorithms, the Chinese remainder theorem, Fermat's and Euler's theorems, RSA, the Fermat primality test, and the Pollard's-rho factorization heuristic.
Sections
31.1 Elementary Number-Theoretic Notions
-
CLRS.Chapter31.division_theorem(Theorem 31.1) — division with a unique quotient and remainder. -
CLRS.Chapter31.IsGCD/CLRS.Chapter31.nat_gcd_isGCD— the greatest-common-divisor property. -
CLRS.Chapter31.divides_refl…CLRS.Chapter31.divides_sub(Lemma 31.1), andCLRS.Chapter31.exists_prime_ge(Euclid's theorem).
31.2 Greatest Common Divisor
-
CLRS.Chapter31.euclid_recursion(Theorem 31.9, the GCD recursion theorem),CLRS.Chapter31.euclid+CLRS.Chapter31.euclid_eq_gcd,CLRS.Chapter31.gcd_is_linear_combination(Lemma 31.3, Bezout),CLRS.Chapter31.gcd_is_smallest_positive_linear_combination(Theorem 31.2), the Corollary 31.3/31.4 facts, andCLRS.Chapter31.extendedEuclid+extendedEuclid_spec. -
Running time (Lamé / Fibonacci):
CLRS.Chapter31.euclidDivisionscounts the recursive calls ofEUCLID;CLRS.Chapter31.fib_le_of_euclidDivisions(Lemma 31.10) givesa ≥ F_{k+2},b ≥ F_{k+1}forkcalls;CLRS.Chapter31.euclidDivisions_lt(Theorem 31.11, Lamé) bounds the call count byb < F_{k+1}; andCLRS.Chapter31.euclidDivisions_le_two_log(Corollary 31.12) is theO(log b)bound.
31.3 Modular Arithmetic
-
CLRS.Chapter31.mod_add/mod_mul(Theorem 31.5),CLRS.Chapter31.exists_mul_inverse_mod(Theorem 31.6),CLRS.Chapter31.mul_left_cancel_mod(Theorem 31.9), andCLRS.Chapter31.modular_linear_solvable(Corollary 31.21).
31.4 Solving Modular Linear Equations
-
CLRS.Chapter31.linear_congruence_shiftandCLRS.Chapter31.linear_congruence_all_solutions: the solutions ofa·x ≡ b (mod n)arex₀ + k·(n/gcd(a,n)).
31.5 The Chinese Remainder Theorem
-
CLRS.Chapter31.chinese_remainder_two,CLRS.Chapter31.chinese_remainder_unique, andCLRS.Chapter31.chinese_remainder(Theorem 31.27, two moduli).
31.6 Powers of an Element
-
CLRS.Chapter31.modularExponentiation+modularExponentiation_spec,CLRS.Chapter31.fermat_little_theorem(Theorem 31.31), andCLRS.Chapter31.euler_theorem.
31.7 The RSA Public-Key Cryptosystem
-
CLRS.Chapter31.totient_mul_primeandCLRS.Chapter31.rsa_correct(Theorem 31.36).
31.8 Primality Testing
-
CLRS.Chapter31.fermat_test(Theorem 31.31),CLRS.Chapter31.fermatPseudoprime, andCLRS.Chapter31.pseudoprime+pseudoprime_correct. -
Carmichael numbers:
CLRS.Chapter31.isCarmichael— a compositenpassing the Fermat test for every coprime base (CLRS.Chapter31.carmichael_fermatPseudoprime);CLRS.Chapter31.isCarmichael_561exhibits the smallest one. -
Miller-Rabin:
CLRS.Chapter31.strongTestParams(the2^s·ddecomposition),CLRS.Chapter31.strongPseudoprime(STRONG-PSEUDOPRIME),CLRS.Chapter31.Witness, and the executableCLRS.Chapter31.millerRabintest. Correctness:CLRS.Chapter31.strongPseudoprime_of_prime(a prime passes every base),CLRS.Chapter31.not_witness_of_prime, andCLRS.Chapter31.witness_not_prime(a witness certifies compositeness). -
Miller-Rabin error bound (Rabin–Monier):
CLRS.Chapter31.goodUnits(S(n), the good subgroup),CLRS.Chapter31.liar_mem_goodSet(every strong liar lies inS(n)), and the counting machinery (CLRS.Chapter31.card_pow_eq_one_crt,CLRS.Chapter31.mTorsion_le_prod_half). The boundCLRS.Chapter31.goodUnits_card_leproves|S(n)| ≤ (n−1)/4(three cases: prime power, semiprime, and ≥3 prime factors), givingCLRS.Chapter31.strongLiars_card_le— at most(n−1)/4of the bases are strong liars (Theorem 31.39, sharpened to(n−1)/4by Rabin–Monier).
31.9 Integer Factorization
⚠️ CLRS 4th edition removed this section (integer factorization is no
longer in the main text of Chapter 31). The POLLARD-RHO formalization is
retained for reference; a repo-wide migration will decide its fate.
-
CLRS.Chapter31.rhoStepandCLRS.Chapter31.rho_collision_factor(Pollard's rho). -
POLLARD-RHO:
CLRS.Chapter31.RhoState(tortoise-and-hare state),CLRS.Chapter31.pollardStep, the loopCLRS.Chapter31.pollardRhoLoop, and the fullCLRS.Chapter31.pollardRhoalgorithm, with soundnessCLRS.Chapter31.pollardRho_sound(a returned factor is a nontrivial divisor ofn) and collision detectionCLRS.Chapter31.pollardStep_collision_factor. -
Probabilistic analysis (CLRS Theorem 31.40): the rho-orbit-read-mod-
pheuristic is made an explicit i.i.d.-uniform model (Fin k → Fin p), with the birthday boundCLRS.Chapter31.birthday_noCollision_le/CLRS.Chapter31.birthday_collision_ge/CLRS.Chapter31.birthday_collision_prob_ge_half, the expected-O(√p)boundCLRS.Chapter31.rho_expected_rounds_le_two/CLRS.Chapter31.rho_expected_draws_le, and the detection lemmasCLRS.Chapter31.pollardStep_detects/CLRS.Chapter31.pollardRhoLoop_terminates_on_collisionbound to the real construction.
Status: selected-section-complete — Sections 31.1–31.9 fully proved,
including the Miller-Rabin error bound and the POLLARD-RHO probabilistic
analysis (birthday bound + expected O(√p)).
Deferred Work
The remaining scope outside the represented fourth-edition sections is the RSA
security (one-way function) claim and the ZMod.chineseRemainder
ring-isomorphism packaging. The executable layers are now complete:
modularLinearEquationSolver (§31.4), modExpWithCount (§31.6),
rsaKeyGen/rsaEncrypt/rsaDecrypt (§31.7), and millerRabinLoop (§31.8).
The legacy §31.9 integer-factorization development is retained as online
material with its probabilistic analysis complete.
namespace CLRSnamespace Chapter31end Chapter31end CLRS