Documentation

CslibFrontier.Algorithms.WordRAM.AKS.OrderSearch

The search for r in the AKS algorithm on the word RAM #

Step 2 of the AKS algorithm Agrawal, Kayal, Saxena, Section 4 finds the least r such that the multiplicative order of n modulo r exceeds a bound L. As in the proof of their Theorem 5.1, the program tries r = 2, 3, … in turn. It accepts r when gcd(n, r) = 1 and n^k mod r ≠ 1 for every 1 ≤ k ≤ L; for r ≥ 2 this is the order condition (Cslib.AKS.lt_orderOf_iff). The powers are computed by one multiplication modulo r each, and the gcd by Euclid's algorithm (Cslib.Algorithms.WordRAM.NatGcd.program). Given any r₀ ≥ 2 satisfying the condition, the search stops at some 2 ≤ r ≤ r₀ satisfying it, within a number of instructions polynomial in r₀ and L.

Main definitions #

Main results #

References #

The condition tested for r: n is coprime to r and no n^k with 1 ≤ k ≤ L is 1 modulo r.

@[reducible, inline]

The number n, preserved.

@[reducible, inline]

The bound L on the order, preserved.

@[reducible, inline]

The candidate r, and the output.

@[reducible, inline]

1 once a candidate is accepted.

@[reducible, inline]

Constant one.

@[reducible, inline]

n mod r.

@[reducible, inline]

The power n^k mod r.

@[reducible, inline]

The exponent k.

@[reducible, inline]

1 while no power n^j with 1 ≤ j ≤ k is 1 modulo r.

@[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.

One more power: p := p · (n mod r) mod r, and record whether it is 1.

Accept r if gcd(n, r) = 1 and no small power is 1; otherwise try r + 1.

The instruction bound of program.

theorem Cslib.Algorithms.WordRAM.AKS.OrderSearch.exec {w : ℕ} (x : WordRAM.Data 13) {N L₀ r₀ : ℕ} (hn : x.regs n = N) (hL : x.regs L = L₀) (hr₀ : 2 ≤ r₀) (hgood : Good N L₀ r₀) (hfN : WordRAM.Fits w N) (hfR : WordRAM.Fits w (r₀ * r₀)) (hfL : WordRAM.Fits w (L₀ + 1)) :
∃ (y : WordRAM.Data 13), WordRAM.ExecWithin w program x y (time L₀ r₀) ∧ 2 ≤ y.regs r ∧ y.regs r ≤ r₀ ∧ Good N L₀ (y.regs r) ∧ y.regs n = N ∧ y.regs L = L₀

The search for r. If some r₀ ≥ 2 satisfies Good n L r₀, and the word size holds n, r₀² and L + 1, the search stops within time L r₀ instructions at some 2 ≤ r ≤ r₀ satisfying Good n L r, preserving n and L.