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

randombit / botan / 21031897445

15 Jan 2026 12:50PM UTC coverage: 90.037% (-0.4%) from 90.395%
21031897445

Pull #5240

github

web-flow
Merge 13ffffdd5 into 32478179d
Pull Request #5240: Some changes to reduce time taken by the coverage build

102027 of 113317 relevant lines covered (90.04%)

11676540.94 hits per line

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

0.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/assert.h>
11
   #include <botan/bigint.h>
12
   #include <botan/internal/divide.h>
13
#endif
14

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

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

25
namespace Botan_CLI {
26

27
#if defined(BOTAN_HAS_BIGINT)
28

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

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

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

41
            while(mul_timer->under(runtime_per_size)) {
×
42
               Botan::BigInt x(config.rng(), bits);
×
43

44
               sqr_timer->start();
×
45
               x.square(ws);
×
46
               sqr_timer->stop();
×
47

48
               x.mask_bits(bits);
×
49

50
               mul_timer->start();
×
51
               x.mul(y, ws);
×
52
               mul_timer->stop();
×
53
            }
×
54

55
            config.record_result(*mul_timer);
×
56
            config.record_result(*sqr_timer);
×
57
         }
×
58
      }
×
59
};
60

61
BOTAN_REGISTER_PERF_TEST("mp_mul", PerfTest_MpMul);
×
62

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

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

72
            auto div_timer = config.make_timer("BigInt div " + bit_descr);
×
73
            auto ct_div_timer = config.make_timer("BigInt ct_div " + bit_descr);
×
74

75
            Botan::BigInt y;
×
76
            Botan::BigInt x;
×
77
            const Botan::secure_vector<Botan::word> ws;
×
78

79
            Botan::BigInt q1;
×
80
            Botan::BigInt r1;
×
81
            Botan::BigInt q2;
×
82
            Botan::BigInt r2;
×
83

84
            while(ct_div_timer->under(runtime_per_size)) {
×
85
               x.randomize(config.rng(), n_bits);
×
86
               y.randomize(config.rng(), q_bits);
×
87

88
               div_timer->start();
×
89
               Botan::vartime_divide(x, y, q1, r1);
×
90
               div_timer->stop();
×
91

92
               ct_div_timer->start();
×
93
               Botan::ct_divide(x, y, q2, r2);
×
94
               ct_div_timer->stop();
×
95

96
               BOTAN_ASSERT_EQUAL(q1, q2, "Quotient ok");
×
97
               BOTAN_ASSERT_EQUAL(r1, r2, "Remainder ok");
×
98
            }
99

100
            config.record_result(*div_timer);
×
101
            config.record_result(*ct_div_timer);
×
102
         }
×
103
      }
×
104
};
105

106
BOTAN_REGISTER_PERF_TEST("mp_div", PerfTest_MpDiv);
×
107

108
class PerfTest_MpDiv10 final : public PerfTest {
×
109
   public:
110
      void go(const PerfConfig& config) override {
×
111
         const std::chrono::milliseconds runtime_per_size = config.runtime();
×
112

113
         for(const size_t n_bits : {256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096}) {
×
114
            const std::string bit_descr = std::to_string(n_bits) + "/10";
×
115

116
            auto div_timer = config.make_timer("BigInt div " + bit_descr);
×
117
            auto ct_div_timer = config.make_timer("BigInt ct_div " + bit_descr);
×
118

119
            Botan::BigInt x;
×
120
            const Botan::secure_vector<Botan::word> ws;
×
121

122
            const auto ten = Botan::BigInt::from_word(10);
×
123
            Botan::BigInt q1;
×
124
            Botan::BigInt r1;
×
125
            Botan::BigInt q2;
×
126
            Botan::word r2 = 0;
×
127

128
            while(ct_div_timer->under(runtime_per_size)) {
×
129
               x.randomize(config.rng(), n_bits);
×
130

131
               div_timer->start();
×
132
               Botan::vartime_divide(x, ten, q1, r1);
×
133
               div_timer->stop();
×
134

135
               ct_div_timer->start();
×
136
               Botan::ct_divide_word(x, 10, q2, r2);
×
137
               ct_div_timer->stop();
×
138

139
               BOTAN_ASSERT_EQUAL(q1, q2, "Quotient ok");
×
140
               BOTAN_ASSERT_EQUAL(r1, r2, "Remainder ok");
×
141
            }
142

143
            config.record_result(*div_timer);
×
144
            config.record_result(*ct_div_timer);
×
145
         }
×
146
      }
×
147
};
148

