{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeApplications #-}
-- According to the documentation for unsafePerformIO:
--
-- > Make sure that the either you switch off let-floating
-- > (-fno-full-laziness), or that the call to unsafePerformIO cannot float
-- > outside a lambda.
--
-- If we do not switch off let-floating, our calls to unsafeDupablePerformIO for
-- FFI functions become nondeterministic in their behaviour when run with
-- parallelism enabled (such as -with-rtsopts=-N), possibly yielding wrong
-- answers on a range of tasks, including serialization.
{-# OPTIONS_GHC -fno-full-laziness #-}

-- | Ed25519 digital signatures.
module Cardano.Crypto.DSIGN.Ed25519
  ( Ed25519DSIGN
  , SigDSIGN (..)
  , SignKeyDSIGN (..)
  , VerKeyDSIGN (..)
  )
where

import Control.DeepSeq (NFData)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks)
import System.IO.Unsafe (unsafeDupablePerformIO)
import GHC.IO.Exception (ioException)
import Control.Monad (unless)
import Foreign.C.Error (errnoToIOError, getErrno)
import Foreign.Ptr (castPtr, nullPtr)
import qualified Data.ByteString as BS

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

import Cardano.Foreign
import Cardano.Crypto.PinnedSizedBytes
import Cardano.Crypto.Libsodium.C

import Cardano.Crypto.DSIGN.Class
import Cardano.Crypto.Seed
import Cardano.Crypto.Util (SignableRepresentation(..))
import Data.Proxy


data Ed25519DSIGN

instance NoThunks (VerKeyDSIGN Ed25519DSIGN)
instance NoThunks (SignKeyDSIGN Ed25519DSIGN)
instance NoThunks (SigDSIGN Ed25519DSIGN)

-- | Convert C-style return code / errno error reporting into Haskell
-- exceptions.
--
-- Runs an IO action (which should be some FFI call into C) that returns a
-- result code; if the result code returned is nonzero, fetch the errno, and
-- throw a suitable IO exception.
cOrError :: String -> String -> IO Int -> IO ()
cOrError :: String -> String -> IO Int -> IO ()
cOrError String
contextDesc String
cFunName IO Int
action = do
  Int
res <- IO Int
action
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int
res forall a. Eq a => a -> a -> Bool
== Int
0) forall a b. (a -> b) -> a -> b
$ do
      Errno
errno <- IO Errno
getErrno
      forall a. IOException -> IO a
ioException forall a b. (a -> b) -> a -> b
$ String -> Errno -> Maybe Handle -> Maybe String -> IOException
errnoToIOError (String
contextDesc forall a. [a] -> [a] -> [a]
++ String
": " forall a. [a] -> [a] -> [a]
++ String
cFunName) Errno
errno forall a. Maybe a
Nothing forall a. Maybe a
Nothing

