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/opcode/54SLOAD_55SSTORE.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
- `SLOAD`: remains the same
- `SSTORE`: -2
- pc + 1
- state_write_counter
- reversible_write_counter
- `SLOAD`: +1 (access_list)
- `SSTORE`: +3 (for storage, access_list & gas_refund respectively)
- gas:
Expand Down
4 changes: 2 additions & 2 deletions specs/opcode/A0_A4LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ denote `LOGN` where `N` in `[0,4]` meaning topic count.

- stack_pointer + 2 + `N`
- pc + 1
- state_write_counter + 1:
- reversible_write_counter + 1:
- log_index + 1
- memory size to expansion
- state_write_counter + 1
- reversible_write_counter + 1
- gas - dynamic_gas
(dynamic_gas = 375 * `N` + 8 * `msize` + memory_expansion_cost)

Expand Down
4 changes: 2 additions & 2 deletions specs/tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Type sizes:
| $counter | $isWrite | CallContext | $callID | | StackPointer | | $value | 0 | 0 | 0 |
| $counter | $isWrite | CallContext | $callID | | GasLeft | | $value | 0 | 0 | 0 |
| $counter | $isWrite | CallContext | $callID | | MemorySize | | $value | 0 | 0 | 0 |
| $counter | $isWrite | CallContext | $callID | | StateWriteCounter | | $value | 0 | 0 | 0 |
| $counter | $isWrite | CallContext | $callID | | ReversibleWriteCounter | | $value | 0 | 0 | 0 |
| | | | | | | | | | | |
| $counter | $isWrite | Stack | $callID | $stackPointer | | | $value | 0 | 0 | 0 |
| $counter | $isWrite | Memory | $callID | $memoryAddress | | | $value | 0 | 0 | 0 |
Expand Down Expand Up @@ -140,7 +140,7 @@ In the case of an account without code, it can still have a row in the bytecode
Proved by the block circuit.

__Note that a generalisation is done by storing the ChainId field inside the block_table__
__when it should indeed live inside of the chain configuration section (which we don't have).__
__when it should indeed live inside of the chain configuration section (which we don't have).__
__Hence the addition inside of the block_table.__

| 0 Tag | 1 | 2 value |
Expand Down
2 changes: 1 addition & 1 deletion src/zkevm_specs/evm/execution/begin_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,5 @@ def begin_tx(instruction: Instruction):
is_create=Transition.to(False),
code_source=Transition.to(code_hash),
gas_left=Transition.to(gas_left),
state_write_counter=Transition.to(2),
reversible_write_counter=Transition.to(2),
)
13 changes: 8 additions & 5 deletions src/zkevm_specs/evm/execution/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def call(instruction: Instruction):
is_reverted_by_caller = is_success.expr() == FQ(1) and reversion_info.is_persistent == FQ(0)
if is_reverted_by_caller:
# Propagate rw_counter_end_of_reversion when callee succeeds but one of callers revert at some point.
# Note that we subtract it with current caller's state_write_counter as callee's endpoint, where caller's
# state_write_counter here is added by 1 due to adding callee to access list
# Note that we subtract it with current caller's reversible_write_counter as callee's endpoint, where caller's
# reversible_write_counter here is added by 1 due to adding callee to access list
instruction.constrain_equal(
callee_reversion_info.rw_counter_end_of_reversion,
reversion_info.rw_counter_of_reversion(),
Expand Down Expand Up @@ -137,7 +137,7 @@ def call(instruction: Instruction):
stack_pointer=Transition.delta(6),
gas_left=Transition.delta(has_value * GAS_STIPEND_CALL_WITH_VALUE - gas_cost),
memory_size=Transition.to(next_memory_size),
state_write_counter=Transition.delta(3),
reversible_write_counter=Transition.delta(3),
# Always stay same
call_id=Transition.same(),
is_root=Transition.same(),
Expand All @@ -151,7 +151,10 @@ def call(instruction: Instruction):
(CallContextFieldTag.StackPointer, instruction.curr.stack_pointer + 6),
(CallContextFieldTag.GasLeft, instruction.curr.gas_left - gas_cost - callee_gas_left),
(CallContextFieldTag.MemorySize, next_memory_size),
(CallContextFieldTag.StateWriteCounter, instruction.curr.state_write_counter + 1),
(
CallContextFieldTag.ReversibleWriteCounter,
instruction.curr.reversible_write_counter + 1,
),
]:
instruction.constrain_equal(
instruction.call_context_lookup(field_tag, RW.Write),
Expand Down Expand Up @@ -195,5 +198,5 @@ def call(instruction: Instruction):
is_create=Transition.to(False),
code_source=Transition.to(callee_code_hash),
gas_left=Transition.to(callee_gas_left),
state_write_counter=Transition.to(2),
reversible_write_counter=Transition.to(2),
)
2 changes: 1 addition & 1 deletion src/zkevm_specs/evm/execution/copy_code_to_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ def copy_code_to_memory(instruction: Instruction):
program_counter=Transition.same(),
stack_pointer=Transition.same(),
memory_size=Transition.same(),
state_write_counter=Transition.same(),
reversible_write_counter=Transition.same(),
)
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 @@ -52,5 +52,5 @@ def copy_to_memory(instruction: Instruction):
program_counter=Transition.same(),
stack_pointer=Transition.same(),
memory_size=Transition.same(),
state_write_counter=Transition.same(),
reversible_write_counter=Transition.same(),
)
4 changes: 2 additions & 2 deletions src/zkevm_specs/evm/execution/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def sload(instruction: Instruction):
rw_counter=Transition.delta(8),
program_counter=Transition.delta(1),
stack_pointer=Transition.delta(0),
state_write_counter=Transition.delta(1),
reversible_write_counter=Transition.delta(1),
dynamic_gas_cost=dynamic_gas_cost,
)

