From ffa4c9eac06b8046ae159a5a2ef96ca86b53227e Mon Sep 17 00:00:00 2001 From: Sebastian Nagel Date: Tue, 11 Jul 2023 17:50:12 +0200 Subject: [PATCH 1/2] Fix compilation of CryptoSpec --- hydra-node/test/Hydra/CryptoSpec.hs | 37 ++++++++++++++--------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/hydra-node/test/Hydra/CryptoSpec.hs b/hydra-node/test/Hydra/CryptoSpec.hs index cfc2c7f88d8..aa479b9da7f 100644 --- a/hydra-node/test/Hydra/CryptoSpec.hs +++ b/hydra-node/test/Hydra/CryptoSpec.hs @@ -13,18 +13,20 @@ import Cardano.Crypto.PinnedSizedBytes (psbFromByteString) import qualified Data.ByteString as BS import qualified Data.ByteString.Char8 as Char8 import qualified Data.Map as Map +import Hydra.Party (Party (vkey), deriveParty) import Test.Aeson.GenericSpecs (roundtripAndGoldenSpecs) import Test.QuickCheck ( - conjoin, counterexample, + elements, forAll, shuffle, + sublistOf, (=/=), - (==>), chooseInt, + (===), + (==>), ) import Test.QuickCheck.Instances.UnorderedContainers () import Test.Util (propCollisionResistant) -import Data.List ((!!)) spec :: Spec spec = do @@ -92,24 +94,21 @@ specMultiSignature = not (null shuffled) ==> verifyMultiSignature (map vkey shuffled) (aggregateInOrder sigs shuffled) msg === Verified - in verifyMultiSignature vks msig msg prop "verifyMultiSignature fails when signature is missing" $ \sks (msg :: ByteString) dummySig -> - (length sks > 2) ==> - forAll (elements sks) $ \missingKeySig -> - sigs = (\sk -> if sk /= missingKeySig then sign sk msg else dummySig) <$> sks - in not (verifyMultiSignature (map getVerificationKey sks) (aggregate sigs) msg) + (length sks > 2) + ==> forAll (elements sks) + $ \missingKeySig -> + let sigs = (\sk -> if sk /= missingKeySig then sign sk msg else dummySig) <$> sks + in verifyMultiSignature (map getVerificationKey sks) (aggregate sigs) msg + =/= Verified prop "does not validate multisig if less keys given" $ \sks (msg :: ByteString) -> do (length sks > 1) - properPrefixes <- sublistOf sks - ==> let sigs = aggregate $ map (`sign` msg) (toList sks) - conjoin - ( map - ( \prefix -> - not (verifyMultiSignature (map getVerificationKey prefix) sigs msg) - & counterexample ("Prefix: " <> show prefix) - & counterexample ("Signature: " <> show sigs) - ) - properPrefixes - ) + ==> forAll (sublistOf sks) + $ \prefix -> + let sigs = aggregate $ map (`sign` msg) (toList sks) + in verifyMultiSignature (map getVerificationKey prefix) sigs msg + =/= Verified + & counterexample ("Keys: " <> show prefix) + & counterexample ("Signature: " <> show sigs) From fbaa3eb44ee01a305c2a7fc1d761249222533066 Mon Sep 17 00:00:00 2001 From: Sebastian Nagel Date: Tue, 11 Jul 2023 17:54:30 +0200 Subject: [PATCH 2/2] Test strictly smaller sub lists and classify results sublistOf is also sometimes yielding the same list, so we discard same length prefixes. Also, classifying empty lists helps in ensuring we have actually non trivial cases. --- hydra-node/test/Hydra/CryptoSpec.hs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/hydra-node/test/Hydra/CryptoSpec.hs b/hydra-node/test/Hydra/CryptoSpec.hs index aa479b9da7f..a0169ff56b0 100644 --- a/hydra-node/test/Hydra/CryptoSpec.hs +++ b/hydra-node/test/Hydra/CryptoSpec.hs @@ -16,6 +16,7 @@ import qualified Data.Map as Map import Hydra.Party (Party (vkey), deriveParty) import Test.Aeson.GenericSpecs (roundtripAndGoldenSpecs) import Test.QuickCheck ( + classify, counterexample, elements, forAll, @@ -104,11 +105,11 @@ specMultiSignature = =/= Verified prop "does not validate multisig if less keys given" $ \sks (msg :: ByteString) -> do - (length sks > 1) - ==> forAll (sublistOf sks) - $ \prefix -> - let sigs = aggregate $ map (`sign` msg) (toList sks) - in verifyMultiSignature (map getVerificationKey prefix) sigs msg - =/= Verified - & counterexample ("Keys: " <> show prefix) - & counterexample ("Signature: " <> show sigs) + forAll (sublistOf sks) $ \prefix -> + (length prefix < length sks) + ==> let sigs = aggregate $ map (`sign` msg) (toList sks) + in verifyMultiSignature (map getVerificationKey prefix) sigs msg + =/= Verified + & classify (null prefix) "empty" + & counterexample ("Keys: " <> show prefix) + & counterexample ("Signature: " <> show sigs)