hashes 0.2.1.0 → 0.2.1.1
raw patch · 5 files changed
+73/−201 lines, 5 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- CHANGELOG.md +4/−0
- bench/Main.hs +63/−189
- hashes.cabal +1/−9
- src/Data/Hash/SipHash.hs +1/−0
- test/Test/Data/Hash/Class/Pure.hs +4/−3
CHANGELOG.md view
@@ -1,5 +1,9 @@ # Revision history for the hashes package +## 0.2.1.1 -- 2021-10-23++* Fixes for building with GHC-9.2+ ## 0.2.1.0 -- 2021-10-22 * Add OpenSSL based implementation of Keccak-512.
bench/Main.hs view
@@ -1,5 +1,7 @@+{-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE CPP #-} {-# LANGUAGE DataKinds #-}+{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-}@@ -25,9 +27,17 @@ import qualified Data.ByteArray.Hash as BA import qualified Crypto.Hash as C #endif++#if defined(WITH_OPENSSL)+import Data.Coerce+#endif++#if defined(BENCHMARK_CRYPTONITE) || defined(WITH_OPENSSL)+import qualified Data.ByteString.Short as BS+#endif+ import qualified Data.ByteString as B import qualified Data.ByteString.Unsafe as B-import qualified Data.ByteString.Short as BS import Data.Word import GHC.Ptr@@ -40,6 +50,7 @@ import qualified Data.Hash.FNV1 as FH #if defined(WITH_OPENSSL)+import qualified Data.Hash.Class.Mutable as H import qualified Data.Hash.SHA3 as SHA3 import qualified Data.Hash.SHA2 as SHA2 import qualified Data.Hash.Blake2 as BLAKE2@@ -53,76 +64,108 @@ main = do defaultMain [ bgroup "sipHash" $ []- <> [sipBench "internal" (internalSipHash 17 17)]+ <> [runBench "internal" (internalSipHash 17 17)] #if defined(BENCHMARK_CRYPTONITE)- <> [sipBench "memory" (memorySipHash 17 17)]+ <> [runBench "memory" (memorySipHash 17 17)] #endif , bgroup "fnv1aHash" $ []- <> [fnv1aBench "internal" internalFnv1a]- <> [fnv1aBench "primitive" primitiveFnv1a]+ <> [runBench "internal" internalFnv1a]+ <> [runBench "primitive" primitiveFnv1a] #if defined(BENCHMARK_CRYPTONITE)- <> [fnv1aBench "memory" memoryFnv1a]+ <> [runBench "memory" memoryFnv1a] #endif , bgroup "Sha2_256" $ [] #if defined(WITH_OPENSSL)- <> [sha2_256Bench "openssl" sha2_256Ssl]+ <> [runBench "openssl" (ossl @SHA2.Sha2_256)] #endif #if defined(BENCHMARK_CRYPTONITE)- <> [sha2_256Bench "cryptonite" cryptoniteSha2_256]+ <> [runBench "cryptonite" (cryptonite @C.SHA256)] #endif , bgroup "Sha2_512" $ [] #if defined(WITH_OPENSSL)- <> [sha2_512Bench "openssl" sha2_512Ssl]+ <> [runBench "openssl" (ossl @SHA2.Sha2_512)] #endif #if defined(BENCHMARK_CRYPTONITE)- <> [sha2_512Bench "cryptonite" cryptoniteSha2_512]+ <> [runBench "cryptonite" (cryptonite @C.SHA512)] #endif , bgroup "Sha3_256" $ [] #if defined(WITH_OPENSSL)- <> [sha3_256Bench "openssl" sha3_256Ssl]+ <> [runBench "openssl" (ossl @SHA3.Sha3_256)] #endif #if defined(BENCHMARK_CRYPTONITE)- <> [sha3_256Bench "cryptonite" cryptoniteSha3_256]+ <> [runBench "cryptonite" (cryptonite @C.SHA3_256)] #endif , bgroup "Sha3_512" $ [] #if defined(WITH_OPENSSL)- <> [sha3_512Bench "openssl" sha3_512Ssl]+ <> [runBench "openssl" (ossl @SHA3.Sha3_512)] #endif #if defined(BENCHMARK_CRYPTONITE)- <> [sha3_512Bench "cryptonite" cryptoniteSha3_512]+ <> [runBench "cryptonite" (cryptonite @C.SHA3_512)] #endif , bgroup "keccak256" $ [] #if defined(WITH_OPENSSL)- <> [keccakBench "openssl" keccak256Ssl]+ <> [runBench "openssl" (ossl @K.Keccak256)] #endif #if defined(BENCHMARK_CRYPTONITE)- <> [keccakBench "cryptonite" cryptoniteKeccak256]+ <> [runBench "cryptonite" (cryptonite @C.Keccak_256)] #endif+ , bgroup "keccak512" $ []+#if defined(WITH_OPENSSL)+ <> [runBench "openssl" (ossl @K.Keccak512)]+#endif+#if defined(BENCHMARK_CRYPTONITE)+ <> [runBench "cryptonite" (cryptonite @C.Keccak_512)]+#endif , bgroup "blake2s256" $ [] #if defined(WITH_OPENSSL)- <> [blake2s256Bench "openssl" blake2s256Ssl]+ <> [ runBench "openssl" (ossl @BLAKE2.Blake2s256) ] #endif #if defined(BENCHMARK_CRYPTONITE)- <> [blake2s256Bench "cryptonite" cryptoniteBlake2s256]+ <> [runBench "cryptonite" (cryptonite @C.Blake2s_256)] #endif , bgroup "blake2b512" $ [] #if defined(WITH_OPENSSL)- <> [ blake2b512Bench "openssl" blake2b512Ssl ]+ <> [ runBench "openssl" (ossl @BLAKE2.Blake2b512) ] #endif #if defined(BENCHMARK_CRYPTONITE)- <> [ blake2b512Bench "cryptonite" cryptoniteBlake2b512 ]+ <> [ runBench "cryptonite" (cryptonite @C.Blake2b_512) ] #endif ] -- -------------------------------------------------------------------------- --+-- Benchmark Runner++runBench :: String -> (B.ByteString -> a) -> Benchmark+runBench l f = bgroup l $ go <$> benchStrings+ where+ go i = bench (show (B.length i)) $ whnf f i++benchStrings :: [B.ByteString]+benchStrings = str <$> [0, 1, 5, 67, 300, 2000, 20000]+ where+ str (i :: Int) = B.pack $ fromIntegral <$> [0..i-1]++#if defined(BENCHMARK_CRYPTONITE)+cryptonite :: forall a . C.HashAlgorithm a => B.ByteString -> BS.ShortByteString+cryptonite b = BS.toShort $ BA.convert $! C.hash @_ @a b+{-# INLINE cryptonite #-}+#endif++#if defined(WITH_OPENSSL)+ossl :: forall a . Coercible a BS.ShortByteString => H.Hash a => B.ByteString -> BS.ShortByteString+ossl b = coerce $! SHA3.hashByteString @a b+{-# INLINE ossl #-}+#endif++-- -------------------------------------------------------------------------- -- -- SipHash #if defined(BENCHMARK_CRYPTONITE)@@ -139,16 +182,6 @@ SH.SipHash r = SH.hashByteString @(SH.SipHash 2 4) (SH.SipHashKey w0 w1) x {-# INLINE internalSipHash #-} -sipBench :: String -> (B.ByteString -> Word64) -> Benchmark-sipBench l f = bgroup l $ go <$> benchStrings- where- go i = bench (show (B.length i)) $ whnf f i--benchStrings :: [B.ByteString]-benchStrings = str <$> [0, 1, 5, 67, 300, 2000, 20000]- where- str (i :: Int) = B.pack $ fromIntegral <$> [0..i-1]- -- -------------------------------------------------------------------------- -- -- Fvn1Hash @@ -171,163 +204,4 @@ B.unsafeUseAsCStringLen b $ \(addr, n) -> fromIntegral <$!> FH.fnv1a_host (castPtr addr) n {-# INLINE primitiveFnv1a #-}--fnv1aBench :: String -> (B.ByteString -> Word64) -> Benchmark-fnv1aBench l f = bgroup l $ go <$> benchStrings- where- go i = bench (show (B.length i)) $ whnf f i---- -------------------------------------------------------------------------- ----- SHA3 256--#if defined(WITH_OPENSSL)-sha3_256Ssl :: B.ByteString -> BS.ShortByteString-sha3_256Ssl b = r- where- SHA3.Sha3_256 r = SHA3.hashByteString @SHA3.Sha3_256 b-{-# INLINE sha3_256Ssl #-}-#endif--#if defined(BENCHMARK_CRYPTONITE)-cryptoniteSha3_256 :: B.ByteString -> BS.ShortByteString-cryptoniteSha3_256 b = BS.toShort $ BA.convert $! C.hash @_ @C.SHA3_256 b-{-# INLINE cryptoniteSha3_256 #-}-#endif--sha3_256Bench :: String -> (B.ByteString -> BS.ShortByteString) -> Benchmark-sha3_256Bench l f = bgroup l $ go <$> benchStrings- where- go i = bench (show (B.length i)) $ whnf f i---- -------------------------------------------------------------------------- ----- SHA3 512--#if defined(WITH_OPENSSL)-sha3_512Ssl :: B.ByteString -> BS.ShortByteString-sha3_512Ssl b = r- where- SHA3.Sha3_512 r = SHA3.hashByteString @SHA3.Sha3_512 b-{-# INLINE sha3_512Ssl #-}-#endif--#if defined(BENCHMARK_CRYPTONITE)-cryptoniteSha3_512 :: B.ByteString -> BS.ShortByteString-cryptoniteSha3_512 b = BS.toShort $ BA.convert $! C.hash @_ @C.SHA3_512 b-{-# INLINE cryptoniteSha3_512 #-}-#endif--sha3_512Bench :: String -> (B.ByteString -> BS.ShortByteString) -> Benchmark-sha3_512Bench l f = bgroup l $ go <$> benchStrings- where- go i = bench (show (B.length i)) $ whnf f i---- -------------------------------------------------------------------------- ----- SHA2_256--#if defined(WITH_OPENSSL)-sha2_256Ssl :: B.ByteString -> BS.ShortByteString-sha2_256Ssl b = r- where- SHA2.Sha2_256 r = SHA3.hashByteString @SHA2.Sha2_256 b-{-# INLINE sha2_256Ssl #-}-#endif--#if defined(BENCHMARK_CRYPTONITE)-cryptoniteSha2_256 :: B.ByteString -> BS.ShortByteString-cryptoniteSha2_256 b = BS.toShort $ BA.convert $! C.hash @_ @C.SHA256 b-{-# INLINE cryptoniteSha2_256 #-}-#endif--sha2_256Bench :: String -> (B.ByteString -> BS.ShortByteString) -> Benchmark-sha2_256Bench l f = bgroup l $ go <$> benchStrings- where- go i = bench (show (B.length i)) $ whnf f i---- -------------------------------------------------------------------------- ----- SHA2_512--#if defined(WITH_OPENSSL)-sha2_512Ssl :: B.ByteString -> BS.ShortByteString-sha2_512Ssl b = r- where- SHA2.Sha2_512 r = SHA3.hashByteString @SHA2.Sha2_512 b-{-# INLINE sha2_512Ssl #-}-#endif--#if defined(BENCHMARK_CRYPTONITE)-cryptoniteSha2_512 :: B.ByteString -> BS.ShortByteString-cryptoniteSha2_512 b = BS.toShort $ BA.convert $! C.hash @_ @C.SHA512 b-{-# INLINE cryptoniteSha2_512 #-}-#endif--sha2_512Bench :: String -> (B.ByteString -> BS.ShortByteString) -> Benchmark-sha2_512Bench l f = bgroup l $ go <$> benchStrings- where- go i = bench (show (B.length i)) $ whnf f i---- -------------------------------------------------------------------------- ----- BLAKE2B512--#if defined(WITH_OPENSSL)-blake2b512Ssl :: B.ByteString -> BS.ShortByteString-blake2b512Ssl b = r- where- BLAKE2.Blake2b512 r = BLAKE2.hashByteString @BLAKE2.Blake2b512 b-{-# INLINE blake2b512Ssl #-}-#endif--#if defined(BENCHMARK_CRYPTONITE)-cryptoniteBlake2b512 :: B.ByteString -> BS.ShortByteString-cryptoniteBlake2b512 b = BS.toShort $ BA.convert $! C.hash @_ @C.Blake2b_512 b-{-# INLINE cryptoniteBlake2b512 #-}-#endif--blake2b512Bench :: String -> (B.ByteString -> BS.ShortByteString) -> Benchmark-blake2b512Bench l f = bgroup l $ go <$> benchStrings- where- go i = bench (show (B.length i)) $ whnf f i---- -------------------------------------------------------------------------- ----- BLAKE2S256--#if defined(WITH_OPENSSL)-blake2s256Ssl :: B.ByteString -> BS.ShortByteString-blake2s256Ssl b = r- where- BLAKE2.Blake2s256 r = BLAKE2.hashByteString @BLAKE2.Blake2s256 b-{-# INLINE blake2s256Ssl #-}-#endif--#if defined(BENCHMARK_CRYPTONITE)-cryptoniteBlake2s256 :: B.ByteString -> BS.ShortByteString-cryptoniteBlake2s256 b = BS.toShort $ BA.convert $! C.hash @_ @C.Blake2s_256 b-{-# INLINE cryptoniteBlake2s256 #-}-#endif--blake2s256Bench :: String -> (B.ByteString -> BS.ShortByteString) -> Benchmark-blake2s256Bench l f = bgroup l $ go <$> benchStrings- where- go i = bench (show (B.length i)) $ whnf f i---- -------------------------------------------------------------------------- ----- Keccak 256--#if defined(WITH_OPENSSL)-keccak256Ssl :: B.ByteString -> BS.ShortByteString-keccak256Ssl b = r- where- K.Keccak256 r = K.hashByteString @K.Keccak256 b-{-# INLINE keccak256Ssl #-}-#endif--#if defined(BENCHMARK_CRYPTONITE)-cryptoniteKeccak256 :: B.ByteString -> BS.ShortByteString-cryptoniteKeccak256 b = BS.toShort $ BA.convert $! C.hash @_ @C.Keccak_256 b-{-# INLINE cryptoniteKeccak256 #-}-#endif--keccakBench :: String -> (B.ByteString -> BS.ShortByteString) -> Benchmark-keccakBench l f = bgroup l $ go <$> benchStrings- where- go i = bench (show (B.length i)) $ whnf f i
hashes.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.4 name: hashes-version: 0.2.1.0+version: 0.2.1.1 synopsis: Hash functions Description: Efficient implementations of hash functions homepage: https://github.com/larskuhtz/hs-hashes@@ -54,14 +54,6 @@ cbits/keccak.h cbits/keccak.c cpp-options: -DWITH_OPENSSL=1- include-dirs:- /usr/local/opt/openssl@3/include- /usr/local/opt/openssl/include- /opt/local/include- extra-lib-dirs:- /usr/local/opt/openssl@3/lib/- /usr/local/opt/openssl/lib/- /opt/local/lib if flag(openssl-use-pkg-config) pkgconfig-depends: libcrypto else
src/Data/Hash/SipHash.hs view
@@ -1,6 +1,7 @@ {-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE BangPatterns #-} {-# LANGUAGE DataKinds #-}+{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE MultiParamTypeClasses #-}
test/Test/Data/Hash/Class/Pure.hs view
@@ -85,9 +85,10 @@ prop_hashStorable b = word8sToWord64 (_getTestHash $ hashStorable @TestHash () b) === b prop_hashPtr :: [Word8] -> Property-prop_hashPtr b = unsafeDupablePerformIO $- B.unsafeUseAsCStringLen (B.pack b) $ \(ptr, len) -> do- return $ unsafeDupablePerformIO (hashPtr @TestHash () (castPtr ptr) len) === TestHash b+prop_hashPtr b = ioProperty $+ B.unsafeUseAsCStringLen (B.pack b) $ \(!ptr, !len) -> do+ !r <- hashPtr @TestHash () (castPtr ptr) len+ return $! r === TestHash b prop_hashByteString :: [Word8] -> Property prop_hashByteString b = hashByteString @TestHash () (B.pack b) === TestHash b