instance DSIGNAlgorithm Ed25519DSIGN where
    -- | Seed size is 32 octets, the same as sign key size, because generating
    -- a sign key is literally just taking a chunk from the seed. We use
    -- SEEDBYTES to define both the seed size and the sign key size.
    type SeedSizeDSIGN Ed25519DSIGN = CRYPTO_SIGN_ED25519_SEEDBYTES
    -- | Ed25519 key size is 32 octets
    -- (per <https://tools.ietf.org/html/rfc8032#section-5.1.6>)
    type SizeVerKeyDSIGN  Ed25519DSIGN = CRYPTO_SIGN_ED25519_PUBLICKEYBYTES
    -- | Ed25519 secret key size is 32 octets; however, libsodium packs both
    -- the secret key and the public key into a 64-octet compound and exposes
    -- that as the secret key; the actual 32-octet secret key is called
    -- \"seed\" in libsodium. For backwards compatibility reasons and
    -- efficiency, we use the 64-octet compounds internally (this is what
    -- libsodium expects), but we only serialize the 32-octet secret key part
    -- (the libsodium \"seed\"). And because of this, we need to define the
    -- sign key size to be SEEDBYTES (which is 32), not PRIVATEKEYBYTES (which
    -- would be 64).
    type SizeSignKeyDSIGN Ed25519DSIGN = CRYPTO_SIGN_ED25519_SEEDBYTES
    -- | Ed25519 signature size is 64 octets
    type SizeSigDSIGN     Ed25519DSIGN = CRYPTO_SIGN_ED25519_BYTES

    --
    -- Key and signature types
    --

    newtype VerKeyDSIGN Ed25519DSIGN = VerKeyEd25519DSIGN (PinnedSizedBytes (SizeVerKeyDSIGN Ed25519DSIGN))
        deriving (Int -> VerKeyDSIGN Ed25519DSIGN -> ShowS
[VerKeyDSIGN Ed25519DSIGN] -> ShowS
VerKeyDSIGN Ed25519DSIGN -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [VerKeyDSIGN Ed25519DSIGN] -> ShowS
$cshowList :: [VerKeyDSIGN Ed25519DSIGN] -> ShowS
show :: VerKeyDSIGN Ed25519DSIGN -> String
$cshow :: VerKeyDSIGN Ed25519DSIGN -> String
showsPrec :: Int -> VerKeyDSIGN Ed25519DSIGN -> ShowS
$cshowsPrec :: Int -> VerKeyDSIGN Ed25519DSIGN -> ShowS
Show, VerKeyDSIGN Ed25519DSIGN -> VerKeyDSIGN Ed25519DSIGN -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: VerKeyDSIGN Ed25519DSIGN -> VerKeyDSIGN Ed25519DSIGN -> Bool
$c/= :: VerKeyDSIGN Ed25519DSIGN -> VerKeyDSIGN Ed25519DSIGN -> Bool
== :: VerKeyDSIGN Ed25519DSIGN -> VerKeyDSIGN Ed25519DSIGN -> Bool
$c== :: VerKeyDSIGN Ed25519DSIGN -> VerKeyDSIGN Ed25519DSIGN -> Bool
Eq, forall x.
Rep (VerKeyDSIGN Ed25519DSIGN) x -> VerKeyDSIGN Ed25519DSIGN
forall x.
VerKeyDSIGN Ed25519DSIGN -> Rep (VerKeyDSIGN Ed25519DSIGN) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep (VerKeyDSIGN Ed25519DSIGN) x -> VerKeyDSIGN Ed25519DSIGN
$cfrom :: forall x.
VerKeyDSIGN Ed25519DSIGN -> Rep (VerKeyDSIGN Ed25519DSIGN) x
Generic)
        deriving newtype VerKeyDSIGN Ed25519DSIGN -> ()
forall a. (a -> ()) -> NFData a
rnf :: VerKeyDSIGN Ed25519DSIGN -> ()
$crnf :: VerKeyDSIGN Ed25519DSIGN -> ()
NFData

    -- Note that the size of the internal key data structure is the SECRET KEY
    -- bytes as per libsodium, while the declared key size (for serialization)
    -- is libsodium's SEED bytes. We expand 32-octet keys to 64-octet ones
    -- during deserialization, and we delete the 32 octets that contain the
    -- public key from the secret key before serializing.
    newtype SignKeyDSIGN Ed25519DSIGN = SignKeyEd25519DSIGN (PinnedSizedBytes CRYPTO_SIGN_ED25519_SECRETKEYBYTES)
        deriving (Int -> SignKeyDSIGN Ed25519DSIGN -> ShowS
[SignKeyDSIGN Ed25519DSIGN] -> ShowS
SignKeyDSIGN Ed25519DSIGN -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SignKeyDSIGN Ed25519DSIGN] -> ShowS
$cshowList :: [SignKeyDSIGN Ed25519DSIGN] -> ShowS
show :: SignKeyDSIGN Ed25519DSIGN -> String
$cshow :: SignKeyDSIGN Ed25519DSIGN -> String
showsPrec :: Int -> SignKeyDSIGN Ed25519DSIGN -> ShowS
$cshowsPrec :: Int -> SignKeyDSIGN Ed25519DSIGN -> ShowS
Show, SignKeyDSIGN Ed25519DSIGN -> SignKeyDSIGN Ed25519DSIGN -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SignKeyDSIGN Ed25519DSIGN -> SignKeyDSIGN Ed25519DSIGN -> Bool
$c/= :: SignKeyDSIGN Ed25519DSIGN -> SignKeyDSIGN Ed25519DSIGN -> Bool
== :: SignKeyDSIGN Ed25519DSIGN -> SignKeyDSIGN Ed25519DSIGN -> Bool
$c== :: SignKeyDSIGN Ed25519DSIGN -> SignKeyDSIGN Ed25519DSIGN -> Bool
Eq, forall x.
Rep (SignKeyDSIGN Ed25519DSIGN) x -> SignKeyDSIGN Ed25519DSIGN
forall x.
SignKeyDSIGN Ed25519DSIGN -> Rep (SignKeyDSIGN Ed25519DSIGN) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep (SignKeyDSIGN Ed25519DSIGN) x -> SignKeyDSIGN Ed25519DSIGN
$cfrom :: forall x.
SignKeyDSIGN Ed25519DSIGN -> Rep (SignKeyDSIGN Ed25519DSIGN) x
Generic)
        deriving newtype SignKeyDSIGN Ed25519DSIGN -> ()
forall a. (a -> ()) -> NFData a
rnf :: SignKeyDSIGN Ed25519DSIGN -> ()
$crnf :: SignKeyDSIGN Ed25519DSIGN -> ()
NFData

    newtype SigDSIGN Ed25519DSIGN = SigEd25519DSIGN (PinnedSizedBytes (SizeSigDSIGN Ed25519DSIGN))
        deriving (Int -> SigDSIGN Ed25519DSIGN -> ShowS
[SigDSIGN Ed25519DSIGN] -> ShowS
SigDSIGN Ed25519DSIGN -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SigDSIGN Ed25519DSIGN] -> ShowS
$cshowList :: [SigDSIGN Ed25519DSIGN] -> ShowS
show :: SigDSIGN Ed25519DSIGN -> String
$cshow :: SigDSIGN Ed25519DSIGN -> String
showsPrec :: Int -> SigDSIGN Ed25519DSIGN -> ShowS
$cshowsPrec :: Int -> SigDSIGN Ed25519DSIGN -> ShowS
Show, SigDSIGN Ed25519DSIGN -> SigDSIGN Ed25519DSIGN -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SigDSIGN Ed25519DSIGN -> SigDSIGN Ed25519DSIGN -> Bool
$c/= :: SigDSIGN Ed25519DSIGN -> SigDSIGN Ed25519DSIGN -> Bool
== :: SigDSIGN Ed25519DSIGN -> SigDSIGN Ed25519DSIGN -> Bool
$c== :: SigDSIGN Ed25519DSIGN -> SigDSIGN Ed25519DSIGN -> Bool
Eq, forall x. Rep (SigDSIGN Ed25519DSIGN) x -> SigDSIGN Ed25519DSIGN
forall x. SigDSIGN Ed25519DSIGN -> Rep (SigDSIGN Ed25519DSIGN) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep (SigDSIGN Ed25519DSIGN) x -> SigDSIGN Ed25519DSIGN
$cfrom :: forall x. SigDSIGN Ed25519DSIGN -> Rep (SigDSIGN Ed25519DSIGN) x
Generic)
        deriving newtype SigDSIGN Ed25519DSIGN -> ()
