{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

-- | Verifiable Random Function (VRF) implemented as FFI wrappers around the
-- implementation in https://github.com/input-output-hk/libsodium
module Cardano.Crypto.VRF.PraosBatchCompat (
  -- * VRFAlgorithm API
  PraosBatchCompatVRF,

  -- * Low-level size specifiers

  --
  -- Sizes of various value types involved in the VRF calculations. Users of
  -- this module will not need these, we are only exporting them for unit
  -- testing purposes.
  crypto_vrf_ietfdraft13_bytes_batchcompat,
  crypto_vrf_ietfdraft13_publickeybytes,
  crypto_vrf_ietfdraft13_secretkeybytes,
  crypto_vrf_ietfdraft13_seedbytes,
  crypto_vrf_ietfdraft13_outputbytes,
  io_crypto_vrf_ietfdraft13_publickeybytes,
  io_crypto_vrf_ietfdraft13_secretkeybytes,

  -- * Key sizes
  certSizeVRF,
  signKeySizeVRF,
  verKeySizeVRF,
  vrfKeySizeVRF,

  -- * Seed and key generation
  Seed,
  genSeed,
  keypairFromSeed,

  -- * Conversions
  unsafeRawSeed,
  outputBytes,
  outputFromBytes,
  outputFromProof,
  proofBytes,
  proofFromBytes,
  skBytes,
  skFromBytes,
  vkBytes,
  vkFromBytes,
  skToVerKey,
  skToSeed,

  -- * Core VRF operations
  prove,
  verify,
  SignKeyVRF (..),
  VerKeyVRF (..),
  CertVRF (..),
  Proof,
  Output,
)
where

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

import Cardano.Crypto.RandomBytes (randombytes_buf)
import Cardano.Crypto.Seed (getBytesFromSeedT)
import Cardano.Crypto.Util (SignableRepresentation (..))
import Cardano.Crypto.VRF.Class

import Control.DeepSeq (NFData (..))
import Control.Monad (void)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.Coerce (coerce)
import Data.Maybe (fromMaybe)
import Data.Proxy (Proxy (..))
import Foreign.C.Types
import Foreign.ForeignPtr
import Foreign.Marshal.Alloc
import Foreign.Marshal.Utils
import Foreign.Ptr
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks, OnlyCheckWhnf (..), OnlyCheckWhnfNamed (..))
import System.IO.Unsafe (unsafePerformIO)

-- Value types.
--
-- These are all transparent to the Haskell side of things, all we ever do
-- with these is pass pointers to them around. We don't want to know anything
-- about them, hence, we make them uninhabited.
--
-- The actual values are kept entirely in C memory, allocated when a value is
-- created, and freed when the value's finalizer runs.
--
-- The reason we have them at all, rather than duplicating C's void pointers,
-- is because we want to distinguish them at the type level.

data SeedValue
data SignKeyValue
data VerKeyValue
data ProofValue
data OutputValue

-- Type aliases for raw pointers
--
-- These will not leave this module, they are only here for our convenience,
-- so we can afford to not newtype them.

type SeedPtr = Ptr SeedValue
type SignKeyPtr = Ptr SignKeyValue
type VerKeyPtr = Ptr VerKeyValue
type ProofPtr = Ptr ProofValue
type OutputPtr = Ptr OutputValue

-- The exported (via the 'VRFAlgorithm' typeclass) types.
--
-- These are wrappers around 'ForeignPtr's; we don't export the constructors,
-- so callers have to go through our blessed API to create any of them. This
-- way we can make sure that we always allocate the correct sizes, and attach
-- finalizers that automatically free the memory for us.

-- | A random seed, used to derive a key pair.
newtype Seed = Seed {Seed -> ForeignPtr SeedValue
unSeed :: ForeignPtr SeedValue}
  deriving (Context -> Seed -> IO (Maybe ThunkInfo)
Proxy Seed -> [Char]
(Context -> Seed -> IO (Maybe ThunkInfo))
-> (Context -> Seed -> IO (Maybe ThunkInfo))
-> (Proxy Seed -> [Char])
-> NoThunks Seed
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> [Char])
-> NoThunks a
$cnoThunks :: Context -> Seed -> IO (Maybe ThunkInfo)
noThunks :: Context -> Seed -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> Seed -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> Seed -> IO (Maybe ThunkInfo)
$cshowTypeOf :: Proxy Seed -> [Char]
showTypeOf :: Proxy Seed -> [Char]
NoThunks) via OnlyCheckWhnf Seed

-- | Signing key. In this implementation, the signing key is actually a 64-byte
-- value that contains both the 32-byte signing key and the corresponding
-- 32-byte verification key.
newtype SignKey = SignKey {SignKey -> ForeignPtr SignKeyValue
unSignKey :: ForeignPtr SignKeyValue}
  deriving ((forall x. SignKey -> Rep SignKey x)
-> (forall x. Rep SignKey x -> SignKey) -> Generic SignKey
forall x. Rep SignKey x -> SignKey
forall x. SignKey -> Rep SignKey x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. SignKey -> Rep SignKey x
from :: forall x. SignKey -> Rep SignKey x
$cto :: forall x. Rep SignKey x -> SignKey
to :: forall x. Rep SignKey x -> SignKey
Generic)
  deriving (Context -> SignKey -> IO (Maybe ThunkInfo)
Proxy SignKey -> [Char]
(Context -> SignKey -> IO (Maybe ThunkInfo))
-> (Context -> SignKey -> IO (Maybe ThunkInfo))
-> (Proxy SignKey -> [Char])
-> NoThunks SignKey
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> [Char])
-> NoThunks a
$cnoThunks :: Context -> SignKey -> IO (Maybe ThunkInfo)
noThunks :: Context -> SignKey -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> SignKey -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> SignKey -> IO (Maybe ThunkInfo)
$cshowTypeOf :: Proxy SignKey -> [Char]
showTypeOf :: Proxy SignKey -> [Char]
NoThunks) via OnlyCheckWhnf SignKey

instance NFData SignKey where
  rnf :: SignKey -> ()
rnf SignKey
a = SignKey -> () -> ()
forall a b. a -> b -> b
seq SignKey
a ()

-- | Verification key.
newtype VerKey = VerKey {VerKey -> ForeignPtr VerKeyValue
unVerKey :: ForeignPtr VerKeyValue}
  deriving ((forall x. VerKey -> Rep VerKey x)
-> (forall x. Rep VerKey x -> VerKey) -> Generic VerKey
forall x. Rep VerKey x -> VerKey
forall x. VerKey -> Rep VerKey x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. VerKey -> Rep VerKey x
from :: forall x. VerKey -> Rep VerKey x
$cto :: forall x. Rep VerKey x -> VerKey
to :: forall x. Rep VerKey x -> VerKey
Generic)
  deriving (Context -> VerKey -> IO (Maybe ThunkInfo)
Proxy VerKey -> [Char]
(Context -> VerKey -> IO (Maybe ThunkInfo))
-> (Context -> VerKey -> IO (Maybe ThunkInfo))
-> (Proxy VerKey -> [Char])
-> NoThunks VerKey
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> [Char])
-> NoThunks a
$cnoThunks :: Context -> VerKey -> IO (Maybe ThunkInfo)
noThunks :: Context -> VerKey -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> VerKey -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> VerKey -> IO (Maybe ThunkInfo)
$cshowTypeOf :: Proxy VerKey -> [Char]
showTypeOf :: Proxy VerKey -> [Char]
NoThunks) via OnlyCheckWhnf VerKey

instance NFData VerKey where
  rnf :: VerKey -> ()
rnf VerKey
a = VerKey -> () -> ()
forall a b. a -> b -> b
seq VerKey
a ()

-- | A proof, as constructed by the 'prove' function.
newtype Proof = Proof {Proof -> ForeignPtr ProofValue
unProof :: ForeignPtr ProofValue}
  deriving ((forall x. Proof -> Rep Proof x)
-> (forall x. Rep Proof x -> Proof) -> Generic Proof
forall x. Rep Proof x -> Proof
forall x. Proof -> Rep Proof x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Proof -> Rep Proof x
from :: forall x. Proof -> Rep Proof x
$cto :: forall x. Rep Proof x -> Proof
to :: forall x. Rep Proof x -> Proof
Generic)
  deriving (Context -> Proof -> IO (Maybe ThunkInfo)
Proxy Proof -> [Char]
(Context -> Proof -> IO (Maybe ThunkInfo))
-> (Context -> Proof -> IO (Maybe ThunkInfo))
-> (Proxy Proof -> [Char])
-> NoThunks Proof
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> [Char])
-> NoThunks a
$cnoThunks :: Context -> Proof -> IO (Maybe ThunkInfo)
noThunks :: Context -> Proof -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> Proof -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> Proof -> IO (Maybe ThunkInfo)
$cshowTypeOf :: Proxy Proof -> [Char]
showTypeOf :: Proxy Proof -> [Char]
NoThunks) via OnlyCheckWhnf Proof

instance NFData Proof where
  rnf :: Proof -> ()
rnf Proof
a = Proof -> () -> ()
forall a b. a -> b -> b
seq Proof
a ()

-- | Hashed output of a proof verification, as returned by the 'verify'
-- function.
newtype Output = Output {Output -> ForeignPtr OutputValue
unOutput :: ForeignPtr OutputValue}
  deriving ((forall x. Output -> Rep Output x)
-> (forall x. Rep Output x -> Output) -> Generic Output
forall x. Rep Output x -> Output
forall x. Output -> Rep Output x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Output -> Rep Output x
from :: forall x. Output -> Rep Output x
$cto :: forall x. Rep Output x -> Output
to :: forall x. Rep Output x -> Output
Generic)
  deriving (Context -> Output -> IO (Maybe ThunkInfo)
Proxy Output -> [Char]
(Context -> Output -> IO (Maybe ThunkInfo))
-> (Context -> Output -> IO (Maybe ThunkInfo))
-> (Proxy Output -> [Char])
-> NoThunks Output
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> [Char])
-> NoThunks a
$cnoThunks :: Context -> Output -> IO (Maybe ThunkInfo)
noThunks :: Context -> Output -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> Output -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> Output -> IO (Maybe ThunkInfo)
$cshowTypeOf :: Proxy Output -> [Char]
showTypeOf :: Proxy Output -> [Char]
NoThunks) via OnlyCheckWhnf Output

