11{-# LANGUAGE ScopedTypeVariables #-}
2+ {-# LANGUAGE TypeApplications #-}
23
34module Cardano.Ledger.Binary.Crypto (
4- -- * DSIGN
5- encodeVerKeyDSIGN ,
6- decodeVerKeyDSIGN ,
7- encodeSignKeyDSIGN ,
8- decodeSignKeyDSIGN ,
9- encodeSigDSIGN ,
10- decodeSigDSIGN ,
11- encodeSignedDSIGN ,
5+ encFixed ,
6+ decFixed ,
127 decodeSignedDSIGN ,
13-
14- -- * KES
15- encodeVerKeyKES ,
16- decodeVerKeyKES ,
17- encodeSigKES ,
18- decodeSigKES ,
19- encodeSignedKES ,
20- decodeSignedKES ,
21-
22- -- * VRF
23- encodeVerKeyVRF ,
24- decodeVerKeyVRF ,
25- encodeSignKeyVRF ,
26- decodeSignKeyVRF ,
27- encodeCertVRF ,
28- decodeCertVRF ,
8+ encodeSignedDSIGN ,
299) where
3010
3111import qualified Cardano.Crypto.DSIGN.Class as C
32- import qualified Cardano.Crypto.KES.Class as C
33- import qualified Cardano.Crypto.VRF.Class as C
34- import Cardano.Ledger.Binary.Decoding.Decoder (Decoder , fromPlainDecoder )
12+ import Cardano.Crypto.FixedSizeBytes (FixedSizeBytes (.. ))
13+ import Cardano.Ledger.Binary.Decoding.Decoder (
14+ Decoder ,
15+ DecoderError (.. ),
16+ cborError ,
17+ decodeBytes ,
18+ fromPlainDecoder ,
19+ )
3520import Cardano.Ledger.Binary.Encoding.Encoder (Encoding , fromPlainEncoding )
36-
37- --------------------------------------------------------------------------------
38- -- DSIGN
39- --------------------------------------------------------------------------------
40-
41- encodeVerKeyDSIGN :: C. DSIGNAlgorithm v => C. VerKeyDSIGN v -> Encoding
42- encodeVerKeyDSIGN = fromPlainEncoding . C. encodeVerKeyDSIGN
43- {-# INLINE encodeVerKeyDSIGN #-}
44-
45- decodeVerKeyDSIGN :: C. DSIGNAlgorithm v => Decoder s (C. VerKeyDSIGN v )
46- decodeVerKeyDSIGN = fromPlainDecoder C. decodeVerKeyDSIGN
47- {-# INLINE decodeVerKeyDSIGN #-}
48-
49- encodeSignKeyDSIGN :: C. DSIGNAlgorithm v => C. SignKeyDSIGN v -> Encoding
50- encodeSignKeyDSIGN = fromPlainEncoding . C. encodeSignKeyDSIGN
51- {-# INLINE encodeSignKeyDSIGN #-}
52-
53- decodeSignKeyDSIGN :: C. DSIGNAlgorithm v => Decoder s (C. SignKeyDSIGN v )
54- decodeSignKeyDSIGN = fromPlainDecoder C. decodeSignKeyDSIGN
55- {-# INLINE decodeSignKeyDSIGN #-}
56-
57- encodeSigDSIGN :: C. DSIGNAlgorithm v => C. SigDSIGN v -> Encoding
58- encodeSigDSIGN = fromPlainEncoding . C. encodeSigDSIGN
59- {-# INLINE encodeSigDSIGN #-}
60-
61- decodeSigDSIGN :: C. DSIGNAlgorithm v => Decoder s (C. SigDSIGN v )
62- decodeSigDSIGN = fromPlainDecoder C. decodeSigDSIGN
63- {-# INLINE decodeSigDSIGN #-}
21+ import qualified Data.ByteString as BS
22+ import Data.Proxy (Proxy (.. ))
23+ import qualified Data.Text as T
24+ import Data.Typeable (typeRep )
25+ import GHC.TypeLits (natVal )
6426
6527encodeSignedDSIGN :: C. DSIGNAlgorithm v => C. SignedDSIGN v a -> Encoding
6628encodeSignedDSIGN = fromPlainEncoding . C. encodeSignedDSIGN
@@ -70,58 +32,18 @@ decodeSignedDSIGN :: C.DSIGNAlgorithm v => Decoder s (C.SignedDSIGN v a)
7032decodeSignedDSIGN = fromPlainDecoder C. decodeSignedDSIGN
7133{-# INLINE decodeSignedDSIGN #-}
7234
73- --------------------------------------------------------------------------------
74- -- KES
75- --------------------------------------------------------------------------------
76-
77- encodeVerKeyKES :: C. KESAlgorithm v => C. VerKeyKES v -> Encoding
78- encodeVerKeyKES = fromPlainEncoding . C. encodeVerKeyKES
79- {-# INLINE encodeVerKeyKES #-}
80-
81- decodeVerKeyKES :: C. KESAlgorithm v => Decoder s (C. VerKeyKES v )
82- decodeVerKeyKES = fromPlainDecoder C. decodeVerKeyKES
83- {-# INLINE decodeVerKeyKES #-}
84-
85- encodeSigKES :: C. KESAlgorithm v => C. SigKES v -> Encoding
86- encodeSigKES = fromPlainEncoding . C. encodeSigKES
87- {-# INLINE encodeSigKES #-}
88-
89- decodeSigKES :: C. KESAlgorithm v => Decoder s (C. SigKES v )
90- decodeSigKES = fromPlainDecoder C. decodeSigKES
91- {-# INLINE decodeSigKES #-}
92-
93- encodeSignedKES :: C. KESAlgorithm v => C. SignedKES v a -> Encoding
94- encodeSignedKES = fromPlainEncoding . C. encodeSignedKES
95- {-# INLINE encodeSignedKES #-}
96-
97- decodeSignedKES :: C. KESAlgorithm v => Decoder s (C. SignedKES v a )
98- decodeSignedKES = fromPlainDecoder C. decodeSignedKES
99- {-# INLINE decodeSignedKES #-}
100-
101- --------------------------------------------------------------------------------
102- -- VRF
103- --------------------------------------------------------------------------------
104-
105- encodeVerKeyVRF :: C. VRFAlgorithm v => C. VerKeyVRF v -> Encoding
106- encodeVerKeyVRF = fromPlainEncoding . C. encodeVerKeyVRF
107- {-# INLINE encodeVerKeyVRF #-}
108-
109- decodeVerKeyVRF :: C. VRFAlgorithm v => Decoder s (C. VerKeyVRF v )
110- decodeVerKeyVRF = fromPlainDecoder C. decodeVerKeyVRF
111- {-# INLINE decodeVerKeyVRF #-}
112-
113- encodeSignKeyVRF :: C. VRFAlgorithm v => C. SignKeyVRF v -> Encoding
114- encodeSignKeyVRF = fromPlainEncoding . C. encodeSignKeyVRF
115- {-# INLINE encodeSignKeyVRF #-}
116-
117- decodeSignKeyVRF :: C. VRFAlgorithm v => Decoder s (C. SignKeyVRF v )
118- decodeSignKeyVRF = fromPlainDecoder C. decodeSignKeyVRF
119- {-# INLINE decodeSignKeyVRF #-}
120-
121- encodeCertVRF :: C. VRFAlgorithm v => C. CertVRF v -> Encoding
122- encodeCertVRF = fromPlainEncoding . C. encodeCertVRF
123- {-# INLINE encodeCertVRF #-}
124-
125- decodeCertVRF :: C. VRFAlgorithm v => Decoder s (C. CertVRF v )
126- decodeCertVRF = fromPlainDecoder C. decodeCertVRF
127- {-# INLINE decodeCertVRF #-}
35+ encFixed :: FixedSizeBytes a => a -> Encoding
36+ encFixed = fromPlainEncoding . encodeFixed
37+
38+ decFixed :: forall a s . FixedSizeBytes a => Decoder s a
39+ decFixed = do
40+ let p = Proxy @ a
41+ bs <- decodeBytes
42+ case rawDecodeFixed bs of
43+ Just vk -> pure vk
44+ Nothing ->
45+ cborError $
46+ DecoderErrorSizeMismatch
47+ (T. pack . show $ typeRep p)
48+ (fromIntegral . natVal $ Proxy @ (FixedSize a ))
49+ (BS. length bs)
0 commit comments