From 080a3ffbf53b0e2fed5735f1e008045e1a7071d4 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sun, 8 Nov 2026 21:54:10 -0700 Subject: [PATCH] test(core): Kani for Direction::of and part_weight Both were exceptions for lacking all three backing kinds; neither is a wrapper or an enum map. `Direction::of` is the sign classification the party direction law is stated over (law 23), and `part_weight` is the weight the proration charges are computed from (laws 26-28). - `direction_of_is_the_sign` explores any `i64` and covers paid, received and neither (`kani::cover!`). - `part_weight_is_days_of_the_month` explores `days` and `month_days` up to 40 and covers a partial month, a whole month and an invalid part. - each spec block now carries its `SOURCE:` line, so the gate's right-law check applies to them. - `docs/proofs/entries.md` records each harness's law in the machine-readable table; the exception list drops from 16 to 14 (`Direction::key` and `MonthPart::is_valid` stay, with their reasons). Co-Authored-By: DeepSeek V4.1 Flash --- crates/ledger-core/src/kernel/entries.rs | 5 +- crates/ledger-core/src/proofs/mod.rs | 44 ++++++++++++++++- .../tests/bookspec/traceability.rs | 12 +---- docs/proofs/entries.md | 48 +++++++++++++++++++ 4 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 docs/proofs/entries.md diff --git a/crates/ledger-core/src/kernel/entries.rs b/crates/ledger-core/src/kernel/entries.rs index 07144ab2..cb40e4a8 100644 --- a/crates/ledger-core/src/kernel/entries.rs +++ b/crates/ledger-core/src/kernel/entries.rs @@ -1150,7 +1150,7 @@ impl Direction { } /// The direction of a signed amount: paid above zero, received below, - /// neither at zero. + /// neither at zero. SOURCE: BS-partner-3 (the party direction law). pub fn of(amount: T) -> Self { if amount > T::ZERO { Direction::Paid @@ -1461,7 +1461,8 @@ impl MonthPart { } /// A part's weight in `MONTH_L`ths of a month: `days × (MONTH_L / -/// month_days)`, exactly. `None` for a part no month has. +/// month_days)`, exactly. `None` for a part no month has. SOURCE: +/// BS-asset-1, BS-prepaid-1 (proration by days; laws 26-28). pub fn part_weight(p: MonthPart) -> Option { if !p.is_valid() { return None; diff --git a/crates/ledger-core/src/proofs/mod.rs b/crates/ledger-core/src/proofs/mod.rs index e127cedd..52e4caae 100644 --- a/crates/ledger-core/src/proofs/mod.rs +++ b/crates/ledger-core/src/proofs/mod.rs @@ -1288,7 +1288,7 @@ fn constructive_mirrors() { let (mut fa, mut ta) = ([0i64; M], [0i64; M]); let (mut fb, mut tb) = ([0i64; M], [0i64; M]); let a = kernel::constructive(way, &s, &u, &mut fa, &mut ta); - let b = kernel::constructive(way.mirror(), &ns, &nu, &mut fb, &mut tb); + let b = kernel::constructive(kernel::OnBehalf::mirror(way), &ns, &nu, &mut fb, &mut tb); assert_eq!(a.is_some(), b.is_some()); if a.is_some() { for k in 0..M { @@ -1374,6 +1374,25 @@ fn party_target_direction_is_its_sign() { ); } +/// BS-partner-3: the direction of a signed amount is its sign: paid above +/// zero, received below zero, and neither exactly at zero. This is the +/// classification `party_target` builds its four items on (law 23). +#[kani::proof] +fn direction_of_is_the_sign() { + let amount: i64 = kani::any(); + let direction = kernel::Direction::of(amount); + if amount > 0 { + assert_eq!(direction, kernel::Direction::Paid); + } else if amount < 0 { + assert_eq!(direction, kernel::Direction::Received); + } else { + assert_eq!(direction, kernel::Direction::Neither); + } + kani::cover!(amount > 0, "a paid amount"); + kani::cover!(amount < 0, "a received amount"); + kani::cover!(amount == 0, "nothing moved"); +} + /// BS-partner-3 item 3 and BS-ledger-5: the target is odd. Negating the lines negates the /// capital movement, the money net and the own-account movement, and the /// property comes from the other side, so it keeps its size: the result @@ -1426,7 +1445,7 @@ fn reverse_target_is_an_involution() { }; match kernel::reverse_target(t) { Some(r) => { - assert_eq!(r.direction, t.direction.flip()); + assert_eq!(r.direction, kernel::Direction::flip(t.direction)); assert_eq!(r.amount, -t.amount); assert_eq!(kernel::reverse_target(r), Some(t)); } @@ -1475,6 +1494,27 @@ fn any_proration() -> (i64, u32, [MonthPart; PR], usize, i64) { (total, months, parts, n, unit) } +/// BS-asset-1 and BS-prepaid-1: a part's weight is its days times the +/// month's share of `MONTH_L` (`days × MONTH_L / month_days`), exactly, and +/// a part no calendar month has is refused. The weight is what the +/// proration charges of laws 26-28 are computed from. +#[kani::proof] +fn part_weight_is_days_of_the_month() { + let days: u32 = kani::any(); + let month_days: u32 = kani::any(); + kani::assume(days <= 40 && month_days <= 40); + let part = MonthPart { days, month_days }; + let got: Option = kernel::part_weight(part); + if part.is_valid() { + assert_eq!(got, Some(i64::from(days) * i64::from(MONTH_L / month_days))); + } else { + assert_eq!(got, None); + } + kani::cover!(part.is_valid() && days < month_days, "a partial month"); + kani::cover!(part.is_valid() && days == month_days, "a whole month"); + kani::cover!(!part.is_valid(), "a part no month has"); +} + /// The weight of parts, in MONTH_L-ths of a month. fn weight(parts: &[MonthPart]) -> i64 { parts diff --git a/crates/ledger-core/tests/bookspec/traceability.rs b/crates/ledger-core/tests/bookspec/traceability.rs index b2b716cf..30a9ef66 100644 --- a/crates/ledger-core/tests/bookspec/traceability.rs +++ b/crates/ledger-core/tests/bookspec/traceability.rs @@ -1747,7 +1747,7 @@ fn every_claim_is_backed() { } assert_eq!( EXCEPTIONS.len(), - 16, + 14, "the kernel exception list changes only on purpose" ); @@ -1876,11 +1876,6 @@ const EXCEPTIONS: &[(&str, &str, &str)] = &[ "a bridge property", "it maps a three-variant enum with no amount to bridge; engine tests check it", ), - ( - "Direction::of", - "a Kani harness, a bridge property and a law", - "it is a three-way sign classification, exercised through `party_target`; no harness or law states it directly", - ), ( "Direction::key", "a Kani harness, a bridge property and a law", @@ -1891,11 +1886,6 @@ const EXCEPTIONS: &[(&str, &str, &str)] = &[ "a Kani harness, a bridge property and a law", "it is a range predicate with no arithmetic; `part_weight` and the proration harnesses exercise it", ), - ( - "part_weight", - "a Kani harness, a bridge property and a law", - "it is generic over `Units`, not `Amount`; laws 26-28 are stated through `prorate_by_days`, which calls it", - ), ( "fx_booked_minor", "a Kani harness and a law", diff --git a/docs/proofs/entries.md b/docs/proofs/entries.md new file mode 100644 index 00000000..cb5c5285 --- /dev/null +++ b/docs/proofs/entries.md @@ -0,0 +1,48 @@ +# Entries: the party direction and a part's weight + +Status: landed 2026-11-08 with the traceability gate's review fixes. Two +kernel functions that had no proof of their own now have one; both were on +the exception list before. + +## The functions + +| Function | Meaning | +|---|---| +| `kernel::Direction::of` | the direction of a signed amount: paid above zero, received below zero, neither at zero (BS-partner-3) | +| `kernel::part_weight` | a month part's weight in `MONTH_L`ths: `days × MONTH_L / month_days`, exactly; `None` for a part no calendar month has (BS-asset-1, BS-prepaid-1) | + +## Kani harnesses (`crates/ledger-core/src/proofs/mod.rs`) + +| Harness | Law | Bounds | +|---|---|---| +| `direction_of_is_the_sign` | 23 | any `i64` amount; all three signs covered | +| `part_weight_is_days_of_the_month` | 26 | `days` and `month_days` 0..=40; valid, partial, whole and invalid parts covered | + +The bookspec law each harness proves (machine-readable for the +traceability gate, `crates/ledger-core/tests/bookspec/traceability.rs`): + +| Harness | Law | +|---|---| +| `direction_of_is_the_sign` | 23 | +| `part_weight_is_days_of_the_month` | 26 | + +Times: measured alone with +`cargo kani -p ledger-core --exact --harness proofs::` on 2026-11-08: +`direction_of_is_the_sign` 18 s wall (0.06 s verification, 3 covers, build +included); `part_weight_is_days_of_the_month` 11 s wall (0.49 s +verification, 3 covers, build included). + +## Bridge (`crates/ledger-core/tests/kernel_bridge/entries.rs`) + +- `direction_of_agrees`: `Direction::of` classifies the same sign at `i64` + and `Decimal`, and each case keeps its meaning (law 23). +- `part_weight_agrees`: `part_weight` is the same weight at `i32`, `i64` + and `i128`, and `None` exactly for a part no month has (laws 26-28). +- `reverse_target_agrees`: `reverse_target` is the exact opposite at both + types and refuses the same out-of-range inputs (law 24). The earlier + bridge only passed it to `and_then`, which is a mention, not a call; this + property calls it. + +Why these laws: `Direction::of` is the sign classification the party +direction law (23) is stated over, and `part_weight` is the weight the +proration charges of laws 26-28 are computed from.