Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions specs/opcode/32ORIGIN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ORIGIN opcode

## Procedure

The `ORIGIN` opcode gets the execution origination address (tx.origin).

## EVM behaviour

The `ORIGIN` opcode loads the sender address (20 bytes) of the transaction that originated this execution, then pushes the address to the stack.

## Circuit behaviour

1. Lookup call context in rw table for the txID
2. Lookup txID in Tx Table for the CallerAddress
3. Lookup stack write operation for the address

## Constraints

1. opId = 0x32
2. State transition:
- gc + 2 (1 stack write, 1 call context read)
- stack_pointer - 1
- pc + 1
- gas + 2
3. Lookups: 3
- `tx_id` is on current call context
- `address` is in the Tx Table
- `address` is on the top of stack

## Exceptions

1. stack overflow: stack is full, stack pointer = 0
2. out of gas: remaining gas is not enough

## Code

Please refer to [`origin.py`](src/zkevm_specs/evm/execution/origin.py)
2 changes: 2 additions & 0 deletions src/zkevm_specs/evm/execution/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .gas import *
from .jump import *
from .jumpi import *
from .origin import *
from .push import *
from .slt_sgt import *
from .gas import *
Expand All @@ -32,6 +33,7 @@
ExecutionState.EndBlock: end_block,
ExecutionState.CopyToMemory: copy_to_memory,
ExecutionState.ADD: add,
ExecutionState.ORIGIN: origin,
ExecutionState.CALLER: caller,
ExecutionState.CALLVALUE: callvalue,
ExecutionState.CALLDATACOPY: calldatacopy,
Expand Down
26 changes: 26 additions & 0 deletions src/zkevm_specs/evm/execution/origin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from ..instruction import Instruction, Transition
from ..opcode import Opcode
from ..table import CallContextFieldTag, TxContextFieldTag
from ...util.param import N_BYTES_ACCOUNT_ADDRESS


def origin(instruction: Instruction):
tx_id = instruction.call_context_lookup(CallContextFieldTag.TxId)

opcode = instruction.opcode_lookup(True)
instruction.constrain_equal(opcode, Opcode.ORIGIN)

instruction.constrain_equal(
instruction.int_to_rlc(
instruction.tx_context_lookup(tx_id, TxContextFieldTag.CallerAddress),
N_BYTES_ACCOUNT_ADDRESS,
),
instruction.stack_push(),
)

instruction.step_state_transition_in_same_context(
opcode,
rw_counter=Transition.delta(2),
program_counter=Transition.delta(1),
stack_pointer=Transition.delta(-1),
)
97 changes: 97 additions & 0 deletions tests/evm/test_origin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import pytest

from zkevm_specs.evm import (
Bytecode,
CallContextFieldTag,
ExecutionState,
StepState,
RW,
RWTableTag,
Tables,
Transaction,
verify_steps,
)
from zkevm_specs.util import rand_fp, rand_address, RLC
from zkevm_specs.util.typing import U256
from zkevm_specs.util.param import N_BYTES_ACCOUNT_ADDRESS

TESTING_DATA = (
0x00,
0x10,
0x302010,
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
rand_address(),
)


@pytest.mark.parametrize("origin", TESTING_DATA)
def test_origin(origin: U256):
randomness = rand_fp()

tx = Transaction(caller_address=origin)

bytecode = Bytecode().origin().stop()
bytecode_hash = RLC(bytecode.hash(), randomness)

tables = Tables(
block_table=set(),
tx_table=set(tx.table_assignments(randomness)),
bytecode_table=set(bytecode.table_assignments(randomness)),
rw_table=set(
[
(
9,
RW.Read,
RWTableTag.CallContext,
1,
CallContextFieldTag.TxId,
0,
tx.id,
0,
0,
0,
),
(
10,
RW.Write,
RWTableTag.Stack,
1,
1023,
0,
RLC(origin, randomness, N_BYTES_ACCOUNT_ADDRESS),
0,
0,
0,
),
]
),
)

verify_steps(
randomness=randomness,
tables=tables,
steps=[
StepState(
execution_state=ExecutionState.ORIGIN,
rw_counter=9,
call_id=1,
is_root=True,
is_create=False,
code_source=bytecode_hash,
program_counter=0,
stack_pointer=1024,
gas_left=2,
),
StepState(
execution_state=ExecutionState.STOP,
rw_counter=11,
call_id=1,
is_root=True,
is_create=False,
code_source=bytecode_hash,
program_counter=1,
stack_pointer=1023,
gas_left=0,
),
],
)