flowfi icon indicating copy to clipboard operation
flowfi copied to clipboard

[Security] Formal Specification & Mathematical Invariant Verification using Kani & Verus

Open blurbeast opened this issue 2 months ago • 9 comments

Overview & Background

While unit tests and property-based fuzzing test thousands of pseudo-random inputs, they cannot guarantee mathematical certainty across infinite state spaces. For payment streaming protocols securing millions in token deposits, formal verification verifies mathematical proofs that core safety invariants can never be violated under any conceivable execution path, timing combination, or arithmetic overflow.


Detailed Problem Statement

  1. Critical Protocol Invariant Risks:
    • Solvency Invariant: The contract's vault token balance must strictly equal or exceed the sum of all active, unwithdrawn stream balances.
    • Conservation of Funds: For any stream, withdrawn_amount + remaining_deposit must always exactly equal initial_deposited_amount + total_top_ups.
    • No Over-Withdrawal: Under no circumstances can withdrawn_amount exceed deposited_amount.
  2. Subtle Arithmetic Edge Cases:
    • Timestamp jumps, rounding division truncations, or boundary cliff calculations could hide micro-leaks (dust loss) that fuzzing might miss.
  3. Audit Verification Assurance:
    • Institutional partners require formal mathematical proofs verifying that contract logic conforms strictly to security specifications.

Technical Specification & Architecture

1. Verification Tooling Integration (Kani Rust Verifier & Verus)

Integrate kani model checker for Soroban contract code in contracts/stream_contract/src/verification/:

  • Proof Harness 1: Solvency Conservation Proof:
    #[kani::proof]
    fn verify_stream_conservation_invariant() {
        let stream = kani::any::<Stream>();
        let current_time = kani::any::<u64>();
        kani::assume(stream.start_time <= stream.end_time);
        kani::assume(stream.rate_per_second >= 0);
        kani::assume(stream.withdrawn_amount <= stream.deposited_amount);
    
        let claimable = calculate_claimable_internal(&stream, current_time);
        kani::assert(claimable >= 0, "Claimable amount can never be negative");
        kani::assert(
            claimable + stream.withdrawn_amount <= stream.deposited_amount,
            "Claimable plus withdrawn cannot exceed deposited amount"
        );
    }
    
  • Proof Harness 2: Arithmetic Non-Overflow Proof:
    • Prove that rate multiplication (elapsed * rate_per_second) never silently overflows i128 within supported timestamps ($0 \le T \le 2^{63}-1$).
  • Proof Harness 3: Pause Invariant Proof:
    • Prove that when is_paused == true, no state mutating operation (create_stream, withdraw, top_up, cancel_stream) can modify stream balances.

2. CI Verification Workflow (.github/workflows/formal-verification.yml)

  • Run Kani verifier on every pull request touching contracts/.

Target Files

  • contracts/stream_contract/src/verification/mod.rs
  • contracts/stream_contract/src/verification/solvency_proofs.rs
  • contracts/stream_contract/src/verification/arithmetic_proofs.rs
  • contracts/stream_contract/Cargo.toml
  • .github/workflows/formal-verification.yml

Acceptance Criteria

  • [ ] Kani verification harness integrated into contracts/stream_contract.
  • [ ] Solvency and non-overflow proofs pass formal model checking without counterexamples.
  • [ ] Emergency pause invariant proven across all mutating entrypoints.
  • [ ] CI pipeline executes formal proofs on contract pull requests.
  • [ ] Detailed formal specification report added to docs/security/FORMAL_VERIFICATION.md.

blurbeast avatar Sep 27 '26 05:09 blurbeast

@code-ony has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

I would love to work on this issue, please assign it to me

ℹ️ Repo Maintainers: To accept this application, review their application or assign @code-ony to this issue.

code-ony avatar Sep 27 '26 15:09 code-ony

@samjay8 has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

I go work on this issue.

ℹ️ Repo Maintainers: To accept this application, review their application or assign @samjay8 to this issue.

samjay8 avatar Sep 27 '26 17:09 samjay8

Congratulations, @code-ony! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on September 30, 2026.

🧑‍💻 @code-ony: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

[!WARNING] When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

🌊 Happy Wave 🌊

drips-wave[bot] avatar Sep 29 '26 17:09 drips-wave[bot]

@codedbytejas has applied to work on this issue as part of the Stellar Wave Program's 10th wave.

Hey! I’m interested in tackling #1479 and contributing to FlowFi’s security and verification efforts. I’d like to help implement formal proof harnesses for fund conservation, arithmetic overflow, and pause invariants, along with CI integration and documentation. Could you please assign this issue to me? 🙌

ℹ️ Repo Maintainers: To accept this application, review their application or assign @codedbytejas to this issue.

codedbytejas avatar Oct 15 '26 13:10 codedbytejas

@Max347bot has applied to work on this issue as part of the Stellar Wave Program's 10th wave.

Hello maintainer please I will like to work on this issue

ℹ️ Repo Maintainers: To accept this application, review their application or assign @Max347bot to this issue.

Max347bot avatar Oct 15 '26 23:10 Max347bot

@annacodervibe has applied to work on this issue as part of the Stellar Wave Program's 10th wave.

I would like to work on this issue. I have experience with Rust and Soroban smart contract development. I will follow the acceptance criteria and add tests. Excited to contribute. Kindly assign.

ℹ️ Repo Maintainers: To accept this application, review their application or assign @annacodervibe to this issue.

annacodervibe avatar Oct 16 '26 12:10 annacodervibe

@Hotmopo has applied to work on this issue as part of the Stellar Wave Program's 10th wave.

Hello maintainer, i would like to work on this issue

ℹ️ Repo Maintainers: To accept this application, review their application or assign @Hotmopo to this issue.

Hotmopo avatar Oct 16 '26 22:10 Hotmopo

Congratulations, @Hotmopo! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on October 22, 2026.

🧑‍💻 @Hotmopo: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

[!WARNING] When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

🌊 Happy Wave 🌊

drips-wave[bot] avatar Oct 16 '26 22:10 drips-wave[bot]

This issue has been completed by @Hotmopo as part of the Stellar Wave Program's 10th Wave 🥳

😎 @Hotmopo: You earned 200 Points for completing this issue! Once the Wave's rewards are finalized, you'll be eligible for a percentage of the Wave's reward pool based on the percentage of total points you've earned. Learn more here. You can also Leave a review to share your experience working on this issue.

🧑‍💻 Repo maintainers: How'd the contributor do? Leave a review to share your experience working with them.

drips-wave[bot] avatar Oct 18 '26 12:10 drips-wave[bot]