Skip to content

Commit b060078

Browse files
committed
Merge remote-tracking branch 'origin/master' into release
2 parents 426e623 + b2e84d1 commit b060078

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

kevm-pyk/src/kevm_pyk/kproj/evm-semantics/lemmas/bitwise-simplification.k

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,25 @@ module BITWISE-SIMPLIFICATION [symbolic]
7979

8080
rule 0 <=Int (X <<Int Y) => true requires 0 <=Int X andBool 0 <=Int Y [simplification]
8181

82+
// ###########################################################################
83+
// bit-xor
84+
// ###########################################################################
85+
86+
// Simplifications for the OpenZeppelin ternary operator function
87+
88+
rule [xorInt-ge-zero]:
89+
0 <=Int X xorInt Y => true
90+
requires 0 <=Int X andBool 0 <=Int Y
91+
[simplification]
92+
93+
rule [xorInt-lt]:
94+
X xorInt Y <Int Z => true
95+
requires 0 <=Int X andBool 0 <=Int Y andBool 0 <Int Z
96+
andBool X <Int ( 2 ^Int log2Int ( Z ) ) andBool Y <Int ( 2 ^Int log2Int ( Z ) )
97+
[simplification, concrete(Z)]
98+
99+
rule [xorInt-to-if]:
100+
X xorInt ( bool2Word ( B ) *Int ( X xorInt Y ) ) => #if B #then Y #else X #fi
101+
[simplification]
102+
82103
endmodule

kevm-pyk/src/kevm_pyk/kproj/evm-semantics/lemmas/bytes-simplification.k

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ module BYTES-SIMPLIFICATION [symbolic]
3434
rule [bytes-concat-right-assoc-symb-r]: (B1:Bytes +Bytes B2:Bytes) +Bytes B3:Bytes => B1 +Bytes (B2 +Bytes B3) [symbolic(B2), simplification(40)]
3535
rule [bytes-concat-left-assoc-conc]: B1:Bytes +Bytes (B2:Bytes +Bytes B3:Bytes) => (B1 +Bytes B2) +Bytes B3 [concrete(B1, B2), symbolic(B3), simplification(40)]
3636

37+
// Can ignore lower bytes for <Int if the corresponding bytes in X are 0 (note this does not apply to <=Int)
38+
rule [asWord-lt-concat-left]:
39+
#asWord ( B1 +Bytes B2 ) <Int X => #asWord ( B1 ) <Int X /Int ( 2 ^Int ( 8 *Int lengthBytes ( B2 ) ) )
40+
requires X modInt ( 2 ^Int ( 8 *Int lengthBytes ( B2 ) ) ) ==Int 0
41+
[simplification, preserves-definedness]
42+
3743
// #buf
3844

3945
rule [buf-empty]: #buf(N:Int, _) => b"" requires N ==Int 0 [simplification]

