Documentation

CslibFrontier.Algorithms.WordRAM.NatGcd

A charged native greatest common divisor #

Euclid's algorithm with one native remainder per round. The second argument strictly decreases, so the number of rounds is at most that argument; this crude bound suffices for callers whose second argument is itself polynomially bounded.

Main definitions #

Main results #

@[reducible, inline]

First argument, and the output.

@[reducible, inline]

Second argument; zero on exit.

@[reducible, inline]

Remainder scratch.

@[reducible, inline]

Constant zero.

One round of Euclid's algorithm: (a, b) ↦ (b, a mod b).

Euclid's algorithm, looping while the second argument is positive.

theorem Cslib.Algorithms.WordRAM.NatGcd.exec {w : ℕ} (x : WordRAM.Data 4) (ha : WordRAM.Fits w (x.regs a)) (hb : WordRAM.Fits w (x.regs b)) :
∃ (y : WordRAM.Data 4), WordRAM.ExecWithin w program x y (5 * x.regs b + 3) ∧ y.regs a = (x.regs a).gcd (x.regs b) ∧ y.mem = x.mem

Compute Nat.gcd a b within 5 b + 3 instructions, preserving memory. Every register value stays below max a b, so no fit hypothesis beyond the arguments is needed.

theorem Cslib.Algorithms.WordRAM.NatGcd.exec_rename {r w : ℕ} (f : Fin 4 → Fin r) (hf : Function.Injective f) (x : WordRAM.Data r) (ha : WordRAM.Fits w (x.regs (f a))) (hb : WordRAM.Fits w (x.regs (f b))) :
∃ (y : WordRAM.Data r), WordRAM.ExecWithin w (WordRAM.Code.rename f program) x y (5 * x.regs (f b) + 3) ∧ y.regs (f a) = (x.regs (f a)).gcd (x.regs (f b)) ∧ y.mem = x.mem

Run Euclid's algorithm on any injectively chosen registers.