Skip to content

Commit

Permalink
Update EIP-663: Replace runtime stack checks with EOF validation rules (
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 authored and RaphaelHardFork committed Jan 30, 2024
1 parent 9465db7 commit 77eb6c0
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions EIPS/eip-663.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ status: Review
type: Standards Track
category: Core
created: 2017-07-03
requires: 3540
requires: 3540, 5450
---

## Abstract
Expand All @@ -34,22 +34,19 @@ We introduce two new instructions:

If the code is legacy bytecode, both of these instructions result in an *exceptional halt*. (*Note: This means no change to behaviour.*)

If the code is valid EOF1, the following execution rules apply:
If the code is valid EOF1, the following rules apply:

1. These instructions are followed by an 8-bit immediate value, which we call `imm`, and can have a value of 0 to 255. We introduce the variable `n` which equals to `imm + 1`.

2. For `DUPN`:
2. Code validation is extended to check that no relative jump instruction (`RJUMP`/`RJUMPI`/`RJUMPV`) targets immmediate values of `DUPN` or `SWAPN`.

- If the current stack height is less than `n`, then a stack underflow exception is issued.
- If the current stack height is at the limit (1024), a stack overflow exception is issued.
- Otherwise the `n`'th stack item is duplicated at the top of the stack. (*Note: We use 1-based indexing here.*)
3. The stack validation algorithm of [EIP-5450](./eip-5450.md) is extended:
3.1. Before `DUPN` if the current stack height is less than `n`, code is invalid. After `DUPN` stack height is incremented.
3.2. Before `SWAPN` if the current stack height is less than `n + 1`, code is invalid. After `SWAPN` stack height is not changed.

3. For `SWAPN`:

- If the current stack height is less than `n + 1`, then a stack underflow exception is issued.
- Otherwise the `n + 1`th stack item is swapped with the top stack item.

Clarification: the "stack underflow/overflow exception" means the EVM execution is halted and all gas is consumed.
4. Execution rules:
4.1. `DUPN`: the `n`'th stack item is duplicated at the top of the stack. (*Note: We use 1-based indexing here.*)
4.2 `SWAPN`: the `n + 1`th stack item is swapped with the top stack item.

The gas cost for both instructions is set at 3.

Expand All @@ -72,13 +69,13 @@ This has no effect on backwards compatibility because the opcodes were not previ

## Test Cases

For `0 <= n <= 255`:
Given variable `n`, which equals to `imm + 1`, for `1 <= n <= 256`:

- `DUPN n` to fail if `stack_height < n`.
- `SWAPN n` to fail if `stack_height < (n + 1)`.
- `DUPN n` to fail if `stack_height + 1 > 1024`.
- `DUPN n` and `SWAPN n` to fail if gas available is less than 3.
- otherwise `DUPN n` should push the `stack[n]` item to the stack, and `SWAPN n` should swap `stack[n + 1]` with `stack[stack.top()]`.
- `DUPN imm` to fail validation if `stack_height < n`.
- `SWAPN imm` to fail validation if `stack_height < (n + 1)`.
- `DUPN imm` to increment maximum stack height of a function. Validation fails if maximum stack height exceeds limit of 1023.
- `DUPN imm` and `SWAPN imm` to fail at run-time if gas available is less than 3.
- otherwise `DUPN imm` should push the `stack[n]` item to the stack, and `SWAPN imm` should swap `stack[n + 1]` with `stack[stack.top()]`.

## Security Considerations

Expand Down

0 comments on commit 77eb6c0

Please sign in to comment.