diff --git a/Crypto/Internal/Compat.hs b/Crypto/Internal/Compat.hs index e89a66b..ea53db7 100644 --- a/Crypto/Internal/Compat.hs +++ b/Crypto/Internal/Compat.hs @@ -11,9 +11,11 @@ {-# LANGUAGE CPP #-} module Crypto.Internal.Compat ( unsafeDoIO + , popCount ) where import System.IO.Unsafe +import Data.Bits (popCount) -- | perform io for hashes that do allocation and ffi. -- unsafeDupablePerformIO is used when possible as the @@ -26,3 +28,10 @@ unsafeDoIO = unsafeDupablePerformIO #else unsafeDoIO = unsafePerformIO #endif + +#if !(MIN_VERSION_base(4,5,0)) +popCount :: Word64 -> Int +popCount n = loop 0 n + where loop c 0 = c + loop c i = loop (c + if testBit c 0 then 1 else 0) (i `shiftR` 1) +#endif diff --git a/Crypto/KDF/Scrypt.hs b/Crypto/KDF/Scrypt.hs index a1f3980..55b9f51 100644 --- a/Crypto/KDF/Scrypt.hs +++ b/Crypto/KDF/Scrypt.hs @@ -16,7 +16,6 @@ module Crypto.KDF.Scrypt ) where import Data.Word -import Data.Bits import Data.Byteable import Data.ByteString (ByteString) import qualified Data.ByteString as B @@ -30,6 +29,7 @@ import System.IO.Unsafe import Crypto.Hash (SHA256(..)) import qualified Crypto.KDF.PBKDF2 as PBKDF2 +import Crypto.Internal.Compat (popCount) -- | Parameters for Scrypt data Parameters = Parameters @@ -44,13 +44,6 @@ data Parameters = Parameters foreign import ccall "cryptonite_scrypt_smix" ccryptonite_scrypt_smix :: Ptr Word8 -> Word32 -> Word64 -> Ptr Word8 -> Ptr Word8 -> IO () -#if !(MIN_VERSION_base(4,5,0)) -popCount :: Word64 -> Int -popCount n = loop 0 n - where loop c 0 = c - loop c i = loop (c + if testBit c 0 then 1 else 0) (i `shiftR` 1) -#endif - -- | Generate the scrypt key derivation data generate :: Parameters -> B.ByteString generate params