Skip to content

feat: EIP-8024 — DUPN, SWAPN, EXCHANGE opcodes (Amsterdam)#11

Merged
garyschulte merged 4 commits intoConsensys:mainfrom
Gabriel-Trintinalia:eip-8024
Apr 1, 2026
Merged

feat: EIP-8024 — DUPN, SWAPN, EXCHANGE opcodes (Amsterdam)#11
garyschulte merged 4 commits intoConsensys:mainfrom
Gabriel-Trintinalia:eip-8024

Conversation

@Gabriel-Trintinalia
Copy link
Copy Markdown

@Gabriel-Trintinalia Gabriel-Trintinalia commented Mar 20, 2026

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:

Opcode Encoding Description
DUPN 0xE6 n Duplicate the n+1th stack item (1-indexed)
SWAPN 0xE7 n Swap top of stack with the n+2th item
EXCHANGE 0xE8 n Swap imm[7:4]+1th and imm[3:0]+2th items
  • Gas cost: G_VERYLOW (3) each

Changes

  • src/bytecode/main.zig — opcode constants + info entries
  • src/interpreter/opcodes/stack.zigopDupN, opSwapN, opExchange implementations
  • src/interpreter/opcodes/main.zig — re-exports
  • src/interpreter/protocol_schedule.zigapplyAmsterdamChanges activating the 3 opcodes

Part 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), and EXCHANGE (0xE8), wiring them into opcode metadata and the Amsterdam dispatch table at G_VERYLOW.

Adds opcode handlers in opcodes/stack.zig that decode 1-byte immediates, enforce EIP-specified invalid immediate ranges (halting with invalid_opcode), and perform the corresponding stack operations; includes extensive new unit tests covering valid cases and failure modes.

Separately hardens memory expansion and KECCAK256 word-cost calculations by switching to overflow-safe std.math.divCeil/mul/add and 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.

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>
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Copy link
Copy Markdown
Collaborator

@garyschulte garyschulte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but cursor bot feedback needs to be fixed

Signed-off-by: garyschulte <garyschulte@gmail.com>
Signed-off-by: garyschulte <garyschulte@gmail.com>
Copy link
Copy Markdown
Collaborator

@garyschulte garyschulte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢

@garyschulte garyschulte merged commit 3886b0d into Consensys:main Apr 1, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants