{-# LANGUAGE BangPatterns #-}
module Crypto.Curve.Secp256k1.MuSig2.Internal.Nonce (
SecNonce,
secNoncePubKey,
secNoncePubNonce,
secNonceAggPubKey,
secNonceMessage,
claimSecNonce,
secNonceIsUsed,
peekSecNonce,
newSecNonce,
secNonceFromScalars,
secNonceFromBytes,
secNonceToBytes,
pubNonceFromScalars,
NonceInputs (..),
noNonceInputs,
validateNonceInputs,
nonceGen,
nonceGenWithRand,
nonceGenCounter,
nonceGenScalars,
deterministicNonceScalars,
nonceAgg,
) where
import Control.DeepSeq (NFData (..), rwhnf)
import Control.Monad (when)
import Crypto.Curve.Secp256k1.MuSig2.Internal.Error (DecodeError (..), NonceGenError (..))
import Crypto.Curve.Secp256k1.MuSig2.Internal.Hash (hashAux, hashDetNonce, hashNonce)
import Crypto.Curve.Secp256k1.MuSig2.Internal.OneShot (OneShot, claimOneShot, isClaimed, newOneShot, peekOneShot)
import Crypto.Curve.Secp256k1.MuSig2.Internal.Point (mulGSecret, sumPoints)
import Crypto.Curve.Secp256k1.MuSig2.Internal.Scalar (SecScalar, secScalarFromBytes, secScalarFromBytesModQ, secScalarToBytes)
import Crypto.Curve.Secp256k1.MuSig2.Internal.Types (
AggNonce (..),
Encode (..),
PubKey (..),
PubNonce (..),
SecKey (..),
XOnlyPubKey (..),
expectLength,
mkAggNonce,
mkPubNonce,
)
import Data.Bits (Bits, shiftR, xor)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.List.NonEmpty (NonEmpty)
import Data.Maybe (fromMaybe)
import Data.Word (Word64, Word8)
import System.Entropy (getEntropy)
data SecNonce = MkSecNonce
{ SecNonce -> OneShot
snSecret :: !OneShot
, SecNonce -> PubKey
snPubKey :: !PubKey
, SecNonce -> PubNonce
snPubNonce :: !PubNonce
, SecNonce -> Maybe XOnlyPubKey
snAggPubKey :: !(Maybe XOnlyPubKey)
, SecNonce -> Maybe ByteString
snMessage :: !(Maybe ByteString)
}
instance NFData SecNonce where
rnf :: SecNonce -> ()
rnf (MkSecNonce OneShot
s PubKey
pk PubNonce
pn Maybe XOnlyPubKey
apk Maybe ByteString
m) = OneShot -> ()
forall a. a -> ()
rwhnf OneShot
s () -> () -> ()
forall a b. a -> b -> b
`seq` PubKey -> ()
forall a. NFData a => a -> ()
rnf PubKey
pk () -> () -> ()
forall a b. a -> b -> b
`seq` PubNonce -> ()
forall a. NFData a => a -> ()
rnf PubNonce
pn () -> () -> ()
forall a b. a -> b -> b
`seq` Maybe XOnlyPubKey -> ()
forall a. NFData a => a -> ()
rnf Maybe XOnlyPubKey
apk () -> () -> ()
forall a b. a -> b -> b
`seq` Maybe ByteString -> ()
forall a. NFData a => a -> ()
rnf Maybe ByteString
m
secNoncePubKey :: SecNonce -> PubKey
secNoncePubKey :: SecNonce -> PubKey
secNoncePubKey = SecNonce -> PubKey
snPubKey
secNoncePubNonce :: SecNonce -> PubNonce
secNoncePubNonce :: SecNonce -> PubNonce
secNoncePubNonce = SecNonce -> PubNonce
snPubNonce
secNonceAggPubKey :: SecNonce -> Maybe XOnlyPubKey
secNonceAggPubKey :: SecNonce -> Maybe XOnlyPubKey
secNonceAggPubKey = SecNonce -> Maybe XOnlyPubKey
snAggPubKey
secNonceMessage :: SecNonce -> Maybe ByteString
secNonceMessage :: SecNonce -> Maybe ByteString
secNonceMessage = SecNonce -> Maybe ByteString
snMessage
claimSecNonce :: SecNonce -> IO (Maybe (SecScalar, SecScalar))
claimSecNonce :: SecNonce -> IO (Maybe (SecScalar, SecScalar))
claimSecNonce SecNonce
sn = do
claimed <- OneShot -> IO (Maybe ByteString)
claimOneShot (SecNonce -> OneShot
snSecret SecNonce
sn)
case claimed of
Maybe ByteString
Nothing -> Maybe (SecScalar, SecScalar) -> IO (Maybe (SecScalar, SecScalar))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (SecScalar, SecScalar)
forall a. Maybe a
Nothing
Just ByteString
bytes -> do
let !k1 :: SecScalar
k1 = ByteString -> SecScalar
secScalarFromBytesModQ (Int -> ByteString -> ByteString
BS.take Int
32 ByteString
bytes)
!k2 :: SecScalar
k2 = ByteString -> SecScalar
secScalarFromBytesModQ (Int -> ByteString -> ByteString
BS.drop Int
32 ByteString
bytes)
Maybe (SecScalar, SecScalar) -> IO (Maybe (SecScalar, SecScalar))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((SecScalar, SecScalar) -> Maybe (SecScalar, SecScalar)
forall a. a -> Maybe a
Just (SecScalar
k1, SecScalar
k2))
secNonceIsUsed :: SecNonce -> IO Bool
secNonceIsUsed :: SecNonce -> IO Bool
secNonceIsUsed = OneShot -> IO Bool
isClaimed (OneShot -> IO Bool)
-> (SecNonce -> OneShot) -> SecNonce -> IO Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SecNonce -> OneShot
snSecret
peekSecNonce :: SecNonce -> IO ByteString
peekSecNonce :: SecNonce -> IO ByteString
peekSecNonce = OneShot -> IO ByteString
peekOneShot (OneShot -> IO ByteString)
-> (SecNonce -> OneShot) -> SecNonce -> IO ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SecNonce -> OneShot
snSecret
newSecNonce :: SecScalar -> SecScalar -> PubKey -> PubNonce -> Maybe XOnlyPubKey -> Maybe ByteString -> IO SecNonce
newSecNonce :: SecScalar
-> SecScalar
-> PubKey
-> PubNonce
-> Maybe XOnlyPubKey
-> Maybe ByteString
-> IO SecNonce
newSecNonce SecScalar
k1 SecScalar
k2 PubKey
pk PubNonce
pn Maybe XOnlyPubKey
apk Maybe ByteString
m = do
secret <- ByteString -> IO OneShot
newOneShot (SecScalar -> ByteString
secScalarToBytes SecScalar
k1 ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> SecScalar -> ByteString
secScalarToBytes SecScalar
k2)
pure (MkSecNonce secret pk pn apk m)
pubNonceFromScalars :: SecScalar -> SecScalar -> Maybe PubNonce
pubNonceFromScalars :: SecScalar -> SecScalar -> Maybe PubNonce
pubNonceFromScalars SecScalar
k1 SecScalar
k2 = Projective -> Projective -> PubNonce
mkPubNonce (Projective -> Projective -> PubNonce)
-> Maybe Projective -> Maybe (Projective -> PubNonce)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SecScalar -> Maybe Projective
mulGSecret SecScalar
k1 Maybe (Projective -> PubNonce)
-> Maybe Projective -> Maybe PubNonce
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SecScalar -> Maybe Projective
mulGSecret SecScalar
k2
secNonceFromScalars :: SecScalar -> SecScalar -> PubKey -> IO (Either DecodeError SecNonce)
secNonceFromScalars :: SecScalar
-> SecScalar -> PubKey -> IO (Either DecodeError SecNonce)
secNonceFromScalars SecScalar
k1 SecScalar
k2 PubKey
pk = case SecScalar -> SecScalar -> Maybe PubNonce
pubNonceFromScalars SecScalar
k1 SecScalar
k2 of
Maybe PubNonce
Nothing -> Either DecodeError SecNonce -> IO (Either DecodeError SecNonce)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DecodeError -> Either DecodeError SecNonce
forall a b. a -> Either a b
Left DecodeError
ScalarZero)
Just PubNonce
pn -> SecNonce -> Either DecodeError SecNonce
forall a b. b -> Either a b
Right (SecNonce -> Either DecodeError SecNonce)
-> IO SecNonce -> IO (Either DecodeError SecNonce)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SecScalar
-> SecScalar
-> PubKey
-> PubNonce
-> Maybe XOnlyPubKey
-> Maybe ByteString
-> IO SecNonce
newSecNonce SecScalar
k1 SecScalar
k2 PubKey
pk PubNonce
pn Maybe XOnlyPubKey
forall a. Maybe a
Nothing Maybe ByteString
forall a. Maybe a
Nothing
secNonceFromBytes :: ByteString -> IO (Either DecodeError SecNonce)
secNonceFromBytes :: ByteString -> IO (Either DecodeError SecNonce)
secNonceFromBytes ByteString
b = case Either DecodeError (SecScalar, SecScalar, PubKey)
parts of
Left DecodeError
err -> Either DecodeError SecNonce -> IO (Either DecodeError SecNonce)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DecodeError -> Either DecodeError SecNonce
forall a b. a -> Either a b
Left DecodeError
err)
Right (SecScalar
k1, SecScalar
k2, PubKey
pk) -> SecScalar
-> SecScalar -> PubKey -> IO (Either DecodeError SecNonce)
secNonceFromScalars SecScalar
k1 SecScalar
k2 PubKey
pk
where
parts :: Either DecodeError (SecScalar, SecScalar, PubKey)
parts = do
Int -> ByteString -> Either DecodeError ()
expectLength Int
97 ByteString
b
pk <- ByteString -> Either DecodeError PubKey
forall a. Encode a => ByteString -> Either DecodeError a
decode (Int -> ByteString -> ByteString
BS.drop Int
64 ByteString
b)
k1 <- secScalarFromBytes (BS.take 32 b)
k2 <- secScalarFromBytes (BS.take 32 (BS.drop 32 b))
Right (k1, k2, pk)
secNonceToBytes :: SecNonce -> IO (Maybe ByteString)
secNonceToBytes :: SecNonce -> IO (Maybe ByteString)
secNonceToBytes SecNonce
sn = (ByteString -> ByteString) -> Maybe ByteString -> Maybe ByteString
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> PubKey -> ByteString
pubKeyBytes (SecNonce -> PubKey
snPubKey SecNonce
sn)) (Maybe ByteString -> Maybe ByteString)
-> IO (Maybe ByteString) -> IO (Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> OneShot -> IO (Maybe ByteString)
claimOneShot (SecNonce -> OneShot
snSecret SecNonce
sn)
data NonceInputs = NonceInputs
{ NonceInputs -> Maybe SecKey
nonceSecKey :: !(Maybe SecKey)
, NonceInputs -> Maybe XOnlyPubKey
nonceAggPubKey :: !(Maybe XOnlyPubKey)
, NonceInputs -> Maybe ByteString
nonceMessage :: !(Maybe ByteString)
, :: !(Maybe ByteString)
}
noNonceInputs :: NonceInputs
noNonceInputs :: NonceInputs
noNonceInputs = Maybe SecKey
-> Maybe XOnlyPubKey
-> Maybe ByteString
-> Maybe ByteString
-> NonceInputs
NonceInputs Maybe SecKey
forall a. Maybe a
Nothing Maybe XOnlyPubKey
forall a. Maybe a
Nothing Maybe ByteString
forall a. Maybe a
Nothing Maybe ByteString
forall a. Maybe a
Nothing
validateNonceInputs :: PubKey -> NonceInputs -> Either NonceGenError ()
validateNonceInputs :: PubKey -> NonceInputs -> Either NonceGenError ()
validateNonceInputs PubKey
pk NonceInputs
inputs = do
case NonceInputs -> Maybe SecKey
nonceSecKey NonceInputs
inputs of
Just SecKey
sk | SecKey -> PubKey
secKeyPub SecKey
sk PubKey -> PubKey -> Bool
forall a. Eq a => a -> a -> Bool
/= PubKey
pk -> NonceGenError -> Either NonceGenError ()
forall a b. a -> Either a b
Left NonceGenError
NonceSecKeyMismatch
Maybe SecKey
_ -> () -> Either NonceGenError ()
forall a b. b -> Either a b
Right ()
Bool -> Either NonceGenError () -> Either NonceGenError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> (ByteString -> Bool) -> Maybe ByteString -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False ((Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
0xFFFFFFFFFFFFFFFF) (Integer -> Bool) -> (ByteString -> Integer) -> ByteString -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> (ByteString -> Int) -> ByteString -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
BS.length) (NonceInputs -> Maybe ByteString
nonceMessage NonceInputs
inputs)) (NonceGenError -> Either NonceGenError ()
forall a b. a -> Either a b
Left NonceGenError
NonceMessageTooLong)
Bool -> Either NonceGenError () -> Either NonceGenError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> (ByteString -> Bool) -> Maybe ByteString -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False ((Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
0xFFFFFFFF) (Integer -> Bool) -> (ByteString -> Integer) -> ByteString -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> (ByteString -> Int) -> ByteString -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
BS.length) (NonceInputs -> Maybe ByteString
nonceExtraInput NonceInputs
inputs)) (NonceGenError -> Either NonceGenError ()
forall a b. a -> Either a b
Left NonceGenError
NonceExtraInputTooLong)
nonceGen :: PubKey -> NonceInputs -> IO (Either NonceGenError (SecNonce, PubNonce))
nonceGen :: PubKey
-> NonceInputs -> IO (Either NonceGenError (SecNonce, PubNonce))
nonceGen PubKey
pk NonceInputs
inputs = case PubKey -> NonceInputs -> Either NonceGenError ()
validateNonceInputs PubKey
pk NonceInputs
inputs of
Left NonceGenError
err -> Either NonceGenError (SecNonce, PubNonce)
-> IO (Either NonceGenError (SecNonce, PubNonce))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NonceGenError -> Either NonceGenError (SecNonce, PubNonce)
forall a b. a -> Either a b
Left NonceGenError
err)
Right () -> IO (Either NonceGenError (SecNonce, PubNonce))
forall {a}. IO (Either a (SecNonce, PubNonce))
loop
where
loop :: IO (Either a (SecNonce, PubNonce))
loop = do
rand <- Int -> IO ByteString
getEntropy Int
32
let (k1, k2) = nonceGenScalars rand pk inputs
case pubNonceFromScalars k1 k2 of
Maybe PubNonce
Nothing -> IO (Either a (SecNonce, PubNonce))
loop
Just PubNonce
pn -> do
sn <- SecScalar
-> SecScalar
-> PubKey
-> PubNonce
-> Maybe XOnlyPubKey
-> Maybe ByteString
-> IO SecNonce
newSecNonce SecScalar
k1 SecScalar
k2 PubKey
pk PubNonce
pn (NonceInputs -> Maybe XOnlyPubKey
nonceAggPubKey NonceInputs
inputs) (NonceInputs -> Maybe ByteString
nonceMessage NonceInputs
inputs)
pure (Right (sn, pn))
nonceGenWithRand :: ByteString -> PubKey -> NonceInputs -> IO (Either NonceGenError (SecNonce, PubNonce))
nonceGenWithRand :: ByteString
-> PubKey
-> NonceInputs
-> IO (Either NonceGenError (SecNonce, PubNonce))
nonceGenWithRand ByteString
rand PubKey
pk NonceInputs
inputs = case Either NonceGenError (SecScalar, SecScalar, PubNonce)
scalars of
Left NonceGenError
err -> Either NonceGenError (SecNonce, PubNonce)
-> IO (Either NonceGenError (SecNonce, PubNonce))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NonceGenError -> Either NonceGenError (SecNonce, PubNonce)
forall a b. a -> Either a b
Left NonceGenError
err)
Right (SecScalar
k1, SecScalar
k2, PubNonce
pn) -> do
sn <- SecScalar
-> SecScalar
-> PubKey
-> PubNonce
-> Maybe XOnlyPubKey
-> Maybe ByteString
-> IO SecNonce
newSecNonce SecScalar
k1 SecScalar
k2 PubKey
pk PubNonce
pn (NonceInputs -> Maybe XOnlyPubKey
nonceAggPubKey NonceInputs
inputs) (NonceInputs -> Maybe ByteString
nonceMessage NonceInputs
inputs)
pure (Right (sn, pn))
where
scalars :: Either NonceGenError (SecScalar, SecScalar, PubNonce)
scalars = do
PubKey -> NonceInputs -> Either NonceGenError ()
validateNonceInputs PubKey
pk NonceInputs
inputs
Bool -> Either NonceGenError () -> Either NonceGenError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> Int
BS.length ByteString
rand Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
32) (NonceGenError -> Either NonceGenError ()
forall a b. a -> Either a b
Left (Int -> NonceGenError
InvalidRandLength (ByteString -> Int
BS.length ByteString
rand)))
let (SecScalar
k1, SecScalar
k2) = ByteString -> PubKey -> NonceInputs -> (SecScalar, SecScalar)
nonceGenScalars ByteString
rand PubKey
pk NonceInputs
inputs
pn <- Either NonceGenError PubNonce
-> (PubNonce -> Either NonceGenError PubNonce)
-> Maybe PubNonce
-> Either NonceGenError PubNonce
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (NonceGenError -> Either NonceGenError PubNonce
forall a b. a -> Either a b
Left NonceGenError
NonceIsZero) PubNonce -> Either NonceGenError PubNonce
forall a b. b -> Either a b
Right (SecScalar -> SecScalar -> Maybe PubNonce
pubNonceFromScalars SecScalar
k1 SecScalar
k2)
Right (k1, k2, pn)
nonceGenCounter :: SecKey -> Word64 -> NonceInputs -> IO (Either NonceGenError (SecNonce, PubNonce))
nonceGenCounter :: SecKey
-> Word64
-> NonceInputs
-> IO (Either NonceGenError (SecNonce, PubNonce))
nonceGenCounter SecKey
sk Word64
counter NonceInputs
inputs =
ByteString
-> PubKey
-> NonceInputs
-> IO (Either NonceGenError (SecNonce, PubNonce))
nonceGenWithRand (Int -> Word64 -> ByteString
forall a. (Integral a, Bits a) => Int -> a -> ByteString
bigEndian Int
8 Word64
counter ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Int -> Word8 -> ByteString
BS.replicate Int
24 Word8
0) (SecKey -> PubKey
secKeyPub SecKey
sk) NonceInputs
inputs{nonceSecKey = Just sk}
nonceGenScalars :: ByteString -> PubKey -> NonceInputs -> (SecScalar, SecScalar)
nonceGenScalars :: ByteString -> PubKey -> NonceInputs -> (SecScalar, SecScalar)
nonceGenScalars ByteString
rand PubKey
pk NonceInputs
inputs = (Word8 -> SecScalar
k Word8
0, Word8 -> SecScalar
k Word8
1)
where
rand' :: ByteString
rand' = case NonceInputs -> Maybe SecKey
nonceSecKey NonceInputs
inputs of
Maybe SecKey
Nothing -> ByteString
rand
Just SecKey
sk -> (Word8 -> Word8 -> Word8) -> ByteString -> ByteString -> ByteString
BS.packZipWith Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
xor (SecScalar -> ByteString
secScalarToBytes (SecKey -> SecScalar
secKeyScalar SecKey
sk)) ([ByteString] -> ByteString
hashAux [ByteString
rand])
pkBytes :: ByteString
pkBytes = PubKey -> ByteString
pubKeyBytes PubKey
pk
aggpk :: ByteString
aggpk = ByteString
-> (XOnlyPubKey -> ByteString) -> Maybe XOnlyPubKey -> ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ByteString
BS.empty XOnlyPubKey -> ByteString
xOnlyBytes (NonceInputs -> Maybe XOnlyPubKey
nonceAggPubKey NonceInputs
inputs)
msgPrefixed :: [ByteString]
msgPrefixed = case NonceInputs -> Maybe ByteString
nonceMessage NonceInputs
inputs of
Maybe ByteString
Nothing -> [Word8 -> ByteString
BS.singleton Word8
0]
Just ByteString
m -> [Word8 -> ByteString
BS.singleton Word8
1, Int -> Int -> ByteString
forall a. (Integral a, Bits a) => Int -> a -> ByteString
bigEndian Int
8 (ByteString -> Int
BS.length ByteString
m), ByteString
m]
extra :: ByteString
extra = ByteString -> Maybe ByteString -> ByteString
forall a. a -> Maybe a -> a
fromMaybe ByteString
BS.empty (NonceInputs -> Maybe ByteString
nonceExtraInput NonceInputs
inputs)
prefix :: [ByteString]
prefix =
[ ByteString
rand'
, Int -> Int -> ByteString
forall a. (Integral a, Bits a) => Int -> a -> ByteString
bigEndian Int
1 (ByteString -> Int
BS.length ByteString
pkBytes)
, ByteString
pkBytes
, Int -> Int -> ByteString
forall a. (Integral a, Bits a) => Int -> a -> ByteString
bigEndian Int
1 (ByteString -> Int
BS.length ByteString
aggpk)
, ByteString
aggpk
]
[ByteString] -> [ByteString] -> [ByteString]
forall a. [a] -> [a] -> [a]
++ [ByteString]
msgPrefixed
[ByteString] -> [ByteString] -> [ByteString]
forall a. [a] -> [a] -> [a]
++ [Int -> Int -> ByteString
forall a. (Integral a, Bits a) => Int -> a -> ByteString
bigEndian Int
4 (ByteString -> Int
BS.length ByteString
extra), ByteString
extra]
k :: Word8 -> SecScalar
k :: Word8 -> SecScalar
k Word8
i = ByteString -> SecScalar
secScalarFromBytesModQ ([ByteString] -> ByteString
hashNonce ([ByteString]
prefix [ByteString] -> [ByteString] -> [ByteString]
forall a. [a] -> [a] -> [a]
++ [Word8 -> ByteString
BS.singleton Word8
i]))
deterministicNonceScalars ::
SecKey ->
AggNonce ->
XOnlyPubKey ->
ByteString ->
Maybe ByteString ->
(SecScalar, SecScalar)
deterministicNonceScalars :: SecKey
-> AggNonce
-> XOnlyPubKey
-> ByteString
-> Maybe ByteString
-> (SecScalar, SecScalar)
deterministicNonceScalars SecKey
sk AggNonce
aggOther XOnlyPubKey
aggpk ByteString
msg Maybe ByteString
rand = (Word8 -> SecScalar
k Word8
0, Word8 -> SecScalar
k Word8
1)
where
skBytes :: ByteString
skBytes = SecScalar -> ByteString
secScalarToBytes (SecKey -> SecScalar
secKeyScalar SecKey
sk)
sk' :: ByteString
sk' = ByteString
-> (ByteString -> ByteString) -> Maybe ByteString -> ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ByteString
skBytes ((Word8 -> Word8 -> Word8) -> ByteString -> ByteString -> ByteString
BS.packZipWith Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
xor ByteString
skBytes (ByteString -> ByteString)
-> (ByteString -> ByteString) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> ByteString
hashAux ([ByteString] -> ByteString)
-> (ByteString -> [ByteString]) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure) Maybe ByteString
rand
prefix :: [ByteString]
prefix = [ByteString
sk', AggNonce -> ByteString
aggNonceBytes AggNonce
aggOther, XOnlyPubKey -> ByteString
xOnlyBytes XOnlyPubKey
aggpk, Int -> Int -> ByteString
forall a. (Integral a, Bits a) => Int -> a -> ByteString
bigEndian Int
8 (ByteString -> Int
BS.length ByteString
msg), ByteString
msg]
k :: Word8 -> SecScalar
k :: Word8 -> SecScalar
k Word8
i = ByteString -> SecScalar
secScalarFromBytesModQ ([ByteString] -> ByteString
hashDetNonce ([ByteString]
prefix [ByteString] -> [ByteString] -> [ByteString]
forall a. [a] -> [a] -> [a]
++ [Word8 -> ByteString
BS.singleton Word8
i]))
bigEndian :: (Integral a, Bits a) => Int -> a -> ByteString
bigEndian :: forall a. (Integral a, Bits a) => Int -> a -> ByteString
bigEndian Int
n a
x = [Word8] -> ByteString
BS.pack [a -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a
x a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` (Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
j)) | Int
j <- [Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1, Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2 .. Int
0]]
nonceAgg :: NonEmpty PubNonce -> AggNonce
nonceAgg :: NonEmpty PubNonce -> AggNonce
nonceAgg NonEmpty PubNonce
pns = Projective -> Projective -> AggNonce
mkAggNonce (NonEmpty Projective -> Projective
forall (f :: * -> *). Foldable f => f Projective -> Projective
sumPoints ((PubNonce -> Projective)
-> NonEmpty PubNonce -> NonEmpty Projective
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PubNonce -> Projective
pubNonceR1 NonEmpty PubNonce
pns)) (NonEmpty Projective -> Projective
forall (f :: * -> *). Foldable f => f Projective -> Projective
sumPoints ((PubNonce -> Projective)
-> NonEmpty PubNonce -> NonEmpty Projective
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PubNonce -> Projective
pubNonceR2 NonEmpty PubNonce
pns))