Skip to content

Commit 772df36

Browse files
Merge #151: MuSig: Add Minimal Compatibility with BIP32 Tweaking
8088edd musig: add test vector for ordinary (non xonly) tweaking (Elliott Jin) 57a1792 musig: add ordinary and xonly tweaking to the example (Jonas Nick) 3710736 musig: allow ordinary, non-xonly tweaking (Jonas Nick) c519b46 musig: add pubkey_get to obtain a full pubkey from a keyagg_cache (Jonas Nick) Pull request description: In short, `musig_pubkey_tweak_add` now allows for xonly _and_ "ordinary" tweaking. Also, in order to allow using `ec_pubkey_tweak_add` on the non-xonly aggregate public key, there's a new function `musig_pubkey_get` that allows obtaining it from the `keyagg_cache`. One alternative would be that instead of adding `musig_pubkey_get`, we could change `pubkey_agg` to output an ordinary (non-xonly) pubkey. Then users of the API who do not need ordinary (BIP32) tweaking would be forced to call `xonly_pubkey_from_pubkey`. And we'd probably want to change the spec. And it would be a bit weird to output a pubkey that can't be directly schnorrsig_verify'd. Based on #131 ACKs for top commit: robot-dreams: ACK 8088edd based on #151 (comment) and the following `range-diff`: Tree-SHA512: a4a0100f0470c870f88a8da27dbcc4684fcc2caabb368d4340e962e08d5ee04634e6289bafa3448dbfd0b5793a3e70de5bd6ddca7a619cc3220ff762d518a8fe
2 parents a5b5909 + 8088edd commit 772df36

File tree

5 files changed

+298
-110
lines changed

5 files changed

+298
-110
lines changed

examples/musig.c

+55-13
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,51 @@ int create_keypair(const secp256k1_context* ctx, struct signer_secrets *signer_s
5252
return 1;
5353
}
5454

