From 82cd2c7f37189e1328a81b936b637c78de73b3e1 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 23 Sep 2018 09:39:53 +0200 Subject: [PATCH 1/4] use NULL instead of 0 --- bn_mp_tc_and.c | 2 +- bn_mp_tc_div_2d.c | 4 ++-- bn_mp_tc_or.c | 2 +- bn_mp_tc_xor.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bn_mp_tc_and.c b/bn_mp_tc_and.c index 0e28ca8..800681a 100644 --- a/bn_mp_tc_and.c +++ b/bn_mp_tc_and.c @@ -18,7 +18,7 @@ int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) { int res = MP_OKAY, bits; int as = mp_isneg(a), bs = mp_isneg(b), s = 0; - mp_int *mx = 0, _mx, acpy, bcpy; + mp_int *mx = NULL, _mx, acpy, bcpy; if (as || bs) { bits = MAX(mp_count_bits(a), mp_count_bits(b)); diff --git a/bn_mp_tc_div_2d.c b/bn_mp_tc_div_2d.c index 5ea4e38..f341c9b 100644 --- a/bn_mp_tc_div_2d.c +++ b/bn_mp_tc_div_2d.c @@ -18,7 +18,7 @@ int mp_tc_div_2d(const mp_int *a, int b, mp_int *c) { int res; if (!mp_isneg(a)) { - return mp_div_2d(a, b, c, 0); + return mp_div_2d(a, b, c, NULL); } res = mp_add_d(a, 1, c); @@ -26,7 +26,7 @@ int mp_tc_div_2d(const mp_int *a, int b, mp_int *c) return res; } - res = mp_div_2d(c, b, c, 0); + res = mp_div_2d(c, b, c, NULL); return res == MP_OKAY ? mp_sub_d(c, 1, c) : res; } #endif diff --git a/bn_mp_tc_or.c b/bn_mp_tc_or.c index 1e7d583..344c20c 100644 --- a/bn_mp_tc_or.c +++ b/bn_mp_tc_or.c @@ -18,7 +18,7 @@ int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) { int res = MP_OKAY, bits; int as = mp_isneg(a), bs = mp_isneg(b), s = 0; - mp_int *mx = 0, _mx, acpy, bcpy; + mp_int *mx = NULL, _mx, acpy, bcpy; if (as || bs) { bits = MAX(mp_count_bits(a), mp_count_bits(b)); diff --git a/bn_mp_tc_xor.c b/bn_mp_tc_xor.c index be9b9d8..09547a5 100644 --- a/bn_mp_tc_xor.c +++ b/bn_mp_tc_xor.c @@ -18,7 +18,7 @@ int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) { int res = MP_OKAY, bits; int as = mp_isneg(a), bs = mp_isneg(b), s = 0; - mp_int *mx = 0, _mx, acpy, bcpy; + mp_int *mx = NULL, _mx, acpy, bcpy; if (as || bs) { bits = MAX(mp_count_bits(a), mp_count_bits(b)); From 519a1de8da916f78c4ca18528795a994d22e18c1 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 23 Sep 2018 09:46:31 +0200 Subject: [PATCH 2/4] literal with suffix --- bn_mp_complement.c | 2 +- bn_mp_tc_and.c | 2 +- bn_mp_tc_div_2d.c | 4 ++-- bn_mp_tc_or.c | 2 +- bn_mp_tc_xor.c | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bn_mp_complement.c b/bn_mp_complement.c index 256ddea..fae3a59 100644 --- a/bn_mp_complement.c +++ b/bn_mp_complement.c @@ -17,7 +17,7 @@ int mp_complement(const mp_int *a, mp_int *b) { int res = mp_neg(a, b); - return res == MP_OKAY ? mp_sub_d(b, 1, b) : res; + return res == MP_OKAY ? mp_sub_d(b, 1uL, b) : res; } #endif diff --git a/bn_mp_tc_and.c b/bn_mp_tc_and.c index 800681a..2969116 100644 --- a/bn_mp_tc_and.c +++ b/bn_mp_tc_and.c @@ -22,7 +22,7 @@ int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) if (as || bs) { bits = MAX(mp_count_bits(a), mp_count_bits(b)); - res = mp_init_set_int(&_mx, 1); + res = mp_init_set_int(&_mx, 1uL); if (res != MP_OKAY) { goto end; } diff --git a/bn_mp_tc_div_2d.c b/bn_mp_tc_div_2d.c index f341c9b..88aa2f8 100644 --- a/bn_mp_tc_div_2d.c +++ b/bn_mp_tc_div_2d.c @@ -21,13 +21,13 @@ int mp_tc_div_2d(const mp_int *a, int b, mp_int *c) return mp_div_2d(a, b, c, NULL); } - res = mp_add_d(a, 1, c); + res = mp_add_d(a, 1uL, c); if (res != MP_OKAY) { return res; } res = mp_div_2d(c, b, c, NULL); - return res == MP_OKAY ? mp_sub_d(c, 1, c) : res; + return res == MP_OKAY ? mp_sub_d(c, 1uL, c) : res; } #endif diff --git a/bn_mp_tc_or.c b/bn_mp_tc_or.c index 344c20c..d3f1100 100644 --- a/bn_mp_tc_or.c +++ b/bn_mp_tc_or.c @@ -22,7 +22,7 @@ int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) if (as || bs) { bits = MAX(mp_count_bits(a), mp_count_bits(b)); - res = mp_init_set_int(&_mx, 1); + 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 09547a5..e1de859 100644 --- a/bn_mp_tc_xor.c +++ b/bn_mp_tc_xor.c @@ -22,7 +22,7 @@ int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) if (as || bs) { bits = MAX(mp_count_bits(a), mp_count_bits(b)); - res = mp_init_set_int(&_mx, 1); + res = mp_init_set_int(&_mx, 1uL); if (res != MP_OKAY) { goto end; } From 482cf1e12828d273842dfd2b24a6f6c02b0b54ab Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 23 Sep 2018 10:26:47 +0200 Subject: [PATCH 3/4] explicit condition --- bn_mp_complement.c | 2 +- bn_mp_tc_and.c | 6 +++--- bn_mp_tc_div_2d.c | 4 ++-- bn_mp_tc_or.c | 6 +++--- bn_mp_tc_xor.c | 6 +++--- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/bn_mp_complement.c b/bn_mp_complement.c index fae3a59..9dfddc3 100644 --- a/bn_mp_complement.c +++ b/bn_mp_complement.c @@ -17,7 +17,7 @@ int mp_complement(const mp_int *a, mp_int *b) { int res = mp_neg(a, b); - return res == MP_OKAY ? mp_sub_d(b, 1uL, b) : res; + return (res == MP_OKAY) ? mp_sub_d(b, 1uL, b) : res; } #endif diff --git a/bn_mp_tc_and.c b/bn_mp_tc_and.c index 2969116..02e43ac 100644 --- a/bn_mp_tc_and.c +++ b/bn_mp_tc_and.c @@ -20,7 +20,7 @@ int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) int as = mp_isneg(a), bs = mp_isneg(b), s = 0; mp_int *mx = NULL, _mx, acpy, bcpy; - if (as || bs) { + if ((as != MP_NO) || (bs != MP_NO)) { bits = MAX(mp_count_bits(a), mp_count_bits(b)); res = mp_init_set_int(&_mx, 1uL); if (res != MP_OKAY) { @@ -33,7 +33,7 @@ int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) goto end; } - if (as) { + if (as != MP_NO) { res = mp_init(&acpy); if (res != MP_OKAY) { goto end; @@ -46,7 +46,7 @@ int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) } a = &acpy; } - if (bs) { + if (bs != MP_NO) { res = mp_init(&bcpy); if (res != MP_OKAY) { goto end; diff --git a/bn_mp_tc_div_2d.c b/bn_mp_tc_div_2d.c index 88aa2f8..3c617ef 100644 --- a/bn_mp_tc_div_2d.c +++ b/bn_mp_tc_div_2d.c @@ -17,7 +17,7 @@ int mp_tc_div_2d(const mp_int *a, int b, mp_int *c) { int res; - if (!mp_isneg(a)) { + if (mp_isneg(a) == MP_NO) { return mp_div_2d(a, b, c, NULL); } @@ -27,7 +27,7 @@ int mp_tc_div_2d(const mp_int *a, int b, mp_int *c) } res = mp_div_2d(c, b, c, NULL); - return res == MP_OKAY ? mp_sub_d(c, 1uL, c) : res; + return (res == MP_OKAY) ? mp_sub_d(c, 1uL, c) : res; } #endif diff --git a/bn_mp_tc_or.c b/bn_mp_tc_or.c index d3f1100..549a328 100644 --- a/bn_mp_tc_or.c +++ b/bn_mp_tc_or.c @@ -20,7 +20,7 @@ int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) int as = mp_isneg(a), bs = mp_isneg(b), s = 0; mp_int *mx = NULL, _mx, acpy, bcpy; - if (as || bs) { + if ((as != MP_NO) || (bs != MP_NO)) { bits = MAX(mp_count_bits(a), mp_count_bits(b)); res = mp_init_set_int(&_mx, 1uL); if (res != MP_OKAY) { @@ -33,7 +33,7 @@ int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) goto end; } - if (as) { + if (as != MP_NO) { res = mp_init(&acpy); if (res != MP_OKAY) { goto end; @@ -46,7 +46,7 @@ int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) } a = &acpy; } - if (bs) { + if (bs != MP_NO) { res = mp_init(&bcpy); if (res != MP_OKAY) { goto end; diff --git a/bn_mp_tc_xor.c b/bn_mp_tc_xor.c index e1de859..771cfd5 100644 --- a/bn_mp_tc_xor.c +++ b/bn_mp_tc_xor.c @@ -20,7 +20,7 @@ int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) int as = mp_isneg(a), bs = mp_isneg(b), s = 0; mp_int *mx = NULL, _mx, acpy, bcpy; - if (as || bs) { + if ((as != MP_NO) || (bs != MP_NO)) { bits = MAX(mp_count_bits(a), mp_count_bits(b)); res = mp_init_set_int(&_mx, 1uL); if (res != MP_OKAY) { @@ -33,7 +33,7 @@ int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) goto end; } - if (as) { + if (as != MP_NO) { res = mp_init(&acpy); if (res != MP_OKAY) { goto end; @@ -46,7 +46,7 @@ int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) } a = &acpy; } - if (bs) { + if (bs != MP_NO) { res = mp_init(&bcpy); if (res != MP_OKAY) { goto end; From 434dbcab27816a6096db70fdeb7fa4b337e2b774 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 23 Sep 2018 10:16:25 +0200 Subject: [PATCH 4/4] refactor without bitwise operator --- bn_mp_tc_and.c | 5 ++--- bn_mp_tc_or.c | 5 ++--- bn_mp_tc_xor.c | 5 ++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/bn_mp_tc_and.c b/bn_mp_tc_and.c index 02e43ac..e9fe4c6 100644 --- a/bn_mp_tc_and.c +++ b/bn_mp_tc_and.c @@ -17,7 +17,7 @@ int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) { int res = MP_OKAY, bits; - int as = mp_isneg(a), bs = mp_isneg(b), s = 0; + int as = mp_isneg(a), bs = mp_isneg(b); mp_int *mx = NULL, _mx, acpy, bcpy; if ((as != MP_NO) || (bs != MP_NO)) { @@ -62,9 +62,8 @@ int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) } res = mp_and(a, b, c); - s = as & bs; - if (s && res == MP_OKAY) { + if ((as != MP_NO) && (bs != MP_NO) && (res == MP_OKAY)) { res = mp_sub(c, mx, c); } diff --git a/bn_mp_tc_or.c b/bn_mp_tc_or.c index 549a328..91b6b40 100644 --- a/bn_mp_tc_or.c +++ b/bn_mp_tc_or.c @@ -17,7 +17,7 @@ int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) { int res = MP_OKAY, bits; - int as = mp_isneg(a), bs = mp_isneg(b), s = 0; + int as = mp_isneg(a), bs = mp_isneg(b); mp_int *mx = NULL, _mx, acpy, bcpy; if ((as != MP_NO) || (bs != MP_NO)) { @@ -62,9 +62,8 @@ int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) } res = mp_or(a, b, c); - s = as | bs; - if (s && res == MP_OKAY) { + if (((as != MP_NO) || (bs != MP_NO)) && (res == MP_OKAY)) { res = mp_sub(c, mx, c); } diff --git a/bn_mp_tc_xor.c b/bn_mp_tc_xor.c index 771cfd5..50fb12d 100644 --- a/bn_mp_tc_xor.c +++ b/bn_mp_tc_xor.c @@ -17,7 +17,7 @@ int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) { int res = MP_OKAY, bits; - int as = mp_isneg(a), bs = mp_isneg(b), s = 0; + int as = mp_isneg(a), bs = mp_isneg(b); mp_int *mx = NULL, _mx, acpy, bcpy; if ((as != MP_NO) || (bs != MP_NO)) { @@ -62,9 +62,8 @@ int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) } res = mp_xor(a, b, c); - s = as ^ bs; - if (s && res == MP_OKAY) { + if ((as != bs) && (res == MP_OKAY)) { res = mp_sub(c, mx, c); }