Coverage Report

Created: 2025-09-08 17:06

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
7
#include <secp256k1_musig.h>
8
9
bool GetMuSig2KeyAggCache(const std::vector<CPubKey>& pubkeys, secp256k1_musig_keyagg_cache& keyagg_cache)
10
12.7k
{
11
    // Parse the pubkeys
12
12.7k
    std::vector<secp256k1_pubkey> secp_pubkeys;
13
12.7k
    std::vector<const secp256k1_pubkey*> pubkey_ptrs;
14
329k
    for (const CPubKey& pubkey : pubkeys) {
15
329k
        if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &secp_pubkeys.emplace_back(), pubkey.data(), pubkey.size())) {
16
0
            return false;
17
0
        }
18
329k
    }
19
12.7k
    pubkey_ptrs.reserve(secp_pubkeys.size());
20
329k
    for (const secp256k1_pubkey& p : secp_pubkeys) {
21
329k
        pubkey_ptrs.push_back(&p);
22
329k
    }
23
24
    // Aggregate the pubkey
25
12.7k
    if (!secp256k1_musig_pubkey_agg(secp256k1_context_static, nullptr, &keyagg_cache, pubkey_ptrs.data(), pubkey_ptrs.size())) {
26
0
        return false;
27
0
    }
28
12.7k
    return true;
29
12.7k
}
30
31
std::optional<CPubKey> GetCPubKeyFromMuSig2KeyAggCache(secp256k1_musig_keyagg_cache& keyagg_cache)
32
12.7k
{
33
    // Get the plain aggregated pubkey
34
12.7k
    secp256k1_pubkey agg_pubkey;
35
12.7k
    if (!secp256k1_musig_pubkey_get(secp256k1_context_static, &agg_pubkey, &keyagg_cache)) {
36
0
        return std::nullopt;
37
0
    }
38
39
    // Turn into CPubKey
40
12.7k
    unsigned char ser_agg_pubkey[CPubKey::COMPRESSED_SIZE];
41
12.7k
    size_t ser_agg_pubkey_len = CPubKey::COMPRESSED_SIZE;
42
12.7k
    secp256k1_ec_pubkey_serialize(secp256k1_context_static, ser_agg_pubkey, &ser_agg_pubkey_len, &agg_pubkey, SECP256K1_EC_COMPRESSED);
43
12.7k
    return CPubKey(ser_agg_pubkey, ser_agg_pubkey + ser_agg_pubkey_len);
44
12.7k
}
45
46
std::optional<CPubKey> MuSig2AggregatePubkeys(const std::vector<CPubKey>& pubkeys)
47
12.7k
{
48
12.7k
    secp256k1_musig_keyagg_cache keyagg_cache;
49
12.7k
    if (!GetMuSig2KeyAggCache(pubkeys, keyagg_cache)) {
50
0
        return std::nullopt;
51
0
    }
52
12.7k
    return GetCPubKeyFromMuSig2KeyAggCache(keyagg_cache);
53
12.7k
}