{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# 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 Control.DeepSeq (NFData)
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 (ErrorMessage (..), TypeError)
import NoThunks.Class (NoThunks)
import Numeric.Natural (Natural)

import qualified Data.ByteString as BS

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

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

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 = forall h a. HashAlgorithm h => (a -> ByteString) -> a -> Hash h a
hashWith 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 ->
    CertVRF v ->
    Maybe (OutputVRF v)

  --
  -- Key generation
  --

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

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

  genKeyPairVRF = \Seed
seed ->
    let sk :: SignKeyVRF v
sk = forall v. VRFAlgorithm v => Seed -> SignKeyVRF v
genKeyVRF Seed
seed
     in (SignKeyVRF v
sk, 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 = 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 = 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 {forall v. OutputVRF v -> ByteString
getOutputVRFBytes :: ByteString}
  deriving (OutputVRF v -> OutputVRF v -> Bool
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, 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
Ord, Int -> OutputVRF v -> ShowS
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, 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
ToCBOR, 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 :: forall s. Decoder s (OutputVRF v)
$cfromCBOR :: forall v s. Typeable v => Decoder s (OutputVRF v)
FromCBOR, Context -> OutputVRF v -> IO (Maybe ThunkInfo)
Proxy (OutputVRF v) -> [Char]
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 -> ()
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 :: forall v. OutputVRF v -> Natural
getOutputVRFNatural = ByteString -> Natural
bytesToNatural forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall v. VRFAlgorithm v => Natural -> OutputVRF v
mkTestOutputVRF = forall v. ByteString -> OutputVRF v
OutputVRF forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Natural -> ByteString
naturalToBytes Int
sz
  where
    sz :: Int
sz = forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall v (proxy :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeOutputVRF (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 :: forall v. VRFAlgorithm v => VerKeyVRF v -> Encoding
encodeVerKeyVRF = ByteString -> Encoding
encodeBytes forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. VRFAlgorithm v => VerKeyVRF v -> ByteString
rawSerialiseVerKeyVRF

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

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

decodeVerKeyVRF :: forall v s. VRFAlgorithm v => Decoder s (VerKeyVRF v)
decodeVerKeyVRF :: forall v s. VRFAlgorithm v => Decoder s (VerKeyVRF v)
decodeVerKeyVRF = do
  ByteString
bs <- forall s. Decoder s ByteString
decodeBytes
  case forall v. VRFAlgorithm v => ByteString -> Maybe (VerKeyVRF v)
rawDeserialiseVerKeyVRF ByteString
bs of
    Just VerKeyVRF v
vk -> forall (m :: * -> *) a. Monad m => a -> m a
return VerKeyVRF v
vk
    Maybe (VerKeyVRF v)
Nothing
      | Int
actual forall a. Eq a => a -> a -> Bool
/= Int
expected ->
          forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail
            ( [Char]
"decodeVerKeyVRF: wrong length, expected "
                forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
expected
                forall a. [a] -> [a] -> [a]
++ [Char]
" bytes but got "
                forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
actual
            )
      | Bool
otherwise -> forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"decodeVerKeyVRF: cannot decode key"
      where
        expected :: Int
expected = forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall v (proxy :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeVerKeyVRF (forall {k} (t :: k). Proxy t
Proxy :: Proxy v))
        actual :: Int
actual = ByteString -> Int
BS.length ByteString
bs
{-# INLINEABLE decodeVerKeyVRF #-}

decodeSignKeyVRF :: forall v s. VRFAlgorithm v => Decoder s (SignKeyVRF v)
decodeSignKeyVRF :: forall v s. VRFAlgorithm v => Decoder s (SignKeyVRF v)
decodeSignKeyVRF = do
  ByteString
bs <- forall s. Decoder s ByteString
decodeBytes
  case forall v. VRFAlgorithm v => ByteString -> Maybe (SignKeyVRF v)
rawDeserialiseSignKeyVRF ByteString
bs of
    Just SignKeyVRF v
sk -> forall (m :: * -> *) a. Monad m => a -> m a
return SignKeyVRF v
sk
    Maybe (SignKeyVRF v)
Nothing
      | Int
actual forall a. Eq a => a -> a -> Bool
/= Int
expected ->
          forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail
            ( [Char]
"decodeSignKeyVRF: wrong length, expected "
                forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
expected
                forall a. [a] -> [a] -> [a]
++ [Char]
" bytes but got "
                forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
actual
            )
      | Bool
otherwise -> forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"decodeSignKeyVRF: cannot decode key"
      where
        expected :: Int
expected = forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall v (proxy :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeSignKeyVRF (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 :: forall v s. VRFAlgorithm v => Decoder s (CertVRF v)
decodeCertVRF = do
  ByteString
bs <- forall s. Decoder s ByteString
decodeBytes
  case forall v. VRFAlgorithm v => ByteString -> Maybe (CertVRF v)
rawDeserialiseCertVRF ByteString
bs of
    Just CertVRF v
crt -> forall (m :: * -> *) a. Monad m => a -> m a
return CertVRF v
crt
    Maybe (CertVRF v)
Nothing
      | Int
actual forall a. Eq a => a -> a -> Bool
/= Int
expected ->
          forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail
            ( [Char]
"decodeCertVRF: wrong length, expected "
                forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
expected
                forall a. [a] -> [a] -> [a]
++ [Char]
" bytes but got "
                forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
actual
            )
      | Bool
otherwise -> forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"decodeCertVRF: cannot decode key"
      where
        expected :: Int
expected = forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall v (proxy :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeCertVRF (forall {k} (t :: k). Proxy t
Proxy :: Proxy v))
        actual :: Int
actual = ByteString -> Int
BS.length ByteString
bs
{-# INLINEABLE decodeCertVRF #-}

data CertifiedVRF v a
  = CertifiedVRF
  { forall v a. CertifiedVRF v a -> OutputVRF v
certifiedOutput :: !(OutputVRF v)
  , forall v a. CertifiedVRF v a -> CertVRF v
certifiedProof :: !(CertVRF v)
  }
  deriving (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
      forall a. Semigroup a => a -> a -> a
<> forall a. ToCBOR a => a -> Encoding
toCBOR (forall v a. CertifiedVRF v a -> OutputVRF v
certifiedOutput CertifiedVRF v a
cvrf)
      forall a. Semigroup a => a -> a -> a
<> forall v. VRFAlgorithm v => CertVRF v -> Encoding
encodeCertVRF (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
      forall a. Num a => a -> a -> a
+ Proxy (OutputVRF v) -> Size
certifiedOutputSize (forall v a. CertifiedVRF v a -> OutputVRF v
certifiedOutput forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (CertifiedVRF v a)
proxy)
      forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall v (proxy :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeCertVRF (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 =
        forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall v (proxy :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeOutputVRF (forall {k} (t :: k). Proxy t
Proxy :: Proxy v)

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

evalCertified ::
  (VRFAlgorithm v, Signable v a) =>
  ContextVRF v ->
  a ->
  SignKeyVRF v ->
  CertifiedVRF v a
evalCertified :: forall v a.
(VRFAlgorithm v, Signable v a) =>
ContextVRF v -> a -> SignKeyVRF v -> CertifiedVRF v a
evalCertified ContextVRF v
ctxt a
a SignKeyVRF v
key = forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall v a. OutputVRF v -> CertVRF v -> CertifiedVRF v a
CertifiedVRF forall a b. (a -> b) -> a -> b
$ 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 :: forall v a.
(VRFAlgorithm v, Signable v a) =>
ContextVRF v -> VerKeyVRF v -> a -> CertifiedVRF v a -> Bool
verifyCertified ContextVRF v
ctxt VerKeyVRF v
vk a
a CertifiedVRF {OutputVRF v
certifiedOutput :: OutputVRF v
certifiedOutput :: forall v a. CertifiedVRF v a -> OutputVRF v
certifiedOutput, CertVRF v
certifiedProof :: CertVRF v
certifiedProof :: forall v a. CertifiedVRF v a -> CertVRF v
certifiedProof} =
  case forall v a.
(VRFAlgorithm v, HasCallStack, Signable v a) =>
ContextVRF v
-> VerKeyVRF v -> a -> CertVRF v -> Maybe (OutputVRF v)
verifyVRF ContextVRF v
ctxt VerKeyVRF v
vk a
a CertVRF v
certifiedProof of
    Maybe (OutputVRF v)
Nothing -> Bool
False
    Just OutputVRF v
output -> OutputVRF v
output forall a. Eq a => a -> a -> Bool
== OutputVRF v
certifiedOutput

--
-- '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 :: forall v. VRFAlgorithm v => Proxy (VerKeyVRF v) -> Size
encodedVerKeyVRFSizeExpr Proxy (VerKeyVRF v)
_proxy =
  -- 'encodeBytes' envelope
  forall a b. (Integral a, Num b) => a -> b
fromIntegral ((forall s a. (Integral s, Integral a) => s -> a
withWordSize :: Word -> Integer) (forall v (proxy :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeVerKeyVRF (forall {k} (t :: k). Proxy t
Proxy :: Proxy v)))
    -- payload
    forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall v (proxy :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeVerKeyVRF (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 :: forall v. VRFAlgorithm v => Proxy (SignKeyVRF v) -> Size
encodedSignKeyVRFSizeExpr Proxy (SignKeyVRF v)
_proxy =
  -- 'encodeBytes' envelope
  forall a b. (Integral a, Num b) => a -> b
fromIntegral ((forall s a. (Integral s, Integral a) => s -> a
withWordSize :: Word -> Integer) (forall v (proxy :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeSignKeyVRF (forall {k} (t :: k). Proxy t
Proxy :: Proxy v)))
    -- payload
    forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall v (proxy :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeSignKeyVRF (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 :: forall v. VRFAlgorithm v => Proxy (CertVRF v) -> Size
encodedCertVRFSizeExpr Proxy (CertVRF v)
_proxy =
  -- 'encodeBytes' envelope
  forall a b. (Integral a, Num b) => a -> b
fromIntegral ((forall s a. (Integral s, Integral a) => s -> a
withWordSize :: Word -> Integer) (forall v (proxy :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeCertVRF (forall {k} (t :: k). Proxy t
Proxy :: Proxy v)))
    -- payload
    forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall v (proxy :: * -> *). VRFAlgorithm v => proxy v -> Word
sizeCertVRF (forall {k} (t :: k). Proxy t
Proxy :: Proxy v))