Skip to content

Commit 428483c

Browse files
authored
Use bit instead of power of two (2 ^ i) (#900)
* Use `bit` instead of power of two (2 ^ i) We are computing powers of two, so using `bit` performs better. * Consider case with i=256 2^256 is not representable in a Word256.
1 parent 8dc4d40 commit 428483c

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

lib/Echidna/ABI.hs

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import Control.Monad (join, liftM2, liftM3, foldM, replicateM)
99
import Control.Monad.Random.Strict (MonadRandom, getRandom, getRandoms, getRandomR, uniformMay)
1010
import Control.Monad.Random.Strict qualified as R
1111
import Data.Binary.Put (runPut, putWord32be)
12+
import Data.BinaryWord (unsignedWord)
13+
import Data.Bits (bit)
1214
import Data.Bool (bool)
1315
import Data.ByteString.Lazy as BSLazy (toStrict)
1416
import Data.ByteString (ByteString)
@@ -49,10 +51,11 @@ commonTypeSizes :: [Int]
4951
commonTypeSizes = [8,16..256]
5052

5153
mkValidAbiInt :: Int -> Int256 -> Maybe AbiValue
52-
mkValidAbiInt i x = if abs x <= 2 ^ (i - 1) - 1 then Just $ AbiInt i x else Nothing
54+
mkValidAbiInt i x = if unsignedWord (abs x) < bit (i - 1) then Just $ AbiInt i x else Nothing
5355

5456
mkValidAbiUInt :: Int -> Word256 -> Maybe AbiValue
55-
mkValidAbiUInt i x = if x <= 2 ^ i - 1 then Just $ AbiUInt i x else Nothing
57+
mkValidAbiUInt 256 x = Just $ AbiUInt 256 x
58+
mkValidAbiUInt i x = if x < bit i then Just $ AbiUInt i x else Nothing
5659

5760
makeNumAbiValues :: Integer -> [AbiValue]
5861
makeNumAbiValues i = let l f = f <$> commonTypeSizes <*> fmap fromIntegral ([i-1..i+1] ++ [(-i)-1 .. (-i)+1]) in

package.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies:
1515
- brick
1616
- bytestring
1717
- containers
18+
- data-bword
1819
- data-dword
1920
- data-has
2021
- extra

0 commit comments

Comments
 (0)