Coverage Report

Created: 2024-11-21 06:38

/src/botan/src/lib/math/numbertheory/primality.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 2016,2018 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#include <botan/internal/primality.h>
8
9
#include <botan/bigint.h>
10
#include <botan/reducer.h>
11
#include <botan/rng.h>
12
#include <botan/internal/monty.h>
13
#include <botan/internal/monty_exp.h>
14
#include <algorithm>
15
16
namespace Botan {
17
18
171
bool is_lucas_probable_prime(const BigInt& C, const Modular_Reducer& mod_C) {
19
171
   if(C == 2 || C == 3 || C == 5 || C == 7 || C == 11 || C == 13) {
20
0
      return true;
21
0
   }
22
23
171
   if(C <= 1 || C.is_even()) {
24
0
      return false;
25
0
   }
26
27
171
   BigInt D = BigInt::from_word(5);
28
29
234
   for(;;) {
30
234
      int32_t j = jacobi(D, C);
31
234
      if(j == 0) {
32
0
         return false;
33
0
      }
34
35
234
      if(j == -1) {
36
171
         break;
37
171
      }
38
39
      // Check 5, -7, 9, -11, 13, -15, 17, ...
40
63
      if(D.is_negative()) {
41
21
         D.flip_sign();
42
21
         D += 2;
43
42
      } else {
44
42
         D += 2;
45
42
         D.flip_sign();
46
42
      }
47
48
63
      if(D == 17 && is_perfect_square(C).is_nonzero()) {
49
0
         return false;
50
0
      }
51
63
   }
52
53
171
   const BigInt K = C + 1;
54
171
   const size_t K_bits = K.bits() - 1;
55
56
171
   BigInt U = BigInt::one();
57
171
   BigInt V = BigInt::one();
58
59
171
   BigInt Ut, Vt, U2, V2;
60
61
19.9k
   for(size_t i = 0; i != K_bits; ++i) {
62
19.7k
      const bool k_bit = K.get_bit(K_bits - 1 - i);
63
64
19.7k
      Ut = mod_C.multiply(U, V);
65
66
19.7k
      Vt = mod_C.reduce(mod_C.square(V) + mod_C.multiply(D, mod_C.square(U)));
67
19.7k
      Vt.ct_cond_add(Vt.is_odd(), C);
68
19.7k
      Vt >>= 1;
69
19.7k
      Vt = mod_C.reduce(Vt);
70
71
19.7k
      U = Ut;
72
19.7k
      V = Vt;
73
74
19.7k
      U2 = mod_C.reduce(Ut + Vt);
75
19.7k
      U2.ct_cond_add(U2.is_odd(), C);
76
19.7k
      U2 >>= 1;
77
78
19.7k
      V2 = mod_C.reduce(Vt + Ut * D);
79
19.7k
      V2.ct_cond_add(V2.is_odd(), C);
80
19.7k
      V2 >>= 1;
81
82
19.7k
      U.ct_cond_assign(k_bit, U2);
83
19.7k
      V.ct_cond_assign(k_bit, V2);
84
19.7k
   }
85
86
171
   return (U == 0);
87
171
}
88
89
0
bool is_bailie_psw_probable_prime(const BigInt& n, const Modular_Reducer& mod_n) {
90
0
   if(n == 2) {
91
0
      return true;
92
0
   } else if(n <= 1 || n.is_even()) {
93
0
      return false;
94
0
   }
95
96
0
   auto monty_n = std::make_shared<Montgomery_Params>(n, mod_n);
97
0
   const auto base = BigInt::from_word(2);
98
0
   return passes_miller_rabin_test(n, mod_n, monty_n, base) && is_lucas_probable_prime(n, mod_n);
99
0
}
100
101
0
bool is_bailie_psw_probable_prime(const BigInt& n) {
102
0
   Modular_Reducer mod_n(n);
103
0
   return is_bailie_psw_probable_prime(n, mod_n);
104
0
}
105
106
bool passes_miller_rabin_test(const BigInt& n,
107
                              const Modular_Reducer& mod_n,
108
                              const std::shared_ptr<Montgomery_Params>& monty_n,
109
15.0k
                              const BigInt& a) {
110
15.0k
   if(n < 3 || n.is_even()) {
111
0
      return false;
112
0
   }
113
114
15.0k
   BOTAN_ASSERT_NOMSG(n > 1);
115
116
15.0k
   const BigInt n_minus_1 = n - 1;
117
15.0k
   const size_t s = low_zero_bits(n_minus_1);
118
15.0k
   const BigInt nm1_s = n_minus_1 >> s;
119
15.0k
   const size_t n_bits = n.bits();
120
121
15.0k
   const size_t powm_window = 4;
122
123
15.0k
   auto powm_a_n = monty_precompute(monty_n, a, powm_window);
124
125
15.0k
   BigInt y = monty_execute(*powm_a_n, nm1_s, n_bits);
126
127
15.0k
   if(y == 1 || y == n_minus_1) {
128
13.1k
      return true;
129
13.1k
   }
130
131
1.80k
   for(size_t i = 1; i != s; ++i) {
132
1.80k
      y = mod_n.square(y);
133
134
1.80k
      if(y == 1) {  // found a non-trivial square root
135
0
         return false;
136
0
      }
137
138
      /*
139
      -1 is the trivial square root of unity, so ``a`` is not a
140
      witness for this number - give up
141
      */
142
1.80k
      if(y == n_minus_1) {
143
1.80k
         return true;
144
1.80k
      }
145
1.80k
   }
146
147
0
   return false;
148
1.80k
}
149
150
bool is_miller_rabin_probable_prime(const BigInt& n,
151
                                    const Modular_Reducer& mod_n,
152
                                    RandomNumberGenerator& rng,
153
344
                                    size_t test_iterations) {
154
344
   if(n < 3 || n.is_even()) {
155
0
      return false;
156
0
   }
157
158
344
   auto monty_n = std::make_shared<Montgomery_Params>(n, mod_n);
159
160
15.5k
   for(size_t i = 0; i != test_iterations; ++i) {
161
15.1k
      const BigInt a = BigInt::random_integer(rng, BigInt::from_word(2), n);
162
163
15.1k
      if(!passes_miller_rabin_test(n, mod_n, monty_n, a)) {
164
0
         return false;
165
0
      }
166
15.1k
   }
167
168
   // Failed to find a counterexample
169
344
   return true;
170
344
}
171
172
344
size_t miller_rabin_test_iterations(size_t n_bits, size_t prob, bool random) {
173
344
   const size_t base = (prob + 2) / 2;  // worst case 4^-t error rate
174
175
   /*
176
   * If the candidate prime was maliciously constructed, we can't rely
177
   * on arguments based on p being random.
178
   */
179
344
   if(random == false) {
180
344
      return base;
181
344
   }
182
183
   /*
184
   * For randomly chosen numbers we can use the estimates from
185
   * http://www.math.dartmouth.edu/~carlp/PDF/paper88.pdf
186
   *
187
   * These values are derived from the inequality for p(k,t) given on
188
   * the second page.
189
   */
190
0
   if(prob <= 128) {
191
0
      if(n_bits >= 1536) {
192
0
         return 4;  // < 2^-133
193
0
      }
194
0
      if(n_bits >= 1024) {
195
0
         return 6;  // < 2^-133
196
0
      }
197
0
      if(n_bits >= 512) {
198
0
         return 12;  // < 2^-129
199
0
      }
200
0
      if(n_bits >= 256) {
201
0
         return 29;  // < 2^-128
202
0
      }
203
0
   }
204
205
   /*
206
   If the user desires a smaller error probability than we have
207
   precomputed error estimates for, just fall back to using the worst
208
   case error rate.
209
   */
210
0
   return base;
211
0
}
212
213
}  // namespace Botan