{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE NoStarIsType #-}

-- | A standard signature scheme is a forward-secure signature scheme with a
-- single time period.
--
-- This is the base case in the naive recursive implementation of the sum
-- composition from section 3 of the \"MMM\" paper:
--
-- /Composition and Efficiency Tradeoffs for Forward-Secure Digital Signatures/
-- By Tal Malkin, Daniele Micciancio and Sara Miner
-- <https://eprint.iacr.org/2001/034>
--
-- Specfically it states:
--
-- > In order to unify the presentation, we regard standard signature schemes
-- > as forward-seure signature schemes with one time period, namely T = 1.
--
-- So this module simply provides a wrapper 'CompactSingleKES' that turns any
-- 'DSIGNMAlgorithm' into an instance of 'KESAlgorithm' with a single period.
--
-- See "Cardano.Crypto.KES.CompactSum" for the composition case.
--
-- Compared to the implementation in 'Cardano.Crypto.KES.Single', this flavor
-- stores the VerKey used for signing along with the signature. The purpose of
-- this is so that we can avoid storing a pair of VerKeys at every branch node,
-- like 'Cardano.Crypto.KES.Sum' does. See 'Cardano.Crypto.KES.CompactSum' for
-- more details.
module Cardano.Crypto.KES.CompactSingle (
  CompactSingleKES,
  VerKeyKES (..),
  SignKeyKES (..),
  SigKES (..),
) where

import Control.Monad (guard, (<$!>))
import qualified Data.ByteString as BS
import Data.Proxy (Proxy (..))
import GHC.Generics (Generic)
import GHC.TypeLits (KnownNat, type (+))
import NoThunks.Class (NoThunks)

import Control.DeepSeq (NFData)
import Control.Exception (assert)

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

import Cardano.Crypto.DSIGN.Class as DSIGN
import Cardano.Crypto.DirectSerialise
import Cardano.Crypto.Hash.Class
import Cardano.Crypto.KES.Class

-- | A standard signature scheme is a forward-secure signature scheme with a
-- single time period.
data CompactSingleKES d

deriving newtype instance NFData (VerKeyDSIGN d) => NFData (VerKeyKES (CompactSingleKES d))
deriving newtype instance NFData (SignKeyDSIGNM d) => NFData (SignKeyKES (CompactSingleKES d))

deriving instance
  (NFData (SigDSIGN d), NFData (VerKeyDSIGN d)) => NFData (SigKES (CompactSingleKES d))

instance
  ( DSIGNMAlgorithm d
  , KnownNat (SizeSigDSIGN d + SizeVerKeyDSIGN d)
  ) =>
  KESAlgorithm (CompactSingleKES d)
  where
  type SeedSizeKES (CompactSingleKES d) = SeedSizeDSIGN d

  --
  -- Key and signature types
  --

  newtype VerKeyKES (CompactSingleKES d) = VerKeyCompactSingleKES (VerKeyDSIGN d)
    deriving (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall d x.
Rep (VerKeyKES (CompactSingleKES d)) x
-> VerKeyKES (CompactSingleKES d)
forall d x.
VerKeyKES (CompactSingleKES d)
-> Rep (VerKeyKES (CompactSingleKES d)) x
$cto :: forall d x.
Rep (VerKeyKES (CompactSingleKES d)) x
-> VerKeyKES (CompactSingleKES d)
$cfrom :: forall d x.
VerKeyKES (CompactSingleKES d)
-> Rep (VerKeyKES (CompactSingleKES d)) x
Generic)

  data SigKES (CompactSingleKES d) = SigCompactSingleKES !(SigDSIGN d) !(VerKeyDSIGN d)
    deriving (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall d x.
Rep (SigKES (CompactSingleKES d)) x -> SigKES (CompactSingleKES d)
forall d x.
SigKES (CompactSingleKES d) -> Rep (SigKES (CompactSingleKES d)) x
$cto :: forall d x.
Rep (SigKES (CompactSingleKES d)) x -> SigKES (CompactSingleKES d)
$cfrom :: forall d x.
SigKES (CompactSingleKES d) -> Rep (SigKES (CompactSingleKES d)) x
Generic)

  newtype SignKeyKES (CompactSingleKES d) = SignKeyCompactSingleKES (SignKeyDSIGNM d)

  type ContextKES (CompactSingleKES d) = ContextDSIGN d
  type Signable (CompactSingleKES d) = DSIGN.Signable d

  --
  -- Metadata and basic key operations
  --

  algorithmNameKES :: forall (proxy :: Type -> Type).
proxy (CompactSingleKES d) -> String
algorithmNameKES proxy (CompactSingleKES d)
_ = forall v (proxy :: Type -> Type).
DSIGNAlgorithm v =>
proxy v -> String
algorithmNameDSIGN (forall {k} (t :: k). Proxy t
Proxy :: Proxy d) forall a. [a] -> [a] -> [a]
++ String
"_kes_2^0"

  totalPeriodsKES :: forall (proxy :: Type -> Type). proxy (CompactSingleKES d) -> Word
totalPeriodsKES proxy (CompactSingleKES d)
_ = Word
1

  --
  -- Core algorithm operations
  --

  verifyKES :: forall a.
(Signable (CompactSingleKES d) a, HasCallStack) =>
ContextKES (CompactSingleKES d)
-> VerKeyKES (CompactSingleKES d)
-> Word
-> a
-> SigKES (CompactSingleKES d)
-> Either String ()
verifyKES = forall v a.
(OptimizedKESAlgorithm v, Signable v a, HasCallStack) =>
ContextKES v
-> VerKeyKES v -> Word -> a -> SigKES v -> Either String ()
verifyOptimizedKES

  --
  -- raw serialise/deserialise
  --

  type SizeVerKeyKES (CompactSingleKES d) = SizeVerKeyDSIGN d
  type SizeSignKeyKES (CompactSingleKES d) = SizeSignKeyDSIGN d
  type SizeSigKES (CompactSingleKES d) = SizeSigDSIGN d + SizeVerKeyDSIGN d

  hashVerKeyKES :: forall h.
HashAlgorithm h =>
VerKeyKES (CompactSingleKES d)
-> Hash h (VerKeyKES (CompactSingleKES d))
hashVerKeyKES (VerKeyCompactSingleKES VerKeyDSIGN d
vk) =
    forall h a b. Hash h a -> Hash h b
castHash (forall v h.
(DSIGNAlgorithm v, HashAlgorithm h) =>
VerKeyDSIGN v -> Hash h (VerKeyDSIGN v)
hashVerKeyDSIGN VerKeyDSIGN d
vk)

  rawSerialiseVerKeyKES :: VerKeyKES (CompactSingleKES d) -> ByteString
rawSerialiseVerKeyKES (VerKeyCompactSingleKES VerKeyDSIGN d
vk) = forall v. DSIGNAlgorithm v => VerKeyDSIGN v -> ByteString
rawSerialiseVerKeyDSIGN VerKeyDSIGN d
vk
  rawSerialiseSigKES :: SigKES (CompactSingleKES d) -> ByteString
rawSerialiseSigKES (SigCompactSingleKES SigDSIGN d
sig VerKeyDSIGN d
vk) =
    forall v. DSIGNAlgorithm v => SigDSIGN v -> ByteString
rawSerialiseSigDSIGN SigDSIGN d
sig forall a. Semigroup a => a -> a -> a
<> forall v. DSIGNAlgorithm v => VerKeyDSIGN v -> ByteString
rawSerialiseVerKeyDSIGN VerKeyDSIGN d
vk

  rawDeserialiseVerKeyKES :: ByteString -> Maybe (VerKeyKES (CompactSingleKES d))
rawDeserialiseVerKeyKES = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap forall d. VerKeyDSIGN d -> VerKeyKES (CompactSingleKES d)
VerKeyCompactSingleKES forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. DSIGNAlgorithm v => ByteString -> Maybe (VerKeyDSIGN v)
rawDeserialiseVerKeyDSIGN
  rawDeserialiseSigKES :: ByteString -> Maybe (SigKES (CompactSingleKES d))
rawDeserialiseSigKES ByteString
b = do
    forall (f :: Type -> Type). Alternative f => Bool -> f ()
guard (ByteString -> Int
BS.length ByteString
b forall a. Eq a => a -> a -> Bool
== forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
size_total)
    SigDSIGN d
sigma <- forall v. DSIGNAlgorithm v => ByteString -> Maybe (SigDSIGN v)
rawDeserialiseSigDSIGN ByteString
b_sig
    VerKeyDSIGN d
vk <- forall v. DSIGNAlgorithm v => ByteString -> Maybe (VerKeyDSIGN v)
rawDeserialiseVerKeyDSIGN ByteString
b_vk
    forall (m :: Type -> Type) a. Monad m => a -> m a
return (forall d.
SigDSIGN d -> VerKeyDSIGN d -> SigKES (CompactSingleKES d)
SigCompactSingleKES SigDSIGN d
sigma VerKeyDSIGN d
vk)
    where
      b_sig :: ByteString
b_sig = Word -> Word -> ByteString -> ByteString
slice Word
off_sig Word
size_sig ByteString
b
      b_vk :: ByteString
b_vk = Word -> Word -> ByteString -> ByteString
slice Word
off_vk Word
size_vk ByteString
b

      size_sig :: Word
size_sig = forall v (proxy :: Type -> Type).
DSIGNAlgorithm v =>
proxy v -> Word
sizeSigDSIGN (forall {k} (t :: k). Proxy t
Proxy :: Proxy d)
      size_vk :: Word
size_vk = forall v (proxy :: Type -> Type).
DSIGNAlgorithm v =>
proxy v -> Word
sizeVerKeyDSIGN (forall {k} (t :: k). Proxy t
Proxy :: Proxy d)
      size_total :: Word
size_total = forall v (proxy :: Type -> Type). KESAlgorithm v => proxy v -> Word
sizeSigKES (forall {k} (t :: k). Proxy t
Proxy :: Proxy (CompactSingleKES d))

      off_sig :: Word
off_sig = Word
0 :: Word
      off_vk :: Word
off_vk = Word
size_sig

  deriveVerKeyKES :: forall (m :: Type -> Type).
(MonadST m, MonadThrow m) =>
SignKeyKES (CompactSingleKES d)
-> m (VerKeyKES (CompactSingleKES d))
deriveVerKeyKES (SignKeyCompactSingleKES SignKeyDSIGNM d
v) =
    forall d. VerKeyDSIGN d -> VerKeyKES (CompactSingleKES d)
VerKeyCompactSingleKES forall (m :: Type -> Type) a b. Monad m => (a -> b) -> m a -> m b
<$!> forall v (m :: Type -> Type).
(DSIGNMAlgorithm v, MonadThrow m, MonadST m) =>
SignKeyDSIGNM v -> m (VerKeyDSIGN v)
deriveVerKeyDSIGNM SignKeyDSIGNM d
v

  --
  -- Core algorithm operations
  --
  signKES :: forall a (m :: Type -> Type).
(Signable (CompactSingleKES d) a, MonadST m, MonadThrow m) =>
ContextKES (CompactSingleKES d)
-> Word
-> a
-> SignKeyKES (CompactSingleKES d)
-> m (SigKES (CompactSingleKES d))
signKES ContextKES (CompactSingleKES d)
ctxt Word
t a
a (SignKeyCompactSingleKES SignKeyDSIGNM d
sk) =
    forall a. HasCallStack => Bool -> a -> a
assert (Word
t forall a. Eq a => a -> a -> Bool
== Word
0) forall a b. (a -> b) -> a -> b
$
      forall d.
SigDSIGN d -> VerKeyDSIGN d -> SigKES (CompactSingleKES d)
SigCompactSingleKES forall (m :: Type -> Type) a b. Monad m => (a -> b) -> m a -> m b
<$!> forall v a (m :: Type -> Type).
(DSIGNMAlgorithm v, Signable v a, MonadST m, MonadThrow m) =>
ContextDSIGN v -> a -> SignKeyDSIGNM v -> m (SigDSIGN v)
signDSIGNM ContextKES (CompactSingleKES d)
ctxt a
a SignKeyDSIGNM d
sk forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> forall v (m :: Type -> Type).
(DSIGNMAlgorithm v, MonadThrow m, MonadST m) =>
SignKeyDSIGNM v -> m (VerKeyDSIGN v)
deriveVerKeyDSIGNM SignKeyDSIGNM d
sk

  updateKESWith :: forall (m :: Type -> Type).
(MonadST m, MonadThrow m) =>
MLockedAllocator m
-> ContextKES (CompactSingleKES d)
-> SignKeyKES (CompactSingleKES d)
-> Word
-> m (Maybe (SignKeyKES (CompactSingleKES d)))
updateKESWith MLockedAllocator m
_allocator ContextKES (CompactSingleKES d)
_ctx (SignKeyCompactSingleKES SignKeyDSIGNM d
_sk) Word
_to = forall (m :: Type -> Type) a. Monad m => a -> m a
return forall a. Maybe a
Nothing

  --
  -- Key generation
  --

  genKeyKESWith :: forall (m :: Type -> Type).
(MonadST m, MonadThrow m) =>
MLockedAllocator m
-> MLockedSeed (SeedSizeKES (CompactSingleKES d))
-> m (SignKeyKES (CompactSingleKES d))
genKeyKESWith MLockedAllocator m
allocator MLockedSeed (SeedSizeKES (CompactSingleKES d))
seed = forall d. SignKeyDSIGNM d -> SignKeyKES (CompactSingleKES d)
SignKeyCompactSingleKES forall (m :: Type -> Type) a b. Monad m => (a -> b) -> m a -> m b
<$!> forall v (m :: Type -> Type).
(DSIGNMAlgorithm v, MonadST m, MonadThrow m) =>
MLockedAllocator m
-> MLockedSeed (SeedSizeDSIGN v) -> m (SignKeyDSIGNM v)
genKeyDSIGNMWith MLockedAllocator m
allocator MLockedSeed (SeedSizeKES (CompactSingleKES d))
seed

  --
  -- forgetting
  --
  forgetSignKeyKESWith :: forall (m :: Type -> Type).
(MonadST m, MonadThrow m) =>
MLockedAllocator m -> SignKeyKES (CompactSingleKES d) -> m ()
forgetSignKeyKESWith MLockedAllocator m
allocator (SignKeyCompactSingleKES SignKeyDSIGNM d
v) =
    forall v (m :: Type -> Type).
(DSIGNMAlgorithm v, MonadST m, MonadThrow m) =>
MLockedAllocator m -> SignKeyDSIGNM v -> m ()
forgetSignKeyDSIGNMWith MLockedAllocator m
allocator SignKeyDSIGNM d
v

instance
  ( KESAlgorithm (CompactSingleKES d)
  , UnsoundDSIGNMAlgorithm d
  ) =>
  UnsoundPureKESAlgorithm (CompactSingleKES d)
  where
  data UnsoundPureSignKeyKES (CompactSingleKES d)
    = UnsoundPureSignKeyCompactSingleKES (SignKeyDSIGN d)
    deriving (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall d x.
Rep (UnsoundPureSignKeyKES (CompactSingleKES d)) x
-> UnsoundPureSignKeyKES (CompactSingleKES d)
forall d x.
UnsoundPureSignKeyKES (CompactSingleKES d)
-> Rep (UnsoundPureSignKeyKES (CompactSingleKES d)) x
$cto :: forall d x.
Rep (UnsoundPureSignKeyKES (CompactSingleKES d)) x
-> UnsoundPureSignKeyKES (CompactSingleKES d)
$cfrom :: forall d x.
UnsoundPureSignKeyKES (CompactSingleKES d)
-> Rep (UnsoundPureSignKeyKES (CompactSingleKES d)) x
Generic)

  unsoundPureSignKES :: forall a.
Signable (CompactSingleKES d) a =>
ContextKES (CompactSingleKES d)
-> Word
-> a
-> UnsoundPureSignKeyKES (CompactSingleKES d)
-> SigKES (CompactSingleKES d)
unsoundPureSignKES ContextKES (CompactSingleKES d)
ctxt Word
t a
a (UnsoundPureSignKeyCompactSingleKES SignKeyDSIGN d
sk) =
    forall a. HasCallStack => Bool -> a -> a
assert (Word
t forall a. Eq a => a -> a -> Bool
== Word
0) forall a b. (a -> b) -> a -> b
$!
      forall d.
SigDSIGN d -> VerKeyDSIGN d -> SigKES (CompactSingleKES d)
SigCompactSingleKES (forall v a.
(DSIGNAlgorithm v, Signable v a, HasCallStack) =>
ContextDSIGN v -> a -> SignKeyDSIGN v -> SigDSIGN v
signDSIGN ContextKES (CompactSingleKES d)
ctxt a
a SignKeyDSIGN d
sk) (forall v. DSIGNAlgorithm v => SignKeyDSIGN v -> VerKeyDSIGN v
deriveVerKeyDSIGN SignKeyDSIGN d
sk)

  unsoundPureUpdateKES :: ContextKES (CompactSingleKES d)
-> UnsoundPureSignKeyKES (CompactSingleKES d)
-> Word
-> Maybe (UnsoundPureSignKeyKES (CompactSingleKES d))
unsoundPureUpdateKES ContextKES (CompactSingleKES d)
_ctx UnsoundPureSignKeyKES (CompactSingleKES d)
_sk Word
_to = forall a. Maybe a
Nothing

  --
  -- Key generation
  --

  unsoundPureGenKeyKES :: Seed -> UnsoundPureSignKeyKES (CompactSingleKES d)
unsoundPureGenKeyKES Seed
seed =
    forall d.
SignKeyDSIGN d -> UnsoundPureSignKeyKES (CompactSingleKES d)
UnsoundPureSignKeyCompactSingleKES forall a b. (a -> b) -> a -> b
$! forall v. DSIGNAlgorithm v => Seed -> SignKeyDSIGN v
genKeyDSIGN Seed
seed

  unsoundPureDeriveVerKeyKES :: UnsoundPureSignKeyKES (CompactSingleKES d)
-> VerKeyKES (CompactSingleKES d)
unsoundPureDeriveVerKeyKES (UnsoundPureSignKeyCompactSingleKES SignKeyDSIGN d
v) =
    forall d. VerKeyDSIGN d -> VerKeyKES (CompactSingleKES d)
VerKeyCompactSingleKES forall a b. (a -> b) -> a -> b
$! forall v. DSIGNAlgorithm v => SignKeyDSIGN v -> VerKeyDSIGN v
deriveVerKeyDSIGN SignKeyDSIGN d
v

  unsoundPureSignKeyKESToSoundSignKeyKES :: forall (m :: Type -> Type).
(MonadST m, MonadThrow m) =>
UnsoundPureSignKeyKES (CompactSingleKES d)
-> m (SignKeyKES (CompactSingleKES d))
unsoundPureSignKeyKESToSoundSignKeyKES =
    forall (m :: Type -> Type) k.
(MonadST m, MonadThrow m, UnsoundKESAlgorithm k,
 UnsoundPureKESAlgorithm k) =>
UnsoundPureSignKeyKES k -> m (SignKeyKES k)
unsoundPureSignKeyKESToSoundSignKeyKESViaSer

  rawSerialiseUnsoundPureSignKeyKES :: UnsoundPureSignKeyKES (CompactSingleKES d) -> ByteString
rawSerialiseUnsoundPureSignKeyKES (UnsoundPureSignKeyCompactSingleKES SignKeyDSIGN d
sk) =
    forall v. DSIGNAlgorithm v => SignKeyDSIGN v -> ByteString
rawSerialiseSignKeyDSIGN SignKeyDSIGN d
sk
  rawDeserialiseUnsoundPureSignKeyKES :: ByteString -> Maybe (UnsoundPureSignKeyKES (CompactSingleKES d))
rawDeserialiseUnsoundPureSignKeyKES ByteString
b =
    forall d.
SignKeyDSIGN d -> UnsoundPureSignKeyKES (CompactSingleKES d)
UnsoundPureSignKeyCompactSingleKES forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> forall v. DSIGNAlgorithm v => ByteString -> Maybe (SignKeyDSIGN v)
rawDeserialiseSignKeyDSIGN ByteString
b

instance
  ( KESAlgorithm (CompactSingleKES d)
  , DSIGNMAlgorithm d
  ) =>
  OptimizedKESAlgorithm (CompactSingleKES d)
  where
  verifySigKES :: forall a.
(Signable (CompactSingleKES d) a, HasCallStack) =>
ContextKES (CompactSingleKES d)
-> Word -> a -> SigKES (CompactSingleKES d) -> Either String ()
verifySigKES ContextKES (CompactSingleKES d)
ctxt Word
t a
a (SigCompactSingleKES SigDSIGN d
sig VerKeyDSIGN d
vk) =
    forall a. HasCallStack => Bool -> a -> a
assert (Word
t forall a. Eq a => a -> a -> Bool
== Word
0) forall a b. (a -> b) -> a -> b
$
      forall v a.
(DSIGNAlgorithm v, Signable v a, HasCallStack) =>
ContextDSIGN v
-> VerKeyDSIGN v -> a -> SigDSIGN v -> Either String ()
verifyDSIGN ContextKES (CompactSingleKES d)
ctxt VerKeyDSIGN d
vk a
a SigDSIGN d
sig

  verKeyFromSigKES :: ContextKES (CompactSingleKES d)
-> Word
-> SigKES (CompactSingleKES d)
-> VerKeyKES (CompactSingleKES d)
verKeyFromSigKES ContextKES (CompactSingleKES d)
_ctxt Word
t (SigCompactSingleKES SigDSIGN d
_ VerKeyDSIGN d
vk) =
    forall a. HasCallStack => Bool -> a -> a
assert (Word
t forall a. Eq a => a -> a -> Bool
== Word
0) forall a b. (a -> b) -> a -> b
$
      forall d. VerKeyDSIGN d -> VerKeyKES (CompactSingleKES d)
VerKeyCompactSingleKES VerKeyDSIGN d
vk

instance
  (KESAlgorithm (CompactSingleKES d), UnsoundDSIGNMAlgorithm d) =>
  UnsoundKESAlgorithm (CompactSingleKES d)
  where
  rawSerialiseSignKeyKES :: forall (m :: Type -> Type).
(MonadST m, MonadThrow m) =>
SignKeyKES (CompactSingleKES d) -> m ByteString
rawSerialiseSignKeyKES (SignKeyCompactSingleKES SignKeyDSIGNM d
sk) = forall v (m :: Type -> Type).
(UnsoundDSIGNMAlgorithm v, MonadST m, MonadThrow m) =>
SignKeyDSIGNM v -> m ByteString
rawSerialiseSignKeyDSIGNM SignKeyDSIGNM d
sk
  rawDeserialiseSignKeyKESWith :: forall (m :: Type -> Type).
(MonadST m, MonadThrow m) =>
MLockedAllocator m
-> ByteString -> m (Maybe (SignKeyKES (CompactSingleKES d)))
rawDeserialiseSignKeyKESWith MLockedAllocator m
allocator ByteString
bs = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap forall d. SignKeyDSIGNM d -> SignKeyKES (CompactSingleKES d)
SignKeyCompactSingleKES forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> forall v (m :: Type -> Type).
(UnsoundDSIGNMAlgorithm v, MonadST m, MonadThrow m) =>
MLockedAllocator m -> ByteString -> m (Maybe (SignKeyDSIGNM v))
rawDeserialiseSignKeyDSIGNMWith MLockedAllocator m
allocator ByteString
bs

--
-- VerKey instances
--

deriving instance DSIGNMAlgorithm d => Show (VerKeyKES (CompactSingleKES d))
deriving instance DSIGNMAlgorithm d => Eq (VerKeyKES (CompactSingleKES d))

instance
  (DSIGNMAlgorithm d, KnownNat (SizeSigDSIGN d + SizeVerKeyDSIGN d)) =>
  ToCBOR (VerKeyKES (CompactSingleKES d))
  where
  toCBOR :: VerKeyKES (CompactSingleKES d) -> Encoding
toCBOR = forall v. KESAlgorithm v => VerKeyKES v -> Encoding
encodeVerKeyKES
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (VerKeyKES (CompactSingleKES d)) -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_size = forall v. KESAlgorithm v => Proxy (VerKeyKES v) -> Size
encodedVerKeyKESSizeExpr

instance
  (DSIGNMAlgorithm d, KnownNat (SizeSigDSIGN d + SizeVerKeyDSIGN d)) =>
  FromCBOR (VerKeyKES (CompactSingleKES d))
  where
  fromCBOR :: forall s. Decoder s (VerKeyKES (CompactSingleKES d))
fromCBOR = forall v s. KESAlgorithm v => Decoder s (VerKeyKES v)
decodeVerKeyKES

instance DSIGNMAlgorithm d => NoThunks (VerKeyKES (CompactSingleKES d))

--
-- SignKey instances
--

deriving via
  (SignKeyDSIGNM d)
  instance
    DSIGNMAlgorithm d => NoThunks (SignKeyKES (CompactSingleKES d))

--
-- Sig instances
--

deriving instance DSIGNMAlgorithm d => Show (SigKES (CompactSingleKES d))
deriving instance DSIGNMAlgorithm d => Eq (SigKES (CompactSingleKES d))

instance DSIGNMAlgorithm d => NoThunks (SigKES (CompactSingleKES d))

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

instance
  (DSIGNMAlgorithm d, KnownNat (SizeSigKES (CompactSingleKES d))) =>
  FromCBOR (SigKES (CompactSingleKES d))
  where
  fromCBOR :: forall s. Decoder s (SigKES (CompactSingleKES d))
fromCBOR = forall v s. KESAlgorithm v => Decoder s (SigKES v)
decodeSigKES

slice :: Word -> Word -> ByteString -> ByteString
slice :: Word -> Word -> ByteString -> ByteString
slice Word
offset Word
size =
  Int -> ByteString -> ByteString
BS.take (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
size)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ByteString -> ByteString
BS.drop (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
offset)

--
-- UnsoundPureSignKey instances
--

deriving instance DSIGNAlgorithm d => Show (UnsoundPureSignKeyKES (CompactSingleKES d))
deriving instance
  (DSIGNAlgorithm d, Eq (SignKeyDSIGN d)) => Eq (UnsoundPureSignKeyKES (CompactSingleKES d))

instance
  (UnsoundDSIGNMAlgorithm d, KnownNat (SizeSigDSIGN d + SizeVerKeyDSIGN d)) =>
  ToCBOR (UnsoundPureSignKeyKES (CompactSingleKES d))
  where
  toCBOR :: UnsoundPureSignKeyKES (CompactSingleKES d) -> Encoding
toCBOR = forall v.
UnsoundPureKESAlgorithm v =>
UnsoundPureSignKeyKES v -> Encoding
encodeUnsoundPureSignKeyKES
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (UnsoundPureSignKeyKES (CompactSingleKES d)) -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_size Proxy (UnsoundPureSignKeyKES (CompactSingleKES d))
_skProxy = forall v. KESAlgorithm v => Proxy (SignKeyKES v) -> Size
encodedSignKeyKESSizeExpr (forall {k} (t :: k). Proxy t
Proxy :: Proxy (SignKeyKES (CompactSingleKES d)))

instance
  (UnsoundDSIGNMAlgorithm d, KnownNat (SizeSigDSIGN d + SizeVerKeyDSIGN d)) =>
  FromCBOR (UnsoundPureSignKeyKES (CompactSingleKES d))
  where
  fromCBOR :: forall s. Decoder s (UnsoundPureSignKeyKES (CompactSingleKES d))
fromCBOR = forall v s.
UnsoundPureKESAlgorithm v =>
Decoder s (UnsoundPureSignKeyKES v)
decodeUnsoundPureSignKeyKES

instance DSIGNAlgorithm d => NoThunks (UnsoundPureSignKeyKES (CompactSingleKES d))

--
-- Direct ser/deser
--

instance DirectSerialise (SignKeyDSIGNM d) => DirectSerialise (SignKeyKES (CompactSingleKES d)) where
  directSerialise :: forall (m :: Type -> Type).
(MonadST m, MonadThrow m) =>
(Ptr CChar -> CSize -> m ())
-> SignKeyKES (CompactSingleKES d) -> m ()
directSerialise Ptr CChar -> CSize -> m ()
push (SignKeyCompactSingleKES SignKeyDSIGNM d
sk) = forall a (m :: Type -> Type).
(DirectSerialise a, MonadST m, MonadThrow m) =>
(Ptr CChar -> CSize -> m ()) -> a -> m ()
directSerialise Ptr CChar -> CSize -> m ()
push SignKeyDSIGNM d
sk

instance DirectDeserialise (SignKeyDSIGNM d) => DirectDeserialise (SignKeyKES (CompactSingleKES d)) where
  directDeserialise :: forall (m :: Type -> Type).
(MonadST m, MonadThrow m) =>
(Ptr CChar -> CSize -> m ()) -> m (SignKeyKES (CompactSingleKES d))
directDeserialise Ptr CChar -> CSize -> m ()
pull = forall d. SignKeyDSIGNM d -> SignKeyKES (CompactSingleKES d)
SignKeyCompactSingleKES forall (m :: Type -> Type) a b. Monad m => (a -> b) -> m a -> m b
<$!> forall a (m :: Type -> Type).
(DirectDeserialise a, MonadST m, MonadThrow m) =>
(Ptr CChar -> CSize -> m ()) -> m a
directDeserialise Ptr CChar -> CSize -> m ()
pull

instance DirectSerialise (VerKeyDSIGN d) => DirectSerialise (VerKeyKES (CompactSingleKES d)) where
  directSerialise :: forall (m :: Type -> Type).
(MonadST m, MonadThrow m) =>
(Ptr CChar -> CSize -> m ())
-> VerKeyKES (CompactSingleKES d) -> m ()
directSerialise Ptr CChar -> CSize -> m ()
push (VerKeyCompactSingleKES VerKeyDSIGN d
sk) = forall a (m :: Type -> Type).
(DirectSerialise a, MonadST m, MonadThrow m) =>
(Ptr CChar -> CSize -> m ()) -> a -> m ()
directSerialise Ptr CChar -> CSize -> m ()
push VerKeyDSIGN d
sk

instance DirectDeserialise (VerKeyDSIGN d) => DirectDeserialise (VerKeyKES (CompactSingleKES d)) where
  directDeserialise :: forall (m :: Type -> Type).
(MonadST m, MonadThrow m) =>
(Ptr CChar -> CSize -> m ()) -> m (VerKeyKES (CompactSingleKES d))
directDeserialise Ptr CChar -> CSize -> m ()
pull = forall d. VerKeyDSIGN d -> VerKeyKES (CompactSingleKES d)
VerKeyCompactSingleKES forall (m :: Type -> Type) a b. Monad m => (a -> b) -> m a -> m b
<$!> forall a (m :: Type -> Type).
(DirectDeserialise a, MonadST m, MonadThrow m) =>
(Ptr CChar -> CSize -> m ()) -> m a
directDeserialise Ptr CChar -> CSize -> m ()
pull