149
BOTAN_REGISTER_PERF_TEST("mp_div10", PerfTest_MpDiv10);
×
150

151
#endif
152

153
#if defined(BOTAN_HAS_NUMBERTHEORY)
154

155
class PerfTest_BnRedc final : public PerfTest {
×
156
   public:
157
      void go(const PerfConfig& config) override {
×
158
         const auto runtime = config.runtime();
×
159

160
         for(const size_t bitsize : {256, 512, 1024, 2048, 4096}) {
×
161
            Botan::BigInt p(config.rng(), bitsize);
×
162

163
            const std::string bit_str = std::to_string(bitsize) + " bit ";
×
164
            auto barrett_setup_pub_timer = config.make_timer(bit_str + "Barrett setup public");
×
165
            auto barrett_setup_sec_timer = config.make_timer(bit_str + "Barrett setup secret");
×
166

167
            while(barrett_setup_sec_timer->under(runtime)) {
×
168
               barrett_setup_sec_timer->run([&]() { Botan::Barrett_Reduction::for_secret_modulus(p); });
×
169
               barrett_setup_pub_timer->run([&]() { Botan::Barrett_Reduction::for_public_modulus(p); });
×
170
            }
171

172
            config.record_result(*barrett_setup_pub_timer);
×
173
            config.record_result(*barrett_setup_sec_timer);
×
174

175
            auto mod_p = Botan::Barrett_Reduction::for_public_modulus(p);
×
176

177
            auto barrett_timer = config.make_timer(bit_str + "Barrett redc");
×
178
            auto knuth_timer = config.make_timer(bit_str + "Knuth redc");
×
179
            auto ct_modulo_timer = config.make_timer(bit_str + "ct_modulo");
×
180

181
            while(ct_modulo_timer->under(runtime)) {
×
182
               const Botan::BigInt x(config.rng(), p.bits() * 2 - 1);
×
183

184
               const Botan::BigInt r1 = barrett_timer->run([&] { return mod_p.reduce(x); });
×
185
               const Botan::BigInt r2 = knuth_timer->run([&] { return x % p; });
×
186
               const Botan::BigInt r3 = ct_modulo_timer->run([&] { return Botan::ct_modulo(x, p); });
×
187

188
               BOTAN_ASSERT(r1 == r2, "Computed different results");
×
189
               BOTAN_ASSERT(r1 == r3, "Computed different results");
×
190
            }
×
191

192
            config.record_result(*barrett_timer);
×
193
            config.record_result(*knuth_timer);
×
194
            config.record_result(*ct_modulo_timer);
×
195
         }
×
196
      }
×
197
};
198

199
BOTAN_REGISTER_PERF_TEST("bn_redc", PerfTest_BnRedc);
×
200

201
class PerfTest_InvMod final : public PerfTest {
×
202
   public:
203
      void go(const PerfConfig& config) override {
×
204
         const auto runtime = config.runtime();
×
205

206
         for(const size_t bits : {256, 384, 512, 1024, 2048}) {
×
207
            const std::string bit_str = std::to_string(bits);
×
208

209
            auto timer = config.make_timer("inverse_mod-" + bit_str);
×
210
            auto gcd_timer = config.make_timer("gcd-" + bit_str);
×
211

212
            while(timer->under(runtime) && gcd_timer->under(runtime)) {
×
213
               const Botan::BigInt x(config.rng(), bits - 1);
×
214
               Botan::BigInt mod(config.rng(), bits);
×
215

216
               const Botan::BigInt x_inv = timer->run([&] { return Botan::inverse_mod(x, mod); });
×
217

218
               const Botan::BigInt g = gcd_timer->run([&] { return gcd(x, mod); });
×
219

220
               if(x_inv == 0) {
×
221
                  BOTAN_ASSERT(g != 1, "Inversion only fails if gcd(x, mod) > 1");
×
222
               } else {
223
                  BOTAN_ASSERT(g == 1, "Inversion succeeds only if gcd != 1");
×
224
                  const Botan::BigInt check = (x_inv * x) % mod;
×
225
                  BOTAN_ASSERT_EQUAL(check, 1, "Const time inversion correct");
×
226
               }
×
227
            }
×
228

229
            config.record_result(*timer);
×
230
            config.record_result(*gcd_timer);
×
231
         }
×
232
      }
×
233
};
234

