Skip to content

Commit

Permalink
Add static test vector for MuSig key aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasnick committed Jan 14, 2021
1 parent 87be1bf commit f6e0bb3
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 106 deletions.
41 changes: 41 additions & 0 deletions src/modules/musig/tests_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,46 @@ void musig_tweak_test(secp256k1_scratch_space *scratch) {
musig_tweak_test_helper(&Q_xonly, sk[0], sk[1], &pre_session_Q);
}

void musig_test_vectors(void) {
secp256k1_xonly_pubkey combined_pk;
unsigned char combined_pk_ser[32];
secp256k1_xonly_pubkey pk[2];
const unsigned char pk_ser1[32] = {
0xF9, 0x30, 0x8A, 0x01, 0x92, 0x58, 0xC3, 0x10,
0x49, 0x34, 0x4F, 0x85, 0xF8, 0x9D, 0x52, 0x29,
0xB5, 0x31, 0xC8, 0x45, 0x83, 0x6F, 0x99, 0xB0,
0x86, 0x01, 0xF1, 0x13, 0xBC, 0xE0, 0x36, 0xF9
};
const unsigned char pk_ser2[32] = {
0xDF, 0xF1, 0xD7, 0x7F, 0x2A, 0x67, 0x1C, 0x5F,
0x36, 0x18, 0x37, 0x26, 0xDB, 0x23, 0x41, 0xBE,
0x58, 0xFE, 0xAE, 0x1D, 0xA2, 0xDE, 0xCE, 0xD8,
0x43, 0x24, 0x0F, 0x7B, 0x50, 0x2B, 0xA6, 0x59
};
const unsigned char combined_pk_expected[32] = {
0xDF, 0x11, 0x92, 0x2A, 0x8F, 0x7A, 0x5D, 0x07,
0xC4, 0x29, 0x41, 0x75, 0x01, 0xBF, 0xE9, 0xA7,
0x5B, 0xD7, 0xE7, 0xB8, 0x0C, 0x21, 0x41, 0x13,
0x27, 0x31, 0x6E, 0x9F, 0x10, 0x7E, 0xF7, 0xD2,
};
CHECK(secp256k1_xonly_pubkey_parse(ctx, &pk[0], pk_ser1));
CHECK(secp256k1_xonly_pubkey_parse(ctx, &pk[1], pk_ser2));
CHECK(secp256k1_musig_pubkey_combine(ctx, NULL, &combined_pk, NULL, pk, 2) == 1);
CHECK(secp256k1_xonly_pubkey_serialize(ctx, combined_pk_ser, &combined_pk));
/* TODO: remove */
/* int i, j; */
/* printf("const unsigned char combined_pk_expected[32] = {\n"); */
/* for (i = 0; i < 4; i++) { */
/* printf(" "); */
/* for (j = 0; j < 8; j++) { */
/* printf("0x%02X, ", combined_pk_ser[i*8+j]); */
/* } */
/* printf("\n"); */
/* } */
/* printf("};\n"); */
CHECK(memcmp(combined_pk_ser, combined_pk_expected, sizeof(combined_pk_ser)) == 0);
}

