Skip to content

Commit 32db1a7

Browse files
committed
Added LTM_USE_ONLY_MR tests to CI
1 parent 16d5926 commit 32db1a7

File tree

4 files changed

+189
-71
lines changed

4 files changed

+189
-71
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: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,7 @@ static const char *SPSP_2_100_LARGE[4] = {
927927
"IjegdWjQLeX1ZQM0HVUUF3WEVhHXcFzF0sMiJU5hl" /* bases 2 - 101 */
928928
};
929929

930+
#ifndef LTM_USE_ONLY_MR
930931
/* Extra strong Lucas test with Baillie's parameters Q = 1, P = 3 */
931932
static const uint32_t ESLPSP[] = {
932933
989, 3239, 5777, 10877, 27971, 29681, 30739, 31631, 39059, 72389,
@@ -944,6 +945,7 @@ static const uint32_t AESLPSP[] = {
944945
1620673, 1813601, 1969109, 2415739, 2595329, 2756837, 3721549,
945946
4269341, 5192309, 7045433, 7226669, 7265561
946947
};
948+
#endif
947949

948950
/* Some randomly choosen 200 decimal digit large primes (https://primes.utm.edu/lists/small/small2.html) */
949951
static const char *medium_primes[10] = {
@@ -1078,7 +1080,7 @@ static int test_mp_prime_miller_rabin(void)
10781080
return EXIT_FAILURE;
10791081
}
10801082

1081-
1083+
#ifndef LTM_USE_ONLY_MR
10821084
static int test_mp_prime_extra_strong_lucas(void)
10831085
{
10841086
mp_int a, b;
@@ -1127,12 +1129,16 @@ static int test_mp_prime_extra_strong_lucas(void)
11271129
mp_clear_multi(&a, &b, NULL);
11281130
return EXIT_FAILURE;
11291131
}
1132+
#endif
11301133

11311134
static int test_mp_prime_is_prime(void)
11321135
{
11331136
int ix;
11341137
mp_err e;
1135-
bool cnt, fu;
1138+
bool cnt;
1139+
#ifndef LTM_USE_ONLY_MR
1140+
bool fu;
1141+
#endif
11361142

11371143
mp_int a, b;
11381144
DOR(mp_init_multi(&a, &b, NULL));
@@ -1191,6 +1197,7 @@ static int test_mp_prime_is_prime(void)
11911197
DO(mp_prime_is_prime(&b, mp_prime_rabin_miller_trials(mp_count_bits(&b)), &cnt));
11921198
/* large problem */
11931199
EXPECT(cnt);
1200+
#ifndef LTM_USE_ONLY_MR
11941201
DO(mp_prime_frobenius_underwood(&b, &fu));
11951202
EXPECT(fu);
11961203
if ((e != MP_OKAY) || !cnt) {
@@ -1202,8 +1209,9 @@ static int test_mp_prime_is_prime(void)
12021209
putchar('\n');
12031210
goto LBL_ERR;
12041211
}
1205-
1212+
#endif
12061213
}
1214+
#ifndef LTM_USE_ONLY_MR
12071215
/* Check regarding problem #143 */
12081216
DO(mp_read_radix(&a,
12091217
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF",
@@ -1217,6 +1225,47 @@ static int test_mp_prime_is_prime(void)
12171225
putchar('\n');
12181226
goto LBL_ERR;
12191227
}
1228+
#endif
1229+
/* Check deterministic tests */
1230+
#ifdef LTM_USE_ONLY_MR
1231+
#if ((defined S_MP_PRIME_IS_DIVISIBLE_C) && (MP_PRIME_TAB_SIZE >= 256))
1232+
/* 2-SPRP 4188889 = 431 * 9719 < 2^22 */
1233+
DO(mp_read_radix(&a,"4188889",10));
1234+
DO(mp_prime_is_prime(&a, 0, &cnt));
1235+
EXPECT(cnt == false);
1236+
/* Last prime < 2^22 */
1237+
DO(mp_read_radix(&a,"4194301",10));
1238+
DO(mp_prime_is_prime(&a, 0, &cnt));
1239+
EXPECT(cnt == true);
1240+
/* 2,3-SPRP 6787327 = 1303 * 5209 < 2^23 */
1241+
DO(mp_read_radix(&a,"6787327",10));
1242+
DO(mp_prime_is_prime(&a, 0, &cnt));
1243+
EXPECT(cnt == false);
1244+
/* Last prime < 2^23 */
1245+
DO(mp_read_radix(&a,"8388593",10));
1246+
DO(mp_prime_is_prime(&a, 0, &cnt));
1247+
EXPECT(cnt == true);
1248+
1249+
/* 2,3,1459-SPRP < 2^32*/
1250+
DO(mp_read_radix(&a,"1518290707",10));
1251+
DO(mp_prime_is_prime(&a, -1, &cnt));
1252+
EXPECT(cnt == false);
1253+
#endif
1254+
/* 2,3,7,61-SPRP < 2^43*/
1255+
DO(mp_read_radix(&a,"7038007247701",10));
1256+
DO(mp_prime_is_prime(&a, -1, &cnt));
1257+
EXPECT(cnt == false);
1258+
1259+
/* 2,325,9375,28178,450775,9780504-SPRP < 2^64
1260+
which is also a
1261+
2,3,325,9375,28178,450775,9780504-SPRP
1262+
*/
1263+
DO(mp_read_radix(&a,"18411296009130176041",10));
1264+
DO(mp_prime_is_prime(&a, -1, &cnt));
1265+
EXPECT(cnt == false);
1266+
1267+
#endif
1268+
12201269

12211270
mp_clear_multi(&a, &b, NULL);
12221271
return EXIT_SUCCESS;
@@ -2708,7 +2757,9 @@ static int unit_tests(int argc, char **argv)
27082757
T1(mp_montgomery_reduce, MP_MONTGOMERY_REDUCE),
27092758
T1(mp_root_n, MP_ROOT_N),
27102759
T1(mp_or, MP_OR),
2760+
#ifndef LTM_USE_ONLY_MR
27112761
T1(mp_prime_extra_strong_lucas, MP_PRIME_EXTRA_STRONG_LUCAS),
2762+
#endif
27122763
T1(mp_prime_miller_rabin, MP_PRIME_MILLER_RABIN),
27132764
T1(mp_prime_is_prime, MP_PRIME_IS_PRIME),
27142765
T1(mp_prime_next_prime, MP_PRIME_NEXT_PRIME),

doc/bn.tex

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ \section{Building LibTomMath}
113113
replace GCC for building the static and the shared library. Editing the makefiles is not needed,
114114
just set the shell variable \texttt{CC} as shown below.
115115
\begin{alltt}
116-
CC=/home/czurnieden/intel/bin/icc make
116+
CC=/home/$USER/intel/bin/icc make
117117
\end{alltt}
118118

119119
ICC does not know all options available for GCC and LibTomMath uses two diagnostics
@@ -1439,7 +1439,7 @@ \section{Integer Division and Remainder}
14391439
mp_err mp_div (const mp_int *a, const mp_int *b, mp_int *c, mp_int *d);
14401440
\end{alltt}
14411441

1442-
This divides $a$ by $b$ and stores the quotient in $c$ and $d$. The signed quotient is computed
1442+
This divides $a$ by $b$ and stores the quotient in $c$ and the remainder in $d$. The signed quotient is computed
14431443
such that $bc + d = a$. Note that either of $c$ or $d$ can be set to \texttt{NULL} if their value
14441444
is not required. If $b$ is zero the function returns \texttt{MP\_VAL}.
14451445

@@ -2003,7 +2003,7 @@ \subsection{Example}
20032003
mp_error_to_string(e));
20042004
exit(EXIT_FAILURE);
20052005
}
2006-
printf("%d\n",output);
2006+
printf("%d\textbackslash{}n",output);
20072007
20082008
mp_clear(&x);
20092009
exit(EXIT_SUCCESS);
@@ -2256,13 +2256,8 @@ \section{Primality Testing}
22562256
\begin{alltt}
22572257
mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
22582258
\end{alltt}
2259-
This will perform a trial division followed by two rounds of Miller--Rabin with bases 2 and 3 and a
2260-
extra strong Lucas test. The Frobenius--Underwood time is available as a compile--time option with the
2261-
preprocessor macro \texttt{LTM\_USE\_FROBENIUS\_TEST}. See file \texttt{bn\_mp\_prime\_is\_prime.c}
2262-
for the necessary details. It shall be noted that both functions are much slower than the
2263-
Miller--Rabin test and if speed is an essential issue, the macro \texttt{LTM\_USE\_ONLY\_MR}
2264-
switches the Frobenius--Underwood test and the Lucas test off and their code will not
2265-
even be compiled into the library.
2259+
This will perform a trial division followed by two rounds of Miller--Rabin to bases 2 and 3 and an
2260+
extra strong Lucas test.
22662261