-- Raw low-level FFI bindings.
--
foreign import ccall "crypto_vrf_ietfdraft13_bytes_batchcompat"
  crypto_vrf_ietfdraft13_bytes_batchcompat :: CSize
foreign import ccall "crypto_vrf_ietfdraft13_publickeybytes"
  crypto_vrf_ietfdraft13_publickeybytes :: CSize
foreign import ccall "crypto_vrf_ietfdraft13_secretkeybytes"
  crypto_vrf_ietfdraft13_secretkeybytes :: CSize
foreign import ccall "crypto_vrf_ietfdraft13_seedbytes"
  crypto_vrf_ietfdraft13_seedbytes :: CSize
foreign import ccall "crypto_vrf_ietfdraft13_outputbytes"
  crypto_vrf_ietfdraft13_outputbytes :: CSize

foreign import ccall "crypto_vrf_ietfdraft13_publickeybytes"
  io_crypto_vrf_ietfdraft13_publickeybytes :: IO CSize
foreign import ccall "crypto_vrf_ietfdraft13_secretkeybytes"
  io_crypto_vrf_ietfdraft13_secretkeybytes :: IO CSize

foreign import ccall "crypto_vrf_seed_keypair"
  crypto_vrf_ietfdraft13_keypair_from_seed :: VerKeyPtr -> SignKeyPtr -> SeedPtr -> IO CInt
foreign import ccall "crypto_vrf_sk_to_pk"
  crypto_vrf_ietfdraft13_sk_to_pk :: VerKeyPtr -> SignKeyPtr -> IO CInt
foreign import ccall "crypto_vrf_sk_to_seed"
  crypto_vrf_ietfdraft13_sk_to_seed :: SeedPtr -> SignKeyPtr -> IO CInt
foreign import ccall "crypto_vrf_ietfdraft13_prove_batchcompat"
  crypto_vrf_ietfdraft13_prove_batchcompat ::
    ProofPtr -> SignKeyPtr -> Ptr CChar -> CULLong -> IO CInt
foreign import ccall "crypto_vrf_ietfdraft13_verify_batchcompat"
  crypto_vrf_ietfdraft13_verify_batchcompat ::
    OutputPtr -> VerKeyPtr -> ProofPtr -> Ptr CChar -> CULLong -> IO CInt

foreign import ccall "crypto_vrf_ietfdraft13_proof_to_hash_batchcompat"
  crypto_vrf_ietfdraft13_proof_to_hash_batchcompat :: OutputPtr -> ProofPtr -> IO CInt

-- Key size constants

certSizeVRF :: Int
certSizeVRF :: Int
certSizeVRF = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CSize -> Int) -> CSize -> Int
forall a b. (a -> b) -> a -> b
$! CSize
crypto_vrf_ietfdraft13_bytes_batchcompat

signKeySizeVRF :: Int
signKeySizeVRF :: Int
signKeySizeVRF = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CSize -> Int) -> CSize -> Int
forall a b. (a -> b) -> a -> b
$! CSize
crypto_vrf_ietfdraft13_secretkeybytes

verKeySizeVRF :: Int
verKeySizeVRF :: Int
verKeySizeVRF = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CSize -> Int) -> CSize -> Int
forall a b. (a -> b) -> a -> b
$! CSize
crypto_vrf_ietfdraft13_publickeybytes

vrfKeySizeVRF :: Int
vrfKeySizeVRF :: Int
vrfKeySizeVRF = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CSize -> Int) -> CSize -> Int
forall a b. (a -> b) -> a -> b
$! CSize
crypto_vrf_ietfdraft13_outputbytes

ioSignKeySizeVRF :: IO Int
ioSignKeySizeVRF :: IO Int
ioSignKeySizeVRF = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CSize -> Int) -> IO CSize -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO CSize
io_crypto_vrf_ietfdraft13_secretkeybytes

ioVerKeySizeVRF :: IO Int
ioVerKeySizeVRF :: IO Int
ioVerKeySizeVRF = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CSize -> Int) -> IO CSize -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO CSize
io_crypto_vrf_ietfdraft13_publickeybytes

-- | Allocate a 'Seed' and attach a finalizer. The allocated memory will not be initialized.
mkSeed :: IO Seed
mkSeed :: IO Seed
mkSeed = do
  Ptr SeedValue
ptr <- Int -> IO (Ptr SeedValue)
forall a. Int -> IO (Ptr a)
mallocBytes (CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
crypto_vrf_ietfdraft13_seedbytes)
  ForeignPtr SeedValue -> Seed
Seed (ForeignPtr SeedValue -> Seed)
-> IO (ForeignPtr SeedValue) -> IO Seed
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FinalizerPtr SeedValue
-> Ptr SeedValue -> IO (ForeignPtr SeedValue)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr SeedValue
forall a. FinalizerPtr a
finalizerFree Ptr SeedValue
ptr

-- | Generate a random seed.
-- Uses 'randombytes_buf' to create random data.
--
-- This function provides an alternative way of generating seeds specifically
-- for the 'PraosVRF' algorithm. Unlike the 'genKeyPairVRF' method, which uses
-- a 'ByteString'-based 'Cardano.Crypto.Seed.Seed', this seed generation method
-- bypasses the GHC heap, keeping the seed in C-allocated memory instead.
--
-- This provides two advantages:
-- 1. It avoids the overhead of unnecessary GHC-side heap allocations.
-- 2. It avoids leaking the seed via the GHC heap; the 'Seed' type itself
--    takes care of zeroing out its memory upon finalization.
genSeed :: IO Seed
genSeed :: IO Seed
genSeed = do
  Seed
