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

Adding ideas from paper by Albert et al. #463

Merged
merged 1 commit into from
Mar 4, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
with declare-fun
- CVC5 needs `--incremental` flag to work properly in abstraction-refinement mode
- cli.hs now uses with-utf8 so no release binary will have locale issues anymore
- Took ideas for simplification rules from "Super-optimization of Smart Contracts" paper by Albert et al.

## [0.53.0] - 2024-02-23

Expand Down
20 changes: 18 additions & 2 deletions src/EVM/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -958,20 +958,28 @@ simplify e = if (mapExpr go e == e)
| a < b = Lit 1
| otherwise = Lit 0
go (EVM.Types.LT _ (Lit 0)) = Lit 0
go (EVM.Types.LT a (Lit 1)) = iszero a

-- normalize all comparisons in terms of LT
go (EVM.Types.GT a b) = lt b a
go (EVM.Types.GEq a b) = leq b a
go (EVM.Types.LEq a b) = EVM.Types.IsZero (gt a b)
go (IsZero a) = iszero a
go (EVM.Types.LEq a b) = iszero (lt b a)
go (SLT a@(Lit _) b@(Lit _)) = slt a b
go (SGT a b) = SLT b a

-- IsZero
go (IsZero (IsZero (IsZero a))) = iszero a
go (IsZero (IsZero (LT x y))) = lt x y
go (IsZero (IsZero (Eq x y))) = eq x y
go (IsZero (Xor x y)) = eq x y
go (IsZero a) = iszero a

-- syntactic Eq reduction
go (Eq (Lit a) (Lit b))
| a == b = Lit 1
| otherwise = Lit 0
go (Eq (Lit 0) (Sub a b)) = eq a b
go (Eq (Lit 0) a) = iszero a
go (Eq a b)
| a == b = Lit 1
| otherwise = eq a b
Expand All @@ -995,6 +1003,10 @@ simplify e = if (mapExpr go e == e)
go (Add o1@(Lit _) o2@(Lit _)) = EVM.Expr.add o1 o2
go (Sub o1@(Lit _) o2@(Lit _)) = EVM.Expr.sub o1 o2

-- Mod
go (Mod _ (Lit 0)) = Lit 0
go (Mod a b) | a == b = Lit 0

-- double add/sub.
-- Notice that everything is done mod 2**256. So for example:
-- (a-b)+c observes the same arithmetic equalities as we are used to
Expand Down Expand Up @@ -1064,12 +1076,15 @@ simplify e = if (mapExpr go e == e)
| x == 0 = Lit 0
| x == maxLit = v
| otherwise = o
go (And a b) | a == b = a
go (And a (Not b)) | a == b = Lit 0
go o@(Or (Lit x) b)
| x == 0 = b
| otherwise = o
go o@(Or a (Lit x))
| x == 0 = a
| otherwise = o
go (Or a b) | a == b = a

-- If x is ever non zero the Or will always evaluate to some non zero value and the false branch will be unreachable
-- NOTE: with AND this does not work, because and(0x8, 0x4) = 0
Expand Down Expand Up @@ -1112,6 +1127,7 @@ simplify e = if (mapExpr go e == e)
go (Div (Lit 0) _) = Lit 0 -- divide 0 by anything (including 0) is zero in EVM
go (Div _ (Lit 0)) = Lit 0 -- divide anything by 0 is zero in EVM
go (Div a (Lit 1)) = a
-- NOTE: Div x x is NOT 1, because Div 0 0 is 0, not 1.

-- If a >= b then the value of the `Max` expression can never be < b
go o@(LT (Max (Lit a) _) (Lit b))
Expand Down
Loading