From edf5a1db2c2ae68d644aa135f2035159958be9cd Mon Sep 17 00:00:00 2001 From: Tobias Dammers Date: Fri, 29 Jan 2021 11:50:13 +0100 Subject: [PATCH] Reinstate CBOR encoding / decoding for SignKeyKES --- .../src/Cardano/Crypto/KES/Class.hs | 17 ++++++++++++++++- cardano-crypto-tests/src/Test/Crypto/KES.hs | 4 ++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cardano-crypto-class/src/Cardano/Crypto/KES/Class.hs b/cardano-crypto-class/src/Cardano/Crypto/KES/Class.hs index e63888396..542d185ac 100644 --- a/cardano-crypto-class/src/Cardano/Crypto/KES/Class.hs +++ b/cardano-crypto-class/src/Cardano/Crypto/KES/Class.hs @@ -26,6 +26,8 @@ module Cardano.Crypto.KES.Class , decodeVerKeyKES , encodeSigKES , decodeSigKES + , encodeSignKeyKES + , decodeSignKeyKES , encodeSignedKES , decodeSignedKES @@ -234,6 +236,9 @@ encodeVerKeyKES = encodeBytes . rawSerialiseVerKeyKES encodeSigKES :: KESAlgorithm v => SigKES v -> Encoding encodeSigKES = encodeBytes . rawSerialiseSigKES +encodeSignKeyKES :: KESAlgorithm v => SignKeyKES v -> SignKeyAccessKES v Encoding +encodeSignKeyKES = fmap encodeBytes . rawSerialiseSignKeyKES + decodeVerKeyKES :: forall v s. KESAlgorithm v => Decoder s (VerKeyKES v) decodeVerKeyKES = do bs <- decodeBytes @@ -257,11 +262,21 @@ decodeSigKES = do | actual /= expected -> fail ("decodeSigKES: wrong length, expected " ++ show expected ++ " bytes but got " ++ show actual) - | otherwise -> fail "decodeSigKES: cannot decode key" + | otherwise -> fail "decodeSigKES: cannot decode signature" where expected = fromIntegral (sizeSigKES (Proxy :: Proxy v)) actual = BS.length bs +decodeSignKeyKES :: forall v s. KESAlgorithm v => Decoder s (SignKeyAccessKES v (Maybe (SignKeyKES v))) +decodeSignKeyKES = do + bs <- decodeBytes + let expected = fromIntegral (sizeSignKeyKES (Proxy :: Proxy v)) + actual = BS.length bs + if actual /= expected then + fail ("decodeSigKES: wrong length, expected " ++ + show expected ++ " bytes but got " ++ show actual) + else + return $ rawDeserialiseSignKeyKES bs -- | The KES period. Periods are enumerated from zero. -- diff --git a/cardano-crypto-tests/src/Test/Crypto/KES.hs b/cardano-crypto-tests/src/Test/Crypto/KES.hs index 9d49b4d73..83c5fe1c1 100644 --- a/cardano-crypto-tests/src/Test/Crypto/KES.hs +++ b/cardano-crypto-tests/src/Test/Crypto/KES.hs @@ -24,6 +24,7 @@ import qualified Data.Set as Set import Foreign.Ptr (WordPtr) import System.IO.Unsafe (unsafePerformIO) import Data.IORef +import Data.Maybe (fromJust) import Control.Exception (evaluate) import Control.Concurrent (threadDelay) @@ -252,6 +253,9 @@ testKESAlgorithm _p n = , testProperty "Sig" $ prop_cbor_with @(SigKES v) encodeSigKES decodeSigKES + , testProperty "SignKey" $ prop_cbor_with @(SignKeyKES v) + (unsafePerformIO . io . encodeSignKeyKES) + (fromJust . unsafePerformIO . io <$> decodeSignKeyKES) ] , testGroup "To/FromCBOR class"