EIP-5283 Semaphore for reentrancy prevention (Semaphore for Reentrancy Protection)
No call decision · Security & privacy · EIP status Stagnant · created 2022-07-17
A proposal to add a precompile (0x0A) that checks the call stack instead of storage to prevent contract reentrancy. Since it doesn't use storage, it doesn't conflict with future parallel execution designs.
Why it is needed
The classic way to prevent reentrancy attacks is a reentrancy guard, which writes to and reads from a single storage cell inside the contract to determine whether reentrancy is occurring. Because this approach reads and writes the same storage cell on every call, it conflicts with parallel execution designs that aim to run the Ethereum Virtual Machine (EVM) across multiple threads simultaneously. The most successful parallelization approach so far detects conflicts at the storage-cell level, and the reentrancy guard's read-write pattern is exactly what blocks that parallel execution. Because it's difficult to automatically analyze and parallelize already-deployed contracts, there is a need for a way for future contracts to prevent reentrancy without touching storage.
What changes
Today
- A reentrancy guard determines whether reentrancy is occurring by writing and reading 0 and 1 in a storage cell.
- Because of this read-write pattern, calls to that contract conflict with storage-cell-level parallel execution.
- After applying EIP-1283, the guard costs about 200 gas.
After EIP-5283
- Placing a Semaphore precompile at address 0x0A and checking only the call stack prevents reentrancy.
- Since it uses no storage at all, it doesn't conflict with storage-cell-level parallelization designs.
- The current call cost is about 800 gas, more expensive than before, but it could drop to about 140 gas if precompile call costs are lowered.
Who is affected
- Contract developer: When using a reentrancy guard, storage can be replaced with a precompile call, allowing code to be written in advance for future parallel execution environments.
- Client and core developers: The new precompile (0x0A) and call-stack scanning logic must be implemented via a hard fork.
- EVM parallelization researchers: Gains a way to bypass the reentrancy guard pattern that was blocking parallel execution designs based on storage-cell-level conflict detection.
In depth
Specification: 0x0A precompile
After the activation block (TBD), a Semaphore precompile is created at address 0x0A. When this contract is called, if the caller's address already appears one or more times on the call stack, it behaves as if the first instruction were REVERT, and the CALL returns 0. Otherwise it executes no code and returns 1. The execution gas cost is fixed at 100 gas regardless of the call's outcome.
Rationale for the address choice
0x0A was chosen because it is the next free address within the precompile address range defined by EIP-1352.
Sample usage code
In Solidity, this is used inside a nonReentrant modifier by calling staticcall(1000, SemaphoreAddress, 0, 0, 0, 0) via inline assembly and executing revert(0, 0) if it fails. In the test cases, calling second() directly in a transaction does not revert, but if first() internally calls this.second() (that is, the same contract address appears twice on the stack), it reverts.
Already deployed contracts cannot be parallelized
To parallelize contracts that use existing storage-based reentrancy guards, the VM would need to automatically detect which storage variables serve as reentrancy guards and prove that they behave correctly. This requires static code analysis and is hard to implement within consensus, since the computational cost of detection and proof can be high and some functions are not protected by a guard, which complicates the proof. So instead of parallelizing already-deployed contracts, this proposal aims to help future contracts be designed so they can be parallelized.
Alternatives considered
The original text reviews three alternatives together. First, transient storage instructions like TLOAD/TSTORE persist only within a transaction and are not written to world state, enabling fine-grained parallelization. Second, an SSTORE_COUNT instruction that returns the number of SSTORE executions also helps with parallelization, but since it returns the count of SSTORE executions rather than the reentrancy count, it is harder to use correctly. Third, a LOCKCALL instruction similar to STATICALL but which only blocks storage writes from the caller contract would be cheaper, but some functions cannot be left without a guard. All of these alternatives require new opcodes, which is a drawback since it requires modifying compilers, debuggers, and static analysis tools, so adding a new opcode is avoided when a precompile can achieve the same thing at the same cost.
Gas cost calculation
The 100 gas cost assumes a worst case where the call stack is nearly full (around 400 addresses). Since the stack always resides in RAM and scanning it is fast, the cost could be lower once implemented and benchmarked in geth. Since the precompile call itself currently costs 700 gas, using Semaphore as a reentrancy check costs 800 gas in total. This is more expensive than the roughly 200 gas cost of a storage-based guard after EIP-1283, which discourages its use, but if the precompile call cost is lowered, it could drop to around 140 gas, making it cheaper than a storage-based guard. This proposal therefore recommends being introduced together with a reduction in precompile call cost.
Requires a hard fork, status is Stagnant
This change requires a hard fork and all full nodes must be updated. The Security Considerations section only states "Needs discussion," so the security discussion has not yet concluded. The current status is Stagnant, meaning discussion has stalled and neither an activation block nor inclusion has been decided. This call's notes contain no related decision.