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
2 changes: 1 addition & 1 deletion specs/tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Type sizes:
| | | *CallContext state* | | | *CallContextFieldTag* (rw) | | | | |
| $counter | $isWrite | CallContext | $callID | | IsRoot | | $value | 0 | 0 |
| $counter | $isWrite | CallContext | $callID | | IsCreate | | $value | 0 | 0 |
| $counter | $isWrite | CallContext | $callID | | CodeSource | | $value | 0 | 0 |
| $counter | $isWrite | CallContext | $callID | | CodeHash | | $value | 0 | 0 |
| $counter | $isWrite | CallContext | $callID | | ProgramCounter | | $value | 0 | 0 |
| $counter | $isWrite | CallContext | $callID | | StackPointer | | $value | 0 | 0 |
| $counter | $isWrite | CallContext | $callID | | GasLeft | | $value | 0 | 0 |
Expand Down
9 changes: 6 additions & 3 deletions src/zkevm_specs/evm/execution/begin_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ def begin_tx(instruction: Instruction):

if tx_is_create == 1:
# TODO: Verify created address
# TODO: Decide what code_source should be (tx_id or hash of creation code)
# code_hash represents the contract creation code
# 1. In the case of root call, code_hash is the hash of the tx calldata.
# 2. In the case of internal call, code_hash is the hash of the chunk of
# caller memory corresponding to the contract init code.
raise NotImplementedError
elif tx_callee_address in list(PrecompiledAddress):
# TODO: Handle precompile
Expand Down Expand Up @@ -99,7 +102,7 @@ def begin_tx(instruction: Instruction):
(CallContextFieldTag.LastCalleeReturnDataLength, FQ(0)),
(CallContextFieldTag.IsRoot, FQ(True)),
(CallContextFieldTag.IsCreate, FQ(False)),
(CallContextFieldTag.CodeSource, code_hash),
(CallContextFieldTag.CodeHash, code_hash),
]:
instruction.constrain_equal(
instruction.call_context_lookup(tag, call_id=call_id), value
Expand All @@ -110,7 +113,7 @@ def begin_tx(instruction: Instruction):
call_id=Transition.to(call_id),
is_root=Transition.to(True),
is_create=Transition.to(False),
code_source=Transition.to(code_hash),
code_hash=Transition.to(code_hash),
gas_left=Transition.to(gas_left),
reversible_write_counter=Transition.to(2),
log_id=Transition.to(0),
Expand Down
6 changes: 3 additions & 3 deletions src/zkevm_specs/evm/execution/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def call(instruction: Instruction):
call_id=Transition.same(),
is_root=Transition.same(),
is_create=Transition.same(),
code_source=Transition.same(),
code_hash=Transition.same(),
)
else:
# Save caller's call state
Expand Down Expand Up @@ -181,7 +181,7 @@ def call(instruction: Instruction):
(CallContextFieldTag.LastCalleeReturnDataLength, FQ(0)),
(CallContextFieldTag.IsRoot, FQ(False)),
(CallContextFieldTag.IsCreate, FQ(False)),
(CallContextFieldTag.CodeSource, callee_code_hash.expr()),
(CallContextFieldTag.CodeHash, callee_code_hash.expr()),
]:
instruction.constrain_equal(
instruction.call_context_lookup(field_tag, call_id=callee_call_id),
Expand All @@ -196,7 +196,7 @@ def call(instruction: Instruction):
call_id=Transition.to(callee_call_id),
is_root=Transition.to(False),
is_create=Transition.to(False),
code_source=Transition.to(callee_code_hash),
code_hash=Transition.to(callee_code_hash),
gas_left=Transition.to(callee_gas_left),
reversible_write_counter=Transition.to(2),
log_id=Transition.same(),
Expand Down
4 changes: 2 additions & 2 deletions src/zkevm_specs/evm/execution/codecopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def codecopy(instruction: Instruction):
memory_offset, size = instruction.memory_offset_and_length(memory_offset_word, size_word)
code_offset = instruction.rlc_to_fq(code_offset_word, N_BYTES_MEMORY_ADDRESS)

code_size = instruction.bytecode_length(instruction.curr.code_source)
code_size = instruction.bytecode_length(instruction.curr.code_hash)

next_memory_size, memory_expansion_gas_cost = instruction.memory_expansion_dynamic_length(
memory_offset, size
Expand All @@ -35,7 +35,7 @@ def codecopy(instruction: Instruction):
instruction.constrain_equal(next_aux.dst_addr, memory_offset)
instruction.constrain_equal(next_aux.src_addr_end, code_size)
instruction.constrain_equal(next_aux.bytes_left, size)
instruction.constrain_equal(next_aux.code_source, instruction.curr.code_source)
instruction.constrain_equal(next_aux.code_hash, instruction.curr.code_hash)

instruction.step_state_transition_in_same_context(
opcode,
Expand Down
6 changes: 3 additions & 3 deletions src/zkevm_specs/evm/execution/copy_code_to_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def copy_code_to_memory(instruction: Instruction):
for idx in range(MAX_N_BYTES_COPY_CODE_TO_MEMORY):
if buffer_reader.read_flag(idx) == 1:
byte = instruction.bytecode_lookup(
aux.code_source,
aux.code_hash,
aux.src_addr + idx,
)
buffer_reader.constrain_byte(idx, byte)
Expand All @@ -47,14 +47,14 @@ def copy_code_to_memory(instruction: Instruction):
instruction.constrain_equal(next_aux.dst_addr, aux.dst_addr + copied_bytes)
instruction.constrain_equal(next_aux.bytes_left + copied_bytes, aux.bytes_left)
instruction.constrain_equal(next_aux.src_addr_end, aux.src_addr_end)
instruction.constrain_equal(next_aux.code_source, aux.code_source)
instruction.constrain_equal(next_aux.code_hash, aux.code_hash)

instruction.constrain_step_state_transition(
rw_counter=Transition.delta(instruction.rw_counter_offset),
call_id=Transition.same(),
is_root=Transition.same(),
is_create=Transition.same(),
code_source=Transition.same(),
code_hash=Transition.same(),
program_counter=Transition.same(),
stack_pointer=Transition.same(),
memory_size=Transition.same(),
Expand Down
2 changes: 1 addition & 1 deletion src/zkevm_specs/evm/execution/memory_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def copy_to_memory(instruction: Instruction):
call_id=Transition.same(),
is_root=Transition.same(),
is_create=Transition.same(),
code_source=Transition.same(),
code_hash=Transition.same(),
program_counter=Transition.same(),
stack_pointer=Transition.same(),
memory_size=Transition.same(),
Expand Down
10 changes: 5 additions & 5 deletions src/zkevm_specs/evm/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def constrain_step_state_transition(self, **kwargs: Transition):
"call_id",
"is_root",
"is_create",
"code_source",
"code_hash",
"program_counter",
"stack_pointer",
"gas_left",
Expand Down Expand Up @@ -209,7 +209,7 @@ def step_state_transition_to_new_context(
call_id: Transition,
is_root: Transition,
is_create: Transition,
code_source: Transition,
code_hash: Transition,
gas_left: Transition,
reversible_write_counter: Transition,
log_id: Transition,
Expand All @@ -219,7 +219,7 @@ def step_state_transition_to_new_context(
call_id=call_id,
is_root=is_root,
is_create=is_create,
code_source=code_source,
code_hash=code_hash,
gas_left=gas_left,
reversible_write_counter=reversible_write_counter,
log_id=log_id,
Expand Down Expand Up @@ -257,7 +257,7 @@ def step_state_transition_in_same_context(
call_id=Transition.same(),
is_root=Transition.same(),
is_create=Transition.same(),
code_source=Transition.same(),
code_hash=Transition.same(),
)

def sum(self, values: Sequence[IntOrFQ]) -> FQ:
Expand Down Expand Up @@ -509,7 +509,7 @@ def opcode_lookup_at(self, index: FQ, is_code: bool) -> FQ:
"The opcode source when is_root and is_create (root creation call) is not determined yet"
)
else:
return self.bytecode_lookup(self.curr.code_source, index, FQ(is_code)).expr()
return self.bytecode_lookup(self.curr.code_hash, index, FQ(is_code)).expr()

def rw_lookup(
self,
Expand Down
29 changes: 13 additions & 16 deletions src/zkevm_specs/evm/step.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class StepState:
Step state EVM circuit tracks step by step and used to ensure the
execution trace is verified continuously and chronologically.
It includes fields that are used from beginning to end like is_root,
is_create and code_source.
is_create and code_hash.
It also includes call's mutable states which change almost every step like
program_counter and stack_pointer.
"""
Expand All @@ -18,18 +18,15 @@ class StepState:
rw_counter: FQ
call_id: FQ

# The following 3 fields decide the opcode source. There are 2 possible
# cases:
# 1. Root creation call (is_root and is_create)
# It was planned to set the code_source to tx_id, then lookup tx_table's
# CallData field directly, but is still yet to be determined.
# See the issue https://github.com/appliedzkp/zkevm-specs/issues/73 for
# further discussion.
# 2. Deployed contract interaction or internal creation call
# We set code_source to bytecode_hash and lookup bytecode_table.
is_root: bool
is_create: bool
code_source: RLC

# code_hash represents the bytecode hash of the bytecode. This is straightforward
# for a contract call that does not create a contract. For a creation call,
# we already populate the bytecode table with the contract creation code either
# from the tx calldata (in the case of a root call) or from the caller's memory
# (in the case of an internal call).
code_hash: RLC

# The following fields change almost every step.
program_counter: FQ
Expand All @@ -55,7 +52,7 @@ def __init__(
call_id: int = 0,
is_root: bool = False,
is_create: bool = False,
code_source: RLC = RLC(0),
code_hash: RLC = RLC(0),
program_counter: int = 0,
stack_pointer: int = 1024,
gas_left: int = 0,
Expand All @@ -69,7 +66,7 @@ def __init__(
self.call_id = FQ(call_id)
self.is_root = is_root
self.is_create = is_create
self.code_source = code_source
self.code_hash = code_hash
self.program_counter = FQ(program_counter)
self.stack_pointer = FQ(stack_pointer)
self.gas_left = FQ(gas_left)
Expand Down Expand Up @@ -131,18 +128,18 @@ class CopyCodeToMemoryAuxData:
dst_addr: FQ
bytes_left: FQ
src_addr_end: FQ
code_source: RLC
code_hash: RLC

def __init__(
self,
src_addr: int,
dst_addr: int,
bytes_left: int,
src_addr_end: int,
code_source: RLC,
code_hash: RLC,
):
self.src_addr = FQ(src_addr)
self.dst_addr = FQ(dst_addr)
self.bytes_left = FQ(bytes_left)
self.src_addr_end = FQ(src_addr_end)
self.code_source = code_source
self.code_hash = code_hash
2 changes: 1 addition & 1 deletion src/zkevm_specs/evm/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class CallContextFieldTag(IntEnum):
IsStatic = auto() # to know if state modification is within static call or not
IsRoot = auto()
IsCreate = auto()
CodeSource = auto()
CodeHash = auto()

# The following are read-only data inside a call like previous section for
# opcode RETURNDATASIZE and RETURNDATACOPY, except they will be updated when
Expand Down
4 changes: 2 additions & 2 deletions tests/evm/test_add_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_add_sub(opcode: Opcode, a: int, b: int):
call_id=1,
is_root=True,
is_create=False,
code_source=bytecode_hash,
code_hash=bytecode_hash,
program_counter=66,
stack_pointer=1022,
gas_left=3,
Expand All @@ -70,7 +70,7 @@ def test_add_sub(opcode: Opcode, a: int, b: int):
call_id=1,
is_root=True,
is_create=False,
code_source=bytecode_hash,
code_hash=bytecode_hash,
program_counter=67,
stack_pointer=1023,
gas_left=0,
Expand Down
4 changes: 2 additions & 2 deletions tests/evm/test_begin_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_begin_tx(tx: Transaction, callee: Account, is_success: bool):
.call_context_read(1, CallContextFieldTag.LastCalleeReturnDataLength, 0) \
.call_context_read(1, CallContextFieldTag.IsRoot, True) \
.call_context_read(1, CallContextFieldTag.IsCreate, False) \
.call_context_read(1, CallContextFieldTag.CodeSource, bytecode_hash)
.call_context_read(1, CallContextFieldTag.CodeHash, bytecode_hash)
# fmt: on

tables = Tables(
Expand All @@ -157,7 +157,7 @@ def test_begin_tx(tx: Transaction, callee: Account, is_success: bool):
call_id=1,
is_root=True,
is_create=False,
code_source=bytecode_hash,
code_hash=bytecode_hash,
program_counter=0,
stack_pointer=1024,
gas_left=0,
Expand Down
4 changes: 2 additions & 2 deletions tests/evm/test_block_ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def verify_block_ctx(
call_id=1,
is_root=True,
is_create=False,
code_source=bytecode_hash,
code_hash=bytecode_hash,
program_counter=0,
stack_pointer=1024,
gas_left=2,
Expand All @@ -150,7 +150,7 @@ def verify_block_ctx(
call_id=1,
is_root=True,
is_create=False,
code_source=bytecode_hash,
code_hash=bytecode_hash,
program_counter=1,
stack_pointer=1023,
gas_left=0,
Expand Down
8 changes: 4 additions & 4 deletions tests/evm/test_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def test_call(
.call_context_read(24, CallContextFieldTag.LastCalleeReturnDataLength, 0) \
.call_context_read(24, CallContextFieldTag.IsRoot, False) \
.call_context_read(24, CallContextFieldTag.IsCreate, False) \
.call_context_read(24, CallContextFieldTag.CodeSource, callee_bytecode_hash)
.call_context_read(24, CallContextFieldTag.CodeHash, callee_bytecode_hash)
# fmt: on

tables = Tables(
Expand All @@ -263,7 +263,7 @@ def test_call(
call_id=1,
is_root=True,
is_create=False,
code_source=caller_bytecode_hash,
code_hash=caller_bytecode_hash,
program_counter=231,
stack_pointer=1017,
gas_left=caller_ctx.gas_left,
Expand All @@ -277,7 +277,7 @@ def test_call(
call_id=1,
is_root=True,
is_create=False,
code_source=caller_bytecode_hash,
code_hash=caller_bytecode_hash,
program_counter=232,
stack_pointer=1023,
gas_left=expected.caller_gas_left,
Expand All @@ -293,7 +293,7 @@ def test_call(
call_id=24,
is_root=False,
is_create=False,
code_source=callee_bytecode_hash,
code_hash=callee_bytecode_hash,
program_counter=0,
stack_pointer=1024,
gas_left=expected.callee_gas_left,
Expand Down
Loading