Skip to content

Commit

Permalink
Get rid of MonadRandom
Browse files Browse the repository at this point in the history
  • Loading branch information
tdammers committed Jul 1, 2020
1 parent 5c6db42 commit 0eef661
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 42 deletions.
12 changes: 5 additions & 7 deletions cardano-crypto-class/src/Cardano/Crypto/VRF/Class.hs
Expand Up @@ -57,8 +57,6 @@ import Cardano.Binary
encodeListLen, enforceSize, decodeBytes, encodeBytes,
withWordSize)

import Crypto.Random (MonadRandom)

import Cardano.Crypto.Util (Empty, bytesToNatural, naturalToBytes)
import Cardano.Crypto.Seed (Seed)
import Cardano.Crypto.Hash.Class (HashAlgorithm, Hash, hashRaw)
Expand Down Expand Up @@ -111,11 +109,11 @@ class ( Typeable v
type Signable c = Empty

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

verifyVRF
:: (HasCallStack, Signable v a)
Expand Down Expand Up @@ -297,12 +295,12 @@ instance (VRFAlgorithm v, Typeable a) => FromCBOR (CertifiedVRF v a) where
decodeCertVRF

evalCertified
:: (VRFAlgorithm v, MonadRandom m, Signable v a)
:: (VRFAlgorithm v, Signable v a)
=> ContextVRF v
-> a
-> SignKeyVRF v
-> m (CertifiedVRF v a)
evalCertified ctxt a key = uncurry CertifiedVRF <$> evalVRF ctxt a key
-> (CertifiedVRF v a)
evalCertified ctxt a key = uncurry CertifiedVRF $ evalVRF ctxt a key

verifyCertified
:: (VRFAlgorithm v, Signable v a)
Expand Down
2 changes: 1 addition & 1 deletion cardano-crypto-class/src/Cardano/Crypto/VRF/Mock.hs
Expand Up @@ -56,7 +56,7 @@ instance VRFAlgorithm MockVRF where

type Signable MockVRF = ToCBOR

evalVRF () a sk = return $ evalVRF' a sk
evalVRF () a sk = evalVRF' a sk

verifyVRF () (VerKeyMockVRF n) a c = evalVRF' a (SignKeyMockVRF n) == c

Expand Down
15 changes: 5 additions & 10 deletions cardano-crypto-class/src/Cardano/Crypto/VRF/Simple.hs
Expand Up @@ -25,13 +25,10 @@ import Numeric.Natural (Natural)
import Cardano.Prelude (NoUnexpectedThunks, UseIsNormalForm(..), force)
import Cardano.Binary (Encoding, FromCBOR (..), ToCBOR (..))

import Crypto.Number.Generate (generateBetween)
import qualified Crypto.PubKey.ECC.Prim as C
import qualified Crypto.PubKey.ECC.Types as C
import Crypto.Random (MonadRandom (..))

import Data.Word
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.Bits (shiftL)

Expand Down Expand Up @@ -101,9 +98,6 @@ h = getHash . hashWithSerialiser @H id
h' :: Encoding -> Integer -> Point
h' enc l = pow $ mod (l * (fromIntegral . bsToNat $ h enc)) q

getR :: MonadRandom m => m Integer
getR = generateBetween 0 (q - 1)

-- | Convert a 'ByteString' into a 'Natural'. Assumes big-endian input.
bsToNat :: ByteString -> Natural
bsToNat = bytesToNatBE . BS.unpack
Expand Down Expand Up @@ -162,14 +156,15 @@ instance VRFAlgorithm SimpleVRF where

type Signable SimpleVRF = ToCBOR

evalVRF () a sk@(SignKeySimpleVRF k) = do
evalVRF () a sk@(SignKeySimpleVRF k) =
let u = h' (toCBOR a) k
y = h $ toCBOR a <> toCBOR u
VerKeySimpleVRF v = deriveVerKeyVRF sk
r <- getR
let c = h $ toCBOR a <> toCBOR v <> toCBOR (pow r) <> toCBOR (h' (toCBOR a) r)

r = fromIntegral (bsToNat y) `mod` q
c = h $ toCBOR a <> toCBOR v <> toCBOR (pow r) <> toCBOR (h' (toCBOR a) r)
s = mod (r + k * fromIntegral (bsToNat c)) q
return (OutputVRF y, CertSimpleVRF u (bsToNat c) s)
in (OutputVRF y, CertSimpleVRF u (bsToNat c) s)

verifyVRF () (VerKeySimpleVRF v) a (OutputVRF y, cert) =
let u = certU cert
Expand Down
10 changes: 5 additions & 5 deletions cardano-crypto-praos/src/Cardano/Crypto/VRF/Praos.hs
Expand Up @@ -429,12 +429,12 @@ instance VRFAlgorithm PraosVRF where

deriveVerKeyVRF = coerce skToVerKey

evalVRF = \_ msg (SignKeyPraosVRF sk) -> do
evalVRF = \_ msg (SignKeyPraosVRF sk) ->
let msgBS = serialize' msg
proof <- maybe (error "Invalid Key") pure $ prove sk msgBS
output <- maybe (error "Invalid Proof") pure $ outputFromProof proof
return $ output `seq` proof `seq`
(OutputVRF (outputBytes output), CertPraosVRF proof)
proof = maybe (error "Invalid Key") id $ prove sk msgBS
output = maybe (error "Invalid Proof") id $ outputFromProof proof
in output `seq` proof `seq`
(OutputVRF (outputBytes output), CertPraosVRF proof)

verifyVRF = \_ (VerKeyPraosVRF pk) msg (_, CertPraosVRF proof) ->
isJust $! verify pk proof (serialize' msg)
Expand Down
32 changes: 13 additions & 19 deletions cardano-crypto-tests/src/Test/Crypto/VRF.hs
Expand Up @@ -11,7 +11,6 @@ module Test.Crypto.VRF
)
where

import Cardano.Binary (FromCBOR, ToCBOR (..))
import Cardano.Crypto.VRF
import Cardano.Crypto.VRF.Praos
import Cardano.Crypto.Util
Expand Down Expand Up @@ -142,49 +141,45 @@ testVRFAlgorithm _ n =

prop_vrf_verify_pos
:: forall a v. (Signable v a, VRFAlgorithm v, ContextVRF v ~ ())
=> TestSeed
-> a
=> a
-> SignKeyVRF v
-> Bool
prop_vrf_verify_pos seed a sk =
let (y, c) = withTestSeed seed $ evalVRF () a sk
prop_vrf_verify_pos a sk =
let (y, c) = evalVRF () a sk
vk = deriveVerKeyVRF sk
in verifyVRF () vk a (y, c)

prop_vrf_verify_neg
:: forall a v. (Signable v a, VRFAlgorithm v, Eq (SignKeyVRF v), ContextVRF v ~ ())
=> TestSeed
-> a
=> a
-> SignKeyVRF v
-> SignKeyVRF v
-> Property
prop_vrf_verify_neg seed a sk sk' =
prop_vrf_verify_neg a sk sk' =
sk /=
sk' ==>
let (y, c) = withTestSeed seed $ evalVRF () a sk'
let (y, c) = evalVRF () a sk'
vk = deriveVerKeyVRF sk
in not $ verifyVRF () vk a (y, c)


prop_vrf_output_size
:: forall a v. (Signable v a, VRFAlgorithm v, ContextVRF v ~ ())
=> TestSeed
-> a
=> a
-> SignKeyVRF v
-> Property
prop_vrf_output_size seed a sk =
let (out, _c) = withTestSeed seed $ evalVRF () a sk
prop_vrf_output_size a sk =
let (out, _c) = evalVRF () a sk
in BS.length (getOutputVRFBytes out)
=== fromIntegral (sizeOutputVRF (Proxy :: Proxy v))

prop_vrf_output_natural
:: forall a v. (Signable v a, VRFAlgorithm v, ContextVRF v ~ ())
=> TestSeed
-> a
=> a
-> SignKeyVRF v
-> Property
prop_vrf_output_natural seed a sk =
let (out, _c) = withTestSeed seed $ evalVRF () a sk
prop_vrf_output_natural a sk =
let (out, _c) = evalVRF () a sk
n = getOutputVRFNatural out
in counterexample (show n) $
mkTestOutputVRF n === out
Expand Down Expand Up @@ -224,6 +219,5 @@ instance (Signable v Int, VRFAlgorithm v, ContextVRF v ~ ())
arbitrary = do
a <- arbitrary :: Gen Int
sk <- arbitrary
seed <- arbitrary
return $ withTestSeed seed $ fmap snd $ evalVRF () a sk
return $ snd $ evalVRF () a sk
shrink = const []

0 comments on commit 0eef661

Please sign in to comment.