diff options
-rw-r--r-- | arith.c | 4 | ||||
-rw-r--r-- | mpi/mpi.c | 15 | ||||
-rw-r--r-- | mpi/mpi.h | 2 | ||||
-rw-r--r-- | mpi/mplogic.c | 4 |
4 files changed, 15 insertions, 10 deletions
@@ -2934,7 +2934,9 @@ val logcount(val n) } case BGNUM: { - mp_size co = mp_count_ones(mp(n)); + mp_err co = mp_count_ones(mp(n)); + if (co < 0) + internal_error("problem in bignum arithmetic"); return unum(co); } default: @@ -2541,11 +2541,11 @@ static mp_size s_mp_count_ones(mp_int *mp) return c; } -mp_size mp_count_ones(mp_int *mp) +mp_err mp_count_ones(mp_int *mp) { if (SIGN(mp) == MP_NEG) { mp_int tmp; - mp_size res; + mp_err res; if ((res = mp_init_copy(&tmp, mp)) != MP_OKAY) return res; if ((res = s_mp_sub_d(&tmp, 1) != MP_OKAY)) @@ -2560,7 +2560,7 @@ mp_size mp_count_ones(mp_int *mp) mp_size mp_is_pow_two(mp_int *mp) { - return s_mp_ispow2(mp) >= 0; + return s_mp_ispow2(mp) < MP_SIZE_MAX; } /* Read an integer from the given string, and set mp to the resulting @@ -2681,7 +2681,7 @@ mp_err mp_toradix_case(mp_int *mp, unsigned char *str, int radix, int low) /* Reverse the digits and sign indicator */ ix = 0; while (ix < pos) { - char tmp2 = str[ix]; + unsigned char tmp2 = str[ix]; str[ix] = str[pos]; str[pos] = tmp2; @@ -3418,12 +3418,15 @@ mp_err s_mp_div_d(mp_int *mp, mp_digit d, mp_digit *r) t = 0; } + assert (t <= MP_DIGIT_MAX); qp[ix] = t; } /* Deliver the remainder, if desired */ - if (r) + if (r) { + assert (w <= MP_DIGIT_MAX); *r = w; + } s_mp_clamp("); mp_exch(", mp); @@ -3950,7 +3953,7 @@ int s_mp_ispow2d(mp_digit d) return -1; /* not a power of two */ /* If d == 0, s_highest_bit returns 0, thus we return -1. */ - return s_highest_bit(d) - 1; + return (int) s_highest_bit(d) - 1; } /* Convert the given character to its digit value, in the given radix. @@ -177,7 +177,7 @@ mp_err mp_to_unsigned_bin(mp_int *mp, unsigned char *str); mp_err mp_to_unsigned_buf(mp_int *mp, unsigned char *str, size_t size); mp_size mp_count_bits(mp_int *mp); -mp_size mp_count_ones(mp_int *mp); +mp_err mp_count_ones(mp_int *mp); mp_size mp_is_pow_two(mp_int *mp); #if MP_COMPAT_MACROS diff --git a/mpi/mplogic.c b/mpi/mplogic.c index eae1ea47..5defba73 100644 --- a/mpi/mplogic.c +++ b/mpi/mplogic.c @@ -185,7 +185,7 @@ mp_err mpl_rsh(mp_int *a, mp_int *b, mp_digit d) /* Now handle any remaining bit shifting */ if (bshift) { - mp_digit prev = 0, next, mask = (1 << bshift) - 1; + mp_digit prev = 0, next, mask = (convert(mp_digit, 1) << bshift) - 1; mp_size ix; /* 'mask' is a digit with the lower bshift bits set, the rest @@ -229,7 +229,7 @@ mp_err mpl_lsh(mp_int *a, mp_int *b, mp_digit d) if (bshift) { mp_size ix; - mp_digit prev = 0, next, mask = (1 << bshift) - 1; + mp_digit prev = 0, next, mask = (convert(mp_digit, 1) << bshift) - 1; for (ix = 0; ix < USED(b); ix++) { next = (DIGIT(b, ix) >> (DIGIT_BIT - bshift)) & mask; |