seed <- IO Seed
mkSeed
  ForeignPtr SeedValue -> (Ptr SeedValue -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Seed -> ForeignPtr SeedValue
unSeed Seed
seed) ((Ptr SeedValue -> IO ()) -> IO ())
-> (Ptr SeedValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr SeedValue
ptr ->
    Ptr SeedValue -> CSize -> IO ()
forall a. Ptr a -> CSize -> IO ()
randombytes_buf Ptr SeedValue
ptr CSize
crypto_vrf_ietfdraft13_seedbytes
  Seed -> IO Seed
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Seed
seed

copyFromByteString :: Ptr a -> ByteString -> Int -> IO ()
copyFromByteString :: forall a. Ptr a -> ByteString -> Int -> IO ()
copyFromByteString Ptr a
ptr ByteString
bs Int
lenExpected =
  ByteString -> (CStringLen -> IO ()) -> IO ()
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.useAsCStringLen ByteString
bs ((CStringLen -> IO ()) -> IO ()) -> (CStringLen -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
cstr, Int
lenActual) ->
    if Int
lenActual Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
lenExpected
      then
        Ptr CChar -> Ptr CChar -> Int -> IO ()
forall a. Ptr a -> Ptr a -> Int -> IO ()
copyBytes (Ptr a -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Ptr CChar
cstr Int
lenExpected
      else
        [Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$
          [Char]
"Invalid input size, expected at least " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> Int -> [Char]
forall a. Show a => a -> [Char]
show Int
lenExpected [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
", but got " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> Int -> [Char]
forall a. Show a => a -> [Char]
show Int
lenActual

seedFromBytes :: ByteString -> Seed
seedFromBytes :: ByteString -> Seed
seedFromBytes ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
crypto_vrf_ietfdraft13_seedbytes =
      [Char] -> Seed
forall a. HasCallStack => [Char] -> a
error ([Char] -> Seed) -> [Char] -> Seed
forall a b. (a -> b) -> a -> b
$ [Char]
"Expected " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ CSize -> [Char]
forall a. Show a => a -> [Char]
show CSize
crypto_vrf_ietfdraft13_seedbytes [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" bytes"
seedFromBytes ByteString
bs = IO Seed -> Seed
forall a. IO a -> a
unsafePerformIO (IO Seed -> Seed) -> IO Seed -> Seed
forall a b. (a -> b) -> a -> b
$ do
  Seed
seed <- IO Seed
mkSeed
  ForeignPtr SeedValue -> (Ptr SeedValue -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Seed -> ForeignPtr SeedValue
unSeed Seed
seed) ((Ptr SeedValue -> IO ()) -> IO ())
-> (Ptr SeedValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr SeedValue
ptr ->
    Ptr SeedValue -> ByteString -> Int -> IO ()
forall a. Ptr a -> ByteString -> Int -> IO ()
copyFromByteString Ptr SeedValue
ptr ByteString
bs (CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
crypto_vrf_ietfdraft13_seedbytes)
  Seed -> IO Seed
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Seed
seed

-- | Convert an opaque 'Seed' into a 'ByteString' that we can inspect.
-- Note that this will copy the seed into RTS-managed memory; this is not
-- currently a problem, but if at any point we decide that we want to make
-- sure the seed is properly mlocked, then this function will leak such a
-- secured seed into non-locked (swappable) memory.
unsafeRawSeed :: Seed -> IO ByteString
unsafeRawSeed :: Seed -> IO ByteString
unsafeRawSeed (Seed ForeignPtr SeedValue
fp) = ForeignPtr SeedValue
-> (Ptr SeedValue -> IO ByteString) -> IO ByteString
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr SeedValue
fp ((Ptr SeedValue -> IO ByteString) -> IO ByteString)
-> (Ptr SeedValue -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr SeedValue
ptr ->
  CStringLen -> IO ByteString
BS.packCStringLen (Ptr SeedValue -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr Ptr SeedValue
ptr, CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
crypto_vrf_ietfdraft13_seedbytes)

-- | Convert a proof verification output hash into a 'ByteString' that we can
-- inspect.
outputBytes :: Output -> ByteString
outputBytes :: Output -> ByteString
outputBytes (Output ForeignPtr OutputValue
op) = IO ByteString -> ByteString
forall a. IO a -> a
unsafePerformIO (IO ByteString -> ByteString) -> IO ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ ForeignPtr OutputValue
-> (Ptr OutputValue -> IO ByteString) -> IO ByteString
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr OutputValue
op ((Ptr OutputValue -> IO ByteString) -> IO ByteString)
-> (Ptr OutputValue -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr OutputValue
ptr ->
  CStringLen -> IO ByteString
BS.packCStringLen (Ptr OutputValue -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr Ptr OutputValue
ptr, CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
crypto_vrf_ietfdraft13_outputbytes)

-- | Convert a proof into a 'ByteString' that we can inspect.
proofBytes :: Proof -> ByteString
proofBytes :: Proof -> ByteString
proofBytes (Proof ForeignPtr ProofValue
op) = IO ByteString -> ByteString
forall a. IO a -> a
unsafePerformIO (IO ByteString -> ByteString) -> IO ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ ForeignPtr ProofValue
-> (Ptr ProofValue -> IO ByteString) -> IO ByteString
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr ProofValue
op ((Ptr ProofValue -> IO ByteString) -> IO ByteString)
-> (Ptr ProofValue -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr ProofValue
ptr ->
  CStringLen -> IO ByteString
BS.packCStringLen (Ptr ProofValue -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr Ptr ProofValue
ptr, Int
certSizeVRF)

-- | Convert a verification key into a 'ByteString' that we can inspect.
vkBytes :: VerKey -> ByteString
vkBytes :: VerKey -> ByteString
vkBytes (VerKey ForeignPtr VerKeyValue
op) = IO ByteString -> ByteString
forall a. IO a -> a
unsafePerformIO (IO ByteString -> ByteString) -> IO ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ ForeignPtr VerKeyValue
-> (Ptr VerKeyValue -> IO ByteString) -> IO ByteString
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr VerKeyValue
op ((Ptr VerKeyValue -> IO ByteString) -> IO ByteString)
-> (Ptr VerKeyValue -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr VerKeyValue
ptr ->
  CStringLen -> IO ByteString
BS.packCStringLen (Ptr VerKeyValue -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr Ptr VerKeyValue
ptr, Int
verKeySizeVRF)

-- | Convert a signing key into a 'ByteString' that we can inspect.
skBytes :: SignKey -> ByteString
skBytes :: SignKey -> ByteString
skBytes (SignKey ForeignPtr SignKeyValue
op) = IO ByteString -> ByteString
forall a. IO a -> a
unsafePerformIO (IO ByteString -> ByteString) -> IO ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ ForeignPtr SignKeyValue
-> (Ptr SignKeyValue -> IO ByteString) -> IO ByteString
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr SignKeyValue
op ((Ptr SignKeyValue -> IO ByteString) -> IO ByteString)
-> (Ptr SignKeyValue -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr SignKeyValue
ptr ->
  CStringLen -> IO ByteString
BS.packCStringLen (Ptr SignKeyValue -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr Ptr SignKeyValue
ptr, Int
signKeySizeVRF)

instance Show Proof where
  show :: Proof -> [Char]
show = ByteString -> [Char]
forall a. Show a => a -> [Char]
show (ByteString -> [Char]) -> (Proof -> ByteString) -> Proof -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proof -> ByteString
proofBytes

instance Eq Proof where
  Proof
a == :: Proof -> Proof -> Bool
== Proof
b = Proof -> ByteString
proofBytes Proof
a ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== Proof -> ByteString
proofBytes Proof
b

instance ToCBOR Proof where
  toCBOR :: Proof -> Encoding
toCBOR = ByteString -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (ByteString -> Encoding)
-> (Proof -> ByteString) -> Proof -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proof -> ByteString
proofBytes
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy Proof -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_ Proxy Proof
_ =
    (forall t. ToCBOR t => Proxy t -> Size) -> Proxy ByteString -> Size
forall a.
ToCBOR a =>
(forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size
encodedSizeExpr (\Proxy t
_ -> Int -> Size
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
certSizeVRF) (Proxy ByteString
forall {k} (t :: k). Proxy t
Proxy :: Proxy ByteString)

instance FromCBOR Proof where
  fromCBOR :: forall s. Decoder s Proof
fromCBOR = ByteString -> Proof
proofFromBytes (ByteString -> Proof) -> Decoder s ByteString -> Decoder s Proof
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s ByteString
forall s. Decoder s ByteString
forall a s. FromCBOR a => Decoder s a
fromCBOR

instance Show SignKey where
  show :: SignKey -> [Char]
show = ByteString -> [Char]
forall a. Show a => a -> [Char]
show (ByteString -> [Char])
-> (SignKey -> ByteString) -> SignKey -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SignKey -> ByteString
skBytes

instance Eq SignKey where
  SignKey
a == :: SignKey -> SignKey -> Bool
== SignKey
b = SignKey -> ByteString
skBytes SignKey
a ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== SignKey -> ByteString
skBytes SignKey
b

instance ToCBOR SignKey where
  toCBOR :: SignKey -> Encoding
toCBOR = ByteString -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (ByteString -> Encoding)
-> (SignKey -> ByteString) -> SignKey -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SignKey -> ByteString
skBytes
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy SignKey -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_ Proxy SignKey
_ =
    (forall t. ToCBOR t => Proxy t -> Size) -> Proxy ByteString -> Size
forall a.
ToCBOR a =>
(forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size
encodedSizeExpr (\Proxy t
_ -> Int -> Size
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
signKeySizeVRF) (Proxy ByteString
forall {k} (t :: k). Proxy t
Proxy :: Proxy ByteString)

instance FromCBOR SignKey where
  fromCBOR :: forall s. Decoder s SignKey
fromCBOR = ByteString -> SignKey
skFromBytes (ByteString -> SignKey)
-> Decoder s ByteString -> Decoder s SignKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s ByteString
forall s. Decoder s ByteString
forall a s. FromCBOR a => Decoder s a
fromCBOR

instance Show VerKey where
  show :: VerKey -> [Char]
show = ByteString -> [Char]
forall a. Show a => a -> [Char]
show (ByteString -> [Char])
-> (VerKey -> ByteString) -> VerKey -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VerKey -> ByteString
vkBytes

instance Eq VerKey where
  VerKey
a == :: VerKey -> VerKey -> Bool
== VerKey
b = VerKey -> ByteString
vkBytes VerKey
a ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== VerKey -> ByteString
vkBytes VerKey
b

instance ToCBOR VerKey where
  toCBOR :: VerKey -> Encoding
toCBOR = ByteString -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (ByteString -> Encoding)
-> (VerKey -> ByteString) -> VerKey -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VerKey -> ByteString
vkBytes
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy VerKey -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_ Proxy VerKey
_ =
    (forall t. ToCBOR t => Proxy t -> Size) -> Proxy ByteString -> Size
forall a.
ToCBOR a =>
(forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size
encodedSizeExpr (\Proxy t
_ -> Int -> Size
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
verKeySizeVRF) (Proxy ByteString
forall {k} (t :: k). Proxy t
Proxy :: Proxy ByteString)

instance FromCBOR VerKey where
  fromCBOR :: forall s. Decoder s VerKey
fromCBOR = ByteString -> VerKey
vkFromBytes (ByteString -> VerKey) -> Decoder s ByteString -> Decoder s VerKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s ByteString
forall s. Decoder s ByteString
forall a s. FromCBOR a => Decoder s a
fromCBOR

-- | Allocate a Verification Key and attach a finalizer. The allocated memory will
-- not be initialized.
mkVerKey :: IO VerKey
mkVerKey :: IO VerKey
mkVerKey = (ForeignPtr VerKeyValue -> VerKey)
-> IO (ForeignPtr VerKeyValue) -> IO VerKey
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ForeignPtr VerKeyValue -> VerKey
VerKey (IO (ForeignPtr VerKeyValue) -> IO VerKey)
-> IO (ForeignPtr VerKeyValue) -> IO VerKey
forall a b. (a -> b) -> a -> b
$ FinalizerPtr VerKeyValue
-> Ptr VerKeyValue -> IO (ForeignPtr VerKeyValue)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr VerKeyValue
forall a. FinalizerPtr a
finalizerFree (Ptr VerKeyValue -> IO (ForeignPtr VerKeyValue))
-> IO (Ptr VerKeyValue) -> IO (ForeignPtr VerKeyValue)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Int -> IO (Ptr VerKeyValue)
forall a. Int -> IO (Ptr a)
mallocBytes Int
verKeySizeVRF

-- | Allocate a Signing Key and attach a finalizer. The allocated memory will
-- not be initialized.
mkSignKey :: IO SignKey
mkSignKey :: IO SignKey
mkSignKey = (ForeignPtr SignKeyValue -> SignKey)
-> IO (ForeignPtr SignKeyValue) -> IO SignKey
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ForeignPtr SignKeyValue -> SignKey
SignKey (IO (ForeignPtr SignKeyValue) -> IO SignKey)
-> IO (ForeignPtr SignKeyValue) -> IO SignKey
forall a b. (a -> b) -> a -> b
$ FinalizerPtr SignKeyValue
-> Ptr SignKeyValue -> IO (ForeignPtr SignKeyValue)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr SignKeyValue
forall a. FinalizerPtr a
finalizerFree (Ptr SignKeyValue -> IO (ForeignPtr SignKeyValue))
-> IO (Ptr SignKeyValue) -> IO (ForeignPtr SignKeyValue)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Int -> IO (Ptr SignKeyValue)
forall a. Int -> IO (Ptr a)
mallocBytes Int
signKeySizeVRF

-- | Allocate a Proof and attach a finalizer. The allocated memory will
-- not be initialized.
mkProof :: IO Proof
mkProof :: IO Proof
mkProof = (ForeignPtr ProofValue -> Proof)
-> IO (ForeignPtr ProofValue) -> IO Proof
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ForeignPtr ProofValue -> Proof
Proof (IO (ForeignPtr ProofValue) -> IO Proof)
-> IO (ForeignPtr ProofValue) -> IO Proof
forall a b. (a -> b) -> a -> b
$ FinalizerPtr ProofValue
-> Ptr ProofValue -> IO (ForeignPtr ProofValue)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr ProofValue
forall a. FinalizerPtr a
finalizerFree (Ptr ProofValue -> IO (ForeignPtr ProofValue))
-> IO (Ptr ProofValue) -> IO (ForeignPtr ProofValue)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Int -> IO (Ptr ProofValue)
forall a. Int -> IO (Ptr a)
mallocBytes Int
certSizeVRF

proofFromBytes :: ByteString -> Proof
proofFromBytes :: ByteString -> Proof
proofFromBytes ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
certSizeVRF =
      [Char] -> Proof
forall a. HasCallStack => [Char] -> a
error [Char]
"Invalid proof length"
  | Bool
otherwise =
      IO Proof -> Proof
forall a. IO a -> a
unsafePerformIO (IO Proof -> Proof) -> IO Proof -> Proof
forall a b. (a -> b) -> a -> b
$ do
        Proof
proof <- IO Proof
mkProof
        ForeignPtr ProofValue -> (Ptr ProofValue -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Proof -> ForeignPtr ProofValue
unProof Proof
proof) ((Ptr ProofValue -> IO ()) -> IO ())
-> (Ptr ProofValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr ProofValue
ptr ->
          Ptr ProofValue -> ByteString -> Int -> IO ()
forall a. Ptr a -> ByteString -> Int -> IO ()
copyFromByteString Ptr ProofValue
ptr ByteString
bs Int
certSizeVRF
        Proof -> IO Proof
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Proof
proof

skFromBytes :: ByteString -> SignKey
skFromBytes :: ByteString -> SignKey
skFromBytes ByteString
bs = IO SignKey -> SignKey
forall a. IO a -> a
unsafePerformIO (IO SignKey -> SignKey) -> IO SignKey -> SignKey
forall a b. (a -> b) -> a -> b
$ do
  if Int
bsLen Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
signKeySizeVRF
    then do
      Int
ioSize <- IO Int
ioSignKeySizeVRF
      [Char] -> IO SignKey
forall a. HasCallStack => [Char] -> a
error
        ( [Char]
"Invalid sk length "
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> [Char]
show @Int Int
bsLen
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
", expecting "
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> [Char]
show @Int Int
signKeySizeVRF
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
" or "
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> [Char]
show @Int Int
ioSize
        )
    else do
      SignKey
sk <- IO SignKey
mkSignKey
      ForeignPtr SignKeyValue -> (Ptr SignKeyValue -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (SignKey -> ForeignPtr SignKeyValue
unSignKey SignKey
sk) ((Ptr SignKeyValue -> IO ()) -> IO ())
-> (Ptr SignKeyValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr SignKeyValue
ptr ->
        Ptr SignKeyValue -> ByteString -> Int -> IO ()
forall a. Ptr a -> ByteString -> Int -> IO ()
copyFromByteString Ptr SignKeyValue
ptr ByteString
bs Int
signKeySizeVRF
      SignKey -> IO SignKey
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return SignKey
sk
  where
    bsLen :: Int
bsLen = ByteString -> Int
BS.length ByteString
bs

vkFromBytes :: ByteString -> VerKey
vkFromBytes :: ByteString -> VerKey
vkFromBytes ByteString
bs = IO VerKey -> VerKey
forall a. IO a -> a
unsafePerformIO (IO VerKey -> VerKey) -> IO VerKey -> VerKey
forall a b. (a -> b) -> a -> b
$ do
  if ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
verKeySizeVRF
    then do
      Int
ioSize <- IO Int
ioVerKeySizeVRF
      [Char] -> IO VerKey
forall a. HasCallStack => [Char] -> a
error
        ( [Char]
"Invalid pk length "
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> [Char]
show @Int Int
bsLen
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
", expecting "
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> [Char]
show @Int Int
verKeySizeVRF
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
" or "
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> [Char]
show @Int Int
ioSize
        )
    else do
      VerKey
pk <- IO VerKey
mkVerKey
      ForeignPtr VerKeyValue -> (Ptr VerKeyValue -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (VerKey -> ForeignPtr VerKeyValue
unVerKey VerKey
pk) ((Ptr VerKeyValue -> IO ()) -> IO ())
-> (Ptr VerKeyValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr VerKeyValue
ptr ->
        Ptr VerKeyValue -> ByteString -> Int -> IO ()
forall a. Ptr a -> ByteString -> Int -> IO ()
copyFromByteString Ptr VerKeyValue
ptr ByteString
bs Int
verKeySizeVRF
      VerKey -> IO VerKey
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return VerKey
pk
  where
    bsLen :: Int
bsLen = ByteString -> Int
BS.length ByteString
bs

-- | Allocate an Output and attach a finalizer. The allocated memory will
-- not be initialized.
mkOutput :: IO Output
mkOutput :: IO Output
mkOutput =
  (ForeignPtr OutputValue -> Output)
-> IO (ForeignPtr OutputValue) -> IO Output
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ForeignPtr OutputValue -> Output
Output (IO (ForeignPtr OutputValue) -> IO Output)
-> IO (ForeignPtr OutputValue) -> IO Output
forall a b. (a -> b) -> a -> b
$
    FinalizerPtr OutputValue
-> Ptr OutputValue -> IO (ForeignPtr OutputValue)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr OutputValue
forall a. FinalizerPtr a
finalizerFree (Ptr OutputValue -> IO (ForeignPtr OutputValue))
-> IO (Ptr OutputValue) -> IO (ForeignPtr OutputValue)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Int -> IO (Ptr OutputValue)
forall a. Int -> IO (Ptr a)
mallocBytes (CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
crypto_vrf_ietfdraft13_outputbytes)

outputFromBytes :: MonadFail m => ByteString -> m Output
outputFromBytes :: forall (m :: * -> *). MonadFail m => ByteString -> m Output
outputFromBytes ByteString
bs = do
  if Int
bsLen Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= forall a b. (Integral a, Num b) => a -> b
fromIntegral @CSize @Int CSize
crypto_vrf_ietfdraft13_outputbytes
    then
      [Char] -> m Output
forall a. [Char] -> m a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail
        ( [Char]
"Invalid output length "
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> Int -> [Char]
forall a. Show a => a -> [Char]
show Int
bsLen
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
", expecting "
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> CSize -> [Char]
forall a. Show a => a -> [Char]
show CSize
crypto_vrf_ietfdraft13_outputbytes
        )
    else Output -> m Output
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Output -> m Output) -> Output -> m Output
forall a b. (a -> b) -> a -> b
$! IO Output -> Output
forall a. IO a -> a
unsafePerformIO (IO Output -> Output) -> IO Output -> Output
forall a b. (a -> b) -> a -> b
$ do
      Output
output <- IO Output
mkOutput
      ForeignPtr OutputValue -> (Ptr OutputValue -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Output -> ForeignPtr OutputValue
unOutput Output
output) ((Ptr OutputValue -> IO ()) -> IO ())
-> (Ptr OutputValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr OutputValue
ptr ->
        Ptr OutputValue -> ByteString -> Int -> IO ()
forall a. Ptr a -> ByteString -> Int -> IO ()
copyFromByteString Ptr OutputValue
ptr ByteString
bs Int
bsLen
      Output -> IO Output
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Output
output
  where
    bsLen :: Int
bsLen = ByteString -> Int
BS.length ByteString
bs

-- | Derive a key pair (Sign + Verify) from a seed.
keypairFromSeed :: Seed -> (VerKey, SignKey)
keypairFromSeed :: Seed -> (VerKey, SignKey)
keypairFromSeed Seed
seed =
  IO (VerKey, SignKey) -> (VerKey, SignKey)
forall a. IO a -> a
unsafePerformIO (IO (VerKey, SignKey) -> (VerKey, SignKey))
-> IO (VerKey, SignKey) -> (VerKey, SignKey)
forall a b. (a -> b) -> a -> b
$ ForeignPtr SeedValue
-> (Ptr SeedValue -> IO (VerKey, SignKey)) -> IO (VerKey, SignKey)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Seed -> ForeignPtr SeedValue
unSeed Seed
seed) ((Ptr SeedValue -> IO (VerKey, SignKey)) -> IO (VerKey, SignKey))
-> (Ptr SeedValue -> IO (VerKey, SignKey)) -> IO (VerKey, SignKey)
forall a b. (a -> b) -> a -> b
$ \Ptr SeedValue
sptr -> do
    VerKey
pk <- IO VerKey
mkVerKey
    SignKey
sk <- IO SignKey
mkSignKey
    ForeignPtr VerKeyValue -> (Ptr VerKeyValue -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (VerKey -> ForeignPtr VerKeyValue
unVerKey VerKey
pk) ((Ptr VerKeyValue -> IO ()) -> IO ())
-> (Ptr VerKeyValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr VerKeyValue
pkPtr -> do
      ForeignPtr SignKeyValue -> (Ptr SignKeyValue -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (SignKey -> ForeignPtr SignKeyValue
unSignKey SignKey
sk) ((Ptr SignKeyValue -> IO ()) -> IO ())
-> (Ptr SignKeyValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr SignKeyValue
skPtr -> do
        IO CInt -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr VerKeyValue -> Ptr SignKeyValue -> Ptr SeedValue -> IO CInt
crypto_vrf_ietfdraft13_keypair_from_seed Ptr VerKeyValue
pkPtr Ptr SignKeyValue
skPtr Ptr SeedValue
sptr
    (VerKey, SignKey) -> IO (VerKey, SignKey)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ((VerKey, SignKey) -> IO (VerKey, SignKey))
-> (VerKey, SignKey) -> IO (VerKey, SignKey)
forall a b. (a -> b) -> a -> b
$ VerKey
pk VerKey -> (VerKey, SignKey) -> (VerKey, SignKey)
forall a b. a -> b -> b
`seq` SignKey
sk SignKey -> (VerKey, SignKey) -> (VerKey, SignKey)
forall a b. a -> b -> b
`seq` (VerKey
pk, SignKey
sk)

-- | Derive a Verification Key from a Signing Key.
skToVerKey :: SignKey -> VerKey
skToVerKey :: SignKey -> VerKey
skToVerKey SignKey
sk =
  IO VerKey -> VerKey
forall a. IO a -> a
unsafePerformIO (IO VerKey -> VerKey) -> IO VerKey -> VerKey
forall a b. (a -> b) -> a -> b
$ ForeignPtr SignKeyValue
-> (Ptr SignKeyValue -> IO VerKey) -> IO VerKey
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (SignKey -> ForeignPtr SignKeyValue
unSignKey SignKey
sk) ((Ptr SignKeyValue -> IO VerKey) -> IO VerKey)
-> (Ptr SignKeyValue -> IO VerKey) -> IO VerKey
forall a b. (a -> b) -> a -> b
$ \Ptr SignKeyValue
skPtr -> do
    VerKey
pk <- IO VerKey
mkVerKey
    ForeignPtr VerKeyValue -> (Ptr VerKeyValue -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (VerKey -> ForeignPtr VerKeyValue
unVerKey VerKey
pk) ((Ptr VerKeyValue -> IO ()) -> IO ())
-> (Ptr VerKeyValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr VerKeyValue
pkPtr -> do
      IO CInt -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr VerKeyValue -> Ptr SignKeyValue -> IO CInt
crypto_vrf_ietfdraft13_sk_to_pk Ptr VerKeyValue
pkPtr Ptr SignKeyValue
skPtr
    VerKey -> IO VerKey
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return VerKey
pk

-- | Get the seed used to generate a given Signing Key
skToSeed :: SignKey -> Seed
skToSeed :: SignKey -> Seed
skToSeed SignKey
sk =
  IO Seed -> Seed
forall a. IO a -> a
unsafePerformIO (IO Seed -> Seed) -> IO Seed -> Seed
forall a b. (a -> b) -> a -> b
$ ForeignPtr SignKeyValue -> (Ptr SignKeyValue -> IO Seed) -> IO Seed
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (SignKey -> ForeignPtr SignKeyValue
unSignKey SignKey
sk) ((Ptr SignKeyValue -> IO Seed) -> IO Seed)
-> (Ptr SignKeyValue -> IO Seed) -> IO Seed
forall a b. (a -> b) -> a -> b
$ \Ptr SignKeyValue
skPtr -> do
    Seed
seed <- IO Seed
mkSeed
    CInt
_ <- ForeignPtr SeedValue -> (Ptr SeedValue -> IO CInt) -> IO CInt
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Seed -> ForeignPtr SeedValue
unSeed Seed
seed) ((Ptr SeedValue -> IO CInt) -> IO CInt)
-> (Ptr SeedValue -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \Ptr SeedValue
seedPtr -> do
      Ptr SeedValue -> Ptr SignKeyValue -> IO CInt
crypto_vrf_ietfdraft13_sk_to_seed Ptr SeedValue
seedPtr Ptr SignKeyValue
skPtr
    Seed -> IO Seed
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Seed
seed

-- | Construct a proof from a Signing Key and a message.
-- Returns 'Just' the proof on success, 'Nothing' if the signing key could not
-- be decoded.
prove :: SignKey -> ByteString -> Maybe Proof
prove :: SignKey -> ByteString -> Maybe Proof
prove SignKey
sk ByteString
msg =
  IO (Maybe Proof) -> Maybe Proof
forall a. IO a -> a
unsafePerformIO (IO (Maybe Proof) -> Maybe Proof)
-> IO (Maybe Proof) -> Maybe Proof
forall a b. (a -> b) -> a -> b
$
    ForeignPtr SignKeyValue
-> (Ptr SignKeyValue -> IO (Maybe Proof)) -> IO (Maybe Proof)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (SignKey -> ForeignPtr SignKeyValue
unSignKey SignKey
sk) ((Ptr SignKeyValue -> IO (Maybe Proof)) -> IO (Maybe Proof))
-> (Ptr SignKeyValue -> IO (Maybe Proof)) -> IO (Maybe Proof)
forall a b. (a -> b) -> a -> b
$ \Ptr SignKeyValue
skPtr -> do
      Proof
proof <- IO Proof
mkProof
      ByteString -> (CStringLen -> IO (Maybe Proof)) -> IO (Maybe Proof)
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.useAsCStringLen ByteString
msg ((CStringLen -> IO (Maybe Proof)) -> IO (Maybe Proof))
-> (CStringLen -> IO (Maybe Proof)) -> IO (Maybe Proof)
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
m, Int
mlen) -> do
        ForeignPtr ProofValue
-> (Ptr ProofValue -> IO (Maybe Proof)) -> IO (Maybe Proof)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Proof -> ForeignPtr ProofValue
unProof Proof
proof) ((Ptr ProofValue -> IO (Maybe Proof)) -> IO (Maybe Proof))
-> (Ptr ProofValue -> IO (Maybe Proof)) -> IO (Maybe Proof)
forall a b. (a -> b) -> a -> b
$ \Ptr ProofValue
proofPtr -> do
          Ptr ProofValue
-> Ptr SignKeyValue -> Ptr CChar -> CULLong -> IO CInt
crypto_vrf_ietfdraft13_prove_batchcompat Ptr ProofValue
proofPtr Ptr SignKeyValue
skPtr Ptr CChar
m (Int -> CULLong
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
mlen) IO CInt -> (CInt -> IO (Maybe Proof)) -> IO (Maybe Proof)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            CInt
0 -> Maybe Proof -> IO (Maybe Proof)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Proof -> IO (Maybe Proof))
-> Maybe Proof -> IO (Maybe Proof)
forall a b. (a -> b) -> a -> b
$ Proof -> Maybe Proof
forall a. a -> Maybe a
Just (Proof -> Maybe Proof) -> Proof -> Maybe Proof
forall a b. (a -> b) -> a -> b
$! Proof
proof
            CInt
_ -> Maybe Proof -> IO (Maybe Proof)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Proof
forall a. Maybe a
Nothing

-- | Verify a VRF proof and validate the Verification Key. Returns 'Just' a hash of
-- the verification result on success, 'Nothing' if the verification did not
-- succeed.
--
-- For a given verification key and message, there are many possible proofs but only
-- one possible output hash.
verify :: VerKey -> Proof -> ByteString -> Maybe Output
verify :: VerKey -> Proof -> ByteString -> Maybe Output
verify VerKey
pk Proof
proof ByteString
msg =
  IO (Maybe Output) -> Maybe Output
forall a. IO a -> a
unsafePerformIO (IO (Maybe Output) -> Maybe Output)
-> IO (Maybe Output) -> Maybe Output
forall a b. (a -> b) -> a -> b
$
    ForeignPtr VerKeyValue
-> (Ptr VerKeyValue -> IO (Maybe Output)) -> IO (Maybe Output)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (VerKey -> ForeignPtr VerKeyValue
unVerKey VerKey
pk) ((Ptr VerKeyValue -> IO (Maybe Output)) -> IO (Maybe Output))
-> (Ptr VerKeyValue -> IO (Maybe Output)) -> IO (Maybe Output)
forall a b. (a -> b) -> a -> b
$ \Ptr VerKeyValue
pkPtr -> do
      ForeignPtr ProofValue
-> (Ptr ProofValue -> IO (Maybe Output)) -> IO (Maybe Output)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Proof -> ForeignPtr ProofValue
unProof Proof
proof) ((Ptr ProofValue -> IO (Maybe Output)) -> IO (Maybe Output))
-> (Ptr ProofValue -> IO (Maybe Output)) -> IO (Maybe Output)
forall a b. (a -> b) -> a -> b
$ \Ptr ProofValue
proofPtr -> do
        Output
output <- IO Output
mkOutput
        ByteString
-> (CStringLen -> IO (Maybe Output)) -> IO (Maybe Output)
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.useAsCStringLen ByteString
msg ((CStringLen -> IO (Maybe Output)) -> IO (Maybe Output))
-> (CStringLen -> IO (Maybe Output)) -> IO (Maybe Output)
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
m, Int
mlen) -> do
          ForeignPtr OutputValue
-> (Ptr OutputValue -> IO (Maybe Output)) -> IO (Maybe Output)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Output -> ForeignPtr OutputValue
unOutput Output
output) ((Ptr OutputValue -> IO (Maybe Output)) -> IO (Maybe Output))
-> (Ptr OutputValue -> IO (Maybe Output)) -> IO (Maybe Output)
forall a b. (a -> b) -> a -> b
$ \Ptr OutputValue
outputPtr -> do
            Ptr OutputValue
-> Ptr VerKeyValue
-> Ptr ProofValue
-> Ptr CChar
-> CULLong
-> IO CInt
crypto_vrf_ietfdraft13_verify_batchcompat Ptr OutputValue
outputPtr Ptr VerKeyValue
pkPtr Ptr ProofValue
proofPtr Ptr CChar
m (Int -> CULLong
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
mlen) IO CInt -> (CInt -> IO (Maybe Output)) -> IO (Maybe Output)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
              CInt
0 -> Maybe Output -> IO (Maybe Output)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Output -> IO (Maybe Output))
-> Maybe Output -> IO (Maybe Output)
forall a b. (a -> b) -> a -> b
$ Output -> Maybe Output
forall a. a -> Maybe a
Just (Output -> Maybe Output) -> Output -> Maybe Output
forall a b. (a -> b) -> a -> b
$! Output
output
              CInt
_ -> Maybe Output -> IO (Maybe Output)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Output
forall a. Maybe a
Nothing

outputFromProof :: Proof -> Maybe Output
outputFromProof :: Proof -> Maybe Output
outputFromProof (Proof ForeignPtr ProofValue
p) =
  IO (Maybe Output) -> Maybe Output
forall a. IO a -> a
unsafePerformIO (IO (Maybe Output) -> Maybe Output)
-> IO (Maybe Output) -> Maybe Output
forall a b. (a -> b) -> a -> b
$
    ForeignPtr ProofValue
-> (Ptr ProofValue -> IO (Maybe Output)) -> IO (Maybe Output)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr ProofValue
p ((Ptr ProofValue -> IO (Maybe Output)) -> IO (Maybe Output))
-> (Ptr ProofValue -> IO (Maybe Output)) -> IO (Maybe Output)
forall a b. (a -> b) -> a -> b
$ \Ptr ProofValue
ptr -> do
      Output
output <- IO Output
mkOutput
      ForeignPtr OutputValue
-> (Ptr OutputValue -> IO (Maybe Output)) -> IO (Maybe Output)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Output -> ForeignPtr OutputValue
unOutput Output
output) ((Ptr OutputValue -> IO (Maybe Output)) -> IO (Maybe Output))
-> (Ptr OutputValue -> IO (Maybe Output)) -> IO (Maybe Output)
forall a b. (a -> b) -> a -> b
$ \Ptr OutputValue
outputPtr -> do
        Ptr OutputValue -> Ptr ProofValue -> IO CInt
crypto_vrf_ietfdraft13_proof_to_hash_batchcompat Ptr OutputValue
outputPtr Ptr ProofValue
ptr IO CInt -> (CInt -> IO (Maybe Output)) -> IO (Maybe Output)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
          CInt
0 -> Maybe Output -> IO (Maybe Output)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Output -> IO (Maybe Output))
-> Maybe Output -> IO (Maybe Output)
forall a b. (a -> b) -> a -> b
$ Output -> Maybe Output
forall a. a -> Maybe a
Just (Output -> Maybe Output) -> Output -> Maybe Output
forall a b. (a -> b) -> a -> b
$! Output
output
          CInt
_ -> Maybe Output -> IO (Maybe Output)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Output
forall a. Maybe a
Nothing

data PraosBatchCompatVRF

instance VRFAlgorithm PraosBatchCompatVRF where
  newtype VerKeyVRF PraosBatchCompatVRF = VerKeyPraosBatchCompatVRF VerKey
    deriving stock (Int -> VerKeyVRF PraosBatchCompatVRF -> [Char] -> [Char]
[VerKeyVRF PraosBatchCompatVRF] -> [Char] -> [Char]
VerKeyVRF PraosBatchCompatVRF -> [Char]
(Int -> VerKeyVRF PraosBatchCompatVRF -> [Char] -> [Char])
-> (VerKeyVRF PraosBatchCompatVRF -> [Char])
-> ([VerKeyVRF PraosBatchCompatVRF] -> [Char] -> [Char])
-> Show (VerKeyVRF PraosBatchCompatVRF)
forall a.
(Int -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
$cshowsPrec :: Int -> VerKeyVRF PraosBatchCompatVRF -> [Char] -> [Char]
showsPrec :: Int -> VerKeyVRF PraosBatchCompatVRF -> [Char] -> [Char]
$cshow :: VerKeyVRF PraosBatchCompatVRF -> [Char]
show :: VerKeyVRF PraosBatchCompatVRF -> [Char]
$cshowList :: [VerKeyVRF PraosBatchCompatVRF] -> [Char] -> [Char]
showList :: [VerKeyVRF PraosBatchCompatVRF] -> [Char] -> [Char]
Show, VerKeyVRF PraosBatchCompatVRF
-> VerKeyVRF PraosBatchCompatVRF -> Bool
(VerKeyVRF PraosBatchCompatVRF
 -> VerKeyVRF PraosBatchCompatVRF -> Bool)
-> (VerKeyVRF PraosBatchCompatVRF
    -> VerKeyVRF PraosBatchCompatVRF -> Bool)
-> Eq (VerKeyVRF PraosBatchCompatVRF)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VerKeyVRF PraosBatchCompatVRF
-> VerKeyVRF PraosBatchCompatVRF -> Bool
== :: VerKeyVRF PraosBatchCompatVRF
-> VerKeyVRF PraosBatchCompatVRF -> Bool
$c/= :: VerKeyVRF PraosBatchCompatVRF
-> VerKeyVRF PraosBatchCompatVRF -> Bool
/= :: VerKeyVRF PraosBatchCompatVRF
-> VerKeyVRF PraosBatchCompatVRF -> Bool
Eq, (forall x.
 VerKeyVRF PraosBatchCompatVRF
 -> Rep (VerKeyVRF PraosBatchCompatVRF) x)
-> (forall x.
    Rep (VerKeyVRF PraosBatchCompatVRF) x
    -> VerKeyVRF PraosBatchCompatVRF)
-> Generic (VerKeyVRF PraosBatchCompatVRF)
forall x.
Rep (VerKeyVRF PraosBatchCompatVRF) x
-> VerKeyVRF PraosBatchCompatVRF
forall x.
VerKeyVRF PraosBatchCompatVRF
-> Rep (VerKeyVRF PraosBatchCompatVRF) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x.
VerKeyVRF PraosBatchCompatVRF
-> Rep (VerKeyVRF PraosBatchCompatVRF) x
from :: forall x.
VerKeyVRF PraosBatchCompatVRF
-> Rep (VerKeyVRF PraosBatchCompatVRF) x
$cto :: forall x.
Rep (VerKeyVRF PraosBatchCompatVRF) x
-> VerKeyVRF PraosBatchCompatVRF
to :: forall x.
Rep (VerKeyVRF PraosBatchCompatVRF) x
-> VerKeyVRF PraosBatchCompatVRF
Generic)
    deriving newtype (Typeable (VerKeyVRF PraosBatchCompatVRF)
Typeable (VerKeyVRF PraosBatchCompatVRF) =>
(VerKeyVRF PraosBatchCompatVRF -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size)
    -> Proxy (VerKeyVRF PraosBatchCompatVRF) -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size)
    -> Proxy [VerKeyVRF PraosBatchCompatVRF] -> Size)
-> ToCBOR (VerKeyVRF PraosBatchCompatVRF)
VerKeyVRF PraosBatchCompatVRF -> Encoding
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [VerKeyVRF PraosBatchCompatVRF] -> Size
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (VerKeyVRF PraosBatchCompatVRF) -> Size
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
$ctoCBOR :: VerKeyVRF PraosBatchCompatVRF -> Encoding
toCBOR :: VerKeyVRF PraosBatchCompatVRF -> Encoding
$cencodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (VerKeyVRF PraosBatchCompatVRF) -> Size
encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (VerKeyVRF PraosBatchCompatVRF) -> Size
$cencodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [VerKeyVRF PraosBatchCompatVRF] -> Size
encodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [VerKeyVRF PraosBatchCompatVRF] -> Size
ToCBOR, Typeable (VerKeyVRF PraosBatchCompatVRF)
Typeable (VerKeyVRF PraosBatchCompatVRF) =>
(forall s. Decoder s (VerKeyVRF PraosBatchCompatVRF))
-> (Proxy (VerKeyVRF PraosBatchCompatVRF) -> Text)
-> FromCBOR (VerKeyVRF PraosBatchCompatVRF)
Proxy (VerKeyVRF PraosBatchCompatVRF) -> Text
forall s. Decoder s (VerKeyVRF PraosBatchCompatVRF)
forall a.
Typeable a =>
(forall s. Decoder s a) -> (Proxy a -> Text) -> FromCBOR a
$cfromCBOR :: forall s. Decoder s (VerKeyVRF PraosBatchCompatVRF)
fromCBOR :: forall s. Decoder s (VerKeyVRF PraosBatchCompatVRF)
$clabel :: Proxy (VerKeyVRF PraosBatchCompatVRF) -> Text
label :: Proxy (VerKeyVRF PraosBatchCompatVRF) -> Text
FromCBOR)
    deriving (Context -> VerKeyVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo)
Proxy (VerKeyVRF PraosBatchCompatVRF) -> [Char]
(Context -> VerKeyVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo))
-> (Context
    -> VerKeyVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo))
-> (Proxy (VerKeyVRF PraosBatchCompatVRF) -> [Char])
-> NoThunks (VerKeyVRF PraosBatchCompatVRF)
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> [Char])
-> NoThunks a
$cnoThunks :: Context -> VerKeyVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo)
noThunks :: Context -> VerKeyVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> VerKeyVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> VerKeyVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo)
$cshowTypeOf :: Proxy (VerKeyVRF PraosBatchCompatVRF) -> [Char]
showTypeOf :: Proxy (VerKeyVRF PraosBatchCompatVRF) -> [Char]
NoThunks) via OnlyCheckWhnfNamed "VerKeyVRF PraosBatchCompatVRF" VerKey
    deriving newtype (VerKeyVRF PraosBatchCompatVRF -> ()
(VerKeyVRF PraosBatchCompatVRF -> ())
-> NFData (VerKeyVRF PraosBatchCompatVRF)
forall a. (a -> ()) -> NFData a
$crnf :: VerKeyVRF PraosBatchCompatVRF -> ()
rnf :: VerKeyVRF PraosBatchCompatVRF -> ()
NFData)

  newtype SignKeyVRF PraosBatchCompatVRF = SignKeyPraosBatchCompatVRF SignKey
    deriving stock (Int -> SignKeyVRF PraosBatchCompatVRF -> [Char] -> [Char]
[SignKeyVRF PraosBatchCompatVRF] -> [Char] -> [Char]
SignKeyVRF PraosBatchCompatVRF -> [Char]
(Int -> SignKeyVRF PraosBatchCompatVRF -> [Char] -> [Char])
-> (SignKeyVRF PraosBatchCompatVRF -> [Char])
-> ([SignKeyVRF PraosBatchCompatVRF] -> [Char] -> [Char])
-> Show (SignKeyVRF PraosBatchCompatVRF)
forall a.
(Int -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
$cshowsPrec :: Int -> SignKeyVRF PraosBatchCompatVRF -> [Char] -> [Char]
showsPrec :: Int -> SignKeyVRF PraosBatchCompatVRF -> [Char] -> [Char]
$cshow :: SignKeyVRF PraosBatchCompatVRF -> [Char]
show :: SignKeyVRF PraosBatchCompatVRF -> [Char]
$cshowList :: [SignKeyVRF PraosBatchCompatVRF] -> [Char] -> [Char]
showList :: [SignKeyVRF PraosBatchCompatVRF] -> [Char] -> [Char]
Show, SignKeyVRF PraosBatchCompatVRF
-> SignKeyVRF PraosBatchCompatVRF -> Bool
(SignKeyVRF PraosBatchCompatVRF
 -> SignKeyVRF PraosBatchCompatVRF -> Bool)
-> (SignKeyVRF PraosBatchCompatVRF
    -> SignKeyVRF PraosBatchCompatVRF -> Bool)
-> Eq (SignKeyVRF PraosBatchCompatVRF)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SignKeyVRF PraosBatchCompatVRF
-> SignKeyVRF PraosBatchCompatVRF -> Bool
== :: SignKeyVRF PraosBatchCompatVRF
-> SignKeyVRF PraosBatchCompatVRF -> Bool
$c/= :: SignKeyVRF PraosBatchCompatVRF
-> SignKeyVRF PraosBatchCompatVRF -> Bool
/= :: SignKeyVRF PraosBatchCompatVRF
-> SignKeyVRF PraosBatchCompatVRF -> Bool
Eq, (forall x.
 SignKeyVRF PraosBatchCompatVRF
 -> Rep (SignKeyVRF PraosBatchCompatVRF) x)
-> (forall x.
    Rep (SignKeyVRF PraosBatchCompatVRF) x
    -> SignKeyVRF PraosBatchCompatVRF)
-> Generic (SignKeyVRF PraosBatchCompatVRF)
forall x.
Rep (SignKeyVRF PraosBatchCompatVRF) x
-> SignKeyVRF PraosBatchCompatVRF
forall x.
SignKeyVRF PraosBatchCompatVRF
-> Rep (SignKeyVRF PraosBatchCompatVRF) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x.
SignKeyVRF PraosBatchCompatVRF
-> Rep (SignKeyVRF PraosBatchCompatVRF) x
from :: forall x.
SignKeyVRF PraosBatchCompatVRF
-> Rep (SignKeyVRF PraosBatchCompatVRF) x
$cto :: forall x.
Rep (SignKeyVRF PraosBatchCompatVRF) x
-> SignKeyVRF PraosBatchCompatVRF
to :: forall x.
Rep (SignKeyVRF PraosBatchCompatVRF) x
-> SignKeyVRF PraosBatchCompatVRF
Generic)
    deriving newtype (Typeable (SignKeyVRF PraosBatchCompatVRF)
Typeable (SignKeyVRF PraosBatchCompatVRF) =>
(SignKeyVRF PraosBatchCompatVRF -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size)
    -> Proxy (SignKeyVRF PraosBatchCompatVRF) -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size)
    -> Proxy [SignKeyVRF PraosBatchCompatVRF] -> Size)
-> ToCBOR (SignKeyVRF PraosBatchCompatVRF)
SignKeyVRF PraosBatchCompatVRF -> Encoding
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [SignKeyVRF PraosBatchCompatVRF] -> Size
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (SignKeyVRF PraosBatchCompatVRF) -> Size
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
$ctoCBOR :: SignKeyVRF PraosBatchCompatVRF -> Encoding
toCBOR :: SignKeyVRF PraosBatchCompatVRF -> Encoding
$cencodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (SignKeyVRF PraosBatchCompatVRF) -> Size
encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (SignKeyVRF PraosBatchCompatVRF) -> Size
$cencodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [SignKeyVRF PraosBatchCompatVRF] -> Size
encodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [SignKeyVRF PraosBatchCompatVRF] -> Size
ToCBOR, Typeable (SignKeyVRF PraosBatchCompatVRF)
Typeable (SignKeyVRF PraosBatchCompatVRF) =>
(forall s. Decoder s (SignKeyVRF PraosBatchCompatVRF))
-> (Proxy (SignKeyVRF PraosBatchCompatVRF) -> Text)
-> FromCBOR (SignKeyVRF PraosBatchCompatVRF)
Proxy (SignKeyVRF PraosBatchCompatVRF) -> Text
forall s. Decoder s (SignKeyVRF PraosBatchCompatVRF)
forall a.
Typeable a =>
(forall s. Decoder s a) -> (Proxy a -> Text) -> FromCBOR a
$cfromCBOR :: forall s. Decoder s (SignKeyVRF PraosBatchCompatVRF)
fromCBOR :: forall s. Decoder s (SignKeyVRF PraosBatchCompatVRF)
$clabel :: Proxy (SignKeyVRF PraosBatchCompatVRF) -> Text
label :: Proxy (SignKeyVRF PraosBatchCompatVRF) -> Text
FromCBOR)
    deriving (Context -> SignKeyVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo)
Proxy (SignKeyVRF PraosBatchCompatVRF) -> [Char]
(Context -> SignKeyVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo))
-> (Context
    -> SignKeyVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo))
-> (Proxy (SignKeyVRF PraosBatchCompatVRF) -> [Char])
-> NoThunks (SignKeyVRF PraosBatchCompatVRF)
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> [Char])
-> NoThunks a
$cnoThunks :: Context -> SignKeyVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo)
noThunks :: Context -> SignKeyVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> SignKeyVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> SignKeyVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo)
$cshowTypeOf :: Proxy (SignKeyVRF PraosBatchCompatVRF) -> [Char]
showTypeOf :: Proxy (SignKeyVRF PraosBatchCompatVRF) -> [Char]
NoThunks) via OnlyCheckWhnfNamed "SignKeyVRF PraosBatchCompatVRF" SignKey
    deriving newtype (SignKeyVRF PraosBatchCompatVRF -> ()
(SignKeyVRF PraosBatchCompatVRF -> ())
-> NFData (SignKeyVRF PraosBatchCompatVRF)
forall a. (a -> ()) -> NFData a
$crnf :: SignKeyVRF PraosBatchCompatVRF -> ()
rnf :: SignKeyVRF PraosBatchCompatVRF -> ()
NFData)

  newtype CertVRF PraosBatchCompatVRF = CertPraosBatchCompatVRF Proof
    deriving stock (Int -> CertVRF PraosBatchCompatVRF -> [Char] -> [Char]
[CertVRF PraosBatchCompatVRF] -> [Char] -> [Char]
CertVRF PraosBatchCompatVRF -> [Char]
(Int -> CertVRF PraosBatchCompatVRF -> [Char] -> [Char])
-> (CertVRF PraosBatchCompatVRF -> [Char])
-> ([CertVRF PraosBatchCompatVRF] -> [Char] -> [Char])
-> Show (CertVRF PraosBatchCompatVRF)
forall a.
(Int -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
$cshowsPrec :: Int -> CertVRF PraosBatchCompatVRF -> [Char] -> [Char]
showsPrec :: Int -> CertVRF PraosBatchCompatVRF -> [Char] -> [Char]
$cshow :: CertVRF PraosBatchCompatVRF -> [Char]
show :: CertVRF PraosBatchCompatVRF -> [Char]
$cshowList :: [CertVRF PraosBatchCompatVRF] -> [Char] -> [Char]
showList :: [CertVRF PraosBatchCompatVRF] -> [Char] -> [Char]
Show, CertVRF PraosBatchCompatVRF -> CertVRF PraosBatchCompatVRF -> Bool
(CertVRF PraosBatchCompatVRF
 -> CertVRF PraosBatchCompatVRF -> Bool)
-> (CertVRF PraosBatchCompatVRF
    -> CertVRF PraosBatchCompatVRF -> Bool)
-> Eq (CertVRF PraosBatchCompatVRF)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CertVRF PraosBatchCompatVRF -> CertVRF PraosBatchCompatVRF -> Bool
== :: CertVRF PraosBatchCompatVRF -> CertVRF PraosBatchCompatVRF -> Bool
$c/= :: CertVRF PraosBatchCompatVRF -> CertVRF PraosBatchCompatVRF -> Bool
/= :: CertVRF PraosBatchCompatVRF -> CertVRF PraosBatchCompatVRF -> Bool
Eq, (forall x.
 CertVRF PraosBatchCompatVRF -> Rep (CertVRF PraosBatchCompatVRF) x)
-> (forall x.
    Rep (CertVRF PraosBatchCompatVRF) x -> CertVRF PraosBatchCompatVRF)
-> Generic (CertVRF PraosBatchCompatVRF)
forall x.
Rep (CertVRF PraosBatchCompatVRF) x -> CertVRF PraosBatchCompatVRF
forall x.
CertVRF PraosBatchCompatVRF -> Rep (CertVRF PraosBatchCompatVRF) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x.
CertVRF PraosBatchCompatVRF -> Rep (CertVRF PraosBatchCompatVRF) x
from :: forall x.
CertVRF PraosBatchCompatVRF -> Rep (CertVRF PraosBatchCompatVRF) x
$cto :: forall x.
Rep (CertVRF PraosBatchCompatVRF) x -> CertVRF PraosBatchCompatVRF
to :: forall x.
Rep (CertVRF PraosBatchCompatVRF) x -> CertVRF PraosBatchCompatVRF
Generic)
    deriving newtype (Typeable (CertVRF PraosBatchCompatVRF)
Typeable (CertVRF PraosBatchCompatVRF) =>
(CertVRF PraosBatchCompatVRF -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size)
    -> Proxy (CertVRF PraosBatchCompatVRF) -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size)
    -> Proxy [CertVRF PraosBatchCompatVRF] -> Size)
-> ToCBOR (CertVRF PraosBatchCompatVRF)
CertVRF PraosBatchCompatVRF -> Encoding
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [CertVRF PraosBatchCompatVRF] -> Size
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (CertVRF PraosBatchCompatVRF) -> Size
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
$ctoCBOR :: CertVRF PraosBatchCompatVRF -> Encoding
toCBOR :: CertVRF PraosBatchCompatVRF -> Encoding
$cencodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (CertVRF PraosBatchCompatVRF) -> Size
encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (CertVRF PraosBatchCompatVRF) -> Size
$cencodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [CertVRF PraosBatchCompatVRF] -> Size
encodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [CertVRF PraosBatchCompatVRF] -> Size
ToCBOR, Typeable (CertVRF PraosBatchCompatVRF)
Typeable (CertVRF PraosBatchCompatVRF) =>
(forall s. Decoder s (CertVRF PraosBatchCompatVRF))
-> (Proxy (CertVRF PraosBatchCompatVRF) -> Text)
-> FromCBOR (CertVRF PraosBatchCompatVRF)
Proxy (CertVRF PraosBatchCompatVRF) -> Text
forall s. Decoder s (CertVRF PraosBatchCompatVRF)
forall a.
Typeable a =>
(forall s. Decoder s a) -> (Proxy a -> Text) -> FromCBOR a
$cfromCBOR :: forall s. Decoder s (CertVRF PraosBatchCompatVRF)
fromCBOR :: forall s. Decoder s (CertVRF PraosBatchCompatVRF)
$clabel :: Proxy (CertVRF PraosBatchCompatVRF) -> Text
label :: Proxy (CertVRF PraosBatchCompatVRF) -> Text
FromCBOR)
    deriving (Context -> CertVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo)
