{-# LANGUAGE BangPatterns #-}
{-# 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.Praos
  (
  -- * VRFAlgorithm API
    PraosVRF

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

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

  -- * Conversions
  , outputBytes
  , proofBytes
  , skBytes
  , vkBytes
  , skToVerKey
  , skToSeed

  , proofFromBytes
  , skFromBytes
  , vkFromBytes


  -- * Core VRF operations
  , prove
  , verify

  , SignKeyVRF (..)
  , VerKeyVRF (..)
  , CertVRF (..)

  -- * Internal types
  , Proof
  , SignKey
  , VerKey
  , 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 qualified Data.ByteString.Unsafe as BS
import Data.Coerce (coerce)
import Data.Maybe (fromMaybe, isJust)
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 -> String
(Context -> Seed -> IO (Maybe ThunkInfo))
-> (Context -> Seed -> IO (Maybe ThunkInfo))
-> (Proxy Seed -> String)
-> NoThunks Seed
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy Seed -> String
$cshowTypeOf :: Proxy Seed -> String
wNoThunks :: Context -> Seed -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> Seed -> IO (Maybe ThunkInfo)
noThunks :: Context -> Seed -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> Seed -> IO (Maybe ThunkInfo)
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
$cto :: forall x. Rep SignKey x -> SignKey
$cfrom :: forall x. SignKey -> Rep SignKey x
Generic)
  deriving Context -> SignKey -> IO (Maybe ThunkInfo)
Proxy SignKey -> String
(Context -> SignKey -> IO (Maybe ThunkInfo))
-> (Context -> SignKey -> IO (Maybe ThunkInfo))
-> (Proxy SignKey -> String)
-> NoThunks SignKey
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy SignKey -> String
$cshowTypeOf :: Proxy SignKey -> String
wNoThunks :: Context -> SignKey -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> SignKey -> IO (Maybe ThunkInfo)
noThunks :: Context -> SignKey -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> SignKey -> IO (Maybe ThunkInfo)
NoThunks via OnlyCheckWhnf SignKey

instance NFData SignKey where
  rnf :: SignKey -> ()
rnf SignKey
a = SignKey -> () -> ()
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
$cto :: forall x. Rep VerKey x -> VerKey
$cfrom :: forall x. VerKey -> Rep VerKey x
Generic)
  deriving Context -> VerKey -> IO (Maybe ThunkInfo)
Proxy VerKey -> String
(Context -> VerKey -> IO (Maybe ThunkInfo))
-> (Context -> VerKey -> IO (Maybe ThunkInfo))
-> (Proxy VerKey -> String)
-> NoThunks VerKey
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy VerKey -> String
$cshowTypeOf :: Proxy VerKey -> String
wNoThunks :: Context -> VerKey -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> VerKey -> IO (Maybe ThunkInfo)
noThunks :: Context -> VerKey -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> VerKey -> IO (Maybe ThunkInfo)
NoThunks via OnlyCheckWhnf VerKey

instance NFData VerKey where
  rnf :: VerKey -> ()
rnf VerKey
a = VerKey -> () -> ()
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
$cto :: forall x. Rep Proof x -> Proof
$cfrom :: forall x. Proof -> Rep Proof x
Generic)
  deriving Context -> Proof -> IO (Maybe ThunkInfo)
Proxy Proof -> String
(Context -> Proof -> IO (Maybe ThunkInfo))
-> (Context -> Proof -> IO (Maybe ThunkInfo))
-> (Proxy Proof -> String)
-> NoThunks Proof
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy Proof -> String
$cshowTypeOf :: Proxy Proof -> String
wNoThunks :: Context -> Proof -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> Proof -> IO (Maybe ThunkInfo)
noThunks :: Context -> Proof -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> Proof -> IO (Maybe ThunkInfo)
NoThunks via OnlyCheckWhnf Proof

instance NFData Proof where
  rnf :: Proof -> ()
rnf Proof
a = Proof -> () -> ()
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
$cto :: forall x. Rep Output x -> Output
$cfrom :: forall x. Output -> Rep Output x
Generic)
  deriving Context -> Output -> IO (Maybe ThunkInfo)
Proxy Output -> String
(Context -> Output -> IO (Maybe ThunkInfo))
-> (Context -> Output -> IO (Maybe ThunkInfo))
-> (Proxy Output -> String)
-> NoThunks Output
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy Output -> String
$cshowTypeOf :: Proxy Output -> String
wNoThunks :: Context -> Output -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> Output -> IO (Maybe ThunkInfo)
noThunks :: Context -> Output -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> Output -> IO (Maybe ThunkInfo)
NoThunks via OnlyCheckWhnf Output

