Skip to content

Conversation

lightclient
Copy link
Member

Adding a stack arg based approach for swap, dup, and exchange.

@lightclient lightclient requested a review from eth-bot as a code owner March 21, 2025 05:07
@github-actions github-actions bot added c-new Creates a brand new proposal s-draft This EIP is a Draft t-core labels Mar 21, 2025
@eth-bot
Copy link
Collaborator

eth-bot commented Mar 21, 2025

File EIPS/eip-7912.md

Requires 1 more reviewers from @g11tech, @SamWilsn

@eth-bot eth-bot added the e-consensus Waiting on editor consensus label Mar 21, 2025
@github-actions github-actions bot added the w-ci Waiting on CI to pass label Mar 21, 2025
instructions. Failure to follow this calling convention will result in an
out-of-gas error. If `N` is zero, fail with out-of-gas error.

`SWAPN` pops `N` from the stack and swaps the new top stack element with the
Copy link
Member

Choose a reason for hiding this comment

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

PUSH1 N SWAPN take 3 bytes while the intermediate could also be done here: SWAPN N which takes two bytes and does not have this extra check "previous opcode must be PUSH1". Why do we want the PUSH1 SWAPN order and not SWAPN N (thus the opcode is defined by two bytes?). This can essentially be added as "just another PUSH1 opcode" to the jumpdest analysis logic for the jumpdest validation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Because adding an immediate to legacy code has historically caused disagreement since it would alter the behavior of contracts already deployed.

Comment on lines 82 to 83
`EXCHANGE` pops `X` from the stack and will swap the stack element at index
`N-1` with the stack element at `M-1`.

Choose a reason for hiding this comment

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

Suggested change
`EXCHANGE` pops `X` from the stack and will swap the stack element at index
`N-1` with the stack element at `M-1`.
`EXCHANGE` pops `X` from the stack and will swap the stack element at index `N-1` with the stack element at `M-1`.

Comment on lines 101 to 102
Since `SWAP1` and `DUP1` operate on the top of the stack, it seems fitting that
`EXCHANGE(1, 2)` operate on the `top` and `top-1`.

Choose a reason for hiding this comment

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

Suggested change
Since `SWAP1` and `DUP1` operate on the top of the stack, it seems fitting that
`EXCHANGE(1, 2)` operate on the `top` and `top-1`.
Since `SWAP1` and `DUP1` operate on the top of the stack, it seems fitting that `EXCHANGE(1, 2)` operate on the `top` and `top-1`.

@eth-bot eth-bot added the e-review Waiting on editor to review label Mar 25, 2025
@eth-bot eth-bot changed the title Add new swap, dup, exchange eip Add EIP: Pragmatic expansion of stack manipulation tools Mar 25, 2025
@github-actions github-actions bot added w-ci Waiting on CI to pass and removed w-ci Waiting on CI to pass labels Mar 25, 2025
Both operations take a single argument from the stack, `N`. This argument must
be provided by a `PUSH1` operation immediately preceding the `SWAPN` and `DUPN`
instructions. Failure to follow this calling convention will result in an
out-of-gas error. If `N` is zero, fail with out-of-gas error.
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be easier to view N after this number has been popped from the stack? This removes the check to see if 0 is being pushed, and it also allows for one extra stack item to be addressed. Maybe this is the reason that 0 is removed here though, it is to not allow PUSH0 DUPN/SWAPN?

Copy link
Member

Choose a reason for hiding this comment

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

Ah ok, and banning N=0 also ensures we keep the same style of names, so DUP0/SWAP0 is not a thing. Makes sense to ban 0 (although for practical reasons could make sense also to allow N=0 and start from there)


The `EXCHANGE` instruction takes a single argument from the stack `X` and
deconstructs it into two operands, `N` and `M`. `N` is `X >> 4` and `M` is
`X & 0x0F`. The argument `X` must be provided by a `PUSH2` operation immediately
Copy link
Member

Choose a reason for hiding this comment

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

If the PUSH2 and not PUSH1 is intended then this it should be written somewhere that N can have values 0-0x0fff, otherwise this might look like it should be PUSH1.


### Gas costs

All operations cost `3` gas. Preceding push operations are charged separately
Copy link
Member

Choose a reason for hiding this comment

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

This might be too low because there are checks for the previous operation being a PUSH1/PUSH2, bitwise operations on EXCHANGE, and the check that the PUSHd values are not 0, plus the actual stack operations.

@g11tech
Copy link
Contributor

g11tech commented Apr 22, 2025

let know when EIP is ready for review w.r.t. draft acceptance

@github-actions github-actions bot removed the w-ci Waiting on CI to pass label Apr 24, 2025
Copy link

The commit 80c988c (as a parent of 6c3d554) contains errors.
Please inspect the Run Summary for details.

@github-actions github-actions bot added the w-ci Waiting on CI to pass label Apr 24, 2025
@eth-bot eth-bot changed the title Add EIP: Pragmatic expansion of stack manipulation tools Add EIP: Pragmatic stack manipulation tools Apr 24, 2025
@github-actions github-actions bot removed the w-ci Waiting on CI to pass label Apr 24, 2025
@lightclient
Copy link
Member Author

@g11tech g2g

## Security Considerations

When verifying the preceding `PUSH` operations, client implementers must ensure
that the preceding bytes are not part of a longer segment of push data (e.g.
Copy link
Contributor

Choose a reason for hiding this comment

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

34 byte code look back; 3 gas seems very low?


## Abstract

Add `SWAP17`-`SWAP24`, `DUP17` - `DUP24`, `SWAPN`, `DUPN`, and `EXCHANGE`
Copy link
Contributor

Choose a reason for hiding this comment

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

Why burn 17 opcodes for the fixed values and add 2 dynamic values?

@lightclient lightclient merged commit d73f59e into ethereum:master Apr 28, 2025
29 of 35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c-new Creates a brand new proposal e-consensus Waiting on editor consensus e-review Waiting on editor to review s-draft This EIP is a Draft t-core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants