Bitcoin Core 32.99.0
P2P Digital Currency
main_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_MAIN_H
7#define SECP256K1_MODULE_SILENTPAYMENTS_MAIN_H
8
9#include "../../../include/secp256k1.h"
10#include "../../../include/secp256k1_extrakeys.h"
11#include "../../../include/secp256k1_silentpayments.h"
12
13#include "../../eckey.h"
14#include "../../ecmult.h"
15#include "../../ecmult_const.h"
16#include "../../ecmult_gen.h"
17#include "../../group.h"
18#include "../../hash.h"
19#include "../../hsort.h"
20
22static const unsigned char secp256k1_silentpayments_prevouts_summary_magic[4] = { 0xa7, 0x1c, 0xd3, 0x5e };
23
30static int secp256k1_silentpayments_recipient_sort_cmp(const void* pk1, const void* pk2, void *ctx) {
33
35 if (ret != 0) {
36 return ret;
37 } else {
38 return (r1->index > r2->index) - (r1->index < r2->index);
39 }
40}
41
42static void secp256k1_silentpayments_recipient_sort(const secp256k1_context* ctx, const secp256k1_silentpayments_recipient **recipients, size_t n_recipients) {
43 /* Suppress wrong warning (fixed in MSVC 19.33) */
44 #if defined(_MSC_VER) && (_MSC_VER < 1933)
45 #pragma warning(push)
46 #pragma warning(disable: 4090)
47 #endif
48
49 secp256k1_hsort(recipients, n_recipients, sizeof(*recipients), secp256k1_silentpayments_recipient_sort_cmp, (void *)ctx);
50
51 #if defined(_MSC_VER) && (_MSC_VER < 1933)
52 #pragma warning(pop)
53 #endif
54}
55
58 static const uint32_t midstate[8] = {
59 0xd4143ffcul, 0x012ea4b5ul, 0x36e21c8ful, 0xf7ec7b54ul,
60 0x4dd4e2acul, 0x9bcaa0a4ul, 0xe244899bul, 0xcd06903eul
61 };
62 secp256k1_sha256_initialize_midstate(hash, 64, midstate);
63}
64
66static int secp256k1_silentpayments_calculate_input_hash_scalar(const secp256k1_hash_ctx *hash_ctx, secp256k1_scalar *input_hash_scalar, const unsigned char *outpoint_smallest36, secp256k1_ge *pubkey_sum) {
68 unsigned char pubkey_sum_ser[33];
69 unsigned char input_hash[32];
70 int overflow;
71
73 secp256k1_sha256_write(hash_ctx, &hash, outpoint_smallest36, 36);
74 secp256k1_ge_serialize33(pubkey_sum, pubkey_sum_ser);
75 secp256k1_sha256_write(hash_ctx, &hash, pubkey_sum_ser, sizeof(pubkey_sum_ser));
76 secp256k1_sha256_finalize(hash_ctx, &hash, input_hash);
77 /* Convert input_hash to a scalar.
78 *
79 * This can only fail if the output of the hash function is zero or greater than or equal to the curve order, which
80 * happens with negligible probability. Normally, we would use VERIFY_CHECK as opposed to returning an error
81 * since returning an error here would result in an untestable branch in the code. But in this case, we return
82 * an error to ensure strict compliance with BIP0352.
83 */
84 secp256k1_scalar_set_b32(input_hash_scalar, input_hash, &overflow);
85 return (!secp256k1_scalar_is_zero(input_hash_scalar)) & (!overflow);
86}
87
88static void secp256k1_silentpayments_create_shared_secret(unsigned char *shared_secret33, const secp256k1_ge *public_component, const secp256k1_scalar *secret_component) {
89 secp256k1_gej ss_j;
90 secp256k1_ge ss;
91
92 VERIFY_CHECK(!secp256k1_ge_is_infinity(public_component));
93 VERIFY_CHECK(!secp256k1_scalar_is_zero(secret_component));
94
95 secp256k1_ecmult_const(&ss_j, public_component, secret_component);
96 secp256k1_ge_set_gej(&ss, &ss_j);
97
98 /* serialize shared secret in constant-time */
101 shared_secret33[0] = SECP256K1_TAG_PUBKEY_EVEN | secp256k1_fe_is_odd(&ss.y);
102 secp256k1_fe_get_b32(&shared_secret33[1], &ss.x);
103
104 /* Leaking these values would break indistinguishability of the transaction, so clear them. */
106 secp256k1_gej_clear(&ss_j);
107}
108
111 static const uint32_t midstate[8] = {
112 0x88831537ul, 0x5127079bul, 0x69c2137bul, 0xab0303e6ul,
113 0x98fa21faul, 0x4a888523ul, 0xbd99daabul, 0xf25e5e0aul
114 };
115 secp256k1_sha256_initialize_midstate(hash, 64, midstate);
116}
117
118static int secp256k1_silentpayments_create_output_tweak(const secp256k1_context *ctx, secp256k1_scalar *t_k_scalar, const unsigned char *shared_secret33, uint32_t k) {
119 const secp256k1_hash_ctx *hash_ctx = &ctx->hash_ctx;
120 secp256k1_sha256 hash;
121 unsigned char hash_ser[32];
122 unsigned char k_serialized[4];
123 int overflow;
124
125 /* Compute hash(shared_secret || ser_32(k)) [sha256 with tag "BIP0352/SharedSecret"] */
127 secp256k1_sha256_write(hash_ctx, &hash, shared_secret33, 33);
128 secp256k1_write_be32(k_serialized, k);
129 secp256k1_sha256_write(hash_ctx, &hash, k_serialized, sizeof(k_serialized));
130 secp256k1_sha256_finalize(hash_ctx, &hash, hash_ser);
131
132 /* The only thing that the attacker can do with the hashed secret is derive the final pubkeys.
133 * On the side of the sender, we assume that will reveal those on the blockchain anyway.
134 * On the side of the scanner, we assume that the caller wants to branch when a payment is found. */
135 secp256k1_declassify(ctx, hash_ser, sizeof(hash_ser));
136
137 /* Convert output tweak t_k to a scalar.
138 *
139 * This can only fail if the output of the hash function is zero or greater than or equal to the curve order, which
140 * happens with negligible probability. Normally, we would use VERIFY_CHECK as opposed to returning an error
141 * since returning an error here would result in an untestable branch in the code. But in this case, we return
142 * an error to ensure strict compliance with BIP0352.
143 */
144 secp256k1_scalar_set_b32(t_k_scalar, hash_ser, &overflow);
145 /* Leaking this value would break indistinguishability of the transaction, so clear it. */
146 secp256k1_memclear_explicit(hash_ser, sizeof(hash_ser));
148 return (!secp256k1_scalar_is_zero(t_k_scalar)) & (!overflow);
149}
150
151static int secp256k1_silentpayments_create_output_pubkey(const secp256k1_context *ctx, secp256k1_xonly_pubkey *output_xonly, const unsigned char *shared_secret33, const secp256k1_pubkey *spend_pubkey, uint32_t k) {
152 secp256k1_ge output_ge;
153 secp256k1_scalar t_k_scalar;
154 /* Calculate the output tweak t_k and convert it to a scalar.
155 *
156 * Note: _create_output_tweak can only fail if the output of the hash function is zero or greater than or equal to
157 * the curve order, which is statistically improbable. Returning an error here results in an untestable branch in
158 * the code, but we do this anyways to ensure strict compliance with BIP0352.
159 */
160 if (!secp256k1_silentpayments_create_output_tweak(ctx, &t_k_scalar, shared_secret33, k)) {
161 secp256k1_scalar_clear(&t_k_scalar);
162 return 0;
163 }
164
165 if (!secp256k1_pubkey_load(ctx, &output_ge, spend_pubkey)) {
166 secp256k1_scalar_clear(&t_k_scalar);
167 return 0;
168 }
169 /* `tweak_add` only fails if t_k_scalar * G = -spend_pubkey. Considering t_k is the output of a hash function, this
170 * will happen only with negligible probability for honestly created spend_pubkey, but we handle this error anyway
171 * to protect against this function being called with malicious inputs, i.e.,
172 * spend_pubkey = -(_create_output_tweak(shared_secret33, k))*G
173 */
174 if (!secp256k1_eckey_pubkey_tweak_add(&output_ge, &t_k_scalar)) {
175 secp256k1_scalar_clear(&t_k_scalar);
176 return 0;
177 }
178 secp256k1_fe_normalize_var(&output_ge.y);
180 secp256k1_xonly_pubkey_save(output_xonly, &output_ge);
181
182 /* Leaking this value would break indistinguishability of the transaction, so clear it. */
183 secp256k1_scalar_clear(&t_k_scalar);
184 return 1;
185}
186
188 const secp256k1_context *ctx,
189 secp256k1_xonly_pubkey **generated_outputs,
190 const secp256k1_silentpayments_recipient **recipients,
191 size_t n_recipients,
192 const unsigned char *outpoint_smallest36,
193 const secp256k1_keypair * const *keypairs,
194 size_t n_keypairs,
195 const unsigned char * const *seckeys,
196 size_t n_seckeys
197) {
198 size_t i;
199 uint32_t k;
200 secp256k1_scalar seckey_sum_scalar, addend, input_hash_scalar;
201 secp256k1_ge prevouts_pubkey_sum_ge;
202 unsigned char shared_secret[33];
203 secp256k1_pubkey current_scan_pubkey;
204 int ret, sum_is_zero;
205
206 /* Sanity check inputs. */
207 VERIFY_CHECK(ctx != NULL);
209 ARG_CHECK(generated_outputs != NULL);
210 ARG_CHECK(recipients != NULL);
211 ARG_CHECK(n_recipients > 0);
212 ARG_CHECK(outpoint_smallest36 != NULL);
213 ARG_CHECK((n_seckeys > 0) || (n_keypairs > 0));
214 if (n_keypairs > 0) {
215 ARG_CHECK(keypairs != NULL);
216 for (i = 0; i < n_keypairs; i++) {
217 ARG_CHECK(keypairs[i] != NULL);
218 }
219 }
220 if (n_seckeys > 0) {
221 ARG_CHECK(seckeys != NULL);
222 for (i = 0; i < n_seckeys; i++) {
223 ARG_CHECK(seckeys[i] != NULL);
224 }
225 }
226 for (i = 0; i < n_recipients; i++) {
227 ARG_CHECK(generated_outputs[i] != NULL);
228 ARG_CHECK(recipients[i] != NULL);
229 ARG_CHECK(recipients[i]->index == i);
230 }
231
232 seckey_sum_scalar = secp256k1_scalar_zero;
233 for (i = 0; i < n_seckeys; i++) {
234 ret = secp256k1_scalar_set_b32_seckey(&addend, seckeys[i]);
235 secp256k1_declassify(ctx, &ret, sizeof(ret));
236 if (!ret) {
237 secp256k1_scalar_clear(&addend);
238 secp256k1_scalar_clear(&seckey_sum_scalar);
239 return 0;
240 }
241 secp256k1_scalar_add(&seckey_sum_scalar, &seckey_sum_scalar, &addend);
242 }
243 /* Secret keys used for taproot outputs have to be negated if they result in an odd point. This is to ensure
244 * the sender and recipient can arrive at the same shared secret when using x-only public keys. */
245 for (i = 0; i < n_keypairs; i++) {
246 secp256k1_ge addend_point;
247 ret = secp256k1_keypair_load(ctx, &addend, &addend_point, keypairs[i]);
248 secp256k1_declassify(ctx, &ret, sizeof(ret));
249 if (!ret) {
250 secp256k1_scalar_clear(&addend);
251 secp256k1_scalar_clear(&seckey_sum_scalar);
252 return 0;
253 }
254 if (secp256k1_fe_is_odd(&addend_point.y)) {
255 secp256k1_scalar_negate(&addend, &addend);
256 }
257 secp256k1_scalar_add(&seckey_sum_scalar, &seckey_sum_scalar, &addend);
258 }
259 /* If there are any failures in loading/summing up the secret keys, fail early. */
260 sum_is_zero = secp256k1_scalar_is_zero(&seckey_sum_scalar);
261 secp256k1_declassify(ctx, &sum_is_zero, sizeof(sum_is_zero));
262 secp256k1_scalar_clear(&addend);
263 if (sum_is_zero) {
264 secp256k1_scalar_clear(&seckey_sum_scalar);
265 return 0;
266 }
267 secp256k1_ecmult_gen_ge(&ctx->ecmult_gen_ctx, &prevouts_pubkey_sum_ge, &seckey_sum_scalar);
268 /* We declassify the pubkey sum because serializing a group element (done in the
269 * `_calculate_input_hash_scalar` call following) is not a constant-time operation.
270 */
271 secp256k1_declassify(ctx, &prevouts_pubkey_sum_ge, sizeof(prevouts_pubkey_sum_ge));
272
273 /* Calculate the input_hash and convert it to a scalar so that it can be multiplied with the summed up private keys, i.e., a_sum = a_sum * input_hash.
274 * By multiplying the scalars together first, we can save an elliptic curve multiplication.
275 *
276 * Note: _input_hash_scalar can only fail if the output of the hash function is zero or greater than or equal to the
277 * curve order, which is statistically improbable. Returning an error here results in an untestable branch in the
278 * code, but we do this anyways to ensure strict compliance with BIP0352.
279 */
280 if (!secp256k1_silentpayments_calculate_input_hash_scalar(&ctx->hash_ctx, &input_hash_scalar, outpoint_smallest36, &prevouts_pubkey_sum_ge)) {
281 secp256k1_scalar_clear(&seckey_sum_scalar);
282 return 0;
283 }
284 secp256k1_scalar_mul(&seckey_sum_scalar, &seckey_sum_scalar, &input_hash_scalar);
285 /* _recipient_sort sorts the array of recipients in place by their scan public keys (lexicographically).
286 * This ensures that all recipients with the same scan public key are grouped together, as specified in BIP0352.
287 *
288 * More specifically, this ensures `k` is incremented from 0 to the number of requested outputs for each recipient group,
289 * where a recipient group is all addresses with the same scan public key.
290 */
291 secp256k1_silentpayments_recipient_sort(ctx, recipients, n_recipients);
292 current_scan_pubkey = recipients[0]->scan_pubkey;
293 k = 0; /* This is a dead store but clang will emit a false positive warning if we omit it. */
294 for (i = 0; i < n_recipients; i++) {
295 if ((i == 0) || (secp256k1_ec_pubkey_cmp(ctx, &current_scan_pubkey, &recipients[i]->scan_pubkey) != 0)) {
296 /* If we are on a different scan pubkey, its time to recreate the shared secret and reset k to 0.
297 * It's very unlikely the scan public key is invalid by this point, since this means the caller would
298 * have created the _silentpayments_recipient object incorrectly, but just to be sure we still check that
299 * the public key is valid.
300 */
302 if (!secp256k1_pubkey_load(ctx, &pk, &recipients[i]->scan_pubkey)) {
303 secp256k1_scalar_clear(&seckey_sum_scalar);
304 /* Leaking this value would break indistinguishability of the transaction, so clear it. */
305 secp256k1_memclear_explicit(&shared_secret, sizeof(shared_secret));
306 return 0;
307 }
308 /* Creating the shared secret requires that the public and secret components are
309 * non-infinity and non-zero, respectively. Note that the involved parts (input hash,
310 * secret key sum, and scan public key) have all been verified at this point. */
311 secp256k1_silentpayments_create_shared_secret(shared_secret, &pk, &seckey_sum_scalar);
312 k = 0;
313 }
314 /* If creating another output for the current recipient group exceeded the
315 * protocol limit, fail, as the recipient wouldn't be guaranteed to find it. */
317 secp256k1_scalar_clear(&seckey_sum_scalar);
318 secp256k1_memclear_explicit(&shared_secret, sizeof(shared_secret));
319 return 0;
320 }
321 if (!secp256k1_silentpayments_create_output_pubkey(ctx, generated_outputs[recipients[i]->index], shared_secret, &recipients[i]->spend_pubkey, k)) {
322 secp256k1_scalar_clear(&seckey_sum_scalar);
323 secp256k1_memclear_explicit(&shared_secret, sizeof(shared_secret));
324 return 0;
325 }
326 current_scan_pubkey = recipients[i]->scan_pubkey;
327 k++;
328 }
329 secp256k1_scalar_clear(&seckey_sum_scalar);
330 secp256k1_memclear_explicit(&shared_secret, sizeof(shared_secret));
331 return 1;
332}
333
336 static const uint32_t midstate[8] = {
337 0x26b95d63ul, 0x8bf1b740ul, 0x10a5986ful, 0x06a387a5ul,
338 0x2d1c1c30ul, 0xd035951aul, 0x2d7f0f96ul, 0x29e3e0dbul
339 };
340 secp256k1_sha256_initialize_midstate(hash, 64, midstate);
341}
342
343static const unsigned char secp256k1_silentpayments_label_magic[4] = { 0x27, 0x9d, 0x44, 0xba };
344
345/* Saves a group element into a label. Requires that the provided group element is not infinity. */
347 memcpy(&label->data[0], secp256k1_silentpayments_label_magic, 4);
348 secp256k1_ge_to_bytes(label->data + 4, ge);
349}
350
351/* Loads a group element from a label. Returns 1 unless the label wasn't properly initialized. */
354 secp256k1_ge_from_bytes(ge, label->data + 4);
355 return 1;
356}
357
359 secp256k1_ge ge;
360
361 VERIFY_CHECK(ctx != NULL);
362 ARG_CHECK(label != NULL);
363 memset(label, 0, sizeof(*label));
364 ARG_CHECK(in33 != NULL);
365
366 if (!secp256k1_ge_parse(&ge, in33, 33)) {
367 return 0;
368 }
369
371 return 1;
372}
373
375 secp256k1_ge ge;
376
377 VERIFY_CHECK(ctx != NULL);
378 ARG_CHECK(out33 != NULL);
379 memset(out33, 0, 33);
380 ARG_CHECK(label != NULL);
381
382 if (!secp256k1_silentpayments_label_load(ctx, &ge, label)) {
383 return 0;
384 }
385 secp256k1_ge_serialize33(&ge, out33);
386 return 1;
387}
388
389int 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) {
390 secp256k1_sha256 hash;
391 unsigned char m_serialized[4];
392 secp256k1_ge label_ge;
393 secp256k1_scalar label_tweak_scalar;
394 int ret;
395
396 /* Sanity check inputs. */
397 VERIFY_CHECK(ctx != NULL);
398 ARG_CHECK(label != NULL);
399 memset(label, 0, sizeof(*label));
401 ARG_CHECK(label_tweak32 != NULL);
402 ARG_CHECK(scan_key32 != NULL);
403
404 /* ensure that the passed scan key is valid, in order to avoid creating unspendable labels */
405 ret = secp256k1_ec_seckey_verify(ctx, scan_key32);
406
407 /* Compute hash(ser_256(b_scan) || ser_32(m)) [sha256 with tag "BIP0352/Label"] */
409 secp256k1_sha256_write(&ctx->hash_ctx, &hash, scan_key32, 32);
410 secp256k1_write_be32(m_serialized, m);
411 secp256k1_sha256_write(&ctx->hash_ctx, &hash, m_serialized, sizeof(m_serialized));
412 secp256k1_sha256_finalize(&ctx->hash_ctx, &hash, label_tweak32);
413
414 ret &= secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &label_tweak_scalar, &label_ge, label_tweak32);
415 secp256k1_silentpayments_label_save(label, &label_ge);
416 secp256k1_memczero(label, sizeof(*label), !ret);
417 secp256k1_memczero(label_tweak32, 32, !ret);
418
419 secp256k1_scalar_clear(&label_tweak_scalar);
420 secp256k1_memclear_explicit(m_serialized, sizeof(m_serialized));
422
423 return ret;
424}
425
427 secp256k1_ge labeled_spend_pubkey_ge, label_addend;
428 secp256k1_gej result_gej;
429 secp256k1_ge result_ge;
430 int ret;
431
432 /* Sanity check inputs. */
433 VERIFY_CHECK(ctx != NULL);
434 ARG_CHECK(labeled_spend_pubkey != NULL);
435 memset(labeled_spend_pubkey, 0, sizeof(*labeled_spend_pubkey));
436 ARG_CHECK(unlabeled_spend_pubkey != NULL);
437 ARG_CHECK(label != NULL);
438
439 /* Calculate labeled_spend_pubkey = unlabeled_spend_pubkey + label.
440 * If either the label or spend public key is invalid, return early.
441 */
442 ret = secp256k1_pubkey_load(ctx, &labeled_spend_pubkey_ge, unlabeled_spend_pubkey);
443 ret &= secp256k1_silentpayments_label_load(ctx, &label_addend, label);
444 if (!ret) {
445 return 0;
446 }
447 secp256k1_gej_set_ge(&result_gej, &labeled_spend_pubkey_ge);
448 secp256k1_gej_add_ge_var(&result_gej, &result_gej, &label_addend, NULL);
449 if (secp256k1_gej_is_infinity(&result_gej)) {
450 return 0;
451 }
452
453 secp256k1_ge_set_gej_var(&result_ge, &result_gej);
454 secp256k1_pubkey_save(labeled_spend_pubkey, &result_ge);
455
456 return 1;
457}
458
485 const secp256k1_context *ctx,
487 const unsigned char *outpoint_smallest36,
488 const secp256k1_xonly_pubkey * const *xonly_pubkeys,
489 size_t n_xonly_pubkeys,
490 const secp256k1_pubkey * const *pubkeys,
491 size_t n_pubkeys
492) {
493 size_t i;
494 secp256k1_ge prevouts_pubkey_sum_ge, addend;
495 secp256k1_gej prevouts_pubkey_sum_gej;
496 secp256k1_scalar input_hash_scalar;
497
498 /* Sanity check inputs */
499 VERIFY_CHECK(ctx != NULL);
500 ARG_CHECK(prevouts_summary != NULL);
501 memset(prevouts_summary, 0, sizeof(*prevouts_summary));
502 ARG_CHECK(outpoint_smallest36 != NULL);
503 ARG_CHECK((n_pubkeys > 0) || (n_xonly_pubkeys > 0));
504 if (n_xonly_pubkeys > 0) {
505 ARG_CHECK(xonly_pubkeys != NULL);
506 for (i = 0; i < n_xonly_pubkeys; i++) {
507 ARG_CHECK(xonly_pubkeys[i] != NULL);
508 }
509 }
510 if (n_pubkeys > 0) {
511 ARG_CHECK(pubkeys != NULL);
512 for (i = 0; i < n_pubkeys; i++) {
513 ARG_CHECK(pubkeys[i] != NULL);
514 }
515 }
516
517 /* Compute prevouts_pubkey_sum = A_1 + A_2 + ... + A_n.
518 *
519 * Since an attacker can maliciously craft transactions where the public keys sum to zero, fail early here
520 * to avoid making the caller do extra work, e.g., when building an index or scanning a malicious transaction.
521 *
522 * This will also fail if any of the provided prevout public keys are malformed.
523 */
524 secp256k1_gej_set_infinity(&prevouts_pubkey_sum_gej);
525 for (i = 0; i < n_pubkeys; i++) {
526 if (!secp256k1_pubkey_load(ctx, &addend, pubkeys[i])) {
527 return 0;
528 }
529 secp256k1_gej_add_ge_var(&prevouts_pubkey_sum_gej, &prevouts_pubkey_sum_gej, &addend, NULL);
530 }
531 for (i = 0; i < n_xonly_pubkeys; i++) {
532 if (!secp256k1_xonly_pubkey_load(ctx, &addend, xonly_pubkeys[i])) {
533 return 0;
534 }
535 secp256k1_gej_add_ge_var(&prevouts_pubkey_sum_gej, &prevouts_pubkey_sum_gej, &addend, NULL);
536 }
537 if (secp256k1_gej_is_infinity(&prevouts_pubkey_sum_gej)) {
538 return 0;
539 }
540 secp256k1_ge_set_gej_var(&prevouts_pubkey_sum_ge, &prevouts_pubkey_sum_gej);
541 /* Calculate the input_hash and convert it to a scalar.
542 *
543 * Note: _input_hash_scalar can only fail if the output of the hash function is zero or greater than or equal to the
544 * curve order, which is statistically improbable. Returning an error here results in an untestable branch in the
545 * code, but we do this anyways to ensure strict compliance with BIP0352.
546 */
547 if (!secp256k1_silentpayments_calculate_input_hash_scalar(&ctx->hash_ctx, &input_hash_scalar, outpoint_smallest36, &prevouts_pubkey_sum_ge)) {
548 return 0;
549 }
550 memcpy(&prevouts_summary->data[0], secp256k1_silentpayments_prevouts_summary_magic, 4);
551 prevouts_summary->data[4] = 0;
552 secp256k1_ge_to_bytes(&prevouts_summary->data[5], &prevouts_pubkey_sum_ge);
553 secp256k1_scalar_get_b32(&prevouts_summary->data[5 + 64], &input_hash_scalar);
554 return 1;
555}
556
557/* Label scanning involves the transformation from Jacobian (gej) to affine (ge) coordinates
558 * for serializing label candidates. As this is an expensive operation involving modular
559 * inversion, we don't do this one by one for each tx output, but collect multiple label
560 * candidates in Jacobian in order to apply Montgomery's trick for batch inversion (using
561 * the function `secp256k1_ge_set_all_gej_var`). For transactions with a large number of
562 * outputs, this speeds up scanning significantly (~2.5x). */
563enum { LABEL_BATCH_SIZE = 8 }; /* batch size expressed in number of tx outputs */
564
565/* Check the label cache for each pair of candidates in a batch.
566 * Returns the first matching tx output index, or -1 if none has a matching label. */
568 secp256k1_ge *label_ge,
569 const unsigned char **label_tweak,
570 const secp256k1_gej *label_candidates_gej,
571 size_t n_batch,
572 size_t j_start,
574 const void *label_context
575) {
576 secp256k1_ge label_candidates_ge[2 * LABEL_BATCH_SIZE];
577 unsigned char label33[33];
578 size_t i;
579
580 *label_tweak = NULL;
581 secp256k1_ge_set_all_gej_var(label_candidates_ge, label_candidates_gej, 2 * n_batch);
582 for (i = 0; i < 2 * n_batch; i++) {
583 /* Note: serialize will only fail if a label candidate is the point at infinity, but we know
584 * this cannot happen since we only collect candidates if tx_output != unlabeled_output. Thus,
585 * we know that label_candidate = tx_output - unlabeled_output cannot be the point at infinity.
586 */
587 VERIFY_CHECK(!secp256k1_ge_is_infinity(&label_candidates_ge[i]));
588 secp256k1_ge_serialize33(&label_candidates_ge[i], label33);
589 *label_tweak = label_lookup(label33, label_context);
590 if (*label_tweak != NULL) {
591 *label_ge = label_candidates_ge[i];
592 return (int)(j_start + i / 2);
593 }
594 }
595 return -1;
596}
597
599 const secp256k1_context *ctx,
600 secp256k1_silentpayments_found_output **found_outputs, uint32_t *n_found_outputs,
601 const secp256k1_xonly_pubkey * const *tx_outputs, size_t n_tx_outputs,
602 const unsigned char *scan_key32,
603 const secp256k1_silentpayments_prevouts_summary *prevouts_summary,
604 const secp256k1_pubkey *unlabeled_spend_pubkey,
606 const void *label_context
607) {
608 secp256k1_scalar scan_key_scalar;
609 secp256k1_ge unlabeled_spend_pubkey_ge, prevouts_pubkey_sum_ge, tx_output_ge;
610 unsigned char shared_secret[33];
611 uint32_t k, k_max;
612 size_t i;
613 int found_idx, combined, valid_scan_key, ret;
614
615 /* Sanity check inputs */
616 VERIFY_CHECK(ctx != NULL);
617 ARG_CHECK(found_outputs != NULL);
618 ARG_CHECK(n_found_outputs != NULL);
619 *n_found_outputs = 0;
620 ARG_CHECK(tx_outputs != NULL);
621 ARG_CHECK(n_tx_outputs > 0);
622 for (i = 0; i < n_tx_outputs; i++) {
623 ARG_CHECK(found_outputs[i] != NULL);
624 ARG_CHECK(tx_outputs[i] != NULL);
625 /* Validate each tx output object early so malformed pubkeys are always rejected. */
626 if (!secp256k1_xonly_pubkey_load(ctx, &tx_output_ge, tx_outputs[i])) {
627 return 0;
628 }
629 }
630 ARG_CHECK(scan_key32 != NULL);
631 ARG_CHECK(prevouts_summary != NULL);
633 ARG_CHECK(unlabeled_spend_pubkey != NULL);
634 /* Passing a context without a lookup function is non-sensical */
635 if (label_context != NULL) {
636 ARG_CHECK(label_lookup != NULL);
637 }
638 valid_scan_key = secp256k1_scalar_set_b32_seckey(&scan_key_scalar, scan_key32);
639 secp256k1_declassify(ctx, &valid_scan_key, sizeof(valid_scan_key));
640 if (!valid_scan_key) {
641 secp256k1_scalar_clear(&scan_key_scalar);
642 return 0;
643 }
644 secp256k1_ge_from_bytes(&prevouts_pubkey_sum_ge, &prevouts_summary->data[5]);
645 combined = (int)prevouts_summary->data[4];
646 /* Note that the "combined" flag can currently only be 0, as we only have support for full nodes, i.e.,
647 * the following branch is always taken. "combined" can also be 1 once we add light client support. */
648 if (!combined) {
649 secp256k1_scalar input_hash_scalar;
650 secp256k1_scalar_set_b32(&input_hash_scalar, &prevouts_summary->data[5 + 64], NULL);
651 secp256k1_scalar_mul(&scan_key_scalar, &scan_key_scalar, &input_hash_scalar);
652 }
653 ret = secp256k1_pubkey_load(ctx, &unlabeled_spend_pubkey_ge, unlabeled_spend_pubkey);
654 if (!ret) {
655 secp256k1_scalar_clear(&scan_key_scalar);
656 return 0;
657 }
658 /* Creating the shared secret requires that the public and secret components are
659 * non-infinity and non-zero, respectively. Note that the involved parts (input hash,
660 * scan secret key, and prevouts public key sum) have all been verified at this point,
661 * assuming that the user hasn't tampered the `prevouts_summary` object manually. */
662 secp256k1_silentpayments_create_shared_secret(shared_secret, &prevouts_pubkey_sum_ge, &scan_key_scalar);
663 /* Clear the scan_key_scalar since we no longer need it and leaking this value would break indistinguishability of the transaction. */
664 secp256k1_scalar_clear(&scan_key_scalar);
665
666 found_idx = -1;
667 /* Don't look further than the per-group recipient limit, in order to avoid quadratic scaling issues. */
668 k_max = (n_tx_outputs < SECP256K1_SILENTPAYMENTS_RECIPIENT_GROUP_LIMIT) ?
670 for (k = 0; k < k_max; k++) {
671 secp256k1_scalar t_k_scalar;
672 secp256k1_xonly_pubkey unlabeled_output_xonly;
673 secp256k1_ge unlabeled_output_ge = unlabeled_spend_pubkey_ge;
674 secp256k1_ge unlabeled_output_negated_ge;
675 secp256k1_gej label_candidates_gej[2 * LABEL_BATCH_SIZE]; /* two candidates per tx output (one per y-parity) */
676 size_t label_batch_idx = 0; /* current index within a batch */
677 const unsigned char *label_tweak = NULL;
678 secp256k1_ge label_ge;
679 size_t j;
680
681 /* Calculate the output tweak t_k and convert it to a scalar.
682 *
683 * Note: _create_output_tweak can only fail if the output of the hash function is zero or greater than or equal
684 * to the curve order, which is statistically improbable. Returning an error here results in an untestable
685 * branch in the code, but we do this anyways to ensure strict compliance with BIP0352.
686 */
687 if (!secp256k1_silentpayments_create_output_tweak(ctx, &t_k_scalar, shared_secret, k)) {
688 secp256k1_scalar_clear(&t_k_scalar);
689 secp256k1_memclear_explicit(&shared_secret, sizeof(shared_secret));
690 return 0;
691 }
692
693 /* Calculate unlabeled_output = unlabeled_spend_pubkey + t_k * G.
694 * This can fail if t_k * G is the negation of unlabeled_spend_pubkey, but this happens only with negligible
695 * probability for honestly created unlabeled_spend_pubkey as t_k is the output of a hash function. */
696 if (!secp256k1_eckey_pubkey_tweak_add(&unlabeled_output_ge, &t_k_scalar)) {
697 /* Leaking these values would break indistinguishability of the transaction, so clear them. */
698 secp256k1_scalar_clear(&t_k_scalar);
699 secp256k1_memclear_explicit(&shared_secret, sizeof(shared_secret));
700 return 0;
701 }
702 /* Calculate unlabeled_output_negated = -unlabeled_output */
703 secp256k1_ge_neg(&unlabeled_output_negated_ge, &unlabeled_output_ge);
704
705 found_idx = -1;
706 secp256k1_fe_normalize_var(&unlabeled_output_ge.y);
707 secp256k1_extrakeys_ge_even_y(&unlabeled_output_ge);
708 secp256k1_xonly_pubkey_save(&unlabeled_output_xonly, &unlabeled_output_ge);
709 for (j = 0; j < n_tx_outputs; j++) {
710 if (secp256k1_xonly_pubkey_cmp(ctx, &unlabeled_output_xonly, tx_outputs[j]) == 0) {
711 label_tweak = NULL;
712 found_idx = j;
713 /* An earlier label match takes precedence over this direct match. */
714 if (label_batch_idx > 0) {
715 int label_found_idx = secp256k1_silentpayments_check_label_batch(
716 &label_ge, &label_tweak, label_candidates_gej, label_batch_idx,
717 j - label_batch_idx, label_lookup, label_context);
718 if (label_found_idx != -1) {
719 found_idx = label_found_idx;
720 }
721 }
722 break;
723 }
724
725 /* If not found, proceed to check for labels (if a label lookup function is provided). */
726 if (label_lookup != NULL) {
727 secp256k1_gej tx_output_gej;
728 secp256k1_gej *label_candidate1 = &label_candidates_gej[2 * label_batch_idx];
729 secp256k1_gej *label_candidate2 = &label_candidates_gej[2 * label_batch_idx + 1];
730
731 /* Calculate scan label candidates:
732 * label_candidate1 = tx_output - unlabeled_output
733 * label_candidate2 = -tx_output - unlabeled_output
734 * and store them in the batch */
735 secp256k1_xonly_pubkey_load(ctx, &tx_output_ge, tx_outputs[j]);
736 secp256k1_gej_set_ge(&tx_output_gej, &tx_output_ge);
737 secp256k1_gej_add_ge_var(label_candidate1, &tx_output_gej, &unlabeled_output_negated_ge, NULL);
738 secp256k1_gej_neg(&tx_output_gej, &tx_output_gej);
739 secp256k1_gej_add_ge_var(label_candidate2, &tx_output_gej, &unlabeled_output_negated_ge, NULL);
740 label_batch_idx++;
741 /* If the batch is filled or we have reached the last transaction output, perform batch
742 * inversion and check the label cache for each label candidate entry in the batch */
743 if (label_batch_idx == LABEL_BATCH_SIZE || j == (n_tx_outputs-1)) {
745 &label_ge, &label_tweak, label_candidates_gej, label_batch_idx,
746 j + 1 - label_batch_idx, label_lookup, label_context);
747 label_batch_idx = 0;
748 }
749 if (found_idx != -1) {
750 break;
751 }
752 }
753 }
754 if (found_idx != -1) {
755 found_outputs[k]->output = *tx_outputs[found_idx];
756 secp256k1_scalar_get_b32(found_outputs[k]->tweak, &t_k_scalar);
757 /* Clear the t_k_scalar since we no longer need it and leaking this value would
758 * break indistinguishability of the transaction. */
759 secp256k1_scalar_clear(&t_k_scalar);
760 if (label_tweak != NULL) {
761 found_outputs[k]->found_with_label = 1;
762 /* This is extremely unlikely to fail in that it can only really happen if label_tweak
763 * is the negation of the shared secret tweak. But since both tweak and label_tweak are
764 * created by hashing data, practically speaking this would only happen if an attacker
765 * tricked us into using a particular label_tweak (deviating from the protocol).
766 * Note that this call could also fail due to a malformed label_tweak data from the
767 * label cache, but we generally assume the passed in data is created using the API
768 * functions and thus have already been checked for correctness.
769 *
770 * Furthermore, although technically a failure for ec_seckey_tweak_add, this is not treated
771 * as a failure for Silent Payments because the output is still spendable with just the
772 * spend secret key. We set `tweak = 0` for this case.
773 */
774 if (!secp256k1_ec_seckey_tweak_add(ctx, found_outputs[k]->tweak, label_tweak)) {
775 memset(found_outputs[k]->tweak, 0, 32);
776 }
777 secp256k1_silentpayments_label_save(&found_outputs[k]->label, &label_ge);
778 } else {
779 found_outputs[k]->found_with_label = 0;
780 /* Set the label to an invalid value. */
781 memset(&found_outputs[k]->label, 0, sizeof(found_outputs[k]->label));
782 }
783 /* Reset everything for the next round of scanning. */
784 label_tweak = NULL;
785 } else {
786 secp256k1_scalar_clear(&t_k_scalar);
787 break;
788 }
789 }
790 *n_found_outputs = k;
791
792 /* Leaking the shared_secret would break indistinguishability of the transaction, so clear it. */
793 secp256k1_memclear_explicit(shared_secret, sizeof(shared_secret));
794 return 1;
795}
796
797#endif
int ret
if(!SetupNetworking())
static int secp256k1_eckey_pubkey_tweak_add(secp256k1_ge *key, const secp256k1_scalar *tweak)
static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *q)
Multiply: R = q*A (in constant-time for q)
static void secp256k1_ecmult_gen_ge(const secp256k1_ecmult_gen_context *ecmult_gen_ctx, secp256k1_ge *r, const secp256k1_scalar *a)
static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context *ecmult_gen_ctx)
static int secp256k1_keypair_load(const secp256k1_context *ctx, secp256k1_scalar *sk, secp256k1_ge *pk, const secp256k1_keypair *keypair)
Definition: main_impl.h:183
static SECP256K1_INLINE void secp256k1_xonly_pubkey_save(secp256k1_xonly_pubkey *pubkey, secp256k1_ge *ge)
Definition: main_impl.h:18
static SECP256K1_INLINE int secp256k1_xonly_pubkey_load(const secp256k1_context *ctx, secp256k1_ge *ge, const secp256k1_xonly_pubkey *pubkey)
Definition: main_impl.h:14
static int secp256k1_extrakeys_ge_even_y(secp256k1_ge *r)
Keeps a group element as is if it has an even Y and otherwise negates it.
Definition: main_impl.h:95
int secp256k1_xonly_pubkey_cmp(const secp256k1_context *ctx, const secp256k1_xonly_pubkey *pk0, const secp256k1_xonly_pubkey *pk1)
Compare two x-only public keys using lexicographic order.
Definition: main_impl.h:66
#define secp256k1_fe_is_odd
Definition: field.h:85
#define secp256k1_fe_normalize_var
Definition: field.h:80
#define secp256k1_fe_get_b32
Definition: field.h:89
#define secp256k1_fe_normalize
Definition: field.h:78
static void secp256k1_gej_clear(secp256k1_gej *r)
Clear a secp256k1_gej to prevent leaking sensitive information.
static void secp256k1_gej_set_infinity(secp256k1_gej *r)
Set a group element (jacobian) equal to the point at infinity.
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
Check whether a group element is the point at infinity.
static void secp256k1_ge_clear(secp256k1_ge *r)
Clear a secp256k1_ge to prevent leaking sensitive information.
static void secp256k1_gej_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, secp256k1_fe *rzr)
Set r equal to the sum of a and b (with b given in affine coordinates).
static void secp256k1_ge_serialize33(secp256k1_ge *elem, unsigned char *pub33)
Serialize a group element (that is not allowed to be infinity) to a compressed public key (33 bytes).
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a)
Set a group element equal to another which is given in jacobian coordinates.
static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a)
Set r equal to the inverse of a (i.e., mirrored around the X axis)
static int secp256k1_ge_is_infinity(const secp256k1_ge *a)
Check whether a group element is the point at infinity.
static int secp256k1_ge_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size)
Parse a group element from a 33-byte compressed or 65-byte uncompressed public key.
static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len)
Set group elements r[0:len] (affine) equal to group elements a[0:len] (jacobian).
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Set a group element (jacobian) equal to another which is given in affine coordinates.
static void secp256k1_ge_to_bytes(unsigned char *buf, const secp256k1_ge *a)
Convert a group element that is not infinity to a 64-byte array.
static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a)
Set a group element equal to another which is given in jacobian coordinates.
static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a)
Set r equal to the inverse of a (i.e., mirrored around the X axis)
static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf)
Convert a 64-byte array into group element.
static void secp256k1_hsort(void *ptr, size_t count, size_t size, int(*cmp)(const void *, const void *, void *), void *cmp_data)
static int tweak(const secp256k1_context *ctx, secp256k1_xonly_pubkey *agg_pk, secp256k1_musig_keyagg_cache *cache)
Definition: musig.c:64
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *bin, int *overflow)
Set a scalar from a big endian byte array.
static int secp256k1_scalar_set_b32_seckey(secp256k1_scalar *r, const unsigned char *bin)
Set a scalar from a big endian byte array and returns 1 if it is a valid seckey and 0 otherwise.
static int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
Check whether a scalar equals zero.
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
Convert a scalar to a byte array.
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
Add two scalars together (modulo the group order).
static void secp256k1_scalar_mul(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
Multiply two scalars (modulo the group order).
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
Compute the complement of a scalar (modulo the group order).
static void secp256k1_scalar_clear(secp256k1_scalar *r)
Clear a scalar to prevent the leak of sensitive data.
static const secp256k1_scalar secp256k1_scalar_zero
Definition: scalar_impl.h:28
static void secp256k1_sha256_finalize(const secp256k1_hash_ctx *hash_ctx, secp256k1_sha256 *hash, unsigned char *out32)
static void secp256k1_sha256_initialize_midstate(secp256k1_sha256 *hash, uint64_t bytes, const uint32_t state[8])
static void secp256k1_sha256_write(const secp256k1_hash_ctx *hash_ctx, secp256k1_sha256 *hash, const unsigned char *data, size_t size)
static void secp256k1_sha256_clear(secp256k1_sha256 *hash)
static SECP256K1_INLINE void secp256k1_memclear_explicit(void *ptr, size_t len)
Definition: util.h:268
static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n)
Semantics like memcmp.
Definition: util.h:281
static SECP256K1_INLINE void secp256k1_write_be32(unsigned char *p, uint32_t x)
Definition: util.h:436
#define VERIFY_CHECK(cond)
Definition: util.h:169
static SECP256K1_INLINE void secp256k1_memczero(void *s, size_t len, int flag)
Definition: util.h:220
#define ARG_CHECK(cond)
Definition: secp256k1.c:45
static int secp256k1_ec_pubkey_create_helper(const secp256k1_ecmult_gen_context *ecmult_gen_ctx, secp256k1_scalar *seckey_scalar, secp256k1_ge *p, const unsigned char *seckey)
Definition: secp256k1.c:622
static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context *ctx, const void *p, size_t len)
Definition: secp256k1.c:250
static int secp256k1_pubkey_load(const secp256k1_context *ctx, secp256k1_ge *ge, const secp256k1_pubkey *pubkey)
Definition: secp256k1.c:254
static void secp256k1_pubkey_save(secp256k1_pubkey *pubkey, secp256k1_ge *ge)
Definition: secp256k1.c:260
#define SECP256K1_TAG_PUBKEY_EVEN
Prefix byte used to tag various encoded curvepoints for specific purposes.
Definition: secp256k1.h:220
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:308
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:611
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:692
#define SECP256K1_SILENTPAYMENTS_RECIPIENT_GROUP_LIMIT
This module provides an implementation for Silent Payments, as specified in BIP352.
const unsigned char *(* secp256k1_silentpayments_label_lookup)(const unsigned char *label33, const void *label_context)
Type of callback function for label lookups.
static void secp256k1_silentpayments_recipient_sort(const secp256k1_context *ctx, const secp256k1_silentpayments_recipient **recipients, size_t n_recipients)
Definition: main_impl.h:42
@ LABEL_BATCH_SIZE
Definition: main_impl.h:563
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
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)
Scan for Silent Payments transaction outputs.
Definition: main_impl.h:598
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)
Create Silent Payments outputs for recipient(s).
Definition: main_impl.h:187
int secp256k1_silentpayments_recipient_label_parse(const secp256k1_context *ctx, secp256k1_silentpayments_label *label, const unsigned char *in33)
Parse a Silent Payments label.
Definition: main_impl.h:358
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)
An explanation of the prevouts_summary object and its usage:
Definition: main_impl.h:484
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
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)
Create Silent Payments label tweak and label.
Definition: main_impl.h:389
int secp256k1_silentpayments_recipient_label_serialize(const secp256k1_context *ctx, unsigned char *out33, const secp256k1_silentpayments_label *label)
Serialize a Silent Payments label.
Definition: main_impl.h:374
static int secp256k1_silentpayments_recipient_sort_cmp(const void *pk1, const void *pk2, void *ctx)
Sort an array of Silent Payments recipients.
Definition: main_impl.h:30
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:335
static int secp256k1_silentpayments_create_output_tweak(const secp256k1_context *ctx, secp256k1_scalar *t_k_scalar, const unsigned char *shared_secret33, uint32_t k)
Definition: main_impl.h:118
static void secp256k1_silentpayments_label_save(secp256k1_silentpayments_label *label, const secp256k1_ge *ge)
Definition: main_impl.h:346
static int secp256k1_silentpayments_calculate_input_hash_scalar(const secp256k1_hash_ctx *hash_ctx, secp256k1_scalar *input_hash_scalar, const unsigned char *outpoint_smallest36, secp256k1_ge *pubkey_sum)
Callers must ensure that pubkey_sum is not the point at infinity before calling this function.
Definition: main_impl.h:66
static void secp256k1_silentpayments_create_shared_secret(unsigned char *shared_secret33, const secp256k1_ge *public_component, const secp256k1_scalar *secret_component)
Definition: main_impl.h:88
static const unsigned char secp256k1_silentpayments_label_magic[4]
Definition: main_impl.h:343
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)
Create Silent Payments labeled spend public key.
Definition: main_impl.h:426
static int secp256k1_silentpayments_check_label_batch(secp256k1_ge *label_ge, const unsigned char **label_tweak, const secp256k1_gej *label_candidates_gej, size_t n_batch, size_t j_start, secp256k1_silentpayments_label_lookup label_lookup, const void *label_context)
Definition: main_impl.h:567
static int secp256k1_silentpayments_create_output_pubkey(const secp256k1_context *ctx, secp256k1_xonly_pubkey *output_xonly, const unsigned char *shared_secret33, const secp256k1_pubkey *spend_pubkey, uint32_t k)
Definition: main_impl.h:151
static const unsigned char secp256k1_silentpayments_prevouts_summary_magic[4]
magic bytes for ensuring prevouts_summary objects were initialized correctly.
Definition: main_impl.h:22
static int secp256k1_silentpayments_label_load(const secp256k1_context *ctx, secp256k1_ge *ge, const secp256k1_silentpayments_label *label)
Definition: main_impl.h:352
const unsigned char * label_lookup(const unsigned char *label33, const void *cache_ptr)
secp256k1_ecmult_gen_context ecmult_gen_ctx
Definition: secp256k1.c:62
secp256k1_hash_ctx hash_ctx
Definition: secp256k1.c:63
A group element in affine coordinates on the secp256k1 curve, or occasionally on an isomorphic curve ...
Definition: group.h:16
secp256k1_fe x
Definition: group.h:17
secp256k1_fe y
Definition: group.h:18
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:28
Opaque data structure that holds a keypair consisting of a secret and a public key.
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:62
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
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.