diff --git a/benchs/Bench.hs b/benchs/Bench.hs new file mode 100644 index 0000000..ecd33bb --- /dev/null +++ b/benchs/Bench.hs @@ -0,0 +1,63 @@ +{-# LANGUAGE PackageImports #-} +module Main where + +import Criterion.Main + +import "cryptonite" Crypto.Hash +import "cryptonite" Crypto.Error +import "cryptonite" Crypto.Cipher.DES +import "cryptonite" Crypto.Cipher.Camellia +import "cryptonite" Crypto.Cipher.AES +import "cryptonite" Crypto.Cipher.Blowfish +import "cryptonite" Crypto.Cipher.Types + +import Crypto.Internal.ByteArray (ByteArray) + +import qualified Data.ByteString as B + +benchHash = + [ + ] + +benchBlockCipher = + [ bgroup "ECB" benchECB + , bgroup "CBC" benchCBC + ] + where + benchECB = + [ bench "DES-input=1024" $ nf (run (undefined :: DES) cipherInit key8) input1024 + , bench "Blowfish128-input=1024" $ nf (run (undefined :: Blowfish128) cipherInit key16) input1024 + , bench "AES128-input=1024" $ nf (run (undefined :: AES128) cipherInit key16) input1024 + , bench "AES256-input=1024" $ nf (run (undefined :: AES256) cipherInit key32) input1024 + ] + where run :: (ByteArray ba, ByteArray key, BlockCipher c) + => c -> (key -> CryptoFailable c) -> key -> ba -> ba + run witness initF key input = + (ecbEncrypt (throwCryptoError (initF key))) input + + benchCBC = + [ bench "DES-input=1024" $ nf (run (undefined :: DES) cipherInit key8 iv8) input1024 + , bench "Blowfish128-input=1024" $ nf (run (undefined :: Blowfish128) cipherInit key16 iv8) input1024 + , bench "AES128-input=1024" $ nf (run (undefined :: AES128) cipherInit key16 iv16) input1024 + , bench "AES256-input=1024" $ nf (run (undefined :: AES256) cipherInit key32 iv16) input1024 + ] + where run :: (ByteArray ba, ByteArray key, BlockCipher c) + => c -> (key -> CryptoFailable c) -> key -> IV c -> ba -> ba + run witness initF key iv input = + (cbcEncrypt (throwCryptoError (initF key))) iv input + + key8 = B.replicate 8 0 + key16 = B.replicate 16 0 + key32 = B.replicate 32 0 + input1024 = B.replicate 1024 0 + + iv8 :: BlockCipher c => IV c + iv8 = maybe (error "iv size 8") id $ makeIV key8 + + iv16 :: BlockCipher c => IV c + iv16 = maybe (error "iv size 16") id $ makeIV key16 + +main = defaultMain + [ bgroup "hash" benchHash + , bgroup "block-cipher" benchBlockCipher + ]