Skip to content

Commit 048d31b

Browse files
committed
back up to keep my thinking of debuging
1 parent b8343fc commit 048d31b

File tree

5 files changed

+110
-6
lines changed

5 files changed

+110
-6
lines changed

kevm-pyk/src/kevm_pyk/__main__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from pyk.proof.tui import APRProofViewer
3333
from pyk.utils import FrozenDict, hash_str, single
3434

35-
from kevm_pyk.summarizer import get_todo_list, summarize
35+
from kevm_pyk.summarizer import KEVMSummarizer, get_todo_list, summarize
3636

3737
from . import VERSION, config
3838
from .cli import _create_argument_parser, generate_options, get_argument_type_setter, get_option_string_destination
@@ -637,10 +637,18 @@ def exec_kast(options: KastOptions) -> None:
637637

638638

639639
def exec_summarize(options: ProveOptions) -> None:
640+
proof_dir = Path(__file__).parent / 'proofs'
641+
save_directory = Path(__file__).parent / 'summaries'
642+
p = APRProof.read_proof_data(Path(__file__).parent / 'proofs', 'STOP-SUMMARY-16-TO-10')
643+
# print('proof initial state -------------------------------')
644+
# print(p.kcfg.node(p.init).cterm)
645+
summarizer = KEVMSummarizer(proof_dir, save_directory)
646+
summarizer.explore(p)
647+
640648
# batch_summarize()
641-
next_opcode = get_todo_list()
642-
_LOGGER.info(f'summarizing {next_opcode}')
643-
summarize(next_opcode[0])
649+
# next_opcode = get_todo_list()
650+
# _LOGGER.info(f'summarizing {next_opcode}')
651+
# summarize(next_opcode[0])
644652
# analyze_proof('CREATE_3', 3)
645653

646654

kevm-pyk/src/kevm_pyk/summarizer.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ def build_spec(self, opcode: KApply, stack_needed: int, id_str: str = '') -> APR
471471
_LOGGER.debug(proof.kcfg.nodes[0].cterm.to_dict())
472472
return proof
473473

474-
def explore(self, proof: APRProof) -> None:
474+
def explore(self, proof: APRProof) -> bool:
475475
"""
476476
Execute the specification to explore the KCFG for all possible instructions.
477477
"""
@@ -568,6 +568,7 @@ def create_kcfg_explore() -> KCFGExplore:
568568
for line in res_lines:
569569
f.write(line)
570570
f.write('\n')
571+
return passed
571572

572573
def summarize(self, proof: APRProof, merge: bool = False) -> None:
573574
# TODO: need customized minimization rules, maybe
@@ -625,12 +626,13 @@ def batch_summarize(num_processes: int = 4) -> None:
625626
_LOGGER.info('Batch summarization completed')
626627

627628

628-
def summarize(opcode_symbol: str) -> None:
629+
def summarize(opcode_symbol: str) -> tuple[KEVMSummarizer, list[APRProof]]:
629630
proof_dir = Path(__file__).parent / 'proofs'
630631
save_directory = Path(__file__).parent / 'summaries'
631632
summarizer = KEVMSummarizer(proof_dir, save_directory)
632633
needs = stack_needed(opcode_symbol)
633634
opcode = OPCODES[opcode_symbol]
635+
proofs = []
634636
for need in needs:
635637
if len(needs) > 1:
636638
opcode = KApply(opcode.label.name, KToken(str(need), KSort('Int')))
@@ -642,6 +644,8 @@ def summarize(opcode_symbol: str) -> None:
642644
)
643645
summarizer.explore(proof)
644646
summarizer.summarize(proof)
647+
proof.write_proof_data()
648+
proofs.append(proof)
645649

646650
proof = summarizer.build_spec(opcode, need, id_str='_TRUE')
647651
_subst = {'K_CELL': KSequence([KApply('#endBasicBlock_EVM_InternalOp'), KVariable('K_CELL')])}
@@ -653,15 +657,22 @@ def summarize(opcode_symbol: str) -> None:
653657
proof.kcfg.let_node(2, cterm=subst(proof.kcfg.get_node(2).cterm), attrs=proof.kcfg.get_node(2).attrs)
654658
summarizer.explore(proof)
655659
summarizer.summarize(proof)
660+
proof.write_proof_data()
661+
proofs.append(proof)
656662
elif opcode_symbol == 'LOG':
657663
need += 2
658664
proof = summarizer.build_spec(opcode, need)
659665
summarizer.explore(proof)
660666
summarizer.summarize(proof)
667+
proof.write_proof_data()
668+
proofs.append(proof)
661669
else:
662670
proof = summarizer.build_spec(opcode, need)
663671
summarizer.explore(proof)
664672
summarizer.summarize(proof)
673+
proof.write_proof_data()
674+
proofs.append(proof)
675+
return summarizer, proofs
665676
# summarizer.analyze_proof(proof_dir / 'STOP_SPEC')
666677
# validation: generate them as claims and call kevm prove.
667678

