Issue #3009: A counter stepped by a constant k (t += 8) gets no t % k invariant, so k[t + 7] under t < 64 stays checked

Open and answer this issue on GitHub.

ready, solver · opened 2026-11-30 · updated 2026-11-30 · 0 comments

Probe

Run on develop at 59f3af8d (target/fast/nikaia, built from that tree), outside any project:

// A counter that moves by 8 from 0 while below 64: every k[t + r] with r < 8 is in.
fn sum(k: ref Vec[u32]) -> u32 {
    if k.len() != 64 {
        return 0
    }
    let mut s: u32 = 0
    let mut t = 0
    while t < 64 {
        s = s.wrapping_add(k[t]).wrapping_add(k[t + 7])
        t += 8
    }
    return s
}

fn main() throws {
    let k: Vec[u32] = [1; 64]
    println(f"{sum(k)}")
}

nikaia check --bounds p3_step8.nika

Expected: both indexes proved. t starts at 0 and only grows by 8, so t % 8 == 0 holds at every turn; with t < 64 that gives t <= 56 and t + 7 <= 63 < k.len().

Actual:

bounds in p3_step8.nika: 2 indexes, 1 proved; ...
     9  k[t + 7]  checked: a counter the loop on line 8 changes, with no bound tied to its turns or to another counter (ADR-361 D13, #1634)

The message cites #1634, which is closed: it built a counter’s bound from its loop’s bound, not a counter’s residue modulo its step.

The governing decision

ADR-361 (the loop’s invariant is searched for, nobody writes it) and its D13 (a counter changed only by constants of one sign is bounded by the turns). No decision yet on an invariant of the form t % k == c for a counter changed only by the constant k; it is the invariant a block-wise loop needs (t += 8 over 64 rounds, i += 4 over groups of four bytes).

Found while #2957 (SHA-256 in Nikaia): eight rounds a turn over while t < 64 { … t += 8 } left k[t + 1] to k[t + 7] checked; the code had to be rewritten as for q in 0..<8 { let t = q * 8 … } to be proved.

To build

  1. A strategy of the loop-invariant search (ADR-361): a counter that starts at a constant c and is changed on every path only by adding the constant k > 1 gets the candidate t % k == c % k, checked as any other candidate.
  2. The bounds walk reads it beside t < N to bound t + r for r < k where N % k == c % k.
  3. A test in tests/compiler/tests/ with the probe above, both proved, and one with t += 4 over a list of 30 where t + 3 stays checked (30 is no multiple of 4).

Done when

The probe prints 2 indexes, 2 proved under --bounds, and the second test keeps its check.


Generated by Claude Code

Back to Open Issues