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

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

import Control.DeepSeq (NFData)
import Data.ByteArray as BA (ByteArrayAccess, convert)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks, InspectHeap(..))

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

import Crypto.Error (CryptoFailable (..))
import Crypto.PubKey.Ed25519 as Ed25519

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


data Ed25519DSIGN

instance DSIGNAlgorithm Ed25519DSIGN where
    type SeedSizeDSIGN Ed25519DSIGN = 32
    -- | Ed25519 key size is 32 octets
    -- (per <https://tools.ietf.org/html/rfc8032#section-5.1.6>)
    type SizeVerKeyDSIGN  Ed25519DSIGN = 32
    type SizeSignKeyDSIGN Ed25519DSIGN = 32
    -- | Ed25519 signature size is 64 octets
    type SizeSigDSIGN     Ed25519DSIGN = 64

    --
    -- Key and signature types
    --

    newtype VerKeyDSIGN Ed25519DSIGN = VerKeyEd25519DSIGN PublicKey
        deriving (Int -> VerKeyDSIGN Ed25519DSIGN -> ShowS
[VerKeyDSIGN Ed25519DSIGN] -> ShowS
VerKeyDSIGN Ed25519DSIGN -> String
(Int -> VerKeyDSIGN Ed25519DSIGN -> ShowS)
-> (VerKeyDSIGN Ed25519DSIGN -> String)
-> ([VerKeyDSIGN Ed25519DSIGN] -> ShowS)
-> Show (VerKeyDSIGN Ed25519DSIGN)
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
(VerKeyDSIGN Ed25519DSIGN -> VerKeyDSIGN Ed25519DSIGN -> Bool)
-> (VerKeyDSIGN Ed25519DSIGN -> VerKeyDSIGN Ed25519DSIGN -> Bool)
-> Eq (VerKeyDSIGN Ed25519DSIGN)
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.
 VerKeyDSIGN Ed25519DSIGN -> Rep (VerKeyDSIGN Ed25519DSIGN) x)
-> (forall x.
    Rep (VerKeyDSIGN Ed25519DSIGN) x -> VerKeyDSIGN Ed25519DSIGN)
-> Generic (VerKeyDSIGN Ed25519DSIGN)
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, VerKeyDSIGN Ed25519DSIGN -> Int
VerKeyDSIGN Ed25519DSIGN -> Ptr p -> IO ()
VerKeyDSIGN Ed25519DSIGN -> (Ptr p -> IO a) -> IO a
(VerKeyDSIGN Ed25519DSIGN -> Int)
-> (forall p a.
    VerKeyDSIGN Ed25519DSIGN -> (Ptr p -> IO a) -> IO a)
-> (forall p. VerKeyDSIGN Ed25519DSIGN -> Ptr p -> IO ())
-> ByteArrayAccess (VerKeyDSIGN Ed25519DSIGN)
forall p. VerKeyDSIGN Ed25519DSIGN -> Ptr p -> IO ()
forall ba.
(ba -> Int)
-> (forall p a. ba -> (Ptr p -> IO a) -> IO a)
-> (forall p. ba -> Ptr p -> IO ())
-> ByteArrayAccess ba
forall p a. VerKeyDSIGN Ed25519DSIGN -> (Ptr p -> IO a) -> IO a
copyByteArrayToPtr :: VerKeyDSIGN Ed25519DSIGN -> Ptr p -> IO ()
$ccopyByteArrayToPtr :: forall p. VerKeyDSIGN Ed25519DSIGN -> Ptr p -> IO ()
withByteArray :: VerKeyDSIGN Ed25519DSIGN -> (Ptr p -> IO a) -> IO a
$cwithByteArray :: forall p a. VerKeyDSIGN Ed25519DSIGN -> (Ptr p -> IO a) -> IO a
length :: VerKeyDSIGN Ed25519DSIGN -> Int
$clength :: VerKeyDSIGN Ed25519DSIGN -> Int
ByteArrayAccess)
        deriving newtype VerKeyDSIGN Ed25519DSIGN -> ()
(VerKeyDSIGN Ed25519DSIGN -> ())
-> NFData (VerKeyDSIGN Ed25519DSIGN)
forall a. (a -> ()) -> NFData a
rnf :: VerKeyDSIGN Ed25519DSIGN -> ()
$crnf :: VerKeyDSIGN Ed25519DSIGN -> ()
NFData
        deriving Context -> VerKeyDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo)
