commit c94c953fcab373c524b5f2f5bf9a55a106411a6c
parent 66f9cce2d1b3efaea49326b6f68ec530ff41edce
Author: Jared Tobin <jared@jtobin.io>
Date: Fri, 13 Sep 2024 23:54:17 +0400
lib: replace hash_lazy with unlifted variant
Diffstat:
4 files changed, 5 insertions(+), 30 deletions(-)
diff --git a/bench/Main.hs b/bench/Main.hs
@@ -6,6 +6,7 @@ import Criterion.Main
import qualified Crypto.Hash.SHA256 as SHA256
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
+import qualified Data.Digest.Pure.SHA as SHA
main :: IO ()
main = defaultMain [
@@ -18,7 +19,7 @@ suite = env setup $ \ ~(bs, bl) ->
bgroup "SHA256 (32B input)" [
bench "hash" $ whnf SHA256.hash bs
, bench "hash_lazy" $ whnf SHA256.hash_lazy bl
- , bench "u_hash_lazy" $ whnf SHA256.u_hash_lazy bl
+ , bench "SHA.sha256" $ whnf SHA.sha256 bl
]
, bgroup "HMAC-SHA256 (32B input)" [
bench "hmac" $ whnf (SHA256.hmac "key") bs
diff --git a/lib/Crypto/Hash/SHA256.hs b/lib/Crypto/Hash/SHA256.hs
@@ -22,7 +22,6 @@ module Crypto.Hash.SHA256 (
-- * SHA-256 message digest functions
hash
, hash_lazy
- , u_hash_lazy
-- * SHA256-based MAC functions
, hmac
@@ -709,21 +708,7 @@ hash =
-- >>> hash_lazy "lazy bytestring input"
-- "<strict 256-bit message digest>"
hash_lazy :: BL.ByteString -> BS.ByteString
-hash_lazy =
- cat
- . L.foldl' hash_alg iv
- . blocks_lazy 64
- . pad_lazy
-
--- | Compute a condensed representation of a lazy bytestring via
--- SHA-256.
---
--- The 256-bit output digest is returned as a strict bytestring.
---
--- >>> hash_lazy "lazy bytestring input"
--- "<strict 256-bit message digest>"
-u_hash_lazy :: BL.ByteString -> BS.ByteString
-u_hash_lazy bl = cat# (go r_iv (pad_lazy bl)) where
+hash_lazy bl = cat# (go r_iv (pad_lazy bl)) where
r_iv = (#
0x6a09e667#Word32, 0xbb67ae85#Word32
, 0x3c6ef372#Word32, 0xa54ff53a#Word32
diff --git a/ppad-sha256.cabal b/ppad-sha256.cabal
@@ -53,13 +53,14 @@ benchmark sha256-bench
main-is: Main.hs
ghc-options:
- -rtsopts -O2 -Wall -fno-warn-orphans
+ -rtsopts -O2 -Wall
build-depends:
base
, bytestring
, criterion
, ppad-sha256
+ , SHA
executable hash-large
main-is: HashLarge.hs
diff --git a/test/Main.hs b/test/Main.hs
@@ -134,13 +134,6 @@ unit_tests = testGroup "ppad-sha256" [
, cmp_hash_lazy "hv3" hv3_put hv3_pec
, cmp_hash_lazy "hv4" hv4_put hv4_pec
]
- , testGroup "u_hash_lazy" [
- cmp_u_hash_lazy "hv0" hv0_put hv0_pec
- , cmp_u_hash_lazy "hv1" hv1_put hv1_pec
- , cmp_u_hash_lazy "hv2" hv2_put hv2_pec
- , cmp_u_hash_lazy "hv3" hv3_put hv3_pec
- , cmp_u_hash_lazy "hv4" hv4_put hv4_pec
- ]
-- uncomment me to run (slow, ~40s)
--
-- , testGroup "hash_lazy (1GB input)" [
@@ -197,11 +190,6 @@ cmp_hash_lazy msg (BL.fromStrict -> put) pec = testCase msg $ do
let out = B16.encode (SHA256.hash_lazy put)
assertEqual mempty pec out
-cmp_u_hash_lazy :: String -> BS.ByteString -> BS.ByteString -> TestTree
-cmp_u_hash_lazy msg (BL.fromStrict -> put) pec = testCase msg $ do
- let out = B16.encode (SHA256.u_hash_lazy put)
- assertEqual mempty pec out
-
cmp_hmac
:: String -> BS.ByteString -> BS.ByteString -> BS.ByteString -> TestTree
cmp_hmac msg key put pec = testCase msg $ do