{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE BangPatterns #-}

module Crypto.Hash.SHA512
    (
      SHA512
    , SHA384
    ) where

import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Lazy.Char8 as LC
import           Data.ByteString (ByteString)
import           Data.ByteString.Builder
import           Control.Monad.ST
import           Data.Int
import           Data.Word
import           Data.Bits
import           Data.Monoid
import           Data.Array.Unboxed
import           Data.Array.Unsafe
import           Data.Array.ST
import           Data.List(foldl')

import           Crypto.Hash.ADT

initHs :: [Word64]
initHs :: [Word64]
initHs = [
    Word64
0x6a09e667f3bcc908, Word64
0xbb67ae8584caa73b, Word64
0x3c6ef372fe94f82b, Word64
0xa54ff53a5f1d36f1
  , Word64
0x510e527fade682d1, Word64
0x9b05688c2b3e6c1f, Word64
0x1f83d9abfb41bd6b, Word64
0x5be0cd19137e2179  ]

initKs :: [Word64]
initKs :: [Word64]
initKs = [
    Word64
0x428a2f98d728ae22, Word64
0x7137449123ef65cd, Word64
0xb5c0fbcfec4d3b2f, Word64
0xe9b5dba58189dbbc, Word64
0x3956c25bf348b538
  , Word64
0x59f111f1b605d019, Word64
0x923f82a4af194f9b, Word64
0xab1c5ed5da6d8118, Word64
0xd807aa98a3030242, Word64
0x12835b0145706fbe
  , Word64
0x243185be4ee4b28c, Word64
0x550c7dc3d5ffb4e2, Word64
0x72be5d74f27b896f, Word64
0x80deb1fe3b1696b1, Word64
0x9bdc06a725c71235
  , Word64
0xc19bf174cf692694, Word64
0xe49b69c19ef14ad2, Word64
0xefbe4786384f25e3, Word64
0x0fc19dc68b8cd5b5, Word64
0x240ca1cc77ac9c65
  , Word64
0x2de92c6f592b0275, Word64
0x4a7484aa6ea6e483, Word64
0x5cb0a9dcbd41fbd4, Word64
0x76f988da831153b5, Word64
0x983e5152ee66dfab
  , Word64
0xa831c66d2db43210, Word64
0xb00327c898fb213f, Word64
0xbf597fc7beef0ee4, Word64
0xc6e00bf33da88fc2, Word64
0xd5a79147930aa725
  , Word64
0x06ca6351e003826f, Word64
0x142929670a0e6e70, Word64
0x27b70a8546d22ffc, Word64
0x2e1b21385c26c926, Word64
0x4d2c6dfc5ac42aed
  , Word64
0x53380d139d95b3df, Word64
0x650a73548baf63de, Word64
0x766a0abb3c77b2a8, Word64
0x81c2c92e47edaee6, Word64
0x92722c851482353b
  , Word64
0xa2bfe8a14cf10364, Word64
0xa81a664bbc423001, Word64
0xc24b8b70d0f89791, Word64
0xc76c51a30654be30, Word64
0xd192e819d6ef5218
  , Word64
0xd69906245565a910, Word64
0xf40e35855771202a, Word64
0x106aa07032bbd1b8, Word64
0x19a4c116b8d2d0c8, Word64
0x1e376c085141ab53
  , Word64
0x2748774cdf8eeb99, Word64
0x34b0bcb5e19b48a8, Word64
0x391c0cb3c5c95a63, Word64
0x4ed8aa4ae3418acb, Word64
0x5b9cca4f7763e373
  , Word64
0x682e6ff3d6b2b8a3, Word64
0x748f82ee5defb2fc, Word64
0x78a5636f43172f60, Word64
0x84c87814a1f0ab72, Word64
0x8cc702081a6439ec
  , Word64
0x90befffa23631e28, Word64
0xa4506cebde82bde9, Word64
0xbef9a3f7b2c67915, Word64
0xc67178f2e372532b, Word64
0xca273eceea26619c
  , Word64
0xd186b8c721c0c207, Word64
0xeada7dd6cde0eb1e, Word64
0xf57d4f7fee6ed178, Word64
0x06f067aa72176fba, Word64
0x0a637dc5a2c898a6
  , Word64
0x113f9804bef90dae, Word64
0x1b710b35131c471b, Word64
0x28db77f523047d84, Word64
0x32caab7b40c72493, Word64
0x3c9ebe0a15c9bebc
  , Word64
0x431d67c49c100d4c, Word64
0x4cc5d4becb3e42b6, Word64
0x597f299cfc657e2a, Word64
0x5fcb6fab3ad6faec, Word64
0x6c44198c4a475817  ]

encodeInt64Helper :: Int64 -> [Word8]
encodeInt64Helper :: Int64 -> [Word8]
encodeInt64Helper Int64
x_ = (forall a. Int -> a -> [a]
replicate Int
8 Word8
0) forall a. Semigroup a => a -> a -> a
<> [Word8
w7, Word8
w6, Word8
w5, Word8
w4, Word8
w3, Word8
w2, Word8
w1, Word8
w0]
  where x :: Int64
x = Int64
x_ forall a. Num a => a -> a -> a
* Int64
8
        w7 :: Word8
w7 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ (Int64
x forall a. Bits a => a -> Int -> a
`shiftR` Int
56) forall a. Bits a => a -> a -> a
.&. Int64
0xff
        w6 :: Word8
w6 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ (Int64
x forall a. Bits a => a -> Int -> a
`shiftR` Int
48) forall a. Bits a => a -> a -> a
.&. Int64
0xff
        w5 :: Word8
w5 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ (Int64
x forall a. Bits a => a -> Int -> a
`shiftR` Int
40) forall a. Bits a => a -> a -> a
.&. Int64
0xff
        w4 :: Word8
w4 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ (Int64
x forall a. Bits a => a -> Int -> a
`shiftR` Int
32) forall a. Bits a => a -> a -> a
.&. Int64
0xff
        w3 :: Word8
w3 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ (Int64
x forall a. Bits a => a -> Int -> a
`shiftR` Int
24) forall a. Bits a => a -> a -> a
.&. Int64
0xff
        w2 :: Word8
w2 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ (Int64
x forall a. Bits a => a -> Int -> a
`shiftR` Int
16) forall a. Bits a => a -> a -> a
.&. Int64
0xff
        w1 :: Word8
w1 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ (Int64
x forall a. Bits a => a -> Int -> a
`shiftR`  Int
8) forall a. Bits a => a -> a -> a
.&. Int64
0xff
        w0 :: Word8
w0 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ (Int64
x forall a. Bits a => a -> Int -> a
`shiftR`  Int
0) forall a. Bits a => a -> a -> a
.&. Int64
0xff

encodeInt64 :: Int64 -> ByteString
encodeInt64 :: Int64 -> ByteString
encodeInt64 = [Word8] -> ByteString
B.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> [Word8]
encodeInt64Helper

sha512ChunkSize :: Int
sha512ChunkSize :: Int
sha512ChunkSize = Int
128

lastChunk :: Int64 -> ByteString -> [ByteString]
lastChunk :: Int64 -> ByteString -> [ByteString]
lastChunk Int64
msglen ByteString
s
  | Int
len  forall a. Ord a => a -> a -> Bool
< Int
112  = [ByteString
s forall a. Semigroup a => a -> a -> a
<> Word8 -> ByteString -> ByteString
B.cons Word8
0x80 (Int -> Word8 -> ByteString
B.replicate (Int
111 forall a. Num a => a -> a -> a
- Int
len) Word8
0x0)  forall a. Semigroup a => a -> a -> a
<> ByteString
encodedLen]
  | Int
len forall a. Ord a => a -> a -> Bool
>= Int
112 = ByteString -> [ByteString]
helper (ByteString
s forall a. Semigroup a => a -> a -> a
<> Word8 -> ByteString -> ByteString
B.cons Word8
0x80 (Int -> Word8 -> ByteString
B.replicate (Int
239 forall a. Num a => a -> a -> a
- Int
len) Word8
0x0) forall a. Semigroup a => a -> a -> a
<> ByteString
encodedLen)
  where
    len :: Int
len        = ByteString -> Int
B.length ByteString
s
    encodedLen :: ByteString
encodedLen = Int64 -> ByteString
encodeInt64 Int64
msglen
    helper :: ByteString -> [ByteString]
helper ByteString
bs   = [ByteString
s1, ByteString
s2]
      where (ByteString
s1, ByteString
s2) = Int -> ByteString -> (ByteString, ByteString)
B.splitAt Int
sha512ChunkSize ByteString
bs

data SHA512 = SHA512  {-# UNPACK #-} !Word64
              {-# UNPACK #-} !Word64
              {-# UNPACK #-} !Word64
              {-# UNPACK #-} !Word64
              {-# UNPACK #-} !Word64
              {-# UNPACK #-} !Word64
              {-# UNPACK #-} !Word64
              {-# UNPACK #-} !Word64
          deriving SHA512 -> SHA512 -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SHA512 -> SHA512 -> Bool
$c/= :: SHA512 -> SHA512 -> Bool
== :: SHA512 -> SHA512 -> Bool
$c== :: SHA512 -> SHA512 -> Bool
Eq

data SHA384 = SHA384  {-# UNPACK #-} !Word64
              {-# UNPACK #-} !Word64
              {-# UNPACK #-} !Word64
              {-# UNPACK #-} !Word64
              {-# UNPACK #-} !Word64
              {-# UNPACK #-} !Word64
              {-# UNPACK #-} !Word64
              {-# UNPACK #-} !Word64

initHash :: SHA512
initHash :: SHA512
initHash = [Word64] -> SHA512
fromList [Word64]
initHs
  where fromList :: [Word64] -> SHA512
fromList (Word64
a:Word64
b:Word64
c:Word64
d:Word64
e:Word64
f:Word64
g:Word64
h:[Word64]
_) = Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> SHA512
SHA512 Word64
a Word64
b Word64
c Word64
d Word64
e Word64
f Word64
g Word64
h

initHash384 :: SHA512
initHash384 :: SHA512
initHash384 = [Word64] -> SHA512
fromList [Word64
0xcbbb9d5dc1059ed8, Word64
0x629a292a367cd507, Word64
0x9159015a3070dd17, Word64
0x152fecd8f70e5939, 
           Word64
0x67332667ffc00b31, Word64
0x8eb44a8768581511, Word64
0xdb0c2e0d64f98fa7, Word64
0x47b5481dbefa4fa4]
  where fromList :: [Word64] -> SHA512
fromList (Word64
a:Word64
b:Word64
c:Word64
d:Word64
e:Word64
f:Word64
g:Word64
h:[Word64]
_) = Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> SHA512
SHA512 Word64
a Word64
b Word64
c Word64
d Word64
e Word64
f Word64
g Word64
h

instance Show SHA512 where
  show :: SHA512 -> String
show = ByteString -> String
LC.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Word64 -> Builder
word64HexFixed forall b c a. (b -> c) -> (a -> b) -> a -> c
. SHA512 -> [Word64]
toList
    where toList :: SHA512 -> [Word64]
toList (SHA512 Word64
a Word64
b Word64
c Word64
d Word64
e Word64
f Word64
g Word64
h) = Word64
aforall a. a -> [a] -> [a]
:Word64
bforall a. a -> [a] -> [a]
:Word64
cforall a. a -> [a] -> [a]
:Word64
dforall a. a -> [a] -> [a]
:Word64
eforall a. a -> [a] -> [a]
:Word64
fforall a. a -> [a] -> [a]
:Word64
gforall a. a -> [a] -> [a]
:[Word64
h]

instance Show SHA384 where
  show :: SHA384 -> String
show = ByteString -> String
LC.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Word64 -> Builder
word64HexFixed forall b c a. (b -> c) -> (a -> b) -> a -> c
. SHA384 -> [Word64]
toList
    where toList :: SHA384 -> [Word64]
toList (SHA384 Word64
a Word64
b Word64
c Word64
d Word64
e Word64
f Word64
_ Word64
_) = Word64
aforall a. a -> [a] -> [a]
:Word64
bforall a. a -> [a] -> [a]
:Word64
cforall a. a -> [a] -> [a]
:Word64
dforall a. a -> [a] -> [a]
:Word64
eforall a. a -> [a] -> [a]
:[Word64
f]

instance Eq SHA384 where
  (SHA384 Word64
a1 Word64
b1 Word64
c1 Word64
d1 Word64
e1 Word64
f1 Word64
_ Word64
_) == :: SHA384 -> SHA384 -> Bool
== (SHA384 Word64
a2 Word64
b2 Word64
c2 Word64
d2 Word64
e2 Word64
f2 Word64
_ Word64
_) =
    Word64
a1 forall a. Eq a => a -> a -> Bool
== Word64
a2 Bool -> Bool -> Bool
&& Word64
b1 forall a. Eq a => a -> a -> Bool
== Word64
b2 Bool -> Bool -> Bool
&& Word64
c1 forall a. Eq a => a -> a -> Bool
== Word64
c2 Bool -> Bool -> Bool
&& Word64
d1 forall a. Eq a => a -> a -> Bool
== Word64
d2 Bool -> Bool -> Bool
&& Word64
e1 forall a. Eq a => a -> a -> Bool
== Word64
e2 Bool -> Bool -> Bool
&& Word64
f1 forall a. Eq a => a -> a -> Bool
== Word64
f2

{-# INLINABLE sha512BlockUpdate #-}
sha512BlockUpdate :: SHA512 -> Word64 -> SHA512
sha512BlockUpdate :: SHA512 -> Word64 -> SHA512
sha512BlockUpdate (SHA512 Word64
a Word64
b Word64
c Word64
d Word64
e Word64
f Word64
g Word64
h) Word64
w =
    let
      !s1 :: Word64
s1    = Word64 -> Word64
mkS1 Word64
e
      !ch :: Word64
ch    = (Word64
e forall a. Bits a => a -> a -> a
.&. Word64
f) forall a. Bits a => a -> a -> a
`xor` (forall a. Bits a => a -> a
complement Word64
e forall a. Bits a => a -> a -> a
.&. Word64
g)
      !temp1 :: Word64
temp1 = Word64
h forall a. Num a => a -> a -> a
+ Word64
s1 forall a. Num a => a -> a -> a
+ Word64
ch forall a. Num a => a -> a -> a
+ Word64
w
      !s0 :: Word64
s0    = Word64 -> Word64
mkS0 Word64
a
      !maj :: Word64
maj   = (Word64
a forall a. Bits a => a -> a -> a
.&. Word64
b) forall a. Bits a => a -> a -> a
`xor` (Word64
a forall a. Bits a => a -> a -> a
.&. Word64
c) forall a. Bits a => a -> a -> a
`xor` (Word64
b forall a. Bits a => a -> a -> a
.&. Word64
c)
      !temp2 :: Word64
temp2 = Word64
s0 forall a. Num a => a -> a -> a
+ Word64
maj
    in Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> SHA512
SHA512 (Word64
temp1 forall a. Num a => a -> a -> a
+ Word64
temp2) Word64
a Word64
b Word64
c (Word64
d forall a. Num a => a -> a -> a
+ Word64
temp1) Word64
e Word64
f Word64
g

{-# INLINE readW64 #-}
readW64 :: ByteString -> Word64
readW64 :: ByteString -> Word64
readW64 = forall a. (a -> Word8 -> a) -> a -> ByteString -> a
B.foldl' Word64 -> Word8 -> Word64
acc Word64
0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ByteString -> ByteString
B.take Int
8
  where acc :: Word64 -> Word8 -> Word64
acc Word64
x Word8
c = Word64
x forall a. Bits a => a -> Int -> a
`shiftL` Int
8 forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
c
        acc :: Word64 -> Word8 -> Word64
        {-# INLINE acc #-}

mkS0 :: Word64 -> Word64
mkS1 :: Word64 -> Word64
mkS0 :: Word64 -> Word64
mkS0 Word64
x = (Word64
x forall a. Bits a => a -> Int -> a
`rotateR` Int
28) forall a. Bits a => a -> a -> a
`xor` (Word64
x forall a. Bits a => a -> Int -> a
`rotateR` Int
34) forall a. Bits a => a -> a -> a
`xor` (Word64
x forall a. Bits a => a -> Int -> a
`rotateR` Int
39)
mkS1 :: Word64 -> Word64
mkS1 Word64
x = (Word64
x forall a. Bits a => a -> Int -> a
`rotateR` Int
14) forall a. Bits a => a -> a -> a
`xor` (Word64
x forall a. Bits a => a -> Int -> a
`rotateR` Int
18) forall a. Bits a => a -> a -> a
`xor` (Word64
x forall a. Bits a => a -> Int -> a
`rotateR` Int
41)
{-# INLINE mkS0 #-}
{-# INLINE mkS1 #-}

mkS00 :: Word64 -> Word64
mkS01 :: Word64 -> Word64
mkS00 :: Word64 -> Word64
mkS00 Word64
x = (Word64
x forall a. Bits a => a -> Int -> a
`rotateR`  Int
1) forall a. Bits a => a -> a -> a
`xor` (Word64
x forall a. Bits a => a -> Int -> a
`rotateR`  Int
8) forall a. Bits a => a -> a -> a
`xor` (Word64
x forall a. Bits a => a -> Int -> a
`shiftR`  Int
7)
mkS01 :: Word64 -> Word64
mkS01 Word64
x = (Word64
x forall a. Bits a => a -> Int -> a
`rotateR` Int
19) forall a. Bits a => a -> a -> a
`xor` (Word64
x forall a. Bits a => a -> Int -> a
`rotateR` Int
61) forall a. Bits a => a -> a -> a
`xor` (Word64
x forall a. Bits a => a -> Int -> a
`shiftR`  Int
6)

prepareBlock :: ByteString -> UArray Int Word64
prepareBlock :: ByteString -> UArray Int Word64
prepareBlock ByteString
s = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
  STUArray s Int Word64
iou <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Int
0, Int
79) Word64
0 :: ST s (STUArray s Int Word64)
  let
    !w1 :: Word64
w1  = ByteString -> Word64
readW64 ByteString
s
    !w2 :: Word64
w2  = ByteString -> Word64
readW64 (Int -> ByteString -> ByteString
B.drop Int
8 ByteString
s)
    !w3 :: Word64
w3  = ByteString -> Word64
readW64 (Int -> ByteString -> ByteString
B.drop Int
16 ByteString
s)
    !w4 :: Word64
w4  = ByteString -> Word64
readW64 (Int -> ByteString -> ByteString
B.drop Int
24 ByteString
s)
    !w5 :: Word64
w5  = ByteString -> Word64
readW64 (Int -> ByteString -> ByteString
B.drop Int
32 ByteString
s)
    !w6 :: Word64
w6  = ByteString -> Word64
readW64 (Int -> ByteString -> ByteString
B.drop Int
40 ByteString
s)
    !w7 :: Word64
w7  = ByteString -> Word64
readW64 (Int -> ByteString -> ByteString
B.drop Int
48 ByteString
s)
    !w8 :: Word64
w8  = ByteString -> Word64
readW64 (Int -> ByteString -> ByteString
B.drop Int
56 ByteString
s)
    !w9 :: Word64
w9  = ByteString -> Word64
readW64 (Int -> ByteString -> ByteString
B.drop Int
64 ByteString
s)
    !w10 :: Word64
w10 = ByteString -> Word64
readW64 (Int -> ByteString -> ByteString
B.drop Int
72 ByteString
s)
    !w11 :: Word64
w11 = ByteString -> Word64
readW64 (Int -> ByteString -> ByteString
B.drop Int
80 ByteString
s)
    !w12 :: Word64
w12 = ByteString -> Word64
readW64 (Int -> ByteString -> ByteString
B.drop Int
88 ByteString
s)
    !w13 :: Word64
w13 = ByteString -> Word64
readW64 (Int -> ByteString -> ByteString
B.drop Int
96 ByteString
s)
    !w14 :: Word64
w14 = ByteString -> Word64
readW64 (Int -> ByteString -> ByteString
B.drop Int
104 ByteString
s)
    !w15 :: Word64
w15 = ByteString -> Word64
readW64 (Int -> ByteString -> ByteString
B.drop Int
112 ByteString
s)
    !w16 :: Word64
w16 = ByteString -> Word64
readW64 (Int -> ByteString -> ByteString
B.drop Int
120 ByteString
s)
  forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
0 Word64
w1 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
1 Word64
w2 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
2 Word64
w3 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
3 Word64
w4 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
4 Word64
w5 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
5 Word64
w6 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
6 Word64
w7 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
7 Word64
w8 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
8 Word64
w9 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
9 Word64
w10 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
10 Word64
w11 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
11 Word64
w12 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
12 Word64
w13 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
13 Word64
w14 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
14 Word64
w15 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
15 Word64
w16
  let go :: Int -> m ()
go Int
i = forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Word64
iou (Int
iforall a. Num a => a -> a -> a
-Int
16) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Word64
x1 ->
        forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Word64
iou (Int
iforall a. Num a => a -> a -> a
-Int
15) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Word64
x2 ->
        forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Word64
iou (Int
iforall a. Num a => a -> a -> a
- Int
7) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Word64
x3 ->
        forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Word64
iou (Int
iforall a. Num a => a -> a -> a
- Int
2) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Word64
x4 ->
        let !s0 :: Word64
s0 = Word64 -> Word64
mkS00 Word64
x2
            !s1 :: Word64
s1 = Word64 -> Word64
mkS01 Word64
x4
        in forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Word64
iou Int
i (Word64
x1 forall a. Num a => a -> a -> a
+ Word64
s0 forall a. Num a => a -> a -> a
+ Word64
x3 forall a. Num a => a -> a -> a
+ Word64
s1)
      {-# INLINE go #-}
  forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall {m :: * -> *}. MArray (STUArray s) Word64 m => Int -> m ()
go [Int
16..Int
79]
  forall i (a :: * -> * -> *) e (m :: * -> *) (b :: * -> * -> *).
(Ix i, MArray a e m, IArray b e) =>
a i e -> m (b i e)
unsafeFreeze STUArray s Int Word64
iou

{-# INLINE encodeChunk #-}
encodeChunk :: SHA512 -> ByteString -> SHA512
encodeChunk :: SHA512 -> ByteString -> SHA512
encodeChunk hv :: SHA512
hv@(SHA512 Word64
a Word64
b Word64
c Word64
d Word64
e Word64
f Word64
g Word64
h) ByteString
bs = Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> SHA512
SHA512 (Word64
aforall a. Num a => a -> a -> a
+Word64
a') (Word64
bforall a. Num a => a -> a -> a
+Word64
b') (Word64
cforall a. Num a => a -> a -> a
+Word64
c') (Word64
dforall a. Num a => a -> a -> a
+Word64
d') (Word64
eforall a. Num a => a -> a -> a
+Word64
e') (Word64
fforall a. Num a => a -> a -> a
+Word64
f') (Word64
gforall a. Num a => a -> a -> a
+Word64
g') (Word64
hforall a. Num a => a -> a -> a
+Word64
h')
  where
    SHA512 Word64
a' Word64
b' Word64
c' Word64
d' Word64
e' Word64
f' Word64
g' Word64
h' = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' SHA512 -> Word64 -> SHA512
sha512BlockUpdate SHA512
hv (forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall a. Num a => a -> a -> a
(+) (forall (a :: * -> * -> *) e i. (IArray a e, Ix i) => a i e -> [e]
elems (ByteString -> UArray Int Word64
prepareBlock ByteString
bs)) [Word64]
initKs)

{-# NOINLINE sha512Hash #-}
sha512Hash :: LBS.ByteString -> SHA512
sha512Hash :: ByteString -> SHA512
sha512Hash = Context SHA512 -> SHA512
sha512Final forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> ByteString -> a) -> a -> ByteString -> a
LBS.foldlChunks Context SHA512 -> ByteString -> Context SHA512
sha512Update Context SHA512
sha512Init

sha512Init :: Context SHA512
sha512Init :: Context SHA512
sha512Init = forall a. Int64 -> Int -> ByteString -> a -> Context a
Context Int64
0 Int
0 ByteString
B.empty SHA512
initHash

{-# NOINLINE sha512Update #-}
sha512Update :: Context SHA512 -> ByteString -> Context SHA512
sha512Update :: Context SHA512 -> ByteString -> Context SHA512
sha512Update ctx :: Context SHA512
ctx@(Context Int64
n Int
k ByteString
w SHA512
hv) ByteString
s
  | ByteString -> Bool
B.null ByteString
s               = Context SHA512
ctx
  | Int
sizeRead  forall a. Ord a => a -> a -> Bool
< Int
sizeToRead = forall a. Int64 -> Int -> ByteString -> a -> Context a
Context (Int64
n forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
sizeRead) (Int
k forall a. Num a => a -> a -> a
+ Int
sizeRead) (ByteString
w forall a. Semigroup a => a -> a -> a
<> ByteString
s1) SHA512
hv
  | Int
sizeRead forall a. Ord a => a -> a -> Bool
>= Int
sizeToRead = Context SHA512 -> ByteString -> Context SHA512
sha512Update (forall a. Int64 -> Int -> ByteString -> a -> Context a
Context (Int64
n forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
sizeToRead) Int
0 forall a. Monoid a => a
mempty (SHA512 -> ByteString -> SHA512
encodeChunk SHA512
hv (ByteString
w forall a. Semigroup a => a -> a -> a
<> ByteString
s1))) ByteString
s'
  where
    !sizeToRead :: Int
sizeToRead  = Int
sha512ChunkSize forall a. Num a => a -> a -> a
- Int
k
    (!ByteString
s1, !ByteString
s')   = Int -> ByteString -> (ByteString, ByteString)
B.splitAt Int
sizeToRead ByteString
s
    !sizeRead :: Int
sizeRead    = ByteString -> Int
B.length ByteString
s1

{-# NOINLINE sha512Final #-}
sha512Final :: Context SHA512 -> SHA512
sha512Final :: Context SHA512 -> SHA512
sha512Final (Context Int64
n Int
_ ByteString
w SHA512
hv) = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' SHA512 -> ByteString -> SHA512
encodeChunk SHA512
hv (Int64 -> ByteString -> [ByteString]
lastChunk Int64
n ByteString
w)

instance HashAlgorithm SHA512 where
  hashBlockSize :: SHA512 -> Int
hashBlockSize = forall a b. a -> b -> a
const Int
128
  hashDigestSize :: SHA512 -> Int
hashDigestSize = forall a b. a -> b -> a
const Int
64
  hashInit :: Context SHA512
hashInit = Context SHA512
sha512Init
  hashUpdate :: Context SHA512 -> ByteString -> Context SHA512
hashUpdate = Context SHA512 -> ByteString -> Context SHA512
sha512Update
  hashFinal :: Context SHA512 -> SHA512
hashFinal = Context SHA512 -> SHA512
sha512Final

fromSHA384 :: SHA384 -> SHA512
fromSHA512 :: SHA512 -> SHA384
fromSHA384 :: SHA384 -> SHA512
fromSHA384 (SHA384 Word64
a Word64
b Word64
c Word64
d Word64
e Word64
f Word64
g Word64
h) = Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> SHA512
SHA512 Word64
a Word64
b Word64
c Word64
d Word64
e Word64
f Word64
g Word64
h
fromSHA512 :: SHA512 -> SHA384
fromSHA512 (SHA512 Word64
a Word64
b Word64
c Word64
d Word64
e Word64
f Word64
g Word64
h) = Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> SHA384
SHA384 Word64
a Word64
b Word64
c Word64
d Word64
e Word64
f Word64
g Word64
h

sha384Init :: Context SHA384
sha384Init :: Context SHA384
sha384Init   = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SHA512 -> SHA384
fromSHA512 (forall a. Int64 -> Int -> ByteString -> a -> Context a
Context Int64
0 Int
0 ByteString
B.empty SHA512
initHash384)
sha384Update :: Context SHA384 -> ByteString -> Context SHA384
sha384Update :: Context SHA384 -> ByteString -> Context SHA384
sha384Update = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SHA512 -> SHA384
fromSHA512) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Context SHA512 -> ByteString -> Context SHA512
sha512Update forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SHA384 -> SHA512
fromSHA384
sha384Final :: Context SHA384 -> SHA384
sha384Final :: Context SHA384 -> SHA384
sha384Final = SHA512 -> SHA384
fromSHA512 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Context SHA512 -> SHA512
sha512Final forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SHA384 -> SHA512
fromSHA384

instance HashAlgorithm SHA384 where
  hashBlockSize :: SHA384 -> Int
hashBlockSize = forall a b. a -> b -> a
const Int
128
  hashDigestSize :: SHA384 -> Int
hashDigestSize = forall a b. a -> b -> a
const Int
48
  hashInit :: Context SHA384
hashInit = Context SHA384
sha384Init
  hashUpdate :: Context SHA384 -> ByteString -> Context SHA384
hashUpdate = Context SHA384 -> ByteString -> Context SHA384
sha384Update
  hashFinal :: Context SHA384 -> SHA384
hashFinal = Context SHA384 -> SHA384
sha384Final