Bitcoin Core 31.99.0
P2P Digital Currency
tests_impl.h
Go to the documentation of this file.
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 "../../unit_test.h"
11#include "../../util.h"
12#include "../../../src/modules/silentpayments/vectors.h"
13
25static unsigned char MALFORMED_SECKEY[32] = { 0x00 };
26static unsigned char BOB_ADDRESS[2][33] = {
27 {
28 0x02, 0x15, 0x40, 0xae, 0xa8, 0x97, 0x54, 0x7a,
29 0xd4, 0x39, 0xb4, 0xe0, 0xf6, 0x09, 0xe5, 0xf0,
30 0xfa, 0x63, 0xde, 0x89, 0xab, 0x11, 0xed, 0xe3,
31 0x1e, 0x8c, 0xde, 0x4b, 0xe2, 0x19, 0x42, 0x5f,
32 0x23
33 },
34 {
35 0x02, 0x3e, 0xff, 0xf8, 0x18, 0x51, 0x65, 0xea,
36 0x63, 0xa9, 0x92, 0xb3, 0x9f, 0x31, 0xd8, 0xfd,
37 0x8e, 0x0e, 0x64, 0xae, 0xf9, 0xd3, 0x88, 0x07,
38 0x34, 0x97, 0x37, 0x14, 0xa5, 0x3d, 0x83, 0x11,
39 0x8d
40 }
41};
42static unsigned char CAROL_ADDRESS[2][33] = {
43 {
44 0x03, 0xbb, 0xc6, 0x3f, 0x12, 0x74, 0x5d, 0x3b,
45 0x9e, 0x9d, 0x24, 0xc6, 0xcd, 0x7a, 0x1e, 0xfe,
46 0xba, 0xd0, 0xa7, 0xf4, 0x69, 0x23, 0x2f, 0xbe,
47 0xcf, 0x31, 0xfb, 0xa7, 0xb4, 0xf7, 0xdd, 0xed,
48 0xa8
49 },
50 {
51 0x03, 0x81, 0xeb, 0x9a, 0x9a, 0x9e, 0xc7, 0x39,
52 0xd5, 0x27, 0xc1, 0x63, 0x1b, 0x31, 0xb4, 0x21,
53 0x56, 0x6f, 0x5c, 0x2a, 0x47, 0xb4, 0xab, 0x5b,
54 0x1f, 0x6a, 0x68, 0x6d, 0xfb, 0x68, 0xea, 0xb7,
55 0x16
56 }
57};
58static unsigned char BOB_OUTPUT[32] = {
59 0x46, 0x0d, 0x68, 0x08, 0x65, 0x64, 0x45, 0xee,
60 0x4d, 0x4e, 0xc0, 0x8e, 0xba, 0x8a, 0x66, 0xea,
61 0x66, 0x8e, 0x4e, 0x12, 0x98, 0x9a, 0x0e, 0x60,
62 0x4b, 0x5c, 0x36, 0x0e, 0x43, 0xf5, 0x5a, 0xfa
63};
64static unsigned char CAROL_OUTPUT_ONE[32] = {
65 0x4b, 0x81, 0x34, 0x5d, 0x53, 0x89, 0xba, 0xa3,
66 0xd8, 0x93, 0xe2, 0xfb, 0xe7, 0x08, 0xdd, 0x6d,
67 0x82, 0xdc, 0xd8, 0x49, 0xab, 0x03, 0xc1, 0xdb,
68 0x68, 0xbe, 0xc7, 0xe9, 0x2a, 0x45, 0xfa, 0xc5
69};
70static unsigned char CAROL_OUTPUT_TWO[32] = {
71 0xb7, 0xf3, 0xc6, 0x79, 0x30, 0x4a, 0xef, 0x8c,
72 0xc0, 0xc7, 0x61, 0xf1, 0x00, 0x99, 0xdd, 0x7b,
73 0x20, 0x65, 0x20, 0xd7, 0x11, 0x6f, 0xb7, 0x91,
74 0xee, 0x74, 0x54, 0xa2, 0xfc, 0x22, 0x79, 0xf4
75};
76static unsigned char SMALLEST_OUTPOINT[36] = {
77 0x16, 0x9e, 0x1e, 0x83, 0xe9, 0x30, 0x85, 0x33, 0x91,
78 0xbc, 0x6f, 0x35, 0xf6, 0x05, 0xc6, 0x75, 0x4c, 0xfe,
79 0xad, 0x57, 0xcf, 0x83, 0x87, 0x63, 0x9d, 0x3b, 0x40,
80 0x96, 0xc5, 0x4f, 0x18, 0xf4, 0x00, 0x00, 0x00, 0x00
81};
82static unsigned char ALICE_SECKEY[32] = {
83 0xea, 0xdc, 0x78, 0x16, 0x5f, 0xf1, 0xf8, 0xea,
84 0x94, 0xad, 0x7c, 0xfd, 0xc5, 0x49, 0x90, 0x73,
85 0x8a, 0x4c, 0x53, 0xf6, 0xe0, 0x50, 0x7b, 0x42,
86 0x15, 0x42, 0x01, 0xb8, 0xe5, 0xdf, 0xf3, 0xb1
87};
88
89struct label_cache_entry {
90 unsigned char label[33];
91 unsigned char label_tweak[32];
92};
93struct labels_cache {
94 size_t entries_used;
95 struct label_cache_entry entries[10];
96};
98const unsigned char* label_lookup(const unsigned char* key, const void* cache_ptr) {
99 const struct labels_cache* cache;
100 size_t i;
101
102 if (cache_ptr == NULL) {
103 return NULL;
104 }
105 cache = (const struct labels_cache*)cache_ptr;
106 for (i = 0; i < cache->entries_used; i++) {
107 if (secp256k1_memcmp_var(cache->entries[i].label, key, 33) == 0) {
108 return cache->entries[i].label_tweak;
109 }
110 }
111 return NULL;
112}
113
114static void test_recipient_sort_helper(unsigned char (*sp_addresses[3])[2][33], unsigned char (*sp_outputs[3])[32]) {
115 unsigned char const *seckey_ptrs[1];
117 const secp256k1_silentpayments_recipient *recipient_ptrs[3];
118 secp256k1_xonly_pubkey generated_outputs[3];
119 secp256k1_xonly_pubkey *generated_output_ptrs[3];
120 unsigned char xonly_ser[32];
121 size_t i;
122 int ret;
123
124 seckey_ptrs[0] = ALICE_SECKEY;
125 for (i = 0; i < 3; i++) {
126 CHECK(secp256k1_ec_pubkey_parse(CTX, &recipients[i].scan_pubkey, (*sp_addresses[i])[0], 33));
127 CHECK(secp256k1_ec_pubkey_parse(CTX, &recipients[i].spend_pubkey,(*sp_addresses[i])[1], 33));
128 recipients[i].index = i;
129 recipient_ptrs[i] = &recipients[i];
130 generated_output_ptrs[i] = &generated_outputs[i];
131 }
133 generated_output_ptrs,
134 recipient_ptrs, 3,
136 NULL, 0,
137 seckey_ptrs, 1
138 );
139 CHECK(ret == 1);
140 for (i = 0; i < 3; i++) {
141 secp256k1_xonly_pubkey_serialize(CTX, xonly_ser, &generated_outputs[i]);
142 CHECK(secp256k1_memcmp_var(xonly_ser, (*sp_outputs[i]), 32) == 0);
143 }
144}
145
146static void test_recipient_sort(void) {
147 unsigned char (*sp_addresses[3])[2][33];
148 unsigned char (*sp_outputs[3])[32];
149
150 /* With a fixed set of addresses and a fixed set of inputs,
151 * test that we always get the same outputs, regardless of the ordering
152 * of the recipients
153 */
154 sp_addresses[0] = &CAROL_ADDRESS;
155 sp_addresses[1] = &BOB_ADDRESS;
156 sp_addresses[2] = &CAROL_ADDRESS;
157
158 sp_outputs[0] = &CAROL_OUTPUT_ONE;
159 sp_outputs[1] = &BOB_OUTPUT;
160 sp_outputs[2] = &CAROL_OUTPUT_TWO;
161 test_recipient_sort_helper(sp_addresses, sp_outputs);
162
163 sp_addresses[0] = &CAROL_ADDRESS;
164 sp_addresses[1] = &CAROL_ADDRESS;
165 sp_addresses[2] = &BOB_ADDRESS;
166
167 sp_outputs[0] = &CAROL_OUTPUT_ONE;
168 sp_outputs[1] = &CAROL_OUTPUT_TWO;
169 sp_outputs[2] = &BOB_OUTPUT;
170 test_recipient_sort_helper(sp_addresses, sp_outputs);
171
172 sp_addresses[0] = &BOB_ADDRESS;
173 sp_addresses[1] = &CAROL_ADDRESS;
174 sp_addresses[2] = &CAROL_ADDRESS;
175
176 sp_outputs[0] = &BOB_OUTPUT;
177 sp_outputs[1] = &CAROL_OUTPUT_ONE;
178 sp_outputs[2] = &CAROL_OUTPUT_TWO;
179 test_recipient_sort_helper(sp_addresses, sp_outputs);
180}
181
182static void test_send_api(void) {
183 unsigned char (*sp_addresses[2])[2][33];
184 unsigned char const *p[1];
185 secp256k1_keypair const *t[1];
190 secp256k1_keypair taproot;
191 size_t i;
192
193 /* Set up Bob and Carol as the recipients */
194 sp_addresses[0] = &BOB_ADDRESS;
195 sp_addresses[1] = &CAROL_ADDRESS;
196 for (i = 0; i < 2; i++) {
197 CHECK(secp256k1_ec_pubkey_parse(CTX, &r[i].scan_pubkey, (*sp_addresses[i])[0], 33));
198 CHECK(secp256k1_ec_pubkey_parse(CTX, &r[i].spend_pubkey,(*sp_addresses[i])[1], 33));
199 /* Set the index value incorrectly */
200 r[i].index = 0;
201 rp[i] = &r[i];
202 op[i] = &o[i];
203 }
204 /* Set up a taproot key and a plain key for Alice */
206 t[0] = &taproot;
207 p[0] = ALICE_SECKEY;
208
209 /* Fails if the index is set incorrectly */
211
212 /* Set the index correctly for the next tests */
213 for (i = 0; i < 2; i++) {
214 r[i].index = i;
215 }
217
218 /* Check that NULL in "array of pointers" arguments is not allowed */
219 for (i = 0; i < 2; i++) {
220 secp256k1_xonly_pubkey *original_ptr_xpk = op[i];
221 const secp256k1_silentpayments_recipient *original_ptr_rec = rp[i];
222
223 op[i] = NULL;
225 op[i] = original_ptr_xpk;
226
227 rp[i] = NULL;
229 rp[i] = original_ptr_rec;
230 }
231 {
232 secp256k1_keypair const *original_ptr = t[0];
233 t[0] = NULL;
235 t[0] = original_ptr;
236 }
237 {
238 unsigned char const *original_ptr = p[0];
239 p[0] = NULL;
241 p[0] = original_ptr;
242 }
243
244 /* Check that null arguments are handled */
250
251 /* Check correct context is used */
253
254 /* Check that array arguments are verified */
259
260 /* Create malformed keys for Alice by using a key that will overflow */
264 /* Malleate the keypair object so that the secret key is all zeros. We need to keep
265 * public key as is since it is loaded first and would hit an ARG_CHECK if invalid.
266 */
267 memset(&taproot.data[0], 0, 32);
268 /* Check that an invalid plain secret key is caught */
270 /* Check that an invalid keypair is caught */
272 /* Create malformed keys for Alice by using a zero'd seckey */
273 p[0] = MALFORMED_SECKEY;
275 p[0] = ALICE_SECKEY;
276 /* Create malformed recipients by setting all of the public key bytes to zero.
277 * Realistically, this would never happen since a bad public key would get caught when
278 * trying to parse the public key with _ec_pubkey_parse
279 */
280 {
282 memset(&r[1].spend_pubkey, 0, sizeof(r[1].spend_pubkey));
284 r[1].spend_pubkey = tmp;
285 }
286 {
287 secp256k1_pubkey tmp = r[1].scan_pubkey;
288 int32_t ecount = 0;
289
290 memset(&r[1].scan_pubkey, 0, sizeof(r[1].scan_pubkey));
293 CHECK(ecount == 2);
295 r[1].scan_pubkey = tmp;
296 }
297 {
298 unsigned char malformed_spend_key[32] = {
299 0x83, 0xe1, 0x79, 0xdf, 0x51, 0xbb, 0xc9, 0x6f,
300 0xfb, 0x59, 0xb6, 0x2e, 0x57, 0xcf, 0x4e, 0x54,
301 0x71, 0x79, 0x04, 0x9c, 0x01, 0x47, 0x00, 0xfe,
302 0x52, 0xef, 0x5f, 0x53, 0x76, 0x39, 0xec, 0xe0
303 };
304 secp256k1_pubkey neg_spend_pubkey;
305 CHECK(secp256k1_ec_pubkey_create(CTX, &neg_spend_pubkey, malformed_spend_key));
306 CHECK(secp256k1_ec_pubkey_negate(CTX, &neg_spend_pubkey));
307 r[0].spend_pubkey = neg_spend_pubkey;
308 for (i = 0; i < 2; i++) {
309 r[i].index = i;
310 rp[i] = &r[i];
311 }
313 }
314
315 /* check that sending API respects the per-group recipient limit (K_max) */
316 {
317 const size_t total_recipients = 10 * SECP256K1_SILENTPAYMENTS_RECIPIENT_GROUP_LIMIT;
318 secp256k1_silentpayments_recipient *recipients = checked_malloc(&CTX->error_callback, sizeof(*recipients) * total_recipients);
319 const secp256k1_silentpayments_recipient **recipients_ptrs = checked_malloc(&CTX->error_callback, sizeof(*recipients_ptrs) * total_recipients);
320 secp256k1_xonly_pubkey *outputs = checked_malloc(&CTX->error_callback, sizeof(*outputs) * total_recipients);
321 secp256k1_xonly_pubkey **outputs_ptrs = checked_malloc(&CTX->error_callback, sizeof(*outputs_ptrs) * total_recipients);
322 size_t test_num_recipients;
323
324 for (i = 0; i < total_recipients; i++) {
325 /* use the same scan/spend pubkey for every recipient initially; the scan pubkeys
326 * will change later on for each test case to modify the group sizes, while the
327 * spend pubkeys will remain unchanged, as they are not relevant for the scenarios */
328 recipients[i].scan_pubkey = r[1].scan_pubkey;
329 recipients[i].spend_pubkey = r[1].spend_pubkey;
330 recipients[i].index = i;
331 recipients_ptrs[i] = &recipients[i];
332 outputs_ptrs[i] = &outputs[i];
333 }
334
335 /* one group with the number of recipients being just on the limit => succeeds */
337 CHECK(secp256k1_silentpayments_sender_create_outputs(CTX, outputs_ptrs, recipients_ptrs,
338 test_num_recipients, SMALLEST_OUTPOINT, NULL, 0, p, 1) == 1);
339
340 /* one group with the number of recipients exceeding the limit => fails */
341 test_num_recipients = SECP256K1_SILENTPAYMENTS_RECIPIENT_GROUP_LIMIT + 1;
342 CHECK(secp256k1_silentpayments_sender_create_outputs(CTX, outputs_ptrs, recipients_ptrs,
343 test_num_recipients, SMALLEST_OUTPOINT, NULL, 0, p, 1) == 0);
344
345 /* multiple groups with each being just on the limit => succeeds */
346 for (i = 0; i < total_recipients; i++) {
347 /* create recipient blocks of K_max size, each with different tweak values */
348 uint32_t tweak_value = i / SECP256K1_SILENTPAYMENTS_RECIPIENT_GROUP_LIMIT;
349 unsigned char tweak[32] = {0};
350 secp256k1_write_be32(&tweak[28], tweak_value);
351 CHECK(secp256k1_ec_pubkey_tweak_add(CTX, &recipients[i].scan_pubkey, tweak) == 1);
352 }
353 test_num_recipients = total_recipients;
354 CHECK(secp256k1_silentpayments_sender_create_outputs(CTX, outputs_ptrs, recipients_ptrs,
355 test_num_recipients, SMALLEST_OUTPOINT, NULL, 0, p, 1) == 1);
356
357 /* multiple groups, one of them exceeding the limit => fails */
358 for (i = 0; i < total_recipients; i++) { /* restore original order first */
359 recipients_ptrs[i] = &recipients[i];
360 }
363 test_num_recipients = total_recipients;
364 CHECK(secp256k1_silentpayments_sender_create_outputs(CTX, outputs_ptrs, recipients_ptrs,
365 test_num_recipients, SMALLEST_OUTPOINT, NULL, 0, p, 1) == 0);
366
367 free(outputs_ptrs);
368 free(outputs);
369 free(recipients_ptrs);
370 free(recipients);
371 }
372}
373
374static void test_label_api(void) {
376 secp256k1_pubkey s, ls, e; /* spend pk, labeled spend pk, expected labeled spend pk */
377 unsigned char lt[32]; /* label tweak */
378 unsigned char label_ser[33]; /* serialized label */
379 const unsigned char expected[33] = {
380 0x03, 0xdc, 0x7f, 0x09, 0x9a, 0xbe, 0x95, 0x7a,
381 0x58, 0x43, 0xd2, 0xb6, 0xbb, 0x35, 0x79, 0x61,
382 0x5c, 0x60, 0x36, 0xa4, 0x9b, 0x86, 0xf4, 0xbe,
383 0x46, 0x38, 0x60, 0x28, 0xa8, 0x1a, 0x77, 0xd4,
384 0x91
385 };
386
387 /* Create a label and labeled spend public key, verify we get the expected result */
391 CHECK(secp256k1_ec_pubkey_parse(CTX, &e, expected, 33));
392 CHECK(secp256k1_ec_pubkey_cmp(CTX, &ls, &e) == 0);
393
394 /* Check label (de)serialization round-trip */
395 {
397 unsigned char parsed_label_ser[33];
398 static const unsigned char invalid_label_ser[33] = {0};
399
401 CHECK(secp256k1_silentpayments_recipient_label_parse(CTX, &parsed_label, label_ser));
402 CHECK(secp256k1_silentpayments_recipient_label_serialize(CTX, parsed_label_ser, &parsed_label));
403 CHECK(secp256k1_memcmp_var(label_ser, parsed_label_ser, 33) == 0);
404
405 CHECK(secp256k1_silentpayments_recipient_label_parse(CTX, &parsed_label, invalid_label_ser) == 0);
407 }
408
409 /* Check null values are handled */
420 /* Check that creating a label with an invalid scan key fails */
423 /* Check for malformed spend public key and label, i.e., any single pubkey is malformed or the public
424 * keys are valid but sum up to zero.
425 */
426 {
427 secp256k1_pubkey neg_spend_pubkey = s;
428 unsigned char neg_spend_label_ser[33];
429 size_t serlen = 33;
430 secp256k1_silentpayments_label neg_spend_label;
431
432 CHECK(secp256k1_ec_pubkey_negate(CTX, &neg_spend_pubkey));
433 CHECK(secp256k1_ec_pubkey_serialize(CTX, neg_spend_label_ser, &serlen, &neg_spend_pubkey, SECP256K1_EC_COMPRESSED));
434 CHECK(secp256k1_silentpayments_recipient_label_parse(CTX, &neg_spend_label, neg_spend_label_ser));
435
437 /* Also test with a malformed spend public key. */
438 memset(&s, 0, sizeof(s));
440 /* Reset s back to a valid public key for the next test. */
442 memset(&l, 0, sizeof(l));
444 /* Reset l back to a valid public key for the next test */
446 memset(&s, 0, sizeof(s));
448 }
449}
450
451static void test_recipient_api(void) {
452 secp256k1_silentpayments_prevouts_summary ps; /* prevouts_summary */
453 secp256k1_silentpayments_found_output f; /* a silent payment found output */
454 secp256k1_silentpayments_found_output *fp[1]; /* array of pointers to found outputs */
455 secp256k1_xonly_pubkey t; /* taproot x-only public key */
456 secp256k1_xonly_pubkey malformed_t; /* malformed x-only public key */
457 secp256k1_xonly_pubkey const *tp[1]; /* array of pointers to xonly pks */
458 secp256k1_pubkey p; /* plain public key */
459 secp256k1_pubkey malformed_p; /* malformed public key */
460 secp256k1_pubkey const *pp[1]; /* array of pointers to plain pks */
461 uint32_t n_f; /* number of found outputs */
462
464 memset(&malformed_p, 0, sizeof(malformed_p));
465 memset(&malformed_t, 0, sizeof(malformed_t));
467 tp[0] = &t;
468 pp[0] = &p;
469 fp[0] = &f;
471 /* Reusing a prevouts_summary after a failure must not leave the old object valid. */
472 {
473 secp256k1_pubkey neg_p = p;
474 secp256k1_pubkey const *pp_sum_zero[2];
475
477 pp_sum_zero[0] = &p;
478 pp_sum_zero[1] = &neg_p;
480 CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 1, ALICE_SECKEY, &ps, &p, NULL, NULL));
482 }
483 /* Check that malformed input public keys are caught. Input public keys summing to zero is tested later,
484 * in the BIP0352 test vectors.
485 */
486 pp[0] = &malformed_p;
488 pp[0] = &p;
489 /* Check that malformed x-only input public keys are caught. */
490 tp[0] = &malformed_t;
492 tp[0] = &t;
493
494 /* Check that NULL in "array of pointers" arguments is not allowed */
495 {
496 secp256k1_xonly_pubkey const *original_ptr = tp[0];
497 tp[0] = NULL;
499 tp[0] = original_ptr;
500 }
501 {
502 secp256k1_pubkey const *original_ptr = pp[0];
503 pp[0] = NULL;
505 pp[0] = original_ptr;
506 }
507
508 /* Check null values are handled */
513
519
520 /* check the _recipient_scan_outputs cornercase where internal tweaking would fail;
521 this is the case if the recipient spend public key is P = -(create_output_tweak(shared_secret, k))*G */
522 {
523 unsigned char output_tweak[32] = {
524 0x96, 0x32, 0xb4, 0x06, 0xeb, 0x56, 0xcc, 0xb2,
525 0x0f, 0xc6, 0xe5, 0x2c, 0x41, 0xd5, 0x73, 0xb2,
526 0xae, 0xa0, 0x45, 0x07, 0x63, 0xf1, 0xf6, 0x22,
527 0xfa, 0x87, 0xc2, 0x4c, 0x7d, 0x80, 0x58, 0x62,
528 };
529 secp256k1_pubkey neg_spend_pubkey;
530 CHECK(secp256k1_ec_pubkey_create(CTX, &neg_spend_pubkey, output_tweak));
531 CHECK(secp256k1_ec_pubkey_negate(CTX, &neg_spend_pubkey));
532 CHECK(secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 1, ALICE_SECKEY, &ps, &neg_spend_pubkey, &label_lookup, &labels_cache) == 0);
533 }
534 /* check the _recipients_scan_outputs cornercase where the output_tweak is the negation of the label_tweak */
535 {
536 unsigned char output_tweak[32] = {
537 0x96, 0x32, 0xb4, 0x06, 0xeb, 0x56, 0xcc, 0xb2,
538 0x0f, 0xc6, 0xe5, 0x2c, 0x41, 0xd5, 0x73, 0xb2,
539 0xae, 0xa0, 0x45, 0x07, 0x63, 0xf1, 0xf6, 0x22,
540 0xfa, 0x87, 0xc2, 0x4c, 0x7d, 0x80, 0x58, 0x62,
541 };
542 static const unsigned char zero[32] = {0};
543 secp256k1_pubkey spk, neg_label_pubkey;
544 secp256k1_xonly_pubkey output_xonly;
545 secp256k1_xonly_pubkey const *output_xonly_ptrs[1];
546 size_t len = 33;
547 uint32_t found;
549 CHECK(secp256k1_xonly_pubkey_from_pubkey(CTX, &output_xonly, NULL, &spk));
550 output_xonly_ptrs[0] = &output_xonly;
551 CHECK(secp256k1_ec_seckey_negate(CTX, output_tweak));
552 CHECK(secp256k1_ec_pubkey_create(CTX, &neg_label_pubkey, output_tweak));
554 memcpy(labels_cache.entries[0].label_tweak, output_tweak, 32);
556 found = 0;
557 CHECK(secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &found, output_xonly_ptrs, 1, ALICE_SECKEY, &ps, &spk, &label_lookup, &labels_cache));
558 CHECK(found == 1);
559 CHECK(secp256k1_memcmp_var(fp[0]->tweak, zero, 32) == 0);
560 CHECK(fp[0]->found_with_label == 1);
561 {
562 unsigned char found_label_ser[33];
563 CHECK(secp256k1_silentpayments_recipient_label_serialize(CTX, found_label_ser, &fp[0]->label));
564 CHECK(secp256k1_memcmp_var(found_label_ser, labels_cache.entries[0].label, 33) == 0);
565 }
566 }
567
568 n_f = 0;
572 CHECK(secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 1, ALICE_SECKEY, &ps, &p, NULL, NULL));
573
574 /* Check that NULL in "array of pointers" arguments is not allowed */
575 {
576 secp256k1_silentpayments_found_output *original_ptr = fp[0];
577 fp[0] = NULL;
578 CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 1, ALICE_SECKEY, &ps, &p, NULL, NULL));
579 fp[0] = original_ptr;
580 }
581 {
582 secp256k1_xonly_pubkey const *original_ptr = tp[0];
583 tp[0] = NULL;
584 CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 1, ALICE_SECKEY, &ps, &p, NULL, NULL));
585 tp[0] = original_ptr;
586 }
587
596
597 /* Check that malformed secret key, public keys, and prevouts_summary arguments are handled */
598 CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 1, ALICE_SECKEY, &ps, &malformed_p, NULL, NULL));
599 CHECK(secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 1, MALFORMED_SECKEY, &ps, &p, NULL, NULL) == 0);
600 memset(&ps, 0, sizeof(ps));
601 CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 1, ALICE_SECKEY, &ps, &p, NULL, NULL));
602}
603
605 static const unsigned char sender_seckey[32] = { 1 };
606 static const unsigned char scan_seckey[32] = { 2 };
607 static const unsigned char spend_seckey[32] = { 3 };
608 secp256k1_pubkey sender_pubkey, scan_pubkey, unlabeled_spend_pubkey, labeled_spend_pubkey;
609 const secp256k1_pubkey *prevout_pubkeys[1];
610 const unsigned char *sender_seckeys[1];
614 const secp256k1_silentpayments_recipient *recipients[1];
615 secp256k1_xonly_pubkey labeled_output, direct_output;
616 secp256k1_xonly_pubkey *generated_outputs[1];
617 const secp256k1_xonly_pubkey *tx_outputs[2];
620 struct labels_cache cache;
621 unsigned char found_label[33];
622 uint32_t n_found_outputs;
623
624 CHECK(secp256k1_ec_pubkey_create(CTX, &sender_pubkey, sender_seckey));
625 CHECK(secp256k1_ec_pubkey_create(CTX, &scan_pubkey, scan_seckey));
626 CHECK(secp256k1_ec_pubkey_create(CTX, &unlabeled_spend_pubkey, spend_seckey));
627 prevout_pubkeys[0] = &sender_pubkey;
628 sender_seckeys[0] = sender_seckey;
630 CTX, &prevouts_summary, SMALLEST_OUTPOINT, NULL, 0, prevout_pubkeys, 1));
631
632 memset(&cache, 0, sizeof(cache));
634 CTX, &label, cache.entries[0].label_tweak, scan_seckey, 1));
636 cache.entries_used = 1;
638 CTX, &labeled_spend_pubkey, &unlabeled_spend_pubkey, &label));
639
640 recipient.scan_pubkey = scan_pubkey;
641 recipient.spend_pubkey = labeled_spend_pubkey;
642 recipient.index = 0;
643 recipients[0] = &recipient;
644 /* Produce two k = 0 outputs, with the labeled output first. */
645 generated_outputs[0] = &labeled_output;
647 CTX, generated_outputs, recipients, 1, SMALLEST_OUTPOINT, NULL, 0, sender_seckeys, 1));
648 recipient.spend_pubkey = unlabeled_spend_pubkey;
649 generated_outputs[0] = &direct_output;
651 CTX, generated_outputs, recipients, 1, SMALLEST_OUTPOINT, NULL, 0, sender_seckeys, 1));
652 CHECK(secp256k1_xonly_pubkey_cmp(CTX, &labeled_output, &direct_output) != 0);
653
654 tx_outputs[0] = &labeled_output;
655 tx_outputs[1] = &direct_output;
656 found_outputs[0] = &found_output[0];
657 found_outputs[1] = &found_output[1];
659 CTX, found_outputs, &n_found_outputs, tx_outputs, 2, scan_seckey, &prevouts_summary,
660 &unlabeled_spend_pubkey, label_lookup, &cache));
661 CHECK(n_found_outputs == 1);
662 CHECK(secp256k1_xonly_pubkey_cmp(CTX, &found_output[0].output, &labeled_output) == 0);
663 CHECK(found_output[0].found_with_label);
664 CHECK(secp256k1_silentpayments_recipient_label_serialize(CTX, found_label, &found_output[0].label));
665 CHECK(secp256k1_memcmp_var(found_label, cache.entries[0].label, sizeof(found_label)) == 0);
666}
667
671 static secp256k1_xonly_pubkey generated_outputs[MAX_OUTPUTS_PER_TEST_CASE];
672 static secp256k1_xonly_pubkey *generated_output_ptrs[MAX_OUTPUTS_PER_TEST_CASE];
674 static secp256k1_keypair const *keypair_ptrs[MAX_INPUTS_PER_TEST_CASE];
675 static unsigned char const *seckeys[MAX_INPUTS_PER_TEST_CASE];
676 unsigned char created_output[32];
677 size_t i, j, k;
678 int match, ret;
679
680 /* Check that sender creates expected outputs */
681 j = 0; /* index to expand given recipient entries (with possible repeated entries) to full list */
682 for (i = 0; i < test->num_recipient_entries; i++) {
683 size_t c;
684 secp256k1_pubkey scan_pubkey, spend_pubkey;
687 for (c = 0; c < test->recipient_pubkeys[i].count; c++) {
688 recipients[j].scan_pubkey = scan_pubkey;
689 recipients[j].spend_pubkey = spend_pubkey;
690 recipients[j].index = j;
691 recipient_ptrs[j] = &recipients[j];
692 generated_output_ptrs[j] = &generated_outputs[j];
693 j++;
694 }
695 }
696 CHECK(j == test->num_outputs);
697 for (i = 0; i < test->num_plain_inputs; i++) {
698 seckeys[i] = test->plain_seckeys[i];
699 }
700 for (i = 0; i < test->num_taproot_inputs; i++) {
701 CHECK(secp256k1_keypair_create(CTX, &keypairs[i], test->taproot_seckeys[i]));
702 keypair_ptrs[i] = &keypairs[i];
703 }
704 {
705 int32_t ecount = 0;
708 generated_output_ptrs,
709 recipient_ptrs,
710 test->num_outputs,
711 test->outpoint_smallest,
712 test->num_taproot_inputs > 0 ? keypair_ptrs : NULL, test->num_taproot_inputs,
713 test->num_plain_inputs > 0 ? seckeys : NULL, test->num_plain_inputs
714 );
716 /* We expect exactly one ARG_CHECK if the number of input keys was 0. */
717 CHECK(ecount == ((test->num_taproot_inputs + test->num_plain_inputs) == 0));
718 }
719 /* If the expected number of recipient outputs is zero, check that the creation of outputs
720 * failed (e.g. due to input keys summing to zero) */
721 if (test->num_recipient_outputs == 0) {
722 CHECK(!ret);
723 return;
724 }
725 CHECK(ret);
726
727 match = 0;
728 for (i = 0; i < test->num_output_sets; i++) {
729 size_t n_matches = 0;
730 for (j = 0; j < test->num_outputs; j++) {
731 CHECK(secp256k1_xonly_pubkey_serialize(CTX, created_output, &generated_outputs[j]));
732 /* Loop over both lists to ensure tests don't fail due to different orderings of outputs */
733 for (k = 0; k < test->num_recipient_outputs; k++) {
734 if (secp256k1_memcmp_var(created_output, test->recipient_outputs[i][k], 32) == 0) {
735 n_matches++;
736 break;
737 }
738 }
739 }
740 if (n_matches == test->num_recipient_outputs) {
741 match = 1;
742 break;
743 }
744 }
745 CHECK(match);
746}
747
749 static secp256k1_pubkey pubkeys_objs[MAX_INPUTS_PER_TEST_CASE];
750 static secp256k1_xonly_pubkey xonly_pubkeys_objs[MAX_INPUTS_PER_TEST_CASE];
753 static secp256k1_pubkey const *pubkeys[MAX_INPUTS_PER_TEST_CASE];
754 static secp256k1_xonly_pubkey const *xonly_pubkeys[MAX_INPUTS_PER_TEST_CASE];
755 static secp256k1_xonly_pubkey const *tx_outputs[MAX_OUTPUTS_PER_TEST_CASE];
757 secp256k1_pubkey recipient_scan_pubkey;
758 secp256k1_pubkey recipient_spend_pubkey;
760 size_t i,j;
761 int ret;
762 uint32_t n_found = 0;
763 unsigned char found_output[32];
765
766
767 /* prepare the inputs */
768 for (i = 0; i < test->num_plain_inputs; i++) {
769 CHECK(secp256k1_ec_pubkey_parse(CTX, &pubkeys_objs[i], test->plain_pubkeys[i], 33));
770 pubkeys[i] = &pubkeys_objs[i];
771 }
772 for (i = 0; i < test->num_taproot_inputs; i++) {
773 CHECK(secp256k1_xonly_pubkey_parse(CTX, &xonly_pubkeys_objs[i], test->xonly_pubkeys[i]));
774 xonly_pubkeys[i] = &xonly_pubkeys_objs[i];
775 }
776 {
777 int32_t ecount = 0;
780 test->outpoint_smallest,
781 test->num_taproot_inputs > 0 ? xonly_pubkeys : NULL, test->num_taproot_inputs,
782 test->num_plain_inputs > 0 ? pubkeys : NULL, test->num_plain_inputs
783 );
785 /* We expect exactly one ARG_CHECK if the number of input keys was 0. */
786 CHECK(ecount == ((test->num_taproot_inputs + test->num_plain_inputs) == 0));
787 }
788 /* If we are unable to create the prevouts_summary object, e.g., the input public keys sum to
789 * zero, check that the expected number of recipient outputs for this test case is zero
790 */
791 if (!ret) {
792 CHECK(subtest->num_found_output_pubkeys == 0);
793 return;
794 }
795 /* prepare the outputs */
796 for (i = 0; i < subtest->num_to_scan_outputs; i++) {
797 CHECK(secp256k1_xonly_pubkey_parse(CTX, &tx_output_objs[i], subtest->to_scan_outputs[i]));
798 tx_outputs[i] = &tx_output_objs[i];
799 found_outputs[i] = &found_output_objs[i];
800 }
801
802 /* scan / spend pubkeys are not in the given data of the recipient part, so let's compute them */
803 CHECK(secp256k1_ec_pubkey_create(CTX, &recipient_scan_pubkey, subtest->scan_seckey));
804 CHECK(secp256k1_ec_pubkey_create(CTX, &recipient_spend_pubkey, subtest->spend_seckey));
805
806 /* create labels cache */
808 for (i = 0; i < subtest->num_labels; i++) {
809 unsigned int m = subtest->label_integers[i];
814 }
816 found_outputs, &n_found,
817 tx_outputs, subtest->num_to_scan_outputs,
818 subtest->scan_seckey,
819 &prevouts_summary,
820 &recipient_spend_pubkey,
822 );
823 if (subtest->full_check) {
824 /* compare expected and scanned outputs (including calculated seckey tweaks and signatures) */
825#ifdef ENABLE_MODULE_SCHNORRSIG
826 static unsigned char found_signatures[MAX_OUTPUTS_PER_TEST_CASE][64];
827 /* sha256("message") */
828 static unsigned char MSG32[32] = {
829 0xab,0x53,0x0a,0x13,0xe4,0x59,0x14,0x98,
830 0x2b,0x79,0xf9,0xb7,0xe3,0xfb,0xa9,0x94,
831 0xcf,0xd1,0xf3,0xfb,0x22,0xf7,0x1c,0xea,
832 0x1a,0xfb,0xf0,0x2b,0x46,0x0c,0x6d,0x1d
833 };
834 /* sha256("random auxiliary data") */
835 static unsigned char AUX32[32] = {
836 0x0b,0x3f,0xdd,0xfd,0x67,0xbf,0x76,0xae,
837 0x76,0x39,0xee,0x73,0x5b,0x70,0xff,0x15,
838 0x83,0xfd,0x92,0x48,0xc0,0x57,0xd2,0x86,
839 0x07,0xa2,0x15,0xf4,0x0b,0x0a,0x3e,0xcc
840 };
841 for (i = 0; i < n_found; i++) {
842 unsigned char full_seckey[32];
843 secp256k1_keypair keypair;
844 unsigned char signature[64];
845 memcpy(&full_seckey, subtest->spend_seckey, 32);
846 CHECK(secp256k1_ec_seckey_tweak_add(CTX, full_seckey, found_outputs[i]->tweak));
847 CHECK(secp256k1_keypair_create(CTX, &keypair, full_seckey));
848 CHECK(secp256k1_schnorrsig_sign32(CTX, signature, MSG32, &keypair, AUX32));
849 memcpy(found_signatures[i], signature, 64);
850 }
851#endif
852
853 for (i = 0; i < n_found; i++) {
854 int match = 0;
855 CHECK(secp256k1_xonly_pubkey_serialize(CTX, found_output, &found_outputs[i]->output));
856 for (j = 0; j < subtest->num_found_output_pubkeys; j++) {
857 if (secp256k1_memcmp_var(&found_output, subtest->found_output_pubkeys[j], 32) == 0) {
858 CHECK(secp256k1_memcmp_var(found_outputs[i]->tweak, subtest->found_seckey_tweaks[j], 32) == 0);
859#ifdef ENABLE_MODULE_SCHNORRSIG
860 CHECK(secp256k1_memcmp_var(found_signatures[i], subtest->found_signatures[j], 64) == 0);
861#endif
862 match = 1;
863 break;
864 }
865 }
866 CHECK(match);
867
868 if (subtest->num_labels == 0) {
869 /* if the test case doesn't involve labels, we must not have any labeled matches */
870 CHECK(!found_outputs[i]->found_with_label);
871 } else if (found_outputs[i]->found_with_label) {
872 /* if the test case involves labels and we have a labeled match, verify that the returned
873 * label is in the list of expected ones by manually checking against the label cache
874 * (note that the test vectors only contain a list of used labels, but not exactly which one
875 * of these have been applied for each individual output, so that's the best we can do) */
876 unsigned char found_label_ser[33];
877 const unsigned char *found_label_tweak;
878 CHECK(secp256k1_silentpayments_recipient_label_serialize(CTX, found_label_ser, &found_outputs[i]->label));
879 found_label_tweak = label_lookup(found_label_ser, &labels_cache);
880 CHECK(found_label_tweak != NULL);
881 }
882 }
883 }
884 CHECK(n_found == subtest->num_found_output_pubkeys);
885}
886
889 {
890 /* "BIP0352/Inputs" */
891 static const unsigned char tag[] = {'B','I','P','0','3','5','2','/','I','n','p','u','t','s'};
893 test_sha256_tag_midstate(&CTX->hash_ctx, &sha, tag, sizeof(tag));
894 }
895 {
896 /* "BIP0352/SharedSecret" */
897 static const unsigned char tag[] = {'B','I','P','0','3','5','2','/','S','h','a','r','e','d', 'S','e','c','r','e','t'};
899 test_sha256_tag_midstate(&CTX->hash_ctx, &sha, tag, sizeof(tag));
900 }
901 {
902 /* "BIP0352/Label" */
903 static const unsigned char tag[] = {'B','I','P','0','3','5','2','/','L','a','b','e','l'};
905 test_sha256_tag_midstate(&CTX->hash_ctx, &sha, tag, sizeof(tag));
906 }
907}
908
909
911 size_t i, j;
912
913 for (i = 0; i < ARRAY_SIZE(bip352_test_vectors); i++) {
914 const struct bip352_test_vector *test = &bip352_test_vectors[i];
916 for (j = 0; j < test->num_receive_subtests; j++) {
918 }
919 }
920}
921
922/* --- Test registry --- */
923static const struct tf_test_entry tests_silentpayments[] = {
931};
932
933#endif
int ret
#define CHECK(cond)
Unconditional failure on condition failure.
Definition: util.h:35
static int tweak(const secp256k1_context *ctx, secp256k1_xonly_pubkey *agg_pk, secp256k1_musig_keyagg_cache *cache)
Definition: musig.c:64
static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n)
Semantics like memcmp.
Definition: util.h:282
#define ARRAY_SIZE(arr)
Definition: util.h:195
static SECP256K1_INLINE void secp256k1_write_be32(unsigned char *p, uint32_t x)
Definition: util.h:437
static SECP256K1_INLINE void * checked_malloc(const secp256k1_callback *cb, size_t size)
Definition: util.h:173
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_negate(const secp256k1_context *ctx, unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Negates a secret key in place.
Definition: secp256k1.c:655
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize a pubkey object into a serialized byte sequence.
Definition: secp256k1.c:287
SECP256K1_API int secp256k1_ec_pubkey_negate(const secp256k1_context *ctx, secp256k1_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Negates a public key in place.
Definition: secp256k1.c:670
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_cmp(const secp256k1_context *ctx, const secp256k1_pubkey *pubkey1, const secp256k1_pubkey *pubkey2) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compare two public keys using lexicographic (of compressed serialization) order.
Definition: secp256k1.c:313
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Verify an elliptic curve secret key.
Definition: secp256k1.c:616
SECP256K1_API void secp256k1_context_set_illegal_callback(secp256k1_context *ctx, void(*fun)(const char *message, void *data), const void *data) SECP256K1_ARG_NONNULL(1)
Set a callback function to be called when an illegal argument is passed to an API call.
Definition: secp256k1.c:202
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *input, size_t inputlen) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a variable-length public key into the pubkey object.
Definition: secp256k1.c:269
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the public key for a secret key.
Definition: secp256k1.c:637
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:225
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a public key by adding tweak times the generator to it.
Definition: secp256k1.c:720
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a secret key by adding tweak to it.
Definition: secp256k1.c:697
SECP256K1_API int secp256k1_xonly_pubkey_cmp(const secp256k1_context *ctx, const secp256k1_xonly_pubkey *pk1, const secp256k1_xonly_pubkey *pk2) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compare two x-only public keys using lexicographic order.
Definition: main_impl.h:66
SECP256K1_API int secp256k1_xonly_pubkey_from_pubkey(const secp256k1_context *ctx, secp256k1_xonly_pubkey *xonly_pubkey, int *pk_parity, const secp256k1_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4)
Converts a secp256k1_pubkey into a secp256k1_xonly_pubkey.
Definition: main_impl.h:106
SECP256K1_API int secp256k1_xonly_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output32, const secp256k1_xonly_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize an xonly_pubkey object into a 32-byte sequence.
Definition: main_impl.h:51
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_create(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the keypair for a valid secret key.
Definition: main_impl.h:203
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_parse(const secp256k1_context *ctx, secp256k1_xonly_pubkey *pubkey, const unsigned char *input32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a 32-byte sequence into a xonly_pubkey object.
Definition: main_impl.h:29
SECP256K1_API int secp256k1_schnorrsig_sign32(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, const unsigned char *aux_rand32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create a Schnorr signature.
Definition: main_impl.h:188
SECP256K1_API int secp256k1_silentpayments_recipient_label_serialize(const secp256k1_context *ctx, unsigned char *out33, const secp256k1_silentpayments_label *label) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize a Silent Payments label.
Definition: main_impl.h:378
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_recipient_prevouts_summary_create(const secp256k1_context *ctx, secp256k1_silentpayments_prevouts_summary *prevouts_summary, const unsigned char *outpoint_smallest36, const secp256k1_xonly_pubkey *const *xonly_pubkeys, size_t n_xonly_pubkeys, const secp256k1_pubkey *const *pubkeys, size_t n_pubkeys) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute Silent Payments prevouts summary from prevout public keys and transaction inputs.
Definition: main_impl.h:488
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_recipient_label_parse(const secp256k1_context *ctx, secp256k1_silentpayments_label *label, const unsigned char *in33) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a Silent Payments label.
Definition: main_impl.h:362
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_sender_create_outputs(const secp256k1_context *ctx, secp256k1_xonly_pubkey **generated_outputs, const secp256k1_silentpayments_recipient **recipients, size_t n_recipients, const unsigned char *outpoint_smallest36, const secp256k1_keypair *const *keypairs, size_t n_keypairs, const unsigned char *const *seckeys, size_t n_seckeys) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(5)
Create Silent Payments outputs for recipient(s).
Definition: main_impl.h:187
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_recipient_create_labeled_spend_pubkey(const secp256k1_context *ctx, secp256k1_pubkey *labeled_spend_pubkey, const secp256k1_pubkey *unlabeled_spend_pubkey, const secp256k1_silentpayments_label *label) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create Silent Payments labeled spend public key.
Definition: main_impl.h:430
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_recipient_scan_outputs(const secp256k1_context *ctx, secp256k1_silentpayments_found_output **found_outputs, uint32_t *n_found_outputs, const secp256k1_xonly_pubkey *const *tx_outputs, size_t n_tx_outputs, const unsigned char *scan_key32, const secp256k1_silentpayments_prevouts_summary *prevouts_summary, const secp256k1_pubkey *unlabeled_spend_pubkey, secp256k1_silentpayments_label_lookup label_lookup, const void *label_context) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(6) SECP256K1_ARG_NONNULL(7) SECP256K1_ARG_NONNULL(8)
Scan for Silent Payments transaction outputs.
Definition: main_impl.h:603
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_recipient_label_create(const secp256k1_context *ctx, secp256k1_silentpayments_label *label, unsigned char *label_tweak32, const unsigned char *scan_key32, uint32_t m) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create Silent Payments label tweak and label.
Definition: main_impl.h:393
#define SECP256K1_SILENTPAYMENTS_RECIPIENT_GROUP_LIMIT
This module provides an implementation for Silent Payments, as specified in BIP352.
static void secp256k1_silentpayments_sha256_init_sharedsecret(secp256k1_sha256 *hash)
Set hash state to the BIP340 tagged hash midstate for "BIP0352/SharedSecret".
Definition: main_impl.h:110
static void secp256k1_silentpayments_sha256_init_inputs(secp256k1_sha256 *hash)
Set hash state to the BIP340 tagged hash midstate for "BIP0352/Inputs".
Definition: main_impl.h:57
static void secp256k1_silentpayments_sha256_init_label(secp256k1_sha256 *hash)
Set hash state to the BIP340 tagged hash midstate for "BIP0352/Label".
Definition: main_impl.h:339
static void test_recipient_sort(void)
Definition: tests_impl.h:146
static void test_recipient_scan_label_precedes_direct_match(void)
Definition: tests_impl.h:604
void run_silentpayments_test_vector_receive(const struct bip352_test_vector *test, const struct bip352_receive_subtest *subtest)
Definition: tests_impl.h:748
static unsigned char SMALLEST_OUTPOINT[36]
Definition: tests_impl.h:76
static unsigned char CAROL_ADDRESS[2][33]
Definition: tests_impl.h:42
static unsigned char BOB_OUTPUT[32]
Definition: tests_impl.h:58
static const struct tf_test_entry tests_silentpayments[]
Definition: tests_impl.h:923
const unsigned char * label_lookup(const unsigned char *key, const void *cache_ptr)
Definition: tests_impl.h:98
static unsigned char BOB_ADDRESS[2][33]
Definition: tests_impl.h:26
static unsigned char ALICE_SECKEY[32]
Definition: tests_impl.h:82
static void test_label_api(void)
Definition: tests_impl.h:374
void run_silentpayments_test_vectors(void)
Definition: tests_impl.h:910
static void test_recipient_api(void)
Definition: tests_impl.h:451
static unsigned char CAROL_OUTPUT_TWO[32]
Definition: tests_impl.h:70
static unsigned char MALFORMED_SECKEY[32]
Constants.
Definition: tests_impl.h:25
static unsigned char CAROL_OUTPUT_ONE[32]
Definition: tests_impl.h:64
void run_silentpayments_test_vector_send(const struct bip352_test_vector *test)
Definition: tests_impl.h:668
static void test_send_api(void)
Definition: tests_impl.h:182
static void test_recipient_sort_helper(unsigned char(*sp_addresses[3])[2][33], unsigned char(*sp_outputs[3])[32])
Definition: tests_impl.h:114
static void silentpayments_sha256_tag_test(void)
Definition: tests_impl.h:887
static const struct bip352_test_vector bip352_test_vectors[SECP256K1_SILENTPAYMENTS_NUMBER_TESTVECTORS]
Definition: vectors.h:61
#define MAX_OUTPUTS_PER_TEST_CASE
Definition: vectors.h:6
#define MAX_INPUTS_PER_TEST_CASE
Definition: vectors.h:5
unsigned char found_output_pubkeys[MAX_OUTPUTS_PER_TEST_CASE][32]
Definition: vectors.h:30
unsigned char to_scan_outputs[MAX_OUTPUTS_PER_TEST_CASE][32]
Definition: vectors.h:23
unsigned char scan_seckey[32]
Definition: vectors.h:20
unsigned char found_seckey_tweaks[MAX_OUTPUTS_PER_TEST_CASE][32]
Definition: vectors.h:31
unsigned char spend_seckey[32]
Definition: vectors.h:21
size_t num_to_scan_outputs
Definition: vectors.h:22
unsigned int label_integers[MAX_LABELS_PER_TEST_CASE]
Definition: vectors.h:25
unsigned char found_signatures[MAX_OUTPUTS_PER_TEST_CASE][64]
Definition: vectors.h:32
size_t num_found_output_pubkeys
Definition: vectors.h:29
unsigned char scan_pubkey[33]
Definition: vectors.h:13
unsigned char spend_pubkey[33]
Definition: vectors.h:14
size_t num_recipient_entries
Definition: vectors.h:47
struct bip352_receive_subtest receive_subtests[MAX_RECEIVE_SUBTESTS]
Definition: vectors.h:56
size_t num_receive_subtests
Definition: vectors.h:55
unsigned char recipient_outputs[MAX_PERMUTATIONS_PER_SENDING_TEST_CASE][MAX_OUTPUTS_PER_TEST_CASE][32]
Definition: vectors.h:53
unsigned char xonly_pubkeys[MAX_INPUTS_PER_TEST_CASE][32]
Definition: vectors.h:42
size_t num_plain_inputs
Definition: vectors.h:37
unsigned char outpoint_smallest[36]
Definition: vectors.h:43
size_t num_recipient_outputs
Definition: vectors.h:52
unsigned char plain_pubkeys[MAX_INPUTS_PER_TEST_CASE][33]
Definition: vectors.h:39
unsigned char plain_seckeys[MAX_INPUTS_PER_TEST_CASE][32]
Definition: vectors.h:38
unsigned char taproot_seckeys[MAX_INPUTS_PER_TEST_CASE][32]
Definition: vectors.h:41
size_t num_outputs
Definition: vectors.h:46
size_t num_taproot_inputs
Definition: vectors.h:40
struct bip352_recipient_addressdata recipient_pubkeys[MAX_RECIPIENT_ENTRIES_PER_TEST_CASE]
Definition: vectors.h:48
size_t num_output_sets
Definition: vectors.h:51
Labels.
unsigned char label_tweak[32]
unsigned char label[33]
struct label_cache_entry entries[5]
size_t entries_used
secp256k1_callback error_callback
Definition: secp256k1.c:65
secp256k1_hash_ctx hash_ctx
Definition: secp256k1.c:63
Opaque data structure that holds a keypair consisting of a secret and a public key.
unsigned char data[96]
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:62
Opaque data structure that holds a Silent Payments label.
Opaque data structure that holds Silent Payments prevouts summary data.
The data from a single recipient address.
Opaque data structure that holds a parsed and valid "x-only" public key.
Definition: unit_test.h:53
static void counting_callback_fn(const char *str, void *data)
Definition: tests.c:87
static void test_sha256_tag_midstate(const secp256k1_hash_ctx *hash_ctx, secp256k1_sha256 *sha_tagged, const unsigned char *tag, size_t taglen)
Definition: tests.c:778
#define CHECK_ILLEGAL(ctx, expr)
Definition: tests.c:84
static secp256k1_context * CTX
Definition: tests.c:47
static secp256k1_context * STATIC_CTX
Definition: tests.c:48
static const unsigned char secp256k1_group_order_bytes[32]
Definition: testutil.h:24
#define CASE1(name)
Definition: unit_test.h:27