| Copyright | (c) 2025 Jose Storopoli |
|---|---|
| License | MIT |
| Maintainer | Jose Storopoli <jose@storopoli.com> |
| Safe Haskell | None |
| Language | Haskell2010 |
Crypto.Curve.Secp256k1.MuSig2.Coordinator
Description
A coordinator for BIP-327 signing sessions: it collects the signers' contributions as bytes from the network, decodes and verifies each one as it arrives, and blames the signer responsible for anything wrong.
The coordinator can be one of the signers or a separate party. It runs two rounds:
- Public nonces.
newNonceRoundon the session's key aggregation context (with all tweaks applied),receivePubNoncefor each signer's encodedPubNonce, thenfinishNonceRound, which gives the aggregate nonce to send back to every signer. - Partial signatures.
newSigRoundon the collected nonces and the message,receivePartialSigfor each signer's encodedPartialSignature(each one is checked withpartialSigVerify), thenfinishSigRound, which aggregates them and checks the final BIP-340 signature.
Signers are identified by their index in keyAggPubKeys, from \(0\). A
public key that appears more than once in the key list belongs to as many
signers, each with its own index, nonce and partial signature.
Every function is pure and every value is immutable, so the rounds are safe
to share between threads. To receive from several threads, keep the round in
an IORef (updated with atomicModifyIORef'), an MVar or a TVar.
Blame
receivePubNonce and receivePartialSig return a Blame naming the
signer and the reason, and leave the round unchanged: the caller decides
whether to abort the session or wait for the signer to send a valid
contribution. A signer may send the same bytes again (a network retry is not
an error), but a different contribution from a signer whose contribution was
accepted is a DuplicateContribution. The finishing functions blame every
signer whose contribution is still missing. Blames carry no secrets.
The signer index passed to receivePubNonce and receivePartialSig is
trusted: take it from the authenticated connection the bytes arrived on (for
example, a roster that maps each signer's authenticated identity to its
index), never from the message itself. A participant that can choose the
index can send a valid nonce in an honest signer's slot first: the honest
signer's own nonce is then blamed as a DuplicateContribution, and its
partial signature, checked against the wrong nonce, as invalid. It cannot
make the coordinator accept an invalid signature, but it can make the
session fail with an honest signer blamed.
Trust
The coordinator learns nothing secret and cannot forge signatures, so the
signers need not trust it: BIP-327 treats the nonce aggregator as untrusted,
and a coordinator that sends a wrong aggregate nonce can only make the
session fail. Its blame, however, is only as reliable as the coordinator
itself: a signer that wants to check the other signers must receive their
public nonces and partial signatures and run nonceAgg and
partialSigVerify itself.
Signers who receive the aggregate nonce from a coordinator decode it as an
tAggNonce, open the session with newSession on the same key aggregation
context and message, and sign.
The coordinator does not support
deterministicSign, whose signer sends its
public nonce together with its partial signature, after the other signers'
nonces are aggregated.
Example
A coordinator collects the contributions of three signers. This example is
compiled and run by the test suite; it needs the OverloadedStrings
language extension and uses orThrow from the example in
Crypto.Curve.Secp256k1.MuSig2.
import Control.Monad (foldM) import qualified Crypto.Curve.Secp256k1.MuSig2 as MuSig2 import qualified Crypto.Curve.Secp256k1.MuSig2.Coordinator as Coordinator import Data.Bifunctor (first) import qualified Data.ByteString as BS import Data.List.NonEmpty (NonEmpty (..)) import qualified Data.List.NonEmpty as NE
-- | A coordinator collects three signers' nonces and partial signatures as bytes.
coordinatorExample :: IO Bool
coordinatorExample = do
sks <- traverse (orThrow . MuSig2.secKeyFromBytes . BS.replicate 32) (0x66 :| [0x77, 0x88])
-- Signer i is the one with the i-th public key of the context.
keyAggCtx <- orThrow (MuSig2.keyAgg (fmap MuSig2.secKeyPubKey sks))
let msg = "pay Carol 1 BTC"
-- Round one: each signer sends its encoded public nonce, in any order.
nonces <- traverse (\sk -> MuSig2.nonceGen (MuSig2.secKeyPubKey sk) MuSig2.noNonceInputs >>= orThrow) (NE.toList sks)
nonceRound <-
orThrow $
foldM
(\r (i, (_, pubNonce)) -> Coordinator.receivePubNonce i (MuSig2.encode pubNonce) r)
(Coordinator.newNonceRound keyAggCtx)
(zip [0 ..] nonces)
-- Fails if a nonce is missing (here reporting only the first such signer).
collected <- orThrow (first NE.head (Coordinator.finishNonceRound nonceRound))
-- The coordinator sends the aggregate nonce to every signer, who decodes it
-- and opens the session.
aggNonce <- orThrow (MuSig2.decode (MuSig2.encode (Coordinator.collectedAggNonce collected)))
let session = MuSig2.newSession keyAggCtx aggNonce msg
-- Round two: each signer sends its encoded partial signature. The
-- coordinator verifies each one and blames the signer of an invalid one.
psigs <- traverse (\(sk, (secNonce, _)) -> MuSig2.sign secNonce sk session >>= orThrow) (zip (NE.toList sks) nonces)
sigRound <-
orThrow $
foldM
(\r (i, psig) -> Coordinator.receivePartialSig i (MuSig2.encode psig) r)
(Coordinator.newSigRound collected msg)
(zip [0 ..] psigs)
-- Aggregates the partial signatures and verifies the result.
signature <- orThrow (Coordinator.finishSigRound sigRound)
pure (MuSig2.verify (MuSig2.aggXOnlyPubKey keyAggCtx) msg signature)Synopsis
- data Blame = Blame {
- blameSigner :: !Int
- blameReason :: !BlameReason
- data BlameReason
- data NonceRound
- newNonceRound :: KeyAggContext -> NonceRound
- receivePubNonce :: Int -> ByteString -> NonceRound -> Either Blame NonceRound
- nonceRoundMissing :: NonceRound -> [Int]
- finishNonceRound :: NonceRound -> Either (NonEmpty Blame) CollectedNonces
- data CollectedNonces
- collectedKeyAgg :: CollectedNonces -> KeyAggContext
- collectedPubNonces :: CollectedNonces -> NonEmpty PubNonce
- collectedAggNonce :: CollectedNonces -> AggNonce
- data SigRound
- newSigRound :: CollectedNonces -> ByteString -> SigRound
- sigRoundSession :: SigRound -> Session
- receivePartialSig :: Int -> ByteString -> SigRound -> Either Blame SigRound
- sigRoundMissing :: SigRound -> [Int]
- finishSigRound :: SigRound -> Either SigRoundError Signature
- data SigRoundError
Blame
A signer who misbehaved, and how.
Constructors
| Blame | |
Fields
| |
Instances
| NFData Blame Source # | |
Defined in Crypto.Curve.Secp256k1.MuSig2.Coordinator | |
| Exception Blame Source # | |
Defined in Crypto.Curve.Secp256k1.MuSig2.Coordinator Methods toException :: Blame -> SomeException # fromException :: SomeException -> Maybe Blame # displayException :: Blame -> String # backtraceDesired :: Blame -> Bool # | |
| Show Blame Source # | |
| Eq Blame Source # | |
data BlameReason Source #
Why a signer is blamed.
Constructors
| InvalidPubNonceEncoding !DecodeError | The signer's public nonce does not decode (wrong length, invalid or infinite point). |
| InvalidPartialSigEncoding !DecodeError | The signer's partial signature does not decode (wrong length, not below \(n\)). |
| InvalidPartialSignature | The signer's partial signature fails |
| DuplicateContribution | The signer already sent a different, accepted contribution in this round. |
| UnknownSigner | The index is not a signer of the session (negative, or not below the number of keys). |
| MissingContribution | The round was finished without the signer's contribution. |
Instances
| NFData BlameReason Source # | |
Defined in Crypto.Curve.Secp256k1.MuSig2.Coordinator Methods rnf :: BlameReason -> () # | |
| Show BlameReason Source # | |
Defined in Crypto.Curve.Secp256k1.MuSig2.Coordinator Methods showsPrec :: Int -> BlameReason -> ShowS # show :: BlameReason -> String # showList :: [BlameReason] -> ShowS # | |
| Eq BlameReason Source # | |
Defined in Crypto.Curve.Secp256k1.MuSig2.Coordinator | |
Round one: public nonces
data NonceRound Source #
Round one in progress: the public nonces received so far, by signer index.
Instances
| NFData NonceRound Source # | |
Defined in Crypto.Curve.Secp256k1.MuSig2.Coordinator Methods rnf :: NonceRound -> () # | |
newNonceRound :: KeyAggContext -> NonceRound Source #
Starts round one for the signers of a key aggregation context, with no nonce received yet.
Pass the context the session will use, with all tweaks applied:
newSigRound opens the session on it.
Arguments
| :: Int | Signer index, from the authenticated connection the bytes arrived on. |
| -> ByteString | The encoded public nonce. |
| -> NonceRound | |
| -> Either Blame NonceRound |
Receives a signer's encoded public nonce (66 bytes).
Blames the signer with UnknownSigner, DuplicateContribution (a
different nonce was already accepted from it; the same bytes again are
accepted and change nothing) or InvalidPubNonceEncoding. On Left the
round is unchanged.
nonceRoundMissing :: NonceRound -> [Int] Source #
The signers whose public nonce has not been received yet, in increasing order.
finishNonceRound :: NonceRound -> Either (NonEmpty Blame) CollectedNonces Source #
Ends round one: aggregates the public nonces (BIP-327 NonceAgg).
Blames every signer whose nonce is missing with MissingContribution, in
increasing index order.
data CollectedNonces Source #
The result of round one: every signer's public nonce and their aggregate.
Instances
| NFData CollectedNonces Source # | |
Defined in Crypto.Curve.Secp256k1.MuSig2.Coordinator Methods rnf :: CollectedNonces -> () # | |
collectedKeyAgg :: CollectedNonces -> KeyAggContext Source #
The key aggregation context the round was started with.
collectedPubNonces :: CollectedNonces -> NonEmpty PubNonce Source #
Every signer's public nonce, in signer order.
collectedAggNonce :: CollectedNonces -> AggNonce Source #
The aggregate nonce, to send to every signer (encode it).
Round two: partial signatures
Round two in progress: the session, and the verified partial signatures received so far, by signer index.
newSigRound :: CollectedNonces -> ByteString -> SigRound Source #
Starts round two: opens the session (newSession) on the key
aggregation context and aggregate nonce of round one, for the message.
sigRoundSession :: SigRound -> Session Source #
The session the partial signatures are verified in (the coordinator can sign in it too).
Arguments
| :: Int | Signer index, from the authenticated connection the bytes arrived on. |
| -> ByteString | The encoded partial signature. |
| -> SigRound | |
| -> Either Blame SigRound |
Receives a signer's encoded partial signature (32 bytes) and verifies it
with partialSigVerify against the signer's public key and public nonce.
Blames the signer with UnknownSigner, DuplicateContribution (a
different partial signature was already accepted from it; the same bytes
again are accepted and change nothing), InvalidPartialSigEncoding or
InvalidPartialSignature. On Left the round is unchanged.
sigRoundMissing :: SigRound -> [Int] Source #
The signers whose partial signature has not been accepted yet, in increasing order.
finishSigRound :: SigRound -> Either SigRoundError Signature Source #
Ends round two: aggregates the partial signatures (BIP-327
PartialSigAgg) and checks the result with verify against the session's
aggXOnlyPubKey before returning it.
data SigRoundError Source #
Why round two cannot produce a signature.
Constructors
| SigRoundBlame !(NonEmpty Blame) | Partial signatures are missing: blames each of those signers with |
| FinalSignatureInvalid | Every partial signature verified, but their aggregate is not a valid BIP-340 signature. This happens if the final nonce is the point at infinity, for which BIP-327 substitutes the generator: the signers' nonces cancel out, which takes every signer colluding (an honest signer's nonce is unpredictable) or negligible luck. Otherwise the coordinator itself is faulty (a bug or, e.g., a bit flip). No single signer can be blamed. |
Instances
| NFData SigRoundError Source # | |
Defined in Crypto.Curve.Secp256k1.MuSig2.Coordinator Methods rnf :: SigRoundError -> () # | |
| Exception SigRoundError Source # | |
Defined in Crypto.Curve.Secp256k1.MuSig2.Coordinator Methods toException :: SigRoundError -> SomeException # fromException :: SomeException -> Maybe SigRoundError # displayException :: SigRoundError -> String # backtraceDesired :: SigRoundError -> Bool # | |
| Show SigRoundError Source # | |
Defined in Crypto.Curve.Secp256k1.MuSig2.Coordinator Methods showsPrec :: Int -> SigRoundError -> ShowS # show :: SigRoundError -> String # showList :: [SigRoundError] -> ShowS # | |
| Eq SigRoundError Source # | |
Defined in Crypto.Curve.Secp256k1.MuSig2.Coordinator Methods (==) :: SigRoundError -> SigRoundError -> Bool # (/=) :: SigRoundError -> SigRoundError -> Bool # | |