diff --git a/.gitignore b/.gitignore index e8dc23c..89863b0 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,5 @@ stest stest.exe rsatest rsatest.exe +timing +timing.exe diff --git a/.travis.yml b/.travis.yml index 7539be2..8dbc64e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,18 +1,26 @@ language: c compiler: - gcc -script: CC="${MYCC}" make test >test_gcc_1.txt 2>test_gcc_2.txt && ./test >test_std.txt 2>test_err.txt +script: CC="${MYCC}" make ${SHARED} test_standalone >test_gcc_1.txt 2>test_gcc_2.txt && ./test >test_std.txt 2>test_err.txt env: - - MYCC="gcc" - - MYCC="gcc -m32" - - MYCC="gcc-4.8" - - MYCC="gcc-4.8 -m32" + - MYCC="gcc" SHARED="" + - MYCC="gcc -m32" SHARED="" + - MYCC="gcc-4.8" SHARED="" + - MYCC="gcc-4.8 -m32" SHARED="" + - MYCC="gcc-4.9" SHARED="" + - MYCC="gcc-4.9 -m32" SHARED="" + - MYCC="gcc" SHARED="-f makefile.shared" + - MYCC="gcc -m32" SHARED="-f makefile.shared" + - MYCC="gcc-4.8" SHARED="-f makefile.shared" + - MYCC="gcc-4.8 -m32" SHARED="-f makefile.shared" + - MYCC="gcc-4.9" SHARED="-f makefile.shared" + - MYCC="gcc-4.9 -m32" SHARED="-f makefile.shared" matrix: fast_finish: true before_script: - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test - sudo apt-get -qq update - - sudo apt-get install gcc-4.8-multilib gcc-multilib build-essential + - sudo apt-get install gcc-4.9-multilib gcc-4.8-multilib gcc-multilib build-essential after_failure: - cat test_gcc_1.txt - cat test_std.txt diff --git a/demo/test.c b/demo/test.c index 43981d5..5ee2d1e 100644 --- a/demo/test.c +++ b/demo/test.c @@ -3,6 +3,11 @@ #include #include + +#ifndef TFM_DEMO_TEST_VS_MTEST +#define TFM_DEMO_TEST_VS_MTEST 1 +#endif + void draw(fp_int *a) { int x; @@ -25,69 +30,18 @@ int myrng(unsigned char *dst, int len, void *dat) return len; } -#ifndef TESTING -/* RDTSC from Scott Duplichan */ -static ulong64 TIMFUNC (void) - { - #if defined __GNUC__ - #if defined(INTEL_CC) - ulong64 a; - asm ("rdtsc":"=A"(a)); - return a; - #elif defined(__i386__) || defined(__x86_64__) - /* 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); - #elif defined(TFM_PPC32) - unsigned long a, b; - __asm__ __volatile__ ("mftbu %1 \nmftb %0\n":"=r"(a), "=r"(b)); - return (((ulong64)b) << 32ULL) | ((ulong64)a); - #elif defined(TFM_AVR32) - FILE *in; - char buf[20]; - in = fopen("/sys/devices/system/cpu/cpu0/pccycles", "r"); - fgets(buf, 20, in); - fclose(in); - return strtoul(buf, NULL, 10); - #else /* gcc-IA64 version */ - unsigned long result; - __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory"); - while (__builtin_expect ((int) result == -1, 0)) - __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory"); - return result; - #endif - - // Microsoft and Intel Windows compilers - #elif defined _M_IX86 - __asm rdtsc - #elif defined _M_AMD64 - return __rdtsc (); - #elif defined _M_IA64 - #if defined __INTEL_COMPILER - #include - #endif - return __getReg (3116); - #else - #error need rdtsc function for this build - #endif - } -#endif - char cmd[4096], buf[4096]; int main(void) { fp_int a,b,c,d,e,f; + unsigned long ix; +#if TFM_DEMO_TEST_VS_MTEST unsigned long expt_n, add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n, inv_n, - div2_n, mul2_n, add_d_n, sub_d_n, mul_d_n, cnt, rr, ix; -#ifndef TESTING - unsigned long t; + div2_n, mul2_n, add_d_n, sub_d_n, mul_d_n, cnt, rr; +#else fp_digit fp; int n, err; - ulong64 t1, t2; #endif srand(time(NULL)); @@ -95,7 +49,7 @@ int main(void) fp_zero(&b); fp_zero(&c); fp_zero(&d); fp_zero(&e); fp_zero(&f); fp_zero(&a); -#ifndef TESTING +#if TFM_DEMO_TEST_VS_MTEST == 0 draw(&a); @@ -231,410 +185,9 @@ int main(void) } printf("\n\n"); -#if 1 - -t1 = TIMFUNC(); -sleep(1); -printf("Ticks per second: %llu\n", TIMFUNC() - t1); - - /* do some timings... */ - printf("Addition:\n"); - for (t = 2; t <= FP_SIZE/2; t += 2) { - fp_zero(&a); - fp_zero(&b); - fp_zero(&c); - for (ix = 0; ix < t; ix++) { - a.dp[ix] = ix; - b.dp[ix] = ix; - } - a.used = t; - b.used = t; - t2 = -1; - for (ix = 0; ix < 25000; ++ix) { - t1 = TIMFUNC(); - fp_add(&a, &b, &c); fp_add(&a, &b, &c); - fp_add(&a, &b, &c); fp_add(&a, &b, &c); - fp_add(&a, &b, &c); fp_add(&a, &b, &c); - fp_add(&a, &b, &c); fp_add(&a, &b, &c); - t2 = (TIMFUNC() - t1)>>3; - if (t1>7; - if (t1>7; - if (t1>6; - if (t1>6; - fp_copy(&b, &c); - fp_copy(&b, &d); - if (t1>1; - fp_copy(&b, &c); - fp_copy(&b, &d); - if (t1 +#include +#include + +/* RDTSC from Scott Duplichan */ +static ulong64 TIMFUNC(void) +{ +#if defined __GNUC__ + #if defined(INTEL_CC) + ulong64 a; + asm ("rdtsc":"=A"(a)); + return a; + #elif defined(__i386__) || defined(__x86_64__) + /* 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); + #elif defined(TFM_PPC32) + unsigned long a, b; + __asm__ __volatile__ ("mftbu %1 \nmftb %0\n":"=r"(a), "=r"(b)); + return (((ulong64)b) << 32ULL) | ((ulong64)a); + #elif defined(TFM_AVR32) + FILE *in; + char buf[20]; + in = fopen("/sys/devices/system/cpu/cpu0/pccycles", "r"); + fgets(buf, 20, in); + fclose(in); + return strtoul(buf, NULL, 10); + #else /* gcc-IA64 version */ + unsigned long result; + __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory"); + while (__builtin_expect ((int) result == -1, 0)) + __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory"); + return result; + #endif + +// Microsoft and Intel Windows compilers +#elif defined _M_IX86 + __asm rdtsc +#elif defined _M_AMD64 + return __rdtsc (); +#elif defined _M_IA64 + #if defined __INTEL_COMPILER + #include + #endif + return __getReg (3116); +#else + #error need rdtsc function for this build +#endif +} + +static ulong64 ticks; +static const char* p_str; + +static void print_start(const char* s) +{ + p_str = s; +} + +static void print_line(ulong64 b, ulong64 t) +{ + printf("%llu;%s;%llu;%llu\n", ticks, p_str, b, t); +} + +int main(void) +{ + fp_int a,b,c,d; + ulong64 t1, t2; + fp_digit fp; + unsigned long t, ix; + + t1 = TIMFUNC(); + sleep(1); + ticks = TIMFUNC() - t1; + fprintf(stderr, "Ticks per second: %llu\n", ticks); + + printf("Ticks/sec;Algorithm;bits;time\n"); + /* do some timings... */ + print_start("Addition"); + for (t = 2; t <= FP_SIZE / 2; t += 2) { + fp_zero(&a); + fp_zero(&b); + fp_zero(&c); + for (ix = 0; ix < t; ix++) { + a.dp[ix] = ix; + b.dp[ix] = ix; + } + a.used = t; + b.used = t; + t2 = -1; + for (ix = 0; ix < 25000; ++ix) { + t1 = TIMFUNC(); + fp_add(&a, &b, &c); + fp_add(&a, &b, &c); + fp_add(&a, &b, &c); + fp_add(&a, &b, &c); + fp_add(&a, &b, &c); + fp_add(&a, &b, &c); + fp_add(&a, &b, &c); + fp_add(&a, &b, &c); + t2 = (TIMFUNC() - t1) >> 3; + if (t1 < t2) { + --ix; + t2 = t1; + } + } + print_line(t * DIGIT_BIT, t2); + } + print_start("Multiplication"); + for (t = 2; t < FP_SIZE / 2; t += 2) { + fp_zero(&a); + fp_zero(&b); + fp_zero(&c); + for (ix = 0; ix < t; ix++) { + a.dp[ix] = ix; + b.dp[ix] = ix; + } + a.used = t; + b.used = t; + t2 = -1; + for (ix = 0; ix < 100; ++ix) { + t1 = TIMFUNC(); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + fp_mul(&a, &b, &c); + t2 = (TIMFUNC() - t1) >> 7; + if (t1 < t2) { + --ix; + t2 = t1; + } + } + print_line(t * DIGIT_BIT, t2); + } + + print_start("Squaring"); + for (t = 2; t < FP_SIZE / 2; t += 2) { + fp_zero(&a); + fp_zero(&b); + for (ix = 0; ix < t; ix++) { + a.dp[ix] = ix; + } + a.used = t; + t2 = -1; + for (ix = 0; ix < 100; ++ix) { + t1 = TIMFUNC(); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + fp_sqr(&a, &b); + t2 = (TIMFUNC() - t1) >> 7; + if (t1 < t2) { + --ix; + t2 = t1; + } + } + print_line(t * DIGIT_BIT, t2); + } + + print_start("Invmod"); + for (t = 2; t < FP_SIZE / 2; t += 2) { + fp_zero(&a); + for (ix = 0; ix < t; ix++) { + a.dp[ix] = ix | 1; + } + a.used = t; + fp_zero(&b); + for (ix = 0; ix < t; ix++) { + b.dp[ix] = rand(); + } + b.used = t; + fp_clamp(&b); + fp_zero(&c); + t2 = -1; + for (ix = 0; ix < 100; ++ix) { + t1 = TIMFUNC(); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + fp_invmod(&b, &a, &c); + t2 = (TIMFUNC() - t1) >> 6; + if (t1 < t2) { + --ix; + t2 = t1; + } + } + print_line(t * DIGIT_BIT, t2); + } + + print_start("Montgomery"); + for (t = 2; t <= (FP_SIZE / 2) - 4; t += 2) { + // printf("%5lu-bit: %9llu\n", t * DIGIT_BIT, t2); + fp_zero(&a); + for (ix = 0; ix < t; ix++) { + a.dp[ix] = ix | 1; + } + a.used = t; + + fp_montgomery_setup(&a, &fp); + fp_sub_d(&a, 3, &b); + fp_sqr(&b, &b); + fp_copy(&b, &c); + fp_copy(&b, &d); + + t2 = -1; + for (ix = 0; ix < 100; ++ix) { + t1 = TIMFUNC(); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + fp_montgomery_reduce(&c, &a, fp); + fp_montgomery_reduce(&d, &a, fp); + t2 = (TIMFUNC() - t1) >> 6; + fp_copy(&b, &c); + fp_copy(&b, &d); + if (t1 < t2) { + --ix; + t2 = t1; + } + } + print_line(t * DIGIT_BIT, t2); + } + + print_start("Exptmod"); + + for (t = 512 / DIGIT_BIT; t <= (FP_SIZE / 2) - 2; t += 256 / DIGIT_BIT) { + fp_zero(&a); + fp_zero(&b); + fp_zero(&c); + for (ix = 0; ix < t; ix++) { + a.dp[ix] = ix + 1; + b.dp[ix] = (fp_digit) rand() * (fp_digit) rand(); + c.dp[ix] = ix; + } + a.used = t; + b.used = t; + c.used = t; + + t2 = -1; + for (ix = 0; ix < 500; ++ix) { + t1 = TIMFUNC(); + fp_exptmod(&c, &b, &a, &d); + fp_exptmod(&c, &b, &a, &d); + t2 = (TIMFUNC() - t1) >> 1; + fp_copy(&b, &c); + fp_copy(&b, &d); + if (t1 < t2) { + t2 = t1; + --ix; + } + } + print_line(t * DIGIT_BIT, t2); + } + return 0; +} diff --git a/makefile b/makefile index 44aa0cc..de90f66 100644 --- a/makefile +++ b/makefile @@ -111,9 +111,15 @@ demo/test.o: CFLAGS+=-Wno-unused-result test: $(LIBNAME) demo/test.o $(CC) $(CFLAGS) demo/test.o $(LIBNAME) $(PROF) -o test -timing: $(LIBNAME) demo/test.o +test_standalone: CFLAGS+=-DTFM_DEMO_TEST_VS_MTEST=0 + +.PHONY: test_standalone +test_standalone: $(LIBNAME) demo/test.o $(CC) $(CFLAGS) demo/test.o $(LIBNAME) $(PROF) -o test +timing: $(LIBNAME) demo/timing.o + $(CC) $(CFLAGS) demo/timing.o $(LIBNAME) $(PROF) -o timing + profiled: CC="$(CC)" PREFIX="${PREFIX} CFLAGS="${CFLAGS} -fprofile-generate" MAKE=${MAKE} ${MAKE} timing ./test diff --git a/makefile.shared b/makefile.shared index dc118d7..39d2148 100644 --- a/makefile.shared +++ b/makefile.shared @@ -87,7 +87,7 @@ $(OBJECTS): $(HEADERS) $(LTCOMPILE) $(CFLAGS) $(LDFLAGS) -o $@ -c $< $(LIBNAME): $(OBJECTS) - libtool --silent --mode=link gcc $(CFLAGS) `find . -type f | grep "[.]lo" | xargs` -o $(LIBNAME) -rpath $(LIBPATH) -version-info $(VERSION) + libtool --silent --mode=link --tag=CC $(CC) $(CFLAGS) `find . -type f | grep "[.]lo" | xargs` -o $(LIBNAME) -rpath $(LIBPATH) -version-info $(VERSION) install: $(LIBNAME) install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(LIBPATH) @@ -98,13 +98,25 @@ install: $(LIBNAME) mtest/mtest: mtest/mtest.c cd mtest ; make mtest +demo/test.o: CFLAGS+=-Wno-unused-result + .PHONY: test test: $(LIBNAME) demo/test.o $(LT) --mode=link --tag=CC $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o test demo/test.o $(LIBNAME) +test_standalone: CFLAGS+=-DTFM_DEMO_TEST_VS_MTEST=0 + +.PHONY: test_standalone +test_standalone: $(LIBNAME) demo/test.o + $(LT) --mode=link --tag=CC $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o test demo/test.o $(LIBNAME) + stest: $(LIBNAME) demo/stest.o $(LT) --mode=link --tag=CC $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o stest demo/stest.o $(LIBNAME) +.PHONY: timing +timing: $(LIBNAME) demo/timing.o + $(LT) --mode=link --tag=CC $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o timing demo/timing.o $(LIBNAME) + # $Source$ # $Revision$ # $Date$ diff --git a/src/mul/fp_mul_comba.c b/src/mul/fp_mul_comba.c index 1bec1e1..ea95443 100644 --- a/src/mul/fp_mul_comba.c +++ b/src/mul/fp_mul_comba.c @@ -349,7 +349,9 @@ void fp_mul_comba(fp_int *A, fp_int *B, fp_int *C) /* execute loop */ COMBA_FORWARD; for (iz = 0; iz < iy; ++iz) { - MULADD(*tmpx++, *tmpy--); + fp_digit _tmpx = *tmpx++; + fp_digit _tmpy = *tmpy--; + MULADD(_tmpx, _tmpy); } /* store term */ diff --git a/src/sqr/fp_sqr_comba.c b/src/sqr/fp_sqr_comba.c index 9a63b4f..49525be 100644 --- a/src/sqr/fp_sqr_comba.c +++ b/src/sqr/fp_sqr_comba.c @@ -109,7 +109,7 @@ asm( \ "addq %%rax,%0 \n\t" \ "adcq %%rdx,%1 \n\t" \ "adcq $0,%2 \n\t" \ - :"=r"(c0), "=r"(c1), "=r"(c2): "0"(c0), "1"(c1), "2"(c2), "g"(i) :"%rax","%rdx","cc"); + :"=r"(c0), "=r"(c1), "=r"(c2): "0"(c0), "1"(c1), "2"(c2), "x"(i) :"%rax","%rdx","cc"); #define SQRADD2(i, j) \ asm( \ diff --git a/src/sqr/fp_sqr_comba_generic.c b/src/sqr/fp_sqr_comba_generic.c index c5d1618..168bd9d 100644 --- a/src/sqr/fp_sqr_comba_generic.c +++ b/src/sqr/fp_sqr_comba_generic.c @@ -68,12 +68,15 @@ void fp_sqr_comba(fp_int *A, fp_int *B) /* execute loop */ for (iz = 0; iz < iy; iz++) { - SQRADD2(*tmpx++, *tmpy--); + fp_digit _tmpx = *tmpx++; + fp_digit _tmpy = *tmpy--; + SQRADD2(_tmpx, _tmpy); } /* even columns have the square term in them */ if ((ix&1) == 0) { - SQRADD(A->dp[ix>>1], A->dp[ix>>1]); + fp_digit _a_dp = A->dp[ix>>1]; + SQRADD(_a_dp, A->dp[ix>>1]); } /* store it */ diff --git a/testme.sh b/testme.sh new file mode 100755 index 0000000..90fa34f --- /dev/null +++ b/testme.sh @@ -0,0 +1,35 @@ +#!/bin/bash -e +# +# Can be run with e.g. ./testme.sh "gcc-4.8 gcc-4.9", defaults to ./testme.sh "gcc" + +_runtest() +{ + echo -n "Run test $1 $2" + trap 'echo " - build not successful, errors are:" && cat test_gcc_errors.txt' INT TERM + make clean > /dev/null + CC="${1}" make test_standalone -j9 $2 > /dev/null 2>test_gcc_errors.txt + trap - INT TERM + local outfile="test_$(echo ${1}${2} | tr -d '\"' | tr ' ' '_').txt" + trap 'echo " - tests not successful, failed at:" && tail ${outfile}' INT TERM + ./test > ${outfile} + echo " successful" + trap - INT TERM +} + +gccopt="-m32 -m64 -mx32" +if [ $# -ge 1 ] +then + gccver=$1 +else + gccver="gcc" +fi + +for gopt in ${gccopt}; +do + for gccv in ${gccver}; + do + _runtest "${gccv} ${gopt}" "-f makefile.shared" + _runtest "${gccv} ${gopt}" "" + done +done +