Skip to content

Challenge 28: Verify flt2dec with production bodies (Kani) - #819

Open
fromtaoyuanhsinchuuuu wants to merge 5 commits into
model-checking:mainfrom
fromtaoyuanhsinchuuuu:ch28/flt2dec-v2
Open

fromtaoyuanhsinchuuuu wants to merge 5 commits into
model-checking:mainfrom
fromtaoyuanhsinchuuuu:ch28/flt2dec-v2

Conversation

@fromtaoyuanhsinchuuuu

@fromtaoyuanhsinchuuuu fromtaoyuanhsinchuuuu commented Dec 3, 2026 •

Copy link
Copy Markdown

Challenge 28: Verify flt2dec with production bodies

Resolves #524.

This is a rework of the earlier #818. It now verifies the production
function bodies
for all twelve Challenge 28 targets, without the
#[cfg(kani)] body swaps or assume(q < 10)-at-the-digit-write pattern that
the review of #606 correctly rejected.

What is proved

For the documented preconditions of each function (the assert!s already in
the bodies):

  • every MaybeUninit::assume_init* call sees a fully written prefix;
  • every buffer write is in bounds;
  • the format_shortest / format_exact lifetime laundering does not cause UB;
  • no dangling/misaligned access, invalid value, immutable-byte mutation or
    problematic intrinsic use is reachable.

Ghost witness

flt2dec::ghost picks one symbolic byte index and asserts with
core::hint::assert_unchecked that the byte is an ASCII digit. Because the
index is symbolic, one assertion covers every byte. Harness buffers start as
zeroes (not a digit), so an unwritten byte cannot satisfy the obligation.
assert_unchecked is a non-panic UB check, so a panic cannot mask a violation.

Grisu: no body swaps

  • The real integral/fractional loops run unchanged, apart from an explicit
    MAX_SIG_DIGITS guard (implied by the Loitsch digit-count theorem) and an
    explicit nine-step bound in the weeding loop. round_and_weed is hoisted so
    the wrapper harness can name it; its body is unchanged.
  • Arithmetic helpers are replaced by documented, lemma-checked models, not alternate
    function bodies:
    • Fp::mul returns a.f / 2, the lower endpoint of the real product
      interval [a.f / 2, a.f) for a normalized cached power; it is monotone in
      a.f, preserving minus < v < plus.
    • cached_power returns an arbitrary table-shaped entry with
      e in [alpha, gamma], i.e. exactly the table postcondition.
    • round_and_weed / round_up only read or rewrite bytes that are already
      initialized; their models never introduce new uninitialized memory.
    • max_pow10_no_more_than is split into its ten possible return values. The
      decade assumption is its complete specification, so ten harnesses cover
      every caller-visible result while keeping the real division concrete.
  • Inputs are the real decode(f32) / decode(f64) domains, not a 4-bit or
    8-bit narrowed mantissa.
  • kani::cover anchors require both the Some and None outcomes.

Dragon

The real format_shortest / format_exact bodies run with bignum arithmetic
replaced by havoc models that keep the representation size valid; comparisons
and is_zero are nondeterministic. format_shortest has an explicit
MAX_SIG_DIGITS loop bound and a guarded final carry write. The harnesses are
#[kani::should_panic] because the havoc models can violate bignum-specific
debug_assert!s; those paths panic, which is safe, while the initialization
obligation is an assert_unchecked check that should_panic cannot mask.
kani::cover anchors keep the digit-producing paths non-vacuous.

Known limitation: the exact-mode Dragon harness constrains len <= 8 for
solver tractability; the loop structure and all write sites are identical for
longer buffers.

flt2dec::mod

The formatting entry points run their real bodies with a callback that
over-approximates format_* by returning an arbitrary non-empty digit slice.
flt2dec::ghost allocates uninitialized buffers with malloc (CBMC
--object-bits 12), so the witness checks a genuinely unwritten buffer rather
than a pre-initialized one.

Local verification

scripts/run-kani.sh --kani-args --harness 'flt2dec::' -j 4
  • 44/44 harnesses verified, 0 failures (about 7 minutes on 20 cores).
  • cargo check -p core: passes.
  • rustfmt --check with the upstream style_edition = "2024" config: clean.

The flt2dec::mod and Dragon parts build on the earlier Challenge 28 work in
this branch; the Grisu approach is informed by the review feedback on #596
and #606 but keeps the production bodies and uses the models documented above.

Resolves #524.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

Model lemmas

check_fp_mul_interval, check_cached_power_contract and
check_max_pow10_contract verify the three numeric models against the real
Fp::mul, cached_power and max_pow10_no_more_than bodies.

fromtaoyuanhsinchuuuu and others added 4 commits December 4, 2026 04:59
Prove the Grisu digit-generation loops with Kani without swapping in
alternate #[cfg(kani)] function bodies:

* The real integral and fractional loops run unchanged apart from an
  explicit MAX_SIG_DIGITS guard, which is implied by the Loitsch bound
  and makes the write bound explicit.
* A symbolic witness index tracks that every byte handed to
  assume_init* has been written; harness buffers are zeroed, so an
  unwritten byte fails the ASCII-digit obligation.
* Heavy arithmetic helpers are replaced by documented models:
  Fp::mul returns the lower endpoint of the real product interval
  (monotone, preserving minus < v < plus), cached_power keeps its table
  postconditions, round_and_weed/round_up only touch initialized digits.
* max_pow10_no_more_than is case-split into the ten decades, which is its
  complete specification; each harness therefore sees a concrete
  ten_kappa and the real division is tractable.

22 harnesses cover the four Grisu functions, including the two Dragon
fallbacks through their wrappers, and pass in about 8 seconds each.

Co-authored-by: v3risec <phalange123@126.com>
Remove the duplicated ghost helper in favor of the shared flt2dec::ghost
module, give the Dragon digit loop an explicit MAX_SIG_DIGITS bound and
carry guard, and document the exact property, ghost witness, arithmetic
models, production bounds, non-vacuity anchors and limitations in the
verify modules.
@fromtaoyuanhsinchuuuu
fromtaoyuanhsinchuuuu requested a review from a team as a code owner December 3, 2026 23:06
Add lemma harnesses for the three numeric models used by the strategy
proofs: the Fp::mul interval, the cached_power table postcondition, and
the complete max_pow10_no_more_than specification.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Challenge 28: Safety of float to decimal conversion module

1 participant