Skip to content

Commit d00d954

Browse files
committed
Add MuSig2 Keyagg Cache helper functions
secp256k1 provides us secp256k1_musig_keyagg_cache objects which we are used as part of session info and to get the aggregate pubkey. These helper functions help us convert to/from the secp256k1 C objects into the Bitcoin Core C++ objects.
1 parent 8ecea91 commit d00d954

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ add_library(bitcoin_common STATIC EXCLUDE_FROM_ALL
161161
key.cpp
162162
key_io.cpp
163163
merkleblock.cpp
164+
musig.cpp
164165
net_permissions.cpp
165166
net_types.cpp
166167
netaddress.cpp

src/musig.cpp

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

src/musig.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2024-present The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or https://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_MUSIG_H
6+
#define BITCOIN_MUSIG_H
7+
8+
#include <pubkey.h>
9+
10+
#include <optional>
11+
#include <vector>
12+
13+
struct secp256k1_musig_keyagg_cache;
14+
15+
//! Create a secp256k1_musig_keyagg_cache from the pubkeys in their current order. This is necessary for most MuSig2 operations
16+
bool GetMuSig2KeyAggCache(const std::vector<CPubKey>& pubkeys, secp256k1_musig_keyagg_cache& keyagg_cache);
17+
//! Retrieve the full aggregate pubkey from the secp256k1_musig_keyagg_cache
18+
std::optional<CPubKey> GetCPubKeyFromMuSig2KeyAggCache(secp256k1_musig_keyagg_cache& cache);
19+
//! Compute the full aggregate pubkey from the given participant pubkeys in their current order
20+
std::optional<CPubKey> MuSig2AggregatePubkeys(const std::vector<CPubKey>& pubkeys);
21+
22+
#endif // BITCOIN_MUSIG_H

0 commit comments

Comments
 (0)