module Botan.Hash.SHAKE
( SHAKE128(..)
, SHAKE128Digest(..)
, shake128
, shake128Lazy
, SHAKE256(..)
, SHAKE256Digest(..)
, shake256
, shake256Lazy
) where

import GHC.TypeLits

import Data.Maybe
import Data.Proxy

import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as Lazy
import qualified Data.Text as Text

import qualified Botan.Hash as Botan
import qualified Botan.Utility as Botan

import Botan.Hash.Class
import Botan.Prelude

-- SHAKE128 type

data SHAKE128 (n :: Nat)

type SHAKE128Size (n :: Nat) = (KnownNat n, (1 <=? n) ~ True, Mod n 8 ~ 0, (n <=? 512) ~ True)

type SHAKE128Digest n = Digest (SHAKE128 n)

newtype instance Digest (SHAKE128 n) = SHAKE128Digest
    { forall (n :: Nat). Digest (SHAKE128 n) -> ByteString
getSHAKE128ByteString :: ByteString {- ByteVector n -} }
    deriving newtype (Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
(Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool)
-> (Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool)
-> Eq (Digest (SHAKE128 n))
forall (n :: Nat).
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall (n :: Nat).
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
== :: Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
$c/= :: forall (n :: Nat).
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
/= :: Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
Eq, Eq (Digest (SHAKE128 n))
Eq (Digest (SHAKE128 n)) =>
(Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Ordering)
-> (Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool)
-> (Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool)
-> (Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool)
-> (Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool)
-> (Digest (SHAKE128 n)
    -> Digest (SHAKE128 n) -> Digest (SHAKE128 n))
-> (Digest (SHAKE128 n)
    -> Digest (SHAKE128 n) -> Digest (SHAKE128 n))
-> Ord (Digest (SHAKE128 n))
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Ordering
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Digest (SHAKE128 n)
forall (n :: Nat). Eq (Digest (SHAKE128 n))
forall (n :: Nat).
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
forall (n :: Nat).
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Ordering
forall (n :: Nat).
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Digest (SHAKE128 n)
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: forall (n :: Nat).
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Ordering
compare :: Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Ordering
$c< :: forall (n :: Nat).
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
< :: Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
$c<= :: forall (n :: Nat).
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
<= :: Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
$c> :: forall (n :: Nat).
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
> :: Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
$c>= :: forall (n :: Nat).
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
>= :: Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Bool
$cmax :: forall (n :: Nat).
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Digest (SHAKE128 n)
max :: Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Digest (SHAKE128 n)
$cmin :: forall (n :: Nat).
Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Digest (SHAKE128 n)
min :: Digest (SHAKE128 n) -> Digest (SHAKE128 n) -> Digest (SHAKE128 n)
Ord)

instance Show (Digest (SHAKE128 n)) where
    show :: Digest (SHAKE128 n) -> String
    show :: Digest (SHAKE128 n) -> String
show (SHAKE128Digest ByteString
bytes) = Text -> String
Text.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ ByteString -> HexCase -> Text
Botan.hexEncode ByteString
bytes HexCase
Botan.Lower

instance (SHAKE128Size n) => Hash (SHAKE128 n) where
    hash :: ByteString -> Digest (SHAKE128 n)
    hash :: ByteString -> Digest (SHAKE128 n)
hash = ByteString -> Digest (SHAKE128 n)
forall (n :: Nat). ByteString -> Digest (SHAKE128 n)
SHAKE128Digest (ByteString -> Digest (SHAKE128 n))
-> (ByteString -> ByteString) -> ByteString -> Digest (SHAKE128 n)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash -> ByteString -> ByteString
Botan.hash Hash
h where
        n :: Int
n = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n -> Integer) -> Proxy n -> Integer
forall a b. (a -> b) -> a -> b
$ forall (t :: Nat). Proxy t
forall {k} (t :: k). Proxy t
Proxy @n
        h :: Hash
h = Maybe Hash -> Hash
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe Hash -> Hash) -> Maybe Hash -> Hash
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Hash
Botan.shake128 Int
n

instance (SHAKE128Size n) => IncrementalHash (SHAKE128 n) where
    hashLazy :: Lazy.ByteString -> Digest (SHAKE128 n)
    hashLazy :: ByteString -> Digest (SHAKE128 n)
hashLazy = ByteString -> Digest (SHAKE128 n)
forall (n :: Nat). ByteString -> Digest (SHAKE128 n)
SHAKE128Digest (ByteString -> Digest (SHAKE128 n))
-> (ByteString -> ByteString) -> ByteString -> Digest (SHAKE128 n)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash -> ByteString -> ByteString
Botan.hashLazy Hash
h where
        n :: Int
n = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n -> Integer) -> Proxy n -> Integer
forall a b. (a -> b) -> a -> b
$ forall (t :: Nat). Proxy t
forall {k} (t :: k). Proxy t
Proxy @n
        h :: Hash
h = Maybe Hash -> Hash
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe Hash -> Hash) -> Maybe Hash -> Hash
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Hash
Botan.shake128 Int
n

-- SHAKE128 hash

shake128 :: (SHAKE128Size n) => ByteString -> SHAKE128Digest n
shake128 :: forall (n :: Nat).
SHAKE128Size n =>
ByteString -> Digest (SHAKE128 n)
shake128 = ByteString -> Digest (SHAKE128 n)
forall hash. Hash hash => ByteString -> Digest hash
hash

