StarIT Firm Initializing
StarITFirm
Engineering Systems Initializing
2026 SQUADSSpecialized Human Engineering Squads Are Now Active — Zero Vibe-Coding & Sovereign Data SanctityExplore Squads
StarIT Firm
Web3 & Security
September 28, 2026
15 min read

Formally Verified DeFi: Halmos & Foundry Proving Zero-CVE Solidity

Why 100% test coverage and fuzzing fail to catch modern flash-loan exploits, and how StarIT Firm uses Halmos symbolic execution over EVM bytecode to mathematically prove invariants in constant-product AMMs.

KS

Kazi Shakil

Head of Web3 & Cryptography, StarIT Firm

Share:𝕏in

The Illusion of 100% Branch Coverage

In Web3 protocols holding tens of millions of dollars in total value locked (TVL), conventional software testing methodologies are catastrophically inadequate. Protocols audited by top-tier security firms that achieved 100% branch coverage with Foundry unit tests and ran 1,000,000 fuzzing runs on Echidna have still suffered devastating flash loan and rounding exploits.

Why Fuzzing Fails in Non-Linear Search Spaces

Fuzzing generates pseudo-random input values hoping to trigger an assertion failure: - In an EVM state space with $2^{256}$ possible word values, the probability of a fuzzer randomly hitting a precise 1-wei rounding divergence combined with an exact flash loan liquidity ratio is $\approx 1 \times 10^{-77}$. - **Symbolic execution** flips this equation: instead of running concrete numbers, it treats inputs as symbolic variables $\alpha, \beta, \gamma$ and constructs an SMT (Satisfiability Modulo Theories) formula. It then asks the solver: *"Does there exist any assignment of inputs where invariant $I$ is violated?"*

Architectural Invariant

If the Z3 SMT solver answers **UNSAT**, it represents a mathematical theorem proving that no exploit can possibly exist within the bounded constraints of the system.

Formulating Bulletproof Mathematical Invariants

In decentralized finance protocols, invariants represent universal truths that must hold true across every block, every transaction, and every reentrant call.

For an automated market maker (AMM) operating on the constant-product formula $x \cdot y = k$: 1. **Solvency Invariant**: $\text{balanceOf}(TokenX) \ge \text{reserve}_x \land \text{balanceOf}(TokenY) \ge \text{reserve}_y$ 2. **K-Monotonicity Invariant**: For every non-zero swap input $\Delta x > 0$, the post-swap invariant $k_{post} \ge k_{pre}$ strictly holds after deducting protocol fees. 3. **No Zero-Cost Token Extraction**: No sequence of deposit, swap, and flash-borrow operations can yield a net positive balance without supplying collateral.

Solidity Contract & Halmos Test Harness Code

Halmos operates directly on Foundry test syntax, allowing engineers to write formal proofs in pure Solidity using `check_` prefixes rather than proprietary verification languages:

solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {Test} from "forge-std/Test.sol";
import {SymTest} from "halmos-cheatcodes/SymTest.sol";

contract StarVault {
    mapping(address => uint256) public balances;
    uint256 public totalDeposits;

    function deposit(uint256 amount) external {
        require(amount > 0, "ZERO_DEPOSIT");
        balances[msg.sender] += amount;
        totalDeposits += amount;
    }

    function withdraw(uint256 amount) external {
        require(balances[msg.sender] >= amount, "INSUFFICIENT_FUNDS");
        // State update prior to external transfer prevents reentrancy
        balances[msg.sender] -= amount;
        totalDeposits -= amount;
    }
}

contract StarVaultFormalVerification is Test, SymTest {
    StarVault vault;

    function setUp() public {
        vault = new StarVault();
    }

    /// @notice Proves that totalDeposits is ALWAYS equal to the sum of all individual balances
    function check_solvencyConservation(address user, uint256 depositAmount, uint256 withdrawAmount) public {
        // Constrain symbolic inputs within sensible EVM bounds
        vm.assume(depositAmount > 0 && depositAmount < type(uint128).max);
        vm.assume(withdrawAmount <= depositAmount);
        vm.assume(user != address(0));

        // Initial deposit
        vm.prank(user);
        vault.deposit(depositAmount);

        assert(vault.balances(user) == depositAmount);
        assert(vault.totalDeposits() == depositAmount);

        // Subsequent withdrawal
        vm.prank(user);
        vault.withdraw(withdrawAmount);

        // Invariant holds for ANY arbitrary symbolic value
        assert(vault.balances(user) == depositAmount - withdrawAmount);
        assert(vault.totalDeposits() == depositAmount - withdrawAmount);
    }
}

Toolchain Comparison: Halmos vs. Certora vs. Echidna

CapabilityHalmosCertora ProverEchidna / Foundry Fuzz
Verification EngineSymbolic Execution (Z3)SMT / Bytecode VerificationDynamic Fuzzer / Property Testing
Language RequiredPure Solidity (`SymTest`)Custom CVL (Certora Spec)Pure Solidity
Proof CompletenessMathematical Proof (UNSAT)Mathematical ProofProbabilistic Sampling
Execution Time1.2s – 18s per rule4 – 15 minutes30s – 10 minutes
Cost / LicenseOpen Source (Apache 2.0)Closed-source SaaSOpen Source
CI/CD IntegrationGitHub Actions 1-line commandComplex cloud coordinatorGitHub Actions

Integrating Formal Proofs into GitHub Actions

At StarIT Firm, pull requests to smart contract repositories are blocked by automated Halmos verification gates. If a developer introduces an unchecked math operation or reentrancy hole, Halmos generates the exact counterexample transaction payload in 2.4 seconds, preventing zero-day vulnerabilities from ever touching mainnet.

Topics Covered
#Web3 Security#Solidity#Foundry#Halmos#Formal Verification#SMT Solvers#DeFi Protocols
KS

Kazi Shakil

Head of Web3 & Cryptography, StarIT Firm

GitHub Profile

Security researcher and lead smart contract architect at StarIT Firm. Specializes in symbolic execution, EVM formal invariants, and zero-knowledge protocols.

Written and verified for StarIT Firm Engineering Publications.
StarIT Enterprise Engineering Pods

Launch Your Frontier Project with Zero Compromise

Whether deploying 20+ agent swarms, sub-50ms RAG pipelines, or formally verified Solidity smart contracts, StarIT Firm builds at 10x velocity.