feat: EIP-8024 — DUPN, SWAPN, EXCHANGE opcodes (Amsterdam)#11
Merged
garyschulte merged 4 commits intoConsensys:mainfrom Apr 1, 2026
Merged
feat: EIP-8024 — DUPN, SWAPN, EXCHANGE opcodes (Amsterdam)#11garyschulte merged 4 commits intoConsensys:mainfrom
garyschulte merged 4 commits intoConsensys:mainfrom
Conversation
Adds three generalized stack manipulation opcodes, each with a 1-byte immediate operand. Gas: G_VERYLOW (3) for all three. - DUPN (0xE6): duplicates stack item at depth (imm+145)%256 - SWAPN (0xE7): swaps top with item at depth (imm+145)%256 - EXCHANGE (0xE8): swaps two non-top items decoded via decode_pair; immediates 82..=127 are invalid (exceptional halt) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
garyschulte
reviewed
Mar 20, 2026
Collaborator
garyschulte
left a comment
There was a problem hiding this comment.
LGTM, but cursor bot feedback needs to be fixed
Signed-off-by: garyschulte <garyschulte@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Implements EIP-8024 for the Amsterdam hardfork.
Adds three generalized stack manipulation opcodes that take a 1-byte immediate operand, lifting the DUP/SWAP limit beyond 16 items:
DUPN0xE6 nn+1th stack item (1-indexed)SWAPN0xE7 nn+2th itemEXCHANGE0xE8 nimm[7:4]+1th andimm[3:0]+2th itemsG_VERYLOW(3) eachChanges
src/bytecode/main.zig— opcode constants + info entriessrc/interpreter/opcodes/stack.zig—opDupN,opSwapN,opExchangeimplementationssrc/interpreter/opcodes/main.zig— re-exportssrc/interpreter/protocol_schedule.zig—applyAmsterdamChangesactivating the 3 opcodesPart of Amsterdam hardfork
Related PRs: EIP-7843 (SLOTNUM), EIP-7954 (max code size), EIP-7708 (transfer logs), EIP-8037 (state gas reservoir)
Note
Medium Risk
Touches core interpreter execution (new opcodes + dispatch) and modifies gas/memory arithmetic; incorrect edge-case handling could change consensus-critical behavior or gas accounting.
Overview
Implements Amsterdam’s EIP-8024 by adding new opcodes
DUPN(0xE6),SWAPN(0xE7), andEXCHANGE(0xE8), wiring them into opcode metadata and the Amsterdam dispatch table atG_VERYLOW.Adds opcode handlers in
opcodes/stack.zigthat decode 1-byte immediates, enforce EIP-specified invalid immediate ranges (halting withinvalid_opcode), and perform the corresponding stack operations; includes extensive new unit tests covering valid cases and failure modes.Separately hardens memory expansion and
KECCAK256word-cost calculations by switching to overflow-safestd.math.divCeil/mul/addand early bounds checks, returning max costs or halting on overflow instead of wrapping.Written by Cursor Bugbot for commit 528163e. This will update automatically on new commits. Configure here.