{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

-- | Mock key evolving signatures.
module Cardano.Crypto.KES.Mock
  ( MockKES
  , VerKeyKES (..)
  , SignKeyKES (..)
  , SigKES (..)
  )
where

import Data.Word (Word64)
import Data.Proxy (Proxy(..))
import GHC.Generics (Generic)
import GHC.TypeNats (Nat, KnownNat, natVal)
import NoThunks.Class (NoThunks)

import Control.Exception (assert)

import Cardano.Binary (FromCBOR (..), ToCBOR (..))

import Cardano.Crypto.Hash
import Cardano.Crypto.Seed
import Cardano.Crypto.KES.Class
import Cardano.Crypto.Util


data MockKES (t :: Nat)

-- | Mock key evolving signatures.
--
-- What is the difference between Mock KES and Simple KES
-- (@Cardano.Crypto.KES.Simple@), you may ask? Simple KES satisfies the outward
-- appearance of a KES scheme through assembling a pre-generated list of keys
-- and iterating through them. Mock KES, on the other hand, pretends to be KES
-- but in fact does no key evolution whatsoever.
--
-- Simple KES is appropriate for testing, since it will for example reject old
-- keys. Mock KES is more suitable for a basic testnet, since it doesn't suffer
-- from the performance implications of shuffling a giant list of keys around
instance KnownNat t => KESAlgorithm (MockKES t) where
    type SeedSizeKES (MockKES t) = 8

    --
    -- Key and signature types
    --

    newtype VerKeyKES (MockKES t) = VerKeyMockKES Word64
        deriving stock   (Int -> VerKeyKES (MockKES t) -> ShowS
[VerKeyKES (MockKES t)] -> ShowS
VerKeyKES (MockKES t) -> String
(Int -> VerKeyKES (MockKES t) -> ShowS)
-> (VerKeyKES (MockKES t) -> String)
-> ([VerKeyKES (MockKES t)] -> ShowS)
-> Show (VerKeyKES (MockKES t))
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (t :: Nat). Int -> VerKeyKES (MockKES t) -> ShowS
forall (t :: Nat). [VerKeyKES (MockKES t)] -> ShowS
forall (t :: Nat). VerKeyKES (MockKES t) -> String
showList :: [VerKeyKES (MockKES t)] -> ShowS
$cshowList :: forall (t :: Nat). [VerKeyKES (MockKES t)] -> ShowS
show :: VerKeyKES (MockKES t) -> String
$cshow :: forall (t :: Nat). VerKeyKES (MockKES t) -> String
showsPrec :: Int -> VerKeyKES (MockKES t) -> ShowS
$cshowsPrec :: forall (t :: Nat). Int -> VerKeyKES (MockKES t) -> ShowS
Show, VerKeyKES (MockKES t) -> VerKeyKES (MockKES t) -> Bool
(VerKeyKES (MockKES t) -> VerKeyKES (MockKES t) -> Bool)
-> (VerKeyKES (MockKES t) -> VerKeyKES (MockKES t) -> Bool)
-> Eq (VerKeyKES (MockKES t))
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (t :: Nat).
VerKeyKES (MockKES t) -> VerKeyKES (MockKES t) -> Bool
/= :: VerKeyKES (MockKES t) -> VerKeyKES (MockKES t) -> Bool
$c/= :: forall (t :: Nat).
VerKeyKES (MockKES t) -> VerKeyKES (MockKES t) -> Bool
== :: VerKeyKES (MockKES t) -> VerKeyKES (MockKES t) -> Bool
$c== :: forall (t :: Nat).
VerKeyKES (MockKES t) -> VerKeyKES (MockKES t) -> Bool
Eq, (forall x. VerKeyKES (MockKES t) -> Rep (VerKeyKES (MockKES t)) x)
-> (forall x.
    Rep (VerKeyKES (MockKES t)) x -> VerKeyKES (MockKES t))
-> Generic (VerKeyKES (MockKES t))
forall x. Rep (VerKeyKES (MockKES t)) x -> VerKeyKES (MockKES t)
forall x. VerKeyKES (MockKES t) -> Rep (VerKeyKES (MockKES t)) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (t :: Nat) x.
Rep (VerKeyKES (MockKES t)) x -> VerKeyKES (MockKES t)
forall (t :: Nat) x.
VerKeyKES (MockKES t) -> Rep (VerKeyKES (MockKES t)) x
$cto :: forall (t :: Nat) x.
Rep (VerKeyKES (MockKES t)) x -> VerKeyKES (MockKES t)
$cfrom :: forall (t :: Nat) x.
VerKeyKES (MockKES t) -> Rep (VerKeyKES (MockKES t)) x
Generic)
        deriving newtype (Context -> VerKeyKES (MockKES t) -> IO (Maybe ThunkInfo)
Proxy (VerKeyKES (MockKES t)) -> String
(Context -> VerKeyKES (MockKES t) -> IO (Maybe ThunkInfo))
-> (Context -> VerKeyKES (MockKES t) -> IO (Maybe ThunkInfo))
-> (Proxy (VerKeyKES (MockKES t)) -> String)
-> NoThunks (VerKeyKES (MockKES t))
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
forall (t :: Nat).
Context -> VerKeyKES (MockKES t) -> IO (Maybe ThunkInfo)
forall (t :: Nat). Proxy (VerKeyKES (MockKES t)) -> String
showTypeOf :: Proxy (VerKeyKES (MockKES t)) -> String
$cshowTypeOf :: forall (t :: Nat). Proxy (VerKeyKES (MockKES t)) -> String
wNoThunks :: Context -> VerKeyKES (MockKES t) -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall (t :: Nat).
Context -> VerKeyKES (MockKES t) -> IO (Maybe ThunkInfo)
noThunks :: Context -> VerKeyKES (MockKES t) -> IO (Maybe ThunkInfo)
$cnoThunks :: forall (t :: Nat).
Context -> VerKeyKES (MockKES t) -> IO (Maybe ThunkInfo)
NoThunks)

    data SignKeyKES (MockKES t) =
           SignKeyMockKES !(VerKeyKES (MockKES t)) !Period
        deriving stock    (Int -> SignKeyKES (MockKES t) -> ShowS
[SignKeyKES (MockKES t)] -> ShowS
SignKeyKES (MockKES t) -> String
(Int -> SignKeyKES (MockKES t) -> ShowS)
-> (SignKeyKES (MockKES t) -> String)
-> ([SignKeyKES (MockKES t)] -> ShowS)
-> Show (SignKeyKES (MockKES t))
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (t :: Nat). Int -> SignKeyKES (MockKES t) -> ShowS
forall (t :: Nat). [SignKeyKES (MockKES t)] -> ShowS
forall (t :: Nat). SignKeyKES (MockKES t) -> String
showList :: [SignKeyKES (MockKES t)] -> ShowS
$cshowList :: forall (t :: Nat). [SignKeyKES (MockKES t)] -> ShowS
show :: SignKeyKES (MockKES t) -> String
$cshow :: forall (t :: Nat). SignKeyKES (MockKES t) -> String
showsPrec :: Int -> SignKeyKES (MockKES t) -> ShowS
$cshowsPrec :: forall (t :: Nat). Int -> SignKeyKES (MockKES t) -> ShowS
Show, SignKeyKES (MockKES t) -> SignKeyKES (MockKES t) -> Bool
(SignKeyKES (MockKES t) -> SignKeyKES (MockKES t) -> Bool)
-> (SignKeyKES (MockKES t) -> SignKeyKES (MockKES t) -> Bool)
-> Eq (SignKeyKES (MockKES t))
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (t :: Nat).
SignKeyKES (MockKES t) -> SignKeyKES (MockKES t) -> Bool
/= :: SignKeyKES (MockKES t) -> SignKeyKES (MockKES t) -> Bool
$c/= :: forall (t :: Nat).
SignKeyKES (MockKES t) -> SignKeyKES (MockKES t) -> Bool
== :: SignKeyKES (MockKES t) -> SignKeyKES (MockKES t) -> Bool
$c== :: forall (t :: Nat).
SignKeyKES (MockKES t) -> SignKeyKES (MockKES t) -> Bool
Eq, (forall x.
 SignKeyKES (MockKES t) -> Rep (SignKeyKES (MockKES t)) x)
-> (forall x.
    Rep (SignKeyKES (MockKES t)) x -> SignKeyKES (MockKES t))
-> Generic (SignKeyKES (MockKES t))
forall x. Rep (SignKeyKES (MockKES t)) x -> SignKeyKES (MockKES t)
forall x. SignKeyKES (MockKES t) -> Rep (SignKeyKES (MockKES t)) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (t :: Nat) x.
Rep (SignKeyKES (MockKES t)) x -> SignKeyKES (MockKES t)
forall (t :: Nat) x.
SignKeyKES (MockKES t) -> Rep (SignKeyKES (MockKES t)) x
$cto :: forall (t :: Nat) x.
Rep (SignKeyKES (MockKES t)) x -> SignKeyKES (MockKES t)
$cfrom :: forall (t :: Nat) x.
SignKeyKES (MockKES t) -> Rep (SignKeyKES (MockKES t)) x
Generic)
        deriving anyclass (Context -> SignKeyKES (MockKES t) -> IO (Maybe ThunkInfo)
Proxy (SignKeyKES (MockKES t)) -> String
(Context -> SignKeyKES (MockKES t) -> IO (Maybe ThunkInfo))
-> (Context -> SignKeyKES (MockKES t) -> IO (Maybe ThunkInfo))
-> (Proxy (SignKeyKES (MockKES t)) -> String)
-> NoThunks (SignKeyKES (MockKES t))
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
forall (t :: Nat).
Context -> SignKeyKES (MockKES t) -> IO (Maybe ThunkInfo)
forall (t :: Nat). Proxy (SignKeyKES (MockKES t)) -> String
showTypeOf :: Proxy (SignKeyKES (MockKES t)) -> String
$cshowTypeOf :: forall (t :: Nat). Proxy (SignKeyKES (MockKES t)) -> String
wNoThunks :: Context -> SignKeyKES (MockKES t) -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall (t :: Nat).
Context -> SignKeyKES (MockKES t) -> IO (Maybe ThunkInfo)
noThunks :: Context -> SignKeyKES (MockKES t) -> IO (Maybe ThunkInfo)
$cnoThunks :: forall (t :: Nat).
Context -> SignKeyKES (MockKES t) -> IO (Maybe ThunkInfo)
NoThunks)

    data SigKES (MockKES t) =
           SigMockKES !(Hash ShortHash ()) !(SignKeyKES (MockKES t))
        deriving stock    (Int -> SigKES (MockKES t) -> ShowS
[SigKES (MockKES t)] -> ShowS
SigKES (MockKES t) -> String
(Int -> SigKES (MockKES t) -> ShowS)
-> (SigKES (MockKES t) -> String)
-> ([SigKES (MockKES t)] -> ShowS)
-> Show (SigKES (MockKES t))
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (t :: Nat). Int -> SigKES (MockKES t) -> ShowS
forall (t :: Nat). [SigKES (MockKES t)] -> ShowS
forall (t :: Nat). SigKES (MockKES t) -> String
showList :: [SigKES (MockKES t)] -> ShowS
$cshowList :: forall (t :: Nat). [SigKES (MockKES t)] -> ShowS
show :: SigKES (MockKES t) -> String
$cshow :: forall (t :: Nat). SigKES (MockKES t) -> String
showsPrec :: Int -> SigKES (MockKES t) -> ShowS
$cshowsPrec :: forall (t :: Nat). Int -> SigKES (MockKES t) -> ShowS
Show, SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
(SigKES (MockKES t) -> SigKES (MockKES t) -> Bool)
-> (SigKES (MockKES t) -> SigKES (MockKES t) -> Bool)
-> Eq (SigKES (MockKES t))
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (t :: Nat). SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
/= :: SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
$c/= :: forall (t :: Nat). SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
== :: SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
$c== :: forall (t :: Nat). SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
Eq, Eq (SigKES (MockKES t))
Eq (SigKES (MockKES t))
-> (SigKES (MockKES t) -> SigKES (MockKES t) -> Ordering)
-> (SigKES (MockKES t) -> SigKES (MockKES t) -> Bool)
-> (SigKES (MockKES t) -> SigKES (MockKES t) -> Bool)
-> (SigKES (MockKES t) -> SigKES (MockKES t) -> Bool)
-> (SigKES (MockKES t) -> SigKES (MockKES t) -> Bool)
-> (SigKES (MockKES t) -> SigKES (MockKES t) -> SigKES (MockKES t))
-> (SigKES (MockKES t) -> SigKES (MockKES t) -> SigKES (MockKES t))
-> Ord (SigKES (MockKES t))
SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
SigKES (MockKES t) -> SigKES (MockKES t) -> Ordering
SigKES (MockKES t) -> SigKES (MockKES t) -> SigKES (MockKES t)
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
forall (t :: Nat). (TypeError ...) => Eq (SigKES (MockKES t))
forall (t :: Nat).
(TypeError ...) =>
SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
forall (t :: Nat).
(TypeError ...) =>
SigKES (MockKES t) -> SigKES (MockKES t) -> Ordering
forall (t :: Nat).
(TypeError ...) =>
SigKES (MockKES t) -> SigKES (MockKES t) -> SigKES (MockKES t)
min :: SigKES (MockKES t) -> SigKES (MockKES t) -> SigKES (MockKES t)
$cmin :: forall (t :: Nat).
(TypeError ...) =>
SigKES (MockKES t) -> SigKES (MockKES t) -> SigKES (MockKES t)
max :: SigKES (MockKES t) -> SigKES (MockKES t) -> SigKES (MockKES t)
$cmax :: forall (t :: Nat).
(TypeError ...) =>
SigKES (MockKES t) -> SigKES (MockKES t) -> SigKES (MockKES t)
>= :: SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
$c>= :: forall (t :: Nat).
(TypeError ...) =>
SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
> :: SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
$c> :: forall (t :: Nat).
(TypeError ...) =>
SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
<= :: SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
$c<= :: forall (t :: Nat).
(TypeError ...) =>
SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
< :: SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
$c< :: forall (t :: Nat).
(TypeError ...) =>
SigKES (MockKES t) -> SigKES (MockKES t) -> Bool
compare :: SigKES (MockKES t) -> SigKES (MockKES t) -> Ordering
$ccompare :: forall (t :: Nat).
(TypeError ...) =>
SigKES (MockKES t) -> SigKES (MockKES t) -> Ordering
$cp1Ord :: forall (t :: Nat). (TypeError ...) => Eq (SigKES (MockKES t))
Ord, (forall x. SigKES (MockKES t) -> Rep (SigKES (MockKES t)) x)
-> (forall x. Rep (SigKES (MockKES t)) x -> SigKES (MockKES t))
-> Generic (SigKES (MockKES t))
forall x. Rep (SigKES (MockKES t)) x -> SigKES (MockKES t)
forall x. SigKES (MockKES t) -> Rep (SigKES (MockKES t)) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (t :: Nat) x.
Rep (SigKES (MockKES t)) x -> SigKES (MockKES t)
forall (t :: Nat) x.
SigKES (MockKES t) -> Rep (SigKES (MockKES t)) x
$cto :: forall (t :: Nat) x.
Rep (SigKES (MockKES t)) x -> SigKES (MockKES t)
$cfrom :: forall (t :: Nat) x.
SigKES (MockKES t) -> Rep (SigKES (MockKES t)) x
Generic)
        deriving anyclass (Context -> SigKES (MockKES t) -> IO (Maybe ThunkInfo)
Proxy (SigKES (MockKES t)) -> String
(Context -> SigKES (MockKES t) -> IO (Maybe ThunkInfo))
-> (Context -> SigKES (MockKES t) -> IO (Maybe ThunkInfo))
-> (Proxy (SigKES (MockKES t)) -> String)
-> NoThunks (SigKES (MockKES t))
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
forall (t :: Nat).
Context -> SigKES (MockKES t) -> IO (Maybe ThunkInfo)
forall (t :: Nat). Proxy (SigKES (MockKES t)) -> String
showTypeOf :: Proxy (SigKES (MockKES t)) -> String
$cshowTypeOf :: forall (t :: Nat). Proxy (SigKES (MockKES t)) -> String
wNoThunks :: Context -> SigKES (MockKES t) -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall (t :: Nat).
Context -> SigKES (MockKES t) -> IO (Maybe ThunkInfo)
noThunks :: Context -> SigKES (MockKES t) -> IO (Maybe ThunkInfo)
$cnoThunks :: forall (t :: Nat).
Context -> SigKES (MockKES t) -> IO (Maybe ThunkInfo)
NoThunks)

    --
    -- Metadata and basic key operations
    --

    algorithmNameKES :: proxy (MockKES t) -> String
algorithmNameKES proxy (MockKES t)
proxy = String
"mock_" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Period -> String
forall a. Show a => a -> String
show (proxy (MockKES t) -> Period
forall v (proxy :: * -> *). KESAlgorithm v => proxy v -> Period
totalPeriodsKES proxy (MockKES t)
proxy)

    deriveVerKeyKES :: SignKeyKES (MockKES t) -> VerKeyKES (MockKES t)
deriveVerKeyKES (SignKeyMockKES vk _) = VerKeyKES (MockKES t)
vk

    sizeVerKeyKES :: proxy (MockKES t) -> Period
sizeVerKeyKES  proxy (MockKES t)
_ = Period
8
    sizeSignKeyKES :: proxy (MockKES t) -> Period
sizeSignKeyKES proxy (MockKES t)
_ = Period
16
    sizeSigKES :: proxy (MockKES t) -> Period
sizeSigKES     proxy (MockKES t)
_ = Period
24


    --
    -- Core algorithm operations
    --

    type Signable (MockKES t) = SignableRepresentation

    updateKES :: ContextKES (MockKES t)
-> SignKeyKES (MockKES t)
-> Period
-> Maybe (SignKeyKES (MockKES t))
updateKES () (SignKeyMockKES vk t') Period
t =
        Bool
-> Maybe (SignKeyKES (MockKES t)) -> Maybe (SignKeyKES (MockKES t))
forall a. HasCallStack => Bool -> a -> a
assert (Period
t Period -> Period -> Bool
forall a. Eq a => a -> a -> Bool
== Period
t') (Maybe (SignKeyKES (MockKES t)) -> Maybe (SignKeyKES (MockKES t)))
-> Maybe (SignKeyKES (MockKES t)) -> Maybe (SignKeyKES (MockKES t))
forall a b. (a -> b) -> a -> b
$
         if Period
tPeriod -> Period -> Period
forall a. Num a => a -> a -> a
+Period
1 Period -> Period -> Bool
forall a. Ord a => a -> a -> Bool
< Proxy (MockKES t) -> Period
forall v (proxy :: * -> *). KESAlgorithm v => proxy v -> Period
totalPeriodsKES (Proxy (MockKES t)
forall k (t :: k). Proxy t
Proxy @(MockKES t))
           then SignKeyKES (MockKES t) -> Maybe (SignKeyKES (MockKES t))
forall a. a -> Maybe a
Just (VerKeyKES (MockKES t) -> Period -> SignKeyKES (MockKES t)
forall (t :: Nat).
VerKeyKES (MockKES t) -> Period -> SignKeyKES (MockKES t)
SignKeyMockKES VerKeyKES (MockKES t)
vk (Period
tPeriod -> Period -> Period
forall a. Num a => a -> a -> a
+Period
1))
           else Maybe (SignKeyKES (MockKES t))
forall a. Maybe a
Nothing

    -- | Produce valid signature only with correct key, i.e., same iteration and
    -- allowed KES period.
    signKES :: ContextKES (MockKES t)
-> Period -> a -> SignKeyKES (MockKES t) -> SigKES (MockKES t)
signKES () Period
t a
a (SignKeyMockKES vk t') =
        Bool -> SigKES (MockKES t) -> SigKES (MockKES t)
forall a. HasCallStack => Bool -> a -> a
assert (Period
t Period -> Period -> Bool
forall a. Eq a => a -> a -> Bool
== Period
t') (SigKES (MockKES t) -> SigKES (MockKES t))
-> SigKES (MockKES t) -> SigKES (MockKES t)
forall a b. (a -> b) -> a -> b
$
        Hash ShortHash () -> SignKeyKES (MockKES t) -> SigKES (MockKES t)
forall (t :: Nat).
Hash ShortHash () -> SignKeyKES (MockKES t) -> SigKES (MockKES t)
SigMockKES (Hash ShortHash a -> Hash ShortHash ()
forall h a b. Hash h a -> Hash h b
castHash ((a -> ByteString) -> a -> Hash ShortHash a
forall h a. HashAlgorithm h => (a -> ByteString) -> a -> Hash h a
hashWith a -> ByteString
forall a. SignableRepresentation a => a -> ByteString
getSignableRepresentation a
a))
                   (VerKeyKES (MockKES t) -> Period -> SignKeyKES (MockKES t)
forall (t :: Nat).
VerKeyKES (MockKES t) -> Period -> SignKeyKES (MockKES t)
SignKeyMockKES VerKeyKES (MockKES t)
vk Period
t)

    verifyKES :: ContextKES (MockKES t)
-> VerKeyKES (MockKES t)
-> Period
-> a
-> SigKES (MockKES t)
-> Either String ()
verifyKES () VerKeyKES (MockKES t)
vk Period
t a
a (SigMockKES h (SignKeyMockKES vk' t'))
      | VerKeyKES (MockKES t)
vk VerKeyKES (MockKES t) -> VerKeyKES (MockKES t) -> Bool
forall a. Eq a => a -> a -> Bool
/= VerKeyKES (MockKES t)
vk'
      = String -> Either String ()
forall a b. a -> Either a b
Left String
"KES verification failed"

      | Period
t' Period -> Period -> Bool
forall a. Eq a => a -> a -> Bool
== Period
t
      , Hash ShortHash a -> Hash ShortHash ()
forall h a b. Hash h a -> Hash h b
castHash ((a -> ByteString) -> a -> Hash ShortHash a
forall h a. HashAlgorithm h => (a -> ByteString) -> a -> Hash h a
hashWith a -> ByteString
forall a. SignableRepresentation a => a -> ByteString
getSignableRepresentation a
a) Hash ShortHash () -> Hash ShortHash () -> Bool
forall a. Eq a => a -> a -> Bool
== Hash ShortHash ()
h
      = () -> Either String ()
forall a b. b -> Either a b
Right ()

      | Bool
otherwise
      = String -> Either String ()
forall a b. a -> Either a b
Left String
"KES verification failed"

    totalPeriodsKES :: proxy (MockKES t) -> Period
totalPeriodsKES  proxy (MockKES t)
_ = Natural -> Period
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy t -> Natural
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Natural
natVal (Proxy t
forall k (t :: k). Proxy t
Proxy @t))

    --
    -- Key generation
    --

    genKeyKES :: Seed -> SignKeyKES (MockKES t)
genKeyKES Seed
seed =
        let vk :: VerKeyKES (MockKES t)
vk = Word64 -> VerKeyKES (MockKES t)
forall (t :: Nat). Word64 -> VerKeyKES (MockKES t)
VerKeyMockKES (Seed -> (forall (m :: * -> *). MonadRandom m => m Word64) -> Word64
forall a. Seed -> (forall (m :: * -> *). MonadRandom m => m a) -> a
runMonadRandomWithSeed Seed
seed forall (m :: * -> *). MonadRandom m => m Word64
getRandomWord64)
         in VerKeyKES (MockKES t) -> Period -> SignKeyKES (MockKES t)
forall (t :: Nat).
VerKeyKES (MockKES t) -> Period -> SignKeyKES (MockKES t)
SignKeyMockKES VerKeyKES (MockKES t)
vk Period
0


    --
    -- raw serialise/deserialise
    --

    rawSerialiseVerKeyKES :: VerKeyKES (MockKES t) -> ByteString
rawSerialiseVerKeyKES (VerKeyMockKES vk) =
        Word64 -> ByteString
writeBinaryWord64 Word64
vk

    rawSerialiseSignKeyKES :: SignKeyKES (MockKES t) -> ByteString
rawSerialiseSignKeyKES (SignKeyMockKES vk t) =
        VerKeyKES (MockKES t) -> ByteString
forall v. KESAlgorithm v => VerKeyKES v -> ByteString
rawSerialiseVerKeyKES VerKeyKES (MockKES t)
vk
     ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Word64 -> ByteString
writeBinaryWord64 (Period -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Period
t)

    rawSerialiseSigKES :: SigKES (MockKES t) -> ByteString
rawSerialiseSigKES (SigMockKES h sk) =
        Hash ShortHash () -> ByteString
forall h a. Hash h a -> ByteString
hashToBytes Hash ShortHash ()
h
     ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> SignKeyKES (MockKES t) -> ByteString
forall v. KESAlgorithm v => SignKeyKES v -> ByteString
rawSerialiseSignKeyKES SignKeyKES (MockKES t)
sk

    rawDeserialiseVerKeyKES :: ByteString -> Maybe (VerKeyKES (MockKES t))
rawDeserialiseVerKeyKES ByteString
bs
      | [ByteString
vkb] <- [Int] -> ByteString -> [ByteString]
splitsAt [Int
8] ByteString
bs
      , let vk :: Word64
vk = ByteString -> Word64
readBinaryWord64 ByteString
vkb
      = VerKeyKES (MockKES t) -> Maybe (VerKeyKES (MockKES t))
forall a. a -> Maybe a
Just (VerKeyKES (MockKES t) -> Maybe (VerKeyKES (MockKES t)))
-> VerKeyKES (MockKES t) -> Maybe (VerKeyKES (MockKES t))
forall a b. (a -> b) -> a -> b
$! Word64 -> VerKeyKES (MockKES t)
forall (t :: Nat). Word64 -> VerKeyKES (MockKES t)
VerKeyMockKES Word64
vk

      | Bool
otherwise
      = Maybe (VerKeyKES (MockKES t))
forall a. Maybe a
Nothing

    rawDeserialiseSignKeyKES :: ByteString -> Maybe (SignKeyKES (MockKES t))
rawDeserialiseSignKeyKES ByteString
bs
      | [ByteString
vkb, ByteString
tb] <- [Int] -> ByteString -> [ByteString]
splitsAt [Int
8, Int
8] ByteString
bs
      , Just VerKeyKES (MockKES t)
vk   <- ByteString -> Maybe (VerKeyKES (MockKES t))
forall v. KESAlgorithm v => ByteString -> Maybe (VerKeyKES v)
rawDeserialiseVerKeyKES ByteString
vkb
      , let t :: Period
t      = Word64 -> Period
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Word64
readBinaryWord64 ByteString
tb)
      = SignKeyKES (MockKES t) -> Maybe (SignKeyKES (MockKES t))
forall a. a -> Maybe a
Just (SignKeyKES (MockKES t) -> Maybe (SignKeyKES (MockKES t)))
-> SignKeyKES (MockKES t) -> Maybe (SignKeyKES (MockKES t))
forall a b. (a -> b) -> a -> b
$! VerKeyKES (MockKES t) -> Period -> SignKeyKES (MockKES t)
forall (t :: Nat).
VerKeyKES (MockKES t) -> Period -> SignKeyKES (MockKES t)
SignKeyMockKES VerKeyKES (MockKES t)
vk Period
t

      | Bool
otherwise
      = Maybe (SignKeyKES (MockKES t))
forall a. Maybe a
Nothing

    rawDeserialiseSigKES :: ByteString -> Maybe (SigKES (MockKES t))
rawDeserialiseSigKES ByteString
bs
      | [ByteString
hb, ByteString
skb] <- [Int] -> ByteString -> [ByteString]
splitsAt [Int
8, Int
16] ByteString
bs
      , Just Hash ShortHash ()
h    <- ByteString -> Maybe (Hash ShortHash ())
forall h a. HashAlgorithm h => ByteString -> Maybe (Hash h a)
hashFromBytes ByteString
hb
      , Just SignKeyKES (MockKES t)
sk   <- ByteString -> Maybe (SignKeyKES (MockKES t))
forall v. KESAlgorithm v => ByteString -> Maybe (SignKeyKES v)
rawDeserialiseSignKeyKES ByteString
skb
      = SigKES (MockKES t) -> Maybe (SigKES (MockKES t))
forall a. a -> Maybe a
Just (SigKES (MockKES t) -> Maybe (SigKES (MockKES t)))
-> SigKES (MockKES t) -> Maybe (SigKES (MockKES t))
forall a b. (a -> b) -> a -> b
$! Hash ShortHash () -> SignKeyKES (MockKES t) -> SigKES (MockKES t)
forall (t :: Nat).
Hash ShortHash () -> SignKeyKES (MockKES t) -> SigKES (MockKES t)
SigMockKES Hash ShortHash ()
h SignKeyKES (MockKES t)
sk

      | Bool
otherwise
      = Maybe (SigKES (MockKES t))
forall a. Maybe a
Nothing



instance KnownNat t => ToCBOR (VerKeyKES (MockKES t)) where
  toCBOR :: VerKeyKES (MockKES t) -> Encoding
toCBOR = VerKeyKES (MockKES t) -> Encoding
forall v. KESAlgorithm v => VerKeyKES v -> Encoding
encodeVerKeyKES
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (VerKeyKES (MockKES t)) -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_size = Proxy (VerKeyKES (MockKES t)) -> Size
forall v. KESAlgorithm v => Proxy (VerKeyKES v) -> Size
encodedVerKeyKESSizeExpr

instance KnownNat t => FromCBOR (VerKeyKES (MockKES t)) where
  fromCBOR :: Decoder s (VerKeyKES (MockKES t))
fromCBOR = Decoder s (VerKeyKES (MockKES t))
forall v s. KESAlgorithm v => Decoder s (VerKeyKES v)
decodeVerKeyKES

instance KnownNat t => ToCBOR (SignKeyKES (MockKES t)) where
  toCBOR :: SignKeyKES (MockKES t) -> Encoding
toCBOR = SignKeyKES (MockKES t) -> Encoding
forall v. KESAlgorithm v => SignKeyKES v -> Encoding
encodeSignKeyKES
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (SignKeyKES (MockKES t)) -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_size = Proxy (SignKeyKES (MockKES t)) -> Size
forall v. KESAlgorithm v => Proxy (SignKeyKES v) -> Size
encodedSignKeyKESSizeExpr

instance KnownNat t => FromCBOR (SignKeyKES (MockKES t)) where
  fromCBOR :: Decoder s (SignKeyKES (MockKES t))
fromCBOR = Decoder s (SignKeyKES (MockKES t))
forall v s. KESAlgorithm v => Decoder s (SignKeyKES v)
decodeSignKeyKES

instance KnownNat t => ToCBOR (SigKES (MockKES t)) where
  toCBOR :: SigKES (MockKES t) -> Encoding
toCBOR = SigKES (MockKES t) -> Encoding
forall v. KESAlgorithm v => SigKES v -> Encoding
encodeSigKES
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (SigKES (MockKES t)) -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_size = Proxy (SigKES (MockKES t)) -> Size
forall v. KESAlgorithm v => Proxy (SigKES v) -> Size
encodedSigKESSizeExpr

instance KnownNat t => FromCBOR (SigKES (MockKES t)) where
  fromCBOR :: Decoder s (SigKES (MockKES t))
fromCBOR = Decoder s (SigKES (MockKES t))
forall v s. KESAlgorithm v => Decoder s (SigKES v)
decodeSigKES