Skip to content

Commit 174e6ca

Browse files
committed
f fix state machine test and improve documentation
1 parent 7bf5cf1 commit 174e6ca

1 file changed

Lines changed: 40 additions & 19 deletions

File tree

src/modules/musig/tests_impl.h

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,14 @@ void musig_api_tests(secp256k1_scratch_space *scratch) {
361361
secp256k1_context_destroy(vrfy);
362362
}
363363

364-
/* Returns the messagehash of a session where get_public_nonce was called with different signers than initialized */
365-
int musig_state_machine_diff_signer_test(unsigned char *msghash, secp256k1_pubkey *pks, secp256k1_pubkey *combined_pk, unsigned char *pk_hash, const unsigned char * const *nonce_commitments, unsigned char *msg, secp256k1_pubkey *nonce_other, unsigned char *sk, unsigned char *session_id) {
364+
/* Initializes two sessions, one use the given parameters (session_id,
365+
* nonce_commitments, etc.) except that `session_tmp` uses new signers with different
366+
* public keys. The point of this test is to call `musig_session_get_public_nonce`
367+
* with signers from `session_tmp` who have different public keys than the correct
368+
* ones and return the resulting messagehash. This should not result in a different
369+
* messagehash because the public keys of the signers are only used during session
370+
* initialization. */
371+
int musig_state_machine_diff_signer_msghash_test(unsigned char *msghash, secp256k1_pubkey *pks, secp256k1_pubkey *combined_pk, unsigned char *pk_hash, const unsigned char * const *nonce_commitments, unsigned char *msg, secp256k1_pubkey *nonce_other, unsigned char *sk, unsigned char *session_id) {
366372
secp256k1_musig_session session;
367373
secp256k1_musig_session session_tmp;
368374
unsigned char nonce_commitment[32];
@@ -383,20 +389,26 @@ int musig_state_machine_diff_signer_test(unsigned char *msghash, secp256k1_pubke
383389

384390
CHECK(secp256k1_musig_session_initialize(ctx, &session, signers, nonce_commitment, session_id, msg, combined_pk, pk_hash, pks, 2, 0, sk) == 1);
385391
CHECK(memcmp(nonce_commitment, nonce_commitments[1], 32) == 0);
386-
CHECK(secp256k1_musig_session_get_public_nonce(ctx, &session, signers, &nonce, nonce_commitments, 2) == 1);
392+
/* Call get_public_nonce with different signers than the signers the session was
393+
* initialized with. */
394+
CHECK(secp256k1_musig_session_get_public_nonce(ctx, &session_tmp, signers, &nonce, nonce_commitments, 2) == 1);
395+
CHECK(secp256k1_musig_session_get_public_nonce(ctx, &session, signers_tmp, &nonce, nonce_commitments, 2) == 1);
387396
CHECK(secp256k1_musig_set_nonce(ctx, &signers[0], nonce_other) == 1);
388397
CHECK(secp256k1_musig_set_nonce(ctx, &signers[1], &nonce) == 1);
389398
CHECK(secp256k1_musig_session_combine_nonces(ctx, &session, signers, 2, NULL, NULL) == 1);
390399

391400
return secp256k1_musig_compute_messagehash(ctx, msghash, &session);
392401
}
393402

394-
/* Creates a new session and tries to combine nonces with given signers_other
395-
*/
396-
int musig_state_machine_diff_signers_combine_nonce_test(secp256k1_pubkey *pks, secp256k1_pubkey *combined_pk, unsigned char *pk_hash, unsigned char *nonce_commitment_other, secp256k1_pubkey *nonce_other, unsigned char *msg, unsigned char *sk, secp256k1_musig_session_signer_data *signers_other) {
397-
/* Create new session (different session id) */
403+
/* Creates a new session (with a different session id) and tries to use that session
404+
* to combine nonces with given signers_other. This should fail, because the nonce
405+
* commitments of signers_other do not match the nonce commitments the new session
406+
* was initialized with. If do_test is 0, the correct signers are being used and
407+
* therefore the function should return 1. */
408+
int musig_state_machine_diff_signers_combine_nonce_test(secp256k1_pubkey *pks, secp256k1_pubkey *combined_pk, unsigned char *pk_hash, unsigned char *nonce_commitment_other, secp256k1_pubkey *nonce_other, unsigned char *msg, unsigned char *sk, secp256k1_musig_session_signer_data *signers_other, int do_test) {
398409
secp256k1_musig_session session;
399410
secp256k1_musig_session_signer_data signers[2];
411+
secp256k1_musig_session_signer_data *signers_to_use;
400412
unsigned char nonce_commitment[32];
401413
unsigned char session_id[32];
402414
secp256k1_pubkey nonce;
@@ -410,12 +422,19 @@ int musig_state_machine_diff_signers_combine_nonce_test(secp256k1_pubkey *pks, s
410422
CHECK(secp256k1_musig_session_get_public_nonce(ctx, &session, signers, &nonce, ncs, 2) == 1);
411423
CHECK(secp256k1_musig_set_nonce(ctx, &signers[0], nonce_other) == 1);
412424
CHECK(secp256k1_musig_set_nonce(ctx, &signers[1], &nonce) == 1);
413-
414-
return secp256k1_musig_session_combine_nonces(ctx, &session, signers_other, 2, NULL, NULL);
425+
CHECK(secp256k1_musig_set_nonce(ctx, &signers[1], &nonce) == 1);
426+
secp256k1_musig_session_combine_nonces(ctx, &session, signers_other, 2, NULL, NULL);
427+
if (do_test) {
428+
signers_to_use = signers_other;
429+
} else {
430+
signers_to_use = signers;
431+
}
432+
return secp256k1_musig_session_combine_nonces(ctx, &session, signers_to_use, 2, NULL, NULL);
415433
}
416434

417-
/* Initialize session with given msg and try to sign. Creates MuSig session
418-
* with a given signer and a signer with sk. Should fail if msg is NULL */
435+
/* Recreates a session with the given session_id, signers, pk, msg etc. parameters
436+
* and tries to sign and verify the other signers partial signature. Both should fail
437+
* if msg is NULL. */
419438
int musig_state_machine_missing_msg_test(secp256k1_pubkey *pks, secp256k1_pubkey *combined_pk, unsigned char *pk_hash, unsigned char *nonce_commitment_other, secp256k1_pubkey *nonce_other, secp256k1_musig_partial_signature *partial_sig_other, unsigned char *sk, unsigned char *session_id, unsigned char *msg) {
420439
secp256k1_musig_session session;
421440
secp256k1_musig_session_signer_data signers[2];
@@ -443,10 +462,10 @@ int musig_state_machine_missing_msg_test(secp256k1_pubkey *pks, secp256k1_pubkey
443462
return partial_sign || partial_verify;
444463
}
445464

446-
447-
/* If do_combine is 0, don't combine nonces before verifying and combining
448-
* partial sigs. Creates MuSig session between given signer and new signer with
449-
* sk, session_id and partial_sig. */
465+
/* Recreates a session with the given session_id, signers, pk, msg etc. parameters
466+
* and tries to verify and combine partial sigs. If do_combine is 0, the
467+
* combine_nonces step is left out. In that case verify and combine should fail and
468+
* this function should return 0. */
450469
int musig_state_machine_missing_combine_test(secp256k1_pubkey *pks, secp256k1_pubkey *combined_pk, unsigned char *pk_hash, unsigned char *nonce_commitment_other, secp256k1_pubkey *nonce_other, secp256k1_musig_partial_signature *partial_sig_other, unsigned char *msg, unsigned char *sk, unsigned char *session_id, secp256k1_musig_partial_signature *partial_sig, int do_combine) {
451470
secp256k1_musig_session session;
452471
secp256k1_musig_session_signer_data signers[2];
@@ -538,7 +557,8 @@ void musig_state_machine_tests(secp256k1_scratch_space *scratch) {
538557
CHECK(secp256k1_musig_set_nonce(ctx, &signers1[1], &nonce[1]) == 1);
539558

540559
/* Can't combine nonces from signers of a different session */
541-
CHECK(musig_state_machine_diff_signers_combine_nonce_test(pk, &combined_pk, pk_hash, nonce_commitment[0], &nonce[0], msg, sk[1], signers1) == 0);
560+
CHECK(musig_state_machine_diff_signers_combine_nonce_test(pk, &combined_pk, pk_hash, nonce_commitment[0], &nonce[0], msg, sk[1], signers1, 1) == 0);
561+
CHECK(musig_state_machine_diff_signers_combine_nonce_test(pk, &combined_pk, pk_hash, nonce_commitment[0], &nonce[0], msg, sk[1], signers1, 0) == 1);
542562

543563
/* Partially sign */
544564
CHECK(secp256k1_musig_partial_sign(ctx, &session[0], &partial_sig[0]) == 1);
@@ -547,10 +567,11 @@ void musig_state_machine_tests(secp256k1_scratch_space *scratch) {
547567
CHECK(secp256k1_musig_partial_sign(ctx, &session[1], &partial_sig[1]) == 0);
548568
CHECK(secp256k1_musig_session_combine_nonces(ctx, &session[1], signers1, 2, NULL, NULL) == 1);
549569
CHECK(secp256k1_musig_partial_sig_verify(ctx, &session[1], &signers1[0], &partial_sig[0]) == 1);
550-
/* messagehash should be the same as a session whose get_public_nonce was
551-
* called with different signers. */
570+
/* messagehash should be the same as a session whose get_public_nonce was called
571+
* with different signers (i.e. they diff in public keys). This is because the
572+
* public keys of the signers is set in stone when initializing the session. */
552573
CHECK(secp256k1_musig_compute_messagehash(ctx, msghash1, &session[1]) == 1);
553-
CHECK(musig_state_machine_diff_signer_test(msghash2, pk, &combined_pk, pk_hash, ncs, msg, &nonce[0], sk[1], session_id[1]) == 1);
574+
CHECK(musig_state_machine_diff_signer_msghash_test(msghash2, pk, &combined_pk, pk_hash, ncs, msg, &nonce[0], sk[1], session_id[1]) == 1);
554575
CHECK(memcmp(msghash1, msghash2, 32) == 0);
555576
CHECK(secp256k1_musig_partial_sign(ctx, &session[1], &partial_sig[1]) == 1);
556577
CHECK(secp256k1_musig_partial_sig_verify(ctx, &session[1], &signers1[1], &partial_sig[1]) == 1);

0 commit comments

Comments
 (0)