docs(proofs): K-U1 notes after review and D-75

The overflow harness proves two concrete cases (and non-positive rate for
every symbolic rate), not every overflow; step <= 0 -> None rests on K-U2's
round_step_refuses_nonpositive_step and the bridge; the D-75 precondition,
the open 96-bit edge, the bridge's new properties, Fx.booked_pos now proved
at the tie, and the BS-ledger-14 link landed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-08 18:08:12 -07:00
1 parent c5a4894985
commit c400624310
1 file changed
+82 -27
+82 -27
View File
@@ -1,6 +1,6 @@
# K-U1: FX conversion, `fx_booked`
Status: landed 2026-11-08. The kernel sweep's U1
Status: landed 2026-11-08; review fixes and D-75 the same day. The kernel sweep's U1
(`plan/kernel-sweep-audit.md` section 3): the one foreign-currency
conversion that books money (`Ledger::add_line_fx`) moves into the
kernel, proved once in bookspec's Lean, checked on `i64` by Kani and
@@ -25,7 +25,39 @@ keeps the pre-kernel scale (an exact product at or under the minor
places unchanged, otherwise exactly the minor places; a currency with
no minor unit books the exact product, as `round_minor` always did).
Behaviour is identical for every input: `add_line_fx` keeps its own
**The precondition (D-75, owner, 2026-11-08).** `Decimal::checked_mul`
is the exact product only while the product keeps its full scale: the
two scales added within 28, and the two mantissas multiplied within
`Decimal`'s 96 bits (7.9e28). Outside that it rounds the product itself
(to 28 places, or to the places 96 bits hold), and `fx_booked` then
rounds the rounded value: a second rounding. The K-U1 review found it
with foreign `0.99999999999999999999999999` (26 places) x rate `0.005`
into USD: the exact product is 0.00499999999999999999999999995, under
half a cent, but `Decimal` holds 0.005 and the booking is 0.01.
`Ledger::add_line_fx` now refuses a foreign amount or a rate of more
than `MAX_LINE_SCALE` (10) decimal places (trailing zeros not counted),
with `LedgerError::ValueOutOfRange` ("foreign amount ..." / "FX rate
..."; the variant for an amount that is not a journal line's), before
computing and never on replay (`is_replaying()`; `replay_add_line_fx`
books a journal's older line verbatim, and a store whose journal holds
a 26-place foreign amount opens with its figures). Ten places each give
a product of at most twenty, under 28. `fx_booked`'s spec block states
the precondition, and states that `Decimal::checked_mul`'s two
behaviours outside it are inherited, not hidden: rounding past scale 28
(and past 96 bits), and `1e-28 x 1e-28` underflowing to zero.
**An open edge, not closed by D-75.** The 96-bit half of the
precondition is not enforced: ten places each still allow a product of
twenty places past about 7.9e8, which needs more than 96 bits.
`1050000010.0000000001 x 9.9999999999` is 10500000099.89499999999999999999
exactly (10500000099.89 to the cent) but `Decimal` holds
10500000099.895, and the engine books 10500000099.90. The bridge pins
this (`the_mantissa_edge_is_outside_the_precondition_and_pinned`) and
the spec block names it; refusing it is a decision for the owner (see
the report that came with this change).
Behaviour is unchanged for every input within the precondition (inputs
outside it are the ones above): `add_line_fx` keeps its own
refusals (`InvalidDocumentLineAmount` for a non-positive foreign
amount, `InvalidFxRate` for a non-positive rate or a result that
rounds to zero, `CurrencyMismatch`, `AmountOutOfRange` on overflow),
@@ -56,6 +88,26 @@ carries the same laws to every `Decimal` step, and Lean to any step.
| `fx_booked_positive_for_positive` | positive inputs book non-negative, positive once the product reaches half a unit | 3.2 s (2 covers) |
| `fx_booked_refuses_overflow` | an overflowing product and a non-positive rate are `None`, never a panic | 2.6 s (2 covers) |
What the harnesses do and do not prove, stated plainly:
- `fx_booked_refuses_overflow` proves **two concrete overflow cases**
(`i64::MAX x 2` and `100 x i64::MAX`, step 1) are `None`, plus that a
non-positive rate is `None` for every symbolic rate <= 0. It is not a
proof that every overflowing product is refused.
- That every overflow is `None` rests on `i64::checked_mul` (and
`Decimal::checked_mul`) returning `None` when the product cannot be
held, which the bridge exercises (`Decimal::MAX x 2`, the proptest's
products), not on a Kani proof.
- **step <= 0 -> `None`** is not a harness of this unit: it is `round_step`'s
refusal, proved by K-U2's `round_step_refuses_nonpositive_step`
(`docs/proofs/k-u2.md`), and checked here by the bridge (`fx_booked(100, 1, 0)` is `None`).
- Every Kani law above is on the integer model, where the product is exact
by construction (`foreign * rate` is a bounded `i64` product, no
scale, no rounding before `round_step`). The D-75 precondition is
what carries those laws to `Decimal`: with it, `Decimal::checked_mul` is
the same exact integer product of mantissas, and the bridge compares
the engine to an independent exact reference (below).
Measured alone on this machine, 2026-11-08, with
`cargo kani -p ledger-core --exact --harness proofs::fx::<name>`
(verification time; wall time includes the build). `just prove`
@@ -68,9 +120,21 @@ standard axioms only.
plain-integer conversions, value for value.
- `decimal_matches_the_exact_reference`: an independent `i128`
reference (exact integer product, one integer rounding; no kernel
code, no `Decimal` division) over foreign amounts and rates of up
to 10 places, booked into 0, 2, and 3 minor units (JPY, USD, KWD),
through both `fx_booked` and `fx_booked_minor`.
code, no `Decimal` division) over foreign amounts and rates of every
scale D-75 allows, 0 to 10 places each (products to scale 20), with
mantissas to 2^47 (so the product fits 96 bits), booked into 0, 2,
and 3 minor units (JPY, USD, KWD), through both `fx_booked` and
`fx_booked_minor`. A result `Decimal` cannot hold is `None` in both.
- `ten_places_each_is_exact_through_the_engine`: the top of D-75's
range through `Ledger::add_line_fx` and the reference, a hand-checked
row each side of half a cent.
- `past_ten_places_the_reference_and_decimal_part_and_the_engine_refuses`:
the D-75 bound is what keeps the reference and the engine equal. The
review's 26-place input has reference 0.00 and kernel 0.01 (the
kernel inherits `Decimal`'s early rounding); `add_line_fx` refuses it
and any 11-place foreign amount or rate (`ValueOutOfRange`).
- `the_mantissa_edge_is_outside_the_precondition_and_pinned`: the open
edge above, pinned as it is.
- `fx_booked_at_the_edges`: 100.00 x 1.0834567 to the cent, 10.00 x
151.237 to the yen, a KWD half-thousandth tie, half-unit ties of
both signs, scale 28 (including `Decimal`'s multiplication
@@ -91,7 +155,10 @@ standard axioms only; `cargo test --workspace` and
- `Fx.booked_whole` — the booked amount is a multiple of the unit.
- `Fx.booked_near` — within half a unit of the exact product, any size.
- `Fx.booked_pos` — positive inputs book non-negative, and positive
once the product reaches half a unit.
once the product reaches half a unit, the tie included
(`step <= 2 * product`; first landed as `step < 2 * product`, short of
the stated law and of the Kani assertion `2 * exact >= step`; the tie
follows from `Round.tie_halfAway_pos` with `k = 0`).
`Fx.booked` is `Round.roundStep .halfAway` of the product, so the
three theorems restate `Round.roundStep_multiple`,
@@ -101,24 +168,12 @@ BS-ledger-1's FX invariants now name `Fx.booked_near`,
`Fx.booked_whole`, and `Fx.booked_pos` where the theorem matches;
its tie invariants keep `Deductible.roundHA_tie_*`.
BS-ledger-14 (the CHOICE split out of BS-ledger-1) is absent from
bookspec `main` (`c467093`, the commit `k-u1` branches from). Its
record is located at `rules/ledger/BS-ledger-14.toml` in commit
`233a569` on the unmerged branch `s-c7a` (worktree
`~/Code/bookspec-wt/s-c7a`, clean at inspection). Linking it from
`k-u1` therefore needs an owner/integrator decision:
1. **(Recommended)** Land S-C7a (or the merged `s-c7`) on `main`
first, rebase `k-u1`, then set BS-ledger-14's two FX invariants
to `lean = "Fx.booked_near"` (the exact-product invariant) and
`lean = "Fx.booked_whole"` (every booked amount in `Z * u`),
regenerating the pages. No other record changes.
2. Cherry-pick `233a569` into `k-u1` now and link there. This was
not done: that commit also creates BS-entry-6..8,
BS-ledger-15..21, BS-period-4/5, BS-statement-6..9 and
BS-prepaid-2..4, well outside this unit, and would collide with
S-C7 at merge.
Outcome recorded here: option 1; the BS-ledger-14 link is the one
item left for the integrator, and everything it needs (theorem
names, which invariant each matches) is stated above.
BS-ledger-14 (the CHOICE split out of BS-ledger-1, S-C7) is on bookspec
`main`; its two FX invariants now cite `Fx.booked_near` (the exact-product
rounding) and `Fx.booked_whole` (every booked amount in `Z * u`). The
D-75 bound is stated in BS-entry-4 as an engine-neutral MUST (a foreign
amount and an exchange rate each take at most ten decimal places, trailing
zeros not counted; replay exempt, BS-entry-8), with D-75 as its
provenance. wanderbooks' claim for B-28 (`docs/spec/claims/entry.md`)
lists the enforcement (`add_line_fx`, `ValueOutOfRange`, never on replay)
and the checks.