{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

-- | Mock implementations of verifiable random functions.
module Cardano.Crypto.VRF.Mock (
  MockVRF,
  VerKeyVRF (..),
  SignKeyVRF (..),
)
where

import Data.Proxy (Proxy (..))
import Data.Word (Word64)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks)

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

import Cardano.Crypto.Hash
import Cardano.Crypto.Seed
import Cardano.Crypto.Util
import Cardano.Crypto.VRF.Class

data MockVRF

instance VRFAlgorithm MockVRF where
  --
  -- Key and signature types
  --

  newtype VerKeyVRF MockVRF = VerKeyMockVRF Word64
    deriving (Int -> VerKeyVRF MockVRF -> ShowS
[VerKeyVRF MockVRF] -> ShowS
VerKeyVRF MockVRF -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [VerKeyVRF MockVRF] -> ShowS
$cshowList :: [VerKeyVRF MockVRF] -> ShowS
show :: VerKeyVRF MockVRF -> String
$cshow :: VerKeyVRF MockVRF -> String
showsPrec :: Int -> VerKeyVRF MockVRF -> ShowS
$cshowsPrec :: Int -> VerKeyVRF MockVRF -> ShowS
Show, VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Bool
$c/= :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Bool
== :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Bool
$c== :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Bool
Eq, Eq (VerKeyVRF MockVRF)
VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Bool
VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Ordering
VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> VerKeyVRF MockVRF
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> VerKeyVRF MockVRF
$cmin :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> VerKeyVRF MockVRF
max :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> VerKeyVRF MockVRF
$cmax :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> VerKeyVRF MockVRF
>= :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Bool
$c>= :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Bool
> :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Bool
$c> :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Bool
<= :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Bool
$c<= :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Bool
< :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Bool
$c< :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Bool
compare :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Ordering
$ccompare :: VerKeyVRF MockVRF -> VerKeyVRF MockVRF -> Ordering
Ord, forall x. Rep (VerKeyVRF MockVRF) x -> VerKeyVRF MockVRF
forall x. VerKeyVRF MockVRF -> Rep (VerKeyVRF MockVRF) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep (VerKeyVRF MockVRF) x -> VerKeyVRF MockVRF
$cfrom :: forall x. VerKeyVRF MockVRF -> Rep (VerKeyVRF MockVRF) x
Generic, Context -> VerKeyVRF MockVRF -> IO (Maybe ThunkInfo)
Proxy (VerKeyVRF MockVRF) -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy (VerKeyVRF MockVRF) -> String
$cshowTypeOf :: Proxy (VerKeyVRF MockVRF) -> String
wNoThunks :: Context -> VerKeyVRF MockVRF -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> VerKeyVRF MockVRF -> IO (Maybe ThunkInfo)
noThunks :: Context -> VerKeyVRF MockVRF -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> VerKeyVRF MockVRF -> IO (Maybe ThunkInfo)
NoThunks)

  newtype SignKeyVRF MockVRF = SignKeyMockVRF Word64
    deriving (Int -> SignKeyVRF MockVRF -> ShowS
[SignKeyVRF MockVRF] -> ShowS
SignKeyVRF MockVRF -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SignKeyVRF MockVRF] -> ShowS
$cshowList :: [SignKeyVRF MockVRF] -> ShowS
show :: SignKeyVRF MockVRF -> String
$cshow :: SignKeyVRF MockVRF -> String
showsPrec :: Int -> SignKeyVRF MockVRF -> ShowS
$cshowsPrec :: Int -> SignKeyVRF MockVRF -> ShowS
Show, SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Bool
$c/= :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Bool
== :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Bool
$c== :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Bool
Eq, Eq (SignKeyVRF MockVRF)
SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Bool
SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Ordering
SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> SignKeyVRF MockVRF
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> SignKeyVRF MockVRF
$cmin :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> SignKeyVRF MockVRF
max :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> SignKeyVRF MockVRF
$cmax :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> SignKeyVRF MockVRF
>= :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Bool
$c>= :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Bool
> :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Bool
$c> :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Bool
<= :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Bool
$c<= :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Bool
< :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Bool
$c< :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Bool
compare :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Ordering
$ccompare :: SignKeyVRF MockVRF -> SignKeyVRF MockVRF -> Ordering
Ord, forall x. Rep (SignKeyVRF MockVRF) x -> SignKeyVRF MockVRF
forall x. SignKeyVRF MockVRF -> Rep (SignKeyVRF MockVRF) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep (SignKeyVRF MockVRF) x -> SignKeyVRF MockVRF
$cfrom :: forall x. SignKeyVRF MockVRF -> Rep (SignKeyVRF MockVRF) x
Generic, Context -> SignKeyVRF MockVRF -> IO (Maybe ThunkInfo)
Proxy (SignKeyVRF MockVRF) -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy (SignKeyVRF MockVRF) -> String
$cshowTypeOf :: Proxy (SignKeyVRF MockVRF) -> String
wNoThunks :: Context -> SignKeyVRF MockVRF -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> SignKeyVRF MockVRF -> IO (Maybe ThunkInfo)
noThunks :: Context -> SignKeyVRF MockVRF -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> SignKeyVRF MockVRF -> IO (Maybe ThunkInfo)
NoThunks)

  newtype CertVRF MockVRF = CertMockVRF Word64
    deriving (Int -> CertVRF MockVRF -> ShowS
[CertVRF MockVRF] -> ShowS
CertVRF MockVRF -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CertVRF MockVRF] -> ShowS
$cshowList :: [CertVRF MockVRF] -> ShowS
show :: CertVRF MockVRF -> String
$cshow :: CertVRF MockVRF -> String
showsPrec :: Int -> CertVRF MockVRF -> ShowS
$cshowsPrec :: Int -> CertVRF MockVRF -> ShowS
Show, CertVRF MockVRF -> CertVRF MockVRF -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CertVRF MockVRF -> CertVRF MockVRF -> Bool
$c/= :: CertVRF MockVRF -> CertVRF MockVRF -> Bool
== :: CertVRF MockVRF -> CertVRF MockVRF -> Bool
$c== :: CertVRF MockVRF -> CertVRF MockVRF -> Bool
Eq, Eq (CertVRF MockVRF)
CertVRF MockVRF -> CertVRF MockVRF -> Bool
CertVRF MockVRF -> CertVRF MockVRF -> Ordering
CertVRF MockVRF -> CertVRF MockVRF -> CertVRF MockVRF
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CertVRF MockVRF -> CertVRF MockVRF -> CertVRF MockVRF
$cmin :: CertVRF MockVRF -> CertVRF MockVRF -> CertVRF MockVRF
max :: CertVRF MockVRF -> CertVRF MockVRF -> CertVRF MockVRF
$cmax :: CertVRF MockVRF -> CertVRF MockVRF -> CertVRF MockVRF
>= :: CertVRF MockVRF -> CertVRF MockVRF -> Bool
$c>= :: CertVRF MockVRF -> CertVRF MockVRF -> Bool
> :: CertVRF MockVRF -> CertVRF MockVRF -> Bool
$c> :: CertVRF MockVRF -> CertVRF MockVRF -> Bool
<= :: CertVRF MockVRF -> CertVRF MockVRF -> Bool
$c<= :: CertVRF MockVRF -> CertVRF MockVRF -> Bool
< :: CertVRF MockVRF -> CertVRF MockVRF -> Bool
$c< :: CertVRF MockVRF -> CertVRF MockVRF -> Bool
compare :: CertVRF MockVRF -> CertVRF MockVRF -> Ordering
$ccompare :: CertVRF MockVRF -> CertVRF MockVRF -> Ordering
Ord, forall x. Rep (CertVRF MockVRF) x -> CertVRF MockVRF
forall x. CertVRF MockVRF -> Rep (CertVRF MockVRF) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep (CertVRF MockVRF) x -> CertVRF MockVRF
$cfrom :: forall x. CertVRF MockVRF -> Rep (CertVRF MockVRF) x
Generic, Context -> CertVRF MockVRF -> IO (Maybe ThunkInfo)
Proxy (CertVRF MockVRF) -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy (CertVRF MockVRF) -> String
$cshowTypeOf :: Proxy (CertVRF MockVRF) -> String
wNoThunks :: Context -> CertVRF MockVRF -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> CertVRF MockVRF -> IO (Maybe ThunkInfo)
noThunks :: Context -> CertVRF MockVRF -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> CertVRF MockVRF -> IO (Maybe ThunkInfo)
NoThunks)

  --
  -- Metadata and basic key operations
  --

  algorithmNameVRF :: forall (proxy :: * -> *). proxy MockVRF -> String
