Skip to content

Commit b40fd7b

Browse files
authored
KEVMSemantics: refactor custom_step and can_make_custom_step (#2662)
1 parent 622dc98 commit b40fd7b

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

kevm-pyk/src/kevm_pyk/kevm.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,10 @@ def _replace(term: KInner) -> KInner:
146146
return CTerm(config=bottom_up(_replace, cterm.config), constraints=cterm.constraints)
147147

148148
def custom_step(self, cterm: CTerm, _cterm_symbolic: CTermSymbolic) -> KCFGExtendResult | None:
149-
"""Given a CTerm, update the JUMPDESTS_CELL and PROGRAM_CELL if the rule 'EVM.program.load' is at the top of the K_CELL.
150-
151-
:param cterm: CTerm of a proof node.
152-
:return: If the K_CELL matches the load_pattern, a Step with depth 1 is returned together with the new configuration, also registering that the `EVM.program.load` rule has been applied. Otherwise, None is returned.
153-
"""
154-
if self.can_make_custom_step(cterm):
155-
subst = self._cached_subst
156-
assert subst is not None
157-
bytecode_sections = flatten_label('_+Bytes__BYTES-HOOKED_Bytes_Bytes_Bytes', subst['###BYTECODE'])
158-
jumpdests_set = compute_jumpdests(bytecode_sections)
159-
new_cterm = CTerm.from_kast(set_cell(cterm.kast, 'JUMPDESTS_CELL', jumpdests_set))
160-
new_cterm = CTerm.from_kast(set_cell(new_cterm.kast, 'PROGRAM_CELL', subst['###BYTECODE']))
161-
new_cterm = CTerm.from_kast(set_cell(new_cterm.kast, 'K_CELL', KSequence(subst['###CONTINUATION'])))
162-
return Step(new_cterm, 1, (), ['EVM.program.load'], cut=True)
163-
return None
149+
if self._check_load_pattern(cterm):
150+
return self._exec_load_custom_step(cterm)
151+
else:
152+
return None
164153

165154
@staticmethod
166155
def cut_point_rules(
@@ -208,7 +197,7 @@ def terminal_rules(break_every_step: bool) -> list[str]:
208197
terminal_rules.append('EVM.step')
209198
return terminal_rules
210199

211-
def can_make_custom_step(self, cterm: CTerm) -> bool:
200+
def _check_load_pattern(self, cterm: CTerm) -> bool:
212201
"""Given a CTerm, check if the rule 'EVM.program.load' is at the top of the K_CELL.
213202
214203
This method checks if the `EVM.program.load` rule is at the top of the `K_CELL` in the given `cterm`.
@@ -220,6 +209,24 @@ def can_make_custom_step(self, cterm: CTerm) -> bool:
220209
self._cached_subst = load_pattern.match(cterm.cell('K_CELL'))
221210
return self._cached_subst is not None
222211

212+
def _exec_load_custom_step(self, cterm: CTerm) -> KCFGExtendResult:
213+
"""Given a CTerm, update the JUMPDESTS_CELL and PROGRAM_CELL if the rule 'EVM.program.load' is at the top of the K_CELL.
214+
215+
:param cterm: CTerm of a proof node.
216+
:return: If the K_CELL matches the load_pattern, a Step with depth 1 is returned together with the new configuration, also registering that the `EVM.program.load` rule has been applied.
217+
"""
218+
subst = self._cached_subst
219+
assert subst is not None
220+
bytecode_sections = flatten_label('_+Bytes__BYTES-HOOKED_Bytes_Bytes_Bytes', subst['###BYTECODE'])
221+
jumpdests_set = compute_jumpdests(bytecode_sections)
222+
new_cterm = CTerm.from_kast(set_cell(cterm.kast, 'JUMPDESTS_CELL', jumpdests_set))
223+
new_cterm = CTerm.from_kast(set_cell(new_cterm.kast, 'PROGRAM_CELL', subst['###BYTECODE']))
224+
new_cterm = CTerm.from_kast(set_cell(new_cterm.kast, 'K_CELL', KSequence(subst['###CONTINUATION'])))
225+
return Step(new_cterm, 1, (), ['EVM.program.load'], cut=True)
226+
227+
def can_make_custom_step(self, cterm: CTerm) -> bool:
228+
return self._check_load_pattern(cterm)
229+
223230
def is_mergeable(self, ct1: CTerm, ct2: CTerm) -> bool:
224231
"""Given two CTerms of Edges' targets, check if they are mergeable.
225232

0 commit comments

Comments
 (0)