Skip to content

Commit 3f65f51

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 - Finer grained early-outs - All determistic tests < 2^64 emprically verified - Additional tests to check the implementations of the Miller-Rabin and Extra Strong Lucas tests - Addition of tests of the LTM_USE_ONLY_MR to the CI - Documentation update
1 parent c6a00c2 commit 3f65f51

16 files changed

+748
-141
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ jobs:
4444
# Run always with valgrind (no sanitizer, but debug info)
4545
- { BUILDOPTIONS: '--with-cc=gcc --with-m64 --with-valgrind', SANITIZER: '', COMPILE_DEBUG: '1', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
4646
# Alternative big-int version of mp_log(_n)
47-
- { BUILDOPTIONS: '--with-cc=gcc --with-m64 --cflags=-DS_MP_WORD_TOO_SMALL_C="" --with-valgrind', SANITIZER: '', COMPILE_DEBUG: '1', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
47+
- { BUILDOPTIONS: '--with-cc=gcc --with-m64 --cflags=-DS_MP_WORD_TOO_SMALL_C="" --cflags=-DLTM_USE_ONLY_MR --with-valgrind', SANITIZER: '', COMPILE_DEBUG: '1', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
4848
# Shared library build
4949
- { BUILDOPTIONS: '--with-cc=gcc --make-option=-f --make-option=makefile.shared', SANITIZER: '', COMPILE_DEBUG: '0', COMPILE_LTO: '1', CONV_WARNINGS: '', OTHERDEPS: 'libtool-bin' }
5050
# GCC for the 32-bit architecture (no valgrind)
5151
- { BUILDOPTIONS: '--with-cc=gcc --with-m32', SANITIZER: '', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: 'libc6-dev-i386 gcc-multilib' }
5252
# Alternative big-int version of mp_log(_n) for the 32-bit architecture (no valgrind)
53-
- { BUILDOPTIONS: '--with-cc=gcc --with-m32 --cflags=-DS_MP_WORD_TOO_SMALL_C="" ', SANITIZER: '', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: 'libc6-dev-i386 gcc-multilib' }
53+
- { BUILDOPTIONS: '--with-cc=gcc --with-m32 --cflags=-DS_MP_WORD_TOO_SMALL_C="" --cflags=-DLTM_USE_ONLY_MR ', SANITIZER: '', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: 'libc6-dev-i386 gcc-multilib' }
5454
# clang for the 32-bit architecture (no valgrind)
5555
- { BUILDOPTIONS: '--with-cc=clang-10 --with-m32', SANITIZER: '', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: 'clang-10 llvm-10 gcc-multilib' }
5656
# RSA superclass with tests (no sanitizer, but debug info)
@@ -108,8 +108,8 @@ jobs:
108108
- { BUILDOPTIONS: '--with-cc=gcc --cflags=-DMP_16BIT --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
109109
- { BUILDOPTIONS: '--with-cc=gcc --cflags=-DMP_32BIT --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
110110
# Alternative big-int version of mp_log(_n)
111-
- { BUILDOPTIONS: '--with-cc=gcc --cflags=-DMP_16BIT --cflags=-DS_MP_WORD_TOO_SMALL_C="" --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
112-
- { BUILDOPTIONS: '--with-cc=gcc --cflags=-DMP_32BIT --cflags=-DS_MP_WORD_TOO_SMALL_C="" --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
111+
- { BUILDOPTIONS: '--with-cc=gcc --cflags=-DMP_16BIT --cflags=-DS_MP_WORD_TOO_SMALL_C="" --cflags=-DLTM_USE_ONLY_MR --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
112+
- { BUILDOPTIONS: '--with-cc=gcc --cflags=-DMP_32BIT --cflags=-DS_MP_WORD_TOO_SMALL_C="" --cflags=-DLTM_USE_ONLY_MR --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
113113

114114
# clang for the x86-64 architecture with restricted limb sizes
115115
- { BUILDOPTIONS: '--with-cc=clang --cflags=-DMP_16BIT --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: 'clang llvm' }

demo/test.c

Lines changed: 300 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ static int test_mp_prime_rand(void)
889889

890890
/* test for size */
891891
for (ix = 10; ix < 128; ix++) {
892-
printf("Testing (not safe-prime): %9d bits \n", ix);
892+
printf("\rTesting (not safe-prime): %9d bits ", ix);
893893
fflush(stdout);
894894
DO(mp_prime_rand(&a, 8, ix, (rand_int() & 1) ? 0 : MP_PRIME_2MSB_ON));
895895
EXPECT(mp_count_bits(&a) == ix);
@@ -902,15 +902,264 @@ static int test_mp_prime_rand(void)
902902
return EXIT_FAILURE;
903903
}
904904

905+
/* Some small pseudoprimes to test the individual implementations */
906+
907+
/* Miller-Rabin base 2 */
908+
static const uint32_t SPSP_2[] = {
909+
2047, 3277, 4033, 4681, 8321, 15841, 29341, 42799,
910+
49141, 52633, 65281, 74665, 80581, 85489, 88357, 90751
911+
};
912+
913+
/* Miller-Rabin base 3 */
914+
static const uint32_t SPSP_3[] = {
915+
121, 703, 1891, 3281, 8401, 8911, 10585, 12403, 16531,
916+
18721, 19345, 23521, 31621, 44287, 47197, 55969, 63139,
917+
74593, 79003, 82513, 87913, 88573, 97567
918+
};
919+
920+
/* SPSP to all bases < 100 */
921+
static const char *SPSP_2_100_LARGE[4] = {
922+
"3L2x7YRmz7g4q+DwxESBacAClxrNiuspLCf8BUEphtky+5VNHLAb2ZZLLI0bu6cAOtNkUXenakBCCL"
923+
"Vn7gqOpkcrQ/ptxZdk+4gnI99wFjgcfM512N71ZzbwvLe+5Pzat2k+nHIjE0w/WbQvzk4a2/syAY8S"
924+
"i1B5XRjXYVAQOLyNWhsFpXeWXUgqiNzv7avfwBA3ZOXt", /* bases 2 - 100 */
925+
"JOcSIwxGqGEjeQ2GsdlnFMwhc+xY7EtZo5Kf4BglOuakxTJaP8qrdZyduXaAZUdzyPgQLf7B8vqvVE"
926+
"VLJwH7dLkLEiw19tfu3naT6DgQWzk+b5WuwWJzsTMdgWWH86M1h/Gjt2J/qABtTTH26C8bS4v/q9Fh"
927+
"R8jqHNOiufUgHkDQdW9Z+BLlf6OVVh2VwPIOGVc7kFF", /* bases 2 - 107 */
928+
"1ZCddPKHO7yeqI5ZeKG5ssTnzJeIDpWElJEZnHwejl4tsyly44JgwdiRmXgsi9FQfYhMzFZMgV6qWZZ"
929+
"sIJl4RNgpD/PDb3nam++ECkzMBuNIXVpmZzw+Gj5xQmpKK+OX8pFSy2IQiKyKAOfSaivXEb2/dga2J/"
930+
"Pc2d23lw+eP3WtBbfHc7TAQGgNI/6Xmcpl1G64eXCrJ", /* bases 2 - 103 */
931+
"cCax282DurA+2Z54W3VLKSC2mwgpilQpGydCDHvXHNRKbJQRa5NtLLfa3sXvCmUWZ9okP2ZSsPDnw0X"
932+
"dUQLzaz59vnw0rKbfsoA4nDBjMXR78Q889+KS4HFKfXkzxsiIKYo0kSfwPKYxFUi4Zj185kwwAPTAr2"
933+
"IjegdWjQLeX1ZQM0HVUUF3WEVhHXcFzF0sMiJU5hl" /* bases 2 - 101 */
934+
};
935+
936+
#ifndef LTM_USE_ONLY_MR
937+
/* Extra strong Lucas test with Baillie's parameters Q = 1, P = 3 */
938+
static const uint32_t ESLPSP[] = {
939+
989, 3239, 5777, 10877, 27971, 29681, 30739, 31631, 39059, 72389,
940+
73919, 75077, 100127, 113573, 125249, 137549, 137801, 153931, 155819,
941+
161027, 162133, 189419, 218321, 231703, 249331, 370229, 429479, 430127,
942+
459191, 473891, 480689, 600059, 621781, 632249, 635627
943+
};
944+
945+
/*
946+
Almost extra strong Lucas test with Baillie's parameters Q = 1, P = 3
947+
Only those that are not in ESLPSP.
948+
*/
949+
static const uint32_t AESLPSP[] = {
950+
10469, 154697, 233659, 472453, 629693, 852389, 1091093, 1560437,
951+
1620673, 1813601, 1969109, 2415739, 2595329, 2756837, 3721549,
952+
4269341, 5192309, 7045433, 7226669, 7265561
953+
};
954+
#endif
955+
956+
/* Some randomly choosen 200 decimal digit large primes (https://primes.utm.edu/lists/small/small2.html) */
957+
static const char *medium_primes[10] = {
958+
"C8Ckh0vviS3HUPdB1NSrSm+gOodw/f1aQ5+aaH1W6RMB0jVkO6lTaL54O3o7U5BSGUFGxm5gAvisbJamasuLZS8g3ZsJ2JM4Vtn9cQZRfkP6b8V",
959+
"64xDN9FqLBiovZ/9q/EPm0DONpIfn5MbJKHa+IjT0fjAzkg34FpAmad+CwhcpKaiTbZEpErut+DhpVyiQfqBFrgcGnGhhIrMF/XkyY3aVx6E96B",
960+
"8cyuMlENm0vh/eWwgHUpDKqmLyCSsRQZRWvbHpA2jHDZv1EhHkVhceg3OFRZn/aXRBnbdtsc2xO6sWh9KZ5Mo7u9rJgBJMVtDnu094MCExj1YvB",
961+
"BRFZFsYjSz45un8qptnuSqEsy9wV0BzbMpVAB1TrwImENOVIc1cASZNQ/mXG2xtazqgn/juVzFo91XLx9PtIlkcK0L2T6fBNgy8Lc7dSVoKQ+XP",
962+
"Ez/mDl+to2gm69+VdIHI9Q7vaO3DuIdLVT69myM3HYwVBE+G24KffAOUAp3FGrSOU+LtERMiIYIEtxPI7n/DRJtmL2i0+REwGpTMge2d2EpabfB",
963+
"5+Uz1gPFjZJ/nNdEOmOaMouJSGzygo42qz7xOwXn/moSUvBpPjo4twRGbK0+qaeU/RI8yYYxXr3OBP4w+/jgL3mN9GiENDM5LtEKMiQrZ9jIVEb",
964+
"AQ5nD1+G1grv41s/XlK+0YTGyZgr/88PzdQJ8QT9tavisTgyG6k8/80A4HQhnFndskHNAaB2EW5fE7KH3kk7m89s8JnVqkJyGZWSfs1+JlmHLPf",
965+
"3F19vPmM0Ih89KZ04Xmd62QB9F6E2sztT10A7Kcqc44eKvsNHh+JY6Z6gJXkbWg1Iw7xr29QAhEF/o1YAgfutQtpdzHkex06Yd71kPsaZdKXiC5",
966+
"2fIcJ1t/VYCColXGs+ji/txNMEXn2FXdowLzlo7QKqzAWHdAbwtltSO5qpSp3OUiEOGUUi3hbyw3iQRE8nFJaikJ89Wdox6vpPtIsc3QRjexMnv",
967+
"8aOicQ5gIbFCarFUgSgzh40LpuZ0jjK1u48/YT+C0h1dAQ8CIEgZjHZT+5/7cCRGmJlo+XCp7S41MSQ2ZNRSJh2texRYtvAXBAZfR8A8twl316P"
968+
};
969+
970+
const mp_digit prime_tab[] = {
971+
0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013,
972+
0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035,
973+
0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059,
974+
0x0061, 0x0065, 0x0067, 0x006B, 0x006D, 0x0071, 0x007F, 0x0083,
975+
0x0089, 0x008B, 0x0095, 0x0097, 0x009D, 0x00A3, 0x00A7, 0x00AD,
976+
0x00B3, 0x00B5, 0x00BF, 0x00C1, 0x00C5, 0x00C7, 0x00D3, 0x00DF,
977+
0x00E3, 0x00E5, 0x00E9, 0x00EF, 0x00F1, 0x00FB, 0x0101, 0x0107,
978+
0x010D, 0x010F, 0x0115, 0x0119, 0x011B, 0x0125, 0x0133, 0x0137,
979+
980+
0x0139, 0x013D, 0x014B, 0x0151, 0x015B, 0x015D, 0x0161, 0x0167,
981+
0x016F, 0x0175, 0x017B, 0x017F, 0x0185, 0x018D, 0x0191, 0x0199,
982+
0x01A3, 0x01A5, 0x01AF, 0x01B1, 0x01B7, 0x01BB, 0x01C1, 0x01C9,
983+
0x01CD, 0x01CF, 0x01D3, 0x01DF, 0x01E7, 0x01EB, 0x01F3, 0x01F7,
984+
0x01FD, 0x0209, 0x020B, 0x021D, 0x0223, 0x022D, 0x0233, 0x0239,
985+
0x023B, 0x0241, 0x024B, 0x0251, 0x0257, 0x0259, 0x025F, 0x0265,
986+
0x0269, 0x026B, 0x0277, 0x0281, 0x0283, 0x0287, 0x028D, 0x0293,
987+
0x0295, 0x02A1, 0x02A5, 0x02AB, 0x02B3, 0x02BD, 0x02C5, 0x02CF,
988+
989+
0x02D7, 0x02DD, 0x02E3, 0x02E7, 0x02EF, 0x02F5, 0x02F9, 0x0301,
990+
0x0305, 0x0313, 0x031D, 0x0329, 0x032B, 0x0335, 0x0337, 0x033B,
991+
0x033D, 0x0347, 0x0355, 0x0359, 0x035B, 0x035F, 0x036D, 0x0371,
992+
0x0373, 0x0377, 0x038B, 0x038F, 0x0397, 0x03A1, 0x03A9, 0x03AD,
993+
0x03B3, 0x03B9, 0x03C7, 0x03CB, 0x03D1, 0x03D7, 0x03DF, 0x03E5,
994+
0x03F1, 0x03F5, 0x03FB, 0x03FD, 0x0407, 0x0409, 0x040F, 0x0419,
995+
0x041B, 0x0425, 0x0427, 0x042D, 0x043F, 0x0443, 0x0445, 0x0449,
996+
0x044F, 0x0455, 0x045D, 0x0463, 0x0469, 0x047F, 0x0481, 0x048B,
997+
998+
0x0493, 0x049D, 0x04A3, 0x04A9, 0x04B1, 0x04BD, 0x04C1, 0x04C7,
999+
0x04CD, 0x04CF, 0x04D5, 0x04E1, 0x04EB, 0x04FD, 0x04FF, 0x0503,
1000+
0x0509, 0x050B, 0x0511, 0x0515, 0x0517, 0x051B, 0x0527, 0x0529,
1001+
0x052F, 0x0551, 0x0557, 0x055D, 0x0565, 0x0577, 0x0581, 0x058F,
1002+
0x0593, 0x0595, 0x0599, 0x059F, 0x05A7, 0x05AB, 0x05AD, 0x05B3,
1003+
0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7,
1004+
0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623,
1005+
0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653
1006+
};
1007+
1008+
#define ARR_LENGTH(a) ((int)(sizeof((a))/sizeof((a)[0])))
1009+
1010+
static int test_mp_prime_miller_rabin(void)
1011+
{
1012+
mp_int a, b, c;
1013+
bool result;
1014+
int i;
1015+
mp_digit j;
1016+
DOR(mp_init_multi(&a, &b, &c, NULL));
1017+
1018+
/* SPSP to base 2 */
1019+
mp_set(&b, 2u);
1020+
for (i = 0; i < ARR_LENGTH(SPSP_2); i++) {
1021+
result = false;
1022+
mp_set_u32(&a, SPSP_2[i]);
1023+
DO(mp_prime_miller_rabin(&a, &b, &result));
1024+
EXPECT(result == true);
1025+
}
1026+
1027+
/* Some larger primes to check for false negatives */
1028+
for (i = 0; i < 10; i++) {
1029+
result = false;
1030+
DO(mp_read_radix(&a, medium_primes[i], 64));
1031+
DO(mp_prime_miller_rabin(&a, &b, &result));
1032+
EXPECT(result == true);
1033+
}
1034+
/* Some semi-primes */
1035+
for (i = 0; i < 5; i += 2) {
1036+
result = false;
1037+
DO(mp_read_radix(&a, medium_primes[i], 64));
1038+
DO(mp_read_radix(&c, medium_primes[i+1], 64));
1039+
DO(mp_mul(&a, &c, &a));
1040+
DO(mp_prime_miller_rabin(&a, &b, &result));
1041+
EXPECT(result == false);
1042+
}
1043+
1044+
/* SPSP to base 3 */
1045+
mp_set(&b, 3u);
1046+
for (i = 0; i < ARR_LENGTH(SPSP_3); i++) {
1047+
result = false;
1048+
mp_set_u32(&a, SPSP_3[i]);
1049+
DO(mp_prime_miller_rabin(&a, &b, &result));
1050+
EXPECT(result == true);
1051+
}
1052+
1053+
/* SPSP to bases 2 -- 100 */
1054+
mp_set(&b, 2u);
1055+
for (i = 0; i < 4; i++) {
1056+
DO(mp_read_radix(&a, SPSP_2_100_LARGE[i], 64));
1057+
for (j = 2u; j <= 100u; j++) {
1058+
result = false;
1059+
mp_set(&b, j);
1060+
DO(mp_prime_miller_rabin(&a, &b, &result));
1061+
EXPECT(result == true);
1062+
}
1063+
/* 107 is a prime that works */
1064+
mp_set(&b, 107u);
1065+
DO(mp_prime_miller_rabin(&a, &b, &result));
1066+
EXPECT(result == false);
1067+
}
1068+
1069+
/* SPSP to bases 2 -- 100, automatic */
1070+
mp_set(&b, 2u);
1071+
for (i = 0; i < 4; i++) {
1072+
DO(mp_read_radix(&a, SPSP_2_100_LARGE[i], 64));
1073+
for (j = 2u; j <= (mp_digit)mp_prime_rabin_miller_trials(mp_count_bits(&a)); j++) {
1074+
result = false;
1075+
mp_set(&b, (mp_digit)prime_tab[j]);
1076+
DO(mp_prime_miller_rabin(&a, &b, &result));
1077+
}
1078+
/* These numbers are not big enough for the heuristics to work */
1079+
EXPECT(result == true);
1080+
}
1081+
1082+
mp_clear_multi(&a, &b, &c, NULL);
1083+
return EXIT_SUCCESS;
1084+
LBL_ERR:
1085+
mp_clear_multi(&a, &b, &c, NULL);
1086+
return EXIT_FAILURE;
1087+
}
1088+
1089+
#ifndef LTM_USE_ONLY_MR
1090+
static int test_mp_prime_extra_strong_lucas(void)
1091+
{
1092+
mp_int a, b;
1093+
bool result;
1094+
int i;
1095+
1096+
DOR(mp_init_multi(&a, &b, NULL));
1097+
1098+
/* Check Extra Strong pseudoprimes */
1099+
for (i = 0; i < ARR_LENGTH(ESLPSP); i++) {
1100+
result = false;
1101+
mp_set_u32(&a, ESLPSP[i]);
1102+
DO(mp_prime_extra_strong_lucas(&a, &result));
1103+
EXPECT(result == true);
1104+
}
1105+
1106+
/* Check Almost Extra Strong pseudoprimes (not in ESLPSP) */
1107+
for (i = 0; i < ARR_LENGTH(AESLPSP); i++) {
1108+
result = false;
1109+
mp_set_u32(&a, AESLPSP[i]);
1110+
DO(mp_prime_extra_strong_lucas(&a, &result));
1111+
EXPECT(result == false);
1112+
}
1113+
1114+
/* Some larger primes to check for false negatives */
1115+
for (i = 0; i < 10; i++) {
1116+
result = false;
1117+
DO(mp_read_radix(&a, medium_primes[i], 64));
1118+
DO(mp_prime_extra_strong_lucas(&a, &result));
1119+
EXPECT(result == true);
1120+
}
1121+
1122+
/* Some semi-primes */
1123+
for (i = 0; i < 5; i++) {
1124+
result = false;
1125+
DO(mp_read_radix(&a, medium_primes[i], 64));
1126+
DO(mp_read_radix(&a, medium_primes[i+1], 64));
1127+
DO(mp_mul(&a, &b, &a));
1128+
DO(mp_prime_extra_strong_lucas(&a, &result));
1129+
EXPECT(result == false);
1130+
}
1131+
1132+
mp_clear_multi(&a, &b, NULL);
1133+
return EXIT_SUCCESS;
1134+
LBL_ERR:
1135+
mp_clear_multi(&a, &b, NULL);
1136+
return EXIT_FAILURE;
1137+
}
1138+
#endif
1139+
9051140
static int test_mp_prime_is_prime(void)
9061141
{
9071142
int ix;
9081143
mp_err e;
909-
bool cnt, fu;
1144+
bool cnt;
1145+
#ifndef LTM_USE_ONLY_MR
1146+
bool fu;
1147+
#endif
9101148

9111149
mp_int a, b;
9121150
DOR(mp_init_multi(&a, &b, NULL));
9131151

1152+
/* strong Miller-Rabin pseudoprimes to the first 100 primes (gernerated with Arnault's method) */
1153+
printf("Testing mp_prime_is_prime() with SPSPs to the first 100 primes\n");
1154+
for (ix = 0; ix < 4; ix++) {
1155+
DO(mp_read_radix(&a,SPSP_2_100_LARGE[ix],64));
1156+
DO(mp_prime_is_prime(&a, mp_prime_rabin_miller_trials(mp_count_bits(&a)), &cnt));
1157+
if (cnt) {
1158+
printf("SPSP_2_100_LARGE[%d] is not prime but mp_prime_is_prime says it is.\n", ix);
1159+
goto LBL_ERR;
1160+
}
1161+
}
1162+
9141163
/* strong Miller-Rabin pseudoprime to the first 200 primes (F. Arnault) */
9151164
printf("Testing mp_prime_is_prime() with Arnault's pseudoprime 803...901");
9161165
DO(mp_read_radix(&a,
@@ -954,6 +1203,7 @@ static int test_mp_prime_is_prime(void)
9541203
DO(mp_prime_is_prime(&b, mp_prime_rabin_miller_trials(mp_count_bits(&b)), &cnt));
9551204
/* large problem */
9561205
EXPECT(cnt);
1206+
#ifndef LTM_USE_ONLY_MR
9571207
DO(mp_prime_frobenius_underwood(&b, &fu));
9581208
EXPECT(fu);
9591209
if ((e != MP_OKAY) || !cnt) {
@@ -965,13 +1215,14 @@ static int test_mp_prime_is_prime(void)
9651215
putchar('\n');
9661216
goto LBL_ERR;
9671217
}
968-
1218+
#endif
9691219
}
1220+
#ifndef LTM_USE_ONLY_MR
9701221
/* Check regarding problem #143 */
9711222
DO(mp_read_radix(&a,
9721223
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF",
9731224
16));
974-
DO(mp_prime_strong_lucas_selfridge(&a, &cnt));
1225+
DO(mp_prime_extra_strong_lucas(&a, &cnt));
9751226
/* large problem */
9761227
EXPECT(cnt);
9771228
if ((e != MP_OKAY) || !cnt) {
@@ -980,6 +1231,47 @@ static int test_mp_prime_is_prime(void)
9801231
putchar('\n');
9811232
goto LBL_ERR;
9821233
}
1234+
#endif
1235+
/* Check deterministic tests */
1236+
#ifdef LTM_USE_ONLY_MR
1237+
#if ((defined S_MP_PRIME_IS_DIVISIBLE_C) && (MP_PRIME_TAB_SIZE >= 256))
1238+
/* 2-SPRP 4188889 = 431 * 9719 < 2^22 */
1239+
DO(mp_read_radix(&a,"4188889",10));
1240+
DO(mp_prime_is_prime(&a, 0, &cnt));
1241+
EXPECT(cnt == false);
1242+
/* Last prime < 2^22 */
1243+
DO(mp_read_radix(&a,"4194301",10));
1244+
DO(mp_prime_is_prime(&a, 0, &cnt));
1245+
EXPECT(cnt == true);
1246+
/* 2,3-SPRP 6787327 = 1303 * 5209 < 2^23 */
1247+
DO(mp_read_radix(&a,"6787327",10));
1248+
DO(mp_prime_is_prime(&a, 0, &cnt));
1249+
EXPECT(cnt == false);
1250+
/* Last prime < 2^23 */
1251+
DO(mp_read_radix(&a,"8388593",10));
1252+
DO(mp_prime_is_prime(&a, 0, &cnt));
1253+
EXPECT(cnt == true);
1254+
1255+
/* 2,3,1459-SPRP < 2^32*/
1256+
DO(mp_read_radix(&a,"1518290707",10));
1257+
DO(mp_prime_is_prime(&a, -1, &cnt));
1258+
EXPECT(cnt == false);
1259+
#endif
1260+
/* 2,3,7,61-SPRP < 2^43*/
1261+
DO(mp_read_radix(&a,"7038007247701",10));
1262+
DO(mp_prime_is_prime(&a, -1, &cnt));
1263+
EXPECT(cnt == false);
1264+
1265+
/* 2,325,9375,28178,450775,9780504-SPRP < 2^64
1266+
which is also a
1267+
2,3,325,9375,28178,450775,9780504-SPRP
1268+
*/
1269+
DO(mp_read_radix(&a,"18411296009130176041",10));
1270+
DO(mp_prime_is_prime(&a, -1, &cnt));
1271+
EXPECT(cnt == false);
1272+
1273+
#endif
1274+
9831275

9841276
mp_clear_multi(&a, &b, NULL);
9851277
return EXIT_SUCCESS;
@@ -2475,6 +2767,10 @@ static int unit_tests(int argc, char **argv)
24752767
T1(mp_montgomery_reduce, MP_MONTGOMERY_REDUCE),
24762768
T1(mp_root_n, MP_ROOT_N),
24772769
T1(mp_or, MP_OR),
2770+
#ifndef LTM_USE_ONLY_MR
2771+
T1(mp_prime_extra_strong_lucas, MP_PRIME_EXTRA_STRONG_LUCAS),
2772+
#endif
2773+
T1(mp_prime_miller_rabin, MP_PRIME_MILLER_RABIN),
24782774
T1(mp_prime_is_prime, MP_PRIME_IS_PRIME),
24792775
T1(mp_prime_next_prime, MP_PRIME_NEXT_PRIME),
24802776
T1(mp_prime_rand, MP_PRIME_RAND),

0 commit comments

Comments
 (0)