algorithmNameVRF proxy MockVRF
_ = String
"mock"

  deriveVerKeyVRF :: SignKeyVRF MockVRF -> VerKeyVRF MockVRF
deriveVerKeyVRF (SignKeyMockVRF Word64
n) = Word64 -> VerKeyVRF MockVRF
VerKeyMockVRF Word64
n

  --
  -- Core algorithm operations
  --

  type Signable MockVRF = SignableRepresentation

  evalVRF :: forall a.
(HasCallStack, Signable MockVRF a) =>
ContextVRF MockVRF
-> a -> SignKeyVRF MockVRF -> (OutputVRF MockVRF, CertVRF MockVRF)
evalVRF () a
a SignKeyVRF MockVRF
sk = forall a.
SignableRepresentation a =>
a -> SignKeyVRF MockVRF -> (OutputVRF MockVRF, CertVRF MockVRF)
evalVRF' a
a SignKeyVRF MockVRF
sk

  verifyVRF :: forall a.
(HasCallStack, Signable MockVRF a) =>
ContextVRF MockVRF
-> VerKeyVRF MockVRF
-> a
-> CertVRF MockVRF
-> Maybe (OutputVRF MockVRF)
verifyVRF () (VerKeyMockVRF Word64
n) a
a CertVRF MockVRF
c
    | CertVRF MockVRF
