Skip to content

Commit 6975614

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 a9af9eb commit 6975614

File tree

3 files changed

+184
-1
lines changed

3 files changed

+184
-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: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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) {
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+
120+
for (i = 0; i < 2; i++) {
121+
found_output_ptrs[i] = &data->found_outputs[i];
122+
tx_output_ptrs[i] = &data->tx_outputs[i];
123+
tx_input_ptrs[i] = &data->tx_inputs[i];
124+
}
125+
for (i = 0; i < iters; i++) {
126+
CHECK(secp256k1_silentpayments_recipient_public_data_create(data->ctx,
127+
&public_data,
128+
data->smallest_outpoint,
129+
tx_input_ptrs, 2,
130+
NULL, 0
131+
));
132+
CHECK(secp256k1_silentpayments_recipient_scan_outputs(data->ctx,
133+
found_output_ptrs, &n_found,
134+
tx_output_ptrs, 2,
135+
data->scan_key,
136+
&public_data,
137+
&data->spend_pubkey,
138+
label_lookup, label_cache)
139+
);
140+
}
141+
}
142+
143+
static void run_silentpayments_bench(int iters, int argc, char** argv) {
144+
bench_silentpayments_data data;
145+
int d = argc == 1;
146+
147+
/* create a context with no capabilities */
148+
data.ctx = secp256k1_context_create(SECP256K1_FLAGS_TYPE_CONTEXT);
149+
150+
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);
151+
if (d || have_flag(argc, argv, "silentpayments")) run_benchmark("silentpayments_output_scan", bench_silentpayments_output_scan, bench_silentpayments_scan_setup, NULL, &data, 10, iters);
152+
153+
secp256k1_context_destroy(data.ctx);
154+
}
155+
156+
#endif /* SECP256K1_MODULE_SILENTPAYMENTS_BENCH_H */

0 commit comments

Comments
 (0)