|
Packit |
5c3484 |
/* mpz_rrandomb -- Generate a positive random mpz_t of specified bit size, with
|
|
Packit |
5c3484 |
long runs of consecutive ones and zeros in the binary representation.
|
|
Packit |
5c3484 |
Meant for testing of other MP routines.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
Copyright 2000-2002, 2004, 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 |
|
|
Packit |
5c3484 |
static void gmp_rrandomb (mp_ptr, gmp_randstate_t, mp_bitcnt_t);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
void
|
|
Packit |
5c3484 |
mpz_rrandomb (mpz_ptr x, gmp_randstate_t rstate, mp_bitcnt_t nbits)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mp_size_t nl;
|
|
Packit |
5c3484 |
mp_ptr xp;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
nl = BITS_TO_LIMBS (nbits);
|
|
Packit |
5c3484 |
if (nbits != 0)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
xp = MPZ_NEWALLOC (x, nl);
|
|
Packit |
5c3484 |
gmp_rrandomb (xp, rstate, nbits);
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
SIZ(x) = nl;
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
/* Ask _gmp_rand for 32 bits per call unless that's more than a limb can hold.
|
|
Packit |
5c3484 |
Thus, we get the same random number sequence in the common cases.
|
|
Packit |
5c3484 |
FIXME: We should always generate the same random number sequence! */
|
|
Packit |
5c3484 |
#if GMP_NUMB_BITS < 32
|
|
Packit |
5c3484 |
#define BITS_PER_RANDCALL GMP_NUMB_BITS
|
|
Packit |
5c3484 |
#else
|
|
Packit |
5c3484 |
#define BITS_PER_RANDCALL 32
|
|
Packit |
5c3484 |
#endif
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
static void
|
|
Packit |
5c3484 |
gmp_rrandomb (mp_ptr rp, gmp_randstate_t rstate, mp_bitcnt_t nbits)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mp_bitcnt_t bi;
|
|
Packit |
5c3484 |
mp_limb_t ranm; /* buffer for random bits */
|
|
Packit |
5c3484 |
unsigned cap_chunksize, chunksize;
|
|
Packit |
5c3484 |
mp_size_t i;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
/* Set entire result to 111..1 */
|
|
Packit |
5c3484 |
i = BITS_TO_LIMBS (nbits) - 1;
|
|
Packit |
5c3484 |
rp[i] = GMP_NUMB_MAX >> (GMP_NUMB_BITS - (nbits % GMP_NUMB_BITS)) % GMP_NUMB_BITS;
|
|
Packit |
5c3484 |
for (i = i - 1; i >= 0; i--)
|
|
Packit |
5c3484 |
rp[i] = GMP_NUMB_MAX;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
_gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
|
|
Packit |
5c3484 |
cap_chunksize = nbits / (ranm % 4 + 1);
|
|
Packit |
5c3484 |
cap_chunksize += cap_chunksize == 0; /* make it at least 1 */
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
bi = nbits;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
for (;;)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
_gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
|
|
Packit |
5c3484 |
chunksize = 1 + ranm % cap_chunksize;
|
|
Packit |
5c3484 |
bi = (bi < chunksize) ? 0 : bi - chunksize;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
if (bi == 0)
|
|
Packit |
5c3484 |
break; /* low chunk is ...1 */
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
rp[bi / GMP_NUMB_BITS] ^= CNST_LIMB (1) << bi % GMP_NUMB_BITS;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
_gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
|
|
Packit |
5c3484 |
chunksize = 1 + ranm % cap_chunksize;
|
|
Packit |
5c3484 |
bi = (bi < chunksize) ? 0 : bi - chunksize;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpn_incr_u (rp + bi / GMP_NUMB_BITS, CNST_LIMB (1) << bi % GMP_NUMB_BITS);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
if (bi == 0)
|
|
Packit |
5c3484 |
break; /* low chunk is ...0 */
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
}
|