Merge bitcoin-core/secp256k1#1898: tests: Silent Payments ctime test never reaches a match or label lookup
What changed, and why it matters
This commit fixes a flawed test in the libsecp256k1 cryptographic library. The test was supposed to exercise the Silent Payments constant-time scanning code, but due to a bug in how the test was written, it never actually verified that the scanner found a matching output or exercised the label-lookup code path. The fix makes the test generate the output with the correct key, adds a non-matching output, supplies a label callback, and checks that exactly one unlabeled match is found. This is a test-only improvement; no production code behavior changes and no vulnerability is introduced or fixed in shipped software.
No urgent action. Treat as a normal test-quality improvement. If auditing constant-time properties of the Silent Payments module, note that this commit improves test coverage but does not itself change the implementation.
Security signals we found
Test-only change in ctime_tests.c
Fixes incorrect test key mutation that prevented matching output
Adds label callback to exercise label-candidate serialization/batching path
Adds assertions on scan result and label lookup count
No change to production secp256k1 or silentpayments implementation code
Evidence from the diff
The change is confined to src/ctime_tests.c, a constant-time test harness. Previously the test created a recipient whose scan_pubkey was derived from key, then mutated key[31] before creating the sender output, so the generated output did not match the recipient’s scan key. It also passed NULL for the label callback, meaning label-candidate serialization and batching were never executed, and it never asserted n_found_outputs. The patch derives recipient.scan_pubkey from the same key used for sender output creation, prepends a non-matching xonly pubkey, provides a label_lookup_none callback that checks each 33-byte candidate is defined memory and counts invocations, and asserts one direct unlabeled match with the expected number of label lookups. This improves constant-time coverage but does not alter the library’s runtime behavior.
Changed components
src/ctime_tests.cSilent Payments constant-time testInspect captured patch +27 / −13
### src/ctime_tests.c
@@ -42,6 +42,15 @@
#ifdef ENABLE_MODULE_SILENTPAYMENTS
#include "../include/secp256k1_silentpayments.h"
+
+static int label_lookups;
+
+static const unsigned char *label_lookup_none(const unsigned char *label33, const void *label_context) {
+ (void)label_context;
+ SECP256K1_CHECKMEM_CHECK(label33, 33);
+ label_lookups++;
+ return NULL;
+}
#endif
#if defined(__GNUC__)
@@ -123,10 +132,10 @@ static void run_tests(secp256k1_context *ctx, unsigned char *key) {
secp256k1_keypair sp_keypair;
const secp256k1_keypair *sp_keypairs[1];
const unsigned char *sp_seckeys[1];
- secp256k1_silentpayments_found_output found_outputs[1];
- secp256k1_silentpayments_found_output *found_outputs_ptrs[1];
+ secp256k1_silentpayments_found_output found_outputs[2];
+ secp256k1_silentpayments_found_output *found_outputs_ptrs[2];
uint32_t n_found_outputs;
- const secp256k1_xonly_pubkey *tx_outputs[1];
+ const secp256k1_xonly_pubkey *tx_outputs[2];
secp256k1_silentpayments_prevouts_summary prevouts_summary;
unsigned char label_tweak[32] = { 0 };
secp256k1_xonly_pubkey sp_xonly_pubkey;
@@ -342,7 +351,6 @@ static void run_tests(secp256k1_context *ctx, unsigned char *key) {
generated_outputs[0] = &generated_output;
/* Initialize recipient */
- CHECK(secp256k1_ec_pubkey_create(ctx, &recipient.scan_pubkey, key));
key[31] ^= 1;
CHECK(secp256k1_ec_pubkey_create(ctx, &recipient.spend_pubkey, key));
key[31] ^= (1 << 1);
@@ -358,9 +366,6 @@ static void run_tests(secp256k1_context *ctx, unsigned char *key) {
sp_keypairs[0] = &sp_keypair;
sp_seckeys[0] = key;
- ret = secp256k1_silentpayments_sender_create_outputs(ctx, generated_outputs, recipients, 1, outpoint_smallest, sp_keypairs, 1, sp_seckeys, 1);
- CHECK(ret == 1);
-
ret = secp256k1_silentpayments_recipient_label_create(ctx, &label, label_tweak, key, 0);
key[31] ^= (1 << 3);
SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
@@ -378,14 +383,23 @@ static void run_tests(secp256k1_context *ctx, unsigned char *key) {
ret = secp256k1_silentpayments_recipient_prevouts_summary_create(ctx, &prevouts_summary, outpoint_smallest, sp_xonly_pubkeys, 1, sp_pubkeys, 1);
CHECK(ret == 1);
- tx_outputs[0] = generated_outputs[0];
+ /* key is also the scan seckey at scan time, so sp_pubkey doubles as the
+ * scan pubkey, making the generated output match during scanning. */
+ recipient.scan_pubkey = sp_pubkey;
+ CHECK(secp256k1_silentpayments_sender_create_outputs(ctx, generated_outputs, recipients, 1, outpoint_smallest, sp_keypairs, 1, sp_seckeys, 1));
+
found_outputs_ptrs[0] = &found_outputs[0];
- n_found_outputs = 1;
+ found_outputs_ptrs[1] = &found_outputs[1];
SECP256K1_CHECKMEM_DEFINE(&recipient.spend_pubkey, sizeof(recipient.spend_pubkey));
- /* It is sufficient to check _recipient_scan_outputs without a label lookup function, since the shared secret is created once (which is where the constant timeness matters)
- * and then reused for the rest of the scanning logic.
- */
- CHECK(secp256k1_silentpayments_recipient_scan_outputs(ctx, found_outputs_ptrs, &n_found_outputs, tx_outputs, 1, key, &prevouts_summary, &recipient.spend_pubkey, NULL, NULL));
+ tx_outputs[0] = &sp_xonly_pubkey;
+ tx_outputs[1] = &generated_output;
+ label_lookups = 0;
+ CHECK(secp256k1_silentpayments_recipient_scan_outputs(ctx, found_outputs_ptrs, &n_found_outputs, tx_outputs, 2, key, &prevouts_summary, &recipient.spend_pubkey, label_lookup_none, NULL));
+ CHECK(n_found_outputs == 1);
+ CHECK(found_outputs[0].found_with_label == 0);
+ /* Two candidates flush right before the direct k=0 match, four at the final k=1 miss.
+ * Both flush paths run as long as LABEL_BATCH_SIZE stays >= 2. */
+ CHECK(label_lookups == 2 + 4);
#endif
}Why this scored 16/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.