{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

-- | Abstract Verifiable Random Functions.
module Cardano.Crypto.VRF.Class
  (
    -- * VRF algorithm class
    VRFAlgorithm (..)

    -- ** VRF output
  , OutputVRF(..)
  , getOutputVRFNatural
  , mkTestOutputVRF

    -- * 'CertifiedVRF' wrapper
  , CertifiedVRF (..)
  , evalCertified
  , verifyCertified

    -- * CBOR encoding and decoding
  , encodeVerKeyVRF
  , decodeVerKeyVRF
  , encodeSignKeyVRF
  , decodeSignKeyVRF
  , encodeCertVRF
  , decodeCertVRF


    -- * Encoded 'Size' expressions
  , encodedVerKeyVRFSizeExpr
  , encodedSignKeyVRFSizeExpr
  , encodedCertVRFSizeExpr
)
where

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 (TypeError, ErrorMessage (..))
import NoThunks.Class (NoThunks)
import Numeric.Natural (Natural)

import qualified Data.ByteString as BS

import Cardano.Prelude (NFData)
import Cardano.Binary
         (Decoder, Encoding, FromCBOR (..), ToCBOR (..), Size,
          encodeListLen, enforceSize, decodeBytes, encodeBytes,
          withWordSize)

import Cardano.Crypto.Util (Empty, bytesToNatural, naturalToBytes)
import Cardano.Crypto.Seed (Seed)
import Cardano.Crypto.Hash.Class (HashAlgorithm, Hash, hashWith)


class ( Typeable v
      , Show (VerKeyVRF v)
      , Eq (VerKeyVRF v)
      , Show (SignKeyVRF v)
      , Show (CertVRF v)
      , Eq (CertVRF v)
      , NoThunks (CertVRF v)
      , NoThunks (VerKeyVRF v)
      , NoThunks (SignKeyVRF v)
      )
      => VRFAlgorithm v where


  --
  -- Key and signature types
  --

  data VerKeyVRF  v :: Type
  data SignKeyVRF v :: Type
  data CertVRF    v :: Type


  --
  -- Metadata and basic key operations
  --

  algorithmNameVRF :: proxy v -> String

  deriveVerKeyVRF :: SignKeyVRF v -> VerKeyVRF v

  hashVerKeyVRF :: HashAlgorithm h => VerKeyVRF v -> Hash h (VerKeyVRF v)
  hashVerKeyVRF = (VerKeyVRF v -> ByteString) -> VerKeyVRF v -> Hash h (VerKeyVRF v)
forall h a. HashAlgorithm h => (a -> ByteString) -> a -> Hash h a
hashWith VerKeyVRF v -> ByteString
forall v. VRFAlgorithm v => VerKeyVRF v -> ByteString
rawSerialiseVerKeyVRF

  --
  -- Core algorithm operations
  --

  -- | Context required to run the VRF algorithm
  --
  -- Unit by default (no context required)
  type ContextVRF v :: Type
  type ContextVRF v = ()

  type Signable v :: Type -> Constraint
  type Signable c = Empty

  evalVRF
    :: (HasCallStack, Signable v a)
    => ContextVRF v
    -> a
    -> SignKeyVRF v
    -> (OutputVRF v, CertVRF v)

  verifyVRF
    :: (HasCallStack, Signable v a)
    => ContextVRF v
    -> VerKeyVRF v
    -> a
    -> (OutputVRF v, CertVRF v)
    -> Bool

  --
  -- Key generation
  --

  genKeyVRF :: Seed -> SignKeyVRF v
  genKeyPairVRF :: Seed -> (SignKeyVRF v, VerKeyVRF v)

  genKeyVRF =
    (SignKeyVRF v, VerKeyVRF v) -> SignKeyVRF v
forall a b. (a, b) -> a
fst ((SignKeyVRF v, VerKeyVRF v) -> SignKeyVRF v)
-> (Seed -> (SignKeyVRF v, VerKeyVRF v)) -> Seed -> SignKeyVRF v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seed -> (SignKeyVRF v, VerKeyVRF v)
forall v. VRFAlgorithm v => Seed -> (SignKeyVRF v, VerKeyVRF v)
genKeyPairVRF

  genKeyPairVRF = \Seed
seed ->
    let sk :: SignKeyVRF v
sk = Seed -> SignKeyVRF v
forall v. VRFAlgorithm v => Seed -> SignKeyVRF v
genKeyVRF Seed
seed
    in (SignKeyVRF v
sk, SignKeyVRF v -> VerKeyVRF v
forall v. VRFAlgorithm v => SignKeyVRF v -> VerKeyVRF v
deriveVerKeyVRF SignKeyVRF v
sk)

  -- | The upper bound on the 'Seed' size needed by 'genKeyVRF', in bytes.
  seedSizeVRF :: proxy v -> Word


  --
  -- Serialisation/(de)serialisation in fixed-size raw format
  --

  sizeVerKeyVRF  :: proxy v -> Word
  sizeSignKeyVRF :: proxy v -> Word
  sizeCertVRF    :: proxy v -> Word
  sizeOutputVRF  :: proxy v -> Word

  rawSerialiseVerKeyVRF    :: VerKeyVRF  v -> ByteString
  rawSerialiseSignKeyVRF   :: SignKeyVRF v -> ByteString
  rawSerialiseCertVRF      :: CertVRF    v -> ByteString

  rawDeserialiseVerKeyVRF  :: ByteString -> Maybe (VerKeyVRF  v)
  rawDeserialiseSignKeyVRF :: ByteString -> Maybe (SignKeyVRF v)
  rawDeserialiseCertVRF    :: ByteString -> Maybe (CertVRF    v)

  {-# MINIMAL
        algorithmNameVRF
      , deriveVerKeyVRF
      , evalVRF
      , verifyVRF
      , seedSizeVRF
      , (genKeyVRF                | genKeyPairVRF)
      , rawSerialiseVerKeyVRF
      , rawSerialiseSignKeyVRF
      , rawSerialiseCertVRF
      , rawDeserialiseVerKeyVRF
      , rawDeserialiseSignKeyVRF
      , rawDeserialiseCertVRF
      , sizeVerKeyVRF
      , sizeSignKeyVRF
      , sizeCertVRF
      , sizeOutputVRF
    #-}

--
-- Do not provide Ord instances for keys, see #38
--

instance ( TypeError ('Text "Ord not supported for signing keys, use the hash instead")
         , Eq (SignKeyVRF v)
         )
      => Ord (SignKeyVRF v) where
    compare :: SignKeyVRF v -> SignKeyVRF v -> Ordering
compare = [Char] -> SignKeyVRF v -> SignKeyVRF v -> Ordering
forall a. HasCallStack => [Char] -> a
error [Char]
"unsupported"

instance ( TypeError ('Text "Ord not supported for verification keys, use the hash instead")
         , Eq (VerKeyVRF v)
         )
      => Ord (VerKeyVRF v) where
    compare :: VerKeyVRF v -> VerKeyVRF v -> Ordering
compare = [Char] -> VerKeyVRF v -> VerKeyVRF v -> Ordering
forall a. HasCallStack => [Char] -> a
error [Char]
"unsupported"

-- | The output bytes of the VRF.
--
-- The output size is a fixed number of bytes and is given by 'sizeOutputVRF'.
--
newtype OutputVRF v = OutputVRF { OutputVRF v -> ByteString
getOutputVRFBytes :: ByteString }
  deriving (OutputVRF v -> OutputVRF v -> Bool
(OutputVRF v -> OutputVRF v -> Bool)
-> (OutputVRF v -> OutputVRF v -> Bool) -> Eq (OutputVRF v)
forall v. OutputVRF v -> OutputVRF v -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OutputVRF v -> OutputVRF v -> Bool
$c/= :: forall v. OutputVRF v -> OutputVRF v -> Bool
== :: OutputVRF v -> OutputVRF v -> Bool
$c== :: forall v. OutputVRF v -> OutputVRF v -> Bool
Eq, Eq (OutputVRF v)
Eq (OutputVRF v)
-> (OutputVRF v -> OutputVRF v -> Ordering)
-> (OutputVRF v -> OutputVRF v -> Bool)
-> (OutputVRF v -> OutputVRF v -> Bool)
-> (OutputVRF v -> OutputVRF v -> Bool)
-> (OutputVRF v -> OutputVRF v -> Bool)
-> (OutputVRF v -> OutputVRF v -> OutputVRF v)
-> (OutputVRF v -> OutputVRF v -> OutputVRF v)
-> Ord (OutputVRF v)
OutputVRF v -> OutputVRF v -> Bool
OutputVRF v -> OutputVRF v -> Ordering
OutputVRF v -> OutputVRF v -> OutputVRF v
forall v. Eq (OutputVRF v)
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 v. OutputVRF v -> OutputVRF v -> Bool
forall v. OutputVRF v -> OutputVRF v -> Ordering
forall v. OutputVRF v -> OutputVRF v -> OutputVRF v
min :: OutputVRF v -> OutputVRF v -> OutputVRF v
$cmin :: forall v. OutputVRF v -> OutputVRF v -> OutputVRF v
max :: OutputVRF v -> OutputVRF v -> OutputVRF v
$cmax :: forall v. OutputVRF v -> OutputVRF v -> OutputVRF v
>= :: OutputVRF v -> OutputVRF v -> Bool
$c>= :: forall v. OutputVRF v -> OutputVRF v -> Bool
> :: OutputVRF v -> OutputVRF v -> Bool
$c> :: forall v. OutputVRF v -> OutputVRF v -> Bool
<= :: OutputVRF v -> OutputVRF v -> Bool
$c<= :: forall v. OutputVRF v -> OutputVRF v -> Bool
< :: OutputVRF v -> OutputVRF v -> Bool
$c< :: forall v. OutputVRF v -> OutputVRF v -> Bool
compare :: OutputVRF v -> OutputVRF v -> Ordering
$ccompare :: forall v. OutputVRF v -> OutputVRF v -> Ordering
$cp1Ord :: forall v. Eq (OutputVRF v)
Ord, Int -> OutputVRF v -> ShowS
[OutputVRF v] -> ShowS
OutputVRF v -> [Char]
(Int -> OutputVRF v -> ShowS)
-> (OutputVRF v -> [Char])
-> ([OutputVRF v] -> ShowS)
-> Show (OutputVRF v)
forall v. Int -> OutputVRF v -> ShowS
forall v. [OutputVRF v] -> ShowS
forall v. OutputVRF v -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [OutputVRF v] -> ShowS
$cshowList :: forall v. [OutputVRF v] -> ShowS
show :: OutputVRF v -> [Char]
$cshow :: forall v. OutputVRF v -> [Char]
showsPrec :: Int -> OutputVRF v -> ShowS
$cshowsPrec :: forall v. Int -> OutputVRF v -> ShowS
Show, Typeable (OutputVRF v)
Typeable (OutputVRF v)
-> (OutputVRF v -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size)
    -> Proxy (OutputVRF v) -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size)
    -> Proxy [OutputVRF v] -> Size)
-> ToCBOR (OutputVRF v)
OutputVRF v -> Encoding
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [OutputVRF v] -> Size
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (OutputVRF v) -> Size
forall v. Typeable v => Typeable (OutputVRF v)
forall v. Typeable v => OutputVRF v -> Encoding
forall a.
Typeable a
-> (a -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy [a] -> Size)
-> ToCBOR a
forall v.
Typeable v =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [OutputVRF v] -> Size
forall v.
Typeable v =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (OutputVRF v) -> Size
encodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [OutputVRF v] -> Size
$cencodedListSizeExpr :: forall v.
Typeable v =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [OutputVRF v] -> Size
encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (OutputVRF v) -> Size
$cencodedSizeExpr :: forall v.
Typeable v =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (OutputVRF v) -> Size
toCBOR :: OutputVRF v -> Encoding
$ctoCBOR :: forall v. Typeable v => OutputVRF v -> Encoding
$cp1ToCBOR :: forall v. Typeable v => Typeable (OutputVRF v)
ToCBOR, Typeable (OutputVRF v)
Decoder s (OutputVRF v)
Typeable (OutputVRF v)
-> (forall s. Decoder s (OutputVRF v))
-> (Proxy (OutputVRF v) -> Text)
-> FromCBOR (OutputVRF v)
Proxy (OutputVRF v) -> Text
forall s. Decoder s (OutputVRF v)
forall v. Typeable v => Typeable (OutputVRF v)
forall v. Typeable v => Proxy (OutputVRF v) -> Text
forall a.
Typeable a
-> (forall s. Decoder s a) -> (Proxy a -> Text) -> FromCBOR a
forall v s. Typeable v => Decoder s (OutputVRF v)
label :: Proxy (OutputVRF v) -> Text
$clabel :: forall v. Typeable v => Proxy (OutputVRF v) -> Text
fromCBOR :: Decoder s (OutputVRF v)
$cfromCBOR :: forall v s. Typeable v => Decoder s (OutputVRF v)
$cp1FromCBOR :: forall v. Typeable v => Typeable (OutputVRF v)
FromCBOR, Context -> OutputVRF v -> IO (Maybe ThunkInfo)
Proxy (OutputVRF v) -> [Char]
(Context -> OutputVRF v -> IO (Maybe ThunkInfo))
-> (Context -> OutputVRF v -> IO (Maybe ThunkInfo))
-> (Proxy (OutputVRF v) -> [Char])
-> NoThunks (OutputVRF v)
forall v. Context -> OutputVRF v -> IO (Maybe ThunkInfo)
forall v. Proxy (OutputVRF v) -> [Char]
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> [Char])
-> NoThunks a
showTypeOf :: Proxy (OutputVRF v) -> [Char]
$cshowTypeOf :: forall v. Proxy (OutputVRF v) -> [Char]
wNoThunks :: Context -> OutputVRF v -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall v. Context -> OutputVRF v -> IO (Maybe ThunkInfo)
noThunks :: Context -> OutputVRF v -> IO (Maybe ThunkInfo)
$cnoThunks :: forall v. Context -> OutputVRF v -> IO (Maybe ThunkInfo)
NoThunks)
  deriving newtype OutputVRF v -> ()
(OutputVRF v -> ()) -> NFData (OutputVRF v)
forall v. OutputVRF v -> ()
forall a. (a -> ()) -> NFData a
rnf :: OutputVRF v -> ()
$crnf :: forall v. OutputVRF v -> ()
NFData


-- | The output bytes of the VRF interpreted as a big endian natural number.
--
-- The range of this number is determined by the size of the VRF output bytes.
-- It is thus in the range @0 ..  2 ^ (8 * sizeOutputVRF proxy) - 1@.
--
getOutputVRFNatural :: OutputVRF v -> Natural
getOutputVRFNatural :: OutputVRF v -> Natural
getOutputVRFNatural = ByteString -> Natural
bytesToNatural (ByteString -> Natural)
-> (OutputVRF v -> ByteString) -> OutputVRF v -> Natural
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OutputVRF v -> ByteString
forall v. OutputVRF v -> ByteString
getOutputVRFBytes

-- | For testing purposes, make an 'OutputVRF' from a 'Natural'.
--
-- The 'OutputVRF' will be of the appropriate size for the 'VRFAlgorithm'.
--
mkTestOutputVRF :: forall v. VRFAlgorithm v => Natural -> OutputVRF v
mkTestOutputVRF :: Natural -> OutputVRF v
mkTestOutputVRF = ByteString -> OutputVRF v
forall v. ByteString -> OutputVRF v
OutputVRF (ByteString -> OutputVRF v)
-> (Natural -> ByteString) -> Natural -> OutputVRF v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Natural -> ByteString
naturalToBytes Int
sz
  where
    sz :: Int
sz = Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy v -> Word
forall v (proxy :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeOutputVRF (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v))

--
-- Convenient CBOR encoding/decoding
--
-- Implementations in terms of the raw (de)serialise
--

encodeVerKeyVRF :: VRFAlgorithm v => VerKeyVRF v -> Encoding
encodeVerKeyVRF :: VerKeyVRF v -> Encoding
encodeVerKeyVRF = ByteString -> Encoding
encodeBytes (ByteString -> Encoding)
-> (VerKeyVRF v -> ByteString) -> VerKeyVRF v -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VerKeyVRF v -> ByteString
forall v. VRFAlgorithm v => VerKeyVRF v -> ByteString
rawSerialiseVerKeyVRF

encodeSignKeyVRF :: VRFAlgorithm v => SignKeyVRF v -> Encoding
encodeSignKeyVRF :: SignKeyVRF v -> Encoding
encodeSignKeyVRF = ByteString -> Encoding
encodeBytes (ByteString -> Encoding)
-> (SignKeyVRF v -> ByteString) -> SignKeyVRF v -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SignKeyVRF v -> ByteString
forall v. VRFAlgorithm v => SignKeyVRF v -> ByteString
rawSerialiseSignKeyVRF

encodeCertVRF :: VRFAlgorithm v => CertVRF v -> Encoding
encodeCertVRF :: CertVRF v -> Encoding
encodeCertVRF = ByteString -> Encoding
encodeBytes (ByteString -> Encoding)
-> (CertVRF v -> ByteString) -> CertVRF v -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CertVRF v -> ByteString
forall v. VRFAlgorithm v => CertVRF v -> ByteString
rawSerialiseCertVRF

decodeVerKeyVRF :: forall v s. VRFAlgorithm v => Decoder s (VerKeyVRF v)
decodeVerKeyVRF :: Decoder s (VerKeyVRF v)
decodeVerKeyVRF = do
    ByteString
bs <- Decoder s ByteString
forall s. Decoder s ByteString
decodeBytes
    case ByteString -> Maybe (VerKeyVRF v)
forall v. VRFAlgorithm v => ByteString -> Maybe (VerKeyVRF v)
rawDeserialiseVerKeyVRF ByteString
bs of
      Just VerKeyVRF v
vk -> VerKeyVRF v -> Decoder s (VerKeyVRF v)
forall (m :: * -> *) a. Monad m => a -> m a
return VerKeyVRF v
vk
      Maybe (VerKeyVRF v)
Nothing
        | Int
actual Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
expected
                    -> [Char] -> Decoder s (VerKeyVRF v)
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char]
"decodeVerKeyVRF: wrong length, expected " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++
                             Int -> [Char]
