Skip to content

Commit 833650d

Browse files
committed
Revert change to MockCrypto and require DSIGN only when running the KES agent
1 parent f791b9c commit 833650d

File tree

3 files changed

+54
-23
lines changed
  • ouroboros-consensus-cardano/src/unstable-shelley-testlib/Test/Consensus/Shelley
  • ouroboros-consensus-protocol/src/ouroboros-consensus-protocol/Ouroboros/Consensus/Protocol/Praos

3 files changed

+54
-23
lines changed

ouroboros-consensus-cardano/src/unstable-shelley-testlib/Test/Consensus/Shelley/MockCrypto.hs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ module Test.Consensus.Shelley.MockCrypto (
1515
, MockCrypto
1616
) where
1717

18+
import Cardano.Crypto.DSIGN (MockDSIGN)
1819
import Cardano.Crypto.KES (MockKES)
1920
import qualified Cardano.Crypto.KES as KES (Signable)
2021
import Cardano.Crypto.Util (SignableRepresentation)
2122
import Cardano.Crypto.VRF (MockVRF)
22-
import Cardano.KESAgent.Protocols.StandardCrypto (MockCrypto)
23+
import qualified Cardano.KESAgent.KES.Crypto as Agent
24+
import qualified Cardano.KESAgent.Processes.ServiceClient as Agent
25+
import qualified Cardano.KESAgent.Protocols.VersionedProtocol as Agent
2326
import Cardano.Ledger.BaseTypes (Seed)
2427
import qualified Cardano.Ledger.Shelley.API as SL
2528
import qualified Cardano.Ledger.Shelley.Core as Core
@@ -40,8 +43,16 @@ import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock,
4043
import Ouroboros.Consensus.Shelley.Protocol.Abstract (ProtoCrypto)
4144
import Test.QuickCheck (Arbitrary)
4245

46+
-- | A mock replacement for 'StandardCrypto'
47+
--
48+
-- We run the tests with this mock crypto, as it is easier to generate and
49+
-- debug things. The code is parametric in the crypto, so it shouldn't make
50+
-- much of a difference. This also has the important advantage
51+
-- that we can reuse the generators from cardano-ledger-specs.
52+
data MockCrypto
53+
4354
instance Crypto MockCrypto where
44-
type KES MockCrypto = MockKES 128
55+
type KES MockCrypto = MockKES 10
4556
type VRF MockCrypto = MockVRF
4657

4758
instance SL.PraosCrypto MockCrypto
@@ -76,5 +87,15 @@ type CanMock proto era =
7687
, Arbitrary (SL.CertState era)
7788
)
7889

90+
instance Agent.NamedCrypto MockCrypto where
91+
cryptoName _ = Agent.CryptoName "Mock"
92+
93+
instance Agent.ServiceClientDrivers MockCrypto where
94+
availableServiceClientDrivers = []
95+
96+
instance Agent.Crypto MockCrypto where
97+
type KES MockCrypto = MockKES 10
98+
type DSIGN MockCrypto = MockDSIGN
99+
79100
instance AgentCrypto MockCrypto where
80101
type ACrypto MockCrypto = MockCrypto

