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

randombit / botan / 20579846577

29 Dec 2025 06:24PM UTC coverage: 90.415% (+0.2%) from 90.243%
20579846577

push

github

web-flow
Merge pull request #5167 from randombit/jack/src-size-reductions

Changes to reduce unnecessary inclusions

101523 of 112285 relevant lines covered (90.42%)

12817276.56 hits per line

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

94.78
/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
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
         const 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
         const 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
         const Botan::Montgomery_Params monty_n(n);
1✔
144

145
         const auto one = Botan::Montgomery_Int::one(monty_n);
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);
1✔
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_n_times(ws, 1);  // x = x^2
147,199✔
169
            x = x + one;
147,199✔
170

171
            t = y - x;
147,199✔
172

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

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

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

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

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

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

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

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

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

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

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

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

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

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

232
}  // namespace Botan_CLI
233

234
#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