1 #include "tommath_private.h"
2 #ifdef MP_PRIME_IS_PRIME_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* portable integer log of two with small footprint */
7 static unsigned int s_floor_ilog2(int value
)
10 while ((value
>>= 1) != 0) {
16 mp_err
mp_prime_is_prime(const mp_int
*a
, int t
, bool *result
)
29 if ((a
->dp
[0] == 0u) || (a
->dp
[0] == 1u)) {
43 /* N is not a perfect square: floor(sqrt(N))^2 != N */
44 if ((err
= mp_is_square(a
, &res
)) != MP_OKAY
) {
51 /* is the input equal to one of the primes in the table? */
52 for (ix
= 0; ix
< MP_PRIME_TAB_SIZE
; ix
++) {
53 if (mp_cmp_d(a
, s_mp_prime_tab
[ix
]) == MP_EQ
) {
58 /* first perform trial division */
59 if ((err
= s_mp_prime_is_divisible(a
, &res
)) != MP_OKAY
) {
63 /* return if it was trivially divisible */
69 Run the Miller-Rabin test with base 2 for the BPSW test.
71 if ((err
= mp_init_set(&b
, 2uL)) != MP_OKAY
) {
75 if ((err
= mp_prime_miller_rabin(a
, &b
, &res
)) != MP_OKAY
) {
82 Rumours have it that Mathematica does a second M-R test with base 3.
83 Other rumours have it that their strong L-S test is slightly different.
84 It does not hurt, though, beside a bit of extra runtime.
87 if ((err
= mp_prime_miller_rabin(a
, &b
, &res
)) != MP_OKAY
) {
95 * Both, the Frobenius-Underwood test and the the Lucas-Selfridge test are quite
96 * slow so if speed is an issue, define LTM_USE_ONLY_MR to use M-R tests with
97 * bases 2, 3 and t random bases.
99 #ifndef LTM_USE_ONLY_MR
101 #ifdef LTM_USE_FROBENIUS_TEST
102 err
= mp_prime_frobenius_underwood(a
, &res
);
103 if ((err
!= MP_OKAY
) && (err
!= MP_ITER
)) {
110 if ((err
= mp_prime_strong_lucas_selfridge(a
, &res
)) != MP_OKAY
) {
120 /* run at least one Miller-Rabin test with a random base */
126 Only recommended if the input range is known to be < 3317044064679887385961981
128 It uses the bases necessary for a deterministic M-R test if the input is
129 smaller than 3317044064679887385961981
130 The caller has to check the size.
131 TODO: can be made a bit finer grained but comparing is not free.
137 Sorenson, Jonathan; Webster, Jonathan (2015).
138 "Strong Pseudoprimes to Twelve Prime Bases".
140 /* 0x437ae92817f9fc85b7e5 = 318665857834031151167461 */
141 if ((err
= mp_read_radix(&b
, "437ae92817f9fc85b7e5", 16)) != MP_OKAY
) {
145 if (mp_cmp(a
, &b
) == MP_LT
) {
148 /* 0x2be6951adc5b22410a5fd = 3317044064679887385961981 */
149 if ((err
= mp_read_radix(&b
, "2be6951adc5b22410a5fd", 16)) != MP_OKAY
) {
153 if (mp_cmp(a
, &b
) == MP_LT
) {
161 /* we did bases 2 and 3 already, skip them */
162 for (ix
= 2; ix
< p_max
; ix
++) {
163 mp_set(&b
, s_mp_prime_tab
[ix
]);
164 if ((err
= mp_prime_miller_rabin(a
, &b
, &res
)) != MP_OKAY
) {
173 Do "t" M-R tests with random bases between 3 and "a".
174 See Fips 186.4 p. 126ff
181 * The mp_digit's have a defined bit-size but the size of the
182 * array a.dp is a simple 'int' and this library can not assume full
183 * compliance to the current C-standard (ISO/IEC 9899:2011) because
184 * it gets used for small embedded processors, too. Some of those MCUs
185 * have compilers that one cannot call standard compliant by any means.
186 * Hence the ugly type-fiddling in the following code.
188 size_a
= mp_count_bits(a
);
189 mask
= (1u << s_floor_ilog2(size_a
)) - 1u;
191 Assuming the General Rieman hypothesis (never thought to write that in a
192 comment) the upper bound can be lowered to 2*(log a)^2.
193 E. Bach, "Explicit bounds for primality testing and related problems,"
194 Math. Comp. 55 (1990), 355-380.
196 size_a = (size_a/10) * 7;
197 len = 2 * (size_a * size_a);
199 E.g.: a number of size 2^2048 would be reduced to the upper limit
201 floor(2048/10)*7 = 1428
204 (would have been ~4030331.9962 with floats and natural log instead)
205 That number is smaller than 2^28, the default bit-size of mp_digit.
209 How many tests, you might ask? Dana Jacobsen of Math::Prime::Util fame
210 does exactly 1. In words: one. Look at the end of _GMP_is_prime() in
211 Math-Prime-Util-GMP-0.50/primality.c if you do not believe it.
213 The function mp_rand() goes to some length to use a cryptographically
214 good PRNG. That also means that the chance to always get the same base
215 in the loop is non-zero, although very low.
216 If the BPSW test and/or the additional Frobenious test have been
217 performed instead of just the Miller-Rabin test with the bases 2 and 3,
218 a single extra test should suffice, so such a very unlikely event
219 will not do much harm.
221 To preemptively answer the dangling question: no, a witness does not
224 for (ix
= 0; ix
< t
; ix
++) {
225 unsigned int fips_rand
;
228 /* mp_rand() guarantees the first digit to be non-zero */
229 if ((err
= mp_rand(&b
, 1)) != MP_OKAY
) {
233 * Reduce digit before casting because mp_digit might be bigger than
234 * an unsigned int and "mask" on the other side is most probably not.
236 fips_rand
= (unsigned int)(b
.dp
[0] & (mp_digit
) mask
);
237 if (fips_rand
> (unsigned int)(INT_MAX
- MP_DIGIT_BIT
)) {
238 len
= INT_MAX
/ MP_DIGIT_BIT
;
240 len
= (((int)fips_rand
+ MP_DIGIT_BIT
) / MP_DIGIT_BIT
);
247 if ((err
= mp_rand(&b
, len
)) != MP_OKAY
) {
251 * That number might got too big and the witness has to be
254 len
= mp_count_bits(&b
);
256 len
= (len
- size_a
) + 1;
257 if ((err
= mp_div_2d(&b
, len
, &b
, NULL
)) != MP_OKAY
) {
261 /* Although the chance for b <= 3 is miniscule, try again. */
262 if (mp_cmp_d(&b
, 3uL) != MP_GT
) {
266 if ((err
= mp_prime_miller_rabin(a
, &b
, &res
)) != MP_OKAY
) {
275 /* passed the test */