Skip to content

Commit

Permalink
Additional arithmetic reasoning needed for symbolic bytes (#2286)
Browse files Browse the repository at this point in the history
* additional arithmetic reasoning needed for symbolic bytes

* Set Version: 1.0.443

* Set Version: 1.0.444

* addressing first batch of comments

* Set Version: 1.0.445

---------

Co-authored-by: devops <[email protected]>
  • Loading branch information
PetarMax and devops authored Feb 7, 2024
1 parent d46ff30 commit ff65472
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 15 deletions.
2 changes: 1 addition & 1 deletion kevm-pyk/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "kevm-pyk"
version = "1.0.444"
version = "1.0.445"
description = ""
authors = [
"Runtime Verification, Inc. <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion kevm-pyk/src/kevm_pyk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
from typing import Final


VERSION: Final = '1.0.444'
VERSION: Final = '1.0.445'
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ module BITWISE-SIMPLIFICATION [symbolic]
andBool 0 <=Int Y andBool Y <Int X +Int 1
[concrete(X), simplification, comm]

rule [bitwise-or-geq-zero]: 0 <=Int (X |Int Y) => true requires 0 <=Int X andBool 0 <=Int Y [simplification]
rule [bitwise-and-geq-zero]: 0 <=Int (X &Int Y) => true requires 0 <=Int X andBool 0 <=Int Y [simplification]
rule [bitwise-or-geq-zero]: 0 <=Int (X |Int Y) => true requires 0 <=Int X andBool 0 <=Int Y [simplification, smt-lemma]
rule [bitwise-and-geq-zero]: 0 <=Int (X &Int Y) => true requires 0 <=Int X andBool 0 <=Int Y [simplification, smt-lemma]

rule [bitwise-or-lt-zero]: (X |Int Y) <Int Z => false requires 0 <=Int X andBool 0 <=Int Y andBool Z <=Int 0 [concrete(Z), simplification]
rule [bitwise-and-lt-zero]: (X &Int Y) <Int Z => false requires 0 <=Int X andBool 0 <=Int Y andBool Z <=Int 0 [concrete(Z), simplification]

rule [bitwise-and-lt]: (X &Int Y) <Int Z => true requires 0 <=Int X andBool 0 <=Int Y andBool (X <Int Z orBool Y <Int Z) [simplification]
rule [bitwise-or-lt-pow256]: (X |Int Y) <Int pow256 => true requires 0 <=Int X andBool 0 <=Int Y andBool X <Int pow256 andBool Y <Int pow256 [simplification]
Expand Down Expand Up @@ -71,6 +74,11 @@ module BITWISE-SIMPLIFICATION [symbolic]
andBool X +Int Z ==Int maxUInt256
[simplification, concrete(X, Z), comm]

rule [notMaxUInt5-bit-and-nested]:
notMaxUInt5 &Int ( ( notMaxUInt5 &Int X ) +Int Y ) => chop ( ( notMaxUInt5 &Int X ) +Int ( notMaxUInt5 &Int Y ) )
requires 0 <=Int X andBool 0 <=Int Y
[simplification, concrete(Y)]

rule [lengthBytes-upInt-32-lower-bound]:
lengthBytes(X) <=Int notMaxUInt5 &Int ( lengthBytes(X) +Int 31 ) => true
requires lengthBytes(X) +Int 31 <Int pow256
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,12 @@ module BYTES-SIMPLIFICATION [symbolic]

// lengthBytes

rule [lengthBytes-geq-zero]: 0 <=Int lengthBytes ( _ ) => true [simplification, smt-lemma]
rule [lengthBytes-concat]: lengthBytes(BUF1 +Bytes BUF2) => lengthBytes(BUF1) +Int lengthBytes(BUF2) [simplification]
rule [lengthBytes-buf]: lengthBytes(#buf(S, _)) => S requires 0 <=Int S [simplification]
rule [lengthBytes-range]: lengthBytes(#range(_, S:Int, W:Int)) => maxInt(0, W) requires 0 <=Int S [simplification]
rule [lengthBytes-prtw]: lengthBytes(#padRightToWidth(W:Int, B:Bytes)) => maxInt(lengthBytes(B), W) [simplification]
rule [lengthBytes-geq-zero]: 0 <=Int lengthBytes ( _ ) => true [simplification, smt-lemma]
rule [lengthBytes-geq-nonPos]: X <=Int lengthBytes ( _ ) => true requires X <=Int 0 [simplification, concrete(X)]
rule [lengthBytes-concat]: lengthBytes(BUF1 +Bytes BUF2) => lengthBytes(BUF1) +Int lengthBytes(BUF2) [simplification]
rule [lengthBytes-buf]: lengthBytes(#buf(S, _)) => S requires 0 <=Int S [simplification]
rule [lengthBytes-range]: lengthBytes(#range(_, S:Int, W:Int)) => maxInt(0, W) requires 0 <=Int S [simplification]
rule [lengthBytes-prtw]: lengthBytes(#padRightToWidth(W:Int, B:Bytes)) => maxInt(lengthBytes(B), W) [simplification]

rule [lengthBytes-leq-zero]: lengthBytes(B:Bytes) <=Int 0 => B ==K .Bytes [simplification]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module EVM-INT-SIMPLIFICATION-COMMON
imports INT
imports BOOL
imports EVM-TYPES
imports WORD

// ###########################################################################
// up/Int
Expand Down Expand Up @@ -52,4 +53,15 @@ module EVM-INT-SIMPLIFICATION-COMMON
requires 0 <Int Y
[simplification, comm, preserves-definedness]

// ###########################################################################
// chop
// ###########################################################################

rule [chop-idempotent]: chop(I) => I requires #rangeUInt( 256 , I ) [simplification]
rule [chop-upper-bound]: 0 <=Int chop(_V) => true [simplification, smt-lemma]
rule [chop-lower-bound]: chop(_V) <Int pow256 => true [simplification, smt-lemma]

rule [chop-sum-left]: chop ( chop ( X:Int ) +Int Y:Int ) => chop ( X +Int Y ) [simplification]
rule [chop-sum-right]: chop ( X:Int +Int chop ( Y:Int ) ) => chop ( X +Int Y ) [simplification]

endmodule
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ module INT-SIMPLIFICATION-HASKELL [symbolic]
rule { A -Int B #Equals C } => { A #Equals C +Int B } [concrete(B, C), symbolic(A), simplification(45), comm]
rule { A -Int B #Equals C } => { A -Int C #Equals B } [concrete(A, C), symbolic(B), simplification(45), comm]

rule A +Int B <Int C +Int D => A -Int C <Int D -Int B [concrete(B, D), symbolic(A, C), simplification(45)]
rule A +Int B <=Int C +Int D => A -Int C <=Int D -Int B [concrete(B, D), symbolic(A, C), simplification(45)]
rule A +Int B >Int C +Int D => A -Int C >Int D -Int B [concrete(B, D), symbolic(A, C), simplification(45)]
rule A +Int B >=Int C +Int D => A -Int C >=Int D -Int B [concrete(B, D), symbolic(A, C), simplification(45)]

rule A +Int B ==Int C +Int D => A -Int C ==Int D -Int B [concrete(B, D), symbolic(A, C), simplification(45)]

rule { A +Int B #Equals C +Int D } => { A -Int C #Equals D -Int B } [concrete(B, D), symbolic(A, C), simplification(45)]

endmodule

module INT-SIMPLIFICATION-COMMON
Expand Down Expand Up @@ -199,6 +208,7 @@ module INT-SIMPLIFICATION-COMMON
rule notBool (A <Int B) => B <=Int A [simplification]
rule notBool (A <=Int B) => B <Int A [simplification]

rule 0 <=Int A +Int B => true requires 0 <=Int A andBool 0 <=Int B [simplification]
rule 0 <=Int A *Int B => true requires 0 <=Int A andBool 0 <=Int B [simplification]

rule A -Int B +Int C <=Int D => false requires D <Int A -Int B andBool 0 <=Int C [simplification]
Expand Down
5 changes: 0 additions & 5 deletions kevm-pyk/src/kevm_pyk/kproj/evm-semantics/lemmas/lemmas.k
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ module LEMMAS [symbolic]
rule 0 <=Int #ceil32(I) => true requires 0 <=Int I [simplification, smt-lemma]
rule 0 <=Int #ceil32(I) -Int I => true requires 0 <=Int I [simplification]

// chop range & simplification
rule chop(I) => I requires #rangeUInt( 256 , I ) [simplification]
rule 0 <=Int chop(_V) => true [simplification, smt-lemma]
rule chop(_V) <Int pow256 => true [simplification]

rule X *Int Y <Int pow256 => true requires Y <=Int maxUInt256 /Int X [simplification]
rule #if B #then C +Int C1 #else C +Int C2 #fi => C +Int #if B #then C1 #else C2 #fi [simplification]

Expand Down
2 changes: 1 addition & 1 deletion package/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.444
1.0.445
10 changes: 10 additions & 0 deletions tests/specs/functional/lemmas-spec.k
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,19 @@ module LEMMAS-SPEC
claim <k> runLemma ( chop( #lookup(M, KEY) ) ) => doneLemma( #lookup(M, KEY) ) ... </k>
claim <k> runLemma ( chop( bool2Word(B:Bool) ) ) => doneLemma( bool2Word(B) ) ... </k>

claim [chop-chop]: <k> runLemma ( chop( chop ( X ) ) ) => doneLemma( chop ( X ) ) ... </k>

claim [chop-range-inferred]: <k> runLemma(X <Int chop(X *Int Y) /Int 1000000000000000000) => doneLemma(X <Int (X *Int Y) /Int 1000000000000000000) ... </k>
requires #rangeUInt(256, X) andBool #rangeUInt(256, Y) andBool Y <=Int maxUInt256 /Int X

claim [chop-notMaxUInt5]:
<k> runLemma (
( notBool ( notBool ( chop ( ( chop ( ( ( notMaxUInt5 &Int ( ( notMaxUInt5 &Int ( lengthBytes ( B:Bytes ) +Int maxUInt5 ) ) +Int 95 ) ) +Int 128 ) ) +Int ( notMaxUInt5 &Int ( ( notMaxUInt5 &Int ( lengthBytes ( B:Bytes ) +Int maxUInt5 ) ) +Int 63 ) ) ) ) <Int chop ( ( ( notMaxUInt5 &Int ( ( notMaxUInt5 &Int ( lengthBytes ( B:Bytes ) +Int maxUInt5 ) ) +Int 95 ) ) +Int 128 ) ) ) ) )
) => doneLemma (
false
) ... </k>
requires lengthBytes(B) <Int 2 ^Int 64

// #padToWidth simplification
// --------------------------

Expand Down

0 comments on commit ff65472

Please sign in to comment.