22672262
If $t$ is set to a positive value $t$ additional rounds of the Miller--Rabin test with random bases
22682263
will be performed to allow for Fips 186.4 (vid.~p.~126ff) compliance. The function
@@ -2274,15 +2269,33 @@ \section{Primality Testing}
22742269
positive value this function will run $t + 1$ Miller--Rabin tests with random bases.
22752270

22762271
If $t$ is set to a negative value the test will run the deterministic Miller--Rabin test for the
2277-
primes up to $3\,317\,044\,064\,679\,887\ 385\,961\,981$\footnote{The semiprime $1287836182261\cdot
2278-
2575672364521$ with both factors smaller than $2^{64}$. An alternative with all factors smaller
2279-
than
2280-
$2^32$ is $4290067842\cdot 262853\cdot 1206721\cdot 2134439 + 3$}. Input larger than the above limits
2272+
primes up to $3\,317\,044\,064\,679\,887\ 385\,961\,981$ Input larger than the above limit
22812273
will return \texttt{MP\_VAL}.
22822274

22832275
If $a$ passes all of the tests $result$ is set to \texttt{true}, otherwise it is set to
22842276
\texttt{false}.
22852277

2278+
\subsection{Compile Time Switches}
2279+
There are several compile-time branches available.
2280+
\begin{description}
2281+
\item[\texttt{LTM\_USE\_ONLY\_MR}]\hfill \\
2282+
Neither run the Frobenius--Underwood nor the Extra--Strong--Lucas test, only Miller-Rabin.
2283+
This does not change the deterministic tests with \texttt{t < 0} but the probabilistic test might
2284+
be a bit weaker.
2285+
2286+
\item[\texttt{LTM\_USE\_FROBENIUS\_TEST}]\hfill \\
2287+
This runs the Frobenius--Underwood test after the the Extra--Strong--Lucas test. This macro is not
2288+
compatible with the macro \texttt{LTM\_USE\_ONLY\_MR}.
2289+
2290+
\item[\texttt{LTM\_USE\_ZHANG}]\hfill \\
2291+
This macro allows the code to use larger bounds up to
2292+
$1\,543\,267\,864\,443\,420\,616\,877\,677\,640\,751\,301$ computed by Zhenxiang Zhang in
2293+
``Two Kinds of Strong Pseudoprimes up to $10^36$'' (Mathematics of computation, 76(260), 2095-2107)
2294+
but they have not been verified independently yet, use with caution.
2295+
\end{description}
2296+
2297+
2298+
22862299
\section{Next Prime}
22872300
\index{mp\_prime\_next\_prime}
22882301
\begin{alltt}
@@ -2730,4 +2743,6 @@ \subsection{Shortcuts}
27302743
\end{appendices}
27312744
\input{bn.ind}
27322745

2746+
2747+
27332748
\end{document}

0 commit comments

Comments
 (0)