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

randombit / botan / 23225340130

18 Mar 2026 01:53AM UTC coverage: 89.677% (-0.001%) from 89.678%
23225340130

push

github

web-flow
Merge pull request #5456 from randombit/jack/clang-tidy-22

Fix various warnings from clang-tidy 22

104438 of 116460 relevant lines covered (89.68%)

11819947.55 hits per line

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

95.65
/src/cli/math.cpp
1
/*
2
* (C) 2009,2010,2015 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "cli.h"
8

9
#if defined(BOTAN_HAS_NUMBERTHEORY)
10

11
   #include <botan/numthry.h>
12
   #include <botan/internal/mod_inv.h>
13
   #include <botan/internal/monty.h>
14
   #include <algorithm>
15

16
namespace Botan_CLI {
17

18
namespace {
19

20
class Modular_Inverse final : public Command {
21
   public:
22
      Modular_Inverse() : Command("mod_inverse n mod") {}
6 ✔
23

24
      std::string group() const override { return "numtheory"; }
1 ✔
25

26
      std::string description() const override { return "Calculates a modular inverse"; }
1 ✔
27

28
      void go() override {
2 ✔
29
         const Botan::BigInt n(get_arg("n"));
4 ✔
30
         const Botan::BigInt mod(get_arg("mod"));
4 ✔
31

32
         if(auto inv = Botan::inverse_mod_general(n, mod)) {
2 ✔
33
            output() << *inv << "\n";
1 ✔
34
         } else {
35
            output() << "No modular inverse exists\n";
1 ✔
36
         }
×
37
      }
2 ✔
38
};
39

40
BOTAN_REGISTER_COMMAND("mod_inverse", Modular_Inverse);
3 ✔
41

42
class Gen_Prime final : public Command {
43
   public:
44
      Gen_Prime() : Command("gen_prime --hex --count=1 bits") {}
8 ✔
45

46
      std::string group() const override { return "numtheory"; }
1 ✔
47

48
      std::string description() const override { return "Samples one or more primes"; }
1 ✔
49

50
      void go() override {
3 ✔
51
         const size_t bits = get_arg_sz("bits");
3 ✔
52
         const size_t cnt = get_arg_sz("count");
3 ✔
53
         const bool hex = flag_set("hex");
3 ✔
54

55
         for(size_t i = 0; i != cnt; ++i) {
6 ✔
56
            const Botan::BigInt p = Botan::random_prime(rng(), bits);
3 ✔
57

58
            if(hex) {
3 ✔
59
               output() << std::hex << p << "\n";
1 ✔
60
            } else {
61
               output() << p << "\n";
2 ✔
62
            }
63
         }
3 ✔
64
      }
3 ✔
65
};
66

67
BOTAN_REGISTER_COMMAND("gen_prime", Gen_Prime);
4 ✔
68

69
class Is_Prime final : public Command {
70
   public:
71
      Is_Prime() : Command("is_prime --prob=56 n") {}
8 ✔
72

73
      std::string group() const override { return "numtheory"; }
1 ✔
74

75
      std::string description() const override { return "Test if the integer n is composite or prime"; }
1 ✔
76

77
      void go() override {
3 ✔
78
         const Botan::BigInt n(get_arg("n"));
6 ✔
79
         const size_t prob = get_arg_sz("prob");
3 ✔
80
         const bool prime = Botan::is_prime(n, rng(), prob);
3 ✔
81

82
         output() << n << " is " << (prime ? "probably prime" : "composite") << "\n";
4 ✔
83
      }
3 ✔
84
};
85

86
BOTAN_REGISTER_COMMAND("is_prime", Is_Prime);
4 ✔
87

88
/*
89
* Factor integers using a combination of trial division by small
90
* primes, and Pollard's Rho algorithm
91
*/
92
class Factor final : public Command {
93
   public:
94
      Factor() : Command("factor n") {}
8 ✔
95

96
      std::string group() const override { return "numtheory"; }
1 ✔
97

98
      std::string description() const override { return "Factor a given integer"; }
1 ✔
99

100
      void go() override {
3 ✔
101
         const Botan::BigInt n(get_arg("n"));
6 ✔
102

103
         std::vector<Botan::BigInt> factors = factorize(n, rng());
3 ✔
104
         std::sort(factors.begin(), factors.end());
3 ✔
105

106
         output() << n << ":";
3 ✔
107
         for(const auto& factor : factors) {
8 ✔
108
            output() << " " << factor;
5 ✔
109
         }
110
         output() << "\n";
3 ✔
111
      }
3 ✔
112

113
   private:
114
      std::vector<Botan::BigInt> factorize(const Botan::BigInt& n_in, Botan::RandomNumberGenerator& rng) {
4 ✔
115
         Botan::BigInt n = n_in;
4 ✔
116
         std::vector<Botan::BigInt> factors = remove_small_factors(n);
4 ✔
117

118
         while(n != 1) {
5 ✔
119
            if(Botan::is_prime(n, rng)) {
4 ✔
120
               factors.push_back(n);
3 ✔
121
               break;
3 ✔
122
            }
123

124
            Botan::BigInt a_factor;
1 ✔
125
            while(a_factor == 0) {
2 ✔
126
               a_factor = rho(n, rng);
1 ✔
127
            }
128

129
            const auto rho_factored = factorize(a_factor, rng);
1 ✔
130
            for(const auto& factor : rho_factored) {
2 ✔
131
               factors.push_back(factor);
1 ✔
132
            }
133

134
            n /= a_factor;
1 ✔
135
         }
1 ✔
136

137
         return factors;
4 ✔
138
      }
4 ✔
139

140
      /*
141
      * Pollard's Rho algorithm, as described in the MIT algorithms book.
142
      * Uses Brent's cycle finding
143
      */
144
      static Botan::BigInt rho(const Botan::BigInt& n, Botan::RandomNumberGenerator& rng) {
1 ✔
145
         const Botan::Montgomery_Params monty_n(n);
1 ✔
146

147
         const auto one = Botan::Montgomery_Int::one(monty_n);
1 ✔
148

149
         const auto two = Botan::BigInt::from_s32(2);
1 ✔
150
         const auto three = Botan::BigInt::from_s32(3);
1 ✔
151
         Botan::Montgomery_Int x(monty_n, Botan::BigInt::random_integer(rng, two, n - three), false);
1 ✔
152
         Botan::Montgomery_Int y = x;
1 ✔
153
         Botan::Montgomery_Int z = one;
1 ✔
154
         Botan::Montgomery_Int t(monty_n);
1 ✔
155
         Botan::BigInt d;
1 ✔
156

157
         Botan::secure_vector<Botan::word> ws;
1 ✔
158

159
         size_t i = 1;
1 ✔
160
         size_t k = 2;
1 ✔
161

162
         while(true) {
147,199 ✔
163
            i++;
147,199 ✔
164

165
            if(i >= 0xFFFF0000)  // bad seed? too slow? bail out
147,199 ✔
166
            {
167
               break;
168
            }
169

170
            x.square_this_n_times(ws, 1);  // x = x^2
147,199 ✔
171
            x = x + one;
147,199 ✔
172

173
            t = y - x;
147,199 ✔
174

175
            z.mul_by(t, ws);
147,199 ✔
176

177
            if(i == k || i % 128 == 0) {
147,199 ✔
178
               d = Botan::gcd(z.value(), n);
1,156 ✔
179
               z = one;
1,156 ✔
180

181
               if(d == n) {
1,156 ✔
182
                  // TODO Should rewind here
183
                  break;
184
               }
185

186
               if(d != 1) {
1,156 ✔
187
                  return d;
1 ✔
188
               }
189
            }
190

191
            if(i == k) {
147,198 ✔
192
               y = x;
17 ✔
193
               k = 2 * k;
17 ✔
194
            }
195
         }
196

197
         // failed
198
         return Botan::BigInt::zero();
×
199
      }
2 ✔
200

201
      // Remove (and return) any small (< 2^16) factors
202
      static std::vector<Botan::BigInt> remove_small_factors(Botan::BigInt& n) {
4 ✔
203
         std::vector<Botan::BigInt> factors;
4 ✔
204

205
         while(n.is_even()) {
8 ✔
206
            factors.push_back(Botan::BigInt::from_s32(2));
×
207
            n >>= 1;
×
208
         }
209

210
         for(size_t j = 0; j != Botan::PRIME_TABLE_SIZE; j++) {
19,651 ✔
211
            auto prime = Botan::BigInt::from_s32(Botan::PRIMES[j]);
19,648 ✔
212
            if(n < prime) {
19,648 ✔
213
               break;
214
            }
215

216
            Botan::BigInt x = Botan::gcd(n, prime);
19,647 ✔
217

218
            if(x != 1) {
19,647 ✔
219
               n /= x;
2 ✔
220

221
               while(x != 1) {
4 ✔
222
                  x /= prime;
2 ✔
223
                  factors.push_back(prime);
2 ✔
224
               }
225
            }
226
         }
19,648 ✔
227

228
         return factors;
4 ✔
229
      }
×
230
};
231

232
BOTAN_REGISTER_COMMAND("factor", Factor);
4 ✔
233

234
}  // namespace
235

236
}  // namespace Botan_CLI
237

238
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc