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.
pull/908/head
Emilio López 2 years ago committed by GitHub
parent 8dc4d40d03
commit 428483c506
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      lib/Echidna/ABI.hs
  2. 1
      package.yaml

@ -9,6 +9,8 @@ import Control.Monad (join, liftM2, liftM3, foldM, replicateM)
import Control.Monad.Random.Strict (MonadRandom, getRandom, getRandoms, getRandomR, uniformMay)
import Control.Monad.Random.Strict qualified as R
import Data.Binary.Put (runPut, putWord32be)
import Data.BinaryWord (unsignedWord)
import Data.Bits (bit)
import Data.Bool (bool)
import Data.ByteString.Lazy as BSLazy (toStrict)
import Data.ByteString (ByteString)
@ -49,10 +51,11 @@ commonTypeSizes :: [Int]
commonTypeSizes = [8,16..256]
mkValidAbiInt :: Int -> Int256 -> Maybe AbiValue
mkValidAbiInt i x = if abs x <= 2 ^ (i - 1) - 1 then Just $ AbiInt i x else Nothing
mkValidAbiInt i x = if unsignedWord (abs x) < bit (i - 1) then Just $ AbiInt i x else Nothing
mkValidAbiUInt :: Int -> Word256 -> Maybe AbiValue
mkValidAbiUInt i x = if x <= 2 ^ i - 1 then Just $ AbiUInt i x else Nothing
mkValidAbiUInt 256 x = Just $ AbiUInt 256 x
mkValidAbiUInt i x = if x < bit i then Just $ AbiUInt i x else Nothing
makeNumAbiValues :: Integer -> [AbiValue]
makeNumAbiValues i = let l f = f <$> commonTypeSizes <*> fmap fromIntegral ([i-1..i+1] ++ [(-i)-1 .. (-i)+1]) in

@ -15,6 +15,7 @@ dependencies:
- brick
- bytestring
- containers
- data-bword
- data-dword
- data-has
- extra

Loading…
Cancel
Save