Merge pull request #97 from fperrad/20171210_lint

some linting
This commit is contained in:
Steffen Jaeckel 2018-02-02 09:44:07 +01:00 committed by GitHub
commit 9f41e565bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 233 additions and 220 deletions

View File

@ -32,7 +32,7 @@
#include "tommath.h"
void ndraw(mp_int *a, char *name)
static void ndraw(mp_int *a, const char *name)
{
char buf[16000];
@ -50,10 +50,10 @@ static void draw(mp_int *a)
}
#endif
#if 0
static unsigned long lfsr = 0xAAAAAAAAUL;
unsigned long lfsr = 0xAAAAAAAAUL;
int lbit(void)
static int lbit(void)
{
if (lfsr & 0x80000000UL) {
lfsr = ((lfsr << 1) ^ 0x8000001BUL) & 0xFFFFFFFFUL;
@ -63,11 +63,13 @@ int lbit(void)
return 0;
}
}
#endif
#if defined(LTM_DEMO_REAL_RAND) && !defined(_WIN32)
static FILE *fd_urandom;
#endif
int myrng(unsigned char *dst, int len, void *dat)
#if LTM_DEMO_TEST_VS_MTEST == 0
static int myrng(unsigned char *dst, int len, void *dat)
{
int x;
(void)dat;
@ -89,6 +91,7 @@ int myrng(unsigned char *dst, int len, void *dat)
}
return len;
}
#endif
#if LTM_DEMO_TEST_VS_MTEST != 0
static void _panic(int l)
@ -104,7 +107,7 @@ static void _panic(int l)
if (!ret) { _panic(__LINE__); } \
}
mp_int a, b, c, d, e, f;
static mp_int a, b, c, d, e, f;
static void _cleanup(void)
{
@ -116,12 +119,13 @@ static void _cleanup(void)
fclose(fd_urandom);
#endif
}
#if LTM_DEMO_TEST_VS_MTEST == 0
struct mp_sqrtmod_prime_st {
unsigned long p;
unsigned long n;
mp_digit r;
};
struct mp_sqrtmod_prime_st sqrtmod_prime[] = {
static struct mp_sqrtmod_prime_st sqrtmod_prime[] = {
{ 5, 14, 3 },
{ 7, 9, 4 },
{ 113, 2, 62 }
@ -130,14 +134,18 @@ struct mp_jacobi_st {
unsigned long n;
int c[16];
};
struct mp_jacobi_st jacobi[] = {
static struct mp_jacobi_st jacobi[] = {
{ 3, { 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1 } },
{ 5, { 0, 1, -1, -1, 1, 0, 1, -1, -1, 1, 0, 1, -1, -1, 1, 0 } },
{ 7, { 1, -1, 1, -1, -1, 0, 1, 1, -1, 1, -1, -1, 0, 1, 1, -1 } },
{ 9, { -1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 } },
};
#endif
char cmd[4096], buf[4096];
#if LTM_DEMO_TEST_VS_MTEST != 0
static char cmd[4096];
#endif
static char buf[4096];
int main(void)
{
unsigned rr;
@ -715,14 +723,14 @@ printf("compare no compare!\n"); return EXIT_FAILURE;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
FGETS(buf, 4095, stdin);
sscanf(buf, "%d", &rr);
sscanf(buf, "%u", &rr);
FGETS(buf, 4095, stdin);
mp_read_radix(&b, buf, 64);
mp_mul_2d(&a, rr, &a);
a.sign = b.sign;
if (mp_cmp(&a, &b) != MP_EQ) {
printf("mul2d failed, rr == %d\n", rr);
printf("mul2d failed, rr == %u\n", rr);
draw(&a);
draw(&b);
return EXIT_FAILURE;
@ -732,17 +740,17 @@ printf("compare no compare!\n"); return EXIT_FAILURE;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
FGETS(buf, 4095, stdin);
sscanf(buf, "%d", &rr);
sscanf(buf, "%u", &rr);
FGETS(buf, 4095, stdin);
mp_read_radix(&b, buf, 64);
mp_div_2d(&a, rr, &a, &e);
a.sign = b.sign;
if (a.used == b.used && a.used == 0) {
if ((a.used == b.used) && (a.used == 0)) {
a.sign = b.sign = MP_ZPOS;
}
if (mp_cmp(&a, &b) != MP_EQ) {
printf("div2d failed, rr == %d\n", rr);
printf("div2d failed, rr == %u\n", rr);
draw(&a);
draw(&b);
return EXIT_FAILURE;
@ -839,7 +847,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE;
mp_read_radix(&d, buf, 64);
mp_div(&a, &b, &e, &f);
if (mp_cmp(&c, &e) != MP_EQ || mp_cmp(&d, &f) != MP_EQ) {
if ((mp_cmp(&c, &e) != MP_EQ) || (mp_cmp(&d, &f) != MP_EQ)) {
printf("div %lu %d, %d, failure!\n", div_n, mp_cmp(&c, &e),
mp_cmp(&d, &f));
draw(&a);

View File

@ -3,8 +3,6 @@
#include <unistd.h>
#include <inttypes.h>
uint64_t _tt;
#ifdef IOWNANATHLON
#include <unistd.h>
#define SLEEP sleep(4)
@ -19,7 +17,7 @@ uint64_t _tt;
#endif
void ndraw(mp_int *a, char *name)
static void ndraw(mp_int *a, const char *name)
{
char buf[4096];
@ -34,9 +32,9 @@ static void draw(mp_int *a)
}
unsigned long lfsr = 0xAAAAAAAAUL;
static unsigned long lfsr = 0xAAAAAAAAUL;
int lbit(void)
static int lbit(void)
{
if (lfsr & 0x80000000UL) {
lfsr = ((lfsr << 1) ^ 0x8000001BUL) & 0xFFFFFFFFUL;
@ -181,7 +179,7 @@ int main(void)
TOOM_SQR_CUTOFF = (ix == 2) ? old_toom_s : 9999;
log = FOPEN((ix == 0) ? "logs/mult.log" : (ix == 1) ? "logs/mult_kara.log" : "logs/mult_toom.log", "w");
for (cnt = 4; cnt <= 10240 / DIGIT_BIT; cnt += 2) {
for (cnt = 4; cnt <= (10240 / DIGIT_BIT); cnt += 2) {
SLEEP;
mp_rand(&a, cnt);
mp_rand(&b, cnt);
@ -202,7 +200,7 @@ int main(void)
FCLOSE(log);
log = FOPEN((ix == 0) ? "logs/sqr.log" : (ix == 1) ? "logs/sqr_kara.log" : "logs/sqr_toom.log", "w");
for (cnt = 4; cnt <= 10240 / DIGIT_BIT; cnt += 2) {
for (cnt = 4; cnt <= (10240 / DIGIT_BIT); cnt += 2) {
SLEEP;
mp_rand(&a, cnt);
rr = 0;
@ -224,7 +222,7 @@ int main(void)
}
{
char *primes[] = {
const char *primes[] = {
/* 2K large moduli */
"179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586239334100047359817950870678242457666208137217",
"32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638099733077152121140120031150424541696791951097529546801429027668869927491725169",
@ -292,7 +290,7 @@ int main(void)
}
printf("Exponentiating\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
FPRINTF(n < 4 ? logd : (n < 9) ? logc : (n < 16) ? logb : log,
FPRINTF((n < 4) ? logd : (n < 9) ? logc : (n < 16) ? logb : log,
"%d %9" PRIu64 "\n", mp_count_bits(&a), tt);
}
}

View File

@ -2,7 +2,7 @@
#include <tommath.h>
#include <time.h>
int sizes[] = {256, 512, 768, 1024, 1536, 2048, 3072, 4096};
static int sizes[] = {256, 512, 768, 1024, 1536, 2048, 3072, 4096};
int main(void)
{
@ -16,61 +16,64 @@ int main(void)
mp_init_multi(&q, &p, NULL);
out = fopen("2kprime.1", "w");
for (x = 0; x < (int)(sizeof(sizes) / sizeof(sizes[0])); x++) {
if (out != NULL) {
for (x = 0; x < (int)(sizeof(sizes) / sizeof(sizes[0])); x++) {
top:
mp_2expt(&q, sizes[x]);
mp_add_d(&q, 3, &q);
z = -3;
mp_2expt(&q, sizes[x]);
mp_add_d(&q, 3, &q);
z = -3;
t1 = clock();
for (;;) {
mp_sub_d(&q, 4, &q);
z += 4;
t1 = clock();
for (;;) {
mp_sub_d(&q, 4, &q);
z += 4;
if (z > MP_MASK) {
printf("No primes of size %d found\n", sizes[x]);
break;
}
if ((clock() - t1) > CLOCKS_PER_SEC) {
printf(".");
fflush(stdout);
// sleep((clock() - t1 + CLOCKS_PER_SEC/2)/CLOCKS_PER_SEC);
t1 = clock();
}
/* quick test on q */
mp_prime_is_prime(&q, 1, &y);
if (y == 0) {
continue;
}
/* find (q-1)/2 */
mp_sub_d(&q, 1, &p);
mp_div_2(&p, &p);
mp_prime_is_prime(&p, 3, &y);
if (y == 0) {
continue;
}
/* test on q */
mp_prime_is_prime(&q, 3, &y);
if (y == 0) {
continue;
}
if (z > MP_MASK) {
printf("No primes of size %d found\n", sizes[x]);
break;
}
if (clock() - t1 > CLOCKS_PER_SEC) {
printf(".");
fflush(stdout);
// sleep((clock() - t1 + CLOCKS_PER_SEC/2)/CLOCKS_PER_SEC);
t1 = clock();
}
/* quick test on q */
mp_prime_is_prime(&q, 1, &y);
if (y == 0) {
continue;
++sizes[x];
goto top;
}
/* find (q-1)/2 */
mp_sub_d(&q, 1, &p);
mp_div_2(&p, &p);
mp_prime_is_prime(&p, 3, &y);
if (y == 0) {
continue;
}
/* test on q */
mp_prime_is_prime(&q, 3, &y);
if (y == 0) {
continue;
}
break;
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);
}
if (y == 0) {
++sizes[x];
goto top;
}
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);
fclose(out);
}
return 0;

View File

@ -1,7 +1,7 @@
/* Makes safe primes of a DR nature */
#include <tommath.h>
int sizes[] = { 1+256/DIGIT_BIT, 1+512/DIGIT_BIT, 1+768/DIGIT_BIT, 1+1024/DIGIT_BIT, 1+2048/DIGIT_BIT, 1+4096/DIGIT_BIT };
static int sizes[] = { 1+256/DIGIT_BIT, 1+512/DIGIT_BIT, 1+768/DIGIT_BIT, 1+1024/DIGIT_BIT, 1+2048/DIGIT_BIT, 1+4096/DIGIT_BIT };
int main(void)
{
@ -14,48 +14,50 @@ int main(void)
mp_init(&b);
out = fopen("drprimes.txt", "w");
for (x = 0; x < (int)(sizeof(sizes)/sizeof(sizes[0])); x++) {
if (out != NULL) {
for (x = 0; x < (int)(sizeof(sizes)/sizeof(sizes[0])); x++) {
top:
printf("Seeking a %d-bit safe prime\n", sizes[x] * DIGIT_BIT);
mp_grow(&a, sizes[x]);
mp_zero(&a);
for (y = 1; y < sizes[x]; y++) {
a.dp[y] = MP_MASK;
}
printf("Seeking a %d-bit safe prime\n", sizes[x] * DIGIT_BIT);
mp_grow(&a, sizes[x]);
mp_zero(&a);
for (y = 1; y < sizes[x]; y++) {
a.dp[y] = MP_MASK;
}
/* make a DR modulus */
a.dp[0] = -1;
a.used = sizes[x];
/* make a DR modulus */
a.dp[0] = -1;
a.used = sizes[x];
/* now loop */
res = 0;
for (;;) {
a.dp[0] += 4;
if (a.dp[0] >= MP_MASK) break;
mp_prime_is_prime(&a, 1, &res);
if (res == 0) continue;
printf(".");
fflush(stdout);
mp_sub_d(&a, 1, &b);
mp_div_2(&b, &b);
mp_prime_is_prime(&b, 3, &res);
if (res == 0) continue;
mp_prime_is_prime(&a, 3, &res);
if (res == 1) break;
}
/* now loop */
res = 0;
for (;;) {
a.dp[0] += 4;
if (a.dp[0] >= MP_MASK) break;
mp_prime_is_prime(&a, 1, &res);
if (res == 0) continue;
printf(".");
fflush(stdout);
mp_sub_d(&a, 1, &b);
mp_div_2(&b, &b);
mp_prime_is_prime(&b, 3, &res);
if (res == 0) continue;
mp_prime_is_prime(&a, 3, &res);
if (res == 1) break;
}
if (res != 1) {
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);
if (res != 1) {
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);
}
}
fclose(out);
}
fclose(out);
mp_clear(&a);
mp_clear(&b);

View File

@ -5,7 +5,7 @@
#include <time.h>
#include <tommath.h>
int is_mersenne(long s, int *pp)
static int is_mersenne(long s, int *pp)
{
mp_int n, u;
int res, k;
@ -32,7 +32,7 @@ int is_mersenne(long s, int *pp)
mp_set(&u, 4);
/* for k=1 to s-2 do */
for (k = 1; k <= s - 2; k++) {
for (k = 1; k <= (s - 2); k++) {
/* u = u^2 - 2 mod n */
if ((res = mp_sqr(&u, &u)) != MP_OKAY) {
goto LBL_MU;
@ -69,7 +69,7 @@ LBL_N:
}
/* square root of a long < 65536 */
long i_sqrt(long x)
static long i_sqrt(long x)
{
long x1, x2;
@ -79,7 +79,7 @@ long i_sqrt(long x)
x2 = x1 - ((x1 * x1) - x) / (2 * x1);
} while (x1 != x2);
if (x1 * x1 > x) {
if ((x1 * x1) > x) {
--x1;
}
@ -87,7 +87,7 @@ long i_sqrt(long x)
}
/* is the long prime by brute force */
int isprime(long k)
static int isprime(long k)
{
long y, z;

View File

@ -1,5 +1,6 @@
/* tests the montgomery routines */
#include <tommath.h>
#include <time.h>
int main(void)
{

View File

@ -7,8 +7,8 @@
#include <time.h>
#include "tommath.h"
int n_prime;
FILE *primes;
static int n_prime;
static FILE *primes;
/* fast square root */
static mp_digit i_sqrt(mp_word x)
@ -21,7 +21,7 @@ static mp_digit i_sqrt(mp_word x)
x2 = x1 - ((x1 * x1) - x) / (2 * x1);
} while (x1 != x2);
if (x1 * x1 > x) {
if ((x1 * x1) > x) {
--x1;
}
@ -36,8 +36,9 @@ static void gen_prime(void)
FILE *out;
out = fopen("pprime.dat", "wb");
if (out != NULL) {
/* write first set of primes */
/* 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);
@ -51,108 +52,109 @@ static void gen_prime(void)
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);
next = (y + 1) * (y + 1);
/* get square root, since if 'r' is composite its factors must be < than this */
y = i_sqrt(r);
next = (y + 1) * (y + 1);
for (;;) {
do {
r += 2; /* next candidate */
r &= MP_MASK;
for (;;) {
do {
r += 2; /* next candidate */
r &= MP_MASK;
if (r < 31) break;
/* update sqrt ? */
if (next <= r) {
++y;
next = (y + 1) * (y + 1);
}
/* loop if divisible by 3,5,7,11,13,17,19,23,29 */
if ((r % 3) == 0) {
x = 0;
continue;
}
if ((r % 5) == 0) {
x = 0;
continue;
}
if ((r % 7) == 0) {
x = 0;
continue;
}
if ((r % 11) == 0) {
x = 0;
continue;
}
if ((r % 13) == 0) {
x = 0;
continue;
}
if ((r % 17) == 0) {
x = 0;
continue;
}
if ((r % 19) == 0) {
x = 0;
continue;
}
if ((r % 23) == 0) {
x = 0;
continue;
}
if ((r % 29) == 0) {
x = 0;
continue;
}
/* now check if r is divisible by x + k={1,7,11,13,17,19,23,29} */
for (x = 30; x <= y; x += 30) {
if ((r % (x + 1)) == 0) {
x = 0;
break;
}
if ((r % (x + 7)) == 0) {
x = 0;
break;
}
if ((r % (x + 11)) == 0) {
x = 0;
break;
}
if ((r % (x + 13)) == 0) {
x = 0;
break;
}
if ((r % (x + 17)) == 0) {
x = 0;
break;
}
if ((r % (x + 19)) == 0) {
x = 0;
break;
}
if ((r % (x + 23)) == 0) {
x = 0;
break;
}
if ((r % (x + 29)) == 0) {
x = 0;
break;
}
}
} while (x == 0);
if (r > 31) {
fwrite(&r, 1, sizeof(mp_digit), out);
printf("%9u\r", r);
fflush(stdout);
}
if (r < 31) break;
/* update sqrt ? */
if (next <= r) {
++y;
next = (y + 1) * (y + 1);
}
/* loop if divisible by 3,5,7,11,13,17,19,23,29 */
if ((r % 3) == 0) {
x = 0;
continue;
}
if ((r % 5) == 0) {
x = 0;
continue;
}
if ((r % 7) == 0) {
x = 0;
continue;
}
if ((r % 11) == 0) {
x = 0;
continue;
}
if ((r % 13) == 0) {
x = 0;
continue;
}
if ((r % 17) == 0) {
x = 0;
continue;
}
if ((r % 19) == 0) {
x = 0;
continue;
}
if ((r % 23) == 0) {
x = 0;
continue;
}
if ((r % 29) == 0) {
x = 0;
continue;
}
/* now check if r is divisible by x + k={1,7,11,13,17,19,23,29} */
for (x = 30; x <= y; x += 30) {
if ((r % (x + 1)) == 0) {
x = 0;
break;
}
if ((r % (x + 7)) == 0) {
x = 0;
break;
}
if ((r % (x + 11)) == 0) {
x = 0;
break;
}
if ((r % (x + 13)) == 0) {
x = 0;
break;
}
if ((r % (x + 17)) == 0) {
x = 0;
break;
}
if ((r % (x + 19)) == 0) {
x = 0;
break;
}
if ((r % (x + 23)) == 0) {
x = 0;
break;
}
if ((r % (x + 29)) == 0) {
x = 0;
break;
}
}
} while (x == 0);
if (r > 31) {
fwrite(&r, 1, sizeof(mp_digit), out);
printf("%9d\r", r);
fflush(stdout);
}
if (r < 31) break;
}
fclose(out);
fclose(out);
}
}
void load_tab(void)
static void load_tab(void)
{
primes = fopen("pprime.dat", "rb");
if (primes == NULL) {
@ -163,7 +165,7 @@ void load_tab(void)
n_prime = ftell(primes) / sizeof(mp_digit);
}
mp_digit prime_digit(void)
static mp_digit prime_digit(void)
{
int n;
mp_digit d;
@ -176,7 +178,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)
static 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;

View File

@ -3,7 +3,6 @@
* Tom St Denis, tstdenis82@gmail.com
*/
#include <tommath.h>
#include <time.h>
#include <stdint.h>
/* how many times todo each size mult. Depends on your computer. For slow computers
@ -50,9 +49,9 @@ static uint64_t TIMFUNC(void)
/* *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; }
static uint64_t LBL_T;
static void t_start(void) { LBL_T = TIMFUNC(); }
static uint64_t t_read(void) { return TIMFUNC() - LBL_T; }
/* *INDENT-ON* */
#else
@ -60,7 +59,7 @@ extern void t_start(void);
extern uint64_t t_read(void);
#endif
uint64_t time_mult(int size, int s)
static uint64_t time_mult(int size, int s)
{
unsigned long x;
mp_int a, b, c;
@ -90,7 +89,7 @@ uint64_t time_mult(int size, int s)
return t1;
}
uint64_t time_sqr(int size, int s)
static uint64_t time_sqr(int size, int s)
{
unsigned long x;
mp_int a, b;