Skip to content

Commit c8de34e

Browse files
committed
Upstream first batch of lemmas from Lido engagement
1 parent bdd3942 commit c8de34e

4 files changed

Lines changed: 70 additions & 0 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,29 @@ 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 X <Int ( 2 ^Int log2Int ( Z ) ) andBool Y <Int ( 2 ^Int log2Int ( Z ) )
96+
[simplification, concrete(Z)]
97+
98+
rule [b2w-lt]:
99+
bool2Word ( B:Bool ) *Int X <Int Y =>
100+
( ( notBool B ) andBool 0 <Int Y ) orBool ( B andBool X <Int Y )
101+
[simplification, concrete(Y)]
102+
103+
rule [xorInt-to-if]:
104+
X xorInt ( bool2Word ( B ) *Int ( X xorInt Y ) ) => #if B #then Y #else X #fi
105+
[simplification]
106+
82107
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ 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 0 >Int A -Int B => B >Int A [simplification, symbolic(A, B)]
102+
rule 0 >=Int A -Int B => B >=Int A [simplification, symbolic(A, B)]
103+
99104
endmodule
100105

101106
module INT-SIMPLIFICATION-COMMON
@@ -148,6 +153,24 @@ module INT-SIMPLIFICATION-COMMON
148153

149154
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]
150155

156+
// Simplification of concrete multiplication
157+
158+
rule A *Int B <Int C => B <Int C /Int A
159+
requires 0 <Int A andBool 0 <=Int C andBool C modInt A ==Int 0
160+
[simplification(40), concrete(A, C), preserves-definedness]
161+
162+
rule A *Int B <=Int C => B <=Int C /Int A
163+
requires 0 <Int A andBool 0 <=Int C andBool C modInt A ==Int 0
164+
[simplification(40), concrete(A, C), preserves-definedness]
165+
166+
rule C <=Int A *Int B => C /Int A <=Int B
167+
requires 0 <Int A andBool 0 <=Int C andBool C modInt A ==Int 0
168+
[simplification(40), concrete(A, C), preserves-definedness]
169+
170+
rule C <Int A *Int B => C /Int A <Int B
171+
requires 0 <Int A andBool 0 <=Int C andBool C modInt A ==Int 0
172+
[simplification(40), concrete(A, C), preserves-definedness]
173+
151174
// ###########################################################################
152175
// div
153176
// ###########################################################################
@@ -169,6 +192,13 @@ module INT-SIMPLIFICATION-COMMON
169192
requires 0 <=Int A andBool 0 <=Int B andBool 0 <Int C andBool A <=Int D andBool B <=Int C
170193
[simplification, preserves-definedness]
171194

195+
rule ( A /Int B ) /Int C => 0
196+
requires 0 <=Int A
197+
andBool 0 <Int B
198+
andBool 0 <Int C
199+
andBool A <Int ( C *Int B )
200+
[simplification, symbolic(A, B), concrete(C), preserves-definedness]
201+
172202
// ###########################################################################
173203
// mod
174204
// ###########################################################################
@@ -223,4 +253,10 @@ module INT-SIMPLIFICATION-COMMON
223253

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

256+
rule A ==Int B => false requires 0 <=Int A andBool B <Int 0 [simplification, concrete(B)]
257+
258+
rule 0 <Int X => true requires 0 <=Int X andBool notBool (X ==Int 0) [simplification(60)]
259+
260+
rule X <=Int maxUInt256 => X <Int pow256 [simplification]
261+
226262
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
@@ -239,4 +239,7 @@ module LEMMAS-HASKELL [symbolic]
239239

240240
rule (notBool (A andBool B)) andBool A => (notBool B) andBool A [simplification]
241241

242+
rule [notBool-or]: notBool ( A orBool B ) => ( notBool A ) andBool ( notBool B ) [simplification(60)]
243+
rule [notBool-and]: notBool ( A andBool B ) => ( notBool A ) orBool ( notBool B ) [simplification(60)]
244+
242245
endmodule

0 commit comments

Comments
 (0)