forall a. Show a => a -> [Char]
show Int
expected [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
" bytes but got " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
actual)
        | Bool
otherwise -> [Char] -> Decoder s (VerKeyVRF v)
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"decodeVerKeyVRF: 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 :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeVerKeyVRF (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v))
          actual :: Int
actual   = ByteString -> Int
BS.length ByteString
bs

decodeSignKeyVRF :: forall v s. VRFAlgorithm v => Decoder s (SignKeyVRF v)
decodeSignKeyVRF :: Decoder s (SignKeyVRF v)
decodeSignKeyVRF = do
    ByteString
bs <- Decoder s ByteString
forall s. Decoder s ByteString
decodeBytes
    case ByteString -> Maybe (SignKeyVRF v)
forall v. VRFAlgorithm v => ByteString -> Maybe (SignKeyVRF v)
rawDeserialiseSignKeyVRF ByteString
bs of
      Just SignKeyVRF v
sk -> SignKeyVRF v -> Decoder s (SignKeyVRF v)
forall (m :: * -> *) a. Monad m => a -> m a
return SignKeyVRF v
sk
      Maybe (SignKeyVRF v)
Nothing
        | Int
actual Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
expected
                    -> [Char] -> Decoder s (SignKeyVRF v)
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char]
"decodeSignKeyVRF: wrong length, expected " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++
                             Int -> [Char]
