Complete native selected-coefficient scan #
The invariant counts exactly the original row-major prefix and preserves all memory. The numeric accumulator bound is derived by summing the nonnegative coefficient bound, rather than assuming that the final answer fits. This is an internal composition contract; the counter derives its residue and product readback from its actual preceding executions.
theorem Cslib.Algorithms.WordRAM.ExactTriangle.CountPrime.exec_count {w n p k : ℕ} (x : WordRAM.Data 128) (keys values : ℕ → ℕ) (hn : x.regs Reg.side = n) (hp : x.regs Reg.modulus = p) (hc : x.regs Reg.cells = n * n) (hpadding : n ≤ 2 ^ k) (hkeys : ∀ i < n * n, keys i < p) (hbound : ∀ i < n * n, values i ≤ n) (hresidues : ∀ i < n * n, x.mem (x.regs Reg.residueAB + i) = keys i) (hproduct :
∀ i < n * n,
x.mem (x.regs Reg.matrixR + Nat.Morton.index (i / n) (i % n) * p + if keys i = 0 then 0 else p - keys i) = WordRAM.Signed.encode ↑(values i)) (h0 : x.regs Reg.zero = 0) (h1 : x.regs Reg.one = 1) (h2 : x.regs Reg.two = 2) (htable : ∀ j < 2 ^ k, x.mem (x.regs Reg.spread + j) = Nat.Morton.spread j) (hw :
WordRAM.Fits w
(x.regs Reg.spread + x.regs Reg.residueAB + x.regs Reg.matrixR + 4 ^ k * p + 4 ^ k + 2 ^ k + n * n * n + n * n + p + 16)) :
Execute the complete original-pair scan, including initialization, every coefficient load/decoding/addition, all loop controls and exit. No memory word is changed.