test(core): the control harnesses prove None only on overflow; i32/i64 checked_sub is native

The tightened harnesses assert Some whenever every i128 prefix fits in i64.
That found the default Amount::checked_sub refusing x - MIN on i32/i64 where
the difference fits; both now use the native subtraction. The bridge pins
aging_total(0,0,0,0,MAX) to Some(MAX), bounds the rounded figures to one
unit of the exact value, and covers scales 0..=28. Two smaller worksheet
harnesses (nine terms on i32, seven on i32) found no verdict in 400 s and
300 s: recorded, no harness.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-09 12:53:09 -07:00
1 parent 632a1a543c
commit c8cfd32723
4 files changed
+95 -28

No files matched your search

+12
View File
@@ -180,6 +180,12 @@ impl Amount for i32 {
fn checked_add(self, rhs: Self) -> Option<Self> {
i32::checked_add(self, rhs)
}
// The native subtraction: the default negates `rhs` first and so
// refused `x - MIN` even where the difference fits (found by the
// K-U8 harnesses: `None` must mean overflow, and only overflow).
fn checked_sub(self, rhs: Self) -> Option<Self> {
i32::checked_sub(self, rhs)
}
fn checked_neg(self) -> Option<Self> {
i32::checked_neg(self)
}
@@ -234,6 +240,12 @@ impl Amount for i64 {
fn checked_add(self, rhs: Self) -> Option<Self> {
i64::checked_add(self, rhs)
}
// The native subtraction: the default negates `rhs` first and so
// refused `x - MIN` even where the difference fits (found by the
// K-U8 harnesses: `None` must mean overflow, and only overflow).
fn checked_sub(self, rhs: Self) -> Option<Self> {
i64::checked_sub(self, rhs)
}
fn checked_neg(self) -> Option<Self> {
i64::checked_neg(self)
}
+28 -1
View File
@@ -22,6 +22,18 @@ fn aging_total_is_sum() {
assert_eq!(i128::from(t), spec);
kani::cover!(t != 0, "a nonzero total exists");
}
// `None` only on overflow: whenever every running prefix fits in
// `i64` (not only inside the line-range box), the total exists.
let mut run: i128 = 0;
let mut fits = true;
for &v in &b {
run += i128::from(v);
fits &= i64::try_from(run).is_ok();
}
if fits {
assert!(got.is_some(), "every prefix fits, so the total exists");
kani::cover!(run != 0, "a nonzero total from fitting prefixes");
}
if b.iter().all(|v| v.in_line_range()) {
assert!(got.is_some(), "in-range buckets always total");
kani::cover!(
@@ -51,7 +63,14 @@ fn aging_total_is_sum() {
fn control_difference_identity() {
let (balance, docs, unattached): (i64, i64, i64) = (kani::any(), kani::any(), kani::any());
let spec = i128::from(balance) - i128::from(docs) - i128::from(unattached);
if let Some(d) = kernel::control_difference(balance, docs, unattached) {
let got = kernel::control_difference(balance, docs, unattached);
// `None` only on overflow: whenever both steps fit in `i64`, it exists.
let first = i128::from(balance) - i128::from(docs);
if i64::try_from(first).is_ok() && i64::try_from(spec).is_ok() {
assert!(got.is_some(), "both steps fit, so the difference exists");
kani::cover!(spec != 0, "a nonzero difference from fitting steps");
}
if let Some(d) = got {
assert_eq!(i128::from(d), spec);
if d == 0 {
assert_eq!(
@@ -90,6 +109,14 @@ fn reconciliation_total_identity() {
assert_eq!(i128::from(t), spec);
kani::cover!(t != 0, "a nonzero total");
}
// `None` only on overflow: whenever every running prefix fits in
// `i64`, the total exists.
let p1 = i128::from(f[0]) + i128::from(f[1]);
let p2 = p1 + i128::from(f[2]);
if [p1, p2, spec].iter().all(|&p| i64::try_from(p).is_ok()) {
assert!(got.is_some(), "every prefix fits, so the total exists");
kani::cover!(spec != 0, "a nonzero total from fitting prefixes");
}
if f.iter().all(|v| v.in_line_range()) {
assert!(got.is_some());
}
@@ -86,13 +86,27 @@ fn dec_wide(cents: i128) -> Decimal {
/// `None` there and `Decimal` is `Some` at a scale below 2; the
/// assertion pins that shape, and the canary test below flips when the
/// fix lands in `impl Amount for Decimal`.
fn same_or_rounded(got: Option<Decimal>, want: Option<i128>) -> Result<(), TestCaseError> {
fn same_or_rounded(
got: Option<Decimal>,
want: Option<i128>,
exact: i128,
) -> Result<(), TestCaseError> {
match (got, want) {
(Some(g), Some(w)) => prop_assert_eq!((g.mantissa(), g.scale()), (w, 2)),
(None, None) => {}
// Rounded at a coarser scale at some step (a later step may bring
// it back to scale 2): not exact, and no panic; the finding.
(Some(_), None) => {}
// The figure is still within one unit, at its own scale, of the
// exact value (`exact`, in cents).
(Some(g), None) => {
prop_assert!(g.scale() < 2, "{g} is not a coarser scale");
let unit = 10i128.pow(2 - g.scale());
let shown = g.mantissa() * unit;
prop_assert!(
(shown - exact).abs() <= unit,
"{g} is not within one unit of {exact} cents"
);
}
(None, Some(w)) => prop_assert!(false, "refused an exact {w}"),
}
Ok(())
@@ -145,15 +159,15 @@ proptest! {
let (a, b, c, d, e) = (f[0], f[1], f[2], f[3], f[4]);
let got = kernel::aging_total(dec_wide(a), dec_wide(b), dec_wide(c), dec_wide(d), dec_wide(e));
let want = ref_chain(&[(a, true), (b, true), (c, true), (d, true), (e, true)]);
same_or_rounded(got, want)?;
same_or_rounded(got, want, a + b + c + d + e)?;
let got = kernel::control_difference(dec_wide(a), dec_wide(b), dec_wide(c));
let want = ref_chain(&[(a, true), (b, false), (c, false)]);
same_or_rounded(got, want)?;
same_or_rounded(got, want, a - b - c)?;
let got = kernel::reconciliation_total(dec_wide(a), dec_wide(b), dec_wide(c), dec_wide(d));
let want = ref_chain(&[(a, true), (b, true), (c, true), (d, false)]);
same_or_rounded(got, want)?;
same_or_rounded(got, want, a + b + c - d)?;
}
}
@@ -166,10 +180,7 @@ fn control_identity_edges() {
assert_eq!(kernel::aging_total(max, zero, zero, zero, zero), Some(max));
assert_eq!(kernel::aging_total(min, zero, zero, zero, zero), Some(min));
assert_eq!(kernel::aging_total(max, one, zero, zero, zero), None);
assert_eq!(
kernel::aging_total(zero, zero, zero, zero, max).map(|_| ()),
Some(())
);
assert_eq!(kernel::aging_total(zero, zero, zero, zero, max), Some(max));
assert_eq!(kernel::aging_total(max, max, zero, zero, zero), None);
assert_eq!(kernel::aging_total(min, min, zero, zero, zero), None);
assert_eq!(kernel::aging_total(max, zero, zero, zero, min), Some(zero));
@@ -253,7 +264,7 @@ fn arb_decimal() -> impl Strategy<Value = Decimal> {
Just((1i128 << 96) - 1),
Just(-((1i128 << 96) - 1)),
],
0u32..=12,
0u32..=28,
)
.prop_map(|(m, s)| Decimal::from_i128_with_scale(m, s))
}
+34 -17
View File
@@ -49,15 +49,24 @@ round.
On `i64`, fixed arrays or plain bindings (no `Vec`, no `copy_from_slice`),
each with `kani::cover!`. No bound is fixed beyond the arity: each
harness quantifies over **arbitrary `i64`s** (the edge included) and
checks `Some(x)` against an exact `i128` specification, and (for
`aging_total_is_sum` and `reconciliation_total_identity`) that figures
within a journal line's range (2^40) always give `Some`.
checks `Some(x)` against an exact `i128` specification, and that `None`
means overflow and only overflow: every harness computes the `i128`
running prefixes and asserts `Some` whenever each prefix fits in `i64`
(the whole `i64` range, not only the journal-line box of 2^40, which is
also kept as a cover).
Finding, from tightening them (2026-11-09): the first run of the
tightened `control_difference_identity` failed. `Amount::checked_sub`'s
default negates `rhs` and adds, so `i64` (and `i32`) refused `x - MIN`
even where the difference fits. A model-type gap, not a `Decimal` one
(`Decimal` negation is total), but exactly what `None only on overflow`
forbids; `i32` and `i64` now override `checked_sub` with the native one.
| Harness | Law | Verification |
|---|---|---|
| `aging_total_is_sum` | 45 | five arbitrary `i64` buckets: `Some(t)` is the true sum; five in-range buckets always total; mixed signs reached (3 covers). 10.4 s verification, 25.6 s wall |
| `control_difference_identity` | 45 | three arbitrary `i64`s: `Some(d)` is `balance − documents − unattached`; `d = 0` exactly when `balance = documents + unattached`; agreeing in-range books differ by `Some(0)` (3 covers). 3.2 s verification, 30.9 s wall |
| `reconciliation_total_identity` | 45 | four arbitrary `i64`s: `Some(t)` is `beginning + gain + without − distributed`; in-range figures always total (1 cover; one `unreachable` check, which is the proof's `expect`, reported unreachable as it should be). 3.6 s verification, 39.6 s wall |
| `aging_total_is_sum` | 46 | five arbitrary `i64` buckets: `Some(t)` is the true sum; five in-range buckets always total; mixed signs reached (4 covers). 20.4 s verification, 31.2 s wall |
| `control_difference_identity` | 46 | three arbitrary `i64`s: `Some(d)` is `balance − documents − unattached`; `d = 0` exactly when `balance = documents + unattached`; agreeing in-range books differ by `Some(0)` (4 covers). 0.8 s verification, 10.3 s wall |
| `reconciliation_total_identity` | 46 | four arbitrary `i64`s: `Some(t)` is `beginning + gain + without − distributed`; in-range figures always total (2 covers; one `unreachable check, which is the proof's `expect`, reported unreachable as it should be). 1.7 s verification, 11.7 s wall |
Times: `cargo kani -p ledger-core --exact --harness proofs::control::<name>`,
the verifier's own time and the wall time including Kani's build, the
@@ -69,7 +78,12 @@ differ at the ending by `reconciliation_total`. As a harness over nine
terms it did not finish in 500 s, and over seven (the common cash, income
and other terms cancelled) not in 110 s; the proof is the equality of two
differently associated 64-bit sums, which bit-blasting is poor at. It was
dropped, not weakened. Lean's `Control.reconciliation_identity` proves it
dropped, not weakened. After review, both smaller forms were tried:
(a) the nine terms on `i32` (line range 2^20): no verdict in 400 s;
(b) the seven terms (cash, income and other fixed at `0`) on `i32`: no
verdict in 300 s; (b) alone on `i64` was the 110 s attempt above. The
solver does not close two differently associated sums at either width,
so no harness. Lean's `Control.reconciliation_identity` proves it
at any size, and `reconciliation_worksheet_identity` in the bridge checks
it on both `i64` and `Decimal`.
@@ -78,9 +92,9 @@ traceability gate, `crates/ledger-core/tests/bookspec/traceability.rs`):
| Harness | Law |
|---|---|
| `aging_total_is_sum` | 45 |
| `control_difference_identity` | 45 |
| `reconciliation_total_identity` | 45 |
| `aging_total_is_sum` | 46 |
| `control_difference_identity` | 46 |
| `reconciliation_total_identity` | 46 |
## Bridge (`tests/kernel_bridge/control.rs`)
@@ -119,7 +133,7 @@ flip when `impl Amount for Decimal` is fixed. It is reachable only near
no ordinary path sees it, but it means "exact or `None`" is not yet true
of `Decimal` at its edge (K-U10's, with K-U4's finding).
## Lean (bookspec `lean/Bookspec/Control.lean`, law 45)
## Lean (bookspec `lean/Bookspec/Control.lean`, law 46)
- `aging_partitions`: any documents placed in one of five buckets have
five bucket sums that add (`agingTotal`) to the sum of their balances,
@@ -155,12 +169,15 @@ and every golden passes untouched.
Left, with why:
- `ledger-package::aging::aging_report`: the column totals use raw
`+=` and `current + d30 + d60 + d90 + d90p`, which **panic** at the
`Decimal` edge (an aging of documents restored "of any size"). The grand
total is `aging_total` of the column sums; routing it would change a
panic into a refusal, which is a behaviour change, and the `+=`
accumulations need `kernel::total` first. A K-U10 case.
- `ledger-package::aging` (the CSV's total and the statement's column
totals): done after review. The raw `+=` and `current + d30 + ...` that
panicked at the `Decimal` edge are now `kernel::total_of` and
`kernel::aging_total`, a past-exact aging returns
`TooLargeToCompute("the aging total")` as an `Err`, and the unit test
`an_aging_past_exact_arithmetic_is_refused` checks it. Only the error
path changed: no golden moves. The store cannot be driven there (a
journal line is capped at one quadrillion), so the test reaches the
seam through the pure helpers `column_totals` and `outstanding_total`.
- `ledger-package::attention`'s overdue sum (`days_30 + 60 + 90 + 90+`)
is already checked (`try_fold(checked_add)`) and is the overdue
subtotal, not the aging total; left.