|
| 1 | +/*********************************************************************** |
| 2 | + * Distributed under the MIT software license, see the accompanying * |
| 3 | + * file COPYING or https://www.opensource.org/licenses/mit-license.php.* |
| 4 | + ***********************************************************************/ |
| 5 | + |
| 6 | +#ifndef SECP256K1_MODULE_SILENTPAYMENTS_TESTS_H |
| 7 | +#define SECP256K1_MODULE_SILENTPAYMENTS_TESTS_H |
| 8 | + |
| 9 | +#include "../../../include/secp256k1_silentpayments.h" |
| 10 | +#include "../../../src/modules/silentpayments/vectors.h" |
| 11 | + |
| 12 | +struct label_cache_entry { |
| 13 | + secp256k1_pubkey label; |
| 14 | + unsigned char label_tweak[32]; |
| 15 | +}; |
| 16 | +struct labels_cache { |
| 17 | + const secp256k1_context *ctx; |
| 18 | + size_t entries_used; |
| 19 | + struct label_cache_entry entries[10]; |
| 20 | +}; |
| 21 | +struct labels_cache labels_cache; |
| 22 | +const unsigned char* label_lookup(const secp256k1_pubkey* key, const void* cache_ptr) { |
| 23 | + const struct labels_cache* cache = (const struct labels_cache*)cache_ptr; |
| 24 | + size_t i; |
| 25 | + for (i = 0; i < cache->entries_used; i++) { |
| 26 | + if (secp256k1_ec_pubkey_cmp(cache->ctx, &cache->entries[i].label, key) == 0) { |
| 27 | + return cache->entries[i].label_tweak; |
| 28 | + } |
| 29 | + } |
| 30 | + return NULL; |
| 31 | +} |
| 32 | + |
| 33 | +void run_silentpayments_test_vector_send(const struct bip352_test_vector *test) { |
| 34 | + secp256k1_silentpayments_recipient recipients[MAX_OUTPUTS_PER_TEST_CASE]; |
| 35 | + const secp256k1_silentpayments_recipient *recipient_ptrs[MAX_OUTPUTS_PER_TEST_CASE]; |
| 36 | + secp256k1_xonly_pubkey generated_outputs[MAX_OUTPUTS_PER_TEST_CASE]; |
| 37 | + secp256k1_xonly_pubkey *generated_output_ptrs[MAX_OUTPUTS_PER_TEST_CASE]; |
| 38 | + secp256k1_keypair taproot_keypairs[MAX_INPUTS_PER_TEST_CASE]; |
| 39 | + secp256k1_keypair const *taproot_keypair_ptrs[MAX_INPUTS_PER_TEST_CASE]; |
| 40 | + unsigned char const *plain_seckeys[MAX_INPUTS_PER_TEST_CASE]; |
| 41 | + unsigned char created_output[32]; |
| 42 | + size_t i, j, k; |
| 43 | + int match; |
| 44 | + |
| 45 | + /* Check that sender creates expected outputs */ |
| 46 | + for (i = 0; i < test->num_outputs; i++) { |
| 47 | + CHECK(secp256k1_ec_pubkey_parse(CTX, &recipients[i].scan_pubkey, test->recipient_pubkeys[i].scan_pubkey, 33)); |
| 48 | + CHECK(secp256k1_ec_pubkey_parse(CTX, &recipients[i].spend_pubkey, test->recipient_pubkeys[i].spend_pubkey, 33)); |
| 49 | + recipients[i].index = i; |
| 50 | + recipient_ptrs[i] = &recipients[i]; |
| 51 | + generated_output_ptrs[i] = &generated_outputs[i]; |
| 52 | + } |
| 53 | + for (i = 0; i < test->num_plain_inputs; i++) { |
| 54 | + plain_seckeys[i] = test->plain_seckeys[i]; |
| 55 | + } |
| 56 | + for (i = 0; i < test->num_taproot_inputs; i++) { |
| 57 | + int ret = secp256k1_keypair_create(CTX, &taproot_keypairs[i], test->taproot_seckeys[i]); |
| 58 | + CHECK(ret); |
| 59 | + taproot_keypair_ptrs[i] = &taproot_keypairs[i]; |
| 60 | + } |
| 61 | + CHECK(secp256k1_silentpayments_sender_create_outputs(CTX, |
| 62 | + generated_output_ptrs, |
| 63 | + recipient_ptrs, |
| 64 | + test->num_outputs, |
| 65 | + test->outpoint_smallest, |
| 66 | + test->num_taproot_inputs > 0 ? taproot_keypair_ptrs : NULL, test->num_taproot_inputs, |
| 67 | + test->num_plain_inputs > 0 ? plain_seckeys : NULL, test->num_plain_inputs |
| 68 | + )); |
| 69 | + match = 0; |
| 70 | + for (i = 0; i < test->num_output_sets; i++) { |
| 71 | + size_t n_matches = 0; |
| 72 | + for (j = 0; j < test->num_outputs; j++) { |
| 73 | + CHECK(secp256k1_xonly_pubkey_serialize(CTX, created_output, &generated_outputs[j])); |
| 74 | + /* Loop over both lists to ensure tests don't fail due to different orderings of outputs */ |
| 75 | + for (k = 0; k < test->num_recipient_outputs; k++) { |
| 76 | + if (secp256k1_memcmp_var(created_output, test->recipient_outputs[i][k], 32) == 0) { |
| 77 | + n_matches++; |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + if (n_matches == test->num_recipient_outputs) { |
| 83 | + match = 1; |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + CHECK(match); |
| 88 | +} |
| 89 | + |
| 90 | +void run_silentpayments_test_vector_receive(const struct bip352_test_vector *test) { |
| 91 | + secp256k1_pubkey plain_pubkeys_objs[MAX_INPUTS_PER_TEST_CASE]; |
| 92 | + secp256k1_xonly_pubkey xonly_pubkeys_objs[MAX_INPUTS_PER_TEST_CASE]; |
| 93 | + secp256k1_xonly_pubkey tx_output_objs[MAX_OUTPUTS_PER_TEST_CASE]; |
| 94 | + secp256k1_silentpayments_found_output found_output_objs[MAX_OUTPUTS_PER_TEST_CASE]; |
| 95 | + secp256k1_pubkey const *plain_pubkeys[MAX_INPUTS_PER_TEST_CASE]; |
| 96 | + secp256k1_xonly_pubkey const *xonly_pubkeys[MAX_INPUTS_PER_TEST_CASE]; |
| 97 | + secp256k1_xonly_pubkey const *tx_outputs[MAX_OUTPUTS_PER_TEST_CASE]; |
| 98 | + secp256k1_silentpayments_found_output *found_outputs[MAX_OUTPUTS_PER_TEST_CASE]; |
| 99 | + unsigned char found_outputs_light_client[MAX_OUTPUTS_PER_TEST_CASE][32]; |
| 100 | + secp256k1_pubkey recipient_scan_pubkey; |
| 101 | + secp256k1_pubkey recipient_spend_pubkey; |
| 102 | + size_t i,j; |
| 103 | + int match; |
| 104 | + size_t n_found = 0; |
| 105 | + unsigned char found_output[32]; |
| 106 | + unsigned char found_signatures[10][64]; |
| 107 | + secp256k1_silentpayments_public_data public_data, public_data_index; |
| 108 | + unsigned char shared_secret_lightclient[33]; |
| 109 | + unsigned char light_client_data[33]; |
| 110 | + |
| 111 | + |
| 112 | + /* prepare the inputs */ |
| 113 | + { |
| 114 | + for (i = 0; i < test->num_plain_inputs; i++) { |
| 115 | + CHECK(secp256k1_ec_pubkey_parse(CTX, &plain_pubkeys_objs[i], test->plain_pubkeys[i], 33)); |
| 116 | + plain_pubkeys[i] = &plain_pubkeys_objs[i]; |
| 117 | + } |
| 118 | + for (i = 0; i < test->num_taproot_inputs; i++) { |
| 119 | + CHECK(secp256k1_xonly_pubkey_parse(CTX, &xonly_pubkeys_objs[i], test->xonly_pubkeys[i])); |
| 120 | + xonly_pubkeys[i] = &xonly_pubkeys_objs[i]; |
| 121 | + } |
| 122 | + CHECK(secp256k1_silentpayments_recipient_public_data_create(CTX, &public_data, |
| 123 | + test->outpoint_smallest, |
| 124 | + test->num_taproot_inputs > 0 ? xonly_pubkeys : NULL, test->num_taproot_inputs, |
| 125 | + test->num_plain_inputs > 0 ? plain_pubkeys : NULL, test->num_plain_inputs |
| 126 | + )); |
| 127 | + } |
| 128 | + /* prepare the outputs */ |
| 129 | + { |
| 130 | + for (i = 0; i < test->num_to_scan_outputs; i++) { |
| 131 | + CHECK(secp256k1_xonly_pubkey_parse(CTX, &tx_output_objs[i], test->to_scan_outputs[i])); |
| 132 | + tx_outputs[i] = &tx_output_objs[i]; |
| 133 | + } |
| 134 | + for (i = 0; i < test->num_found_output_pubkeys; i++) { |
| 135 | + found_outputs[i] = &found_output_objs[i]; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /* scan / spend pubkeys are not in the given data of the recipient part, so let's compute them */ |
| 140 | + CHECK(secp256k1_ec_pubkey_create(CTX, &recipient_scan_pubkey, test->scan_seckey)); |
| 141 | + CHECK(secp256k1_ec_pubkey_create(CTX, &recipient_spend_pubkey, test->spend_seckey)); |
| 142 | + |
| 143 | + /* create labels cache */ |
| 144 | + labels_cache.ctx = CTX; |
| 145 | + labels_cache.entries_used = 0; |
| 146 | + for (i = 0; i < test->num_labels; i++) { |
| 147 | + unsigned int m = test->label_integers[i]; |
| 148 | + struct label_cache_entry *cache_entry = &labels_cache.entries[labels_cache.entries_used]; |
| 149 | + CHECK(secp256k1_silentpayments_recipient_create_label_tweak(CTX, &cache_entry->label, cache_entry->label_tweak, test->scan_seckey, m)); |
| 150 | + labels_cache.entries_used++; |
| 151 | + } |
| 152 | + CHECK(secp256k1_silentpayments_recipient_scan_outputs(CTX, |
| 153 | + found_outputs, &n_found, |
| 154 | + tx_outputs, test->num_to_scan_outputs, |
| 155 | + test->scan_seckey, |
| 156 | + &public_data, |
| 157 | + &recipient_spend_pubkey, |
| 158 | + label_lookup, &labels_cache) |
| 159 | + ); |
| 160 | + for (i = 0; i < n_found; i++) { |
| 161 | + unsigned char full_seckey[32]; |
| 162 | + secp256k1_keypair keypair; |
| 163 | + unsigned char signature[64]; |
| 164 | + const unsigned char msg32[32] = /* sha256("message") */ |
| 165 | + {0xab,0x53,0x0a,0x13,0xe4,0x59,0x14,0x98,0x2b,0x79,0xf9,0xb7,0xe3,0xfb,0xa9,0x94, |
| 166 | + 0xcf,0xd1,0xf3,0xfb,0x22,0xf7,0x1c,0xea,0x1a,0xfb,0xf0,0x2b,0x46,0x0c,0x6d,0x1d}; |
| 167 | + const unsigned char aux32[32] = /* sha256("random auxiliary data") */ |
| 168 | + {0x0b,0x3f,0xdd,0xfd,0x67,0xbf,0x76,0xae,0x76,0x39,0xee,0x73,0x5b,0x70,0xff,0x15, |
| 169 | + 0x83,0xfd,0x92,0x48,0xc0,0x57,0xd2,0x86,0x07,0xa2,0x15,0xf4,0x0b,0x0a,0x3e,0xcc}; |
| 170 | + memcpy(&full_seckey, test->spend_seckey, 32); |
| 171 | + CHECK(secp256k1_ec_seckey_tweak_add(CTX, full_seckey, found_outputs[i]->tweak)); |
| 172 | + CHECK(secp256k1_keypair_create(CTX, &keypair, full_seckey)); |
| 173 | + CHECK(secp256k1_schnorrsig_sign32(CTX, signature, msg32, &keypair, aux32)); |
| 174 | + memcpy(found_signatures[i], signature, 64); |
| 175 | + } |
| 176 | + |
| 177 | + /* compare expected and scanned outputs (including calculated seckey tweaks and signatures) */ |
| 178 | + match = 0; |
| 179 | + for (i = 0; i < n_found; i++) { |
| 180 | + CHECK(secp256k1_xonly_pubkey_serialize(CTX, found_output, &found_outputs[i]->output)); |
| 181 | + for (j = 0; j < test->num_found_output_pubkeys; j++) { |
| 182 | + if (secp256k1_memcmp_var(&found_output, test->found_output_pubkeys[j], 32) == 0) { |
| 183 | + CHECK(secp256k1_memcmp_var(found_outputs[i]->tweak, test->found_seckey_tweaks[j], 32) == 0); |
| 184 | + CHECK(secp256k1_memcmp_var(found_signatures[i], test->found_signatures[j], 64) == 0); |
| 185 | + match = 1; |
| 186 | + break; |
| 187 | + } |
| 188 | + } |
| 189 | + CHECK(match); |
| 190 | + } |
| 191 | + CHECK(n_found == test->num_found_output_pubkeys); |
| 192 | + /* Scan as a light client |
| 193 | + * it is not recommended to use labels as a light client so here we are only |
| 194 | + * running this on tests that do not involve labels. Primarily, this test is to |
| 195 | + * ensure that _recipient_created_shared_secret and _create_shared_secret are the same |
| 196 | + */ |
| 197 | + if (test->num_labels == 0) { |
| 198 | + CHECK(secp256k1_silentpayments_recipient_public_data_serialize(CTX, light_client_data, &public_data)); |
| 199 | + CHECK(secp256k1_silentpayments_recipient_public_data_parse(CTX, &public_data_index, light_client_data)); |
| 200 | + CHECK(secp256k1_silentpayments_recipient_create_shared_secret(CTX, shared_secret_lightclient, test->scan_seckey, &public_data_index)); |
| 201 | + n_found = 0; |
| 202 | + { |
| 203 | + int found = 0; |
| 204 | + size_t k = 0; |
| 205 | + secp256k1_xonly_pubkey potential_output; |
| 206 | + |
| 207 | + while(1) { |
| 208 | + |
| 209 | + CHECK(secp256k1_silentpayments_recipient_create_output_pubkey(CTX, |
| 210 | + &potential_output, |
| 211 | + shared_secret_lightclient, |
| 212 | + &recipient_spend_pubkey, |
| 213 | + k |
| 214 | + )); |
| 215 | + /* At this point, we check that the utxo exists with a light client protocol. |
| 216 | + * For this example, we'll just iterate through the list of pubkeys */ |
| 217 | + found = 0; |
| 218 | + for (i = 0; i < test->num_to_scan_outputs; i++) { |
| 219 | + if (secp256k1_xonly_pubkey_cmp(CTX, &potential_output, tx_outputs[i]) == 0) { |
| 220 | + secp256k1_xonly_pubkey_serialize(CTX, found_outputs_light_client[n_found], &potential_output); |
| 221 | + found = 1; |
| 222 | + n_found++; |
| 223 | + k++; |
| 224 | + break; |
| 225 | + } |
| 226 | + } |
| 227 | + if (!found) { |
| 228 | + break; |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + CHECK(n_found == test->num_found_output_pubkeys); |
| 233 | + for (i = 0; i < n_found; i++) { |
| 234 | + match = 0; |
| 235 | + for (j = 0; j < test->num_found_output_pubkeys; j++) { |
| 236 | + if (secp256k1_memcmp_var(&found_outputs_light_client[i], test->found_output_pubkeys[j], 32) == 0) { |
| 237 | + match = 1; |
| 238 | + break; |
| 239 | + } |
| 240 | + } |
| 241 | + CHECK(match); |
| 242 | + } |
| 243 | + } |
| 244 | +} |
| 245 | + |
| 246 | +void run_silentpayments_test_vectors(void) { |
| 247 | + size_t i; |
| 248 | + |
| 249 | + |
| 250 | + for (i = 0; i < sizeof(bip352_test_vectors) / sizeof(bip352_test_vectors[0]); i++) { |
| 251 | + const struct bip352_test_vector *test = &bip352_test_vectors[i]; |
| 252 | + run_silentpayments_test_vector_send(test); |
| 253 | + run_silentpayments_test_vector_receive(test); |
| 254 | + } |
| 255 | +} |
| 256 | + |
| 257 | +void run_silentpayments_tests(void) { |
| 258 | + run_silentpayments_test_vectors(); |
| 259 | + /* TODO: add a few manual tests here, that target the ECC-related parts of silent payments */ |
| 260 | +} |
| 261 | + |
| 262 | +#endif |
0 commit comments