Skip to content

Commit

Permalink
Add new tests vectors from updated spec
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseposner committed Feb 15, 2021
1 parent 3cebf11 commit 0e2f43c
Showing 1 changed file with 226 additions and 2 deletions.
228 changes: 226 additions & 2 deletions src/modules/ecdsa_adaptor/tests_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,23 @@ void test_ecdsa_adaptor_spec_vectors_check_recover(const unsigned char *adaptor_
CHECK(secp256k1_ecdsa_signature_parse_compact(ctx, &sig, signature64) == 1);
CHECK(expected == secp256k1_ecdsa_adaptor_recover(ctx, deckey32, &sig, adaptor_sig162, &encryption_key));
if (decryption_key32 != NULL) {
CHECK(expected == !secp256k1_memcmp_var(deckey32, decryption_key32, 32));
CHECK(expected == !(secp256k1_memcmp_var(deckey32, decryption_key32, 32) & 1));
}
}

/* Helper function for test_ecdsa_adaptor_spec_vectors
* Checks deserialization and serialization. */
void test_ecdsa_adaptor_spec_vectors_check_serialization(const unsigned char *adaptor_sig162, int expected) {
unsigned char buf[162];
secp256k1_scalar dleq_proof_s, dleq_proof_e;
secp256k1_ge r, rp;
secp256k1_scalar sp;
secp256k1_scalar sigr;

CHECK(expected == secp256k1_ecdsa_adaptor_sig_deserialize(&r, &sigr, &rp, &sp, &dleq_proof_e, &dleq_proof_s, adaptor_sig162));
if (expected == 1) {
CHECK(secp256k1_ecdsa_adaptor_sig_serialize(buf, &r, &rp, &sp, &dleq_proof_e, &dleq_proof_s) == 1);
CHECK(secp256k1_memcmp_var(buf, adaptor_sig162, 162) == 0);
}
}

