{-# LANGUAGE MultiParamTypeClasses #-}
import qualified Data.CryptoHash.SHA256 as SHA
import Benchmark.Crypto
import Criterion
import Data.Tagged
import Data.Crypto.Classes
import Criterion.Main
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import Data.Serialize as Ser
import Data.Serialize.Put as S
import Data.Binary as Bin
import Data.Binary.Put as P
import Control.Monad (liftM)
import System.Environment (getArgs)
import Codec.Crypto.SimpleAES as AES
import Test.Crypto
-- 128KB strings
ps = B.replicate (2^27) 0
lps = L.replicate (2^27) 0

toKey = AESKey
data AESKey = AESKey B.ByteString

-- SimpleAES is a little dumb wrt IV
damnedIV = B.replicate 16 0

instance BlockCipher AESKey where
	blockSize = Tagged 128
	encryptBlock (AESKey k) bs = B.concat . L.toChunks $ crypt ECB k damnedIV Encrypt (L.fromChunks [bs]) 
	decryptBlock (AESKey k) bs = B.concat . L.toChunks $ crypt ECB k damnedIV Decrypt (L.fromChunks [bs])
	buildKey bs
		| B.length bs >= 32 = Just $ toKey bs
		| B.length bs >= 24 = Just $ toKey bs
		| B.length bs >= 16 = Just $ toKey bs
		| otherwise = Nothing
	keyLength (AESKey k) = B.length k * 8

instance Serialize AESKey where
        get = undefined
        put _ = undefined

instance Binary AESKey where
        get = undefined
        put _ = undefined

main = do
	let (Just k128) = buildKey (B.pack [0..15]) :: Maybe AESKey
	    (Just k192) = buildKey (B.pack [0..23]) :: Maybe AESKey
	    (Just k256) = buildKey (B.pack [0..31]) :: Maybe AESKey
	defaultMain	[ benchmarkBlockCipher k128 "SimpleAES-128"
			, benchmarkBlockCipher k192 "SimpleAES-192"
			, benchmarkBlockCipher k256 "SimpleAES-256"]

{-

[tommd@Mavlo Test]$ ghc --make aes.hs -O2
[1 of 1] Compiling Main             ( aes.hs, aes.o )
Linking aes ...
[tommd@Mavlo Test]$ ./aes
warming up
estimating clock resolution...
mean is 13.42969 us (40001 iterations)
found 19199 outliers among 39999 samples (48.0%)
  9995 (25.0%) low severe
  9204 (23.0%) high severe
estimating cost of a clock call...
mean is 1.603564 us (62 iterations)
found 3 outliers among 62 samples (4.8%)
  3 (4.8%) high severe

benchmarking SimpleAES-128/enc
collecting 100 samples, 1 iterations each, in estimated 45.45150 s
bootstrapping with 100000 resamples
mean: 367.1993 ms, lb 365.1902 ms, ub 370.0333 ms, ci 0.950
std dev: 12.05922 ms, lb 9.232010 ms, ub 19.61416 ms, ci 0.950
found 1 outliers among 100 samples (1.0%)
  1 (1.0%) high severe
variance introduced by outliers: 0.999%
variance is unaffected by outliers

benchmarking SimpleAES-128/dec
collecting 100 samples, 1 iterations each, in estimated 39.95039 s
bootstrapping with 100000 resamples
mean: 386.8182 ms, lb 385.1160 ms, ub 388.6453 ms, ci 0.950
std dev: 9.071388 ms, lb 8.356004 ms, ub 10.69953 ms, ci 0.950
variance introduced by outliers: 0.997%
variance is unaffected by outliers

benchmarking SimpleAES-192/enc
collecting 100 samples, 1 iterations each, in estimated 37.85820 s
bootstrapping with 100000 resamples
mean: 372.3900 ms, lb 370.7320 ms, ub 374.0644 ms, ci 0.950
std dev: 8.536895 ms, lb 8.024271 ms, ub 9.092091 ms, ci 0.950
variance introduced by outliers: 0.997%
variance is unaffected by outliers

benchmarking SimpleAES-192/dec
collecting 100 samples, 1 iterations each, in estimated 39.69769 s
bootstrapping with 100000 resamples
mean: 402.1715 ms, lb 400.3723 ms, ub 403.8912 ms, ci 0.950
std dev: 9.021947 ms, lb 8.269479 ms, ub 9.845174 ms, ci 0.950
variance introduced by outliers: 0.997%
variance is unaffected by outliers

benchmarking SimpleAES-256/enc
collecting 100 samples, 1 iterations each, in estimated 40.79909 s
bootstrapping with 100000 resamples
mean: 398.0561 ms, lb 396.2561 ms, ub 399.8757 ms, ci 0.950
std dev: 9.271390 ms, lb 8.639287 ms, ub 9.991074 ms, ci 0.950
variance introduced by outliers: 0.997%
variance is unaffected by outliers

benchmarking SimpleAES-256/dec
collecting 100 samples, 1 iterations each, in estimated 43.98489 s
bootstrapping with 100000 resamples
mean: 451.3232 ms, lb 449.6229 ms, ub 452.9410 ms, ci 0.950
std dev: 8.448031 ms, lb 7.700411 ms, ub 9.353656 ms, ci 0.950
variance introduced by outliers: 0.996%
variance is unaffected by outliers

-}
