test(core): round_step's directed modes and finer steps are proved and tested
Review found two holes in K-U2's evidence.
1. The spec block's invariants 3, 4 and 6 (toward zero: |r| <= |x| and
|r - x| < step; toward negative infinity: r <= x < r + step; no mode
flips the sign) had no proof: Kani checked those modes only at exact
ties. Three harnesses now assert them for every amount over the fixed
steps (round_step_toward_zero_bounds, round_step_toward_neg_inf_is_floor,
round_step_sign_preserved), each with covers for a non-tie and a negative
non-multiple; bookspec's Lean carries the same laws for any step. With
TowardZero mutated to round 7 at step 10 up to 10, the first harness
fails ("magnitude never grows"); reverted.
2. The bridge never ran checked_round_to_step's finer-step branch (step
with more places than the amount and a mantissa above one). Three
properties compare the kernel with a second, independent i128 reference
that takes any step (mantissa 1..1000 at scales 0..10, amounts at scales
0..28, small amounts, and constructed exact ties). Replacing
.checked_add(remainder / b) with .checked_add(0) in the digit loop fails
all three; reverted.
docs/proofs/k-u2.md also states the one exception to "no result changes":
a negative-zero input (round_dp keeps -0.00; round_places gives 0.00; no
call site can produce one).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f16525fd35
commit
f0a5d10708
3 files changed
+225
-6
No files matched your search
@@ -188,6 +188,72 @@ fn round_step_never_negative_zero() {
|
||||
);
|
||||
}
|
||||
|
||||
/// Toward zero never grows the magnitude and lands less than a step from
|
||||
/// the amount: `|r| <= |x|` and `|r - x| < step`, for every amount (ties
|
||||
/// are not special: both modes pick the same multiple whether or not the
|
||||
/// amount is exactly between two).
|
||||
#[kani::proof]
|
||||
#[kani::unwind(8)]
|
||||
fn round_step_toward_zero_bounds() {
|
||||
let value = any_value();
|
||||
for step in STEPS {
|
||||
let rounded =
|
||||
round_step(value, step, Rounding::TowardZero).expect("no overflow under these bounds");
|
||||
assert!(rounded.abs() <= value.abs(), "magnitude never grows");
|
||||
assert!((rounded - value).abs() < step, "less than a step away");
|
||||
}
|
||||
cover!(value % 10 != 0 && value > 0, "a positive non-multiple");
|
||||
cover!(value % 10 != 0 && value < 0, "a negative non-multiple");
|
||||
cover!(
|
||||
value.abs() % 10 != 0 && value.abs() % 10 != 5,
|
||||
"a non-tie, a non-multiple of ten"
|
||||
);
|
||||
}
|
||||
|
||||
/// Toward negative infinity is the floor: `r <= x < r + step`, for every
|
||||
/// amount of either sign.
|
||||
#[kani::proof]
|
||||
#[kani::unwind(8)]
|
||||
fn round_step_toward_neg_inf_is_floor() {
|
||||
let value = any_value();
|
||||
for step in STEPS {
|
||||
let rounded = round_step(value, step, Rounding::TowardNegativeInfinity)
|
||||
.expect("no overflow under these bounds");
|
||||
assert!(rounded <= value, "never above the amount");
|
||||
assert!(value < rounded + step, "less than a step below it");
|
||||
}
|
||||
cover!(value % 10 != 0 && value > 0, "a positive non-multiple");
|
||||
cover!(
|
||||
value % 10 != 0 && value < 0,
|
||||
"a negative non-multiple (the floor moves away from zero)"
|
||||
);
|
||||
cover!(
|
||||
value.abs() % 10 != 0 && value.abs() % 10 != 5,
|
||||
"a non-tie, a non-multiple of ten"
|
||||
);
|
||||
}
|
||||
|
||||
/// No mode flips the sign: a non-negative amount rounds to a non-negative
|
||||
/// one, and a non-positive amount to a non-positive one.
|
||||
#[kani::proof]
|
||||
#[kani::unwind(8)]
|
||||
fn round_step_sign_preserved() {
|
||||
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!(rounded >= 0, "a non-negative amount stays non-negative");
|
||||
}
|
||||
if value <= 0 {
|
||||
assert!(rounded <= 0, "a non-positive amount stays non-positive");
|
||||
}
|
||||
}
|
||||
cover!(value > 0, "a positive amount");
|
||||
cover!(value < 0, "a negative amount");
|
||||
cover!(value == 0, "zero");
|
||||
}
|
||||
|
||||
/// A step of zero or less is refused, never guessed at.
|
||||
#[kani::proof]
|
||||
fn round_step_refuses_nonpositive_step() {
|
||||
|
||||
@@ -63,6 +63,75 @@ fn reference(value: i128, scale: u32, places: u32, mode: Rounding) -> Option<Dec
|
||||
Decimal::try_from_i128_with_scale(if negative { -count } else { count }, places).ok()
|
||||
}
|
||||
|
||||
/// An independent integer reference for any step `mantissa x 10^-step_scale`
|
||||
/// (not only a power of ten): both operands go to their common scale as
|
||||
/// `i128`, the quotient and remainder come from `i128` division, and the
|
||||
/// result `count x step` is reduced by trailing zeros to the smallest scale
|
||||
/// that holds it. Shares no code with the kernel, and no digit loop.
|
||||
fn reference_step(
|
||||
value: i128,
|
||||
scale: u32,
|
||||
mantissa: i128,
|
||||
step_scale: u32,
|
||||
mode: Rounding,
|
||||
) -> Option<Decimal> {
|
||||
let common = scale.max(step_scale);
|
||||
let amount = value.checked_mul(10i128.checked_pow(common - scale)?)?;
|
||||
let step = mantissa.checked_mul(10i128.checked_pow(common - step_scale)?)?;
|
||||
let negative = amount < 0;
|
||||
let magnitude = amount.unsigned_abs();
|
||||
let step = u128::try_from(step).ok()?;
|
||||
let floor = magnitude / step;
|
||||
let remainder = magnitude % step;
|
||||
let twice = remainder.checked_mul(2)?;
|
||||
let round_up = match mode {
|
||||
Rounding::TowardZero => false,
|
||||
Rounding::TowardNegativeInfinity => negative && remainder != 0,
|
||||
Rounding::HalfAwayFromZero => twice >= step,
|
||||
Rounding::HalfToEven => twice > step || (twice == step && !floor.is_multiple_of(2)),
|
||||
};
|
||||
let count = if round_up {
|
||||
floor.checked_add(1)?
|
||||
} else {
|
||||
floor
|
||||
};
|
||||
let mut result = i128::try_from(count.checked_mul(step)?).ok()?;
|
||||
let mut places = common;
|
||||
while places > 0 && result % 10 == 0 && result != 0 {
|
||||
result /= 10;
|
||||
places -= 1;
|
||||
}
|
||||
if result == 0 {
|
||||
places = 0;
|
||||
}
|
||||
let result = if negative { -result } else { result };
|
||||
Decimal::try_from_i128_with_scale(result, places).ok()
|
||||
}
|
||||
|
||||
/// Rounds `value x 10^-scale` to the step `mantissa x 10^-step_scale` with
|
||||
/// the kernel and with the reference, and asserts they are the same value.
|
||||
fn assert_matches_reference(
|
||||
value: i128,
|
||||
scale: u32,
|
||||
mantissa: i128,
|
||||
step_scale: u32,
|
||||
mode: Rounding,
|
||||
) -> Result<(), TestCaseError> {
|
||||
let amount = Decimal::try_from_i128_with_scale(value, scale).expect("in Decimal's range");
|
||||
let step = Decimal::try_from_i128_with_scale(mantissa, step_scale).expect("in range");
|
||||
let want = reference_step(value, scale, mantissa, step_scale, mode);
|
||||
prop_assert!(want.is_some(), "the reference fits for these ranges");
|
||||
prop_assert_eq!(round_step(amount, step, mode), want);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The mantissas and scales of the finer-step tests: a step is any
|
||||
/// `mantissa x 10^-scale`; a tie needs an even mantissa, so a tie row
|
||||
/// builds its own.
|
||||
fn step_parts() -> impl Strategy<Value = (i128, u32)> {
|
||||
(1i128..=1000, 0u32..=10)
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#![proptest_config(ProptestConfig::with_cases(2000))]
|
||||
|
||||
@@ -99,6 +168,59 @@ proptest! {
|
||||
);
|
||||
}
|
||||
|
||||
/// Steps that are not powers of ten: a mantissa 1..1000 at scales
|
||||
/// 0..10 against amounts at scales 0..28, every mode, checked against
|
||||
/// the independent reference. When the step carries more places than
|
||||
/// the amount, the kernel's digit loop runs with a mantissa above one
|
||||
/// (the finer-step branch); otherwise the coarser-step branch does.
|
||||
#[test]
|
||||
fn decimal_matches_the_reference_for_any_step(
|
||||
value in -(1i128 << 60)..=(1i128 << 60),
|
||||
scale in 0u32..=28,
|
||||
(mantissa, step_scale) in step_parts(),
|
||||
mode in any_mode(),
|
||||
) {
|
||||
assert_matches_reference(value, scale, mantissa, step_scale, mode)?;
|
||||
}
|
||||
|
||||
/// Small amounts, so exact multiples are common: the same comparison.
|
||||
#[test]
|
||||
fn decimal_matches_the_reference_for_any_step_on_small_amounts(
|
||||
value in -5000i128..=5000,
|
||||
scale in 0u32..=12,
|
||||
(mantissa, step_scale) in step_parts(),
|
||||
mode in any_mode(),
|
||||
) {
|
||||
assert_matches_reference(value, scale, mantissa, step_scale, mode)?;
|
||||
}
|
||||
|
||||
/// Exact ties: the amount is an odd number of half-steps, written with
|
||||
/// up to three trailing zeros removed (so the step often has more
|
||||
/// places than the amount). Every mode's tie rule against the reference.
|
||||
#[test]
|
||||
fn decimal_matches_the_reference_at_ties(
|
||||
q in 0i128..=10_000,
|
||||
half in 1i128..=100,
|
||||
tens in 0u32..=3,
|
||||
step_scale in 0u32..=10,
|
||||
strip in 0u32..=3,
|
||||
negative in any::<bool>(),
|
||||
mode in any_mode(),
|
||||
) {
|
||||
let half = half * 10i128.pow(tens);
|
||||
let mantissa = 2 * half;
|
||||
let mut value = (2 * q + 1) * half;
|
||||
let mut scale = step_scale;
|
||||
for _ in 0..strip {
|
||||
if scale > 0 && value % 10 == 0 {
|
||||
value /= 10;
|
||||
scale -= 1;
|
||||
}
|
||||
}
|
||||
let value = if negative { -value } else { value };
|
||||
assert_matches_reference(value, scale, mantissa, step_scale, mode)?;
|
||||
}
|
||||
|
||||
/// Behaviour preservation: with a power-of-ten step, `round_step` is
|
||||
/// exactly `round_dp_with_strategy`, the call sites' old primitive.
|
||||
#[test]
|
||||
|
||||
+37
-6
@@ -49,14 +49,26 @@ bookspec's Lean proves them for any step.
|
||||
| `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_toward_zero_bounds` | toward zero: `\|r\| <= \|x\|` and `\|r - x\| < step`, every amount (ties are not special) | 16 s wall incl. build (3 covers) |
|
||||
| `round_step_toward_neg_inf_is_floor` | toward negative infinity: `r <= x < r + step`, both signs | 21 s wall incl. build (3 covers) |
|
||||
| `round_step_sign_preserved` | every mode: `0 <= x` gives `0 <= r`, `x <= 0` gives `r <= 0` | 13 s wall incl. build (3 covers) |
|
||||
| `round_step_refuses_nonpositive_step` | a step of zero or less is `None` | 8 s total (1 cover) |
|
||||
|
||||
The three directed-mode harnesses were added after review: the spec block's
|
||||
invariants 3, 4 and 6 had no proof of their own (Lean had the ties, Kani
|
||||
checked these two modes only at exact ties). Demonstrated: with
|
||||
`TowardZero` mutated to round up on `twice >= divisor` (7 at step 10 becomes
|
||||
10), `round_step_toward_zero_bounds` fails on "magnitude never grows"; the
|
||||
mutation was reverted.
|
||||
|
||||
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.
|
||||
`just prove` (fast set), before the review fixes: **59 harnesses, 0 failures**
|
||||
(53 before this unit, 6 added), 440 s of Kani, 470 s wall including the Lean
|
||||
build; all covers satisfied. The review fixes add 3 harnesses (62 in all);
|
||||
the nine `proofs::round::*` harnesses were each re-run alone and all verify
|
||||
with every cover satisfied.
|
||||
|
||||
## Bridge (`crates/ledger-core/tests/kernel_bridge/round.rs`)
|
||||
|
||||
@@ -65,6 +77,17 @@ after the last cover was added: 406 s wall); all covers satisfied.
|
||||
- `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.
|
||||
- `decimal_matches_the_reference_for_any_step`,
|
||||
`..._for_any_step_on_small_amounts`, `..._at_ties`: steps that are not
|
||||
powers of ten: a mantissa 1..1000 at scales 0..10 against amounts at
|
||||
scales 0..28 (small amounts for exact multiples; constructed exact ties
|
||||
with trailing zeros stripped), every mode, against a second independent
|
||||
`i128` reference that takes any step (common scale, `i128` division,
|
||||
trailing-zero reduction; no digit loop). When the step has more places
|
||||
than the amount this runs the kernel's finer-step digit loop with a
|
||||
mantissa above one, which the power-of-ten properties never reach.
|
||||
Demonstrated: replacing `.checked_add(remainder / b)` with
|
||||
`.checked_add(0)` in that loop fails all three; reverted.
|
||||
- `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
|
||||
@@ -88,6 +111,12 @@ from 39 to 40).
|
||||
- `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.towardZero_bounds` — toward zero: `|r| <= |x|` and `|r - x| < step`
|
||||
(the spec block's invariant 3).
|
||||
- `Round.towardNegInf_bounds` — toward negative infinity is the floor:
|
||||
`r <= x < r + step` (invariant 4).
|
||||
- `Round.sign_preserved` — every mode: `0 <= x` gives `0 <= r`, `x <= 0`
|
||||
gives `r <= 0` (invariant 6).
|
||||
- `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
|
||||
@@ -120,9 +149,11 @@ Counts by file (14 replaced):
|
||||
| `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.
|
||||
No result changes to the character, except a negative-zero input
|
||||
(`round_dp` keeps `-0.00`; `round_places` gives `0.00`; no call site can
|
||||
produce one): the wrapper reproduces `round_dp_with_strategy` exactly
|
||||
(value, scale and early return), the bridge checks that, and every existing
|
||||
test and golden passed unchanged.
|
||||
|
||||
### Currency-blind sites (kept at two places, on purpose)
|
||||
|
||||
|
||||
Reference in new issue
Block a user