module Crypto.Curve.Secp256k1.MuSig2.Coordinator (
Blame (..),
BlameReason (..),
NonceRound,
newNonceRound,
receivePubNonce,
nonceRoundMissing,
finishNonceRound,
CollectedNonces,
collectedKeyAgg,
collectedPubNonces,
collectedAggNonce,
SigRound,
newSigRound,
sigRoundSession,
receivePartialSig,
sigRoundMissing,
finishSigRound,
SigRoundError (..),
) where
import Control.DeepSeq (NFData (..), rwhnf)
import Control.Exception (Exception)
import Crypto.Curve.Secp256k1.MuSig2 (
AggNonce,
DecodeError,
Encode (..),
KeyAggContext,
PartialSignature,
PubKey,
PubNonce,
Session,
Signature,
aggXOnlyPubKey,
keyAggPubKeys,
newSession,
nonceAgg,
partialSigAgg,
partialSigVerify,
sessionKeyAgg,
verify,
)
import Data.Bifunctor (first)
import Data.ByteString (ByteString)
import Data.Foldable (toList)
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IntMap
import Data.List.NonEmpty (NonEmpty (..), nonEmpty)
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
data Blame = Blame
{ Blame -> Int
blameSigner :: !Int
, Blame -> BlameReason
blameReason :: !BlameReason
}
deriving (Blame -> Blame -> Bool
(Blame -> Blame -> Bool) -> (Blame -> Blame -> Bool) -> Eq Blame
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Blame -> Blame -> Bool
== :: Blame -> Blame -> Bool
$c/= :: Blame -> Blame -> Bool
/= :: Blame -> Blame -> Bool
Eq, Int -> Blame -> ShowS
[Blame] -> ShowS
Blame -> String
(Int -> Blame -> ShowS)
-> (Blame -> String) -> ([Blame] -> ShowS) -> Show Blame
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Blame -> ShowS
showsPrec :: Int -> Blame -> ShowS
$cshow :: Blame -> String
show :: Blame -> String
$cshowList :: [Blame] -> ShowS
showList :: [Blame] -> ShowS
Show)
instance Exception Blame
instance NFData Blame where
rnf :: Blame -> ()
rnf = Blame -> ()
forall a. a -> ()
rwhnf
data BlameReason
=
InvalidPubNonceEncoding !DecodeError
|
InvalidPartialSigEncoding !DecodeError
|
InvalidPartialSignature
|
DuplicateContribution
|
UnknownSigner
|
MissingContribution
deriving (BlameReason -> BlameReason -> Bool
(BlameReason -> BlameReason -> Bool)
-> (BlameReason -> BlameReason -> Bool) -> Eq BlameReason
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BlameReason -> BlameReason -> Bool
== :: BlameReason -> BlameReason -> Bool
$c/= :: BlameReason -> BlameReason -> Bool
/= :: BlameReason -> BlameReason -> Bool
Eq, Int -> BlameReason -> ShowS
[BlameReason] -> ShowS
BlameReason -> String
(Int -> BlameReason -> ShowS)
-> (BlameReason -> String)
-> ([BlameReason] -> ShowS)
-> Show BlameReason
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BlameReason -> ShowS
showsPrec :: Int -> BlameReason -> ShowS
$cshow :: BlameReason -> String
show :: BlameReason -> String
$cshowList :: [BlameReason] -> ShowS
showList :: [BlameReason] -> ShowS
Show)
instance NFData BlameReason where
rnf :: BlameReason -> ()
rnf = BlameReason -> ()
forall a. a -> ()
rwhnf
collect :: Int -> IntMap a -> Either (NonEmpty Blame) (NonEmpty a)
collect :: forall a. Int -> IntMap a -> Either (NonEmpty Blame) (NonEmpty a)
collect Int
size IntMap a
received = case [Blame] -> Maybe (NonEmpty Blame)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty [Int -> BlameReason -> Blame
Blame Int
i BlameReason
MissingContribution | Left Int
i <- NonEmpty (Either Int a) -> [Either Int a]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList NonEmpty (Either Int a)
entries] of
Just NonEmpty Blame
missing -> NonEmpty Blame -> Either (NonEmpty Blame) (NonEmpty a)
forall a b. a -> Either a b
Left NonEmpty Blame
missing
Maybe (NonEmpty Blame)
Nothing -> (Either Int a -> Either (NonEmpty Blame) a)
-> NonEmpty (Either Int a) -> Either (NonEmpty Blame) (NonEmpty a)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> NonEmpty a -> f (NonEmpty b)
traverse ((Int -> NonEmpty Blame)
-> Either Int a -> Either (NonEmpty Blame) a
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (\Int
i -> Int -> BlameReason -> Blame
Blame Int
i BlameReason
MissingContribution Blame -> [Blame] -> NonEmpty Blame
forall a. a -> [a] -> NonEmpty a
:| [])) NonEmpty (Either Int a)
entries
where
entries :: NonEmpty (Either Int a)
entries = (Int -> Either Int a) -> NonEmpty Int -> NonEmpty (Either Int a)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Int
i -> Either Int a -> (a -> Either Int a) -> Maybe a -> Either Int a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Int -> Either Int a
forall a b. a -> Either a b
Left Int
i) a -> Either Int a
forall a b. b -> Either a b
Right (Int -> IntMap a -> Maybe a
forall a. Int -> IntMap a -> Maybe a
IntMap.lookup Int
i IntMap a
received)) (Int
0 Int -> [Int] -> NonEmpty Int
forall a. a -> [a] -> NonEmpty a
:| [Int
1 .. Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1])
missingFrom :: Int -> IntMap a -> [Int]
missingFrom :: forall a. Int -> IntMap a -> [Int]
missingFrom Int
size IntMap a
received = (Int -> Bool) -> [Int] -> [Int]
forall a. (a -> Bool) -> [a] -> [a]
filter (Int -> IntMap a -> Bool
forall a. Int -> IntMap a -> Bool
`IntMap.notMember` IntMap a
received) [Int
0 .. Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
data NonceRound = NonceRound
{ NonceRound -> KeyAggContext
nrKeyAgg :: !KeyAggContext
, NonceRound -> Int
nrSize :: !Int
, NonceRound -> IntMap PubNonce
nrNonces :: !(IntMap PubNonce)
}
instance NFData NonceRound where
rnf :: NonceRound -> ()
rnf (NonceRound KeyAggContext
ctx Int
size IntMap PubNonce
nonces) = KeyAggContext -> ()
forall a. NFData a => a -> ()
rnf KeyAggContext
ctx () -> () -> ()
forall a b. a -> b -> b
`seq` Int -> ()
forall a. NFData a => a -> ()
rnf Int
size () -> () -> ()
forall a b. a -> b -> b
`seq` IntMap PubNonce -> ()
forall a. NFData a => a -> ()
rnf IntMap PubNonce
nonces
newNonceRound :: KeyAggContext -> NonceRound
newNonceRound :: KeyAggContext -> NonceRound
newNonceRound KeyAggContext
ctx = KeyAggContext -> Int -> IntMap PubNonce -> NonceRound
NonceRound KeyAggContext
ctx (NonEmpty PubKey -> Int
forall a. NonEmpty a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (KeyAggContext -> NonEmpty PubKey
keyAggPubKeys KeyAggContext
ctx)) IntMap PubNonce
forall a. IntMap a
IntMap.empty
receivePubNonce ::
Int ->
ByteString ->
NonceRound ->
Either Blame NonceRound
receivePubNonce :: Int -> ByteString -> NonceRound -> Either Blame NonceRound
receivePubNonce Int
i ByteString
bytes NonceRound
nr
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= NonceRound -> Int
nrSize NonceRound
nr = BlameReason -> Either Blame NonceRound
forall {b}. BlameReason -> Either Blame b
blame BlameReason
UnknownSigner
| Just PubNonce
prev <- Int -> IntMap PubNonce -> Maybe PubNonce
forall a. Int -> IntMap a -> Maybe a
IntMap.lookup Int
i (NonceRound -> IntMap PubNonce
nrNonces NonceRound
nr) =
if PubNonce -> ByteString
forall a. Encode a => a -> ByteString
encode PubNonce
prev ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
bytes then NonceRound -> Either Blame NonceRound
forall a b. b -> Either a b
Right NonceRound
nr else BlameReason -> Either Blame NonceRound
forall {b}. BlameReason -> Either Blame b
blame BlameReason
DuplicateContribution
| Bool
otherwise = case ByteString -> Either DecodeError PubNonce
forall a. Encode a => ByteString -> Either DecodeError a
decode ByteString
bytes of
Left DecodeError
err -> BlameReason -> Either Blame NonceRound
forall {b}. BlameReason -> Either Blame b
blame (DecodeError -> BlameReason
InvalidPubNonceEncoding DecodeError
err)
Right PubNonce
pn -> NonceRound -> Either Blame NonceRound
forall a b. b -> Either a b
Right NonceRound
nr{nrNonces = IntMap.insert i pn (nrNonces nr)}
where
blame :: BlameReason -> Either Blame b
blame = Blame -> Either Blame b
forall a b. a -> Either a b
Left (Blame -> Either Blame b)
-> (BlameReason -> Blame) -> BlameReason -> Either Blame b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> BlameReason -> Blame
Blame Int
i
nonceRoundMissing :: NonceRound -> [Int]
nonceRoundMissing :: NonceRound -> [Int]
nonceRoundMissing NonceRound
nr = Int -> IntMap PubNonce -> [Int]
forall a. Int -> IntMap a -> [Int]
missingFrom (NonceRound -> Int
nrSize NonceRound
nr) (NonceRound -> IntMap PubNonce
nrNonces NonceRound
nr)
finishNonceRound :: NonceRound -> Either (NonEmpty Blame) CollectedNonces
finishNonceRound :: NonceRound -> Either (NonEmpty Blame) CollectedNonces
finishNonceRound NonceRound
nr = do
pns <- Int
-> IntMap PubNonce -> Either (NonEmpty Blame) (NonEmpty PubNonce)
forall a. Int -> IntMap a -> Either (NonEmpty Blame) (NonEmpty a)
collect (NonceRound -> Int
nrSize NonceRound
nr) (NonceRound -> IntMap PubNonce
nrNonces NonceRound
nr)
Right (CollectedNonces (nrKeyAgg nr) pns (nonceAgg pns))
data CollectedNonces = CollectedNonces
{ CollectedNonces -> KeyAggContext
cnKeyAgg :: !KeyAggContext
, CollectedNonces -> NonEmpty PubNonce
cnPubNonces :: !(NonEmpty PubNonce)
, CollectedNonces -> AggNonce
cnAggNonce :: !AggNonce
}
instance NFData CollectedNonces where
rnf :: CollectedNonces -> ()
rnf (CollectedNonces KeyAggContext
ctx NonEmpty PubNonce
pns AggNonce
agg) = KeyAggContext -> ()
forall a. NFData a => a -> ()
rnf KeyAggContext
ctx () -> () -> ()
forall a b. a -> b -> b
`seq` NonEmpty PubNonce -> ()
forall a. NFData a => a -> ()
rnf NonEmpty PubNonce
pns () -> () -> ()
forall a b. a -> b -> b
`seq` AggNonce -> ()
forall a. NFData a => a -> ()
rnf AggNonce
agg
collectedKeyAgg :: CollectedNonces -> KeyAggContext
collectedKeyAgg :: CollectedNonces -> KeyAggContext
collectedKeyAgg = CollectedNonces -> KeyAggContext
cnKeyAgg
collectedPubNonces :: CollectedNonces -> NonEmpty PubNonce
collectedPubNonces :: CollectedNonces -> NonEmpty PubNonce
collectedPubNonces = CollectedNonces -> NonEmpty PubNonce
cnPubNonces
collectedAggNonce :: CollectedNonces -> AggNonce
collectedAggNonce :: CollectedNonces -> AggNonce
collectedAggNonce = CollectedNonces -> AggNonce
cnAggNonce
data SigRound = SigRound
{ SigRound -> Session
srSession :: !Session
, SigRound -> ByteString
srMessage :: !ByteString
, SigRound -> Seq (PubKey, PubNonce)
srSigners :: !(Seq (PubKey, PubNonce))
, SigRound -> IntMap PartialSignature
srPsigs :: !(IntMap PartialSignature)
}
instance NFData SigRound where
rnf :: SigRound -> ()
rnf (SigRound Session
ses ByteString
msg Seq (PubKey, PubNonce)
signers IntMap PartialSignature
psigs) = Session -> ()
forall a. NFData a => a -> ()
rnf Session
ses () -> () -> ()
forall a b. a -> b -> b
`seq` ByteString -> ()
forall a. NFData a => a -> ()
rnf ByteString
msg () -> () -> ()
forall a b. a -> b -> b
`seq` Seq (PubKey, PubNonce) -> ()
forall a. NFData a => a -> ()
rnf Seq (PubKey, PubNonce)
signers () -> () -> ()
forall a b. a -> b -> b
`seq` IntMap PartialSignature -> ()
forall a. NFData a => a -> ()
rnf IntMap PartialSignature
psigs
newSigRound :: CollectedNonces -> ByteString -> SigRound
newSigRound :: CollectedNonces -> ByteString -> SigRound
newSigRound CollectedNonces
cn ByteString
msg =
SigRound
{ srSession :: Session
srSession = KeyAggContext -> AggNonce -> ByteString -> Session
newSession (CollectedNonces -> KeyAggContext
cnKeyAgg CollectedNonces
cn) (CollectedNonces -> AggNonce
cnAggNonce CollectedNonces
cn) ByteString
msg
, srMessage :: ByteString
srMessage = ByteString
msg
, srSigners :: Seq (PubKey, PubNonce)
srSigners = [(PubKey, PubNonce)] -> Seq (PubKey, PubNonce)
forall a. [a] -> Seq a
Seq.fromList ([PubKey] -> [PubNonce] -> [(PubKey, PubNonce)]
forall a b. [a] -> [b] -> [(a, b)]
zip (NonEmpty PubKey -> [PubKey]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (KeyAggContext -> NonEmpty PubKey
keyAggPubKeys (CollectedNonces -> KeyAggContext
cnKeyAgg CollectedNonces
cn))) (NonEmpty PubNonce -> [PubNonce]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (CollectedNonces -> NonEmpty PubNonce
cnPubNonces CollectedNonces
cn)))
, srPsigs :: IntMap PartialSignature
srPsigs = IntMap PartialSignature
forall a. IntMap a
IntMap.empty
}
sigRoundSession :: SigRound -> Session
sigRoundSession :: SigRound -> Session
sigRoundSession = SigRound -> Session
srSession
receivePartialSig ::
Int ->
ByteString ->
SigRound ->
Either Blame SigRound
receivePartialSig :: Int -> ByteString -> SigRound -> Either Blame SigRound
receivePartialSig Int
i ByteString
bytes SigRound
sr = case Int -> Seq (PubKey, PubNonce) -> Maybe (PubKey, PubNonce)
forall a. Int -> Seq a -> Maybe a
Seq.lookup Int
i (SigRound -> Seq (PubKey, PubNonce)
srSigners SigRound
sr) of
Maybe (PubKey, PubNonce)
Nothing -> BlameReason -> Either Blame SigRound
forall {b}. BlameReason -> Either Blame b
blame BlameReason
UnknownSigner
Just (PubKey
pk, PubNonce
pn) -> case Int -> IntMap PartialSignature -> Maybe PartialSignature
forall a. Int -> IntMap a -> Maybe a
IntMap.lookup Int
i (SigRound -> IntMap PartialSignature
srPsigs SigRound
sr) of
Just PartialSignature
prev -> if PartialSignature -> ByteString
forall a. Encode a => a -> ByteString
encode PartialSignature
prev ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
bytes then SigRound -> Either Blame SigRound
forall a b. b -> Either a b
Right SigRound
sr else BlameReason -> Either Blame SigRound
forall {b}. BlameReason -> Either Blame b
blame BlameReason
DuplicateContribution
Maybe PartialSignature
Nothing -> case ByteString -> Either DecodeError PartialSignature
forall a. Encode a => ByteString -> Either DecodeError a
decode ByteString
bytes of
Left DecodeError
err -> BlameReason -> Either Blame SigRound
forall {b}. BlameReason -> Either Blame b
blame (DecodeError -> BlameReason
InvalidPartialSigEncoding DecodeError
err)
Right PartialSignature
psig
| Session -> PubKey -> PubNonce -> PartialSignature -> Bool
partialSigVerify (SigRound -> Session
srSession SigRound
sr) PubKey
pk PubNonce
pn PartialSignature
psig -> SigRound -> Either Blame SigRound
forall a b. b -> Either a b
Right SigRound
sr{srPsigs = IntMap.insert i psig (srPsigs sr)}
| Bool
otherwise -> BlameReason -> Either Blame SigRound
forall {b}. BlameReason -> Either Blame b
blame BlameReason
InvalidPartialSignature
where
blame :: BlameReason -> Either Blame b
blame = Blame -> Either Blame b
forall a b. a -> Either a b
Left (Blame -> Either Blame b)
-> (BlameReason -> Blame) -> BlameReason -> Either Blame b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> BlameReason -> Blame
Blame Int
i
sigRoundMissing :: SigRound -> [Int]
sigRoundMissing :: SigRound -> [Int]
sigRoundMissing SigRound
sr = Int -> IntMap PartialSignature -> [Int]
forall a. Int -> IntMap a -> [Int]
missingFrom (Seq (PubKey, PubNonce) -> Int
forall a. Seq a -> Int
Seq.length (SigRound -> Seq (PubKey, PubNonce)
srSigners SigRound
sr)) (SigRound -> IntMap PartialSignature
srPsigs SigRound
sr)
data SigRoundError
=
SigRoundBlame !(NonEmpty Blame)
|
FinalSignatureInvalid
deriving (SigRoundError -> SigRoundError -> Bool
(SigRoundError -> SigRoundError -> Bool)
-> (SigRoundError -> SigRoundError -> Bool) -> Eq SigRoundError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SigRoundError -> SigRoundError -> Bool
== :: SigRoundError -> SigRoundError -> Bool
$c/= :: SigRoundError -> SigRoundError -> Bool
/= :: SigRoundError -> SigRoundError -> Bool
Eq, Int -> SigRoundError -> ShowS
[SigRoundError] -> ShowS
SigRoundError -> String
(Int -> SigRoundError -> ShowS)
-> (SigRoundError -> String)
-> ([SigRoundError] -> ShowS)
-> Show SigRoundError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SigRoundError -> ShowS
showsPrec :: Int -> SigRoundError -> ShowS
$cshow :: SigRoundError -> String
show :: SigRoundError -> String
$cshowList :: [SigRoundError] -> ShowS
showList :: [SigRoundError] -> ShowS
Show)
instance Exception SigRoundError
instance NFData SigRoundError where
rnf :: SigRoundError -> ()
rnf = SigRoundError -> ()
forall a. a -> ()
rwhnf
finishSigRound :: SigRound -> Either SigRoundError Signature
finishSigRound :: SigRound -> Either SigRoundError Signature
finishSigRound SigRound
sr = do
psigs <- (NonEmpty Blame -> SigRoundError)
-> Either (NonEmpty Blame) (NonEmpty PartialSignature)
-> Either SigRoundError (NonEmpty PartialSignature)
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first NonEmpty Blame -> SigRoundError
SigRoundBlame (Int
-> IntMap PartialSignature
-> Either (NonEmpty Blame) (NonEmpty PartialSignature)
forall a. Int -> IntMap a -> Either (NonEmpty Blame) (NonEmpty a)
collect (Seq (PubKey, PubNonce) -> Int
forall a. Seq a -> Int
Seq.length (SigRound -> Seq (PubKey, PubNonce)
srSigners SigRound
sr)) (SigRound -> IntMap PartialSignature
srPsigs SigRound
sr))
let ses = SigRound -> Session
srSession SigRound
sr
sig = Session -> NonEmpty PartialSignature -> Signature
partialSigAgg Session
ses NonEmpty PartialSignature
psigs
if verify (aggXOnlyPubKey (sessionKeyAgg ses)) (srMessage sr) sig then Right sig else Left FinalSignatureInvalid