Skip to content
Closed
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
45 changes: 19 additions & 26 deletions src/zkevm_specs/evm/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from ..util import (
Array4,
Array8,
Array10,
RLC,
MAX_N_BYTES,
N_BYTES_MEMORY_ADDRESS,
Expand Down Expand Up @@ -349,7 +349,7 @@ def opcode_lookup_at(self, index: int, is_code: bool) -> int:
else:
return self.bytecode_lookup(self.curr.code_source, index, is_code)

def rw_lookup(self, rw: RW, tag: RWTableTag, inputs: Sequence[int], rw_counter: Optional[int] = None) -> Array8:
def rw_lookup(self, rw: RW, tag: RWTableTag, inputs: Sequence[int], rw_counter: Optional[int] = None) -> Array10:
if rw_counter is None:
rw_counter = self.curr.rw_counter + self.rw_counter_offset
self.rw_counter_offset += 1
Expand All @@ -361,13 +361,13 @@ def state_write_only_persistent(
tag: RWTableTag,
inputs: Sequence[int],
is_persistent: bool,
) -> Array8:
) -> Array10:
assert tag.write_only_persistent()

if is_persistent:
return self.rw_lookup(RW.Write, tag, inputs)

return 8 * [None]
return 10 * [None]

def state_write_with_reversion(
self,
Expand All @@ -376,7 +376,7 @@ def state_write_with_reversion(
is_persistent: bool,
rw_counter_end_of_reversion: int,
state_write_counter: Optional[int] = None,
) -> Array8:
) -> Array10:
assert tag.write_with_reversion()

