-- SPDX-FileCopyrightText: 2021 Oxhead Alpha
-- SPDX-License-Identifier: LicenseRef-MIT-OA

-- | Cryptographic primitives related to hashing.

module Morley.Tezos.Crypto.Hash
  ( blake2b
  , blake2b160
  , keccak
  , sha256
  , sha3
  , sha512
  ) where

import Crypto.Hash (Blake2b_160, Blake2b_256, Digest, Keccak_256, SHA256, SHA3_256, SHA512, hash)
import Data.ByteArray qualified as ByteArray

-- | Compute a cryptographic hash of a bytestring using the
-- Blake2b_256 cryptographic hash function. It's used by the BLAKE2B
-- instruction in Michelson.
blake2b :: ByteString -> ByteString
blake2b :: ByteString -> ByteString
blake2b = forall a. Digest a -> ByteString
fromDigest @Blake2b_256 (Digest Blake2b_256 -> ByteString)
-> (ByteString -> Digest Blake2b_256) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest Blake2b_256
forall ba a.
(ByteArrayAccess ba, HashAlgorithm a) =>
ba -> Digest a
hash

-- | Compute a cryptographic hash of a bytestring using the
-- Blake2b_160 cryptographic hash function.
blake2b160 :: ByteString -> ByteString
blake2b160 :: ByteString -> ByteString
blake2b160 = forall a. Digest a -> ByteString
fromDigest @Blake2b_160 (Digest Blake2b_160 -> ByteString)
-> (ByteString -> Digest Blake2b_160) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest Blake2b_160
forall ba a.
(ByteArrayAccess ba, HashAlgorithm a) =>
ba -> Digest a
hash

-- | Compute a cryptographic hash of a bytestring using the
-- Sha256 cryptographic hash function.
sha256 :: ByteString -> ByteString
sha256 :: ByteString -> ByteString
sha256 = forall a. Digest a -> ByteString
fromDigest @SHA256 (Digest SHA256 -> ByteString)
-> (ByteString -> Digest SHA256) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA256
forall ba a.
(ByteArrayAccess ba, HashAlgorithm a) =>
ba -> Digest a
hash

-- | Compute a cryptographic hash of a bytestring using the
-- Sha512 cryptographic hash function.
sha512 :: ByteString -> ByteString
sha512 :: ByteString -> ByteString
sha512 = forall a. Digest a -> ByteString
fromDigest @SHA512 (Digest SHA512 -> ByteString)
-> (ByteString -> Digest SHA512) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA512
forall ba a.
(ByteArrayAccess ba, HashAlgorithm a) =>
ba -> Digest a
hash

-- | Compute a cryptographic hash of a bytestring using the Sha3_256
-- cryptographic hash function. It is used by the SHA3 Michelson instruction.
sha3 :: ByteString -> ByteString
sha3 :: ByteString -> ByteString
sha3 = forall a. Digest a -> ByteString
fromDigest @SHA3_256 (Digest SHA3_256 -> ByteString)
-> (ByteString -> Digest SHA3_256) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest SHA3_256
forall ba a.
(ByteArrayAccess ba, HashAlgorithm a) =>
ba -> Digest a
hash

-- | Compute a cryptographic hash of a bytestring using the Keccak_256
-- cryptographic hash function. It is used by the KECCAK Michelson instruction.
keccak :: ByteString -> ByteString
keccak :: ByteString -> ByteString
keccak = forall a. Digest a -> ByteString
fromDigest @Keccak_256 (Digest Keccak_256 -> ByteString)
-> (ByteString -> Digest Keccak_256) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Digest Keccak_256
forall ba a.
(ByteArrayAccess ba, HashAlgorithm a) =>
ba -> Digest a
hash

fromDigest :: forall a. Digest a -> ByteString
fromDigest :: forall a. Digest a -> ByteString
fromDigest = Digest a -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
ByteArray.convert