Documentation

CslibFrontier.Foundations.NumberTheory.AKS.IntegerVariant

The AKS tests with integer thresholds #

The AKS algorithm Agrawal, Kayal, Saxena, Section 4 compares the multiplicative order of n modulo r with log₂² n (step 2) and tests the congruence for a ≤ ⌊√φ(r) log₂ n⌋ (step 5). A program decides these comparisons more easily with the integer threshold c = ⌈log₂ n⌉ in place of log₂ n: step 2 asks for ord_r(n) > c², and step 5 tests a ≤ ⌊√φ(r) · c⌋ = ⌊√(φ(r) c²)⌋. This is a standard integer variant. It is still correct, by Theorem 4.1 of the paper (Cslib.AKS.prime_iff_passes): c² ≥ log₂² n, so the order condition is stronger than the paper's, and every prime passes the congruence for every a, so testing more values of a loses nothing.

Cslib.AKS.PassesUpTo n r A states the tests with the congruence checked for 1 ≤ a ≤ A; the paper's Cslib.AKS.Passes n r is its case A = ⌊√φ(r) log₂ n⌋. For n ≥ 2, ord_r(n) > c² and A ≥ ⌊√φ(r) log₂ n⌋, it holds exactly when n is prime (Cslib.AKS.prime_iff_passesUpTo). The order condition is decided by small powers: for r ≥ 2, ord_r(n) > L exactly when n is coprime to r and no n^k with 1 ≤ k ≤ L is 1 modulo r (Cslib.AKS.lt_orderOf_iff).

Main definitions #

Main results #

References #

The integer bound ⌊√(φ(r) c²)⌋ = ⌊√φ(r) · c⌋ of step 5, with c = ⌈log₂ n⌉.

theorem Cslib.AKS.logb_le_clog (n : ℕ) :
Real.logb 2 ↑n ≤ ↑(Nat.clog 2 n)

log₂ n ≤ ⌈log₂ n⌉.

The integer bound tests at least the paper's values: ⌊√φ(r) log₂ n⌋ ≤ ⌊√(φ(r) c²)⌋.

theorem Cslib.AKS.lt_orderOf_iff {n r L : ℕ} (hr : 2 ≤ r) :
L < orderOf ↑n ↔ n.Coprime r ∧ ∀ (k : ℕ), 1 ≤ k → k ≤ L → n ^ k % r ≠ 1

The order condition by small powers: for r ≥ 2, the multiplicative order of n modulo r exceeds L exactly when n is coprime to r and no n^k with 1 ≤ k ≤ L is 1 modulo r.

theorem Cslib.AKS.prime_iff_passesUpTo {n r A : ℕ} (hn : 2 ≤ n) (hr : Nat.clog 2 n ^ 2 < orderOf ↑n) (hA : numTests n r ≤ A) :

The tests with integer thresholds characterize the primes: for n ≥ 2, r such that the order of n modulo r exceeds ⌈log₂ n⌉², and A ≥ ⌊√φ(r) log₂ n⌋, n passes the tests up to A exactly when it is prime. This is Agrawal, Kayal, Saxena, Theorem 4.1 with integer thresholds.

theorem Cslib.AKS.prime_iff_passesUpTo_numTestsNat {n r : ℕ} (hn : 2 ≤ n) (hr : Nat.clog 2 n ^ 2 < orderOf ↑n) :

The tests with the integer bound ⌊√(φ(r) ⌈log₂ n⌉²)⌋ characterize the primes.