@@ -671,3 +682,10 @@ def analyze_proof(opcode: str, node_id: int) -> None:
671682
save_directory = Path(__file__).parent / 'summaries'
672683
summarizer = KEVMSummarizer(proof_dir, save_directory)
673684
summarizer.analyze_proof(proof_dir / f'{opcode}_SPEC', node_id)
685+
686+
def debug_proof(proof_id: str) -> None:
687+
proof_dir = Path(__file__).parent / 'proofs'
688+
save_directory = Path(__file__).parent / 'summaries'
689+
summarizer = KEVMSummarizer(proof_dir, save_directory)
690+
proof = APRProof.read_proof_data(proof_dir, proof_id)
691+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from ast import Import
2+
from kevm_pyk.summarizer import OPCODES, get_summary_status, OPCODES_SUMMARY_STATUS, summarize
3+
import pytest
4+
from pyk.kcfg import KCFG
5+
from pyk.kast.inner import KVariable, Subst
6+
from pyk.kast.outer import KFlatModule, KRequire, KImport
7+
from pyk.kast.att import Atts
8+
from pyk.cterm import CSubst
9+
from pyk.proof.reachability import APRProof
10+
11+
@pytest.mark.parametrize('opcode', OPCODES.keys())
12+
def test_summarize(opcode: str) -> None:
13+
if get_summary_status(opcode) != 'PASSED':
14+
pytest.skip(f'{opcode} status: {OPCODES_SUMMARY_STATUS[opcode]}')
15+
if opcode == 'STOP':
16+
summarizer, proofs = summarize(opcode)
17+
for proof in proofs:
18+
claims = []
19+
20+
# no pending, failing, bounded nodes in the proof
21+
for leaf in proof.kcfg.leaves:
22+
assert not proof.is_pending(leaf.id)
23+
assert not proof.kcfg.is_stuck(leaf.id)
24+
assert not proof.kcfg.is_vacuous(leaf.id)
25+
assert not proof.is_bounded(leaf.id)
26+
27+
# only one successor from the initial node in the proof
28+
successors = proof.kcfg.successors(proof.init)
29+
assert len(successors) == 1
30+
successor = successors[0]
31+
32+
# only one edge to the terminal node
33+
if isinstance(successor, KCFG.Split):
34+
targets = successor.targets
35+
assert len(proof.kcfg.edges()) == len(targets)
36+
for target in targets:
37+
successors = proof.kcfg.successors(target.id)
38+
assert len(successors) == 1
39+
edge = successors[0]
40+
assert isinstance(edge, KCFG.Edge)
41+
assert proof.is_terminal(edge.target.id)
42+
claims.append(edge.to_rule(f'{opcode}-SUMMARY', claim = True))
43+
else:
44+
assert len(proof.kcfg.edges()) == 1
45+
assert isinstance(successor, KCFG.Edge)
46+
assert proof.is_terminal(successor.target.id)
47+
claims.append(successor.to_rule(f'{opcode}-SUMMARY', claim = True))
48+
49+
# prove the correctness of the summary
50+
# KRequire('edsl.md'),
51+
# module = KFlatModule(f'{opcode}-SUMMARY', claims)
52+
# with open(summarizer.save_directory / proof.id / f'{opcode.lower()}-summary.k', 'w') as f:
53+
# res_lines = 'requires "edsl.md"\n'
54+
# res_lines += summarizer.kevm.pretty_print(module)
55+
# splited_lines = res_lines.split('\n')
56+
# splited_lines.insert(2, ' imports public EDSL')
57+
# res_lines = '\n'.join(splited_lines)
58+
# f.write(res_lines)
59+
60+
# int = 0
61+
# print(len(claims))
62+
# proof_result = summarizer.explore(APRProof.from_claim(summarizer.kevm.definition, claims[2], {}, summarizer.proof_dir))
63+
# print(proof_result)
64+
for claim in claims:
65+
assert summarizer.explore(APRProof.from_claim(summarizer.kevm.definition, claim, {}, summarizer.proof_dir))
66+
# # TODO: fix the sort issue caused by `APRProof.from_claim`
67+
# # print(f'validating {claim.att[Atts.LABEL]}')
68+
# # _subst = {'_USEGAS_CELL': KVariable('_USEGAS_CELL', sort = 'Bool')}
69+
# # subst = CSubst(Subst(_subst), ())
70+
# summary_proof = APRProof.from_claim(summarizer.kevm.definition, claim, {}, summarizer.proof_dir)
71+
# # summary_proof.kcfg.let_node(1, cterm = subst(summary_proof.kcfg.get_node(1).cterm), attrs = summary_proof.kcfg.get_node(1).attrs)
72+
# proof_result = summarizer.explore(summary_proof)
73+
# # print(f'{claim.att[Atts.LABEL]} proof result: {proof_result}')
74+
# assert proof_result
75+
else:
76+
pytest.skip(f'{opcode} ignored')

kevm-pyk/src/tests/integration/test_summary.py

Whitespace-only changes.

xx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
new_init_constraints: [KApply(label=KLabel(name='#Equals', params=(KSort(name='Bool'), KSort(name='GeneratedTopCell'))), args=(KToken(token='true', sort=KSort(name='Bool')), KVariable(name='_USEGAS_CELL', sort=KSort(name='Bool')))), KApply(label=KLabel(name='#Equals', params=(KSort(name='Bool'), KSort(name='GeneratedTopCell'))), args=(KToken(token='true', sort=KSort(name='Bool')), KApply(label=KLabel(name='_<=Gas__GAS-SYNTAX_Bool_Gas_Gas', params=()), args=(KApply(label=KLabel(name='_<_>_SCHEDULE_Int_ScheduleConst_Schedule', params=()), args=(KApply(label=KLabel(name='Gzero_SCHEDULE_ScheduleConst', params=()), args=()), KVariable(name='SCHEDULE_CELL', sort=KSort(name='Schedule')))), KVariable(name='GAS_CELL', sort=KSort(name='Gas'))))))]
2+
Proof timing STOP-SUMMARY-16-TO-10: 59.61101531982422s

0 commit comments

Comments
 (0)