Merge bitcoin-core/secp256k1#1983: silentpayments: return 0 instead of `ARG_CHECK`ing for empty inputs/outputs
What changed, and why it matters
This update fixes a crash risk in Bitcoin's silent payments scanning code. Before the change, if a transaction had no suitable inputs or no taproot outputs, the library treated that as an illegal argument and could call abort(), killing the program. Now it calmly returns 0, matching its own documentation. This matters because a stranger could craft a transaction that crashes a wallet or indexing service that forgot to filter these cases first.
Upgrade to a release containing this commit. If running a service that scans blocks or builds silent-payments indexes, ensure the library is updated so that transactions with no eligible inputs or no taproot outputs no longer crash the process. No application-level workaround is needed after patching.
Security signals we found
Denial-of-service vector: maliciously crafted transaction could trigger abort() in a scanning/indexing process
API contract violation: documented return-0 behavior was not implemented for empty inputs/outputs
ARG_CHECK removal in two public silentpayments functions
NULL pointer attributes relaxed for empty arrays
Test expectations changed from CHECK_ILLEGAL to CHECK(ret == 0)
Evidence from the diff
The patch removes ARG_CHECK guards that aborted on empty input/output arrays in secp256k1_silentpayments_recipient_prevouts_summary_create and secp256k1_silentpayments_recipient_scan_outputs. For prevouts_summary_create, dropping the non-empty-key ARG_CHECK lets the existing ‘sum is infinity’ check return 0 when no pubkeys are supplied. For scan_outputs, the code now validates tx_outputs/found_outputs only when n_tx_outputs > 0 and explicitly returns 0 for n_tx_outputs == 0 after validating other arguments. The header’s SECP256K1_ARG_NONNULL attributes and documentation are updated accordingly. Tests are adjusted to expect return 0 instead of illegal-callback invocation.
Changed components
src/modules/silentpayments/main_impl.hinclude/secp256k1_silentpayments.hsrc/modules/silentpayments/tests_impl.hInspect captured patch +46 / −33
### CHANGELOG.md
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
#### Fixed
- Module `silentpayments`: `secp256k1_silentpayments_sender_create_outputs` and `secp256k1_silentpayments_recipient_prevouts_summary_create` no longer require empty key arrays to be passed as `NULL`. If the corresponding size argument is 0, the array pointer is ignored. This matches the API documentation, which only states that unused arrays *can* be `NULL`, and spares callers from special-casing empty arrays.
+ - Module `silentpayments`: `secp256k1_silentpayments_recipient_prevouts_summary_create` and `secp256k1_silentpayments_recipient_scan_outputs` now return 0 when called with no input public keys or no taproot outputs, respectively, instead of treating this as an illegal argument (which by default calls `abort()`). In addition, `secp256k1_silentpayments_recipient_scan_outputs` no longer requires `tx_outputs` and `found_outputs` to be non-`NULL` if `n_tx_outputs` is 0. This matches the API documentation, which states that 0 is returned if the transaction is not a Silent Payments transaction, and allows callers to pass transactions without eligible inputs or without taproot outputs as is.
## [0.8.0] - 2026-08-03
### include/secp256k1_silentpayments.h
@@ -261,7 +261,9 @@ typedef struct secp256k1_silentpayments_prevouts_summary {
* The resulting data can be used for scanning on the recipient side.
*
* Returns: 1 if prevouts summary creation was successful.
- * 0 if the transaction is not a Silent Payments transaction.
+ * 0 if the transaction is not a Silent Payments transaction, e.g.,
+ * if no public keys are passed (the transaction has no eligible
+ * inputs) or if the public keys sum to the point at infinity.
*
* Args: ctx: pointer to a context object
* Out: prevouts_summary: pointer to prevouts_summary object containing the
@@ -347,20 +349,23 @@ typedef struct secp256k1_silentpayments_found_output {
* key group than this limit.
*
* Returns: 1 if output scanning was successful.
- * 0 if the transaction is not a Silent Payments transaction,
+ * 0 if the transaction is not a Silent Payments transaction (e.g.,
+ * if it has no taproot outputs, i.e., n_tx_outputs is 0),
* or if the arguments are invalid.
*
* Args: ctx: pointer to a context object
* Out: found_outputs: pointer to an array of pointers to found
* output objects. The found outputs array MUST
- * have the same length as the tx_outputs array.
+ * have the same length as the tx_outputs array
+ * (can be NULL if n_tx_outputs is 0).
* n_found_outputs: pointer to an integer indicating the final
* size of the found outputs array. This number
* represents the number of outputs found while
* scanning (0 if none are found). Can't be larger than
* SECP256K1_SILENTPAYMENTS_RECIPIENT_GROUP_LIMIT.
* In: tx_outputs: pointer to the transaction's x-only public key outputs,
- * in their original transaction (vout) order
+ * in their original transaction (vout) order (can be
+ * NULL if n_tx_outputs is 0)
* n_tx_outputs: the size of the tx_outputs array.
* scan_key32: pointer to the recipient's 32 byte scan key. The scan
* key is valid if it passes secp256k1_ec_seckey_verify
@@ -387,7 +392,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_recipien
const secp256k1_pubkey *unlabeled_spend_pubkey,
secp256k1_silentpayments_label_lookup label_lookup,
const void *label_context
-) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(6) SECP256K1_ARG_NONNULL(7) SECP256K1_ARG_NONNULL(8);
+) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(6) SECP256K1_ARG_NONNULL(7) SECP256K1_ARG_NONNULL(8);
#ifdef __cplusplus
}
### src/modules/silentpayments/main_impl.h
@@ -497,7 +497,6 @@ int secp256k1_silentpayments_recipient_prevouts_summary_create(
ARG_CHECK(prevouts_summary != NULL);
memset(prevouts_summary, 0, sizeof(*prevouts_summary));
ARG_CHECK(outpoint_smallest36 != NULL);
- ARG_CHECK((n_pubkeys > 0) || (n_xonly_pubkeys > 0));
if (n_xonly_pubkeys > 0) {
ARG_CHECK(xonly_pubkeys != NULL);
for (i = 0; i < n_xonly_pubkeys; i++) {
@@ -516,7 +515,8 @@ int secp256k1_silentpayments_recipient_prevouts_summary_create(
* Since an attacker can maliciously craft transactions where the public keys sum to zero, fail early here
* to avoid making the caller do extra work, e.g., when building an index or scanning a malicious transaction.
*
- * This will also fail if any of the provided prevout public keys are malformed.
+ * This will also fail if any of the provided prevout public keys are malformed, or if no public keys were
+ * provided at all (i.e., the transaction has no eligible inputs), as the sum is then the point at infinity.
*/
secp256k1_gej_set_infinity(&prevouts_pubkey_sum_gej);
for (i = 0; i < n_pubkeys; i++) {
@@ -611,17 +611,18 @@ int secp256k1_silentpayments_recipient_scan_outputs(
/* Sanity check inputs */
VERIFY_CHECK(ctx != NULL);
- ARG_CHECK(found_outputs != NULL);
ARG_CHECK(n_found_outputs != NULL);
*n_found_outputs = 0;
- ARG_CHECK(tx_outputs != NULL);
- ARG_CHECK(n_tx_outputs > 0);
- for (i = 0; i < n_tx_outputs; i++) {
- ARG_CHECK(found_outputs[i] != NULL);
- ARG_CHECK(tx_outputs[i] != NULL);
- /* Validate each tx output object early so malformed pubkeys are always rejected. */
- if (!secp256k1_xonly_pubkey_load(ctx, &tx_output_ge, tx_outputs[i])) {
- return 0;
+ if (n_tx_outputs > 0) {
+ ARG_CHECK(found_outputs != NULL);
+ ARG_CHECK(tx_outputs != NULL);
+ for (i = 0; i < n_tx_outputs; i++) {
+ ARG_CHECK(found_outputs[i] != NULL);
+ ARG_CHECK(tx_outputs[i] != NULL);
+ /* Validate each tx output object early so malformed pubkeys are always rejected. */
+ if (!secp256k1_xonly_pubkey_load(ctx, &tx_output_ge, tx_outputs[i])) {
+ return 0;
+ }
}
}
ARG_CHECK(scan_key32 != NULL);
@@ -632,6 +633,10 @@ int secp256k1_silentpayments_recipient_scan_outputs(
if (label_context != NULL) {
ARG_CHECK(label_lookup != NULL);
}
+ /* A transaction without any taproot outputs is not a Silent Payments transaction. */
+ if (n_tx_outputs == 0) {
+ return 0;
+ }
valid_scan_key = secp256k1_scalar_set_b32_seckey(&scan_key_scalar, scan_key32);
secp256k1_declassify(ctx, &valid_scan_key, sizeof(valid_scan_key));
if (!valid_scan_key) {
### src/modules/silentpayments/tests_impl.h
@@ -556,8 +556,10 @@ static void test_recipient_api(void) {
CHECK(secp256k1_silentpayments_recipient_prevouts_summary_create(CTX, &ps, SMALLEST_OUTPOINT, tp, 0, pp, 1));
CHECK(secp256k1_silentpayments_recipient_prevouts_summary_create(CTX, &ps, SMALLEST_OUTPOINT, tp, 1, NULL, 0));
CHECK(secp256k1_silentpayments_recipient_prevouts_summary_create(CTX, &ps, SMALLEST_OUTPOINT, tp, 1, pp, 0));
- CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_prevouts_summary_create(CTX, &ps, SMALLEST_OUTPOINT, NULL, 0, pp, 0));
- CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_prevouts_summary_create(CTX, &ps, SMALLEST_OUTPOINT, NULL, 0, NULL, 0));
+ /* A transaction without any eligible inputs is not a Silent Payments transaction (no illegal argument) */
+ CHECK(secp256k1_silentpayments_recipient_prevouts_summary_create(CTX, &ps, SMALLEST_OUTPOINT, NULL, 0, pp, 0) == 0);
+ CHECK(secp256k1_silentpayments_recipient_prevouts_summary_create(CTX, &ps, SMALLEST_OUTPOINT, tp, 0, pp, 0) == 0);
+ CHECK(secp256k1_silentpayments_recipient_prevouts_summary_create(CTX, &ps, SMALLEST_OUTPOINT, NULL, 0, NULL, 0) == 0);
CHECK(secp256k1_silentpayments_recipient_prevouts_summary_create(CTX, &ps, SMALLEST_OUTPOINT, tp, 1, pp, 1));
/* check the _recipient_scan_outputs cornercase where internal tweaking would fail;
@@ -634,8 +636,14 @@ static void test_recipient_api(void) {
CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 1, NULL, &ps, &p, &label_lookup, &labels_cache));
CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 1, ALICE_SECKEY, NULL, &p, &label_lookup, &labels_cache));
CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 1, ALICE_SECKEY, &ps, NULL, &label_lookup, &labels_cache));
- CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 0, ALICE_SECKEY, &ps, &p, &label_lookup, &labels_cache));
CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 1, ALICE_SECKEY, &ps, &p, NULL, &labels_cache));
+ /* A transaction without any taproot outputs is not a Silent Payments transaction (no illegal argument) */
+ n_f = 1;
+ CHECK(secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 0, ALICE_SECKEY, &ps, &p, &label_lookup, &labels_cache) == 0);
+ CHECK(n_f == 0);
+ n_f = 1;
+ CHECK(secp256k1_silentpayments_recipient_scan_outputs(CTX, NULL, &n_f, NULL, 0, ALICE_SECKEY, &ps, &p, &label_lookup, &labels_cache) == 0);
+ CHECK(n_f == 0);
/* Check that malformed secret key, public keys, and prevouts_summary arguments are handled */
CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 1, ALICE_SECKEY, &ps, &malformed_p, NULL, NULL));
@@ -876,20 +884,14 @@ void run_silentpayments_test_vector_receive(const struct bip352_test_vector *tes
CHECK(secp256k1_xonly_pubkey_parse(CTX, &xonly_pubkeys_objs[i], test->xonly_pubkeys[i]));
xonly_pubkeys[i] = &xonly_pubkeys_objs[i];
}
- {
- int32_t ecount = 0;
- secp256k1_context_set_illegal_callback(CTX, counting_callback_fn, &ecount);
- ret = secp256k1_silentpayments_recipient_prevouts_summary_create(CTX, &prevouts_summary,
- test->outpoint_smallest,
- test->num_taproot_inputs > 0 ? xonly_pubkeys : NULL, test->num_taproot_inputs,
- test->num_plain_inputs > 0 ? pubkeys : NULL, test->num_plain_inputs
- );
- secp256k1_context_set_illegal_callback(CTX, NULL, NULL);
- /* We expect exactly one ARG_CHECK if the number of input keys was 0. */
- CHECK(ecount == ((test->num_taproot_inputs + test->num_plain_inputs) == 0));
- }
- /* If we are unable to create the prevouts_summary object, e.g., the input public keys sum to
- * zero, check that the expected number of recipient outputs for this test case is zero
+ ret = secp256k1_silentpayments_recipient_prevouts_summary_create(CTX, &prevouts_summary,
+ test->outpoint_smallest,
+ xonly_pubkeys, test->num_taproot_inputs,
+ pubkeys, test->num_plain_inputs
+ );
+ /* If we are unable to create the prevouts_summary object, e.g., there are no eligible inputs or
+ * the input public keys sum to zero, check that the expected number of recipient outputs for
+ * this test case is zero
*/
if (!ret) {
CHECK(subtest->num_found_output_pubkeys == 0);Why this scored 76/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.