{-# LANGUAGE CPP                  #-}
{-# LANGUAGE DataKinds            #-}
{-# LANGUAGE FlexibleInstances    #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

-- | Some common instances that are required by the test cases.
module Tests.Core.Instances where

import Tests.Core.Imports
import Tests.Core.Utils

import Raaz.Primitive.Poly1305.Internal as Poly1305
import Raaz.Core.Types.Internal
import Raaz.Primitive.Keyed.Internal as Keyed


instance Arbitrary w => Arbitrary (LE w) where
  arbitrary :: Gen (LE w)
arbitrary = w -> LE w
forall w. w -> LE w
littleEndian (w -> LE w) -> Gen w -> Gen (LE w)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen w
forall a. Arbitrary a => Gen a
arbitrary

instance Arbitrary w => Arbitrary (BE w) where
  arbitrary :: Gen (BE w)
arbitrary = w -> BE w
forall w. w -> BE w
bigEndian (w -> BE w) -> Gen w -> Gen (BE w)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen w
forall a. Arbitrary a => Gen a
arbitrary

instance Arbitrary w => Arbitrary (BYTES w) where
  arbitrary :: Gen (BYTES w)
arbitrary = w -> BYTES w
forall a. a -> BYTES a
BYTES (w -> BYTES w) -> Gen w -> Gen (BYTES w)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen w
forall a. Arbitrary a => Gen a
arbitrary

instance Arbitrary ByteString where
  arbitrary :: Gen ByteString
arbitrary = [Word8] -> ByteString
pack ([Word8] -> ByteString) -> Gen [Word8] -> Gen ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen [Word8]
forall a. Arbitrary a => Gen a
arbitrary


---------------   Arbitrary instances for Hashes ----------------

instance Arbitrary Sha256 where
  arbitrary :: Gen Sha256
arbitrary = Gen Sha256
forall a. (Encodable a, Storable a) => Gen a
genEncodable

instance Arbitrary Sha512 where
  arbitrary :: Gen Sha512
arbitrary = Gen Sha512
forall a. (Encodable a, Storable a) => Gen a
genEncodable

instance Arbitrary Blake2b where
  arbitrary :: Gen Blake2b
arbitrary = Gen Blake2b
forall a. (Encodable a, Storable a) => Gen a
genEncodable

instance Arbitrary Blake2s where
  arbitrary :: Gen Blake2s
arbitrary = Gen Blake2s
forall a. (Encodable a, Storable a) => Gen a
genEncodable

---------------- Parameter block for Blake          -------------

---------------- Arbitrary instaces of encoded data --------------

instance Arbitrary Base16 where
  arbitrary :: Gen Base16
arbitrary =  ByteString -> Base16
forall fmt. Format fmt => ByteString -> fmt
encodeByteString (ByteString -> Base16)
-> ([Word8] -> ByteString) -> [Word8] -> Base16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> ByteString
pack ([Word8] -> Base16) -> Gen [Word8] -> Gen Base16
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Word8 -> Gen [Word8]
forall a. Gen a -> Gen [a]
listOf Gen Word8
forall a. Arbitrary a => Gen a
arbitrary

instance Arbitrary Base64 where
  arbitrary :: Gen Base64
arbitrary =  ByteString -> Base64
forall fmt. Format fmt => ByteString -> fmt
encodeByteString (ByteString -> Base64)
-> ([Word8] -> ByteString) -> [Word8] -> Base64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> ByteString
pack ([Word8] -> Base64) -> Gen [Word8] -> Gen Base64
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Word8 -> Gen [Word8]
forall a. Gen a -> Gen [a]
listOf Gen Word8
forall a. Arbitrary a => Gen a
arbitrary

------------------ For ChaCha20 types -------------------------

instance Arbitrary (Key ChaCha20) where
  arbitrary :: Gen (Key ChaCha20)
arbitrary = Gen (Key ChaCha20)
forall a. (Encodable a, Storable a) => Gen a
genEncodable

instance Arbitrary (Nounce ChaCha20) where
  arbitrary :: Gen (Nounce ChaCha20)
arbitrary = Gen (Nounce ChaCha20)
forall a. (Encodable a, Storable a) => Gen a
genEncodable

instance Arbitrary (BlockCount ChaCha20) where
  arbitrary :: Gen (BlockCount ChaCha20)
arbitrary = Int -> BlockCount ChaCha20
forall a. Enum a => Int -> a
toEnum (Int -> BlockCount ChaCha20)
-> Gen Int -> Gen (BlockCount ChaCha20)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Int
forall a. Arbitrary a => Gen a
arbitrary

------------------ For XChaCha20 types -------------------------

instance Arbitrary (Key XChaCha20) where
  arbitrary :: Gen (Key XChaCha20)
arbitrary = Gen (Key XChaCha20)
forall a. (Encodable a, Storable a) => Gen a
genEncodable

instance Arbitrary (Key (Keyed prim)) where
  arbitrary :: Gen (Key (Keyed prim))
arbitrary = ByteString -> Key (Keyed prim)
forall prim. ByteString -> Key (Keyed prim)
Keyed.Key (ByteString -> Key (Keyed prim))
-> ([Word8] -> ByteString) -> [Word8] -> Key (Keyed prim)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> ByteString
pack ([Word8] -> Key (Keyed prim))
-> Gen [Word8] -> Gen (Key (Keyed prim))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Word8 -> Gen [Word8]
forall a. Gen a -> Gen [a]
listOf1 Gen Word8
forall a. Arbitrary a => Gen a
arbitrary

instance Arbitrary (Nounce XChaCha20) where
  arbitrary :: Gen (Nounce XChaCha20)
arbitrary = Gen (Nounce XChaCha20)
forall a. (Encodable a, Storable a) => Gen a
genEncodable

instance Arbitrary (BlockCount XChaCha20) where
  arbitrary :: Gen (BlockCount XChaCha20)
arbitrary = Int -> BlockCount XChaCha20
forall a. Enum a => Int -> a
toEnum (Int -> BlockCount XChaCha20)
-> Gen Int -> Gen (BlockCount XChaCha20)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Int
forall a. Arbitrary a => Gen a
arbitrary

------------------ Arbitrary instances for Poly1305 -------------
instance Arbitrary Poly1305.R where
  arbitrary :: Gen R
arbitrary = Gen R
forall a. (Encodable a, Storable a) => Gen a
genEncodable

instance Arbitrary Poly1305.S where
  arbitrary :: Gen S
arbitrary = Gen S
forall a. (Encodable a, Storable a) => Gen a
genEncodable

instance Arbitrary Poly1305.Poly1305 where
  arbitrary :: Gen Poly1305
arbitrary = Gen Poly1305
forall a. (Encodable a, Storable a) => Gen a
genEncodable

instance Arbitrary (Key Poly1305) where
  arbitrary :: Gen (Key Poly1305)
arbitrary = R -> S -> Key Poly1305
Poly1305.Key (R -> S -> Key Poly1305) -> Gen R -> Gen (S -> Key Poly1305)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen R
forall a. Arbitrary a => Gen a
arbitrary Gen (S -> Key Poly1305) -> Gen S -> Gen (Key Poly1305)
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen S
forall a. Arbitrary a => Gen a
arbitrary