Documentation

CslibFrontier.Algorithms.WordRAM.NatNthRoot

A charged native integer k-th root #

The k-th root Nat.nthRoot k n, the largest a with a ^ k ≤ n, is found by binary search on [0, n + 1). Each probe compares m ^ k with n by a capped power: repeated multiplication that stops as soon as the partial product exceeds n, so every product stays below n (n + 1) and fits a word of about twice the bit length of n. The search halves the interval, so it makes at most ⌈log₂ (n + 1)⌉ probes.

Main definitions #

Main results #

@[reducible, inline]

Base, preserved.

@[reducible, inline]

Exponent, preserved.

@[reducible, inline]

Bound, preserved.

@[reducible, inline]

Capped power, the output.

@[reducible, inline]

Counter.

@[reducible, inline]

Constant one.

Stop once the partial product exceeds the bound; otherwise multiply once more.

theorem Cslib.Algorithms.WordRAM.NatNthRoot.CapPow.exec {w : ℕ} (x : WordRAM.Data 6) (hm : 1 ≤ x.regs m) (h1 : WordRAM.Fits w 1) (hk : WordRAM.Fits w (x.regs k)) (hprod : WordRAM.Fits w (x.regs n * x.regs m)) :
∃ (y : WordRAM.Data 6), WordRAM.ExecWithin w program x y (6 * x.regs k + 5) ∧ (y.regs p ≤ x.regs n ↔ x.regs m ^ x.regs k ≤ x.regs n) ∧ (x.regs m ^ x.regs k ≤ x.regs n → y.regs p = x.regs m ^ x.regs k) ∧ y.regs m = x.regs m ∧ y.regs k = x.regs k ∧ y.regs n = x.regs n ∧ y.regs one = 1 ∧ y.mem = x.mem

For a positive base, the capped power is at most the bound exactly when the true power is, and then the two agree. The products stay below n m.

theorem Cslib.Algorithms.WordRAM.NatNthRoot.CapPow.exec_rename {r w : ℕ} (f : Fin 6 → Fin r) (hf : Function.Injective f) (x : WordRAM.Data r) (hm : 1 ≤ x.regs (f m)) (h1 : WordRAM.Fits w 1) (hk : WordRAM.Fits w (x.regs (f k))) (hprod : WordRAM.Fits w (x.regs (f n) * x.regs (f m))) :
∃ (y : WordRAM.Data r), WordRAM.ExecWithin w (WordRAM.Code.rename f program) x y (6 * x.regs (f k) + 5) ∧ (y.regs (f p) ≤ x.regs (f n) ↔ x.regs (f m) ^ x.regs (f k) ≤ x.regs (f n)) ∧ (x.regs (f m) ^ x.regs (f k) ≤ x.regs (f n) → y.regs (f p) = x.regs (f m) ^ x.regs (f k)) ∧ y.regs (f m) = x.regs (f m) ∧ y.regs (f k) = x.regs (f k) ∧ y.regs (f n) = x.regs (f n) ∧ y.regs (f one) = 1 ∧ y.mem = x.mem

Run the capped power on any injectively chosen registers.

@[reducible, inline]

Bound n, preserved.

@[reducible, inline]

Root degree k, preserved.

@[reducible, inline]

Lower end of the search interval, and the output.

@[reducible, inline]

Upper end of the search interval.

@[reducible, inline]

Probe.

@[reducible, inline]

Capped power of the probe.

@[reducible, inline]

Counter of the capped power.

@[reducible, inline]

Constant one.

@[reducible, inline]

Constant two.

@[reducible, inline]

lo + 1, for the loop test.

The registers of the capped power inside the search.

One probe: halve the interval according to the capped power of its midpoint.

theorem Cslib.Algorithms.WordRAM.NatNthRoot.exec {w : ℕ} (x : WordRAM.Data 10) (hk : 1 ≤ x.regs k) (hkfit : WordRAM.Fits w (x.regs k)) (hfit : WordRAM.Fits w (x.regs n * x.regs n + 2 * x.regs n + 2)) :
∃ (y : WordRAM.Data 10), WordRAM.ExecWithin w program x y (Nat.clog 2 (x.regs n + 1) * (6 * x.regs k + 13) + 7) ∧ y.regs lo = (x.regs k).nthRoot (x.regs n) ∧ y.regs n = x.regs n ∧ y.regs k = x.regs k ∧ y.regs one = 1 ∧ y.regs two = 2 ∧ y.mem = x.mem

Compute Nat.nthRoot k n for k ≥ 1 within ⌈log₂ (n + 1)⌉ (6 k + 13) + 7 instructions, preserving n, k and memory. Products and search bounds are at most n² + 2 n + 1, but the probe counters run up to k whatever n is, hence the separate hypothesis hkfit.

theorem Cslib.Algorithms.WordRAM.NatNthRoot.exec_rename {r w : ℕ} (f : Fin 10 → Fin r) (hf : Function.Injective f) (x : WordRAM.Data r) (hk : 1 ≤ x.regs (f k)) (hkfit : WordRAM.Fits w (x.regs (f k))) (hfit : WordRAM.Fits w (x.regs (f n) * x.regs (f n) + 2 * x.regs (f n) + 2)) :
∃ (y : WordRAM.Data r), WordRAM.ExecWithin w (WordRAM.Code.rename f program) x y (Nat.clog 2 (x.regs (f n) + 1) * (6 * x.regs (f k) + 13) + 7) ∧ y.regs (f lo) = (x.regs (f k)).nthRoot (x.regs (f n)) ∧ y.regs (f n) = x.regs (f n) ∧ y.regs (f k) = x.regs (f k) ∧ y.regs (f one) = 1 ∧ y.regs (f two) = 2 ∧ y.mem = x.mem

Run the binary search on any injectively chosen registers.