Documentation

CslibFrontier.Algorithms.WordRAM.AKS.GcdTotient

Step 3 of the AKS algorithm and Euler's totient on the word RAM #

Step 3 of the AKS algorithm Agrawal, Kayal, Saxena, Section 4 outputs COMPOSITE if 1 < gcd(a, n) < n for some a ≤ r, and step 5 needs φ(r). One loop over 0 ≤ a ≤ r does both, with two runs of Euclid's algorithm (Cslib.Algorithms.WordRAM.NatGcd.program) per a: it clears a flag when gcd(n, a) is neither 1 nor n, and counts the a with gcd(r, a) = 1. The index a = 0 is harmless (gcd(n, 0) = n), and for r ≥ 2 neither 0 nor r is coprime to r, so the count is φ(r).

Main definitions #

Main results #

References #

@[reducible, inline]

The number n, preserved.

@[reducible, inline]

The modulus r, preserved.

@[reducible, inline]

The loop index a.

@[reducible, inline]

The loop end r + 1.

@[reducible, inline]

1 while every gcd(n, a) seen is 1 or n.

@[reducible, inline]

The number of a seen with gcd(r, a) = 1.

@[reducible, inline]

Constant one.

@[reducible, inline]

First argument of the gcd, and its result.

@[reducible, inline]

Second argument of the gcd.

Euclid's algorithm runs on ga, gb and two scratch registers.

Test gcd(n, a), count gcd(r, a) = 1, and advance a.

theorem Cslib.Algorithms.WordRAM.AKS.GcdTotient.exec {w : ℕ} (x : WordRAM.Data 11) {N R : ℕ} (hn : x.regs n = N) (hr : x.regs r = R) (hR : 2 ≤ R) (hfN : WordRAM.Fits w N) (hfR : WordRAM.Fits w (R + 1)) :
∃ (y : WordRAM.Data 11), WordRAM.ExecWithin w program x y (time R) ∧ y.regs ok ≤ 1 ∧ (y.regs ok = 1 ↔ ∀ (b : ℕ), 1 ≤ b → b ≤ R → b.gcd N = 1 ∨ b.gcd N = N) ∧ y.regs phi = R.totient ∧ y.regs n = N ∧ y.regs r = R

Step 3 and the totient. For r ≥ 2, with the word size holding n and r + 1, within time r instructions the flag ok is 1 exactly when every 1 ≤ a ≤ r has gcd(a, n) = 1 or gcd(a, n) = n, and phi is φ(r). For n ≥ 1 the flag condition says that no 1 ≤ a ≤ r has 1 < gcd(a, n) < n; for n = 0 it is stronger, since gcd(a, 0) = a.