diff --git a/.gitignore b/.gitignore index 840488d..373c3ad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,13 @@ # suppress compiler/linker output *.[oa] +*.l[oa] *.obj *.gcda *.gcno *.lib Debug/ Release/ +.libs/ # suppress output of build process and *nix/windows test executables ltmtest diff --git a/bn_mp_get_int.c b/bn_mp_get_int.c index 27e3351..c4673fb 100644 --- a/bn_mp_get_int.c +++ b/bn_mp_get_int.c @@ -19,7 +19,7 @@ unsigned long mp_get_int(mp_int * a) { int i; - unsigned long res; + mp_min_u32 res; if (a->used == 0) { return 0; diff --git a/bn_mp_montgomery_setup.c b/bn_mp_montgomery_setup.c index cc99d9b..17a2a3c 100644 --- a/bn_mp_montgomery_setup.c +++ b/bn_mp_montgomery_setup.c @@ -48,7 +48,7 @@ mp_montgomery_setup (mp_int * n, mp_digit * rho) #endif /* rho = -1/m mod b */ - *rho = (unsigned long)(((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK; + *rho = (mp_digit)(((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK; return MP_OKAY; } diff --git a/bn_mp_reduce.c b/bn_mp_reduce.c index 737bc58..e7f25ce 100644 --- a/bn_mp_reduce.c +++ b/bn_mp_reduce.c @@ -33,7 +33,7 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu) mp_rshd (&q, um - 1); /* according to HAC this optimization is ok */ - if (((unsigned long) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) { + if (((mp_digit) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) { if ((res = mp_mul (&q, mu, &q)) != MP_OKAY) { goto CLEANUP; } diff --git a/booker.pl b/booker.pl index 3dc1c97..7d81ece 100644 --- a/booker.pl +++ b/booker.pl @@ -82,7 +82,7 @@ while () { # scan till next end of comment, e.g. skip license while () { $text[$line++] = $_; - last if ($_ =~ /math\.libtomcrypt\.com/); + last if ($_ =~ /libtom\.org/); } ; } @@ -263,3 +263,5 @@ print "Read $readline lines, wrote $wroteline lines\n"; close (OUT); close (IN); + +system('perl -pli -e "s/\s*$//" tommath.tex'); diff --git a/demo/demo.c b/demo/demo.c index e2cc8c0..37dba51 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -31,7 +31,6 @@ #include "tommath.h" -#if LTM_DEMO_TEST_VS_MTEST void ndraw(mp_int * a, char *name) { char buf[16000]; @@ -41,6 +40,7 @@ void ndraw(mp_int * a, char *name) printf("%s\n", buf); } +#if LTM_DEMO_TEST_VS_MTEST static void draw(mp_int * a) { ndraw(a, ""); @@ -97,6 +97,23 @@ int main(void) srand(LTM_DEMO_RAND_SEED); #if LTM_DEMO_TEST_VS_MTEST == 0 +#ifdef MP_8BIT + printf("Digit size 8 Bit \n"); +#endif +#ifdef MP_16BIT + printf("Digit size 16 Bit \n"); +#endif +#ifdef MP_32BIT + printf("Digit size 32 Bit \n"); +#endif +#ifdef MP_64BIT + printf("Digit size 64 Bit \n"); +#endif + printf("Size of mp_digit: %u\n", sizeof(mp_digit)); + printf("Size of mp_word: %u\n", sizeof(mp_word)); + printf("DIGIT_BIT: %d\n", DIGIT_BIT); + printf("MP_PREC: %d\n", MP_PREC); + // test montgomery printf("Testing: montgomery...\n"); for (i = 1; i < 10; i++) { diff --git a/demo/timing.c b/demo/timing.c index 12f30e3..cf94772 100644 --- a/demo/timing.c +++ b/demo/timing.c @@ -44,10 +44,12 @@ static ulong64 TIMFUNC(void) { #if defined __GNUC__ #if defined(__i386__) || defined(__x86_64__) - unsigned long long a; - __asm__ __volatile__("rdtsc\nmovl %%eax,%0\nmovl %%edx,4+%0\n":: - "m"(a):"%eax", "%edx"); - return a; + /* version from http://www.mcs.anl.gov/~kazutomo/rdtsc.html + * the old code always got a warning issued by gcc, clang did not complain... + */ + unsigned hi, lo; + __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); + return ((ulong64)lo)|( ((ulong64)hi)<<32); #else /* gcc-IA64 version */ unsigned long result; __asm__ __volatile__("mov %0=ar.itc":"=r"(result)::"memory"); diff --git a/etc/tune.c b/etc/tune.c index ffdfab4..c2ac998 100644 --- a/etc/tune.c +++ b/etc/tune.c @@ -10,14 +10,19 @@ */ #define TIMES (1UL<<14UL) +#ifndef X86_TIMER + /* RDTSC from Scott Duplichan */ static ulong64 TIMFUNC (void) { #if defined __GNUC__ #if defined(__i386__) || defined(__x86_64__) - unsigned long long a; - __asm__ __volatile__ ("rdtsc\nmovl %%eax,%0\nmovl %%edx,4+%0\n"::"m"(a):"%eax","%edx"); - return a; + /* version from http://www.mcs.anl.gov/~kazutomo/rdtsc.html + * the old code always got a warning issued by gcc, clang did not complain... + */ + unsigned hi, lo; + __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); + return ((ulong64)lo)|( ((ulong64)hi)<<32); #else /* gcc-IA64 version */ unsigned long result; __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory"); @@ -42,8 +47,6 @@ static ulong64 TIMFUNC (void) } -#ifndef X86_TIMER - /* generic ISO C timer */ ulong64 LBL_T; void t_start(void) { LBL_T = TIMFUNC(); } diff --git a/makefile b/makefile index 1868255..ed7bd91 100644 --- a/makefile +++ b/makefile @@ -12,7 +12,9 @@ ifndef PREFIX PREFIX= endif -CC=$(PREFIX)gcc +ifeq ($(CC),cc) + CC = $(PREFIX)gcc +endif LD=$(PREFIX)ld AR=$(PREFIX)ar RANLIB=$(PREFIX)ranlib @@ -116,7 +118,7 @@ profiled: 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 -o ltmtest + $(CC) $(CFLAGS) -DTESTING -DTIMER demo/timing.c mpi.o -lgcov -o ltmtest ./ltmtest rm -f *.o ltmtest $(CC) $(CFLAGS) -fbranch-probabilities -DTESTING -c mpi.c -o mpi.o @@ -132,7 +134,7 @@ install: $(LIBNAME) test: $(LIBNAME) demo/demo.o $(CC) $(CFLAGS) demo/demo.o $(LIBNAME) -o test -mtest: test +mtest: cd mtest ; $(CC) $(CFLAGS) mtest.c -o mtest timing: $(LIBNAME) diff --git a/makefile.shared b/makefile.shared index f4161b1..da31a17 100644 --- a/makefile.shared +++ b/makefile.shared @@ -1,7 +1,7 @@ #Makefile for GCC # #Tom St Denis -VERSION=0:41 +VERSION=0:42 LT ?= libtool LTCOMPILE = $(LT) --mode=compile --tag=CC $(CC) @@ -79,7 +79,8 @@ bn_mp_fread.o bn_mp_fwrite.o bn_mp_cnt_lsb.o bn_error.o \ bn_mp_init_multi.o bn_mp_clear_multi.o bn_mp_exteuclid.o bn_mp_toradix_n.o \ bn_mp_prime_random_ex.o bn_mp_get_int.o bn_mp_sqrt.o bn_mp_is_square.o bn_mp_init_set.o \ bn_mp_init_set_int.o bn_mp_invmod_slow.o bn_mp_prime_rabin_miller_trials.o \ -bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin_n.o bn_mp_import.o bn_mp_export.o +bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin_n.o bn_mp_import.o bn_mp_export.o \ +bn_mp_balance_mul.o objs: $(OBJECTS) @@ -99,7 +100,7 @@ test: $(LIBNAME) demo/demo.o $(CC) $(CFLAGS) -c demo/demo.c -o demo/demo.o $(LT) --mode=link $(CC) $(LDFLAGS) -o test demo/demo.o $(LIBNAME_S) -mtest: test +mtest: cd mtest ; $(CC) $(CFLAGS) $(LDFLAGS) mtest.c -o mtest timing: $(LIBNAME) diff --git a/tommath.h b/tommath.h index 9fd62f3..8a71d3c 100644 --- a/tommath.h +++ b/tommath.h @@ -47,7 +47,7 @@ extern "C" { /* detect 64-bit mode if possible */ #if defined(__x86_64__) - #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT)) + #if !(defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT)) #define MP_64BIT #endif #endif @@ -63,9 +63,15 @@ extern "C" { #ifdef MP_8BIT typedef unsigned char mp_digit; typedef unsigned short mp_word; +#ifdef DIGIT_BIT +#error You must not define DIGIT_BIT when using MP_8BIT +#endif #elif defined(MP_16BIT) typedef unsigned short mp_digit; - typedef unsigned long mp_word; + typedef unsigned int mp_word; +#ifdef DIGIT_BIT +#error You must not define DIGIT_BIT when using MP_16BIT +#endif #elif defined(MP_64BIT) /* for GCC only on supported platforms */ #ifndef CRYPT @@ -73,7 +79,7 @@ extern "C" { typedef signed long long long64; #endif - typedef unsigned long mp_digit; + typedef unsigned long long mp_digit; typedef unsigned long mp_word __attribute__ ((mode(TI))); #define DIGIT_BIT 60 @@ -125,8 +131,12 @@ extern "C" { /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */ #ifndef DIGIT_BIT #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1))) /* bits per digit */ + typedef unsigned long mp_min_u32; +#else + typedef mp_digit mp_min_u32; #endif + #define MP_DIGIT_BIT DIGIT_BIT #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1)) #define MP_DIGIT_MAX MP_MASK