void run_musig_tests(void) {
int i;
secp256k1_scratch_space *scratch = secp256k1_scratch_space_create(ctx, 1024 * 1024);
Expand All @@ -961,6 +1001,7 @@ void run_musig_tests(void) {
scriptless_atomic_swap(scratch);
musig_tweak_test(scratch);
}
musig_test_vectors();
sha256_tag_test();

secp256k1_scratch_space_destroy(ctx, scratch);
Expand Down
212 changes: 106 additions & 106 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -5909,117 +5909,117 @@ int main(int argc, char **argv) {
CHECK(secp256k1_context_randomize(ctx, secp256k1_testrand_bits(1) ? rand32 : NULL));
}

run_rand_bits();
run_rand_int();
run_util_tests();

run_sha256_tests();
run_hmac_sha256_tests();
run_rfc6979_hmac_sha256_tests();

#ifndef USE_NUM_NONE
/* num tests */
run_num_smalltests();
#endif

/* scalar tests */
run_scalar_tests();

/* field tests */
run_field_inv();
run_field_inv_var();
run_field_inv_all_var();
run_field_misc();
run_field_convert();
run_sqr();
run_sqrt();

/* group tests */
run_ge();
run_group_decompress();

/* ecmult tests */
run_wnaf();
run_point_times_order();
run_ecmult_near_split_bound();
run_ecmult_chain();
run_ecmult_constants();
run_ecmult_gen_blind();
run_ecmult_const_tests();
run_ecmult_multi_tests();
run_ec_combine();
run_ec_commit();

/* endomorphism tests */
run_endomorphism_tests();

/* EC point parser test */
run_ec_pubkey_parse_test();

/* EC key edge cases */
run_eckey_edge_case_test();

/* EC key arithmetic test */
run_eckey_negate_test();

#ifdef ENABLE_MODULE_ECDH
/* ecdh tests */
run_ecdh_tests();
#endif
/* run_rand_bits(); */
/* run_rand_int(); */
/* run_util_tests(); */

/* run_sha256_tests(); */
/* run_hmac_sha256_tests(); */
/* run_rfc6979_hmac_sha256_tests(); */

/* #ifndef USE_NUM_NONE */
/* /\* num tests *\/ */
/* run_num_smalltests(); */
/* #endif */

/* /\* scalar tests *\/ */
/* run_scalar_tests(); */

/* /\* field tests *\/ */
/* run_field_inv(); */
/* run_field_inv_var(); */
/* run_field_inv_all_var(); */
/* run_field_misc(); */
/* run_field_convert(); */
/* run_sqr(); */
/* run_sqrt(); */

/* /\* group tests *\/ */
/* run_ge(); */
/* run_group_decompress(); */

/* /\* ecmult tests *\/ */
/* run_wnaf(); */
/* run_point_times_order(); */
/* run_ecmult_near_split_bound(); */
/* run_ecmult_chain(); */
/* run_ecmult_constants(); */
/* run_ecmult_gen_blind(); */
/* run_ecmult_const_tests(); */
/* run_ecmult_multi_tests(); */
/* run_ec_combine(); */
/* run_ec_commit(); */

/* /\* endomorphism tests *\/ */
/* run_endomorphism_tests(); */

/* /\* EC point parser test *\/ */
/* run_ec_pubkey_parse_test(); */

/* /\* EC key edge cases *\/ */
/* run_eckey_edge_case_test(); */

/* /\* EC key arithmetic test *\/ */
/* run_eckey_negate_test(); */

/* #ifdef ENABLE_MODULE_ECDH */
/* /\* ecdh tests *\/ */
/* run_ecdh_tests(); */
/* #endif */

#ifdef ENABLE_MODULE_MUSIG
run_musig_tests();
#endif

/* ecdsa tests */
run_random_pubkeys();
run_ecdsa_der_parse();
run_ecdsa_sign_verify();
run_ecdsa_end_to_end();
run_ecdsa_edge_cases();
#ifdef ENABLE_OPENSSL_TESTS
run_ecdsa_openssl();
#endif

#ifdef ENABLE_MODULE_RECOVERY
/* ECDSA pubkey recovery tests */
run_recovery_tests();
#endif

#ifdef ENABLE_MODULE_GENERATOR
run_generator_tests();
#endif

#ifdef ENABLE_MODULE_RANGEPROOF
run_rangeproof_tests();
#endif

#ifdef ENABLE_MODULE_WHITELIST
/* Key whitelisting tests */
run_whitelist_tests();
#endif

#ifdef ENABLE_MODULE_SURJECTIONPROOF
run_surjection_tests();
#endif

#ifdef ENABLE_MODULE_EXTRAKEYS
run_extrakeys_tests();
#endif

#ifdef ENABLE_MODULE_SCHNORRSIG
run_schnorrsig_tests();
#endif

#ifdef ENABLE_MODULE_ECDSA_S2C
/* ECDSA sign to contract */
run_ecdsa_s2c_tests();
#endif

/* util tests */
run_secp256k1_memczero_test();

run_cmov_tests();
/* /\* ecdsa tests *\/ */
/* run_random_pubkeys(); */
/* run_ecdsa_der_parse(); */
/* run_ecdsa_sign_verify(); */
/* run_ecdsa_end_to_end(); */
/* run_ecdsa_edge_cases(); */
/* #ifdef ENABLE_OPENSSL_TESTS */
/* run_ecdsa_openssl(); */
/* #endif */

/* #ifdef ENABLE_MODULE_RECOVERY */
/* /\* ECDSA pubkey recovery tests *\/ */
/* run_recovery_tests(); */
/* #endif */

/* #ifdef ENABLE_MODULE_GENERATOR */
/* run_generator_tests(); */
/* #endif */

/* #ifdef ENABLE_MODULE_RANGEPROOF */
/* run_rangeproof_tests(); */
/* #endif */

/* #ifdef ENABLE_MODULE_WHITELIST */
/* /\* Key whitelisting tests *\/ */
/* run_whitelist_tests(); */
/* #endif */

/* #ifdef ENABLE_MODULE_SURJECTIONPROOF */
/* run_surjection_tests(); */
/* #endif */

/* #ifdef ENABLE_MODULE_EXTRAKEYS */
/* run_extrakeys_tests(); */
/* #endif */

/* #ifdef ENABLE_MODULE_SCHNORRSIG */
/* run_schnorrsig_tests(); */
/* #endif */

/* #ifdef ENABLE_MODULE_ECDSA_S2C */
/* /\* ECDSA sign to contract *\/ */
/* run_ecdsa_s2c_tests(); */
/* #endif */

/* /\* util tests *\/ */
/* run_secp256k1_memczero_test(); */

/* run_cmov_tests(); */

secp256k1_testrand_finish();

Expand Down

0 comments on commit f6e0bb3

Please sign in to comment.