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

Open
wants to merge 63 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
656af89
provide `KEVMSummarizer` to sammarize rules for all the instruction r…
Stevengre Jan 7, 2025
0d14ca7
solve error call for legacy_explore and incorrect name for `#next[_]`
Stevengre Jan 7, 2025
9914ea6
kevm-pyk/: get code passing basic type checking issues
ehildenb Jan 7, 2025
d84279e
qupdate poetry to 2.0.0
Stevengre Jan 8, 2025
4836190
add new command `summarize` for summarizing evm instructions
Stevengre Jan 8, 2025
319ee05
don't use `summarize` but `kevm summarize` instead.
Stevengre Jan 8, 2025
c7cb688
provide spec with initial and target node that are not terminal accor…
Stevengre Jan 8, 2025
cd9fa50
make format & make check
Stevengre Jan 8, 2025
d1efcbd
print nodes and add summarize function to generate optimized rules fo…
Stevengre Jan 8, 2025
c31c3ea
make format & make check
Stevengre Jan 8, 2025
4574319
add ignore
Stevengre Jan 8, 2025
2c25133
allow_symbolic_program
Stevengre Jan 8, 2025
483b389
summarize instructions one by one
Stevengre Jan 9, 2025
7a0bf10
summarize `STOP`
Stevengre Jan 9, 2025
8b810d3
solve the unhook problem during summarizing `ADD`.
Stevengre Jan 9, 2025
5bfa83e
summarize all the evm opcode using `KEVMSummarizer.batch_summarize`. …
Stevengre Jan 10, 2025
eb7da5f
fix the summarization for `STOP`.
Stevengre Jan 10, 2025
03b449e
update the status of `STOP`.
Stevengre Jan 10, 2025
2ee2381
avoid useless overflow check
Stevengre Jan 13, 2025
a859d33
don't use optimizations during summarization
Stevengre Jan 13, 2025
189d8ef
summarize `ADD` successfully with new simplification rules, wrapper r…
Stevengre Jan 13, 2025
0966ab3
summarized `MUL`
Stevengre Jan 14, 2025
5344241
summarized `SUB`
Stevengre Jan 14, 2025
e1f9852
summarized `DIV`
Stevengre Jan 14, 2025
85ac190
summarized `SDIV`
Stevengre Jan 14, 2025
b98d574
summarized `MOD`
Stevengre Jan 14, 2025
16580e8
summarized `SMOD`
Stevengre Jan 14, 2025
e748627
summarized `ADDMOD`.
Stevengre Jan 14, 2025
786b337
summarized `MULMOD`
Stevengre Jan 14, 2025
82db1b5
try to summarize all the opcode, under checking.
Stevengre Jan 14, 2025
065f10c
check all the finished summarization.
Stevengre Jan 14, 2025
35d1479
update the summarization result
Stevengre Jan 15, 2025
d653da0
update the summarization statuses.
Stevengre Jan 15, 2025
666e182
fix the summarization of `EXP`
Stevengre Jan 15, 2025
2189e75
fix the summarization of `ADDRESS`
Stevengre Jan 15, 2025
a72a40e
fix summarization of
Stevengre Jan 15, 2025
d8f53dd
fixed the summarization of `ORIGIN` and `EXTCODECOPY`
Stevengre Jan 16, 2025
807cf47
fix summarization of `CALLER`
Stevengre Jan 16, 2025
bb123c4
fix summarization of `EXTCODESIZE`
Stevengre Jan 16, 2025
329022a
fix the summarization of `RETURNDATASIZE`
Stevengre Jan 16, 2025
4e2cd4a
fix the summarization of `MLOAD`
Stevengre Jan 16, 2025
7c0b2aa
fix summarization of `MLOAD`
Stevengre Jan 16, 2025
a404da2
fix the summarization of `MSTORE8`
Stevengre Jan 16, 2025
0cc42a9
fix the summarization of
Stevengre Jan 16, 2025
e6863d0
fix the summarization of `JUMPI`
Stevengre Jan 17, 2025
7cb0c30
fix the summarization of `PUSH`
Stevengre Jan 17, 2025
82a4167
fix the summarization of `LOG`.
Stevengre Jan 17, 2025
51dbef4
backup all the useful informations in the PR before preparing the merge
Stevengre Jan 22, 2025
100c6f4
delete the logs
Stevengre Jan 22, 2025
5cce016
delete all the proof and summary files
Stevengre Jan 22, 2025
57f0228
ignore the proofs and summaries.
Stevengre Jan 22, 2025
a785d2a
back up to keep my thinking of debuging
Stevengre Jan 22, 2025
ea4b52d
finish the test process on `STOP`
Stevengre Jan 22, 2025
a1211d6
simlify the implementation
Stevengre Jan 23, 2025
4487f2c
comment rule generation validation for faster check
Stevengre Jan 23, 2025
52d4d18
fix make format & check
Stevengre Jan 23, 2025
00f3b66
poetry lock 2.0.1
Stevengre Jan 23, 2025
4dab75e
delete analysis codes in `exec_summarize`
Stevengre Jan 23, 2025
7b66e13
make format
Stevengre Jan 23, 2025
05a772c
recover the definition and create a new target for summarization
Stevengre Jan 23, 2025
abf5410
delete useless lemma comments.
Stevengre Jan 23, 2025
3005ef4
test all the passed opcodes
Stevengre Jan 23, 2025
3815a63
fix poetry.lock
Stevengre Jan 24, 2025
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: 2 additions & 0 deletions kevm-pyk/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/dist/
__pycache__/
.coverage
proofs/
summaries/
6 changes: 6 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 batch_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, filter_gst_keys, gst_to_kore, kore_pgm_to_kore
Expand Down Expand Up @@ -634,6 +636,10 @@ def exec_kast(options: KastOptions) -> None:
print(output_text)