forall a. Show a => a -> [Char]
show Int
expected [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
" bytes but got " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
actual)
        | Bool
otherwise -> [Char] -> Decoder s (SignKeyVRF v)
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"decodeSignKeyVRF: 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 :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeSignKeyVRF (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v))
          actual :: Int
actual   = ByteString -> Int
BS.length ByteString
bs

decodeCertVRF :: forall v s. VRFAlgorithm v => Decoder s (CertVRF v)
decodeCertVRF :: Decoder s (CertVRF v)
decodeCertVRF = do
    ByteString
bs <- Decoder s ByteString
forall s. Decoder s ByteString
decodeBytes
    case ByteString -> Maybe (CertVRF v)
forall v. VRFAlgorithm v => ByteString -> Maybe (CertVRF v)
rawDeserialiseCertVRF ByteString
bs of
      Just CertVRF v
crt -> CertVRF v -> Decoder s (CertVRF v)
forall (m :: * -> *) a. Monad m => a -> m a
return CertVRF v
crt
      Maybe (CertVRF v)
Nothing
        | Int
actual Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
expected
                    -> [Char] -> Decoder s (CertVRF v)
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char]
"decodeCertVRF: wrong length, expected " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++
                             Int -> [Char]
forall a. Show a => a -> [Char]
show Int
expected [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
" bytes but got " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
actual)
        | Bool
