{-# LANGUAGE TemplateHaskell #-}
module Test.Cardano.Crypto.Signing.Signing (tests, genData) where
import Cardano.Crypto.Signing (SignTag (..), sign, toVerification, verifySignature)
import Cardano.Ledger.Binary (encCBOR)
import Cardano.Prelude
import Hedgehog (
Gen,
Property,
assert,
checkParallel,
discover,
forAll,
property,
)
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import qualified Test.Cardano.Crypto.Dummy as Dummy
import Test.Cardano.Crypto.Gen (
genKeypair,
genSigningKey,
genVerificationKey,
)
tests :: IO Bool
tests :: IO Bool
tests = Group -> IO Bool
forall (m :: * -> *). MonadIO m => Group -> m Bool
checkParallel $$String
[(PropertyName, Property)]
Property
String -> GroupName
String -> PropertyName
GroupName -> [(PropertyName, Property)] -> Group
prop_sign :: Property
prop_signDifferentKey :: Property
prop_signDifferentData :: Property
discover
prop_sign :: Property
prop_sign :: Property
prop_sign = HasCallStack => PropertyT IO () -> Property
PropertyT IO () -> Property
property (PropertyT IO () -> Property) -> PropertyT IO () -> Property
forall a b. (a -> b) -> a -> b
$ do
(VerificationKey
vk, SigningKey
sk) <- Gen (VerificationKey, SigningKey)
-> PropertyT IO (VerificationKey, SigningKey)
forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen (VerificationKey, SigningKey)
genKeypair
[Int32]
a <- Gen [Int32] -> PropertyT IO [Int32]
forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen [Int32]
genData
Bool -> PropertyT IO ()
forall (m :: * -> *). (MonadTest m, HasCallStack) => Bool -> m ()
assert
(Bool -> PropertyT IO ()) -> Bool -> PropertyT IO ()
forall a b. (a -> b) -> a -> b
$ ([Int32] -> Encoding)
-> ProtocolMagicId
-> SignTag
-> VerificationKey
-> [Int32]
-> Signature [Int32]
-> Bool
forall a.
(a -> Encoding)
-> ProtocolMagicId
-> SignTag
-> VerificationKey
-> a
-> Signature a
-> Bool
verifySignature [Int32] -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR ProtocolMagicId
Dummy.protocolMagicId SignTag
SignForTestingOnly VerificationKey
vk [Int32]
a
(Signature [Int32] -> Bool) -> Signature [Int32] -> Bool
forall a b. (a -> b) -> a -> b
$ ProtocolMagicId
-> SignTag -> SigningKey -> [Int32] -> Signature [Int32]
forall a.
EncCBOR a =>
ProtocolMagicId -> SignTag -> SigningKey -> a -> Signature a
sign ProtocolMagicId
Dummy.protocolMagicId SignTag
SignForTestingOnly SigningKey
sk [Int32]
a
prop_signDifferentKey :: Property
prop_signDifferentKey :: Property
prop_signDifferentKey = HasCallStack => PropertyT IO () -> Property
PropertyT IO () -> Property
property (PropertyT IO () -> Property) -> PropertyT IO () -> Property
forall a b. (a -> b) -> a -> b
$ do
SigningKey
sk <- Gen SigningKey -> PropertyT IO SigningKey
forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen SigningKey
genSigningKey
VerificationKey
vk <- Gen VerificationKey -> PropertyT IO VerificationKey
forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll (Gen VerificationKey -> PropertyT IO VerificationKey)
-> Gen VerificationKey -> PropertyT IO VerificationKey
forall a b. (a -> b) -> a -> b
$ (VerificationKey -> Bool)
-> Gen VerificationKey -> Gen VerificationKey
forall (m :: * -> *) a.
(MonadGen m, GenBase m ~ Identity) =>
(a -> Bool) -> m a -> m a
Gen.filter (VerificationKey -> VerificationKey -> Bool
forall a. Eq a => a -> a -> Bool
/= SigningKey -> VerificationKey
toVerification SigningKey
sk) Gen VerificationKey
genVerificationKey
[Int32]
a <- Gen [Int32] -> PropertyT IO [Int32]
forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen [Int32]
genData
Bool -> PropertyT IO ()
forall (m :: * -> *). (MonadTest m, HasCallStack) => Bool -> m ()
assert
(Bool -> PropertyT IO ())
-> (Bool -> Bool) -> Bool -> PropertyT IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Bool -> Bool
not
(Bool -> PropertyT IO ()) -> Bool -> PropertyT IO ()
forall a b. (a -> b) -> a -> b
$ ([Int32] -> Encoding)
-> ProtocolMagicId
-> SignTag
-> VerificationKey
-> [Int32]
-> Signature [Int32]
-> Bool
forall a.
(a -> Encoding)
-> ProtocolMagicId
-> SignTag
-> VerificationKey
-> a
-> Signature a
-> Bool
verifySignature [Int32] -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR ProtocolMagicId
Dummy.protocolMagicId SignTag
SignForTestingOnly VerificationKey
vk [Int32]
a
(Signature [Int32] -> Bool) -> Signature [Int32] -> Bool
forall a b. (a -> b) -> a -> b
$ ProtocolMagicId
-> SignTag -> SigningKey -> [Int32] -> Signature [Int32]
forall a.
EncCBOR a =>
ProtocolMagicId -> SignTag -> SigningKey -> a -> Signature a
sign ProtocolMagicId
Dummy.protocolMagicId SignTag
SignForTestingOnly SigningKey
sk [Int32]
a
prop_signDifferentData :: Property
prop_signDifferentData :: Property
prop_signDifferentData = HasCallStack => PropertyT IO () -> Property
PropertyT IO () -> Property
property (PropertyT IO () -> Property) -> PropertyT IO () -> Property
forall a b. (a -> b) -> a -> b
$ do
(VerificationKey
vk, SigningKey
sk) <- Gen (VerificationKey, SigningKey)
-> PropertyT IO (VerificationKey, SigningKey)
forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen (VerificationKey, SigningKey)
genKeypair
[Int32]
a <- Gen [Int32] -> PropertyT IO [Int32]
forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen [Int32]
genData
[Int32]
b <- Gen [Int32] -> PropertyT IO [Int32]
forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll (Gen [Int32] -> PropertyT IO [Int32])
-> Gen [Int32] -> PropertyT IO [Int32]
forall a b. (a -> b) -> a -> b
$ ([Int32] -> Bool) -> Gen [Int32] -> Gen [Int32]
forall (m :: * -> *) a.
(MonadGen m, GenBase m ~ Identity) =>
(a -> Bool) -> m a -> m a
Gen.filter ([Int32] -> [Int32] -> Bool
forall a. Eq a => a -> a -> Bool
/= [Int32]
a) Gen [Int32]
genData
Bool -> PropertyT IO ()
forall (m :: * -> *). (MonadTest m, HasCallStack) => Bool -> m ()
assert
(Bool -> PropertyT IO ())
-> (Bool -> Bool) -> Bool -> PropertyT IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Bool -> Bool
not
(Bool -> PropertyT IO ()) -> Bool -> PropertyT IO ()
forall a b. (a -> b) -> a -> b
$ ([Int32] -> Encoding)
-> ProtocolMagicId
-> SignTag
-> VerificationKey
-> [Int32]
-> Signature [Int32]
-> Bool
forall a.
(a -> Encoding)
-> ProtocolMagicId
-> SignTag
-> VerificationKey
-> a
-> Signature a
-> Bool
verifySignature [Int32] -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR ProtocolMagicId
Dummy.protocolMagicId SignTag
SignForTestingOnly VerificationKey
vk [Int32]
b
(Signature [Int32] -> Bool) -> Signature [Int32] -> Bool
forall a b. (a -> b) -> a -> b
$ ProtocolMagicId
-> SignTag -> SigningKey -> [Int32] -> Signature [Int32]
forall a.
EncCBOR a =>
ProtocolMagicId -> SignTag -> SigningKey -> a -> Signature a
sign ProtocolMagicId
Dummy.protocolMagicId SignTag
SignForTestingOnly SigningKey
sk [Int32]
a
genData :: Gen [Int32]
genData :: Gen [Int32]
genData = Range Int -> GenT Identity Int32 -> Gen [Int32]
forall (m :: * -> *) a. MonadGen m => Range Int -> m a -> m [a]
Gen.list (Int -> Int -> Range Int
forall a. a -> a -> Range a
Range.constant Int
0 Int
50) (Range Int32 -> GenT Identity Int32
forall (m :: * -> *). MonadGen m => Range Int32 -> m Int32
Gen.int32 Range Int32
forall a. (Bounded a, Num a) => Range a
Range.constantBounded)