Proxy (VerKeyDSIGN Ed25519DSIGN) -> String
(Context -> VerKeyDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo))
-> (Context -> VerKeyDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo))
-> (Proxy (VerKeyDSIGN Ed25519DSIGN) -> String)
-> NoThunks (VerKeyDSIGN Ed25519DSIGN)
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy (VerKeyDSIGN Ed25519DSIGN) -> String
$cshowTypeOf :: Proxy (VerKeyDSIGN Ed25519DSIGN) -> String
wNoThunks :: Context -> VerKeyDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> VerKeyDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo)
noThunks :: Context -> VerKeyDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> VerKeyDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo)
NoThunks via InspectHeap PublicKey

    newtype SignKeyDSIGN Ed25519DSIGN = SignKeyEd25519DSIGN SecretKey
        deriving (Int -> SignKeyDSIGN Ed25519DSIGN -> ShowS
[SignKeyDSIGN Ed25519DSIGN] -> ShowS
SignKeyDSIGN Ed25519DSIGN -> String
(Int -> SignKeyDSIGN Ed25519DSIGN -> ShowS)
-> (SignKeyDSIGN Ed25519DSIGN -> String)
-> ([SignKeyDSIGN Ed25519DSIGN] -> ShowS)
-> Show (SignKeyDSIGN Ed25519DSIGN)
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
(SignKeyDSIGN Ed25519DSIGN -> SignKeyDSIGN Ed25519DSIGN -> Bool)
-> (SignKeyDSIGN Ed25519DSIGN -> SignKeyDSIGN Ed25519DSIGN -> Bool)
-> Eq (SignKeyDSIGN Ed25519DSIGN)
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.
 SignKeyDSIGN Ed25519DSIGN -> Rep (SignKeyDSIGN Ed25519DSIGN) x)
-> (forall x.
    Rep (SignKeyDSIGN Ed25519DSIGN) x -> SignKeyDSIGN Ed25519DSIGN)
-> Generic (SignKeyDSIGN Ed25519DSIGN)
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, SignKeyDSIGN Ed25519DSIGN -> Int
SignKeyDSIGN Ed25519DSIGN -> Ptr p -> IO ()
SignKeyDSIGN Ed25519DSIGN -> (Ptr p -> IO a) -> IO a
(SignKeyDSIGN Ed25519DSIGN -> Int)
-> (forall p a.
    SignKeyDSIGN Ed25519DSIGN -> (Ptr p -> IO a) -> IO a)
-> (forall p. SignKeyDSIGN Ed25519DSIGN -> Ptr p -> IO ())
-> ByteArrayAccess (SignKeyDSIGN Ed25519DSIGN)
forall p. SignKeyDSIGN Ed25519DSIGN -> Ptr p -> IO ()
forall ba.
(ba -> Int)
-> (forall p a. ba -> (Ptr p -> IO a) -> IO a)
-> (forall p. ba -> Ptr p -> IO ())
-> ByteArrayAccess ba
forall p a. SignKeyDSIGN Ed25519DSIGN -> (Ptr p -> IO a) -> IO a
copyByteArrayToPtr :: SignKeyDSIGN Ed25519DSIGN -> Ptr p -> IO ()
$ccopyByteArrayToPtr :: forall p. SignKeyDSIGN Ed25519DSIGN -> Ptr p -> IO ()
withByteArray :: SignKeyDSIGN Ed25519DSIGN -> (Ptr p -> IO a) -> IO a
$cwithByteArray :: forall p a. SignKeyDSIGN Ed25519DSIGN -> (Ptr p -> IO a) -> IO a
length :: SignKeyDSIGN Ed25519DSIGN -> Int
$clength :: SignKeyDSIGN Ed25519DSIGN -> Int
ByteArrayAccess)
        deriving newtype SignKeyDSIGN Ed25519DSIGN -> ()
(SignKeyDSIGN Ed25519DSIGN -> ())
-> NFData (SignKeyDSIGN Ed25519DSIGN)
forall a. (a -> ()) -> NFData a
rnf :: SignKeyDSIGN Ed25519DSIGN -> ()
$crnf :: SignKeyDSIGN Ed25519DSIGN -> ()
NFData
        deriving Context -> SignKeyDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo)
