@@ -16,9 +16,9 @@ static unsigned int s_floor_ilog2(int value)
16
16
mp_err mp_prime_is_prime (const mp_int * a , int t , bool * result )
17
17
{
18
18
mp_int b ;
19
- int ix ;
19
+ int ix , bits ;
20
20
bool res ;
21
- mp_err err ;
21
+ mp_err err = MP_OKAY ;
22
22
23
23
/* default to no */
24
24
* result = false;
@@ -59,11 +59,19 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
59
59
if ((err = s_mp_prime_is_divisible (a , & res )) != MP_OKAY ) {
60
60
return err ;
61
61
}
62
-
63
62
/* return if it was trivially divisible */
64
63
if (res ) {
65
64
return MP_OKAY ;
66
65
}
66
+ /* floor(log_2(a)) */
67
+ bits = mp_count_bits (a ) - 1 ;
68
+
69
+ /* If the whole prime table up to p = 1619 has been tested, than all
70
+ numbers below 1621^2 = 2,627,641 are prime now. log_2(1621^2) ~ 21.33 */
71
+ if (bits < 21 ) {
72
+ * result = true;
73
+ return MP_OKAY ;
74
+ }
67
75
68
76
/*
69
77
Run the Miller-Rabin test with base 2 for the BPSW test.
@@ -78,6 +86,15 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
78
86
if (!res ) {
79
87
goto LBL_B ;
80
88
}
89
+ /* If the whole prime table up to p = 1619 and the Miller-Rabin tests to base two
90
+ has been applied, than all numbers below 4,469,471 (~2^{22.1}) are prime now.
91
+ With 1659 SPSPs < 2^32 left */
92
+ #if ((defined S_MP_PRIME_IS_DIVISIBLE_C ) && (MP_PRIME_TAB_SIZE >= 256 ))
93
+ if (bits < 22 ) {
94
+ * result = true;
95
+ goto LBL_B ;
96
+ }
97
+ #endif
81
98
/*
82
99
Rumours have it that Mathematica does a second M-R test with base 3.
83
100
Other rumours have it that their strong L-S test is slightly different.
@@ -91,6 +108,15 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
91
108
goto LBL_B ;
92
109
}
93
110
111
+ /* If the whole prime table up to p = 1619 and the Miller-Rabin tests to bases
112
+ two and three have been applied, than all numbers below 11,541,307 (~2^{23.5}) are prime now.
113
+ With 89 SPSPs < 2^32 left */
114
+ #if ((defined S_MP_PRIME_IS_DIVISIBLE_C ) && (MP_PRIME_TAB_SIZE >= 256 ))
115
+ if (bits < 23 ) {
116
+ * result = true;
117
+ goto LBL_B ;
118
+ }
119
+ #endif
94
120
/*
95
121
* Both, the Frobenius-Underwood test and the the extra strong Lucas test are quite
96
122
* slow so if speed is an issue, define LTM_USE_ONLY_MR to use M-R tests with
@@ -125,7 +151,7 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
125
151
if (t == 0 ) {
126
152
#ifndef LTM_USE_ONLY_MR
127
153
/* The BPSW version as used here has no counter-example below 2^64 */
128
- if (mp_count_bits ( a ) < 64 ) {
154
+ if (bits < 64 ) {
129
155
* result = true;
130
156
goto LBL_B ;
131
157
}
@@ -142,9 +168,7 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
142
168
The caller has to check the maximum size.
143
169
*/
144
170
if (t < 0 ) {
145
- int p_max = 0 , bits ;
146
- bits = mp_count_bits (a );
147
-
171
+ int p_max = 0 ;
148
172
#ifndef LTM_USE_ONLY_MR
149
173
if (bits < 64 ) {
150
174
/* Just complete the BPSW test */
@@ -155,8 +179,8 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
155
179
goto LBL_B ;
156
180
}
157
181
#else
158
- /* Base 2 has been done already at this point */
159
-
182
+ /* Base 2 has been done already at this point. Also possible: { (2, 3,) 5, 13} to avoid waste */
183
+ /* 2, 7, 61 found by Gerhard Jaeschke 1993 */
160
184
mp_digit bases32 = {7u , 61u };
161
185
/* 2, 325, 9375, 28178, 450775, 9780504, 1795265022 found by Jim Sinclair 2011 */
162
186
uint32_t bases64 = {325ull , 9375ull , 28178ull , 450775ull , 9780504ull , 1795265022ull };
@@ -196,21 +220,27 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
196
220
Comparing bigints is not free, so give the magnitude of "n" a rough check
197
221
before spending computing time.
198
222
*/
199
- if (bits <= 78 ) {
223
+
224
+ else if ((bits >= 64 ) && (bits <= 78 )) {
200
225
/* 0x437ae92817f9fc85b7e5 = 318665857834031151167461 */
201
226
if ((err = mp_read_radix (& b , "437ae92817f9fc85b7e5" , 16 )) != MP_OKAY ) {
202
227
goto LBL_B ;
203
228
}
204
229
if (mp_cmp (a , & b ) == MP_LT ) {
205
230
p_max = 12 ;
231
+ } else {
232
+ p_max = 13 ;
206
233
}
207
- } else if ((bits > 78 ) && (bits < 82 )) {
234
+ } else if ((bits >= 78 ) && (bits <= 81 )) {
208
235
/* 0x2be6951adc5b22410a5fd = 3317044064679887385961981 */
209
236
if ((err = mp_read_radix (& b , "2be6951adc5b22410a5fd" , 16 )) != MP_OKAY ) {
210
237
goto LBL_B ;
211
238
}
212
239
if (mp_cmp (a , & b ) == MP_LT ) {
213
240
p_max = 13 ;
241
+ } else {
242
+ err = MP_VAL ;
243
+ goto LBL_B ;
214
244
}
215
245
} else {
216
246
err = MP_VAL ;
@@ -232,10 +262,9 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
232
262
Do "t" M-R tests with random bases between 3 and "a".
233
263
See Fips 186.4 p. 126ff
234
264
*/
235
- else if (t > 0 ) {
265
+ if (t > 0 ) {
236
266
unsigned int mask ;
237
267
int size_a ;
238
-
239
268
/*
240
269
* The mp_digit's have a defined bit-size but the size of the
241
270
* array a.dp is a simple 'int' and this library can not assume full
@@ -283,7 +312,6 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
283
312
for (ix = 0 ; ix < t ; ix ++ ) {
284
313
unsigned int fips_rand ;
285
314
int len ;
286
-
287
315
/* mp_rand() guarantees the first digit to be non-zero */
288
316
if ((err = mp_rand (& b , 1 )) != MP_OKAY ) {
289
317
goto LBL_B ;
0 commit comments