Skip to content

Commit

Permalink
Merge branch 'master' into iquerejeta/update-nix
Browse files Browse the repository at this point in the history
  • Loading branch information
iquerejeta committed Feb 8, 2023
2 parents 3fd8c44 + b05fbc7 commit 86cfc89
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 3 deletions.
1 change: 1 addition & 0 deletions CODE-OF-CONDUCT.md
@@ -0,0 +1 @@
See the code of conduct in the [https://github.com/input-output-hk/cardano-engineering-handbook/blob/main/CODE-OF-CONDUCT.md](Cardano engineering handbook).
1 change: 0 additions & 1 deletion cardano-crypto-praos/cardano-crypto-praos.cabal
Expand Up @@ -23,7 +23,6 @@ extra-source-files: cbits/crypto_vrf.h
cbits/vrf13_batchcompat/crypto_vrf_ietfdraft13.h

cbits/private/common.h
cbits/private/quirks.h
cbits/private/ed25519_ref10.h
cbits/private/core_h2c.h
cbits/private/ed25519_ref10_fe_25_5.h
Expand Down
28 changes: 28 additions & 0 deletions cardano-crypto-praos/src/Cardano/Crypto/VRF/Praos.hs
Expand Up @@ -41,6 +41,10 @@ module Cardano.Crypto.VRF.Praos
, skFromBytes
, vkFromBytes

, vkToBatchCompat
, skToBatchCompat
, outputToBatchCompat


-- * Core VRF operations
, prove
Expand All @@ -66,6 +70,7 @@ import Cardano.Crypto.RandomBytes (randombytes_buf)
import Cardano.Crypto.Seed (getBytesFromSeedT)
import Cardano.Crypto.Util (SignableRepresentation (..))
import Cardano.Crypto.VRF.Class
import qualified Cardano.Crypto.VRF.PraosBatchCompat as BC
import Control.DeepSeq (NFData (..))
import Control.Monad (void)
import Data.ByteString (ByteString)
Expand Down Expand Up @@ -418,6 +423,29 @@ prove sk msg =
0 -> return $ Just $! proof
_ -> return Nothing

-- | Construct a BatchCompat vkey from praos, non-batchcompat
vkToBatchCompat :: VerKeyVRF PraosVRF -> VerKeyVRF BC.PraosBatchCompatVRF
vkToBatchCompat praosVk =
case rawDeserialiseVerKeyVRF (rawSerialiseVerKeyVRF praosVk) of
Just vk -> vk
Nothing -> error "VerKeyVRF: Unable to convert PraosVK to BatchCompatVK."

-- | Construct a BatchCompat skey from praos, non-batchcompat
skToBatchCompat :: SignKeyVRF PraosVRF -> SignKeyVRF BC.PraosBatchCompatVRF
skToBatchCompat praosSk =
case rawDeserialiseSignKeyVRF (rawSerialiseSignKeyVRF praosSk) of
Just sk -> sk
Nothing -> error "SignKeyVRF: Unable to convert PraosSK to BatchCompatSK."

-- | Construct a BatchCompat output from praos, non-batchcompat
outputToBatchCompat :: OutputVRF PraosVRF -> OutputVRF BC.PraosBatchCompatVRF
outputToBatchCompat praosOutput =
if vrfKeySizeVRF /= BC.vrfKeySizeVRF
then error "OutputVRF: Unable to convert PraosSK to BatchCompatSK."
else
OutputVRF (getOutputVRFBytes praosOutput)


-- | 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.
Expand Down
9 changes: 9 additions & 0 deletions cardano-crypto-tests/src/Test/Crypto/Util.hs
Expand Up @@ -8,6 +8,7 @@
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE DataKinds #-}

module Test.Crypto.Util
( -- * CBOR
Expand All @@ -33,6 +34,8 @@ module Test.Crypto.Util
, nullTestSeed

-- * Seeds
, SizedSeed
, unSizedSeed
, arbitrarySeedOfSize

-- * test messages for signings
Expand Down Expand Up @@ -103,6 +106,7 @@ import Test.QuickCheck
import Formatting.Buildable (build)
import qualified Test.QuickCheck.Gen as Gen
import Control.Monad (guard, when)
import GHC.TypeLits (Nat, KnownNat, natVal)

--------------------------------------------------------------------------------
-- Connecting MonadRandom to Gen
Expand Down Expand Up @@ -134,6 +138,11 @@ instance Arbitrary TestSeed where
-- Seeds
--------------------------------------------------------------------------------

newtype SizedSeed (n :: Nat) = SizedSeed { unSizedSeed :: Seed } deriving Show

instance (KnownNat n) => Arbitrary (SizedSeed n) where
arbitrary = SizedSeed <$> arbitrarySeedOfSize (fromIntegral $ natVal (Proxy :: Proxy n))

arbitrarySeedOfSize :: Word -> Gen Seed
arbitrarySeedOfSize sz = mkSeedFromBytes . BS.pack <$> vector (fromIntegral sz)

Expand Down
96 changes: 94 additions & 2 deletions cardano-crypto-tests/src/Test/Crypto/VRF.hs
Expand Up @@ -3,6 +3,7 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE DataKinds #-}

{-# OPTIONS_GHC -Wno-orphans #-}

Expand All @@ -25,10 +26,9 @@ import Test.QuickCheck
((==>), (===), Arbitrary(..), Gen, Property, NonNegative(..),
counterexample)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.QuickCheck (testProperty)
import Test.Tasty.QuickCheck (testProperty, vectorOf)

{- HLINT IGNORE "Use <$>" -}

--
-- The list of all tests
--
Expand All @@ -44,6 +44,15 @@ tests =
[ testProperty "bytesToNatural" prop_bytesToNatural
, testProperty "naturalToBytes" prop_naturalToBytes
]
, testGroup "ConvertingTypes"
[ testProperty "pubKeyToBatchCompat" prop_pubKeyToBatchComopat
, testProperty "signKeyToBatchCompat" prop_signKeyToBatchCompat
, testProperty "outputToBatchCompat" prop_outputToBatchComat
, testProperty "compatibleVerKeyConversion" prop_verKeyValidConversion
, testProperty "compatibleOutputConversion" prop_outputValidConversion
, testProperty "compatibleSignKeyConversion" prop_signKeyValidConversion
, testProperty "compatibleFullConversion" prop_fullValidConversion
]
]

testVRFAlgorithm
Expand Down Expand Up @@ -207,6 +216,84 @@ prop_naturalToBytes (NonNegative sz) n =
sz >= 8 ==>
bytesToNatural (naturalToBytes sz (fromIntegral n)) == fromIntegral n

--
-- Praos <-> BatchCompatPraos VerKey conversion
--
prop_pubKeyToBatchComopat :: VerKeyVRF PraosVRF -> Property
prop_pubKeyToBatchComopat vk =
rawSerialiseVerKeyVRF (vkToBatchCompat vk) === rawSerialiseVerKeyVRF vk

--
-- Praos <-> BatchCompatPraos SignKey conversion
--
prop_signKeyToBatchCompat :: SignKeyVRF PraosVRF -> Property
prop_signKeyToBatchCompat sk =
rawSerialiseSignKeyVRF (skToBatchCompat sk) === rawSerialiseSignKeyVRF sk

--
-- Praos <-> BatchCompatPraos Output conversion
--
prop_outputToBatchComat :: OutputVRF PraosVRF -> Property
prop_outputToBatchComat output =
getOutputVRFBytes (outputToBatchCompat output) === getOutputVRFBytes output

--
-- Praos <-> BatchCompatPraos VerKey compatibility. We check that a proof is validated with a
-- transformed key
--
prop_verKeyValidConversion :: SizedSeed 32 -> Message -> Bool
prop_verKeyValidConversion sharedBytes msg =
let
vkPraos = deriveVerKeyVRF . genKeyVRF . unSizedSeed $ sharedBytes
skBatchCompat = genKeyVRF . unSizedSeed $ sharedBytes
vkBatchCompat = vkToBatchCompat vkPraos
(y, c) = evalVRF () msg skBatchCompat
in
verifyVRF () vkBatchCompat msg (y, c)

--
-- Praos <-> BatchCompatPraos SignKey compatibility. We check that a proof is validated with a
-- transformed key
--
prop_signKeyValidConversion :: SizedSeed 32 -> Bool
prop_signKeyValidConversion sharedBytes =
let
skPraos = genKeyVRF . unSizedSeed $ sharedBytes
skBatchCompat = genKeyVRF . unSizedSeed $ sharedBytes
in
skBatchCompat == skToBatchCompat skPraos

--
-- Praos <-> BatchCompatPraos Output compatibility. We check that a proof is validated with a
-- transformed output
--
prop_outputValidConversion :: SizedSeed 32 -> Message -> Bool
prop_outputValidConversion sharedBytes msg =
let
skPraos = genKeyVRF . unSizedSeed $ sharedBytes
(outPraos, _c) = evalVRF () msg skPraos
skBatchCompat = genKeyVRF . unSizedSeed $ sharedBytes
vkBatchCompat = deriveVerKeyVRF skBatchCompat
(_out, c) = evalVRF () msg skBatchCompat
outBatchCompat = outputToBatchCompat outPraos
in
verifyVRF () vkBatchCompat msg (outBatchCompat, c)

--
-- Praos <-> BatchCompatPraos compatibility. We check that a proof is validated with a
-- transformed key and output
--
prop_fullValidConversion :: SizedSeed 32 -> Message -> Bool
prop_fullValidConversion sharedBytes msg =
let
skPraos = genKeyVRF . unSizedSeed $ sharedBytes
vkPraos = deriveVerKeyVRF skPraos
(outPraos, _c) = evalVRF () msg skPraos
skBatchCompat = skToBatchCompat skPraos
vkBatchCompat = vkToBatchCompat vkPraos
(_out, c) = evalVRF () msg skBatchCompat
outBatchCompat = outputToBatchCompat outPraos
in verifyVRF () vkBatchCompat msg (outBatchCompat, c)

--
-- Arbitrary instances
Expand All @@ -230,3 +317,8 @@ instance (VRFAlgorithm v,
sk <- arbitrary
return $ snd $ evalVRF () a sk
shrink = const []

instance VRFAlgorithm v => Arbitrary (OutputVRF v) where
arbitrary = do
bytes <- BS.pack <$> vectorOf (fromIntegral (sizeOutputVRF (Proxy :: Proxy v))) arbitrary
return $ OutputVRF bytes

0 comments on commit 86cfc89

Please sign in to comment.