Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide KEVMSummarizer to sammarize rules for all the instruction rules #2676

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions kevm-pyk/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/dist/
__pycache__/
.coverage
proofs/
324 changes: 202 additions & 122 deletions kevm-pyk/poetry.lock

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions kevm-pyk/src/kevm_pyk/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
from pyk.proof.tui import APRProofViewer
from pyk.utils import FrozenDict, hash_str, single

from kevm_pyk.summarizer import analyze_proof, batch_summarize, summarize

from . import VERSION, config
from .cli import _create_argument_parser, generate_options, get_argument_type_setter, get_option_string_destination
from .gst_to_kore import SORT_ETHEREUM_SIMULATION, gst_to_kore, kore_pgm_to_kore
Expand Down Expand Up @@ -633,6 +635,12 @@ def exec_kast(options: KastOptions) -> None:
print(output_text)


def exec_summarize(options: ProveOptions) -> None:
# batch_summarize()
summarize('STOP')
# analyze_proof('STOP', 11)


# Helpers


Expand Down
23 changes: 23 additions & 0 deletions kevm-pyk/src/kevm_pyk/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def generate_options(args: dict[str, Any]) -> LoggingOptions:
return KastOptions(args)
case 'run':
return RunOptions(args)
case 'summarize':
return ProveOptions(args)
case _:
raise ValueError(f'Unrecognized command: {command}')

Expand All @@ -99,6 +101,8 @@ def get_option_string_destination(command: str, option_string: str) -> str:
option_string_destinations = KastOptions.from_option_string()
case 'run':
option_string_destinations = RunOptions.from_option_string()
case 'summarize':
option_string_destinations = ProveOptions.from_option_string()

return option_string_destinations.get(option_string, option_string.replace('-', '_'))

Expand Down Expand Up @@ -127,6 +131,8 @@ def func(par: str) -> str:
option_types = KastOptions.get_argument_type()
case 'run':
option_types = RunOptions.get_argument_type()
case 'summarize':
option_types = ProveOptions.get_argument_type()

return option_types.get(option_string, func)

Expand Down Expand Up @@ -183,6 +189,23 @@ def _create_argument_parser() -> ArgumentParser:
help='Maximum worker threads to use on a single proof to explore separate branches in parallel.',
)

command_parser.add_parser(
'summarize',
help='Summarize an Opcode.',
parents=[
kevm_cli_args.logging_args,
kevm_cli_args.parallel_args,
kevm_cli_args.k_args,
kevm_cli_args.kprove_args,
kevm_cli_args.rpc_args,
kevm_cli_args.bug_report_args,
kevm_cli_args.smt_args,
kevm_cli_args.explore_args,
# kevm_cli_args.spec_args,
config_args.config_args,
],
)

