commit 66f9cce2d1b3efaea49326b6f68ec530ff41edce
parent f1600bc1a6088232e44f8ed54f9044df6b5c4561
Author: Jared Tobin <jared@jtobin.io>
Date: Fri, 13 Sep 2024 23:38:57 +0400
lib: update tests, benchmarks
Diffstat:
3 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/bench/Main.hs b/bench/Main.hs
@@ -18,6 +18,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
]
, 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,6 +22,7 @@ module Crypto.Hash.SHA256 (
-- * SHA-256 message digest functions
hash
, hash_lazy
+ , u_hash_lazy
-- * SHA256-based MAC functions
, hmac
@@ -160,7 +161,7 @@ pad m = BL.toStrict . BSB.toLazyByteString $ padded where
padded = BSB.byteString m <> fill (sol l) (BSB.word8 0x80)
- fill j acc
+ fill j !acc
| j == 0 = acc <> BSB.word64BE (l * 8)
| otherwise = fill (pred j) (acc <> BSB.word8 0x00)
@@ -721,8 +722,8 @@ hash_lazy =
--
-- >>> hash_lazy "lazy bytestring input"
-- "<strict 256-bit message digest>"
-hash_lazy# :: BL.ByteString -> BS.ByteString
-hash_lazy# bl = cat# (go r_iv (pad_lazy bl)) where
+u_hash_lazy :: BL.ByteString -> BS.ByteString
+u_hash_lazy bl = cat# (go r_iv (pad_lazy bl)) where
r_iv = (#
0x6a09e667#Word32, 0xbb67ae85#Word32
, 0x3c6ef372#Word32, 0xa54ff53a#Word32
diff --git a/test/Main.hs b/test/Main.hs
@@ -134,6 +134,13 @@ 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)" [
@@ -190,6 +197,11 @@ 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