1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
// Copyright (c) 2020-2023, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "tests/benchmark.h"
#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/fusion/algorithm/iteration/fold.hpp>
#include <boost/preprocessor/seq/enum.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/seq/seq.hpp>
#include <boost/preprocessor/seq.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <boost/spirit/include/karma_char.hpp>
#include <boost/spirit/include/karma_format.hpp>
#include <boost/spirit/include/karma_repeat.hpp>
#include <boost/spirit/include/karma_right_alignment.hpp>
#include <boost/spirit/include/karma_sequence.hpp>
#include <boost/spirit/include/karma_string.hpp>
#include <boost/spirit/include/karma_uint.hpp>
#include <boost/spirit/include/qi_char.hpp>
#include <boost/spirit/include/qi_list.hpp>
#include <boost/spirit/include/qi_parse.hpp>
#include <boost/spirit/include/qi_uint.hpp>
#include <chrono>
#include <cstring>
#include <functional>
#include <iostream>
#include <stdexcept>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
#include "crypto/crypto.h"
#include "cryptonote_basic/cryptonote_basic.h"
#include "monero/crypto/amd64-64-24k.h"
#include "monero/crypto/amd64-51-30k.h"
#define CHECK(...) \
if(!( __VA_ARGS__ )) \
throw std::runtime_error{ \
"TEST FAILED (line " \
BOOST_PP_STRINGIZE( __LINE__ ) \
"): " \
BOOST_PP_STRINGIZE( __VA_ARGS__ ) \
}
//! Define function that forwards arguments to `crypto::func`.
#define FORWARD_FUNCTION(func) \
template<typename... T> \
static bool func (T&&... args) \
{ \
return ::crypto:: func (std::forward<T>(args)...); \
}
#define CRYPTO_FUNCTION(library, func) \
BOOST_PP_CAT(BOOST_PP_CAT(monero_crypto_, library), func)
#define CRYPTO_BENCHMARK(r, _, library) \
struct library \
{ \
static constexpr const char* name() noexcept { return BOOST_PP_STRINGIZE(library); } \
static bool generate_key_derivation(const ::crypto::public_key &tx_pub, const ::crypto::secret_key &view_sec, ::crypto::key_derivation &out) \
{ \
return CRYPTO_FUNCTION(library, _generate_key_derivation) (out.data, tx_pub.data, view_sec.data) == 0; \
} \
static bool derive_subaddress_public_key(const ::crypto::public_key &spend_pub, const ::crypto::key_derivation &d, std::size_t index, ::crypto::public_key &out) \
{ \
::crypto::ec_scalar scalar; \
::crypto::derivation_to_scalar(d, index, scalar); \
return CRYPTO_FUNCTION(library, _generate_subaddress_public_key) (out.data, spend_pub.data, scalar.data) == 0; \
} \
};
namespace
{
//! Default number of iterations for benchmark timing.
constexpr const unsigned default_iterations = 1000;
//! \return Byte compare two objects of `T`.
template<typename T>
bool compare(const T& lhs, const T& rhs) noexcept
{
static_assert(!epee::has_padding<T>(), "type might have padding");
return std::memcmp(std::addressof(lhs), std::addressof(rhs), sizeof(T)) == 0;
}
//! Benchmark default monero crypto library - a re-arranged ref10 implementation.
struct cn
{
static constexpr const char* name() noexcept { return "cn"; }
FORWARD_FUNCTION( generate_key_derivation );
FORWARD_FUNCTION( derive_subaddress_public_key );
};
// Define functions for every library except for `cn` which is the head library.
BOOST_PP_SEQ_FOR_EACH(CRYPTO_BENCHMARK, _, BOOST_PP_SEQ_TAIL(BENCHMARK_LIBRARIES));
// All enabled benchmark libraries
using enabled_libraries = std::tuple<BOOST_PP_SEQ_ENUM(BENCHMARK_LIBRARIES)>;
//! Callable that runs a benchmark against all enabled libraries
template<typename R>
struct run_benchmark
{
using result = R;
template<typename B>
result operator()(result out, const B benchmark) const
{
using inner_result = typename B::result;
out.push_back({boost::fusion::fold(enabled_libraries{}, inner_result{}, benchmark), benchmark.name()});
std::sort(out.back().first.begin(), out.back().first.end());
return out;
}
};
//! Run 0+ benchmarks against all enabled libraries
template<typename R, typename... B>
R run_benchmarks(B&&... benchmarks)
{
auto out = boost::fusion::fold(std::make_tuple(std::forward<B>(benchmarks)...), R{}, run_benchmark<R>{});
std::sort(out.begin(), out.end());
return out;
}
//! Run a suite of benchmarks - allows for comparison against a subset of benchmarks
template<typename S>
std::pair<typename S::result, std::string> run_suite(const S& suite)
{
return {suite(), suite.name()};
}
//! Arguments given to every crypto library being benchmarked.
struct bench_args
{
explicit bench_args(unsigned iterations)
: iterations(iterations), one(), two()
{
crypto::generate_keys(one.pub, one.sec, one.sec, false);
crypto::generate_keys(two.pub, two.sec, two.sec, false);
}
const unsigned iterations;
cryptonote::keypair one;
cryptonote::keypair two;
};
/*! Tests the ECDH step used for monero txes where the tx-pub is always
de-compressed into a table every time. */
struct tx_pub_standard
{
using result = std::vector<std::pair<std::chrono::steady_clock::duration, std::string>>;
static constexpr const char* name() noexcept { return "standard"; }
const bench_args args;
template<typename L>
result operator()(result out, const L library) const
{
crypto::key_derivation us;
crypto::key_derivation them;
CHECK(crypto::generate_key_derivation(args.one.pub, args.two.sec, them));
CHECK(library.generate_key_derivation(args.one.pub, args.two.sec, us));
CHECK(compare(us, them));
unsigned i = 0;
for (unsigned j = 0; j < 100; ++j)
i += library.generate_key_derivation(args.one.pub, args.two.sec, us);
CHECK(i == 100);
i = 0;
const auto start = std::chrono::steady_clock::now();
for (unsigned j = 0; j < args.iterations; ++j)
i += library.generate_key_derivation(args.one.pub, args.two.sec, us);
const auto end = std::chrono::steady_clock::now();
CHECK(i == args.iterations);
CHECK(compare(us, them));
out.push_back({end - start, library.name()});
return out;
}
};
//! Tests various possible optimizations for tx ECDH-step.
struct tx_pub_suite
{
using result = std::vector<std::pair<tx_pub_standard::result, std::string>>;
static constexpr const char* name() noexcept { return "generate_key_derivation step"; }
const bench_args args;
result operator()() const
{
return run_benchmarks<result>(tx_pub_standard{args});
}
};
/*! Tests the shared-secret to output-key step used for monero txes where
the users spend-public is always de-compressed. */
struct output_pub_standard
{
using result = std::vector<std::pair<std::chrono::steady_clock::duration, std::string>>;
static constexpr const char* name() noexcept { return "standard"; }
const bench_args args;
template<typename L>
result operator()(result out, const L library) const
{
crypto::key_derivation derived;
crypto::public_key us;
crypto::public_key them;
CHECK(crypto::generate_key_derivation(args.one.pub, args.two.sec, derived));
CHECK(library.derive_subaddress_public_key(args.two.pub, derived, 0, us));
CHECK(crypto::derive_subaddress_public_key(args.two.pub, derived, 0, them));
CHECK(compare(us, them));
unsigned i = 0;
for (unsigned j = 0; j < 100; ++j)
i += library.derive_subaddress_public_key(args.two.pub, derived, j, us);
CHECK(i == 100);
i = 0;
const auto start = std::chrono::steady_clock::now();
for (unsigned j = 0; j < args.iterations; ++j)
i += library.derive_subaddress_public_key(args.two.pub, derived, j, us);
const auto end = std::chrono::steady_clock::now();
CHECK(i == args.iterations);
out.push_back({end - start, library.name()});
return out;
}
};
//! Tests various possible optimizations for shared-secret to output-key step.
struct output_pub_suite
{
using result = std::vector<std::pair<output_pub_standard::result, std::string>>;
static constexpr const char* name() noexcept { return "derive_subaddress_public_key step"; }
const bench_args args;
result operator()() const
{
return run_benchmarks<result>(output_pub_standard{args});
}
};
struct tx_bench_args
{
const bench_args main;
unsigned outputs;
};
/*! Simulates "standard" tx scanning where a tx-pubkey is de-compressed into
a table and user spend-public is de-compressed, every time. */
struct tx_standard
{
using result = std::vector<std::pair<std::chrono::steady_clock::duration, std::string>>;
static constexpr const char* name() noexcept { return "standard"; }
const tx_bench_args args;
template<typename L>
result operator()(result out, const L library) const
{
crypto::key_derivation derived_us;
crypto::key_derivation derived_them;
crypto::public_key us;
crypto::public_key them;
CHECK(library.generate_key_derivation(args.main.one.pub, args.main.two.sec, derived_us));
CHECK(crypto::generate_key_derivation(args.main.one.pub, args.main.two.sec, derived_them));
CHECK(library.derive_subaddress_public_key(args.main.two.pub, derived_us, 0, us));
CHECK(crypto::derive_subaddress_public_key(args.main.two.pub, derived_them, 0, them));
CHECK(compare(us, them));
unsigned i = 0;
for (unsigned j = 0; j < 100; ++j)
{
i += library.generate_key_derivation(args.main.one.pub, args.main.two.sec, derived_us);
i += library.derive_subaddress_public_key(args.main.two.pub, derived_us, j, us);
}
CHECK(i == 200);
i = 0;
const auto start = std::chrono::steady_clock::now();
for (unsigned j = 0; j < args.main.iterations; ++j)
{
i += library.generate_key_derivation(args.main.one.pub, args.main.two.sec, derived_us);
for (unsigned k = 0; k < args.outputs; ++k)
i += library.derive_subaddress_public_key(args.main.two.pub, derived_us, k, us);
}
const auto end = std::chrono::steady_clock::now();
CHECK(i == args.main.iterations + args.main.iterations * args.outputs);
out.push_back({end - start, library.name()});
return out;
}
};
//! Tests various possible optimizations for tx scanning.
struct tx_suite
{
using result = std::vector<std::pair<output_pub_standard::result, std::string>>;
std::string name() const { return "Transactions with " + std::to_string(args.outputs) + " outputs"; }
const tx_bench_args args;
result operator()() const
{
return run_benchmarks<result>(tx_standard{args});
}
};
std::chrono::steady_clock::duration print(const tx_pub_standard::result& leaf, std::ostream& out, unsigned depth)
{
namespace karma = boost::spirit::karma;
const std::size_t align = leaf.empty() ?
0 : std::to_string(leaf.back().first.count()).size();
const auto best = leaf.empty() ?
std::chrono::steady_clock::duration::max() : leaf.front().first;
for (auto const& entry : leaf)
{
out << karma::format(karma::repeat(depth ? depth - 1 : 0)["| "]) << '|';
out << karma::format((karma::right_align(std::min(20u - depth, 20u), '-')["> " << karma::string]), entry.second);
out << " => " << karma::format((karma::right_align(align)[karma::uint_]), entry.first.count());
out << " ns (+";
out << (double((entry.first - best).count()) / best.count()) * 100 << "%)" << std::endl;
}
out << karma::format(karma::repeat(depth ? depth - 1 : 0)["| "]) << std::endl;
return best;
}
template<typename T>
std::chrono::steady_clock::duration
print(const std::vector<std::pair<T, std::string>>& node, std::ostream& out, unsigned depth)
{
auto best = std::chrono::steady_clock::duration::max();
for (auto const& entry : node)
{
std::stringstream buffer{};
auto last = print(entry.first, buffer, depth + 1);
if (last != std::chrono::steady_clock::duration::max())
{
namespace karma = boost::spirit::karma;
best = std::min(best, last);
out << karma::format(karma::repeat(depth)["|-"]);
out << "+ " << entry.second << ' ';
out << last.count() << " ns (+";
out << (double((last - best).count()) / best.count()) * 100 << "%)" << std::endl;
out << buffer.str();
}
}
return best;
}
} // anonymous namespace
int main(int argc, char** argv)
{
using results = std::vector<std::pair<tx_pub_suite::result, std::string>>;
try
{
unsigned iterations = default_iterations;
std::vector<unsigned> nums{};
if (2 <= argc) iterations = std::stoul(argv[1]);
if (3 <= argc)
{
namespace qi = boost::spirit::qi;
if (!qi::parse(argv[2], argv[2] + strlen(argv[2]), (qi::uint_ % ','), nums))
throw std::runtime_error{"bad tx outputs string"};
}
else
{
nums = {2, 4};
}
std::sort(nums.begin(), nums.end());
nums.erase(std::unique(nums.begin(), nums.end()), nums.end());
std::cout << "Running benchmark using " << iterations << " iterations" << std::endl;
const bench_args args{iterations};
results val{};
std::cout << "Transaction Component Benchmarks" << std::endl;
std::cout << "--------------------------------" << std::endl;
val.push_back(run_suite(tx_pub_suite{args}));
val.push_back(run_suite(output_pub_suite{args}));
std::sort(val.begin(), val.end());
print(val, std::cout, 0);
val.clear();
std::cout << "Transaction Benchmarks" << std::endl;
std::cout << "----------------------" << std::endl;
for (const unsigned num : nums)
val.push_back(run_suite(tx_suite{{args, num}}));
std::sort(val.begin(), val.end());
print(val, std::cout, 0);
}
catch (const std::exception& e)
{
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}
|