• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 19068123965

04 Nov 2025 12:08PM UTC coverage: 90.686% (+0.007%) from 90.679%
19068123965

push

github

web-flow
Merge pull request #5076 from reneme/feature/ascon_aead128

Feature: Ascon-AEAD128

100606 of 110939 relevant lines covered (90.69%)

12536473.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.68
/src/cli/speed.cpp
1
/*
2
* (C) 2009,2010,2014,2015,2017,2018,2024 Jack Lloyd
3
* (C) 2015 Simon Warta (Kullo GmbH)
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "cli.h"
9
#include "perf.h"
10

11
#include <algorithm>
12
#include <chrono>
13
#include <iomanip>
14
#include <map>
15
#include <set>
16
#include <sstream>
17

18
// Always available:
19
#include <botan/version.h>
20
#include <botan/internal/target_info.h>
21

22
#if defined(BOTAN_HAS_CPUID)
23
   #include <botan/internal/cpuid.h>
24
#endif
25

26
#if defined(BOTAN_HAS_OS_UTILS)
27
   #include <botan/internal/os_utils.h>
28
#endif
29

30
#if defined(BOTAN_HAS_ECC_GROUP)
31
   #include <botan/ec_group.h>
32
#endif
33

34
namespace Botan_CLI {
35

36
namespace {
37

38
class JSON_Output final {
2✔
39
   public:
40
      void add(const Timer& timer) { m_results.push_back(timer); }
2✔
41

42
      std::string print() const {
1✔
43
         std::ostringstream out;
1✔
44

45
         out << "[\n";
1✔
46

47
         out << "{"
1✔
48
             << R"("arch": ")" << BOTAN_TARGET_ARCH << "\", "
49
             << R"("version": ")" << Botan::short_version_cstr() << "\", ";
1✔
50

51
         if(auto vc_revision = Botan::version_vc_revision()) {
1✔
52
            out << R"("git": ")" << *vc_revision << "\", ";
×
53
         }
×
54

55
         out << R"("compiler": ")" << BOTAN_COMPILER_INVOCATION_STRING << "\""
1✔
56
             << "},\n";
1✔
57

58
         for(size_t i = 0; i != m_results.size(); ++i) {
3✔
59
            const Timer& t = m_results[i];
2✔
60

61
            out << "{"
2✔
62
                << R"("algo": ")" << t.get_name() << "\", "
2✔
63
                << R"("op": ")" << t.doing() << "\", "
2✔
64
                << "\"events\": " << t.events() << ", ";
2✔
65

66
            if(t.cycles_consumed() > 0) {
4✔
67
               out << "\"cycles\": " << t.cycles_consumed() << ", ";
4✔
68
            }
69

70
            if(t.buf_size() > 0) {
2✔
71
               out << "\"bps\": " << static_cast<uint64_t>(t.events() / (t.nanoseconds() / 1000000000.0)) << ", ";
2✔
72
               out << "\"buf_size\": " << t.buf_size() << ", ";
2✔
73
            }
74

75
            out << "\"nanos\": " << t.value() << "}";
2✔
76

77
            if(i != m_results.size() - 1) {
2✔
78
               out << ",";
1✔
79
            }
80

81
            out << "\n";
2✔
82
         }
83
         out << "]\n";
1✔
84

85
         return out.str();
2✔
86
      }
1✔
87

88
   private:
89
      std::vector<Timer> m_results;
90
};
91

92
class Summary final {
1✔
93
   public:
94
      Summary() = default;
1✔
95

96
      void add(const Timer& t) {
2✔
97
         if(t.buf_size() == 0) {
2✔
98
            m_ops_entries.push_back(t);
×
99
         } else {
100
            m_bps_entries[std::make_pair(t.doing(), t.get_name())].push_back(t);
4✔
101
         }
102
      }
2✔
103

104
      std::string print() {
1✔
105
         const size_t name_padding = 35;
1✔
106
         const size_t op_name_padding = 16;
1✔
107
         const size_t op_padding = 16;
1✔
108

109
         std::ostringstream result_ss;
1✔
110
         result_ss << std::fixed;
1✔
111

112
         if(!m_bps_entries.empty()) {
1✔
113
            result_ss << "\n";
1✔
114

115
            // add table header
116
            result_ss << std::setw(name_padding) << std::left << "algo" << std::setw(op_name_padding) << std::left
1✔
117
                      << "operation";
1✔
118

119
            for(const Timer& t : m_bps_entries.begin()->second) {
2✔
120
               result_ss << std::setw(op_padding) << std::right << (std::to_string(t.buf_size()) + " bytes");
2✔
121
            }
122
            result_ss << "\n";
1✔
123

124
            // add table entries
125
            for(const auto& entry : m_bps_entries) {
3✔
126
               if(entry.second.empty()) {
2✔
127
                  continue;
×
128
               }
129

130
               result_ss << std::setw(name_padding) << std::left << (entry.first.second) << std::setw(op_name_padding)
2✔
131
                         << std::left << (entry.first.first);
2✔
132

133
               for(const Timer& t : entry.second) {
4✔
134
                  if(t.events() == 0) {
2✔
135
                     result_ss << std::setw(op_padding) << std::right << "N/A";
×
136
                  } else {
137
                     result_ss << std::setw(op_padding) << std::right << std::setprecision(2)
2✔
138
                               << (t.bytes_per_second() / 1000.0);
2✔
139
                  }
140
               }
141

142
               result_ss << "\n";
2✔
143
            }
144

145
            result_ss << "\n[results are the number of 1000s bytes processed per second]\n";
1✔
146
         }
147

148
         if(!m_ops_entries.empty()) {
1✔
149
            result_ss << std::setprecision(6) << "\n";
×
150

151
            // sort entries
152
            std::sort(m_ops_entries.begin(), m_ops_entries.end());
×
153

154
            // add table header
155
            result_ss << std::setw(name_padding) << std::left << "algo" << std::setw(op_name_padding) << std::left
×
156
                      << "operation" << std::setw(op_padding) << std::right << "sec/op" << std::setw(op_padding)
×
157
                      << std::right << "op/sec"
×
158
                      << "\n";
×
159

160
            // add table entries
161
            for(const Timer& entry : m_ops_entries) {
×
162
               result_ss << std::setw(name_padding) << std::left << entry.get_name() << std::setw(op_name_padding)
×
163
                         << std::left << entry.doing() << std::setw(op_padding) << std::right
×
164
                         << entry.seconds_per_event() << std::setw(op_padding) << std::right
×
165
                         << entry.events_per_second() << "\n";
×
166
            }
167
         }
168

169
         return result_ss.str();
2✔
170
      }
1✔
171

172
   private:
173
      std::map<std::pair<std::string, std::string>, std::vector<Timer>> m_bps_entries;
174
      std::vector<Timer> m_ops_entries;
175
};
176

177
std::vector<size_t> unique_buffer_sizes(const std::string& cmdline_arg) {
28✔
178
   const size_t MAX_BUF_SIZE = 64 * 1024 * 1024;
28✔
179

180
   std::set<size_t> buf;
28✔
181
   for(const std::string& size_str : Command::split_on(cmdline_arg, ',')) {
54✔
182
      size_t x = 0;
29✔
183
      try {
29✔
184
         size_t converted = 0;
29✔
185
         x = static_cast<size_t>(std::stoul(size_str, &converted, 0));
29✔
186

187
         if(converted != size_str.size()) {
28✔
188
            throw CLI_Usage_Error("Invalid integer");
×
189
         }
190
      } catch(std::exception&) {
1✔
191
         throw CLI_Usage_Error("Invalid integer value '" + size_str + "' for option buf-size");
2✔
192
      }
1✔
193

194
      if(x == 0) {
28✔
195
         throw CLI_Usage_Error("Cannot have a zero-sized buffer");
2✔
196
      }
197

198
      if(x > MAX_BUF_SIZE) {
27✔
199
         throw CLI_Usage_Error("Specified buffer size is too large");
2✔
200
      }
201

202
      buf.insert(x);
26✔
203
   }
28✔
204

205
   return std::vector<size_t>(buf.begin(), buf.end());
28✔
206
}
25✔
207

208
std::string format_timer(const Timer& t, size_t time_unit) {
494✔
209
   constexpr size_t MiB = 1024 * 1024;
494✔
210

211
   std::ostringstream oss;
494✔
212

213
   oss << t.get_name() << " ";
494✔
214

215
   const uint64_t events = t.events();
494✔
216

217
   if(t.buf_size() == 0) {
494✔
218
      // Report operations/time unit
219

220
      if(events == 0) {
471✔
221
         oss << "no events ";
×
222
      } else {
223
         oss << static_cast<uint64_t>(t.events_per_second()) << ' ' << t.doing() << "/sec; ";
942✔
224

225
         if(time_unit == 1000) {
471✔
226
            oss << std::setprecision(2) << std::fixed << (t.milliseconds() / events) << " ms/op ";
471✔
227
         } else if(time_unit == 1000 * 1000) {
×
228
            oss << std::setprecision(2) << std::fixed << (t.microseconds() / events) << " us/op ";
×
229
         } else if(time_unit == 1000 * 1000 * 1000) {
×
230
            oss << std::setprecision(0) << std::fixed << (t.nanoseconds() / events) << " ns/op ";
×
231
         }
232

233
         if(t.cycles_consumed() != 0 && events > 0) {
942✔
234
            const double cycles_per_op = static_cast<double>(t.cycles_consumed()) / events;
471✔
235
            const int precision = (cycles_per_op < 10000) ? 2 : 0;
471✔
236
            oss << std::fixed << std::setprecision(precision) << cycles_per_op << " cycles/op ";
471✔
237
         }
238

239
         oss << "(" << events << " " << (events == 1 ? "op" : "ops") << " in " << t.milliseconds() << " ms)";
726✔
240
      }
241
   } else {
242
      // Bulk op - report bytes/time unit
243

244
      const double MiB_total = static_cast<double>(events) / MiB;
23✔
245
      const double MiB_per_sec = MiB_total / t.seconds();
23✔
246

247
      if(!t.doing().empty()) {
23✔
248
         oss << t.doing() << " ";
23✔
249
      }
250

251
      if(t.buf_size() > 0) {
23✔
252
         oss << "buffer size " << t.buf_size() << " bytes: ";
23✔
253
      }
254

255
      if(events == 0) {
23✔
256
         oss << "N/A ";
×
257
      } else {
258
         oss << std::fixed << std::setprecision(3) << MiB_per_sec << " MiB/sec ";
23✔
259
      }
260

261
      if(t.cycles_consumed() != 0 && events > 0) {
46✔
262
         const double cycles_per_byte = static_cast<double>(t.cycles_consumed()) / events;
23✔
263
         oss << std::fixed << std::setprecision(2) << cycles_per_byte << " cycles/byte ";
23✔
264
      }
265

266
      oss << "(" << MiB_total << " MiB in " << t.milliseconds() << " ms)";
23✔
267
   }
268

269
   return oss.str();
988✔
270
}
494✔
271

272
}  // namespace
273

274
class Speed final : public Command {
×
275
   public:
276
      Speed() :
29✔
277
            Command(
278
               "speed --msec=500 --format=default --time-unit=ms --ecc-groups= --buf-size=1024 --clear-cpuid= --cpu-clock-speed=0 --cpu-clock-ratio=1.0 *algos") {
58✔
279
      }
29✔
280

281
      static std::vector<std::string> default_benchmark_list() {
×
282
         /*
283
         This is not intended to be exhaustive: it just hits the high
284
         points of the most interesting or widely used algorithms.
285
         */