def exec_summarize(options: ProveOptions) -> None:
batch_summarize()


# 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
8 changes: 8 additions & 0 deletions kevm-pyk/src/kevm_pyk/kdist/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ def context(self) -> dict[str, str]:
'syntax_module': 'EDSL',
},
),
'summary': KEVMTarget(
{
'target': KompileTarget.HASKELL,
'main_file': config.EVM_SEMANTICS_DIR / 'edsl-sum.md',
'main_module': 'EDSL',
'syntax_module': 'EDSL',
},
),
'haskell-standalone': KEVMTarget(
{
'target': KompileTarget.HASKELL,
Expand Down
35 changes: 35 additions & 0 deletions kevm-pyk/src/kevm_pyk/kproj/evm-semantics/edsl-sum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
eDSL High-Level Notations
=========================

The eDSL high-level notations make the EVM specifications more succinct and closer to their high-level specifications.
The succinctness increases the readability, and the closeness helps "eye-ball validation" of the specification refinement.
The high-level notations are defined by translation to the corresponding EVM terms, and thus can be freely used with other EVM terms.
The notations are inspired by the production compilers of the smart contract languages like Solidity and Vyper, and their definition is derived by formalizing the corresponding translation made by the compilers.

```k
requires "buf.md"
requires "hashed-locations.md"
requires "abi.md"
requires "gas.md"
requires "lemmas/lemmas.k"

module EDSL
imports BUF
imports HASHED-LOCATIONS
imports SOLIDITY-FIELDS
imports EVM-ABI
imports INFINITE-GAS
imports BIN-RUNTIME
imports LEMMAS
endmodule

module BIN-RUNTIME
imports EVM-ABI

syntax Contract
syntax Bytes ::= #binRuntime ( Contract ) [alias, symbol(binRuntime) , function, no-evaluators]
| #initBytecode ( Contract ) [alias, symbol(initBytecode), function, no-evaluators]
// --------------------------------------------------------------------------------------------------

endmodule
```
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/lemmas.k"

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

module BIN-RUNTIME
Expand Down
24 changes: 13 additions & 11 deletions kevm-pyk/src/kevm_pyk/kproj/evm-semantics/evm.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ This file only defines the local execution operations, the file `driver.md` will
requires "data.md"
requires "network.md"
requires "gas.md"
requires "serialization.md"

module EVM
imports STRING
imports EVM-DATA
imports NETWORK
imports GAS
imports SERIALIZATION
```

Configuration
Expand Down Expand Up @@ -363,7 +365,7 @@ The `#next [_]` operator initiates execution by:
| #stackOverflow ( WordStack , OpCode ) [symbol(#stackOverflow), macro]
// ---------------------------------------------------------------------------------------
rule #stackUnderflow(WS, OP:OpCode) => #sizeWordStack(WS) <Int #stackNeeded(OP)
rule #stackOverflow (WS, OP) => #sizeWordStack(WS) +Int #stackDelta(OP) >Int 1024
rule #stackOverflow (WS, OP) => (#stackDelta(OP) >Int 0) andBool (#sizeWordStack(WS) +Int #stackDelta(OP) >Int 1024)

syntax Int ::= #stackNeeded ( OpCode ) [symbol(#stackNeeded), function]
// -----------------------------------------------------------------------
Expand Down Expand Up @@ -1918,9 +1920,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)
rule <k> #ecadd(P1, P2) => #end EVMC_SUCCESS ... </k> <output> _ => #point(BN128Add(P1, P2)) </output>
requires isValidPoint(P1) andBool isValidPoint(P2)
requires notBool isValidPointWrapper(P1) orBool notBool isValidPointWrapper(P2)
rule <k> #ecadd(P1, P2) => #end EVMC_SUCCESS ... </k> <output> _ => #point(BN128AddWrapper(P1, P2)) </output>
requires isValidPointWrapper(P1) andBool isValidPointWrapper(P2)

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

syntax Bytes ::= #point ( G1Point ) [symbol(#point), function]
// --------------------------------------------------------------
Expand All @@ -1952,19 +1954,19 @@ Precompiled Contracts
rule <k> (.K => #checkPoint) ~> #ecpairing((.List => ListItem((#asWord(#range(DATA, I, 32)), #asWord(#range(DATA, I +Int 32, 32))))) _, (.List => ListItem((#asWord(#range(DATA, I +Int 96, 32)) x #asWord(#range(DATA, I +Int 64, 32)) , #asWord(#range(DATA, I +Int 160, 32)) x #asWord(#range(DATA, I +Int 128, 32))))) _, I => I +Int 192, DATA, LEN) ... </k>
requires I =/=Int LEN
rule <k> #ecpairing(A, B, LEN, _, LEN) => #end EVMC_SUCCESS ... </k>
<output> _ => #padToWidth(32, #asByteStack(bool2Word(BN128AtePairing(A, B)))) </output>
<output> _ => #padToWidth(32, #asByteStack(bool2Word(BN128AtePairingWrapper(A, B)))) </output>

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"
// ----------------------------------
rule <k> BLAKE2F => #end EVMC_SUCCESS ... </k>
<output> _ => #parseByteStack( Blake2Compress( DATA ) ) </output>
<output> _ => #parseByteStack( Blake2CompressWrapper( DATA ) ) </output>
<callData> DATA </callData>
requires lengthBytes( DATA ) ==Int 213
andBool DATA[212] <=Int 1
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
Expand Up @@ -223,4 +223,9 @@ module INT-SIMPLIFICATION-COMMON

rule A -Int B +Int C <=Int D => false requires D <Int A -Int B andBool 0 <=Int C [simplification]

// ###########################################################################
// log
// ###########################################################################

rule #Ceil ( log2Int ( X:Int ) ) => { true #Equals X >Int 0 } [simplification]
endmodule
2 changes: 1 addition & 1 deletion kevm-pyk/src/kevm_pyk/kproj/evm-semantics/lemmas/lemmas.k
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module LEMMAS-WITHOUT-SLOT-UPDATES [symbolic]
// Word Reasoning
// ########################

rule 0 <=Int #sizeWordStack ( _ , N ) => true requires 0 <=Int N [simplification, smt-lemma]
rule N <=Int #sizeWordStack ( _ , N ) => true requires 0 <=Int N [simplification, smt-lemma]

// #newAddr range
rule 0 <=Int #newAddr(_,_) => true [simplification]
Expand Down
37 changes: 37 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,42 @@ module SERIALIZATION
Address/Hash Helpers
--------------------

- `BN128AddWrapper` and `BN128MulWrapper` serve as wrappers around the `BN128Add` and `BN128Mul` in `KRYPTO`.

```k
syntax G1Point ::= BN128AddWrapper ( G1Point , G1Point ) [symbol(BN128AddWrapper), function, total, smtlib(smt_krypto_bn128add)]
| BN128MulWrapper ( G1Point , Int ) [symbol(BN128MulWrapper), function, total, smtlib(smt_krypto_bn128mul)]
// -----------------------------------------------------------------------------------------------------------------------------------------
rule [BN128AddWrapper]: BN128AddWrapper(G1, G2) => BN128Add(G1, G2) [concrete]
rule [BN128MulWrapper]: BN128MulWrapper(G1, I) => BN128Mul(G1, I) [concrete]
```

- `BN128AtePairing` serves as a wrapper around the `BN128AtePairing` in `KRYPTO`.

```k
syntax Bool ::= BN128AtePairingWrapper ( List , List ) [symbol(BN128AtePairingWrapper), function, total, smtlib(smt_krypto_bn128ate)]
// ------------------------------------------------------------------------------------------------------------------------------------------------
rule [BN128AtePairingWrapper]: BN128AtePairingWrapper(G1, G2) => BN128AtePairing(G1, G2) [concrete]
```

- `Blake2CompressWrapper` serves as a wrapper around the `Blake2Compress` in `KRYPTO`.

```k
syntax String ::= Blake2CompressWrapper ( Bytes ) [symbol(Blake2CompressWrapper), function, total, smtlib(smt_krypto_blake2compress)]
// ------------------------------------------------------------------------------------------------------------------------------------
rule [Blake2CompressWrapper]: Blake2CompressWrapper(BYTES) => Blake2Compress(BYTES) [concrete]
```

- `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 All @@ -33,6 +69,7 @@ Address/Hash Helpers
| #newAddr ( Int , Int , Bytes ) [symbol(newAddrCreate2), function]
// --------------------------------------------------------------------------------
rule [#newAddr]: #newAddr(ACCT, NONCE) => #addr(#parseHexWord(Keccak256(#rlpEncode([#addrBytes(ACCT), NONCE])))) [concrete]
rule [#newAddr-definedness]: #Ceil(#newAddr(@ACCT, @NONCE)) => #Ceil(@ACCT) #And #Ceil(@NONCE) [simplification]
rule [#newAddrCreate2]: #newAddr(ACCT, SALT, INITCODE) => #addr(#parseHexWord(Keccak256(b"\xff" +Bytes #addrBytes(ACCT) +Bytes #wordBytes(SALT) +Bytes #parseByteStack(Keccak256(INITCODE))))) [concrete]
```

Expand Down
Loading
Loading