run astyle

This commit is contained in:
Francois Perrad 2017-10-18 10:44:06 +02:00
parent 32e710ae07
commit 08741a141c
10 changed files with 1486 additions and 1338 deletions

View File

@ -32,7 +32,7 @@
#include "tommath.h"
void ndraw(mp_int * a, char *name)
void ndraw(mp_int *a, char *name)
{
char buf[16000];
@ -44,7 +44,7 @@ void ndraw(mp_int * a, char *name)
}
#if LTM_DEMO_TEST_VS_MTEST
static void draw(mp_int * a)
static void draw(mp_int *a)
{
ndraw(a, "");
}
@ -65,7 +65,7 @@ int lbit(void)
}
#if defined(LTM_DEMO_REAL_RAND) && !defined(_WIN32)
static FILE* fd_urandom;
static FILE *fd_urandom;
#endif
int myrng(unsigned char *dst, int len, void *dat)
{
@ -73,20 +73,19 @@ int myrng(unsigned char *dst, int len, void *dat)
(void)dat;
#if defined(LTM_DEMO_REAL_RAND)
if (!fd_urandom) {
#if !defined(_WIN32)
# if !defined(_WIN32)
fprintf(stderr, "\nno /dev/urandom\n");
#endif
}
else {
# endif
} else {
return fread(dst, 1, len, fd_urandom);
}
#endif
for (x = 0; x < len; ) {
for (x = 0; x < len;) {
unsigned int r = (unsigned int)rand();
do {
dst[x++] = r & 0xFF;
r >>= 8;
} while((r != 0) && (x < len));
} while ((r != 0) && (x < len));
}
return len;
}
@ -107,7 +106,7 @@ static void _cleanup(void)
printf("\n");
#ifdef LTM_DEMO_REAL_RAND
if(fd_urandom)
if (fd_urandom)
fclose(fd_urandom);
#endif
}
@ -140,7 +139,7 @@ int main(void)
#if LTM_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;
char* ret;
char *ret;
#else
unsigned long s, t;
unsigned long long q, r;
@ -157,9 +156,9 @@ int main(void)
if (!fd_urandom) {
fd_urandom = fopen("/dev/urandom", "r");
if (!fd_urandom) {
#if !defined(_WIN32)
# if !defined(_WIN32)
fprintf(stderr, "\ncould not open /dev/urandom\n");
#endif
# endif
}
}
#endif
@ -274,10 +273,10 @@ int main(void)
// test mp_get_int
printf("\n\nTesting: mp_get_int");
for (i = 0; i < 1000; ++i) {
t = ((unsigned long) rand () * rand () + 1) & 0xFFFFFFFF;
mp_set_int (&a, t);
if (t != mp_get_int (&a)) {
printf ("\nmp_get_int() bad result!");
t = ((unsigned long) rand() * rand() + 1) & 0xFFFFFFFF;
mp_set_int(&a, t);
if (t != mp_get_int(&a)) {
printf("\nmp_get_int() bad result!");
return EXIT_FAILURE;
}
}
@ -309,7 +308,7 @@ int main(void)
return EXIT_FAILURE;
}
t <<= 1;
} while(t);
} while (t);
}
printf("\n\nTesting: mp_get_long_long\n");
@ -329,58 +328,58 @@ int main(void)
return EXIT_FAILURE;
}
r <<= 1;
} while(r);
} while (r);
}
// test mp_sqrt
printf("\n\nTesting: mp_sqrt\n");
for (i = 0; i < 1000; ++i) {
printf ("%6d\r", i);
fflush (stdout);
n = (rand () & 15) + 1;
mp_rand (&a, n);
if (mp_sqrt (&a, &b) != MP_OKAY) {
printf ("\nmp_sqrt() error!");
printf("%6d\r", i);
fflush(stdout);
n = (rand() & 15) + 1;
mp_rand(&a, n);
if (mp_sqrt(&a, &b) != MP_OKAY) {
printf("\nmp_sqrt() error!");
return EXIT_FAILURE;
}
mp_n_root_ex (&a, 2, &c, 0);
mp_n_root_ex (&a, 2, &d, 1);
if (mp_cmp_mag (&c, &d) != MP_EQ) {
printf ("\nmp_n_root_ex() bad result!");
mp_n_root_ex(&a, 2, &c, 0);
mp_n_root_ex(&a, 2, &d, 1);
if (mp_cmp_mag(&c, &d) != MP_EQ) {
printf("\nmp_n_root_ex() bad result!");
return EXIT_FAILURE;
}
if (mp_cmp_mag (&b, &c) != MP_EQ) {
printf ("mp_sqrt() bad result!\n");
if (mp_cmp_mag(&b, &c) != MP_EQ) {
printf("mp_sqrt() bad result!\n");
return EXIT_FAILURE;
}
}
printf("\n\nTesting: mp_is_square\n");
for (i = 0; i < 1000; ++i) {
printf ("%6d\r", i);
fflush (stdout);
printf("%6d\r", i);
fflush(stdout);
/* test mp_is_square false negatives */
n = (rand () & 7) + 1;
mp_rand (&a, n);
mp_sqr (&a, &a);
if (mp_is_square (&a, &n) != MP_OKAY) {
printf ("\nfn:mp_is_square() error!");
n = (rand() & 7) + 1;
mp_rand(&a, n);
mp_sqr(&a, &a);
if (mp_is_square(&a, &n) != MP_OKAY) {
printf("\nfn:mp_is_square() error!");
return EXIT_FAILURE;
}
if (n == 0) {
printf ("\nfn:mp_is_square() bad result!");
printf("\nfn:mp_is_square() bad result!");
return EXIT_FAILURE;
}
/* test for false positives */
mp_add_d (&a, 1, &a);
if (mp_is_square (&a, &n) != MP_OKAY) {
printf ("\nfp:mp_is_square() error!");
mp_add_d(&a, 1, &a);
if (mp_is_square(&a, &n) != MP_OKAY) {
printf("\nfp:mp_is_square() error!");
return EXIT_FAILURE;
}
if (n == 1) {
printf ("\nfp:mp_is_square() bad result!");
printf("\nfp:mp_is_square() bad result!");
return EXIT_FAILURE;
}
@ -404,42 +403,42 @@ int main(void)
/* test for size */
for (ix = 10; ix < 128; ix++) {
printf ("Testing (not safe-prime): %9d bits \r", ix);
fflush (stdout);
err = mp_prime_random_ex (&a, 8, ix,
(rand () & 1) ? 0 : LTM_PRIME_2MSB_ON, myrng,
printf("Testing (not safe-prime): %9d bits \r", ix);
fflush(stdout);
err = mp_prime_random_ex(&a, 8, ix,
(rand() & 1) ? 0 : LTM_PRIME_2MSB_ON, myrng,
NULL);
if (err != MP_OKAY) {
printf ("failed with err code %d\n", err);
printf("failed with err code %d\n", err);
return EXIT_FAILURE;
}
if (mp_count_bits (&a) != ix) {
printf ("Prime is %d not %d bits!!!\n", mp_count_bits (&a), ix);
if (mp_count_bits(&a) != ix) {
printf("Prime is %d not %d bits!!!\n", mp_count_bits(&a), ix);
return EXIT_FAILURE;
}
}
printf("\n");
for (ix = 16; ix < 128; ix++) {
printf ("Testing ( safe-prime): %9d bits \r", ix);
fflush (stdout);
err = mp_prime_random_ex (
&a, 8, ix, ((rand () & 1) ? 0 : LTM_PRIME_2MSB_ON) | LTM_PRIME_SAFE,
printf("Testing ( safe-prime): %9d bits \r", ix);
fflush(stdout);
err = mp_prime_random_ex(
&a, 8, ix, ((rand() & 1) ? 0 : LTM_PRIME_2MSB_ON) | LTM_PRIME_SAFE,
myrng, NULL);
if (err != MP_OKAY) {
printf ("failed with err code %d\n", err);
printf("failed with err code %d\n", err);
return EXIT_FAILURE;
}
if (mp_count_bits (&a) != ix) {
printf ("Prime is %d not %d bits!!!\n", mp_count_bits (&a), ix);
if (mp_count_bits(&a) != ix) {
printf("Prime is %d not %d bits!!!\n", mp_count_bits(&a), ix);
return EXIT_FAILURE;
}
/* let's see if it's really a safe prime */
mp_sub_d (&a, 1, &a);
mp_div_2 (&a, &a);
mp_prime_is_prime (&a, 8, &cnt);
mp_sub_d(&a, 1, &a);
mp_div_2(&a, &a);
mp_prime_is_prime(&a, 8, &cnt);
if (cnt != MP_YES) {
printf ("sub is not prime!\n");
printf("sub is not prime!\n");
return EXIT_FAILURE;
}
}
@ -472,15 +471,17 @@ int main(void)
mp_mulmod(&c, &b, &a, &c);
if (mp_cmp(&c, &d) != MP_EQ) {
/* *INDENT-OFF* */
printf("d = e mod a, c = e MOD a\n");
mp_todecimal(&a, buf); printf("a = %s\n", buf);
mp_todecimal(&e, buf); printf("e = %s\n", buf);
mp_todecimal(&d, buf); printf("d = %s\n", buf);
mp_todecimal(&c, buf); printf("c = %s\n", buf);
printf("compare no compare!\n"); return EXIT_FAILURE; }
printf("compare no compare!\n"); return EXIT_FAILURE;
/* *INDENT-ON* */
}
/* only one big montgomery reduction */
if (i > 10)
{
if (i > 10) {
n = 1000;
ix = 100;
}
@ -513,49 +514,48 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
printf("\n\nTesting: mp_cnt_lsb");
mp_set(&a, 1);
for (ix = 0; ix < 1024; ix++) {
if (mp_cnt_lsb (&a) != ix) {
printf ("Failed at %d, %d\n", ix, mp_cnt_lsb (&a));
if (mp_cnt_lsb(&a) != ix) {
printf("Failed at %d, %d\n", ix, mp_cnt_lsb(&a));
return EXIT_FAILURE;
}
mp_mul_2 (&a, &a);
mp_mul_2(&a, &a);
}
/* test mp_reduce_2k */
/* test mp_reduce_2k */
printf("\n\nTesting: mp_reduce_2k\n");
for (cnt = 3; cnt <= 128; ++cnt) {
mp_digit tmp;
mp_2expt (&a, cnt);
mp_sub_d (&a, 2, &a); /* a = 2**cnt - 2 */
mp_2expt(&a, cnt);
mp_sub_d(&a, 2, &a); /* a = 2**cnt - 2 */
printf ("\r %4d bits", cnt);
printf ("(%d)", mp_reduce_is_2k (&a));
mp_reduce_2k_setup (&a, &tmp);
printf ("(%lu)", (unsigned long) tmp);
printf("\r %4d bits", cnt);
printf("(%d)", mp_reduce_is_2k(&a));
mp_reduce_2k_setup(&a, &tmp);
printf("(%lu)", (unsigned long) tmp);
for (ix = 0; ix < 1000; ix++) {
if (!(ix & 127)) {
printf (".");
fflush (stdout);
printf(".");
fflush(stdout);
}
mp_rand (&b, (cnt / DIGIT_BIT + 1) * 2);
mp_copy (&c, &b);
mp_mod (&c, &a, &c);
mp_reduce_2k (&b, &a, 2);
if (mp_cmp (&c, &b)) {
printf ("FAILED\n");
mp_rand(&b, (cnt / DIGIT_BIT + 1) * 2);
mp_copy(&c, &b);
mp_mod(&c, &a, &c);
mp_reduce_2k(&b, &a, 2);
if (mp_cmp(&c, &b)) {
printf("FAILED\n");
return EXIT_FAILURE;
}
}
}
/* test mp_div_3 */
/* test mp_div_3 */
printf("\n\nTesting: mp_div_3...\n");
mp_set(&d, 3);
for (cnt = 0; cnt < 10000;) {
mp_digit r2;
if (!(++cnt & 127))
{
if (!(++cnt & 127)) {
printf("%9d\r", cnt);
fflush(stdout);
}
@ -569,65 +569,65 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
}
printf("\nPassed div_3 testing");
/* test the DR reduction */
/* test the DR reduction */
printf("\n\nTesting: mp_dr_reduce...\n");
for (cnt = 2; cnt < 32; cnt++) {
printf ("\r%d digit modulus", cnt);
mp_grow (&a, cnt);
mp_zero (&a);
printf("\r%d digit modulus", cnt);
mp_grow(&a, cnt);
mp_zero(&a);
for (ix = 1; ix < cnt; ix++) {
a.dp[ix] = MP_MASK;
}
a.used = cnt;
a.dp[0] = 3;
mp_rand (&b, cnt - 1);
mp_copy (&b, &c);
mp_rand(&b, cnt - 1);
mp_copy(&b, &c);
rr = 0;
do {
if (!(rr & 127)) {
printf (".");
fflush (stdout);
printf(".");
fflush(stdout);
}
mp_sqr (&b, &b);
mp_add_d (&b, 1, &b);
mp_copy (&b, &c);
mp_sqr(&b, &b);
mp_add_d(&b, 1, &b);
mp_copy(&b, &c);
mp_mod (&b, &a, &b);
mp_mod(&b, &a, &b);
mp_dr_setup(&a, &mp),
mp_dr_reduce (&c, &a, mp);
mp_dr_reduce(&c, &a, mp);
if (mp_cmp (&b, &c) != MP_EQ) {
printf ("Failed on trial %u\n", rr);
if (mp_cmp(&b, &c) != MP_EQ) {
printf("Failed on trial %u\n", rr);
return EXIT_FAILURE;
}
} while (++rr < 500);
printf (" passed");
fflush (stdout);
printf(" passed");
fflush(stdout);
}
#if LTM_DEMO_TEST_REDUCE_2K_L
/* test the mp_reduce_2k_l code */
#if LTM_DEMO_TEST_REDUCE_2K_L == 1
/* first load P with 2^1024 - 0x2A434 B9FDEC95 D8F9D550 FFFFFFFF FFFFFFFF */
# if LTM_DEMO_TEST_REDUCE_2K_L
/* test the mp_reduce_2k_l code */
# if LTM_DEMO_TEST_REDUCE_2K_L == 1
/* first load P with 2^1024 - 0x2A434 B9FDEC95 D8F9D550 FFFFFFFF FFFFFFFF */
mp_2expt(&a, 1024);
mp_read_radix(&b, "2A434B9FDEC95D8F9D550FFFFFFFFFFFFFFFF", 16);
mp_sub(&a, &b, &a);
#elif LTM_DEMO_TEST_REDUCE_2K_L == 2
/* p = 2^2048 - 0x1 00000000 00000000 00000000 00000000 4945DDBF 8EA2A91D 5776399B B83E188F */
# elif LTM_DEMO_TEST_REDUCE_2K_L == 2
/* p = 2^2048 - 0x1 00000000 00000000 00000000 00000000 4945DDBF 8EA2A91D 5776399B B83E188F */
mp_2expt(&a, 2048);
mp_read_radix(&b,
"1000000000000000000000000000000004945DDBF8EA2A91D5776399BB83E188F",
16);
mp_sub(&a, &b, &a);
#else
#error oops
#endif
# else
# error oops
# endif
mp_todecimal(&a, buf);
printf("\n\np==%s\n", buf);
/* now mp_reduce_is_2k_l() should return */
/* now mp_reduce_is_2k_l() should return */
if (mp_reduce_is_2k_l(&a) != 1) {
printf("mp_reduce_is_2k_l() return 0, should be 1\n");
return EXIT_FAILURE;
@ -656,13 +656,11 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
}
}
printf("...Passed\n");
#endif /* LTM_DEMO_TEST_REDUCE_2K_L */
# endif /* LTM_DEMO_TEST_REDUCE_2K_L */
#else
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 = cnt = add_d_n =
sub_d_n = 0;
sub_n = mul_n = div_n = sqr_n = mul2d_n = div2d_n = cnt = add_d_n = sub_d_n = 0;
/* force KARA and TOOM to enable despite cutoffs */
KARATSUBA_SQR_CUTOFF = KARATSUBA_MUL_CUTOFF = 8;
@ -700,21 +698,32 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
}
printf
("%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu ",
printf("%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu ",
add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n,
expt_n, inv_n, div2_n, mul2_n, add_d_n, sub_d_n);
ret=fgets(cmd, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(cmd, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
cmd[strlen(cmd) - 1] = 0;
printf("%-6s ]\r", cmd);
fflush(stdout);
if (!strcmp(cmd, "mul2d")) {
++mul2d_n;
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&a, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
sscanf(buf, "%d", &rr);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&b, buf, 64);
mp_mul_2d(&a, rr, &a);
@ -727,11 +736,20 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
}
} else if (!strcmp(cmd, "div2d")) {
++div2d_n;
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&a, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
sscanf(buf, "%d", &rr);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&b, buf, 64);
mp_div_2d(&a, rr, &a, &e);
@ -747,11 +765,20 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
}
} else if (!strcmp(cmd, "add")) {
++add_n;
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&a, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&b, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&c, buf, 64);
mp_copy(&a, &d);
mp_add(&d, &b, &d);
@ -791,11 +818,20 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
} else if (!strcmp(cmd, "sub")) {
++sub_n;
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&a, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&b, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&c, buf, 64);
mp_copy(&a, &d);
mp_sub(&d, &b, &d);
@ -809,11 +845,20 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
}
} else if (!strcmp(cmd, "mul")) {
++mul_n;
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&a, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&b, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&c, buf, 64);
mp_copy(&a, &d);
mp_mul(&d, &b, &d);
@ -827,13 +872,25 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
}
} else if (!strcmp(cmd, "div")) {
++div_n;
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&a, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&b, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&c, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&d, buf, 64);
mp_div(&a, &b, &e, &f);
@ -851,9 +908,15 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
} else if (!strcmp(cmd, "sqr")) {
++sqr_n;
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&a, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&b, buf, 64);
mp_copy(&a, &c);
mp_sqr(&c, &c);
@ -866,11 +929,20 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
}
} else if (!strcmp(cmd, "gcd")) {
++gcd_n;
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&a, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&b, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&c, buf, 64);
mp_copy(&a, &d);
mp_gcd(&d, &b, &d);
@ -885,11 +957,20 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
}
} else if (!strcmp(cmd, "lcm")) {
++lcm_n;
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&a, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&b, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&c, buf, 64);
mp_copy(&a, &d);
mp_lcm(&d, &b, &d);
@ -904,13 +985,25 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
}
} else if (!strcmp(cmd, "expt")) {
++expt_n;
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&a, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&b, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&c, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&d, buf, 64);
mp_copy(&a, &e);
mp_exptmod(&e, &b, &c, &e);
@ -925,11 +1018,20 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
}
} else if (!strcmp(cmd, "invmod")) {
++inv_n;
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&a, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&b, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&c, buf, 64);
mp_invmod(&a, &b, &d);
mp_mulmod(&d, &a, &b, &e);
@ -947,9 +1049,15 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
} else if (!strcmp(cmd, "div2")) {
++div2_n;
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&a, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&b, buf, 64);
mp_div_2(&a, &c);
if (mp_cmp(&c, &b) != MP_EQ) {
@ -961,9 +1069,15 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
}
} else if (!strcmp(cmd, "mul2")) {
++mul2_n;
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&a, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&b, buf, 64);
mp_mul_2(&a, &c);
if (mp_cmp(&c, &b) != MP_EQ) {
@ -975,11 +1089,20 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
}
} else if (!strcmp(cmd, "add_d")) {
++add_d_n;
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&a, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
sscanf(buf, "%d", &ix);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&b, buf, 64);
mp_add_d(&a, ix, &c);
if (mp_cmp(&b, &c) != MP_EQ) {
@ -992,11 +1115,20 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
}
} else if (!strcmp(cmd, "sub_d")) {
++sub_d_n;
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&a, buf, 64);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
sscanf(buf, "%d", &ix);
ret=fgets(buf, 4095, stdin); if(!ret){_panic(__LINE__);}
ret=fgets(buf, 4095, stdin);
if (!ret) {
_panic(__LINE__);
}
mp_read_radix(&b, buf, 64);
mp_sub_d(&a, ix, &c);
if (mp_cmp(&b, &c) != MP_EQ) {

View File

@ -19,7 +19,7 @@ uint64_t _tt;
#endif
void ndraw(mp_int * a, char *name)
void ndraw(mp_int *a, char *name)
{
char buf[4096];
@ -28,7 +28,7 @@ void ndraw(mp_int * a, char *name)
printf("%s\n", buf);
}
static void draw(mp_int * a)
static void draw(mp_int *a)
{
ndraw(a, "");
}
@ -56,8 +56,8 @@ static uint64_t TIMFUNC(void)
* 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 ((uint64_t)lo)|( ((uint64_t)hi)<<32);
__asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
return ((uint64_t)lo)|(((uint64_t)hi)<<32);
#else /* gcc-IA64 version */
unsigned long result;
__asm__ __volatile__("mov %0=ar.itc":"=r"(result)::"memory");

View File

@ -17,13 +17,13 @@ int main(void)
out = fopen("2kprime.1", "w");
for (x = 0; x < (int)(sizeof(sizes) / sizeof(sizes[0])); x++) {
top:
top:
mp_2expt(&q, sizes[x]);
mp_add_d(&q, 3, &q);
z = -3;
t1 = clock();
for(;;) {
for (;;) {
mp_sub_d(&q, 4, &q);
z += 4;
@ -33,7 +33,8 @@ int main(void)
}
if (clock() - t1 > CLOCKS_PER_SEC) {
printf("."); fflush(stdout);
printf(".");
fflush(stdout);
// sleep((clock() - t1 + CLOCKS_PER_SEC/2)/CLOCKS_PER_SEC);
t1 = clock();
}
@ -68,7 +69,8 @@ int main(void)
mp_toradix(&q, buf, 10);
printf("\n\n%d-bits (k = %lu) = %s\n", sizes[x], z, buf);
fprintf(out, "%d-bits (k = %lu) = %s\n", sizes[x], z, buf); fflush(out);
fprintf(out, "%d-bits (k = %lu) = %s\n", sizes[x], z, buf);
fflush(out);
}
return 0;

View File

@ -15,7 +15,7 @@ int main(void)
out = fopen("drprimes.txt", "w");
for (x = 0; x < (int)(sizeof(sizes)/sizeof(sizes[0])); x++) {
top:
top:
printf("Seeking a %d-bit safe prime\n", sizes[x] * DIGIT_BIT);
mp_grow(&a, sizes[x]);
mp_zero(&a);
@ -34,7 +34,8 @@ int main(void)
if (a.dp[0] >= MP_MASK) break;
mp_prime_is_prime(&a, 1, &res);
if (res == 0) continue;
printf("."); fflush(stdout);
printf(".");
fflush(stdout);
mp_sub_d(&a, 1, &b);
mp_div_2(&b, &b);
mp_prime_is_prime(&b, 3, &res);
@ -44,11 +45,14 @@ int main(void)
}
if (res != 1) {
printf("Error not DR modulus\n"); sizes[x] += 1; goto top;
printf("Error not DR modulus\n");
sizes[x] += 1;
goto top;
} else {
mp_toradix(&a, buf, 10);
printf("\n\np == %s\n\n", buf);
fprintf(out, "%d-bit prime:\np == %s\n\n", mp_count_bits(&a), buf); fflush(out);
fprintf(out, "%d-bit prime:\np == %s\n\n", mp_count_bits(&a), buf);
fflush(out);
}
}
fclose(out);

View File

@ -5,19 +5,18 @@
#include <time.h>
#include <tommath.h>
int
is_mersenne (long s, int *pp)
int is_mersenne(long s, int *pp)
{
mp_int n, u;
int res, k;
*pp = 0;
if ((res = mp_init (&n)) != MP_OKAY) {
if ((res = mp_init(&n)) != MP_OKAY) {
return res;
}
if ((res = mp_init (&u)) != MP_OKAY) {
if ((res = mp_init(&u)) != MP_OKAY) {
goto LBL_N;
}
@ -25,51 +24,52 @@ is_mersenne (long s, int *pp)
if ((res = mp_2expt(&n, s)) != MP_OKAY) {
goto LBL_MU;
}
if ((res = mp_sub_d (&n, 1, &n)) != MP_OKAY) {
if ((res = mp_sub_d(&n, 1, &n)) != MP_OKAY) {
goto LBL_MU;
}
/* set u=4 */
mp_set (&u, 4);
mp_set(&u, 4);
/* for k=1 to s-2 do */
for (k = 1; k <= s - 2; k++) {
/* u = u^2 - 2 mod n */
if ((res = mp_sqr (&u, &u)) != MP_OKAY) {
if ((res = mp_sqr(&u, &u)) != MP_OKAY) {
goto LBL_MU;
}
if ((res = mp_sub_d (&u, 2, &u)) != MP_OKAY) {
if ((res = mp_sub_d(&u, 2, &u)) != MP_OKAY) {
goto LBL_MU;
}
/* make sure u is positive */
while (u.sign == MP_NEG) {
if ((res = mp_add (&u, &n, &u)) != MP_OKAY) {
if ((res = mp_add(&u, &n, &u)) != MP_OKAY) {
goto LBL_MU;
}
}
/* reduce */
if ((res = mp_reduce_2k (&u, &n, 1)) != MP_OKAY) {
if ((res = mp_reduce_2k(&u, &n, 1)) != MP_OKAY) {
goto LBL_MU;
}
}
/* if u == 0 then its prime */
if (mp_iszero (&u) == 1) {
if (mp_iszero(&u) == 1) {
mp_prime_is_prime(&n, 8, pp);
if (*pp != 1) printf("FAILURE\n");
}
res = MP_OKAY;
LBL_MU:mp_clear (&u);
LBL_N:mp_clear (&n);
LBL_MU:
mp_clear(&u);
LBL_N:
mp_clear(&n);
return res;
}
/* square root of a long < 65536 */
long
i_sqrt (long x)
long i_sqrt(long x)
{
long x1, x2;
@ -87,12 +87,11 @@ i_sqrt (long x)
}
/* is the long prime by brute force */
int
isprime (long k)
int isprime(long k)
{
long y, z;
y = i_sqrt (k);
y = i_sqrt(k);
for (z = 2; z <= y; z++) {
if ((k % z) == 0)
return 0;
@ -101,8 +100,7 @@ isprime (long k)
}
int
main (void)
int main(void)
{
int pp;
long k;
@ -112,27 +110,27 @@ main (void)
for (;;) {
/* start time */
tt = clock ();
tt = clock();
/* test if 2^k - 1 is prime */
if (is_mersenne (k, &pp) != MP_OKAY) {
printf ("Whoa error\n");
if (is_mersenne(k, &pp) != MP_OKAY) {
printf("Whoa error\n");
return -1;
}
if (pp == 1) {
/* count time */
tt = clock () - tt;
tt = clock() - tt;
/* display if prime */
printf ("2^%-5ld - 1 is prime, test took %ld ticks\n", k, tt);
printf("2^%-5ld - 1 is prime, test took %ld ticks\n", k, tt);
}
/* goto next odd exponent */
k += 2;
/* but make sure its prime */
while (isprime (k) == 0) {
while (isprime(k) == 0) {
k += 2;
}
}

View File

@ -12,7 +12,8 @@ int main(void)
/* loop through various sizes */
for (x = 4; x < 256; x++) {
printf("DIGITS == %3ld...", x); fflush(stdout);
printf("DIGITS == %3ld...", x);
fflush(stdout);
/* make up the odd modulus */
mp_rand(&modulus, x);

View File

@ -11,8 +11,7 @@ int n_prime;
FILE *primes;
/* fast square root */
static mp_digit
i_sqrt (mp_word x)
static mp_digit i_sqrt(mp_word x)
{
mp_word x1, x2;
@ -31,7 +30,7 @@ i_sqrt (mp_word x)
/* generates a prime digit */
static void gen_prime (void)
static void gen_prime(void)
{
mp_digit r, x, y, next;
FILE *out;
@ -39,6 +38,7 @@ static void gen_prime (void)
out = fopen("pprime.dat", "wb");
/* write first set of primes */
/* *INDENT-OFF* */
r = 3; fwrite(&r, 1, sizeof(mp_digit), out);
r = 5; fwrite(&r, 1, sizeof(mp_digit), out);
r = 7; fwrite(&r, 1, sizeof(mp_digit), out);
@ -49,9 +49,10 @@ static void gen_prime (void)
r = 23; fwrite(&r, 1, sizeof(mp_digit), out);
r = 29; fwrite(&r, 1, sizeof(mp_digit), out);
r = 31; fwrite(&r, 1, sizeof(mp_digit), out);
/* *INDENT-ON* */
/* get square root, since if 'r' is composite its factors must be < than this */
y = i_sqrt (r);
y = i_sqrt(r);
next = (y + 1) * (y + 1);
for (;;) {
@ -140,7 +141,11 @@ static void gen_prime (void)
}
}
} while (x == 0);
if (r > 31) { fwrite(&r, 1, sizeof(mp_digit), out); printf("%9d\r", r); fflush(stdout); }
if (r > 31) {
fwrite(&r, 1, sizeof(mp_digit), out);
printf("%9d\r", r);
fflush(stdout);
}
if (r < 31) break;
}
@ -171,8 +176,7 @@ mp_digit prime_digit(void)
/* makes a prime of at least k bits */
int
pprime (int k, int li, mp_int * p, mp_int * q)
int pprime(int k, int li, mp_int *p, mp_int *q)
{
mp_int a, b, c, n, x, y, z, v;
int res, ii;
@ -180,140 +184,140 @@ pprime (int k, int li, mp_int * p, mp_int * q)
/* single digit ? */
if (k <= (int) DIGIT_BIT) {
mp_set (p, prime_digit ());
mp_set(p, prime_digit());
return MP_OKAY;
}
if ((res = mp_init (&c)) != MP_OKAY) {
if ((res = mp_init(&c)) != MP_OKAY) {
return res;
}
if ((res = mp_init (&v)) != MP_OKAY) {
if ((res = mp_init(&v)) != MP_OKAY) {
goto LBL_C;
}
/* product of first 50 primes */
if ((res =
mp_read_radix (&v,
mp_read_radix(&v,
"19078266889580195013601891820992757757219839668357012055907516904309700014933909014729740190",
10)) != MP_OKAY) {
goto LBL_V;
}
if ((res = mp_init (&a)) != MP_OKAY) {
if ((res = mp_init(&a)) != MP_OKAY) {
goto LBL_V;
}
/* set the prime */
mp_set (&a, prime_digit ());
mp_set(&a, prime_digit());
if ((res = mp_init (&b)) != MP_OKAY) {
if ((res = mp_init(&b)) != MP_OKAY) {
goto LBL_A;
}
if ((res = mp_init (&n)) != MP_OKAY) {
if ((res = mp_init(&n)) != MP_OKAY) {
goto LBL_B;
}
if ((res = mp_init (&x)) != MP_OKAY) {
if ((res = mp_init(&x)) != MP_OKAY) {
goto LBL_N;
}
if ((res = mp_init (&y)) != MP_OKAY) {
if ((res = mp_init(&y)) != MP_OKAY) {
goto LBL_X;
}
if ((res = mp_init (&z)) != MP_OKAY) {
if ((res = mp_init(&z)) != MP_OKAY) {
goto LBL_Y;
}
/* now loop making the single digit */
while (mp_count_bits (&a) < k) {
fprintf (stderr, "prime has %4d bits left\r", k - mp_count_bits (&a));
fflush (stderr);
top:
mp_set (&b, prime_digit ());
while (mp_count_bits(&a) < k) {
fprintf(stderr, "prime has %4d bits left\r", k - mp_count_bits(&a));
fflush(stderr);
top:
mp_set(&b, prime_digit());
/* now compute z = a * b * 2 */
if ((res = mp_mul (&a, &b, &z)) != MP_OKAY) { /* z = a * b */
if ((res = mp_mul(&a, &b, &z)) != MP_OKAY) { /* z = a * b */
goto LBL_Z;
}
if ((res = mp_copy (&z, &c)) != MP_OKAY) { /* c = a * b */
if ((res = mp_copy(&z, &c)) != MP_OKAY) { /* c = a * b */
goto LBL_Z;
}
if ((res = mp_mul_2 (&z, &z)) != MP_OKAY) { /* z = 2 * a * b */
if ((res = mp_mul_2(&z, &z)) != MP_OKAY) { /* z = 2 * a * b */
goto LBL_Z;
}
/* n = z + 1 */
if ((res = mp_add_d (&z, 1, &n)) != MP_OKAY) { /* n = z + 1 */
if ((res = mp_add_d(&z, 1, &n)) != MP_OKAY) { /* n = z + 1 */
goto LBL_Z;
}
/* check (n, v) == 1 */
if ((res = mp_gcd (&n, &v, &y)) != MP_OKAY) { /* y = (n, v) */
if ((res = mp_gcd(&n, &v, &y)) != MP_OKAY) { /* y = (n, v) */
goto LBL_Z;
}
if (mp_cmp_d (&y, 1) != MP_EQ)
if (mp_cmp_d(&y, 1) != MP_EQ)
goto top;
/* now try base x=bases[ii] */
for (ii = 0; ii < li; ii++) {
mp_set (&x, bases[ii]);
mp_set(&x, bases[ii]);
/* compute x^a mod n */
if ((res = mp_exptmod (&x, &a, &n, &y)) != MP_OKAY) { /* y = x^a mod n */
if ((res = mp_exptmod(&x, &a, &n, &y)) != MP_OKAY) { /* y = x^a mod n */
goto LBL_Z;
}
/* if y == 1 loop */
if (mp_cmp_d (&y, 1) == MP_EQ)
if (mp_cmp_d(&y, 1) == MP_EQ)
continue;
/* now x^2a mod n */
if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2a mod n */
if ((res = mp_sqrmod(&y, &n, &y)) != MP_OKAY) { /* y = x^2a mod n */
goto LBL_Z;
}
if (mp_cmp_d (&y, 1) == MP_EQ)
if (mp_cmp_d(&y, 1) == MP_EQ)
continue;
/* compute x^b mod n */
if ((res = mp_exptmod (&x, &b, &n, &y)) != MP_OKAY) { /* y = x^b mod n */
if ((res = mp_exptmod(&x, &b, &n, &y)) != MP_OKAY) { /* y = x^b mod n */
goto LBL_Z;
}
/* if y == 1 loop */
if (mp_cmp_d (&y, 1) == MP_EQ)
if (mp_cmp_d(&y, 1) == MP_EQ)
continue;
/* now x^2b mod n */
if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2b mod n */
if ((res = mp_sqrmod(&y, &n, &y)) != MP_OKAY) { /* y = x^2b mod n */
goto LBL_Z;
}
if (mp_cmp_d (&y, 1) == MP_EQ)
if (mp_cmp_d(&y, 1) == MP_EQ)
continue;
/* compute x^c mod n == x^ab mod n */
if ((res = mp_exptmod (&x, &c, &n, &y)) != MP_OKAY) { /* y = x^ab mod n */
if ((res = mp_exptmod(&x, &c, &n, &y)) != MP_OKAY) { /* y = x^ab mod n */
goto LBL_Z;
}
/* if y == 1 loop */
if (mp_cmp_d (&y, 1) == MP_EQ)
if (mp_cmp_d(&y, 1) == MP_EQ)
continue;
/* now compute (x^c mod n)^2 */
if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2ab mod n */
if ((res = mp_sqrmod(&y, &n, &y)) != MP_OKAY) { /* y = x^2ab mod n */
goto LBL_Z;
}
/* y should be 1 */
if (mp_cmp_d (&y, 1) != MP_EQ)
if (mp_cmp_d(&y, 1) != MP_EQ)
continue;
break;
}
@ -322,7 +326,7 @@ pprime (int k, int li, mp_int * p, mp_int * q)
if (ii == li)
goto top;
{
{
char buf[4096];
mp_toradix(&n, buf, 10);
@ -332,65 +336,72 @@ pprime (int k, int li, mp_int * p, mp_int * q)
mp_toradix(&b, buf, 10);
printf("B == \n%s\n\nG == %d\n", buf, bases[ii]);
printf("----------------------------------------------------------------\n");
}
}
/* a = n */
mp_copy (&n, &a);
mp_copy(&n, &a);
}
/* get q to be the order of the large prime subgroup */
mp_sub_d (&n, 1, q);
mp_div_2 (q, q);
mp_div (q, &b, q, NULL);
mp_sub_d(&n, 1, q);
mp_div_2(q, q);
mp_div(q, &b, q, NULL);
mp_exch (&n, p);
mp_exch(&n, p);
res = MP_OKAY;
LBL_Z:mp_clear (&z);
LBL_Y:mp_clear (&y);
LBL_X:mp_clear (&x);
LBL_N:mp_clear (&n);
LBL_B:mp_clear (&b);
LBL_A:mp_clear (&a);
LBL_V:mp_clear (&v);
LBL_C:mp_clear (&c);
LBL_Z:
mp_clear(&z);
LBL_Y:
mp_clear(&y);
LBL_X:
mp_clear(&x);
LBL_N:
mp_clear(&n);
LBL_B:
mp_clear(&b);
LBL_A:
mp_clear(&a);
LBL_V:
mp_clear(&v);
LBL_C:
mp_clear(&c);
return res;
}
int
main (void)
int main(void)
{
mp_int p, q;
char buf[4096];
int k, li;
clock_t t1;
srand (time (NULL));
srand(time(NULL));
load_tab();
printf ("Enter # of bits: \n");
fgets (buf, sizeof (buf), stdin);
sscanf (buf, "%d", &k);
printf("Enter # of bits: \n");
fgets(buf, sizeof(buf), stdin);
sscanf(buf, "%d", &k);
printf ("Enter number of bases to try (1 to 8):\n");
fgets (buf, sizeof (buf), stdin);
sscanf (buf, "%d", &li);
printf("Enter number of bases to try (1 to 8):\n");
fgets(buf, sizeof(buf), stdin);
sscanf(buf, "%d", &li);
mp_init (&p);
mp_init (&q);
mp_init(&p);
mp_init(&q);
t1 = clock ();
pprime (k, li, &p, &q);
t1 = clock () - t1;
t1 = clock();
pprime(k, li, &p, &q);
t1 = clock() - t1;
printf ("\n\nTook %ld ticks, %d bits\n", t1, mp_count_bits (&p));
printf("\n\nTook %ld ticks, %d bits\n", t1, mp_count_bits(&p));
mp_toradix (&p, buf, 10);
printf ("P == %s\n", buf);
mp_toradix (&q, buf, 10);
printf ("Q == %s\n", buf);
mp_toradix(&p, buf, 10);
printf("P == %s\n", buf);
mp_toradix(&q, buf, 10);
printf("Q == %s\n", buf);
return 0;
}

View File

@ -14,44 +14,46 @@
#ifndef X86_TIMER
/* RDTSC from Scott Duplichan */
static uint64_t TIMFUNC (void)
{
#if defined __GNUC__
#if defined(__i386__) || defined(__x86_64__)
static uint64_t TIMFUNC(void)
{
# if defined __GNUC__
# if 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 ((uint64_t)lo)|( ((uint64_t)hi)<<32);
#else /* gcc-IA64 version */
__asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
return ((uint64_t)lo)|(((uint64_t)hi)<<32);
# else /* gcc-IA64 version */
unsigned long result;
__asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
while (__builtin_expect ((int) result == -1, 0))
while (__builtin_expect((int) result == -1, 0))
__asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
return result;
#endif
# endif
// Microsoft and Intel Windows compilers
#elif defined _M_IX86
# elif defined _M_IX86
__asm rdtsc
#elif defined _M_AMD64
return __rdtsc ();
#elif defined _M_IA64
#if defined __INTEL_COMPILER
#include <ia64intrin.h>
#endif
return __getReg (3116);
#else
#error need rdtsc function for this build
#endif
}
# elif defined _M_AMD64
return __rdtsc();
# elif defined _M_IA64
# if defined __INTEL_COMPILER
# include <ia64intrin.h>
# endif
return __getReg(3116);
# else
# error need rdtsc function for this build
# endif
}
/* *INDENT-OFF* */
/* generic ISO C timer */
uint64_t LBL_T;
void t_start(void) { LBL_T = TIMFUNC(); }
uint64_t t_read(void) { return TIMFUNC() - LBL_T; }
/* *INDENT-ON* */
#else
extern void t_start(void);
@ -64,12 +66,12 @@ uint64_t time_mult(int size, int s)
mp_int a, b, c;
uint64_t t1;
mp_init (&a);
mp_init (&b);
mp_init (&c);
mp_init(&a);
mp_init(&b);
mp_init(&c);
mp_rand (&a, size);
mp_rand (&b, size);
mp_rand(&a, size);
mp_rand(&b, size);
if (s == 1) {
KARATSUBA_MUL_CUTOFF = size;
@ -82,9 +84,9 @@ uint64_t time_mult(int size, int s)
mp_mul(&a,&b,&c);
}
t1 = t_read();
mp_clear (&a);
mp_clear (&b);
mp_clear (&c);
mp_clear(&a);
mp_clear(&b);
mp_clear(&c);
return t1;
}
@ -94,10 +96,10 @@ uint64_t time_sqr(int size, int s)
mp_int a, b;
uint64_t t1;
mp_init (&a);
mp_init (&b);
mp_init(&a);
mp_init(&b);
mp_rand (&a, size);
mp_rand(&a, size);
if (s == 1) {
KARATSUBA_SQR_CUTOFF = size;
@ -110,13 +112,12 @@ uint64_t time_sqr(int size, int s)
mp_sqr(&a,&b);
}
t1 = t_read();
mp_clear (&a);
mp_clear (&b);
mp_clear(&a);
mp_clear(&b);
return t1;
}
int
main (void)
int main(void)
{
uint64_t t1, t2;
int x, y;

View File

@ -147,4 +147,4 @@ perlcritic:
perlcritic *.pl
astyle:
astyle --options=astylerc $(OBJECTS:.o=.c)
astyle --options=astylerc $(OBJECTS:.o=.c) tommath*.h demo/*.c etc/*.c mtest/mtest.c

View File

@ -123,14 +123,13 @@ int main(int argc, char *argv[])
} else if (max == 0) {
max = 1;
}
}
else {
} else {
max = 0;
}
/* initial (2^n - 1)^2 testing, makes sure the comba multiplier works [it has the new carry code] */
/*
/*
mp_set(&a, 1);
for (n = 1; n < 8192; n++) {
mp_mul(&a, &a, &c);
@ -144,7 +143,7 @@ int main(int argc, char *argv[])
mp_mul_2(&a, &a);
mp_sub_d(&a, 1, &a);
}
*/
*/
#ifdef LTM_MTEST_REAL_RAND
rng = fopen("/dev/urandom", "rb");