mpn_modexact_1_odd idea

David Sparks sparks05 at proton.me
Mon Feb 16 08:43:04 CET 2026


The comment above JACOBI_MOD_OR_MODEXACT_1_ODD says:
/* Set a_rem to {a_ptr,a_size} reduced modulo b, either using mod_1 or
   modexact_1_odd, but in either case leaving a_rem<b.  b must be odd and
   unsigned.  modexact_1_odd effectively calculates -a mod b, and
   result_bit1 is adjusted for the factor of -1.

   The way mpn_modexact_1_odd sometimes bases its remainder on a_size and
   sometimes on a_size-1 means if GMP_NUMB_BITS is odd we can't know what
   factor to introduce into result_bit1, so for that case use mpn_mod_1
   unconditionally.

   FIXME: mpn_modexact_1_odd is more efficient, so some way to get it used
   for odd GMP_NUMB_BITS would be good.  Perhaps it could mung its result,
   or not skip a divide step, or something. */

One way to do this would be, in the case that GMP_NUMB_BITS is odd,
have mpn_modexact_1_odd add an extra factor of 2 when required.
This is a simple modular doubling, so is a pretty cheap fixup.
(Or it could be a modular halving, whichever is easier.
rem = (rem >> 1) + ((rem & 1) ? modulus/2+1 : 0);)

Still, it does add to the cost of GCD computations, and I don't
know the ratio of GCDs to Jacobi symbol computations.


More information about the gmp-devel mailing list