Commit 759a926cfe34827e3c3e9acaf10578d0a294ec3c

Steffen Jaeckel 2014-09-28T13:12:10

minor changes & clean-up & small fixes gitignore: add libtool output booker.pl: fix source code parsing and trim trailing spaces demo/demo: fix compiler warning, add informational output demo/timing & etc/tune: fix TIMFUNC() makefile: minor changes makefile.shared: increase version, add missing mp_balance_mul tommath: make sure that DIGIT_BIT is correct

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/booker.pl b/booker.pl
index 3dc1c97..7d81ece 100644
--- a/booker.pl
+++ b/booker.pl
@@ -82,7 +82,7 @@ while (<IN>) {
          # scan till next end of comment, e.g. skip license
          while (<SRC>) {
             $text[$line++] = $_;
-            last if ($_ =~ /math\.libtomcrypt\.com/);
+            last if ($_ =~ /libtom\.org/);
          }
          <SRC>;
       }
@@ -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 591d67c..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 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