Proxy (SignKeyDSIGN Ed25519DSIGN) -> String
(Context -> SignKeyDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo))
-> (Context -> SignKeyDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo))
-> (Proxy (SignKeyDSIGN Ed25519DSIGN) -> String)
-> NoThunks (SignKeyDSIGN Ed25519DSIGN)
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy (SignKeyDSIGN Ed25519DSIGN) -> String
$cshowTypeOf :: Proxy (SignKeyDSIGN Ed25519DSIGN) -> String
wNoThunks :: Context -> SignKeyDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> SignKeyDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo)
noThunks :: Context -> SignKeyDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> SignKeyDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo)
NoThunks via InspectHeap SecretKey

    newtype SigDSIGN Ed25519DSIGN = SigEd25519DSIGN Signature
        deriving (Int -> SigDSIGN Ed25519DSIGN -> ShowS
[SigDSIGN Ed25519DSIGN] -> ShowS
SigDSIGN Ed25519DSIGN -> String
(Int -> SigDSIGN Ed25519DSIGN -> ShowS)
-> (SigDSIGN Ed25519DSIGN -> String)
-> ([SigDSIGN Ed25519DSIGN] -> ShowS)
-> Show (SigDSIGN Ed25519DSIGN)
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
(SigDSIGN Ed25519DSIGN -> SigDSIGN Ed25519DSIGN -> Bool)
-> (SigDSIGN Ed25519DSIGN -> SigDSIGN Ed25519DSIGN -> Bool)
-> Eq (SigDSIGN Ed25519DSIGN)
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. SigDSIGN Ed25519DSIGN -> Rep (SigDSIGN Ed25519DSIGN) x)
-> (forall x.
    Rep (SigDSIGN Ed25519DSIGN) x -> SigDSIGN Ed25519DSIGN)
-> Generic (SigDSIGN Ed25519DSIGN)
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, SigDSIGN Ed25519DSIGN -> Int
SigDSIGN Ed25519DSIGN -> Ptr p -> IO ()
SigDSIGN Ed25519DSIGN -> (Ptr p -> IO a) -> IO a
(SigDSIGN Ed25519DSIGN -> Int)
-> (forall p a. SigDSIGN Ed25519DSIGN -> (Ptr p -> IO a) -> IO a)
-> (forall p. SigDSIGN Ed25519DSIGN -> Ptr p -> IO ())
-> ByteArrayAccess (SigDSIGN Ed25519DSIGN)
forall p. SigDSIGN Ed25519DSIGN -> Ptr p -> IO ()
forall ba.
(ba -> Int)
-> (forall p a. ba -> (Ptr p -> IO a) -> IO a)
-> (forall p. ba -> Ptr p -> IO ())
-> ByteArrayAccess ba
forall p a. SigDSIGN Ed25519DSIGN -> (Ptr p -> IO a) -> IO a
copyByteArrayToPtr :: SigDSIGN Ed25519DSIGN -> Ptr p -> IO ()
$ccopyByteArrayToPtr :: forall p. SigDSIGN Ed25519DSIGN -> Ptr p -> IO ()
withByteArray :: SigDSIGN Ed25519DSIGN -> (Ptr p -> IO a) -> IO a
$cwithByteArray :: forall p a. SigDSIGN Ed25519DSIGN -> (Ptr p -> IO a) -> IO a
length :: SigDSIGN Ed25519DSIGN -> Int
$clength :: SigDSIGN Ed25519DSIGN -> Int
ByteArrayAccess)
        deriving Context -> SigDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo)
Proxy (SigDSIGN Ed25519DSIGN) -> String
(Context -> SigDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo))
-> (Context -> SigDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo))
-> (Proxy (SigDSIGN Ed25519DSIGN) -> String)
-> NoThunks (SigDSIGN Ed25519DSIGN)
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy (SigDSIGN Ed25519DSIGN) -> String
$cshowTypeOf :: Proxy (SigDSIGN Ed25519DSIGN) -> String
wNoThunks :: Context -> SigDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> SigDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo)
noThunks :: Context -> SigDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> SigDSIGN Ed25519DSIGN -> IO (Maybe ThunkInfo)
NoThunks via InspectHeap Signature
        deriving newtype SigDSIGN Ed25519DSIGN -> ()
(SigDSIGN Ed25519DSIGN -> ()) -> NFData (SigDSIGN Ed25519DSIGN)
forall a. (a -> ()) -> NFData a
rnf :: SigDSIGN Ed25519DSIGN -> ()
$crnf :: SigDSIGN Ed25519DSIGN -> ()
NFData

    --
    -- Metadata and basic key operations
    --

    algorithmNameDSIGN :: proxy Ed25519DSIGN -> String
algorithmNameDSIGN proxy Ed25519DSIGN
_ = String
"ed25519"

    deriveVerKeyDSIGN :: SignKeyDSIGN Ed25519DSIGN -> VerKeyDSIGN Ed25519DSIGN