shake128Lazy :: (SHAKE128Size n) => Lazy.ByteString -> SHAKE128Digest n
shake128Lazy :: forall (n :: Nat).
SHAKE128Size n =>
ByteString -> Digest (SHAKE128 n)
shake128Lazy = ByteString -> Digest (SHAKE128 n)
forall hash. IncrementalHash hash => ByteString -> Digest hash
hashLazy

-- SHAKE256 type

data SHAKE256 (n :: Nat)

type SHAKE256Size (n :: Nat) = (KnownNat n, (1 <=? n) ~ True, Mod n 8 ~ 0, (n <=? 512) ~ True)

type SHAKE256Digest n = Digest (SHAKE256 n)

newtype instance Digest (SHAKE256 n) = SHAKE256Digest
    { forall (n :: Nat). Digest (SHAKE256 n) -> ByteString
getSHAKE256ByteString :: ByteString {- ByteVector n -} }
    deriving newtype (Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
(Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool)
-> (Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool)
-> Eq (Digest (SHAKE256 n))
forall (n :: Nat).
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall (n :: Nat).
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
== :: Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
$c/= :: forall (n :: Nat).
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
/= :: Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
Eq, Eq (Digest (SHAKE256 n))
Eq (Digest (SHAKE256 n)) =>
(Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Ordering)
-> (Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool)
-> (Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool)
-> (Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool)
-> (Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool)
-> (Digest (SHAKE256 n)
    -> Digest (SHAKE256 n) -> Digest (SHAKE256 n))
-> (Digest (SHAKE256 n)
    -> Digest (SHAKE256 n) -> Digest (SHAKE256 n))
-> Ord (Digest (SHAKE256 n))
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Ordering
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Digest (SHAKE256 n)
forall (n :: Nat). Eq (Digest (SHAKE256 n))
forall (n :: Nat).
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
forall (n :: Nat).
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Ordering
forall (n :: Nat).
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Digest (SHAKE256 n)
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: forall (n :: Nat).
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Ordering
compare :: Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Ordering
$c< :: forall (n :: Nat).
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
< :: Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
$c<= :: forall (n :: Nat).
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
<= :: Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
$c> :: forall (n :: Nat).
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
> :: Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
$c>= :: forall (n :: Nat).
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
>= :: Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Bool
$cmax :: forall (n :: Nat).
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Digest (SHAKE256 n)
max :: Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Digest (SHAKE256 n)
$cmin :: forall (n :: Nat).
Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Digest (SHAKE256 n)
min :: Digest (SHAKE256 n) -> Digest (SHAKE256 n) -> Digest (SHAKE256 n)
Ord)

instance Show (Digest (SHAKE256 n)) where
    show :: Digest (SHAKE256 n) -> String
    show :: Digest (SHAKE256 n) -> String
show (SHAKE256Digest ByteString
bytes) = Text -> String
Text.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ ByteString -> HexCase -> Text
Botan.hexEncode ByteString
bytes HexCase
Botan.Lower

instance (SHAKE256Size n) => Hash (SHAKE256 n) where
    hash :: ByteString -> Digest (SHAKE256 n)
    hash :: ByteString -> Digest (SHAKE256 n)
hash = ByteString -> Digest (SHAKE256 n)
forall (n :: Nat). ByteString -> Digest (SHAKE256 n)
SHAKE256Digest (ByteString -> Digest (SHAKE256 n))
-> (ByteString -> ByteString) -> ByteString -> Digest (SHAKE256 n)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash -> ByteString -> ByteString
Botan.hash Hash
h where
        n :: Int
n = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n -> Integer) -> Proxy n -> Integer
forall a b. (a -> b) -> a -> b
$ forall (t :: Nat). Proxy t
forall {k} (t :: k). Proxy t
Proxy @n
        h :: Hash
h = Maybe Hash -> Hash
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe Hash -> Hash) -> Maybe Hash -> Hash
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Hash
Botan.shake256 Int
n

instance (SHAKE256Size n) => IncrementalHash (SHAKE256 n) where
    hashLazy :: Lazy.ByteString -> Digest (SHAKE256 n)
    hashLazy :: ByteString -> Digest (SHAKE256 n)
hashLazy = ByteString -> Digest (SHAKE256 n)
forall (n :: Nat). ByteString -> Digest (SHAKE256 n)
SHAKE256Digest (ByteString -> Digest (SHAKE256 n))
-> (ByteString -> ByteString) -> ByteString -> Digest (SHAKE256 n)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash -> ByteString -> ByteString
Botan.hashLazy Hash
h where
        n :: Int
n = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n -> Integer) -> Proxy n -> Integer
forall a b. (a -> b) -> a -> b
$ forall (t :: Nat). Proxy t
forall {k} (t :: k). Proxy t
Proxy @n
        h :: Hash
h = Maybe Hash -> Hash
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe Hash -> Hash) -> Maybe Hash -> Hash
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Hash
Botan.shake256 Int
n

-- SHAKE256 hash

shake256 :: (SHAKE256Size n) => ByteString -> SHAKE256Digest n
shake256 :: forall (n :: Nat).
SHAKE256Size n =>
ByteString -> Digest (SHAKE256 n)
shake256 = ByteString -> Digest (SHAKE256 n)
forall hash. Hash hash => ByteString -> Digest hash
hash

shake256Lazy :: (SHAKE256Size n) => Lazy.ByteString -> SHAKE256Digest n
shake256Lazy :: forall (n :: Nat).
SHAKE256Size n =>
ByteString -> Digest (SHAKE256 n)
shake256Lazy = ByteString -> Digest (SHAKE256 n)
forall hash. IncrementalHash hash => ByteString -> Digest hash
hashLazy