kevm-pyk/src/kevm_pyk/kproj/evm-semantics/lemmas/int-simplification.k

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,17 @@ module INT-SIMPLIFICATION-HASKELL [symbolic]
9696
rule { A +Int B #Equals C +Int B } => { A #Equals C } [simplification(40)]
9797
rule { A +Int B #Equals C +Int D } => { A -Int C #Equals D -Int B } [simplification(45), symbolic(A, C), concrete(B, D)]
9898

99+
rule 0 <Int A -Int B => B <Int A [simplification, symbolic(A, B)]
100+
rule 0 <=Int A -Int B => B <=Int A [simplification, symbolic(A, B)]
101+
rule A -Int B <Int 0 => A <Int B [simplification, symbolic(A, B)]
102+
rule A -Int B <=Int 0 => A <=Int B [simplification, symbolic(A, B)]
103+
99104
endmodule
100105

101106
module INT-SIMPLIFICATION-COMMON
102107
imports INT
103108
imports BOOL
109+
imports WORD
104110

105111
// ###########################################################################
106112
// add, sub
@@ -148,6 +154,24 @@ module INT-SIMPLIFICATION-COMMON
148154

149155
rule (E *Int A) +Int B +Int C +Int D +Int (F *Int A) => ((E +Int F) *Int A) +Int B +Int C +Int D [simplification]
150156

157+
// Simplification of concrete multiplication
158+
159+
rule A *Int B <Int C => B <Int C /Int A
160+
requires 0 <Int A andBool 0 <=Int C andBool C modInt A ==Int 0
161+
[simplification(40), concrete(A, C), preserves-definedness]
162+
163+
rule A *Int B <=Int C => B <=Int C /Int A
164+
requires 0 <Int A andBool 0 <=Int C andBool C modInt A ==Int 0
165+
[simplification(40), concrete(A, C), preserves-definedness]
166+
167+
rule C <=Int A *Int B => C /Int A <=Int B
168+
requires 0 <Int A andBool 0 <=Int C andBool C modInt A ==Int 0
169+
[simplification(40), concrete(A, C), preserves-definedness]
170+
171+
rule C <Int A *Int B => C /Int A <Int B
172+
requires 0 <Int A andBool 0 <=Int C andBool C modInt A ==Int 0
173+
[simplification(40), concrete(A, C), preserves-definedness]
174+
151175
// ###########################################################################
152176
// div
153177
// ###########################################################################
@@ -169,6 +193,13 @@ module INT-SIMPLIFICATION-COMMON
169193
requires 0 <=Int A andBool 0 <=Int B andBool 0 <Int C andBool A <=Int D andBool B <=Int C
170194
[simplification, preserves-definedness]
171195

196+
rule ( A /Int B ) /Int C => 0
197+
requires 0 <=Int A
198+
andBool 0 <Int B
199+
andBool 0 <Int C
200+
andBool A <Int ( C *Int B )
201+
[simplification, symbolic(A, B), concrete(C), preserves-definedness]
202+
172203
// ###########################################################################
173204
// mod
174205
// ###########################################################################
@@ -223,4 +254,10 @@ module INT-SIMPLIFICATION-COMMON
223254

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

257+
rule A ==Int B => false requires 0 <=Int A andBool B <Int 0 [simplification, concrete(B)]
258+
259+
rule 0 <Int X => true requires 0 <=Int X andBool notBool (X ==Int 0) [simplification(60)]
260+
261+
rule X <=Int maxUInt256 => X <Int pow256 [simplification]
262+
226263
endmodule

kevm-pyk/src/kevm_pyk/kproj/evm-semantics/lemmas/lemmas.k

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,7 @@ module LEMMAS-HASKELL [symbolic]
252252

253253
rule (notBool (A andBool B)) andBool A => (notBool B) andBool A [simplification]
254254

255+
rule [notBool-or]: notBool ( A orBool B ) => ( notBool A ) andBool ( notBool B ) [simplification(60)]
256+
rule [notBool-and]: notBool ( A andBool B ) => ( notBool A ) orBool ( notBool B ) [simplification(60)]
257+
255258
endmodule

tests/specs/functional/lemmas-spec.k

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,27 @@ module LEMMAS-SPEC
9090
) ... </k>
9191
requires 0 <=Int X andBool X <Int 2 ^Int 144
9292

93+
claim [div-div]: <k> runLemma ( ( A /Int B ) /Int 1024 ) => doneLemma ( 0 ) ... </k>
94+
requires 0 <=Int A andBool 0 <Int B andBool A <Int ( 1024 *Int B )
95+
96+
// Booleans
97+
// ---------
98+
99+
claim [notBool-or]:
100+
<k> runLemma (
101+
notBool ( ( X <Int LOWER ) orBool ( UPPER <=Int X ) )
102+
) => doneLemma (
103+
( notBool ( X <Int LOWER ) ) andBool ( notBool ( UPPER <=Int X ) )
104+
) ...
105+
</k>
106+
107+
claim [notBool-and]:
108+
<k> runLemma (
109+
notBool ( ( LOWER <=Int X ) andBool ( X <Int UPPER ) )
110+
) => doneLemma (
111+
( notBool ( LOWER <=Int X ) ) orBool ( notBool ( X <Int UPPER ) )
112+
) ...
113+
</k>
93114

94115
// Comparisons
95116
// -----------
@@ -130,6 +151,34 @@ module LEMMAS-SPEC
130151
) ...
131152
</k>
132153

