mini-gmp: mpz_abs_sub_bit needs more normalization

Torbjörn Granlund tg at
Mon Aug 25 21:24:53 UTC 2014

nisse at (Niels Möller) writes:

  I've found a bug in mini-gmp. mpz_clrbit fails and produces an
  unnormalized mpz, if one clears the most significant one bit of a
  number, and that one bit is followed by a long string of zeros.
  --- a/mini-gmp.c
  +++ b/mini-gmp.c
  @@ -3569,7 +3569,7 @@ mpz_abs_sub_bit (mpz_t d, mp_bitcnt_t bit_index)
     gmp_assert_nocarry (mpn_sub_1 (dp + limb_index, dp + limb_index,
   				 dn - limb_index, bit));
  -  dn -= (dp[dn-1] == 0);
  +  dn = mpn_normalized_size (dp, dn);
     d->_mp_size = (d->_mp_size < 0) ? - dn : dn;
A pretty obvious bug, and the result of over-agressive optimisation
which might not have place in mini-gmp.

Will you please take a look to see if this is done in more places?  Then
fix for the 6.0 repo as well as the head repo (and perhaps 5.1 repo if
the bug is there).

  We should also add a test case.
I thought mini-gmp now had complete testing after my rants about it...

Please encrypt, key id 0xC8601622

More information about the gmp-bugs mailing list