Skip to content

Commit ee94cf3

Browse files
committed
silentpayments: add benchmarks for scanning
Add a benchmark for a full transaction scan and for scanning a single output. Only benchmarks for scanning are added as this is the most performance critical portion of the protocol.
1 parent c8ccfd3 commit ee94cf3

File tree

3 files changed

+156
-1
lines changed

3 files changed

+156
-1
lines changed

src/bench.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ static void help(int default_iters) {
3232
printf(" - ElligatorSwift (optional module)\n");
3333
#endif
3434

35+
#ifdef ENABLE_MODULE_SILENTPAYMENTS
36+
printf(" - Silent payments (optional module)\n");
37+
#endif
38+
3539
printf("\n");
3640
printf("The default number of iterations for each benchmark is %d. This can be\n", default_iters);
3741
printf("customized using the SECP256K1_BENCH_ITERS environment variable.\n");
@@ -68,6 +72,10 @@ static void help(int default_iters) {
6872
printf(" ellswift_ecdh : ECDH on ElligatorSwift keys\n");
6973
#endif
7074

75+
#ifdef ENABLE_MODULE_SILENTPAYMENTS
76+
printf(" silentpayments : Silent payments recipient scanning\n");
77+
#endif
78+
7179
printf("\n");
7280
}
7381

@@ -170,6 +178,10 @@ static void bench_keygen_run(void *arg, int iters) {
170178
# include "modules/ellswift/bench_impl.h"
171179
#endif
172180

181+
#ifdef ENABLE_MODULE_SILENTPAYMENTS
182+
# include "modules/silentpayments/bench_impl.h"
183+
#endif
184+
173185
int main(int argc, char** argv) {
174186
int i;
175187
secp256k1_pubkey pubkey;
@@ -184,7 +196,7 @@ int main(int argc, char** argv) {
184196
char* valid_args[] = {"ecdsa", "verify", "ecdsa_verify", "sign", "ecdsa_sign", "ecdh", "recover",
185197
"ecdsa_recover", "schnorrsig", "schnorrsig_verify", "schnorrsig_sign", "ec",
186198
"keygen", "ec_keygen", "ellswift", "encode", "ellswift_encode", "decode",
187-
"ellswift_decode", "ellswift_keygen", "ellswift_ecdh"};
199+
"ellswift_decode", "ellswift_keygen", "ellswift_ecdh", "silentpayments"};
188200
size_t valid_args_size = sizeof(valid_args)/sizeof(valid_args[0]);
189201
int invalid_args = have_invalid_args(argc, argv, valid_args, valid_args_size);
190202

@@ -236,6 +248,14 @@ int main(int argc, char** argv) {
236248
}
237249
#endif
238250

251+
#ifndef ENABLE_MODULE_SILENTPAYMENTS
252+
if (have_flag(argc, argv, "silentpayments")) {
253+
fprintf(stderr, "./bench: silentpayments module not enabled.\n");
254+
fprintf(stderr, "Use ./configure --enable-module-silentpayments.\n\n");
255+
return 1;
256+
}
257+
#endif
258+
239259
/* ECDSA benchmark */
240260
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
241261

@@ -280,5 +300,11 @@ int main(int argc, char** argv) {
280300
run_ellswift_bench(iters, argc, argv);
281301
#endif
282302

303+
#ifdef ENABLE_MODULE_SILENTPAYMENTS
304+
/* SilentPayments benchmarks */
305+
run_silentpayments_bench(iters, argc, argv);
306+
#endif
307+
308+
283309
return EXIT_SUCCESS;
284310
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
include_HEADERS += include/secp256k1_silentpayments.h
22
noinst_HEADERS += src/modules/silentpayments/main_impl.h
3+
noinst_HEADERS += src/modules/silentpayments/bench_impl.h
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/***********************************************************************
2+
* Copyright (c) 2024 josibake *
3+
* Distributed under the MIT software license, see the accompanying *
4+
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5+
***********************************************************************/
6+
7+
#ifndef SECP256K1_MODULE_SILENTPAYMENTS_BENCH_H
8+
#define SECP256K1_MODULE_SILENTPAYMENTS_BENCH_H
9+
10+
#include "../../../include/secp256k1_silentpayments.h"
11+
12+
typedef struct {
13+
secp256k1_context *ctx;
14+
secp256k1_pubkey spend_pubkey;
15+
unsigned char scan_key[32];
16+
secp256k1_silentpayments_recipient_public_data public_data;
17+
secp256k1_xonly_pubkey tx_outputs[4];
18+
secp256k1_silentpayments_found_output found_outputs[4];
19+
unsigned char scalar[32];
20+
} bench_silentpayments_data;
21+
22+
/* we need a non-null pointer for the cache */
23+
static int noop;
24+
void* label_cache = &noop;
25+
const unsigned char* label_lookup(const unsigned char* key, const void* cache_ptr) {
26+
(void)key;
27+
(void)cache_ptr;
28+
return NULL;
29+
}
30+
31+
static void bench_silentpayments_scan_setup(void* arg) {
32+
int i;
33+
bench_silentpayments_data *data = (bench_silentpayments_data*)arg;
34+
const unsigned char tx_outputs[4][32] = {
35+
{0x84,0x17,0x92,0xc3,0x3c,0x9d,0xc6,0x19,0x3e,0x76,0x74,0x41,0x34,0x12,0x5d,0x40,0xad,0xd8,0xf2,0xf4,0xa9,0x64,0x75,0xf2,0x8b,0xa1,0x50,0xbe,0x03,0x2d,0x64,0xe8},
36+
{0x2e,0x84,0x7b,0xb0,0x1d,0x1b,0x49,0x1d,0xa5,0x12,0xdd,0xd7,0x60,0xb8,0x50,0x96,0x17,0xee,0x38,0x05,0x70,0x03,0xd6,0x11,0x5d,0x00,0xba,0x56,0x24,0x51,0x32,0x3a},
37+
{0xf2,0x07,0x16,0x2b,0x1a,0x7a,0xbc,0x51,0xc4,0x20,0x17,0xbe,0xf0,0x55,0xe9,0xec,0x1e,0xfc,0x3d,0x35,0x67,0xcb,0x72,0x03,0x57,0xe2,0xb8,0x43,0x25,0xdb,0x33,0xac},
38+
{0xe9,0x76,0xa5,0x8f,0xbd,0x38,0xae,0xb4,0xe6,0x09,0x3d,0x4d,0xf0,0x2e,0x9c,0x1d,0xe0,0xc4,0x51,0x3a,0xe0,0xc5,0x88,0xce,0xf6,0x8c,0xda,0x5b,0x2f,0x88,0x34,0xca}
39+
};
40+
const unsigned char spend_pubkey[33] = {
41+
0x02,0xee,0x97,0xdf,0x83,0xb2,0x54,0x6a,
42+
0xf5,0xa7,0xd0,0x62,0x15,0xd9,0x8b,0xcb,
43+
0x63,0x7f,0xe0,0x5d,0xd0,0xfa,0x37,0x3b,
44+
0xd8,0x20,0xe6,0x64,0xd3,0x72,0xde,0x9a,0x01
45+
};
46+
const unsigned char scan_key[32] = {
47+
0xa8,0x90,0x54,0xc9,0x5b,0xe3,0xc3,0x01,
48+
0x56,0x65,0x74,0xf2,0xaa,0x93,0xad,0xe0,
49+
0x51,0x85,0x09,0x03,0xa6,0x9c,0xbd,0xd1,
50+
0xd4,0x7e,0xae,0x26,0x3d,0x7b,0xc0,0x31
51+
};
52+
secp256k1_keypair input_keypair;
53+
secp256k1_pubkey input_pubkey;
54+
unsigned char input_pubkey33[33];
55+
size_t pubkeylen = 33;
56+
57+
for (i = 0; i < 32; i++) {
58+
data->scalar[i] = i + 1;
59+
}
60+
for (i = 0; i < 4; i++) {
61+
CHECK(secp256k1_xonly_pubkey_parse(data->ctx, &data->tx_outputs[i], tx_outputs[i]));
62+
}
63+
CHECK(secp256k1_keypair_create(data->ctx, &input_keypair, data->scalar));
64+
CHECK(secp256k1_keypair_pub(data->ctx, &input_pubkey, &input_keypair));
65+
CHECK(secp256k1_ec_pubkey_serialize(data->ctx, input_pubkey33, &pubkeylen, &input_pubkey, SECP256K1_EC_COMPRESSED));
66+
CHECK(secp256k1_silentpayments_recipient_public_data_parse(data->ctx, &data->public_data, input_pubkey33));
67+
CHECK(secp256k1_ec_pubkey_parse(data->ctx, &data->spend_pubkey, spend_pubkey, pubkeylen));
68+
memcpy(data->scan_key, scan_key, 32);
69+
}
70+
71+
static void bench_silentpayments_output_scan(void* arg, int iters) {
72+
int i, k = 0;
73+
bench_silentpayments_data *data = (bench_silentpayments_data*)arg;
74+
75+
for (i = 0; i < iters; i++) {
76+
unsigned char shared_secret[33];
77+
secp256k1_xonly_pubkey xonly_output;
78+
CHECK(secp256k1_silentpayments_recipient_create_shared_secret(data->ctx,
79+
shared_secret,
80+
data->scan_key,
81+
&data->public_data
82+
));
83+
CHECK(secp256k1_silentpayments_recipient_create_output_pubkey(data->ctx,
84+
&xonly_output,
85+
shared_secret,
86+
&data->spend_pubkey,
87+
k
88+
));
89+
}
90+
}
91+
92+
static void bench_silentpayments_full_tx_scan(void* arg, int iters) {
93+
int i;
94+
size_t n_found = 0;
95+
secp256k1_silentpayments_found_output *found_output_ptrs[4];
96+
const secp256k1_xonly_pubkey *tx_output_ptrs[4];
97+
bench_silentpayments_data *data = (bench_silentpayments_data*)arg;
98+
99+
for (i = 0; i < 4; i++) {
100+
found_output_ptrs[i] = &data->found_outputs[i];
101+
tx_output_ptrs[i] = &data->tx_outputs[i];
102+
}
103+
for (i = 0; i < iters; i++) {
104+
CHECK(secp256k1_silentpayments_recipient_scan_outputs(data->ctx,
105+
found_output_ptrs, &n_found,
106+
tx_output_ptrs, 4,
107+
data->scan_key,
108+
&data->public_data,
109+
&data->spend_pubkey,
110+
label_lookup, label_cache)
111+
);
112+
}
113+
}
114+
115+
static void run_silentpayments_bench(int iters, int argc, char** argv) {
116+
bench_silentpayments_data data;
117+
int d = argc == 1;
118+
119+
/* create a context with no capabilities */
120+
data.ctx = secp256k1_context_create(SECP256K1_FLAGS_TYPE_CONTEXT);
121+
122+
if (d || have_flag(argc, argv, "silentpayments")) run_benchmark("silentpayments_full_tx_scan", bench_silentpayments_full_tx_scan, bench_silentpayments_scan_setup, NULL, &data, 10, iters);
123+
if (d || have_flag(argc, argv, "silentpayments")) run_benchmark("silentpayments_output_scan", bench_silentpayments_output_scan, bench_silentpayments_scan_setup, NULL, &data, 10, iters);
124+
125+
secp256k1_context_destroy(data.ctx);
126+
}
127+
128+
#endif /* SECP256K1_MODULE_SILENTPAYMENTS_BENCH_H */

0 commit comments

Comments
 (0)