Skip to content

Commit 2931f09

Browse files
authored
Merge pull request #800 from ethereum/fixing-missing-sar
Adding missing SAR simplification
2 parents 24cd01a + 3d714c2 commit 2931f09

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6969
- PLEq is now concretized in case it can be computed
7070
- More SignExtend test cases
7171
- Rewrite rules to deal with specific exponentiation patterns
72+
- Added missing simplification and concretization of the SAR instruction
7273

7374
## Fixed
7475
- We now try to simplify expressions fully before trying to cast them to a concrete value

src/EVM/Expr.hs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ sar :: Expr EWord -> Expr EWord -> Expr EWord
197197
sar = op2 SAR (\x y ->
198198
let msb = testBit y 255
199199
asSigned = fromIntegral y :: Int256
200-
in if x > 256 then
200+
in if x >= 255 then
201201
if msb then maxBound else 0
202202
else
203203
fromIntegral $ shiftR asSigned (fromIntegral x))
@@ -1213,7 +1213,7 @@ simplifyNoLitToKeccak e = untilFixpoint (mapExpr go) e
12131213

12141214
go (EqByte a b) = eqByte a b
12151215

1216-
-- SHL / SHR by 0
1216+
-- SHL / SHR / SAR
12171217
go (SHL a v)
12181218
| a == (Lit 0) = v
12191219
| v == (Lit 0) = v
@@ -1222,6 +1222,11 @@ simplifyNoLitToKeccak e = untilFixpoint (mapExpr go) e
12221222
| a == (Lit 0) = v
12231223
| v == (Lit 0) = v
12241224
| otherwise = shr a v
1225+
go (SAR _ (Lit v)) | v == maxBound = Lit v
1226+
go (SAR a v)
1227+
| a == (Lit 0) = v
1228+
| v == (Lit 0) = v
1229+
| otherwise = sar a v
12251230

12261231
-- Bitwise AND & OR. These MUST preserve bitwise equivalence
12271232
go (And a b)
@@ -1329,7 +1334,7 @@ simplifyNoLitToKeccak e = untilFixpoint (mapExpr go) e
13291334
simplifyProps :: [Prop] -> [Prop]
13301335
simplifyProps ps = if cannotBeSat then [PBool False] else simplified
13311336
where
1332-
simplified = if (goOne ps == ps) then ps else simplifyProps (goOne ps)
1337+
simplified = untilFixpoint goOne ps
13331338
cannotBeSat = PBool False `elem` simplified
13341339
goOne :: [Prop] -> [Prop]
13351340
goOne = remRedundantProps . map simplifyProp . constPropagate . flattenProps
@@ -1770,7 +1775,7 @@ constPropagate ps =
17701775
concKeccakSimpExpr :: Expr a -> Expr a
17711776
concKeccakSimpExpr orig = untilFixpoint (simplifyNoLitToKeccak . (mapExpr concKeccakOnePass)) (simplify orig)
17721777

1773-
-- Only concretize Keccak in Props
1778+
-- Concretize Keccak in Props, but don't simplify
17741779
-- Needed because if it also simplified, we may not find some simplification errors, as
17751780
-- simplification would always be ON
17761781
concKeccakProps :: [Prop] -> [Prop]

0 commit comments

Comments
 (0)