Differential Testing Explained in Detail
Differential testing feeds the same inputs or action sequences to two systems and compares the results. The systems might be an old version and a new version, a simple reference and an optimized implementation, or two chain deployments.
It is especially useful when code is refactored, optimized, or upgraded.
Smart contract example
assertEq(newVault.previewDeposit(x), oldVault.previewDeposit(x));
If the outputs differ, the test should explain whether the difference is expected.
Differential Testing in Auditing
Many bugs appear when teams rewrite math, upgrade vaults, or port protocols across chains. Differential testing catches unintended behavior changes quickly.
Auditors use it to protect accounting, pricing, and permission behavior during changes.
Red flags in code
-
The reference implementation is assumed correct without review.
-
Rounding differences are ignored or overaccepted.
-
Tests compare only outputs, not state changes.
-
Stateful action sequences are missing.
-
Edge inputs are not fuzzed.
How to test or review it
-
Define which differences are allowed.
-
Compare outputs, events, balances, and storage changes.
-
Use fuzzing for input ranges and action sequences.
-
Include edge cases around zero, max values, and rounding boundaries.
-
Pair with invariant testing for stateful protocols.
Keep learning this topic
Fuzz Testing
Fuzz testing sends many generated inputs through smart contract code to find edge cases, unexpected reverts, broken assumptions, and state transitions that normal unit tests miss.
Invariant Testing
Invariant testing checks that important smart contract properties stay true across many generated call sequences, actors, and state transitions.
Formal Verification
Formal verification uses mathematical methods to prove that code satisfies specified properties under stated assumptions.
Practice this in real audit scenarios
Definitions help, but auditors need reps. SCH turns concepts like Differential Testing into exploit labs, code review habits, and report-writing practice.
Start the free trial or see the full smart contract auditing course.