{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module Cardano.Crypto.DSIGN.Class
(
DSIGNAlgorithm (..)
, Seed
, seedSizeDSIGN
, sizeVerKeyDSIGN
, sizeSignKeyDSIGN
, sizeSigDSIGN
, SignedDSIGN (..)
, signedDSIGN
, verifySignedDSIGN
, encodeVerKeyDSIGN
, decodeVerKeyDSIGN
, encodeSignKeyDSIGN
, decodeSignKeyDSIGN
, encodeSigDSIGN
, decodeSigDSIGN
, encodeSignedDSIGN
, decodeSignedDSIGN
, encodedVerKeyDSIGNSizeExpr
, encodedSignKeyDESIGNSizeExpr
, encodedSigDSIGNSizeExpr
)
where
import Control.DeepSeq (NFData)
import qualified Data.ByteString as BS
import Data.ByteString (ByteString)
import Data.Kind (Type)
import Data.Proxy (Proxy(..))
import Data.Typeable (Typeable)
import GHC.Exts (Constraint)
import GHC.Generics (Generic)
import GHC.Stack
import GHC.TypeLits (KnownNat, Nat, natVal, TypeError, ErrorMessage (..))
import NoThunks.Class (NoThunks)
import Cardano.Binary (Decoder, decodeBytes, Encoding, encodeBytes, Size, withWordSize)
import Cardano.Crypto.Util (Empty)
import Cardano.Crypto.Seed
import Cardano.Crypto.Hash.Class (HashAlgorithm, Hash, hashWith)
class ( Typeable v
, Show (VerKeyDSIGN v)
, Eq (VerKeyDSIGN v)
, Show (SignKeyDSIGN v)
, Show (SigDSIGN v)
, Eq (SigDSIGN v)
, NoThunks (SigDSIGN v)
, NoThunks (SignKeyDSIGN v)
, NoThunks (VerKeyDSIGN v)
, KnownNat (SeedSizeDSIGN v)
, KnownNat (SizeVerKeyDSIGN v)
, KnownNat (SizeSignKeyDSIGN v)
, KnownNat (SizeSigDSIGN v)
)
=> DSIGNAlgorithm v where
type SeedSizeDSIGN v :: Nat
type SizeVerKeyDSIGN v :: Nat
type SizeSignKeyDSIGN v :: Nat
type SizeSigDSIGN v :: Nat
data VerKeyDSIGN v :: Type
data SignKeyDSIGN v :: Type
data SigDSIGN v :: Type
algorithmNameDSIGN :: proxy v -> String
deriveVerKeyDSIGN :: SignKeyDSIGN v -> VerKeyDSIGN v
hashVerKeyDSIGN :: HashAlgorithm h => VerKeyDSIGN v -> Hash h (VerKeyDSIGN v)
hashVerKeyDSIGN = (VerKeyDSIGN v -> ByteString)
-> VerKeyDSIGN v -> Hash h (VerKeyDSIGN v)
forall h a. HashAlgorithm h => (a -> ByteString) -> a -> Hash h a
hashWith VerKeyDSIGN v -> ByteString
forall v. DSIGNAlgorithm v => VerKeyDSIGN v -> ByteString
rawSerialiseVerKeyDSIGN
type ContextDSIGN v :: Type
type ContextDSIGN v = ()
type Signable v :: Type -> Constraint
type Signable v = Empty
signDSIGN
:: (Signable v a, HasCallStack)
=> ContextDSIGN v
-> a
-> SignKeyDSIGN v
-> SigDSIGN v
verifyDSIGN
:: (Signable v a, HasCallStack)
=> ContextDSIGN v
-> VerKeyDSIGN v
-> a
-> SigDSIGN v
-> Either String ()
genKeyDSIGN :: Seed -> SignKeyDSIGN v
rawSerialiseVerKeyDSIGN :: VerKeyDSIGN v -> ByteString
rawSerialiseSignKeyDSIGN :: SignKeyDSIGN v -> ByteString
rawSerialiseSigDSIGN :: SigDSIGN v -> ByteString
rawDeserialiseVerKeyDSIGN :: ByteString -> Maybe (VerKeyDSIGN v)
rawDeserialiseSignKeyDSIGN :: ByteString -> Maybe (SignKeyDSIGN v)
rawDeserialiseSigDSIGN :: ByteString -> Maybe (SigDSIGN v)
instance ( TypeError ('Text "Ord not supported for signing keys, use the hash instead")
, Eq (SignKeyDSIGN v)
)
=> Ord (SignKeyDSIGN v) where
compare :: SignKeyDSIGN v -> SignKeyDSIGN v -> Ordering
compare = [Char] -> SignKeyDSIGN v -> SignKeyDSIGN v -> Ordering
forall a. HasCallStack => [Char] -> a
error [Char]
"unsupported"
instance ( TypeError ('Text "Ord not supported for verification keys, use the hash instead")
, Eq (VerKeyDSIGN v)
)
=> Ord (VerKeyDSIGN v) where
compare :: VerKeyDSIGN v -> VerKeyDSIGN v -> Ordering
compare = [Char] -> VerKeyDSIGN v -> VerKeyDSIGN v -> Ordering
forall a. HasCallStack => [Char] -> a
error [Char]
"unsupported"
seedSizeDSIGN :: forall v proxy. DSIGNAlgorithm v => proxy v -> Word
seedSizeDSIGN :: proxy v -> Word
seedSizeDSIGN proxy v
_ = Integer -> Word
forall a. Num a => Integer -> a
fromInteger (Proxy (SeedSizeDSIGN v) -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (SeedSizeDSIGN v)
forall k (t :: k). Proxy t
Proxy @(SeedSizeDSIGN v)))
sizeVerKeyDSIGN :: forall v proxy. DSIGNAlgorithm v => proxy v -> Word
sizeVerKeyDSIGN :: proxy v -> Word
sizeVerKeyDSIGN proxy v
_ = Integer -> Word
forall a. Num a => Integer -> a
fromInteger (Proxy (SizeVerKeyDSIGN v) -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (SizeVerKeyDSIGN v)
forall k (t :: k). Proxy t
Proxy @(SizeVerKeyDSIGN v)))
sizeSignKeyDSIGN :: forall v proxy. DSIGNAlgorithm v => proxy v -> Word
sizeSignKeyDSIGN :: proxy v -> Word
sizeSignKeyDSIGN proxy v
_ = Integer -> Word
forall a. Num a => Integer -> a
fromInteger (Proxy (SizeSignKeyDSIGN v) -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (SizeSignKeyDSIGN v)
forall k (t :: k). Proxy t
Proxy @(SizeSignKeyDSIGN v)))
sizeSigDSIGN :: forall v proxy. DSIGNAlgorithm v => proxy v -> Word
sizeSigDSIGN :: proxy v -> Word
sizeSigDSIGN proxy v
_ = Integer -> Word
forall a. Num a => Integer -> a
fromInteger (Proxy (SizeSigDSIGN v) -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (SizeSigDSIGN v)
forall k (t :: k). Proxy t
Proxy @(SizeSigDSIGN v)))
encodeVerKeyDSIGN :: DSIGNAlgorithm v => VerKeyDSIGN v -> Encoding
encodeVerKeyDSIGN :: VerKeyDSIGN v -> Encoding
encodeVerKeyDSIGN = ByteString -> Encoding
encodeBytes (ByteString -> Encoding)
-> (VerKeyDSIGN v -> ByteString) -> VerKeyDSIGN v -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VerKeyDSIGN v -> ByteString
forall v. DSIGNAlgorithm v => VerKeyDSIGN v -> ByteString
rawSerialiseVerKeyDSIGN
encodeSignKeyDSIGN :: DSIGNAlgorithm v => SignKeyDSIGN v -> Encoding
encodeSignKeyDSIGN :: SignKeyDSIGN v -> Encoding
encodeSignKeyDSIGN = ByteString -> Encoding
encodeBytes (ByteString -> Encoding)
-> (SignKeyDSIGN v -> ByteString) -> SignKeyDSIGN v -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SignKeyDSIGN v -> ByteString
forall v. DSIGNAlgorithm v => SignKeyDSIGN v -> ByteString
rawSerialiseSignKeyDSIGN
encodeSigDSIGN :: DSIGNAlgorithm v => SigDSIGN v -> Encoding
encodeSigDSIGN :: SigDSIGN v -> Encoding
encodeSigDSIGN = ByteString -> Encoding
encodeBytes (ByteString -> Encoding)
-> (SigDSIGN v -> ByteString) -> SigDSIGN v -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SigDSIGN v -> ByteString
forall v. DSIGNAlgorithm v => SigDSIGN v -> ByteString
rawSerialiseSigDSIGN
decodeVerKeyDSIGN :: forall v s. DSIGNAlgorithm v => Decoder s (VerKeyDSIGN v)
decodeVerKeyDSIGN :: Decoder s (VerKeyDSIGN v)
decodeVerKeyDSIGN = do
ByteString
bs <- Decoder s ByteString
forall s. Decoder s ByteString
decodeBytes
case ByteString -> Maybe (VerKeyDSIGN v)
forall v. DSIGNAlgorithm v => ByteString -> Maybe (VerKeyDSIGN v)
rawDeserialiseVerKeyDSIGN ByteString
bs of
Just VerKeyDSIGN v
vk -> VerKeyDSIGN v -> Decoder s (VerKeyDSIGN v)
forall (m :: * -> *) a. Monad m => a -> m a
return VerKeyDSIGN v
vk
Maybe (VerKeyDSIGN v)
Nothing
| Int
actual Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
expected
-> [Char] -> Decoder s (VerKeyDSIGN v)
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char]
"decodeVerKeyDSIGN: wrong length, expected " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
Int -> [Char]
forall a. Show a => a -> [Char]
show Int
expected [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" bytes but got " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
actual)
| Bool
otherwise -> [Char] -> Decoder s (VerKeyDSIGN v)
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"decodeVerKeyDSIGN: cannot decode key"
where
expected :: Int
expected = Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy v -> Word
forall v (proxy :: * -> *). DSIGNAlgorithm v => proxy v -> Word
sizeVerKeyDSIGN (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v))
actual :: Int
actual = ByteString -> Int
BS.length ByteString
bs
decodeSignKeyDSIGN :: forall v s. DSIGNAlgorithm v => Decoder s (SignKeyDSIGN v)
decodeSignKeyDSIGN :: Decoder s (SignKeyDSIGN v)
decodeSignKeyDSIGN = do
ByteString
bs <- Decoder s ByteString
forall s. Decoder s ByteString
decodeBytes
case ByteString -> Maybe (SignKeyDSIGN v)
forall v. DSIGNAlgorithm v => ByteString -> Maybe (SignKeyDSIGN v)
rawDeserialiseSignKeyDSIGN ByteString
bs of
Just SignKeyDSIGN v
sk -> SignKeyDSIGN v -> Decoder s (SignKeyDSIGN v)
forall (m :: * -> *) a. Monad m => a -> m a
return SignKeyDSIGN v
sk
Maybe (SignKeyDSIGN v)
Nothing
| Int
actual Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
expected
-> [Char] -> Decoder s (SignKeyDSIGN v)
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char]
"decodeSignKeyDSIGN: wrong length, expected " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
Int -> [Char]
forall a. Show a => a -> [Char]
show Int
expected [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" bytes but got " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
actual)
| Bool
otherwise -> [Char] -> Decoder s (SignKeyDSIGN v)
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"decodeSignKeyDSIGN: cannot decode key"
where
expected :: Int
expected = Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy v -> Word
forall v (proxy :: * -> *). DSIGNAlgorithm v => proxy v -> Word
sizeSignKeyDSIGN (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v))
actual :: Int
actual = ByteString -> Int
BS.length ByteString
bs
decodeSigDSIGN :: forall v s. DSIGNAlgorithm v => Decoder s (SigDSIGN v)
decodeSigDSIGN :: Decoder s (SigDSIGN v)
decodeSigDSIGN = do
ByteString
bs <- Decoder s ByteString
forall s. Decoder s ByteString
decodeBytes
case ByteString -> Maybe (SigDSIGN v)
forall v. DSIGNAlgorithm v => ByteString -> Maybe (SigDSIGN v)
rawDeserialiseSigDSIGN ByteString
bs of
Just SigDSIGN v
sig -> SigDSIGN v -> Decoder s (SigDSIGN v)
forall (m :: * -> *) a. Monad m => a -> m a
return SigDSIGN v
sig
Maybe (SigDSIGN v)
Nothing
| Int
actual Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
expected
-> [Char] -> Decoder s (SigDSIGN v)
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char]
"decodeSigDSIGN: wrong length, expected " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
Int -> [Char]
forall a. Show a => a -> [Char]
show Int
expected [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" bytes but got " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
actual)
| Bool
otherwise -> [Char] -> Decoder s (SigDSIGN v)
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"decodeSigDSIGN: cannot decode signature"
where
expected :: Int
expected = Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy v -> Word
forall v (proxy :: * -> *). DSIGNAlgorithm v => proxy v -> Word
sizeSigDSIGN (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v))
actual :: Int
actual = ByteString -> Int
BS.length ByteString
bs
newtype SignedDSIGN v a = SignedDSIGN (SigDSIGN v)
deriving (forall x. SignedDSIGN v a -> Rep (SignedDSIGN v a) x)
-> (forall x. Rep (SignedDSIGN v a) x -> SignedDSIGN v a)
-> Generic (SignedDSIGN v a)
forall x. Rep (SignedDSIGN v a) x -> SignedDSIGN v a
forall x. SignedDSIGN v a -> Rep (SignedDSIGN v a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall v a x. Rep (SignedDSIGN v a) x -> SignedDSIGN v a
forall v a x. SignedDSIGN v a -> Rep (SignedDSIGN v a) x
$cto :: forall v a x. Rep (SignedDSIGN v a) x -> SignedDSIGN v a
$cfrom :: forall v a x. SignedDSIGN v a -> Rep (SignedDSIGN v a) x
Generic
deriving instance DSIGNAlgorithm v => Show (SignedDSIGN v a)
deriving instance DSIGNAlgorithm v => Eq (SignedDSIGN v a)
deriving instance NFData (SigDSIGN v) => NFData (SignedDSIGN v a)
instance DSIGNAlgorithm v => NoThunks (SignedDSIGN v a)
signedDSIGN
:: (DSIGNAlgorithm v, Signable v a)
=> ContextDSIGN v
-> a
-> SignKeyDSIGN v
-> SignedDSIGN v a
signedDSIGN :: ContextDSIGN v -> a -> SignKeyDSIGN v -> SignedDSIGN v a
signedDSIGN ContextDSIGN v
ctxt a
a SignKeyDSIGN v
key = SigDSIGN v -> SignedDSIGN v a
forall v a. SigDSIGN v -> SignedDSIGN v a
SignedDSIGN (ContextDSIGN v -> a -> SignKeyDSIGN v -> SigDSIGN v
forall v a.
(DSIGNAlgorithm v, Signable v a, HasCallStack) =>
ContextDSIGN v -> a -> SignKeyDSIGN v -> SigDSIGN v
signDSIGN ContextDSIGN v
ctxt a
a SignKeyDSIGN v
key)
verifySignedDSIGN
:: (DSIGNAlgorithm v, Signable v a, HasCallStack)
=> ContextDSIGN v
-> VerKeyDSIGN v
-> a
-> SignedDSIGN v a
-> Either String ()
verifySignedDSIGN :: ContextDSIGN v
-> VerKeyDSIGN v -> a -> SignedDSIGN v a -> Either [Char] ()
verifySignedDSIGN ContextDSIGN v
ctxt VerKeyDSIGN v
key a
a (SignedDSIGN SigDSIGN v
s) = ContextDSIGN v
-> VerKeyDSIGN v -> a -> SigDSIGN v -> Either [Char] ()
forall v a.
(DSIGNAlgorithm v, Signable v a, HasCallStack) =>
ContextDSIGN v
-> VerKeyDSIGN v -> a -> SigDSIGN v -> Either [Char] ()
verifyDSIGN ContextDSIGN v
ctxt VerKeyDSIGN v
key a
a SigDSIGN v
s
encodeSignedDSIGN :: DSIGNAlgorithm v => SignedDSIGN v a -> Encoding
encodeSignedDSIGN :: SignedDSIGN v a -> Encoding
encodeSignedDSIGN (SignedDSIGN SigDSIGN v
s) = SigDSIGN v -> Encoding
forall v. DSIGNAlgorithm v => SigDSIGN v -> Encoding
encodeSigDSIGN SigDSIGN v
s
decodeSignedDSIGN :: DSIGNAlgorithm v => Decoder s (SignedDSIGN v a)
decodeSignedDSIGN :: Decoder s (SignedDSIGN v a)
decodeSignedDSIGN = SigDSIGN v -> SignedDSIGN v a
forall v a. SigDSIGN v -> SignedDSIGN v a
SignedDSIGN (SigDSIGN v -> SignedDSIGN v a)
-> Decoder s (SigDSIGN v) -> Decoder s (SignedDSIGN v a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (SigDSIGN v)
forall v s. DSIGNAlgorithm v => Decoder s (SigDSIGN v)
decodeSigDSIGN
encodedVerKeyDSIGNSizeExpr :: forall v. DSIGNAlgorithm v => Proxy (VerKeyDSIGN v) -> Size
encodedVerKeyDSIGNSizeExpr :: Proxy (VerKeyDSIGN v) -> Size
encodedVerKeyDSIGNSizeExpr Proxy (VerKeyDSIGN v)
_proxy =
Integer -> Size
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word -> Integer
forall s a. (Integral s, Integral a) => s -> a
withWordSize :: Word -> Integer) (Proxy v -> Word
forall v (proxy :: * -> *). DSIGNAlgorithm v => proxy v -> Word
sizeVerKeyDSIGN (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v)))
Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Word -> Size
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy v -> Word
forall v (proxy :: * -> *). DSIGNAlgorithm v => proxy v -> Word
sizeVerKeyDSIGN (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v))
encodedSignKeyDESIGNSizeExpr :: forall v. DSIGNAlgorithm v => Proxy (SignKeyDSIGN v) -> Size
encodedSignKeyDESIGNSizeExpr :: Proxy (SignKeyDSIGN v) -> Size
encodedSignKeyDESIGNSizeExpr Proxy (SignKeyDSIGN v)
_proxy =
Integer -> Size
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word -> Integer
forall s a. (Integral s, Integral a) => s -> a
withWordSize :: Word -> Integer) (Proxy v -> Word
forall v (proxy :: * -> *). DSIGNAlgorithm v => proxy v -> Word
sizeSignKeyDSIGN (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v)))
Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Word -> Size
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy v -> Word
forall v (proxy :: * -> *). DSIGNAlgorithm v => proxy v -> Word
sizeSignKeyDSIGN (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v))
encodedSigDSIGNSizeExpr :: forall v. DSIGNAlgorithm v => Proxy (SigDSIGN v) -> Size
encodedSigDSIGNSizeExpr :: Proxy (SigDSIGN v) -> Size
encodedSigDSIGNSizeExpr Proxy (SigDSIGN v)
_proxy =
Integer -> Size
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word -> Integer
forall s a. (Integral s, Integral a) => s -> a
withWordSize :: Word -> Integer) (Proxy v -> Word
forall v (proxy :: * -> *). DSIGNAlgorithm v => proxy v -> Word
sizeSigDSIGN (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v)))
Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Word -> Size
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy v -> Word
forall v (proxy :: * -> *). DSIGNAlgorithm v => proxy v -> Word
sizeSigDSIGN (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v))