diff --git a/src/zkevm_specs/evm/instruction.py b/src/zkevm_specs/evm/instruction.py index 02527ff10..d9a570682 100644 --- a/src/zkevm_specs/evm/instruction.py +++ b/src/zkevm_specs/evm/instruction.py @@ -4,7 +4,7 @@ from ..util import ( Array4, - Array8, + Array10, RLC, MAX_N_BYTES, N_BYTES_MEMORY_ADDRESS, @@ -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 @@ -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, @@ -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) @@ -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 @@ -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 @@ -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) @@ -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, @@ -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, @@ -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) @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/src/zkevm_specs/evm/table.py b/src/zkevm_specs/evm/table.py index 116605a2f..993495e15 100644 --- a/src/zkevm_specs/evm/table.py +++ b/src/zkevm_specs/evm/table.py @@ -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, @@ -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, ] @@ -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 @@ -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) diff --git a/src/zkevm_specs/util/typing.py b/src/zkevm_specs/util/typing.py index 370f562db..0ab5b8234 100644 --- a/src/zkevm_specs/util/typing.py +++ b/src/zkevm_specs/util/typing.py @@ -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[ diff --git a/tests/evm/test_add.py b/tests/evm/test_add.py index d54a97ddf..da3d369c8 100644 --- a/tests/evm/test_add.py +++ b/tests/evm/test_add.py @@ -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), ] ), ) diff --git a/tests/evm/test_begin_tx.py b/tests/evm/test_begin_tx.py index ac199750a..6697256a9 100644 --- a/tests/evm/test_begin_tx.py +++ b/tests/evm/test_begin_tx.py @@ -97,30 +97,45 @@ def test_begin_tx(tx: Transaction, callee: Account, result: bool): bytecode_table=set(callee.code.table_assignments(randomness)), rw_table=set( [ - (1, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, tx.id, 0, 0), + (1, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, 0, tx.id, 0, 0, 0), ( 2, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.RwCounterEndOfReversion, + 0, 0 if result else rw_counter_end_of_reversion, 0, 0, + 0, ), - (3, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.IsPersistent, result, 0, 0), - (4, RW.Write, RWTableTag.Account, tx.caller_address, AccountFieldTag.Nonce, tx.nonce + 1, tx.nonce, 0), - (5, RW.Write, RWTableTag.TxAccessListAccount, 1, tx.caller_address, 1, 0, 0), - (6, RW.Write, RWTableTag.TxAccessListAccount, 1, tx.callee_address, 1, 0, 0), + (3, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.IsPersistent, 0, result, 0, 0, 0), + ( + 4, + RW.Write, + RWTableTag.Account, + tx.caller_address, + AccountFieldTag.Nonce, + 0, + tx.nonce + 1, + tx.nonce, + 0, + 0, + ), + (5, RW.Write, RWTableTag.TxAccessListAccount, 1, tx.caller_address, 0, 1, 0, 0, 0), + (6, RW.Write, RWTableTag.TxAccessListAccount, 1, tx.callee_address, 0, 1, 0, 0, 0), ( 7, RW.Write, RWTableTag.Account, tx.caller_address, AccountFieldTag.Balance, + 0, RLC(caller_balance, randomness), RLC(caller_balance_prev, randomness), 0, + 0, ), ( 8, @@ -128,9 +143,11 @@ def test_begin_tx(tx: Transaction, callee: Account, result: bool): RWTableTag.Account, tx.callee_address, AccountFieldTag.Balance, + 0, RLC(callee_balance, randomness), RLC(callee_balance_prev, randomness), 0, + 0, ), ( 9, @@ -138,26 +155,63 @@ def test_begin_tx(tx: Transaction, callee: Account, result: bool): RWTableTag.Account, tx.callee_address, AccountFieldTag.CodeHash, + 0, bytecode_hash, bytecode_hash, 0, + 0, + ), + (10, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.Depth, 0, 1, 0, 0, 0), + ( + 11, + RW.Read, + RWTableTag.CallContext, + 1, + CallContextFieldTag.CallerAddress, + 0, + tx.caller_address, + 0, + 0, + 0, + ), + ( + 12, + RW.Read, + RWTableTag.CallContext, + 1, + CallContextFieldTag.CalleeAddress, + 0, + tx.callee_address, + 0, + 0, + 0, + ), + (13, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallDataOffset, 0, 0, 0, 0, 0), + ( + 14, + RW.Read, + RWTableTag.CallContext, + 1, + CallContextFieldTag.CallDataLength, + 0, + len(tx.call_data), + 0, + 0, + 0, ), - (10, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.Depth, 1, 0, 0), - (11, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallerAddress, tx.caller_address, 0, 0), - (12, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CalleeAddress, tx.callee_address, 0, 0), - (13, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallDataOffset, 0, 0, 0), - (14, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallDataLength, len(tx.call_data), 0, 0), ( 15, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.Value, + 0, RLC(tx.value, randomness), 0, 0, + 0, ), - (16, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.IsStatic, 0, 0, 0), + (16, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.IsStatic, 0, 0, 0, 0, 0), ] + ( [] @@ -169,9 +223,11 @@ def test_begin_tx(tx: Transaction, callee: Account, result: bool): RWTableTag.Account, tx.callee_address, AccountFieldTag.Balance, + 0, RLC(callee_balance_prev, randomness), RLC(callee_balance, randomness), 0, + 0, ), ( rw_counter_end_of_reversion, @@ -179,9 +235,11 @@ def test_begin_tx(tx: Transaction, callee: Account, result: bool): RWTableTag.Account, tx.caller_address, AccountFieldTag.Balance, + 0, RLC(caller_balance_prev, randomness), RLC(caller_balance, randomness), 0, + 0, ), ] ) diff --git a/tests/evm/test_calldatasize.py b/tests/evm/test_calldatasize.py index 84bbeecac..0f652fee3 100644 --- a/tests/evm/test_calldatasize.py +++ b/tests/evm/test_calldatasize.py @@ -35,8 +35,8 @@ def test_calldatasize(calldatasize: U64): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallDataLength, calldatasize, 0, 0), - (10, RW.Write, RWTableTag.Stack, 1, 1023, RLC(calldatasize, randomness, N_BYTES_U64), 0, 0), + (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallDataLength, 0, calldatasize, 0, 0, 0), + (10, RW.Write, RWTableTag.Stack, 1, 1023, 0, RLC(calldatasize, randomness, N_BYTES_U64), 0, 0, 0), ] ), ) diff --git a/tests/evm/test_caller.py b/tests/evm/test_caller.py index 5d25a927d..b04988eac 100644 --- a/tests/evm/test_caller.py +++ b/tests/evm/test_caller.py @@ -36,8 +36,8 @@ def test_caller(caller: U160): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallerAddress, caller, 0, 0), - (10, RW.Write, RWTableTag.Stack, 1, 1023, RLC(caller, randomness, N_BYTES_ACCOUNT_ADDRESS), 0, 0), + (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallerAddress, 0, caller, 0, 0, 0), + (10, RW.Write, RWTableTag.Stack, 1, 1023, 0, RLC(caller, randomness, N_BYTES_ACCOUNT_ADDRESS), 0, 0, 0), ] ), ) diff --git a/tests/evm/test_callvalue.py b/tests/evm/test_callvalue.py index 5e4b619bc..48cf38ba1 100644 --- a/tests/evm/test_callvalue.py +++ b/tests/evm/test_callvalue.py @@ -38,8 +38,8 @@ def test_callvalue(callvalue: U256): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.Value, callvalue_rlc, 0, 0), - (10, RW.Write, RWTableTag.Stack, 1, 1023, callvalue_rlc, 0, 0), + (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.Value, 0, callvalue_rlc, 0, 0, 0), + (10, RW.Write, RWTableTag.Stack, 1, 1023, 0, callvalue_rlc, 0, 0, 0), ] ), ) diff --git a/tests/evm/test_coinbase.py b/tests/evm/test_coinbase.py index c63506fd0..482753baf 100644 --- a/tests/evm/test_coinbase.py +++ b/tests/evm/test_coinbase.py @@ -31,7 +31,7 @@ def test_coinbase(coinbase: U160): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Write, RWTableTag.Stack, 1, 1023, RLC(coinbase, randomness, 20), 0, 0), + (9, RW.Write, RWTableTag.Stack, 1, 1023, 0, RLC(coinbase, randomness, 20), 0, 0, 0), ] ), ) diff --git a/tests/evm/test_end_block.py b/tests/evm/test_end_block.py index 21873f85b..005b1be8f 100644 --- a/tests/evm/test_end_block.py +++ b/tests/evm/test_end_block.py @@ -31,7 +31,7 @@ def test_end_block(is_last_step: bool): chain( # dummy read/write for counting [(i, *7 * [0]) for i in range(22)], - [(22, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, tx.id, 0, 0)] + [(22, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, 0, tx.id, 0, 0, 0)] if is_last_step else [], ) diff --git a/tests/evm/test_end_tx.py b/tests/evm/test_end_tx.py index b69725937..284ce88f3 100644 --- a/tests/evm/test_end_tx.py +++ b/tests/evm/test_end_tx.py @@ -58,17 +58,19 @@ def test_end_tx(tx: Transaction, gas_left: int, refund: int, is_last_tx: bool): bytecode_table=set(), rw_table=set( [ - (17, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, tx.id, 0, 0), - (18, RW.Read, RWTableTag.TxRefund, tx.id, refund, refund, 0, 0), + (17, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, 0, tx.id, 0, 0, 0), + (18, RW.Read, RWTableTag.TxRefund, tx.id, 0, 0, refund, refund, 0, 0), ( 19, RW.Write, RWTableTag.Account, tx.caller_address, AccountFieldTag.Balance, + 0, RLC(caller_balance, randomness), RLC(caller_balance_prev, randomness), 0, + 0, ), ( 20, @@ -76,15 +78,17 @@ def test_end_tx(tx: Transaction, gas_left: int, refund: int, is_last_tx: bool): RWTableTag.Account, block.coinbase, AccountFieldTag.Balance, + 0, RLC(coinbase_balance, randomness), RLC(coinbase_balance_prev, randomness), 0, + 0, ), ] + ( [] if is_last_tx - else [(21, RW.Read, RWTableTag.CallContext, 22, CallContextFieldTag.TxId, tx.id + 1, 0, 0)] + else [(21, RW.Read, RWTableTag.CallContext, 22, CallContextFieldTag.TxId, 0, tx.id + 1, 0, 0, 0)] ) ), ) diff --git a/tests/evm/test_jump.py b/tests/evm/test_jump.py index 06ea99e65..c30adb868 100644 --- a/tests/evm/test_jump.py +++ b/tests/evm/test_jump.py @@ -34,7 +34,7 @@ def test_jump(opcode: Opcode, dest_bytes: bytes): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Read, RWTableTag.Stack, 1, 1021, dest, 0, 0), + (9, RW.Read, RWTableTag.Stack, 1, 1021, 0, dest, 0, 0, 0), ] ), ) diff --git a/tests/evm/test_jumpi.py b/tests/evm/test_jumpi.py index 6c7105c7b..ae49736af 100644 --- a/tests/evm/test_jumpi.py +++ b/tests/evm/test_jumpi.py @@ -35,8 +35,8 @@ def test_jumpi_cond_nonzero(opcode: Opcode, cond_bytes: bytes, dest_bytes: bytes bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Read, RWTableTag.Stack, 1, 1021, dest, 0, 0), - (10, RW.Read, RWTableTag.Stack, 1, 1022, cond, 0, 0), + (9, RW.Read, RWTableTag.Stack, 1, 1021, 0, dest, 0, 0, 0), + (10, RW.Read, RWTableTag.Stack, 1, 1022, 0, cond, 0, 0, 0), ], ), ) @@ -92,8 +92,8 @@ def test_jumpi_cond_zero(opcode: Opcode, cond_bytes: bytes, dest_bytes: bytes): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Read, RWTableTag.Stack, 1, 1021, dest, 0, 0), - (10, RW.Read, RWTableTag.Stack, 1, 1022, cond, 0, 0), + (9, RW.Read, RWTableTag.Stack, 1, 1021, 0, dest, 0, 0, 0), + (10, RW.Read, RWTableTag.Stack, 1, 1022, 0, cond, 0, 0, 0), ], ), ) diff --git a/tests/evm/test_push.py b/tests/evm/test_push.py index 252b11a17..2262db9ed 100644 --- a/tests/evm/test_push.py +++ b/tests/evm/test_push.py @@ -39,7 +39,7 @@ def test_push(value_be_bytes: bytes): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (8, RW.Write, RWTableTag.Stack, 1, 1023, value, 0, 0), + (8, RW.Write, RWTableTag.Stack, 1, 1023, 0, value, 0, 0, 0), ] ), ) diff --git a/tests/evm/test_slt_sgt.py b/tests/evm/test_slt_sgt.py index 7d26ccb5c..f0fcb015d 100644 --- a/tests/evm/test_slt_sgt.py +++ b/tests/evm/test_slt_sgt.py @@ -201,9 +201,9 @@ def test_slt_sgt(opcode: Opcode, a: int, b: int, res: 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, res, 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, res, 0, 0, 0), ] ), )