forall a. (a -> ()) -> NFData a
rnf :: SigDSIGN Ed25519DSIGN -> ()
$crnf :: SigDSIGN Ed25519DSIGN -> ()
NFData

    --
    -- Metadata and basic key operations
    --

    algorithmNameDSIGN :: forall (proxy :: * -> *). proxy Ed25519DSIGN -> String
algorithmNameDSIGN proxy Ed25519DSIGN
_ = String
"ed25519"

    deriveVerKeyDSIGN :: SignKeyDSIGN Ed25519DSIGN -> VerKeyDSIGN Ed25519DSIGN
deriveVerKeyDSIGN (SignKeyEd25519DSIGN PinnedSizedBytes CRYPTO_SIGN_ED25519_BYTES
sk) =
      PinnedSizedBytes (SizeVerKeyDSIGN Ed25519DSIGN)
-> VerKeyDSIGN Ed25519DSIGN
VerKeyEd25519DSIGN forall a b. (a -> b) -> a -> b
$
        forall a. IO a -> a
unsafeDupablePerformIO forall a b. (a -> b) -> a -> b
$
        forall (n :: Nat) r.
PinnedSizedBytes n -> (SizedPtr n -> IO r) -> IO r
psbUseAsSizedPtr PinnedSizedBytes CRYPTO_SIGN_ED25519_BYTES
sk forall a b. (a -> b) -> a -> b
$ \SizedPtr CRYPTO_SIGN_ED25519_BYTES
skPtr ->
        forall (n :: Nat).
