Skip to content

Commit

Permalink
Added LTM_USE_ONLY_MR tests to CI
Browse files Browse the repository at this point in the history
  • Loading branch information
czurnieden committed Apr 14, 2023
1 parent 16d5926 commit 32db1a7
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 71 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ jobs:
# Run always with valgrind (no sanitizer, but debug info)
- { BUILDOPTIONS: '--with-cc=gcc --with-m64 --with-valgrind', SANITIZER: '', COMPILE_DEBUG: '1', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
# Alternative big-int version of mp_log(_n)
- { 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: '' }
- { 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: '' }
# Shared library build
- { BUILDOPTIONS: '--with-cc=gcc --make-option=-f --make-option=makefile.shared', SANITIZER: '', COMPILE_DEBUG: '0', COMPILE_LTO: '1', CONV_WARNINGS: '', OTHERDEPS: 'libtool-bin' }
# GCC for the 32-bit architecture (no valgrind)
- { BUILDOPTIONS: '--with-cc=gcc --with-m32', SANITIZER: '', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: 'libc6-dev-i386 gcc-multilib' }
# Alternative big-int version of mp_log(_n) for the 32-bit architecture (no valgrind)
- { 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' }
- { 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' }
# clang for the 32-bit architecture (no valgrind)
- { BUILDOPTIONS: '--with-cc=clang-10 --with-m32', SANITIZER: '', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: 'clang-10 llvm-10 gcc-multilib' }
# RSA superclass with tests (no sanitizer, but debug info)
Expand Down Expand Up @@ -108,8 +108,8 @@ jobs:
- { BUILDOPTIONS: '--with-cc=gcc --cflags=-DMP_16BIT --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
- { BUILDOPTIONS: '--with-cc=gcc --cflags=-DMP_32BIT --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
# Alternative big-int version of mp_log(_n)
- { 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: '' }
- { 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: '' }
- { 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: '' }
- { 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: '' }

# clang for the x86-64 architecture with restricted limb sizes
- { BUILDOPTIONS: '--with-cc=clang --cflags=-DMP_16BIT --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: 'clang llvm' }
Expand Down
57 changes: 54 additions & 3 deletions demo/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ static const char *SPSP_2_100_LARGE[4] = {
"IjegdWjQLeX1ZQM0HVUUF3WEVhHXcFzF0sMiJU5hl" /* bases 2 - 101 */
};

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

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


#ifndef LTM_USE_ONLY_MR
static int test_mp_prime_extra_strong_lucas(void)
{
mp_int a, b;
Expand Down Expand Up @@ -1127,12 +1129,16 @@ static int test_mp_prime_extra_strong_lucas(void)
mp_clear_multi(&a, &b, NULL);
return EXIT_FAILURE;
}
#endif

static int test_mp_prime_is_prime(void)
{
int ix;
mp_err e;
bool cnt, fu;
bool cnt;
#ifndef LTM_USE_ONLY_MR
bool fu;
#endif

mp_int a, b;
DOR(mp_init_multi(&a, &b, NULL));
Expand Down Expand Up @@ -1191,6 +1197,7 @@ static int test_mp_prime_is_prime(void)
DO(mp_prime_is_prime(&b, mp_prime_rabin_miller_trials(mp_count_bits(&b)), &cnt));
/* large problem */
EXPECT(cnt);
#ifndef LTM_USE_ONLY_MR
DO(mp_prime_frobenius_underwood(&b, &fu));
EXPECT(fu);
if ((e != MP_OKAY) || !cnt) {
Expand All @@ -1202,8 +1209,9 @@ static int test_mp_prime_is_prime(void)
putchar('\n');
goto LBL_ERR;
}

#endif
}
#ifndef LTM_USE_ONLY_MR
/* Check regarding problem #143 */
DO(mp_read_radix(&a,
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF",
Expand All @@ -1217,6 +1225,47 @@ static int test_mp_prime_is_prime(void)
putchar('\n');
goto LBL_ERR;
}
#endif
/* Check deterministic tests */
#ifdef LTM_USE_ONLY_MR
#if ((defined S_MP_PRIME_IS_DIVISIBLE_C) && (MP_PRIME_TAB_SIZE >= 256))
/* 2-SPRP 4188889 = 431 * 9719 < 2^22 */
DO(mp_read_radix(&a,"4188889",10));
DO(mp_prime_is_prime(&a, 0, &cnt));
EXPECT(cnt == false);
/* Last prime < 2^22 */
DO(mp_read_radix(&a,"4194301",10));
DO(mp_prime_is_prime(&a, 0, &cnt));
EXPECT(cnt == true);
/* 2,3-SPRP 6787327 = 1303 * 5209 < 2^23 */
DO(mp_read_radix(&a,"6787327",10));
DO(mp_prime_is_prime(&a, 0, &cnt));
EXPECT(cnt == false);
/* Last prime < 2^23 */
DO(mp_read_radix(&a,"8388593",10));
DO(mp_prime_is_prime(&a, 0, &cnt));
EXPECT(cnt == true);

/* 2,3,1459-SPRP < 2^32*/
DO(mp_read_radix(&a,"1518290707",10));
DO(mp_prime_is_prime(&a, -1, &cnt));
EXPECT(cnt == false);
#endif
/* 2,3,7,61-SPRP < 2^43*/
DO(mp_read_radix(&a,"7038007247701",10));
DO(mp_prime_is_prime(&a, -1, &cnt));
EXPECT(cnt == false);

/* 2,325,9375,28178,450775,9780504-SPRP < 2^64
which is also a
2,3,325,9375,28178,450775,9780504-SPRP
*/
DO(mp_read_radix(&a,"18411296009130176041",10));
DO(mp_prime_is_prime(&a, -1, &cnt));
EXPECT(cnt == false);

#endif


mp_clear_multi(&a, &b, NULL);
return EXIT_SUCCESS;
Expand Down Expand Up @@ -2708,7 +2757,9 @@ static int unit_tests(int argc, char **argv)
T1(mp_montgomery_reduce, MP_MONTGOMERY_REDUCE),
T1(mp_root_n, MP_ROOT_N),
T1(mp_or, MP_OR),
#ifndef LTM_USE_ONLY_MR
T1(mp_prime_extra_strong_lucas, MP_PRIME_EXTRA_STRONG_LUCAS),
#endif
T1(mp_prime_miller_rabin, MP_PRIME_MILLER_RABIN),
T1(mp_prime_is_prime, MP_PRIME_IS_PRIME),
T1(mp_prime_next_prime, MP_PRIME_NEXT_PRIME),
Expand Down
43 changes: 29 additions & 14 deletions doc/bn.tex
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ \section{Building LibTomMath}
replace GCC for building the static and the shared library. Editing the makefiles is not needed,
just set the shell variable \texttt{CC} as shown below.
\begin{alltt}
CC=/home/czurnieden/intel/bin/icc make
CC=/home/$USER/intel/bin/icc make
\end{alltt}

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

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

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

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

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

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

\subsection{Compile Time Switches}
There are several compile-time branches available.
\begin{description}
\item[\texttt{LTM\_USE\_ONLY\_MR}]\hfill \\
Neither run the Frobenius--Underwood nor the Extra--Strong--Lucas test, only Miller-Rabin.
This does not change the deterministic tests with \texttt{t < 0} but the probabilistic test might
be a bit weaker.

\item[\texttt{LTM\_USE\_FROBENIUS\_TEST}]\hfill \\
This runs the Frobenius--Underwood test after the the Extra--Strong--Lucas test. This macro is not
compatible with the macro \texttt{LTM\_USE\_ONLY\_MR}.

\item[\texttt{LTM\_USE\_ZHANG}]\hfill \\
This macro allows the code to use larger bounds up to
$1\,543\,267\,864\,443\,420\,616\,877\,677\,640\,751\,301$ computed by Zhenxiang Zhang in
``Two Kinds of Strong Pseudoprimes up to $10^36$'' (Mathematics of computation, 76(260), 2095-2107)
but they have not been verified independently yet, use with caution.
\end{description}



\section{Next Prime}
\index{mp\_prime\_next\_prime}
\begin{alltt}
Expand Down Expand Up @@ -2730,4 +2743,6 @@ \subsection{Shortcuts}
\end{appendices}
\input{bn.ind}



\end{document}
Loading

0 comments on commit 32db1a7

Please sign in to comment.