Fold the six proof notes of plan P14's kernel sweep into the one place the proofs are described. verification.md gains: - the harness table's thirty-four new rows, each with its note's law number and measured time (all run in `prove`; none is in `kani_slow`); - four new law sections in the existing house style: law 40 rounding to a step (K-U2), law 41 FX booking at the transaction-date rate (K-U1), law 42 shares by weights (K-U3), and law 43 equal monthly parts (K-U5); - law 4's collection-total harnesses (K-U4), law 23's Direction::of and law 27's part_weight harnesses (the entries note), with their bridges and the Lean theorems each cites; - the sweep's Kani bounds (fixed steps, rates, month counts, weights totals, the `unwind(4)` and MAX_CORRECTION_STEPS bounds) and the symbolic-divisor note that carries them to Lean and the bridge; - "What the proofs found" and "What is and isn't proved": K-U1's double rounding and D-75 precondition with its open 96-bit edge, K-U4's checked_add dust at Decimal::MAX, K-U3's quotient-crossing-a-step and non-terminating correction loop, the pinned tie ranking, the preserved printed scale, and K-U10's MAX and scale-28 edges, each with the test that pins it; - the overview table's scope, the sweep's bridge reference, and the harness/audit counts (97 harnesses, 87 in `prove`; 188 Lean theorems). Each unit note now opens with the section it was folded into; the notes stay as the units' records. No code changed. Co-Authored-By: DeepSeek V4.1 Flash <noreply@opencode.ai>
15 KiB
K-U3: proportional shares and weighted splits
Folded into docs/verification.md, section "Shares by weights
(BS-partner-1, BS-ledger-1)".
Status: landed 2026-11-09. The kernel sweep's U3
(plan/kernel-sweep-audit.md section 3): the six local copies of
"share = whole × weight ÷ total, round, hand out the leftover" call one
implementation; the laws are proved on i64 by Kani, checked on
Decimal against an independent exact reference, and stated once in
bookspec's Lean.
The API
pub enum Leftover { LargestRemainder, LastAbsorbs, FirstAbsorbs }
pub fn share_of<T: Amount>(whole: T, weight: T, total: T) -> Option<T>
pub fn share_floor_with<T: Amount>(
whole: T, weight: T, total: T, step: T,
round: impl Fn(T) -> Option<T>,
) -> Option<(T, T)>
pub fn split_by_weights_into<T: Amount>(
whole: T, weights: &[T], total: T, unit: T,
round: impl Fn(T) -> Option<T>, policy: Leftover,
parts: &mut [T], remainders: &mut [T],
) -> Option<()>
pub fn split_by_weights_with<T: Amount>(
whole: T, weights: &[T], total: T, unit: T,
round: impl Fn(T) -> Option<T>, policy: Leftover,
) -> Option<Vec<T>>
share_of is whole × weight ÷ total through the new
Amount::checked_div: Decimal::checked_div (exact where it terminates,
rounded at 28 significant digits otherwise) for the engine, truncating
integer division for a machine integer. The product is a
Decimal::checked_mul, which rounds at 96 bits too. That is the
computation every call site made before U3, to the digit.
share_floor_with is the exact floor of a share to a step, found by
multiplication rather than by trusting the quotient, plus the exact
remainder scaled by total. It is the arithmetic of the 1099 worksheet's
old correction loop, now bounded: past MAX_CORRECTION_STEPS steps in
either direction it returns None instead of walking without end.
split_by_weights_into is the split itself. The caller supplies the
total (not necessarily the sum of the weights: the conventions' shares
sum to one, the K-1 and party splits to their own total) and the
rounding function (the call sites pass round_places, whose scale rule
is what preserves the filed figures' scale; the Kani model passes
round_step). The leftover policy is explicit: largest remainder,
first absorbs, or last absorbs. The cumulative policy stays
apportion_cumulative, the running version, which tax_capital already
used. split_by_weights_with is the same split allocating its two
buffers; it and share_floor_with are the public API (share_floor and
split_by_weights had no engine caller and are gone).
Amount::checked_div is the one trait addition. It is documented as the
engine's division, not an exact-rational claim; checked_div_exact is
that one.
Kani harnesses (crates/ledger-core/src/proofs/allocate.rs)
All at i64, fixed arrays (no symbolic-length Vec, no
copy_from_slice), each with kani::cover!. The divisor is fixed in
every harness: CBMC's bit-blasted division by a symbolic i64 did not
finish in 30 minutes in K-U2, and every split divides by a total or a
step. The fixed weights are 2/3/5 of 10 (three different weights, a tied
split when the whole divides), the step is 1 (or 10 for the floor
harness), and the bounds are whole 0 to 1,000 (100 for the floor
harness). The laws are about the shape of the split, not the magnitude;
Lean proves them of the exact-share model at any size, and the bridge
checks the Decimal engine, whose ranking uses the rounded quotient.
| Harness | Law | Verification |
|---|---|---|
split_by_weights_sums |
18 | the parts sum to the whole exactly; a shortfall is handed out; the largest weight takes more. 23.9 s (2 covers) |
split_by_weights_within_one_step |
42 | each part is above -total and at most total from its share, scaled by the total; a part off its share and a part exactly one step above are reached. 14.4 s (2 covers) |
split_by_weights_no_part_exceeds |
18 | no part is negative or above the whole. 12.4 s (1 cover) |
split_by_weights_first_absorbs |
18 | the first part is its rounded share plus the shortfall; the sum is the whole; no part exceeds it. 2.3 s (1 cover) |
split_by_weights_last_absorbs |
18 | the last part is the whole less the others; the sum is the whole; no part exceeds it. 4.3 s (1 cover) |
split_by_weights_zero_total_is_zero |
18 | a zero total is zeros; all-zero weights hand the whole out by largest remainder while the shortfall fits, and refuse past it. 4.2 s (1 cover) |
share_of_refuses_overflow |
42 | share_of(whole, 2, 1) = whole × 2 exactly when the product fits; a zero total and an overflowing product are None, never a panic. 0.47 s (2 covers) |
share_floor_is_the_exact_floor |
42 | the part is a multiple of the step, one more step passes the share, and the remainder is exact below step × total; the correction down is reached. 5.1 s (2 covers) |
The upper bound of split_by_weights_within_one_step is inclusive on
the i64 model: its division truncates, so a part that divides exactly
can still take a unit when another part's truncation left one over
(whole = 5, weights 2/3/5: the first's 1 becomes 2, exactly one step
above its truncated share). The Decimal quotient keeps the fraction, a
part with no remainder never takes a unit, and the bridge checks the
strict bound there.
share_floor_is_the_exact_floor needed #[kani::unwind(4)]: without a
bound on the correction loops, CBMC tried to unwind them without end and
the harness ran past an hour at 1,000 × 100. With the bound and smaller
bounds (whole to 100, weight to 10, total 10, step 10) it verifies in
5.3 s. The bound is enough because half-away's first estimate is at most
one step above the exact floor and the increment loop at most one step
below; the bridge's exact reference carries the law for every other
step and magnitude. The loops also carry MAX_CORRECTION_STEPS now, so
a malformed input refuses; that bound only adds a condition inside the
same loops, and unwind(4) still holds.
Measured alone on this machine, 2026-11-09, with
cargo kani -p ledger-core --exact --harness proofs::allocate::<name>.
just prove (fast set): 80 harnesses, 0 failures. The recipe's
1800-second batch cap was hit twice while the shared machine carried a
load average of 26-33 (the log's completed harnesses all passed, the
batch was killed while the rest ran); the same fast set run manually
with -j 8 verified 80 of 80 harnesses, 0 failures (the heaviest
harness 500.8 s, the run inside its 3600-second cap). Lean:
verify/lean 151 theorems, standard axioms only.
Bridge (crates/ledger-core/tests/kernel_bridge/allocate.rs)
i64_and_decimal_agree: the Kani model in cents and the engine in dollars agree value for value where the shares divide exactly (thei64quotient is then the same exact share, so the largest-remainder recipient is the same).decimal_split_satisfies_the_kani_laws: for every whole, remainder or not, theDecimalparts sum to the whole, each is within one step of its share (strictly, scaled by the total), and none exceeds the whole.decimal_matches_the_exact_reference: an independenti128reference (integer products and remainders, no kernel code and noDecimal) over weights 2/3/5 of 10, whole in cents.decimal_matches_the_exact_reference_weights_sum_to_one: the same reference with weights 0.5/0.3/0.2 and total one.leftovers_at_each_policy: first and last absorbs against their own reference; the parts still sum.zero_weights_one_member_and_many: zero weights (the handout and the refusal past one unit per part), one member, and one to ten equal members, against the reference.share_floor_matches_the_exact_reference:share_floor_withagainst the exacti128floor for non-terminating totals (3, 7, 11), any step to 100; the quotient's 28-digit rounding is corrected.split_at_the_call_sites_scales: the K-1 property split (401 by 750/250), a cent 50/50, whole yen (101 by 51/51), and the exact no-rounding split, at the scales the call sites produce.decimal_edges_are_none_or_exact:Decimal::MAX, a product past it, a zero total, and scale 28: eachNoneor exact, never a panic.largest_remainder_ranks_like_the_exact_reference: random distinct weights over a non-terminating total (a factor of 3 or 7), against the exacti128reference's ranking; exact ties are pinned below, not left to fail this property.the_absorbing_parts_keep_the_old_scale: the strings, scale included, for 4860 by 6/0.7/0.5 (first4050, as the oldsplit_firstgave) and 902 by 1/0.000001/0.000001 (first902.00); last absorbs against the oldplug_lastand the no-places conventions split.split_into_matches_the_allocating_split: the fixed-arraysplit_by_weights_intoandsplit_by_weights_withagree, scale included (the gate requires a bridge that calls the fixed-array form).finding_ties_follow_the_rounded_quotient: 20.00 split 4/1/1 over 6 is 13.33/3.34/3.33, and 20 whole is 13/4/3, not the exact tie's earlier part (K-U10 decides).share_floor_refuses_past_the_correction_cap: the review's 0.1113 × 5909471247797156928749568911.8 ÷ 0.11134381908633486049673 returnsNonepromptly instead of hanging.finding_the_quotients_rounding_can_cross_a_step: the K-U10 case below.
Lean (bookspec, law 42)
lean/Bookspec/Apportion.lean, law 42 in spec/laws.md, headline
theorems in lean/Audit.lean. Build: lake build && lake env lean Audit.lean, standard axioms only; cargo test --workspace, render -- --check and scripts/spec-floors.sh main pass.
Apportion.exact_shares_sum— with the weights summing toW, the exact shares' numeratorsx · w_isum tox · W; no weight is lost or invented.Apportion.shares_of_near— a split part is within one step of its exact share, gain or loss. It isallocW_driftunder the name the kernel's split uses; nothing is reproved.
BS-partner-1's invariants now link exact_shares_sum and
shares_of_near (the split's own framing of the existing
allocW_drift bound). Apportion.apportion_sum, apportion_drift and
the allocW_* theorems were reused, not restated.
Findings
- No behaviour change for posted values. Every golden and every
existing test passes unchanged. The six sites keep their own rounding
mode, step, leftover policy, sign handling and error:
tax_capital::RunningSplit::share(used bysplitandunsplit) →share_of;k1::share_property→split_by_weights_with, total = Σ credits,10^-places, toward zero, largest remainder (shares::share_byreaches it through this);conventions::split→split_by_weights_with, total = one,10^-places, toward zero, largest remainder; no places → identity rounding, last absorbs;shares::split_first→split_by_weights_with, total = Σ weights, the cent, toward zero, first absorbs;form1099::vendor_part→share_ofthenround_places(thepayment == targetandtarget == 0short cuts stay);form1099::split_share→share_floor_withper bucket thenapportion(the hand-rolled correction loop is gone);cashflow::split_part→share_ofthenround_places.periods::allocateandperiods::allocate_last_absorbsare frozen replay rules: their arithmetic is untouched, and their doc comments now name the kernel policy each equals.k1.rs's test helper namedsplitat ~684 is not arithmetic (it readsk1_detailrows) and needed no routing.
- The
i64model's division truncates. The Kani within-one-step bound is therefore inclusive oni64; theDecimalbridge checks the strict bound for the engine. This is a model statement, not an engine change. - K-U10, Decimal's edges. At
Decimal's 96-bit mantissa edge,checked_div's rounding can move a share across a step, andchecked_mul's rounding hides it fromshare_floor_with's correction. Pinned byfinding_the_quotients_rounding_can_cross_a_step: the exact share is just above the whole, the quotient rounds to one cent above it, the largest-remainder split refuses (a part above the whole is never emitted), and the floor keeps the rounded step. That pinned case is the one no posted line reaches (the line cap is 10^15). The product's rounding is reachable: a scale-10 line below the cap multiplied by itself,123456789.0123456789², gives15241578753238836.750190519988, not the exact square. Both roundings are K-U10 items: it is for K-U10 to settle whether the kernel should refuse either outright. - One panic became a refusal.
k1::share_property's oldapportion(...).expect(...)would panic at the edge above (a shortfall of one unit per part is not rounding); the kernel returnsNoneand the site reportsTooLargeToCompute. Unreachable with line-capped credits, and the bar is no panic. - The correction is bounded. The floor harness's loops needed
#[kani::unwind(4)], and the review found an input whose rounded product put the estimate far from the exact floor, so the old loop did not terminate:0.1113 × 5909471247797156928749568911.8 ÷ 0.11134381908633486049673. The kernel now takes at mostMAX_CORRECTION_STEPSin each direction and returnsNonepast the cap; no figure the loop produced within the cap changes. The bridge'sshare_floor_refuses_past_the_correction_cappins the input, and its exacti128reference carries the same law for every step and magnitude. - The first part keeps the old scale. The old
shares::split_firstcomputedparts[0] + (whole − Σ parts); the first kernel version computedwhole − Σ rest. The values are equal, but the scale differs in 665 of 400k fuzzed cases: 4860 by 6/0.7/0.5 gave4050, 472.50, 337.50and the kernel's4050.00, …; 902 by 1/0.000001/0.000001 gave902.00and the kernel's902. That scale reachesPart.amount, so a printed share moved. The kernel computes the old expression now (with checked arithmetic), andthe_absorbing_parts_keep_the_old_scalecompares strings.LastAbsorbswas already the oldplug_lastand the no-places conventions split, and the same property checks it. - Ties follow the rounded quotient.
split_by_weights_intoranks the rounded quotients' remainders, not exact rationals, so an exact tie goes where the 28th digit sends it, not to the earlier part: 20.00 split 4/1/1 over 6 is13.33, 3.34, 3.33(exact ranking would be13.34, 3.33, 3.33), and 20 whole is13, 4, 3(exact would be14, 3, 3). The pre-U3 sites did the same. Pinned byfinding_ties_follow_the_rounded_quotient; the owner decides the exact ranking in K-U10, and the ranking is unchanged here.