|
| 1 | +#ifndef SECP256K1_MODULE_EXTRAKEYS_BATCH_ADD_TESTS_IMPL_H |
| 2 | +#define SECP256K1_MODULE_EXTRAKEYS_BATCH_ADD_TESTS_IMPL_H |
| 3 | + |
| 4 | +#include "../../../include/secp256k1_extrakeys.h" |
| 5 | +#include "../../../include/secp256k1_batch.h" |
| 6 | +#include "../../../include/secp256k1_tweak_check_batch.h" |
| 7 | + |
| 8 | +/* Checks that a bit flip in the n_flip-th argument (that has n_bytes many |
| 9 | + * bytes) changes the hash function */ |
| 10 | +void batch_xonlypub_tweak_randomizer_gen_bitflip(secp256k1_sha256 *sha, unsigned char **args, size_t n_flip, size_t n_bytes) { |
| 11 | + unsigned char randomizers[2][32]; |
| 12 | + secp256k1_sha256 sha_cpy; |
| 13 | + sha_cpy = *sha; |
| 14 | + secp256k1_batch_xonlypub_tweak_randomizer_gen(randomizers[0], &sha_cpy, args[0], args[1], args[2], args[3]); |
| 15 | + testrand_flip(args[n_flip], n_bytes); |
| 16 | + sha_cpy = *sha; |
| 17 | + secp256k1_batch_xonlypub_tweak_randomizer_gen(randomizers[1], &sha_cpy, args[0], args[1], args[2], args[3]); |
| 18 | + CHECK(secp256k1_memcmp_var(randomizers[0], randomizers[1], 32) != 0); |
| 19 | +} |
| 20 | + |
| 21 | +void run_batch_xonlypub_tweak_randomizer_gen_tests(void) { |
| 22 | + secp256k1_sha256 sha; |
| 23 | + size_t n_checks = 20; |
| 24 | + unsigned char tweaked_pk[32]; |
| 25 | + unsigned char tweaked_pk_parity; |
| 26 | + unsigned char tweak[32]; |
| 27 | + unsigned char internal_pk[33]; |
| 28 | + unsigned char *args[4]; |
| 29 | + size_t i; /* loops through n_checks */ |
| 30 | + int j; /* loops through count */ |
| 31 | + |
| 32 | + secp256k1_batch_sha256_tagged(&sha); |
| 33 | + |
| 34 | + for (i = 0; i < n_checks; i++) { |
| 35 | + uint8_t temp_rand; |
| 36 | + |
| 37 | + /* generate i-th tweak check data */ |
| 38 | + testrand256(tweaked_pk); |
| 39 | + tweaked_pk_parity = (unsigned char) testrand_int(2); |
| 40 | + testrand256(tweak); |
| 41 | + testrand256(&internal_pk[1]); |
| 42 | + temp_rand = testrand_int(2) + 2; /* randomly choose 2 or 3 */ |
| 43 | + internal_pk[0] = (unsigned char)temp_rand; |
| 44 | + |
| 45 | + /* check bitflip in any argument results in generates randomizers */ |
| 46 | + args[0] = tweaked_pk; |
| 47 | + args[1] = &tweaked_pk_parity; |
| 48 | + args[2] = internal_pk; |
| 49 | + args[3] = tweak; |
| 50 | + |
| 51 | + for (j = 0; j < COUNT; j++) { |
| 52 | + batch_xonlypub_tweak_randomizer_gen_bitflip(&sha, args, 0, 32); |
| 53 | + batch_xonlypub_tweak_randomizer_gen_bitflip(&sha, args, 1, 1); |
| 54 | + batch_xonlypub_tweak_randomizer_gen_bitflip(&sha, args, 2, 33); |
| 55 | + batch_xonlypub_tweak_randomizer_gen_bitflip(&sha, args, 3, 32); |
| 56 | + } |
| 57 | + |
| 58 | + /* write i-th tweak check data to the sha object |
| 59 | + * this is required for generating the next randomizer */ |
| 60 | + secp256k1_sha256_write(&sha, tweaked_pk, 32); |
| 61 | + secp256k1_sha256_write(&sha, &tweaked_pk_parity, 1); |
| 62 | + secp256k1_sha256_write(&sha, tweak, 32); |
| 63 | + secp256k1_sha256_write(&sha, internal_pk, 33); |
| 64 | + } |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +void test_batch_add_xonlypub_tweak_api(void) { |
| 69 | + unsigned char sk[32]; |
| 70 | + secp256k1_keypair keypair; |
| 71 | + secp256k1_xonly_pubkey pk; |
| 72 | + /* xonly pubkey tweak checks data */ |
| 73 | + unsigned char tweaked_pk[32]; |
| 74 | + int tweaked_pk_parity; |
| 75 | + unsigned char tweak[32]; |
| 76 | + secp256k1_pubkey tmp_pk; |
| 77 | + secp256k1_xonly_pubkey tmp_xonly_pk; |
| 78 | + unsigned char overflows[32]; |
| 79 | + |
| 80 | + /** setup **/ |
| 81 | + secp256k1_batch *batch = secp256k1_batch_create(CTX, 1, NULL); |
| 82 | + |
| 83 | + /** generate keypair data **/ |
| 84 | + testrand256(sk); |
| 85 | + CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1); |
| 86 | + CHECK(secp256k1_keypair_xonly_pub(CTX, &pk, NULL, &keypair) == 1); |
| 87 | + memset(overflows, 0xFF, sizeof(overflows)); |
| 88 | + |
| 89 | + /** generate tweak check data (tweaked_pk, tweaked_pk_parity, tweak) **/ |
| 90 | + testrand256(tweak); |
| 91 | + CHECK(secp256k1_xonly_pubkey_tweak_add(CTX, &tmp_pk, &pk, tweak)); |
| 92 | + CHECK(secp256k1_xonly_pubkey_from_pubkey(CTX, &tmp_xonly_pk, &tweaked_pk_parity, &tmp_pk)); |
| 93 | + CHECK(secp256k1_xonly_pubkey_serialize(CTX, tweaked_pk, &tmp_xonly_pk)); |
| 94 | + CHECK(secp256k1_xonly_pubkey_tweak_add_check(CTX, tweaked_pk, tweaked_pk_parity, &pk, tweak)); |
| 95 | + |
| 96 | + CHECK(batch != NULL); |
| 97 | + |
| 98 | + /** main test body **/ |
| 99 | + CHECK(secp256k1_batch_add_xonlypub_tweak_check(CTX, batch, tweaked_pk, tweaked_pk_parity, &pk, tweak) == 1); |
| 100 | + CHECK(secp256k1_batch_verify(CTX, batch) == 1); |
| 101 | + CHECK_ILLEGAL(CTX, secp256k1_batch_add_xonlypub_tweak_check(CTX, batch, NULL, tweaked_pk_parity, &pk, tweak)); |
| 102 | + CHECK_ILLEGAL(CTX, secp256k1_batch_add_xonlypub_tweak_check(CTX, batch, tweaked_pk, tweaked_pk_parity, NULL, tweak)); |
| 103 | + CHECK_ILLEGAL(CTX, secp256k1_batch_add_xonlypub_tweak_check(CTX, batch, tweaked_pk, tweaked_pk_parity, &pk, NULL)); |
| 104 | + CHECK_ILLEGAL(CTX, secp256k1_batch_add_xonlypub_tweak_check(CTX, NULL, tweaked_pk, tweaked_pk_parity, &pk, tweak)); |
| 105 | + /** overflowing tweak not allowed **/ |
| 106 | + CHECK(secp256k1_batch_add_xonlypub_tweak_check(CTX, batch, tweaked_pk, tweaked_pk_parity, &pk, overflows) == 0); |
| 107 | + /** x-coordinate of tweaked pubkey should be less than prime order **/ |
| 108 | + CHECK(secp256k1_batch_add_xonlypub_tweak_check(CTX, batch, overflows, tweaked_pk_parity, &pk, tweak) == 0); |
| 109 | + |
| 110 | + /** batch_verify should fail for incorrect tweak **/ |
| 111 | + CHECK(secp256k1_batch_usable(CTX, batch)); |
| 112 | + CHECK(secp256k1_batch_add_xonlypub_tweak_check(CTX, batch, tweaked_pk, !tweaked_pk_parity, &pk, tweak) == 1); |
| 113 | + CHECK(secp256k1_batch_verify(CTX, batch) == 0); |
| 114 | + |
| 115 | + /** batch_add_ should ignore unusable batch object (i.e, batch->result = 0) **/ |
| 116 | + CHECK(secp256k1_batch_usable(CTX, batch) == 0); |
| 117 | + CHECK(secp256k1_batch_add_xonlypub_tweak_check(CTX, batch, tweaked_pk, tweaked_pk_parity, &pk, tweak) == 0); |
| 118 | + |
| 119 | + secp256k1_batch_destroy(CTX, batch); |
| 120 | +} |
| 121 | + |
| 122 | +void run_batch_add_xonlypub_tweak_tests(void) { |
| 123 | + run_batch_xonlypub_tweak_randomizer_gen_tests(); |
| 124 | + test_batch_add_xonlypub_tweak_api(); |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | +#endif /* SECP256K1_MODULE_EXTRAKEYS_BATCH_ADD_TESTS_IMPL_H */ |
0 commit comments