KnownNat n =>
(SizedPtr n -> IO ()) -> IO (PinnedSizedBytes n)
psbCreateSized forall a b. (a -> b) -> a -> b
$ \SizedPtr (SizeVerKeyDSIGN Ed25519DSIGN)
pkPtr ->
          String -> String -> IO Int -> IO ()
cOrError String
"deriveVerKeyDSIGN @Ed25519DSIGN" String
"c_crypto_sign_ed25519_sk_to_pk"
            forall a b. (a -> b) -> a -> b
$ SizedPtr CRYPTO_SIGN_ED25519_PUBLICKEYBYTES
-> SizedPtr CRYPTO_SIGN_ED25519_BYTES -> IO Int
c_crypto_sign_ed25519_sk_to_pk SizedPtr (SizeVerKeyDSIGN Ed25519DSIGN)
pkPtr SizedPtr CRYPTO_SIGN_ED25519_BYTES
skPtr

    --
    -- Core algorithm operations
    --

    type Signable Ed25519DSIGN = SignableRepresentation

    signDSIGN :: forall a.
(Signable Ed25519DSIGN a, HasCallStack) =>
ContextDSIGN Ed25519DSIGN
-> a -> SignKeyDSIGN Ed25519DSIGN -> SigDSIGN Ed25519DSIGN
signDSIGN () a
a (SignKeyEd25519DSIGN PinnedSizedBytes CRYPTO_SIGN_ED25519_BYTES
sk) =
      let bs :: ByteString
bs = forall a. SignableRepresentation a => a -> ByteString
getSignableRepresentation a
a
      in PinnedSizedBytes (SizeSigDSIGN Ed25519DSIGN)
-> SigDSIGN Ed25519DSIGN
SigEd25519DSIGN forall a b. (a -> b) -> a -> b
$ forall a. IO a -> a
unsafeDupablePerformIO forall a b. (a -> b) -> a -> b
$
            forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.useAsCStringLen ByteString
bs forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
ptr, Int
len) ->
            forall (n :: Nat) r.
PinnedSizedBytes n -> (SizedPtr n -> IO r) -> IO r
psbUseAsSizedPtr PinnedSizedBytes CRYPTO_SIGN_ED25519_BYTES
sk forall a b. (a -> b) -> a -> b
$ \SizedPtr CRYPTO_SIGN_ED25519_BYTES
skPtr ->
            forall (n :: Nat) b. KnownNat n => (SizedPtr n -> IO b) -> IO b
allocaSized forall a b. (a -> b) -> a -> b
$ \SizedPtr CRYPTO_SIGN_ED25519_PUBLICKEYBYTES
pkPtr -> do
                String -> String -> IO Int -> IO ()
cOrError String
"signDSIGN @Ed25519DSIGN" String
"c_crypto_sign_ed25519_sk_to_pk"
                  forall a b. (a -> b) -> a -> b
$ SizedPtr CRYPTO_SIGN_ED25519_PUBLICKEYBYTES
-> SizedPtr CRYPTO_SIGN_ED25519_BYTES -> IO Int
c_crypto_sign_ed25519_sk_to_pk SizedPtr CRYPTO_SIGN_ED25519_PUBLICKEYBYTES
pkPtr SizedPtr CRYPTO_SIGN_ED25519_BYTES
skPtr
                forall (n :: Nat).
KnownNat n =>
(SizedPtr n -> IO ()) -> IO (PinnedSizedBytes n)
psbCreateSized forall a b. (a -> b) -> a -> b
$ \SizedPtr CRYPTO_SIGN_ED25519_BYTES
sigPtr -> do
                  String -> String -> IO Int -> IO ()
