From 9d2f076933bcb38ae468d8edb814cf6f9515ab40 Mon Sep 17 00:00:00 2001 From: Alexey Kuleshevich Date: Tue, 17 Sep 2024 17:32:27 -0600 Subject: [PATCH 01/52] Update to an unreleased version of ledger with mempack usage --- cabal.project | 33 +++++++++++++++++++ .../Ouroboros/Consensus/Util/Orphans.hs | 5 +-- .../Ouroboros/Consensus/Mock/Ledger/Block.hs | 16 ++++++--- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/cabal.project b/cabal.project index 6ecd7821eb..62ee4b8fc8 100644 --- a/cabal.project +++ b/cabal.project @@ -44,3 +44,36 @@ package ouroboros-network if(os(windows)) constraints: bitvec -simd + +source-repository-package + type: git + location: https://github.com/IntersectMBO/cardano-ledger + tag: c50d89688d9f30ea2dbd01afb19dbcaaf03e3da7 + --sha256: sha256-3OVXLYCKSN4HPd3nsObK2mG8mB28AX46vuMqs+Jn3kw= + subdir: + eras/allegra/impl + eras/alonzo/impl + eras/alonzo/test-suite + eras/babbage/impl + eras/babbage/test-suite + eras/conway/impl + eras/conway/test-suite + eras/mary/impl + eras/shelley/impl + eras/shelley/test-suite + eras/shelley-ma/test-suite + libs/cardano-ledger-api + libs/cardano-ledger-core + libs/cardano-ledger-binary + libs/cardano-protocol-tpraos + libs/non-integral + libs/small-steps + libs/cardano-data + libs/set-algebra + libs/vector-map + eras/byron/chain/executable-spec + eras/byron/ledger/executable-spec + eras/byron/ledger/impl + eras/byron/ledger/impl/test + eras/byron/crypto + eras/byron/crypto/test diff --git a/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Util/Orphans.hs b/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Util/Orphans.hs index 0ba537b87b..ea3bd3ecdb 100644 --- a/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Util/Orphans.hs +++ b/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Util/Orphans.hs @@ -14,7 +14,7 @@ module Ouroboros.Consensus.Util.Orphans () where import Cardano.Crypto.DSIGN.Class import Cardano.Crypto.DSIGN.Mock (MockDSIGN) -import Cardano.Crypto.Hash (Hash) +import Cardano.Crypto.Hash (Hash, SizeHash) import Cardano.Ledger.Genesis (NoGenesis (..)) import Codec.CBOR.Decoding (Decoder) import Codec.Serialise (Serialise (..)) @@ -26,6 +26,7 @@ import qualified Data.IntPSQ as PSQ import Data.MultiSet (MultiSet) import qualified Data.MultiSet as MultiSet import Data.SOP.BasicFunctors +import GHC.TypeLits (KnownNat) import NoThunks.Class (InspectHeap (..), InspectHeapNamed (..), NoThunks (..), OnlyCheckWhnfNamed (..), allNoThunks, noThunksInKeysAndValues) @@ -38,7 +39,7 @@ import System.FS.CRC (CRC (CRC)) Serialise -------------------------------------------------------------------------------} -instance Serialise (Hash h a) where +instance KnownNat (SizeHash h) => Serialise (Hash h a) where instance Serialise (VerKeyDSIGN MockDSIGN) where encode = encodeVerKeyDSIGN diff --git a/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Ledger/Block.hs b/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Ledger/Block.hs index f6db8e90cf..386eb05aa2 100644 --- a/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Ledger/Block.hs +++ b/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Ledger/Block.hs @@ -76,6 +76,7 @@ import Data.Proxy import Data.Typeable import Data.Word import GHC.Generics (Generic) +import GHC.TypeNats (KnownNat) import NoThunks.Class (NoThunks (..)) import Ouroboros.Consensus.Block import Ouroboros.Consensus.Config @@ -170,7 +171,10 @@ data SimpleStdHeader c ext = SimpleStdHeader { , simpleBodySize :: SizeInBytes } deriving stock (Generic, Show, Eq) - deriving anyclass (Serialise, NoThunks) + deriving anyclass (NoThunks) + +deriving anyclass instance KnownNat (Hash.SizeHash (SimpleHash c)) => + Serialise (SimpleStdHeader c ext) data SimpleBody = SimpleBody { simpleTxs :: [Mock.Tx] @@ -367,7 +371,10 @@ newtype instance LedgerState (SimpleBlock c ext) = SimpleLedgerState { simpleLedgerState :: MockState (SimpleBlock c ext) } deriving stock (Generic, Show, Eq) - deriving newtype (Serialise, NoThunks) + deriving newtype (NoThunks) + +deriving anyclass instance KnownNat (Hash.SizeHash (SimpleHash c)) => + Serialise (LedgerState (SimpleBlock c ext)) -- Ticking has no effect on the simple ledger state newtype instance Ticked (LedgerState (SimpleBlock c ext)) = TickedSimpleLedgerState { @@ -541,7 +548,7 @@ instance InspectLedger (SimpleBlock c ext) where Crypto needed for simple blocks -------------------------------------------------------------------------------} -class (HashAlgorithm (SimpleHash c), Typeable c) => SimpleCrypto c where +class (KnownNat (Hash.SizeHash (SimpleHash c)), HashAlgorithm (SimpleHash c), Typeable c) => SimpleCrypto c where type family SimpleHash c :: Type data SimpleStandardCrypto @@ -598,7 +605,8 @@ instance Condense ext' => Condense (SimpleBlock' c ext ext') where instance ToCBOR SimpleBody where toCBOR = encode -encodeSimpleHeader :: (ext' -> CBOR.Encoding) +encodeSimpleHeader :: KnownNat (Hash.SizeHash (SimpleHash c)) + => (ext' -> CBOR.Encoding) -> Header (SimpleBlock' c ext ext') -> CBOR.Encoding encodeSimpleHeader encodeExt SimpleHeader{..} = mconcat [ From 773047feb6d73a5e3574217d98f2be35510fc4e9 Mon Sep 17 00:00:00 2001 From: Alexey Kuleshevich Date: Tue, 28 Jan 2025 14:40:26 -0700 Subject: [PATCH 02/52] update srp --- cabal.project | 60 ++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/cabal.project b/cabal.project index 62ee4b8fc8..99959c30f3 100644 --- a/cabal.project +++ b/cabal.project @@ -47,33 +47,35 @@ if(os(windows)) source-repository-package type: git - location: https://github.com/IntersectMBO/cardano-ledger - tag: c50d89688d9f30ea2dbd01afb19dbcaaf03e3da7 - --sha256: sha256-3OVXLYCKSN4HPd3nsObK2mG8mB28AX46vuMqs+Jn3kw= + location: https://github.com/input-output-hk/cardano-ledger + tag: 92007c180df876775c48c3c5128323eb5449c2c2 + --sha256: sha256-rZUw+rn0xqe8ee7vUlCQm9fYXCDiWZtZNqorWAtwiFY= subdir: - eras/allegra/impl - eras/alonzo/impl - eras/alonzo/test-suite - eras/babbage/impl - eras/babbage/test-suite - eras/conway/impl - eras/conway/test-suite - eras/mary/impl - eras/shelley/impl - eras/shelley/test-suite - eras/shelley-ma/test-suite - libs/cardano-ledger-api - libs/cardano-ledger-core - libs/cardano-ledger-binary - libs/cardano-protocol-tpraos - libs/non-integral - libs/small-steps - libs/cardano-data - libs/set-algebra - libs/vector-map - eras/byron/chain/executable-spec - eras/byron/ledger/executable-spec - eras/byron/ledger/impl - eras/byron/ledger/impl/test - eras/byron/crypto - eras/byron/crypto/test + eras/allegra/impl + eras/alonzo/impl + eras/alonzo/test-suite + eras/babbage/impl + eras/babbage/test-suite + eras/byron/chain/executable-spec + eras/byron/crypto + eras/byron/crypto/test + eras/byron/ledger/executable-spec + eras/byron/ledger/impl + eras/byron/ledger/impl/test + eras/conway/impl + eras/conway/test-suite + eras/mary/impl + eras/shelley/impl + eras/shelley-ma/test-suite + eras/shelley/test-suite + libs/cardano-data + libs/cardano-ledger-api + libs/cardano-ledger-binary + libs/cardano-ledger-core + libs/cardano-ledger-test + libs/cardano-protocol-tpraos + libs/constrained-generators + libs/non-integral + libs/set-algebra + libs/small-steps + libs/vector-map From 518b7f17ea6e39d1b39b47fdc45a59687580f1d3 Mon Sep 17 00:00:00 2001 From: Alexey Kuleshevich Date: Tue, 28 Jan 2025 18:08:39 -0700 Subject: [PATCH 03/52] Update to use newest cardano-crypto-class with unsound pure KES implementation --- cabal.project | 12 +++++++-- .../ouroboros-consensus-cardano.cabal | 6 ++--- .../Ouroboros/Consensus/Byron/Crypto/DSIGN.hs | 2 +- .../Consensus/Shelley/Node/Common.hs | 4 ++- .../Cardano/Api/KeysPraos.hs | 16 ++++++------ .../Test/ThreadNet/Infra/Shelley.hs | 9 ++++--- .../ouroboros-consensus-protocol.cabal | 2 +- .../Consensus/Protocol/Ledger/HotKey.hs | 16 ++++++------ .../Protocol/Serialisation/Generators.hs | 4 +-- .../Consensus/Protocol/Praos/Header.hs | 25 +++++++++---------- ouroboros-consensus/ouroboros-consensus.cabal | 2 +- .../Ouroboros/Consensus/Mock/Node/Praos.hs | 2 +- .../Consensus/Mock/Protocol/Praos.hs | 16 ++++++------ 13 files changed, 65 insertions(+), 51 deletions(-) diff --git a/cabal.project b/cabal.project index 99959c30f3..07c8fe000c 100644 --- a/cabal.project +++ b/cabal.project @@ -45,11 +45,19 @@ if(os(windows)) constraints: bitvec -simd +source-repository-package + type: git + location: https://github.com/input-output-hk/cardano-base + tag: b2cec3fbcde4bacb9c961e5510d5a1d3754c4e2b + --sha256: sha256-TDEBINZ3SkhpRNomMdt53bR3gdzgkWR9jIlAr8yrU6o= + subdir: + cardano-crypto-class + source-repository-package type: git location: https://github.com/input-output-hk/cardano-ledger - tag: 92007c180df876775c48c3c5128323eb5449c2c2 - --sha256: sha256-rZUw+rn0xqe8ee7vUlCQm9fYXCDiWZtZNqorWAtwiFY= + tag: 9d380ab7d6ae52ff66aae9a19dbb3036b1b13c94 + --sha256: sha256-N4XRVqC+UgWej+J16RPh3EO6MSIE3wmJvmP5/nRgIuw= subdir: eras/allegra/impl eras/alonzo/impl diff --git a/ouroboros-consensus-cardano/ouroboros-consensus-cardano.cabal b/ouroboros-consensus-cardano/ouroboros-consensus-cardano.cabal index 4bcdff0c4e..95cf3dd9b0 100644 --- a/ouroboros-consensus-cardano/ouroboros-consensus-cardano.cabal +++ b/ouroboros-consensus-cardano/ouroboros-consensus-cardano.cabal @@ -135,10 +135,10 @@ library cardano-ledger-alonzo ^>=1.12, cardano-ledger-api ^>=1.10, cardano-ledger-babbage ^>=1.10, - cardano-ledger-binary ^>=1.5, + cardano-ledger-binary ^>=1.6, cardano-ledger-byron ^>=1.0.1, cardano-ledger-conway ^>=1.18, - cardano-ledger-core ^>=1.16, + cardano-ledger-core ^>=1.17, cardano-ledger-mary ^>=1.7, cardano-ledger-shelley ^>=1.15, cardano-prelude, @@ -147,7 +147,7 @@ library cardano-strict-containers, cborg ^>=0.2.2, containers >=0.5 && <0.8, - cryptonite >=0.25 && <0.31, + crypton, deepseq, formatting >=6.3 && <7.3, measures, diff --git a/ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Crypto/DSIGN.hs b/ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Crypto/DSIGN.hs index 5fe214077a..bb8fe671dd 100644 --- a/ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Crypto/DSIGN.hs +++ b/ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Crypto/DSIGN.hs @@ -92,7 +92,7 @@ instance DSIGNAlgorithm ByronDSIGN where where seedBytes = case getBytesFromSeed 32 seed of Just (x,_) -> x - Nothing -> throw $ SeedBytesExhausted (-1) -- TODO We can't get the seed size! + Nothing -> throw $ SeedBytesExhausted (-1) (-1) -- TODO We can't get the seed size! deriveVerKeyDSIGN (SignKeyByronDSIGN sk) = VerKeyByronDSIGN $ toVerification sk diff --git a/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Node/Common.hs b/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Node/Common.hs index 572ed23a4e..daba0e4192 100644 --- a/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Node/Common.hs +++ b/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Node/Common.hs @@ -20,6 +20,8 @@ module Ouroboros.Consensus.Shelley.Node.Common ( , shelleyBlockIssuerVKey ) where +import Cardano.Crypto.KES (UnsoundPureSignKeyKES) +import Cardano.Ledger.Crypto import qualified Cardano.Ledger.Keys as SL import qualified Cardano.Ledger.Shelley.API as SL import Cardano.Ledger.Slot @@ -52,7 +54,7 @@ data ShelleyLeaderCredentials c = ShelleyLeaderCredentials -- -- Note that this is not inside 'ShelleyCanBeLeader' since it gets evolved -- automatically, whereas 'ShelleyCanBeLeader' does not change. - shelleyLeaderCredentialsInitSignKey :: SL.SignKeyKES c, + shelleyLeaderCredentialsInitSignKey :: UnsoundPureSignKeyKES (KES c), shelleyLeaderCredentialsCanBeLeader :: PraosCanBeLeader c, -- | Identifier for this set of credentials. -- diff --git a/ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Api/KeysPraos.hs b/ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Api/KeysPraos.hs index 153f0a3cd5..1b2909216f 100644 --- a/ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Api/KeysPraos.hs +++ b/ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Api/KeysPraos.hs @@ -31,7 +31,7 @@ import qualified Cardano.Crypto.DSIGN.Class as Crypto import qualified Cardano.Crypto.Hash.Class as Crypto import qualified Cardano.Crypto.KES.Class as Crypto import qualified Cardano.Crypto.VRF.Class as Crypto -import Cardano.Ledger.Crypto (StandardCrypto) +import Cardano.Ledger.Crypto (Crypto(..), StandardCrypto) import qualified Cardano.Ledger.Crypto as Shelley (KES, VRF) import qualified Cardano.Ledger.Keys as Shelley import Data.String (IsString (..)) @@ -56,15 +56,15 @@ instance Key KesKey where deriving anyclass SerialiseAsCBOR newtype SigningKey KesKey = - KesSigningKey (Shelley.SignKeyKES StandardCrypto) + KesSigningKey (Crypto.UnsoundPureSignKeyKES (KES StandardCrypto)) deriving (Show, IsString) via UsingRawBytesHex (SigningKey KesKey) - deriving newtype (EncCBOR, DecCBOR, ToCBOR, FromCBOR) - deriving anyclass SerialiseAsCBOR + deriving newtype (ToCBOR, FromCBOR) + deriving anyclass (EncCBOR, DecCBOR, SerialiseAsCBOR) --This loses the mlock safety of the seed, since it starts from a normal in-memory seed. deterministicSigningKey :: AsType KesKey -> Crypto.Seed -> SigningKey KesKey deterministicSigningKey AsKesKey = - KesSigningKey . Crypto.genKeyKES + KesSigningKey . Crypto.unsoundPureGenKeyKES deterministicSigningKeySeedSize :: AsType KesKey -> Word deterministicSigningKeySeedSize AsKesKey = @@ -75,7 +75,7 @@ instance Key KesKey where getVerificationKey :: SigningKey KesKey -> VerificationKey KesKey getVerificationKey (KesSigningKey sk) = - KesVerificationKey (Crypto.deriveVerKeyKES sk) + KesVerificationKey (Crypto.unsoundPureDeriveVerKeyKES sk) verificationKeyHash :: VerificationKey KesKey -> Hash KesKey verificationKeyHash (KesVerificationKey vkey) = @@ -92,10 +92,10 @@ instance SerialiseAsRawBytes (VerificationKey KesKey) where instance SerialiseAsRawBytes (SigningKey KesKey) where serialiseToRawBytes (KesSigningKey sk) = - Crypto.rawSerialiseSignKeyKES sk + Crypto.rawSerialiseUnsoundPureSignKeyKES sk deserialiseFromRawBytes (AsSigningKey AsKesKey) bs = - KesSigningKey <$> Crypto.rawDeserialiseSignKeyKES bs + KesSigningKey <$> Crypto.rawDeserialiseUnsoundPureSignKeyKES bs instance SerialiseAsBech32 (VerificationKey KesKey) where bech32PrefixFor _ = "kes_vk" diff --git a/ouroboros-consensus-cardano/src/unstable-shelley-testlib/Test/ThreadNet/Infra/Shelley.hs b/ouroboros-consensus-cardano/src/unstable-shelley-testlib/Test/ThreadNet/Infra/Shelley.hs index b67594b964..407e6b91dc 100644 --- a/ouroboros-consensus-cardano/src/unstable-shelley-testlib/Test/ThreadNet/Infra/Shelley.hs +++ b/ouroboros-consensus-cardano/src/unstable-shelley-testlib/Test/ThreadNet/Infra/Shelley.hs @@ -37,7 +37,8 @@ module Test.ThreadNet.Infra.Shelley ( import Cardano.Crypto.DSIGN (DSIGNAlgorithm (..), seedSizeDSIGN) import Cardano.Crypto.Hash (HashAlgorithm) -import Cardano.Crypto.KES (KESAlgorithm (..)) +import Cardano.Crypto.KES (UnsoundPureSignKeyKES, KESAlgorithm (..), + seedSizeKES, unsoundPureGenKeyKES, unsoundPureDeriveVerKeyKES) import Cardano.Crypto.Seed (mkSeedFromBytes) import qualified Cardano.Crypto.Seed as Cardano.Crypto import Cardano.Crypto.VRF (SignKeyVRF, deriveVerKeyVRF, genKeyVRF, @@ -138,7 +139,7 @@ data CoreNode c = CoreNode { -- ^ The hash of the corresponding verification (public) key will be -- used as the staking credential. , cnVRF :: !(SL.SignKeyVRF c) - , cnKES :: !(SL.SignKeyKES c) + , cnKES :: !(UnsoundPureSignKeyKES (KES c)) , cnOCert :: !(SL.OCert c) } @@ -180,8 +181,8 @@ genCoreNode startKESPeriod = do delKey <- genKeyDSIGN <$> genSeed (seedSizeDSIGN (Proxy @(DSIGN c))) stkKey <- genKeyDSIGN <$> genSeed (seedSizeDSIGN (Proxy @(DSIGN c))) vrfKey <- genKeyVRF <$> genSeed (seedSizeVRF (Proxy @(VRF c))) - kesKey <- genKeyKES <$> genSeed (seedSizeKES (Proxy @(KES c))) - let kesPub = deriveVerKeyKES kesKey + kesKey <- unsoundPureGenKeyKES <$> genSeed (seedSizeKES (Proxy @(KES c))) + let kesPub = unsoundPureDeriveVerKeyKES kesKey sigma = LK.signedDSIGN @c delKey diff --git a/ouroboros-consensus-protocol/ouroboros-consensus-protocol.cabal b/ouroboros-consensus-protocol/ouroboros-consensus-protocol.cabal index 9725c76e0b..1430e9164c 100644 --- a/ouroboros-consensus-protocol/ouroboros-consensus-protocol.cabal +++ b/ouroboros-consensus-protocol/ouroboros-consensus-protocol.cabal @@ -119,7 +119,7 @@ test-suite protocol-test base, cardano-crypto-class, cardano-ledger-binary:testlib, - cardano-ledger-core ^>=1.16, + cardano-ledger-core ^>=1.17, containers, ouroboros-consensus:{ouroboros-consensus, unstable-consensus-testlib}, ouroboros-consensus-protocol, diff --git a/ouroboros-consensus-protocol/src/ouroboros-consensus-protocol/Ouroboros/Consensus/Protocol/Ledger/HotKey.hs b/ouroboros-consensus-protocol/src/ouroboros-consensus-protocol/Ouroboros/Consensus/Protocol/Ledger/HotKey.hs index 8a46088450..b4cea24444 100644 --- a/ouroboros-consensus-protocol/src/ouroboros-consensus-protocol/Ouroboros/Consensus/Protocol/Ledger/HotKey.hs +++ b/ouroboros-consensus-protocol/src/ouroboros-consensus-protocol/Ouroboros/Consensus/Protocol/Ledger/HotKey.hs @@ -25,7 +25,8 @@ module Ouroboros.Consensus.Protocol.Ledger.HotKey ( ) where import qualified Cardano.Crypto.KES as Relative (Period) -import Cardano.Ledger.Crypto (Crypto) +import Cardano.Crypto.KES +import Cardano.Ledger.Crypto (Crypto (..)) import qualified Cardano.Ledger.Keys as SL import qualified Cardano.Protocol.TPraos.OCert as Absolute (KESPeriod (..)) import Data.Word (Word64) @@ -148,7 +149,7 @@ sign = sign_ -- | The actual KES key, unless it expired, in which case it is replaced by -- \"poison\". data KESKey c = - KESKey !(SL.SignKeyKES c) + KESKey !(UnsoundPureSignKeyKES (KES c)) | KESKeyPoisoned deriving (Generic) @@ -168,7 +169,7 @@ instance Crypto c => NoThunks (KESState c) mkHotKey :: forall m c. (Crypto c, IOLike m) - => SL.SignKeyKES c + => UnsoundPureSignKeyKES (KES c) -> Absolute.KESPeriod -- ^ Start period -> Word64 -- ^ Max KES evolutions -> m (HotKey c m) @@ -184,7 +185,7 @@ mkHotKey initKey startPeriod@(Absolute.KESPeriod start) maxKESEvolutions = do KESKeyPoisoned -> error "trying to sign with a poisoned key" KESKey key -> do let evolution = kesEvolution kesStateInfo - signed = SL.signedKES () evolution toSign key + signed = unsoundPureSignedKES () evolution toSign key -- Force the signature to WHNF (for 'SignedKES', WHNF implies -- NF) so that we don't have any thunks holding on to a key that -- might be destructively updated when evolved. @@ -260,17 +261,18 @@ evolveKey varKESState targetPeriod = modifyMVar varKESState $ \kesState -> do -- | PRECONDITION: -- -- > targetEvolution >= curEvolution - go :: KESEvolution -> KESInfo -> SL.SignKeyKES c -> m (KESState c) + go :: KESEvolution -> KESInfo -> UnsoundPureSignKeyKES (KES c) -> m (KESState c) go targetEvolution info key | targetEvolution <= curEvolution = return $ KESState { kesStateInfo = info, kesStateKey = KESKey key } | otherwise - = case SL.updateKES () key curEvolution of + = case unsoundPureUpdateKES () key curEvolution of -- This cannot happen Nothing -> error "Could not update KES key" Just !key' -> do -- Clear the memory associated with the old key - forgetSignKeyKES key + -- FIXME: Here we want to forget, but it was never implemented + -- forgetSignKeyKES key let info' = info { kesEvolution = curEvolution + 1 } go targetEvolution info' key' where diff --git a/ouroboros-consensus-protocol/src/unstable-protocol-testlib/Test/Consensus/Protocol/Serialisation/Generators.hs b/ouroboros-consensus-protocol/src/unstable-protocol-testlib/Test/Consensus/Protocol/Serialisation/Generators.hs index 7686d00a04..02f5a93b8a 100644 --- a/ouroboros-consensus-protocol/src/unstable-protocol-testlib/Test/Consensus/Protocol/Serialisation/Generators.hs +++ b/ouroboros-consensus-protocol/src/unstable-protocol-testlib/Test/Consensus/Protocol/Serialisation/Generators.hs @@ -6,7 +6,7 @@ -- to be semantically correct at all, only structurally correct. module Test.Consensus.Protocol.Serialisation.Generators () where -import Cardano.Crypto.KES (signedKES) +import Cardano.Crypto.KES (unsoundPureSignedKES) import Cardano.Crypto.VRF (evalCertified) import Cardano.Protocol.TPraos.BHeader (HashHeader, PrevHash (..)) import Cardano.Protocol.TPraos.OCert (KESPeriod (KESPeriod), @@ -60,7 +60,7 @@ instance Praos.PraosCrypto c => Arbitrary (Header c) where hBody <- arbitrary period <- arbitrary sKey <- arbitrary - let hSig = signedKES () period hBody sKey + let hSig = unsoundPureSignedKES () period hBody sKey pure $ Header hBody hSig instance Praos.PraosCrypto c => Arbitrary (PraosState c) where diff --git a/ouroboros-consensus-protocol/src/unstable-protocol-testlib/Test/Ouroboros/Consensus/Protocol/Praos/Header.hs b/ouroboros-consensus-protocol/src/unstable-protocol-testlib/Test/Ouroboros/Consensus/Protocol/Praos/Header.hs index 09c9f65c6e..05ff3d27a6 100644 --- a/ouroboros-consensus-protocol/src/unstable-protocol-testlib/Test/Ouroboros/Consensus/Protocol/Praos/Header.hs +++ b/ouroboros-consensus-protocol/src/unstable-protocol-testlib/Test/Ouroboros/Consensus/Protocol/Praos/Header.hs @@ -27,8 +27,6 @@ import Cardano.Crypto.DSIGN import Cardano.Crypto.Hash (Blake2b_256, Hash, hashFromBytes, hashToBytes, hashWith) import qualified Cardano.Crypto.KES as KES -import Cardano.Crypto.KES.Class (genKeyKES, rawDeserialiseSignKeyKES, - rawSerialiseSignKeyKES) import Cardano.Crypto.Seed (mkSeedFromBytes) import Cardano.Crypto.VRF (deriveVerKeyVRF, hashVerKeyVRF, rawDeserialiseSignKeyVRF, rawSerialiseSignKeyVRF) @@ -115,14 +113,14 @@ mutate context header mutation = let Header body _ = header newKESSignKey <- newKESSigningKey <$> gen32Bytes KESPeriod kesPeriod <- genValidKESPeriod (hbSlotNo body) praosSlotsPerKESPeriod - let sig' = KES.signKES () kesPeriod body newKESSignKey + let sig' = KES.unsoundPureSignKES () kesPeriod body newKESSignKey pure (context, Header body (KES.SignedKES sig')) MutateColdKey -> do let Header body _ = header newColdSignKey <- genKeyDSIGN . mkSeedFromBytes <$> gen32Bytes (hbOCert, KESPeriod kesPeriod) <- genCert (hbSlotNo body) context{coldSignKey = newColdSignKey} let newBody = body{hbOCert} - let sig' = KES.signKES () kesPeriod newBody kesSignKey + let sig' = KES.unsoundPureSignKES () kesPeriod newBody kesSignKey pure (context, Header newBody (KES.SignedKES sig')) MutateKESPeriod -> do let Header body _ = header @@ -137,7 +135,7 @@ mutate context header mutation = , ocertSigma = signedDSIGN @StandardCrypto coldSignKey (OCertSignable ocertVkHot ocertN newKESPeriod) } } - let sig' = KES.signKES () kesPeriod' newBody kesSignKey + let sig' = KES.unsoundPureSignKES () kesPeriod' newBody kesSignKey pure (context, Header newBody (KES.SignedKES sig')) MutateKESPeriodBefore -> do let Header body _ = header @@ -147,7 +145,7 @@ mutate context header mutation = period' = unSlotNo newSlotNo `div` praosSlotsPerKESPeriod hbVrfRes = VRF.evalCertified () rho' vrfSignKey newBody = body{hbSlotNo = newSlotNo, hbVrfRes} - sig' = KES.signKES () (fromIntegral period' - kesPeriod) newBody kesSignKey + sig' = KES.unsoundPureSignKES () (fromIntegral period' - kesPeriod) newBody kesSignKey pure (context, Header newBody (KES.SignedKES sig')) MutateCounterOver1 -> do let poolId = coerce $ hashKey $ VKey $ deriveVerKeyDSIGN coldSignKey @@ -255,13 +253,13 @@ instance Json.FromJSON MutatedHeader where either (fail . show) pure $ decodeFullAnnotator @(Header StandardCrypto) testVersion "Header" decCBOR $ LBS.fromStrict headerBytes -- * Generators -type KESKey = KES.SignKeyKES (KES.Sum6KES Ed25519DSIGN Blake2b_256) +type KESKey = KES.UnsoundPureSignKeyKES (KES.Sum6KES Ed25519DSIGN Blake2b_256) newVRFSigningKey :: ByteString -> (VRF.SignKeyVRF VRF.PraosVRF, VRF.VerKeyVRF VRF.PraosVRF) newVRFSigningKey = VRF.genKeyPairVRF . mkSeedFromBytes newKESSigningKey :: ByteString -> KESKey -newKESSigningKey = genKeyKES . mkSeedFromBytes +newKESSigningKey = KES.unsoundPureGenKeyKES . mkSeedFromBytes data GeneratorContext = GeneratorContext { praosSlotsPerKESPeriod :: !Word64 @@ -279,7 +277,8 @@ instance Eq GeneratorContext where a == b = praosSlotsPerKESPeriod a == praosSlotsPerKESPeriod b && praosMaxKESEvo a == praosMaxKESEvo b - && serialize' testVersion (kesSignKey a) == serialize' testVersion (kesSignKey b) + && serialize' testVersion (KES.encodeUnsoundPureSignKeyKES (kesSignKey a)) == + serialize' testVersion (KES.encodeUnsoundPureSignKeyKES (kesSignKey b)) && coldSignKey a == coldSignKey b && vrfSignKey a == vrfSignKey b && nonce a == nonce b @@ -298,7 +297,7 @@ instance Json.ToJSON GeneratorContext where , "activeSlotCoeff" .= activeSlotVal activeSlotCoeff ] where - rawKesSignKey = decodeUtf8 . Base16.encode $ rawSerialiseSignKeyKES kesSignKey + rawKesSignKey = decodeUtf8 . Base16.encode $ KES.rawSerialiseUnsoundPureSignKeyKES kesSignKey rawColdSignKey = decodeUtf8 . Base16.encode $ rawSerialiseSignKeyDSIGN coldSignKey rawVrfSignKey = decodeUtf8 . Base16.encode $ rawSerialiseSignKeyVRF $ skToBatchCompat vrfSignKey rawVrVKeyHash = decodeUtf8 . Base16.encode $ hashToBytes $ hashVerKeyVRF @_ @Blake2b_256 $ deriveVerKeyVRF vrfSignKey @@ -337,7 +336,7 @@ instance Json.FromJSON GeneratorContext where case Base16.decode (encodeUtf8 rawKey) of Left err -> fail err Right keyBytes -> - case rawDeserialiseSignKeyKES keyBytes of + case KES.rawDeserialiseUnsoundPureSignKeyKES keyBytes of Nothing -> fail $ "Invalid KES key bytes: " <> show rawKey Just key -> pure key parseVrfSignKey rawKey = do @@ -376,7 +375,7 @@ generated for the purpose of producing the header are returned. genHeader :: GeneratorContext -> Gen (Header StandardCrypto) genHeader context = do (body, KESPeriod kesPeriod) <- genHeaderBody context - let sign = KES.SignedKES $ KES.signKES () kesPeriod body kesSignKey + let sign = KES.SignedKES $ KES.unsoundPureSignKES () kesPeriod body kesSignKey pure $ (Header body sign) where GeneratorContext{kesSignKey} = context @@ -420,7 +419,7 @@ protocolVersionZero = ProtVer versionZero 0 genCert :: SlotNo -> GeneratorContext -> Gen (OCert StandardCrypto, KESPeriod) genCert slotNo context = do - let ocertVkHot = KES.deriveVerKeyKES kesSignKey + let ocertVkHot = KES.unsoundPureDeriveVerKeyKES kesSignKey poolId = coerce $ hashKey $ VKey $ deriveVerKeyDSIGN coldSignKey ocertN = fromMaybe 0 $ Map.lookup poolId ocertCounters ocertKESPeriod <- genValidKESPeriod slotNo praosSlotsPerKESPeriod diff --git a/ouroboros-consensus/ouroboros-consensus.cabal b/ouroboros-consensus/ouroboros-consensus.cabal index 27552f9e20..e40c67e707 100644 --- a/ouroboros-consensus/ouroboros-consensus.cabal +++ b/ouroboros-consensus/ouroboros-consensus.cabal @@ -278,7 +278,7 @@ library bytestring >=0.10 && <0.13, cardano-binary, cardano-crypto-class, - cardano-ledger-core ^>=1.16, + cardano-ledger-core ^>=1.17, cardano-prelude, cardano-slotting, cardano-strict-containers, diff --git a/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Node/Praos.hs b/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Node/Praos.hs index 00d678da6d..0b3ce9a5d9 100644 --- a/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Node/Praos.hs +++ b/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Node/Praos.hs @@ -104,7 +104,7 @@ blockForgingPraos numCoreNodes nid = sequence [praosBlockForging nid initHotKey] initHotKey = HotKey 0 - (SignKeyMockKES + (UnsoundPureSignKeyMockKES -- key ID (fst $ verKeys Map.! nid) -- KES initial slot diff --git a/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Protocol/Praos.hs b/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Protocol/Praos.hs index 73407f75af..2524bd86d6 100644 --- a/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Protocol/Praos.hs +++ b/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Protocol/Praos.hs @@ -43,7 +43,7 @@ module Ouroboros.Consensus.Mock.Protocol.Praos ( ) where import Cardano.Binary (FromCBOR (..), ToCBOR (..), serialize') -import Cardano.Crypto.DSIGN.Ed448 (Ed448DSIGN) +import Cardano.Crypto.DSIGN.Ed25519 (Ed25519DSIGN) import Cardano.Crypto.Hash.Class (HashAlgorithm (..), hashToBytes, hashWithSerialiser, sizeHash) import Cardano.Crypto.Hash.SHA256 (SHA256) @@ -203,12 +203,14 @@ praosValidateView getFields hdr = data HotKey c = HotKey !Period -- ^ Absolute period of the KES key - !(SignKeyKES (PraosKES c)) + !(UnsoundPureSignKeyKES (PraosKES c)) | HotKeyPoisoned deriving (Generic) instance PraosCrypto c => NoThunks (HotKey c) -deriving instance PraosCrypto c => Show (HotKey c) +instance PraosCrypto c => Show (HotKey c) where + show (HotKey p _) = "HotKey " ++ show p ++ "