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

randombit / botan / 13227325872

09 Feb 2025 03:02PM UTC coverage: 91.652% (-0.01%) from 91.662%
13227325872

push

github

web-flow
Merge pull request #4574 from Rohde-Schwarz/ci/ccache_tweaks

CI: Tune the ccache size

94833 of 103471 relevant lines covered (91.65%)

11293160.6 hits per line

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

98.0
/src/cli/perf_math.cpp
1
/*
2
* (C) 2024 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "perf.h"
8

9
#if defined(BOTAN_HAS_BIGINT)
10
   #include <botan/bigint.h>
11
   #include <botan/internal/divide.h>
12
#endif
13

14
#if defined(BOTAN_HAS_NUMBERTHEORY)
15
   #include <botan/numthry.h>
16
   #include <botan/reducer.h>
17
   #include <botan/internal/primality.h>
18
#endif
19

20
#if defined(BOTAN_HAS_DL_GROUP)
21
   #include <botan/dl_group.h>
22
#endif
23

24
namespace Botan_CLI {
25

26
#if defined(BOTAN_HAS_BIGINT)
27

28
class PerfTest_MpMul final : public PerfTest {
1✔
29
   public:
30
      void go(const PerfConfig& config) override {
1✔
31
         std::chrono::milliseconds runtime_per_size = config.runtime();
1✔
32

33
         for(size_t bits : {256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096}) {
10✔
34
            auto mul_timer = config.make_timer("BigInt mul " + std::to_string(bits));
18✔
35
            auto sqr_timer = config.make_timer("BigInt sqr " + std::to_string(bits));
18✔
36

37
            const Botan::BigInt y(config.rng(), bits);
9✔
38
            Botan::secure_vector<Botan::word> ws;
9✔
39

40
            while(mul_timer->under(runtime_per_size)) {
9,018✔
41
               Botan::BigInt x(config.rng(), bits);
9,009✔
42

43
               sqr_timer->start();
9,009✔
44
               x.square(ws);
9,009✔
45
               sqr_timer->stop();
9,009✔
46

47
               x.mask_bits(bits);
9,009✔
48

49
               mul_timer->start();
9,009✔
50
               x.mul(y, ws);
9,009✔
51
               mul_timer->stop();
9,009✔
52
            }
9,009✔
53

54
            config.record_result(*mul_timer);
9✔
55
            config.record_result(*sqr_timer);
18✔
56
         }
18✔
57
      }
1✔
58
};
59

60
BOTAN_REGISTER_PERF_TEST("mp_mul", PerfTest_MpMul);
1✔
61

62
class PerfTest_MpDiv final : public PerfTest {
1✔
63
   public:
64
      void go(const PerfConfig& config) override {
1✔
65
         std::chrono::milliseconds runtime_per_size = config.runtime();
1✔
66

67
         for(size_t n_bits : {256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096}) {
10✔
68
            const size_t q_bits = n_bits / 2;
9✔
69
            const std::string bit_descr = std::to_string(n_bits) + "/" + std::to_string(q_bits);
27✔
70

71
            auto div_timer = config.make_timer("BigInt div " + bit_descr);
18✔
72
            auto ct_div_timer = config.make_timer("BigInt ct_div " + bit_descr);
18✔
73

74
            Botan::BigInt y;
9✔
75
            Botan::BigInt x;
9✔
76
            Botan::secure_vector<Botan::word> ws;
9✔
77

78
            Botan::BigInt q1, r1, q2, r2;
9✔
79

80
            while(ct_div_timer->under(runtime_per_size)) {
9✔
81
               x.randomize(config.rng(), n_bits);
68✔
82
               y.randomize(config.rng(), q_bits);
68✔
83

84
               div_timer->start();
68✔
85
               Botan::vartime_divide(x, y, q1, r1);
68✔
86
               div_timer->stop();
68✔
87

88
               ct_div_timer->start();
68✔
89
               Botan::ct_divide(x, y, q2, r2);
68✔
90
               ct_div_timer->stop();
68✔
91

92
               BOTAN_ASSERT_EQUAL(q1, q2, "Quotient ok");
68✔
93
               BOTAN_ASSERT_EQUAL(r1, r2, "Remainder ok");
145✔
94
            }
95

96
            config.record_result(*div_timer);
9✔
97
            config.record_result(*ct_div_timer);
9✔
98
         }
18✔
99
      }
1✔
100
};
101

102
BOTAN_REGISTER_PERF_TEST("mp_div", PerfTest_MpDiv);
1✔
103

104
class PerfTest_MpDiv10 final : public PerfTest {
1✔
105
   public:
106
      void go(const PerfConfig& config) override {
1✔
107
         std::chrono::milliseconds runtime_per_size = config.runtime();
1✔
108

109
         for(size_t n_bits : {256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096}) {
10✔
110
            const std::string bit_descr = std::to_string(n_bits) + "/10";
18✔
111

112
            auto div_timer = config.make_timer("BigInt div " + bit_descr);
18✔
113
            auto ct_div_timer = config.make_timer("BigInt ct_div " + bit_descr);
18✔
114

115
            Botan::BigInt x;
9✔
116
            Botan::secure_vector<Botan::word> ws;
9✔
117

118
            const auto ten = Botan::BigInt::from_word(10);
9✔
119
            Botan::BigInt q1, r1, q2;
9✔
120
            Botan::word r2;
9✔
121

122
            while(ct_div_timer->under(runtime_per_size)) {
9✔
123
               x.randomize(config.rng(), n_bits);
699✔
124

125
               div_timer->start();
699✔
126
               Botan::vartime_divide(x, ten, q1, r1);
699✔
127
               div_timer->stop();
699✔
128

129
               ct_div_timer->start();
699✔
130
               Botan::ct_divide_word(x, 10, q2, r2);
699✔
131
               ct_div_timer->stop();
699✔
132

133
               BOTAN_ASSERT_EQUAL(q1, q2, "Quotient ok");
699✔
134
               BOTAN_ASSERT_EQUAL(r1, r2, "Remainder ok");
1,407✔
135
            }
136

137
            config.record_result(*div_timer);
9✔
138
            config.record_result(*ct_div_timer);
9✔
139
         }
18✔
140
      }
1✔
141
};
142

143
BOTAN_REGISTER_PERF_TEST("mp_div10", PerfTest_MpDiv10);
1✔
144

145
#endif
146

147
#if defined(BOTAN_HAS_NUMBERTHEORY)
148

149
class PerfTest_BnRedc final : public PerfTest {
1✔
150
   public:
151
      void go(const PerfConfig& config) override {
1✔
152
         const auto runtime = config.runtime();
1✔
153

154
         for(size_t bitsize : {512, 1024, 2048, 4096}) {
5✔
155
            Botan::BigInt p(config.rng(), bitsize);
4✔
156

157
            std::string bit_str = std::to_string(bitsize) + " bit ";
8✔
158
            auto barrett_setup_pub_timer = config.make_timer(bit_str + "Barrett setup public");
8✔
159
            auto barrett_setup_sec_timer = config.make_timer(bit_str + "Barrett setup secret");
8✔
160

161
            while(barrett_setup_sec_timer->under(runtime)) {
26✔
162
               barrett_setup_sec_timer->run([&]() { Botan::Modular_Reducer::for_secret_modulus(p); });
44✔
163
               barrett_setup_pub_timer->run([&]() { Botan::Modular_Reducer::for_public_modulus(p); });
44✔
164
            }
165

166
            config.record_result(*barrett_setup_pub_timer);
4✔
167
            config.record_result(*barrett_setup_sec_timer);
4✔
168

169
            auto mod_p = Botan::Modular_Reducer::for_public_modulus(p);
4✔
170

171
            auto barrett_timer = config.make_timer(bit_str + "Barrett redc");
8✔
172
            auto knuth_timer = config.make_timer(bit_str + "Knuth redc");
8✔
173
            auto ct_modulo_timer = config.make_timer(bit_str + "ct_modulo");
8✔
174

175
            while(ct_modulo_timer->under(runtime)) {
12✔
176
               const Botan::BigInt x(config.rng(), p.bits() * 2 - 1);
8✔
177

178
               const Botan::BigInt r1 = barrett_timer->run([&] { return mod_p.reduce(x); });
16✔
179
               const Botan::BigInt r2 = knuth_timer->run([&] { return x % p; });
16✔
180
               const Botan::BigInt r3 = ct_modulo_timer->run([&] { return Botan::ct_modulo(x, p); });
16✔
181

182
               BOTAN_ASSERT(r1 == r2, "Computed different results");
8✔
183
               BOTAN_ASSERT(r1 == r3, "Computed different results");
8✔
184
            }
8✔
185

186
            config.record_result(*barrett_timer);
4✔
187
            config.record_result(*knuth_timer);
4✔
188
            config.record_result(*ct_modulo_timer);
8✔
189
         }
20✔
190
      }
1✔
191
};
192

193
BOTAN_REGISTER_PERF_TEST("bn_redc", PerfTest_BnRedc);
1✔
194

195
class PerfTest_InvMod final : public PerfTest {
1✔
196
   public:
197
      void go(const PerfConfig& config) override {
1✔
198
         const auto runtime = config.runtime();
1✔
199

200
         for(size_t bits : {256, 384, 512, 1024, 2048}) {
6✔
201
            const std::string bit_str = std::to_string(bits);
5✔
202

203
            auto timer = config.make_timer("inverse_mod-" + bit_str);
10✔
204
            auto gcd_timer = config.make_timer("gcd-" + bit_str);
10✔
205

206
            while(timer->under(runtime) && gcd_timer->under(runtime)) {
13✔
207
               const Botan::BigInt x(config.rng(), bits - 1);
8✔
208
               Botan::BigInt mod(config.rng(), bits);
8✔
209

210
               const Botan::BigInt x_inv = timer->run([&] { return Botan::inverse_mod(x, mod); });
16✔
211

212
               const Botan::BigInt g = gcd_timer->run([&] { return gcd(x, mod); });
16✔
213

214
               if(x_inv == 0) {
8✔
215
                  BOTAN_ASSERT(g != 1, "Inversion only fails if gcd(x, mod) > 1");
×
216
               } else {
217
                  BOTAN_ASSERT(g == 1, "Inversion succeeds only if gcd != 1");
8✔
218
                  const Botan::BigInt check = (x_inv * x) % mod;
8✔
219
                  BOTAN_ASSERT_EQUAL(check, 1, "Const time inversion correct");
8✔
220
               }
8✔
221
            }
8✔
222

223
            config.record_result(*timer);
5✔
224
            config.record_result(*gcd_timer);
10✔
225
         }
5✔
226
      }
1✔
227
};
228

229
BOTAN_REGISTER_PERF_TEST("inverse_mod", PerfTest_InvMod);
1✔
230

231
class PerfTest_IsPrime final : public PerfTest {
1✔
232
   public:
233
      void go(const PerfConfig& config) override {
1✔
234
         const auto runtime = config.runtime();
1✔
235

236
         for(size_t bits : {256, 512, 1024}) {
4✔
237
            auto mr_timer = config.make_timer("Miller-Rabin-" + std::to_string(bits));
6✔
238
            auto bpsw_timer = config.make_timer("Bailie-PSW-" + std::to_string(bits));
6✔
239
            auto lucas_timer = config.make_timer("Lucas-" + std::to_string(bits));
6✔
240

241
            Botan::BigInt n = Botan::random_prime(config.rng(), bits);
3✔
242

243
            while(lucas_timer->under(runtime)) {
6✔
244
               auto mod_n = Botan::Modular_Reducer::for_public_modulus(n);
3✔
245

246
               mr_timer->run([&]() { return Botan::is_miller_rabin_probable_prime(n, mod_n, config.rng(), 2); });
6✔
247

248
               bpsw_timer->run([&]() { return Botan::is_bailie_psw_probable_prime(n, mod_n); });
6✔
249

250
               lucas_timer->run([&]() { return Botan::is_lucas_probable_prime(n, mod_n); });
6✔
251

252
               n += 2;
3✔
253
            }
3✔
254

255
            config.record_result(*mr_timer);
3✔
256
            config.record_result(*bpsw_timer);
3✔
257
            config.record_result(*lucas_timer);
3✔
258
         }
9✔
259
      }
1✔
260
};
261

262
BOTAN_REGISTER_PERF_TEST("primality_test", PerfTest_IsPrime);
1✔
263

264
class PerfTest_RandomPrime final : public PerfTest {
1✔
265
   public:
266
      void go(const PerfConfig& config) override {
1✔
267
         const auto coprime = Botan::BigInt::from_word(0x10001);
1✔
268
         const auto runtime = config.runtime();
1✔
269

270
         auto& rng = config.rng();
1✔
271

272
         for(size_t bits : {256, 384, 512, 768, 1024, 1536}) {
7✔
273
            auto genprime_timer = config.make_timer("random_prime " + std::to_string(bits));
12✔
274
            auto gensafe_timer = config.make_timer("random_safe_prime " + std::to_string(bits));
12✔
275
            auto is_prime_timer = config.make_timer("is_prime " + std::to_string(bits));
12✔
276

277
            while(gensafe_timer->under(runtime)) {
12✔
278
               const Botan::BigInt p = genprime_timer->run([&] { return Botan::random_prime(rng, bits, coprime); });
12✔
279

280
               if(!is_prime_timer->run([&] { return Botan::is_prime(p, rng, 64, true); })) {
12✔
281
                  config.error_output() << "Generated prime " << p << " which failed a primality test";
×
282
               }
283

284
               const Botan::BigInt sg = gensafe_timer->run([&] { return Botan::random_safe_prime(rng, bits); });
12✔
285

286
               if(!is_prime_timer->run([&] { return Botan::is_prime(sg, rng, 64, true); })) {
12✔
287
                  config.error_output() << "Generated safe prime " << sg << " which failed a primality test";
×
288
               }
289

290
               if(!is_prime_timer->run([&] { return Botan::is_prime(sg / 2, rng, 64, true); })) {
12✔
291
                  config.error_output() << "Generated prime " << sg / 2 << " which failed a primality test";
×
292
               }
293

294
               // Now test p+2, p+4, ... which may or may not be prime
295
               for(size_t i = 2; i <= 64; i += 2) {
198✔
296
                  is_prime_timer->run([&]() { Botan::is_prime(p + i, rng, 64, true); });
384✔
297
               }
298
            }
6✔
299

300
            config.record_result(*genprime_timer);
6✔
301
            config.record_result(*gensafe_timer);
6✔
302
            config.record_result(*is_prime_timer);
12✔
303
         }
12✔
304
      }
1✔
305
};
306

307
BOTAN_REGISTER_PERF_TEST("random_prime", PerfTest_RandomPrime);
1✔
308

309
#endif
310

311
#if defined(BOTAN_HAS_DL_GROUP)
312

313
class PerfTest_ModExp final : public PerfTest {
1✔
314
   public:
315
      void go(const PerfConfig& config) override {
1✔
316
         for(size_t group_bits : {1024, 1536, 2048, 3072, 4096, 6144, 8192}) {
8✔
317
            const std::string group_name = "modp/ietf/" + std::to_string(group_bits);
14✔
318
            auto group = Botan::DL_Group::from_name(group_name);
7✔
319

320
            const size_t e_bits = group.exponent_bits();
7✔
321
            const size_t f_bits = group_bits - 1;
7✔
322

323
            const Botan::BigInt random_e(config.rng(), e_bits);
7✔
324
            const Botan::BigInt random_f(config.rng(), f_bits);
7✔
325

326
            auto e_timer = config.make_timer(group_name + " short exp");
14✔
327
            auto f_timer = config.make_timer(group_name + "  full exp");
14✔
328

329
            while(f_timer->under(config.runtime())) {
15✔
330
               e_timer->run([&]() { group.power_g_p(random_e, e_bits); });
16✔
331
               f_timer->run([&]() { group.power_g_p(random_f, f_bits); });
16✔
332
            }
333

334
            config.record_result(*e_timer);
7✔
335
            config.record_result(*f_timer);
14✔
336
         }
21✔
337
      }
1✔
338
};
339

340
BOTAN_REGISTER_PERF_TEST("modexp", PerfTest_ModExp);
1✔
341

342
#endif
343

344
}  // 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