Expand Down Expand Up @@ -133,6 +133,6 @@ def sstore(instruction: Instruction):
rw_counter=Transition.delta(9),
program_counter=Transition.delta(1),
stack_pointer=Transition.delta(2),
state_write_counter=Transition.delta(3),
reversible_write_counter=Transition.delta(3),
dynamic_gas_cost=dynamic_gas_cost,
)
22 changes: 11 additions & 11 deletions src/zkevm_specs/evm/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,21 @@ def to(to: Union[int, FQ, RLC]):
class ReversionInfo:
rw_counter_end_of_reversion: FQ
is_persistent: FQ
state_write_counter: FQ
reversible_write_counter: FQ

def __init__(
self,
rw_counter_end_of_reversion: Expression,
is_persistent: Expression,
state_write_counter: Expression,
reversible_write_counter: Expression,
) -> None:
self.rw_counter_end_of_reversion = rw_counter_end_of_reversion.expr()
self.is_persistent = is_persistent.expr()
self.state_write_counter = state_write_counter.expr()
self.reversible_write_counter = reversible_write_counter.expr()

def rw_counter_of_reversion(self) -> FQ:
rw_counter_of_reversion = self.rw_counter_end_of_reversion - self.state_write_counter
self.state_write_counter += 1
rw_counter_of_reversion = self.rw_counter_end_of_reversion - self.reversible_write_counter
self.reversible_write_counter += 1
return rw_counter_of_reversion