ouroboros-consensus-protocol/src/ouroboros-consensus-protocol/Ouroboros/Consensus/Protocol/Praos/AgentClient.hs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class ( Crypto c
6868
, Agent.Crypto (ACrypto c)
6969
, Agent.NamedCrypto (ACrypto c)
7070
, Agent.KES (ACrypto c) ~ KES c
71-
, Agent.DSIGN (ACrypto c) ~ DSIGN
71+
7272
, ContextKES (KES c) ~ ()
7373
, ContextVRF (VRF c) ~ ()
7474
, Typeable (ACrypto c)
@@ -77,25 +77,23 @@ class ( Crypto c
7777
, DirectDeserialise (SignKeyKES (KES c))
7878
)
7979
=> AgentCrypto c where
80-
type ACrypto c :: Type
80+
type ACrypto c :: Type
8181

8282
instance AgentCrypto StandardCrypto where
8383
type ACrypto StandardCrypto = Agent.StandardCrypto
8484

85+
convertOCert :: (AgentCrypto c, Agent.DSIGN (ACrypto c) ~ DSIGN) => Agent.OCert (ACrypto c) -> OCert.OCert c
86+
convertOCert oca =
87+
OCert.OCert
88+
{ OCert.ocertVkHot = Agent.ocertVkHot oca
89+
, OCert.ocertN = Agent.ocertN oca
90+
, OCert.ocertKESPeriod = OCert.KESPeriod (Agent.unKESPeriod $ Agent.ocertKESPeriod oca)
91+
, OCert.ocertSigma = coerce (Agent.ocertSigma oca)
92+
}
93+
8594
convertPeriod :: Agent.KESPeriod -> OCert.KESPeriod
8695
convertPeriod (Agent.KESPeriod p) = OCert.KESPeriod p
8796

88-
convertOCert :: ( AgentCrypto c
89-
)
90-
=> Agent.OCert (ACrypto c) -> OCert.OCert c
91-
convertOCert oca =
92-
OCert.OCert
93-
{ OCert.ocertVkHot = Agent.ocertVkHot oca
94-
, OCert.ocertN = Agent.ocertN oca
95-
, OCert.ocertKESPeriod = OCert.KESPeriod (Agent.unKESPeriod $ Agent.ocertKESPeriod oca)
96-
, OCert.ocertSigma = coerce (Agent.ocertSigma oca)
97-
}
98-
9997
class (MonadFail m, Show (Addr m)) => MonadKESAgent m where
10098
type FD m :: Type
10199
type Addr m :: Type
@@ -130,6 +128,7 @@ instance SimSnocket.GlobalAddressScheme FilePath where
130128

131129
runKESAgentClient :: forall m c.
132130
( KESAgentContext c m
131+
, Agent.DSIGN (ACrypto c) ~ DSIGN
133132
)
134133
=> Tracer m KESAgentClientTrace
135134
-> FilePath

ouroboros-consensus-protocol/src/ouroboros-consensus-protocol/Ouroboros/Consensus/Protocol/Praos/Common.hs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
{-# LANGUAGE DeriveGeneric #-}
44
{-# LANGUAGE DerivingVia #-}
55
{-# LANGUAGE FlexibleContexts #-}
6+
{-# LANGUAGE GADTs #-}
67
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
8+
{-# LANGUAGE LambdaCase #-}
79
{-# LANGUAGE ScopedTypeVariables #-}
810
{-# LANGUAGE TypeApplications #-}
911
{-# LANGUAGE TypeFamilies #-}
@@ -26,9 +28,10 @@ module Ouroboros.Consensus.Protocol.Praos.Common (
2628
import qualified Cardano.Crypto.KES.Class as KES
2729
import Cardano.Crypto.VRF
2830
import qualified Cardano.Crypto.VRF as VRF
31+
import qualified Cardano.KESAgent.KES.Crypto as Agent
2932
import Cardano.Ledger.BaseTypes (Nonce)
3033
import qualified Cardano.Ledger.BaseTypes as SL
31-
import Cardano.Ledger.Keys (KeyHash, KeyRole (BlockIssuer))
34+
import Cardano.Ledger.Keys (DSIGN, KeyHash, KeyRole (BlockIssuer))
3235
import qualified Cardano.Ledger.Shelley.API as SL
3336
import Cardano.Protocol.Crypto (Crypto, KES, VRF)
3437
import qualified Cardano.Protocol.TPraos.OCert as OCert
@@ -40,6 +43,7 @@ import Data.Map.Strict (Map)
4043
import Data.Ord (Down (Down))
4144
import Data.Word (Word64)
4245
import GHC.Generics (Generic)
46+
import NoThunks.Class
4347
import Ouroboros.Consensus.Protocol.Abstract
4448
import qualified Ouroboros.Consensus.Protocol.Ledger.HotKey as HotKey
4549
import Ouroboros.Consensus.Protocol.Praos.AgentClient
@@ -267,18 +271,25 @@ instance (NoThunks (SignKeyVRF (VRF c)), NoThunks (KES.UnsoundPureSignKeyKES (KE
267271

268272
-- | Defines a method for obtaining Praos credentials (opcert + KES signing
269273
-- key).
270-
data PraosCredentialsSource c
271-
= -- | Pass an opcert and sign key directly. This uses
274+
data PraosCredentialsSource c where
275+
-- | Pass an opcert and sign key directly. This uses
272276
-- 'KES.UnsoundPureSignKeyKES', which does not provide mlocking guarantees,
273277
-- violating the rule that KES secrets must never be stored on disk, but
274278
-- allows the sign key to be loaded from a local file. This method is
275279
-- provided for backwards compatibility.
276-
PraosCredentialsUnsound (OCert.OCert c) (KES.UnsoundPureSignKeyKES (KES c))
277-
| -- | Connect to a KES agent listening on a service socket at the given path.
278-
PraosCredentialsAgent FilePath
279-
deriving (Generic)
280+
PraosCredentialsUnsound :: OCert.OCert c -> KES.UnsoundPureSignKeyKES (KES c) -> PraosCredentialsSource c
281+
-- | Connect to a KES agent listening on a service socket at the given path.
282+
PraosCredentialsAgent :: Agent.DSIGN (ACrypto c) ~ DSIGN => FilePath -> PraosCredentialsSource c
283+
284+
instance (NoThunks (KES.UnsoundPureSignKeyKES (KES c)), Crypto c) => NoThunks (PraosCredentialsSource c) where
285+
wNoThunks ctxt = \case
286+
PraosCredentialsUnsound oca k -> allNoThunks [
287+
noThunks ctxt oca
288+
, noThunks ctxt k
289+
]
290+
PraosCredentialsAgent fp -> noThunks ctxt fp
280291

281-
instance (NoThunks (KES.UnsoundPureSignKeyKES (KES c)), Crypto c) => NoThunks (PraosCredentialsSource c)
292+
showTypeOf _ = "PraosCredentialsSource"
282293

283294
instantiatePraosCredentials :: forall m c.
284295
( KESAgentContext c m

0 commit comments

Comments
 (0)