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