Bitcoin Core Fuzz Coverage Report

Coverage Report

Created: 2026-03-24 13:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/musig.cpp
Line
Count
Source
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>
6
#include <support/allocators/secure.h>
7
8
#include <secp256k1_musig.h>
9
10
//! MuSig2 chaincode as defined by BIP 328
11
using namespace util::hex_literals;
12
constexpr uint256 MUSIG_CHAINCODE{
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
17
static bool GetMuSig2KeyAggCache(const std::vector<CPubKey>& pubkeys, secp256k1_musig_keyagg_cache& keyagg_cache)
18
0
{
19
    // Parse the pubkeys
20
0
    std::vector<secp256k1_pubkey> secp_pubkeys;
21
0
    std::vector<const secp256k1_pubkey*> pubkey_ptrs;
22
0
    for (const CPubKey& pubkey : pubkeys) {
23
0
        if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &secp_pubkeys.emplace_back(), pubkey.data(), pubkey.size())) {
24
0
            return false;
25
0
        }
26
0
    }
27
0
    pubkey_ptrs.reserve(secp_pubkeys.size());
28
0
    for (const secp256k1_pubkey& p : secp_pubkeys) {
29
0
        pubkey_ptrs.push_back(&p);
30
0
    }
31
32
    // Aggregate the pubkey
33
0
    if (!secp256k1_musig_pubkey_agg(secp256k1_context_static, nullptr, &keyagg_cache, pubkey_ptrs.data(), pubkey_ptrs.size())) {
34
0
        return false;
35
0
    }
36
0
    return true;
37
0
}
38
39
static std::optional<CPubKey> GetCPubKeyFromMuSig2KeyAggCache(secp256k1_musig_keyagg_cache& keyagg_cache)
40
0
{
41
    // Get the plain aggregated pubkey
42
0
    secp256k1_pubkey agg_pubkey;
43
0
    if (!secp256k1_musig_pubkey_get(secp256k1_context_static, &agg_pubkey, &keyagg_cache)) {
44
0
        return std::nullopt;
45
0
    }
46
47
    // Turn into CPubKey
48
0
    unsigned char ser_agg_pubkey[CPubKey::COMPRESSED_SIZE];
49
0
    size_t ser_agg_pubkey_len = CPubKey::COMPRESSED_SIZE;
50
0
    secp256k1_ec_pubkey_serialize(secp256k1_context_static, ser_agg_pubkey, &ser_agg_pubkey_len, &agg_pubkey, SECP256K1_EC_COMPRESSED);
Line
Count
Source
224
0
#define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
Line
Count
Source
205
0
#define SECP256K1_FLAGS_TYPE_COMPRESSION (1 << 1)
#define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
Line
Count
Source
210
0
#define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8)
51
0
    return CPubKey(ser_agg_pubkey, ser_agg_pubkey + ser_agg_pubkey_len);
52
0
}
53
54
std::optional<CPubKey> MuSig2AggregatePubkeys(const std::vector<CPubKey>& pubkeys, secp256k1_musig_keyagg_cache& keyagg_cache, const std::optional<CPubKey>& expected_aggregate)
55
0
{
56
0
    if (!GetMuSig2KeyAggCache(pubkeys, keyagg_cache)) {
57
0
        return std::nullopt;
58
0
    }
59
0
    std::optional<CPubKey> agg_key = GetCPubKeyFromMuSig2KeyAggCache(keyagg_cache);
60
0
    if (!agg_key.has_value()) return std::nullopt;
61
0
    if (expected_aggregate.has_value() && expected_aggregate != agg_key) return std::nullopt;
62
0
    return agg_key;
63
0
}
64
65
std::optional<CPubKey> MuSig2AggregatePubkeys(const std::vector<CPubKey>& pubkeys)
66
0
{
67
0
    secp256k1_musig_keyagg_cache keyagg_cache;
68
0
    return MuSig2AggregatePubkeys(pubkeys, keyagg_cache, std::nullopt);
69
0
}
70
71
CExtPubKey CreateMuSig2SyntheticXpub(const CPubKey& pubkey)
72
0
{
73
0
    CExtPubKey extpub;
74
0
    extpub.nDepth = 0;
75
0
    std::memset(extpub.vchFingerprint, 0, 4);
76
0
    extpub.nChild = 0;
77
0
    extpub.chaincode = MUSIG_CHAINCODE;
78
0
    extpub.pubkey = pubkey;
79
0
    return extpub;
80
0
}
81
82
class MuSig2SecNonceImpl
83
{
84
private:
85
    //! The actual secnonce itself
86
    secure_unique_ptr<secp256k1_musig_secnonce> m_nonce;
87
88
public:
89
0
    MuSig2SecNonceImpl() : m_nonce{make_secure_unique<secp256k1_musig_secnonce>()} {}
90
91
    // Delete copy constructors
92
    MuSig2SecNonceImpl(const MuSig2SecNonceImpl&) = delete;
93
    MuSig2SecNonceImpl& operator=(const MuSig2SecNonceImpl&) = delete;
94
95
0
    secp256k1_musig_secnonce* Get() const { return m_nonce.get(); }
96
0
    void Invalidate() { m_nonce.reset(); }
97
0
    bool IsValid() { return m_nonce != nullptr; }
98
};
99
100
0
MuSig2SecNonce::MuSig2SecNonce() : m_impl{std::make_unique<MuSig2SecNonceImpl>()} {}
101
102
0
MuSig2SecNonce::MuSig2SecNonce(MuSig2SecNonce&&) noexcept = default;
103
0
MuSig2SecNonce& MuSig2SecNonce::operator=(MuSig2SecNonce&&) noexcept = default;
104
105
0
MuSig2SecNonce::~MuSig2SecNonce() = default;
106
107
secp256k1_musig_secnonce* MuSig2SecNonce::Get() const
108
0
{
109
0
    return m_impl->Get();
110
0
}
111
112
void MuSig2SecNonce::Invalidate()
113
0
{
114
0
    return m_impl->Invalidate();
115
0
}
116
117
bool MuSig2SecNonce::IsValid()
118
0
{
119
0
    return m_impl->IsValid();
120
0
}
121
122
uint256 MuSig2SessionID(const CPubKey& script_pubkey, const CPubKey& part_pubkey, const uint256& sighash)
123
0
{
124
0
    HashWriter hasher;
125
0
    hasher << script_pubkey << part_pubkey << sighash;
126
0
    return hasher.GetSHA256();
127
0
}
128
129
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)
130
0
{
131
0
    if (!part_pubkeys.size()) return std::nullopt;
132
133
    // Get the keyagg cache and aggregate pubkey
134
0
    secp256k1_musig_keyagg_cache keyagg_cache;
135
0
    if (!MuSig2AggregatePubkeys(part_pubkeys, keyagg_cache, aggregate_pubkey)) return std::nullopt;
136
137
    // Check if enough pubnonces and partial sigs
138
0
    if (pubnonces.size() != part_pubkeys.size()) return std::nullopt;
139
0
    if (partial_sigs.size() != part_pubkeys.size()) return std::nullopt;
140
141
    // Parse the pubnonces and partial sigs
142
0
    std::vector<std::tuple<secp256k1_pubkey, secp256k1_musig_pubnonce, secp256k1_musig_partial_sig>> signers_data;
143
0
    std::vector<const secp256k1_musig_pubnonce*> pubnonce_ptrs;
144
0
    std::vector<const secp256k1_musig_partial_sig*> partial_sig_ptrs;
145
0
    for (const CPubKey& part_pk : part_pubkeys) {
146
0
        const auto& pn_it = pubnonces.find(part_pk);
147
0
        if (pn_it == pubnonces.end()) return std::nullopt;
148
0
        const std::vector<uint8_t> pubnonce = pn_it->second;
149
0
        if (pubnonce.size() != MUSIG2_PUBNONCE_SIZE) return std::nullopt;
150
0
        const auto& it = partial_sigs.find(part_pk);
151
0
        if (it == partial_sigs.end()) return std::nullopt;
152
0
        const uint256& partial_sig = it->second;
153
154
0
        auto& [secp_pk, secp_pn, secp_ps] = signers_data.emplace_back();
155
156
0
        if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &secp_pk, part_pk.data(), part_pk.size())) {
157
0
            return std::nullopt;
158
0
        }
