test(core): Kani harness and bridge property for the deductible share

`deductible_amount_rounds_once` model-checks up to three accounts at i64
cents: the result is exactly the reference round-half-away-from-zero of
the exact numerator, within half a cent of the sum, never the opposite
sign, and (a cover) not the sum of per-account roundings; it also covers
a half-cent tie and a negative numerator (a refund). It is slow (992 s
alone on 2026-11-08), so it joins the justfile's kani_slow.
`deductible_amount_refuses_overflow` proves the two overflow paths give
None, not a wrapped figure.

The bridge property `deductible_amount_agrees` checks the i64 cents
result against the Decimal dollars result on the same accounts, and the
Decimal result against the exact sum within half a cent, with the sign
law and no negative zero: what Kani proved on i64 holds on Decimal, the
type the engine runs.

Co-Authored-By: DeepSeek V4.1 Flash <noreply@opencode.ai>
This commit is contained in:
swaitsandDeepSeek V4.1 Flash committed 2026-11-08 05:44:56 -07:00
1 parent 3cc9965a04
commit 9603eadfde
3 files changed
+117 -1

No files matched your search

+80
View File
@@ -1983,6 +1983,86 @@ fn line_amount_is_exact() {
);
}
// ---------------------------------------------------------------------------
// Tax categories (B-32)
// ---------------------------------------------------------------------------
/// `n / 100` rounded half away from zero: the reference for the one
/// rounding `kernel::deductible_amount` makes.
fn round_cent(n: i64) -> Option<i64> {
let q = n.checked_div(100)?;
let r = n.checked_rem(100)?;
if r >= 50 {
q.checked_add(1)
} else if r <= -50 {
q.checked_sub(1)
} else {
Some(q)
}
}
/// `kernel::deductible_amount` (rules.md B-32, B-21; CHOICE, owner
/// 2026-11-07, D-67) over up to three accounts in exact range: the exact
/// sum of the shares (`book × percent ÷ 100`) rounded once to the nearest
/// cent, ties away from zero; never the sum of per-account roundings; and
/// never the opposite sign to its sum.
#[kani::proof]
#[kani::unwind(5)]
fn deductible_amount_rounds_once() {
const T: usize = 3;
let n: usize = kani::any();
kani::assume(n <= T);
let shares: [(i64, i64); T] = core::array::from_fn(|_| {
let book: i64 = kani::any();
kani::assume(book.unsigned_abs() <= (1 << 20));
let percent: i64 = kani::any();
kani::assume(percent >= 0 && percent <= 100);
(book, percent)
});
// The exact numerator, and what rounding each account alone would
// have given. Under these bounds three shares cannot leave `i64`.
let mut exact = 0i64;
let mut per_account = 0i64;
for &(book, percent) in &shares[..n] {
let term = book * percent;
exact += term;
per_account += round_cent(term).expect("an i64 quotient by 100 fits");
}
let want = round_cent(exact).expect("an i64 quotient by 100 fits");
let got = kernel::deductible_amount(&shares[..n]);
assert_eq!(got, Some(want));
let err = i128::from(exact) - i128::from(want) * 100;
assert!(err.abs() <= 50, "within half a cent");
if exact >= 0 {
assert!(want >= 0, "the sum's sign, not its opposite");
} else {
assert!(want <= 0, "the sum's sign, not its opposite");
}
kani::cover!(n == T && exact % 100 == 50, "a tie at half a cent");
kani::cover!(exact < 0, "a negative numerator (a refund)");
kani::cover!(
per_account != want,
"rounding the category once differs from rounding each share"
);
}
/// `kernel::deductible_amount` refuses an overflow instead of wrapping: a
/// share too large for `i64`, and a sum of shares too large, both `None`.
#[kani::proof]
fn deductible_amount_refuses_overflow() {
// A product past `i64`: 100 percent of a book above i64::MAX / 100.
let book: i64 = kani::any();
kani::assume(book > i64::MAX / 100);
assert!(kernel::deductible_amount(&[(book, 100i64)]).is_none());
kani::cover!(book == i64::MAX, "the largest share");
// Two shares whose sum overflows, each product in range.
let book: i64 = kani::any();
kani::assume(book > i64::MAX / 2);
assert!(kernel::deductible_amount(&[(book, 1i64), (book, 1i64)]).is_none());
kani::cover!(book == i64::MAX, "a sum of shares too large");
}
// ---------------------------------------------------------------------------
// The checker itself
// ---------------------------------------------------------------------------
+36
View File
@@ -554,4 +554,40 @@ proptest! {
let wide = kernel::line_amount(dec(quantity), dec(unit_price));
prop_assert_eq!(narrow.map(|n| Decimal::new(n, 4)), wide);
}
/// The tax summary's one rounding (rules.md B-32, B-21):
/// `kernel::deductible_amount` at i64 cents (what Kani
/// `deductible_amount_rounds_once` proved) and at Decimal dollars
/// agree on the same accounts, and the Decimal result keeps the laws:
/// within half a cent of the exact sum, never the opposite sign, never
/// a negative zero.
#[test]
fn deductible_amount_agrees(
shares in prop::collection::vec((arb_cents(), 0u32..=100), 0..5),
) {
let i: Vec<(i64, i64)> = shares
.iter()
.map(|&(book, percent)| (book, i64::from(percent)))
.collect();
let d: Vec<(Decimal, Decimal)> = shares
.iter()
.map(|&(book, percent)| (dec(book), Decimal::from(percent)))
.collect();
let narrow = kernel::deductible_amount(&i);
let wide = kernel::deductible_amount(&d);
prop_assert_eq!(narrow.map(dec), wide);
let exact_cents: i64 = shares
.iter()
.map(|&(book, percent)| book * i64::from(percent))
.sum();
// The exact share in dollars: book cents x percent, over 10,000.
let exact = Decimal::new(exact_cents, 4);
if let Some(a) = wide {
let half_cent = Decimal::new(5, 3);
prop_assert!((a - exact).abs() <= half_cent);
prop_assert!(!(a > Decimal::ZERO && exact < Decimal::ZERO));
prop_assert!(!(a < Decimal::ZERO && exact > Decimal::ZERO));
prop_assert!(!(a.is_zero() && a.is_sign_negative()));
}
}
}
+1 -1
View File
@@ -65,7 +65,7 @@ sweep-test-tmp:
# 100 minutes of CPU, 2026-10-21) can't finish inside the 30-minute cap at
# four jobs, a CI runner's. Every other harness in src/proofs.rs runs in `prove`, a new one
# included; a name here that isn't a harness fails both (docs/verification.md).
kani_slow := "accounting_equation cumulative_close_never_drifts_a_unit_at_near_thirds cumulative_closing_entry_balances discount_max_is_bounded entry_flows_pure_are_movements entry_flows_sum_to_cash prorate_parts_sum_to_total sums_are_exact the_last_months_charge_is_within_half_a_cent"
kani_slow := "accounting_equation cumulative_close_never_drifts_a_unit_at_near_thirds cumulative_closing_entry_balances deductible_amount_rounds_once discount_max_is_bounded entry_flows_pure_are_movements entry_flows_sum_to_cash prorate_parts_sum_to_total sums_are_exact the_last_months_charge_is_within_half_a_cent"
# Formal proofs: Kani on the accounting kernel (all but kani_slow), Lean for books of any size (docs/verification.md)
prove: (_kani "fast")