Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#ifndef SECP256K1_MUSIG_H2#define SECP256K1_MUSIG_H4#include "secp256k1_extrakeys.h"6#ifdef __cplusplus7extern "C" {8#endif10#include <stddef.h>11#include <stdint.h>13/** This module implements BIP 327 "MuSig2 for BIP340-compatible14* Multi-Signatures"15* (https://github.com/bitcoin/bips/blob/master/bip-0327.mediawiki)16* v1.0.0. You can find an example demonstrating the musig module in17* examples/musig.c.18*19* The module also supports BIP 341 ("Taproot") public key tweaking.20*21* It is recommended to read the documentation in this include file carefully.22* Further notes on API usage can be found in doc/musig.md23*24* Since the first version of MuSig is essentially replaced by MuSig2, we use25* MuSig, musig and MuSig2 synonymously unless noted otherwise.26*/28/** Opaque data structures29*30* The exact representation of data inside the opaque data structures is31* implementation defined and not guaranteed to be portable between different32* platforms or versions. With the exception of `secp256k1_musig_secnonce`, the33* data structures can be safely copied/moved. If you need to convert to a34* format suitable for storage, transmission, or comparison, use the35* corresponding serialization and parsing functions.36*/38/** Opaque data structure that caches information about public key aggregation.39*40* Guaranteed to be 197 bytes in size. No serialization and parsing functions41* (yet).42*/43typedef struct secp256k1_musig_keyagg_cache {44unsigned char data[197];45} secp256k1_musig_keyagg_cache;47/** Opaque data structure that holds a signer's _secret_ nonce.48*49* Guaranteed to be 132 bytes in size.50*51* WARNING: This structure MUST NOT be copied or read or written to directly. A52* signer who is online throughout the whole process and can keep this53* structure in memory can use the provided API functions for a safe standard54* workflow.55*56* Copying this data structure can result in nonce reuse which will leak the57* secret signing key.58*/59typedef struct secp256k1_musig_secnonce {60unsigned char data[132];61} secp256k1_musig_secnonce;63/** Opaque data structure that holds a signer's public nonce.64*65* Guaranteed to be 132 bytes in size. Serialized and parsed with66* `musig_pubnonce_serialize` and `musig_pubnonce_parse`.67*/68typedef struct secp256k1_musig_pubnonce {69unsigned char data[132];70} secp256k1_musig_pubnonce;72/** Opaque data structure that holds an aggregate public nonce.73*74* Guaranteed to be 132 bytes in size. Serialized and parsed with75* `musig_aggnonce_serialize` and `musig_aggnonce_parse`.76*/77typedef struct secp256k1_musig_aggnonce {78unsigned char data[132];79} secp256k1_musig_aggnonce;81/** Opaque data structure that holds a MuSig session.82*83* This structure is not required to be kept secret for the signing protocol to84* be secure. Guaranteed to be 133 bytes in size. No serialization and parsing85* functions (yet).86*/87typedef struct secp256k1_musig_session {88unsigned char data[133];89} secp256k1_musig_session;91/** Opaque data structure that holds a partial MuSig signature.92*93* Guaranteed to be 36 bytes in size. Serialized and parsed with94* `musig_partial_sig_serialize` and `musig_partial_sig_parse`.95*/96typedef struct secp256k1_musig_partial_sig {97unsigned char data[36];98} secp256k1_musig_partial_sig;100/** Parse a signer's public nonce.101*102* Returns: 1 when the nonce could be parsed, 0 otherwise.103* Args: ctx: pointer to a context object104* Out: nonce: pointer to a nonce object105* In: in66: pointer to the 66-byte nonce to be parsed106*/107SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubnonce_parse(108const secp256k1_context *ctx,109secp256k1_musig_pubnonce *nonce,110const unsigned char *in66111) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);113/** Serialize a signer's public nonce114*115* Returns: 1 always116* Args: ctx: pointer to a context object117* Out: out66: pointer to a 66-byte array to store the serialized nonce118* In: nonce: pointer to the nonce119*/120SECP256K1_API int secp256k1_musig_pubnonce_serialize(121const secp256k1_context *ctx,122unsigned char *out66,123const secp256k1_musig_pubnonce *nonce124) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);126/** Parse an aggregate public nonce.127*128* Returns: 1 when the nonce could be parsed, 0 otherwise.129* Args: ctx: pointer to a context object130* Out: nonce: pointer to a nonce object131* In: in66: pointer to the 66-byte nonce to be parsed132*/133SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_aggnonce_parse(134const secp256k1_context *ctx,135secp256k1_musig_aggnonce *nonce,136const unsigned char *in66137) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);139/** Serialize an aggregate public nonce140*141* Returns: 1 always142* Args: ctx: pointer to a context object143* Out: out66: pointer to a 66-byte array to store the serialized nonce144* In: nonce: pointer to the nonce145*/146SECP256K1_API int secp256k1_musig_aggnonce_serialize(147const secp256k1_context *ctx,148unsigned char *out66,149const secp256k1_musig_aggnonce *nonce150) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);152/** Parse a MuSig partial signature.153*154* Returns: 1 when the signature could be parsed, 0 otherwise.155* Args: ctx: pointer to a context object156* Out: sig: pointer to a signature object157* In: in32: pointer to the 32-byte signature to be parsed158*/159SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_partial_sig_parse(160const secp256k1_context *ctx,161secp256k1_musig_partial_sig *sig,162const unsigned char *in32163) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);165/** Serialize a MuSig partial signature166*167* Returns: 1 always168* Args: ctx: pointer to a context object169* Out: out32: pointer to a 32-byte array to store the serialized signature170* In: sig: pointer to the signature171*/172SECP256K1_API int secp256k1_musig_partial_sig_serialize(173const secp256k1_context *ctx,174unsigned char *out32,175const secp256k1_musig_partial_sig *sig176) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);178/** Computes an aggregate public key and uses it to initialize a keyagg_cache179*180* Different orders of `pubkeys` result in different `agg_pk`s.181*182* Before aggregating, the pubkeys can be sorted with `secp256k1_ec_pubkey_sort`183* which ensures the same `agg_pk` result for the same multiset of pubkeys.184* This is useful to do before `pubkey_agg`, such that the order of pubkeys185* does not affect the aggregate public key.186*187* Returns: 0 if the arguments are invalid, 1 otherwise188* Args: ctx: pointer to a context object189* Out: agg_pk: the MuSig-aggregated x-only public key. If you do not need it,190* this arg can be NULL.191* keyagg_cache: if non-NULL, pointer to a musig_keyagg_cache struct that192* is required for signing (or observing the signing session193* and verifying partial signatures).194* In: pubkeys: input array of pointers to public keys to aggregate. The order195* is important; a different order will result in a different196* aggregate public key.197* n_pubkeys: length of pubkeys array. Must be greater than 0.198*/199SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubkey_agg(200const secp256k1_context *ctx,201secp256k1_xonly_pubkey *agg_pk,202secp256k1_musig_keyagg_cache *keyagg_cache,203const secp256k1_pubkey * const *pubkeys,204size_t n_pubkeys205) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(4);207/** Obtain the aggregate public key from a keyagg_cache.208*209* This is only useful if you need the non-xonly public key, in particular for210* plain (non-xonly) tweaking or batch-verifying multiple key aggregations211* (not implemented).212*213* Returns: 0 if the arguments are invalid, 1 otherwise214* Args: ctx: pointer to a context object215* Out: agg_pk: the MuSig-aggregated public key.216* In: keyagg_cache: pointer to a `musig_keyagg_cache` struct initialized by217* `musig_pubkey_agg`218*/219SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubkey_get(220const secp256k1_context *ctx,221secp256k1_pubkey *agg_pk,222const secp256k1_musig_keyagg_cache *keyagg_cache223) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);225/** Apply plain "EC" tweaking to a public key in a given keyagg_cache by adding226* the generator multiplied with `tweak32` to it. This is useful for deriving227* child keys from an aggregate public key via BIP 32 where `tweak32` is set to228* a hash as defined in BIP 32.229*230* Callers are responsible for deriving `tweak32` in a way that does not reduce231* the security of MuSig (for example, by following BIP 32).232*233* The tweaking method is the same as `secp256k1_ec_pubkey_tweak_add`. So after234* the following pseudocode buf and buf2 have identical contents (absent235* earlier failures).236*237* secp256k1_musig_pubkey_agg(..., keyagg_cache, pubkeys, ...)238* secp256k1_musig_pubkey_get(..., agg_pk, keyagg_cache)239* secp256k1_musig_pubkey_ec_tweak_add(..., output_pk, tweak32, keyagg_cache)240* secp256k1_ec_pubkey_serialize(..., buf, ..., output_pk, ...)241* secp256k1_ec_pubkey_tweak_add(..., agg_pk, tweak32)242* secp256k1_ec_pubkey_serialize(..., buf2, ..., agg_pk, ...)243*244* This function is required if you want to _sign_ for a tweaked aggregate key.245* If you are only computing a public key but not intending to create a246* signature for it, use `secp256k1_ec_pubkey_tweak_add` instead.247*248* Returns: 0 if the arguments are invalid, 1 otherwise249* Args: ctx: pointer to a context object250* Out: output_pubkey: pointer to a public key to store the result. Will be set
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.