{-# LANGUAGE OverloadedStrings #-}
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
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
streamThreshold :: Int
streamThreshold :: Int
streamThreshold = Int
65536
hashKeyAggList :: [ByteString] -> ByteString
hashKeyAggList :: [ByteString] -> ByteString
hashKeyAggList = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"KeyAgg list"
{-# NOINLINE hashKeyAggList #-}
hashKeyAggCoef :: [ByteString] -> ByteString
hashKeyAggCoef :: [ByteString] -> ByteString
hashKeyAggCoef = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"KeyAgg coefficient"
{-# NOINLINE hashKeyAggCoef #-}
hashNonceCoef :: [ByteString] -> ByteString
hashNonceCoef :: [ByteString] -> ByteString
hashNonceCoef = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"MuSig/noncecoef"
{-# NOINLINE hashNonceCoef #-}
hashChallenge :: [ByteString] -> ByteString
hashChallenge :: [ByteString] -> ByteString
hashChallenge = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"BIP0340/challenge"
{-# NOINLINE hashChallenge #-}
hashAux :: [ByteString] -> ByteString
hashAux :: [ByteString] -> ByteString
hashAux = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"MuSig/aux"
{-# NOINLINE hashAux #-}
hashNonce :: [ByteString] -> ByteString
hashNonce :: [ByteString] -> ByteString
hashNonce = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"MuSig/nonce"
{-# NOINLINE hashNonce #-}
hashDetNonce :: [ByteString] -> ByteString
hashDetNonce :: [ByteString] -> ByteString
hashDetNonce = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"MuSig/deterministic/nonce"
{-# NOINLINE hashDetNonce #-}
hashTapTweak :: [ByteString] -> ByteString
hashTapTweak :: [ByteString] -> ByteString
hashTapTweak = ByteString -> [ByteString] -> ByteString
taggedHash ByteString
"TapTweak"
{-# NOINLINE hashTapTweak #-}