cOrError String
"signDSIGN @Ed25519DSIGN" String
"c_crypto_sign_ed25519_detached"
                    forall a b. (a -> b) -> a -> b
$ SizedPtr CRYPTO_SIGN_ED25519_BYTES
-> Ptr CULLong
-> Ptr CUChar
-> CULLong
-> SizedPtr CRYPTO_SIGN_ED25519_BYTES
-> IO Int
c_crypto_sign_ed25519_detached SizedPtr CRYPTO_SIGN_ED25519_BYTES
sigPtr forall a. Ptr a
nullPtr (forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
ptr) (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len) SizedPtr CRYPTO_SIGN_ED25519_BYTES
skPtr

    verifyDSIGN :: forall a.
(Signable Ed25519DSIGN a, HasCallStack) =>
ContextDSIGN Ed25519DSIGN
-> VerKeyDSIGN Ed25519DSIGN
-> a
-> SigDSIGN Ed25519DSIGN
-> Either String ()
verifyDSIGN () (VerKeyEd25519DSIGN PinnedSizedBytes (SizeVerKeyDSIGN Ed25519DSIGN)
vk) a
a (SigEd25519DSIGN PinnedSizedBytes (SizeSigDSIGN Ed25519DSIGN)
sig) =
        let bs :: ByteString
bs = forall a. SignableRepresentation a => a -> ByteString
getSignableRepresentation a
a
        in forall a. IO a -> a
unsafeDupablePerformIO forall a b. (a -> b) -> a -> b
$
          forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.useAsCStringLen ByteString
bs forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
ptr, Int
len) ->
          forall (n :: Nat) r.
PinnedSizedBytes n -> (SizedPtr n -> IO r) -> IO r
psbUseAsSizedPtr PinnedSizedBytes (SizeVerKeyDSIGN Ed25519DSIGN)
vk forall a b. (a -> b) -> a -> b
$ \SizedPtr CRYPTO_SIGN_ED25519_PUBLICKEYBYTES
vkPtr ->
          forall (n :: Nat) r.
PinnedSizedBytes n -> (SizedPtr n -> IO r) -> IO r
psbUseAsSizedPtr PinnedSizedBytes (SizeSigDSIGN Ed25519DSIGN)
sig forall a b. (a -> b) -> a -> b
$ \SizedPtr CRYPTO_SIGN_ED25519_BYTES
sigPtr -> do
              Int
res <- SizedPtr CRYPTO_SIGN_ED25519_BYTES
-> Ptr CUChar
-> CULLong
-> SizedPtr CRYPTO_SIGN_ED25519_PUBLICKEYBYTES
-> IO Int
c_crypto_sign_ed25519_verify_detached SizedPtr CRYPTO_SIGN_ED25519_BYTES
sigPtr (forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
ptr) (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len) SizedPtr CRYPTO_SIGN_ED25519_PUBLICKEYBYTES
vkPtr
              if Int
res forall a. Eq a => a -> a -> Bool
== Int
0
              then forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right ())
              else do
                  -- errno <- getErrno
                  forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left  String
"Verification failed")

    --
    -- Key generation
    --
    genKeyDSIGN :: Seed -> SignKeyDSIGN Ed25519DSIGN
genKeyDSIGN Seed
seed = PinnedSizedBytes CRYPTO_SIGN_ED25519_BYTES
-> SignKeyDSIGN Ed25519DSIGN
SignKeyEd25519DSIGN forall a b. (a -> b) -> a -> b
$
      let (ByteString
sb, Seed
_) = Word -> Seed -> (ByteString, Seed)
getBytesFromSeedT (forall v (proxy :: * -> *). DSIGNAlgorithm v => proxy v -> Word
seedSizeDSIGN (forall {k} (t :: k). Proxy t
Proxy @Ed25519DSIGN)) Seed
seed
      in forall a. IO a -> a
unsafeDupablePerformIO forall a b. (a -> b) -> a -> b
$ do
          forall (n :: Nat).
