{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module Cardano.Ledger.SafeHash (
SafeHash,
SafeToHash (..),
HashAnnotated,
hashAnnotated,
HashWithCrypto (..),
unsafeMakeSafeHash,
castSafeHash,
extractHash,
indexProxy,
)
where
import qualified Cardano.Crypto.Hash as Hash
import Cardano.HeapWords (HeapWords (..))
import Cardano.Ledger.Binary (DecCBOR (..), EncCBOR (..))
import Cardano.Ledger.Binary.Plain (FromCBOR (..), ToCBOR (..))
import Cardano.Ledger.Crypto
import Cardano.Ledger.Orphans ()
import Control.DeepSeq (NFData)
import Data.Aeson (FromJSON, ToJSON)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS (length)
import Data.ByteString.Short (ShortByteString, fromShort)
import qualified Data.ByteString.Short as SBS (length)
import Data.Default (Default (..))
import Data.Typeable
import NoThunks.Class (NoThunks (..))
newtype SafeHash c index = SafeHash (Hash.Hash (HASH c) index)
deriving (Int -> SafeHash c index -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall c index. Int -> SafeHash c index -> ShowS
forall c index. [SafeHash c index] -> ShowS
forall c index. SafeHash c index -> String
showList :: [SafeHash c index] -> ShowS
$cshowList :: forall c index. [SafeHash c index] -> ShowS
show :: SafeHash c index -> String
$cshow :: forall c index. SafeHash c index -> String
showsPrec :: Int -> SafeHash c index -> ShowS
$cshowsPrec :: forall c index. Int -> SafeHash c index -> ShowS
Show, SafeHash c index -> SafeHash c index -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall c index. SafeHash c index -> SafeHash c index -> Bool
/= :: SafeHash c index -> SafeHash c index -> Bool
$c/= :: forall c index. SafeHash c index -> SafeHash c index -> Bool
== :: SafeHash c index -> SafeHash c index -> Bool
$c== :: forall c index. SafeHash c index -> SafeHash c index -> Bool
Eq, SafeHash c index -> SafeHash c index -> Bool
SafeHash c index -> SafeHash c index -> Ordering
SafeHash c index -> SafeHash c index -> SafeHash c index
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall c index. Eq (SafeHash c index)
forall c index. SafeHash c index -> SafeHash c index -> Bool
forall c index. SafeHash c index -> SafeHash c index -> Ordering
forall c index.
SafeHash c index -> SafeHash c index -> SafeHash c index
min :: SafeHash c index -> SafeHash c index -> SafeHash c index
$cmin :: forall c index.
SafeHash c index -> SafeHash c index -> SafeHash c index
max :: SafeHash c index -> SafeHash c index -> SafeHash c index
$cmax :: forall c index.
SafeHash c index -> SafeHash c index -> SafeHash c index
>= :: SafeHash c index -> SafeHash c index -> Bool
$c>= :: forall c index. SafeHash c index -> SafeHash c index -> Bool
> :: SafeHash c index -> SafeHash c index -> Bool
$c> :: forall c index. SafeHash c index -> SafeHash c index -> Bool
<= :: SafeHash c index -> SafeHash c index -> Bool
$c<= :: forall c index. SafeHash c index -> SafeHash c index -> Bool
< :: SafeHash c index -> SafeHash c index -> Bool
$c< :: forall c index. SafeHash c index -> SafeHash c index -> Bool
compare :: SafeHash c index -> SafeHash c index -> Ordering
$ccompare :: forall c index. SafeHash c index -> SafeHash c index -> Ordering
Ord, Context -> SafeHash c index -> IO (Maybe ThunkInfo)
Proxy (SafeHash c index) -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
forall c index. Context -> SafeHash c index -> IO (Maybe ThunkInfo)
forall c index. Proxy (SafeHash c index) -> String
showTypeOf :: Proxy (SafeHash c index) -> String
$cshowTypeOf :: forall c index. Proxy (SafeHash c index) -> String
wNoThunks :: Context -> SafeHash c index -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall c index. Context -> SafeHash c index -> IO (Maybe ThunkInfo)
noThunks :: Context -> SafeHash c index -> IO (Maybe ThunkInfo)
$cnoThunks :: forall c index. Context -> SafeHash c index -> IO (Maybe ThunkInfo)
NoThunks, SafeHash c index -> ()
forall a. (a -> ()) -> NFData a
forall c index. SafeHash c index -> ()
rnf :: SafeHash c index -> ()
$crnf :: forall c index. SafeHash c index -> ()
NFData)
deriving newtype instance
Hash.HashAlgorithm (HASH c) =>
SafeToHash (SafeHash c index)
deriving newtype instance HeapWords (SafeHash c i)
deriving instance (Typeable index, Crypto c) => ToCBOR (SafeHash c index)
deriving instance (Typeable index, Crypto c) => FromCBOR (SafeHash c index)
deriving instance (Typeable index, Crypto c) => EncCBOR (SafeHash c index)
deriving instance (Typeable index, Crypto c) => DecCBOR (SafeHash c index)
deriving instance Crypto c => ToJSON (SafeHash c index)
deriving instance Crypto c => FromJSON (SafeHash c index)
instance Crypto c => Default (SafeHash c i) where
def :: SafeHash c i
def = forall c index. Hash (HASH c) index -> SafeHash c index
unsafeMakeSafeHash forall a. Default a => a
def
extractHash :: SafeHash c i -> Hash.Hash (HASH c) i
(SafeHash Hash (HASH c) i
h) = Hash (HASH c) i
h
unsafeMakeSafeHash :: Hash.Hash (HASH c) index -> SafeHash c index
unsafeMakeSafeHash :: forall c index. Hash (HASH c) index -> SafeHash c index
unsafeMakeSafeHash = forall c index. Hash (HASH c) index -> SafeHash c index
SafeHash
class SafeToHash t where
originalBytes :: t -> ByteString
originalBytesSize :: t -> Int
originalBytesSize = ByteString -> Int
BS.length forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. SafeToHash t => t -> ByteString
originalBytes
makeHashWithExplicitProxys ::
Hash.HashAlgorithm (HASH c) =>
Proxy c ->
Proxy index ->
t ->
SafeHash c index
makeHashWithExplicitProxys Proxy c
_ Proxy index
_ t
x = forall c index. Hash (HASH c) index -> SafeHash c index
SafeHash forall a b. (a -> b) -> a -> b
$ forall h a b. Hash h a -> Hash h b
Hash.castHash (forall h a. HashAlgorithm h => (a -> ByteString) -> a -> Hash h a
Hash.hashWith forall t. SafeToHash t => t -> ByteString
originalBytes t
x)
instance SafeToHash ShortByteString where
originalBytes :: ShortByteString -> ByteString
originalBytes = ShortByteString -> ByteString
fromShort
originalBytesSize :: ShortByteString -> Int
originalBytesSize = ShortByteString -> Int
SBS.length
instance SafeToHash ByteString where
originalBytes :: ByteString -> ByteString
originalBytes ByteString
x = ByteString
x
instance Hash.HashAlgorithm c => SafeToHash (Hash.Hash c i) where
originalBytes :: Hash c i -> ByteString
originalBytes = forall h a. Hash h a -> ByteString
Hash.hashToBytes
class SafeToHash x => HashAnnotated x index c | x -> index c where
indexProxy :: x -> Proxy index
indexProxy x
_ = forall {k} (t :: k). Proxy t
Proxy @index
hashAnnotated :: Hash.HashAlgorithm (HASH c) => x -> SafeHash c index
hashAnnotated = forall t c index.
(SafeToHash t, HashAlgorithm (HASH c)) =>
Proxy c -> Proxy index -> t -> SafeHash c index
makeHashWithExplicitProxys (forall {k} (t :: k). Proxy t
Proxy @c) (forall {k} (t :: k). Proxy t
Proxy @index)
{-# INLINE hashAnnotated #-}
class SafeToHash x => HashWithCrypto x index | x -> index where
hashWithCrypto ::
forall c.
Hash.HashAlgorithm (HASH c) =>
Proxy c ->
x ->
SafeHash c index
hashWithCrypto Proxy c
proxy = forall t c index.
(SafeToHash t, HashAlgorithm (HASH c)) =>
Proxy c -> Proxy index -> t -> SafeHash c index
makeHashWithExplicitProxys Proxy c
proxy (forall {k} (t :: k). Proxy t
Proxy @index)
{-# INLINE hashWithCrypto #-}
castSafeHash :: forall i j c. SafeHash c i -> SafeHash c j
castSafeHash :: forall i j c. SafeHash c i -> SafeHash c j
castSafeHash (SafeHash Hash (HASH c) i
h) = forall c index. Hash (HASH c) index -> SafeHash c index
SafeHash (forall h a b. Hash h a -> Hash h b
Hash.castHash Hash (HASH c) i
h)