Expand Down Expand Up @@ -168,7 +168,7 @@ def constrain_step_state_transition(self, **kwargs: Transition):
"stack_pointer",
"gas_left",
"memory_size",
"state_write_counter",
"reversible_write_counter",
"log_id",
]
)
Expand Down Expand Up @@ -210,7 +210,7 @@ def step_state_transition_to_new_context(
is_create: Transition,
code_source: Transition,
gas_left: Transition,
state_write_counter: Transition,
reversible_write_counter: Transition,
):
self.constrain_step_state_transition(
rw_counter=rw_counter,
Expand All @@ -219,7 +219,7 @@ def step_state_transition_to_new_context(
is_create=is_create,
code_source=code_source,
gas_left=gas_left,
state_write_counter=state_write_counter,
reversible_write_counter=reversible_write_counter,
# Initailization unconditionally
program_counter=Transition.to(0),
stack_pointer=Transition.to(1024),
Expand All @@ -233,7 +233,7 @@ def step_state_transition_in_same_context(
program_counter: Transition = Transition.same(),
stack_pointer: Transition = Transition.same(),
memory_size: Transition = Transition.same(),
state_write_counter: Transition = Transition.same(),
reversible_write_counter: Transition = Transition.same(),
dynamic_gas_cost: IntOrFQ = 0,
log_id: Transition = Transition.same(),
):
Expand All @@ -248,7 +248,7 @@ def step_state_transition_in_same_context(
stack_pointer=stack_pointer,
gas_left=Transition.delta(-gas_cost),
memory_size=memory_size,
state_write_counter=state_write_counter,
reversible_write_counter=reversible_write_counter,
log_id=log_id,
# Always stay same
call_id=Transition.same(),
Expand Down Expand Up @@ -577,7 +577,7 @@ def reversion_info(self, call_id: Expression = None) -> ReversionInfo:
return ReversionInfo(
rw_counter_end_of_reversion,
is_persistent,
self.curr.state_write_counter if call_id is None else FQ(0),
self.curr.reversible_write_counter if call_id is None else FQ(0),
)

def stack_pop(self) -> RLC:
Expand Down
6 changes: 3 additions & 3 deletions src/zkevm_specs/evm/step.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class StepState:
# The following fields could be further moved into rw_table if we find them
# not often used.
memory_size: FQ
state_write_counter: FQ
reversible_write_counter: FQ

# log index of current tx/receipt, this field maybe moved if we find them
# not often used.
Expand All @@ -60,7 +60,7 @@ def __init__(
stack_pointer: int = 1024,
gas_left: int = 0,
memory_size: int = 0,
state_write_counter: int = 0,
reversible_write_counter: int = 0,
log_id: int = 0,
aux_data: Any = None,
) -> None:
Expand All @@ -74,7 +74,7 @@ def __init__(
self.stack_pointer = FQ(stack_pointer)
self.gas_left = FQ(gas_left)
self.memory_size = FQ(memory_size)
self.state_write_counter = FQ(state_write_counter)
self.reversible_write_counter = FQ(reversible_write_counter)
self.log_id = FQ(log_id)
self.aux_data = aux_data

Expand Down
2 changes: 1 addition & 1 deletion src/zkevm_specs/evm/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class CallContextFieldTag(IntEnum):
StackPointer = auto()
GasLeft = auto()
MemorySize = auto()
StateWriteCounter = auto()
ReversibleWriteCounter = auto()


class TxLogFieldTag(IntEnum):
Expand Down
2 changes: 1 addition & 1 deletion tests/evm/test_begin_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def test_begin_tx(tx: Transaction, callee: Account, is_success: bool):
program_counter=0,
stack_pointer=1024,
gas_left=0,
state_write_counter=2,
reversible_write_counter=2,
),
],
begin_with_first_step=True,
Expand Down
16 changes: 8 additions & 8 deletions tests/evm/test_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"is_persistent",
"gas_left",
"memory_size",
"state_write_counter",
"reversible_write_counter",
],
defaults=[0, True, 0, 0, 2],
)
Expand Down Expand Up @@ -102,7 +102,7 @@ def gen_testing_data():
]
call_contexts = [
CallContext(gas_left=100000, is_persistent=True),
CallContext(gas_left=100000, is_persistent=True, memory_size=8, state_write_counter=5),
CallContext(gas_left=100000, is_persistent=True, memory_size=8, reversible_write_counter=5),
CallContext(gas_left=100000, is_persistent=False, rw_counter_end_of_reversion=88),
]
stacks = [
Expand Down Expand Up @@ -175,7 +175,7 @@ def test_call(
80
if is_reverted_by_callee
else (
caller_ctx.rw_counter_end_of_reversion - (caller_ctx.state_write_counter + 1)
caller_ctx.rw_counter_end_of_reversion - (caller_ctx.reversible_write_counter + 1)
if is_reverted_by_caller
else 0
)
Expand All @@ -198,7 +198,7 @@ def test_call(
.stack_read(1, 1022, RLC(stack.rd_offset, randomness))
.stack_read(1, 1023, RLC(stack.rd_length, randomness))
.stack_write(1, 1023, RLC(is_success, randomness))
.tx_access_list_account_write(1, callee.address, True, is_warm_access, rw_counter_of_reversion=None if caller_ctx.is_persistent else caller_ctx.rw_counter_end_of_reversion - caller_ctx.state_write_counter)
.tx_access_list_account_write(1, callee.address, True, is_warm_access, rw_counter_of_reversion=None if caller_ctx.is_persistent else caller_ctx.rw_counter_end_of_reversion - caller_ctx.reversible_write_counter)
.call_context_read(24, CallContextFieldTag.RwCounterEndOfReversion, callee_rw_counter_end_of_reversion)
.call_context_read(24, CallContextFieldTag.IsPersistent, callee_is_persistent)
.account_write(caller.address, AccountFieldTag.Balance, caller_balance, caller_balance_prev, rw_counter_of_reversion=None if callee_is_persistent else callee_rw_counter_end_of_reversion)
Expand All @@ -220,7 +220,7 @@ def test_call(
.call_context_write(1, CallContextFieldTag.StackPointer, 1023) \
.call_context_write(1, CallContextFieldTag.GasLeft, expected.caller_gas_left) \
.call_context_write(1, CallContextFieldTag.MemorySize, expected.next_memory_size) \
.call_context_write(1, CallContextFieldTag.StateWriteCounter, caller_ctx.state_write_counter + 1) \
.call_context_write(1, CallContextFieldTag.ReversibleWriteCounter, caller_ctx.reversible_write_counter + 1) \
.call_context_read(24, CallContextFieldTag.CallerId, 1) \
.call_context_read(24, CallContextFieldTag.TxId, 1) \
.call_context_read(24, CallContextFieldTag.Depth, 2) \
Expand Down Expand Up @@ -268,7 +268,7 @@ def test_call(
stack_pointer=1017,
gas_left=caller_ctx.gas_left,
memory_size=caller_ctx.memory_size,
state_write_counter=caller_ctx.state_write_counter,
reversible_write_counter=caller_ctx.reversible_write_counter,
),
(
StepState(
Expand All @@ -282,7 +282,7 @@ def test_call(
stack_pointer=1023,
gas_left=expected.caller_gas_left,
memory_size=expected.next_memory_size,
state_write_counter=caller_ctx.state_write_counter + 3,
reversible_write_counter=caller_ctx.reversible_write_counter + 3,
)
if callee.code_hash() == EMPTY_CODE_HASH
else StepState(
Expand All @@ -297,7 +297,7 @@ def test_call(
program_counter=0,
stack_pointer=1024,
gas_left=expected.callee_gas_left,
state_write_counter=2,
reversible_write_counter=2,
)
),
],
Expand Down
2 changes: 1 addition & 1 deletion tests/evm/test_end_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_end_tx(tx: Transaction, gas_left: int, refund: int, is_last_tx: bool):
program_counter=0,
stack_pointer=1024,
gas_left=gas_left,
state_write_counter=2,
reversible_write_counter=2,
),
StepState(
execution_state=ExecutionState.EndBlock if is_last_tx else ExecutionState.BeginTx,
Expand Down
4 changes: 2 additions & 2 deletions tests/evm/test_extcodehash.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_extcodehash(
call_id = 1

rw_counter_end_of_reversion = 0 if is_persistent else 9
state_write_counter = 0
reversible_write_counter = 0

rw_table = set(
RWDictionary(1)
Expand All @@ -81,7 +81,7 @@ def test_extcodehash(
address,
True,
is_warm,
rw_counter_of_reversion=rw_counter_end_of_reversion - state_write_counter,
rw_counter_of_reversion=rw_counter_end_of_reversion - reversible_write_counter,
)
.account_read(address, AccountFieldTag.Nonce, RLC(nonce, randomness))
.account_read(address, AccountFieldTag.Balance, RLC(balance, randomness))
Expand Down
8 changes: 4 additions & 4 deletions tests/evm/test_sload.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_sload(tx: Transaction, storage_key_be_bytes: bytes, warm: bool, is_pers
value_committed = RLC(0, randomness)

rw_counter_end_of_reversion = 19
state_write_counter = 3
reversible_write_counter = 3

tables = Tables(
block_table=set(Block().table_assignments(randomness)),
Expand All @@ -70,7 +70,7 @@ def test_sload(tx: Transaction, storage_key_be_bytes: bytes, warm: bool, is_pers
.stack_read(1, 1023, storage_key)
.account_storage_read(tx.callee_address, storage_key, value, tx.id, value_committed)
.stack_write(1, 1023, value)
.tx_access_list_account_storage_write(tx.id, tx.callee_address, storage_key, 1, 1 if warm else 0, rw_counter_of_reversion=None if is_persistent else rw_counter_end_of_reversion - state_write_counter)
.tx_access_list_account_storage_write(tx.id, tx.callee_address, storage_key, 1, 1 if warm else 0, rw_counter_of_reversion=None if is_persistent else rw_counter_end_of_reversion - reversible_write_counter)
.rws
# fmt: on
),
Expand All @@ -89,7 +89,7 @@ def test_sload(tx: Transaction, storage_key_be_bytes: bytes, warm: bool, is_pers
code_source=bytecode_hash,
program_counter=33,
stack_pointer=1023,
state_write_counter=state_write_counter,
reversible_write_counter=reversible_write_counter,
gas_left=WARM_STORAGE_READ_COST if warm else COLD_SLOAD_COST,
),
StepState(
Expand All @@ -101,7 +101,7 @@ def test_sload(tx: Transaction, storage_key_be_bytes: bytes, warm: bool, is_pers
code_source=bytecode_hash,
program_counter=34,
stack_pointer=1023,
state_write_counter=state_write_counter + 1,
reversible_write_counter=reversible_write_counter + 1,
gas_left=0,
),
],
Expand Down
4 changes: 2 additions & 2 deletions tests/evm/test_sstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def test_sstore(
code_source=bytecode_hash,
program_counter=66,
stack_pointer=1022,
state_write_counter=0,
reversible_write_counter=0,
gas_left=expected_gas_cost,
),
StepState(
Expand All @@ -177,7 +177,7 @@ def test_sstore(
code_source=bytecode_hash,
program_counter=67,
stack_pointer=1024,
state_write_counter=3,
reversible_write_counter=3,
gas_left=0,
),
],
Expand Down