-- Raw low-level FFI bindings.
--
foreign import ccall "crypto_vrf_ietfdraft03_proofbytes" crypto_vrf_proofbytes :: CSize

foreign import ccall "crypto_vrf_ietfdraft03_publickeybytes" crypto_vrf_publickeybytes :: CSize

foreign import ccall "crypto_vrf_ietfdraft03_secretkeybytes" crypto_vrf_secretkeybytes :: CSize

foreign import ccall "crypto_vrf_ietfdraft03_seedbytes" crypto_vrf_seedbytes :: CSize

foreign import ccall "crypto_vrf_ietfdraft03_outputbytes" crypto_vrf_outputbytes :: CSize

foreign import ccall "crypto_vrf_ietfdraft03_keypair_from_seed"
  crypto_vrf_keypair_from_seed :: VerKeyPtr -> SignKeyPtr -> SeedPtr -> IO CInt

foreign import ccall "crypto_vrf_ietfdraft03_sk_to_pk"
  crypto_vrf_sk_to_pk :: VerKeyPtr -> SignKeyPtr -> IO ()

foreign import ccall "crypto_vrf_ietfdraft03_sk_to_seed"
  crypto_vrf_sk_to_seed :: SeedPtr -> SignKeyPtr -> IO ()

foreign import ccall "crypto_vrf_ietfdraft03_prove"
  crypto_vrf_prove :: ProofPtr -> SignKeyPtr -> Ptr CChar -> CULLong -> IO CInt

foreign import ccall "crypto_vrf_ietfdraft03_verify"
  crypto_vrf_verify :: OutputPtr -> VerKeyPtr -> ProofPtr -> Ptr CChar -> CULLong -> IO CInt

foreign import ccall "crypto_vrf_ietfdraft03_proof_to_hash"
  crypto_vrf_proof_to_hash :: 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_proofbytes

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_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_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_outputbytes

-- | 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_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_seedbytes
  Seed -> IO Seed
forall (m :: * -> *) a. Monad m => a -> m a
return Seed
seed

copyFromByteString :: Ptr a -> ByteString -> Int -> IO ()
copyFromByteString :: 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.unsafeUseAsCStringLen 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 String -> IO ()
forall a. HasCallStack => String -> a
error (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$
           String
"Invalid input size, expected at least " String -> String -> String
forall a. Semigroup a => a -> a -> a
<>
           Int -> String
forall a. Show a => a -> String
show Int
lenExpected String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
", but got " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
lenActual

seedFromBytes :: ByteString -> Seed
seedFromBytes :: ByteString -> Seed
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_seedbytes)
  Seed -> IO Seed
forall (m :: * -> *) a. Monad m => a -> m a
return Seed
seed

-- | 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_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 -> String
show = ByteString -> String
forall a. Show a => a -> String
show (ByteString -> String) -> (Proof -> ByteString) -> Proof -> String
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 :: Decoder s Proof
fromCBOR = Decoder s ByteString
forall a s. FromCBOR a => Decoder s a
fromCBOR Decoder s ByteString
-> (ByteString -> Decoder s Proof) -> Decoder s Proof
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Decoder s Proof
forall (m :: * -> *). MonadFail m => ByteString -> m Proof
proofFromBytes

instance Show SignKey where
  show :: SignKey -> String
show = ByteString -> String
forall a. Show a => a -> String
show (ByteString -> String)
-> (SignKey -> ByteString) -> SignKey -> String
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 :: Decoder s SignKey
fromCBOR = Decoder s ByteString
forall a s. FromCBOR a => Decoder s a
fromCBOR Decoder s ByteString
-> (ByteString -> Decoder s SignKey) -> Decoder s SignKey
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Decoder s SignKey
forall (m :: * -> *). MonadFail m => ByteString -> m SignKey
skFromBytes

instance Show VerKey where
  show :: VerKey -> String
show = ByteString -> String
forall a. Show a => a -> String
show (ByteString -> String)
-> (VerKey -> ByteString) -> VerKey -> String
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 :: Decoder s VerKey
fromCBOR = Decoder s ByteString
forall a s. FromCBOR a => Decoder s a
fromCBOR Decoder s ByteString
-> (ByteString -> Decoder s VerKey) -> Decoder s VerKey
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Decoder s VerKey
forall (m :: * -> *). MonadFail m => ByteString -> m VerKey
vkFromBytes

