Bitcoin Core 30.99.0
P2P Digital Currency
musig.cpp
Go to the documentation of this file.
1// Copyright (c) 2024-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <musig.h>
7
8#include <secp256k1_musig.h>
9
10static bool GetMuSig2KeyAggCache(const std::vector<CPubKey>& pubkeys, secp256k1_musig_keyagg_cache& keyagg_cache)
11{
12 // Parse the pubkeys
13 std::vector<secp256k1_pubkey> secp_pubkeys;
14 std::vector<const secp256k1_pubkey*> pubkey_ptrs;
15 for (const CPubKey& pubkey : pubkeys) {
16 if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &secp_pubkeys.emplace_back(), pubkey.data(), pubkey.size())) {
17 return false;
18 }
19 }
20 pubkey_ptrs.reserve(secp_pubkeys.size());
21 for (const secp256k1_pubkey& p : secp_pubkeys) {
22 pubkey_ptrs.push_back(&p);
23 }
24
25 // Aggregate the pubkey
26 if (!secp256k1_musig_pubkey_agg(secp256k1_context_static, nullptr, &keyagg_cache, pubkey_ptrs.data(), pubkey_ptrs.size())) {
27 return false;
28 }
29 return true;
30}
31
32static std::optional<CPubKey> GetCPubKeyFromMuSig2KeyAggCache(secp256k1_musig_keyagg_cache& keyagg_cache)
33{
34 // Get the plain aggregated pubkey
35 secp256k1_pubkey agg_pubkey;
36 if (!secp256k1_musig_pubkey_get(secp256k1_context_static, &agg_pubkey, &keyagg_cache)) {
37 return std::nullopt;
38 }
39
40 // Turn into CPubKey
41 unsigned char ser_agg_pubkey[CPubKey::COMPRESSED_SIZE];
42 size_t ser_agg_pubkey_len = CPubKey::COMPRESSED_SIZE;
43 secp256k1_ec_pubkey_serialize(secp256k1_context_static, ser_agg_pubkey, &ser_agg_pubkey_len, &agg_pubkey, SECP256K1_EC_COMPRESSED);
44 return CPubKey(ser_agg_pubkey, ser_agg_pubkey + ser_agg_pubkey_len);
45}
46
47std::optional<CPubKey> MuSig2AggregatePubkeys(const std::vector<CPubKey>& pubkeys, secp256k1_musig_keyagg_cache& keyagg_cache, const std::optional<CPubKey>& expected_aggregate)
48{
49 if (!GetMuSig2KeyAggCache(pubkeys, keyagg_cache)) {
50 return std::nullopt;
51 }
52 std::optional<CPubKey> agg_key = GetCPubKeyFromMuSig2KeyAggCache(keyagg_cache);
53 if (!agg_key.has_value()) return std::nullopt;
54 if (expected_aggregate.has_value() && expected_aggregate != agg_key) return std::nullopt;
55 return agg_key;
56}
57
58std::optional<CPubKey> MuSig2AggregatePubkeys(const std::vector<CPubKey>& pubkeys)
59{
61 return MuSig2AggregatePubkeys(pubkeys, keyagg_cache, std::nullopt);
62}
63
65{
66 CExtPubKey extpub;
67 extpub.nDepth = 0;
68 std::memset(extpub.vchFingerprint, 0, 4);
69 extpub.nChild = 0;
71 extpub.pubkey = pubkey;
72 return extpub;
73}
74
76{
77private:
80
81public:
83
84 // Delete copy constructors
87
88 secp256k1_musig_secnonce* Get() const { return m_nonce.get(); }
89 void Invalidate() { m_nonce.reset(); }
90 bool IsValid() { return m_nonce != nullptr; }
91};
92
94
96MuSig2SecNonce& MuSig2SecNonce::operator=(MuSig2SecNonce&&) noexcept = default;
97
98MuSig2SecNonce::~MuSig2SecNonce() = default;
99
101{
102 return m_impl->Get();
103}
104
106{
107 return m_impl->Invalidate();
108}
109
111{
112 return m_impl->IsValid();
113}
114
115uint256 MuSig2SessionID(const CPubKey& script_pubkey, const CPubKey& part_pubkey, const uint256& sighash)
116{
117 HashWriter hasher;
118 hasher << script_pubkey << part_pubkey << sighash;
119 return hasher.GetSHA256();
120}
121
122std::optional<std::vector<uint8_t>> CreateMuSig2AggregateSig(const std::vector<CPubKey>& part_pubkeys, const CPubKey& aggregate_pubkey, const std::vector<std::pair<uint256, bool>>& tweaks, const uint256& sighash, const std::map<CPubKey, std::vector<uint8_t>>& pubnonces, const std::map<CPubKey, uint256>& partial_sigs)
123{
124 if (!part_pubkeys.size()) return std::nullopt;
125
126 // Get the keyagg cache and aggregate pubkey
127 secp256k1_musig_keyagg_cache keyagg_cache;
128 if (!MuSig2AggregatePubkeys(part_pubkeys, keyagg_cache, aggregate_pubkey)) return std::nullopt;
129
130 // Check if enough pubnonces and partial sigs
131 if (pubnonces.size() != part_pubkeys.size()) return std::nullopt;
132 if (partial_sigs.size() != part_pubkeys.size()) return std::nullopt;
133
134 // Parse the pubnonces and partial sigs
135 std::vector<std::tuple<secp256k1_pubkey, secp256k1_musig_pubnonce, secp256k1_musig_partial_sig>> signers_data;
136 std::vector<const secp256k1_musig_pubnonce*> pubnonce_ptrs;
137 std::vector<const secp256k1_musig_partial_sig*> partial_sig_ptrs;
138 for (const CPubKey& part_pk : part_pubkeys) {
139 const auto& pn_it = pubnonces.find(part_pk);
140 if (pn_it == pubnonces.end()) return std::nullopt;
141 const std::vector<uint8_t> pubnonce = pn_it->second;
142 if (pubnonce.size() != MUSIG2_PUBNONCE_SIZE) return std::nullopt;
143 const auto& it = partial_sigs.find(part_pk);
144 if (it == partial_sigs.end()) return std::nullopt;
145 const uint256& partial_sig = it->second;
146
147 auto& [secp_pk, secp_pn, secp_ps] = signers_data.emplace_back();
148
149 if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &secp_pk, part_pk.data(), part_pk.size())) {
150 return std::nullopt;
151 }
152
153 if (!secp256k1_musig_pubnonce_parse(secp256k1_context_static, &secp_pn, pubnonce.data())) {
154 return std::nullopt;
155 }
156
157 if (!secp256k1_musig_partial_sig_parse(secp256k1_context_static, &secp_ps, partial_sig.data())) {
158 return std::nullopt;
159 }
160 }
161 pubnonce_ptrs.reserve(signers_data.size());
162 partial_sig_ptrs.reserve(signers_data.size());
163 for (auto& [_, pn, ps] : signers_data) {
164 pubnonce_ptrs.push_back(&pn);
165 partial_sig_ptrs.push_back(&ps);
166 }
167
168 // Aggregate nonces
170 if (!secp256k1_musig_nonce_agg(secp256k1_context_static, &aggnonce, pubnonce_ptrs.data(), pubnonce_ptrs.size())) {
171 return std::nullopt;
172 }
173
174 // Apply tweaks
175 for (const auto& [tweak, xonly] : tweaks) {
176 if (xonly) {
177 if (!secp256k1_musig_pubkey_xonly_tweak_add(secp256k1_context_static, nullptr, &keyagg_cache, tweak.data())) {
178 return std::nullopt;
179 }
180 } else if (!secp256k1_musig_pubkey_ec_tweak_add(secp256k1_context_static, nullptr, &keyagg_cache, tweak.data())) {
181 return std::nullopt;
182 }
183 }
184
185 // Create musig_session
187 if (!secp256k1_musig_nonce_process(secp256k1_context_static, &session, &aggnonce, sighash.data(), &keyagg_cache)) {
188 return std::nullopt;
189 }
190
191 // Verify partial sigs
192 for (const auto& [pk, pb, ps] : signers_data) {
193 if (!secp256k1_musig_partial_sig_verify(secp256k1_context_static, &ps, &pb, &pk, &keyagg_cache, &session)) {
194 return std::nullopt;
195 }
196 }
197
198 // Aggregate partial sigs
199 std::vector<uint8_t> sig;
200 sig.resize(64);
201 if (!secp256k1_musig_partial_sig_agg(secp256k1_context_static, sig.data(), &session, partial_sig_ptrs.data(), partial_sig_ptrs.size())) {
202 return std::nullopt;
203 }
204
205 return sig;
206}
An encapsulated public key.
Definition: pubkey.h:34
static constexpr unsigned int COMPRESSED_SIZE
Definition: pubkey.h:40
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:101
uint256 GetSHA256()
Compute the SHA256 hash of all data written to this object.
Definition: hash.h:126
MuSig2SecNonce encapsulates a secret nonce in use in a MuSig2 signing session.
Definition: musig.h:49
void Invalidate()
Definition: musig.cpp:105
bool IsValid()
Definition: musig.cpp:110
MuSig2SecNonce()
Definition: musig.cpp:93
secp256k1_musig_secnonce * Get() const
Definition: musig.cpp:100
std::unique_ptr< MuSig2SecNonceImpl > m_impl
Definition: musig.h:51
secp256k1_musig_secnonce * Get() const
Definition: musig.cpp:88
MuSig2SecNonceImpl & operator=(const MuSig2SecNonceImpl &)=delete
secure_unique_ptr< secp256k1_musig_secnonce > m_nonce
The actual secnonce itself.
Definition: musig.cpp:79
bool IsValid()
Definition: musig.cpp:90
void Invalidate()
Definition: musig.cpp:89
MuSig2SecNonceImpl(const MuSig2SecNonceImpl &)=delete
constexpr const unsigned char * data() const
Definition: uint256.h:98
256-bit opaque blob.
Definition: uint256.h:196
static int tweak(const secp256k1_context *ctx, secp256k1_xonly_pubkey *agg_pk, secp256k1_musig_keyagg_cache *cache)
Definition: musig.c:64
CExtPubKey CreateMuSig2SyntheticXpub(const CPubKey &pubkey)
Construct the BIP 328 synthetic xpub for a pubkey.
Definition: musig.cpp:64
std::optional< std::vector< uint8_t > > CreateMuSig2AggregateSig(const std::vector< CPubKey > &part_pubkeys, const CPubKey &aggregate_pubkey, const std::vector< std::pair< uint256, bool > > &tweaks, const uint256 &sighash, const std::map< CPubKey, std::vector< uint8_t > > &pubnonces, const std::map< CPubKey, uint256 > &partial_sigs)
Definition: musig.cpp:122
uint256 MuSig2SessionID(const CPubKey &script_pubkey, const CPubKey &part_pubkey, const uint256 &sighash)
Definition: musig.cpp:115
static std::optional< CPubKey > GetCPubKeyFromMuSig2KeyAggCache(secp256k1_musig_keyagg_cache &keyagg_cache)
Definition: musig.cpp:32
static bool GetMuSig2KeyAggCache(const std::vector< CPubKey > &pubkeys, secp256k1_musig_keyagg_cache &keyagg_cache)
Definition: musig.cpp:10
std::optional< CPubKey > MuSig2AggregatePubkeys(const std::vector< CPubKey > &pubkeys, secp256k1_musig_keyagg_cache &keyagg_cache, const std::optional< CPubKey > &expected_aggregate)
Compute the full aggregate pubkey from the given participant pubkeys in their current order.
Definition: musig.cpp:47
constexpr size_t MUSIG2_PUBNONCE_SIZE
Definition: musig.h:26
constexpr uint256 MUSIG_CHAINCODE
Definition: musig.h:19
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize a pubkey object into a serialized byte sequence.
Definition: secp256k1.c:268
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *input, size_t inputlen) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a variable-length public key into the pubkey object.
Definition: secp256k1.c:250
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:224
SECP256K1_API const secp256k1_context *const secp256k1_context_static
A built-in constant secp256k1 context object with static storage duration, to be used in conjunction ...
Definition: secp256k1.h:245
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubkey_ec_tweak_add(const secp256k1_context *ctx, secp256k1_pubkey *output_pubkey, secp256k1_musig_keyagg_cache *keyagg_cache, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Apply plain "EC" tweaking to a public key in a given keyagg_cache by adding the generator multiplied ...
Definition: keyagg_impl.h:283
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubkey_xonly_tweak_add(const secp256k1_context *ctx, secp256k1_pubkey *output_pubkey, secp256k1_musig_keyagg_cache *keyagg_cache, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Apply x-only tweaking to a public key in a given keyagg_cache by adding the generator multiplied with...
Definition: keyagg_impl.h:287
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_partial_sig_parse(const secp256k1_context *ctx, secp256k1_musig_partial_sig *sig, const unsigned char *in32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a MuSig partial signature.
Definition: session_impl.h:275
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubnonce_parse(const secp256k1_context *ctx, secp256k1_musig_pubnonce *nonce, const unsigned char *in66) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a signer's public nonce.
Definition: session_impl.h:194
SECP256K1_API int secp256k1_musig_nonce_agg(const secp256k1_context *ctx, secp256k1_musig_aggnonce *aggnonce, const secp256k1_musig_pubnonce *const *pubnonces, size_t n_pubnonces) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Aggregates the nonces of all signers into a single nonce.
Definition: session_impl.h:544
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubkey_get(const secp256k1_context *ctx, secp256k1_pubkey *agg_pk, const secp256k1_musig_keyagg_cache *keyagg_cache) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Obtain the aggregate public key from a keyagg_cache.
Definition: keyagg_impl.h:233
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_partial_sig_verify(const secp256k1_context *ctx, const secp256k1_musig_partial_sig *partial_sig, const secp256k1_musig_pubnonce *pubnonce, const secp256k1_pubkey *pubkey, const secp256k1_musig_keyagg_cache *keyagg_cache, const secp256k1_musig_session *session) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(6)
Verifies an individual signer's partial signature.
Definition: session_impl.h:736
SECP256K1_API int secp256k1_musig_partial_sig_agg(const secp256k1_context *ctx, unsigned char *sig64, const secp256k1_musig_session *session, const secp256k1_musig_partial_sig *const *partial_sigs, size_t n_sigs) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Aggregates partial signatures.
Definition: session_impl.h:799
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubkey_agg(const secp256k1_context *ctx, secp256k1_xonly_pubkey *agg_pk, secp256k1_musig_keyagg_cache *keyagg_cache, const secp256k1_pubkey *const *pubkeys, size_t n_pubkeys) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(4)
Computes an aggregate public key and uses it to initialize a keyagg_cache.
Definition: keyagg_impl.h:175
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_nonce_process(const secp256k1_context *ctx, secp256k1_musig_session *session, const secp256k1_musig_aggnonce *aggnonce, const unsigned char *msg32, const secp256k1_musig_keyagg_cache *keyagg_cache) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5)
Takes the aggregate nonce and creates a session that is required for signing and verification of part...
Definition: session_impl.h:620
secure_unique_ptr< T > make_secure_unique(Args &&... as)
Definition: secure.h:71
std::unique_ptr< T, SecureUniqueDeleter< T > > secure_unique_ptr
Definition: secure.h:68
ChainCode chaincode
Definition: pubkey.h:351
unsigned char vchFingerprint[4]
Definition: pubkey.h:349
unsigned char nDepth
Definition: pubkey.h:348
CPubKey pubkey
Definition: pubkey.h:352
unsigned int nChild
Definition: pubkey.h:350
Opaque data structure that holds an aggregate public nonce.
unsigned char data[132]
This module implements BIP 327 "MuSig2 for BIP340-compatible Multi-Signatures" (https://github....
Opaque data structure that holds a signer's secret nonce.
Opaque data structure that holds a MuSig session.
unsigned char data[133]
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:61
consteval auto _(util::TranslatedLiteral str)
Definition: translation.h:79