Skip to content

Commit ee15ef7

Browse files
committed
Overhaul of the prime-tests
- Removal of the Fermat test mp_prime_fermat - Replacement of the Strong Lucas-Selfridge test with the Extra Strong Lucas test with Robert Baillie's parameters P = 3 and Q = 1 - Additional tests to check the implementations of the Miller-Rabin and Extra Strong Lucas tests
1 parent 96f9edf commit ee15ef7

15 files changed

+341
-83
lines changed

demo/test.c

Lines changed: 163 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,165 @@ static int test_mp_prime_rand(void)
824824
return EXIT_FAILURE;
825825
}
826826

827+
/* Some small pseudoprimes to test the individual implementations */
828+
829+
/* Miller-Rabin base 2 */
830+
static const uint32_t SPSP_2[] = {
831+
2047, 3277, 4033, 4681, 8321, 15841, 29341, 42799,
832+
49141, 52633, 65281, 74665, 80581, 85489, 88357, 90751
833+
};
834+
835+
/* Miller-Rabin base 3 */
836+
static const uint32_t SPSP_3[] = {
837+
121, 703, 1891, 3281, 8401, 8911, 10585, 12403, 16531,
838+
18721, 19345, 23521, 31621, 44287, 47197, 55969, 63139,
839+
74593, 79003, 82513, 87913, 88573, 97567
840+
};
841+
842+
/* SPSP to all bases < 100 */
843+
/* still needs computing
844+
static const char *SPSP_2_100_LARGE[] = {
845+
"",
846+
"",
847+
"",
848+
"",
849+
""
850+
};
851+
*/
852+
853+
/* Extra strong Lucas test with Baillie's parameters Q = 1, P = 3 */
854+
static const uint32_t ESLPSP[] = {
855+
989, 3239, 5777, 10877, 27971, 29681, 30739, 31631, 39059, 72389,
856+
73919, 75077, 100127, 113573, 125249, 137549, 137801, 153931, 155819,
857+
161027, 162133, 189419, 218321, 231703, 249331, 370229, 429479, 430127,
858+
459191, 473891, 480689, 600059, 621781, 632249, 635627
859+
};
860+
861+
/*
862+
Almost extra strong Lucas test with Baillie's parameters Q = 1, P = 3
863+
Only those that are not in ESLPSP.
864+
*/
865+
static const uint32_t AESLPSP[] = {
866+
10469, 154697, 233659, 472453, 629693, 852389, 1091093, 1560437,
867+
1620673, 1813601, 1969109, 2415739, 2595329, 2756837, 3721549,
868+
4269341, 5192309, 7045433, 7226669, 7265561
869+
};
870+
871+
/* Some randomly choosen 200 decimal digits large primes (https://primes.utm.edu/lists/small/small2.html) */
872+
static const char *medium_primes[10] = {
873+
"C8Ckh0vviS3HUPdB1NSrSm+gOodw/f1aQ5+aaH1W6RMB0jVkO6lTaL54O3o7U5BSGUFGxm5gAvisbJamasuLZS8g3ZsJ2JM4Vtn9cQZRfkP6b8V",
874+
"64xDN9FqLBiovZ/9q/EPm0DONpIfn5MbJKHa+IjT0fjAzkg34FpAmad+CwhcpKaiTbZEpErut+DhpVyiQfqBFrgcGnGhhIrMF/XkyY3aVx6E96B",
875+
"8cyuMlENm0vh/eWwgHUpDKqmLyCSsRQZRWvbHpA2jHDZv1EhHkVhceg3OFRZn/aXRBnbdtsc2xO6sWh9KZ5Mo7u9rJgBJMVtDnu094MCExj1YvB",
876+
"BRFZFsYjSz45un8qptnuSqEsy9wV0BzbMpVAB1TrwImENOVIc1cASZNQ/mXG2xtazqgn/juVzFo91XLx9PtIlkcK0L2T6fBNgy8Lc7dSVoKQ+XP",
877+
"Ez/mDl+to2gm69+VdIHI9Q7vaO3DuIdLVT69myM3HYwVBE+G24KffAOUAp3FGrSOU+LtERMiIYIEtxPI7n/DRJtmL2i0+REwGpTMge2d2EpabfB",
878+
"5+Uz1gPFjZJ/nNdEOmOaMouJSGzygo42qz7xOwXn/moSUvBpPjo4twRGbK0+qaeU/RI8yYYxXr3OBP4w+/jgL3mN9GiENDM5LtEKMiQrZ9jIVEb",
879+
"AQ5nD1+G1grv41s/XlK+0YTGyZgr/88PzdQJ8QT9tavisTgyG6k8/80A4HQhnFndskHNAaB2EW5fE7KH3kk7m89s8JnVqkJyGZWSfs1+JlmHLPf",
880+
"3F19vPmM0Ih89KZ04Xmd62QB9F6E2sztT10A7Kcqc44eKvsNHh+JY6Z6gJXkbWg1Iw7xr29QAhEF/o1YAgfutQtpdzHkex06Yd71kPsaZdKXiC5",
881+
"2fIcJ1t/VYCColXGs+ji/txNMEXn2FXdowLzlo7QKqzAWHdAbwtltSO5qpSp3OUiEOGUUi3hbyw3iQRE8nFJaikJ89Wdox6vpPtIsc3QRjexMnv",
882+
"8aOicQ5gIbFCarFUgSgzh40LpuZ0jjK1u48/YT+C0h1dAQ8CIEgZjHZT+5/7cCRGmJlo+XCp7S41MSQ2ZNRSJh2texRYtvAXBAZfR8A8twl316P"
883+
};
884+
885+
#define ARR_LENGTH(a) ((int)(sizeof((a))/sizeof((a)[0])))
886+
887+
static int test_mp_prime_miller_rabin(void)
888+
{
889+
mp_int a, b, c;
890+
bool result;
891+
int i;
892+
DOR(mp_init_multi(&a, &b, &c, NULL));
893+
894+
/* SPSP to base 2 */
895+
mp_set(&b, 2u);
896+
for (i = 0; i < ARR_LENGTH(SPSP_2); i++) {
897+
result = false;
898+
mp_set_u32(&a, SPSP_2[i]);
899+
DO(mp_prime_miller_rabin(&a, &b, &result));
900+
EXPECT(result == true);
901+
}
902+
903+
/* Some larger primes to check for false negatives */
904+
for (i = 0; i < 10; i++) {
905+
result = false;
906+
DO(mp_read_radix(&a, medium_primes[i], 64));
907+
DO(mp_prime_miller_rabin(&a, &b, &result));
908+
EXPECT(result == true);
909+
}
910+
/* Some semi-primes */
911+
for (i = 0; i < 5; i += 2) {
912+
result = false;
913+
DO(mp_read_radix(&a, medium_primes[i], 64));
914+
DO(mp_read_radix(&c, medium_primes[i+1], 64));
915+
DO(mp_mul(&a, &c, &a));
916+
DO(mp_prime_miller_rabin(&a, &b, &result));
917+
EXPECT(result == false);
918+
}
919+
920+
/* SPSP to base 3 */
921+
mp_set(&b, 3u);
922+
for (i = 0; i < ARR_LENGTH(SPSP_3); i++) {
923+
result = false;
924+
mp_set_u32(&a, SPSP_3[i]);
925+
DO(mp_prime_miller_rabin(&a, &b, &result));
926+
EXPECT(result == true);
927+
}
928+
929+
mp_clear_multi(&a, &b, &c, NULL);
930+
return EXIT_SUCCESS;
931+
LBL_ERR:
932+
mp_clear_multi(&a, &b, &c, NULL);
933+
return EXIT_FAILURE;
934+
}
935+
936+
937+
static int test_mp_prime_extra_strong_lucas(void)
938+
{
939+
mp_int a, b;
940+
bool result;
941+
int i;
942+
943+
DOR(mp_init_multi(&a, &b, NULL));
944+
945+
/* Check Extra Strong pseudoprimes */
946+
for (i = 0; i < ARR_LENGTH(ESLPSP); i++) {
947+
result = false;
948+
mp_set_u32(&a, ESLPSP[i]);
949+
DO(mp_prime_extra_strong_lucas(&a, &result));
950+
EXPECT(result == true);
951+
}
952+
953+
/* Check Almost Extra Strong pseudoprimes (not in ESLPSP) */
954+
for (i = 0; i < ARR_LENGTH(AESLPSP); i++) {
955+
result = false;
956+
mp_set_u32(&a, AESLPSP[i]);
957+
DO(mp_prime_extra_strong_lucas(&a, &result));
958+
EXPECT(result == false);
959+
}
960+
961+
/* Some larger primes to check for false negatives */
962+
for (i = 0; i < 10; i++) {
963+
result = false;
964+
DO(mp_read_radix(&a, medium_primes[i], 64));
965+
DO(mp_prime_extra_strong_lucas(&a, &result));
966+
EXPECT(result == true);
967+
}
968+
969+
/* Some semi-primes */
970+
for (i = 0; i < 5; i++) {
971+
result = false;
972+
DO(mp_read_radix(&a, medium_primes[i], 64));
973+
DO(mp_read_radix(&a, medium_primes[i+1], 64));
974+
DO(mp_mul(&a, &b, &a));
975+
DO(mp_prime_extra_strong_lucas(&a, &result));
976+
EXPECT(result == false);
977+
}
978+
979+
mp_clear_multi(&a, &b, NULL);
980+
return EXIT_SUCCESS;
981+
LBL_ERR:
982+
mp_clear_multi(&a, &b, NULL);
983+
return EXIT_FAILURE;
984+
}
985+
827986
static int test_mp_prime_is_prime(void)
828987
{
829988
int ix;
@@ -893,7 +1052,7 @@ static int test_mp_prime_is_prime(void)
8931052
DO(mp_read_radix(&a,
8941053
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF",
8951054
16));
896-
DO(mp_prime_strong_lucas_selfridge(&a, &cnt));
1055+
DO(mp_prime_extra_strong_lucas(&a, &cnt));
8971056
/* large problem */
8981057
EXPECT(cnt);
8991058
if ((e != MP_OKAY) || !cnt) {
@@ -1358,7 +1517,7 @@ static int test_mp_log_n(void)
13581517
mp_int a;
13591518
mp_digit d;
13601519
int base, lb, size;
1361-
const int max_base = MP_MIN(INT_MAX, MP_DIGIT_MAX);
1520+
const mp_digit max_base = MP_MIN(INT_MAX, MP_DIGIT_MAX);
13621521

13631522
DOR(mp_init(&a));
13641523

@@ -2236,6 +2395,8 @@ static int unit_tests(int argc, char **argv)
22362395
T1(mp_montgomery_reduce, MP_MONTGOMERY_REDUCE),
22372396
T1(mp_root_n, MP_ROOT_N),
22382397
T1(mp_or, MP_OR),
2398+
T1(mp_prime_extra_strong_lucas, MP_PRIME_EXTRA_STRONG_LUCAS),
2399+
T1(mp_prime_miller_rabin, MP_PRIME_MILLER_RABIN),
22392400
T1(mp_prime_is_prime, MP_PRIME_IS_PRIME),
22402401
T1(mp_prime_next_prime, MP_PRIME_NEXT_PRIME),
22412402
T1(mp_prime_rand, MP_PRIME_RAND),

doc/bn.tex

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,15 +2014,6 @@ \subsection{Example}
20142014

20152015
\chapter{Prime Numbers}
20162016

2017-
\section{Fermat Test}
2018-
\index{mp\_prime\_fermat}
2019-
\begin{alltt}
2020-
mp_err mp_prime_fermat (const mp_int *a, const mp_int *b, int *result)
2021-
\end{alltt}
2022-
Performs a Fermat primality test to the base $b$. That is it computes $b^a \mbox{ mod }a$ and
2023-
tests whether the value is equal to $b$ or not. If the values are equal then $a$ is probably prime
2024-
and $result$ is set to one. Otherwise $result$ is set to zero.
2025-
20262017
\section{Miller--Rabin Test}
20272018
\index{mp\_prime\_miller\_rabin}
20282019
\begin{alltt}
@@ -2032,9 +2023,6 @@ \section{Miller--Rabin Test}
20322023
test and is very hard to fool (besides with Carmichael numbers). If $a$ passes the test (therefore
20332024
is probably prime) $result$ is set to one. Otherwise $result$ is set to zero.
20342025

2035-
Note that it is suggested that you use the Miller--Rabin test instead of the Fermat test since all
2036-
of the failures of Miller--Rabin are a subset of the failures of the Fermat test.
2037-
20382026
\subsection{Required Number of Tests}
20392027
Generally to ensure a number is very likely to be prime you have to perform the Miller--Rabin with
20402028
at least a half--dozen or so unique bases. However, it has been proven that the probability of
@@ -2229,12 +2217,12 @@ \subsection{Required Number of Tests}
22292217

22302218
See also table C.1 in FIPS 186-4.
22312219

2232-
\section{Strong Lucas--Selfridge Test}
2233-
\index{mp\_prime\_strong\_lucas\_selfridge}
2220+
\section{Extra Strong Lucas Test}
2221+
\index{mp\_prime\_extra\_strong\_lucas}
22342222
\begin{alltt}
2235-
mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, bool *result)
2223+
mp_err mp_prime_extra_strong_lucas(const mp_int *a, bool *result)
22362224
\end{alltt}
2237-
Performs a strong Lucas--Selfridge test. The strong Lucas--Selfridge test together with the
2225+
Performs a extra strong Lucas test. The extra strong Lucas test together with the
22382226
Rabin--Miller test with bases $2$ and $3$ resemble the BPSW test. The single internal use is a
22392227
compile--time option in \texttt{mp\_prime\_is\_prime} and can be excluded from the Libtommath build
22402228
if not needed.
@@ -2246,8 +2234,7 @@ \section{Frobenius (Underwood) Test}
22462234
\end{alltt}
22472235
Performs the variant of the Frobenius test as described by Paul Underwood. It can be included at
22482236
build--time if the preprocessor macro \texttt{LTM\_USE\_FROBENIUS\_TEST} is defined and will be
2249-
used
2250-
instead of the Lucas--Selfridge test.
2237+
used instead of the extra strong Lucas test.
22512238

22522239
It returns \texttt{MP\_ITER} if the number of iterations is exhausted, assumes a composite as the
22532240
input and sets \texttt{result} accordingly. This will reduce the set of available pseudoprimes by a

libtommath_VS2008.vcproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@
609609
>
610610
</File>
611611
<File
612-
RelativePath="mp_prime_fermat.c"
612+
RelativePath="mp_prime_extra_strong_lucas.c"
613613
>
614614
</File>
615615
<File

makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ mp_get_mag_u64.o mp_get_mag_ul.o mp_grow.o mp_hash.o mp_init.o mp_init_copy.o mp
3535
mp_init_l.o mp_init_multi.o mp_init_set.o mp_init_size.o mp_init_u32.o mp_init_u64.o mp_init_ul.o \
3636
mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o mp_log_n.o mp_lshd.o mp_mod.o mp_mod_2d.o \
3737
mp_montgomery_calc_normalization.o mp_montgomery_reduce.o mp_montgomery_setup.o mp_mul.o mp_mul_2.o \
38-
mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_fermat.o \
38+
mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_extra_strong_lucas.o \
3939
mp_prime_frobenius_underwood.o mp_prime_is_prime.o mp_prime_miller_rabin.o mp_prime_next_prime.o \
4040
mp_prime_rabin_miller_trials.o mp_prime_rand.o mp_prime_strong_lucas_selfridge.o mp_radix_size.o \
4141
mp_radix_size_overestimate.o mp_rand.o mp_rand_source.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o \

makefile.mingw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ mp_get_mag_u64.o mp_get_mag_ul.o mp_grow.o mp_hash.o mp_init.o mp_init_copy.o mp
3737
mp_init_l.o mp_init_multi.o mp_init_set.o mp_init_size.o mp_init_u32.o mp_init_u64.o mp_init_ul.o \
3838
mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o mp_log_n.o mp_lshd.o mp_mod.o mp_mod_2d.o \
3939
mp_montgomery_calc_normalization.o mp_montgomery_reduce.o mp_montgomery_setup.o mp_mul.o mp_mul_2.o \
40-
mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_fermat.o \
40+
mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_extra_strong_lucas.o \
4141
mp_prime_frobenius_underwood.o mp_prime_is_prime.o mp_prime_miller_rabin.o mp_prime_next_prime.o \
4242
mp_prime_rabin_miller_trials.o mp_prime_rand.o mp_prime_strong_lucas_selfridge.o mp_radix_size.o \
4343
mp_radix_size_overestimate.o mp_rand.o mp_rand_source.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o \

makefile.msvc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ mp_get_mag_u64.obj mp_get_mag_ul.obj mp_grow.obj mp_hash.obj mp_init.obj mp_init
3333
mp_init_l.obj mp_init_multi.obj mp_init_set.obj mp_init_size.obj mp_init_u32.obj mp_init_u64.obj mp_init_ul.obj \
3434
mp_invmod.obj mp_is_square.obj mp_kronecker.obj mp_lcm.obj mp_log_n.obj mp_lshd.obj mp_mod.obj mp_mod_2d.obj \
3535
mp_montgomery_calc_normalization.obj mp_montgomery_reduce.obj mp_montgomery_setup.obj mp_mul.obj mp_mul_2.obj \
36-
mp_mul_2d.obj mp_mul_d.obj mp_mulmod.obj mp_neg.obj mp_or.obj mp_pack.obj mp_pack_count.obj mp_prime_fermat.obj \
36+
mp_mul_2d.obj mp_mul_d.obj mp_mulmod.obj mp_neg.obj mp_or.obj mp_pack.obj mp_pack_count.obj mp_prime_extra_strong_lucas.obj \
3737
mp_prime_frobenius_underwood.obj mp_prime_is_prime.obj mp_prime_miller_rabin.obj mp_prime_next_prime.obj \
3838
mp_prime_rabin_miller_trials.obj mp_prime_rand.obj mp_prime_strong_lucas_selfridge.obj mp_radix_size.obj \
3939
mp_radix_size_overestimate.obj mp_rand.obj mp_rand_source.obj mp_read_radix.obj mp_reduce.obj mp_reduce_2k.obj \

makefile.shared

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mp_get_mag_u64.o mp_get_mag_ul.o mp_grow.o mp_hash.o mp_init.o mp_init_copy.o mp
3232
mp_init_l.o mp_init_multi.o mp_init_set.o mp_init_size.o mp_init_u32.o mp_init_u64.o mp_init_ul.o \
3333
mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o mp_log_n.o mp_lshd.o mp_mod.o mp_mod_2d.o \
3434
mp_montgomery_calc_normalization.o mp_montgomery_reduce.o mp_montgomery_setup.o mp_mul.o mp_mul_2.o \
35-
mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_fermat.o \
35+
mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_extra_strong_lucas.o \
3636
mp_prime_frobenius_underwood.o mp_prime_is_prime.o mp_prime_miller_rabin.o mp_prime_next_prime.o \
3737
mp_prime_rabin_miller_trials.o mp_prime_rand.o mp_prime_strong_lucas_selfridge.o mp_radix_size.o \
3838
mp_radix_size_overestimate.o mp_rand.o mp_rand_source.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o \

makefile.unix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ mp_get_mag_u64.o mp_get_mag_ul.o mp_grow.o mp_hash.o mp_init.o mp_init_copy.o mp
3838
mp_init_l.o mp_init_multi.o mp_init_set.o mp_init_size.o mp_init_u32.o mp_init_u64.o mp_init_ul.o \
3939
mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o mp_log_n.o mp_lshd.o mp_mod.o mp_mod_2d.o \
4040
mp_montgomery_calc_normalization.o mp_montgomery_reduce.o mp_montgomery_setup.o mp_mul.o mp_mul_2.o \
41-
mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_fermat.o \
41+
mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_extra_strong_lucas.o \
4242
mp_prime_frobenius_underwood.o mp_prime_is_prime.o mp_prime_miller_rabin.o mp_prime_next_prime.o \
4343
mp_prime_rabin_miller_trials.o mp_prime_rand.o mp_prime_strong_lucas_selfridge.o mp_radix_size.o \
4444
mp_radix_size_overestimate.o mp_rand.o mp_rand_source.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o \

0 commit comments

Comments
 (0)