c forall a. Eq a => a -> a -> Bool
== CertVRF MockVRF
c' = forall a. a -> Maybe a
Just OutputVRF MockVRF
o
    | Bool
otherwise = forall a. Maybe a
Nothing
    where
      (OutputVRF MockVRF
o, CertVRF MockVRF
c') = forall a.
SignableRepresentation a =>
a -> SignKeyVRF MockVRF -> (OutputVRF MockVRF, CertVRF MockVRF)
evalVRF' a
a (Word64 -> SignKeyVRF MockVRF
SignKeyMockVRF Word64
n)

  sizeOutputVRF :: forall (proxy :: * -> *). proxy MockVRF -> Word
sizeOutputVRF proxy MockVRF
_ = forall h (proxy :: * -> *). HashAlgorithm h => proxy h -> Word
sizeHash (forall {k} (t :: k). Proxy t
Proxy :: Proxy ShortHash)

  --
  -- Key generation
  --

  seedSizeVRF :: forall (proxy :: * -> *). proxy MockVRF -> Word
seedSizeVRF proxy MockVRF
_ = Word
8
  genKeyVRF :: Seed -> SignKeyVRF MockVRF
genKeyVRF Seed
seed = Word64 -> SignKeyVRF MockVRF
SignKeyMockVRF Word64
sk
    where
      sk :: Word64
sk = forall a. Seed -> (forall (m :: * -> *). MonadRandom m => m a) -> a
runMonadRandomWithSeed Seed
seed forall (m :: * -> *). MonadRandom m => m Word64
getRandomWord64

  --
  -- raw serialise/deserialise
  --

  sizeVerKeyVRF :: forall (proxy :: * -> *). proxy MockVRF -> Word
sizeVerKeyVRF proxy MockVRF
_ = Word
8
  sizeSignKeyVRF :: forall (proxy :: * -> *). proxy MockVRF -> Word
sizeSignKeyVRF proxy MockVRF
_ = Word
8
  sizeCertVRF :: forall (proxy :: * -> *). proxy MockVRF -> Word
sizeCertVRF proxy MockVRF
_ = Word
8

  rawSerialiseVerKeyVRF :: VerKeyVRF MockVRF -> ByteString
rawSerialiseVerKeyVRF (VerKeyMockVRF Word64
k) = Word64 -> ByteString
writeBinaryWord64 Word64
k
  rawSerialiseSignKeyVRF :: SignKeyVRF MockVRF -> ByteString
rawSerialiseSignKeyVRF (SignKeyMockVRF Word64
k) = Word64 -> ByteString
writeBinaryWord64 Word64
k
  rawSerialiseCertVRF :: CertVRF MockVRF -> ByteString
rawSerialiseCertVRF (CertMockVRF Word64
k) = Word64 -> ByteString
writeBinaryWord64 Word64
k

  rawDeserialiseVerKeyVRF :: ByteString -> Maybe (VerKeyVRF MockVRF)
rawDeserialiseVerKeyVRF ByteString
bs
    | [ByteString
kb] <- [Int] -> ByteString -> [ByteString]
splitsAt [Int
8] ByteString
bs
    , let k :: Word64
k = ByteString -> Word64
readBinaryWord64 ByteString
kb =
        forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! Word64 -> VerKeyVRF MockVRF
VerKeyMockVRF Word64
k
    | Bool
otherwise =
        forall a. Maybe a
Nothing

  rawDeserialiseSignKeyVRF :: ByteString -> Maybe (SignKeyVRF MockVRF)
rawDeserialiseSignKeyVRF ByteString
bs
    | [ByteString
kb] <- [Int] -> ByteString -> [ByteString]
splitsAt [Int
8] ByteString
bs
    , let k :: Word64
k = ByteString -> Word64
readBinaryWord64 ByteString
kb =
        forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! Word64 -> SignKeyVRF MockVRF
SignKeyMockVRF Word64
k
    | Bool
otherwise =
        forall a. Maybe a
Nothing

  rawDeserialiseCertVRF :: ByteString -> Maybe (CertVRF MockVRF)
rawDeserialiseCertVRF ByteString
bs
    | [ByteString
kb] <- [Int] -> ByteString -> [ByteString]
splitsAt [Int
8] ByteString
bs
    , let k :: Word64
k = ByteString -> Word64
readBinaryWord64 ByteString
kb =
        forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! Word64 -> CertVRF MockVRF
CertMockVRF Word64
k
    | Bool
otherwise =
        forall a. Maybe a
Nothing

instance ToCBOR (VerKeyVRF MockVRF) where
  toCBOR :: VerKeyVRF MockVRF -> Encoding
toCBOR = forall v. VRFAlgorithm v => VerKeyVRF v -> Encoding
encodeVerKeyVRF
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (VerKeyVRF MockVRF) -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_size = forall v. VRFAlgorithm v => Proxy (VerKeyVRF v) -> Size
encodedVerKeyVRFSizeExpr

instance FromCBOR (VerKeyVRF MockVRF) where
  fromCBOR :: forall s. Decoder s (VerKeyVRF MockVRF)
fromCBOR = forall v s. VRFAlgorithm v => Decoder s (VerKeyVRF v)
decodeVerKeyVRF

instance ToCBOR (SignKeyVRF MockVRF) where
  toCBOR :: SignKeyVRF MockVRF -> Encoding
toCBOR = forall v. VRFAlgorithm v => SignKeyVRF v -> Encoding
encodeSignKeyVRF
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (SignKeyVRF MockVRF) -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_size = forall v. VRFAlgorithm v => Proxy (SignKeyVRF v) -> Size
encodedSignKeyVRFSizeExpr

instance FromCBOR (SignKeyVRF MockVRF) where
  fromCBOR :: forall s. Decoder s (SignKeyVRF MockVRF)
fromCBOR = forall v s. VRFAlgorithm v => Decoder s (SignKeyVRF v)
decodeSignKeyVRF

instance ToCBOR (CertVRF MockVRF) where
  toCBOR :: CertVRF MockVRF -> Encoding
toCBOR = forall v. VRFAlgorithm v => CertVRF v -> Encoding
encodeCertVRF
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (CertVRF MockVRF) -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
_size = forall v. VRFAlgorithm v => Proxy (CertVRF v) -> Size
encodedCertVRFSizeExpr

instance FromCBOR (CertVRF MockVRF) where
  fromCBOR :: forall s. Decoder s (CertVRF MockVRF)
fromCBOR = forall v s. VRFAlgorithm v => Decoder s (CertVRF v)
decodeCertVRF

evalVRF' ::
  SignableRepresentation a =>
  a ->
  SignKeyVRF MockVRF ->
  (OutputVRF MockVRF, CertVRF MockVRF)
evalVRF' :: forall a.
SignableRepresentation a =>
a -> SignKeyVRF MockVRF -> (OutputVRF MockVRF, CertVRF MockVRF)
evalVRF' a
a sk :: SignKeyVRF MockVRF
sk@(SignKeyMockVRF Word64
n) =
  let y :: ByteString
y =
        forall h a. Hash h a -> ByteString
hashToBytes forall a b. (a -> b) -> a -> b
$
          forall h a. HashAlgorithm h => (a -> Encoding) -> a -> Hash h a
hashWithSerialiser @ShortHash forall a. a -> a
id forall a b. (a -> b) -> a -> b
$
            forall a. ToCBOR a => a -> Encoding
toCBOR (forall a. SignableRepresentation a => a -> ByteString
getSignableRepresentation a
a) forall a. Semigroup a => a -> a -> a
<> forall a. ToCBOR a => a -> Encoding
toCBOR SignKeyVRF MockVRF
sk
   in (forall v. ByteString -> OutputVRF v
OutputVRF ByteString
y, Word64 -> CertVRF MockVRF
CertMockVRF Word64
n)