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 #
NatGcd.program: Euclid's algorithm on two registers.
Main results #
NatGcd.exec:Nat.gcd a bwithin5 b + 3instructions, preserving memory.NatGcd.exec_rename: the same on injectively chosen registers.
@[reducible, inline]
First argument, and the output.
@[reducible, inline]
Second argument; zero on exit.
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)) :
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))) :
Run Euclid's algorithm on any injectively chosen registers.