Blame mpz/urandomm.c

Packit 5c3484
/* mpz_urandomm (rop, state, n) -- Generate a uniform pseudorandom
Packit 5c3484
   integer in the range 0 to N-1, using STATE as the random state
Packit 5c3484
   previously initialized by a call to gmp_randinit().
Packit 5c3484
Packit 5c3484
Copyright 2000, 2002, 2012 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
#include "longlong.h" /* for count_leading_zeros */
Packit 5c3484
Packit 5c3484
Packit 5c3484
#define MAX_URANDOMM_ITER  80
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
mpz_urandomm (mpz_ptr rop, gmp_randstate_t rstate, mpz_srcptr n)
Packit 5c3484
{
Packit 5c3484
  mp_ptr rp, np, nlast;
Packit 5c3484
  mp_size_t nbits, size;
Packit 5c3484
  int count;
Packit 5c3484
  int pow2;
Packit 5c3484
  int cmp;
Packit 5c3484
  TMP_DECL;
Packit 5c3484
Packit 5c3484
  size = ABSIZ (n);
Packit 5c3484
  if (UNLIKELY (size == 0))
Packit 5c3484
    DIVIDE_BY_ZERO;
Packit 5c3484
Packit 5c3484
  nlast = &PTR (n)[size - 1];
Packit 5c3484
Packit 5c3484
  /* Detect whether n is a power of 2.  */
Packit 5c3484
  pow2 = POW2_P (*nlast);
Packit 5c3484
  if (pow2 != 0)
Packit 5c3484
    for (np = PTR (n); np < nlast; np++)
Packit 5c3484
      if (*np != 0)
Packit 5c3484
	{
Packit 5c3484
	  pow2 = 0;		/* Mark n as `not a power of two'.  */
Packit 5c3484
	  break;
Packit 5c3484
	}
Packit 5c3484
Packit 5c3484
  count_leading_zeros (count, *nlast);
Packit 5c3484
  nbits = size * GMP_NUMB_BITS - (count - GMP_NAIL_BITS) - pow2;
Packit 5c3484
  if (nbits == 0)		/* nbits == 0 means that n was == 1.  */
Packit 5c3484
    {
Packit 5c3484
      SIZ (rop) = 0;
Packit 5c3484
      return;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  TMP_MARK;
Packit 5c3484
  np = PTR (n);
Packit 5c3484
  if (rop == n)
Packit 5c3484
    {
Packit 5c3484
      mp_ptr tp;
Packit 5c3484
      tp = TMP_ALLOC_LIMBS (size);
Packit 5c3484
      MPN_COPY (tp, np, size);
Packit 5c3484
      np = tp;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  /* Here the allocated size can be one too much if n is a power of
Packit 5c3484
     (2^GMP_NUMB_BITS) but it's convenient for using mpn_cmp below.  */
Packit 5c3484
  rp = MPZ_REALLOC (rop, size);
Packit 5c3484
  /* Clear last limb to prevent the case in which size is one too much.  */
Packit 5c3484
  rp[size - 1] = 0;
Packit 5c3484
Packit 5c3484
  count = MAX_URANDOMM_ITER;	/* Set iteration count limit.  */
Packit 5c3484
  do
Packit 5c3484
    {
Packit 5c3484
      _gmp_rand (rp, rstate, nbits);
Packit 5c3484
      MPN_CMP (cmp, rp, np, size);
Packit 5c3484
    }
Packit 5c3484
  while (cmp >= 0 && --count != 0);
Packit 5c3484
Packit 5c3484
  if (count == 0)
Packit 5c3484
    /* Too many iterations; return result mod n == result - n */
Packit 5c3484
    mpn_sub_n (rp, rp, np, size);
Packit 5c3484
Packit 5c3484
  MPN_NORMALIZE (rp, size);
Packit 5c3484
  SIZ (rop) = size;
Packit 5c3484
  TMP_FREE;
Packit 5c3484
}