From 3fba991e03846b767da131cc58466e8e7e446d8b Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Tue, 29 Oct 2019 18:41:25 +0100 Subject: [PATCH] first batch of simplifications --- demo/mtest_opponent.c | 8 +++--- demo/test.c | 6 ++--- doc/bn.tex | 8 +++--- mp_abs.c | 3 +-- mp_add.c | 37 ++++++++++---------------- mp_clear_multi.c | 7 +++-- mp_cmp.c | 15 ++++------- mp_cmp_d.c | 10 +++---- mp_cmp_mag.c | 28 +++++--------------- mp_cnt_lsb.c | 11 ++++---- mp_copy.c | 14 +++------- mp_div.c | 13 ++++----- mp_div_3.c | 7 ++--- mp_exch.c | 6 +---- mp_exptmod.c | 12 +++++---- mp_exteuclid.c | 1 - mp_fread.c | 6 ++--- mp_from_sbin.c | 8 ++---- mp_from_ubin.c | 4 +-- mp_fwrite.c | 23 ++++++---------- mp_grow.c | 16 +++++------- mp_is_square.c | 8 +++--- mp_kronecker.c | 2 +- mp_lshd.c | 13 +++------ mp_mod_2d.c | 8 +++--- mp_montgomery_calc_normalization.c | 1 - mp_neg.c | 8 ++---- mp_pack.c | 12 ++++----- mp_prime_rabin_miller_trials.c | 3 ++- mp_prime_rand.c | 10 +++---- mp_read_radix.c | 8 +++--- mp_reduce.c | 22 ++++++++-------- mp_reduce_is_2k.c | 8 ++---- mp_reduce_is_2k_l.c | 3 +-- mp_rshd.c | 18 ++++--------- mp_set.c | 3 ++- mp_shrink.c | 10 +++---- mp_signed_rsh.c | 11 ++++---- mp_sqrt.c | 18 ++++++------- mp_sub.c | 42 ++++++++++++++---------------- mp_to_radix.c | 14 +++------- mp_to_sbin.c | 4 +-- mp_to_ubin.c | 4 +-- mp_unpack.c | 10 +++---- mp_zero.c | 2 +- s_mp_div_recursive.c | 6 +---- s_mp_div_school.c | 2 -- tommath.h | 8 +++--- tommath_private.h | 6 +++-- 49 files changed, 208 insertions(+), 299 deletions(-) diff --git a/demo/mtest_opponent.c b/demo/mtest_opponent.c index 6c49de23a..1beab5fce 100644 --- a/demo/mtest_opponent.c +++ b/demo/mtest_opponent.c @@ -139,9 +139,9 @@ static int mtest_opponent(void) /* test the sign/unsigned storage functions */ rr = (unsigned)mp_sbin_size(&c); - DO(mp_to_sbin(&c, (unsigned char *) cmd, (size_t)rr, NULL)); + DO(mp_to_sbin(&c, (uint8_t *) cmd, (size_t)rr, NULL)); memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr); - DO(mp_from_sbin(&d, (unsigned char *) cmd, (size_t)rr)); + DO(mp_from_sbin(&d, (uint8_t *) cmd, (size_t)rr)); if (mp_cmp(&c, &d) != MP_EQ) { printf("mp_signed_bin failure!\n"); draw(&c); @@ -150,9 +150,9 @@ static int mtest_opponent(void) } rr = (unsigned)mp_ubin_size(&c); - DO(mp_to_ubin(&c, (unsigned char *) cmd, (size_t)rr, NULL)); + DO(mp_to_ubin(&c, (uint8_t *) cmd, (size_t)rr, NULL)); memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr); - DO(mp_from_ubin(&d, (unsigned char *) cmd, (size_t)rr)); + DO(mp_from_ubin(&d, (uint8_t *) cmd, (size_t)rr)); if (mp_cmp_mag(&c, &d) != MP_EQ) { printf("mp_unsigned_bin failure!\n"); draw(&c); diff --git a/demo/test.c b/demo/test.c index 7a4233010..8e37f712a 100644 --- a/demo/test.c +++ b/demo/test.c @@ -2169,7 +2169,7 @@ static int test_mp_read_write_ubin(void) { mp_int a, b, c; size_t size, len; - unsigned char *buf = NULL; + uint8_t *buf = NULL; DOR(mp_init_multi(&a, &b, &c, NULL)); @@ -2207,7 +2207,7 @@ static int test_mp_read_write_sbin(void) { mp_int a, b, c; size_t size, len; - unsigned char *buf = NULL; + uint8_t *buf = NULL; DOR(mp_init_multi(&a, &b, &c, NULL)); @@ -2246,7 +2246,7 @@ static int test_mp_pack_unpack(void) { mp_int a, b; size_t written, count; - unsigned char *buf = NULL; + uint8_t *buf = NULL; mp_order order = MP_LSB_FIRST; mp_endian endianess = MP_NATIVE_ENDIAN; diff --git a/doc/bn.tex b/doc/bn.tex index 1e8ac7403..b8a6404b7 100644 --- a/doc/bn.tex +++ b/doc/bn.tex @@ -2444,7 +2444,7 @@ \section{Binary Conversions} \index{mp\_to\_ubin} \begin{alltt} -mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) +mp_err mp_to_ubin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written) \end{alltt} This will store $a$ into the buffer \texttt{buf} of size \texttt{maxlen} in big--endian format storing the number of bytes written in \texttt{len}. Fortunately this is exactly what DER (or is @@ -2452,7 +2452,7 @@ \section{Binary Conversions} \index{mp\_from\_ubin} \begin{alltt} -mp_err mp_from_ubin(mp_int *a, unsigned char *b, size_t size); +mp_err mp_from_ubin(mp_int *a, uint8_t *b, size_t size); \end{alltt} This will read in an unsigned big--endian array of bytes (octets) from \texttt{b} of length \texttt{size} into $a$. The resulting big--integer $a$ will always be positive. @@ -2462,8 +2462,8 @@ \section{Binary Conversions} \index{mp\_sbin\_size} \index{mp\_from\_sbin} \index{mp\_to\_sbin} \begin{alltt} size_t mp_sbin_size(const mp_int *a); -mp_err mp_from_sbin(mp_int *a, const unsigned char *b, size_t size); -mp_err mp_to_sbin(const mp_int *a, unsigned char *b, size_t maxsize, size_t *len); +mp_err mp_from_sbin(mp_int *a, const uint8_t *b, size_t size); +mp_err mp_to_sbin(const mp_int *a, uint8_t *b, size_t maxsize, size_t *len); \end{alltt} They operate essentially the same as the unsigned copies except they prefix the data with zero or non--zero byte depending on the sign. If the sign is \texttt{MP\_ZPOS} (e.g. not negative) the diff --git a/mp_abs.c b/mp_abs.c index 4ad1a4a90..902279e93 100644 --- a/mp_abs.c +++ b/mp_abs.c @@ -9,10 +9,9 @@ */ mp_err mp_abs(const mp_int *a, mp_int *b) { - mp_err err; - /* copy a to b */ if (a != b) { + mp_err err; if ((err = mp_copy(a, b)) != MP_OKAY) { return err; } diff --git a/mp_add.c b/mp_add.c index c78614b6f..bf7a61e25 100644 --- a/mp_add.c +++ b/mp_add.c @@ -6,33 +6,24 @@ /* high level addition (handles signs) */ mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c) { - mp_sign sa, sb; - mp_err err; - - /* get sign of both inputs */ - sa = a->sign; - sb = b->sign; - /* handle two cases, not four */ - if (sa == sb) { + if (a->sign == b->sign) { /* both positive or both negative */ /* add their magnitudes, copy the sign */ - c->sign = sa; - err = s_mp_add(a, b, c); - } else { - /* one positive, the other negative */ - /* subtract the one with the greater magnitude from */ - /* the one of the lesser magnitude. The result gets */ - /* the sign of the one with the greater magnitude. */ - if (mp_cmp_mag(a, b) == MP_LT) { - c->sign = sb; - err = s_mp_sub(b, a, c); - } else { - c->sign = sa; - err = s_mp_sub(a, b, c); - } + c->sign = a->sign; + return s_mp_add(a, b, c); } - return err; + + /* one positive, the other negative */ + /* subtract the one with the greater magnitude from */ + /* the one of the lesser magnitude. The result gets */ + /* the sign of the one with the greater magnitude. */ + if (mp_cmp_mag(a, b) == MP_LT) { + MP_EXCH(const mp_int *, a, b); + } + + c->sign = a->sign; + return s_mp_sub(a, b, c); } #endif diff --git a/mp_clear_multi.c b/mp_clear_multi.c index 74406c7a0..9c7aed831 100644 --- a/mp_clear_multi.c +++ b/mp_clear_multi.c @@ -7,12 +7,11 @@ void mp_clear_multi(mp_int *mp, ...) { - mp_int *next_mp = mp; va_list args; va_start(args, mp); - while (next_mp != NULL) { - mp_clear(next_mp); - next_mp = va_arg(args, mp_int *); + while (mp != NULL) { + mp_clear(mp); + mp = va_arg(args, mp_int *); } va_end(args); } diff --git a/mp_cmp.c b/mp_cmp.c index a9bd910a9..b9c45920b 100644 --- a/mp_cmp.c +++ b/mp_cmp.c @@ -8,19 +8,14 @@ mp_ord mp_cmp(const mp_int *a, const mp_int *b) { /* compare based on sign */ if (a->sign != b->sign) { - if (a->sign == MP_NEG) { - return MP_LT; - } else { - return MP_GT; - } + return a->sign == MP_NEG ? MP_LT : MP_GT; } - /* compare digits */ + /* if negative compare opposite direction */ if (a->sign == MP_NEG) { - /* if negative compare opposite direction */ - return mp_cmp_mag(b, a); - } else { - return mp_cmp_mag(a, b); + MP_EXCH(const mp_int *, a, b); } + + return mp_cmp_mag(a, b); } #endif diff --git a/mp_cmp_d.c b/mp_cmp_d.c index 03d8e2c6f..0d98e05a0 100644 --- a/mp_cmp_d.c +++ b/mp_cmp_d.c @@ -17,12 +17,10 @@ mp_ord mp_cmp_d(const mp_int *a, mp_digit b) } /* compare the only digit of a to b */ - if (a->dp[0] > b) { - return MP_GT; - } else if (a->dp[0] < b) { - return MP_LT; - } else { - return MP_EQ; + if (a->dp[0] != b) { + return a->dp[0] > b ? MP_GT : MP_LT; } + + return MP_EQ; } #endif diff --git a/mp_cmp_mag.c b/mp_cmp_mag.c index b3a7b040f..e5e502b8c 100644 --- a/mp_cmp_mag.c +++ b/mp_cmp_mag.c @@ -6,34 +6,20 @@ /* compare maginitude of two ints (unsigned) */ mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b) { - int n; - const mp_digit *tmpa, *tmpb; + int n; /* compare based on # of non-zero digits */ - if (a->used > b->used) { - return MP_GT; + if (a->used != b->used) { + return a->used > b->used ? MP_GT : MP_LT; } - if (a->used < b->used) { - return MP_LT; - } - - /* alias for a */ - tmpa = a->dp + (a->used - 1); - - /* alias for b */ - tmpb = b->dp + (a->used - 1); - /* compare based on digits */ - for (n = 0; n < a->used; ++n, --tmpa, --tmpb) { - if (*tmpa > *tmpb) { - return MP_GT; - } - - if (*tmpa < *tmpb) { - return MP_LT; + for (n = a->used; n --> 0;) { + if (a->dp[n] != b->dp[n]) { + return a->dp[n] > b->dp[n] ? MP_GT : MP_LT; } } + return MP_EQ; } #endif diff --git a/mp_cnt_lsb.c b/mp_cnt_lsb.c index 7ae8bc13c..8519ad1b0 100644 --- a/mp_cnt_lsb.c +++ b/mp_cnt_lsb.c @@ -3,7 +3,7 @@ /* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ -static const int lnz[16] = { +static const char lnz[16] = { 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 }; @@ -11,7 +11,7 @@ static const int lnz[16] = { int mp_cnt_lsb(const mp_int *a) { int x; - mp_digit q, qq; + mp_digit q; /* easy out */ if (mp_iszero(a)) { @@ -25,11 +25,12 @@ int mp_cnt_lsb(const mp_int *a) /* now scan this digit until a 1 is found */ if ((q & 1u) == 0u) { + mp_digit p; do { - qq = q & 15u; - x += lnz[qq]; + p = q & 15u; + x += lnz[p]; q >>= 4; - } while (qq == 0u); + } while (p == 0u); } return x; } diff --git a/mp_copy.c b/mp_copy.c index a7ac34af3..cf4d5e0b4 100644 --- a/mp_copy.c +++ b/mp_copy.c @@ -7,8 +7,6 @@ mp_err mp_copy(const mp_int *a, mp_int *b) { int n; - mp_digit *tmpa, *tmpb; - mp_err err; /* if dst == src do nothing */ if (a == b) { @@ -17,27 +15,21 @@ mp_err mp_copy(const mp_int *a, mp_int *b) /* grow dest */ if (b->alloc < a->used) { + mp_err err; if ((err = mp_grow(b, a->used)) != MP_OKAY) { return err; } } /* zero b and copy the parameters over */ - /* pointer aliases */ - - /* source */ - tmpa = a->dp; - - /* destination */ - tmpb = b->dp; /* copy all the digits */ for (n = 0; n < a->used; n++) { - *tmpb++ = *tmpa++; + b->dp[n] = a->dp[n]; } /* clear high digits */ - MP_ZERO_DIGITS(tmpb, b->used - n); + MP_ZERO_DIGITS(b->dp + a->used, b->used - a->used); /* copy used count and sign */ b->used = a->used; diff --git a/mp_div.c b/mp_div.c index 23a2acf93..05b96dd51 100644 --- a/mp_div.c +++ b/mp_div.c @@ -15,14 +15,14 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) /* if a < b then q = 0, r = a */ if (mp_cmp_mag(a, b) == MP_LT) { if (d != NULL) { - err = mp_copy(a, d); - } else { - err = MP_OKAY; + if ((err = mp_copy(a, d)) != MP_OKAY) { + return err; + } } if (c != NULL) { mp_zero(c); } - return err; + return MP_OKAY; } if (MP_HAS(S_MP_DIV_RECURSIVE) @@ -31,11 +31,12 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) err = s_mp_div_recursive(a, b, c, d); } else if (MP_HAS(S_MP_DIV_SCHOOL)) { err = s_mp_div_school(a, b, c, d); - } else { + } else if (MP_HAS(S_MP_DIV_SMALL)) { err = s_mp_div_small(a, b, c, d); + } else { + err = MP_VAL; } return err; } #endif - diff --git a/mp_div_3.c b/mp_div_3.c index 5789b2d62..c26692cb2 100644 --- a/mp_div_3.c +++ b/mp_div_3.c @@ -7,7 +7,7 @@ mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) { mp_int q; - mp_word w, t; + mp_word w; mp_digit b; mp_err err; int ix; @@ -22,7 +22,8 @@ mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) q.used = a->used; q.sign = a->sign; w = 0; - for (ix = a->used - 1; ix >= 0; ix--) { + for (ix = a->used; ix --> 0;) { + mp_word t; w = (w << (mp_word)MP_DIGIT_BIT) | (mp_word)a->dp[ix]; if (w >= 3u) { @@ -57,7 +58,7 @@ mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) } mp_clear(&q); - return err; + return MP_OKAY; } #endif diff --git a/mp_exch.c b/mp_exch.c index 7bc4ee706..50b97d963 100644 --- a/mp_exch.c +++ b/mp_exch.c @@ -8,10 +8,6 @@ */ void mp_exch(mp_int *a, mp_int *b) { - mp_int t; - - t = *a; - *a = *b; - *b = t; + MP_EXCH(mp_int, *a, *b); } #endif diff --git a/mp_exptmod.c b/mp_exptmod.c index e643ded2e..b917c0bb8 100644 --- a/mp_exptmod.c +++ b/mp_exptmod.c @@ -64,13 +64,15 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) /* if the modulus is odd or dr != 0 use the montgomery method */ if (MP_HAS(S_MP_EXPTMOD_FAST) && (mp_isodd(P) || (dr != 0))) { return s_mp_exptmod_fast(G, X, P, Y, dr); - } else if (MP_HAS(S_MP_EXPTMOD)) { - /* otherwise use the generic Barrett reduction technique */ + } + + /* otherwise use the generic Barrett reduction technique */ + if (MP_HAS(S_MP_EXPTMOD)) { return s_mp_exptmod(G, X, P, Y, 0); - } else { - /* no exptmod for evens */ - return MP_VAL; } + + /* no exptmod for evens */ + return MP_VAL; } #endif diff --git a/mp_exteuclid.c b/mp_exteuclid.c index eb8ad3728..0d0bfd339 100644 --- a/mp_exteuclid.c +++ b/mp_exteuclid.c @@ -65,7 +65,6 @@ mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp mp_exch(U3, &u3); } - err = MP_OKAY; LBL_ERR: mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL); return err; diff --git a/mp_fread.c b/mp_fread.c index 767e5a3c0..005c62ae9 100644 --- a/mp_fread.c +++ b/mp_fread.c @@ -32,7 +32,7 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream) mp_zero(a); do { - int y; + uint8_t y; unsigned pos; ch = (radix <= 36) ? MP_TOUPPER(ch) : ch; pos = (unsigned)(ch - (int)'('); @@ -40,7 +40,7 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream) break; } - y = (int)s_mp_rmap_reverse[pos]; + y = s_mp_rmap_reverse[pos]; if ((y == 0xff) || (y >= radix)) { break; @@ -50,7 +50,7 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream) if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) { return err; } - if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) { + if ((err = mp_add_d(a, y, a)) != MP_OKAY) { return err; } } while ((ch = fgetc(stream)) != EOF); diff --git a/mp_from_sbin.c b/mp_from_sbin.c index 4335d8860..3ff7d5d8f 100644 --- a/mp_from_sbin.c +++ b/mp_from_sbin.c @@ -4,7 +4,7 @@ /* SPDX-License-Identifier: Unlicense */ /* read signed bin, big endian, first byte is 0==positive or 1==negative */ -mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size) +mp_err mp_from_sbin(mp_int *a, const uint8_t *buf, size_t size) { mp_err err; @@ -14,11 +14,7 @@ mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size) } /* first byte is 0 for positive, non-zero for negative */ - if (buf[0] == (unsigned char)0) { - a->sign = MP_ZPOS; - } else { - a->sign = MP_NEG; - } + a->sign = (buf[0] == (uint8_t)0) ? MP_ZPOS : MP_NEG; return MP_OKAY; } diff --git a/mp_from_ubin.c b/mp_from_ubin.c index 315ff080e..ae79be3cf 100644 --- a/mp_from_ubin.c +++ b/mp_from_ubin.c @@ -3,8 +3,8 @@ /* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ -/* reads a unsigned char array, assumes the msb is stored first [big endian] */ -mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size) +/* reads a uint8_t array, assumes the msb is stored first [big endian] */ +mp_err mp_from_ubin(mp_int *a, const uint8_t *buf, size_t size) { mp_err err; diff --git a/mp_fwrite.c b/mp_fwrite.c index be78f7f28..42d728778 100644 --- a/mp_fwrite.c +++ b/mp_fwrite.c @@ -8,31 +8,24 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) { char *buf; mp_err err; - size_t len, written; + size_t size, written; - /* TODO: this function is not in this PR */ - if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) { + if ((err = mp_radix_size(a, radix, &size)) != MP_OKAY) { return err; } - buf = (char *) MP_MALLOC(len); + buf = (char *) MP_MALLOC(size); if (buf == NULL) { return MP_MEM; } - if ((err = mp_to_radix(a, buf, len, &written, radix)) != MP_OKAY) { - goto LBL_ERR; + if ((err = mp_to_radix(a, buf, size, &written, radix)) == MP_OKAY) { + if (fwrite(buf, written, 1uL, stream) != 1uL) { + err = MP_ERR; + } } - if (fwrite(buf, written, 1uL, stream) != 1uL) { - err = MP_ERR; - goto LBL_ERR; - } - err = MP_OKAY; - - -LBL_ERR: - MP_FREE_BUFFER(buf, len); + MP_FREE_BUFFER(buf, size); return err; } #endif diff --git a/mp_grow.c b/mp_grow.c index 3354e5964..25be5edd2 100644 --- a/mp_grow.c +++ b/mp_grow.c @@ -6,9 +6,6 @@ /* grow as required */ mp_err mp_grow(mp_int *a, int size) { - int i; - mp_digit *tmp; - /* if the alloc size is smaller alloc more ram */ if (a->alloc < size) { /* reallocate the array a->dp @@ -17,21 +14,20 @@ mp_err mp_grow(mp_int *a, int size) * in case the operation failed we don't want * to overwrite the dp member of a. */ - tmp = (mp_digit *) MP_REALLOC(a->dp, - (size_t)a->alloc * sizeof(mp_digit), - (size_t)size * sizeof(mp_digit)); - if (tmp == NULL) { + mp_digit *dp = (mp_digit *) MP_REALLOC(a->dp, + (size_t)a->alloc * sizeof(mp_digit), + (size_t)size * sizeof(mp_digit)); + if (dp == NULL) { /* reallocation failed but "a" is still valid [can be freed] */ return MP_MEM; } /* reallocation succeeded so set a->dp */ - a->dp = tmp; + a->dp = dp; /* zero excess digits */ - i = a->alloc; + MP_ZERO_DIGITS(a->dp + a->alloc, size - a->alloc); a->alloc = size; - MP_ZERO_DIGITS(a->dp + i, a->alloc - i); } return MP_OKAY; } diff --git a/mp_is_square.c b/mp_is_square.c index f92ecbfb8..47f830015 100644 --- a/mp_is_square.c +++ b/mp_is_square.c @@ -28,10 +28,10 @@ static const char rem_105[105] = { /* Store non-zero to ret if arg is square, and zero if not */ mp_err mp_is_square(const mp_int *arg, bool *ret) { - mp_err err; - mp_digit c; - mp_int t; - unsigned long r; + mp_err err; + mp_digit c; + mp_int t; + uint32_t r; /* Default to Non-square :) */ *ret = false; diff --git a/mp_kronecker.c b/mp_kronecker.c index 0ac6338b9..b106f7731 100644 --- a/mp_kronecker.c +++ b/mp_kronecker.c @@ -23,7 +23,7 @@ mp_err mp_kronecker(const mp_int *a, const mp_int *p, int *c) mp_err err; int v, k; - static const int table[8] = {0, 1, 0, -1, 0, -1, 0, 1}; + static const char table[] = {0, 1, 0, -1, 0, -1, 0, 1}; if (mp_iszero(p)) { if ((a->used == 1) && (a->dp[0] == 1u)) { diff --git a/mp_lshd.c b/mp_lshd.c index b0a845486..6c1440261 100644 --- a/mp_lshd.c +++ b/mp_lshd.c @@ -7,8 +7,6 @@ mp_err mp_lshd(mp_int *a, int b) { int x; - mp_err err; - mp_digit *top, *bottom; /* if its less than zero return */ if (b <= 0) { @@ -21,6 +19,7 @@ mp_err mp_lshd(mp_int *a, int b) /* grow to fit the new digits */ if (a->alloc < (a->used + b)) { + mp_err err; if ((err = mp_grow(a, a->used + b)) != MP_OKAY) { return err; } @@ -29,18 +28,12 @@ mp_err mp_lshd(mp_int *a, int b) /* increment the used by the shift amount then copy upwards */ a->used += b; - /* top */ - top = a->dp + a->used - 1; - - /* base */ - bottom = (a->dp + a->used - 1) - b; - /* much like mp_rshd this is implemented using a sliding window * except the window goes the otherway around. Copying from * the bottom to the top. see mp_rshd.c for more info. */ - for (x = a->used - 1; x >= b; x--) { - *top-- = *bottom--; + for (x = a->used; x --> b;) { + a->dp[x] = a->dp[x - b]; } /* zero the lower digits */ diff --git a/mp_mod_2d.c b/mp_mod_2d.c index 651c79a6c..a94a314cd 100644 --- a/mp_mod_2d.c +++ b/mp_mod_2d.c @@ -9,8 +9,11 @@ mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c) int x; mp_err err; - /* if b is <= 0 then zero the int */ - if (b <= 0) { + if (b < 0) { + return MP_VAL; + } + + if (b == 0) { mp_zero(c); return MP_OKAY; } @@ -20,7 +23,6 @@ mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c) return mp_copy(a, c); } - /* copy */ if ((err = mp_copy(a, c)) != MP_OKAY) { return err; } diff --git a/mp_montgomery_calc_normalization.c b/mp_montgomery_calc_normalization.c index 0d0d5c482..cc07799dc 100644 --- a/mp_montgomery_calc_normalization.c +++ b/mp_montgomery_calc_normalization.c @@ -26,7 +26,6 @@ mp_err mp_montgomery_calc_normalization(mp_int *a, const mp_int *b) bits = 1; } - /* now compute C = A * B mod b */ for (x = bits - 1; x < (int)MP_DIGIT_BIT; x++) { if ((err = mp_mul_2(a, a)) != MP_OKAY) { diff --git a/mp_neg.c b/mp_neg.c index 2fc1854bf..f54ef3edd 100644 --- a/mp_neg.c +++ b/mp_neg.c @@ -6,18 +6,14 @@ /* b = -a */ mp_err mp_neg(const mp_int *a, mp_int *b) { - mp_err err; if (a != b) { + mp_err err; if ((err = mp_copy(a, b)) != MP_OKAY) { return err; } } - if (!mp_iszero(b)) { - b->sign = (a->sign == MP_ZPOS) ? MP_NEG : MP_ZPOS; - } else { - b->sign = MP_ZPOS; - } + b->sign = mp_iszero(b) || b->sign == MP_NEG ? MP_ZPOS : MP_NEG; return MP_OKAY; } diff --git a/mp_pack.c b/mp_pack.c index ec0f62f69..447f1fdf9 100644 --- a/mp_pack.c +++ b/mp_pack.c @@ -11,7 +11,7 @@ mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size { mp_err err; size_t odd_nails, nail_bytes, i, j, count; - unsigned char odd_nail_mask; + uint8_t odd_nail_mask; mp_int t; @@ -32,22 +32,22 @@ mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size odd_nails = (nails % 8u); odd_nail_mask = 0xff; for (i = 0u; i < odd_nails; ++i) { - odd_nail_mask ^= (unsigned char)(1u << (7u - i)); + odd_nail_mask ^= (uint8_t)(1u << (7u - i)); } nail_bytes = nails / 8u; for (i = 0u; i < count; ++i) { for (j = 0u; j < size; ++j) { - unsigned char *byte = (unsigned char *)rop + - (((order == MP_LSB_FIRST) ? i : ((count - 1u) - i)) * size) + - ((endian == MP_LITTLE_ENDIAN) ? j : ((size - 1u) - j)); + uint8_t *byte = (uint8_t *)rop + + (((order == MP_LSB_FIRST) ? i : ((count - 1u) - i)) * size) + + ((endian == MP_LITTLE_ENDIAN) ? j : ((size - 1u) - j)); if (j >= (size - nail_bytes)) { *byte = 0; continue; } - *byte = (unsigned char)((j == ((size - nail_bytes) - 1u)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFFuL)); + *byte = (uint8_t)((j == ((size - nail_bytes) - 1u)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFFuL)); if ((err = mp_div_2d(&t, (j == ((size - nail_bytes) - 1u)) ? (int)(8u - odd_nails) : 8, &t, NULL)) != MP_OKAY) { goto LBL_ERR; diff --git a/mp_prime_rabin_miller_trials.c b/mp_prime_rabin_miller_trials.c index 1728142c5..9f66f8d77 100644 --- a/mp_prime_rabin_miller_trials.c +++ b/mp_prime_rabin_miller_trials.c @@ -36,7 +36,8 @@ int mp_prime_rabin_miller_trials(int size) for (x = 0; x < (int)(sizeof(sizes)/(sizeof(sizes[0]))); x++) { if (sizes[x].k == size) { return sizes[x].t; - } else if (sizes[x].k > size) { + } + if (sizes[x].k > size) { return (x == 0) ? sizes[0].t : sizes[x - 1].t; } } diff --git a/mp_prime_rand.c b/mp_prime_rand.c index ff6df9c39..8476b4f80 100644 --- a/mp_prime_rand.c +++ b/mp_prime_rand.c @@ -20,7 +20,7 @@ /* This is possibly the mother of all prime generation functions, muahahahahaha! */ mp_err mp_prime_rand(mp_int *a, int t, int size, int flags) { - unsigned char *tmp, maskAND, maskOR_msb, maskOR_lsb; + uint8_t *tmp, maskAND, maskOR_msb, maskOR_lsb; int bsize, maskOR_msb_offset; bool res; mp_err err; @@ -39,19 +39,19 @@ mp_err mp_prime_rand(mp_int *a, int t, int size, int flags) bsize = (size>>3) + ((size&7)?1:0); /* we need a buffer of bsize bytes */ - tmp = (unsigned char *) MP_MALLOC((size_t)bsize); + tmp = (uint8_t *) MP_MALLOC((size_t)bsize); if (tmp == NULL) { return MP_MEM; } /* calc the maskAND value for the MSbyte*/ - maskAND = ((size&7) == 0) ? 0xFFu : (unsigned char)(0xFFu >> (8 - (size & 7))); + maskAND = ((size&7) == 0) ? 0xFFu : (uint8_t)(0xFFu >> (8 - (size & 7))); /* calc the maskOR_msb */ maskOR_msb = 0; maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0; if ((flags & MP_PRIME_2MSB_ON) != 0) { - maskOR_msb |= (unsigned char)(0x80 >> ((9 - size) & 7)); + maskOR_msb |= (uint8_t)(0x80 >> ((9 - size) & 7)); } /* get the maskOR_lsb */ @@ -68,7 +68,7 @@ mp_err mp_prime_rand(mp_int *a, int t, int size, int flags) /* work over the MSbyte */ tmp[0] &= maskAND; - tmp[0] |= (unsigned char)(1 << ((size - 1) & 7)); + tmp[0] |= (uint8_t)(1 << ((size - 1) & 7)); /* mix in the maskORs */ tmp[maskOR_msb_offset] |= maskOR_msb; diff --git a/mp_read_radix.c b/mp_read_radix.c index d4a3d1ecf..df8059a55 100644 --- a/mp_read_radix.c +++ b/mp_read_radix.c @@ -31,13 +31,13 @@ mp_err mp_read_radix(mp_int *a, const char *str, int radix) * this allows numbers like 1AB and 1ab to represent the same value * [e.g. in hex] */ - int y; + uint8_t y; char ch = (radix <= 36) ? (char)MP_TOUPPER((int)*str) : *str; unsigned pos = (unsigned)(ch - '('); if (MP_RMAP_REVERSE_SIZE < pos) { break; } - y = (int)s_mp_rmap_reverse[pos]; + y = s_mp_rmap_reverse[pos]; /* if the char was found in the map * and is less than the given radix add it @@ -49,14 +49,14 @@ mp_err mp_read_radix(mp_int *a, const char *str, int radix) if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) { return err; } - if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) { + if ((err = mp_add_d(a, y, a)) != MP_OKAY) { return err; } ++str; } /* if an illegal character was found, fail. */ - if (!((*str == '\0') || (*str == '\r') || (*str == '\n'))) { + if ((*str != '\0') && (*str != '\r') && (*str != '\n')) { return MP_VAL; } diff --git a/mp_reduce.c b/mp_reduce.c index 1b4435c95..5226fe7d4 100644 --- a/mp_reduce.c +++ b/mp_reduce.c @@ -24,19 +24,19 @@ mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu) /* according to HAC this optimization is ok */ if ((mp_digit)um > ((mp_digit)1 << (MP_DIGIT_BIT - 1))) { if ((err = mp_mul(&q, mu, &q)) != MP_OKAY) { - goto CLEANUP; + goto LBL_ERR; } } else if (MP_HAS(S_MP_MUL_HIGH_DIGS)) { if ((err = s_mp_mul_high_digs(&q, mu, &q, um)) != MP_OKAY) { - goto CLEANUP; + goto LBL_ERR; } } else if (MP_HAS(S_MP_MUL_HIGH_DIGS_FAST)) { if ((err = s_mp_mul_high_digs_fast(&q, mu, &q, um)) != MP_OKAY) { - goto CLEANUP; + goto LBL_ERR; } } else { err = MP_VAL; - goto CLEANUP; + goto LBL_ERR; } /* q3 = q2 / b**(k+1) */ @@ -44,38 +44,38 @@ mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu) /* x = x mod b**(k+1), quick (no division) */ if ((err = mp_mod_2d(x, MP_DIGIT_BIT * (um + 1), x)) != MP_OKAY) { - goto CLEANUP; + goto LBL_ERR; } /* q = q * m mod b**(k+1), quick (no division) */ if ((err = s_mp_mul_digs(&q, m, &q, um + 1)) != MP_OKAY) { - goto CLEANUP; + goto LBL_ERR; } /* x = x - q */ if ((err = mp_sub(x, &q, x)) != MP_OKAY) { - goto CLEANUP; + goto LBL_ERR; } /* If x < 0, add b**(k+1) to it */ if (mp_cmp_d(x, 0uL) == MP_LT) { mp_set(&q, 1uL); if ((err = mp_lshd(&q, um + 1)) != MP_OKAY) { - goto CLEANUP; + goto LBL_ERR; } if ((err = mp_add(x, &q, x)) != MP_OKAY) { - goto CLEANUP; + goto LBL_ERR; } } /* Back off if it's too big */ while (mp_cmp(x, m) != MP_LT) { if ((err = s_mp_sub(x, m, x)) != MP_OKAY) { - goto CLEANUP; + goto LBL_ERR; } } -CLEANUP: +LBL_ERR: mp_clear(&q); return err; diff --git a/mp_reduce_is_2k.c b/mp_reduce_is_2k.c index 618ab54ab..a5798e62f 100644 --- a/mp_reduce_is_2k.c +++ b/mp_reduce_is_2k.c @@ -6,17 +6,13 @@ /* determines if mp_reduce_2k can be used */ bool mp_reduce_is_2k(const mp_int *a) { - int ix, iy, iw; - mp_digit iz; - if (a->used == 0) { return false; } else if (a->used == 1) { return true; } else if (a->used > 1) { - iy = mp_count_bits(a); - iz = 1; - iw = 1; + int ix, iy = mp_count_bits(a), iw = 1; + mp_digit iz = 1; /* Test every bit from the second digit up, must be 1 */ for (ix = MP_DIGIT_BIT; ix < iy; ix++) { diff --git a/mp_reduce_is_2k_l.c b/mp_reduce_is_2k_l.c index 30fc10d0c..dca2d7e01 100644 --- a/mp_reduce_is_2k_l.c +++ b/mp_reduce_is_2k_l.c @@ -6,14 +6,13 @@ /* determines if reduce_2k_l can be used */ bool mp_reduce_is_2k_l(const mp_int *a) { - int ix, iy; - if (a->used == 0) { return false; } else if (a->used == 1) { return true; } else if (a->used > 1) { /* if more than half of the digits are -1 we're sold */ + int ix, iy; for (iy = ix = 0; ix < a->used; ix++) { if (a->dp[ix] == MP_DIGIT_MAX) { ++iy; diff --git a/mp_rshd.c b/mp_rshd.c index 2eabb1234..d798907a2 100644 --- a/mp_rshd.c +++ b/mp_rshd.c @@ -6,8 +6,7 @@ /* shift right a certain amount of digits */ void mp_rshd(mp_int *a, int b) { - int x; - mp_digit *bottom, *top; + int x; /* if b <= 0 then ignore it */ if (b <= 0) { @@ -20,15 +19,8 @@ void mp_rshd(mp_int *a, int b) return; } - /* shift the digits down */ - - /* bottom */ - bottom = a->dp; - - /* top [offset into digits] */ - top = a->dp + b; - - /* this is implemented as a sliding window where + /* shift the digits down. + * this is implemented as a sliding window where * the window is b-digits long and digits from * the top of the window are copied to the bottom * @@ -39,11 +31,11 @@ void mp_rshd(mp_int *a, int b) \-------------------/ ----> */ for (x = 0; x < (a->used - b); x++) { - *bottom++ = *top++; + a->dp[x] = a->dp[x + b]; } /* zero the top digits */ - MP_ZERO_DIGITS(bottom, a->used - x); + MP_ZERO_DIGITS(a->dp + a->used - b, b); /* remove excess digits */ a->used -= b; diff --git a/mp_set.c b/mp_set.c index 0777f09d7..3ee5f81f7 100644 --- a/mp_set.c +++ b/mp_set.c @@ -6,9 +6,10 @@ /* set to a digit */ void mp_set(mp_int *a, mp_digit b) { + int oldused = a->used; a->dp[0] = b & MP_MASK; a->sign = MP_ZPOS; a->used = (a->dp[0] != 0u) ? 1 : 0; - MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used); + MP_ZERO_DIGITS(a->dp + a->used, oldused - a->used); } #endif diff --git a/mp_shrink.c b/mp_shrink.c index 6c3c95ba4..e5814cbdf 100644 --- a/mp_shrink.c +++ b/mp_shrink.c @@ -6,15 +6,15 @@ /* shrink a bignum */ mp_err mp_shrink(mp_int *a) { - mp_digit *tmp; int alloc = MP_MAX(MP_MIN_PREC, a->used); if (a->alloc != alloc) { - if ((tmp = (mp_digit *) MP_REALLOC(a->dp, - (size_t)a->alloc * sizeof(mp_digit), - (size_t)alloc * sizeof(mp_digit))) == NULL) { + mp_digit *dp = (mp_digit *) MP_REALLOC(a->dp, + (size_t)a->alloc * sizeof(mp_digit), + (size_t)alloc * sizeof(mp_digit)); + if (dp == NULL) { return MP_MEM; } - a->dp = tmp; + a->dp = dp; a->alloc = alloc; } return MP_OKAY; diff --git a/mp_signed_rsh.c b/mp_signed_rsh.c index c56dfba6a..ecaaa2192 100644 --- a/mp_signed_rsh.c +++ b/mp_signed_rsh.c @@ -6,17 +6,16 @@ /* shift right by a certain bit count with sign extension */ mp_err mp_signed_rsh(const mp_int *a, int b, mp_int *c) { - mp_err res; + mp_err err; if (a->sign == MP_ZPOS) { return mp_div_2d(a, b, c, NULL); } - res = mp_add_d(a, 1uL, c); - if (res != MP_OKAY) { - return res; + if ((err = mp_add_d(a, 1uL, c)) != MP_OKAY) { + return err; } - res = mp_div_2d(c, b, c, NULL); - return (res == MP_OKAY) ? mp_sub_d(c, 1uL, c) : res; + err = mp_div_2d(c, b, c, NULL); + return (err == MP_OKAY) ? mp_sub_d(c, 1uL, c) : err; } #endif diff --git a/mp_sqrt.c b/mp_sqrt.c index b51f6151f..e36a81a9c 100644 --- a/mp_sqrt.c +++ b/mp_sqrt.c @@ -25,7 +25,7 @@ mp_err mp_sqrt(const mp_int *arg, mp_int *ret) } if ((err = mp_init(&t2)) != MP_OKAY) { - goto E2; + goto LBL_ERR2; } /* First approx. (not very bad for large arg) */ @@ -33,33 +33,33 @@ mp_err mp_sqrt(const mp_int *arg, mp_int *ret) /* t1 > 0 */ if ((err = mp_div(arg, &t1, &t2, NULL)) != MP_OKAY) { - goto E1; + goto LBL_ERR1; } if ((err = mp_add(&t1, &t2, &t1)) != MP_OKAY) { - goto E1; + goto LBL_ERR1; } if ((err = mp_div_2(&t1, &t1)) != MP_OKAY) { - goto E1; + goto LBL_ERR1; } /* And now t1 > sqrt(arg) */ do { if ((err = mp_div(arg, &t1, &t2, NULL)) != MP_OKAY) { - goto E1; + goto LBL_ERR1; } if ((err = mp_add(&t1, &t2, &t1)) != MP_OKAY) { - goto E1; + goto LBL_ERR1; } if ((err = mp_div_2(&t1, &t1)) != MP_OKAY) { - goto E1; + goto LBL_ERR1; } /* t1 >= sqrt(arg) >= t2 at this point */ } while (mp_cmp_mag(&t1, &t2) == MP_GT); mp_exch(&t1, ret); -E1: +LBL_ERR1: mp_clear(&t2); -E2: +LBL_ERR2: mp_clear(&t1); return err; } diff --git a/mp_sub.c b/mp_sub.c index c859026f5..810474045 100644 --- a/mp_sub.c +++ b/mp_sub.c @@ -6,35 +6,31 @@ /* high level subtraction (handles signs) */ mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c) { - mp_sign sa = a->sign, sb = b->sign; - mp_err err; - - if (sa != sb) { + if (a->sign != b->sign) { /* subtract a negative from a positive, OR */ /* subtract a positive from a negative. */ /* In either case, ADD their magnitudes, */ /* and use the sign of the first number. */ - c->sign = sa; - err = s_mp_add(a, b, c); + c->sign = a->sign; + return s_mp_add(a, b, c); + } + + /* subtract a positive from a positive, OR */ + /* subtract a negative from a negative. */ + /* First, take the difference between their */ + /* magnitudes, then... */ + if (mp_cmp_mag(a, b) == MP_LT) { + /* The second has a larger magnitude */ + /* The result has the *opposite* sign from */ + /* the first number. */ + c->sign = (a->sign == MP_ZPOS) ? MP_NEG : MP_ZPOS; + MP_EXCH(const mp_int *, a, b); } else { - /* subtract a positive from a positive, OR */ - /* subtract a negative from a negative. */ - /* First, take the difference between their */ - /* magnitudes, then... */ - if (mp_cmp_mag(a, b) != MP_LT) { - /* Copy the sign from the first */ - c->sign = sa; - /* The first has a larger or equal magnitude */ - err = s_mp_sub(a, b, c); - } else { - /* The result has the *opposite* sign from */ - /* the first number. */ - c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS; - /* The second has a larger magnitude */ - err = s_mp_sub(b, a, c); - } + /* The first has a larger or equal magnitude */ + /* Copy the sign from the first */ + c->sign = a->sign; } - return err; + return s_mp_sub(a, b, c); } #endif diff --git a/mp_to_radix.c b/mp_to_radix.c index 78ad98988..8b7728d87 100644 --- a/mp_to_radix.c +++ b/mp_to_radix.c @@ -4,17 +4,11 @@ /* SPDX-License-Identifier: Unlicense */ /* reverse an array, used for radix code */ -static void s_mp_reverse(unsigned char *s, size_t len) +static void s_mp_reverse(char *s, size_t len) { - size_t ix, iy; - unsigned char t; - - ix = 0u; - iy = len - 1u; + size_t ix = 0, iy = len - 1u; while (ix < iy) { - t = s[ix]; - s[ix] = s[iy]; - s[iy] = t; + MP_EXCH(char, s[ix], s[iy]); ++ix; --iy; } @@ -83,7 +77,7 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, i /* reverse the digits of the string. In this case _s points * to the first digit [exluding the sign] of the number */ - s_mp_reverse((unsigned char *)_s, digs); + s_mp_reverse(_s, digs); /* append a NULL so the string is properly terminated */ *str = '\0'; diff --git a/mp_to_sbin.c b/mp_to_sbin.c index ed21adcdb..8225d1306 100644 --- a/mp_to_sbin.c +++ b/mp_to_sbin.c @@ -4,7 +4,7 @@ /* SPDX-License-Identifier: Unlicense */ /* store in signed [big endian] format */ -mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) +mp_err mp_to_sbin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written) { mp_err err; if (maxlen == 0u) { @@ -16,7 +16,7 @@ mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *wr if (written != NULL) { (*written)++; } - buf[0] = (a->sign == MP_ZPOS) ? (unsigned char)0 : (unsigned char)1; + buf[0] = (a->sign == MP_ZPOS) ? (uint8_t)0 : (uint8_t)1; return MP_OKAY; } #endif diff --git a/mp_to_ubin.c b/mp_to_ubin.c index 25835037d..e8643cc07 100644 --- a/mp_to_ubin.c +++ b/mp_to_ubin.c @@ -4,7 +4,7 @@ /* SPDX-License-Identifier: Unlicense */ /* store in unsigned [big endian] format */ -mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) +mp_err mp_to_ubin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written) { size_t x, count; mp_err err; @@ -20,7 +20,7 @@ mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *wr } for (x = count; x --> 0u;) { - buf[x] = (unsigned char)(t.dp[0] & 255u); + buf[x] = (uint8_t)(t.dp[0] & 255u); if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) { goto LBL_ERR; } diff --git a/mp_unpack.c b/mp_unpack.c index 5305b435a..f0127fa44 100644 --- a/mp_unpack.c +++ b/mp_unpack.c @@ -11,7 +11,7 @@ mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size, { mp_err err; size_t odd_nails, nail_bytes, i, j; - unsigned char odd_nail_mask; + uint8_t odd_nail_mask; mp_zero(rop); @@ -22,15 +22,15 @@ mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size, odd_nails = (nails % 8u); odd_nail_mask = 0xff; for (i = 0; i < odd_nails; ++i) { - odd_nail_mask ^= (unsigned char)(1u << (7u - i)); + odd_nail_mask ^= (uint8_t)(1u << (7u - i)); } nail_bytes = nails / 8u; for (i = 0; i < count; ++i) { for (j = 0; j < (size - nail_bytes); ++j) { - unsigned char byte = *((const unsigned char *)op + - (((order == MP_MSB_FIRST) ? i : ((count - 1u) - i)) * size) + - ((endian == MP_BIG_ENDIAN) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes))); + uint8_t byte = *((const uint8_t *)op + + (((order == MP_MSB_FIRST) ? i : ((count - 1u) - i)) * size) + + ((endian == MP_BIG_ENDIAN) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes))); if ((err = mp_mul_2d(rop, (j == 0u) ? (int)(8u - odd_nails) : 8, rop)) != MP_OKAY) { return err; diff --git a/mp_zero.c b/mp_zero.c index 0b79f5008..b7dddd2f3 100644 --- a/mp_zero.c +++ b/mp_zero.c @@ -7,7 +7,7 @@ void mp_zero(mp_int *a) { a->sign = MP_ZPOS; + MP_ZERO_DIGITS(a->dp, a->used); a->used = 0; - MP_ZERO_DIGITS(a->dp, a->alloc); } #endif diff --git a/s_mp_div_recursive.c b/s_mp_div_recursive.c index d641123fd..7007aef06 100644 --- a/s_mp_div_recursive.c +++ b/s_mp_div_recursive.c @@ -17,10 +17,9 @@ static mp_err s_mp_recursion(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r) { mp_err err; - int m, k; mp_int A1, A2, B1, B0, Q1, Q0, R1, R0, t; + int m = a->used - b->used, k = m/2; - m = a->used - b->used; if (m < MP_KARATSUBA_MUL_CUTOFF) { return s_mp_div_school(a, b, q, r); } @@ -29,9 +28,6 @@ static mp_err s_mp_recursion(const mp_int *a, const mp_int *b, mp_int *q, mp_int goto LBL_ERR; } - /* k = floor(m/2) */ - k = m/2; - /* B1 = b / beta^k, B0 = b % beta^k*/ if ((err = mp_div_2d(b, k * MP_DIGIT_BIT, &B1, &B0)) != MP_OKAY) goto LBL_ERR; diff --git a/s_mp_div_school.c b/s_mp_div_school.c index 6ff427a1b..cf34cc97b 100644 --- a/s_mp_div_school.c +++ b/s_mp_div_school.c @@ -140,8 +140,6 @@ mp_err s_mp_div_school(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) mp_exch(&x, d); } - err = MP_OKAY; - LBL_Y: mp_clear(&y); LBL_X: diff --git a/tommath.h b/tommath.h index 874212628..86b19d3be 100644 --- a/tommath.h +++ b/tommath.h @@ -570,12 +570,12 @@ mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c) MP_WUR; int mp_count_bits(const mp_int *a) MP_WUR; size_t mp_ubin_size(const mp_int *a) MP_WUR; -mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR; -mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR; +mp_err mp_from_ubin(mp_int *a, const uint8_t *buf, size_t size) MP_WUR; +mp_err mp_to_ubin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written) MP_WUR; size_t mp_sbin_size(const mp_int *a) MP_WUR; -mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR; -mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR; +mp_err mp_from_sbin(mp_int *a, const uint8_t *buf, size_t size) MP_WUR; +mp_err mp_to_sbin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written) MP_WUR; mp_err mp_read_radix(mp_int *a, const char *str, int radix) MP_WUR; mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix) MP_WUR; diff --git a/tommath_private.h b/tommath_private.h index 0f5ac9345..31f1ea578 100644 --- a/tommath_private.h +++ b/tommath_private.h @@ -148,6 +148,8 @@ extern void MP_FREE(void *mem, size_t size); #define MP_TOUPPER(c) ((((c) >= 'a') && ((c) <= 'z')) ? (((c) + 'A') - 'a') : (c)) +#define MP_EXCH(t, a, b) do { t _c = a; a = b; b = _c; } while (0) + /* Static assertion */ #define MP_STATIC_ASSERT(msg, cond) typedef char mp_static_assert_##msg[(cond) ? 1 : -1]; @@ -267,9 +269,9 @@ extern MP_PRIVATE const mp_digit s_mp_prime_tab[]; #define MP_GET_MAG(name, type) \ type name(const mp_int* a) \ { \ - unsigned i = MP_MIN((unsigned)a->used, (unsigned)((MP_SIZEOF_BITS(type) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \ + int i = MP_MIN(a->used, (int)((MP_SIZEOF_BITS(type) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \ type res = 0u; \ - while (i --> 0u) { \ + while (i --> 0) { \ res <<= ((MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \ res |= (type)a->dp[i]; \ if (MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) { break; } \