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

WordStack optimizations #2632

Closed
wants to merge 23 commits into from
Closed
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
59 changes: 24 additions & 35 deletions kevm-pyk/src/kevm_pyk/kproj/evm-semantics/evm-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,71 +238,60 @@ A cons-list is used for the EVM wordstack.
rule I : BS => Int2Bytes(1, I, BE) +Bytes BS requires I <Int 256
```

- `#take(N , WS)` keeps the first `N` elements of a `WordStack` (passing with zeros as needed).
- `#drop(N , WS)` removes the first `N` elements of a `WordStack`.
- `#take(N , WS)` takes the first `N` elements of a `WordStack`.
- `#drop(N , WS)` drops the first `N` elements of a `WordStack`.

```k
syntax WordStack ::= #take ( Int , WordStack ) [symbol(takeWordStack), function, total]
// ---------------------------------------------------------------------------------------
rule [#take.base]: #take(N, _WS) => .WordStack requires notBool N >Int 0
rule [#take.zero-pad]: #take(N, .WordStack) => 0 : #take(N -Int 1, .WordStack) requires N >Int 0
rule [#take.recursive]: #take(N, (W : WS):WordStack) => W : #take(N -Int 1, WS) requires N >Int 0
// Expected use cases
rule [ws-take-b]: #take(0, _) => .WordStack
rule [ws-take-i]: #take(N, W0 : WS) => W0 : #take(N -Int 1, WS) requires 0 <Int N
// For totality
rule [ws-take-o]: #take(_, _) => .WordStack [owise]

syntax WordStack ::= #drop ( Int , WordStack ) [symbol(dropWordStack), function, total]
// ---------------------------------------------------------------------------------------
rule #drop(N, WS:WordStack) => WS requires notBool N >Int 0
rule #drop(N, .WordStack) => .WordStack requires N >Int 0
rule #drop(N, (W : WS):WordStack) => #drop(1, #drop(N -Int 1, (W : WS))) requires N >Int 1
rule #drop(1, (_ : WS):WordStack) => WS
// Expected use cases
rule [ws-drop-b]: #drop(0, WS) => WS
rule [ws-drop-i]: #drop(N, W0 : WS) => #drop(N -Int 1, WS) requires 0 <Int N
// For totality
rule [ws-drop-O]: #drop(_, _) => .WordStack [owise]
```

### Element Access

- `WS [ N ]` accesses element `N` of `WS`.
- `WS [ N := W ]` sets element `N` of `WS` to `W` (padding with zeros as needed).
- `WS [ N := W ]` sets element `N` of `WS` to `W`.

```k
syntax Int ::= WordStack "[" Int "]" [function, total]
// ------------------------------------------------------
rule (W : _):WordStack [ N ] => W requires N ==Int 0
rule WS:WordStack [ N ] => #drop(N, WS) [ 0 ] requires N >Int 0
rule _:WordStack [ N ] => 0 requires N <Int 0
// Expected use cases
rule [ws-get-b]: (W : _):WordStack [ 0 ] => W
rule [ws-get-i]: (W : WS):WordStack [ N ] => WS:WordStack [ N -Int 1 ] requires 0 <Int N
// For totality
rule [ws-get-o]: _:WordStack [ _:Int ] => 0 [owise]

syntax WordStack ::= WordStack "[" Int ":=" Int "]" [function, total]
// ---------------------------------------------------------------------
rule (_W0 : WS):WordStack [ N := W ] => W : WS requires N ==Int 0
rule ( W0 : WS):WordStack [ N := W ] => W0 : (WS [ N -Int 1 := W ]) requires N >Int 0
rule WS :WordStack [ N := _ ] => WS requires N <Int 0
rule .WordStack [ N := W ] => (0 : .WordStack) [ N := W ]
// Expected use cases
rule [ws-set-b]: (_ : WS):WordStack [ 0 := V ] => ( V : WS ):WordStack
rule [ws-set-i]: (W : WS):WordStack [ N := V ] => ( W : (WS [ N -Int 1 := V ]) ):WordStack requires 0 <Int N
// For totality
rule [ws-set-O]: _:WordStack [ _ := _ ] => .WordStack [owise]
```

- `#sizeWordStack` calculates the size of a `WordStack`.
- `_in_` determines if a `Int` occurs in a `WordStack`.

```k
syntax Int ::= #sizeWordStack ( WordStack ) [symbol(#sizeWordStack), function, total, smtlib(sizeWordStack)]
| #sizeWordStack ( WordStack , Int ) [symbol(sizeWordStackAux), function, total, smtlib(sizeWordStackAux)]
// -----------------------------------------------------------------------------------------------------------------------
rule #sizeWordStack ( WS ) => #sizeWordStack(WS, 0)

rule #sizeWordStack ( .WordStack, SIZE ) => SIZE
rule #sizeWordStack ( _ : WS, SIZE ) => #sizeWordStack(WS, SIZE +Int 1)

syntax Bool ::= Int "in" WordStack [function]
// ---------------------------------------------
rule _ in .WordStack => false
rule W in (W' : WS) => (W ==K W') orElseBool (W in WS)
```

- `#replicateAux` pushes `N` copies of `A` onto a `WordStack`.
- `#replicate` is a `WordStack` of length `N` with `A` the value of every element.

```k
syntax WordStack ::= #replicate ( Int, Int ) [symbol(#replicate), function, total]
| #replicateAux ( Int, Int, WordStack ) [symbol(#replicateAux), function, total]
// ---------------------------------------------------------------------------------------------------
rule #replicate ( N, A ) => #replicateAux(N, A, .WordStack)
rule #replicateAux( N, A, WS ) => #replicateAux(N -Int 1, A, A : WS) requires N >Int 0
rule #replicateAux( N, _A, WS ) => WS requires notBool N >Int 0
```

- `WordStack2List` converts a term of sort `WordStack` to a term of sort `List`.
Expand Down
5 changes: 4 additions & 1 deletion kevm-pyk/src/tests/integration/test_prove.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,10 @@ def _get_optimization_proofs() -> list[APRProof]:
kore_rpc_command = ('booster-dev',) if use_booster_dev else ('kore-rpc-booster',)

with legacy_explore(
kevm, kcfg_semantics=KEVMSemantics(allow_symbolic_program=True), kore_rpc_command=kore_rpc_command
kevm,
kcfg_semantics=KEVMSemantics(allow_symbolic_program=True),
kore_rpc_command=kore_rpc_command,
smt_timeout=1000,
) as kcfg_explore:
prover = APRProver(
kcfg_explore,
Expand Down
1 change: 1 addition & 0 deletions tests/specs/opcodes/verification.k
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ module VERIFICATION
rule #sizeWordStack(WS [ I := _ ], N) => #sizeWordStack(WS, N) requires I <Int #sizeWordStack(WS) [simplification]
rule 0 <=Int #sizeWordStack(_ , 0) => true [simplification]
rule #sizeWordStack(_ , 0) <Int N => false requires N <=Int 0 [simplification]

endmodule
Loading