KnownNat n =>
(SizedPtr n -> IO ()) -> IO (PinnedSizedBytes n)
psbCreateSized forall a b. (a -> b) -> a -> b
$ \SizedPtr CRYPTO_SIGN_ED25519_BYTES
skPtr ->
            forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.useAsCStringLen ByteString
sb forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
seedPtr, Int
_) ->
            forall (n :: Nat) b. KnownNat n => (SizedPtr n -> IO b) -> IO b
allocaSized forall a b. (a -> b) -> a -> b
$ \SizedPtr CRYPTO_SIGN_ED25519_PUBLICKEYBYTES
pkPtr -> do
                String -> String -> IO Int -> IO ()
cOrError String
"genKeyDSIGN @Ed25519DSIGN" String
"c_crypto_sign_ed25519_seed_keypair"
                  forall a b. (a -> b) -> a -> b
$ SizedPtr CRYPTO_SIGN_ED25519_PUBLICKEYBYTES
-> SizedPtr CRYPTO_SIGN_ED25519_BYTES
-> SizedPtr CRYPTO_SIGN_ED25519_PUBLICKEYBYTES
-> IO Int
c_crypto_sign_ed25519_seed_keypair SizedPtr CRYPTO_SIGN_ED25519_PUBLICKEYBYTES
pkPtr SizedPtr CRYPTO_SIGN_ED25519_BYTES
skPtr (forall (n :: Nat). Ptr Void -> SizedPtr n
SizedPtr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Ptr a -> Ptr b
castPtr forall a b. (a -> b) -> a -> b
$ Ptr CChar
seedPtr)
    --
    -- raw serialise/deserialise
    --

    rawSerialiseVerKeyDSIGN :: VerKeyDSIGN Ed25519DSIGN -> ByteString
rawSerialiseVerKeyDSIGN   (VerKeyEd25519DSIGN PinnedSizedBytes (SizeVerKeyDSIGN Ed25519DSIGN)
vk) = forall (n :: Nat). PinnedSizedBytes n -> ByteString
psbToByteString PinnedSizedBytes (SizeVerKeyDSIGN Ed25519DSIGN)
vk
    rawSerialiseSignKeyDSIGN :: SignKeyDSIGN Ed25519DSIGN -> ByteString
rawSerialiseSignKeyDSIGN  (SignKeyEd25519DSIGN PinnedSizedBytes CRYPTO_SIGN_ED25519_BYTES
sk) =
        forall (n :: Nat). PinnedSizedBytes n -> ByteString
psbToByteString @(SeedSizeDSIGN Ed25519DSIGN) forall a b. (a -> b) -> a -> b
$ forall a. IO a -> a
unsafeDupablePerformIO forall a b. (a -> b) -> a -> b
$ do
          forall (n :: Nat).
KnownNat n =>
(SizedPtr n -> IO ()) -> IO (PinnedSizedBytes n)
psbCreateSized forall a b. (a -> b) -> a -> b
$ \SizedPtr (SeedSizeDSIGN Ed25519DSIGN)
seedPtr ->
            forall (n :: Nat) r.
PinnedSizedBytes n -> (SizedPtr n -> IO r) -> IO r
psbUseAsSizedPtr PinnedSizedBytes CRYPTO_SIGN_ED25519_BYTES
sk forall a b. (a -> b) -> a -> b
$ \SizedPtr CRYPTO_SIGN_ED25519_BYTES
skPtr ->
              String -> String -> IO Int -> IO ()
cOrError String
"deriveVerKeyDSIGN @Ed25519DSIGN" String
"c_crypto_sign_ed25519_sk_to_seed"
                forall a b. (a -> b) -> a -> b
$ SizedPtr CRYPTO_SIGN_ED25519_PUBLICKEYBYTES
-> SizedPtr CRYPTO_SIGN_ED25519_BYTES -> IO Int
c_crypto_sign_ed25519_sk_to_seed SizedPtr (SeedSizeDSIGN Ed25519DSIGN)
seedPtr SizedPtr CRYPTO_SIGN_ED25519_BYTES
skPtr

    rawSerialiseSigDSIGN :: SigDSIGN Ed25519DSIGN -> ByteString