159
160
0
        if (!secp256k1_musig_pubnonce_parse(secp256k1_context_static, &secp_pn, pubnonce.data())) {
161
0
            return std::nullopt;
162
0
        }
163
164
0
        if (!secp256k1_musig_partial_sig_parse(secp256k1_context_static, &secp_ps, partial_sig.data())) {
165
0
            return std::nullopt;
166
0
        }
167
0
    }
168
0
    pubnonce_ptrs.reserve(signers_data.size());
169
0
    partial_sig_ptrs.reserve(signers_data.size());
170
0
    for (auto& [_, pn, ps] : signers_data) {
171
0
        pubnonce_ptrs.push_back(&pn);
172
0
        partial_sig_ptrs.push_back(&ps);
173
0
    }
174
175
    // Aggregate nonces
176
0
    secp256k1_musig_aggnonce aggnonce;
177
0
    if (!secp256k1_musig_nonce_agg(secp256k1_context_static, &aggnonce, pubnonce_ptrs.data(), pubnonce_ptrs.size())) {
178
0
        return std::nullopt;
179
0
    }
180
181
    // Apply tweaks
182
0
    for (const auto& [tweak, xonly] : tweaks) {
183
0
        if (xonly) {
184
0
            if (!secp256k1_musig_pubkey_xonly_tweak_add(secp256k1_context_static, nullptr, &keyagg_cache, tweak.data())) {
185
0
                return std::nullopt;
186
0
            }
187
0
        } else if (!secp256k1_musig_pubkey_ec_tweak_add(secp256k1_context_static, nullptr, &keyagg_cache, tweak.data())) {
188
0
            return std::nullopt;
189
0
        }
190
0
    }
191
192
    // Create musig_session
193
0
    secp256k1_musig_session session;
194
0
    if (!secp256k1_musig_nonce_process(secp256k1_context_static, &session, &aggnonce, sighash.data(), &keyagg_cache)) {
195
0
        return std::nullopt;
196
0
    }
197
198
    // Verify partial sigs
199
0
    for (const auto& [pk, pb, ps] : signers_data) {
200
0
        if (!secp256k1_musig_partial_sig_verify(secp256k1_context_static, &ps, &pb, &pk, &keyagg_cache, &session)) {
201
0
            return std::nullopt;
202
0
        }
203
0
    }
204
205
    // Aggregate partial sigs
206
0
    std::vector<uint8_t> sig;
207
0
    sig.resize(64);
208
0
    if (!secp256k1_musig_partial_sig_agg(secp256k1_context_static, sig.data(), &session, partial_sig_ptrs.data(), partial_sig_ptrs.size())) {
209
0
        return std::nullopt;
210
0
    }
211
212
0
    return sig;
213
0
}