Skip to content

Commit 185c8bc

Browse files
josibakejonasnicktheStack
committed
tests: add constant time tests
Co-authored-by: Jonas Nick <[email protected]> Co-authored-by: Sebastian Falbesoner <[email protected]>
1 parent 73fd32d commit 185c8bc

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

src/ctime_tests.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
#include "../include/secp256k1_ellswift.h"
4141
#endif
4242

43+
#ifdef ENABLE_MODULE_SILENTPAYMENTS
44+
#include "../include/secp256k1_silentpayments.h"
45+
#endif
46+
4347
static void run_tests(secp256k1_context *ctx, unsigned char *key);
4448

4549
int main(void) {
@@ -94,6 +98,26 @@ static void run_tests(secp256k1_context *ctx, unsigned char *key) {
9498
unsigned char ellswift[64];
9599
static const unsigned char prefix[64] = {'t', 'e', 's', 't'};
96100
#endif
101+
#ifdef ENABLE_MODULE_SILENTPAYMENTS
102+
secp256k1_xonly_pubkey generated_output;
103+
secp256k1_xonly_pubkey *generated_outputs[1];
104+
secp256k1_silentpayments_recipient recipient;
105+
const secp256k1_silentpayments_recipient *recipients[1];
106+
unsigned char outpoint_smallest[36] = { 0 };
107+
secp256k1_keypair taproot_seckey;
108+
const secp256k1_keypair *taproot_seckeys[1];
109+
const unsigned char *plain_seckeys[1];
110+
secp256k1_silentpayments_found_output *found_outputs[1];
111+
size_t n_found_outputs;
112+
const secp256k1_xonly_pubkey *tx_outputs[1];
113+
secp256k1_silentpayments_prevouts_summary prevouts_summary;
114+
unsigned char label_tweak[32] = { 0 };
115+
secp256k1_xonly_pubkey xonly_pubkey;
116+
const secp256k1_xonly_pubkey *xonly_pubkeys[1];
117+
secp256k1_pubkey plain_pubkey;
118+
const secp256k1_pubkey *plain_pubkeys[1];
119+
unsigned char shared_secret[33] = { 0 };
120+
#endif
97121

98122
for (i = 0; i < 32; i++) {
99123
msg[i] = i + 1;
@@ -263,5 +287,59 @@ static void run_tests(secp256k1_context *ctx, unsigned char *key) {
263287
CHECK(ret == 1);
264288
}
265289

290+
#endif
291+
292+
#ifdef ENABLE_MODULE_SILENTPAYMENTS
293+
SECP256K1_CHECKMEM_DEFINE(key, 32);
294+
295+
generated_outputs[0] = &generated_output;
296+
297+
/* Initialize recipient */
298+
CHECK(secp256k1_ec_pubkey_create(ctx, &recipient.scan_pubkey, key));
299+
key[31] ^= 1;
300+
CHECK(secp256k1_ec_pubkey_create(ctx, &recipient.spend_pubkey, key));
301+
key[31] ^= (1 << 1);
302+
recipient.index = 0;
303+
recipients[0] = &recipient;
304+
305+
/* Set up secret keys */
306+
SECP256K1_CHECKMEM_UNDEFINE(key, 32);
307+
ret = secp256k1_keypair_create(ctx, &taproot_seckey, key);
308+
SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
309+
CHECK(ret);
310+
key[31] ^= (1 << 2);
311+
taproot_seckeys[0] = &taproot_seckey;
312+
plain_seckeys[0] = key;
313+
314+
ret = secp256k1_silentpayments_sender_create_outputs(ctx, generated_outputs, recipients, 1, outpoint_smallest, taproot_seckeys, 1, plain_seckeys, 1);
315+
CHECK(ret == 1);
316+
317+
ret = secp256k1_silentpayments_recipient_create_label(ctx, &recipient.spend_pubkey, label_tweak, key, 0);
318+
key[31] ^= (1 << 3);
319+
SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
320+
CHECK(ret == 1);
321+
322+
CHECK(secp256k1_keypair_xonly_pub(ctx, &xonly_pubkey, NULL, &taproot_seckey));
323+
SECP256K1_CHECKMEM_DEFINE(&xonly_pubkey, sizeof(xonly_pubkey));
324+
xonly_pubkeys[0] = &xonly_pubkey;
325+
ret = secp256k1_ec_pubkey_create(ctx, &plain_pubkey, plain_seckeys[0]);
326+
SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
327+
CHECK(ret == 1);
328+
SECP256K1_CHECKMEM_DEFINE(&plain_pubkey, sizeof(plain_pubkey));
329+
plain_pubkeys[0] = &plain_pubkey;
330+
331+
ret = secp256k1_silentpayments_recipient_prevouts_summary_create(ctx, &prevouts_summary, outpoint_smallest, xonly_pubkeys, 1, plain_pubkeys, 1);
332+
CHECK(ret == 1);
333+
334+
tx_outputs[0] = generated_outputs[0];
335+
n_found_outputs = 1;
336+
SECP256K1_CHECKMEM_DEFINE(&recipient.spend_pubkey, sizeof(recipient.spend_pubkey));
337+
/* 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)
338+
* and then reused for the rest of the scanning logic.
339+
*/
340+
CHECK(secp256k1_silentpayments_recipient_scan_outputs(ctx, found_outputs, &n_found_outputs, tx_outputs, 1, key, &prevouts_summary, &recipient.spend_pubkey, NULL, NULL));
341+
CHECK(secp256k1_silentpayments_recipient_create_shared_secret(ctx, shared_secret, key, &prevouts_summary));
342+
CHECK(secp256k1_silentpayments_recipient_create_output_pubkey(ctx, &xonly_pubkey, shared_secret, &recipient.spend_pubkey, 0));
343+
266344
#endif
267345
}

0 commit comments

Comments
 (0)