Skip to content

Commit b498180

Browse files
committed
fixup! split In/Out arg in musig_adapt into two args
1 parent 5f772dd commit b498180

File tree

4 files changed

+40
-33
lines changed

4 files changed

+40
-33
lines changed

include/secp256k1_musig.h

+10-8
Original file line numberDiff line numberDiff line change
@@ -462,26 +462,28 @@ SECP256K1_API int secp256k1_musig_nonce_parity(
462462
const secp256k1_musig_session *session
463463
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
464464

465-
/** Converts a pre-signature that misses the adaptor into a full signature
465+
/** Creates a signature from a pre-signature and an adaptor.
466466
*
467-
* If the sec_adaptor32 argument is incorrect, the adapted signature will be
468-
* invalid. This function does not verify the adapted signature.
467+
* If the sec_adaptor32 argument is incorrect, the output signature will be
468+
* invalid. This function does not verify the signature.
469469
*
470-
* Returns: 0 if the arguments are invalid, or sig64 or sec_adaptor32 contain
470+
* Returns: 0 if the arguments are invalid, or pre_sig64 or sec_adaptor32 contain
471471
* invalid (overflowing) values. 1 otherwise (which does NOT mean the
472472
* signature or the adaptor are valid!)
473473
* Args: ctx: pointer to a context object
474-
* In/Out: sig64: 64-byte pre-signature that is adapted to a complete signature
475-
* In: sec_adaptor32: 32-byte secret adaptor to add to the partial signature
474+
* Out: sig64: 64-byte signature
475+
* In: pre_sig64: 64-byte pre-signature
476+
* sec_adaptor32: 32-byte secret adaptor to add to the pre-signature
476477
* nonce_parity: the output of `musig_nonce_parity` called with the
477-
* session used for producing sig64
478+
* session used for producing the pre-signature
478479
*/
479480
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_adapt(
480481
const secp256k1_context* ctx,
481482
unsigned char *sig64,
483+
const unsigned char *pre_sig64,
482484
const unsigned char *sec_adaptor32,
483485
int nonce_parity
484-
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
486+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
485487

486488
/** Extracts a secret adaptor from a MuSig pre-signature and corresponding
487489
* signature

src/modules/musig/adaptor_impl.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef SECP256K1_MODULE_MUSIG_ADAPTOR_IMPL_H
88
#define SECP256K1_MODULE_MUSIG_ADAPTOR_IMPL_H
99

10+
#include <string.h>
11+
1012
#include "../../../include/secp256k1.h"
1113
#include "../../../include/secp256k1_musig.h"
1214

@@ -26,18 +28,19 @@ int secp256k1_musig_nonce_parity(const secp256k1_context* ctx, int *nonce_parity
2628
return 1;
2729
}
2830

29-
int secp256k1_musig_adapt(const secp256k1_context* ctx, unsigned char *sig64, const unsigned char *sec_adaptor32, int nonce_parity) {
31+
int secp256k1_musig_adapt(const secp256k1_context* ctx, unsigned char *sig64, const unsigned char *pre_sig64, const unsigned char *sec_adaptor32, int nonce_parity) {
3032
secp256k1_scalar s;
3133
secp256k1_scalar t;
3234
int overflow;
3335
int ret = 1;
3436

3537
VERIFY_CHECK(ctx != NULL);
3638
ARG_CHECK(sig64 != NULL);
39+
ARG_CHECK(pre_sig64 != NULL);
3740
ARG_CHECK(sec_adaptor32 != NULL);
3841
ARG_CHECK(nonce_parity == 0 || nonce_parity == 1);
3942

40-
secp256k1_scalar_set_b32(&s, &sig64[32], &overflow);
43+
secp256k1_scalar_set_b32(&s, &pre_sig64[32], &overflow);
4144
if (overflow) {
4245
return 0;
4346
}
@@ -52,14 +55,15 @@ int secp256k1_musig_adapt(const secp256k1_context* ctx, unsigned char *sig64, co
5255
* Since a BIP340 signature requires an x-only public nonce, in the case where
5356
* (r + t)*G has odd Y-coordinate (i.e. nonce_parity == 1), the x-only public nonce
5457
* corresponding to the signature is actually (-r - t)*G. Thus adapting a
55-
* presignature requires negating t in this case.
58+
* pre-signature requires negating t in this case.
5659
*/
5760
if (nonce_parity) {
5861
secp256k1_scalar_negate(&t, &t);
5962
}
6063

6164
secp256k1_scalar_add(&s, &s, &t);
6265
secp256k1_scalar_get_b32(&sig64[32], &s);
66+
memmove(sig64, pre_sig64, 32);
6367
secp256k1_scalar_clear(&t);
6468
return ret;
6569
}

src/modules/musig/tests_impl.h

+22-20
Original file line numberDiff line numberDiff line change
@@ -539,23 +539,25 @@ void musig_api_tests(secp256k1_scratch_space *scratch) {
539539
CHECK(ecount == 3);
540540

541541
ecount = 0;
542-
{
543-
unsigned char tmp_sig[64];
544-
memcpy(tmp_sig, pre_sig, sizeof(tmp_sig));
545-
CHECK(secp256k1_musig_adapt(none, tmp_sig, sec_adaptor, nonce_parity) == 1);
546-
CHECK(secp256k1_musig_adapt(none, NULL, sec_adaptor, 0) == 0);
547-
CHECK(ecount == 1);
548-
CHECK(secp256k1_musig_adapt(none, max64, sec_adaptor, 0) == 0);
549-
CHECK(ecount == 1);
550-
CHECK(secp256k1_musig_adapt(none, tmp_sig, NULL, 0) == 0);
551-
CHECK(ecount == 2);
552-
CHECK(secp256k1_musig_adapt(none, tmp_sig, max64, nonce_parity) == 0);
553-
CHECK(ecount == 2);
554-
CHECK(secp256k1_musig_adapt(none, tmp_sig, sec_adaptor, 2) == 0);
555-
CHECK(ecount == 3);
556-
}
542+
CHECK(secp256k1_musig_adapt(none, final_sig, pre_sig, sec_adaptor, nonce_parity) == 1);
543+
CHECK(secp256k1_musig_adapt(none, NULL, pre_sig, sec_adaptor, 0) == 0);
544+
CHECK(ecount == 1);
545+
CHECK(secp256k1_musig_adapt(none, final_sig, NULL, sec_adaptor, 0) == 0);
546+
CHECK(ecount == 2);
547+
CHECK(secp256k1_musig_adapt(none, final_sig, max64, sec_adaptor, 0) == 0);
548+
CHECK(ecount == 2);
549+
CHECK(secp256k1_musig_adapt(none, final_sig, pre_sig, NULL, 0) == 0);
550+
CHECK(ecount == 3);
551+
CHECK(secp256k1_musig_adapt(none, final_sig, pre_sig, max64, 0) == 0);
552+
CHECK(ecount == 3);
553+
CHECK(secp256k1_musig_adapt(none, final_sig, pre_sig, sec_adaptor, 2) == 0);
554+
CHECK(ecount == 4);
555+
/* sig and pre_sig argument point to the same location */
557556
memcpy(final_sig, pre_sig, sizeof(final_sig));
558-
CHECK(secp256k1_musig_adapt(none, final_sig, sec_adaptor, nonce_parity) == 1);
557+
CHECK(secp256k1_musig_adapt(none, final_sig, final_sig, sec_adaptor, nonce_parity) == 1);
558+
CHECK(secp256k1_schnorrsig_verify(vrfy, final_sig, msg, sizeof(msg), &agg_pk) == 1);
559+
560+
CHECK(secp256k1_musig_adapt(none, final_sig, pre_sig, sec_adaptor, nonce_parity) == 1);
559561
CHECK(secp256k1_schnorrsig_verify(vrfy, final_sig, msg, sizeof(msg), &agg_pk) == 1);
560562

561563
/** Secret adaptor can be extracted from signature */
@@ -647,6 +649,7 @@ void scriptless_atomic_swap(secp256k1_scratch_space *scratch) {
647649
* while the indices 0 and 1 refer to the two signers. Here signer 0 is
648650
* sending a-coins to signer 1, while signer 1 is sending b-coins to signer
649651
* 0. Signer 0 produces the adaptor signatures. */
652+
unsigned char pre_sig_a[64];
650653
unsigned char final_sig_a[64];
651654
unsigned char pre_sig_b[64];
652655
unsigned char final_sig_b[64];
@@ -733,17 +736,16 @@ void scriptless_atomic_swap(secp256k1_scratch_space *scratch) {
733736
* signature from signer 1 and adapts it. This results in a complete
734737
* signature which is broadcasted by signer 0 to take B-coins. */
735738
CHECK(secp256k1_musig_partial_sig_agg(ctx, pre_sig_b, &session_b, partial_sig_b_ptr, 2) == 1);
736-
memcpy(final_sig_b, pre_sig_b, sizeof(final_sig_b));
737-
CHECK(secp256k1_musig_adapt(ctx, final_sig_b, sec_adaptor, nonce_parity_b) == 1);
739+
CHECK(secp256k1_musig_adapt(ctx, final_sig_b, pre_sig_b, sec_adaptor, nonce_parity_b) == 1);
738740
CHECK(secp256k1_schnorrsig_verify(ctx, final_sig_b, msg32_b, sizeof(msg32_b), &agg_pk_b) == 1);
739741

740742
/* Step 6: Signer 1 signs, extracts adaptor from the published signature,
741743
* and adapts the signature to take A-coins. */
742744
CHECK(secp256k1_musig_partial_sign(ctx, &partial_sig_a[1], &secnonce_a[1], &keypair_a[1], &keyagg_cache_a, &session_a) == 1);
743-
CHECK(secp256k1_musig_partial_sig_agg(ctx, final_sig_a, &session_a, partial_sig_a_ptr, 2) == 1);
745+
CHECK(secp256k1_musig_partial_sig_agg(ctx, pre_sig_a, &session_a, partial_sig_a_ptr, 2) == 1);
744746
CHECK(secp256k1_musig_extract_adaptor(ctx, sec_adaptor_extracted, final_sig_b, pre_sig_b, nonce_parity_b) == 1);
745747
CHECK(memcmp(sec_adaptor_extracted, sec_adaptor, sizeof(sec_adaptor)) == 0); /* in real life we couldn't check this, of course */
746-
CHECK(secp256k1_musig_adapt(ctx, final_sig_a, sec_adaptor_extracted, nonce_parity_a) == 1);
748+
CHECK(secp256k1_musig_adapt(ctx, final_sig_a, pre_sig_a, sec_adaptor_extracted, nonce_parity_a) == 1);
747749
CHECK(secp256k1_schnorrsig_verify(ctx, final_sig_a, msg32_a, sizeof(msg32_a), &agg_pk_a) == 1);
748750
}
749751

src/valgrind_ctime_test.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ void run_tests(secp256k1_context *ctx, unsigned char *key) {
304304
VALGRIND_MAKE_MEM_DEFINED(pre_sig, sizeof(pre_sig));
305305

306306
CHECK(secp256k1_musig_nonce_parity(ctx, &nonce_parity, &session));
307-
memcpy(sig, pre_sig, sizeof(pre_sig));
308-
ret = secp256k1_musig_adapt(ctx, sig, sec_adaptor, nonce_parity);
307+
ret = secp256k1_musig_adapt(ctx, sig, pre_sig, sec_adaptor, nonce_parity);
309308
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
310309
CHECK(ret == 1);
311310
ret = secp256k1_musig_extract_adaptor(ctx, sec_adaptor, sig, pre_sig, nonce_parity);

0 commit comments

Comments
 (0)