{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}

-- | Implementation of short hashing algorithm, suitable for testing.
module Cardano.Crypto.Hash.Short (
  ShortHash,
  Blake2bPrefix,
)
where

import Cardano.Crypto.Hash.Blake2b (blake2b_libsodium)
import Cardano.Crypto.Hash.Class

import Data.Proxy (Proxy (..))
import GHC.TypeLits (CmpNat, KnownNat, Nat, natVal)

type ShortHash = Blake2bPrefix 8

data Blake2bPrefix (n :: Nat)

instance (KnownNat n, CmpNat n 33 ~ 'LT) => HashAlgorithm (Blake2bPrefix n) where
  type SizeHash (Blake2bPrefix n) = n
  hashAlgorithmName :: forall (proxy :: * -> *). proxy (Blake2bPrefix n) -> String
hashAlgorithmName proxy (Blake2bPrefix n)
_ = String
"blake2b_prefix_" forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show (forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (forall {k} (t :: k). Proxy t
Proxy :: Proxy n))
  digest :: forall (proxy :: * -> *).
proxy (Blake2bPrefix n) -> ByteString -> ByteString
digest proxy (Blake2bPrefix n)
_ = Int -> ByteString -> ByteString
blake2b_libsodium (forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (forall {k} (t :: k). Proxy t
Proxy :: Proxy n)))