286
         // clang-format off
287
         return {
×
288
            /* Block ciphers */
289
            "AES-128",
290
            "AES-192",
291
            "AES-256",
292
            "ARIA-128",
293
            "ARIA-192",
294
            "ARIA-256",
295
            "Blowfish",
296
            "CAST-128",
297
            "Camellia-128",
298
            "Camellia-192",
299
            "Camellia-256",
300
            "DES",
301
            "TripleDES",
302
            "GOST-28147-89",
303
            "IDEA",
304
            "Noekeon",
305
            "SHACAL2",
306
            "SM4",
307
            "Serpent",
308
            "Threefish-512",
309
            "Twofish",
310

311
            /* Cipher modes */
312
            "AES-128/CBC",
313
            "AES-128/CTR-BE",
314
            "AES-128/EAX",
315
            "AES-128/OCB",
316
            "AES-128/GCM",
317
            "AES-128/XTS",
318
            "AES-128/SIV",
319
            "Ascon-AEAD128",
320

321
            "Serpent/CBC",
322
            "Serpent/CTR-BE",
323
            "Serpent/EAX",
324
            "Serpent/OCB",
325
            "Serpent/GCM",
326
            "Serpent/XTS",
327
            "Serpent/SIV",
328

329
            "ChaCha20Poly1305",
330

331
            /* Stream ciphers */
332
            "RC4",
333
            "Salsa20",
334
            "ChaCha20",
335

336
            /* Hashes */
337
            "SHA-1",
338
            "SHA-256",
339
            "SHA-512",
340
            "SHA-3(256)",
341
            "SHA-3(512)",
342
            "Ascon-Hash256",
343
            "RIPEMD-160",
344
            "Skein-512",
345
            "Blake2b",
346
            "Whirlpool",
347

348
            /* XOFs */
349
            "SHAKE-128",
350
            "SHAKE-256",
351
            "Ascon-XOF128",
352

353
            /* MACs */
354
            "CMAC(AES-128)",
355
            "HMAC(SHA-256)",
356

357
            /* pubkey */
358
            "RSA",
359
            "DH",
360
            "ECDH",
361
            "ECDSA",
362
            "Ed25519",
363
            "Ed448",
364
            "X25519",
365
            "X448",
366
            "ML-KEM",
367
            "ML-DSA",
368
            "SLH-DSA",
369
            "FrodoKEM",
370
            "HSS-LMS",
371
         };
