Skip to content

Commit

Permalink
Crypto deprecation to promote migration using deprecation.
Browse files Browse the repository at this point in the history
Move `Crypto` and `StandardCrypto` back into `cardano-ledger-core` and
use deprecation on its imports in order to make this migration easier
for the downstream users.

Also deprecate all the VRF and KES related type synonyms, just liek it
was done with Hash and DSIGN in the previous commit. Crypto related
type synonyms have been a point of confusion on many occasions
for many people. In other words, practice showed once again how type
synonyms can be harmful.
  • Loading branch information
lehins committed Dec 20, 2024
1 parent 753752f commit 33f7c01
Show file tree
Hide file tree
Showing 20 changed files with 199 additions and 152 deletions.
8 changes: 8 additions & 0 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/API/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,25 @@ import Cardano.Ledger.Hashes as X (
ScriptHash (..),
)
import Cardano.Ledger.Keys as X (
CertifiedVRF,
GenDelegPair (..),
GenDelegs (..),
Hash,
KESignable,
KeyHash (..),
KeyRole (..),
SignKeyDSIGN,
SignKeyKES,
SignKeyVRF,
SignedDSIGN,
SignedKES,
VKey (..),
VerKeyKES,
VerKeyVRF,
WitVKey (..),
coerceKeyRole,
hashKey,
hashVerKeyVRF,
)
import Cardano.Ledger.Keys.Bootstrap as X (
BootstrapWitness (..),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import Cardano.Ledger.AuxiliaryData
import Cardano.Ledger.BaseTypes
import Cardano.Ledger.Coin
import Cardano.Ledger.EpochBoundary
import Cardano.Ledger.Keys
import Cardano.Ledger.Keys hiding (hashVerKeyVRF)
import Cardano.Ledger.PoolDistr
import Cardano.Ledger.Shelley (ShelleyEra)
import Cardano.Ledger.Shelley.API
import Cardano.Ledger.Shelley.API hiding (hashVerKeyVRF)
import Cardano.Ledger.Shelley.Core
import Cardano.Ledger.Shelley.LedgerState
import Cardano.Ledger.Shelley.Rules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ import Cardano.Ledger.Shelley.TxOut (ShelleyTxOut (..))
import Cardano.Ledger.Shelley.TxWits (ShelleyTxWits, addrWits)
import Cardano.Ledger.Slot (BlockNo (..), EpochNo (..), SlotNo (..))
import Cardano.Ledger.TxIn (TxId, TxIn (..))
import Cardano.Protocol.Crypto hiding (CertifiedVRF, SignedKES)
import Cardano.Protocol.Crypto
import Cardano.Protocol.TPraos.BHeader (
BHBody (..),
BHeader (..),
Expand Down
1 change: 1 addition & 0 deletions libs/cardano-ledger-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.11.0.0

* Stop re-exporting `Crypto` and `StandardCrypto`, since they have been moved to `cardano-protocol-tpraos`
* Removal of crypto parametrization:
* `LatestKnownEra`
* `HotCredAuthStatus` and `CommitteeMemberState`
Expand Down
3 changes: 3 additions & 0 deletions libs/cardano-ledger-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.17.0.0

* Deprecate `Cardano.Ledger.Crypto` module in favor of `cardano-protocol-tpraos:Cardano.Protocol.Crypto`
* Deprecate `KESignable`, `SignedKES`, `SignKeyKES`, `VerKeyKES`, `VRFSignable`,
`CertifiedVRF`, `SignKeyVRF` and `VerKeyVRF` type synonyms.
* Deprecate `Hash`, `SignedDSIGN` and `SignKeyDSIGN` type synonyms.
* Deprecate `hashSignature` in favor of new `hashTxBodySignature`
* Move into `Cardano.Ledger.Hashes`:
Expand Down
2 changes: 2 additions & 0 deletions libs/cardano-ledger-core/cardano-ledger-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ library
Cardano.Ledger.Core
Cardano.Ledger.Credential
Cardano.Ledger.Crypto
Cardano.Ledger.Crypto.Internal
Cardano.Ledger.DRep
Cardano.Ledger.EpochBoundary
Cardano.Ledger.Genesis
Expand Down Expand Up @@ -98,6 +99,7 @@ library
bytestring,
cardano-crypto,
cardano-crypto-class ^>=2.2,
cardano-crypto-praos ^>=2.2,
cardano-crypto-wrapper,
cardano-data ^>=1.2,
cardano-ledger-binary ^>=1.6,
Expand Down
5 changes: 3 additions & 2 deletions libs/cardano-ledger-core/src/Cardano/Ledger/Crypto.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ module Cardano.Ledger.Crypto
\ this type class as well as `StandardCrypto` definition will be migrated to `Cardano.Protocol.Crypto` module\
\ and from now on should be imported from 'cardano-protocol-tpraos' package instead."
#-} (
Crypto (..),
StandardCrypto,
HASH,
ADDRHASH,
DSIGN,
) where

import Cardano.Ledger.Hashes (ADDRHASH, HASH)
import Cardano.Ledger.Keys.Internal (DSIGN)
import Cardano.Ledger.Crypto.Internal
45 changes: 45 additions & 0 deletions libs/cardano-ledger-core/src/Cardano/Ledger/Crypto/Internal.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}