-- | 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 (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 (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 (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 :: MonadFail m => ByteString -> m Proof
proofFromBytes :: ByteString -> m Proof
proofFromBytes ByteString
bs
  | Int
bsLen Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
certSizeVRF =
    String -> m Proof
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m Proof) -> String -> m Proof
forall a b. (a -> b) -> a -> b
$
      String
"Invalid proof length "
        String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show @Int Int
bsLen
        String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
", expecting "
        String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show @Int Int
certSizeVRF
  | Bool
otherwise = Proof -> m Proof
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Proof -> m Proof) -> Proof -> m Proof
forall a b. (a -> b) -> a -> b
$! 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 (m :: * -> *) a. Monad m => a -> m a
return Proof
proof
    where
      bsLen :: Int
bsLen = ByteString -> Int
BS.length ByteString
bs


skFromBytes :: MonadFail m => ByteString -> m SignKey
skFromBytes :: ByteString -> m SignKey
skFromBytes ByteString
bs = do
  if Int
bsLen Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
signKeySizeVRF
    then
      String -> m SignKey
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m SignKey) -> String -> m SignKey
forall a b. (a -> b) -> a -> b
$
        String
"Invalid SignKey length "
          String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show @Int Int
bsLen
          String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
", expecting "
          String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show @Int Int
signKeySizeVRF
    else SignKey -> m SignKey
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SignKey -> m SignKey) -> SignKey -> m SignKey
forall a b. (a -> b) -> a -> b
$! IO SignKey -> SignKey
forall a. IO a -> a
unsafePerformIO (IO SignKey -> SignKey) -> IO SignKey -> SignKey
forall a b. (a -> b) -> a -> b
$ 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 (m :: * -> *) a. Monad m => a -> m a
return SignKey
sk
  where
    bsLen :: Int
bsLen = ByteString -> Int
BS.length ByteString
bs

vkFromBytes :: MonadFail m => ByteString -> m VerKey
vkFromBytes :: ByteString -> m VerKey
vkFromBytes ByteString
bs = do
  if Int
bsLen Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
verKeySizeVRF
    then
      String -> m VerKey
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m VerKey) -> String -> m VerKey
forall a b. (a -> b) -> a -> b
$
        String
"Invalid VerKey length "
          String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show @Int Int
bsLen
          String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
", expecting "
          String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show @Int Int
verKeySizeVRF
    else
      VerKey -> m VerKey
