Skip to content

Commit 6fbbc0e

Browse files
committed
Further refinement of the deterministic part of prime_is_prime
1 parent e1e24c6 commit 6fbbc0e

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

mp_prime_is_prime.c

+42-14
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ static unsigned int s_floor_ilog2(int value)
1616
mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
1717
{
1818
mp_int b;
19-
int ix;
19+
int ix, bits;
2020
bool res;
21-
mp_err err;
21+
mp_err err = MP_OKAY;
2222

2323
/* default to no */
2424
*result = false;
@@ -59,11 +59,19 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
5959
if ((err = s_mp_prime_is_divisible(a, &res)) != MP_OKAY) {
6060
return err;
6161
}
62-
6362
/* return if it was trivially divisible */
6463
if (res) {
6564
return MP_OKAY;
6665
}
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+
}
6775

6876
/*
6977
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)
7886
if (!res) {
7987
goto LBL_B;
8088
}
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
8198
/*
8299
Rumours have it that Mathematica does a second M-R test with base 3.
83100
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)
91108
goto LBL_B;
92109
}
93110

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
94120
/*
95121
* Both, the Frobenius-Underwood test and the the extra strong Lucas test are quite
96122
* 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)
125151
if (t == 0) {
126152
#ifndef LTM_USE_ONLY_MR
127153
/* The BPSW version as used here has no counter-example below 2^64 */
128-
if (mp_count_bits(a) < 64) {
154+
if (bits < 64) {
129155
*result = true;
130156
goto LBL_B;
131157
}
@@ -142,9 +168,7 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
142168
The caller has to check the maximum size.
143169
*/
144170
if (t < 0) {
145-
int p_max = 0, bits;
146-
bits = mp_count_bits(a);
147-
171+
int p_max = 0;
148172
#ifndef LTM_USE_ONLY_MR
149173
if (bits < 64) {
150174
/* Just complete the BPSW test */
@@ -155,8 +179,8 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
155179
goto LBL_B;
156180
}
157181
#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 */
160184
mp_digit bases32 = {7u, 61u};
161185
/* 2, 325, 9375, 28178, 450775, 9780504, 1795265022 found by Jim Sinclair 2011 */
162186
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)
196220
Comparing bigints is not free, so give the magnitude of "n" a rough check
197221
before spending computing time.
198222
*/
199-
if (bits <= 78) {
223+
224+
else if ((bits >= 64) && (bits <= 78)) {
200225
/* 0x437ae92817f9fc85b7e5 = 318665857834031151167461 */
201226
if ((err = mp_read_radix(&b, "437ae92817f9fc85b7e5", 16)) != MP_OKAY) {
202227
goto LBL_B;
203228
}
204229
if (mp_cmp(a, &b) == MP_LT) {
205230
p_max = 12;
231+
} else {
232+
p_max = 13;
206233
}
207-
} else if ((bits > 78) && (bits < 82)) {
234+
} else if ((bits >= 78) && (bits <= 81)) {
208235
/* 0x2be6951adc5b22410a5fd = 3317044064679887385961981 */
209236
if ((err = mp_read_radix(&b, "2be6951adc5b22410a5fd", 16)) != MP_OKAY) {
210237
goto LBL_B;
211238
}
212239
if (mp_cmp(a, &b) == MP_LT) {
213240
p_max = 13;
241+
} else {
242+
err = MP_VAL;
243+
goto LBL_B;
214244
}
215245
} else {
216246
err = MP_VAL;
@@ -232,10 +262,9 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
232262
Do "t" M-R tests with random bases between 3 and "a".
233263
See Fips 186.4 p. 126ff
234264
*/
235-
else if (t > 0) {
265+
if (t > 0) {
236266
unsigned int mask;
237267
int size_a;
238-
239268
/*
240269
* The mp_digit's have a defined bit-size but the size of the
241270
* 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)
283312
for (ix = 0; ix < t; ix++) {
284313
unsigned int fips_rand;
285314
int len;
286-
287315
/* mp_rand() guarantees the first digit to be non-zero */
288316
if ((err = mp_rand(&b, 1)) != MP_OKAY) {
289317
goto LBL_B;

0 commit comments

Comments
 (0)