mini-gmp mpz_powm incorrect result

Vincent Lefevre vincent at vinc17.net
Tue Aug 30 10:25:32 CEST 2022


On 2022-08-30 08:45:34 +0200, Niels Möller wrote:
> Paul Zimmermann <Paul.Zimmermann at inria.fr> writes:
> 
> > $ diff -u mini-gmp.c.orig mini-gmp.c
> > --- mini-gmp.c.orig     2022-08-29 10:28:20.700995412 +0200
> > +++ mini-gmp.c  2022-08-29 10:27:36.112191428 +0200
> > @@ -3060,6 +3060,7 @@
> >    if (en == 0)
> >      {
> >        mpz_set_ui (r, 1);
> > +      mpz_tdiv_r (r, r, m);
> >        return;
> >      }
> 
> Should solve the problem, but maybe a bit overkill to call mpz_tdiv_r. Perhaps
> 
>    mpz_set_ui (r, mpz_cmpabs_ui (m, 1) != 0);
> 
> or even (mn == 0 check just above this code rules out |m| < 1)
> 
>    mpz_set_ui (r, mpz_cmpabs_ui (m, 1));

Concerning this second solution, the GMP manual says:

 -- Function: int mpz_cmpabs_ui (const mpz_t OP1, unsigned long int OP2)
     Compare the absolute values of OP1 and OP2.  Return a positive
     value if abs(OP1) > abs(OP2), zero if abs(OP1) = abs(OP2), or a
     negative value if abs(OP1) < abs(OP2).

So if the mpz_cmpabs_ui implementation is changed so that it can
return a value larger than 1, you need to make sure to remember to
update the code.

-- 
Vincent Lefèvre <vincent at vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


More information about the gmp-bugs mailing list