Bitcoin Core 28.99.0
P2P Digital Currency
musig.c
Go to the documentation of this file.
1/*************************************************************************
2 * To the extent possible under law, the author(s) have dedicated all *
3 * copyright and related and neighboring rights to the software in this *
4 * file to the public domain worldwide. This software is distributed *
5 * without any warranty. For the CC0 Public Domain Dedication, see *
6 * EXAMPLES_COPYING or https://creativecommons.org/publicdomain/zero/1.0 *
7 *************************************************************************/
8
14#include <stdio.h>
15#include <assert.h>
16#include <string.h>
17
18#include <secp256k1.h>
19#include <secp256k1_extrakeys.h>
20#include <secp256k1_musig.h>
22
23#include "examples_util.h"
24
28};
29
30struct signer {
34};
35
36 /* Number of public keys involved in creating the aggregate signature */
37#define N_SIGNERS 3
38/* Create a key pair, store it in signer_secrets->keypair and signer->pubkey */
40 unsigned char seckey[32];
41
42 if (!fill_random(seckey, sizeof(seckey))) {
43 printf("Failed to generate randomness\n");
44 return 0;
45 }
46 /* Try to create a keypair with a valid context. This only fails if the
47 * secret key is zero or out of range (greater than secp256k1's order). Note
48 * that the probability of this occurring is negligible with a properly
49 * functioning random number generator. */
50 if (!secp256k1_keypair_create(ctx, &signer_secrets->keypair, seckey)) {
51 return 0;
52 }
54 return 0;
55 }
56
57 secure_erase(seckey, sizeof(seckey));
58 return 1;
59}
60
61/* Tweak the pubkey corresponding to the provided keyagg cache, update the cache
62 * and return the tweaked aggregate pk. */
64 secp256k1_pubkey output_pk;
65 /* For BIP 32 tweaking the plain_tweak is set to a hash as defined in BIP
66 * 32. */
67 unsigned char plain_tweak[32] = "this could be a BIP32 tweak....";
68 /* For Taproot tweaking the xonly_tweak is set to the TapTweak hash as
69 * defined in BIP 341 */
70 unsigned char xonly_tweak[32] = "this could be a Taproot tweak..";
71
72
73 /* Plain tweaking which, for example, allows deriving multiple child
74 * public keys from a single aggregate key using BIP32 */
75 if (!secp256k1_musig_pubkey_ec_tweak_add(ctx, NULL, cache, plain_tweak)) {
76 return 0;
77 }
78 /* Note that we did not provide an output_pk argument, because the
79 * resulting pk is also saved in the cache and so if one is just interested
80 * in signing, the output_pk argument is unnecessary. On the other hand, if
81 * one is not interested in signing, the same output_pk can be obtained by
82 * calling `secp256k1_musig_pubkey_get` right after key aggregation to get
83 * the full pubkey and then call `secp256k1_ec_pubkey_tweak_add`. */
84
85 /* Xonly tweaking which, for example, allows creating Taproot commitments */
86 if (!secp256k1_musig_pubkey_xonly_tweak_add(ctx, &output_pk, cache, xonly_tweak)) {
87 return 0;
88 }
89 /* Note that if we wouldn't care about signing, we can arrive at the same
90 * output_pk by providing the untweaked public key to
91 * `secp256k1_xonly_pubkey_tweak_add` (after converting it to an xonly pubkey
92 * if necessary with `secp256k1_xonly_pubkey_from_pubkey`). */
93
94 /* Now we convert the output_pk to an xonly pubkey to allow to later verify
95 * the Schnorr signature against it. For this purpose we can ignore the
96 * `pk_parity` output argument; we would need it if we would have to open
97 * the Taproot commitment. */
98 if (!secp256k1_xonly_pubkey_from_pubkey(ctx, agg_pk, NULL, &output_pk)) {
99 return 0;
100 }
101 return 1;
102}
103
104/* Sign a message hash with the given key pairs and store the result in sig */
105static int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secrets, struct signer *signer, const secp256k1_musig_keyagg_cache *cache, const unsigned char *msg32, unsigned char *sig64) {
106 int i;
107 const secp256k1_musig_pubnonce *pubnonces[N_SIGNERS];
108 const secp256k1_musig_partial_sig *partial_sigs[N_SIGNERS];
109 /* The same for all signers */
111 secp256k1_musig_aggnonce agg_pubnonce;
112
113 for (i = 0; i < N_SIGNERS; i++) {
114 unsigned char seckey[32];
115 unsigned char session_secrand[32];
116 /* Create random session ID. It is absolutely necessary that the session ID
117 * is unique for every call of secp256k1_musig_nonce_gen. Otherwise
118 * it's trivial for an attacker to extract the secret key! */
119 if (!fill_random(session_secrand, sizeof(session_secrand))) {
120 return 0;
121 }
122 if (!secp256k1_keypair_sec(ctx, seckey, &signer_secrets[i].keypair)) {
123 return 0;
124 }
125 /* Initialize session and create secret nonce for signing and public
126 * nonce to send to the other signers. */
127 if (!secp256k1_musig_nonce_gen(ctx, &signer_secrets[i].secnonce, &signer[i].pubnonce, session_secrand, seckey, &signer[i].pubkey, msg32, NULL, NULL)) {
128 return 0;
129 }
130 pubnonces[i] = &signer[i].pubnonce;
131
132 secure_erase(seckey, sizeof(seckey));
133 }
134
135 /* Communication round 1: Every signer sends their pubnonce to the
136 * coordinator. The coordinator runs secp256k1_musig_nonce_agg and sends
137 * agg_pubnonce to each signer */
138 if (!secp256k1_musig_nonce_agg(ctx, &agg_pubnonce, pubnonces, N_SIGNERS)) {
139 return 0;
140 }
141
142 /* Every signer creates a partial signature */
143 for (i = 0; i < N_SIGNERS; i++) {
144 /* Initialize the signing session by processing the aggregate nonce */
145 if (!secp256k1_musig_nonce_process(ctx, &session, &agg_pubnonce, msg32, cache)) {
146 return 0;
147 }
148 /* partial_sign will clear the secnonce by setting it to 0. That's because
149 * you must _never_ reuse the secnonce (or use the same session_secrand to
150 * create a secnonce). If you do, you effectively reuse the nonce and
151 * leak the secret key. */
152 if (!secp256k1_musig_partial_sign(ctx, &signer[i].partial_sig, &signer_secrets[i].secnonce, &signer_secrets[i].keypair, cache, &session)) {
153 return 0;
154 }
155 partial_sigs[i] = &signer[i].partial_sig;
156 }
157 /* Communication round 2: Every signer sends their partial signature to the
158 * coordinator, who verifies the partial signatures and aggregates them. */
159 for (i = 0; i < N_SIGNERS; i++) {
160 /* To check whether signing was successful, it suffices to either verify
161 * the aggregate signature with the aggregate public key using
162 * secp256k1_schnorrsig_verify, or verify all partial signatures of all
163 * signers individually. Verifying the aggregate signature is cheaper but
164 * verifying the individual partial signatures has the advantage that it
165 * can be used to determine which of the partial signatures are invalid
166 * (if any), i.e., which of the partial signatures cause the aggregate
167 * signature to be invalid and thus the protocol run to fail. It's also
168 * fine to first verify the aggregate sig, and only verify the individual
169 * sigs if it does not work.
170 */
171 if (!secp256k1_musig_partial_sig_verify(ctx, &signer[i].partial_sig, &signer[i].pubnonce, &signer[i].pubkey, cache, &session)) {
172 return 0;
173 }
174 }
175 return secp256k1_musig_partial_sig_agg(ctx, sig64, &session, partial_sigs, N_SIGNERS);
176}
177
178int main(void) {
180 int i;
182 struct signer signers[N_SIGNERS];
183 const secp256k1_pubkey *pubkeys_ptr[N_SIGNERS];
186 unsigned char msg[32] = "this_could_be_the_hash_of_a_msg";
187 unsigned char sig[64];
188
189 /* Create a secp256k1 context */
191 printf("Creating key pairs......");
192 fflush(stdout);
193 for (i = 0; i < N_SIGNERS; i++) {
194 if (!create_keypair(ctx, &signer_secrets[i], &signers[i])) {
195 printf("FAILED\n");
196 return 1;
197 }
198 pubkeys_ptr[i] = &signers[i].pubkey;
199 }
200 printf("ok\n");
201
202 /* The aggregate public key produced by secp256k1_musig_pubkey_agg depends
203 * on the order of the provided public keys. If there is no canonical order
204 * of the signers, the individual public keys can optionally be sorted with
205 * secp256k1_ec_pubkey_sort to ensure that the aggregate public key is
206 * independent of the order of signers. */
207 printf("Sorting public keys.....");
208 fflush(stdout);
209 if (!secp256k1_ec_pubkey_sort(ctx, pubkeys_ptr, N_SIGNERS)) {
210 printf("FAILED\n");
211 return 1;
212 }
213 printf("ok\n");
214
215 printf("Combining public keys...");
216 fflush(stdout);
217 /* If you just want to aggregate and not sign, you can call
218 * secp256k1_musig_pubkey_agg with the keyagg_cache argument set to NULL
219 * while providing a non-NULL agg_pk argument. */
220 if (!secp256k1_musig_pubkey_agg(ctx, NULL, &cache, pubkeys_ptr, N_SIGNERS)) {
221 printf("FAILED\n");
222 return 1;
223 }
224 printf("ok\n");
225 printf("Tweaking................");
226 fflush(stdout);
227 /* Optionally tweak the aggregate key */
228 if (!tweak(ctx, &agg_pk, &cache)) {
229 printf("FAILED\n");
230 return 1;
231 }
232 printf("ok\n");
233 printf("Signing message.........");
234 fflush(stdout);
235 if (!sign(ctx, signer_secrets, signers, &cache, msg, sig)) {
236 printf("FAILED\n");
237 return 1;
238 }
239 printf("ok\n");
240 printf("Verifying signature.....");
241 fflush(stdout);
242 if (!secp256k1_schnorrsig_verify(ctx, sig, msg, 32, &agg_pk)) {
243 printf("FAILED\n");
244 return 1;
245 }
246 printf("ok\n");
247
248 /* It's best practice to try to clear secrets from memory after using them.
249 * This is done because some bugs can allow an attacker to leak memory, for
250 * example through "out of bounds" array access (see Heartbleed), or the OS
251 * swapping them to disk. Hence, we overwrite secret key material with zeros.
252 *
253 * Here we are preventing these writes from being optimized out, as any good compiler
254 * will remove any writes that aren't used. */
255 for (i = 0; i < N_SIGNERS; i++) {
257 }
259 return 0;
260}
static int fill_random(unsigned char *data, size_t size)
Definition: examples_util.h:43
static void secure_erase(void *ptr, size_t len)
Definition: examples_util.h:86
static int sign(const secp256k1_context *ctx, struct signer_secrets *signer_secrets, struct signer *signer, const secp256k1_musig_keyagg_cache *cache, const unsigned char *msg32, unsigned char *sig64)
Definition: musig.c:105
int main(void)
Definition: musig.c:178
static int create_keypair(const secp256k1_context *ctx, struct signer_secrets *signer_secrets, struct signer *signer)
Definition: musig.c:39
#define N_SIGNERS
Definition: musig.c:37
static int tweak(const secp256k1_context *ctx, secp256k1_xonly_pubkey *agg_pk, secp256k1_musig_keyagg_cache *cache)
Definition: musig.c:63
void printf(FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1089
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:187
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
Definition: secp256k1.c:141
#define SECP256K1_CONTEXT_NONE
Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size,...
Definition: secp256k1.h:202
SECP256K1_API int secp256k1_ec_pubkey_sort(const secp256k1_context *ctx, const secp256k1_pubkey **pubkeys, size_t n_pubkeys) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Sort public keys using lexicographic (of compressed serialization) order.
Definition: secp256k1.c:323
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_pub(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const secp256k1_keypair *keypair) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Get the public key from a keypair.
Definition: main_impl.h:224
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_sec(const secp256k1_context *ctx, unsigned char *seckey, const secp256k1_keypair *keypair) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Get the secret key from a keypair.
Definition: main_impl.h:214
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_create(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the keypair for a valid secret key.
Definition: main_impl.h:196
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_from_pubkey(const secp256k1_context *ctx, secp256k1_xonly_pubkey *xonly_pubkey, int *pk_parity, const secp256k1_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4)
Converts a secp256k1_pubkey into a secp256k1_xonly_pubkey.
Definition: main_impl.h:99
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 ...
Definition: keyagg_impl.h:283
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...
Definition: keyagg_impl.h:287
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.
Definition: session_impl.h:536
SECP256K1_API int secp256k1_musig_partial_sign(const secp256k1_context *ctx, secp256k1_musig_partial_sig *partial_sig, secp256k1_musig_secnonce *secnonce, const secp256k1_keypair *keypair, 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)
Produces a partial signature.
Definition: session_impl.h:658
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_nonce_gen(const secp256k1_context *ctx, secp256k1_musig_secnonce *secnonce, secp256k1_musig_pubnonce *pubnonce, unsigned char *session_secrand32, const unsigned char *seckey, const secp256k1_pubkey *pubkey, const unsigned char *msg32, const secp256k1_musig_keyagg_cache *keyagg_cache, const unsigned char *extra_input32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(6)
Starts a signing session by generating a nonce.
Definition: session_impl.h:461
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.
Definition: session_impl.h:728
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.
Definition: session_impl.h:791
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
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...
Definition: session_impl.h:612
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorrsig_verify(const secp256k1_context *ctx, const unsigned char *sig64, const unsigned char *msg, size_t msglen, const secp256k1_xonly_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(5)
Verify a Schnorr signature.
Definition: main_impl.h:221
Opaque data structure that holds a keypair consisting of a secret and a public key.
Opaque data structure that holds an aggregate public nonce.
This module implements BIP 327 "MuSig2 for BIP340-compatible Multi-Signatures" (https://github....
Opaque data structure that holds a partial MuSig signature.
Opaque data structure that holds a signer's public nonce.
Opaque data structure that holds a signer's secret nonce.
Opaque data structure that holds a MuSig session.
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:61
Opaque data structure that holds a parsed and valid "x-only" public key.
This file demonstrates how to use the MuSig module to create a 3-of-3 multisignature.
Definition: musig.c:25
secp256k1_musig_secnonce secnonce
Definition: musig.c:27
secp256k1_keypair keypair
Definition: musig.c:26
Definition: musig.c:30
secp256k1_pubkey pubkey
Definition: musig.c:31
secp256k1_musig_partial_sig partial_sig
Definition: musig.c:33
secp256k1_musig_pubnonce pubnonce
Definition: musig.c:32