Skip to content

Commit 4709f66

Browse files
authored
Support gas cost summarization for all supported opcode. (#2727)
* Support gas cost summarization for all supported opcode. - Updated the handling of `GAS_CELL` to ensure it uses infinite gas for SLOAD and SSTORE operations. - Adjusted the initialization of `STATIC_CELL` for the `SSTORE` opcode to improve clarity. - Increased the `max_iterations` parameter in the summarization process to 300 to simplify the analysis of error cases. - Simplified the `#accessStorage` rule in the EVM semantics to avoid unexpected (nd)branches during gas consumption summarization. * make format
1 parent a469735 commit 4709f66

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

kevm-pyk/src/kevm_pyk/kproj/evm-semantics/evm.md

+3-7
Original file line numberDiff line numberDiff line change
@@ -1597,16 +1597,11 @@ The various `CALL*` (and other inter-contract control flow) operations will be d
15971597
syntax KItem ::= "#accessStorage" Account Int
15981598
// ---------------------------------------------
15991599
rule <k> #accessStorage ACCT INDEX => .K ... </k>
1600-
<accessedStorage> ... ACCT |-> (TS:Set => TS |Set SetItem(INDEX)) ... </accessedStorage>
1600+
<accessedStorage> TS => TS[ACCT <- {TS[ACCT] orDefault .Set}:>Set |Set SetItem(INDEX)] </accessedStorage>
16011601
<schedule> SCHED </schedule>
16021602
requires Ghasaccesslist << SCHED >>
16031603
[preserves-definedness]
16041604
1605-
rule <k> #accessStorage ACCT INDEX => .K ... </k>
1606-
<accessedStorage> TS => TS[ACCT <- SetItem(INDEX)] </accessedStorage>
1607-
<schedule> SCHED </schedule>
1608-
requires Ghasaccesslist << SCHED >> andBool notBool ACCT in_keys(TS)
1609-
16101605
rule <k> #accessStorage _ _ => .K ... </k>
16111606
<schedule> SCHED </schedule>
16121607
requires notBool Ghasaccesslist << SCHED >>
@@ -2310,7 +2305,8 @@ Access List Gas
23102305
<schedule> SCHED </schedule>
23112306
requires Ghasaccesslist << SCHED >> andBool #usesAccessList(OP)
23122307
2313-
rule <k> #access [ _ , _ ] => .K ... </k> <schedule> _ </schedule> [owise]
2308+
rule <k> #access [ OP , _ ] => .K ... </k> <schedule> SCHED </schedule>
2309+
requires notBool (Ghasaccesslist << SCHED >> andBool #usesAccessList(OP))
23142310
23152311
syntax InternalOp ::= #gasAccess ( Schedule, OpCode ) [symbol(#gasAccess)]
23162312
// --------------------------------------------------------------------------

kevm-pyk/src/kevm_pyk/summarizer.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,7 @@ def _build_spec(
433433
# This is because #push doesn't handle `.Account`. And it's okay to be Int for other opcodes.
434434
_init_subst['CALLER_CELL'] = KVariable('CALLER_CELL', KSort('Int')) # CALLER_CELL should be Int for CALLER.
435435
_init_subst['ORIGIN_CELL'] = KVariable('ORIGIN_CELL', KSort('Int')) # ORIGIN_CELL should be Int for ORIGIN.
436-
inf_gas_cell = KEVM.inf_gas(KVariable('GAS_CELL', KSort('Gas')))
437-
_init_subst['GAS_CELL'] = inf_gas_cell # inf_gas to reduce the computation cost.
436+
_init_subst['GAS_CELL'] = KEVM.inf_gas(KVariable('GAS_CELL', 'Gas')) # SLOAD & SSTORE must use infinite gas.
438437
_init_subst.update(init_map)
439438
init_subst = CSubst(Subst(_init_subst), init_constraints)
440439

@@ -467,6 +466,9 @@ def _le(a: KInner, b: KInner) -> KInner:
467466
init_subst: dict[str, KInner] = {}
468467
final_subst: dict[str, KInner] = {}
469468

469+
if opcode_symbol == 'SSTORE':
470+
init_subst['STATIC_CELL'] = KToken('false', KSort('Bool'))
471+
470472
if opcode_symbol in NOT_USEGAS_OPCODES:
471473
# TODO: Should allow infGas to calculate gas. Skip for now.
472474
init_subst['USEGAS_CELL'] = KToken('false', KSort('Bool'))
@@ -572,7 +574,7 @@ def create_kcfg_explore() -> KCFGExplore:
572574
proof,
573575
create_kcfg_explore=create_kcfg_explore,
574576
max_depth=1,
575-
max_iterations=None,
577+
max_iterations=300,
576578
cut_point_rules=KEVMSemantics.cut_point_rules(
577579
break_on_jumpi=False,
578580
break_on_jump=False,

0 commit comments

Comments
 (0)