{-# LANGUAGE OverloadedStrings #-}

{- |
Module: Crypto.Curve.Secp256k1.MuSig2.Internal.Hash
Copyright: (c) 2025 Jose Storopoli
License: MIT
Maintainer: Jose Storopoli <jose@storopoli.com>

The BIP-340 tagged hashes used by BIP-327 and BIP-341, with the tag prefix
\(SHA256(tag) \| SHA256(tag)\) computed once per tag.

Each hash takes its input as a list of chunks, hashed as their
concatenation. Inputs shorter than 'streamThreshold' bytes are joined and
hashed with the hardware-accelerated @hash@ of @ppad-sha256@; longer ones
(long messages or extra inputs) are hashed chunk by chunk with @hash_lazy@,
so the library never copies a long message to hash it.

Not part of the public API.
-}
module Crypto.Curve.Secp256k1.MuSig2.Internal.Hash (
  taggedHash,
  streamThreshold,
  hashKeyAggList,
  hashKeyAggCoef,
  hashNonceCoef,
  hashChallenge,
  hashAux,
  hashNonce,
  hashDetNonce,
  hashTapTweak,
) where

import Crypto.Hash.SHA256 (hash, hash_lazy)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL

{- | @hash_tag(x) = SHA256(SHA256(tag) || SHA256(tag) || x)@, where \(x\) is
the concatenation of the chunks.

Partially apply it to a tag to share the prefix between calls.
-}
taggedHash :: ByteString -> [ByteString] -> ByteString
taggedHash :: ByteString -> [ByteString] -> ByteString
taggedHash ByteString
tag = \[ByteString]
xs ->
  let chunks :: [ByteString]
chunks = ByteString
prefix ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
xs
   in if [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((ByteString -> Int) -> [ByteString] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ByteString -> Int
BS.length [ByteString]
xs) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
streamThreshold
        then ByteString -> ByteString
hash ([ByteString] -> ByteString
BS.concat [ByteString]
chunks)
        else ByteString -> ByteString
hash_lazy ([ByteString] -> ByteString
BL.fromChunks [ByteString]
chunks)
 where
  th :: ByteString
th = ByteString -> ByteString
hash ByteString
tag
  prefix :: ByteString
prefix = ByteString
th ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
th

{- | Inputs of at least this many bytes (64 KiB) are hashed chunk by chunk
instead of being joined first.

Below it, joining costs less than the slower chunked hash; above it, the
copy would dominate memory use.
-}
streamThreshold :: Int
streamThreshold :: Int
streamThreshold = Int
65536

-- | @hash_KeyAgg list@.
hashKeyAggList :: [ByteString] -> ByteString
hashKeyAggList :: [ByteString] -> ByteString
hashKeyAggList = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"KeyAgg list"
{-# NOINLINE hashKeyAggList #-}

-- | @hash_KeyAgg coefficient@.
hashKeyAggCoef :: [ByteString] -> ByteString
hashKeyAggCoef :: [ByteString] -> ByteString
hashKeyAggCoef = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"KeyAgg coefficient"
{-# NOINLINE hashKeyAggCoef #-}

-- | @hash_MuSig/noncecoef@.
hashNonceCoef :: [ByteString] -> ByteString
hashNonceCoef :: [ByteString] -> ByteString
hashNonceCoef = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"MuSig/noncecoef"
{-# NOINLINE hashNonceCoef #-}

-- | @hash_BIP0340/challenge@.
hashChallenge :: [ByteString] -> ByteString
hashChallenge :: [ByteString] -> ByteString
hashChallenge = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"BIP0340/challenge"
{-# NOINLINE hashChallenge #-}

-- | @hash_MuSig/aux@.
hashAux :: [ByteString] -> ByteString
hashAux :: [ByteString] -> ByteString
hashAux = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"MuSig/aux"
{-# NOINLINE hashAux #-}

-- | @hash_MuSig/nonce@.
hashNonce :: [ByteString] -> ByteString
hashNonce :: [ByteString] -> ByteString
hashNonce = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"MuSig/nonce"
{-# NOINLINE hashNonce #-}

-- | @hash_MuSig/deterministic/nonce@.
hashDetNonce :: [ByteString] -> ByteString
hashDetNonce :: [ByteString] -> ByteString
hashDetNonce = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"MuSig/deterministic/nonce"
{-# NOINLINE hashDetNonce #-}

-- | @hash_TapTweak@ (BIP-341).
hashTapTweak :: [ByteString] -> ByteString
hashTapTweak :: [ByteString] -> ByteString
hashTapTweak = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"TapTweak"
{-# NOINLINE hashTapTweak #-}