rawSerialiseSigDSIGN      (SigEd25519DSIGN PinnedSizedBytes (SizeSigDSIGN Ed25519DSIGN)
sig) = forall (n :: Nat). PinnedSizedBytes n -> ByteString
psbToByteString PinnedSizedBytes (SizeSigDSIGN Ed25519DSIGN)
sig

    rawDeserialiseVerKeyDSIGN :: ByteString -> Maybe (VerKeyDSIGN Ed25519DSIGN)
rawDeserialiseVerKeyDSIGN  = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PinnedSizedBytes (SizeVerKeyDSIGN Ed25519DSIGN)
-> VerKeyDSIGN Ed25519DSIGN
VerKeyEd25519DSIGN forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Nat).
KnownNat n =>
ByteString -> Maybe (PinnedSizedBytes n)
psbFromByteStringCheck
    rawDeserialiseSignKeyDSIGN :: ByteString -> Maybe (SignKeyDSIGN Ed25519DSIGN)
rawDeserialiseSignKeyDSIGN = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. DSIGNAlgorithm v => Seed -> SignKeyDSIGN v
genKeyDSIGN forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Seed
mkSeedFromBytes
    rawDeserialiseSigDSIGN :: ByteString -> Maybe (SigDSIGN Ed25519DSIGN)
rawDeserialiseSigDSIGN     = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PinnedSizedBytes (SizeSigDSIGN Ed25519DSIGN)
-> SigDSIGN Ed25519DSIGN
SigEd25519DSIGN forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Nat).
KnownNat n =>
ByteString -> Maybe (PinnedSizedBytes n)
psbFromByteStringCheck


instance ToCBOR (VerKeyDSIGN Ed25519DSIGN) where
  toCBOR :: VerKeyDSIGN Ed25519DSIGN -> Encoding
toCBOR = forall v. DSIGNAlgorithm v => VerKeyDSIGN v -> Encoding
encodeVerKeyDSIGN
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (VerKeyDSIGN Ed25519DSIGN) -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_ = forall v. DSIGNAlgorithm v => Proxy (VerKeyDSIGN v) -> Size
encodedVerKeyDSIGNSizeExpr

instance FromCBOR (VerKeyDSIGN Ed25519DSIGN) where
  fromCBOR :: forall s. Decoder s (VerKeyDSIGN Ed25519DSIGN)
fromCBOR = forall v s. DSIGNAlgorithm v => Decoder s (VerKeyDSIGN v)
decodeVerKeyDSIGN

instance ToCBOR (SignKeyDSIGN Ed25519DSIGN) where
  toCBOR :: SignKeyDSIGN Ed25519DSIGN -> Encoding
toCBOR = forall v. DSIGNAlgorithm v => SignKeyDSIGN v -> Encoding
encodeSignKeyDSIGN
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (SignKeyDSIGN Ed25519DSIGN) -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_ = forall v. DSIGNAlgorithm v => Proxy (SignKeyDSIGN v) -> Size
encodedSignKeyDESIGNSizeExpr

instance FromCBOR (SignKeyDSIGN Ed25519DSIGN) where
  fromCBOR :: forall s. Decoder s (SignKeyDSIGN Ed25519DSIGN)
fromCBOR = forall v s. DSIGNAlgorithm v => Decoder s (SignKeyDSIGN v)
decodeSignKeyDSIGN

instance ToCBOR (SigDSIGN Ed25519DSIGN) where
  toCBOR :: SigDSIGN Ed25519DSIGN -> Encoding
toCBOR = forall v. DSIGNAlgorithm v => SigDSIGN v -> Encoding
encodeSigDSIGN
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (SigDSIGN Ed25519DSIGN) -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_ = forall v. DSIGNAlgorithm v => Proxy (SigDSIGN v) -> Size
encodedSigDSIGNSizeExpr

instance FromCBOR (SigDSIGN Ed25519DSIGN) where
  fromCBOR :: forall s. Decoder s (SigDSIGN Ed25519DSIGN)
fromCBOR = forall v s. DSIGNAlgorithm v => Decoder s (SigDSIGN v)
decodeSigDSIGN