From 4fec1ae6f2a61d8c0003e053d883b602bf159ca4 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Thu, 27 Dec 2018 08:50:34 +0100 Subject: [PATCH 01/10] remove side effect inside parameter of macro MAX --- bn_mp_tc_and.c | 6 ++++-- bn_mp_tc_or.c | 6 ++++-- bn_mp_tc_xor.c | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/bn_mp_tc_and.c b/bn_mp_tc_and.c index e9fe4c6..d1f1b91 100644 --- a/bn_mp_tc_and.c +++ b/bn_mp_tc_and.c @@ -16,12 +16,14 @@ /* two complement and */ int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) { - int res = MP_OKAY, bits; + int res = MP_OKAY, bits, abits, bbits; int as = mp_isneg(a), bs = mp_isneg(b); mp_int *mx = NULL, _mx, acpy, bcpy; if ((as != MP_NO) || (bs != MP_NO)) { - bits = MAX(mp_count_bits(a), mp_count_bits(b)); + abits = mp_count_bits(a); + bbits = mp_count_bits(b); + bits = MAX(abits, bbits); res = mp_init_set_int(&_mx, 1uL); if (res != MP_OKAY) { goto end; diff --git a/bn_mp_tc_or.c b/bn_mp_tc_or.c index 91b6b40..f177c39 100644 --- a/bn_mp_tc_or.c +++ b/bn_mp_tc_or.c @@ -16,12 +16,14 @@ /* two complement or */ int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) { - int res = MP_OKAY, bits; + int res = MP_OKAY, bits, abits, bbits; int as = mp_isneg(a), bs = mp_isneg(b); mp_int *mx = NULL, _mx, acpy, bcpy; if ((as != MP_NO) || (bs != MP_NO)) { - bits = MAX(mp_count_bits(a), mp_count_bits(b)); + abits = mp_count_bits(a); + bbits = mp_count_bits(b); + bits = MAX(abits, bbits); res = mp_init_set_int(&_mx, 1uL); if (res != MP_OKAY) { goto end; diff --git a/bn_mp_tc_xor.c b/bn_mp_tc_xor.c index 50fb12d..a2c67a2 100644 --- a/bn_mp_tc_xor.c +++ b/bn_mp_tc_xor.c @@ -16,12 +16,14 @@ /* two complement xor */ int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) { - int res = MP_OKAY, bits; + int res = MP_OKAY, bits, abits, bbits; int as = mp_isneg(a), bs = mp_isneg(b); mp_int *mx = NULL, _mx, acpy, bcpy; if ((as != MP_NO) || (bs != MP_NO)) { - bits = MAX(mp_count_bits(a), mp_count_bits(b)); + abits = mp_count_bits(a); + bbits = mp_count_bits(b); + bits = MAX(abits, bbits); res = mp_init_set_int(&_mx, 1uL); if (res != MP_OKAY) { goto end; From e519d5ef418ea5f18ccacfee31decdedd6331609 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Thu, 27 Dec 2018 08:56:32 +0100 Subject: [PATCH 02/10] fix format --- etc/pprime.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/etc/pprime.c b/etc/pprime.c index 213f3c5..65a6792 100644 --- a/etc/pprime.c +++ b/etc/pprime.c @@ -144,7 +144,7 @@ static void gen_prime(void) } while (x == 0uL); if (r > 31uL) { fwrite(&r, 1uL, sizeof(mp_digit), out); - printf("%9u\r", r); + printf("%9lu\r", r); fflush(stdout); } if (r < 31uL) break; @@ -336,7 +336,7 @@ top: mp_toradix(&a, buf, 10); printf("A == \n%s\n\n", buf); mp_toradix(&b, buf, 10); - printf("B == \n%s\n\nG == %d\n", buf, bases[ii]); + printf("B == \n%s\n\nG == %lu\n", buf, bases[ii]); printf("----------------------------------------------------------------\n"); } From 4f902d9f105e060eefab5057bcecb7b7dab23e23 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Thu, 27 Dec 2018 09:02:31 +0100 Subject: [PATCH 03/10] remove useless assignment of cnt --- demo/demo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/demo.c b/demo/demo.c index b0ac10c..a8ef15f 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -933,7 +933,7 @@ printf("compare no compare!\n"); return EXIT_FAILURE; #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 = add_d_n = sub_d_n = 0; /* force KARA and TOOM to enable despite cutoffs */ KARATSUBA_SQR_CUTOFF = KARATSUBA_MUL_CUTOFF = 8; From efda3c0e10dd42beebf6ac78d9f2b793fa7f5372 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Thu, 27 Dec 2018 09:10:09 +0100 Subject: [PATCH 04/10] declare cnt only when useful --- demo/demo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demo/demo.c b/demo/demo.c index a8ef15f..71a9582 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -156,7 +156,7 @@ static char buf[4096]; int main(void) { unsigned rr; - int cnt, ix; + int ix; #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; @@ -165,7 +165,7 @@ int main(void) long k, m; unsigned long long q, r; mp_digit mp; - int i, n, err, should; + int i, n, err, should, cnt; #endif if (mp_init_multi(&a, &b, &c, &d, &e, &f, NULL)!= MP_OKAY) From ea44272d4173894d3ba938973338fe510feadd39 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Thu, 27 Dec 2018 09:12:22 +0100 Subject: [PATCH 05/10] always use #if LTM_DEMO_TEST_VS_MTEST != 0 --- demo/demo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demo/demo.c b/demo/demo.c index 71a9582..1771073 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -36,7 +36,7 @@ static void ndraw(mp_int *a, const char *name) printf("0x%s\n", buf); } -#if LTM_DEMO_TEST_VS_MTEST +#if LTM_DEMO_TEST_VS_MTEST != 0 static void draw(mp_int *a) { ndraw(a, ""); @@ -157,7 +157,7 @@ int main(void) { unsigned rr; int ix; -#if LTM_DEMO_TEST_VS_MTEST +#if LTM_DEMO_TEST_VS_MTEST != 0 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; #else From b6fa97c591f40937d2f08836605de4b11dee081b Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Thu, 27 Dec 2018 09:19:57 +0100 Subject: [PATCH 06/10] explicit operator precedence --- demo/demo.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/demo/demo.c b/demo/demo.c index 1771073..2bb2ed3 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -284,7 +284,7 @@ int main(void) printf("Failed executing mp_jacobi(%d | %lu) %s.\n", n, jacobi[cnt].n, mp_error_to_string(err)); return EXIT_FAILURE; } - if (err == MP_OKAY && i != jacobi[cnt].c[n + 5]) { + if ((err == MP_OKAY) && (i != jacobi[cnt].c[n + 5])) { printf("Failed trivial mp_jacobi(%d | %lu) %d != %d\n", n, jacobi[cnt].n, i, jacobi[cnt].c[n + 5]); return EXIT_FAILURE; } @@ -322,7 +322,7 @@ int main(void) printf("Failed executing mp_kronecker(%ld | %ld) %s.\n", kronecker[cnt].n, m, mp_error_to_string(err)); return EXIT_FAILURE; } - if (err == MP_OKAY && i != kronecker[cnt].c[m + 10]) { + if ((err == MP_OKAY) && (i != kronecker[cnt].c[m + 10])) { printf("Failed trivial mp_kronecker(%ld | %ld) %d != %d\n", kronecker[cnt].n, m, i, kronecker[cnt].c[m + 10]); return EXIT_FAILURE; } @@ -544,7 +544,7 @@ int main(void) } printf("\n\nTesting: mp_get_long\n"); - for (i = 0; i < (int)(sizeof(unsigned long)*CHAR_BIT) - 1; ++i) { + for (i = 0; i < ((int)(sizeof(unsigned long)*CHAR_BIT) - 1); ++i) { t = (1ULL << (i+1)) - 1; if (!t) t = -1; @@ -564,7 +564,7 @@ int main(void) } printf("\n\nTesting: mp_get_long_long\n"); - for (i = 0; i < (int)(sizeof(unsigned long long)*CHAR_BIT) - 1; ++i) { + for (i = 0; i < ((int)(sizeof(unsigned long long)*CHAR_BIT) - 1); ++i) { r = (1ULL << (i+1)) - 1; if (!r) r = -1; From c63ce5bf1f17e17c4525161efe85c212358be8e1 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Thu, 27 Dec 2018 09:21:12 +0100 Subject: [PATCH 07/10] fix indentation --- demo/demo.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/demo/demo.c b/demo/demo.c index 2bb2ed3..2ea5aa0 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -745,12 +745,12 @@ int main(void) 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("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; /* *INDENT-ON* */ } /* only one big montgomery reduction */ From 2bde5bb5d53299fd5daf546ed3b58dbdc1c8241d Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Thu, 27 Dec 2018 09:34:11 +0100 Subject: [PATCH 08/10] semicolon --- demo/demo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demo/demo.c b/demo/demo.c index 2ea5aa0..15975dd 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -868,8 +868,8 @@ int main(void) mp_copy(&b, &c); mp_mod(&b, &a, &b); - mp_dr_setup(&a, &mp), - mp_dr_reduce(&c, &a, mp); + mp_dr_setup(&a, &mp); + mp_dr_reduce(&c, &a, mp); if (mp_cmp(&b, &c) != MP_EQ) { printf("Failed on trial %u\n", rr); From f1f6762616bb634cee170c381701132968caa218 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Thu, 27 Dec 2018 09:24:25 +0100 Subject: [PATCH 09/10] conditional definition of FGETS --- demo/demo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/demo.c b/demo/demo.c index 15975dd..2e630ad 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -77,13 +77,13 @@ static void _panic(int l) fprintf(stderr, "\n%d: fgets failed\n", l); exit(EXIT_FAILURE); } -#endif #define FGETS(str, size, stream) \ { \ char *ret = fgets(str, size, stream); \ if (!ret) { _panic(__LINE__); } \ } +#endif static mp_int a, b, c, d, e, f; From ab074176abe93582681deffaf93027c7b47c3c9f Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Thu, 27 Dec 2018 09:38:26 +0100 Subject: [PATCH 10/10] explicit condition --- demo/demo.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/demo/demo.c b/demo/demo.c index 2e630ad..a93ce1d 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -560,7 +560,7 @@ int main(void) return EXIT_FAILURE; } t <<= 1; - } while (t); + } while (t != 0uL); } printf("\n\nTesting: mp_get_long_long\n"); @@ -580,7 +580,7 @@ int main(void) return EXIT_FAILURE; } r <<= 1; - } while (r); + } while (r != 0uLL); } /* test mp_sqrt */ @@ -815,7 +815,7 @@ int main(void) mp_copy(&c, &b); mp_mod(&c, &a, &c); mp_reduce_2k(&b, &a, 2uL); - if (mp_cmp(&c, &b)) { + if (mp_cmp(&c, &b) != MP_EQ) { printf("FAILED\n"); return EXIT_FAILURE; }