Skip to content

Commit 5ce5a45

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 59fb619 commit 5ce5a45

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/ctime_tests.c

+79
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,25 @@ 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_recipient_public_data public_data;
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+
#endif
97120

98121
for (i = 0; i < 32; i++) {
99122
msg[i] = i + 1;
@@ -263,5 +286,61 @@ static void run_tests(secp256k1_context *ctx, unsigned char *key) {
263286
CHECK(ret == 1);
264287
}
265288

289+
#endif
290+
291+
#ifdef ENABLE_MODULE_SILENTPAYMENTS
292+
SECP256K1_CHECKMEM_DEFINE(key, 32);
293+
294+
generated_outputs[0] = &generated_output;
295+
296+
/* Initialize recipient */
297+
CHECK(secp256k1_ec_pubkey_create(ctx, &recipient.scan_pubkey, key));
298+
key[31] ^= 1;
299+
CHECK(secp256k1_ec_pubkey_create(ctx, &recipient.spend_pubkey, key));
300+
key[31] ^= (1 << 1);
301+
recipient.index = 0;
302+
recipients[0] = &recipient;
303+
304+
/* Set up secret keys */
305+
SECP256K1_CHECKMEM_UNDEFINE(key, 32);
306+
ret = secp256k1_keypair_create(ctx, &taproot_seckey, key);
307+
SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
308+
CHECK(ret);
309+
key[31] ^= (1 << 2);
310+
taproot_seckeys[0] = &taproot_seckey;
311+
plain_seckeys[0] = key;
312+
313+
ret = secp256k1_silentpayments_sender_create_outputs(ctx, generated_outputs, recipients, 1, outpoint_smallest, taproot_seckeys, 1, plain_seckeys, 1);
314+
CHECK(ret == 1);
315+
316+
/* TODO: use non-confusing public key */
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_public_data_create(ctx, &public_data, 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+
/* TODO: make sure we're actually go through all relevant code paths */
338+
ret = secp256k1_silentpayments_recipient_scan_outputs(ctx, found_outputs, &n_found_outputs, tx_outputs, 1, key, &public_data, &recipient.spend_pubkey, NULL, NULL);
339+
CHECK(ret == 1);
340+
341+
/* TODO: this fails */
342+
/* CHECK(secp256k1_silentpayments_recipient_create_shared_secret(ctx, shared_secret, key, &public_data)); */
343+
/* TODO: test secp256k1_silentpayments_recipient_create_output_pubkey */
344+
266345
#endif
267346
}

0 commit comments

Comments
 (0)