What Is a Smart Contract Invariant? Definition and Examples

Learn how smart contract invariants work, why they matter, and how to test and formally verify them.

Author:

Certora

A smart contract invariant is a property that holds in every state the contract can reach, such as the sum of all account balances equaling the total supply in a token contract. Invariant testing checks this property after each call in a generated sequence, establishing that it holds in the states reached during that run, while formal verification can prove that it holds in every reachable state under the model’s assumptions.

What is an invariant in a smart contract?

Consider a ledger in double-entry accounting. Every transaction changes several accounts, and the balances shift with each one, but after every completed entry total debits still equal total credits. If a bookkeeper ever finds that they don’t, they know an error has crept in, even without knowing which entry caused it.

A smart contract invariant is the same kind of property for a contract. The Certora User’s Guide defines it as "a property of the contract's storage state that should hold between calls to the contract."

Take a voting contract with three counters: votes in favor, votes against and total votes. Every vote the contract records raises the total and one of the two categories, so the numbers keep moving, yet votes in favor plus votes against always equal total votes. That equation is the invariant. In CVL, Certora's specification language, the tutorial writes it as:

invariant sumResultsEqualsTotalVotes() votesInFavor() + votesAgainst() == to_mathint(totalVotes());

Token contracts follow the same pattern. In many ERC-20 contracts the balance of address(0) is always zero, and the sum of all balances equals the total supply.

Invariants are safety properties, which ethereum.org describes as statements that "nothing bad ever happens." 

Formal verification checks the invariant for every reachable state but it does not enumerate them all, so instead it checks two things: it holds for the contract’s initial state, and each relevant operation preserves it. An invariant that passes both checks is called an inductive invariant. You can read more about this is in Certora’s CVL documentation.

Why do invariants matter?

When a team writes its requirements down as invariants, security reviews become more systematic, because the reviewer works from a written list and does not depend on what happens to come to mind. Each invariant is a plain sentence, such as “the vault holds assets that cover its depositors’ claims,” which engineers can check and executives can read. Tests on their own leave a gap here: ethereum.org points out that testing “cannot prove correct execution for input values that are not part of the sample,” while formal verification “can formally prove that a smart contract satisfies requirements for an infinite range of executions.”

Examples of smart contract invariants

Common examples include conservation, solvency, bounds, and consistency between related state variables.

TypeWhat it saysExample
ConservationWhat exists adds upThe sum of all account balances equals total supply in a standard token
SolvencyAssets cover claimsA simple custody vault holds assets at least equal to its depositors' outstanding claims
BoundsA value stays in rangeNo holder's balance exceeds the total supply (Trail of Bits' Echidna demo)
State consistencyRelated variables stay in syncVotes in favor plus votes against equal total votes

What does ‘invariant’ mean in Uniswap v2?

Each Uniswap v2 pair holds reserves x and y of two tokens. A swap must leave the pair satisfying the fee-adjusted constraint in section 3.2.1 of the v2 whitepaper:

(x1 − 0.003 · xin) · (y1 − 0.003 · yin) ≥ x0 · y0

Here x0 and y0 are the reserves before the swap, x1 and y1 the reserves after, and xin and yin the amounts paid in, with the 0.003 term applying the 0.30% fee. Because fees stay in the pool, the plain product of the reserves grows over a series of swaps, which is how liquidity providers are paid, and the pair checks this invariant “accounting for fees” after every trade.

This formula compares the pair’s reserves before and after a swap. It is a transition property: it constrains what a swap can change. The distinction matters because adding or removing liquidity also changes the reserves, and those operations follow different rules.”

How is an invariant checked?

MethodWhat it checksWhat a passing test or successful proof establishes
Unit testScenarios the developer wroteThe invariant held in those scenarios
Invariant testingRandom call sequences, with the invariant checked after each callThe invariant held in the sequences generated
Formal verificationEvery reachable state, within the modelThe invariant holds within the stated model and assumptions

Foundry's fuzzer runs random sequences of function calls and checks invariants after each call. Trail of Bits shows Echidna finding a call sequence, transfer(0x0,1002), that lets a demo token's holder exceed the total supply because the demo omits overflow checks.

A proof works in two steps. The Prover checks that the invariant holds after the constructor runs. It then assumes the invariant holds before a method runs and checks that it holds after. The CVL documentation calls these the initial state check and the preservation check.

A verification run can end in three ways: a proof, a counterexample, or no verdict. A counterexample shows a modeled execution that violates the property under the supplied assumptions. It may reveal an implementation bug, or show that the specification, the model or the set of supporting invariants needs work. It does not by itself demonstrate an exploitable attack.

How do you write a good invariant?