row = self.rw_lookup(RW.Write, tag, inputs)
Expand All @@ -390,14 +390,7 @@ def state_write_with_reversion(
if not is_persistent:
# Swap value and value_prev
inputs = list(row[3:])
if tag == RWTableTag.TxAccessListAccount:
inputs[2], inputs[3] = inputs[3], inputs[2]
elif tag == RWTableTag.TxAccessListAccountStorage:
inputs[3], inputs[4] = inputs[4], inputs[3]
elif tag == RWTableTag.Account:
inputs[2], inputs[3] = inputs[3], inputs[2]
elif tag == RWTableTag.AccountStorage:
inputs[2], inputs[3] = inputs[3], inputs[2]
inputs[-3], inputs[-4] = inputs[-4], inputs[-3]
self.rw_lookup(RW.Write, tag, inputs, rw_counter=rw_counter)

return row
Expand All @@ -408,7 +401,7 @@ def call_context_lookup(
if call_id is None:
call_id = self.curr.call_id

return self.rw_lookup(rw, RWTableTag.CallContext, [call_id, field_tag])[5]
return self.rw_lookup(rw, RWTableTag.CallContext, [call_id, field_tag])[-4]

def stack_pop(self) -> Union[int, RLC]:
stack_pointer_offset = self.stack_pointer_offset
Expand All @@ -421,7 +414,7 @@ def stack_push(self) -> Union[int, RLC]:

def stack_lookup(self, rw: RW, stack_pointer_offset: int) -> Union[int, RLC]:
stack_pointer = self.curr.stack_pointer + stack_pointer_offset
return self.rw_lookup(rw, RWTableTag.Stack, [self.curr.call_id, stack_pointer])[5]
return self.rw_lookup(rw, RWTableTag.Stack, [self.curr.call_id, stack_pointer])[-4]

def memory_write(self, memory_address: int, call_id: Optional[int] = None) -> int:
return self.memory_lookup(RW.Write, memory_address, call_id)
Expand All @@ -430,15 +423,15 @@ def memory_lookup(self, rw: RW, memory_address: int, call_id: Optional[int] = No
if call_id is None:
call_id = self.curr.call_id

return self.rw_lookup(rw, RWTableTag.Memory, [call_id, memory_address])[5]
return self.rw_lookup(rw, RWTableTag.Memory, [call_id, memory_address])[-4]

def tx_refund_read(self, tx_id) -> int:
row = self.rw_lookup(RW.Read, RWTableTag.TxRefund, [tx_id])
return row[4]
return row[-4]

def account_read(self, account_address: int, account_field_tag: AccountFieldTag) -> int:
row = self.rw_lookup(RW.Read, RWTableTag.Account, [account_address, account_field_tag])
return row[5]
return row[-4]

def account_write(
self,
Expand All @@ -450,7 +443,7 @@ def account_write(
RWTableTag.Account,
[account_address, account_field_tag],
)
return row[5], row[6]
return row[-4], row[-3]

def account_write_with_reversion(
self,
Expand All @@ -467,7 +460,7 @@ def account_write_with_reversion(
rw_counter_end_of_reversion,
state_write_counter,
)
return row[5], row[6]
return row[-4], row[-3]

def add_balance(self, account_address: int, values: Sequence[int]) -> Tuple[int, int]:
balance, balance_prev = self.account_write(account_address, AccountFieldTag.Balance)
Expand Down Expand Up @@ -523,9 +516,9 @@ def add_account_to_access_list(
row = self.rw_lookup(
RW.Write,
RWTableTag.TxAccessListAccount,
[tx_id, account_address, 1],
[tx_id, account_address, 0, 1],
)
return row[5] - row[6]
return row[-4] - row[-3]

def add_account_to_access_list_with_reversion(
self,
Expand All @@ -537,12 +530,12 @@ def add_account_to_access_list_with_reversion(
) -> bool:
row = self.state_write_with_reversion(
RWTableTag.TxAccessListAccount,
[tx_id, account_address, 1],
[tx_id, account_address, 0, 1],
is_persistent,
rw_counter_end_of_reversion,
state_write_counter,
)
return row[5] - row[6]
return row[-4] - row[-3]

def add_account_storage_to_access_list(
self,
Expand All @@ -555,7 +548,7 @@ def add_account_storage_to_access_list(
RWTableTag.TxAccessListAccountStorage,
[tx_id, account_address, storage_key, 1],
)
return row[6] - row[7]
return row[-4] - row[-3]

def add_account_storage_to_access_list_with_reversion(
self,
Expand All @@ -573,7 +566,7 @@ def add_account_storage_to_access_list_with_reversion(
rw_counter_end_of_reversion,
state_write_counter,
)
return row[6] - row[7]
return row[-4] - row[-3]

def transfer_with_gas_fee(
self,
Expand Down
33 changes: 14 additions & 19 deletions src/zkevm_specs/evm/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from enum import IntEnum, auto
from itertools import chain, product

from ..util import Array3, Array4, Array8
from ..util import Array3, Array4, Array10
from .execution_state import ExecutionState
from .opcode import (
invalid_opcodes,
Expand Down Expand Up @@ -166,15 +166,8 @@ def write_with_reversion(self) -> bool:
RWTableTag.TxAccessListAccountStorage,
RWTableTag.Account,
RWTableTag.AccountStorage,
]

# For state writes which don't affect future execution before reversion, we
# don't need to write them with reversion, instead we only need to write
# them (enable the lookup) when is_persistent is True.
def write_only_persistent(self) -> bool:
return self in [
RWTableTag.TxRefund,
RWTableTag.AccountDestructed,
RWTableTag.TxRefund,
]


Expand Down Expand Up @@ -283,20 +276,22 @@ class Tables:
# Each row in RWTable contains:
# - rw_counter
# - is_write
# - tag
# - value1
# - value2
# - value3
# - value4
# - value5
rw_table: Set[Array8]
# - key1 (tag)
# - key2
# - key3
# - key4
# - value
# - value_prev
# - aux1
# - aux2
rw_table: Set[Array10]

def __init__(
self,
block_table: Set[Array3],
tx_table: Set[Array4],
bytecode_table: Set[Array4],
rw_table: Set[Array8],
rw_table: Set[Array10],
) -> None:
self.block_table = block_table
self.tx_table = tx_table
Expand All @@ -319,8 +314,8 @@ def bytecode_lookup(self, inputs: Sequence[int]) -> Array4:
assert len(inputs) <= 4
return _lookup("bytecode_table", self.bytecode_table, inputs)

def rw_lookup(self, inputs: Sequence[int]) -> Array8:
assert len(inputs) <= 8
def rw_lookup(self, inputs: Sequence[int]) -> Array10:
assert len(inputs) <= 10
return _lookup("rw_table", self.rw_table, inputs)


Expand Down
1 change: 1 addition & 0 deletions src/zkevm_specs/util/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Array3 = NewType("Array3", Tuple[int, int, int])
Array4 = NewType("Array4", Tuple[int, int, int, int])
Array8 = NewType("Array8", Tuple[int, int, int, int, int, int, int, int])
Array10 = NewType("Array10", Tuple[int, int, int, int, int, int, int, int, int, int])
Array32 = NewType(
"Array32",
Tuple[
Expand Down
6 changes: 3 additions & 3 deletions tests/evm/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def test_add(opcode: Opcode, a: int, b: int, c: Optional[int]):
bytecode_table=set(bytecode.table_assignments(randomness)),
rw_table=set(
[
(9, RW.Read, RWTableTag.Stack, 1, 1022, a, 0, 0),
(10, RW.Read, RWTableTag.Stack, 1, 1023, b, 0, 0),
(11, RW.Write, RWTableTag.Stack, 1, 1023, c, 0, 0),
(9, RW.Read, RWTableTag.Stack, 1, 1022, 0, a, 0, 0, 0),
(10, RW.Read, RWTableTag.Stack, 1, 1023, 0, b, 0, 0, 0),
(11, RW.Write, RWTableTag.Stack, 1, 1023, 0, c, 0, 0, 0),
]
),
)
Expand Down
Loading