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

randombit / botan / 16395825001

19 Jul 2025 11:30PM UTC coverage: 90.635% (-0.07%) from 90.708%
16395825001

push

github

web-flow
Merge pull request #4998 from randombit/jack/fix-clang-tidy-readability-isolate-declaration

Enable and fix clang-tidy warning readability-isolate-declaration

99940 of 110266 relevant lines covered (90.64%)

12341110.13 hits per line

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

94.83
/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 <iterator>
15

16
namespace Botan_CLI {
17

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

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

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

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

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

38
BOTAN_REGISTER_COMMAND("mod_inverse", Modular_Inverse);
3✔
39

40
class Gen_Prime final : public Command {
41
   public:
42
      Gen_Prime() : Command("gen_prime --hex --count=1 bits") {}
6✔
43

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

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

48
      void go() override {
2✔
49
         const size_t bits = get_arg_sz("bits");
2✔
50
         const size_t cnt = get_arg_sz("count");
2✔
51
         const bool hex = flag_set("hex");
2✔
52

53
         for(size_t i = 0; i != cnt; ++i) {
4✔
54
            const Botan::BigInt p = Botan::random_prime(rng(), bits);
2✔
55

56
            if(hex) {
2✔
57
               output() << "0x" << std::hex << p << "\n";
×
58
            } else {
59
               output() << p << "\n";
2✔
60
            }
61
         }
2✔
62
      }
2✔
63
};
64

65
BOTAN_REGISTER_COMMAND("gen_prime", Gen_Prime);
3✔
66

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

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

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

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

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

84
BOTAN_REGISTER_COMMAND("is_prime", Is_Prime);
4✔
85

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

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

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

98
      void go() override {
3✔
99
         Botan::BigInt n(get_arg("n"));
6✔
100

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

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

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

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

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

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

132
            n /= a_factor;
1✔
133
         }
1✔
134

135
         return factors;
4✔
136
      }
4✔
137

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

145
         const Botan::Montgomery_Int one(monty_n, monty_n->R1(), false);
1✔
146

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

155
         Botan::secure_vector<Botan::word> ws;
1✔
156

157
         size_t i = 1;
1✔
158
         size_t k = 2;
1✔
159

160
         while(true) {
147,199✔
161
            i++;
147,199✔
162

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

168
            x.square_this(ws);  // x = x^2
147,199✔
169
            x.add(one, ws);
147,199✔
170

171
            t = y;
147,199✔
172
            t.sub(x, ws);
147,199✔
173

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

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

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

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

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

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

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

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

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

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

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

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

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

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

233
}  // namespace Botan_CLI
234

235
#endif
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