Proxy (CertVRF PraosBatchCompatVRF) -> [Char]
(Context -> CertVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo))
-> (Context -> CertVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo))
-> (Proxy (CertVRF PraosBatchCompatVRF) -> [Char])
-> NoThunks (CertVRF PraosBatchCompatVRF)
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> [Char])
-> NoThunks a
$cnoThunks :: Context -> CertVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo)
noThunks :: Context -> CertVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> CertVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> CertVRF PraosBatchCompatVRF -> IO (Maybe ThunkInfo)
$cshowTypeOf :: Proxy (CertVRF PraosBatchCompatVRF) -> [Char]
showTypeOf :: Proxy (CertVRF PraosBatchCompatVRF) -> [Char]
NoThunks) via OnlyCheckWhnfNamed "CertKeyVRF PraosBatchCompatVRF" Proof
    deriving newtype (CertVRF PraosBatchCompatVRF -> ()
(CertVRF PraosBatchCompatVRF -> ())
-> NFData (CertVRF PraosBatchCompatVRF)
forall a. (a -> ()) -> NFData a
$crnf :: CertVRF PraosBatchCompatVRF -> ()
rnf :: CertVRF PraosBatchCompatVRF -> ()
NFData)

  type Signable PraosBatchCompatVRF = SignableRepresentation

  algorithmNameVRF :: forall (proxy :: * -> *). proxy PraosBatchCompatVRF -> [Char]