-- Contents of this module will be moved to `Cardano.Protocol.Crypto` at some point later
-- whenever we are ready to remove derpecations
module Cardano.Ledger.Crypto.Internal (
Crypto (..),
StandardCrypto,
HASH,
ADDRHASH,
DSIGN,
) where

import Cardano.Crypto.DSIGN
import Cardano.Crypto.Hash as Hash
import qualified Cardano.Crypto.KES as KES
import qualified Cardano.Crypto.VRF as VRF
import qualified Cardano.Crypto.VRF.Praos as VRF
import Cardano.Ledger.Hashes (ADDRHASH, HASH)
import Cardano.Ledger.Keys.Internal (DSIGN)
import Data.Kind (Type)
import Data.Typeable (Typeable)

class
( KES.UnsoundPureKESAlgorithm (KES c)
, VRF.VRFAlgorithm (VRF c)
, KES.ContextKES (KES c) ~ ()
, VRF.ContextVRF (VRF c) ~ ()
, Typeable c
) =>
Crypto c
where
type KES c :: Type
type VRF c :: Type

-- ================================

-- | The same crypto used on the net
data StandardCrypto

instance Crypto StandardCrypto where
type KES StandardCrypto = KES.Sum6KES Ed25519DSIGN Blake2b_256
type VRF StandardCrypto = VRF.PraosVRF
84 changes: 82 additions & 2 deletions libs/cardano-ledger-core/src/Cardano/Ledger/Keys.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# OPTIONS_GHC -Wno-deprecations #-}

