test(core): Kani and bridge for round_step
Kani checks the primitive on i64 over fixed steps: a whole multiple, the half modes nearest, each mode's tie, idempotence, no negative zero, and a non-positive step refused. A symbolic divisor did not finish in 30 minutes (CBMC's bit-blasted division), so the steps are fixed constants; the bridge's exact-reference property carries the laws to every Decimal step. The bridge agrees i64 with Decimal, checks Decimal against an independent i128 reference over amounts of up to ten places and steps down to 1e-4, checks round_places against round_dp_with_strategy to the character, and has the edge table (.5, -.5, 0.004, 1.005, -1.005, scale 28, Decimal::MAX). docs/proofs/k-u2.md records the harnesses, bounds, times, bridge properties and the bookspec Lean theorem names. Co-Authored-By: DeepSeek V4.1 Flash <noreply@opencode.ai>
This commit is contained in:
1 parent
74943fb699
commit
f16525fd35
3 files changed
+609
-5
No files matched your search
@@ -1,5 +1,200 @@
|
||||
//! U2 proof harnesses: the rounding primitive (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::round_step` on `i64` money in the smallest unit: the result is
|
||||
//! a whole multiple of the step; the half modes are nearest; each mode
|
||||
//! breaks a tie as it says; rounding twice is rounding once; and the sign
|
||||
//! never flips (so no zero is ever a negative zero).
|
||||
//!
|
||||
//! The steps are the fixed constants below rather than a symbolic `step`:
|
||||
//! a symbolic divisor makes CBMC's bit-blasted division intractable (one
|
||||
//! harness did not finish in 30 minutes). They cover one, an even step, an
|
||||
//! odd step, a non-power-of-ten, a decimal step and a larger step; the
|
||||
//! bridge's exact-reference property carries the same laws to every
|
||||
//! `Decimal` step, and bookspec's Lean proves the law for any step. The
|
||||
//! amount is symbolic up to [`VALUE`].
|
||||
|
||||
use crate::kernel::{Rounding, round_step};
|
||||
use kani::cover;
|
||||
|
||||
/// Every amount the harnesses explore is within this magnitude.
|
||||
const VALUE: i64 = 1 << 12;
|
||||
/// The fixed steps: one, evens (a tie exists), odds, a non-power of ten,
|
||||
/// a decimal step, and a step well above one.
|
||||
const STEPS: [i64; 6] = [1, 2, 3, 7, 10, 101];
|
||||
/// The even steps, where an exact half exists.
|
||||
const EVEN_STEPS: [i64; 4] = [2, 4, 10, 100];
|
||||
|
||||
fn any_mode() -> Rounding {
|
||||
let mode: u8 = kani::any();
|
||||
kani::assume(mode < 4);
|
||||
match mode {
|
||||
0 => Rounding::HalfAwayFromZero,
|
||||
1 => Rounding::HalfToEven,
|
||||
2 => Rounding::TowardZero,
|
||||
_ => Rounding::TowardNegativeInfinity,
|
||||
}
|
||||
}
|
||||
|
||||
fn any_half_mode() -> Rounding {
|
||||
if kani::any() {
|
||||
Rounding::HalfAwayFromZero
|
||||
} else {
|
||||
Rounding::HalfToEven
|
||||
}
|
||||
}
|
||||
|
||||
fn any_value() -> i64 {
|
||||
let value: i64 = kani::any();
|
||||
kani::assume(value >= -VALUE && value <= VALUE);
|
||||
value
|
||||
}
|
||||
|
||||
fn check_multiple(value: i64, step: i64, mode: Rounding) {
|
||||
let rounded = round_step(value, step, mode).expect("no overflow under these bounds");
|
||||
assert_eq!(rounded % step, 0, "a whole multiple of the step");
|
||||
if value > 0 {
|
||||
assert!(rounded >= 0, "a positive amount never rounds below zero");
|
||||
} else if value < 0 {
|
||||
assert!(rounded <= 0, "a negative amount never rounds above zero");
|
||||
} else {
|
||||
assert_eq!(rounded, 0, "zero rounds to zero");
|
||||
}
|
||||
}
|
||||
|
||||
/// The result is a whole multiple of the step, and it never crosses zero:
|
||||
/// a positive amount rounds to a non-negative amount, a negative one to a
|
||||
/// non-positive amount, and zero to zero.
|
||||
#[kani::proof]
|
||||
#[kani::unwind(8)]
|
||||
fn round_step_is_multiple() {
|
||||
let value = any_value();
|
||||
let mode = any_mode();
|
||||
for step in STEPS {
|
||||
check_multiple(value, step, mode);
|
||||
cover!(
|
||||
step > 1 && value % step != 0,
|
||||
"a non-multiple rounded to a step above one"
|
||||
);
|
||||
}
|
||||
cover!(value.abs() % 101 > 1, "a non-multiple of a larger step");
|
||||
}
|
||||
|
||||
fn check_nearest(value: i64, step: i64, mode: Rounding) {
|
||||
let rounded = round_step(value, step, mode).expect("no overflow under these bounds");
|
||||
let distance = (rounded - value).abs();
|
||||
assert!(
|
||||
2 * distance <= step,
|
||||
"nearest: at most half a step away, a tie at exactly half"
|
||||
);
|
||||
}
|
||||
|
||||
/// The half modes are nearest: the result is within half a step of the
|
||||
/// amount, ties included.
|
||||
#[kani::proof]
|
||||
#[kani::unwind(8)]
|
||||
fn round_step_is_nearest() {
|
||||
let value = any_value();
|
||||
let mode = any_half_mode();
|
||||
for step in STEPS {
|
||||
check_nearest(value, step, mode);
|
||||
}
|
||||
cover!(value % 10 != 0, "a value that actually moves");
|
||||
cover!(value.abs() % 10 == 5, "an exact tie (the mode decides it)");
|
||||
cover!(value.abs() % 10 != 0 && value.abs() % 10 != 5, "a non-tie");
|
||||
}
|
||||
|
||||
/// At an exact tie every mode picks the multiple its definition names.
|
||||
/// The tie is built as `whole x step + step/2` (and its negation) with an
|
||||
/// even fixed step, so it is exact in `i64` and both signs are reached.
|
||||
#[kani::proof]
|
||||
#[kani::unwind(8)]
|
||||
fn round_step_ties() {
|
||||
let whole: i64 = kani::any();
|
||||
kani::assume(whole >= 0 && whole <= 64);
|
||||
let negative: bool = kani::any();
|
||||
for step in EVEN_STEPS {
|
||||
let half = step / 2;
|
||||
let magnitude = whole * step + half;
|
||||
let value = if negative { -magnitude } else { magnitude };
|
||||
|
||||
let up = whole + 1;
|
||||
let even = if whole % 2 == 0 { whole } else { up };
|
||||
let cases = [
|
||||
(Rounding::HalfAwayFromZero, up),
|
||||
(Rounding::HalfToEven, even),
|
||||
(Rounding::TowardZero, whole),
|
||||
(
|
||||
Rounding::TowardNegativeInfinity,
|
||||
if negative { up } else { whole },
|
||||
),
|
||||
];
|
||||
for (mode, pick) in cases {
|
||||
let want = if negative {
|
||||
-(pick * step)
|
||||
} else {
|
||||
pick * step
|
||||
};
|
||||
assert_eq!(
|
||||
round_step(value, step, mode),
|
||||
Some(want),
|
||||
"each mode's tie rule"
|
||||
);
|
||||
}
|
||||
}
|
||||
cover!(!negative, "a positive tie");
|
||||
cover!(negative, "a negative tie");
|
||||
cover!(whole % 2 == 1, "HalfToEven rounds an odd floor up");
|
||||
cover!(whole % 2 == 0, "HalfToEven keeps an even floor");
|
||||
}
|
||||
|
||||
/// Rounding twice is rounding once, in every mode.
|
||||
#[kani::proof]
|
||||
#[kani::unwind(8)]
|
||||
fn round_step_is_idempotent() {
|
||||
let value = any_value();
|
||||
let mode = any_mode();
|
||||
for step in STEPS {
|
||||
let once = round_step(value, step, mode).expect("no overflow under these bounds");
|
||||
assert_eq!(round_step(once, step, mode), Some(once));
|
||||
}
|
||||
}
|
||||
|
||||
/// No negative zero and no sign flip: zero in gives zero out, and a result
|
||||
/// that reaches zero only does so from an amount that rounds there.
|
||||
#[kani::proof]
|
||||
#[kani::unwind(8)]
|
||||
fn round_step_never_negative_zero() {
|
||||
let value = any_value();
|
||||
let mode = any_mode();
|
||||
for step in STEPS {
|
||||
let rounded = round_step(value, step, mode).expect("no overflow under these bounds");
|
||||
if value == 0 {
|
||||
assert_eq!(rounded, 0);
|
||||
}
|
||||
if rounded == 0 {
|
||||
assert!(
|
||||
value.abs() < step,
|
||||
"a zero is a rounding to the zero multiple"
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
value == 0 || rounded == 0 || (rounded > 0) == (value > 0),
|
||||
"the sign never flips"
|
||||
);
|
||||
}
|
||||
cover!(
|
||||
value % 3 != 0 || value % 10 != 0,
|
||||
"a non-zero amount rounded to another multiple"
|
||||
);
|
||||
}
|
||||
|
||||
/// A step of zero or less is refused, never guessed at.
|
||||
#[kani::proof]
|
||||
fn round_step_refuses_nonpositive_step() {
|
||||
let value = any_value();
|
||||
let mode = any_mode();
|
||||
for step in [0i64, -1, -7, i64::MIN] {
|
||||
assert_eq!(round_step(value, step, mode), None);
|
||||
}
|
||||
cover!(value > 0, "a positive amount with a refused step");
|
||||
}
|
||||
@@ -1,4 +1,268 @@
|
||||
//! U2 bridge properties: the rounding primitive (kernel sweep).
|
||||
//!
|
||||
//! Empty in U0: the unit adds its `Decimal`-vs-integer properties here,
|
||||
//! beside `main.rs` and the other units.
|
||||
//! `proofs/round.rs` proves `round_step` on `i64` money in the smallest
|
||||
//! unit; the engine runs the same code on `Decimal`. These properties check
|
||||
//! that both instantiations agree, that `Decimal` agrees with an
|
||||
//! independent integer reference on amounts of up to ten decimal places,
|
||||
//! that the primitive reproduces `round_dp_with_strategy` exactly (the
|
||||
//! behaviour U2 preserves), and the edge table: halves both ways, a
|
||||
//! sub-half amount, a binary-looking 1.005, scale 28 and `Decimal::MAX`.
|
||||
|
||||
use ledger_core::kernel::{Rounding, round_step};
|
||||
use proptest::prelude::*;
|
||||
use rust_decimal::Decimal;
|
||||
use rust_decimal::RoundingStrategy;
|
||||
|
||||
fn parse(text: &str) -> Decimal {
|
||||
text.parse().expect("a decimal literal")
|
||||
}
|
||||
|
||||
fn any_mode() -> impl Strategy<Value = Rounding> {
|
||||
prop_oneof![
|
||||
Just(Rounding::HalfAwayFromZero),
|
||||
Just(Rounding::HalfToEven),
|
||||
Just(Rounding::TowardZero),
|
||||
Just(Rounding::TowardNegativeInfinity),
|
||||
]
|
||||
}
|
||||
|
||||
fn strategy(mode: Rounding) -> RoundingStrategy {
|
||||
match mode {
|
||||
Rounding::HalfAwayFromZero => RoundingStrategy::MidpointAwayFromZero,
|
||||
Rounding::HalfToEven => RoundingStrategy::MidpointNearestEven,
|
||||
Rounding::TowardZero => RoundingStrategy::ToZero,
|
||||
Rounding::TowardNegativeInfinity => RoundingStrategy::ToNegativeInfinity,
|
||||
}
|
||||
}
|
||||
|
||||
/// An independent integer reference: `value x 10^-scale` rounded to a
|
||||
/// whole multiple of `10^-places`, in `mode`, using only `i128` division
|
||||
/// and remainder. Shares no code with the kernel.
|
||||
fn reference(value: i128, scale: u32, places: u32, mode: Rounding) -> Option<Decimal> {
|
||||
let common = scale.max(places);
|
||||
let amount = value.checked_mul(10i128.checked_pow(common - scale)?)?;
|
||||
let divisor = 10i128.checked_pow(common - places)?;
|
||||
let negative = amount < 0;
|
||||
let magnitude = amount.unsigned_abs();
|
||||
let divisor = u128::try_from(divisor).ok()?;
|
||||
let floor = magnitude / divisor;
|
||||
let remainder = magnitude % divisor;
|
||||
let twice = remainder.checked_mul(2)?;
|
||||
let round_up = match mode {
|
||||
Rounding::TowardZero => false,
|
||||
Rounding::TowardNegativeInfinity => negative && remainder != 0,
|
||||
Rounding::HalfAwayFromZero => twice >= divisor,
|
||||
Rounding::HalfToEven => twice > divisor || (twice == divisor && !floor.is_multiple_of(2)),
|
||||
};
|
||||
let count = if round_up {
|
||||
floor.checked_add(1)?
|
||||
} else {
|
||||
floor
|
||||
};
|
||||
let count = i128::try_from(count).ok()?;
|
||||
Decimal::try_from_i128_with_scale(if negative { -count } else { count }, places).ok()
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#![proptest_config(ProptestConfig::with_cases(2000))]
|
||||
|
||||
/// The `i64` kernel (the Kani model) and the `Decimal` kernel (the
|
||||
/// engine) agree on the same money in cents.
|
||||
#[test]
|
||||
fn i64_and_decimal_agree(
|
||||
value in -(1i64 << 40)..=(1i64 << 40),
|
||||
step in 1i64..=(1i64 << 20),
|
||||
mode in any_mode(),
|
||||
) {
|
||||
let ours = round_step(value, step, mode).map(|r| Decimal::new(r, 2));
|
||||
prop_assert_eq!(
|
||||
ours,
|
||||
round_step(Decimal::new(value, 2), Decimal::new(step, 2), mode)
|
||||
);
|
||||
}
|
||||
|
||||
/// The exact reference above, on books of up to ten decimal places and
|
||||
/// steps from one to a ten-thousandth: the `Decimal` primitive is the
|
||||
/// integer rounding, carried out exactly.
|
||||
#[test]
|
||||
fn decimal_matches_the_integer_reference(
|
||||
value in -(1i128 << 60)..=(1i128 << 60),
|
||||
scale in 0u32..=10,
|
||||
places in 0u32..=4,
|
||||
mode in any_mode(),
|
||||
) {
|
||||
let amount = Decimal::try_from_i128_with_scale(value, scale).expect("in Decimal's range");
|
||||
let step = Decimal::new(1, places);
|
||||
prop_assert_eq!(
|
||||
round_step(amount, step, mode),
|
||||
reference(value, scale, places, mode)
|
||||
);
|
||||
}
|
||||
|
||||
/// Behaviour preservation: with a power-of-ten step, `round_step` is
|
||||
/// exactly `round_dp_with_strategy`, the call sites' old primitive.
|
||||
#[test]
|
||||
fn round_step_is_round_dp_for_a_power_of_ten(
|
||||
value in -(1i128 << 60)..=(1i128 << 60),
|
||||
scale in 0u32..=10,
|
||||
places in 0u32..=10,
|
||||
mode in any_mode(),
|
||||
) {
|
||||
let amount = Decimal::try_from_i128_with_scale(value, scale).expect("in Decimal's range");
|
||||
let mut want = amount.round_dp_with_strategy(places, strategy(mode));
|
||||
// The kernel never returns a negative zero; Decimal's rounding can.
|
||||
if want.is_zero() && want.is_sign_negative() {
|
||||
want = Decimal::ZERO;
|
||||
}
|
||||
prop_assert_eq!(round_step(amount, Decimal::new(1, places), mode), Some(want));
|
||||
}
|
||||
|
||||
/// And `round_places` matches it to the character: same value, same
|
||||
/// scale, the call sites' exact output. (A negative zero becomes +0.)
|
||||
#[test]
|
||||
fn round_places_matches_round_dp_to_the_character(
|
||||
value in -(1i128 << 60)..=(1i128 << 60),
|
||||
scale in 0u32..=10,
|
||||
places in 0u32..=10,
|
||||
mode in any_mode(),
|
||||
) {
|
||||
let amount = Decimal::try_from_i128_with_scale(value, scale).expect("in Decimal's range");
|
||||
let got = ledger_core::kernel::round_places(amount, places, mode).unwrap();
|
||||
let want = amount.round_dp_with_strategy(places, strategy(mode));
|
||||
if want.is_zero() {
|
||||
prop_assert!(got.is_zero());
|
||||
prop_assert!(!got.is_sign_negative());
|
||||
} else {
|
||||
prop_assert_eq!(got.to_string(), want.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The edge table, each row checked by hand.
|
||||
#[test]
|
||||
fn round_step_at_the_edges() {
|
||||
let cent = parse("0.01");
|
||||
// A half, both signs, in every mode.
|
||||
assert_eq!(
|
||||
round_step(parse("0.5"), cent, Rounding::HalfAwayFromZero),
|
||||
Some(parse("0.50"))
|
||||
);
|
||||
assert_eq!(
|
||||
round_step(parse("-0.5"), cent, Rounding::HalfAwayFromZero),
|
||||
Some(parse("-0.50"))
|
||||
);
|
||||
assert_eq!(
|
||||
round_step(parse("0.5"), cent, Rounding::HalfToEven),
|
||||
Some(parse("0.50"))
|
||||
);
|
||||
assert_eq!(
|
||||
round_step(parse("-0.5"), cent, Rounding::TowardNegativeInfinity),
|
||||
Some(parse("-0.50"))
|
||||
);
|
||||
// Under half a cent: zero, and never a negative zero.
|
||||
let tiny = round_step(parse("0.004"), cent, Rounding::HalfAwayFromZero).unwrap();
|
||||
assert!(tiny.is_zero());
|
||||
assert!(!tiny.is_sign_negative());
|
||||
let tiny = round_step(parse("-0.004"), cent, Rounding::HalfToEven).unwrap();
|
||||
assert!(tiny.is_zero());
|
||||
assert!(!tiny.is_sign_negative());
|
||||
// 1.005 is exactly a half at the cent (Decimal carries it exactly).
|
||||
assert_eq!(
|
||||
round_step(parse("1.005"), cent, Rounding::HalfAwayFromZero),
|
||||
Some(parse("1.01"))
|
||||
);
|
||||
assert_eq!(
|
||||
round_step(parse("1.005"), cent, Rounding::HalfToEven),
|
||||
Some(parse("1.00"))
|
||||
);
|
||||
assert_eq!(
|
||||
round_step(parse("-1.005"), cent, Rounding::HalfAwayFromZero),
|
||||
Some(parse("-1.01"))
|
||||
);
|
||||
assert_eq!(
|
||||
round_step(parse("-1.005"), cent, Rounding::HalfToEven),
|
||||
Some(parse("-1.00"))
|
||||
);
|
||||
assert_eq!(
|
||||
round_step(parse("-1.005"), cent, Rounding::TowardNegativeInfinity),
|
||||
Some(parse("-1.01"))
|
||||
);
|
||||
// Scale 28: a whole multiple, and a tie at the last place.
|
||||
let last = parse("0.0000000000000000000000000001");
|
||||
let five = parse("0.0000000000000000000000000005");
|
||||
assert_eq!(round_step(five, last, Rounding::HalfToEven), Some(five));
|
||||
let two_last = parse("0.0000000000000000000000000002");
|
||||
assert_eq!(
|
||||
round_step(five, two_last, Rounding::HalfToEven),
|
||||
Some(parse("0.0000000000000000000000000004"))
|
||||
);
|
||||
assert_eq!(
|
||||
round_step(five, two_last, Rounding::HalfAwayFromZero),
|
||||
Some(parse("0.0000000000000000000000000006"))
|
||||
);
|
||||
// Decimal's largest value is a whole multiple of a unit and a cent.
|
||||
assert_eq!(
|
||||
round_step(Decimal::MAX, Decimal::ONE, Rounding::HalfToEven),
|
||||
Some(Decimal::MAX)
|
||||
);
|
||||
assert_eq!(
|
||||
round_step(Decimal::MAX, cent, Rounding::HalfAwayFromZero),
|
||||
Some(Decimal::MAX)
|
||||
);
|
||||
// A step that is not positive is refused.
|
||||
assert_eq!(
|
||||
round_step(Decimal::ONE, Decimal::ZERO, Rounding::HalfToEven),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
round_step(Decimal::ONE, -Decimal::ONE, Rounding::HalfToEven),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
/// `round_places` is the behaviour-preserving wrapper the call sites use:
|
||||
/// it keeps `round_dp_with_strategy`'s scale and its early return.
|
||||
#[test]
|
||||
fn round_places_keeps_the_old_scale() {
|
||||
use ledger_core::kernel::round_places;
|
||||
assert_eq!(
|
||||
round_places(parse("1.200"), 2, Rounding::HalfToEven),
|
||||
Some(parse("1.20"))
|
||||
);
|
||||
assert_eq!(
|
||||
round_places(parse("1.5"), 2, Rounding::HalfToEven),
|
||||
Some(parse("1.5"))
|
||||
);
|
||||
let zero = round_places(parse("0.004"), 2, Rounding::HalfAwayFromZero).unwrap();
|
||||
assert_eq!(zero.to_string(), "0.00");
|
||||
assert!(!zero.is_sign_negative());
|
||||
}
|
||||
|
||||
/// `round_minor` picks the currency's own unit; a currency with none is
|
||||
/// returned exactly (the FX rule, B-3).
|
||||
#[test]
|
||||
fn round_minor_follows_the_currency() {
|
||||
use ledger_core::Currency;
|
||||
use ledger_core::kernel::round_minor;
|
||||
assert_eq!(
|
||||
round_minor(parse("1.2345"), Currency::USD, Rounding::HalfAwayFromZero),
|
||||
Some(parse("1.23"))
|
||||
);
|
||||
assert_eq!(
|
||||
round_minor(parse("1234.5"), Currency::JPY, Rounding::HalfAwayFromZero),
|
||||
Some(parse("1235"))
|
||||
);
|
||||
assert_eq!(
|
||||
round_minor(
|
||||
parse("1.2345"),
|
||||
Currency::new("KWD").unwrap(),
|
||||
Rounding::HalfAwayFromZero
|
||||
),
|
||||
Some(parse("1.235"))
|
||||
);
|
||||
let gold = Currency::new("XAU").unwrap();
|
||||
assert_eq!(
|
||||
round_minor(parse("1.23456789"), gold, Rounding::HalfAwayFromZero),
|
||||
Some(parse("1.23456789"))
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
# K-U2: one rounding primitive, `round_step`
|
||||
|
||||
Status: landed 2026-11-08. The kernel sweep's U2
|
||||
(`plan/kernel-sweep-audit.md` section 3): every money `round_dp*`/`rescale`
|
||||
in `ledger-core` goes through one kernel primitive, the law is proved once
|
||||
in bookspec's Lean, and the primitive is checked on `i64` by Kani and on
|
||||
`Decimal` by the bridge.
|
||||
|
||||
## The API
|
||||
|
||||
```rust
|
||||
pub enum Rounding { HalfAwayFromZero, HalfToEven, TowardZero, TowardNegativeInfinity }
|
||||
|
||||
pub fn round_step<T: Amount>(amount: T, step: T, mode: Rounding) -> Option<T>
|
||||
pub fn round_places(amount: Decimal, places: u32, mode: Rounding) -> Option<Decimal>
|
||||
pub fn round_minor(amount: Decimal, currency: Currency, mode: Rounding) -> Option<Decimal>
|
||||
```
|
||||
|
||||
`round_step` is `amount` to a whole multiple of a positive `step` in `mode`;
|
||||
`None` for `step <= 0` or an unrepresentable result; never a panic and never
|
||||
a negative zero. `round_places` is the behaviour-preserving call-site
|
||||
wrapper: when the amount carries no more places than `places` it is returned
|
||||
unchanged, otherwise the result carries exactly `places` places, matching
|
||||
`Decimal::round_dp_with_strategy` to the character (including its scale).
|
||||
`round_minor` picks `10^-currency.minor_units()`; a currency ISO 4217 gives
|
||||
no minor unit is returned exactly, the FX rule's existing behaviour (B-3).
|
||||
|
||||
The `Amount` trait gained `checked_round_to_step`, implemented for
|
||||
`Decimal`, `i32` and `i64` exactly as `checked_div_round_cent` was: the
|
||||
`Decimal` implementation is exact integer arithmetic on mantissas and scales
|
||||
(no 28-digit intermediate division), with the trailing-zero reduction that
|
||||
`checked_div_round_cent` uses when the result can still be held.
|
||||
|
||||
## Kani harnesses (`crates/ledger-core/src/proofs/round.rs`)
|
||||
|
||||
All run at `i64`, amounts symbolic in `[-2^12, 2^12]`, over the fixed steps
|
||||
`1, 2, 3, 7, 10, 101` (and the even steps `2, 4, 10, 100` for the tie
|
||||
harness). A symbolic divisor was tried first and did not finish in 30
|
||||
minutes: CBMC's bit-blasted division by a symbolic `i64` is intractable. The
|
||||
fixed steps include an even step (a tie exists), odd steps, a
|
||||
non-power-of-ten, a decimal step and a step well above one; the bridge's
|
||||
exact-reference property carries the same laws to every `Decimal` step, and
|
||||
bookspec's Lean proves them for any step.
|
||||
|
||||
| Harness | Law | Verification |
|
||||
|---|---|---|
|
||||
| `round_step_is_multiple` | the result is a whole multiple of the step; the sign never crosses zero; zero in, zero out | 5.1 s (2 covers) |
|
||||
| `round_step_is_nearest` | the half modes are within half a step (a tie at exactly half) | 22 s total (3 covers) |
|
||||
| `round_step_ties` | an exact tie: half away goes on, half even takes the even index, toward zero truncates, toward negative infinity floors (both signs) | 22 s total (4 covers) |
|
||||
| `round_step_is_idempotent` | rounding twice is rounding once | 16 s total |
|
||||
| `round_step_never_negative_zero` | zero in gives zero; a zero result comes from under a step; the sign never flips | 12 s total (1 cover) |
|
||||
| `round_step_refuses_nonpositive_step` | a step of zero or less is `None` | 8 s total (1 cover) |
|
||||
|
||||
Measured alone on this machine, 2026-11-08, with
|
||||
`cargo kani -p ledger-core --exact --harness proofs::round::<name>`.
|
||||
|
||||
`just prove` (fast set): **59 harnesses, 0 failures** (53 before this unit,
|
||||
6 added), 440 s of Kani, 470 s wall including the Lean build (a second run,
|
||||
after the last cover was added: 406 s wall); all covers satisfied.
|
||||
|
||||
## Bridge (`crates/ledger-core/tests/kernel_bridge/round.rs`)
|
||||
|
||||
- `i64_and_decimal_agree`: the Kani model in cents and the engine in dollars
|
||||
agree, value for value, for every mode.
|
||||
- `decimal_matches_the_integer_reference`: an independent `i128` reference
|
||||
(integer division and remainder, sharing no code with the kernel) over
|
||||
amounts of 0 to 10 decimal places and steps 1, 0.1, 0.01, 0.001, 0.0001.
|
||||
- `round_step_is_round_dp_for_a_power_of_ten`: with a power-of-ten step the
|
||||
primitive is exactly the old `round_dp_with_strategy` value.
|
||||
- `round_places_matches_round_dp_to_the_character`: and the wrapper matches
|
||||
it including scale (a negative zero becomes `+0`).
|
||||
- `round_step_at_the_edges`: `.5`, `-.5`, `0.004`, `1.005`, `-1.005`, a tie
|
||||
at scale 28, `Decimal::MAX`, and a non-positive step.
|
||||
- `round_places_keeps_the_old_scale`, `round_minor_follows_the_currency`:
|
||||
the wrapper's early return and scale, and USD/JPY/KWD/gold minor units.
|
||||
|
||||
All pass with the rest of `kernel_bridge`.
|
||||
|
||||
## Lean (bookspec, branch `k-u2`)
|
||||
|
||||
`lean/Bookspec/Round.lean`, law 40 in `spec/laws.md`, headline theorems in
|
||||
`lean/Audit.lean`. Build: `lake build && lake env lean Audit.lean`, standard
|
||||
axioms only; `cargo test --workspace` passes (the checker's law count moves
|
||||
from 39 to 40).
|
||||
|
||||
- `Round.roundStep_multiple` — the result is a multiple of the step.
|
||||
- `Round.near_halfAway`, `Round.near_halfEven` — within half a step.
|
||||
- `Round.tie_halfAway_pos` / `_neg`, `Round.tie_halfEven_pos` / `_neg`,
|
||||
`Round.tie_towardZero_pos` / `_neg`, `Round.tie_towardNegInf_pos` / `_neg`
|
||||
— each mode's tie rule, both signs.
|
||||
- `Round.roundStep_idempotent` — rounding twice is rounding once.
|
||||
- `Round.roundStep_odd` — the symmetric modes are odd
|
||||
(`round(-x) = -round(x)`); toward negative infinity is excluded by a
|
||||
hypothesis because it is not symmetric.
|
||||
- `Round.roundStep_scale` — `round(c·x, c·step) = c·round(x, step)` for a
|
||||
positive `c`.
|
||||
|
||||
The half-away mode is `Deductible.roundHA`; its near, tie, odd and scale
|
||||
theorems are reused, not restated. The model rounds the magnitude and puts
|
||||
the sign back, so an `Int` has no negative zero.
|
||||
|
||||
## Call sites
|
||||
|
||||
Every money `round_dp*` was replaced; the remaining `rescale`s in
|
||||
`ledger-core` are display padding (`money.rs` `Steps::decimal`,
|
||||
`reports.rs` contribution-margin's one place), not rounding, and were left.
|
||||
|
||||
Counts by file (14 replaced):
|
||||
|
||||
| File | Sites |
|
||||
|---|---|
|
||||
| `ledger.rs` | 1 (`add_line_fx`, via `round_minor`) |
|
||||
| `periods.rs` | 3 |
|
||||
| `schedules.rs` | 1 |
|
||||
| `form1099.rs` | 2 |
|
||||
| `shares.rs` | 1 |
|
||||
| `k1.rs` | 1 |
|
||||
| `conventions.rs` | 1 |
|
||||
| `tax_capital.rs` | 2 |
|
||||
| `cashflow.rs` | 1 |
|
||||
| `reports.rs` | 1 |
|
||||
|
||||
No result changes: the wrapper reproduces `round_dp_with_strategy` exactly
|
||||
(value, scale and early return), the bridge checks that to the character,
|
||||
and every existing test and golden passed unchanged.
|
||||
|
||||
### Currency-blind sites (kept at two places, on purpose)
|
||||
|
||||
These round to cents whatever the base currency; fixing them is another
|
||||
unit's decision (D-72 for schedules), not this behaviour-preserving one:
|
||||
|
||||
- `schedules.rs` `amortization_plan` — the monthly-even plan's replay-frozen
|
||||
cent rule; K-U5 owns the move to the currency's minor unit (D-72).
|
||||
- `form1099.rs` `vendor_part` and `split_share` — the 1099 worksheet's cents.
|
||||
- `periods.rs` `allocate` and `allocate_last_absorbs` — frozen legacy close
|
||||
replay rules, cents by their recorded policy.
|
||||
- `shares.rs` `split_first` — the legacy first-absorbs split, cents.
|
||||
- `tax_capital.rs` `RunningSplit::split` — the tax-basis K-1 worksheet's
|
||||
cents (after a `10`-place exact snap).
|
||||
|
||||
Display-only rounding in `ledger-gui`/`ledger-import` is K-U9's and was left
|
||||
untouched. The figures there that reach an exported report are
|
||||
`ledger-import/report.rs`'s `format_amount`/`amount_parts` (2 places,
|
||||
half away) and `xlsx.rs`'s `exact_f64` (2 places); both are presentation, not
|
||||
booked money.
|
||||
Reference in new issue
Block a user