diff --git a/examples/musig.c b/examples/musig.c index d4a02195a..150f27042 100644 --- a/examples/musig.c +++ b/examples/musig.c @@ -21,6 +21,7 @@ #include #include "examples_util.h" +#include struct signer_secrets { secp256k1_keypair keypair; @@ -100,6 +101,19 @@ static int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secr /* The same for all signers */ secp256k1_musig_session session; + /* For adapter signature, committing to random scalar */ + unsigned char presig[64]; + int nonce_parity; + unsigned char adaptor_key[32]; + unsigned char extracted_adaptor[32]; + secp256k1_pubkey adaptor; + if (!fill_random(adaptor_key, sizeof(adaptor_key))) { + return 0; + } + if (!secp256k1_ec_pubkey_create(ctx, &adaptor, adaptor_key)) { + return 0; + } + for (i = 0; i < N_SIGNERS; i++) { unsigned char seckey[32]; unsigned char session_id[32]; @@ -128,7 +142,7 @@ static int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secr if (!secp256k1_musig_nonce_agg(ctx, &agg_pubnonce, pubnonces, N_SIGNERS)) { return 0; } - if (!secp256k1_musig_nonce_process(ctx, &session, &agg_pubnonce, msg32, cache, NULL)) { + if (!secp256k1_musig_nonce_process(ctx, &session, &agg_pubnonce, msg32, cache, &adaptor)) { return 0; } /* partial_sign will clear the secnonce by setting it to 0. That's because @@ -158,7 +172,23 @@ static int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secr return 0; } } - return secp256k1_musig_partial_sig_agg(ctx, sig64, &session, partial_sigs, N_SIGNERS); + + /* Since we are doing adaptor sig, complete pre-signature */ + if (!secp256k1_musig_nonce_parity(ctx, &nonce_parity, &session)) { + return 0; + } + if (!secp256k1_musig_partial_sig_agg(ctx, presig, &session, partial_sigs, N_SIGNERS)) { + return 0; + } + if (!secp256k1_musig_adapt(ctx, sig64, presig, adaptor_key, nonce_parity)) { + return 0; + } + /* With sig64 "on-chain" now, other party can grab the revealed adaptor secret */ + if (!secp256k1_musig_extract_adaptor(ctx, extracted_adaptor, sig64, presig, nonce_parity)) { + return 0; + } + assert(memcmp(extracted_adaptor, adaptor_key, sizeof(adaptor_key)) == 0); + return 1; } int main(void) {