forall (f :: * -> *) a. Applicative f => a -> f a
pure (VerKey -> m VerKey) -> VerKey -> m VerKey
forall a b. (a -> b) -> a -> b
$! IO VerKey -> VerKey
forall a. IO a -> a
unsafePerformIO (IO VerKey -> VerKey) -> IO VerKey -> VerKey
forall a b. (a -> b) -> a -> b
$ 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 (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 (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_outputbytes)

-- | 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_keypair_from_seed Ptr VerKeyValue
pkPtr Ptr SignKeyValue
skPtr Ptr SeedValue
sptr
    (VerKey, SignKey) -> IO (VerKey, SignKey)
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)
`seq` SignKey
sk SignKey -> (VerKey, SignKey) -> (VerKey, SignKey)
`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
      Ptr VerKeyValue -> Ptr SignKeyValue -> IO ()
crypto_vrf_sk_to_pk Ptr VerKeyValue
pkPtr Ptr SignKeyValue
skPtr
    VerKey -> IO VerKey
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
    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
seedPtr ->
      Ptr SeedValue -> Ptr SignKeyValue -> IO ()
crypto_vrf_sk_to_seed Ptr SeedValue
seedPtr Ptr SignKeyValue
skPtr
    Seed -> IO Seed
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_prove 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 (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            CInt
0 -> Maybe Proof -> IO (Maybe Proof)
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 (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_verify 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 (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
              CInt
0 -> Maybe Output -> IO (Maybe Output)
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 (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_proof_to_hash Ptr OutputValue
outputPtr Ptr ProofValue
ptr IO CInt -> (CInt -> IO (Maybe Output)) -> IO (Maybe Output)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
          CInt
0 -> Maybe Output -> IO (Maybe Output)
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 (m :: * -> *) a. Monad m => a -> m a
return Maybe Output
forall a. Maybe a
Nothing

data PraosVRF

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

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

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

  type Signable PraosVRF = SignableRepresentation

  algorithmNameVRF :: proxy PraosVRF -> String
algorithmNameVRF = String -> proxy PraosVRF -> String
forall a b. a -> b -> a
const String
"PraosVRF"

  deriveVerKeyVRF :: SignKeyVRF PraosVRF -> VerKeyVRF PraosVRF
deriveVerKeyVRF = (SignKey -> VerKey) -> SignKeyVRF PraosVRF -> VerKeyVRF PraosVRF
coerce SignKey -> VerKey
skToVerKey

  evalVRF :: ContextVRF PraosVRF
-> a
-> SignKeyVRF PraosVRF
-> (OutputVRF PraosVRF, CertVRF PraosVRF)
evalVRF = \ContextVRF PraosVRF
_ a
msg (SignKeyPraosVRF 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 (String -> Proof
forall a. HasCallStack => String -> a
error String
"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 :: ByteString
output = ByteString -> (Output -> ByteString) -> Maybe Output -> ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> ByteString
forall a. HasCallStack => String -> a
error String
"Invalid Proof") Output -> ByteString
outputBytes (Maybe Output -> ByteString) -> Maybe Output -> ByteString
forall a b. (a -> b) -> a -> b
$ Proof -> Maybe Output
outputFromProof Proof
proof
     in (ByteString -> OutputVRF PraosVRF
forall v. ByteString -> OutputVRF v
OutputVRF ByteString
output, Proof -> CertVRF PraosVRF
CertPraosVRF Proof
proof)

  verifyVRF :: ContextVRF PraosVRF
-> VerKeyVRF PraosVRF
-> a
-> (OutputVRF PraosVRF, CertVRF PraosVRF)
-> Bool
verifyVRF = \ContextVRF PraosVRF
_ (VerKeyPraosVRF pk) a
msg (OutputVRF PraosVRF
_, CertPraosVRF proof) ->
    Maybe Output -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Output -> Bool) -> Maybe Output -> Bool
forall a b. (a -> b) -> a -> b
$! VerKey -> Proof -> ByteString -> Maybe Output
verify VerKey
pk Proof
proof (a -> ByteString
forall a. SignableRepresentation a => a -> ByteString
getSignableRepresentation a
msg)

  sizeOutputVRF :: proxy PraosVRF -> Word
sizeOutputVRF proxy PraosVRF
_ = CSize -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
crypto_vrf_outputbytes
  seedSizeVRF :: proxy PraosVRF -> Word
seedSizeVRF proxy PraosVRF
_ = CSize -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
crypto_vrf_seedbytes

  genKeyPairVRF :: Seed -> (SignKeyVRF PraosVRF, VerKeyVRF PraosVRF)
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_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 -> SignKeyVRF PraosVRF
SignKeyPraosVRF SignKey
sk, VerKey -> VerKeyVRF PraosVRF
VerKeyPraosVRF VerKey
pk)

  rawSerialiseVerKeyVRF :: VerKeyVRF PraosVRF -> ByteString
rawSerialiseVerKeyVRF (VerKeyPraosVRF pk) = VerKey -> ByteString
vkBytes VerKey
pk
  rawSerialiseSignKeyVRF :: SignKeyVRF PraosVRF -> ByteString
rawSerialiseSignKeyVRF (SignKeyPraosVRF sk) = SignKey -> ByteString
skBytes SignKey
sk
  rawSerialiseCertVRF :: CertVRF PraosVRF -> ByteString
rawSerialiseCertVRF (CertPraosVRF proof) = Proof -> ByteString
proofBytes Proof
proof
  rawDeserialiseVerKeyVRF :: ByteString -> Maybe (VerKeyVRF PraosVRF)
rawDeserialiseVerKeyVRF = (VerKey -> VerKeyVRF PraosVRF)
-> Maybe VerKey -> Maybe (VerKeyVRF PraosVRF)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap VerKey -> VerKeyVRF PraosVRF
VerKeyPraosVRF (Maybe VerKey -> Maybe (VerKeyVRF PraosVRF))
-> (ByteString -> Maybe VerKey)
-> ByteString
-> Maybe (VerKeyVRF PraosVRF)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe VerKey
forall (m :: * -> *). MonadFail m => ByteString -> m VerKey
vkFromBytes
  rawDeserialiseSignKeyVRF :: ByteString -> Maybe (SignKeyVRF PraosVRF)
rawDeserialiseSignKeyVRF = (SignKey -> SignKeyVRF PraosVRF)
-> Maybe SignKey -> Maybe (SignKeyVRF PraosVRF)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SignKey -> SignKeyVRF PraosVRF
SignKeyPraosVRF (Maybe SignKey -> Maybe (SignKeyVRF PraosVRF))
-> (ByteString -> Maybe SignKey)
-> ByteString
-> Maybe (SignKeyVRF PraosVRF)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe SignKey
forall (m :: * -> *). MonadFail m => ByteString -> m SignKey
skFromBytes
  rawDeserialiseCertVRF :: ByteString -> Maybe (CertVRF PraosVRF)
rawDeserialiseCertVRF = (Proof -> CertVRF PraosVRF)
-> Maybe Proof -> Maybe (CertVRF PraosVRF)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Proof -> CertVRF PraosVRF
CertPraosVRF (Maybe Proof -> Maybe (CertVRF PraosVRF))
-> (ByteString -> Maybe Proof)
-> ByteString
-> Maybe (CertVRF PraosVRF)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe Proof
forall (m :: * -> *). MonadFail m => ByteString -> m Proof
proofFromBytes

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