Bitcoin Core 31.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_eckey_pubkey_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 = secp256k1_get_hash_context(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((seckeys != NULL) || (keypairs != NULL));
214 if (keypairs != NULL) {
215 ARG_CHECK(n_keypairs > 0);
216 for (i = 0; i < n_keypairs; i++) {
217 ARG_CHECK(keypairs[i] != NULL);
218 }
219 } else {
220 ARG_CHECK(n_keypairs == 0);
221 }
222 if (seckeys != NULL) {
223 ARG_CHECK(n_seckeys > 0);
224 for (i = 0; i < n_seckeys; i++) {
225 ARG_CHECK(seckeys[i] != NULL);
226 }
227 } else {
228 ARG_CHECK(n_seckeys == 0);
229 }
230 for (i = 0; i < n_recipients; i++) {
231 ARG_CHECK(generated_outputs[i] != NULL);
232 ARG_CHECK(recipients[i] != NULL);
233 ARG_CHECK(recipients[i]->index == i);
234 }
235
236 seckey_sum_scalar = secp256k1_scalar_zero;
237 for (i = 0; i < n_seckeys; i++) {
238 ret = secp256k1_scalar_set_b32_seckey(&addend, seckeys[i]);
239 secp256k1_declassify(ctx, &ret, sizeof(ret));
240 if (!ret) {
241 secp256k1_scalar_clear(&addend);
242 secp256k1_scalar_clear(&seckey_sum_scalar);
243 return 0;
244 }
245 secp256k1_scalar_add(&seckey_sum_scalar, &seckey_sum_scalar, &addend);
246 }
247 /* Secret keys used for taproot outputs have to be negated if they result in an odd point. This is to ensure
248 * the sender and recipient can arrive at the same shared secret when using x-only public keys. */
249 for (i = 0; i < n_keypairs; i++) {
250 secp256k1_ge addend_point;
251 ret = secp256k1_keypair_load(ctx, &addend, &addend_point, keypairs[i]);
252 secp256k1_declassify(ctx, &ret, sizeof(ret));
253 if (!ret) {
254 secp256k1_scalar_clear(&addend);
255 secp256k1_scalar_clear(&seckey_sum_scalar);
256 return 0;
257 }
258 if (secp256k1_fe_is_odd(&addend_point.y)) {
259 secp256k1_scalar_negate(&addend, &addend);
260 }
261 secp256k1_scalar_add(&seckey_sum_scalar, &seckey_sum_scalar, &addend);
262 }
263 /* If there are any failures in loading/summing up the secret keys, fail early. */
264 sum_is_zero = secp256k1_scalar_is_zero(&seckey_sum_scalar);
265 secp256k1_declassify(ctx, &sum_is_zero, sizeof(sum_is_zero));
266 secp256k1_scalar_clear(&addend);
267 if (sum_is_zero) {
268 secp256k1_scalar_clear(&seckey_sum_scalar);
269 return 0;
270 }
271 secp256k1_ecmult_gen_ge(&ctx->ecmult_gen_ctx, &prevouts_pubkey_sum_ge, &seckey_sum_scalar);
272 /* We declassify the pubkey sum because serializing a group element (done in the
273 * `_calculate_input_hash_scalar` call following) is not a constant-time operation.
274 */
275 secp256k1_declassify(ctx, &prevouts_pubkey_sum_ge, sizeof(prevouts_pubkey_sum_ge));
276
277 /* 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.
278 * By multiplying the scalars together first, we can save an elliptic curve multiplication.
279 *
280 * Note: _input_hash_scalar can only fail if the output of the hash function is zero or greater than or equal to the
281 * curve order, which is statistically improbable. Returning an error here results in an untestable branch in the
282 * code, but we do this anyways to ensure strict compliance with BIP0352.
283 */
284 if (!secp256k1_silentpayments_calculate_input_hash_scalar(secp256k1_get_hash_context(ctx), &input_hash_scalar, outpoint_smallest36, &prevouts_pubkey_sum_ge)) {
285 secp256k1_scalar_clear(&seckey_sum_scalar);
286 return 0;
287 }
288 secp256k1_scalar_mul(&seckey_sum_scalar, &seckey_sum_scalar, &input_hash_scalar);
289 /* _recipient_sort sorts the array of recipients in place by their scan public keys (lexicographically).
290 * This ensures that all recipients with the same scan public key are grouped together, as specified in BIP0352.
291 *
292 * More specifically, this ensures `k` is incremented from 0 to the number of requested outputs for each recipient group,
293 * where a recipient group is all addresses with the same scan public key.
294 */
295 secp256k1_silentpayments_recipient_sort(ctx, recipients, n_recipients);
296 current_scan_pubkey = recipients[0]->scan_pubkey;
297 k = 0; /* This is a dead store but clang will emit a false positive warning if we omit it. */
298 for (i = 0; i < n_recipients; i++) {
299 if ((i == 0) || (secp256k1_ec_pubkey_cmp(ctx, &current_scan_pubkey, &recipients[i]->scan_pubkey) != 0)) {
300 /* If we are on a different scan pubkey, its time to recreate the shared secret and reset k to 0.
301 * It's very unlikely the scan public key is invalid by this point, since this means the caller would
302 * have created the _silentpayments_recipient object incorrectly, but just to be sure we still check that
303 * the public key is valid.
304 */
306 if (!secp256k1_pubkey_load(ctx, &pk, &recipients[i]->scan_pubkey)) {
307 secp256k1_scalar_clear(&seckey_sum_scalar);
308 /* Leaking this value would break indistinguishability of the transaction, so clear it. */
309 secp256k1_memclear_explicit(&shared_secret, sizeof(shared_secret));
310 return 0;
311 }
312 /* Creating the shared secret requires that the public and secret components are
313 * non-infinity and non-zero, respectively. Note that the involved parts (input hash,
314 * secret key sum, and scan public key) have all been verified at this point. */
315 secp256k1_silentpayments_create_shared_secret(shared_secret, &pk, &seckey_sum_scalar);
316 k = 0;
317 }
318 /* If creating another output for the current recipient group exceeded the
319 * protocol limit, fail, as the recipient wouldn't be guaranteed to find it. */
321 secp256k1_scalar_clear(&seckey_sum_scalar);
322 secp256k1_memclear_explicit(&shared_secret, sizeof(shared_secret));
323 return 0;
324 }
325 if (!secp256k1_silentpayments_create_output_pubkey(ctx, generated_outputs[recipients[i]->index], shared_secret, &recipients[i]->spend_pubkey, k)) {
326 secp256k1_scalar_clear(&seckey_sum_scalar);
327 secp256k1_memclear_explicit(&shared_secret, sizeof(shared_secret));
328 return 0;
329 }
330 current_scan_pubkey = recipients[i]->scan_pubkey;
331 k++;
332 }
333 secp256k1_scalar_clear(&seckey_sum_scalar);
334 secp256k1_memclear_explicit(&shared_secret, sizeof(shared_secret));
335 return 1;
336}
337
340 static const uint32_t midstate[8] = {
341 0x26b95d63ul, 0x8bf1b740ul, 0x10a5986ful, 0x06a387a5ul,
342 0x2d1c1c30ul, 0xd035951aul, 0x2d7f0f96ul, 0x29e3e0dbul
343 };
344 secp256k1_sha256_initialize_midstate(hash, 64, midstate);
345}
346
347static const unsigned char secp256k1_silentpayments_label_magic[4] = { 0x27, 0x9d, 0x44, 0xba };
348
349/* Saves a group element into a label. Requires that the provided group element is not infinity. */
351 memcpy(&label->data[0], secp256k1_silentpayments_label_magic, 4);
352 secp256k1_ge_to_bytes(label->data + 4, ge);
353}
354
355/* Loads a group element from a label. Returns 1 unless the label wasn't properly initialized. */
358 secp256k1_ge_from_bytes(ge, label->data + 4);
359 return 1;
360}
361
363 secp256k1_ge ge;
364
365 VERIFY_CHECK(ctx != NULL);
366 ARG_CHECK(label != NULL);
367 memset(label, 0, sizeof(*label));
368 ARG_CHECK(in33 != NULL);
369
370 if (!secp256k1_eckey_pubkey_parse(&ge, in33, 33)) {
371 return 0;
372 }
373
375 return 1;
376}
377
379 secp256k1_ge ge;
380
381 VERIFY_CHECK(ctx != NULL);
382 ARG_CHECK(out33 != NULL);
383 memset(out33, 0, 33);
384 ARG_CHECK(label != NULL);
385
386 if (!secp256k1_silentpayments_label_load(ctx, &ge, label)) {
387 return 0;
388 }
390 return 1;
391}
392
393int 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) {
394 secp256k1_sha256 hash;
395 unsigned char m_serialized[4];
396 secp256k1_ge label_ge;
397 secp256k1_scalar label_tweak_scalar;
398 int ret;
399
400 /* Sanity check inputs. */
401 VERIFY_CHECK(ctx != NULL);
402 ARG_CHECK(label != NULL);
403 memset(label, 0, sizeof(*label));
405 ARG_CHECK(label_tweak32 != NULL);
406 ARG_CHECK(scan_key32 != NULL);
407
408 /* ensure that the passed scan key is valid, in order to avoid creating unspendable labels */
409 ret = secp256k1_ec_seckey_verify(ctx, scan_key32);
410
411 /* Compute hash(ser_256(b_scan) || ser_32(m)) [sha256 with tag "BIP0352/Label"] */
413 secp256k1_sha256_write(secp256k1_get_hash_context(ctx), &hash, scan_key32, 32);
414 secp256k1_write_be32(m_serialized, m);
415 secp256k1_sha256_write(secp256k1_get_hash_context(ctx), &hash, m_serialized, sizeof(m_serialized));
416 secp256k1_sha256_finalize(secp256k1_get_hash_context(ctx), &hash, label_tweak32);
417
418 ret &= secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &label_tweak_scalar, &label_ge, label_tweak32);
419 secp256k1_silentpayments_label_save(label, &label_ge);
420 secp256k1_memczero(label, sizeof(*label), !ret);
421 secp256k1_memczero(label_tweak32, 32, !ret);
422
423 secp256k1_scalar_clear(&label_tweak_scalar);
424 secp256k1_memclear_explicit(m_serialized, sizeof(m_serialized));
426
427 return ret;
428}
429
431 secp256k1_ge labeled_spend_pubkey_ge, label_addend;
432 secp256k1_gej result_gej;
433 secp256k1_ge result_ge;
434 int ret;
435
436 /* Sanity check inputs. */
437 VERIFY_CHECK(ctx != NULL);
438 ARG_CHECK(labeled_spend_pubkey != NULL);
439 memset(labeled_spend_pubkey, 0, sizeof(*labeled_spend_pubkey));
440 ARG_CHECK(unlabeled_spend_pubkey != NULL);
441 ARG_CHECK(label != NULL);
442
443 /* Calculate labeled_spend_pubkey = unlabeled_spend_pubkey + label.
444 * If either the label or spend public key is invalid, return early.
445 */
446 ret = secp256k1_pubkey_load(ctx, &labeled_spend_pubkey_ge, unlabeled_spend_pubkey);
447 ret &= secp256k1_silentpayments_label_load(ctx, &label_addend, label);
448 if (!ret) {
449 return 0;
450 }
451 secp256k1_gej_set_ge(&result_gej, &labeled_spend_pubkey_ge);
452 secp256k1_gej_add_ge_var(&result_gej, &result_gej, &label_addend, NULL);
453 if (secp256k1_gej_is_infinity(&result_gej)) {
454 return 0;
455 }
456
457 secp256k1_ge_set_gej_var(&result_ge, &result_gej);
458 secp256k1_pubkey_save(labeled_spend_pubkey, &result_ge);
459
460 return 1;
461}
462
489 const secp256k1_context *ctx,
491 const unsigned char *outpoint_smallest36,
492 const secp256k1_xonly_pubkey * const *xonly_pubkeys,
493 size_t n_xonly_pubkeys,
494 const secp256k1_pubkey * const *pubkeys,
495 size_t n_pubkeys
496) {
497 size_t i;
498 secp256k1_ge prevouts_pubkey_sum_ge, addend;
499 secp256k1_gej prevouts_pubkey_sum_gej;
500 secp256k1_scalar input_hash_scalar;
501
502 /* Sanity check inputs */
503 VERIFY_CHECK(ctx != NULL);
504 ARG_CHECK(prevouts_summary != NULL);
505 memset(prevouts_summary, 0, sizeof(*prevouts_summary));
506 ARG_CHECK(outpoint_smallest36 != NULL);
507 ARG_CHECK((pubkeys != NULL) || (xonly_pubkeys != NULL));
508 if (xonly_pubkeys != NULL) {
509 ARG_CHECK(n_xonly_pubkeys > 0);
510 for (i = 0; i < n_xonly_pubkeys; i++) {
511 ARG_CHECK(xonly_pubkeys[i] != NULL);
512 }
513 } else {
514 ARG_CHECK(n_xonly_pubkeys == 0);
515 }
516 if (pubkeys != NULL) {
517 ARG_CHECK(n_pubkeys > 0);
518 for (i = 0; i < n_pubkeys; i++) {
519 ARG_CHECK(pubkeys[i] != NULL);
520 }
521 } else {
522 ARG_CHECK(n_pubkeys == 0);
523 }
524
525 /* Compute prevouts_pubkey_sum = A_1 + A_2 + ... + A_n.
526 *
527 * Since an attacker can maliciously craft transactions where the public keys sum to zero, fail early here
528 * to avoid making the caller do extra work, e.g., when building an index or scanning a malicious transaction.
529 *
530 * This will also fail if any of the provided prevout public keys are malformed.
531 */
532 secp256k1_gej_set_infinity(&prevouts_pubkey_sum_gej);
533 for (i = 0; i < n_pubkeys; i++) {
534 if (!secp256k1_pubkey_load(ctx, &addend, pubkeys[i])) {
535 return 0;
536 }
537 secp256k1_gej_add_ge_var(&prevouts_pubkey_sum_gej, &prevouts_pubkey_sum_gej, &addend, NULL);
538 }
539 for (i = 0; i < n_xonly_pubkeys; i++) {
540 if (!secp256k1_xonly_pubkey_load(ctx, &addend, xonly_pubkeys[i])) {
541 return 0;
542 }
543 secp256k1_gej_add_ge_var(&prevouts_pubkey_sum_gej, &prevouts_pubkey_sum_gej, &addend, NULL);
544 }
545 if (secp256k1_gej_is_infinity(&prevouts_pubkey_sum_gej)) {
546 return 0;
547 }
548 secp256k1_ge_set_gej_var(&prevouts_pubkey_sum_ge, &prevouts_pubkey_sum_gej);
549 /* Calculate the input_hash and convert it to a scalar.
550 *
551 * Note: _input_hash_scalar can only fail if the output of the hash function is zero or greater than or equal to the
552 * curve order, which is statistically improbable. Returning an error here results in an untestable branch in the
553 * code, but we do this anyways to ensure strict compliance with BIP0352.
554 */
555 if (!secp256k1_silentpayments_calculate_input_hash_scalar(secp256k1_get_hash_context(ctx), &input_hash_scalar, outpoint_smallest36, &prevouts_pubkey_sum_ge)) {
556 return 0;
557 }
558 memcpy(&prevouts_summary->data[0], secp256k1_silentpayments_prevouts_summary_magic, 4);
559 prevouts_summary->data[4] = 0;
560 secp256k1_ge_to_bytes(&prevouts_summary->data[5], &prevouts_pubkey_sum_ge);
561 secp256k1_scalar_get_b32(&prevouts_summary->data[5 + 64], &input_hash_scalar);
562 return 1;
563}
564
565/* Label scanning involves the transformation from Jacobian (gej) to affine (ge) coordinates
566 * for serializing label candidates. As this is an expensive operation involving modular
567 * inversion, we don't do this one by one for each tx output, but collect multiple label
568 * candidates in Jacobian in order to apply Montgomery's trick for batch inversion (using
569 * the function `secp256k1_ge_set_all_gej_var`). For transactions with a large number of
570 * outputs, this speeds up scanning significantly (~2.5x). */
571enum { LABEL_BATCH_SIZE = 8 }; /* batch size expressed in number of tx outputs */
572
573/* Check the label cache for each pair of candidates in a batch.
574 * Returns the first matching tx output index, or -1 if none has a matching label. */
576 secp256k1_ge *label_ge,
577 const unsigned char **label_tweak,
578 const secp256k1_gej *label_candidates_gej,
579 size_t n_batch,
580 size_t j_start,
582 const void *label_context
583) {
584 secp256k1_ge label_candidates_ge[2 * LABEL_BATCH_SIZE];
585 unsigned char label33[33];
586 size_t i;
587
588 *label_tweak = NULL;
589 secp256k1_ge_set_all_gej_var(label_candidates_ge, label_candidates_gej, 2 * n_batch);
590 for (i = 0; i < 2 * n_batch; i++) {
591 /* Serialize only non-infinity points because candidates are collected only when
592 * tx_output != unlabeled_output_xonly. */
593 secp256k1_eckey_pubkey_serialize33(&label_candidates_ge[i], label33);
594 *label_tweak = label_lookup(label33, label_context);
595 if (*label_tweak != NULL) {
596 *label_ge = label_candidates_ge[i];
597 return (int)(j_start + i / 2);
598 }
599 }
600 return -1;
601}
602
604 const secp256k1_context *ctx,
605 secp256k1_silentpayments_found_output **found_outputs, uint32_t *n_found_outputs,
606 const secp256k1_xonly_pubkey * const *tx_outputs, size_t n_tx_outputs,
607 const unsigned char *scan_key32,
608 const secp256k1_silentpayments_prevouts_summary *prevouts_summary,
609 const secp256k1_pubkey *unlabeled_spend_pubkey,
611 const void *label_context
612) {
613 secp256k1_scalar scan_key_scalar;
614 secp256k1_ge unlabeled_spend_pubkey_ge, prevouts_pubkey_sum_ge, tx_output_ge;
615 unsigned char shared_secret[33];
616 uint32_t k, k_max;
617 size_t i;
618 int found_idx, combined, valid_scan_key, ret;
619
620 /* Sanity check inputs */
621 VERIFY_CHECK(ctx != NULL);
622 ARG_CHECK(found_outputs != NULL);
623 ARG_CHECK(n_found_outputs != NULL);
624 *n_found_outputs = 0;
625 ARG_CHECK(tx_outputs != NULL);
626 ARG_CHECK(n_tx_outputs > 0);
627 for (i = 0; i < n_tx_outputs; i++) {
628 ARG_CHECK(found_outputs[i] != NULL);
629 ARG_CHECK(tx_outputs[i] != NULL);
630 /* Validate each tx output object early so malformed pubkeys are always rejected. */
631 if (!secp256k1_xonly_pubkey_load(ctx, &tx_output_ge, tx_outputs[i])) {
632 return 0;
633 }
634 }
635 ARG_CHECK(scan_key32 != NULL);
636 ARG_CHECK(prevouts_summary != NULL);
638 ARG_CHECK(unlabeled_spend_pubkey != NULL);
639 /* Passing a context without a lookup function is non-sensical */
640 if (label_context != NULL) {
641 ARG_CHECK(label_lookup != NULL);
642 }
643 valid_scan_key = secp256k1_scalar_set_b32_seckey(&scan_key_scalar, scan_key32);
644 secp256k1_declassify(ctx, &valid_scan_key, sizeof(valid_scan_key));
645 if (!valid_scan_key) {
646 secp256k1_scalar_clear(&scan_key_scalar);
647 return 0;
648 }
649 secp256k1_ge_from_bytes(&prevouts_pubkey_sum_ge, &prevouts_summary->data[5]);
650 combined = (int)prevouts_summary->data[4];
651 if (!combined) {
652 secp256k1_scalar input_hash_scalar;
653 secp256k1_scalar_set_b32(&input_hash_scalar, &prevouts_summary->data[5 + 64], NULL);
654 secp256k1_scalar_mul(&scan_key_scalar, &scan_key_scalar, &input_hash_scalar);
655 }
656 ret = secp256k1_pubkey_load(ctx, &unlabeled_spend_pubkey_ge, unlabeled_spend_pubkey);
657 if (!ret) {
658 secp256k1_scalar_clear(&scan_key_scalar);
659 return 0;
660 }
661 /* Creating the shared secret requires that the public and secret components are
662 * non-infinity and non-zero, respectively. Note that the involved parts (input hash,
663 * scan secret key, and prevouts public key sum) have all been verified at this point,
664 * assuming that the user hasn't tampered the `prevouts_summary` object manually. */
665 secp256k1_silentpayments_create_shared_secret(shared_secret, &prevouts_pubkey_sum_ge, &scan_key_scalar);
666 /* Clear the scan_key_scalar since we no longer need it and leaking this value would break indistinguishability of the transaction. */
667 secp256k1_scalar_clear(&scan_key_scalar);
668
669 found_idx = -1;
670 /* Don't look further than the per-group recipient limit, in order to avoid quadratic scaling issues. */
671 k_max = (n_tx_outputs < SECP256K1_SILENTPAYMENTS_RECIPIENT_GROUP_LIMIT) ?
673 for (k = 0; k < k_max; k++) {
674 secp256k1_scalar t_k_scalar;
675 secp256k1_xonly_pubkey unlabeled_output_xonly;
676 secp256k1_ge unlabeled_output_ge = unlabeled_spend_pubkey_ge;
677 secp256k1_ge unlabeled_output_negated_ge;
678 secp256k1_gej label_candidates_gej[2 * LABEL_BATCH_SIZE]; /* two candidates per tx output (one per y-parity) */
679 size_t label_batch_idx = 0; /* current index within a batch */
680 const unsigned char *label_tweak = NULL;
681 secp256k1_ge label_ge;
682 size_t j;
683
684 /* Calculate the output tweak t_k and convert it to a scalar.
685 *
686 * Note: _create_output_tweak can only fail if the output of the hash function is zero or greater than or equal
687 * to the curve order, which is statistically improbable. Returning an error here results in an untestable
688 * branch in the code, but we do this anyways to ensure strict compliance with BIP0352.
689 */
690 if (!secp256k1_silentpayments_create_output_tweak(ctx, &t_k_scalar, shared_secret, k)) {
691 secp256k1_scalar_clear(&t_k_scalar);
692 secp256k1_memclear_explicit(&shared_secret, sizeof(shared_secret));
693 return 0;
694 }
695
696 /* Calculate unlabeled_output = unlabeled_spend_pubkey + t_k * G.
697 * This can fail if t_k * G is the negation of unlabeled_spend_pubkey, but this happens only with negligible
698 * probability for honestly created unlabeled_spend_pubkey as t_k is the output of a hash function. */
699 if (!secp256k1_eckey_pubkey_tweak_add(&unlabeled_output_ge, &t_k_scalar)) {
700 /* Leaking these values would break indistinguishability of the transaction, so clear them. */
701 secp256k1_scalar_clear(&t_k_scalar);
702 secp256k1_memclear_explicit(&shared_secret, sizeof(shared_secret));
703 return 0;
704 }
705 /* Calculate unlabeled_output_negated = -unlabeled_output */
706 secp256k1_ge_neg(&unlabeled_output_negated_ge, &unlabeled_output_ge);
707
708 found_idx = -1;
709 secp256k1_fe_normalize_var(&unlabeled_output_ge.y);
710 secp256k1_extrakeys_ge_even_y(&unlabeled_output_ge);
711 secp256k1_xonly_pubkey_save(&unlabeled_output_xonly, &unlabeled_output_ge);
712 for (j = 0; j < n_tx_outputs; j++) {
713 if (secp256k1_xonly_pubkey_cmp(ctx, &unlabeled_output_xonly, tx_outputs[j]) == 0) {
714 label_tweak = NULL;
715 found_idx = j;
716 /* An earlier label match takes precedence over this direct match. */
717 if (label_batch_idx > 0) {
718 int label_found_idx = secp256k1_silentpayments_check_label_batch(
719 &label_ge, &label_tweak, label_candidates_gej, label_batch_idx,
720 j - label_batch_idx, label_lookup, label_context);
721 if (label_found_idx != -1) {
722 found_idx = label_found_idx;
723 }
724 }
725 break;
726 }
727
728 /* If not found, proceed to check for labels (if a label lookup function is provided). */
729 if (label_lookup != NULL) {
730 secp256k1_gej tx_output_gej;
731 secp256k1_gej *label_candidate1 = &label_candidates_gej[2 * label_batch_idx];
732 secp256k1_gej *label_candidate2 = &label_candidates_gej[2 * label_batch_idx + 1];
733
734 /* Calculate scan label candidates:
735 * label_candidate1 = tx_output - unlabeled_output
736 * label_candidate2 = -tx_output - unlabeled_output
737 * and store them in the batch */
738 secp256k1_xonly_pubkey_load(ctx, &tx_output_ge, tx_outputs[j]);
739 secp256k1_gej_set_ge(&tx_output_gej, &tx_output_ge);
740 secp256k1_gej_add_ge_var(label_candidate1, &tx_output_gej, &unlabeled_output_negated_ge, NULL);
741 secp256k1_gej_neg(&tx_output_gej, &tx_output_gej);
742 secp256k1_gej_add_ge_var(label_candidate2, &tx_output_gej, &unlabeled_output_negated_ge, NULL);
743 label_batch_idx++;
744 /* If the batch is filled or we have reached the last transaction output, perform batch
745 * inversion and check the label cache for each label candidate entry in the batch */
746 if (label_batch_idx == LABEL_BATCH_SIZE || j == (n_tx_outputs-1)) {
748 &label_ge, &label_tweak, label_candidates_gej, label_batch_idx,
749 j + 1 - label_batch_idx, label_lookup, label_context);
750 label_batch_idx = 0;
751 }
752 if (found_idx != -1) {
753 break;
754 }
755 }
756 }
757 if (found_idx != -1) {
758 found_outputs[k]->output = *tx_outputs[found_idx];
759 secp256k1_scalar_get_b32(found_outputs[k]->tweak, &t_k_scalar);
760 /* Clear the t_k_scalar since we no longer need it and leaking this value would
761 * break indistinguishability of the transaction. */
762 secp256k1_scalar_clear(&t_k_scalar);
763 if (label_tweak != NULL) {
764 found_outputs[k]->found_with_label = 1;
765 /* This is extremely unlikely to fail in that it can only really happen if label_tweak
766 * is the negation of the shared secret tweak. But since both tweak and label_tweak are
767 * created by hashing data, practically speaking this would only happen if an attacker
768 * tricked us into using a particular label_tweak (deviating from the protocol).
769 * Note that this call could also fail due to a malformed label_tweak data from the
770 * label cache, but we generally assume the passed in data is created using the API
771 * functions and thus have already been checked for correctness.
772 *
773 * Furthermore, although technically a failure for ec_seckey_tweak_add, this is not treated
774 * as a failure for Silent Payments because the output is still spendable with just the
775 * spend secret key. We set `tweak = 0` for this case.
776 */
777 if (!secp256k1_ec_seckey_tweak_add(ctx, found_outputs[k]->tweak, label_tweak)) {
778 memset(found_outputs[k]->tweak, 0, 32);
779 }
780 secp256k1_silentpayments_label_save(&found_outputs[k]->label, &label_ge);
781 } else {
782 found_outputs[k]->found_with_label = 0;
783 /* Set the label to an invalid value. */
784 memset(&found_outputs[k]->label, 0, sizeof(found_outputs[k]->label));
785 }
786 /* Reset everything for the next round of scanning. */
787 label_tweak = NULL;
788 } else {
789 secp256k1_scalar_clear(&t_k_scalar);
790 break;
791 }
792 }
793 *n_found_outputs = k;
794
795 /* Leaking the shared_secret would break indistinguishability of the transaction, so clear it. */
796 secp256k1_memclear_explicit(shared_secret, sizeof(shared_secret));
797 return 1;
798}
799
800#endif
int ret
if(!SetupNetworking())
static int secp256k1_eckey_pubkey_tweak_add(secp256k1_ge *key, const secp256k1_scalar *tweak)
static void secp256k1_eckey_pubkey_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 int secp256k1_eckey_pubkey_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size)
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 *ctx, secp256k1_ge *r, const secp256k1_scalar *a)
static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context *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_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 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:269
static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n)
Semantics like memcmp.
Definition: util.h:282
static SECP256K1_INLINE void secp256k1_write_be32(unsigned char *p, uint32_t x)
Definition: util.h:437
#define VERIFY_CHECK(cond)
Definition: util.h:170
static SECP256K1_INLINE void secp256k1_memczero(void *s, size_t len, int flag)
Definition: util.h:221
#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:627
static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context *ctx, const void *p, size_t len)
Definition: secp256k1.c:255
static int secp256k1_pubkey_load(const secp256k1_context *ctx, secp256k1_ge *ge, const secp256k1_pubkey *pubkey)
Definition: secp256k1.c:259
static void secp256k1_pubkey_save(secp256k1_pubkey *pubkey, secp256k1_ge *ge)
Definition: secp256k1.c:265
static SECP256K1_INLINE const secp256k1_hash_ctx * secp256k1_get_hash_context(const secp256k1_context *ctx)
Definition: secp256k1.c:238
#define SECP256K1_TAG_PUBKEY_EVEN
Prefix byte used to tag various encoded curvepoints for specific purposes.
Definition: secp256k1.h:229
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 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
#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:571
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:603
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:362
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:488
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:393
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:378
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:339
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:350
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:347
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:430
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:575
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:356
const unsigned char * label_lookup(const unsigned char *label33, const void *cache_ptr)
secp256k1_ecmult_gen_context ecmult_gen_ctx
Definition: secp256k1.c:62
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.