Skip to content

Commit 427ca95

Browse files
committed
Add MuSig2 adaptor sig exercise in example code
1 parent 7a30cb0 commit 427ca95

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

examples/musig.c

+24-2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secrets, st
9898
/* The same for all signers */
9999
secp256k1_musig_session session;
100100

101+
/* For adapter signature, committing to random scalar */
102+
int nonce_parity;
103+
unsigned char adaptor_key[32];
104+
secp256k1_pubkey adaptor;
105+
if (!fill_random(adaptor_key, sizeof(adaptor_key))) {
106+
return 0;
107+
}
108+
if (!secp256k1_ec_pubkey_create(ctx, &adaptor, adaptor_key)) {
109+
return 0;
110+
}
111+
101112
for (i = 0; i < N_SIGNERS; i++) {
102113
unsigned char seckey[32];
103114
unsigned char session_id[32];
@@ -126,7 +137,7 @@ int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secrets, st
126137
if (!secp256k1_musig_nonce_agg(ctx, &agg_pubnonce, pubnonces, N_SIGNERS)) {
127138
return 0;
128139
}
129-
if (!secp256k1_musig_nonce_process(ctx, &session, &agg_pubnonce, msg32, cache, NULL)) {
140+
if (!secp256k1_musig_nonce_process(ctx, &session, &agg_pubnonce, msg32, cache, &adaptor)) {
130141
return 0;
131142
}
132143
/* partial_sign will clear the secnonce by setting it to 0. That's because
@@ -156,7 +167,18 @@ int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secrets, st
156167
return 0;
157168
}
158169
}
159-
return secp256k1_musig_partial_sig_agg(ctx, sig64, &session, partial_sigs, N_SIGNERS);
170+
171+
/* Since we are doing adaptor sig, complete pre-signature */
172+
if (!secp256k1_musig_nonce_parity(ctx, &nonce_parity, &session)) {
173+
return 0;
174+
}
175+
if (!secp256k1_musig_partial_sig_agg(ctx, sig64, &session, partial_sigs, N_SIGNERS)){
176+
return 0;
177+
}
178+
if (!secp256k1_musig_adapt(ctx, sig64, sig64, adaptor_key, nonce_parity)) {
179+
return 0;
180+
}
181+
return 1;
160182
}
161183

162184
int main(void) {

0 commit comments

Comments
 (0)