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

randombit / botan / 13424504184

19 Feb 2025 11:37PM UTC coverage: 91.644% (+0.004%) from 91.64%
13424504184

push

github

web-flow
Merge pull request #4692 from randombit/jack/no-word4-unroll

Don't bother with loop blocking for carry chains with constant lengths

95002 of 103664 relevant lines covered (91.64%)

11426910.46 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/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/reducer.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 {
1✔
30
   public:
31
      void go(const PerfConfig& config) override {
1✔
32
         std::chrono::milliseconds runtime_per_size = config.runtime();
1✔
33

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

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

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

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

48
               x.mask_bits(bits);
9,092✔
49

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

134
               BOTAN_ASSERT_EQUAL(q1, q2, "Quotient ok");
703✔
135
               BOTAN_ASSERT_EQUAL(r1, r2, "Remainder ok");
1,415✔
136
            }
137

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

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

146
#endif
147

148
#if defined(BOTAN_HAS_NUMBERTHEORY)
149

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

310
#endif
311

312
#if defined(BOTAN_HAS_DL_GROUP)
313

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

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

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

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

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

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

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

343
#endif
344

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