-
Notifications
You must be signed in to change notification settings - Fork 152
Upstream first batch of lemmas from Lido engagement #2787
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
base: master
Are you sure you want to change the base?
Changes from all commits
6c91e85
f432111
4377112
8826f77
716b22c
6950359
db01bb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,11 +96,17 @@ module INT-SIMPLIFICATION-HASKELL [symbolic] | |
rule { A +Int B #Equals C +Int B } => { A #Equals C } [simplification(40)] | ||
rule { A +Int B #Equals C +Int D } => { A -Int C #Equals D -Int B } [simplification(45), symbolic(A, C), concrete(B, D)] | ||
|
||
rule 0 <Int A -Int B => B <Int A [simplification, symbolic(A, B)] | ||
rule 0 <=Int A -Int B => B <=Int A [simplification, symbolic(A, B)] | ||
rule A -Int B <Int 0 => A <Int B [simplification, symbolic(A, B)] | ||
rule A -Int B <=Int 0 => A <=Int B [simplification, symbolic(A, B)] | ||
|
||
endmodule | ||
|
||
module INT-SIMPLIFICATION-COMMON | ||
imports INT | ||
imports BOOL | ||
imports WORD | ||
|
||
// ########################################################################### | ||
// add, sub | ||
|
@@ -148,6 +154,24 @@ module INT-SIMPLIFICATION-COMMON | |
|
||
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] | ||
|
||
// Simplification of concrete multiplication | ||
|
||
rule A *Int B <Int C => B <Int C /Int A | ||
requires 0 <Int A andBool 0 <=Int C andBool C modInt A ==Int 0 | ||
[simplification(40), concrete(A, C), preserves-definedness] | ||
|
||
rule A *Int B <=Int C => B <=Int C /Int A | ||
requires 0 <Int A andBool 0 <=Int C andBool C modInt A ==Int 0 | ||
[simplification(40), concrete(A, C), preserves-definedness] | ||
|
||
rule C <=Int A *Int B => C /Int A <=Int B | ||
requires 0 <Int A andBool 0 <=Int C andBool C modInt A ==Int 0 | ||
[simplification(40), concrete(A, C), preserves-definedness] | ||
|
||
rule C <Int A *Int B => C /Int A <Int B | ||
requires 0 <Int A andBool 0 <=Int C andBool C modInt A ==Int 0 | ||
[simplification(40), concrete(A, C), preserves-definedness] | ||
|
||
// ########################################################################### | ||
// div | ||
// ########################################################################### | ||
|
@@ -169,6 +193,13 @@ module INT-SIMPLIFICATION-COMMON | |
requires 0 <=Int A andBool 0 <=Int B andBool 0 <Int C andBool A <=Int D andBool B <=Int C | ||
[simplification, preserves-definedness] | ||
|
||
rule ( A /Int B ) /Int C => 0 | ||
requires 0 <=Int A | ||
andBool 0 <Int B | ||
andBool 0 <Int C | ||
andBool A <Int ( C *Int B ) | ||
[simplification, symbolic(A, B), concrete(C), preserves-definedness] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a fairly specific lemma, but I suppose it might be worth having if we run into an expression like this again. |
||
// ########################################################################### | ||
// mod | ||
// ########################################################################### | ||
|
@@ -223,4 +254,10 @@ module INT-SIMPLIFICATION-COMMON | |
|
||
rule A -Int B +Int C <=Int D => false requires D <Int A -Int B andBool 0 <=Int C [simplification] | ||
|
||
rule A ==Int B => false requires 0 <=Int A andBool B <Int 0 [simplification, concrete(B)] | ||
|
||
rule 0 <Int X => true requires 0 <=Int X andBool notBool (X ==Int 0) [simplification(60)] | ||
|
||
rule X <=Int maxUInt256 => X <Int pow256 [simplification] | ||
anvacaru marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not entirely sure these three lemmas are strictly necessary. They seem to be things that the SMT solver could easily resolve, but maybe they can help trigger further simplifications. |
||
endmodule |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,4 +252,7 @@ module LEMMAS-HASKELL [symbolic] | |
|
||
rule (notBool (A andBool B)) andBool A => (notBool B) andBool A [simplification] | ||
|
||
rule [notBool-or]: notBool ( A orBool B ) => ( notBool A ) andBool ( notBool B ) [simplification(60)] | ||
rule [notBool-and]: notBool ( A andBool B ) => ( notBool A ) orBool ( notBool B ) [simplification(60)] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set a later priority for these rules so they don't conflict with the preceding ones. |
||
endmodule |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,27 @@ module LEMMAS-SPEC | |
) ... </k> | ||
requires 0 <=Int X andBool X <Int 2 ^Int 144 | ||
|
||
claim [div-div]: <k> runLemma ( ( A /Int B ) /Int 1024 ) => doneLemma ( 0 ) ... </k> | ||
requires 0 <=Int A andBool 0 <Int B andBool A <Int ( 1024 *Int B ) | ||
|
||
// Booleans | ||
// --------- | ||
|
||
claim [notBool-or]: | ||
<k> runLemma ( | ||
notBool ( ( X <Int LOWER ) orBool ( UPPER <=Int X ) ) | ||
) => doneLemma ( | ||
( notBool ( X <Int LOWER ) ) andBool ( notBool ( UPPER <=Int X ) ) | ||
) ... | ||
</k> | ||
|
||
claim [notBool-and]: | ||
<k> runLemma ( | ||
notBool ( ( LOWER <=Int X ) andBool ( X <Int UPPER ) ) | ||
) => doneLemma ( | ||
( notBool ( LOWER <=Int X ) ) orBool ( notBool ( X <Int UPPER ) ) | ||
) ... | ||
</k> | ||
|
||
// Comparisons | ||
// ----------- | ||
|
@@ -130,6 +151,34 @@ module LEMMAS-SPEC | |
) ... | ||
</k> | ||
|
||
claim [zero-lt-sub]: <k> runLemma ( 0 <Int A -Int B ) => doneLemma ( B <Int A ) ... </k> | ||
claim [zero-le-sub]: <k> runLemma ( 0 <=Int A -Int B ) => doneLemma ( B <=Int A ) ... </k> | ||
claim [zero-gt-sub]: <k> runLemma ( 0 >Int A -Int B ) => doneLemma ( B >Int A ) ... </k> | ||
claim [zero-ge-sub]: <k> runLemma ( 0 >=Int A -Int B ) => doneLemma ( B >=Int A ) ... </k> | ||
|
||
claim [sub-gt-zero]: <k> runLemma ( A -Int B >Int 0 ) => doneLemma ( A >Int B ) ... </k> | ||
claim [sub-ge-zero]: <k> runLemma ( A -Int B >=Int 0 ) => doneLemma ( A >=Int B ) ... </k> | ||
claim [sub-lt-zero]: <k> runLemma ( A -Int B <Int 0 ) => doneLemma ( A <Int B ) ... </k> | ||
claim [sub-le-zero]: <k> runLemma ( A -Int B <=Int 0 ) => doneLemma ( A <=Int B ) ... </k> | ||
|
||
claim [mul-lt-const]: <k> runLemma ( 36 *Int B <Int 1728 ) => doneLemma ( B <Int 48 ) ... </k> | ||
claim [mul-le-const]: <k> runLemma ( 36 *Int B <=Int 1728 ) => doneLemma ( B <=Int 48 ) ... </k> | ||
claim [mul-gt-const]: <k> runLemma ( 36 *Int B >Int 1728 ) => doneLemma ( B >Int 48 ) ... </k> | ||
claim [mul-ge-const]: <k> runLemma ( 36 *Int B >=Int 1728 ) => doneLemma ( B >=Int 48 ) ... </k> | ||
|
||
claim [const-gt-mul]: <k> runLemma ( 1728 >Int 36 *Int B ) => doneLemma ( 48 >Int B ) ... </k> | ||
claim [const-ge-mul]: <k> runLemma ( 1728 >=Int 36 *Int B ) => doneLemma ( 48 >=Int B ) ... </k> | ||
claim [const-lt-mul]: <k> runLemma ( 1728 <Int 36 *Int B ) => doneLemma ( 48 <Int B ) ... </k> | ||
claim [const-le-mul]: <k> runLemma ( 1728 <=Int 36 *Int B ) => doneLemma ( 48 <=Int B ) ... </k> | ||
|
||
claim [eq-neg]: <k> runLemma ( A ==Int -1 ) => doneLemma ( false ) ... </k> | ||
requires 0 <=Int A | ||
|
||
claim [nonneg-and-nonzero]: <k> runLemma ( 0 <Int X ) => doneLemma ( true ) ... </k> | ||
requires 0 <=Int X andBool notBool ( X ==Int 0 ) | ||
|
||
claim [le-maxuint256]: <k> runLemma ( X <=Int maxUInt256 ) => doneLemma ( X <Int pow256 ) ... </k> | ||
|
||
Comment on lines
+154
to
+181
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These claims pass even without the corresponding lemmas, but only because the SMT solver can prove the implication. The lemmas are needed if we want to actually simplify the expressions. |
||
|
||
// Sets | ||
// ---- | ||
|
@@ -657,6 +706,12 @@ module LEMMAS-SPEC | |
claim [shift-range]: <k> runLemma ( #rangeUInt ( 256 , X <<Int 16 ) ) => doneLemma ( true ) ... </k> requires #rangeUInt ( 16 , X ) | ||
claim [shift-mod]: <k> runLemma ( ( X <<Int 16 ) modInt pow256 ) => doneLemma ( X <<Int 16 ) ... </k> requires #rangeUInt ( 16 , X ) | ||
|
||
// xor | ||
// ----- | ||
|
||
claim [xorInt-range]: <k> runLemma ( #rangeUInt ( 256 , X xorInt Y ) ) => doneLemma ( true ) ... </k> requires #rangeUInt ( 256 , X ) andBool #rangeUInt ( 256 , Y ) | ||
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> | ||
|
||
// concat | ||
// ------ | ||
|
||
|
@@ -769,6 +824,13 @@ module LEMMAS-SPEC | |
requires #asWord ( BA3 ) ==Int X modInt ( 2 ^Int ( 8 *Int lengthBytes ( BA3 ) ) ) | ||
andBool 0 <Int lengthBytes(BA1) andBool lengthBytes(BA2 +Bytes BA3) ==Int 32 | ||
|
||
claim [asWord-lt-concat-left]: | ||
<k> runLemma ( | ||
#asWord ( B1 +Bytes b"\xde\xad\xbe\xef" ) <Int #asWord ( b"\xfa\xca\xde\x00\x00\x00\x00" ) | ||
) => doneLemma ( | ||
#asWord ( B1 ) <Int #asWord ( b"\xfa\xca\xde") | ||
) ... </k> | ||
|
||
// bool2Word | ||
// --------- | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if too specific. I considered if it should use the
concrete(X)
attribute, but if it didn't include it in the first place it's probably because the expression that originally motivated it had a symbolicX
.