Question about mpz_powm usage

261 views
Skip to first unread message

michael85

unread,
Jun 24, 2012, 4:47:36 PM6/24/12
to mpir-...@googlegroups.com
Hello Everyone,

For some reason, I am not able to understand why the following code prints different results:

    mpz_t x, n, out;

    mpz_init_set_ui(x, 2UL);
    mpz_init_set_ui(n, 7UL);
    mpz_init(out);

    mpz_invert(out, x, n);
    gmp_printf ("%Zd\n", out);//prints 4. 2 * 4 (mod 7) = 1. OK

    mpz_powm_ui(out, x, -1UL, n);//prints 1. 2 * 1 (mod 7) = 2. How come?
    gmp_printf ("%Zd\n", out);

    mpz_clear(x);
    mpz_clear(n);
    mpz_clear(out);

The documentation states that negative exponents are supported by mpz_powm if base^-1 nod n exists. This behavior is the same as in GMP 4.1 on Windows. What am I missing here?

Best regards,
Mihai Todor

michael85

unread,
Jun 24, 2012, 5:21:00 PM6/24/12
to mpir-...@googlegroups.com
There's a small copy / paste gone wrong issue in the code: mpz_powm_ui(out, x, -1UL, n). Unfortunately, even after I remove the UL, the behavior is the same.

Mihai

Case Van Horsen

unread,
Jun 24, 2012, 5:29:36 PM6/24/12
to mpir-...@googlegroups.com
On Sun, Jun 24, 2012 at 2:21 PM, michael85 <todor...@gmail.com> wrote:
> There's a small copy / paste gone wrong issue in the code: mpz_powm_ui(out,
> x, -1UL, n). Unfortunately, even after I remove the UL, the behavior is the
> same.

You can't pass a negative value as an unsigned long parameter. -1UL
is really to 2**32 - 1. Just create another mpz_t for the exponent and
set it to -1.

casevh

michael85

unread,
Jun 24, 2012, 5:32:22 PM6/24/12
to mpir-...@googlegroups.com
Thanks for pointing that out. I just noticed it myself, after pulling hairs out for a few hours :( I guess this is a documentation bug, or something...

Regards,
Mihai

Bill Hart

unread,
Jun 24, 2012, 6:08:33 PM6/24/12
to mpir-...@googlegroups.com
The documentation makes no sense. The prototype says it is an unsigned
long, which can never be negative.

We should amend the documentation. It would make sense if there were
an mpz_powm_si function.

Bill.
> --
> You received this message because you are subscribed to the Google Groups
> "mpir-devel" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mpir-devel/-/1VgKlOh8w4kJ.
>
> To post to this group, send email to mpir-...@googlegroups.com.
> To unsubscribe from this group, send email to
> mpir-devel+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mpir-devel?hl=en.
Reply all
Reply to author
Forward
0 new messages