Botan  2.8.0
Crypto and TLS for C++11
xmss_tools.cpp
Go to the documentation of this file.
1 /*
2  * XMSS Tools
3  * (C) 2017 Matthias Gierlings
4  *
5  * Botan is released under the Simplified BSD License (see license.txt)
6  **/
7 
8 #include <botan/xmss_tools.h>
9 
10 namespace Botan {
11 
12 #if defined(BOTAN_TARGET_OS_HAS_THREADS)
13 
15  {
16  static const size_t threads { bench_threads() };
17  return threads;
18  }
19 
20 size_t XMSS_Tools::bench_threads()
21  {
22  const size_t hardware_concurrency = std::thread::hardware_concurrency();
23 
24  if(hardware_concurrency <= 1)
25  {
26  return 1;
27  }
28  const size_t BENCH_ITERATIONS = 1000;
29  std::vector<std::thread> threads;
30  threads.reserve(hardware_concurrency);
31  std::vector<std::chrono::nanoseconds> durations;
32 
33  std::vector<size_t> concurrency { hardware_concurrency,
34  hardware_concurrency / 2 };
35 
36  for(const auto& cc : concurrency)
37  {
38  std::vector<XMSS_Hash> hash(hardware_concurrency,
39  XMSS_Hash("SHA-256"));
40 
41  const std::vector<uint8_t> buffer(hash[0].output_length());
42  std::vector<secure_vector<uint8_t>> data(
43  hardware_concurrency,
44  secure_vector<uint8_t>(hash[0].output_length()));
45  auto start = std::chrono::high_resolution_clock::now();
46  for(size_t i = 0; i < cc; ++i)
47  {
48  auto& hs = hash[i];
49  auto& d = data[i];
50 
51  const size_t n_iters = BENCH_ITERATIONS * (hardware_concurrency / cc);
52  threads.emplace_back(std::thread([n_iters, &hs, &d]()
53  {
54  for(size_t n = 0; n < n_iters; n++)
55  {
56  hs.h(d, d, d);
57  }
58  }
59  ));
60  }
61  durations.emplace_back(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - start));
62  for(auto& t : threads)
63  {
64  t.join();
65  }
66  threads.clear();
67  }
68 
69  if(durations[0].count() < durations[1].count())
70  {
71 #if defined(BOTAN_TEST_MODE)
72  return 4;
73 #else
74  return concurrency[0];
75 #endif
76  }
77  else
78  {
79 #if defined(BOTAN_TEST_MODE)
80  return 4;
81 #else
82  return concurrency[1];
83 #endif
84  }
85  }
86 
87 #endif
88 
89 }
90 
static size_t max_threads()
Definition: alg_id.cpp:13
MechanismType hash