Bitcoin Core 29.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>
6
7#include <secp256k1_musig.h>
8
9bool 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
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
46std::optional<CPubKey> MuSig2AggregatePubkeys(const std::vector<CPubKey>& pubkeys)
47{
49 if (!GetMuSig2KeyAggCache(pubkeys, keyagg_cache)) {
50 return std::nullopt;
51 }
52 return GetCPubKeyFromMuSig2KeyAggCache(keyagg_cache);
53}
An encapsulated public key.
Definition: pubkey.h:34
static constexpr unsigned int COMPRESSED_SIZE
Definition: pubkey.h:40
bool GetMuSig2KeyAggCache(const std::vector< CPubKey > &pubkeys, secp256k1_musig_keyagg_cache &keyagg_cache)
Create a secp256k1_musig_keyagg_cache from the pubkeys in their current order. This is necessary for ...
Definition: musig.cpp:9
std::optional< CPubKey > GetCPubKeyFromMuSig2KeyAggCache(secp256k1_musig_keyagg_cache &keyagg_cache)
Retrieve the full aggregate pubkey from the secp256k1_musig_keyagg_cache.
Definition: musig.cpp:31
std::optional< CPubKey > MuSig2AggregatePubkeys(const std::vector< CPubKey > &pubkeys)
Compute the full aggregate pubkey from the given participant pubkeys in their current order.
Definition: musig.cpp:46
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_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_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
This module implements BIP 327 "MuSig2 for BIP340-compatible Multi-Signatures" (https://github....
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:61