mini-gmp mpz_powm incorrect result

Paul Zimmermann Paul.Zimmermann at inria.fr
Mon Aug 29 10:29:45 CEST 2022


thank you, I confirm the bug, here is a potential fix:

$ 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;
     }

Paul

> From: Guido Vranken <guidovranken at gmail.com>
> Date: Sun, 28 Aug 2022 16:22:40 +0200
> 
> The following program computes 1^0 % 1:
> 
> //#include <gmp.h>
> #include "mini-gmp.c"
> #include <stdio.h>
> 
> #define CF_CHECK_EQ(expr, res) if ( (expr) != (res) ) { goto end; }
> 
> int main(void)
> {
>     mpz_t a, b, c, res;
>     char* s = NULL;
>     /* noret */ mpz_init(a);
>     /* noret */ mpz_init(b);
>     /* noret */ mpz_init(c);
>     /* noret */ mpz_init(res);
> 
>     CF_CHECK_EQ(mpz_set_str(a, "1", 10), 0);
>     CF_CHECK_EQ(mpz_set_str(b, "0", 10), 0);
>     CF_CHECK_EQ(mpz_set_str(c, "1", 10), 0);
>     CF_CHECK_EQ(mpz_set_str(res, "0", 10), 0);
> 
>     /* noret */ mpz_powm(res, a, b, c);
> 
>     printf("%s\n", mpz_get_str(NULL, 10, res));
> end:
>     return 0;
> }
> 
> The result should be 0, which is the case with regular libgmp, but with
> mini-gmp the result is 1, and this is incorrect.
> 
> Tested on version 6.2.1 and the latest repository checkout, with recent
> clang and gcc, on x64 Linux.
> _______________________________________________
> gmp-bugs mailing list
> gmp-bugs at gmplib.org
> https://gmplib.org/mailman/listinfo/gmp-bugs


More information about the gmp-bugs mailing list