From 7c671e10d7396f4961f46dc996694a61c2918903 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Fri, 2 Feb 2018 13:14:05 +0100 Subject: [PATCH 1/5] remove unreachable code --- etc/mersenne.c | 1 - 1 file changed, 1 deletion(-) diff --git a/etc/mersenne.c b/etc/mersenne.c index 75dd9d9..c5b1d6e 100644 --- a/etc/mersenne.c +++ b/etc/mersenne.c @@ -134,7 +134,6 @@ int main(void) k += 2; } } - return 0; } /* ref: $Format:%D$ */ From d051d6ba58ea438d852145f59282395202b95e96 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Fri, 2 Feb 2018 13:38:56 +0100 Subject: [PATCH 2/5] remove unused code --- demo/demo.c | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/demo/demo.c b/demo/demo.c index 57afbaf..024eb66 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -1,13 +1,6 @@ #include #include -#ifdef IOWNANATHLON -#include -#define SLEEP sleep(4) -#else -#define SLEEP -#endif - /* * Configuration */ @@ -50,21 +43,6 @@ static void draw(mp_int *a) } #endif -#if 0 -static unsigned long lfsr = 0xAAAAAAAAUL; - -static int lbit(void) -{ - if (lfsr & 0x80000000UL) { - lfsr = ((lfsr << 1) ^ 0x8000001BUL) & 0xFFFFFFFFUL; - return 1; - } else { - lfsr <<= 1; - return 0; - } -} -#endif - #if defined(LTM_DEMO_REAL_RAND) && !defined(_WIN32) static FILE *fd_urandom; #endif From e3598dc8b20b75fb15d365844a83c15ac53532f3 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Fri, 2 Feb 2018 17:44:40 +0100 Subject: [PATCH 3/5] explicit condition --- demo/demo.c | 32 ++++++++++++++++---------------- demo/timing.c | 6 +++--- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/demo/demo.c b/demo/demo.c index 024eb66..9710151 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -696,7 +696,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; cmd[strlen(cmd) - 1] = 0; printf("%-6s ]\r", cmd); fflush(stdout); - if (!strcmp(cmd, "mul2d")) { + if (strcmp(cmd, "mul2d") == 0) { ++mul2d_n; FGETS(buf, 4095, stdin); mp_read_radix(&a, buf, 64); @@ -713,7 +713,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; draw(&b); return EXIT_FAILURE; } - } else if (!strcmp(cmd, "div2d")) { + } else if (strcmp(cmd, "div2d") == 0) { ++div2d_n; FGETS(buf, 4095, stdin); mp_read_radix(&a, buf, 64); @@ -733,7 +733,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; draw(&b); return EXIT_FAILURE; } - } else if (!strcmp(cmd, "add")) { + } else if (strcmp(cmd, "add") == 0) { ++add_n; FGETS(buf, 4095, stdin); mp_read_radix(&a, buf, 64); @@ -777,7 +777,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; return EXIT_FAILURE; } - } else if (!strcmp(cmd, "sub")) { + } else if (strcmp(cmd, "sub") == 0) { ++sub_n; FGETS(buf, 4095, stdin); mp_read_radix(&a, buf, 64); @@ -795,7 +795,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; draw(&d); return EXIT_FAILURE; } - } else if (!strcmp(cmd, "mul")) { + } else if (strcmp(cmd, "mul") == 0) { ++mul_n; FGETS(buf, 4095, stdin); mp_read_radix(&a, buf, 64); @@ -813,7 +813,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; draw(&d); return EXIT_FAILURE; } - } else if (!strcmp(cmd, "div")) { + } else if (strcmp(cmd, "div") == 0) { ++div_n; FGETS(buf, 4095, stdin); mp_read_radix(&a, buf, 64); @@ -837,7 +837,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; return EXIT_FAILURE; } - } else if (!strcmp(cmd, "sqr")) { + } else if (strcmp(cmd, "sqr") == 0) { ++sqr_n; FGETS(buf, 4095, stdin); mp_read_radix(&a, buf, 64); @@ -852,7 +852,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; draw(&c); return EXIT_FAILURE; } - } else if (!strcmp(cmd, "gcd")) { + } else if (strcmp(cmd, "gcd") == 0) { ++gcd_n; FGETS(buf, 4095, stdin); mp_read_radix(&a, buf, 64); @@ -871,7 +871,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; draw(&d); return EXIT_FAILURE; } - } else if (!strcmp(cmd, "lcm")) { + } else if (strcmp(cmd, "lcm") == 0) { ++lcm_n; FGETS(buf, 4095, stdin); mp_read_radix(&a, buf, 64); @@ -890,7 +890,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; draw(&d); return EXIT_FAILURE; } - } else if (!strcmp(cmd, "expt")) { + } else if (strcmp(cmd, "expt") == 0) { ++expt_n; FGETS(buf, 4095, stdin); mp_read_radix(&a, buf, 64); @@ -911,7 +911,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; draw(&e); return EXIT_FAILURE; } - } else if (!strcmp(cmd, "invmod")) { + } else if (strcmp(cmd, "invmod") == 0) { ++inv_n; FGETS(buf, 4095, stdin); mp_read_radix(&a, buf, 64); @@ -933,7 +933,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; return EXIT_FAILURE; } - } else if (!strcmp(cmd, "div2")) { + } else if (strcmp(cmd, "div2") == 0) { ++div2_n; FGETS(buf, 4095, stdin); mp_read_radix(&a, buf, 64); @@ -947,7 +947,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; draw(&c); return EXIT_FAILURE; } - } else if (!strcmp(cmd, "mul2")) { + } else if (strcmp(cmd, "mul2") == 0) { ++mul2_n; FGETS(buf, 4095, stdin); mp_read_radix(&a, buf, 64); @@ -961,7 +961,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; draw(&c); return EXIT_FAILURE; } - } else if (!strcmp(cmd, "add_d")) { + } else if (strcmp(cmd, "add_d") == 0) { ++add_d_n; FGETS(buf, 4095, stdin); mp_read_radix(&a, buf, 64); @@ -978,7 +978,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; printf("d == %d\n", ix); return EXIT_FAILURE; } - } else if (!strcmp(cmd, "sub_d")) { + } else if (strcmp(cmd, "sub_d") == 0) { ++sub_d_n; FGETS(buf, 4095, stdin); mp_read_radix(&a, buf, 64); @@ -995,7 +995,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; printf("d == %d\n", ix); return EXIT_FAILURE; } - } else if (!strcmp(cmd, "exit")) { + } else if (strcmp(cmd, "exit") == 0) { printf("\nokay, exiting now\n"); break; } diff --git a/demo/timing.c b/demo/timing.c index 9080723..de58278 100644 --- a/demo/timing.c +++ b/demo/timing.c @@ -36,7 +36,7 @@ static unsigned long lfsr = 0xAAAAAAAAUL; static int lbit(void) { - if (lfsr & 0x80000000UL) { + if ((lfsr & 0x80000000UL) != 0UL) { lfsr = ((lfsr << 1) ^ 0x8000001BUL) & 0xFFFFFFFFUL; return 1; } else { @@ -258,7 +258,7 @@ int main(void) logb = FOPEN("logs/expt_dr.log", "w"); logc = FOPEN("logs/expt_2k.log", "w"); logd = FOPEN("logs/expt_2kl.log", "w"); - for (n = 0; primes[n]; n++) { + for (n = 0; primes[n] != NULL; n++) { SLEEP; mp_read_radix(&a, primes[n], 10); mp_zero(&b); @@ -283,7 +283,7 @@ int main(void) mp_sub(&e, &b, &b); mp_exptmod(&c, &b, &a, &e); /* c^(p-1-b) mod a */ mp_mulmod(&e, &d, &a, &d); /* c^b * c^(p-1-b) == c^p-1 == 1 */ - if (mp_cmp_d(&d, 1)) { + if (mp_cmp_d(&d, 1) != MP_EQ) { printf("Different (%d)!!!\n", mp_count_bits(&a)); draw(&d); exit(0); From 9960fe3fe8ff2a30b62eab8f3052701c76f51dca Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Tue, 13 Feb 2018 19:04:25 +0100 Subject: [PATCH 4/5] literal suffix --- demo/demo.c | 46 ++++++++--------- demo/timing.c | 56 ++++++++++----------- etc/2kprime.c | 8 +-- etc/drprime.c | 4 +- etc/mersenne.c | 8 +-- etc/mont.c | 2 +- etc/pprime.c | 130 ++++++++++++++++++++++++------------------------- 7 files changed, 127 insertions(+), 127 deletions(-) diff --git a/demo/demo.c b/demo/demo.c index 9710151..357d26e 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -57,15 +57,15 @@ static int myrng(unsigned char *dst, int len, void *dat) fprintf(stderr, "\nno /dev/urandom\n"); # endif } else { - return fread(dst, 1, len, fd_urandom); + return fread(dst, 1uL, len, fd_urandom); } #endif for (x = 0; x < len;) { unsigned int r = (unsigned int)rand(); do { - dst[x++] = r & 0xFF; + dst[x++] = r & 0xFFu; r >>= 8; - } while ((r != 0) && (x < len)); + } while ((r != 0u) && (x < len)); } return len; } @@ -195,7 +195,7 @@ int main(void) return EXIT_FAILURE; } // a: -5-> b: -4 - mp_add_d(&a, 1, &b); + mp_add_d(&a, 1uL, &b); if (mp_isneg(&b) != MP_YES) { return EXIT_FAILURE; } @@ -203,18 +203,18 @@ int main(void) return EXIT_FAILURE; } // a: -5-> b: 1 - mp_add_d(&a, 6, &b); + mp_add_d(&a, 6uL, &b); if (mp_get_int(&b) != 1) { return EXIT_FAILURE; } // a: -5-> a: 1 - mp_add_d(&a, 6, &a); + mp_add_d(&a, 6uL, &a); if (mp_get_int(&a) != 1) { return EXIT_FAILURE; } mp_zero(&a); // a: 0-> a: 6 - mp_add_d(&a, 6, &a); + mp_add_d(&a, 6uL, &a); if (mp_get_int(&a) != 6) { return EXIT_FAILURE; } @@ -264,7 +264,7 @@ 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; + t = ((unsigned long) rand() * rand() + 1) & 0xFFFFFFFFuL; mp_set_int(&a, t); if (t != mp_get_int(&a)) { printf("\nmp_get_int() bad result!"); @@ -276,8 +276,8 @@ int main(void) printf("\nmp_get_int() bad result!"); return EXIT_FAILURE; } - mp_set_int(&a, 0xffffffff); - if (mp_get_int(&a) != 0xffffffff) { + mp_set_int(&a, 0xFFFFFFFFuL); + if (mp_get_int(&a) != 0xFFFFFFFFuL) { printf("\nmp_get_int() bad result!"); return EXIT_FAILURE; } @@ -364,7 +364,7 @@ int main(void) } /* test for false positives */ - mp_add_d(&a, 1, &a); + mp_add_d(&a, 1uL, &a); if (mp_is_square(&a, &n) != MP_OKAY) { printf("\nfp:mp_is_square() error!"); return EXIT_FAILURE; @@ -425,7 +425,7 @@ int main(void) return EXIT_FAILURE; } /* let's see if it's really a safe prime */ - mp_sub_d(&a, 1, &a); + mp_sub_d(&a, 1uL, &a); mp_div_2(&a, &a); mp_prime_is_prime(&a, 8, &cnt); if (cnt != MP_YES) { @@ -503,7 +503,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; /* test mp_cnt_lsb */ printf("\n\nTesting: mp_cnt_lsb"); - mp_set(&a, 1); + mp_set(&a, 1uL); for (ix = 0; ix < 1024; ix++) { if (mp_cnt_lsb(&a) != ix) { printf("Failed at %d, %d\n", ix, mp_cnt_lsb(&a)); @@ -518,7 +518,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; mp_digit tmp; mp_2expt(&a, cnt); - mp_sub_d(&a, 2, &a); /* a = 2**cnt - 2 */ + mp_sub_d(&a, 2uL, &a); /* a = 2**cnt - 2 */ printf("\r %4d bits", cnt); printf("(%d)", mp_reduce_is_2k(&a)); @@ -532,7 +532,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; mp_rand(&b, (cnt / DIGIT_BIT + 1) * 2); mp_copy(&c, &b); mp_mod(&c, &a, &c); - mp_reduce_2k(&b, &a, 2); + mp_reduce_2k(&b, &a, 2uL); if (mp_cmp(&c, &b)) { printf("FAILED\n"); return EXIT_FAILURE; @@ -542,7 +542,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; /* test mp_div_3 */ printf("\n\nTesting: mp_div_3...\n"); - mp_set(&d, 3); + mp_set(&d, 3uL); for (cnt = 0; cnt < 10000;) { mp_digit r2; @@ -582,7 +582,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; fflush(stdout); } mp_sqr(&b, &b); - mp_add_d(&b, 1, &b); + mp_add_d(&b, 1uL, &b); mp_copy(&b, &c); mp_mod(&b, &a, &b); @@ -632,10 +632,10 @@ printf("compare no compare!\n"); return EXIT_FAILURE; fflush(stdout); for (cnt = 0; cnt < (int)(1UL << 20); cnt++) { mp_sqr(&b, &b); - mp_add_d(&b, 1, &b); + mp_add_d(&b, 1uL, &b); mp_reduce_2k_l(&b, &a, &d); mp_sqr(&c, &c); - mp_add_d(&c, 1, &c); + mp_add_d(&c, 1uL, &c); mp_mod(&c, &a, &c); if (mp_cmp(&b, &c) != MP_EQ) { printf("mp_reduce_2k_l() failed at step %d\n", cnt); @@ -693,7 +693,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; 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); FGETS(cmd, 4095, stdin); - cmd[strlen(cmd) - 1] = 0; + cmd[strlen(cmd) - 1u] = '\0'; printf("%-6s ]\r", cmd); fflush(stdout); if (strcmp(cmd, "mul2d") == 0) { @@ -756,7 +756,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; rr = mp_signed_bin_size(&c); mp_to_signed_bin(&c, (unsigned char *) cmd); - memset(cmd + rr, rand() & 255, sizeof(cmd) - rr); + memset(cmd + rr, rand() & 0xFFu, sizeof(cmd) - rr); mp_read_signed_bin(&d, (unsigned char *) cmd, rr); if (mp_cmp(&c, &d) != MP_EQ) { printf("mp_signed_bin failure!\n"); @@ -768,7 +768,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; rr = mp_unsigned_bin_size(&c); mp_to_unsigned_bin(&c, (unsigned char *) cmd); - memset(cmd + rr, rand() & 255, sizeof(cmd) - rr); + memset(cmd + rr, rand() & 0xFFu, sizeof(cmd) - rr); mp_read_unsigned_bin(&d, (unsigned char *) cmd, rr); if (mp_cmp_mag(&c, &d) != MP_EQ) { printf("mp_unsigned_bin failure!\n"); @@ -921,7 +921,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; mp_read_radix(&c, buf, 64); mp_invmod(&a, &b, &d); mp_mulmod(&d, &a, &b, &e); - if (mp_cmp_d(&e, 1) != MP_EQ) { + if (mp_cmp_d(&e, 1uL) != MP_EQ) { printf("inv [wrong value from MPI?!] failure\n"); draw(&a); draw(&b); diff --git a/demo/timing.c b/demo/timing.c index de58278..2b4f30f 100644 --- a/demo/timing.c +++ b/demo/timing.c @@ -32,12 +32,12 @@ static void draw(mp_int *a) } -static unsigned long lfsr = 0xAAAAAAAAUL; +static unsigned long lfsr = 0xAAAAAAAAuL; static int lbit(void) { - if ((lfsr & 0x80000000UL) != 0UL) { - lfsr = ((lfsr << 1) ^ 0x8000001BUL) & 0xFFFFFFFFUL; + if ((lfsr & 0x80000000uL) != 0uL) { + lfsr = ((lfsr << 1) ^ 0x8000001BuL) & 0xFFFFFFFFuL; return 1; } else { lfsr <<= 1; @@ -126,15 +126,15 @@ int main(void) SLEEP; mp_rand(&a, cnt); mp_rand(&b, cnt); - rr = 0; - tt = -1; + rr = 0u; + tt = UINT64_MAX; do { gg = TIMFUNC(); DO(mp_add(&a, &b, &c)); gg = (TIMFUNC() - gg) >> 1; if (tt > gg) tt = gg; - } while (++rr < 100000); + } while (++rr < 100000u); printf("Adding\t\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n", mp_count_bits(&a), CLK_PER_SEC / tt, tt); FPRINTF(log, "%d %9" PRIu64 "\n", cnt * DIGIT_BIT, tt); @@ -147,15 +147,15 @@ int main(void) SLEEP; mp_rand(&a, cnt); mp_rand(&b, cnt); - rr = 0; - tt = -1; + rr = 0u; + tt = UINT64_MAX; do { gg = TIMFUNC(); DO(mp_sub(&a, &b, &c)); gg = (TIMFUNC() - gg) >> 1; if (tt > gg) tt = gg; - } while (++rr < 100000); + } while (++rr < 100000u); printf("Subtracting\t\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n", mp_count_bits(&a), CLK_PER_SEC / tt, tt); @@ -183,15 +183,15 @@ int main(void) SLEEP; mp_rand(&a, cnt); mp_rand(&b, cnt); - rr = 0; - tt = -1; + rr = 0u; + tt = UINT64_MAX; do { gg = TIMFUNC(); DO(mp_mul(&a, &b, &c)); gg = (TIMFUNC() - gg) >> 1; if (tt > gg) tt = gg; - } while (++rr < 100); + } while (++rr < 100u); printf("Multiplying\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n", mp_count_bits(&a), CLK_PER_SEC / tt, tt); FPRINTF(log, "%d %9" PRIu64 "\n", mp_count_bits(&a), tt); @@ -203,15 +203,15 @@ int main(void) for (cnt = 4; cnt <= (10240 / DIGIT_BIT); cnt += 2) { SLEEP; mp_rand(&a, cnt); - rr = 0; - tt = -1; + rr = 0u; + tt = UINT64_MAX; do { gg = TIMFUNC(); DO(mp_sqr(&a, &b)); gg = (TIMFUNC() - gg) >> 1; if (tt > gg) tt = gg; - } while (++rr < 100); + } while (++rr < 100u); printf("Squaring\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n", mp_count_bits(&a), CLK_PER_SEC / tt, tt); FPRINTF(log, "%d %9" PRIu64 "\n", mp_count_bits(&a), tt); @@ -267,23 +267,23 @@ int main(void) b.dp[0] |= lbit(); b.used += 1; } - mp_sub_d(&a, 1, &c); + mp_sub_d(&a, 1uL, &c); mp_mod(&b, &c, &b); - mp_set(&c, 3); - rr = 0; - tt = -1; + mp_set(&c, 3uL); + rr = 0u; + tt = UINT64_MAX; do { gg = TIMFUNC(); DO(mp_exptmod(&c, &b, &a, &d)); gg = (TIMFUNC() - gg) >> 1; if (tt > gg) tt = gg; - } while (++rr < 10); - mp_sub_d(&a, 1, &e); + } while (++rr < 10u); + mp_sub_d(&a, 1uL, &e); mp_sub(&e, &b, &b); mp_exptmod(&c, &b, &a, &e); /* c^(p-1-b) mod a */ mp_mulmod(&e, &d, &a, &d); /* c^b * c^(p-1-b) == c^p-1 == 1 */ - if (mp_cmp_d(&d, 1) != MP_EQ) { + if (mp_cmp_d(&d, 1uL) != MP_EQ) { printf("Different (%d)!!!\n", mp_count_bits(&a)); draw(&d); exit(0); @@ -306,21 +306,21 @@ int main(void) mp_rand(&b, cnt); do { - mp_add_d(&b, 1, &b); + mp_add_d(&b, 1uL, &b); mp_gcd(&a, &b, &c); - } while (mp_cmp_d(&c, 1) != MP_EQ); + } while (mp_cmp_d(&c, 1uL) != MP_EQ); - rr = 0; - tt = -1; + rr = 0u; + tt = UINT64_MAX; do { gg = TIMFUNC(); DO(mp_invmod(&b, &a, &c)); gg = (TIMFUNC() - gg) >> 1; if (tt > gg) tt = gg; - } while (++rr < 1000); + } while (++rr < 1000u); mp_mulmod(&b, &c, &a, &d); - if (mp_cmp_d(&d, 1) != MP_EQ) { + if (mp_cmp_d(&d, 1uL) != MP_EQ) { printf("Failed to invert\n"); return 0; } diff --git a/etc/2kprime.c b/etc/2kprime.c index f8761fb..803ece7 100644 --- a/etc/2kprime.c +++ b/etc/2kprime.c @@ -20,13 +20,13 @@ int main(void) for (x = 0; x < (int)(sizeof(sizes) / sizeof(sizes[0])); x++) { top: mp_2expt(&q, sizes[x]); - mp_add_d(&q, 3, &q); + mp_add_d(&q, 3uL, &q); z = -3; t1 = clock(); for (;;) { - mp_sub_d(&q, 4, &q); - z += 4; + mp_sub_d(&q, 4uL, &q); + z += 4uL; if (z > MP_MASK) { printf("No primes of size %d found\n", sizes[x]); @@ -47,7 +47,7 @@ top: } /* find (q-1)/2 */ - mp_sub_d(&q, 1, &p); + mp_sub_d(&q, 1uL, &p); mp_div_2(&p, &p); mp_prime_is_prime(&p, 3, &y); if (y == 0) { diff --git a/etc/drprime.c b/etc/drprime.c index 42504b9..dd1d9d6 100644 --- a/etc/drprime.c +++ b/etc/drprime.c @@ -31,13 +31,13 @@ top: /* now loop */ res = 0; for (;;) { - a.dp[0] += 4; + a.dp[0] += 4uL; 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_sub_d(&a, 1uL, &b); mp_div_2(&b, &b); mp_prime_is_prime(&b, 3, &res); if (res == 0) continue; diff --git a/etc/mersenne.c b/etc/mersenne.c index c5b1d6e..6e4759b 100644 --- a/etc/mersenne.c +++ b/etc/mersenne.c @@ -24,12 +24,12 @@ static int 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, 1uL, &n)) != MP_OKAY) { goto LBL_MU; } /* set u=4 */ - mp_set(&u, 4); + mp_set(&u, 4uL); /* for k=1 to s-2 do */ for (k = 1; k <= (s - 2); k++) { @@ -37,7 +37,7 @@ static int is_mersenne(long s, int *pp) 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, 2uL, &u)) != MP_OKAY) { goto LBL_MU; } @@ -49,7 +49,7 @@ static int is_mersenne(long s, int *pp) } /* reduce */ - if ((res = mp_reduce_2k(&u, &n, 1)) != MP_OKAY) { + if ((res = mp_reduce_2k(&u, &n, 1uL)) != MP_OKAY) { goto LBL_MU; } } diff --git a/etc/mont.c b/etc/mont.c index 368b1ca..97b36e1 100644 --- a/etc/mont.c +++ b/etc/mont.c @@ -18,7 +18,7 @@ int main(void) /* make up the odd modulus */ mp_rand(&modulus, x); - modulus.dp[0] |= 1; + modulus.dp[0] |= 1uL; /* now find the R value */ mp_montgomery_calc_normalization(&R, &modulus); diff --git a/etc/pprime.c b/etc/pprime.c index 50ee219..213f3c5 100644 --- a/etc/pprime.c +++ b/etc/pprime.c @@ -18,7 +18,7 @@ static mp_digit i_sqrt(mp_word x) x2 = x; do { x1 = x2; - x2 = x1 - ((x1 * x1) - x) / (2 * x1); + x2 = x1 - ((x1 * x1) - x) / (2u * x1); } while (x1 != x2); if ((x1 * x1) > x) { @@ -40,114 +40,114 @@ static void gen_prime(void) /* 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); - r = 11; fwrite(&r, 1, sizeof(mp_digit), out); - r = 13; fwrite(&r, 1, sizeof(mp_digit), out); - r = 17; fwrite(&r, 1, sizeof(mp_digit), out); - r = 19; fwrite(&r, 1, sizeof(mp_digit), out); - 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); + r = 3uL; fwrite(&r, 1uL, sizeof(mp_digit), out); + r = 5uL; fwrite(&r, 1uL, sizeof(mp_digit), out); + r = 7uL; fwrite(&r, 1uL, sizeof(mp_digit), out); + r = 11uL; fwrite(&r, 1uL, sizeof(mp_digit), out); + r = 13uL; fwrite(&r, 1uL, sizeof(mp_digit), out); + r = 17uL; fwrite(&r, 1uL, sizeof(mp_digit), out); + r = 19uL; fwrite(&r, 1uL, sizeof(mp_digit), out); + r = 23uL; fwrite(&r, 1uL, sizeof(mp_digit), out); + r = 29uL; fwrite(&r, 1uL, sizeof(mp_digit), out); + r = 31uL; fwrite(&r, 1uL, 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); + next = (y + 1uL) * (y + 1uL); for (;;) { do { - r += 2; /* next candidate */ + r += 2uL; /* next candidate */ r &= MP_MASK; - if (r < 31) break; + if (r < 31uL) break; /* update sqrt ? */ if (next <= r) { ++y; - next = (y + 1) * (y + 1); + next = (y + 1uL) * (y + 1uL); } /* loop if divisible by 3,5,7,11,13,17,19,23,29 */ - if ((r % 3) == 0) { - x = 0; + if ((r % 3uL) == 0uL) { + x = 0uL; continue; } - if ((r % 5) == 0) { - x = 0; + if ((r % 5uL) == 0uL) { + x = 0uL; continue; } - if ((r % 7) == 0) { - x = 0; + if ((r % 7uL) == 0uL) { + x = 0uL; continue; } - if ((r % 11) == 0) { - x = 0; + if ((r % 11uL) == 0uL) { + x = 0uL; continue; } - if ((r % 13) == 0) { - x = 0; + if ((r % 13uL) == 0uL) { + x = 0uL; continue; } - if ((r % 17) == 0) { - x = 0; + if ((r % 17uL) == 0uL) { + x = 0uL; continue; } - if ((r % 19) == 0) { - x = 0; + if ((r % 19uL) == 0uL) { + x = 0uL; continue; } - if ((r % 23) == 0) { - x = 0; + if ((r % 23uL) == 0uL) { + x = 0uL; continue; } - if ((r % 29) == 0) { - x = 0; + if ((r % 29uL) == 0uL) { + x = 0uL; 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; + for (x = 30uL; x <= y; x += 30uL) { + if ((r % (x + 1uL)) == 0uL) { + x = 0uL; break; } - if ((r % (x + 7)) == 0) { - x = 0; + if ((r % (x + 7uL)) == 0uL) { + x = 0uL; break; } - if ((r % (x + 11)) == 0) { - x = 0; + if ((r % (x + 11uL)) == 0uL) { + x = 0uL; break; } - if ((r % (x + 13)) == 0) { - x = 0; + if ((r % (x + 13uL)) == 0uL) { + x = 0uL; break; } - if ((r % (x + 17)) == 0) { - x = 0; + if ((r % (x + 17uL)) == 0uL) { + x = 0uL; break; } - if ((r % (x + 19)) == 0) { - x = 0; + if ((r % (x + 19uL)) == 0uL) { + x = 0uL; break; } - if ((r % (x + 23)) == 0) { - x = 0; + if ((r % (x + 23uL)) == 0uL) { + x = 0uL; break; } - if ((r % (x + 29)) == 0) { - x = 0; + if ((r % (x + 29uL)) == 0uL) { + x = 0uL; break; } } - } while (x == 0); - if (r > 31) { - fwrite(&r, 1, sizeof(mp_digit), out); + } while (x == 0uL); + if (r > 31uL) { + fwrite(&r, 1uL, sizeof(mp_digit), out); printf("%9u\r", r); fflush(stdout); } - if (r < 31) break; + if (r < 31uL) break; } fclose(out); @@ -161,7 +161,7 @@ static void load_tab(void) gen_prime(); primes = fopen("pprime.dat", "rb"); } - fseek(primes, 0, SEEK_END); + fseek(primes, 0L, SEEK_END); n_prime = ftell(primes) / sizeof(mp_digit); } @@ -172,7 +172,7 @@ static mp_digit prime_digit(void) n = abs(rand()) % n_prime; fseek(primes, n * sizeof(mp_digit), SEEK_SET); - fread(&d, 1, sizeof(mp_digit), primes); + fread(&d, 1uL, sizeof(mp_digit), primes); return d; } @@ -254,7 +254,7 @@ top: } /* n = z + 1 */ - if ((res = mp_add_d(&z, 1, &n)) != MP_OKAY) { /* n = z + 1 */ + if ((res = mp_add_d(&z, 1uL, &n)) != MP_OKAY) { /* n = z + 1 */ goto LBL_Z; } @@ -263,7 +263,7 @@ top: goto LBL_Z; } - if (mp_cmp_d(&y, 1) != MP_EQ) + if (mp_cmp_d(&y, 1uL) != MP_EQ) goto top; /* now try base x=bases[ii] */ @@ -276,7 +276,7 @@ top: } /* if y == 1 loop */ - if (mp_cmp_d(&y, 1) == MP_EQ) + if (mp_cmp_d(&y, 1uL) == MP_EQ) continue; /* now x^2a mod n */ @@ -284,7 +284,7 @@ top: goto LBL_Z; } - if (mp_cmp_d(&y, 1) == MP_EQ) + if (mp_cmp_d(&y, 1uL) == MP_EQ) continue; /* compute x^b mod n */ @@ -293,7 +293,7 @@ top: } /* if y == 1 loop */ - if (mp_cmp_d(&y, 1) == MP_EQ) + if (mp_cmp_d(&y, 1uL) == MP_EQ) continue; /* now x^2b mod n */ @@ -301,7 +301,7 @@ top: goto LBL_Z; } - if (mp_cmp_d(&y, 1) == MP_EQ) + if (mp_cmp_d(&y, 1uL) == MP_EQ) continue; /* compute x^c mod n == x^ab mod n */ @@ -310,7 +310,7 @@ top: } /* if y == 1 loop */ - if (mp_cmp_d(&y, 1) == MP_EQ) + if (mp_cmp_d(&y, 1uL) == MP_EQ) continue; /* now compute (x^c mod n)^2 */ @@ -319,7 +319,7 @@ top: } /* y should be 1 */ - if (mp_cmp_d(&y, 1) != MP_EQ) + if (mp_cmp_d(&y, 1uL) != MP_EQ) continue; break; } @@ -345,7 +345,7 @@ top: } /* get q to be the order of the large prime subgroup */ - mp_sub_d(&n, 1, q); + mp_sub_d(&n, 1uL, q); mp_div_2(q, q); mp_div(q, &b, q, NULL); From 802d8294db1654e869e2283fb6e9c24fd0ef55b8 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Mon, 5 Feb 2018 20:22:17 +0100 Subject: [PATCH 5/5] fix type & cast --- demo/demo.c | 2 +- demo/timing.c | 6 +++--- etc/2kprime.c | 5 +++-- etc/mersenne.c | 2 +- etc/mont.c | 4 ++-- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/demo/demo.c b/demo/demo.c index 357d26e..4f32072 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -264,7 +264,7 @@ 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) & 0xFFFFFFFFuL; + t = (unsigned long)(rand() * rand() + 1) & 0xFFFFFFFFuL; mp_set_int(&a, t); if (t != mp_get_int(&a)) { printf("\nmp_get_int() bad result!"); diff --git a/demo/timing.c b/demo/timing.c index 2b4f30f..d22e471 100644 --- a/demo/timing.c +++ b/demo/timing.c @@ -34,14 +34,14 @@ static void draw(mp_int *a) static unsigned long lfsr = 0xAAAAAAAAuL; -static int lbit(void) +static unsigned int lbit(void) { if ((lfsr & 0x80000000uL) != 0uL) { lfsr = ((lfsr << 1) ^ 0x8000001BuL) & 0xFFFFFFFFuL; - return 1; + return 1u; } else { lfsr <<= 1; - return 0; + return 0u; } } diff --git a/etc/2kprime.c b/etc/2kprime.c index 803ece7..a43e1b4 100644 --- a/etc/2kprime.c +++ b/etc/2kprime.c @@ -7,7 +7,8 @@ static int sizes[] = {256, 512, 768, 1024, 1536, 2048, 3072, 4096}; int main(void) { char buf[2000]; - int x, y; + size_t x; + int y; mp_int q, p; FILE *out; clock_t t1; @@ -17,7 +18,7 @@ int main(void) out = fopen("2kprime.1", "w"); if (out != NULL) { - for (x = 0; x < (int)(sizeof(sizes) / sizeof(sizes[0])); x++) { + for (x = 0; x < (sizeof(sizes) / sizeof(sizes[0])); x++) { top: mp_2expt(&q, sizes[x]); mp_add_d(&q, 3uL, &q); diff --git a/etc/mersenne.c b/etc/mersenne.c index 6e4759b..23420fd 100644 --- a/etc/mersenne.c +++ b/etc/mersenne.c @@ -21,7 +21,7 @@ static int is_mersenne(long s, int *pp) } /* n = 2^s - 1 */ - if ((res = mp_2expt(&n, s)) != MP_OKAY) { + if ((res = mp_2expt(&n, (int)s)) != MP_OKAY) { goto LBL_MU; } if ((res = mp_sub_d(&n, 1uL, &n)) != MP_OKAY) { diff --git a/etc/mont.c b/etc/mont.c index 97b36e1..eb1a714 100644 --- a/etc/mont.c +++ b/etc/mont.c @@ -6,14 +6,14 @@ int main(void) { mp_int modulus, R, p, pp; mp_digit mp; - long x, y; + int x, y; srand(time(NULL)); mp_init_multi(&modulus, &R, &p, &pp, NULL); /* loop through various sizes */ for (x = 4; x < 256; x++) { - printf("DIGITS == %3ld...", x); + printf("DIGITS == %3d...", x); fflush(stdout); /* make up the odd modulus */