Start from intended behavior. Certora's introduction to formal verification warns that "merely describing what the existing code does could preserve its mistakes." Write the invariant from what the contract promises its users, then check whether the code agrees.

Begin simple. Foundry's guide advises beginning with basic invariants and adding complexity. Proven invariants can then serve as assumptions in later proofs through requireInvariant, so small statements become building blocks for larger ones.

Add assumptions with care. A proof sometimes needs an extra condition on the starting state, and CVL provides preserved blocks to state it. The documentation warns that extra require statements are unsound if they exclude states the contract can actually reach.

Carefully think about the property. The prover can reject a true invariant. It checks one step: assume the invariant, run any single call, check the invariant again. If the invariant survives, it is inductive. If not, the prover shows you a state where the invariant held and one call broke it. That state may be unreachable. In this case, add the reason to the invariant and check again. If it is reachable then it is a bug.

There is another consideration. Normally a “step” is a whole call, so the invariant only has to hold between calls. But if a contract calls another, that one can call back before the first call returns and see the contract half-updated. For such contracts the invariant should also hold at every external call. CVL spells the two choices weak invariant and strong invariant.

What can a proven invariant miss?

A proof is only as complete as the specification behind it. Ethereum.org notes that verification “can only check if the execution of the smart contract matches the formal specification,” so if an invariant leaves a requirement out, the proof does not cover it.

Assumptions bound the guarantee. Certora's introduction to formal verification states that a proof assuming accurate prices does not establish safety when those prices are false.

An unsound setup undermines a proof. The CVL documentation warns that filters, which skip checks for chosen methods, and extra require statements can make a proof unsound. It also shows that an invariant expression that can revert needs care: get(i) == 0 passes even though a call breaks it, because get(i) reverts in the state before the call. Writing invariants over view functions is advised, and a view function can still revert.

Some attempts remain inconclusive. Ethereum.org lists state and path explosion among the performance limits of verification, so a run can end without a verdict, and when that happens the cause is a limit of the analysis, and the result gives no indication whether the statement is true.

Case study: Uniswap v4 solvency

In March 2025, Certora published an explanation of its solvency proof for Uniswap v4. The central invariant is that the contract always holds enough funds to pay all liquidity providers, with accounting that covers pool reserves, uncollected liquidity-provider fees, protocol fees, ERC-6909 reserves, and temporary currency deltas and debt.

Proving that requirement also required supporting invariants. Because swaps depend on the pool’s recorded liquidity, the proof needed to establish that this value equals the sum of liquidity in active positions, supported by another invariant governing liquidity changes at price ticks. Together, these invariants had to hold after the constructor and remain preserved by every public function.

The proof also depends on assumptions about token behavior. The post identifies rebasing tokens such as stETH as unsupported by Uniswap v4 and describes Synthetix as technically supported but outside the proof’s coverage. It also explains that token transfer fees can reduce what a user receives, illustrating a loss the solvency proof does not prevent.

From invariants to a running verification process

Choosing the right invariants is a central part of formal verification. AutoProver is built to help with that work. It starts by reading the code and documentation and proposing candidate properties that describe the intended behavior. The user can then  review those against what the contract is meant to do, and AutoProver then generates Foundry tests and Certora Prover rules, runs them, and triages any failures. The resulting verification assets can become a pull request and live beside your code as it changes.

A successful proof establishes that it holds within the verification model and assumptions; human review determines whether it captures the intended requirement. AutoProver Beta is available now for Solidity.

Start with AutoProver to turn your contract's requirements into checked properties. For help choosing and proving the invariants that matter most for your protocol, talk to Certora's verification team. Follow Certora on X and LinkedIn for new research.

Frequently asked questions

What is an invariant in a smart contract?

A smart contract invariant is a property that holds in every state the contract can reach between calls, such as the sum of all account balances equaling the total supply in a token contract.

What is the difference between a property and an invariant?

A property is a statement about how a contract should behave, while a state invariant is a particular kind of property that holds in every reachable state between calls. Other properties can describe who may call a function, how an operation changes the contract’s state, or whether a user can eventually withdraw funds.

What is the difference between invariant testing and formal verification?

Invariant testing checks a property after each call in generated sequences, so a passing run establishes that it held in the states those calls reached. Formal verification can prove that the property holds in every reachable state under the model’s assumptions, although a verification run can also produce a counterexample or remain inconclusive.

Can a proven invariant still leave bugs uncovered?

Yes. A proof covers the requirements expressed in the specification under the model’s assumptions, so omitted requirements or inaccurate assumptions can leave bugs uncovered. An unsound verification setup, such as an assumption that excludes reachable states or a filter that skips a relevant method, can also make the proof unreliable.

Get every blog post delivered

Certora Logo
logologo
Terms of UsePrivacy Policy