Expand All @@ -101,6 +117,8 @@ void test_ecdsa_adaptor_spec_vectors_check_recover(const unsigned char *adaptor_
void test_ecdsa_adaptor_spec_vectors(void) {
{
/* Test vector 0 */
/* kind: verification test */
/* plain valid adaptor signature */
const unsigned char adaptor_sig[162] = {
0x03, 0x42, 0x4d, 0x14, 0xa5, 0x47, 0x1c, 0x04,
0x8a, 0xb8, 0x7b, 0x3b, 0x83, 0xf6, 0x08, 0x5d,
Expand Down Expand Up @@ -166,6 +184,9 @@ void test_ecdsa_adaptor_spec_vectors(void) {
}
{
/* Test vector 1 */
/* verification test */
/* the decrypted signature is high so it must be negated first
* AND the extracted decryption key must be negated */
const unsigned char adaptor_sig[162] = {
0x03, 0x60, 0x35, 0xc8, 0x98, 0x60, 0xec, 0x62,
0xad, 0x15, 0x3f, 0x69, 0xb5, 0xb3, 0x07, 0x7b,
Expand Down Expand Up @@ -231,6 +252,8 @@ void test_ecdsa_adaptor_spec_vectors(void) {
}
{
/* Test vector 2 */
/* verification test */
/* proof is wrong */
const unsigned char adaptor_sig[162] = {
0x03, 0xf9, 0x4d, 0xca, 0x20, 0x6d, 0x75, 0x82,
0xc0, 0x15, 0xfb, 0x9b, 0xff, 0xe4, 0xe4, 0x3b,
Expand Down Expand Up @@ -296,6 +319,8 @@ void test_ecdsa_adaptor_spec_vectors(void) {
}
{
/* Test vector 3 */
/* recovery test */
/* plain recovery */
const unsigned char adaptor_sig[162] = {
0x03, 0xf2, 0xdb, 0x6e, 0x9e, 0xd3, 0x30, 0x92,
0xcc, 0x0b, 0x89, 0x8f, 0xd6, 0xb2, 0x82, 0xe9,
Expand Down Expand Up @@ -346,7 +371,9 @@ void test_ecdsa_adaptor_spec_vectors(void) {
test_ecdsa_adaptor_spec_vectors_check_recover(adaptor_sig, encryption_key, decryption_key, signature, 1);
}
{
/* Test vector 5 */
/* Test vector 4 */
/* recovery test */
/* the R value of the signature does not match */
const unsigned char adaptor_sig[162] = {
0x03, 0xaa, 0x86, 0xd7, 0x80, 0x59, 0xa9, 0x10,
0x59, 0xc2, 0x9e, 0xc1, 0xa7, 0x57, 0xc4, 0xdc,
Expand Down Expand Up @@ -389,6 +416,203 @@ void test_ecdsa_adaptor_spec_vectors(void) {
};
test_ecdsa_adaptor_spec_vectors_check_recover(adaptor_sig, encryption_key, NULL, signature, 0);
}
{
/* Test vector 5 */
/* recovery test */
/* recovery from high s signature */
const unsigned char adaptor_sig[162] = {
0x03, 0x2c, 0x63, 0x7c, 0xd7, 0x97, 0xdd, 0x8c,
0x2c, 0xe2, 0x61, 0x90, 0x7e, 0xd4, 0x3e, 0x82,
0xd6, 0xd1, 0xa4, 0x8c, 0xba, 0xbb, 0xbe, 0xce,
0x80, 0x11, 0x33, 0xdd, 0x8d, 0x70, 0xa0, 0x1b,
0x14, 0x03, 0xeb, 0x61, 0x5a, 0x3e, 0x59, 0xb1,
0xcb, 0xbf, 0x4f, 0x87, 0xac, 0xaf, 0x64, 0x5b,
0xe1, 0xed, 0xa3, 0x2a, 0x06, 0x66, 0x11, 0xf3,
0x5d, 0xd5, 0x55, 0x78, 0x02, 0x80, 0x2b, 0x14,
0xb1, 0x9c, 0x81, 0xc0, 0x4c, 0x3f, 0xef, 0xac,
0x57, 0x83, 0xb2, 0x07, 0x7b, 0xd4, 0x3f, 0xa0,
0xa3, 0x9a, 0xb8, 0xa6, 0x4d, 0x4d, 0x78, 0x33,
0x2a, 0x5d, 0x62, 0x1e, 0xa2, 0x3e, 0xca, 0x46,
0xbc, 0x01, 0x10, 0x11, 0xab, 0x82, 0xdd, 0xa6,
0xde, 0xb8, 0x56, 0x99, 0xf5, 0x08, 0x74, 0x4d,
0x70, 0xd4, 0x13, 0x4b, 0xea, 0x03, 0xf7, 0x84,
0xd2, 0x85, 0xb5, 0xc6, 0xc1, 0x5a, 0x56, 0xe4,
0xe1, 0xfa, 0xb4, 0xbc, 0x35, 0x6a, 0xbb, 0xde,
0xbb, 0x3b, 0x8f, 0xe1, 0xe5, 0x5e, 0x6d, 0xd6,
0xd2, 0xa9, 0xea, 0x45, 0x7e, 0x91, 0xb2, 0xe6,
0x64, 0x2f, 0xae, 0x69, 0xf9, 0xdb, 0xb5, 0x25,
0x88, 0x54
};
const unsigned char encryption_key[33] = {
0x02, 0x04, 0x25, 0x37, 0xe9, 0x13, 0xad, 0x74,
0xc4, 0xbb, 0xd8, 0xda, 0x96, 0x07, 0xad, 0x3b,
0x9c, 0xb2, 0x97, 0xd0, 0x8e, 0x01, 0x4a, 0xfc,
0x51, 0x13, 0x30, 0x83, 0xf1, 0xbd, 0x68, 0x7a,
0x62
};
const unsigned char decryption_key[32] = {
0x32, 0x47, 0x19, 0xb5, 0x1f, 0xf2, 0x47, 0x4c,
0x94, 0x38, 0xeb, 0x76, 0x49, 0x4b, 0x0d, 0xc0,
0xbc, 0xce, 0xeb, 0x52, 0x9f, 0x0a, 0x54, 0x28,
0xfd, 0x19, 0x8a, 0xd8, 0xf8, 0x86, 0xe9, 0x9c
};
const unsigned char signature[64] = {
0x2c, 0x63, 0x7c, 0xd7, 0x97, 0xdd, 0x8c, 0x2c,
0xe2, 0x61, 0x90, 0x7e, 0xd4, 0x3e, 0x82, 0xd6,
0xd1, 0xa4, 0x8c, 0xba, 0xbb, 0xbe, 0xce, 0x80,
0x11, 0x33, 0xdd, 0x8d, 0x70, 0xa0, 0x1b, 0x14,
0xb5, 0xf2, 0x43, 0x21, 0xf5, 0x50, 0xb7, 0xb9,
0xdd, 0x06, 0xee, 0x4f, 0xcf, 0xd8, 0x2b, 0xda,
0xd8, 0xb1, 0x42, 0xff, 0x93, 0xa7, 0x90, 0xcc,
0x4d, 0x9f, 0x79, 0x62, 0xb3, 0x8c, 0x6a, 0x3b
};
test_ecdsa_adaptor_spec_vectors_check_decrypt(adaptor_sig, decryption_key, signature, 0);
test_ecdsa_adaptor_spec_vectors_check_recover(adaptor_sig, encryption_key, decryption_key, signature, 1);
}
{
/* Test vector 6 */
/* serialization test */
const unsigned char adaptor_sig[162] = {
0x03, 0xe6, 0xd5, 0x1d, 0xa7, 0xbc, 0x2b, 0xf2,
0x4c, 0xf9, 0xdf, 0xd9, 0xac, 0xc6, 0xc4, 0xf0,
0xa3, 0xe7, 0x4d, 0x8a, 0x62, 0x73, 0xee, 0x5a,
0x57, 0x3e, 0xd6, 0x81, 0x8e, 0x30, 0x95, 0xb6,
0x09, 0x03, 0xf3, 0x3b, 0xc9, 0x8f, 0x9d, 0x2e,
0xa3, 0x51, 0x1f, 0x2e, 0x24, 0xf3, 0x35, 0x85,
0x57, 0xc8, 0x15, 0xab, 0xd7, 0x71, 0x3c, 0x93,
0x18, 0xaf, 0x9f, 0x4d, 0xfa, 0xb4, 0x44, 0x18,
0x98, 0xec, 0xd6, 0x19, 0xac, 0xb1, 0xcb, 0x75,
0xc1, 0xa5, 0x94, 0x6f, 0xba, 0xf7, 0x16, 0xd2,
0x27, 0x19, 0x9a, 0x64, 0x79, 0xa6, 0x78, 0xd1,
0x0a, 0x6d, 0x95, 0x51, 0x2d, 0x67, 0x4f, 0xb7,
0x70, 0x3d, 0x85, 0xb5, 0x89, 0x80, 0xb8, 0xe6,
0xc5, 0x4b, 0xd2, 0x06, 0x16, 0xbd, 0xb9, 0x46,
0x1d, 0xcc, 0xd8, 0xee, 0xbb, 0x7d, 0x7e, 0x7c,
0x83, 0xa9, 0x14, 0x52, 0xcc, 0x20, 0xed, 0xf5,
0x3b, 0xe5, 0xb0, 0xfe, 0x0d, 0xb4, 0x4d, 0xdd,
0xaa, 0xaf, 0xbe, 0x73, 0x76, 0x78, 0xc6, 0x84,
0xb6, 0xe8, 0x9b, 0x9b, 0x4b, 0x67, 0x9b, 0x18,
0x55, 0xaa, 0x6e, 0xd6, 0x44, 0x49, 0x8b, 0x89,
0xc9, 0x18
};
test_ecdsa_adaptor_spec_vectors_check_serialization(adaptor_sig, 1);
}
{
/* Test vector 7 */
/* serialization test */
/* R can be above curve order */
const unsigned char adaptor_sig[162] = {
0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc,
0x2c, 0x03, 0xf3, 0x3b, 0xc9, 0x8f, 0x9d, 0x2e,
0xa3, 0x51, 0x1f, 0x2e, 0x24, 0xf3, 0x35, 0x85,
0x57, 0xc8, 0x15, 0xab, 0xd7, 0x71, 0x3c, 0x93,
0x18, 0xaf, 0x9f, 0x4d, 0xfa, 0xb4, 0x44, 0x18,
0x98, 0xec, 0xd6, 0x19, 0xac, 0xb1, 0xcb, 0x75,
0xc1, 0xa5, 0x94, 0x6f, 0xba, 0xf7, 0x16, 0xd2,
0x27, 0x19, 0x9a, 0x64, 0x79, 0xa6, 0x78, 0xd1,
0x0a, 0x6d, 0x95, 0x51, 0x2d, 0x67, 0x4f, 0xb7,
0x70, 0x3d, 0x85, 0xb5, 0x89, 0x80, 0xb8, 0xe6,
0xc5, 0x4b, 0xd2, 0x06, 0x16, 0xbd, 0xb9, 0x46,
0x1d, 0xcc, 0xd8, 0xee, 0xbb, 0x7d, 0x7e, 0x7c,
0x83, 0xa9, 0x14, 0x52, 0xcc, 0x20, 0xed, 0xf5,
0x3b, 0xe5, 0xb0, 0xfe, 0x0d, 0xb4, 0x4d, 0xdd,
0xaa, 0xaf, 0xbe, 0x73, 0x76, 0x78, 0xc6, 0x84,
0xb6, 0xe8, 0x9b, 0x9b, 0x4b, 0x67, 0x9b, 0x18,
0x55, 0xaa, 0x6e, 0xd6, 0x44, 0x49, 0x8b, 0x89,
0xc9, 0x18
};
test_ecdsa_adaptor_spec_vectors_check_serialization(adaptor_sig, 1);
}
{
/* Test vector 8 */
/* serialization test */
/* R_a can be above curve order */
const unsigned char adaptor_sig[162] = {
0x03, 0xe6, 0xd5, 0x1d, 0xa7, 0xbc, 0x2b, 0xf2,
0x4c, 0xf9, 0xdf, 0xd9, 0xac, 0xc6, 0xc4, 0xf0,
0xa3, 0xe7, 0x4d, 0x8a, 0x62, 0x73, 0xee, 0x5a,
0x57, 0x3e, 0xd6, 0x81, 0x8e, 0x30, 0x95, 0xb6,
0x09, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff,
0xfc, 0x2c, 0xd6, 0x19, 0xac, 0xb1, 0xcb, 0x75,
0xc1, 0xa5, 0x94, 0x6f, 0xba, 0xf7, 0x16, 0xd2,
0x27, 0x19, 0x9a, 0x64, 0x79, 0xa6, 0x78, 0xd1,
0x0a, 0x6d, 0x95, 0x51, 0x2d, 0x67, 0x4f, 0xb7,
0x70, 0x3d, 0x85, 0xb5, 0x89, 0x80, 0xb8, 0xe6,
0xc5, 0x4b, 0xd2, 0x06, 0x16, 0xbd, 0xb9, 0x46,
0x1d, 0xcc, 0xd8, 0xee, 0xbb, 0x7d, 0x7e, 0x7c,
0x83, 0xa9, 0x14, 0x52, 0xcc, 0x20, 0xed, 0xf5,
0x3b, 0xe5, 0xb0, 0xfe, 0x0d, 0xb4, 0x4d, 0xdd,
0xaa, 0xaf, 0xbe, 0x73, 0x76, 0x78, 0xc6, 0x84,
0xb6, 0xe8, 0x9b, 0x9b, 0x4b, 0x67, 0x9b, 0x18,
0x55, 0xaa, 0x6e, 0xd6, 0x44, 0x49, 0x8b, 0x89,
0xc9, 0x18
};
test_ecdsa_adaptor_spec_vectors_check_serialization(adaptor_sig, 1);
}
{
/* Test vector 9 */
/* serialization test */
/* s_a cannot be zero */
const unsigned char adaptor_sig[162] = {
0x03, 0xe6, 0xd5, 0x1d, 0xa7, 0xbc, 0x2b, 0xf2,
0x4c, 0xf9, 0xdf, 0xd9, 0xac, 0xc6, 0xc4, 0xf0,
0xa3, 0xe7, 0x4d, 0x8a, 0x62, 0x73, 0xee, 0x5a,
0x57, 0x3e, 0xd6, 0x81, 0x8e, 0x30, 0x95, 0xb6,
0x09, 0x03, 0xf3, 0x3b, 0xc9, 0x8f, 0x9d, 0x2e,
0xa3, 0x51, 0x1f, 0x2e, 0x24, 0xf3, 0x35, 0x85,
0x57, 0xc8, 0x15, 0xab, 0xd7, 0x71, 0x3c, 0x93,
0x18, 0xaf, 0x9f, 0x4d, 0xfa, 0xb4, 0x44, 0x18,
0x98, 0xec, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x85, 0xb5, 0x89, 0x80, 0xb8, 0xe6,
0xc5, 0x4b, 0xd2, 0x06, 0x16, 0xbd, 0xb9, 0x46,
0x1d, 0xcc, 0xd8, 0xee, 0xbb, 0x7d, 0x7e, 0x7c,
0x83, 0xa9, 0x14, 0x52, 0xcc, 0x20, 0xed, 0xf5,
0x3b, 0xe5, 0xb0, 0xfe, 0x0d, 0xb4, 0x4d, 0xdd,
0xaa, 0xaf, 0xbe, 0x73, 0x76, 0x78, 0xc6, 0x84,
0xb6, 0xe8, 0x9b, 0x9b, 0x4b, 0x67, 0x9b, 0x18,
0x55, 0xaa, 0x6e, 0xd6, 0x44, 0x49, 0x8b, 0x89,
0xc9, 0x18
};
test_ecdsa_adaptor_spec_vectors_check_serialization(adaptor_sig, 0);
}
{
/* Test vector 10 */
/* serialization test */
/* s_a too high */
const unsigned char adaptor_sig[162] = {
0x03, 0xe6, 0xd5, 0x1d, 0xa7, 0xbc, 0x2b, 0xf2,
0x4c, 0xf9, 0xdf, 0xd9, 0xac, 0xc6, 0xc4, 0xf0,
0xa3, 0xe7, 0x4d, 0x8a, 0x62, 0x73, 0xee, 0x5a,
0x57, 0x3e, 0xd6, 0x81, 0x8e, 0x30, 0x95, 0xb6,
0x09, 0x03, 0xf3, 0x3b, 0xc9, 0x8f, 0x9d, 0x2e,
0xa3, 0x51, 0x1f, 0x2e, 0x24, 0xf3, 0x35, 0x85,
0x57, 0xc8, 0x15, 0xab, 0xd7, 0x71, 0x3c, 0x93,
0x18, 0xaf, 0x9f, 0x4d, 0xfa, 0xb4, 0x44, 0x18,
0x98, 0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48,
0xa0, 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36,
0x41, 0x41, 0x85, 0xb5, 0x89, 0x80, 0xb8, 0xe6,
0xc5, 0x4b, 0xd2, 0x06, 0x16, 0xbd, 0xb9, 0x46,
0x1d, 0xcc, 0xd8, 0xee, 0xbb, 0x7d, 0x7e, 0x7c,
0x83, 0xa9, 0x14, 0x52, 0xcc, 0x20, 0xed, 0xf5,
0x3b, 0xe5, 0xb0, 0xfe, 0x0d, 0xb4, 0x4d, 0xdd,
0xaa, 0xaf, 0xbe, 0x73, 0x76, 0x78, 0xc6, 0x84,
0xb6, 0xe8, 0x9b, 0x9b, 0x4b, 0x67, 0x9b, 0x18,
0x55, 0xaa, 0x6e, 0xd6, 0x44, 0x49, 0x8b, 0x89,
0xc9, 0x18
};
test_ecdsa_adaptor_spec_vectors_check_serialization(adaptor_sig, 0);
}
}

/* Nonce function that returns constant 0 */
Expand Down

0 comments on commit 0e2f43c

Please sign in to comment.