deriveVerKeyDSIGN (SignKeyEd25519DSIGN sk) = PublicKey -> VerKeyDSIGN Ed25519DSIGN
VerKeyEd25519DSIGN (PublicKey -> VerKeyDSIGN Ed25519DSIGN)
-> PublicKey -> VerKeyDSIGN Ed25519DSIGN
forall a b. (a -> b) -> a -> b
$ SecretKey -> PublicKey
toPublic SecretKey
sk


    --
    -- Core algorithm operations
    --

    type Signable Ed25519DSIGN = SignableRepresentation

    signDSIGN :: ContextDSIGN Ed25519DSIGN
-> a -> SignKeyDSIGN Ed25519DSIGN -> SigDSIGN Ed25519DSIGN
signDSIGN () a
a (SignKeyEd25519DSIGN sk) =
        let vk :: PublicKey
vk = SecretKey -> PublicKey
toPublic SecretKey
sk
            bs :: ByteString
bs = a -> ByteString
forall a. SignableRepresentation a => a -> ByteString
getSignableRepresentation a
a
         in Signature -> SigDSIGN Ed25519DSIGN
SigEd25519DSIGN (Signature -> SigDSIGN Ed25519DSIGN)
-> Signature -> SigDSIGN Ed25519DSIGN
forall a b. (a -> b) -> a -> b
$ SecretKey -> PublicKey -> ByteString -> Signature
forall ba.
ByteArrayAccess ba =>
SecretKey -> PublicKey -> ba -> Signature
sign SecretKey
sk PublicKey
vk ByteString
bs

    verifyDSIGN :: ContextDSIGN Ed25519DSIGN
-> VerKeyDSIGN Ed25519DSIGN
-> a
-> SigDSIGN Ed25519DSIGN
-> Either String ()
verifyDSIGN () (VerKeyEd25519DSIGN vk) a
a (SigEd25519DSIGN sig) =
        if PublicKey -> ByteString -> Signature -> Bool
forall ba.
ByteArrayAccess ba =>
PublicKey -> ba -> Signature -> Bool
verify PublicKey
vk (a -> ByteString
forall a. SignableRepresentation a => a -> ByteString
getSignableRepresentation a
a) Signature
sig
          then () -> Either String ()
forall a b. b -> Either a b
Right ()
          else String -> Either String ()
forall a b. a -> Either a b
Left String
"Verification failed"

    --
    -- Key generation
    --

    genKeyDSIGN :: Seed -> SignKeyDSIGN Ed25519DSIGN
genKeyDSIGN Seed
seed =
        let sk :: SecretKey
sk = Seed
-> (forall (m :: * -> *). MonadRandom m => m SecretKey)
-> SecretKey
forall a. Seed -> (forall (m :: * -> *). MonadRandom m => m a) -> a
runMonadRandomWithSeed Seed
seed forall (m :: * -> *). MonadRandom m => m SecretKey
Ed25519.generateSecretKey
         in SecretKey -> SignKeyDSIGN Ed25519DSIGN
SignKeyEd25519DSIGN SecretKey
sk

    --
    -- raw serialise/deserialise
    --

    rawSerialiseVerKeyDSIGN :: VerKeyDSIGN Ed25519DSIGN -> ByteString
rawSerialiseVerKeyDSIGN   = VerKeyDSIGN Ed25519DSIGN -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert
    rawSerialiseSignKeyDSIGN :: SignKeyDSIGN Ed25519DSIGN -> ByteString
rawSerialiseSignKeyDSIGN  = SignKeyDSIGN Ed25519DSIGN -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert
    rawSerialiseSigDSIGN :: SigDSIGN Ed25519DSIGN -> ByteString
rawSerialiseSigDSIGN      = SigDSIGN Ed25519DSIGN -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert

    rawDeserialiseVerKeyDSIGN :: ByteString -> Maybe (VerKeyDSIGN Ed25519DSIGN)
rawDeserialiseVerKeyDSIGN  = (PublicKey -> VerKeyDSIGN Ed25519DSIGN)
-> Maybe PublicKey -> Maybe (VerKeyDSIGN Ed25519DSIGN)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PublicKey -> VerKeyDSIGN Ed25519DSIGN
VerKeyEd25519DSIGN
                               (Maybe PublicKey -> Maybe (VerKeyDSIGN Ed25519DSIGN))