algorithmNameVRF = [Char] -> proxy PraosBatchCompatVRF -> [Char]
forall a b. a -> b -> a
const [Char]
"PraosBatchCompatVRF"

  deriveVerKeyVRF :: SignKeyVRF PraosBatchCompatVRF -> VerKeyVRF PraosBatchCompatVRF
deriveVerKeyVRF = (SignKey -> VerKey)
-> SignKeyVRF PraosBatchCompatVRF -> VerKeyVRF PraosBatchCompatVRF
forall a b. Coercible a b => a -> b
coerce SignKey -> VerKey
skToVerKey

  evalVRF :: forall a.
(HasCallStack, Signable PraosBatchCompatVRF a) =>
ContextVRF PraosBatchCompatVRF
-> a
-> SignKeyVRF PraosBatchCompatVRF
-> (OutputVRF PraosBatchCompatVRF, CertVRF PraosBatchCompatVRF)
evalVRF = \ContextVRF PraosBatchCompatVRF
_ a
msg (SignKeyPraosBatchCompatVRF SignKey
sk) ->
    let msgBS :: ByteString
msgBS = a -> ByteString
forall a. SignableRepresentation a => a -> ByteString
getSignableRepresentation a
msg
        proof :: Proof
proof = Proof -> Maybe Proof -> Proof
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> Proof
forall a. HasCallStack => [Char] -> a
error [Char]
"Invalid Key") (Maybe Proof -> Proof) -> Maybe Proof -> Proof
forall a b. (a -> b) -> a -> b
$ SignKey -> ByteString -> Maybe Proof
prove SignKey
sk ByteString
msgBS
        output :: Output
