Skip to content

Commit f6106ff

Browse files
josibaketheStack
andcommitted
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. Co-authored-by: Sebastian Falbesoner <[email protected]>
1 parent 371165a commit f6106ff

File tree

4 files changed

+222
-19
lines changed

4 files changed

+222
-19
lines changed

src/bench.c

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,40 +32,51 @@ 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");
3842
printf("\n");
3943
printf("Usage: ./bench [args]\n");
4044
printf("By default, all benchmarks will be run.\n");
4145
printf("args:\n");
42-
printf(" help : display this help and exit\n");
43-
printf(" ecdsa : all ECDSA algorithms--sign, verify, recovery (if enabled)\n");
44-
printf(" ecdsa_sign : ECDSA siging algorithm\n");
45-
printf(" ecdsa_verify : ECDSA verification algorithm\n");
46-
printf(" ec : all EC public key algorithms (keygen)\n");
47-
printf(" ec_keygen : EC public key generation\n");
46+
printf(" help : display this help and exit\n");
47+
printf(" ecdsa : all ECDSA algorithms--sign, verify, recovery (if enabled)\n");
48+
printf(" ecdsa_sign : ECDSA siging algorithm\n");
49+
printf(" ecdsa_verify : ECDSA verification algorithm\n");
50+
printf(" ec : all EC public key algorithms (keygen)\n");
51+
printf(" ec_keygen : EC public key generation\n");
4852

4953
#ifdef ENABLE_MODULE_RECOVERY
50-
printf(" ecdsa_recover : ECDSA public key recovery algorithm\n");
54+
printf(" ecdsa_recover : ECDSA public key recovery algorithm\n");
5155
#endif
5256

5357
#ifdef ENABLE_MODULE_ECDH
54-
printf(" ecdh : ECDH key exchange algorithm\n");
58+
printf(" ecdh : ECDH key exchange algorithm\n");
5559
#endif
5660

5761
#ifdef ENABLE_MODULE_SCHNORRSIG
58-
printf(" schnorrsig : all Schnorr signature algorithms (sign, verify)\n");
59-
printf(" schnorrsig_sign : Schnorr sigining algorithm\n");
60-
printf(" schnorrsig_verify : Schnorr verification algorithm\n");
62+
printf(" schnorrsig : all Schnorr signature algorithms (sign, verify)\n");
63+
printf(" schnorrsig_sign : Schnorr sigining algorithm\n");
64+
printf(" schnorrsig_verify : Schnorr verification algorithm\n");
6165
#endif
6266

6367
#ifdef ENABLE_MODULE_ELLSWIFT
64-
printf(" ellswift : all ElligatorSwift benchmarks (encode, decode, keygen, ecdh)\n");
65-
printf(" ellswift_encode : ElligatorSwift encoding\n");
66-
printf(" ellswift_decode : ElligatorSwift decoding\n");
67-
printf(" ellswift_keygen : ElligatorSwift key generation\n");
68-
printf(" ellswift_ecdh : ECDH on ElligatorSwift keys\n");
68+
printf(" ellswift : all ElligatorSwift benchmarks (encode, decode, keygen, ecdh)\n");
69+
printf(" ellswift_encode : ElligatorSwift encoding\n");
70+
printf(" ellswift_decode : ElligatorSwift decoding\n");
71+
printf(" ellswift_keygen : ElligatorSwift key generation\n");
72+
printf(" ellswift_ecdh : ECDH on ElligatorSwift keys\n");
73+
#endif
74+
75+
#ifdef ENABLE_MODULE_SILENTPAYMENTS
76+
printf(" silentpayments : all Silent payments benchmarks (full_scan, full_scan_with_labels, output_scan)\n");
77+
printf(" silentpayments_full_scan : Silent payments full transaction scanning\n");
78+
printf(" silentpayments_full_scan_with_labels : Silent payments full transaction scanning with labels\n");
79+
printf(" silentpayments_output_scan : Silent payments scan on a single output (e.g., light client)\n");
6980
#endif
7081

