Small old bug in mpz_powm

Marco Bodrato bodrato at mail.dm.unipi.it
Sun Dec 8 16:29:53 UTC 2019


There is a small old bug in mpz_powm, which can be detected with the
following program:

#include "gmp-impl.h"
int
main (int argc, char *argv[])
{
  mpz_t M, N, E;

  mpz_init_set_ui (M, 1);
  mpz_init_set_ui (E, 1);
  mpz_init (N);
  mpz_setbit (M, 10 * GMP_NUMB_BITS);
  mpz_setbit (M, 0);
  mpz_ui_sub (N, 17, M);
  mpz_powm (N, N, E, M);
  MPZ_CHECK_FORMAT (N);
  mpz_clear (M);
  mpz_clear (N);
  mpz_clear (E);
}

I'd suggest the following patch:

diff -r 2109bbf292bd mpz/powm.c
--- a/mpz/powm.c	Thu Dec 05 07:44:46 2019 +0100
+++ b/mpz/powm.c	Sun Dec 08 17:27:56 2019 +0100
@@ -138,7 +138,7 @@
 	    {
 	      mpn_sub (rp, mp, n, bp, bn);
 	      rn = n;
-	      rn -= (rp[rn - 1] == 0);
+	      MPN_NORMALIZE_NOT_ZERO (rp, rn);
 	    }
 	  else
 	    {


Ĝis,
m

-- 
http://bodrato.it/papers/



More information about the gmp-devel mailing list