output = Output -> Maybe Output -> Output
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> Output
forall a. HasCallStack => [Char] -> a
error [Char]
"Invalid Proof") (Maybe Output -> Output) -> Maybe Output -> Output
forall a b. (a -> b) -> a -> b
$ Proof -> Maybe Output
outputFromProof Proof
proof
     in Output
output Output
-> (OutputVRF PraosBatchCompatVRF, CertVRF PraosBatchCompatVRF)
-> (OutputVRF PraosBatchCompatVRF, CertVRF PraosBatchCompatVRF)
forall a b. a -> b -> b
`seq`
          Proof
proof Proof
-> (OutputVRF PraosBatchCompatVRF, CertVRF PraosBatchCompatVRF)
-> (OutputVRF PraosBatchCompatVRF, CertVRF PraosBatchCompatVRF)
forall a b. a -> b -> b
`seq`
            (ByteString -> OutputVRF PraosBatchCompatVRF
forall v. ByteString -> OutputVRF v
OutputVRF (Output -> ByteString
outputBytes Output
output), Proof -> CertVRF PraosBatchCompatVRF
CertPraosBatchCompatVRF Proof
proof)

  verifyVRF :: forall a.
(HasCallStack, Signable PraosBatchCompatVRF a) =>
ContextVRF PraosBatchCompatVRF
-> VerKeyVRF PraosBatchCompatVRF
-> a
-> CertVRF PraosBatchCompatVRF
-> Maybe (OutputVRF PraosBatchCompatVRF)
verifyVRF = \ContextVRF PraosBatchCompatVRF
_ (VerKeyPraosBatchCompatVRF VerKey
pk) a
msg (CertPraosBatchCompatVRF Proof
proof) ->
    (ByteString -> OutputVRF PraosBatchCompatVRF
forall v. ByteString -> OutputVRF v
OutputVRF (ByteString -> OutputVRF PraosBatchCompatVRF)
-> (Output -> ByteString)
-> Output
-> OutputVRF PraosBatchCompatVRF
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Output -> ByteString
outputBytes) (Output -> OutputVRF PraosBatchCompatVRF)
-> Maybe Output -> Maybe (OutputVRF PraosBatchCompatVRF)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> VerKey -> Proof -> ByteString -> Maybe Output
verify VerKey
pk Proof
proof (a -> ByteString
forall a. SignableRepresentation a => a -> ByteString
getSignableRepresentation a
msg)

  sizeOutputVRF :: forall (proxy :: * -> *). proxy PraosBatchCompatVRF -> Word
