In this track: Smart contract security foundations
advanced · 4 MIN READ
Arithmetic checks and unchecked blocks
Removing overflow checks requires a proved bound.
LET’S PICTURE IT
Skipping a safety stop is reasonable only when the route cannot reach the dangerous boundary.
SEE IT FOR YOURSELF
Watch a stack machine execute a tiny programStep 1 / 4
The program bytes are PUSH1 02, PUSH1 03, ADD, STOP. The program counter begins at byte zero.
The program bytes are PUSH1 02, PUSH1 03, ADD, STOP. The program counter begins at byte zero.PUSH1 reads its next byte as an immediate value and places two on the stack. The next instruction begins at byte two.The next PUSH1 places three above two. Stack order matters for operations that are not commutative.ADD consumes the top two values and pushes five. The next instruction is STOP at byte five; no storage write is implied by ADD.
Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.
Know the language behavior
Modern Solidity normally checks integer overflow and underflow. An unchecked block changes applicable arithmetic overflow behavior, not every possible runtime failure.
State the bound explicitly
A loop increment may be safe under a particular condition, while a balance subtraction may depend on earlier checks. Write the argument instead of assuming small usual inputs.
Keep division behavior separate
Unchecked arithmetic does not make division by zero valid. Precision loss and rounding also remain even when no overflow occurs.
Test boundary values
Exercise zero, maximum values and values immediately around the proved bound. A handful of normal examples cannot establish the full arithmetic property.
Reason about the scenario
before unchecked subtraction:
prove amount <= balance
then verify the proof still holds after all reachable interactions
Pseudocode or a simplified scenario for local reasoning. This is not a production contract or a completed security review.