test(core): Kani and bridge for the kernel payroll

Three Kani harnesses on i64, fixed arrays (five deductions, a
two-employee plan of eight lines): net_pay_is_gross_less_deductions
(19.5s verification), net_pay_nonnegative_when (7.0s) and
payroll_plan_balances through check_lines (297.6s), each with covers.
The bridge checks i64 against Decimal and Decimal against an
independent exact i128 reference, with the edges (Decimal::MAX/MIN,
mid-fold overflow) giving None, never a panic.

All three prove law 45 (bookspec, BS-entry-1/BS-entry-3/BS-entry-4).
The traceability gate passes against the k-u7 bookspec branch: 63
kernel functions, 48 fully backed, 45 law rows. docs/proofs/k-u7.md
is the unit's record.

Co-Authored-By: Muse Wonder 1.0
This commit is contained in:
swaitsandMuse Wonder 1.0 committed 2026-11-09 10:37:16 -07:00
1 parent e8f60532b1
commit fbe7eb7125
3 files changed
+477 -5

No files matched your search

+170 -3
View File
@@ -1,5 +1,172 @@
//! U7 proof harnesses: payroll arithmetic (kernel sweep).
//!
//! Empty in U0: the unit adds its Kani harnesses here; `just prove` scans
//! every file in `proofs/`, so a harness needs no change to the module
//! root or the recipe.
//! `kernel::net_pay` on `i64`: net is gross less the deductions exactly,
//! it is never negative when they do not exceed gross, and a payroll
//! plan built from net pay and totals balances through `check_lines`.
//!
//! A payroll line carries five employee deductions (federal, state,
//! Social Security, Medicare, other), so the deduction arrays are fixed
//! at five. The plan harness carries two employees and the plan's eight
//! totals (gross, employer FICA, the five payables, net), each a kernel
//! total of the two lines, in a fixed array of eight `(Side, amount)`
//! lines. Amounts run to a few thousand units: the law is about the
//! shape of the subtraction and the balance, not the magnitude, and
//! every total stays far inside the `i64` line range (2^40) that
//! `check_lines` requires. Lean proves the same laws at any size.
//!
//! ```text
//! GOAL: net pay is gross less the withholdings exactly; it is not
//! negative when they do not exceed gross; and the payroll plan
//! (Dr gross + employer FICA, Cr the payables + net) balances.
//! SOURCE: BS-entry-3 (exact amounts), BS-entry-4 (every payroll
//! figure and every derived figure computed with checked
//! arithmetic), BS-entry-1 (debits equal credits, the plan
//! through check_lines).
//! INVARIANTS:
//! 1. net_pay(gross, ds) = gross - sum(ds) whenever it is Some, and
//! None exactly when a running difference leaves the range.
//! 2. sum(ds) <= gross (ds non-negative) gives a net that is Some and
//! not negative.
//! 3. The eight plan lines pass check_lines: two or more lines, every
//! amount positive and in range, debits equal to credits.
//! SCENARIOS:
//! 1. IF the withholdings exceed gross: the net is the exact negative
//! difference (the engine refuses the line, not the arithmetic).
//! 2. IF a deduction is past the range: SHOULD be None.
//! ```
use crate::Side;
use crate::kernel::{check_lines, net_pay, total};
/// The five employee deductions of one payroll line.
const DEDUCTIONS: usize = 5;
/// One employee's figures for the plan harness: gross, the five
/// withholdings, and the two employer shares.
struct Employee {
gross: i64,
deductions: [i64; DEDUCTIONS],
ss_employer: i64,
medicare_employer: i64,
}
fn any_employee() -> Employee {
let gross: i64 = kani::any();
kani::assume((1..=10_000).contains(&gross));
let mut deductions = [0i64; DEDUCTIONS];
for deduction in &mut deductions {
let d: i64 = kani::any();
kani::assume((0..=1_000).contains(&d));
*deduction = d;
}
// Net pay is at least one unit, so the plan's cash line is posted.
kani::assume(total(deductions.iter().copied()).expect("in range") < gross);
let ss_employer: i64 = kani::any();
kani::assume((1..=1_000).contains(&ss_employer));
let medicare_employer: i64 = kani::any();
kani::assume((1..=1_000).contains(&medicare_employer));
Employee {
gross,
deductions,
ss_employer,
medicare_employer,
}
}
/// Net pay is gross less the deductions: the exact difference of the
/// gross and the deductions' total, for deductions of either sign.
#[kani::proof]
fn net_pay_is_gross_less_deductions() {
let gross: i64 = kani::any();
kani::assume((-10_000..=10_000).contains(&gross));
let mut deductions = [0i64; DEDUCTIONS];
for deduction in &mut deductions {
let d: i64 = kani::any();
kani::assume((-1_000..=1_000).contains(&d));
*deduction = d;
}
let deducted = total(deductions.iter().copied()).expect("in range");
let net = net_pay(gross, &deductions).expect("in range");
assert_eq!(net, gross - deducted, "net is gross less the deductions");
// Past the range the same call is None, never a panic.
assert!(net_pay(i64::MAX, &[-1]).is_none(), "overflow is refused");
assert!(net_pay(i64::MIN, &[1]).is_none(), "underflow is refused");
kani::cover!(deducted > gross, "withholdings exceed gross");
kani::cover!(net < 0, "a negative net is a value");
}
/// When the (non-negative) deductions do not exceed gross, the net is
/// the gross less their sum and is not negative.
#[kani::proof]
fn net_pay_nonnegative_when() {
let gross: i64 = kani::any();
kani::assume((0..=10_000).contains(&gross));
let mut deductions = [0i64; DEDUCTIONS];
for deduction in &mut deductions {
let d: i64 = kani::any();
kani::assume((0..=2_000).contains(&d));
*deduction = d;
}
let deducted = total(deductions.iter().copied()).expect("in range");
kani::assume(deducted <= gross);
let net = net_pay(gross, &deductions).expect("in range");
assert_eq!(net, gross - deducted, "net is gross less the deductions");
assert!(net >= 0, "the net is not negative");
kani::cover!(net == 0, "withholdings can take the whole gross");
kani::cover!(deducted > 0 && net > 0, "a partial withholding");
}
/// The payroll plan balances through `check_lines`: two employees, the
/// eight totals `payroll_entry_plan` posts (Dr gross, Dr employer FICA,
/// Cr federal, state, Social Security, Medicare and other payables, Cr
/// cash net), every total positive, debits equal to credits exactly.
#[kani::proof]
fn payroll_plan_balances() {
let employees = [any_employee(), any_employee()];
let gross = total(employees.iter().map(|e| e.gross)).expect("in range");
let employer = total(
employees
.iter()
.map(|e| e.ss_employer + e.medicare_employer),
)
.expect("in range");
let federal = total(employees.iter().map(|e| e.deductions[0])).expect("in range");
let state = total(employees.iter().map(|e| e.deductions[1])).expect("in range");
let ss = total(employees.iter().map(|e| e.deductions[2] + e.ss_employer)).expect("in range");
let medicare = total(
employees
.iter()
.map(|e| e.deductions[3] + e.medicare_employer),
)
.expect("in range");
let other = total(employees.iter().map(|e| e.deductions[4])).expect("in range");
let net = total(
employees
.iter()
.map(|e| net_pay(e.gross, &e.deductions).expect("in range")),
)
.expect("in range");
// The real plan skips a zero total; this harness takes the case
// where none is zero (gross, employer FICA, both FICA payables and
// net are positive by construction above).
kani::assume(federal > 0 && state > 0 && other > 0);
let plan: [(Side, i64); 8] = [
(Side::Debit, gross),
(Side::Debit, employer),
(Side::Credit, federal),
(Side::Credit, state),
(Side::Credit, ss),
(Side::Credit, medicare),
(Side::Credit, other),
(Side::Credit, net),
];
assert!(
check_lines(&plan).is_ok(),
"the payroll plan is an acceptable entry"
);
kani::cover!(
employees[0].gross != employees[1].gross,
"two employees differ"
);
kani::cover!(net > gross, "net over two employees exceeds one gross");
}
@@ -1,4 +1,140 @@
//! U7 bridge properties: payroll arithmetic (kernel sweep).
//!
//! Empty in U0: the unit adds its `Decimal`-vs-integer properties here,
//! beside `main.rs` and the other units.
//! `proofs/payroll.rs` proves `kernel::net_pay` on `i64` (five
//! deductions, a two-employee plan through `check_lines`); the engine
//! runs the same code on `Decimal`. These properties check that the two
//! instantiations agree, that `Decimal` agrees with an independent exact
//! `i128` reference (mantissas over a common power of ten, sharing no
//! code with the kernel), and the edge table: a gross or a running
//! difference past the type's range is `None`, never a panic.
use ledger_core::kernel;
use proptest::prelude::*;
use rust_decimal::Decimal;
fn dec(cents: i64) -> Decimal {
Decimal::new(cents, 2)
}
/// The exact net reference: gross less the deductions, from the
/// mantissas over their common power of ten, in `i128` throughout.
fn net_reference(gross: (i128, u32), deductions: &[(i128, u32)]) -> Option<Decimal> {
let scale = deductions
.iter()
.map(|&(_, s)| s)
.max()
.unwrap_or(0)
.max(gross.1);
let mut net: i128 = gross.0.checked_mul(10i128.checked_pow(scale - gross.1)?)?;
for &(mantissa, s) in deductions {
net = net.checked_sub(mantissa.checked_mul(10i128.checked_pow(scale - s)?)?)?;
}
Decimal::try_from_i128_with_scale(net, scale).ok()
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(2000))]
/// The `i64` kernel (cents) and the `Decimal` kernel (dollars) give
/// the same net for the same gross and five deductions.
#[test]
fn i64_and_decimal_net_pay_agree(
gross in -1_000_000_000i64..=1_000_000_000,
deductions in prop::collection::vec(-100_000_000i64..=100_000_000, 5),
) {
let i = kernel::net_pay(gross, &deductions);
let d = kernel::net_pay(
dec(gross),
&deductions.iter().copied().map(dec).collect::<Vec<_>>(),
);
prop_assert_eq!(i.map(dec), d);
}
/// `Decimal`'s net is the exact difference, for a gross and
/// deductions of every sign and scale.
#[test]
fn decimal_net_pay_matches_the_exact_reference(
gross_mantissa in -1_000_000_000i128..=1_000_000_000i128,
gross_scale in 0u32..=4,
deductions in prop::collection::vec(
(-1_000_000_000i128..=1_000_000_000i128, 0u32..=4),
0..=5,
),
) {
let gross = Decimal::try_from_i128_with_scale(gross_mantissa, gross_scale).unwrap();
let ds: Vec<Decimal> = deductions
.iter()
.map(|&(m, s)| Decimal::try_from_i128_with_scale(m, s).unwrap())
.collect();
let want = net_reference((gross_mantissa, gross_scale), &deductions);
prop_assert_eq!(kernel::net_pay(gross, &ds), want);
}
/// When the deductions do not exceed gross, the net is the gross
/// less their sum and is not negative, at both instantiations.
#[test]
fn decimal_net_pay_nonnegative_when(
gross in 0i64..=1_000_000_000,
deductions in prop::collection::vec(0i64..=100_000_000, 5),
) {
let deducted: i64 = deductions.iter().sum();
prop_assume!(deducted <= gross);
let net = kernel::net_pay(
dec(gross),
&deductions.iter().copied().map(dec).collect::<Vec<_>>(),
)
.expect("in range");
prop_assert_eq!(net, dec(gross - deducted));
prop_assert!(net >= Decimal::ZERO);
}
}
/// The edges: empty deductions, a zero net, and figures past the type's
/// range. Each answer is exact or `None`, never a panic.
#[test]
fn decimal_net_pay_at_the_edges() {
// No deductions: the net is the gross itself.
assert_eq!(
kernel::net_pay(Decimal::new(500_000, 2), &[]),
Some(Decimal::new(500_000, 2))
);
// The payroll scenario: 5,000.00 less 750, 250, 310, 72.50, 100.
assert_eq!(
kernel::net_pay(
Decimal::new(500_000, 2),
&[
Decimal::new(75_000, 2),
Decimal::new(25_000, 2),
Decimal::new(31_000, 2),
Decimal::new(7_250, 2),
Decimal::new(10_000, 2),
],
),
Some(Decimal::new(351_750, 2))
);
// Withholdings taking the whole gross give exactly zero, and more
// than the gross give the exact negative difference.
assert_eq!(
kernel::net_pay(Decimal::ONE, &[Decimal::ONE]),
Some(Decimal::ZERO)
);
assert_eq!(
kernel::net_pay(Decimal::ONE, &[Decimal::from(2)]),
Some(-Decimal::ONE)
);
// Past the range, in either direction and mid-fold: None.
assert_eq!(kernel::net_pay(Decimal::MAX, &[-Decimal::ONE]), None);
assert_eq!(kernel::net_pay(Decimal::MIN, &[Decimal::ONE]), None);
assert_eq!(
kernel::net_pay(Decimal::ZERO, &[Decimal::MAX, Decimal::MAX]),
None
);
// The same edges on the Kani instantiation.
assert_eq!(kernel::net_pay(i64::MAX, &[-1]), None);
assert_eq!(kernel::net_pay(i64::MIN, &[1]), None);
assert_eq!(kernel::net_pay(0i64, &[i64::MAX, i64::MAX]), None);
assert_eq!(
kernel::net_pay(5_000i64, &[750, 250, 310, 72, 100]),
Some(3_518)
);
}