-> (ByteString -> Maybe PublicKey)
-> ByteString
-> Maybe (VerKeyDSIGN Ed25519DSIGN)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CryptoFailable PublicKey -> Maybe PublicKey
forall a. CryptoFailable a -> Maybe a
cryptoFailableToMaybe (CryptoFailable PublicKey -> Maybe PublicKey)
-> (ByteString -> CryptoFailable PublicKey)
-> ByteString
-> Maybe PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> CryptoFailable PublicKey
forall ba. ByteArrayAccess ba => ba -> CryptoFailable PublicKey
Ed25519.publicKey
    rawDeserialiseSignKeyDSIGN :: ByteString -> Maybe (SignKeyDSIGN Ed25519DSIGN)
rawDeserialiseSignKeyDSIGN = (SecretKey -> SignKeyDSIGN Ed25519DSIGN)
-> Maybe SecretKey -> Maybe (SignKeyDSIGN Ed25519DSIGN)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SecretKey -> SignKeyDSIGN Ed25519DSIGN
SignKeyEd25519DSIGN
                               (Maybe SecretKey -> Maybe (SignKeyDSIGN Ed25519DSIGN))
-> (ByteString -> Maybe SecretKey)
-> ByteString
-> Maybe (SignKeyDSIGN Ed25519DSIGN)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CryptoFailable SecretKey -> Maybe SecretKey
forall a. CryptoFailable a -> Maybe a
cryptoFailableToMaybe (CryptoFailable SecretKey -> Maybe SecretKey)
-> (ByteString -> CryptoFailable SecretKey)
-> ByteString
-> Maybe SecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> CryptoFailable SecretKey
forall ba. ByteArrayAccess ba => ba -> CryptoFailable SecretKey
Ed25519.secretKey
    rawDeserialiseSigDSIGN :: ByteString -> Maybe (SigDSIGN Ed25519DSIGN)
rawDeserialiseSigDSIGN     = (Signature -> SigDSIGN Ed25519DSIGN)
-> Maybe Signature -> Maybe (SigDSIGN Ed25519DSIGN)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Signature -> SigDSIGN Ed25519DSIGN
SigEd25519DSIGN
                               (Maybe Signature -> Maybe (SigDSIGN Ed25519DSIGN))
-> (ByteString -> Maybe Signature)
-> ByteString
-> Maybe (SigDSIGN Ed25519DSIGN)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CryptoFailable Signature -> Maybe Signature
forall a. CryptoFailable a -> Maybe a
cryptoFailableToMaybe (CryptoFailable Signature -> Maybe Signature)
-> (ByteString -> CryptoFailable Signature)
-> ByteString
-> Maybe Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> CryptoFailable Signature
forall ba. ByteArrayAccess ba => ba -> CryptoFailable Signature
Ed25519.signature


instance ToCBOR (VerKeyDSIGN Ed25519DSIGN) where
  toCBOR :: VerKeyDSIGN Ed25519DSIGN -> Encoding
toCBOR = VerKeyDSIGN Ed25519DSIGN -> Encoding
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
_ = Proxy (VerKeyDSIGN Ed25519DSIGN) -> Size
forall v. DSIGNAlgorithm v => Proxy (VerKeyDSIGN v) -> Size
encodedVerKeyDSIGNSizeExpr

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

instance ToCBOR (SignKeyDSIGN Ed25519DSIGN) where
  toCBOR :: SignKeyDSIGN Ed25519DSIGN -> Encoding
toCBOR = SignKeyDSIGN Ed25519DSIGN -> Encoding
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
_ = Proxy (SignKeyDSIGN Ed25519DSIGN) -> Size
forall v. DSIGNAlgorithm v => Proxy (SignKeyDSIGN v) -> Size
encodedSignKeyDESIGNSizeExpr

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

instance ToCBOR (SigDSIGN Ed25519DSIGN) where
  toCBOR :: SigDSIGN Ed25519DSIGN -> Encoding
toCBOR = SigDSIGN Ed25519DSIGN -> Encoding
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
_ = Proxy (SigDSIGN Ed25519DSIGN) -> Size
forall v. DSIGNAlgorithm v => Proxy (SigDSIGN v) -> Size
encodedSigDSIGNSizeExpr

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


cryptoFailableToMaybe :: CryptoFailable a -> Maybe a
cryptoFailableToMaybe :: CryptoFailable a -> Maybe a
cryptoFailableToMaybe (CryptoPassed a
a) = a -> Maybe a
forall a. a -> Maybe a
Just a
a
cryptoFailableToMaybe (CryptoFailed CryptoError
_) = Maybe a
forall a. Maybe a
Nothing