×
372
         // clang-format on
373
      }
374

375
      std::string group() const override { return "misc"; }
1✔
376

377
      std::string description() const override { return "Measures the speed of algorithms"; }
1✔
378

379
      void go() override {
28✔
380
         std::chrono::milliseconds msec(get_arg_sz("msec"));
28✔
381
         std::vector<std::string> ecc_groups = Command::split_on(get_arg("ecc-groups"), ',');
56✔
382
         const std::string format = get_arg("format");
28✔
383
         const std::string clock_ratio = get_arg("cpu-clock-ratio");
31✔
384

385
         const size_t clock_speed = get_arg_sz("cpu-clock-speed");
28✔
386

387
         double clock_cycle_ratio = std::strtod(clock_ratio.c_str(), nullptr);
28✔
388

389
         m_time_unit = [](std::string_view tu) {
115✔
390
            if(tu == "ms") {
28✔
391
               return 1000;
28✔
392
            } else if(tu == "us") {
×
393
               return 1000 * 1000;
×
394
            } else if(tu == "ns") {
×
395
               return 1000 * 1000 * 1000;
×
396
            } else {
397
               throw CLI_Usage_Error("Unknown time unit (supported: ms, us, ns)");
×
398
            }
399
         }(get_arg("time-unit"));
28✔
400

401
         /*
402
         * This argument is intended to be the ratio between the cycle counter
403
         * and the actual machine cycles. It is extremely unlikely that there is
404
         * any machine where the cycle counter increments faster than the actual
405
         * clock.
406
         */
407
         if(clock_cycle_ratio < 0.0 || clock_cycle_ratio > 1.0) {
28✔
408
            throw CLI_Usage_Error("Unlikely CPU clock ratio of " + clock_ratio);
×
409
         }
410

411
         clock_cycle_ratio = 1.0 / clock_cycle_ratio;
28✔
412

413
#if defined(BOTAN_HAS_OS_UTILS)
414
         if(clock_speed != 0 && Botan::OS::get_cpu_cycle_counter() != 0) {
28✔
415
            error_output() << "The --cpu-clock-speed option is only intended to be used on "
×
416
                              "platforms without access to a cycle counter.\n"
417
                              "Expect incorrect results\n\n";
×
418
         }
419
#endif
420

421
         if(format == "table") {
28✔
422
            m_summary = std::make_unique<Summary>();
1✔
423
         } else if(format == "json") {
27✔
424
            m_json = std::make_unique<JSON_Output>();
1✔
425
         } else if(format != "default") {
26✔
426
            throw CLI_Usage_Error("Unknown --format type '" + format + "'");
×
427
         }
428

429
#if defined(BOTAN_HAS_ECC_GROUP)
430
         if(ecc_groups.empty()) {
28✔
431
            ecc_groups = {"secp256r1", "secp384r1", "secp521r1", "brainpool256r1", "brainpool384r1", "brainpool512r1"};
224✔
432
         } else if(ecc_groups.size() == 1 && ecc_groups[0] == "all") {
×
433
            const auto& all = Botan::EC_Group::known_named_groups();
×
434
            ecc_groups.assign(all.begin(), all.end());
×
435
         } else if(ecc_groups.size() == 1 && ecc_groups[0] == "generic") {
×
436
            ecc_groups.clear();
×
437
            for(const auto& group_name : Botan::EC_Group::known_named_groups()) {
×
438
               Botan::EC_Group group(group_name);
×
439
               if(group.engine() == Botan::EC_Group_Engine::Generic) {
×
440
                  ecc_groups.push_back(group_name);
×
441
               }
442
            }
×
443
         }
444
#endif
445

446
         std::vector<std::string> algos = get_arg_list("algos");
31✔
447

448
         const std::vector<size_t> buf_sizes = unique_buffer_sizes(get_arg("buf-size"));
59✔
449

450
#if defined(BOTAN_HAS_CPUID)
451
         for(const std::string& cpuid_to_clear : Command::split_on(get_arg("clear-cpuid"), ',')) {
26✔
452
            if(auto bit = Botan::CPUID::bit_from_string(cpuid_to_clear)) {
1✔
453
               Botan::CPUID::clear_cpuid_bit(*bit);
×
454
            } else {
455
               error_output() << "Warning don't know CPUID flag '" << cpuid_to_clear << "'\n";
1✔
456
            }
457
         }
25✔
458
#endif
459

460
         if(verbose() || m_summary) {
25✔
461
#if defined(BOTAN_HAS_CPUID)
462
            output() << Botan::version_string() << "\n"
2✔
463
                     << "CPUID: " << Botan::CPUID::to_string() << "\n\n";
3✔
464
#else
465
            output() << Botan::version_string() << "\n\n";
466
#endif
467
         }
468

469
         const bool using_defaults = (algos.empty());
25✔
470
         if(using_defaults) {
25✔
471
            algos = default_benchmark_list();
×
472
         }
473

474
         PerfConfig perf_config([&](const Timer& t) { this->record_result(t); },
521✔
475
                                clock_speed,
476
                                clock_cycle_ratio,
477
                                msec,
478
                                ecc_groups,
479
                                buf_sizes,
480
                                this->error_output(),
481
                                this->rng());
25✔
482

483
         for(const auto& algo : algos) {
70✔
484
            if(auto perf = PerfTest::get(algo)) {
45✔
485
               perf->go(perf_config);
45✔
486
            } else if(verbose() || !using_defaults) {
×
487
               error_output() << "Unknown algorithm '" << algo << "'\n";
×
488
            }
45✔
489
         }
490

491
         if(m_json) {
25✔
492
            output() << m_json->print();
2✔
493
         }
494
         if(m_summary) {
25✔
495
            output() << m_summary->print() << "\n";
3✔
496
         }
497

498
         if(verbose() && clock_speed == 0 && m_cycles_consumed > 0 && m_ns_taken > 0) {
25✔
499
            const double seconds = static_cast<double>(m_ns_taken) / 1000000000;
×
500
            const double Hz = static_cast<double>(m_cycles_consumed) / seconds;
×
501
            const double MHz = Hz / 1000000;
×
502
            output() << "\nEstimated clock speed " << MHz << " MHz\n";
×
503
         }
504
      }
115✔
505

506
   private:
507
      size_t m_time_unit = 0;
508
      uint64_t m_cycles_consumed = 0;
509
      uint64_t m_ns_taken = 0;
510
      std::unique_ptr<Summary> m_summary;
511
      std::unique_ptr<JSON_Output> m_json;
512

513
      void record_result(const Timer& t) {
496✔
514
         m_ns_taken += t.value();
496✔
515
         m_cycles_consumed += t.cycles_consumed();
496✔
516
         if(m_json) {
496✔
517
            m_json->add(t);
2✔
518
         } else {
519
            output() << format_timer(t, m_time_unit) << "\n";
988✔
520

521
            if(m_summary) {
494✔
522
               m_summary->add(t);
2✔
523
            }
524
         }
525
      }
496✔
526
};
527

528
BOTAN_REGISTER_COMMAND("speed", Speed);
29✔
529

530
}  // namespace Botan_CLI
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc