Skip to content

Commit

Permalink
Adapting tests for Error types on progress
Browse files Browse the repository at this point in the history
  • Loading branch information
curiecrypt committed Nov 21, 2022
1 parent dbdaa6e commit 0d68221
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 51 deletions.
78 changes: 39 additions & 39 deletions tests/functiontest.c
Expand Up @@ -14,19 +14,19 @@ TEST (musig2, valid_signature) {

// Init signers, store public keys, generate batch commitments for `NR_SIGNERS`.
err = init_musig2(serialized_pk_list, serialized_batch_list, mcs_list, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Aggregate public keys and batch commitments.
err = aggregate_pk_batch(serialized_pk_list, serialized_batch_list, mcs_list);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Generate partial signatures for `less_signers`.
err = sign_partial(mcs_list, mps, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Aggregate partial signatures for `NR_SIGNERS`..
err = musig2_aggregate_partial_sig(mps, signature, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

err = musig2_ver_musig(ctx, signature, mcs_list[0].mc.aggr_pk, MSG_1, MSG_1_LEN);
ASSERT_EQ(err, 1);
Expand All @@ -45,19 +45,19 @@ TEST (musig2, not_enough_signatures) {

// Init signers, store public keys, generate batch commitments for `NR_SIGNERS`.
err = init_musig2(serialized_pk_list, serialized_batch_list, mcs_list, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Aggregate public keys and batch commitments.
err = aggregate_pk_batch(serialized_pk_list, serialized_batch_list, mcs_list);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Generate partial signatures for `less_signers`.
err = sign_partial(mcs_list, mps, less_signers);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Aggregate partial signatures for `NR_SIGNERS`..
err = musig2_aggregate_partial_sig(mps, signature, less_signers);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

err = musig2_ver_musig(ctx, signature, mcs_list[0].mc.aggr_pk, MSG_1, MSG_1_LEN);
ASSERT_EQ(err, 0);
Expand All @@ -76,19 +76,19 @@ TEST (musig2, non_corresponding_signers) {

// Init signers, store public keys, create batch commitments for `nr_participants`.
err = init_musig2(serialized_pk_list, serialized_batch_list, mcs_list, nr_participants);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Aggregate public keys and batch commitments for `mcs_list[1], ..., mcs_list[NR_SIGNERS]`.
err = aggregate_pk_batch(serialized_pk_list, serialized_batch_list, &mcs_list[1]);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Generate partial signatures for `mcs_list[1], ..., mcs_list[NR_SIGNERS]`.
err = sign_partial(&mcs_list[1], mps, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Aggregate partial signatures for ``mcs_list[0], ..., mcs_list[NR_SIGNERS - 1]`.
err = musig2_aggregate_partial_sig(mps, signature, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Verify the aggregated signature with secp256k1_schnorrsig_verify
// Verification should fail since the aggregated signature does not correspond to the aggregated public key.
Expand All @@ -111,14 +111,14 @@ TEST (musig2, incorrect_aggregated_commitment_of_nonces) {

// Init signers, store public keys, generate batch commitments for `NR_SIGNERS`.
err = init_musig2(serialized_pk_list, serialized_batch_list, mcs_list, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

err = aggregate_pk_batch(serialized_pk_list, serialized_batch_list, mcs_list);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Aggregate public keys and batch commitments.
err = sign_partial(mcs_list, mps, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Modify one of the aggregated commitment of nonce of one of the signers.

Expand All @@ -127,7 +127,7 @@ TEST (musig2, incorrect_aggregated_commitment_of_nonces) {

// Aggregation of partial signatures should fail since one of the signatures have incorrect aggregated commitment of nonce.
err = musig2_aggregate_partial_sig(mps, signature, NR_SIGNERS);
ASSERT_EQ(err, -1);
ASSERT_EQ(err, MUSIG2_ERR_CMP_R);

// Verify the aggregated signature with secp256k1_schnorrsig_verify
// Verification should fail because the aggregation is not complete.
Expand All @@ -153,16 +153,16 @@ TEST (musig2, previous_state) {
// Musig2 proceeds as it is supposed to do for the first state.

err = init_musig2(serialized_pk_list, serialized_batch_list, mcs_list, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

err = aggregate_pk_batch(serialized_pk_list, serialized_batch_list, mcs_list);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

err = sign_partial(mcs_list, mps1, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

err = musig2_aggregate_partial_sig(mps1, signature1, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

err = musig2_ver_musig(ctx, signature1, mcs_list[0].mc.aggr_pk, MSG_1, MSG_1_LEN);
ASSERT_EQ(err, 1);
Expand All @@ -175,18 +175,18 @@ TEST (musig2, previous_state) {

// Signature generation should fail for the incorrect state.
err = musig2_sign(&mcs_list[0], &mps2[0], MSG_2, MSG_2_LEN);
ASSERT_EQ(err, -1);
ASSERT_EQ(err, MUSIG2_ERR_CHECK_COMM);

// The rest of the signers generate their partial signatures.
for (i = 1; i < NR_SIGNERS; i++) {
/* Generate the partial signatures */
err = musig2_sign(&mcs_list[i], &mps2[i], MSG_2, MSG_2_LEN);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);
}

// Aggregation should fail.
err = musig2_aggregate_partial_sig(mps2, signature2, NR_SIGNERS);
ASSERT_EQ(err, -1);
ASSERT_EQ(err, MUSIG2_ERR_CMP_R);

// Verification should fail.
err = musig2_ver_musig(ctx, signature2, mcs_list[0].mc.aggr_pk, MSG_2, MSG_2_LEN);
Expand All @@ -206,18 +206,18 @@ TEST (musig2, future_state) {
unsigned char serialized_batch_list[NR_MESSAGES * NR_SIGNERS * V * MUSIG2_PUBKEY_BYTES_COMPRESSED];

err = init_musig2(serialized_pk_list, serialized_batch_list, mcs_list, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

err = aggregate_pk_batch(serialized_pk_list, serialized_batch_list, mcs_list);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// One of the signers will sign for a future state.
mcs_list[0].state = 1;
err = sign_partial(mcs_list, mps, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

err = musig2_aggregate_partial_sig(mps, signature, NR_SIGNERS);
ASSERT_EQ(err, -1);
ASSERT_EQ(err, MUSIG2_ERR_CMP_R);

// Verification should fail since one of the signers' signature used a future state.
err = musig2_ver_musig(ctx, signature, mcs_list[0].mc.aggr_pk, MSG_1, MSG_1_LEN);
Expand All @@ -236,18 +236,18 @@ TEST (musig2, invalid_signer_key) {
unsigned char serialized_batch_list[NR_MESSAGES * NR_SIGNERS * V * MUSIG2_PUBKEY_BYTES_COMPRESSED];

err = init_musig2(serialized_pk_list, serialized_batch_list, mcs_list, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

err = aggregate_pk_batch(serialized_pk_list, serialized_batch_list, mcs_list);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Flip a bit of a signer's keypair.
mcs_list[0].keypair.data[31] ^= 1;
err = sign_partial(mcs_list, mps, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

err = musig2_aggregate_partial_sig(mps, signature, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Verification should fail since one of the signers' key is incorrect.
err = musig2_ver_musig(ctx, signature, mcs_list[0].mc.aggr_pk, MSG_1, MSG_1_LEN);
Expand All @@ -266,19 +266,19 @@ TEST (musig2, invalid_single_signature) {
unsigned char serialized_batch_list[NR_MESSAGES * NR_SIGNERS * V * MUSIG2_PUBKEY_BYTES_COMPRESSED];

err = init_musig2(serialized_pk_list, serialized_batch_list, mcs_list, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

err = aggregate_pk_batch(serialized_pk_list, serialized_batch_list, mcs_list);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

err = sign_partial(mcs_list, mps, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Flip a bit of a single signature.
mps[0].sig[0] ^= 1;

err = musig2_aggregate_partial_sig(mps, signature, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Verification should fail since one of the single signatures is incorrect.
err = musig2_ver_musig(ctx, signature, mcs_list[0].mc.aggr_pk, MSG_1, MSG_1_LEN);
Expand All @@ -297,18 +297,18 @@ TEST (musig2, aggregate_invalid_public_key) {
unsigned char serialized_batch_list[NR_MESSAGES * NR_SIGNERS * V * MUSIG2_PUBKEY_BYTES_COMPRESSED];

err = init_musig2(serialized_pk_list, serialized_batch_list, mcs_list, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Flip a bit of one of the signers' public key.
serialized_pk_list[0] ^= 1;
err = aggregate_pk_batch(serialized_pk_list, serialized_batch_list, mcs_list);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

err = sign_partial(mcs_list, mps, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

err = musig2_aggregate_partial_sig(mps, signature, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

// Verification should fail since one of the signers' public key is incorrect.
err = musig2_ver_musig(ctx, signature, mcs_list[0].mc.aggr_pk, MSG_1, MSG_1_LEN);
Expand Down
4 changes: 2 additions & 2 deletions tests/serdetest.c
Expand Up @@ -16,7 +16,7 @@ TEST (musig2, pk_list_serialize_deserialize) {
secp256k1_pubkey deser_pk_list[NR_SIGNERS];

err = init_musig2(serialized_pk_list, serialized_batch_list, mcs_list, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

for (i = 0; i < NR_SIGNERS; i++)
ASSERT_EQ(secp256k1_ec_pubkey_parse(ctx, &deser_pk_list[i], &serialized_pk_list[i * ser_size], ser_size), 1);
Expand All @@ -42,7 +42,7 @@ TEST (musig2, commitments_serialize_deserialize) {
secp256k1_pubkey deser_batch_list[NR_SIGNERS * V * NR_MESSAGES];

err = init_musig2(serialized_pk_list, serialized_batch_list, mcs_list, NR_SIGNERS);
ASSERT_EQ(err, 1);
ASSERT_EQ(err, MUSIG2_OK);

for (i = 0; i < NR_SIGNERS * V * NR_MESSAGES; i++)
ASSERT_EQ(secp256k1_ec_pubkey_parse(ctx, &deser_batch_list[i], &serialized_batch_list[i * ser_size], ser_size), 1);
Expand Down
20 changes: 10 additions & 10 deletions tests/testmusig2.c
Expand Up @@ -21,7 +21,7 @@ int init_musig2(unsigned char *serialized_pk_list, unsigned char *serialized_bat

err = musig2_serialise_shareable_context(&mcs_list[i], serialized_pubkey, serialized_comm_list);
if (err != 1)
return err;
return -1;

memcpy(&serialized_pk_list[i * MUSIG2_PUBKEY_BYTES_COMPRESSED], serialized_pubkey, MUSIG2_PUBKEY_BYTES_COMPRESSED);
l = 0; // the index of the signer's commitment list.
Expand All @@ -30,31 +30,31 @@ int init_musig2(unsigned char *serialized_pk_list, unsigned char *serialized_bat
memcpy(&serialized_batch_list[(k * NR_SIGNERS * V + i * V + j) * MUSIG2_PUBKEY_BYTES_COMPRESSED], serialized_comm_list[l],
MUSIG2_PUBKEY_BYTES_COMPRESSED);
}
return 1;
return MUSIG2_OK;
}

int aggregate_pk_batch(unsigned char *serialized_pk_list, unsigned char *serialized_batch_list, musig2_context_sig *mcs_list) {
/**** Aggregate the public keys and batch commitments for each signer ****/
int i;
int cnt = 0;
for (i = 0; i < NR_SIGNERS; i++)
cnt += musig2_signer_precomputation(&mcs_list[i].mc, serialized_pk_list, serialized_batch_list, NR_SIGNERS, NR_MESSAGES);
if (cnt != NR_SIGNERS) {
return cnt;
int err = 0;
for (i = 0; i < NR_SIGNERS; i++){
err = musig2_signer_precomputation(&mcs_list[i].mc, serialized_pk_list, serialized_batch_list, NR_SIGNERS, NR_MESSAGES);
if (err != MUSIG2_OK)
return err;
}
return 1;
return MUSIG2_OK;
}

int sign_partial(musig2_context_sig *mcs_list, musig2_partial_signature *mps, int nr_participants) {
int i, err;
for (i = 0; i < nr_participants; i++) {
/* Generate the partial signatures */
err = musig2_sign(&mcs_list[i], &mps[i], MSG_1, MSG_1_LEN);
if (err != 1) {
if (err != MUSIG2_OK) {
return err;
}
}
return 1;
return MUSIG2_OK;
}

int musig2_ver_musig(secp256k1_context *ctx, const unsigned char *signature, secp256k1_pubkey aggr_pk , const unsigned char *msg, int msg_len ){
Expand Down

0 comments on commit 0d68221

Please sign in to comment.