sizeOutputVRF proxy PraosBatchCompatVRF
_ = CSize -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
crypto_vrf_ietfdraft13_outputbytes
  seedSizeVRF :: forall (proxy :: * -> *). proxy PraosBatchCompatVRF -> Word
seedSizeVRF proxy PraosBatchCompatVRF
_ = CSize -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
crypto_vrf_ietfdraft13_seedbytes

  genKeyPairVRF :: Seed
-> (SignKeyVRF PraosBatchCompatVRF, VerKeyVRF PraosBatchCompatVRF)
genKeyPairVRF = \Seed
cryptoseed ->
    let seed :: Seed
seed =
          ByteString -> Seed
seedFromBytes (ByteString -> Seed) -> (Seed -> ByteString) -> Seed -> Seed
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString, Seed) -> ByteString
forall a b. (a, b) -> a
fst ((ByteString, Seed) -> ByteString)
-> (Seed -> (ByteString, Seed)) -> Seed -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Seed -> (ByteString, Seed)
getBytesFromSeedT (CSize -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
crypto_vrf_ietfdraft13_seedbytes) (Seed -> Seed) -> Seed -> Seed
forall a b. (a -> b) -> a -> b
$ Seed
cryptoseed
        (VerKey
pk, SignKey
sk) = Seed -> (VerKey, SignKey)
keypairFromSeed Seed
seed
     in SignKey
