From d6bc8d6cfe3595c419831bab3c9c8f365e856342 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 9 Nov 2026 01:31:45 -0700 Subject: [PATCH] docs(core): what the shares round, where ties go, and the API The K-U3 note listed share_floor and split_by_weights, which are gone; it now names the two public functions. The Kani table carries the law number in its own column (the traceability gate reads only a bare integer there), law 18 for the split's shape and law 42 for the exact share and the floor. The note records the bridge's ranking, ties, scale and cap properties, and its findings say both checked_mul and checked_div can round at Decimal's edge (the product's rounding is reachable below the line cap, the pinned quotient case is not), that the correction is bounded, that the first part keeps the old scale, and that ties follow the rounded quotient. The proof-harness comments no longer claim a step of 7 or a strict i64 bound, and Lean is described as proving the exact-share model at any size, not the engine's rounded ranking. Co-Authored-By: DeepSeek V4.1 Flash --- docs/proofs/k-u3.md | 112 ++++++++++++++++++++++++++++++-------------- 1 file changed, 78 insertions(+), 34 deletions(-) diff --git a/docs/proofs/k-u3.md b/docs/proofs/k-u3.md index 4a7805ce..d3a00496 100644 --- a/docs/proofs/k-u3.md +++ b/docs/proofs/k-u3.md @@ -18,9 +18,6 @@ pub fn share_floor_with( whole: T, weight: T, total: T, step: T, round: impl Fn(T) -> Option, ) -> Option<(T, T)> -pub fn share_floor( - whole: T, weight: T, total: T, step: T, mode: Rounding, -) -> Option<(T, T)> pub fn split_by_weights_into( whole: T, weights: &[T], total: T, unit: T, @@ -31,22 +28,20 @@ pub fn split_by_weights_with( whole: T, weights: &[T], total: T, unit: T, round: impl Fn(T) -> Option, policy: Leftover, ) -> Option> -pub fn split_by_weights( - whole: T, weights: &[T], total: T, step: T, mode: Rounding, - policy: Leftover, -) -> Option> ``` `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. That is the computation every -call site made before U3, to the digit. +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; the loop is gone. +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 @@ -56,7 +51,9 @@ 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. +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 @@ -68,22 +65,23 @@ 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 (a tied split with three -different remainders), 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 at -any size and the bridge checks the `Decimal` engine. +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` | 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` | 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` | no part is negative or above the whole | 12.4 s (1 cover) | -| `split_by_weights_first_absorbs` | the first part is the whole less the others; the sum is the whole; no part exceeds it | 8.7 s (1 cover) | -| `split_by_weights_last_absorbs` | the last part is the whole less the others; the sum is the whole; no part exceeds it | 8.7 s (1 cover) | -| `split_by_weights_zero_total_is_zero` | 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` | `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` | 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.3 s (2 covers) | +| `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 @@ -100,7 +98,9 @@ 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. +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::`. @@ -140,10 +140,27 @@ harness 500.8 s, the run inside its 3600-second cap). Lean: 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: each `None` or 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 + exact `i128` reference'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 (first `4050`, as the old `split_first` gave) and + 902 by 1/0.000001/0.000001 (first `902.00`); last absorbs against the + old `plug_last` and the no-places conventions split. +- `split_into_matches_the_allocating_split`: the fixed-array + `split_by_weights_into` and `split_by_weights_with` agree, 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 + returns `None` promptly instead of hanging. - `finding_the_quotients_rounding_can_cross_a_step`: the K-U10 case below. -## Lean (bookspec, branch `k-u3`) +## 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 @@ -197,16 +214,43 @@ the `allocW_*` theorems were reused, not restated. Pinned by `finding_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. No posted line - reaches this edge (the line cap is 10^15); it is for K-U10 to settle - whether the kernel should refuse the quotient's rounding outright. + 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²`, gives + `15241578753238836.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. 4. **One panic became a refusal.** `k1::share_property`'s old `apportion(...).expect(...)` would panic at the edge above (a shortfall of one unit per part is not rounding); the kernel returns `None` and the site reports `TooLargeToCompute`. Unreachable with line-capped credits, and the bar is no panic. -5. **The floor harness's loops needed a bound.** Without - `#[kani::unwind(4)]` the correction loops were intractable; the - bridge's exact `i128` reference carries the same law for every step - and magnitude, including the non-terminating divisions the bound - harness does not reach. +5. **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 most + `MAX_CORRECTION_STEPS` in each direction and returns `None` past the + cap; no figure the loop produced within the cap changes. The bridge's + `share_floor_refuses_past_the_correction_cap` pins the input, and its + exact `i128` reference carries the same law for every step and + magnitude. +6. **The first part keeps the old scale.** The old `shares::split_first` + computed `parts[0] + (whole − Σ parts)`; the first kernel version + computed `whole − Σ rest`. The values are equal, but the scale differs + in 665 of 400k fuzzed cases: 4860 by 6/0.7/0.5 gave `4050, 472.50, + 337.50` and the kernel's `4050.00, …`; 902 by 1/0.000001/0.000001 gave + `902.00` and the kernel's `902`. That scale reaches `Part.amount`, so a + printed share moved. The kernel computes the old expression now (with + checked arithmetic), and `the_absorbing_parts_keep_the_old_scale` + compares strings. `LastAbsorbs` was already the old `plug_last` and the + no-places conventions split, and the same property checks it. +7. **Ties follow the rounded quotient.** `split_by_weights_into` ranks 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 is `13.33, 3.34, 3.33` (exact ranking would be + `13.34, 3.33, 3.33`), and 20 whole is `13, 4, 3` (exact would be + `14, 3, 3`). The pre-U3 sites did the same. Pinned by + `finding_ties_follow_the_rounded_quotient`; the owner decides the exact + ranking in K-U10, and the ranking is unchanged here.