EIP-7519 Atomic storage increment/decrement opcodes (Atomic Storage Operations SCREDIT and SDEBIT)
No call decision · EVM & execution · EIP status Stagnant · created 2023-09-16
Adds new opcodes SCREDIT and SDEBIT that increment and decrement smart contract storage values, providing atomic operations that revert on overflow or underflow.
Why it is needed
Research into parallel EVM (an execution model that processes multiple transactions concurrently) is underway on several chains, but the EVM lacks primitive operations to support models other than optimistic concurrency control (OCC). Values that are added to and subtracted from, like token balances or counters, are safe to execute in any order as long as the final result doesn't overflow or underflow, even without knowing the exact intermediate values. But today this requires reading with SLOAD, computing with ADD or SUB, and writing with SSTORE, three steps across which overflow and underflow checks must be written by hand, and because these three steps are not atomic, safety is hard to guarantee in a parallel execution environment that assumes transaction reordering. SCREDIT and SDEBIT combine increment/decrement and overflow/underflow checking into a single operation, providing a foundation for building more advanced parallel execution models at, for example, layer 2s.
What changes
Today
- It requires multiple steps: reading the slot value with SLOAD, adding or subtracting with ADD or SUB, checking for overflow or underflow by hand, and then storing with SSTORE.
- Because these multiple steps are not atomic, safety is hard to guarantee if transaction reordering occurs in a parallel execution environment.
- General concurrency control relies on optimistic concurrency control (OCC), which is not well suited to balance or counter updates.
After EIP-7519
- A single SCREDIT increments the slot value and automatically reverts on overflow.
- A single SDEBIT decrements the slot value and automatically reverts on underflow.
- The gas cost is the same as SSTORE, which keeps implementation and gas accounting simple.
- Increment and decrement operations are limited to storage slots, for use with balance- and counter-like data that persists across transactions.
Who is affected
- Smart contract developers: In contracts dealing with token balances or counters, a single SCREDIT or SDEBIT operation can handle overflow and underflow checking as well, instead of SLOAD-ADD/SUB-SSTORE.
- Layer 2 and parallel EVM researchers: They gain an atomic increment/decrement primitive for designing parallel execution models beyond optimistic concurrency control.
- Client and compiler teams: New opcodes must be added to the EVM specification and gas schedule, and compilers can optimize to make use of this operation.
- Security auditors: A new point at which reverts can occur is introduced, which must be considered together when reviewing for reentrancy.
In depth
Opcode composition
SCREDIT and SDEBIT are new opcodes assigned to addresses 0xTBD and 0xTBD+1 respectively; both take 2 stack inputs (slot, value) and produce no output. SCREDIT: slot, value adds value to the value stored at slot. SDEBIT: slot, value subtracts value from the value stored at slot.
Overflow and underflow handling
SCREDIT raises an exception and aborts the operation if the addition result is 2^256 or greater. SDEBIT likewise aborts if the subtraction result is less than 0. This check looks at whether the result falls outside the unsigned 256-bit integer range, and it happens before the value is actually written to storage.
Gas cost
The gas schedule for both opcodes is set equal to SSTORE, and EIP-2929's cold/warm access rules, including interaction with the warm storage slot list, apply in the same way. If the SSTORE gas calculation rules (EIP-2200) change in the future, the same changes will apply to SCREDIT and SDEBIT. The source cites implementation simplicity as the reason for matching costs this way, along with the fact that the additional cost of the addition/comparison logic itself (about 4 gas, or 0.2%, relative to SSTORE for ADD+LT) is negligible compared to the cost of database access.
Scope limited to storage slots
The purpose of this EIP is not general-purpose concurrency control but storage values used to track assets that persist across transactions, like token balances. It therefore does not apply to the transient storage defined by EIP-1153, and general-purpose concurrency tools like locks or semaphores are out of scope.
Why a system contract was not used instead of an opcode
Implementing SCREDIT and SDEBIT as a system contract at layer 2 was also considered. Since the cost of SSTORE is always greater than the cost of a call, a gas discount would have been possible, but this would not have solved the code size growth caused by the added call, so the opcode approach was chosen instead.
Difference from the existing SLOAD-ADD/SUB-SSTORE
These opcodes are not simply a combination of SLOAD, ADD/SUB, and SSTORE into one; they differ from the existing sequence in that overflow and underflow checking is built in. They do not remove any existing EVM functionality.
Security considerations and current status
The source states that since a revert on overflow or underflow becomes a new halting condition, auditors must consider this together when analyzing for reentrancy. This proposal's status is Stagnant. The opcode numbers (0xTBD) have not yet been assigned and the reference implementation also remains TBD, so the details and whether it will be adopted are not finalized.