Blame mpn/generic/bsqrtinv.c

Packit 5c3484
/* mpn_bsqrtinv, compute r such that r^2 * y = 1 (mod 2^{b+1}).
Packit 5c3484
Packit 5c3484
   Contributed to the GNU project by Martin Boij (as part of perfpow.c).
Packit 5c3484
Packit 5c3484
Copyright 2009, 2010, 2012, 2015 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
Packit 5c3484
/* Compute r such that r^2 * y = 1 (mod 2^{b+1}).
Packit 5c3484
   Return non-zero if such an integer r exists.
Packit 5c3484
Packit 5c3484
   Iterates
Packit 5c3484
     r' <-- (3r - r^3 y) / 2
Packit 5c3484
   using Hensel lifting.  Since we divide by two, the Hensel lifting is
Packit 5c3484
   somewhat degenerates.  Therefore, we lift from 2^b to 2^{b+1}-1.
Packit 5c3484
Packit 5c3484
   FIXME:
Packit 5c3484
     (1) Simplify to do precision book-keeping in limbs rather than bits.
Packit 5c3484
Packit 5c3484
     (2) Rewrite iteration as
Packit 5c3484
	   r' <-- r - r (r^2 y - 1) / 2
Packit 5c3484
	 and take advantage of zero low part of r^2 y - 1.
Packit 5c3484
Packit 5c3484
     (3) Use wrap-around trick.
Packit 5c3484
Packit 5c3484
     (4) Use a small table to get starting value.
Packit 5c3484
*/
Packit 5c3484
int
Packit 5c3484
mpn_bsqrtinv (mp_ptr rp, mp_srcptr yp, mp_bitcnt_t bnb, mp_ptr tp)
Packit 5c3484
{
Packit 5c3484
  mp_ptr tp2;
Packit 5c3484
  mp_size_t bn, order[GMP_LIMB_BITS + 1];
Packit 5c3484
  int i, d;
Packit 5c3484
Packit 5c3484
  ASSERT (bnb > 0);
Packit 5c3484
Packit 5c3484
  bn = 1 + bnb / GMP_LIMB_BITS;
Packit 5c3484
Packit 5c3484
  tp2 = tp + bn;
Packit 5c3484
Packit 5c3484
  rp[0] = 1;
Packit 5c3484
  if (bnb == 1)
Packit 5c3484
    {
Packit 5c3484
      if ((yp[0] & 3) != 1)
Packit 5c3484
	return 0;
Packit 5c3484
    }
Packit 5c3484
  else
Packit 5c3484
    {
Packit 5c3484
      if ((yp[0] & 7) != 1)
Packit 5c3484
	return 0;
Packit 5c3484
Packit 5c3484
      d = 0;
Packit 5c3484
      for (; bnb != 2; bnb = (bnb + 2) >> 1)
Packit 5c3484
	order[d++] = bnb;
Packit 5c3484
Packit 5c3484
      for (i = d - 1; i >= 0; i--)
Packit 5c3484
	{
Packit 5c3484
	  bnb = order[i];
Packit 5c3484
	  bn = 1 + bnb / GMP_LIMB_BITS;
Packit 5c3484
Packit 5c3484
	  mpn_sqrlo (tp, rp, bn);
Packit 5c3484
	  mpn_mullo_n (tp2, rp, tp, bn); /* tp2 <- rp ^ 3 */
Packit 5c3484
Packit 5c3484
	  mpn_mul_1 (tp, rp, bn, 3);
Packit 5c3484
Packit 5c3484
	  mpn_mullo_n (rp, yp, tp2, bn);
Packit 5c3484
Packit 5c3484
#if HAVE_NATIVE_mpn_rsh1sub_n
Packit 5c3484
	  mpn_rsh1sub_n (rp, tp, rp, bn);
Packit 5c3484
#else
Packit 5c3484
	  mpn_sub_n (tp2, tp, rp, bn);
Packit 5c3484
	  mpn_rshift (rp, tp2, bn, 1);
Packit 5c3484
#endif
Packit 5c3484
	}
Packit 5c3484
    }
Packit 5c3484
  return 1;
Packit 5c3484
}