154+
claim [zero-lt-sub]: <k> runLemma ( 0 <Int A -Int B ) => doneLemma ( B <Int A ) ... </k>
155+
claim [zero-le-sub]: <k> runLemma ( 0 <=Int A -Int B ) => doneLemma ( B <=Int A ) ... </k>
156+
claim [zero-gt-sub]: <k> runLemma ( 0 >Int A -Int B ) => doneLemma ( B >Int A ) ... </k>
157+
claim [zero-ge-sub]: <k> runLemma ( 0 >=Int A -Int B ) => doneLemma ( B >=Int A ) ... </k>
158+
159+
claim [sub-gt-zero]: <k> runLemma ( A -Int B >Int 0 ) => doneLemma ( A >Int B ) ... </k>
160+
claim [sub-ge-zero]: <k> runLemma ( A -Int B >=Int 0 ) => doneLemma ( A >=Int B ) ... </k>
161+
claim [sub-lt-zero]: <k> runLemma ( A -Int B <Int 0 ) => doneLemma ( A <Int B ) ... </k>
162+
claim [sub-le-zero]: <k> runLemma ( A -Int B <=Int 0 ) => doneLemma ( A <=Int B ) ... </k>
163+
164+
claim [mul-lt-const]: <k> runLemma ( 36 *Int B <Int 1728 ) => doneLemma ( B <Int 48 ) ... </k>
165+
claim [mul-le-const]: <k> runLemma ( 36 *Int B <=Int 1728 ) => doneLemma ( B <=Int 48 ) ... </k>
166+
claim [mul-gt-const]: <k> runLemma ( 36 *Int B >Int 1728 ) => doneLemma ( B >Int 48 ) ... </k>
167+
claim [mul-ge-const]: <k> runLemma ( 36 *Int B >=Int 1728 ) => doneLemma ( B >=Int 48 ) ... </k>
168+
169+
claim [const-gt-mul]: <k> runLemma ( 1728 >Int 36 *Int B ) => doneLemma ( 48 >Int B ) ... </k>
170+
claim [const-ge-mul]: <k> runLemma ( 1728 >=Int 36 *Int B ) => doneLemma ( 48 >=Int B ) ... </k>
171+
claim [const-lt-mul]: <k> runLemma ( 1728 <Int 36 *Int B ) => doneLemma ( 48 <Int B ) ... </k>
172+
claim [const-le-mul]: <k> runLemma ( 1728 <=Int 36 *Int B ) => doneLemma ( 48 <=Int B ) ... </k>
173+
174+
claim [eq-neg]: <k> runLemma ( A ==Int -1 ) => doneLemma ( false ) ... </k>
175+
requires 0 <=Int A
176+
177+
claim [nonneg-and-nonzero]: <k> runLemma ( 0 <Int X ) => doneLemma ( true ) ... </k>
178+
requires 0 <=Int X andBool notBool ( X ==Int 0 )
179+
180+
claim [le-maxuint256]: <k> runLemma ( X <=Int maxUInt256 ) => doneLemma ( X <Int pow256 ) ... </k>
181+
133182

134183
// Sets
135184
// ----
@@ -657,6 +706,12 @@ module LEMMAS-SPEC
657706
claim [shift-range]: <k> runLemma ( #rangeUInt ( 256 , X <<Int 16 ) ) => doneLemma ( true ) ... </k> requires #rangeUInt ( 16 , X )
658707
claim [shift-mod]: <k> runLemma ( ( X <<Int 16 ) modInt pow256 ) => doneLemma ( X <<Int 16 ) ... </k> requires #rangeUInt ( 16 , X )
659708

709+
// xor
710+
// -----
711+
712+
claim [xorInt-range]: <k> runLemma ( #rangeUInt ( 256 , X xorInt Y ) ) => doneLemma ( true ) ... </k> requires #rangeUInt ( 256 , X ) andBool #rangeUInt ( 256 , Y )
713+
claim [xorInt-to-if]: <k> runLemma ( A xorInt ( bool2Word ( A <=Int B ) *Int ( A xorInt B ) ) ) => doneLemma ( #if ( A <=Int B ) #then B #else A #fi ) ... </k>
714+
660715
// concat
661716
// ------
662717

@@ -769,6 +824,13 @@ module LEMMAS-SPEC
769824
requires #asWord ( BA3 ) ==Int X modInt ( 2 ^Int ( 8 *Int lengthBytes ( BA3 ) ) )
770825
andBool 0 <Int lengthBytes(BA1) andBool lengthBytes(BA2 +Bytes BA3) ==Int 32
771826

827+
claim [asWord-lt-concat-left]:
828+
<k> runLemma (
829+
#asWord ( B1 +Bytes b"\xde\xad\xbe\xef" ) <Int #asWord ( b"\xfa\xca\xde\x00\x00\x00\x00" )
830+
) => doneLemma (
831+
#asWord ( B1 ) <Int #asWord ( b"\xfa\xca\xde")
832+
) ... </k>
833+
772834
// bool2Word
773835
// ---------
774836

0 commit comments

Comments
 (0)