otherwise -> [Char] -> Decoder s (CertVRF v)
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"decodeCertVRF: 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 :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeCertVRF (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v))
          actual :: Int
actual   = ByteString -> Int
BS.length ByteString
bs

data CertifiedVRF v a
  = CertifiedVRF
      { CertifiedVRF v a -> OutputVRF v
certifiedOutput :: !(OutputVRF v)
      , CertifiedVRF v a -> CertVRF v
certifiedProof :: !(CertVRF v)
      }
  deriving (forall x. CertifiedVRF v a -> Rep (CertifiedVRF v a) x)
-> (forall x. Rep (CertifiedVRF v a) x -> CertifiedVRF v a)
-> Generic (CertifiedVRF v a)
forall x. Rep (CertifiedVRF v a) x -> CertifiedVRF v a
forall x. CertifiedVRF v a -> Rep (CertifiedVRF v a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall v a x. Rep (CertifiedVRF v a) x -> CertifiedVRF v a
forall v a x. CertifiedVRF v a -> Rep (CertifiedVRF v a) x
$cto :: forall v a x. Rep (CertifiedVRF v a) x -> CertifiedVRF v a
$cfrom :: forall v a x. CertifiedVRF v a -> Rep (CertifiedVRF v a) x
Generic

deriving instance VRFAlgorithm v => Show (CertifiedVRF v a)
deriving instance VRFAlgorithm v => Eq   (CertifiedVRF v a)

instance VRFAlgorithm v => NoThunks (CertifiedVRF v a)
  -- use generic instance

instance (VRFAlgorithm v, Typeable a) => ToCBOR (CertifiedVRF v a) where
  toCBOR :: CertifiedVRF v a -> Encoding
toCBOR CertifiedVRF v a
cvrf =
    Word -> Encoding
encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<>
      OutputVRF v -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (CertifiedVRF v a -> OutputVRF v
forall v a. CertifiedVRF v a -> OutputVRF v
certifiedOutput CertifiedVRF v a
cvrf) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<>
      CertVRF v -> Encoding
forall v. VRFAlgorithm v => CertVRF v -> Encoding
encodeCertVRF (CertifiedVRF v a -> CertVRF v
forall v a. CertifiedVRF v a -> CertVRF v
certifiedProof CertifiedVRF v a
cvrf)

  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (CertifiedVRF v a) -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_size Proxy (CertifiedVRF v a)
proxy =
        Size
1
      Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy (OutputVRF v) -> Size
certifiedOutputSize (CertifiedVRF v a -> OutputVRF v
forall v a. CertifiedVRF v a -> OutputVRF v
certifiedOutput (CertifiedVRF v a -> OutputVRF v)
-> Proxy (CertifiedVRF v a) -> Proxy (OutputVRF v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (CertifiedVRF v a)
proxy)
      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 :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeCertVRF (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v))
    where
      certifiedOutputSize :: Proxy (OutputVRF v) -> Size
      certifiedOutputSize :: Proxy (OutputVRF v) -> Size
certifiedOutputSize Proxy (OutputVRF v)
_proxy =
        Word -> Size
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word -> Size) -> Word -> Size
forall a b. (a -> b) -> a -> b
$ Proxy v -> Word
forall v (proxy :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeOutputVRF (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v)

instance (VRFAlgorithm v, Typeable a) => FromCBOR (CertifiedVRF v a) where
  fromCBOR :: Decoder s (CertifiedVRF v a)
fromCBOR =
    OutputVRF v -> CertVRF v -> CertifiedVRF v a
forall v a. OutputVRF v -> CertVRF v -> CertifiedVRF v a
CertifiedVRF (OutputVRF v -> CertVRF v -> CertifiedVRF v a)
-> Decoder s ()
-> Decoder s (OutputVRF v -> CertVRF v -> CertifiedVRF v a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$
      Text -> Int -> Decoder s ()
forall s. Text -> Int -> Decoder s ()
enforceSize Text
"CertifiedVRF" Int
2 Decoder s (OutputVRF v -> CertVRF v -> CertifiedVRF v a)
-> Decoder s (OutputVRF v)
-> Decoder s (CertVRF v -> CertifiedVRF v a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*>
      Decoder s (OutputVRF v)
forall a s. FromCBOR a => Decoder s a
fromCBOR Decoder s (CertVRF v -> CertifiedVRF v a)
-> Decoder s (CertVRF v) -> Decoder s (CertifiedVRF v a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*>
      Decoder s (CertVRF v)
forall v s. VRFAlgorithm v => Decoder s (CertVRF v)
decodeCertVRF

evalCertified
  :: (VRFAlgorithm v, Signable v a)
  => ContextVRF v
  -> a
  -> SignKeyVRF v
  -> CertifiedVRF v a
evalCertified :: ContextVRF v -> a -> SignKeyVRF v -> CertifiedVRF v a
evalCertified ContextVRF v
ctxt a
a SignKeyVRF v
key = (OutputVRF v -> CertVRF v -> CertifiedVRF v a)
-> (OutputVRF v, CertVRF v) -> CertifiedVRF v a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry OutputVRF v -> CertVRF v -> CertifiedVRF v a
forall v a. OutputVRF v -> CertVRF v -> CertifiedVRF v a
CertifiedVRF ((OutputVRF v, CertVRF v) -> CertifiedVRF v a)
-> (OutputVRF v, CertVRF v) -> CertifiedVRF v a
forall a b. (a -> b) -> a -> b
$ ContextVRF v -> a -> SignKeyVRF v -> (OutputVRF v, CertVRF v)
forall v a.
(VRFAlgorithm v, HasCallStack, Signable v a) =>
ContextVRF v -> a -> SignKeyVRF v -> (OutputVRF v, CertVRF v)
evalVRF ContextVRF v
ctxt a
a SignKeyVRF v
key

verifyCertified
  :: (VRFAlgorithm v, Signable v a)
  => ContextVRF v
  -> VerKeyVRF v
  -> a
  -> CertifiedVRF v a
  -> Bool
verifyCertified :: ContextVRF v -> VerKeyVRF v -> a -> CertifiedVRF v a -> Bool
verifyCertified ContextVRF v
ctxt VerKeyVRF v
vk a
a CertifiedVRF {OutputVRF v
CertVRF v
certifiedProof :: CertVRF v
certifiedOutput :: OutputVRF v
certifiedProof :: forall v a. CertifiedVRF v a -> CertVRF v
certifiedOutput :: forall v a. CertifiedVRF v a -> OutputVRF v
..} = ContextVRF v
-> VerKeyVRF v -> a -> (OutputVRF v, CertVRF v) -> Bool
forall v a.
(VRFAlgorithm v, HasCallStack, Signable v a) =>
ContextVRF v
-> VerKeyVRF v -> a -> (OutputVRF v, CertVRF v) -> Bool
verifyVRF ContextVRF v
ctxt VerKeyVRF v
vk a
a (OutputVRF v
certifiedOutput, CertVRF v
certifiedProof)

--
-- 'Size' expressions for 'ToCBOR' instances
--

-- | 'Size' expression for 'VerKeyVRF' which is using 'sizeVerKeyVRF' encoded as
-- 'Size'.
--
encodedVerKeyVRFSizeExpr :: forall v. VRFAlgorithm v => Proxy (VerKeyVRF v) -> Size
encodedVerKeyVRFSizeExpr :: Proxy (VerKeyVRF v) -> Size
encodedVerKeyVRFSizeExpr Proxy (VerKeyVRF v)
_proxy =
      -- 'encodeBytes' envelope
      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 :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeVerKeyVRF (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v)))
      -- payload
    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 :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeVerKeyVRF (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v))

-- | 'Size' expression for 'SignKeyVRF' which is using 'sizeSignKeyVRF' encoded
-- as 'Size'
--
encodedSignKeyVRFSizeExpr :: forall v. VRFAlgorithm v => Proxy (SignKeyVRF v) -> Size
encodedSignKeyVRFSizeExpr :: Proxy (SignKeyVRF v) -> Size
encodedSignKeyVRFSizeExpr Proxy (SignKeyVRF v)
_proxy =
      -- 'encodeBytes' envelope
      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 :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeSignKeyVRF (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v)))
      -- payload
    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 :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeSignKeyVRF (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v))

-- | 'Size' expression for 'CertVRF' which is using 'sizeCertVRF' encoded as
-- 'Size'.
--
encodedCertVRFSizeExpr :: forall v. VRFAlgorithm v => Proxy (CertVRF v) -> Size
encodedCertVRFSizeExpr :: Proxy (CertVRF v) -> Size
encodedCertVRFSizeExpr Proxy (CertVRF v)
_proxy =
      -- 'encodeBytes' envelope
      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 :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeCertVRF (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v)))
      -- payload
    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 :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeCertVRF (Proxy v
forall k (t :: k). Proxy t
Proxy :: Proxy v))