Skip to content

Commit

Permalink
Reinstate CBOR encoding / decoding for SignKeyKES
Browse files Browse the repository at this point in the history
  • Loading branch information
tdammers committed Sep 27, 2021
1 parent 0c071a8 commit edf5a1d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
17 changes: 16 additions & 1 deletion cardano-crypto-class/src/Cardano/Crypto/KES/Class.hs
Expand Up @@ -26,6 +26,8 @@ module Cardano.Crypto.KES.Class
, decodeVerKeyKES
, encodeSigKES
, decodeSigKES
, encodeSignKeyKES
, decodeSignKeyKES
, encodeSignedKES
, decodeSignedKES

Expand Down Expand Up @@ -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
Expand All @@ -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.
--
Expand Down
4 changes: 4 additions & 0 deletions cardano-crypto-tests/src/Test/Crypto/KES.hs
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit edf5a1d

Please sign in to comment.