7182
printf("\n");
@@ -170,6 +181,10 @@ static void bench_keygen_run(void *arg, int iters) {
170181
# include "modules/ellswift/bench_impl.h"
171182
#endif
172183

184+
#ifdef ENABLE_MODULE_SILENTPAYMENTS
185+
# include "modules/silentpayments/bench_impl.h"
186+
#endif
187+
173188
int main(int argc, char** argv) {
174189
int i;
175190
secp256k1_pubkey pubkey;
@@ -184,7 +199,8 @@ int main(int argc, char** argv) {
184199
char* valid_args[] = {"ecdsa", "verify", "ecdsa_verify", "sign", "ecdsa_sign", "ecdh", "recover",
185200
"ecdsa_recover", "schnorrsig", "schnorrsig_verify", "schnorrsig_sign", "ec",
186201
"keygen", "ec_keygen", "ellswift", "encode", "ellswift_encode", "decode",
187-
"ellswift_decode", "ellswift_keygen", "ellswift_ecdh"};
202+
"ellswift_decode", "ellswift_keygen", "ellswift_ecdh", "silentpayments",
203+
"silentpayments_output_scan", "silentpayments_full_scan", "silentpayments_full_scan_with_labels"};
188204
size_t valid_args_size = sizeof(valid_args)/sizeof(valid_args[0]);
189205
int invalid_args = have_invalid_args(argc, argv, valid_args, valid_args_size);
190206

@@ -236,6 +252,15 @@ int main(int argc, char** argv) {
236252
}
237253
#endif
238254

255+
#ifndef ENABLE_MODULE_SILENTPAYMENTS
256+
if (have_flag(argc, argv, "silentpayments") || have_flag(argc, argv, "silentpayments_full_scan") ||
257+
have_flag(argc, argv, "silentpayments_full_scan_with_labels") || have_flag(argc, argv, "silentpayments_output_scan")) {
258+
fprintf(stderr, "./bench: silentpayments module not enabled.\n");
259+
fprintf(stderr, "Use ./configure --enable-module-silentpayments.\n\n");
260+
return 1;
261+
}
262+
#endif
263+
239264
/* ECDSA benchmark */
240265
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
241266

@@ -280,5 +305,11 @@ int main(int argc, char** argv) {
280305
run_ellswift_bench(iters, argc, argv);
281306
#endif
282307

308+
#ifdef ENABLE_MODULE_SILENTPAYMENTS
309+
/* SilentPayments benchmarks */
310+
run_silentpayments_bench(iters, argc, argv);
311+
#endif
312+
313+
283314
return EXIT_SUCCESS;
284315
}

src/bench.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static void run_benchmark(char *name, void (*benchmark)(void*, int), void (*setu
120120
sum += total;
121121
}
122122
/* ',' is used as a column delimiter */
123-
printf("%-30s, ", name);
123+
printf("%-40s, ", name);
124124
print_number(min * FP_MULT / iter);
125125
printf(" , ");
126126
print_number(((sum * FP_MULT) / count) / iter);
@@ -181,7 +181,7 @@ static void print_output_table_header_row(void) {
181181
char* min_str = " Min(us) "; /* center alignment */
182182
char* avg_str = " Avg(us) ";
183183
char* max_str = " Max(us) ";
184-
printf("%-30s,%-15s,%-15s,%-15s\n", bench_str, min_str, avg_str, max_str);
184+
printf("%-40s,%-15s,%-15s,%-15s\n", bench_str, min_str, avg_str, max_str);
185185
printf("\n");
186186
}
187187

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: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
unsigned char input_pubkey33[33];
17+
secp256k1_xonly_pubkey tx_outputs[2];
18+
secp256k1_xonly_pubkey tx_inputs[2];
19+
secp256k1_silentpayments_found_output found_outputs[2];
20+
unsigned char scalar[32];
21+
unsigned char smallest_outpoint[36];
22+
} bench_silentpayments_data;
23+
24+
/* we need a non-null pointer for the cache */
25+
static int noop;
26+
void* label_cache = &noop;
27+
const unsigned char* label_lookup(const unsigned char* key, const void* cache_ptr) {
28+
(void)key;
29+
(void)cache_ptr;
30+
return NULL;
31+
}
32+
33+
static void bench_silentpayments_scan_setup(void* arg) {
34+
int i;
35+
bench_silentpayments_data *data = (bench_silentpayments_data*)arg;
36+
const unsigned char tx_outputs[2][32] = {
37+
{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},
38+
{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},
39+
};
40+
const unsigned char static_tx_input[32] = {
41+
0xf2,0x07,0x16,0x2b,0x1a,0x7a,0xbc,0x51,
42+
0xc4,0x20,0x17,0xbe,0xf0,0x55,0xe9,0xec,
43+
0x1e,0xfc,0x3d,0x35,0x67,0xcb,0x72,0x03,
44+
0x57,0xe2,0xb8,0x43,0x25,0xdb,0x33,0xac
45+
};
46+
const unsigned char smallest_outpoint[36] = {
47+
0x16, 0x9e, 0x1e, 0x83, 0xe9, 0x30, 0x85, 0x33, 0x91,
48+
0xbc, 0x6f, 0x35, 0xf6, 0x05, 0xc6, 0x75, 0x4c, 0xfe,
49+
0xad, 0x57, 0xcf, 0x83, 0x87, 0x63, 0x9d, 0x3b, 0x40,
50+
0x96, 0xc5, 0x4f, 0x18, 0xf4, 0x00, 0x00, 0x00, 0x00,
51+
};
52+
const unsigned char spend_pubkey[33] = {
53+
0x02,0xee,0x97,0xdf,0x83,0xb2,0x54,0x6a,
54+
0xf5,0xa7,0xd0,0x62,0x15,0xd9,0x8b,0xcb,
55+
0x63,0x7f,0xe0,0x5d,0xd0,0xfa,0x37,0x3b,
56+
0xd8,0x20,0xe6,0x64,0xd3,0x72,0xde,0x9a,0x01
57+
};
58+
const unsigned char scan_key[32] = {
59+
0xa8,0x90,0x54,0xc9,0x5b,0xe3,0xc3,0x01,
60+
0x56,0x65,0x74,0xf2,0xaa,0x93,0xad,0xe0,
61+
0x51,0x85,0x09,0x03,0xa6,0x9c,0xbd,0xd1,
62+
0xd4,0x7e,0xae,0x26,0x3d,0x7b,0xc0,0x31
63+
};
64+
secp256k1_keypair input_keypair;
65+
secp256k1_pubkey input_pubkey;
66+
size_t pubkeylen = 33;
67+
68+
for (i = 0; i < 32; i++) {
69+
data->scalar[i] = i + 1;
70+
}
71+
for (i = 0; i < 2; i++) {
72+
CHECK(secp256k1_xonly_pubkey_parse(data->ctx, &data->tx_outputs[i], tx_outputs[i]));
73+
}
74+
/* Create the first input public key from the scalar.
75+
* This input is also used to create the serialized public data object for the light client
76+
*/
77+
CHECK(secp256k1_keypair_create(data->ctx, &input_keypair, data->scalar));
78+
CHECK(secp256k1_keypair_pub(data->ctx, &input_pubkey, &input_keypair));
79+
CHECK(secp256k1_ec_pubkey_serialize(data->ctx, data->input_pubkey33, &pubkeylen, &input_pubkey, SECP256K1_EC_COMPRESSED));
80+
/* Create the input public keys for the full scan */
81+
CHECK(secp256k1_keypair_xonly_pub(data->ctx, &data->tx_inputs[0], NULL, &input_keypair));
82+
CHECK(secp256k1_xonly_pubkey_parse(data->ctx, &data->tx_inputs[1], static_tx_input));
83+
CHECK(secp256k1_ec_pubkey_parse(data->ctx, &data->spend_pubkey, spend_pubkey, pubkeylen));
84+
memcpy(data->scan_key, scan_key, 32);
85+
memcpy(data->smallest_outpoint, smallest_outpoint, 36);
86+
}
87+
88+
static void bench_silentpayments_output_scan(void* arg, int iters) {
89+
int i, k = 0;
90+
bench_silentpayments_data *data = (bench_silentpayments_data*)arg;
91+
secp256k1_silentpayments_recipient_public_data public_data;
92+
93+
for (i = 0; i < iters; i++) {
94+
unsigned char shared_secret[33];
95+
secp256k1_xonly_pubkey xonly_output;
96+
CHECK(secp256k1_silentpayments_recipient_public_data_parse(data->ctx, &public_data, data->input_pubkey33));
97+
CHECK(secp256k1_silentpayments_recipient_create_shared_secret(data->ctx,
98+
shared_secret,
99+
data->scan_key,
100+
&public_data
101+
));
102+
CHECK(secp256k1_silentpayments_recipient_create_output_pubkey(data->ctx,
103+
&xonly_output,
104+
shared_secret,
105+
&data->spend_pubkey,
106+
k
107+
));
108+
}
109+
}
110+
111+
static void bench_silentpayments_full_tx_scan(void* arg, int iters, int use_labels) {
112+
int i;
113+
size_t n_found = 0;
114+
secp256k1_silentpayments_found_output *found_output_ptrs[2];
115+
const secp256k1_xonly_pubkey *tx_output_ptrs[2];
116+
const secp256k1_xonly_pubkey *tx_input_ptrs[2];
117+
bench_silentpayments_data *data = (bench_silentpayments_data*)arg;
118+
secp256k1_silentpayments_recipient_public_data public_data;
119+
const secp256k1_silentpayments_label_lookup label_lookup_fn = use_labels ? label_lookup : NULL;
120+
const void *label_context = use_labels ? label_cache : NULL;
121+
122+
for (i = 0; i < 2; i++) {
123+
found_output_ptrs[i] = &data->found_outputs[i];
124+
tx_output_ptrs[i] = &data->tx_outputs[i];
125+
tx_input_ptrs[i] = &data->tx_inputs[i];
126+
}
127+
for (i = 0; i < iters; i++) {
128+
CHECK(secp256k1_silentpayments_recipient_public_data_create(data->ctx,
129+
&public_data,
130+
data->smallest_outpoint,
131+
tx_input_ptrs, 2,
132+
NULL, 0
133+
));
134+
CHECK(secp256k1_silentpayments_recipient_scan_outputs(data->ctx,
135+
found_output_ptrs, &n_found,
136+
tx_output_ptrs, 2,
137+
data->scan_key,
138+
&public_data,
139+
&data->spend_pubkey,
140+
label_lookup_fn, label_context)
141+
);
142+
}
143+
}
144+
145+
static void bench_silentpayments_full_scan(void *arg, int iters) {
146+
bench_silentpayments_full_tx_scan(arg, iters, 0);
147+
}
148+
149+
/* TODO: currently, the with_labels benchmark ensures all of the labels code paths
150+
* are hit during scanning, but should be extended to measure scanning for labels
151+
* with a sizable labels cache.
152+
*/
153+
static void bench_silentpayments_full_scan_with_labels(void *arg, int iters) {
154+
bench_silentpayments_full_tx_scan(arg, iters, 1);
155+
}
156+
157+
static void run_silentpayments_bench(int iters, int argc, char** argv) {
158+
bench_silentpayments_data data;
159+
int d = argc == 1;
160+
161+
/* create a context with no capabilities */
162+
data.ctx = secp256k1_context_create(SECP256K1_FLAGS_TYPE_CONTEXT);
163+
164+
if (d || have_flag(argc, argv, "silentpayments") || have_flag(argc, argv, "silentpayments_output_scan")) run_benchmark("silentpayments_output_scan", bench_silentpayments_output_scan, bench_silentpayments_scan_setup, NULL, &data, 10, iters);
165+
if (d || have_flag(argc, argv, "silentpayments") || have_flag(argc, argv, "silentpayments_full_scan")) run_benchmark("silentpayments_full_scan", bench_silentpayments_full_scan, bench_silentpayments_scan_setup, NULL, &data, 10, iters);
166+
if (d || have_flag(argc, argv, "silentpayments") || have_flag(argc, argv, "silentpayments_full_scan_with_labels")) run_benchmark("silentpayments_full_scan_with_labels", bench_silentpayments_full_scan_with_labels, bench_silentpayments_scan_setup, NULL, &data, 10, iters);
167+
168+
secp256k1_context_destroy(data.ctx);
169+
}
170+
171+
#endif /* SECP256K1_MODULE_SILENTPAYMENTS_BENCH_H */

0 commit comments

Comments
 (0)