What is fuzzing in smart contracts?

Fuzzing is testing with generated inputs: instead of checking a function with a few values you picked, a fuzzer calls it with many values of its own choosing and reports any input that makes a stated property fail. In Solidity projects it usually means property tests that take parameters, which the test framework fills in on every run.

Last updated . 2 minute read. By the QuillAudits Team.

How does a fuzz test work in Foundry?

Forge fuzzes any test function that takes parameters and runs it 256 times by default with different inputs (Foundry), and the runs setting under [fuzz] changes the count (Foundry config).

A fee function and a fuzz test of its properties (illustrative, not audited code) FeeMathFuzz.sol

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

contract FeeMath {
    // 0.3% fee, rounded down.
    function split(uint256 amount) public pure returns (uint256 net, uint256 fee) {
        fee = (amount * 3) / 1000;
        net = amount - fee;
    }
}

contract FeeMathFuzzTest {
    FeeMath internal math = new FeeMath();

    // Forge calls this with a new `amount` on every run.
    function testFuzz_splitAddsUp(uint256 amount) public view {
        amount = amount % 1e30; // keep amount * 3 from overflowing
        (uint256 net, uint256 fee) = math.split(amount);
        assert(net + fee == amount);
        assert(fee <= amount / 100);
    }
}

The inputs aren't purely random. By default 40% of them come from a dictionary of values seen in the project, including storage values of the contracts a run touches (Foundry config), and a failing run is reported with its seed so it can be replayed (Foundry).

What does fuzzing find, and what does it miss?

It finds the edge values people skip: zero, one wei, the largest amounts and the inputs where rounding or casting goes wrong. It struggles with bugs that need a particular series of calls, and with conditions so narrow that random inputs rarely meet them.

Technique Inputs Finds
Fuzzing Generated values for one call Edge values that break a property of one function
Invariant testing Generated sequences of calls States that break a system-wide property
Symbolic execution Solved from the branch conditions Narrow inputs that random values rarely hit

Trail of Bits names Medusa, Echidna and Foundry as fuzzers for testing invariants (Trail of Bits).

FAQ

How many fuzz runs are enough?

There is no fixed number. Start from the Foundry default of 256 (Foundry), raise it in CI for code that handles funds, and bound inputs to realistic ranges so the runs spend their time where bugs live.

Sources

QuillShield is building the security layer between AI and money, so you can keep shipping between audits.

Sign-up is free, and new accounts get $50 in credit, good for up to half of an order.

  1. 1Sign up and import a repo
  2. 2See the exact price before anything is charged
  3. 3Approve it, and the audit starts