prune_args = command_parser.add_parser(
'prune',
help='Remove a node and its successors from the proof state.',
Expand Down
2 changes: 2 additions & 0 deletions kevm-pyk/src/kevm_pyk/kproj/evm-semantics/edsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ requires "hashed-locations.md"
requires "abi.md"
requires "gas.md"
requires "optimizations.md"
requires "lemmas/summary-simplification.k"

module EDSL
imports BUF
Expand All @@ -21,6 +22,7 @@ module EDSL
imports EVM-OPTIMIZATIONS
imports INFINITE-GAS
imports BIN-RUNTIME
imports SUMMARY-SIMPLIFICATION
endmodule

module BIN-RUNTIME
Expand Down
12 changes: 6 additions & 6 deletions kevm-pyk/src/kevm_pyk/kproj/evm-semantics/evm.md
Original file line number Diff line number Diff line change
Expand Up @@ -1886,9 +1886,9 @@ Precompiled Contracts
syntax InternalOp ::= #ecadd(G1Point, G1Point) [symbol(#ecadd)]
// ---------------------------------------------------------------
rule <k> #ecadd(P1, P2) => #end EVMC_PRECOMPILE_FAILURE ... </k>
requires notBool isValidPoint(P1) orBool notBool isValidPoint(P2)
requires notBool isValidPointWrapper(P1) orBool notBool isValidPointWrapper(P2)
rule <k> #ecadd(P1, P2) => #end EVMC_SUCCESS ... </k> <output> _ => #point(BN128Add(P1, P2)) </output>
requires isValidPoint(P1) andBool isValidPoint(P2)
requires isValidPointWrapper(P1) andBool isValidPointWrapper(P2)

syntax PrecompiledOp ::= "ECMUL"
// --------------------------------
Expand All @@ -1898,9 +1898,9 @@ Precompiled Contracts
syntax InternalOp ::= #ecmul(G1Point, Int) [symbol(#ecmul)]
// -----------------------------------------------------------
rule <k> #ecmul(P, _S) => #end EVMC_PRECOMPILE_FAILURE ... </k>
requires notBool isValidPoint(P)
requires notBool isValidPointWrapper(P)
rule <k> #ecmul(P, S) => #end EVMC_SUCCESS ... </k> <output> _ => #point(BN128Mul(P, S)) </output>
requires isValidPoint(P)
requires isValidPointWrapper(P)

syntax Bytes ::= #point ( G1Point ) [symbol(#point), function]
// --------------------------------------------------------------
Expand All @@ -1925,9 +1925,9 @@ Precompiled Contracts
syntax InternalOp ::= "#checkPoint"
// -----------------------------------
rule <k> (#checkPoint => .K) ~> #ecpairing(ListItem(AK::G1Point) _, ListItem(BK::G2Point) _, _, _, _) ... </k>
requires isValidPoint(AK) andBool isValidPoint(BK)
requires isValidPointWrapper(AK) andBool isValidPointWrapper(BK)
rule <k> #checkPoint ~> #ecpairing(ListItem(AK::G1Point) _, ListItem(BK::G2Point) _, _, _, _) => #end EVMC_PRECOMPILE_FAILURE ... </k>
requires notBool isValidPoint(AK) orBool notBool isValidPoint(BK)
requires notBool isValidPointWrapper(AK) orBool notBool isValidPointWrapper(BK)

syntax PrecompiledOp ::= "BLAKE2F"
// ----------------------------------
Expand Down
3 changes: 3 additions & 0 deletions kevm-pyk/src/kevm_pyk/kproj/evm-semantics/gas.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,8 @@ module GAS-SIMPLIFICATION [symbolic]
imports BOOL

rule A <Gas B => false requires B <=Gas A [simplification]
rule notBool (A <=Gas B) => B <Gas A [simplification]
rule notBool (A <Gas B) => B <=Gas A [simplification]
endmodule
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
requires "../gas.md"

module SUMMARY-SIMPLIFICATION
imports SUMMARY-SIMPLIFICATION-COMMON
endmodule

module SUMMARY-SIMPLIFICATION-COMMON
imports GAS

// rule { true #Equals ( notBool G0 <Gas G1 ) } #And { true #Equals ( notBool G1 <=Gas G0 ) } => false [simplification]

// ---------------------------------------------------------------------
// simpification rules for stack underflow check
// rule notBool #sizeWordStack ( WORDSTACK:WordStack , 0 ) <Int 1 => { WORDSTACK #Equals ( ?W0:Int : ?WS:WordStack ) } [simplification, symbolic]
// rule notBool #sizeWordStack ( WORDSTACK:WordStack , 0 ) <Int 2 => { WORDSTACK #Equals ( ?W0:Int : ( ?W1:Int : ?WS:WordStack ) ) } [simplification, symbolic]
// rule { true #Equals ( notBool #sizeWordStack ( WORDSTACK:WordStack , 0 ) <Int 2 ) } #And #Not ( #Exists W0:Int . #Exists W1:Int . #Exists WS:WordStack . { WORDSTACK:WordStack #Equals ( W0:Int : ( W1:Int : WS:WordStack ) ) } ) => false [simplification]
// rule notBool #sizeWordStack ( WORDSTACK:WordStack , 0 ) <Int 3 => { WORDSTACK #Equals ( ?W0:Int : ( ?W1:Int : ( ?W2:Int : ?WS:WordStack ) ) ) } [simplification, symbolic]
// rule notBool #sizeWordStack ( WORDSTACK:WordStack , 0 ) <Int 4 => { WORDSTACK #Equals ( ?W0:Int : ( ?W1:Int : ( ?W2:Int : ( ?W3:Int : ?WS:WordStack ) ) ) ) } [simplification, symbolic]

endmodule
10 changes: 10 additions & 0 deletions kevm-pyk/src/kevm_pyk/kproj/evm-semantics/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ module SERIALIZATION
Address/Hash Helpers
--------------------

- `isValidPointWrapper` serves as a wrapper around the `isValidPoint` in `KRYPTO`.

```k
syntax Bool ::= isValidPointWrapper ( G1Point ) [symbol(isValidPointWrapper), function, total, smtlib(smt_krypto_bn128valid)]
| isValidPointWrapper ( G2Point ) [symbol(isValidG2PointWrapper), function, total, smtlib(smt_krypto_bn128g2valid)]
// -----------------------------------------------------------------------------------------------------------------------------
rule [isValidPointWrapper]: isValidPointWrapper(P:G1Point) => isValidPoint(P) [concrete]
rule [isValidG2PointWrapper]: isValidPointWrapper(P:G2Point) => isValidPoint(P) [concrete]
```

- `keccak` serves as a wrapper around the `Keccak256` in `KRYPTO`.

```k
Expand Down
154 changes: 154 additions & 0 deletions kevm-pyk/src/kevm_pyk/summaries/ADDMOD_SPEC/proof-result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
Proof ADDMOD_SPEC Failed

┌─ 1 (root, split, init)
│ k: #next [ ADDMOD ] ~> _K_CELL:K
│ pc: _PC_CELL:Int
│ callDepth: _CALLDEPTH_CELL:Int
│ statusCode: _STATUSCODE_CELL:StatusCode
┃ (branch)
┣━━┓ subst: .Subst
┃ ┃ constraint:
┃ ┃ #sizeWordStack ( _WORDSTACK_CELL:WordStack , 0 ) <Int 3
┃ │
┃ └─ 3 (leaf, pending)
┃ k: #next [ ADDMOD ] ~> _K_CELL:K
┃ pc: _PC_CELL:Int
┃ callDepth: _CALLDEPTH_CELL:Int
┃ statusCode: _STATUSCODE_CELL:StatusCode
┣━━┓ subst: .Subst
┃ ┃ constraint:
┃ ┃ ( #sizeWordStack ( _WORDSTACK_CELL:WordStack , 0 ) +Int -2 ) >Int 1024
┃ │
┃ └─ 4 (leaf, pending)
┃ k: #next [ ADDMOD ] ~> _K_CELL:K
┃ pc: _PC_CELL:Int
┃ callDepth: _CALLDEPTH_CELL:Int
┃ statusCode: _STATUSCODE_CELL:StatusCode
┗━━┓ subst: .Subst
┃ constraint:
┃ ( notBool #sizeWordStack ( _WORDSTACK_CELL:WordStack , 0 ) <Int 3 )
┃ ( notBool ( #sizeWordStack ( _WORDSTACK_CELL:WordStack , 0 ) +Int -2 ) >Int 1024 )
└─ 5 (leaf, pending)
k: #next [ ADDMOD ] ~> _K_CELL:K
pc: _PC_CELL:Int
callDepth: _CALLDEPTH_CELL:Int
statusCode: _STATUSCODE_CELL:StatusCode


┌─ 2 (root, leaf, target)
│ k: _K_CELL:K
│ pc: ?_FINAL_PC_CELL:Int
│ callDepth: ?_FINAL_CALLDEPTH_CELL:Int
│ statusCode: ?_FINAL_STATUSCODE_CELL:StatusCode


Node 1:

<generatedTop>
<kevm>
<k>
#next [ ADDMOD ]
~> _K_CELL:K
</k>
...
</kevm>
...
</generatedTop>



Node 2:

...



Node 3:

( <generatedTop>
<kevm>
<k>
#next [ ADDMOD ]
~> _K_CELL:K
</k>
<ethereum>
<evm>
<callState>
<wordStack>
_WORDSTACK_CELL:WordStack
</wordStack>
...
</callState>
...
</evm>
...
</ethereum>
...
</kevm>
...
</generatedTop>
#And { true #Equals #sizeWordStack ( _WORDSTACK_CELL:WordStack , 0 ) <Int 3 } )



Node 4:

( <generatedTop>
<kevm>
<k>
#next [ ADDMOD ]
~> _K_CELL:K
</k>
<ethereum>
<evm>
<callState>
<wordStack>
_WORDSTACK_CELL:WordStack
</wordStack>
...
</callState>
...
</evm>
...
</ethereum>
...
</kevm>
...
</generatedTop>
#And { true #Equals ( #sizeWordStack ( _WORDSTACK_CELL:WordStack , 0 ) +Int -2 ) >Int 1024 } )



Node 5:

( <generatedTop>
<kevm>
<k>
#next [ ADDMOD ]
~> _K_CELL:K
</k>
<ethereum>
<evm>
<callState>
<wordStack>
_WORDSTACK_CELL:WordStack
</wordStack>
...
</callState>
...
</evm>
...
</ethereum>
...
</kevm>
...
</generatedTop>
#And ( { false #Equals #sizeWordStack ( _WORDSTACK_CELL:WordStack , 0 ) <Int 3 }
#And { false #Equals ( #sizeWordStack ( _WORDSTACK_CELL:WordStack , 0 ) +Int -2 ) >Int 1024 } ) )



54 changes: 54 additions & 0 deletions kevm-pyk/src/kevm_pyk/summaries/ADDMOD_SPEC/summary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

┌─ 1 (root, split, init)
│ k: #next [ ADDMOD ] ~> _K_CELL:K
│ pc: _PC_CELL:Int
│ callDepth: _CALLDEPTH_CELL:Int
│ statusCode: _STATUSCODE_CELL:StatusCode
┃ (branch)
┣━━┓ subst: .Subst
┃ ┃ constraint:
┃ ┃ #sizeWordStack ( _WORDSTACK_CELL:WordStack , 0 ) <Int 3
┃ │
┃ └─ 3 (leaf, pending)
┃ k: #next [ ADDMOD ] ~> _K_CELL:K
┃ pc: _PC_CELL:Int
┃ callDepth: _CALLDEPTH_CELL:Int
┃ statusCode: _STATUSCODE_CELL:StatusCode
┣━━┓ subst: .Subst
┃ ┃ constraint:
┃ ┃ ( #sizeWordStack ( _WORDSTACK_CELL:WordStack , 0 ) +Int -2 ) >Int 1024
┃ │
┃ └─ 4 (leaf, pending)
┃ k: #next [ ADDMOD ] ~> _K_CELL:K
┃ pc: _PC_CELL:Int
┃ callDepth: _CALLDEPTH_CELL:Int
┃ statusCode: _STATUSCODE_CELL:StatusCode
┗━━┓ subst: .Subst
┃ constraint:
┃ ( notBool #sizeWordStack ( _WORDSTACK_CELL:WordStack , 0 ) <Int 3 )
┃ ( notBool ( #sizeWordStack ( _WORDSTACK_CELL:WordStack , 0 ) +Int -2 ) >Int 1024 )
└─ 5 (leaf, pending)
k: #next [ ADDMOD ] ~> _K_CELL:K
pc: _PC_CELL:Int
callDepth: _CALLDEPTH_CELL:Int
statusCode: _STATUSCODE_CELL:StatusCode


┌─ 2 (root, leaf, target)
│ k: _K_CELL:K
│ pc: ?_FINAL_PC_CELL:Int
│ callDepth: ?_FINAL_CALLDEPTH_CELL:Int
│ statusCode: ?_FINAL_STATUSCODE_CELL:StatusCode



module SUMMARY-ADDMOD-SPEC




endmodule
Loading
Loading