235
BOTAN_REGISTER_PERF_TEST("inverse_mod", PerfTest_InvMod);
×
236

237
class PerfTest_IsPrime final : public PerfTest {
×
238
   public:
239
      void go(const PerfConfig& config) override {
×
240
         const auto runtime = config.runtime();
×
241

242
         for(const size_t bits : {256, 512, 1024}) {
×
243
            auto mr_timer = config.make_timer("Miller-Rabin-" + std::to_string(bits));
×
244
            auto bpsw_timer = config.make_timer("Bailie-PSW-" + std::to_string(bits));
×
245
            auto lucas_timer = config.make_timer("Lucas-" + std::to_string(bits));
×
246

247
            Botan::BigInt n = Botan::random_prime(config.rng(), bits);
×
248

249
            while(lucas_timer->under(runtime)) {
×
250
               auto mod_n = Botan::Barrett_Reduction::for_public_modulus(n);
×
251

252
               mr_timer->run([&]() { return Botan::is_miller_rabin_probable_prime(n, mod_n, config.rng(), 2); });
×
253

254
               bpsw_timer->run([&]() { return Botan::is_bailie_psw_probable_prime(n, mod_n); });
×
255

256
               lucas_timer->run([&]() { return Botan::is_lucas_probable_prime(n, mod_n); });
×
257

258
               n += 2;
×
259
            }
×
260

261
            config.record_result(*mr_timer);
×
262
            config.record_result(*bpsw_timer);
×
263
            config.record_result(*lucas_timer);
×
264
         }
×
265
      }
×
266
};
267

268
BOTAN_REGISTER_PERF_TEST("primality_test", PerfTest_IsPrime);
×
269

270
class PerfTest_RandomPrime final : public PerfTest {
×
271
   public:
272
      void go(const PerfConfig& config) override {
×
273
         const auto coprime = Botan::BigInt::from_word(0x10001);
×
274
         const auto runtime = config.runtime();
×
275

276
         auto& rng = config.rng();
×
277

278
         for(size_t bits : {256, 384, 512, 768, 1024, 1536}) {
×
279
            auto genprime_timer = config.make_timer("random_prime " + std::to_string(bits));
×
280
            auto is_prime_timer = config.make_timer("is_prime " + std::to_string(bits));
×
281

282
            while(genprime_timer->under(runtime) && is_prime_timer->under(runtime)) {
×
283
               const Botan::BigInt p = genprime_timer->run([&] { return Botan::random_prime(rng, bits, coprime); });
×
284

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

289
               // Now test p+2, p+4, ... which may or may not be prime
290
               for(size_t i = 2; i <= 64; i += 2) {
×
291
                  is_prime_timer->run([&]() { Botan::is_prime(p + i, rng, 64, true); });
×
292
               }
293
            }
×
294

295
            config.record_result(*genprime_timer);
×
296
            config.record_result(*is_prime_timer);
×
297
         }
×
298
      }
×
299
};
300

301
BOTAN_REGISTER_PERF_TEST("random_prime", PerfTest_RandomPrime);
×
302

303
#endif
304

305
#if defined(BOTAN_HAS_DL_GROUP)
306

307
class PerfTest_ModExp final : public PerfTest {
×
308
   public:
309
      void go(const PerfConfig& config) override {
×
310
         for(const size_t group_bits : {1024, 1536, 2048, 3072, 4096, 6144, 8192}) {
×
311
            const std::string group_name = "modp/ietf/" + std::to_string(group_bits);
×
312
            auto group = Botan::DL_Group::from_name(group_name);
×
313

314
            const size_t e_bits = group.exponent_bits();
×
315
            const size_t f_bits = group_bits - 1;
×
316

317
            const Botan::BigInt random_e(config.rng(), e_bits);
×
318
            const Botan::BigInt random_f(config.rng(), f_bits);
×
319

320
            auto e_timer = config.make_timer(group_name + " short exp");
×
321
            auto f_timer = config.make_timer(group_name + "  full exp");
×
322

323
            while(f_timer->under(config.runtime())) {
×
324
               e_timer->run([&]() { group.power_g_p(random_e, e_bits); });
×
325
               f_timer->run([&]() { group.power_g_p(random_f, f_bits); });
×
326
            }
327

328
            config.record_result(*e_timer);
×
329
            config.record_result(*f_timer);
×
330
         }
×
331
      }
×
332
};
333

334
BOTAN_REGISTER_PERF_TEST("modexp", PerfTest_ModExp);
×
335

336
#endif
337

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