Bitcoin Core 31.1.0
P2P Digital Currency
Loading...
Searching...
No Matches
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
11using namespace util::hex_literals;
13 // Use immediate lambda to work around GCC-14 bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117966
14 []() consteval { return uint256{"868087ca02a6f974c4598924c36b57762d32cb45717167e300622c7167e38965"_hex_u8}; }(),
15};
16
17static bool GetMuSig2KeyAggCache(const std::vector<CPubKey>& pubkeys, secp256k1_musig_keyagg_cache& keyagg_cache)
18{
19 if (pubkeys.empty()) {
20 return false;
21 }
22
23 // Parse the pubkeys
24 std::vector<secp256k1_pubkey> secp_pubkeys;
25 std::vector<const secp256k1_pubkey*> pubkey_ptrs;
26 for (const CPubKey& pubkey : pubkeys) {
27 if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &secp_pubkeys.emplace_back(), pubkey.data(), pubkey.size())) {
28 return false;
29 }
30 }
31 pubkey_ptrs.reserve(secp_pubkeys.size());
32 for (const secp256k1_pubkey& p : secp_pubkeys) {
33 pubkey_ptrs.push_back(&p);
34 }
35
36 // Aggregate the pubkey
37 if (!secp256k1_musig_pubkey_agg(secp256k1_context_static, nullptr, &keyagg_cache, pubkey_ptrs.data(), pubkey_ptrs.size())) {
38 return false;
39 }
40 return true;
41}
42
43static std::optional<CPubKey> GetCPubKeyFromMuSig2KeyAggCache(secp256k1_musig_keyagg_cache& keyagg_cache)
44{
45 // Get the plain aggregated pubkey
46 secp256k1_pubkey agg_pubkey;
47 if (!secp256k1_musig_pubkey_get(secp256k1_context_static, &agg_pubkey, &keyagg_cache)) {
48 return std::nullopt;
49 }
50
51 // Turn into CPubKey
52 unsigned char ser_agg_pubkey[CPubKey::COMPRESSED_SIZE];
53 size_t ser_agg_pubkey_len = CPubKey::COMPRESSED_SIZE;
54 secp256k1_ec_pubkey_serialize(secp256k1_context_static, ser_agg_pubkey, &ser_agg_pubkey_len, &agg_pubkey, SECP256K1_EC_COMPRESSED);
55 return CPubKey(ser_agg_pubkey, ser_agg_pubkey + ser_agg_pubkey_len);
56}
57
58std::optional<CPubKey> MuSig2AggregatePubkeys(const std::vector<CPubKey>& pubkeys, secp256k1_musig_keyagg_cache& keyagg_cache, const std::optional<CPubKey>& expected_aggregate)
59{
60 if (!GetMuSig2KeyAggCache(pubkeys, keyagg_cache)) {
61 return std::nullopt;
62 }
63 std::optional<CPubKey> agg_key = GetCPubKeyFromMuSig2KeyAggCache(keyagg_cache);
64 if (!agg_key.has_value()) return std::nullopt;
65 if (expected_aggregate.has_value() && expected_aggregate != agg_key) return std::nullopt;
66 return agg_key;
67}
68
69std::optional<CPubKey> MuSig2AggregatePubkeys(const std::vector<CPubKey>& pubkeys)
70{
72 return MuSig2AggregatePubkeys(pubkeys, keyagg_cache, std::nullopt);
73}
74
76{
77 CExtPubKey extpub;
78 extpub.nDepth = 0;
79 std::memset(extpub.vchFingerprint, 0, 4);
80 extpub.nChild = 0;
82 extpub.pubkey = pubkey;
83 return extpub;
84}
85
87{
88private:
91
92public:
94
95 // Delete copy constructors
98
99 secp256k1_musig_secnonce* Get() const { return m_nonce.get(); }
100 void Invalidate() { m_nonce.reset(); }
101 bool IsValid() { return m_nonce != nullptr; }
102};
103
105
107MuSig2SecNonce& MuSig2SecNonce::operator=(MuSig2SecNonce&&) noexcept = default;
108
109MuSig2SecNonce::~MuSig2SecNonce() = default;
110
112{
113 return m_impl->Get();
114}
115
117{
118 return m_impl->Invalidate();
119}
120
122{
123 return m_impl->IsValid();
124}
125
126uint256 MuSig2SessionID(const CPubKey& script_pubkey, const CPubKey& part_pubkey, const uint256& sighash)
127{
128 HashWriter hasher;
129 hasher << script_pubkey << part_pubkey << sighash;
130 return hasher.GetSHA256();
131}
132
133std::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)
134{
135 if (!part_pubkeys.size()) return std::nullopt;
136
137 // Get the keyagg cache and aggregate pubkey
138 secp256k1_musig_keyagg_cache keyagg_cache;
139 if (!MuSig2AggregatePubkeys(part_pubkeys, keyagg_cache, aggregate_pubkey)) return std::nullopt;
140
141 // Check if enough pubnonces and partial sigs
142 if (pubnonces.size() != part_pubkeys.size()) return std::nullopt;
143 if (partial_sigs.size() != part_pubkeys.size()) return std::nullopt;
144
145 // Parse the pubnonces and partial sigs
146 std::vector<std::tuple<secp256k1_pubkey, secp256k1_musig_pubnonce, secp256k1_musig_partial_sig>> signers_data;
147 std::vector<const secp256k1_musig_pubnonce*> pubnonce_ptrs;
148 std::vector<const secp256k1_musig_partial_sig*> partial_sig_ptrs;
149 for (const CPubKey& part_pk : part_pubkeys) {
150 const auto& pn_it = pubnonces.find(part_pk);
151 if (pn_it == pubnonces.end()) return std::nullopt;
152 const std::vector<uint8_t> pubnonce = pn_it->second;
153 if (pubnonce.size() != MUSIG2_PUBNONCE_SIZE) return std::nullopt;
154 const auto& it = partial_sigs.find(part_pk);
155 if (it == partial_sigs.end()) return std::nullopt;
156 const uint256& partial_sig = it->second;
157
158 auto& [secp_pk, secp_pn, secp_ps] = signers_data.emplace_back();
159
160 if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &secp_pk, part_pk.data(), part_pk.size())) {
161 return std::nullopt;
162 }
163
164 if (!secp256k1_musig_pubnonce_parse(secp256k1_context_static, &secp_pn, pubnonce.data())) {
165 return std::nullopt;
166 }
167
168 if (!secp256k1_musig_partial_sig_parse(secp256k1_context_static, &secp_ps, partial_sig.data())) {
169 return std::nullopt;
170 }
171 }
172 pubnonce_ptrs.reserve(signers_data.size());
173 partial_sig_ptrs.reserve(signers_data.size());
174 for (auto& [_, pn, ps] : signers_data) {
175 pubnonce_ptrs.push_back(&pn);
176 partial_sig_ptrs.push_back(&ps);
177 }
178
179 // Aggregate nonces
181 if (!secp256k1_musig_nonce_agg(secp256k1_context_static, &aggnonce, pubnonce_ptrs.data(), pubnonce_ptrs.size())) {
182 return std::nullopt;
183 }
184
185 // Apply tweaks
186 for (const auto& [tweak, xonly] : tweaks) {
187 if (xonly) {
188 if (!secp256k1_musig_pubkey_xonly_tweak_add(secp256k1_context_static, nullptr, &keyagg_cache, tweak.data())) {
189 return std::nullopt;
190 }
191 } else if (!secp256k1_musig_pubkey_ec_tweak_add(secp256k1_context_static, nullptr, &keyagg_cache, tweak.data())) {
192 return std::nullopt;
193 }
194 }
195
196 // Create musig_session
198 if (!secp256k1_musig_nonce_process(secp256k1_context_static, &session, &aggnonce, sighash.data(), &keyagg_cache)) {
199 return std::nullopt;
200 }
201
202 // Verify partial sigs
203 for (const auto& [pk, pb, ps] : signers_data) {
204 if (!secp256k1_musig_partial_sig_verify(secp256k1_context_static, &ps, &pb, &pk, &keyagg_cache, &session)) {
205 return std::nullopt;
206 }
207 }
208
209 // Aggregate partial sigs
210 std::vector<uint8_t> sig;
211 sig.resize(64);
212 if (!secp256k1_musig_partial_sig_agg(secp256k1_context_static, sig.data(), &session, partial_sig_ptrs.data(), partial_sig_ptrs.size())) {
213 return std::nullopt;
214 }
215
216 return sig;
217}
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:40
void Invalidate()
Definition musig.cpp:116
bool IsValid()
Definition musig.cpp:121
secp256k1_musig_secnonce * Get() const
Definition musig.cpp:111
std::unique_ptr< MuSig2SecNonceImpl > m_impl
Definition musig.h:42
secp256k1_musig_secnonce * Get() const
Definition musig.cpp:99
MuSig2SecNonceImpl & operator=(const MuSig2SecNonceImpl &)=delete
secure_unique_ptr< secp256k1_musig_secnonce > m_nonce
The actual secnonce itself.
Definition musig.cpp:90
MuSig2SecNonceImpl(const MuSig2SecNonceImpl &)=delete
constexpr const unsigned char * data() const
Definition uint256.h:97
256-bit opaque blob.
Definition uint256.h:195
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:75
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:133
uint256 MuSig2SessionID(const CPubKey &script_pubkey, const CPubKey &part_pubkey, const uint256 &sighash)
Definition musig.cpp:126
static std::optional< CPubKey > GetCPubKeyFromMuSig2KeyAggCache(secp256k1_musig_keyagg_cache &keyagg_cache)
Definition musig.cpp:43
constexpr uint256 MUSIG_CHAINCODE
Definition musig.cpp:12
static bool GetMuSig2KeyAggCache(const std::vector< CPubKey > &pubkeys, secp256k1_musig_keyagg_cache &keyagg_cache)
Definition musig.cpp:17
std::optional< CPubKey > MuSig2AggregatePubkeys(const std::vector< CPubKey > &pubkeys, secp256k1_musig_keyagg_cache &keyagg_cache, const std::optional< CPubKey > &expected_aggregate)
Definition musig.cpp:58
constexpr size_t MUSIG2_PUBNONCE_SIZE
Definition musig.h:17
Definition common.h:29
""_hex is a compile-time user-defined literal returning a std::array<std::byte>, equivalent to ParseH...
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 ...
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...
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.
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.
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.
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.
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.
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.
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.
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...
secure_unique_ptr< T > make_secure_unique(Args &&... as)
Definition secure.h:66
std::unique_ptr< T, SecureUniqueDeleter< T > > secure_unique_ptr
Definition secure.h:63
ChainCode chaincode
Definition pubkey.h:341
unsigned char vchFingerprint[4]
Definition pubkey.h:339
unsigned char nDepth
Definition pubkey.h:338
CPubKey pubkey
Definition pubkey.h:342
unsigned int nChild
Definition pubkey.h:340
Opaque data structure that holds an aggregate public nonce.
unsigned char data[132]
This module implements BIP 327 "MuSig2 for BIP340-compatibleMulti-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