Skip to content

Commit a794d23

Browse files
josibakestratospher
authored andcommitted
tests: add BIP-352 test vectors
Add the BIP-352 test vectors. The vectors are generated with a Python script that converts the .json file from the BIP to C code: $ ./tools/tests_silentpayments_generate.py test_vectors.json > ./src/modules/silentpayments/vectors.h
1 parent 55f6bdf commit a794d23

File tree

8 files changed

+7479
-0
lines changed

8 files changed

+7479
-0
lines changed

Makefile.am

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,16 @@ maintainer-clean-local: clean-precomp
259259
### Pregenerated test vectors
260260
### (see the comments in the previous section for detailed rationale)
261261
TESTVECTORS = src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.h
262+
TESTVECTORS += src/modules/silentpayments/vectors.h
262263

263264
src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.h:
264265
mkdir -p $(@D)
265266
python3 $(top_srcdir)/tools/tests_wycheproof_generate.py $(top_srcdir)/src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.json > $@
266267

268+
src/modules/silentpayments/vectors.h:
269+
mkdir -p $(@D)
270+
python3 $(top_srcdir)/tools/tests_silentpayments_generate.py $(top_srcdir)/src/modules/silentpayments/bip352_send_and_receive_test_vectors.json > $@
271+
267272
testvectors: $(TESTVECTORS)
268273

269274
BUILT_SOURCES += $(TESTVECTORS)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
include_HEADERS += include/secp256k1_silentpayments.h
22
noinst_HEADERS += src/modules/silentpayments/main_impl.h
33
noinst_HEADERS += src/modules/silentpayments/bench_impl.h
4+
noinst_HEADERS += src/modules/silentpayments/tests_impl.h
5+
noinst_HEADERS += src/modules/silentpayments/vectors.h

src/modules/silentpayments/bip352_send_and_receive_test_vectors.json

Lines changed: 2760 additions & 0 deletions
Large diffs are not rendered by default.

src/modules/silentpayments/tests_impl.h

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define SECP256K1_MODULE_SILENTPAYMENTS_TESTS_H
88

99
#include "../../../include/secp256k1_silentpayments.h"
10+
#include "../../../src/modules/silentpayments/vectors.h"
1011
#include "include/secp256k1.h"
1112

1213
/** Constants
@@ -86,6 +87,20 @@ static unsigned char ALICE_SECKEY[32] = {
8687
0x8a,0x4c,0x53,0xf6,0xe0,0x50,0x7b,0x42,
8788
0x15,0x42,0x01,0xb8,0xe5,0xdf,0xf3,0xb1
8889
};
90+
/* sha256("message") */
91+
static unsigned char MSG32[32] = {
92+
0xab,0x53,0x0a,0x13,0xe4,0x59,0x14,0x98,
93+
0x2b,0x79,0xf9,0xb7,0xe3,0xfb,0xa9,0x94,
94+
0xcf,0xd1,0xf3,0xfb,0x22,0xf7,0x1c,0xea,
95+
0x1a,0xfb,0xf0,0x2b,0x46,0x0c,0x6d,0x1d
96+
};
97+
/* sha256("random auxiliary data") */
98+
static unsigned char AUX32[32] = {
99+
0x0b,0x3f,0xdd,0xfd,0x67,0xbf,0x76,0xae,
100+
0x76,0x39,0xee,0x73,0x5b,0x70,0xff,0x15,
101+
0x83,0xfd,0x92,0x48,0xc0,0x57,0xd2,0x86,
102+
0x07,0xa2,0x15,0xf4,0x0b,0x0a,0x3e,0xcc
103+
};
89104

90105
struct label_cache_entry {
91106
unsigned char label[33];
@@ -349,11 +364,246 @@ static void test_recipient_api(void) {
349364
CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 1, ALICE_SECKEY, &pd, &p, &label_lookup, NULL));
350365
}
351366

