{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Bench.Crypto.HASH (
  benchmarks,
) where

import Cardano.Binary
import Data.Proxy

import Cardano.Crypto.Hash.Blake2b
import Cardano.Crypto.Hash.Class

import Criterion

import Bench.Crypto.BenchData

benchmarks :: Benchmark
benchmarks :: Benchmark
benchmarks =
  String -> [Benchmark] -> Benchmark
bgroup
    String
"HASH"
    [ forall (proxy :: * -> *) h.
HashAlgorithm h =>
proxy h -> String -> Benchmark
benchHASH (forall {k} (t :: k). Proxy t
Proxy @Blake2b_224) String
"Blake2b_224"
    , forall (proxy :: * -> *) h.
HashAlgorithm h =>
proxy h -> String -> Benchmark
benchHASH (forall {k} (t :: k). Proxy t
Proxy @Blake2b_256) String
"Blake2b_256"
    ]

benchHASH ::
  forall proxy h.
  HashAlgorithm h =>
  proxy h ->
  [Char] ->
  Benchmark
benchHASH :: forall (proxy :: * -> *) h.
HashAlgorithm h =>
proxy h -> String -> Benchmark
benchHASH proxy h
_ String
lbl =
  String -> [Benchmark] -> Benchmark
bgroup
    String
lbl
    [ String -> Benchmarkable -> Benchmark
bench String
"hashWith" forall a b. (a -> b) -> a -> b
$
        forall b a. NFData b => (a -> b) -> a -> Benchmarkable
nf (forall h a. HashAlgorithm h => (a -> ByteString) -> a -> Hash h a
hashWith @h forall a. a -> a
id) ByteString
testBytes
    , forall env. NFData env => IO env -> (env -> Benchmark) -> Benchmark
env (forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. ToCBOR a => a -> ByteString
serialize' (forall h a. HashAlgorithm h => (a -> ByteString) -> a -> Hash h a
hashWith @h forall a. a -> a
id ByteString
testBytes))) forall a b. (a -> b) -> a -> b
$
        String -> Benchmarkable -> Benchmark
bench String
"decodeHash"
          forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. NFData b => (a -> b) -> a -> Benchmarkable
nf (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a. HasCallStack => String -> a
error forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show) (forall a. a -> a
id @(Hash h ByteString)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. FromCBOR a => ByteString -> Either DecoderError a
decodeFull')
    ]