{-# LANGUAGE TemplateHaskell #-}

module Test.Cardano.Crypto.Signing.Redeem (
  tests,
) where

import Cardano.Crypto.Signing (SignTag (..))
import Cardano.Crypto.Signing.Redeem (
  redeemSign,
  redeemToVerification,
  verifyRedeemSig,
 )
import Cardano.Prelude
import Hedgehog (
  Property,
  assert,
  checkParallel,
  discover,
  forAll,
  property,
 )
import qualified Hedgehog.Gen as Gen
import qualified Test.Cardano.Crypto.Dummy as Dummy
import Test.Cardano.Crypto.Gen (
  genRedeemKeypair,
  genRedeemSigningKey,
  genRedeemVerificationKey,
 )
import Test.Cardano.Crypto.Signing.Signing (genData)

--------------------------------------------------------------------------------
-- Main Test Action
--------------------------------------------------------------------------------

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_redeemSign :: Property
prop_redeemSignDifferentKey :: Property
prop_redeemSignDifferentData :: Property
discover

--------------------------------------------------------------------------------
-- Redeem Signature Properties
--------------------------------------------------------------------------------

-- | Signing and verification with a redeem keys works
prop_redeemSign :: Property
prop_redeemSign :: Property
prop_redeemSign = HasCallStack => PropertyT IO () -> Property
PropertyT IO () -> Property
property (PropertyT IO () -> Property) -> PropertyT IO () -> Property
forall a b. (a -> b) -> a -> b
$ do
  (vk, sk) <- Gen (RedeemVerificationKey, RedeemSigningKey)
-> PropertyT IO (RedeemVerificationKey, RedeemSigningKey)
forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen (RedeemVerificationKey, RedeemSigningKey)
genRedeemKeypair
  a <- forAll genData

  assert
    $ verifyRedeemSig Dummy.protocolMagicId SignForTestingOnly vk a
    $ redeemSign Dummy.protocolMagicId SignForTestingOnly sk a

-- | Signing fails when the wrong 'RedeemVerificationKey' is used
prop_redeemSignDifferentKey :: Property
prop_redeemSignDifferentKey :: Property
prop_redeemSignDifferentKey = HasCallStack => PropertyT IO () -> Property
PropertyT IO () -> Property
property (PropertyT IO () -> Property) -> PropertyT IO () -> Property
forall a b. (a -> b) -> a -> b
$ do
  sk <- Gen RedeemSigningKey -> PropertyT IO RedeemSigningKey
forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen RedeemSigningKey
genRedeemSigningKey
  vk <- forAll $ Gen.filter (/= redeemToVerification sk) genRedeemVerificationKey
  a <- forAll genData

  assert
    . not
    $ verifyRedeemSig Dummy.protocolMagicId SignForTestingOnly vk a
    $ redeemSign Dummy.protocolMagicId SignForTestingOnly sk a

-- | Signing fails when then wrong signature data is used
prop_redeemSignDifferentData :: Property
prop_redeemSignDifferentData :: Property
prop_redeemSignDifferentData = HasCallStack => PropertyT IO () -> Property
PropertyT IO () -> Property
property (PropertyT IO () -> Property) -> PropertyT IO () -> Property
forall a b. (a -> b) -> a -> b
$ do
  (vk, sk) <- Gen (RedeemVerificationKey, RedeemSigningKey)
-> PropertyT IO (RedeemVerificationKey, RedeemSigningKey)
forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen (RedeemVerificationKey, RedeemSigningKey)
genRedeemKeypair
  a <- forAll genData
  b <- forAll $ Gen.filter (/= a) genData

  assert
    . not
    $ verifyRedeemSig Dummy.protocolMagicId SignForTestingOnly vk b
    $ redeemSign Dummy.protocolMagicId SignForTestingOnly sk a