367+
void run_silentpayments_test_vector_send(const struct bip352_test_vector *test) {
368+
secp256k1_silentpayments_recipient recipients[MAX_OUTPUTS_PER_TEST_CASE];
369+
const secp256k1_silentpayments_recipient *recipient_ptrs[MAX_OUTPUTS_PER_TEST_CASE];
370+
secp256k1_xonly_pubkey generated_outputs[MAX_OUTPUTS_PER_TEST_CASE];
371+
secp256k1_xonly_pubkey *generated_output_ptrs[MAX_OUTPUTS_PER_TEST_CASE];
372+
secp256k1_keypair taproot_keypairs[MAX_INPUTS_PER_TEST_CASE];
373+
secp256k1_keypair const *taproot_keypair_ptrs[MAX_INPUTS_PER_TEST_CASE];
374+
unsigned char const *plain_seckeys[MAX_INPUTS_PER_TEST_CASE];
375+
unsigned char created_output[32];
376+
size_t i, j, k;
377+
int match, ret;
378+
379+
/* Check that sender creates expected outputs */
380+
for (i = 0; i < test->num_outputs; i++) {
381+
CHECK(secp256k1_ec_pubkey_parse(CTX, &recipients[i].scan_pubkey, test->recipient_pubkeys[i].scan_pubkey, 33));
382+
CHECK(secp256k1_ec_pubkey_parse(CTX, &recipients[i].spend_pubkey, test->recipient_pubkeys[i].spend_pubkey, 33));
383+
recipients[i].index = i;
384+
recipient_ptrs[i] = &recipients[i];
385+
generated_output_ptrs[i] = &generated_outputs[i];
386+
}
387+
for (i = 0; i < test->num_plain_inputs; i++) {
388+
plain_seckeys[i] = test->plain_seckeys[i];
389+
}
390+
for (i = 0; i < test->num_taproot_inputs; i++) {
391+
CHECK(secp256k1_keypair_create(CTX, &taproot_keypairs[i], test->taproot_seckeys[i]));
392+
taproot_keypair_ptrs[i] = &taproot_keypairs[i];
393+
}
394+
ret = secp256k1_silentpayments_sender_create_outputs(CTX,
395+
generated_output_ptrs,
396+
recipient_ptrs,
397+
test->num_outputs,
398+
test->outpoint_smallest,
399+
test->num_taproot_inputs > 0 ? taproot_keypair_ptrs : NULL, test->num_taproot_inputs,
400+
test->num_plain_inputs > 0 ? plain_seckeys : NULL, test->num_plain_inputs
401+
);
402+
/* If we are unable to create outputs, e.g., the input keys sum to zero, check that the
403+
* expected number of recipient outputs for this test case is zero
404+
*/
405+
if (!ret) {
406+
CHECK(test->num_recipient_outputs == 0);
407+
return;
408+
}
409+
410+
match = 0;
411+
for (i = 0; i < test->num_output_sets; i++) {
412+
size_t n_matches = 0;
413+
for (j = 0; j < test->num_outputs; j++) {
414+
CHECK(secp256k1_xonly_pubkey_serialize(CTX, created_output, &generated_outputs[j]));
415+
/* Loop over both lists to ensure tests don't fail due to different orderings of outputs */
416+
for (k = 0; k < test->num_recipient_outputs; k++) {
417+
if (secp256k1_memcmp_var(created_output, test->recipient_outputs[i][k], 32) == 0) {
418+
n_matches++;
419+
break;
420+
}
421+
}
422+
}
423+
if (n_matches == test->num_recipient_outputs) {
424+
match = 1;
425+
break;
426+
}
427+
}
428+
CHECK(match);
429+
}
430+
431+
void run_silentpayments_test_vector_receive(const struct bip352_test_vector *test) {
432+
secp256k1_pubkey plain_pubkeys_objs[MAX_INPUTS_PER_TEST_CASE];
433+
secp256k1_xonly_pubkey xonly_pubkeys_objs[MAX_INPUTS_PER_TEST_CASE];
434+
secp256k1_xonly_pubkey tx_output_objs[MAX_OUTPUTS_PER_TEST_CASE];
435+
secp256k1_silentpayments_found_output found_output_objs[MAX_OUTPUTS_PER_TEST_CASE];
436+
secp256k1_pubkey const *plain_pubkeys[MAX_INPUTS_PER_TEST_CASE];
437+
secp256k1_xonly_pubkey const *xonly_pubkeys[MAX_INPUTS_PER_TEST_CASE];
438+
secp256k1_xonly_pubkey const *tx_outputs[MAX_OUTPUTS_PER_TEST_CASE];
439+
secp256k1_silentpayments_found_output *found_outputs[MAX_OUTPUTS_PER_TEST_CASE];
440+
unsigned char found_outputs_light_client[MAX_OUTPUTS_PER_TEST_CASE][32];
441+
secp256k1_pubkey recipient_scan_pubkey;
442+
secp256k1_pubkey recipient_spend_pubkey;
443+
secp256k1_pubkey label;
444+
size_t len = 33;
445+
size_t i,j;
446+
int match, ret;
447+
size_t n_found = 0;
448+
unsigned char found_output[32];
449+
unsigned char found_signatures[10][64];
450+
secp256k1_silentpayments_public_data public_data, public_data_index;
451+
unsigned char shared_secret_lightclient[33];
452+
unsigned char light_client_data[33];
453+
454+
455+
/* prepare the inputs */
456+
{
457+
for (i = 0; i < test->num_plain_inputs; i++) {
458+
CHECK(secp256k1_ec_pubkey_parse(CTX, &plain_pubkeys_objs[i], test->plain_pubkeys[i], 33));
459+
plain_pubkeys[i] = &plain_pubkeys_objs[i];
460+
}
461+
for (i = 0; i < test->num_taproot_inputs; i++) {
462+
CHECK(secp256k1_xonly_pubkey_parse(CTX, &xonly_pubkeys_objs[i], test->xonly_pubkeys[i]));
463+
xonly_pubkeys[i] = &xonly_pubkeys_objs[i];
464+
}
465+
ret = secp256k1_silentpayments_recipient_public_data_create(CTX, &public_data,
466+
test->outpoint_smallest,
467+
test->num_taproot_inputs > 0 ? xonly_pubkeys : NULL, test->num_taproot_inputs,
468+
test->num_plain_inputs > 0 ? plain_pubkeys : NULL, test->num_plain_inputs
469+
);
470+
/* If we are unable to create the public_data object, e.g., the input public keys sum to
471+
* zero, check that the expected number of recipient outputs for this test case is zero
472+
*/
473+
if (!ret) {
474+
CHECK(test->num_found_output_pubkeys == 0);
475+
return;
476+
}
477+
}
478+
/* prepare the outputs */
479+
{
480+
for (i = 0; i < test->num_to_scan_outputs; i++) {
481+
CHECK(secp256k1_xonly_pubkey_parse(CTX, &tx_output_objs[i], test->to_scan_outputs[i]));
482+
tx_outputs[i] = &tx_output_objs[i];
483+
}
484+
for (i = 0; i < test->num_found_output_pubkeys; i++) {
485+
found_outputs[i] = &found_output_objs[i];
486+
}
487+
}
488+
489+
/* scan / spend pubkeys are not in the given data of the recipient part, so let's compute them */
490+
CHECK(secp256k1_ec_pubkey_create(CTX, &recipient_scan_pubkey, test->scan_seckey));
491+
CHECK(secp256k1_ec_pubkey_create(CTX, &recipient_spend_pubkey, test->spend_seckey));
492+
493+
/* create labels cache */
494+
labels_cache.entries_used = 0;
495+
for (i = 0; i < test->num_labels; i++) {
496+
unsigned int m = test->label_integers[i];
497+
struct label_cache_entry *cache_entry = &labels_cache.entries[labels_cache.entries_used];
498+
CHECK(secp256k1_silentpayments_recipient_create_label_tweak(CTX, &label, cache_entry->label_tweak, test->scan_seckey, m));
499+
CHECK(secp256k1_ec_pubkey_serialize(CTX, cache_entry->label, &len, &label, SECP256K1_EC_COMPRESSED));
500+
labels_cache.entries_used++;
501+
}
502+
CHECK(secp256k1_silentpayments_recipient_scan_outputs(CTX,
503+
found_outputs, &n_found,
504+
tx_outputs, test->num_to_scan_outputs,
505+
test->scan_seckey,
506+
&public_data,
507+
&recipient_spend_pubkey,
508+
label_lookup, &labels_cache)
509+
);
510+
for (i = 0; i < n_found; i++) {
511+
unsigned char full_seckey[32];
512+
secp256k1_keypair keypair;
513+
unsigned char signature[64];
514+
memcpy(&full_seckey, test->spend_seckey, 32);
515+
CHECK(secp256k1_ec_seckey_tweak_add(CTX, full_seckey, found_outputs[i]->tweak));
516+
CHECK(secp256k1_keypair_create(CTX, &keypair, full_seckey));
517+
CHECK(secp256k1_schnorrsig_sign32(CTX, signature, MSG32, &keypair, AUX32));
518+
memcpy(found_signatures[i], signature, 64);
519+
}
520+
521+
/* compare expected and scanned outputs (including calculated seckey tweaks and signatures) */
522+
match = 0;
523+
for (i = 0; i < n_found; i++) {
524+
CHECK(secp256k1_xonly_pubkey_serialize(CTX, found_output, &found_outputs[i]->output));
525+
for (j = 0; j < test->num_found_output_pubkeys; j++) {
526+
if (secp256k1_memcmp_var(&found_output, test->found_output_pubkeys[j], 32) == 0) {
527+
CHECK(secp256k1_memcmp_var(found_outputs[i]->tweak, test->found_seckey_tweaks[j], 32) == 0);
528+
CHECK(secp256k1_memcmp_var(found_signatures[i], test->found_signatures[j], 64) == 0);
529+
match = 1;
530+
break;
531+
}
532+
}
533+
CHECK(match);
534+
}
535+
CHECK(n_found == test->num_found_output_pubkeys);
536+
/* Scan as a light client
537+
* it is not recommended to use labels as a light client so here we are only
538+
* running this on tests that do not involve labels. Primarily, this test is to
539+
* ensure that _recipient_created_shared_secret and _create_shared_secret are the same
540+
*/
541+
if (test->num_labels == 0) {
542+
CHECK(secp256k1_silentpayments_recipient_public_data_serialize(CTX, light_client_data, &public_data));
543+
CHECK(secp256k1_silentpayments_recipient_public_data_parse(CTX, &public_data_index, light_client_data));
544+
CHECK(secp256k1_silentpayments_recipient_create_shared_secret(CTX, shared_secret_lightclient, test->scan_seckey, &public_data_index));
545+
n_found = 0;
546+
{
547+
int found = 0;
548+
size_t k = 0;
549+
secp256k1_xonly_pubkey potential_output;
550+
551+
while(1) {
552+
553+
CHECK(secp256k1_silentpayments_recipient_create_output_pubkey(CTX,
554+
&potential_output,
555+
shared_secret_lightclient,
556+
&recipient_spend_pubkey,
557+
k
558+
));
559+
/* At this point, we check that the utxo exists with a light client protocol.
560+
* For this example, we'll just iterate through the list of pubkeys */
561+
found = 0;
562+
for (i = 0; i < test->num_to_scan_outputs; i++) {
563+
if (secp256k1_xonly_pubkey_cmp(CTX, &potential_output, tx_outputs[i]) == 0) {
564+
secp256k1_xonly_pubkey_serialize(CTX, found_outputs_light_client[n_found], &potential_output);
565+
found = 1;
566+
n_found++;
567+
k++;
568+
break;
569+
}
570+
}
571+
if (!found) {
572+
break;
573+
}
574+
}
575+
}
576+
CHECK(n_found == test->num_found_output_pubkeys);
577+
for (i = 0; i < n_found; i++) {
578+
match = 0;
579+
for (j = 0; j < test->num_found_output_pubkeys; j++) {
580+
if (secp256k1_memcmp_var(&found_outputs_light_client[i], test->found_output_pubkeys[j], 32) == 0) {
581+
match = 1;
582+
break;
583+
}
584+
}
585+
CHECK(match);
586+
}
587+
}
588+
}
589+
590+
void run_silentpayments_test_vectors(void) {
591+
size_t i;
592+
593+
594+
for (i = 0; i < sizeof(bip352_test_vectors) / sizeof(bip352_test_vectors[0]); i++) {
595+
const struct bip352_test_vector *test = &bip352_test_vectors[i];
596+
run_silentpayments_test_vector_send(test);
597+
run_silentpayments_test_vector_receive(test);
598+
}
599+
}
600+
352601
void run_silentpayments_tests(void) {
353602
test_recipient_sort();
354603
test_send_api();
355604
test_label_api();
356605
test_recipient_api();
606+
run_silentpayments_test_vectors();
357607
}
358608

359609
#endif

0 commit comments

Comments
 (0)