add secp256k1_ec_pubkey_cmp method
This commit is contained in:
50
src/tests.c
50
src/tests.c
@@ -4809,6 +4809,55 @@ void test_random_pubkeys(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void run_pubkey_comparison(void) {
|
||||
unsigned char pk1_ser[33] = {
|
||||
0x02,
|
||||
0x58, 0x84, 0xb3, 0xa2, 0x4b, 0x97, 0x37, 0x88, 0x92, 0x38, 0xa6, 0x26, 0x62, 0x52, 0x35, 0x11,
|
||||
0xd0, 0x9a, 0xa1, 0x1b, 0x80, 0x0b, 0x5e, 0x93, 0x80, 0x26, 0x11, 0xef, 0x67, 0x4b, 0xd9, 0x23
|
||||
};
|
||||
const unsigned char pk2_ser[33] = {
|
||||
0x02,
|
||||
0xde, 0x36, 0x0e, 0x87, 0x59, 0x8f, 0x3c, 0x01, 0x36, 0x2a, 0x2a, 0xb8, 0xc6, 0xf4, 0x5e, 0x4d,
|
||||
0xb2, 0xc2, 0xd5, 0x03, 0xa7, 0xf9, 0xf1, 0x4f, 0xa8, 0xfa, 0x95, 0xa8, 0xe9, 0x69, 0x76, 0x1c
|
||||
};
|
||||
secp256k1_pubkey pk1;
|
||||
secp256k1_pubkey pk2;
|
||||
int32_t ecount = 0;
|
||||
|
||||
CHECK(secp256k1_ec_pubkey_parse(ctx, &pk1, pk1_ser, sizeof(pk1_ser)) == 1);
|
||||
CHECK(secp256k1_ec_pubkey_parse(ctx, &pk2, pk2_ser, sizeof(pk2_ser)) == 1);
|
||||
|
||||
secp256k1_context_set_illegal_callback(ctx, counting_illegal_callback_fn, &ecount);
|
||||
CHECK(secp256k1_ec_pubkey_cmp(ctx, NULL, &pk2) < 0);
|
||||
CHECK(ecount == 1);
|
||||
CHECK(secp256k1_ec_pubkey_cmp(ctx, &pk1, NULL) > 0);
|
||||
CHECK(ecount == 2);
|
||||
CHECK(secp256k1_ec_pubkey_cmp(ctx, &pk1, &pk2) < 0);
|
||||
CHECK(secp256k1_ec_pubkey_cmp(ctx, &pk2, &pk1) > 0);
|
||||
CHECK(secp256k1_ec_pubkey_cmp(ctx, &pk1, &pk1) == 0);
|
||||
CHECK(secp256k1_ec_pubkey_cmp(ctx, &pk2, &pk2) == 0);
|
||||
CHECK(ecount == 2);
|
||||
{
|
||||
secp256k1_pubkey pk_tmp;
|
||||
memset(&pk_tmp, 0, sizeof(pk_tmp)); /* illegal pubkey */
|
||||
CHECK(secp256k1_ec_pubkey_cmp(ctx, &pk_tmp, &pk2) < 0);
|
||||
CHECK(ecount == 3);
|
||||
CHECK(secp256k1_ec_pubkey_cmp(ctx, &pk_tmp, &pk_tmp) == 0);
|
||||
CHECK(ecount == 5);
|
||||
CHECK(secp256k1_ec_pubkey_cmp(ctx, &pk2, &pk_tmp) > 0);
|
||||
CHECK(ecount == 6);
|
||||
}
|
||||
|
||||
secp256k1_context_set_illegal_callback(ctx, NULL, NULL);
|
||||
|
||||
/* Make pk2 the same as pk1 but with 3 rather than 2. Note that in
|
||||
* an uncompressed encoding, these would have the opposite ordering */
|
||||
pk1_ser[0] = 3;
|
||||
CHECK(secp256k1_ec_pubkey_parse(ctx, &pk2, pk1_ser, sizeof(pk1_ser)) == 1);
|
||||
CHECK(secp256k1_ec_pubkey_cmp(ctx, &pk1, &pk2) < 0);
|
||||
CHECK(secp256k1_ec_pubkey_cmp(ctx, &pk2, &pk1) > 0);
|
||||
}
|
||||
|
||||
void run_random_pubkeys(void) {
|
||||
int i;
|
||||
for (i = 0; i < 10*count; i++) {
|
||||
@@ -5860,6 +5909,7 @@ int main(int argc, char **argv) {
|
||||
#endif
|
||||
|
||||
/* ecdsa tests */
|
||||
run_pubkey_comparison();
|
||||
run_random_pubkeys();
|
||||
run_ecdsa_der_parse();
|
||||
run_ecdsa_sign_verify();
|
||||
|
||||
Reference in New Issue
Block a user