Skip to content

Commit

Permalink
Merge pull request #341 from libtom/improve-tests
Browse files Browse the repository at this point in the history
Improve tests
  • Loading branch information
sjaeckel committed Sep 7, 2019
2 parents e5f00d4 + 234d474 commit c63799c
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 137 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion bn_s_mp_rand_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions demo/opponent.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
121 changes: 68 additions & 53 deletions demo/test.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <inttypes.h>
#include "shared.h"

static long rand_long(void)
Expand Down Expand Up @@ -976,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;
Expand Down Expand Up @@ -1010,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;
Expand All @@ -1035,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;
Expand Down Expand Up @@ -2219,54 +2220,62 @@ 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;
int res = EXIT_SUCCESS, j;
unsigned long i, ok, fail, nop;
uint64_t t;
int j;

s_mp_rand_jenkins_init((uint64_t)time(NULL));
ok = fail = nop = 0;

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) {
Expand All @@ -2279,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;
}
17 changes: 9 additions & 8 deletions etc/makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion helper.pl
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ sub update_dep
# 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]/;
Expand Down
25 changes: 10 additions & 15 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ 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
${silent} ${CC} -c ${CFLAGS} $< -o $@
${silent} ${CC} -c ${LTM_CFLAGS} $< -o $@

LCOV_ARGS=--directory .

Expand Down Expand Up @@ -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) $@
Expand All @@ -79,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)

Expand All @@ -97,21 +95,18 @@ 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)
$(CC) $(CFLAGS) $^ $(LFLAGS) -o test
test test_standalone: demo/main.o demo/opponent.o demo/test.o $(LIBNAME)
$(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: <the token>"
Expand Down
27 changes: 9 additions & 18 deletions makefile.shared
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ 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:
$(LTCOMPILE) $(CFLAGS) $(LDFLAGS) -o $@ -c $<
.c.o: $(HEADERS)
$(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)
Expand All @@ -79,27 +79,18 @@ 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) $(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
Loading

0 comments on commit c63799c

Please sign in to comment.