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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

95.41
/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/monty.h>
13
   #include <iterator>
14

15
namespace Botan_CLI {
16

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

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

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

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

29
         output() << Botan::inverse_mod(n, mod) << "\n";
4✔
30
      }
4✔
31
};
32

33
BOTAN_REGISTER_COMMAND("mod_inverse", Modular_Inverse);
3✔
34

35
class Gen_Prime final : public Command {
36
   public:
37
      Gen_Prime() : Command("gen_prime --hex --count=1 bits") {}
6✔
38

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

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

43
      void go() override {
2✔
44
         const size_t bits = get_arg_sz("bits");
2✔
45
         const size_t cnt = get_arg_sz("count");
2✔
46
         const bool hex = flag_set("hex");
2✔
47

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

51
            if(hex)
2✔
52
               output() << "0x" << std::hex << p << "\n";
×
53
            else
54
               output() << p << "\n";
2✔
55
         }
2✔
56
      }
2✔
57
};
58

59
BOTAN_REGISTER_COMMAND("gen_prime", Gen_Prime);
3✔
60

61
class Is_Prime final : public Command {
62
   public:
63
      Is_Prime() : Command("is_prime --prob=56 n") {}
8✔
64

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

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

69
      void go() override {
3✔
70
         Botan::BigInt n(get_arg("n"));
6✔
71
         const size_t prob = get_arg_sz("prob");
3✔
72
         const bool prime = Botan::is_prime(n, rng(), prob);
3✔
73

74
         output() << n << " is " << (prime ? "probably prime" : "composite") << "\n";
4✔
75
      }
3✔
76
};
77

78
BOTAN_REGISTER_COMMAND("is_prime", Is_Prime);
4✔
79

80
/*
81
* Factor integers using a combination of trial division by small
82
* primes, and Pollard's Rho algorithm
83
*/
84
class Factor final : public Command {
85
   public:
86
      Factor() : Command("factor n") {}
8✔
87

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

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

92
      void go() override {
3✔
93
         Botan::BigInt n(get_arg("n"));
6✔
94

95
         std::vector<Botan::BigInt> factors = factorize(n, rng());
3✔
96
         std::sort(factors.begin(), factors.end());
3✔
97

98
         output() << n << ": ";
3✔
99
         std::copy(factors.begin(), factors.end(), std::ostream_iterator<Botan::BigInt>(output(), " "));
3✔
100
         output() << std::endl;
3✔
101
      }
6✔
102

103
   private:
104
      std::vector<Botan::BigInt> factorize(const Botan::BigInt& n_in, Botan::RandomNumberGenerator& rng) {
4✔
105
         Botan::BigInt n = n_in;
4✔
106
         std::vector<Botan::BigInt> factors = remove_small_factors(n);
4✔
107

108
         while(n != 1) {
5✔
109
            if(Botan::is_prime(n, rng)) {
4✔
110
               factors.push_back(n);
3✔
111
               break;
3✔
112
            }
113

114
            Botan::BigInt a_factor = 0;
1✔
115
            while(a_factor == 0) {
2✔
116
               a_factor = rho(n, rng);
1✔
117
            }
118

119
            const auto rho_factored = factorize(a_factor, rng);
1✔
120
            for(const auto& factor : rho_factored) {
2✔
121
               factors.push_back(factor);
1✔
122
            }
123

124
            n /= a_factor;
1✔
125
         }
2✔
126

127
         return factors;
4✔
128
      }
4✔
129

130
      /*
131
      * Pollard's Rho algorithm, as described in the MIT algorithms book.
132
      * Uses Brent's cycle finding
133
      */
134
      static Botan::BigInt rho(const Botan::BigInt& n, Botan::RandomNumberGenerator& rng) {
1✔
135
         auto monty_n = std::make_shared<Botan::Montgomery_Params>(n);
1✔
136

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

139
         Botan::Montgomery_Int x(monty_n, Botan::BigInt::random_integer(rng, 2, n - 3), false);
5✔
140
         Botan::Montgomery_Int y = x;
1✔
141
         Botan::Montgomery_Int z = one;
1✔
142
         Botan::Montgomery_Int t(monty_n);
1✔
143
         Botan::BigInt d;
1✔
144

145
         Botan::secure_vector<Botan::word> ws;
1✔
146

147
         size_t i = 1, k = 2;
1✔
148

149
         while(true) {
147,199✔
150
            i++;
147,199✔
151

152
            if(i >= 0xFFFF0000)  // bad seed? too slow? bail out
147,199✔
153
            {
154
               break;
155
            }
156

157
            x.square_this(ws);  // x = x^2
147,199✔
158
            x.add(one, ws);
147,199✔
159

160
            t = y;
147,199✔
161
            t.sub(x, ws);
147,199✔
162

163
            z.mul_by(t, ws);
147,199✔
164

165
            if(i == k || i % 128 == 0) {
147,199✔
166
               d = Botan::gcd(z.value(), n);
2,311✔
167
               z = one;
1,156✔
168

169
               if(d == n) {
1,156✔
170
                  // TODO Should rewind here
171
                  break;
172
               }
173

174
               if(d != 1)
1,156✔
175
                  return d;
1✔
176
            }
177

178
            if(i == k) {
147,198✔
179
               y = x;
17✔
180
               k = 2 * k;
17✔
181
            }
182
         }
183

184
         // failed
185
         return 0;
×
186
      }
2✔
187

188
      // Remove (and return) any small (< 2^16) factors
189
      static std::vector<Botan::BigInt> remove_small_factors(Botan::BigInt& n) {
4✔
190
         std::vector<Botan::BigInt> factors;
4✔
191

192
         while(n.is_even()) {
8✔
193
            factors.push_back(2);
×
194
            n /= 2;
×
195
         }
196

197
         for(size_t j = 0; j != Botan::PRIME_TABLE_SIZE; j++) {
19,651✔
198
            uint16_t prime = Botan::PRIMES[j];
19,648✔
199
            if(n < prime) {
19,648✔
200
               break;
201
            }
202

203
            Botan::BigInt x = Botan::gcd(n, prime);
19,647✔
204

205
            if(x != 1) {
19,647✔
206
               n /= x;
2✔
207

208
               while(x != 1) {
4✔
209
                  x /= prime;
2✔
210
                  factors.push_back(prime);
4✔
211
               }
212
            }
213
         }
19,647✔
214

215
         return factors;
4✔
216
      }
×
217
};
218

219
BOTAN_REGISTER_COMMAND("factor", Factor);
4✔
220

221
}
222

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

© 2025 Coveralls, Inc