module Cardano.Ledger.Keys (
-- * VKey
VKey (..),
Expand Down Expand Up @@ -29,7 +35,6 @@ module Cardano.Ledger.Keys (
GenDelegs (..),
module Cardano.Ledger.Keys.WitVKey,
module Cardano.Ledger.Keys.Bootstrap,
-- TODO: Deprecate and remove:

-- * To be removed

Expand All @@ -38,14 +43,33 @@ module Cardano.Ledger.Keys (
encodeSignedDSIGN,
Hash.hashWithSerialiser,

-- ** Concrete crypto algorithms
-- * Deprecated
Hash,

-- ** DSIGN
SignedDSIGN,
SignKeyDSIGN,

-- ** KES
KESignable,
SignedKES,
SignKeyKES,
VerKeyKES,

-- ** VRF
VRFSignable,
CertifiedVRF,
SignKeyVRF,
VerKeyVRF,
hashVerKeyVRF,
)
where

import qualified Cardano.Crypto.DSIGN as DSIGN
import qualified Cardano.Crypto.Hash as Hash
import qualified Cardano.Crypto.KES as KES
import qualified Cardano.Crypto.VRF as VRF
import Cardano.Ledger.Crypto
import Cardano.Ledger.Hashes (
GenDelegPair (..),
GenDelegs (..),
Expand All @@ -59,3 +83,59 @@ import Cardano.Ledger.Hashes (
import Cardano.Ledger.Keys.Bootstrap
import Cardano.Ledger.Keys.Internal
import Cardano.Ledger.Keys.WitVKey

hashVerKeyVRF :: Crypto c => VRF.VerKeyVRF (VRF c) -> VRFVerKeyHash (r :: KeyRoleVRF)
hashVerKeyVRF = VRFVerKeyHash . Hash.castHash . VRF.hashVerKeyVRF
{-# DEPRECATED hashVerKeyVRF "Use `Cardano.Protocol.Crypto.hashVerKeyVRF` instead" #-}

type Hash = Hash.Hash HASH
{-# DEPRECATED Hash "In favor of `Hash.Hash` `HASH`" #-}

type SignedDSIGN = DSIGN.SignedDSIGN DSIGN
{-# DEPRECATED SignedDSIGN "In favor of @`DSIGN.SignedDSIGN` `DSIGN`@" #-}

type SignKeyDSIGN = DSIGN.SignKeyDSIGN DSIGN
{-# DEPRECATED SignKeyDSIGN "In favor of @`DSIGN.SignKeyDSIGN` `DSIGN`@" #-}

-- | Hash a given signature
hashSignature ::
SignedDSIGN (Hash h) ->
Hash (SignedDSIGN (Hash h))
hashSignature (DSIGN.SignedDSIGN sigDSIGN) = Hash.castHash $ Hash.hashWith DSIGN.rawSerialiseSigDSIGN sigDSIGN
{-# DEPRECATED
hashSignature
"In favor of `Cardano.Ledger.Hashes.hashTxBodySignature`. \
\Fallback on `Hash.hashWith` if you need more general hashing functionality."
#-}

--------------------------------------------------------------------------------
-- KES
--------------------------------------------------------------------------------

type KESignable c = KES.Signable (KES c)
{-# DEPRECATED KESignable "In favor of @`KES.Signable` (`KES` c)@" #-}

type SignedKES c = KES.SignedKES (KES c)
{-# DEPRECATED SignedKES "In favor of @`KES.SignedKES` (`KES` c)@`" #-}

type SignKeyKES c = KES.SignKeyKES (KES c)
{-# DEPRECATED SignKeyKES "In favor of @`KES.SignKeyKES` (`KES` c)@`" #-}

type VerKeyKES c = KES.VerKeyKES (KES c)
{-# DEPRECATED VerKeyKES "In favor of @`KES.VerKeyKES` (`KES` c)@`" #-}

--------------------------------------------------------------------------------
-- VRF
--------------------------------------------------------------------------------

type VRFSignable c = VRF.Signable (VRF c)
{-# DEPRECATED VRFSignable "In favor of @`VRF.Signable` (`VRF` c)@`" #-}

type CertifiedVRF c = VRF.CertifiedVRF (VRF c)
{-# DEPRECATED CertifiedVRF "In favor of @`VRF.CertifiedVRF` (`VRF` c)@`" #-}

type SignKeyVRF c = VRF.SignKeyVRF (VRF c)
{-# DEPRECATED SignKeyVRF "In favor of @`VRF.SignKeyVRF` (`VRF` c)@`" #-}

type VerKeyVRF c = VRF.VerKeyVRF (VRF c)
{-# DEPRECATED VerKeyVRF "In favor of @`VRF.VerKeyVRF` (`VRF` c)@`" #-}
52 changes: 11 additions & 41 deletions libs/cardano-ledger-core/src/Cardano/Ledger/Keys/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module Cardano.Ledger.Keys.Internal (
VKey (..),
signedDSIGN,
verifySignedDSIGN,
hashSignature,

-- * Key roles
KeyRole (..),
Expand All @@ -26,16 +25,16 @@ module Cardano.Ledger.Keys.Internal (
-- * Re-exports from cardano-crypto-class
decodeSignedDSIGN,
encodeSignedDSIGN,

-- * Concrete crypto algorithms
Hash,
SignedDSIGN,
SignKeyDSIGN,
)
where

import Cardano.Crypto.DSIGN hiding (
decodeSignedDSIGN,
encodeSignedDSIGN,
signedDSIGN,
verifySignedDSIGN,
)
import qualified Cardano.Crypto.DSIGN as DSIGN
import qualified Cardano.Crypto.Hash as Hash
import Cardano.Ledger.Binary (
DecCBOR (..),
EncCBOR (..),
Expand Down Expand Up @@ -132,48 +131,19 @@ instance Typeable kd => ToCBOR (VKey kd) where

-- | Produce a digital signature
signedDSIGN ::
DSIGN.Signable DSIGN a =>
DSIGN.SignKeyDSIGN DSIGN ->
Signable DSIGN a =>
SignKeyDSIGN DSIGN ->
a ->
SignedDSIGN a
SignedDSIGN DSIGN a
signedDSIGN key a = DSIGN.signedDSIGN () a key

-- | Verify a digital signature
verifySignedDSIGN ::
DSIGN.Signable DSIGN a =>
Signable DSIGN a =>
VKey kd ->
a ->
SignedDSIGN a ->
SignedDSIGN DSIGN a ->
Bool
verifySignedDSIGN (VKey vk) vd sigDSIGN =
either (const False) (const True) $ DSIGN.verifySignedDSIGN () vk vd sigDSIGN
{-# INLINE verifySignedDSIGN #-}

-- | Hash a given signature
hashSignature ::
SignedDSIGN (Hash h) ->
Hash (SignedDSIGN (Hash h))
hashSignature (DSIGN.SignedDSIGN sigDSIGN) = Hash.castHash $ Hash.hashWith DSIGN.rawSerialiseSigDSIGN sigDSIGN
{-# DEPRECATED
hashSignature
"In favor of `Cardano.Ledger.Hashes.hasTxBodySignature`. \
\Fallback on `Hash.hashWith` if you need more general hashsing functionality"
#-}

--------------------------------------------------------------------------------
-- crypto-parametrised types
--
-- Within `cardano-ledger`, we parametrise everything on our `crypto` type
-- "package". However, in `cardano-crypto-class`, things are parametrised on the
-- original algorithm. In order to make using types from that module easier, we
-- provide some type aliases which unwrap the crypto parameters.
--------------------------------------------------------------------------------

type Hash = Hash.Hash Hash.Blake2b_256
{-# DEPRECATED Hash "In favor of `Hash.Hash` `Cardano.Ledger.Hashes.HASH`" #-}

type SignedDSIGN = DSIGN.SignedDSIGN DSIGN
{-# DEPRECATED SignedDSIGN "In favor of `DSIGN.SignedDSIGN` `DSIGN`" #-}

type SignKeyDSIGN = DSIGN.SignKeyDSIGN DSIGN
{-# DEPRECATED SignKeyDSIGN "In favor of `DSIGN.SignKeyDSIGN` `DSIGN`" #-}
1 change: 0 additions & 1 deletion libs/cardano-protocol-tpraos/cardano-protocol-tpraos.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ library
base >=4.14 && <5,
bytestring,
cardano-crypto-class ^>=2.2,
cardano-crypto-praos ^>=2.2,
cardano-ledger-allegra >=1.1,
cardano-ledger-alonzo >=1.2,
cardano-ledger-babbage >=1.1,
Expand Down
Loading

0 comments on commit 33f7c01

Please sign in to comment.