55+
/* Tweak the pubkey corresponding to the provided keyagg cache, update the cache
56+
* and return the tweaked aggregate pk. */
57+
int tweak(const secp256k1_context* ctx, secp256k1_xonly_pubkey *agg_pk, secp256k1_musig_keyagg_cache *cache) {
58+
secp256k1_pubkey output_pk;
59+
unsigned char ordinary_tweak[32] = "this could be a BIP32 tweak....";
60+
unsigned char xonly_tweak[32] = "this could be a taproot tweak..";
61+
62+
63+
/* Ordinary tweaking which, for example, allows deriving multiple child
64+
* public keys from a single aggregate key using BIP32 */
65+
if (!secp256k1_musig_pubkey_ec_tweak_add(ctx, NULL, cache, ordinary_tweak)) {
66+
return 0;
67+
}
68+
/* Note that we did not provided an output_pk argument, because the
69+
* resulting pk is also saved in the cache and so if one is just interested
70+
* in signing the output_pk argument is unnecessary. On the other hand, if
71+
* one is not interested in signing, the same output_pk can be obtained by
72+
* calling `secp256k1_musig_pubkey_get` right after key aggregation to get
73+
* the full pubkey and then call `secp256k1_ec_pubkey_tweak_add`. */
74+
75+
/* Xonly tweaking which, for example, allows creating taproot commitments */
76+
if (!secp256k1_musig_pubkey_xonly_tweak_add(ctx, &output_pk, cache, xonly_tweak)) {
77+
return 0;
78+
}
79+
/* Note that if we wouldn't care about signing, we can arrive at the same
80+
* output_pk by providing the untweaked public key to
81+
* `secp256k1_xonly_pubkey_tweak_add` (after converting it to an xonly pubkey
82+
* if necessary with `secp256k1_xonly_pubkey_from_pubkey`). */
83+
84+
/* Now we convert the output_pk to an xonly pubkey to allow to later verify
85+
* the Schnorr signature against it. For this purpose we can ignore the
86+
* `pk_parity` output argument; we would need it if we would have to open
87+
* the taproot commitment. */
88+
if (!secp256k1_xonly_pubkey_from_pubkey(ctx, agg_pk, NULL, &output_pk)) {
89+
return 0;
90+
}
91+
return 1;
92+
}
93+
5594
/* Sign a message hash with the given key pairs and store the result in sig */
56-
int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secrets, struct signer *signer, const unsigned char* msg32, unsigned char *sig64) {
95+
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) {
5796
int i;
58-
const secp256k1_xonly_pubkey *pubkeys[N_SIGNERS];
5997
const secp256k1_musig_pubnonce *pubnonces[N_SIGNERS];
6098
const secp256k1_musig_partial_sig *partial_sigs[N_SIGNERS];
6199
/* The same for all signers */
62-
secp256k1_musig_keyagg_cache cache;
63100
secp256k1_musig_session session;
64101

65102
for (i = 0; i < N_SIGNERS; i++) {
@@ -86,29 +123,25 @@ int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secrets, st
86123
if (!secp256k1_musig_nonce_gen(ctx, &signer_secrets[i].secnonce, &signer[i].pubnonce, session_id, seckey, msg32, NULL, NULL)) {
87124
return 0;
88125
}
89-
pubkeys[i] = &signer[i].pubkey;
90126
pubnonces[i] = &signer[i].pubnonce;
91127
}
92128
/* Communication round 1: A production system would exchange public nonces
93129
* here before moving on. */
94130
for (i = 0; i < N_SIGNERS; i++) {
95131
secp256k1_musig_aggnonce agg_pubnonce;
96132

97-
/* Create aggregate pubkey, aggregate nonce and initialize signer data */
98-
if (!secp256k1_musig_pubkey_agg(ctx, NULL, NULL, &cache, pubkeys, N_SIGNERS)) {
99-
return 0;
100-
}
133+
/* Create aggregate nonce and initialize the session */
101134
if (!secp256k1_musig_nonce_agg(ctx, &agg_pubnonce, pubnonces, N_SIGNERS)) {
102135
return 0;
103136
}
104-
if (!secp256k1_musig_nonce_process(ctx, &session, &agg_pubnonce, msg32, &cache, NULL)) {
137+
if (!secp256k1_musig_nonce_process(ctx, &session, &agg_pubnonce, msg32, cache, NULL)) {
105138
return 0;
106139
}
107140
/* partial_sign will clear the secnonce by setting it to 0. That's because
108141
* you must _never_ reuse the secnonce (or use the same session_id to
109142
* create a secnonce). If you do, you effectively reuse the nonce and
110143
* leak the secret key. */
111-
if (!secp256k1_musig_partial_sign(ctx, &signer[i].partial_sig, &signer_secrets[i].secnonce, &signer_secrets[i].keypair, &cache, &session)) {
144+
if (!secp256k1_musig_partial_sign(ctx, &signer[i].partial_sig, &signer_secrets[i].secnonce, &signer_secrets[i].keypair, cache, &session)) {
112145
return 0;
113146
}
114147
partial_sigs[i] = &signer[i].partial_sig;
@@ -127,7 +160,7 @@ int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secrets, st
127160
* fine to first verify the aggregate sig, and only verify the individual
128161
* sigs if it does not work.
129162
*/
130-
if (!secp256k1_musig_partial_sig_verify(ctx, &signer[i].partial_sig, &signer[i].pubnonce, &signer[i].pubkey, &cache, &session)) {
163+
if (!secp256k1_musig_partial_sig_verify(ctx, &signer[i].partial_sig, &signer[i].pubnonce, &signer[i].pubkey, cache, &session)) {
131164
return 0;
132165
}
133166
}
@@ -141,6 +174,7 @@ int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secrets, st
141174
struct signer signers[N_SIGNERS];
142175
const secp256k1_xonly_pubkey *pubkeys_ptr[N_SIGNERS];
143176
secp256k1_xonly_pubkey agg_pk;
177+
secp256k1_musig_keyagg_cache cache;
144178
unsigned char msg[32] = "this_could_be_the_hash_of_a_msg!";
145179
unsigned char sig[64];
146180

@@ -156,13 +190,21 @@ int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secrets, st
156190
}
157191
printf("ok\n");
158192
printf("Combining public keys...");
159-
if (!secp256k1_musig_pubkey_agg(ctx, NULL, &agg_pk, NULL, pubkeys_ptr, N_SIGNERS)) {
193+
/* If you just want to aggregate and not sign the cache can be NULL */
194+
if (!secp256k1_musig_pubkey_agg(ctx, NULL, &agg_pk, &cache, pubkeys_ptr, N_SIGNERS)) {
195+
printf("FAILED\n");
196+
return 1;
197+
}
198+
printf("ok\n");
199+
printf("Tweaking................");
200+
/* Optionally tweak the aggregate key */
201+
if (!tweak(ctx, &agg_pk, &cache)) {
160202
printf("FAILED\n");
161203
return 1;
162204
}
163205
printf("ok\n");
164206
printf("Signing message.........");
165-
if (!sign(ctx, signer_secrets, signers, msg, sig)) {
207+
if (!sign(ctx, signer_secrets, signers, &cache, msg, sig)) {
166208
printf("FAILED\n");
167209
return 1;
168210
}

include/secp256k1_musig.h

+66-5
Original file line numberDiff line numberDiff line change
@@ -223,16 +223,77 @@ SECP256K1_API int secp256k1_musig_pubkey_agg(
223223
size_t n_pubkeys
224224
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(5);
225225

226-
/** Tweak an x-only public key in a given keyagg_cache by adding
227-
* the generator multiplied with `tweak32` to it.
226+
/** Obtain the aggregate public key from a keyagg_cache.
227+
*
228+
* This is only useful if you need the non-xonly public key, in particular for
229+
* ordinary (non-xonly) tweaking or batch-verifying multiple key aggregations
230+
* (not implemented).
231+
*
232+
* Returns: 0 if the arguments are invalid, 1 otherwise
233+
* Args: ctx: pointer to a context object
234+
* Out: agg_pk: the MuSig-aggregated public key.
235+
* In: keyagg_cache: pointer to a `musig_keyagg_cache` struct initialized by
236+
* `musig_pubkey_agg`
237+
*/
238+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubkey_get(
239+
const secp256k1_context* ctx,
240+
secp256k1_pubkey *agg_pk,
241+
secp256k1_musig_keyagg_cache *keyagg_cache
242+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
243+
244+
/** Apply ordinary "EC" tweaking to a public key in a given keyagg_cache by
245+
* adding the generator multiplied with `tweak32` to it. This is useful for
246+
* deriving child keys from an aggregate public key via BIP32.
247+
*
248+
* The tweaking method is the same as `secp256k1_ec_pubkey_tweak_add`. So after
249+
* the following pseudocode buf and buf2 have identical contents (absent
250+
* earlier failures).
251+
*
252+
* secp256k1_musig_pubkey_agg(..., keyagg_cache, pubkeys, ...)
253+
* secp256k1_musig_pubkey_get(..., agg_pk, keyagg_cache)
254+
* secp256k1_musig_pubkey_ec_tweak_add(..., output_pk, tweak32, keyagg_cache)
255+
* secp256k1_ec_pubkey_serialize(..., buf, output_pk)
256+
* secp256k1_ec_pubkey_tweak_add(..., agg_pk, tweak32)
257+
* secp256k1_ec_pubkey_serialize(..., buf2, agg_pk)
258+
*
259+
* This function is required if you want to _sign_ for a tweaked aggregate key.
260+
* On the other hand, if you are only computing a public key, but not intending
261+
* to create a signature for it, you can just use
262+
* `secp256k1_ec_pubkey_tweak_add`.
263+
*
264+
* Returns: 0 if the arguments are invalid or the resulting public key would be
265+
* invalid (only when the tweak is the negation of the corresponding
266+
* secret key). 1 otherwise.
267+
* Args: ctx: pointer to a context object initialized for verification
268+
* Out: output_pubkey: pointer to a public key to store the result. Will be set
269+
* to an invalid value if this function returns 0. If you
270+
* do not need it, this arg can be NULL.
271+
* In/Out: keyagg_cache: pointer to a `musig_keyagg_cache` struct initialized by
272+
* `musig_pubkey_agg`
273+
* In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid
274+
* according to `secp256k1_ec_seckey_verify`, this function
275+
* returns 0. For uniformly random 32-byte arrays the
276+
* chance of being invalid is negligible (around 1 in
277+
* 2^128).
278+
*/
279+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubkey_ec_tweak_add(
280+
const secp256k1_context* ctx,
281+
secp256k1_pubkey *output_pubkey,
282+
secp256k1_musig_keyagg_cache *keyagg_cache,
283+
const unsigned char *tweak32
284+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
285+
286+
/** Apply x-only tweaking to a public key in a given keyagg_cache by adding the
287+
* generator multiplied with `tweak32` to it. This is useful for creating
288+
* Taproot outputs.
228289
*
229290
* The tweaking method is the same as `secp256k1_xonly_pubkey_tweak_add`. So in
230291
* the following pseudocode xonly_pubkey_tweak_add_check (absent earlier
231292
* failures) returns 1.
232293
*
233294
* secp256k1_musig_pubkey_agg(..., agg_pk, keyagg_cache, pubkeys, ...)
234-
* secp256k1_musig_pubkey_tweak_add(..., output_pubkey, tweak32, keyagg_cache)
235-
* secp256k1_xonly_pubkey_serialize(..., buf, output_pubkey)
295+
* secp256k1_musig_pubkey_xonly_tweak_add(..., output_pk, tweak32, keyagg_cache)
296+
* secp256k1_xonly_pubkey_serialize(..., buf, output_pk)
236297
* secp256k1_xonly_pubkey_tweak_add_check(..., buf, ..., agg_pk, tweak32)
237298
*
238299
* This function is required if you want to _sign_ for a tweaked aggregate key.
@@ -255,7 +316,7 @@ SECP256K1_API int secp256k1_musig_pubkey_agg(
255316
* chance of being invalid is negligible (around 1 in
256317
* 2^128).
257318
*/
258-
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubkey_tweak_add(
319+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubkey_xonly_tweak_add(
259320
const secp256k1_context* ctx,
260321
secp256k1_pubkey *output_pubkey,
261322
secp256k1_musig_keyagg_cache *keyagg_cache,

src/modules/musig/keyagg_impl.h

+24-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,21 @@ int secp256k1_musig_pubkey_agg(const secp256k1_context* ctx, secp256k1_scratch_s
244244
return 1;
245245
}
246246

247-
int secp256k1_musig_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *output_pubkey, secp256k1_musig_keyagg_cache *keyagg_cache, const unsigned char *tweak32) {
247+
int secp256k1_musig_pubkey_get(const secp256k1_context* ctx, secp256k1_pubkey *agg_pk, secp256k1_musig_keyagg_cache *keyagg_cache) {
248+
secp256k1_keyagg_cache_internal cache_i;
249+
VERIFY_CHECK(ctx != NULL);
250+
ARG_CHECK(agg_pk != NULL);
251+
memset(agg_pk, 0, sizeof(*agg_pk));
252+
ARG_CHECK(keyagg_cache != NULL);
253+
254+
if(!secp256k1_keyagg_cache_load(ctx, &cache_i, keyagg_cache)) {
255+
return 0;
256+
}
257+
secp256k1_pubkey_save(agg_pk, &cache_i.pk);
258+
return 1;
259+
}
260+
261+
static int secp256k1_musig_pubkey_tweak_add_internal(const secp256k1_context* ctx, secp256k1_pubkey *output_pubkey, secp256k1_musig_keyagg_cache *keyagg_cache, const unsigned char *tweak32, int xonly) {
248262
secp256k1_keyagg_cache_internal cache_i;
249263
int overflow = 0;
250264
secp256k1_scalar tweak;
@@ -263,7 +277,7 @@ int secp256k1_musig_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pub
263277
if (overflow) {
264278
return 0;
265279
}
266-
if (secp256k1_extrakeys_ge_even_y(&cache_i.pk)) {
280+
if (xonly && secp256k1_extrakeys_ge_even_y(&cache_i.pk)) {
267281
cache_i.internal_key_parity ^= 1;
268282
secp256k1_scalar_negate(&cache_i.tweak, &cache_i.tweak);
269283
}
@@ -280,4 +294,12 @@ int secp256k1_musig_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pub
280294
return 1;
281295
}
282296

297+
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) {
298+
return secp256k1_musig_pubkey_tweak_add_internal(ctx, output_pubkey, keyagg_cache, tweak32, 0);
299+
}
300+
301+
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) {
302+
return secp256k1_musig_pubkey_tweak_add_internal(ctx, output_pubkey, keyagg_cache, tweak32, 1);
303+
}
304+
283305
#endif

0 commit comments

Comments
 (0)