From 9eecab7b493d84da84ca4b147cc168b547ab0dbd Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 5 Sep 2019 16:41:36 +0200 Subject: [PATCH 01/11] print jenkins seed on startup I just had a case around where the `mp_prime_rand()` testcase failed but I couldn't reproduce. I'm pretty sure that was caused by all my changes and partial rebuilds but anyways it'll give us in the future the possibility to debug such a case. --- demo/test.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/demo/test.c b/demo/test.c index 390d32d..8da3596 100644 --- a/demo/test.c +++ b/demo/test.c @@ -1,3 +1,4 @@ +#include #include "shared.h" static long rand_long(void) @@ -2264,9 +2265,12 @@ int unit_tests(int argc, char **argv) #undef T }; unsigned long i; + uint64_t t; int res = EXIT_SUCCESS, j; - s_mp_rand_jenkins_init((uint64_t)time(NULL)); + t = (uint64_t)time(NULL); + printf("SEED: 0x%"PRIx64"\n\n", t); + s_mp_rand_jenkins_init(t); mp_rand_source(s_mp_rand_jenkins); for (i = 0; i < sizeof(test) / sizeof(test[0]); ++i) { From f38a65e637302db6b8298e127ac94c7b072600db Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 5 Sep 2019 16:58:39 +0200 Subject: [PATCH 02/11] simplify some of the build rules --- makefile | 9 ++------- makefile.shared | 15 +++------------ 2 files changed, 5 insertions(+), 19 deletions(-) diff --git a/makefile b/makefile index beab4fc..8de82d1 100644 --- a/makefile +++ b/makefile @@ -17,7 +17,7 @@ coverage: LIBNAME:=-Wl,--whole-archive $(LIBNAME) -Wl,--no-whole-archive include makefile_include.mk -%.o: %.c +%.o: %.c $(HEADERS) ifneq ($V,1) @echo " * ${CC} $@" endif @@ -58,8 +58,6 @@ bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_s #END_INS -$(OBJECTS): $(HEADERS) - $(LIBNAME): $(OBJECTS) $(AR) $(ARFLAGS) $@ $(OBJECTS) $(RANLIB) $@ @@ -97,10 +95,7 @@ uninstall: rm $(DESTDIR)$(LIBPATH)/$(LIBNAME) rm $(HEADERS_PUB:%=$(DESTDIR)$(INCPATH)/%) -test: demo/main.o demo/opponent.o demo/test.o $(LIBNAME) - $(CC) $(CFLAGS) $^ $(LFLAGS) -o test - -test_standalone: demo/main.o demo/opponent.o demo/test.o $(LIBNAME) +test test_standalone: demo/main.o demo/opponent.o demo/test.o $(LIBNAME) $(CC) $(CFLAGS) $^ $(LFLAGS) -o test .PHONY: mtest diff --git a/makefile.shared b/makefile.shared index 25e2fe5..e6bb1ef 100644 --- a/makefile.shared +++ b/makefile.shared @@ -57,7 +57,7 @@ bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_s objs: $(OBJECTS) -.c.o: +.c.o: $(HEADERS) $(LTCOMPILE) $(CFLAGS) $(LDFLAGS) -o $@ -c $< LOBJECTS = $(OBJECTS:.o=.lo) @@ -79,17 +79,8 @@ uninstall: rm $(HEADERS_PUB:%=$(DESTDIR)$(INCPATH)/%) rm $(DESTDIR)$(LIBPATH)/pkgconfig/libtommath.pc -test: $(LIBNAME) - $(LTCOMPILE) $(CFLAGS) -c demo/main.c -o demo/main.o - $(LTCOMPILE) $(CFLAGS) -c demo/opponent.c -o demo/opponent.o - $(LTCOMPILE) $(CFLAGS) -c demo/test.c -o demo/test.o - $(LTLINK) $(LDFLAGS) -o test demo/main.o demo/opponent.o demo/test.o $(LIBNAME) - -test_standalone: $(LIBNAME) - $(LTCOMPILE) $(CFLAGS) -c demo/main.c -o demo/main.o - $(LTCOMPILE) $(CFLAGS) -c demo/opponent.c -o demo/opponent.o - $(LTCOMPILE) $(CFLAGS) -c demo/test.c -o demo/test.o - $(LTLINK) $(LDFLAGS) -o test demo/main.o demo/opponent.o demo/test.o $(LIBNAME) +test test_standalone: demo/main.o demo/opponent.o demo/test.o $(LIBNAME) + $(LTLINK) $(LDFLAGS) $^ -o test .PHONY: mtest mtest: From 1cd8ab1652612a2a8bb18292b43e47c3a4d96c21 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 5 Sep 2019 16:59:23 +0200 Subject: [PATCH 03/11] fix define --- bn_s_mp_rand_platform.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bn_s_mp_rand_platform.c b/bn_s_mp_rand_platform.c index 950faed..07555db 100644 --- a/bn_s_mp_rand_platform.c +++ b/bn_s_mp_rand_platform.c @@ -121,7 +121,7 @@ static mp_err s_read_urandom(void *p, size_t n) #endif #if defined(MP_PRNG_ENABLE_LTM_RNG) -#define B_S_READ_LTM_RNG +#define BN_S_READ_LTM_RNG unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void)); void (*ltm_rng_callback)(void); From 54661db6a4db1fe4879f04ec6e747b4953cdd1b8 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 5 Sep 2019 17:10:48 +0200 Subject: [PATCH 04/11] allow RSA superclass to be built with tests --- demo/opponent.c | 2 ++ tommath_superclass.h | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/demo/opponent.c b/demo/opponent.c index 69c1c85..dc0893c 100644 --- a/demo/opponent.c +++ b/demo/opponent.c @@ -35,9 +35,11 @@ int mtest_opponent(void) div2_n = mul2_n = inv_n = expt_n = lcm_n = gcd_n = add_n = sub_n = mul_n = div_n = sqr_n = mul2d_n = div2d_n = add_d_n = sub_d_n = 0; +#ifndef MP_FIXED_CUTOFFS /* force KARA and TOOM to enable despite cutoffs */ KARATSUBA_SQR_CUTOFF = KARATSUBA_MUL_CUTOFF = 8; TOOM_SQR_CUTOFF = TOOM_MUL_CUTOFF = 16; +#endif for (;;) { /* randomly clear and re-init one variable, this has the affect of triming the alloc space */ diff --git a/tommath_superclass.h b/tommath_superclass.h index cf17866..df23490 100644 --- a/tommath_superclass.h +++ b/tommath_superclass.h @@ -4,10 +4,13 @@ /* super class file for PK algos */ /* default ... include all MPI */ +#ifndef LTM_NOTHING #define LTM_ALL +#endif /* RSA only (does not support DH/DSA/ECC) */ /* #define SC_RSA_1 */ +/* #define SC_RSA_1_WITH_TESTS */ /* For reference.... On an Athlon64 optimizing for speed... @@ -15,11 +18,38 @@ */ +#ifdef SC_RSA_1_WITH_TESTS +# define BN_CUTOFFS_C +# define BN_MP_ERROR_TO_STRING_C +# define BN_MP_FREAD_C +# define BN_MP_FWRITE_C +# define BN_MP_INCR_C +# define BN_MP_ISEVEN_C +# define BN_MP_ISODD_C +# define BN_MP_NEG_C +# define BN_MP_PRIME_FROBENIUS_UNDERWOOD_C +# define BN_MP_RADIX_SIZE_C +# define BN_MP_RADIX_SMAP_C +# define BN_MP_RAND_C +# define BN_MP_REDUCE_C +# define BN_MP_REDUCE_2K_L_C +# define BN_MP_READ_SIGNED_BIN_C +# define BN_MP_ROOT_U32_C +# define BN_MP_SET_L_C +# define BN_MP_SET_UL_C +# define BN_MP_SIGNED_BIN_SIZE_C +# define BN_MP_TO_RADIX_C +# define BN_MP_TO_SIGNED_BIN_C +# define BN_S_MP_RAND_JENKINS_C +# define BN_S_MP_RAND_PLATFORM_C +#endif + /* Works for RSA only, mpi.o is 68KiB */ -#ifdef SC_RSA_1 +#if defined(SC_RSA_1) || defined (SC_RSA_1_WITH_TESTS) # define BN_MP_SHRINK_C # define BN_MP_LCM_C -# define BN_MP_PRIME_RANDOM_EX_C +# define BN_MP_PRIME_RAND_C +# define BN_MP_PRIME_IS_PRIME_C # define BN_MP_INVMOD_C # define BN_MP_GCD_C # define BN_MP_MOD_C @@ -39,6 +69,7 @@ /* other modifiers */ # define BN_MP_DIV_SMALL /* Slower division, not critical */ + /* here we are on the last pass so we turn things off. The functions classes are still there * but we remove them specifically from the build. This also invokes tweaks in functions * like removing support for even moduli, etc... @@ -48,7 +79,6 @@ # undef BN_S_MP_TOOM_SQR_C # undef BN_S_MP_KARATSUBA_MUL_C # undef BN_S_MP_KARATSUBA_SQR_C -# undef BN_MP_REDUCE_C # undef BN_MP_REDUCE_SETUP_C # undef BN_MP_DR_IS_MODULUS_C # undef BN_MP_DR_SETUP_C @@ -62,6 +92,10 @@ # undef BN_S_MP_MUL_HIGH_DIGS_FAST_C # undef BN_S_MP_INVMOD_FAST_C +# ifndef SC_RSA_1_WITH_TESTS +# undef BN_MP_REDUCE_C +# endif + /* To safely undefine these you have to make sure your RSA key won't exceed the Comba threshold * which is roughly 255 digits [7140 bits for 32-bit machines, 15300 bits for 64-bit machines] * which means roughly speaking you can handle upto 2536-bit RSA keys with these defined without From 75c52a893b4ac3d96373c020b638cc04dba73d44 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 5 Sep 2019 17:12:05 +0200 Subject: [PATCH 05/11] be nice and tell when it's hex output did you know that 0x35 is equal to 53? ... ;-) --- demo/test.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/demo/test.c b/demo/test.c index 8da3596..9c65eac 100644 --- a/demo/test.c +++ b/demo/test.c @@ -977,7 +977,7 @@ static int test_mp_prime_is_prime(void) printf("A certified prime is a prime but mp_prime_is_prime says it is not.\n"); } if ((err != MP_OKAY) || (cnt == MP_NO)) { - printf("prime tested was: "); + printf("prime tested was: 0x"); mp_fwrite(&a,16,stdout); putchar('\n'); goto LBL_ERR; @@ -1011,10 +1011,10 @@ static int test_mp_prime_is_prime(void) printf("\nfrobenius-underwood says sub is not prime!\n"); } if ((err != MP_OKAY) || (cnt == MP_NO)) { - printf("prime tested was: "); + printf("prime tested was: 0x"); mp_fwrite(&a,16,stdout); putchar('\n'); - printf("sub tested was: "); + printf("sub tested was: 0x"); mp_fwrite(&b,16,stdout); putchar('\n'); goto LBL_ERR; @@ -1036,7 +1036,7 @@ static int test_mp_prime_is_prime(void) printf("\n\nissue #143 - mp_prime_strong_lucas_selfridge FAILED!\n"); } if ((err != MP_OKAY) || (cnt == MP_NO)) { - printf("prime tested was: "); + printf("prime tested was: 0x"); mp_fwrite(&a,16,stdout); putchar('\n'); goto LBL_ERR; From c4e205183a78d1dc8fb4bec833e38cdbbdbf08b6 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 5 Sep 2019 17:26:34 +0200 Subject: [PATCH 06/11] add testbuild for RSA superclass --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index dba45d4..37b0d9d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -90,6 +90,9 @@ matrix: - libc6-dev-i386 - gcc-multilib + # RSA superclass with tests (no sanitizer, but debug info) + - env: COMPILE_DEBUG=1 BUILDOPTIONS='--with-cc=gcc-5 --with-m64 --cflags=-DLTM_NOTHING --cflags=-DSC_RSA_1_WITH_TESTS --with-travis-valgrind' + # Test "autotuning", the automatic evaluation and setting of the Toom-Cook cut-offs. #- env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT --with-travis-valgrind --make-option=tune' #- env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-travis-valgrind --make-option=tune' From 8721ca12390a4fcecd9c2edc8d3e44978d6bf7f0 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Fri, 6 Sep 2019 10:18:36 +0200 Subject: [PATCH 07/11] order defines alphabetically --- tommath_superclass.h | 46 ++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/tommath_superclass.h b/tommath_superclass.h index df23490..ee12ea8 100644 --- a/tommath_superclass.h +++ b/tommath_superclass.h @@ -46,25 +46,25 @@ /* Works for RSA only, mpi.o is 68KiB */ #if defined(SC_RSA_1) || defined (SC_RSA_1_WITH_TESTS) -# define BN_MP_SHRINK_C -# define BN_MP_LCM_C -# define BN_MP_PRIME_RAND_C -# define BN_MP_PRIME_IS_PRIME_C -# define BN_MP_INVMOD_C -# define BN_MP_GCD_C -# define BN_MP_MOD_C -# define BN_MP_MULMOD_C # define BN_MP_ADDMOD_C -# define BN_MP_EXPTMOD_C -# define BN_MP_SET_INT_C -# define BN_MP_INIT_MULTI_C # define BN_MP_CLEAR_MULTI_C -# define BN_MP_UNSIGNED_BIN_SIZE_C -# define BN_MP_TO_UNSIGNED_BIN_C +# define BN_MP_EXPTMOD_C +# define BN_MP_GCD_C +# define BN_MP_INIT_MULTI_C +# define BN_MP_INVMOD_C +# define BN_MP_LCM_C +# define BN_MP_MOD_C # define BN_MP_MOD_D_C +# define BN_MP_MULMOD_C +# define BN_MP_PRIME_IS_PRIME_C # define BN_MP_PRIME_RABIN_MILLER_TRIALS_C -# define BN_S_MP_REVERSE_C +# define BN_MP_PRIME_RAND_C +# define BN_MP_SET_INT_C +# define BN_MP_SHRINK_C +# define BN_MP_TO_UNSIGNED_BIN_C +# define BN_MP_UNSIGNED_BIN_SIZE_C # define BN_PRIME_TAB_C +# define BN_S_MP_REVERSE_C /* other modifiers */ # define BN_MP_DIV_SMALL /* Slower division, not critical */ @@ -75,22 +75,22 @@ * like removing support for even moduli, etc... */ # ifdef LTM_LAST -# undef BN_S_MP_TOOM_MUL_C -# undef BN_S_MP_TOOM_SQR_C -# undef BN_S_MP_KARATSUBA_MUL_C -# undef BN_S_MP_KARATSUBA_SQR_C -# undef BN_MP_REDUCE_SETUP_C # undef BN_MP_DR_IS_MODULUS_C # undef BN_MP_DR_SETUP_C # undef BN_MP_DR_REDUCE_C -# undef BN_MP_REDUCE_IS_2K_C +# undef BN_MP_DIV_3_C # undef BN_MP_REDUCE_2K_SETUP_C # undef BN_MP_REDUCE_2K_C +# undef BN_MP_REDUCE_IS_2K_C +# undef BN_MP_REDUCE_SETUP_C # undef BN_S_MP_EXPTMOD_C -# undef BN_MP_DIV_3_C +# undef BN_S_MP_INVMOD_FAST_C +# undef BN_S_MP_KARATSUBA_MUL_C +# undef BN_S_MP_KARATSUBA_SQR_C # undef BN_S_MP_MUL_HIGH_DIGS_C # undef BN_S_MP_MUL_HIGH_DIGS_FAST_C -# undef BN_S_MP_INVMOD_FAST_C +# undef BN_S_MP_TOOM_MUL_C +# undef BN_S_MP_TOOM_SQR_C # ifndef SC_RSA_1_WITH_TESTS # undef BN_MP_REDUCE_C @@ -101,9 +101,9 @@ * which means roughly speaking you can handle upto 2536-bit RSA keys with these defined without * trouble. */ +# undef BN_MP_MONTGOMERY_REDUCE_C # undef BN_S_MP_MUL_DIGS_C # undef BN_S_MP_SQR_C -# undef BN_MP_MONTGOMERY_REDUCE_C # endif #endif From 702e5391d06b351847afb0152dedf4f44e53a615 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Fri, 6 Sep 2019 10:22:45 +0200 Subject: [PATCH 08/11] add function pointers to tommath_class.h --- helper.pl | 2 +- tommath_class.h | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/helper.pl b/helper.pl index bde8cdf..e60c1a7 100755 --- a/helper.pl +++ b/helper.pl @@ -381,7 +381,7 @@ EOS # scan for mp_* and make classes my @deps = (); foreach my $line (split /\n/, $content) { - while ($line =~ /(fast_)?(s_)?mp\_[a-z_0-9]*(?=\()|(?<=\()mp\_[a-z_0-9]*(?=,)/g) { + while ($line =~ /(fast_)?(s_)?mp\_[a-z_0-9]*((?=\;)|(?=\())|(?<=\()mp\_[a-z_0-9]*(?=\()/g) { my $a = $&; next if $a eq "mp_err"; $a =~ tr/[a-z]/[A-Z]/; diff --git a/tommath_class.h b/tommath_class.h index 24122d1..32d1b2d 100644 --- a/tommath_class.h +++ b/tommath_class.h @@ -827,6 +827,7 @@ # define BN_MP_GROW_C # define BN_MP_RAND_SOURCE_C # define BN_MP_ZERO_C +# define BN_S_MP_RAND_PLATFORM_C # define BN_S_MP_RAND_SOURCE_C #endif @@ -1118,7 +1119,9 @@ # define BN_MP_INIT_C # define BN_MP_MOD_C # define BN_MP_MUL_C +# define BN_MP_REDUCE_2K_L_C # define BN_MP_REDUCE_2K_SETUP_L_C +# define BN_MP_REDUCE_C # define BN_MP_REDUCE_SETUP_C # define BN_MP_SET_C # define BN_MP_SQR_C @@ -1128,17 +1131,21 @@ # define BN_MP_CLEAR_C # define BN_MP_COPY_C # define BN_MP_COUNT_BITS_C +# define BN_MP_DR_REDUCE_C # define BN_MP_DR_SETUP_C # define BN_MP_EXCH_C # define BN_MP_INIT_SIZE_C # define BN_MP_MOD_C # define BN_MP_MONTGOMERY_CALC_NORMALIZATION_C +# define BN_MP_MONTGOMERY_REDUCE_C # define BN_MP_MONTGOMERY_SETUP_C # define BN_MP_MULMOD_C # define BN_MP_MUL_C +# define BN_MP_REDUCE_2K_C # define BN_MP_REDUCE_2K_SETUP_C # define BN_MP_SET_C # define BN_MP_SQR_C +# define BN_S_MP_MONTGOMERY_REDUCE_FAST_C #endif #if defined(BN_S_MP_GET_BIT_C) From 7482b3a95ae0fa206e308159ef07485eda79f2aa Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Fri, 6 Sep 2019 11:51:10 +0200 Subject: [PATCH 09/11] fix missing symbols when building `SC_RSA_1` --- tommath_superclass.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tommath_superclass.h b/tommath_superclass.h index ee12ea8..b007c24 100644 --- a/tommath_superclass.h +++ b/tommath_superclass.h @@ -19,7 +19,6 @@ */ #ifdef SC_RSA_1_WITH_TESTS -# define BN_CUTOFFS_C # define BN_MP_ERROR_TO_STRING_C # define BN_MP_FREAD_C # define BN_MP_FWRITE_C @@ -29,7 +28,6 @@ # define BN_MP_NEG_C # define BN_MP_PRIME_FROBENIUS_UNDERWOOD_C # define BN_MP_RADIX_SIZE_C -# define BN_MP_RADIX_SMAP_C # define BN_MP_RAND_C # define BN_MP_REDUCE_C # define BN_MP_REDUCE_2K_L_C @@ -46,6 +44,7 @@ /* Works for RSA only, mpi.o is 68KiB */ #if defined(SC_RSA_1) || defined (SC_RSA_1_WITH_TESTS) +# define BN_CUTOFFS_C # define BN_MP_ADDMOD_C # define BN_MP_CLEAR_MULTI_C # define BN_MP_EXPTMOD_C @@ -59,6 +58,7 @@ # define BN_MP_PRIME_IS_PRIME_C # define BN_MP_PRIME_RABIN_MILLER_TRIALS_C # define BN_MP_PRIME_RAND_C +# define BN_MP_RADIX_SMAP_C # define BN_MP_SET_INT_C # define BN_MP_SHRINK_C # define BN_MP_TO_UNSIGNED_BIN_C @@ -83,6 +83,7 @@ # undef BN_MP_REDUCE_2K_C # undef BN_MP_REDUCE_IS_2K_C # undef BN_MP_REDUCE_SETUP_C +# undef BN_S_MP_BALANCE_MUL_C # undef BN_S_MP_EXPTMOD_C # undef BN_S_MP_INVMOD_FAST_C # undef BN_S_MP_KARATSUBA_MUL_C From 1687b152490ad414fce2d2806cdb5798299dee4f Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Fri, 6 Sep 2019 16:10:44 +0200 Subject: [PATCH 10/11] allow the user to override {C,L,LD}FLAGS --- etc/makefile | 17 +++++++++-------- makefile | 16 ++++++++-------- makefile.shared | 14 +++++++------- makefile_include.mk | 43 +++++++++++++++++++++++++------------------ 4 files changed, 49 insertions(+), 41 deletions(-) diff --git a/etc/makefile b/etc/makefile index ce9cf06..bad1741 100644 --- a/etc/makefile +++ b/etc/makefile @@ -1,4 +1,5 @@ -CFLAGS += -Wall -W -Wextra -Wshadow -O3 -I../ +LTM_CFLAGS += -Wall -W -Wextra -Wshadow -O3 -I../ +LTM_CFLAGS += $(CFLAGS) # default lib name (requires install with root) # LIBNAME=-ltommath @@ -8,31 +9,31 @@ LIBNAME=../libtommath.a #provable primes pprime: pprime.o - $(CC) $(CFLAGS) pprime.o $(LIBNAME) -o pprime + $(CC) $(LTM_CFLAGS) pprime.o $(LIBNAME) -o pprime # portable [well requires clock()] tuning app tune: tune.o - $(CC) $(CFLAGS) tune.o $(LIBNAME) -o tune + $(CC) $(LTM_CFLAGS) tune.o $(LIBNAME) -o tune ./tune_it.sh test_standalone: tune.o # The benchmark program works as a testtool, too - $(CC) $(CFLAGS) tune.o $(LIBNAME) -o test + $(CC) $(LTM_CFLAGS) tune.o $(LIBNAME) -o test # spits out mersenne primes mersenne: mersenne.o - $(CC) $(CFLAGS) mersenne.o $(LIBNAME) -o mersenne + $(CC) $(LTM_CFLAGS) mersenne.o $(LIBNAME) -o mersenne # finds DR safe primes for the given config drprime: drprime.o - $(CC) $(CFLAGS) drprime.o $(LIBNAME) -o drprime + $(CC) $(LTM_CFLAGS) drprime.o $(LIBNAME) -o drprime # finds 2k safe primes for the given config 2kprime: 2kprime.o - $(CC) $(CFLAGS) 2kprime.o $(LIBNAME) -o 2kprime + $(CC) $(LTM_CFLAGS) 2kprime.o $(LIBNAME) -o 2kprime mont: mont.o - $(CC) $(CFLAGS) mont.o $(LIBNAME) -o mont + $(CC) $(LTM_CFLAGS) mont.o $(LIBNAME) -o mont clean: diff --git a/makefile b/makefile index 8de82d1..89d2b3b 100644 --- a/makefile +++ b/makefile @@ -21,7 +21,7 @@ include makefile_include.mk ifneq ($V,1) @echo " * ${CC} $@" endif - ${silent} ${CC} -c ${CFLAGS} $< -o $@ + ${silent} ${CC} -c ${LTM_CFLAGS} $< -o $@ LCOV_ARGS=--directory . @@ -77,11 +77,11 @@ profiled: #make a single object profiled library profiled_single: perl gen.pl - $(CC) $(CFLAGS) -fprofile-arcs -DTESTING -c mpi.c -o mpi.o - $(CC) $(CFLAGS) -DTESTING -DTIMER demo/timing.c mpi.o -lgcov -o timing + $(CC) $(LTM_CFLAGS) -fprofile-arcs -DTESTING -c mpi.c -o mpi.o + $(CC) $(LTM_CFLAGS) -DTESTING -DTIMER demo/timing.c mpi.o -lgcov -o timing ./timing rm -f *.o timing - $(CC) $(CFLAGS) -fbranch-probabilities -DTESTING -c mpi.c -o mpi.o + $(CC) $(LTM_CFLAGS) -fbranch-probabilities -DTESTING -c mpi.c -o mpi.o $(AR) $(ARFLAGS) $(LIBNAME) mpi.o ranlib $(LIBNAME) @@ -96,17 +96,17 @@ uninstall: rm $(HEADERS_PUB:%=$(DESTDIR)$(INCPATH)/%) test test_standalone: demo/main.o demo/opponent.o demo/test.o $(LIBNAME) - $(CC) $(CFLAGS) $^ $(LFLAGS) -o test + $(CC) $(LTM_CFLAGS) $^ $(LTM_LFLAGS) -o test .PHONY: mtest mtest: - cd mtest ; $(CC) $(CFLAGS) -O0 mtest.c $(LFLAGS) -o mtest + cd mtest ; $(CC) $(LTM_CFLAGS) -O0 mtest.c $(LTM_LFLAGS) -o mtest timing: $(LIBNAME) demo/timing.c - $(CC) $(CFLAGS) -DTIMER demo/timing.c $(LIBNAME) $(LFLAGS) -o timing + $(CC) $(LTM_CFLAGS) -DTIMER demo/timing.c $(LIBNAME) $(LTM_LFLAGS) -o timing tune: $(LIBNAME) - $(MAKE) -C etc tune + $(MAKE) -C etc tune CFLAGS="$(LTM_CFLAGS)" $(MAKE) # You have to create a file .coveralls.yml with the content "repo_token: " diff --git a/makefile.shared b/makefile.shared index e6bb1ef..f77b5e5 100644 --- a/makefile.shared +++ b/makefile.shared @@ -58,12 +58,12 @@ bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_s objs: $(OBJECTS) .c.o: $(HEADERS) - $(LTCOMPILE) $(CFLAGS) $(LDFLAGS) -o $@ -c $< + $(LTCOMPILE) $(LTM_CFLAGS) $(LTM_LDFLAGS) -o $@ -c $< LOBJECTS = $(OBJECTS:.o=.lo) $(LIBNAME): $(OBJECTS) - $(LTLINK) $(LDFLAGS) $(LOBJECTS) -o $(LIBNAME) -rpath $(LIBPATH) -version-info $(VERSION_SO) $(LIBTOOLFLAGS) + $(LTLINK) $(LTM_LDFLAGS) $(LOBJECTS) -o $(LIBNAME) -rpath $(LIBPATH) -version-info $(VERSION_SO) $(LTM_LIBTOOLFLAGS) install: $(LIBNAME) install -d $(DESTDIR)$(LIBPATH) @@ -80,17 +80,17 @@ uninstall: rm $(DESTDIR)$(LIBPATH)/pkgconfig/libtommath.pc test test_standalone: demo/main.o demo/opponent.o demo/test.o $(LIBNAME) - $(LTLINK) $(LDFLAGS) $^ -o test + $(LTLINK) $(LTM_LDFLAGS) $^ -o test .PHONY: mtest mtest: - cd mtest ; $(CC) $(CFLAGS) -O0 mtest.c $(LDFLAGS) -o mtest + cd mtest ; $(CC) $(LTM_CFLAGS) -O0 mtest.c $(LTM_LDFLAGS) -o mtest timing: $(LIBNAME) demo/timing.c - $(LTLINK) $(CFLAGS) $(LDFLAGS) -DTIMER demo/timing.c $(LIBNAME) -o timing + $(LTLINK) $(LTM_CFLAGS) $(LTM_LDFLAGS) -DTIMER demo/timing.c $(LIBNAME) -o timing tune: $(LIBNAME) - $(LTCOMPILE) $(CFLAGS) -c etc/tune.c -o etc/tune.o - $(LTLINK) $(LDFLAGS) -o etc/tune etc/tune.o $(LIBNAME) + $(LTCOMPILE) $(LTM_CFLAGS) -c etc/tune.c -o etc/tune.o + $(LTLINK) $(LTM_LDFLAGS) -o etc/tune etc/tune.o $(LIBNAME) cd etc/; /bin/sh tune_it.sh; cd .. $(MAKE) -f makefile.shared diff --git a/makefile_include.mk b/makefile_include.mk index 9303ffc..a766343 100644 --- a/makefile_include.mk +++ b/makefile_include.mk @@ -47,60 +47,67 @@ else endif endif -CFLAGS += -I./ -Wall -Wsign-compare -Wextra -Wshadow +LTM_CFLAGS += -I./ -Wall -Wsign-compare -Wextra -Wshadow ifdef SANITIZER -CFLAGS += -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=float-divide-by-zero +LTM_CFLAGS += -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=float-divide-by-zero endif ifndef NO_ADDTL_WARNINGS # additional warnings -CFLAGS += -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align -CFLAGS += -Wstrict-prototypes -Wpointer-arith +LTM_CFLAGS += -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align +LTM_CFLAGS += -Wstrict-prototypes -Wpointer-arith endif ifdef CONV_WARNINGS -CFLAGS += -std=c89 -Wconversion -Wsign-conversion +LTM_CFLAGS += -std=c89 -Wconversion -Wsign-conversion ifeq ($(CONV_WARNINGS), strict) -CFLAGS += -DMP_USE_ENUMS -Wc++-compat +LTM_CFLAGS += -DMP_USE_ENUMS -Wc++-compat endif else -CFLAGS += -Wsystem-headers +LTM_CFLAGS += -Wsystem-headers endif ifdef COMPILE_DEBUG #debug -CFLAGS += -g3 +LTM_CFLAGS += -g3 endif ifdef COMPILE_SIZE #for size -CFLAGS += -Os +LTM_CFLAGS += -Os else ifndef IGNORE_SPEED #for speed -CFLAGS += -O3 -funroll-loops +LTM_CFLAGS += -O3 -funroll-loops #x86 optimizations [should be valid for any GCC install though] -CFLAGS += -fomit-frame-pointer +LTM_CFLAGS += -fomit-frame-pointer endif endif # COMPILE_SIZE ifneq ($(findstring clang,$(CC)),) -CFLAGS += -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header +LTM_CFLAGS += -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header endif ifneq ($(findstring mingw,$(CC)),) -CFLAGS += -Wno-shadow +LTM_CFLAGS += -Wno-shadow endif ifeq ($(PLATFORM), Darwin) -CFLAGS += -Wno-nullability-completeness +LTM_CFLAGS += -Wno-nullability-completeness endif ifeq ($(PLATFORM), CYGWIN) LIBTOOLFLAGS += -no-undefined endif +# add in the standard FLAGS +LTM_CFLAGS += $(CFLAGS) +LTM_LFLAGS += $(LFLAGS) +LTM_LDFLAGS += $(LDFLAGS) +LTM_LIBTOOLFLAGS += $(LIBTOOLFLAGS) + + ifeq ($(PLATFORM),FreeBSD) _ARCH := $(shell sysctl -b hw.machine_arch) else @@ -119,7 +126,7 @@ endif HEADERS_PUB=tommath.h HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB) -test_standalone: CFLAGS+=-DLTM_DEMO_TEST_VS_MTEST=0 +test_standalone: LTM_CFLAGS+=-DLTM_DEMO_TEST_VS_MTEST=0 #LIBPATH The directory for libtommath to be installed to. #INCPATH The directory to install the header files for libtommath. @@ -132,9 +139,9 @@ DATAPATH ?= $(PREFIX)/share/doc/libtommath/pdf #make the code coverage of the library # -coverage: CFLAGS += -fprofile-arcs -ftest-coverage -DTIMING_NO_LOGS -coverage: LFLAGS += -lgcov -coverage: LDFLAGS += -lgcov +coverage: LTM_CFLAGS += -fprofile-arcs -ftest-coverage -DTIMING_NO_LOGS +coverage: LTM_LFLAGS += -lgcov +coverage: LTM_LDFLAGS += -lgcov coverage: $(COVERAGE) $(COVERAGE_APP) From 234d474fe6b78b3414caff7f931449230325e6ac Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Sat, 7 Sep 2019 15:01:46 +0200 Subject: [PATCH 11/11] only run tests if the module is available --- demo/test.c | 107 +++++++++++++++++++++++++++++----------------------- 1 file changed, 59 insertions(+), 48 deletions(-) diff --git a/demo/test.c b/demo/test.c index 9c65eac..79cfb38 100644 --- a/demo/test.c +++ b/demo/test.c @@ -2220,53 +2220,58 @@ int unit_tests(int argc, char **argv) const char *name; int (*fn)(void); } test[] = { -#define T(n) { #n, test_##n } - T(feature_detection), - T(trivial_stuff), - T(mp_get_set_i32), - T(mp_get_set_i64), - T(mp_and), - T(mp_cnt_lsb), - T(mp_complement), - T(mp_decr), - T(mp_div_3), - T(mp_dr_reduce), - T(mp_fread_fwrite), - T(mp_get_u32), - T(mp_get_u64), - T(mp_get_ul), - T(mp_ilogb), - T(mp_incr), - T(mp_invmod), - T(mp_is_square), - T(mp_kronecker), - T(mp_montgomery_reduce), - T(mp_root_u32), - T(mp_or), - T(mp_prime_is_prime), - T(mp_prime_next_prime), - T(mp_prime_rand), - T(mp_rand), - T(mp_read_radix), - T(mp_reduce_2k), - T(mp_reduce_2k_l), +#define T0(n) { #n, test_##n } +#define T1(n, o) { #n, MP_HAS(o) ? test_##n : NULL } +#define T2(n, o1, o2) { #n, MP_HAS(o1) && MP_HAS(o2) ? test_##n : NULL } + T0(feature_detection), + T0(trivial_stuff), + T2(mp_get_set_i32, MP_GET_I32, MP_GET_MAG_U32), + T2(mp_get_set_i64, MP_GET_I64, MP_GET_MAG_U64), + T1(mp_and, MP_AND), + T1(mp_cnt_lsb, MP_CNT_LSB), + T1(mp_complement, MP_COMPLEMENT), + T1(mp_decr, MP_DECR), + T1(mp_div_3, MP_DIV_3), + T1(mp_dr_reduce, MP_DR_REDUCE), + T2(mp_fread_fwrite, MP_FREAD, MP_FWRITE), + T1(mp_get_u32, MP_GET_I32), + T1(mp_get_u64, MP_GET_I64), + T1(mp_get_ul, MP_GET_L), + T1(mp_ilogb, MP_ILOGB), + T1(mp_incr, MP_INCR), + T1(mp_invmod, MP_INVMOD), + T1(mp_is_square, MP_IS_SQUARE), + T1(mp_kronecker, MP_KRONECKER), + T1(mp_montgomery_reduce, MP_MONTGOMERY_REDUCE), + T1(mp_root_u32, MP_ROOT_U32), + T1(mp_or, MP_OR), + T1(mp_prime_is_prime, MP_PRIME_IS_PRIME), + T1(mp_prime_next_prime, MP_PRIME_NEXT_PRIME), + T1(mp_prime_rand, MP_PRIME_RAND), + T1(mp_rand, MP_RAND), + T1(mp_read_radix, MP_READ_RADIX), + T1(mp_reduce_2k, MP_REDUCE_2K), + T1(mp_reduce_2k_l, MP_REDUCE_2K_L), #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) - T(mp_set_double), + T1(mp_set_double, MP_SET_DOUBLE), #endif - T(mp_signed_rsh), - T(mp_sqrt), - T(mp_sqrtmod_prime), - T(mp_xor), - T(s_mp_balance_mul), - T(s_mp_karatsuba_mul), - T(s_mp_karatsuba_sqr), - T(s_mp_toom_mul), - T(s_mp_toom_sqr) -#undef T + T1(mp_signed_rsh, MP_SIGNED_RSH), + T1(mp_sqrt, MP_SQRT), + T1(mp_sqrtmod_prime, MP_SQRTMOD_PRIME), + T1(mp_xor, MP_XOR), + T1(s_mp_balance_mul, S_MP_BALANCE_MUL), + T1(s_mp_karatsuba_mul, S_MP_KARATSUBA_MUL), + T1(s_mp_karatsuba_sqr, S_MP_KARATSUBA_SQR), + T1(s_mp_toom_mul, S_MP_TOOM_MUL), + T1(s_mp_toom_sqr, S_MP_TOOM_SQR), +#undef T2 +#undef T1 }; - unsigned long i; + unsigned long i, ok, fail, nop; uint64_t t; - int res = EXIT_SUCCESS, j; + int j; + + ok = fail = nop = 0; t = (uint64_t)time(NULL); printf("SEED: 0x%"PRIx64"\n\n", t); @@ -2283,13 +2288,19 @@ int unit_tests(int argc, char **argv) if (j == argc) continue; } printf("TEST %s\n\n", test[i].name); - if (test[i].fn() != EXIT_SUCCESS) { + if (test[i].fn == NULL) { + nop++; + printf("NOP %s\n\n", test[i].name); + } else if (test[i].fn() == EXIT_SUCCESS) { + ok++; + printf("\n\n"); + } else { + fail++; printf("\n\nFAIL %s\n\n", test[i].name); - res = EXIT_FAILURE; - break; } - printf("\n\n"); } + printf("Tests OK/NOP/FAIL: %lu/%lu/%lu\n", ok, nop, fail); - return res; + if (fail != 0) return EXIT_FAILURE; + else return EXIT_SUCCESS; }