feat: Kani formal verification of collateral, payoff and fee arithmetic (#119) - #160
Merged
dami-005 merged 2 commits intoSep 30, 2026
Conversation
Collateral and payoff math decides how much users lock and receive, so a single overflow, sign error or rounding-direction mistake is a direct fund-safety bug that example tests can't rule out. Add integer-scaled prototypes of the fund-critical math and bounded-model-check them over their full documented domain: non-negativity, monotonicity in contracts and strike, short-put collateral covering the maximum possible loss, mirrored legs summing to exactly zero, protocol-favouring fee rounding, and no overflow inside the documented bounds. Normal builds stay fast because the harnesses are gated behind #[cfg(kani)]; Kani runs in CI on every PR and nightly, and docs/verification.md records the properties and the f64 gap.
|
@Whiznificent Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
…rification-issue-119 # Conflicts: # Cargo.toml # README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #119.
What
Formally verifies the fund-critical arithmetic with Kani bounded model checking — proofs over the whole bounded input domain, not sampled examples:
src/math_fixed.rs— integer-scaled prototypes ofcollateral::collateral_required, thepayoff.rsintrinsic/P&L arithmetic, and protocol-fee rounding, with rounding always up (protocol-favouring).src/kani_proofs.rs— 8 proof harnesses, gated behind#[cfg(kani)]so normalcargo test/clippybuilds never pay for them:.github/workflows/kani.yml— proofs run in CI viakani-github-actionon every PR, push to main, and nightly (45-min budget, superseded runs auto-cancelled).docs/verification.md— the verification report: property table, reproduction, expected output, counterexample policy, and the precisef64gap.Proof output
All 8 harnesses verified in CI:
Full run: 1m21s including Kani setup.
The
f64gap (as scoped by the issue)Verifying
f64Black-Scholes with BMC is out of scope (not tractable), so the proofs target integer-scaled prototypes of the same formulas.docs/verification.mddocuments the mapping precisely, including thatfee_fixedpins the rounding direction before any fee-charging code exists. When the fixed-point migration lands, these same properties port to the production width unchanged.Notes on tractability
The word width is deliberately narrow (
i16,MAX_INPUT = 50): Kani's SAT encoding of symbolic multiply/divide grows superlinearly with bit width, and 32-bit versions of these exact harnesses were run and did not finish inside a 45-minute budget (5/8 verified before cancellation). At 16 bits all 8 harnesses verify fully symbolically in about a minute. The proven properties are algebraic facts about the formulas and do not depend on magnitude — this trade-off, and the porting path to the production width, are recorded indocs/verification.md.Test evidence
fmt,clippy -D warnings,test, andcargo kani(8/8 verified) all pass on the branch.