Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8594676

Browse files
theStackjosibake
authored andcommittedApr 19, 2024
tests: add 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 bf349c2 commit 8594676

File tree

7 files changed

+4480
-0
lines changed

7 files changed

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

0 commit comments

Comments
 (0)
Please sign in to comment.