sk SignKey
-> (SignKeyVRF PraosBatchCompatVRF, VerKeyVRF PraosBatchCompatVRF)
-> (SignKeyVRF PraosBatchCompatVRF, VerKeyVRF PraosBatchCompatVRF)
forall a b. a -> b -> b
`seq` VerKey
pk VerKey
-> (SignKeyVRF PraosBatchCompatVRF, VerKeyVRF PraosBatchCompatVRF)
-> (SignKeyVRF PraosBatchCompatVRF, VerKeyVRF PraosBatchCompatVRF)
forall a b. a -> b -> b
`seq` (SignKey -> SignKeyVRF PraosBatchCompatVRF
SignKeyPraosBatchCompatVRF SignKey
sk, VerKey -> VerKeyVRF PraosBatchCompatVRF
VerKeyPraosBatchCompatVRF VerKey
pk)

  rawSerialiseVerKeyVRF :: VerKeyVRF PraosBatchCompatVRF -> ByteString
rawSerialiseVerKeyVRF (VerKeyPraosBatchCompatVRF VerKey
pk) = VerKey -> ByteString
vkBytes VerKey
pk
  rawSerialiseSignKeyVRF :: SignKeyVRF PraosBatchCompatVRF -> ByteString
rawSerialiseSignKeyVRF (SignKeyPraosBatchCompatVRF SignKey
sk) = SignKey -> ByteString
skBytes SignKey
sk
  rawSerialiseCertVRF :: CertVRF PraosBatchCompatVRF -> ByteString
rawSerialiseCertVRF (CertPraosBatchCompatVRF Proof
proof) = Proof -> ByteString
proofBytes Proof
proof
  rawDeserialiseVerKeyVRF :: ByteString -> Maybe (VerKeyVRF PraosBatchCompatVRF)
rawDeserialiseVerKeyVRF = (ByteString -> VerKeyVRF PraosBatchCompatVRF)
-> Maybe ByteString -> Maybe (VerKeyVRF PraosBatchCompatVRF)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (VerKey -> VerKeyVRF PraosBatchCompatVRF
VerKeyPraosBatchCompatVRF (VerKey -> VerKeyVRF PraosBatchCompatVRF)
-> (ByteString -> VerKey)
-> ByteString
-> VerKeyVRF PraosBatchCompatVRF
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> VerKey
vkFromBytes) (Maybe ByteString -> Maybe (VerKeyVRF PraosBatchCompatVRF))
-> (ByteString -> Maybe ByteString)
-> ByteString
-> Maybe (VerKeyVRF PraosBatchCompatVRF)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ByteString -> Maybe ByteString
assertLength Int
verKeySizeVRF
  rawDeserialiseSignKeyVRF :: ByteString -> Maybe (SignKeyVRF PraosBatchCompatVRF)
rawDeserialiseSignKeyVRF = (ByteString -> SignKeyVRF PraosBatchCompatVRF)
-> Maybe ByteString -> Maybe (SignKeyVRF PraosBatchCompatVRF)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (SignKey -> SignKeyVRF PraosBatchCompatVRF
SignKeyPraosBatchCompatVRF (SignKey -> SignKeyVRF PraosBatchCompatVRF)
-> (ByteString -> SignKey)
-> ByteString
-> SignKeyVRF PraosBatchCompatVRF
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> SignKey
skFromBytes) (Maybe ByteString -> Maybe (SignKeyVRF PraosBatchCompatVRF))
-> (ByteString -> Maybe ByteString)
-> ByteString
-> Maybe (SignKeyVRF PraosBatchCompatVRF)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ByteString -> Maybe ByteString
assertLength Int
signKeySizeVRF
  rawDeserialiseCertVRF :: ByteString -> Maybe (CertVRF PraosBatchCompatVRF)
rawDeserialiseCertVRF = (ByteString -> CertVRF PraosBatchCompatVRF)
-> Maybe ByteString -> Maybe (CertVRF PraosBatchCompatVRF)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Proof -> CertVRF PraosBatchCompatVRF
CertPraosBatchCompatVRF (Proof -> CertVRF PraosBatchCompatVRF)
-> (ByteString -> Proof)
-> ByteString
-> CertVRF PraosBatchCompatVRF
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Proof
proofFromBytes) (Maybe ByteString -> Maybe (CertVRF PraosBatchCompatVRF))
-> (ByteString -> Maybe ByteString)
-> ByteString
-> Maybe (CertVRF PraosBatchCompatVRF)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ByteString -> Maybe ByteString
assertLength Int
certSizeVRF

  sizeVerKeyVRF :: forall (proxy :: * -> *). proxy PraosBatchCompatVRF -> Word
sizeVerKeyVRF proxy PraosBatchCompatVRF
_ = Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
verKeySizeVRF
  sizeSignKeyVRF :: forall (proxy :: * -> *). proxy PraosBatchCompatVRF -> Word
sizeSignKeyVRF proxy PraosBatchCompatVRF
_ = Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
signKeySizeVRF
  sizeCertVRF :: forall (proxy :: * -> *). proxy PraosBatchCompatVRF -> Word
sizeCertVRF proxy PraosBatchCompatVRF
_ = Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
certSizeVRF

assertLength :: Int -> ByteString -> Maybe ByteString
assertLength :: Int -> ByteString -> Maybe ByteString
assertLength Int
l ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
l =
      ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
bs
  | Bool
otherwise =
      Maybe ByteString
forall a. Maybe a
Nothing