Austin Schuh | dace2a6 | 2020-08-18 10:56:48 -0700 | [diff] [blame] | 1 | /* mpz_rrandomb -- Generate a positive random mpz_t of specified bit size, with |
| 2 | long runs of consecutive ones and zeros in the binary representation. |
| 3 | Meant for testing of other MP routines. |
| 4 | |
| 5 | Copyright 2000-2002, 2004, 2012 Free Software Foundation, Inc. |
| 6 | |
| 7 | This file is part of the GNU MP Library. |
| 8 | |
| 9 | The GNU MP Library is free software; you can redistribute it and/or modify |
| 10 | it under the terms of either: |
| 11 | |
| 12 | * the GNU Lesser General Public License as published by the Free |
| 13 | Software Foundation; either version 3 of the License, or (at your |
| 14 | option) any later version. |
| 15 | |
| 16 | or |
| 17 | |
| 18 | * the GNU General Public License as published by the Free Software |
| 19 | Foundation; either version 2 of the License, or (at your option) any |
| 20 | later version. |
| 21 | |
| 22 | or both in parallel, as here. |
| 23 | |
| 24 | The GNU MP Library is distributed in the hope that it will be useful, but |
| 25 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 26 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 27 | for more details. |
| 28 | |
| 29 | You should have received copies of the GNU General Public License and the |
| 30 | GNU Lesser General Public License along with the GNU MP Library. If not, |
| 31 | see https://www.gnu.org/licenses/. */ |
| 32 | |
| 33 | #include "gmp-impl.h" |
| 34 | |
| 35 | static void gmp_rrandomb (mp_ptr, gmp_randstate_t, mp_bitcnt_t); |
| 36 | |
| 37 | void |
| 38 | mpz_rrandomb (mpz_ptr x, gmp_randstate_t rstate, mp_bitcnt_t nbits) |
| 39 | { |
| 40 | mp_size_t nl; |
| 41 | mp_ptr xp; |
| 42 | |
| 43 | nl = BITS_TO_LIMBS (nbits); |
| 44 | if (nbits != 0) |
| 45 | { |
| 46 | xp = MPZ_NEWALLOC (x, nl); |
| 47 | gmp_rrandomb (xp, rstate, nbits); |
| 48 | } |
| 49 | |
| 50 | SIZ(x) = nl; |
| 51 | } |
| 52 | |
| 53 | /* Ask _gmp_rand for 32 bits per call unless that's more than a limb can hold. |
| 54 | Thus, we get the same random number sequence in the common cases. |
| 55 | FIXME: We should always generate the same random number sequence! */ |
| 56 | #if GMP_NUMB_BITS < 32 |
| 57 | #define BITS_PER_RANDCALL GMP_NUMB_BITS |
| 58 | #else |
| 59 | #define BITS_PER_RANDCALL 32 |
| 60 | #endif |
| 61 | |
| 62 | static void |
| 63 | gmp_rrandomb (mp_ptr rp, gmp_randstate_t rstate, mp_bitcnt_t nbits) |
| 64 | { |
| 65 | mp_bitcnt_t bi; |
| 66 | mp_limb_t ranm; /* buffer for random bits */ |
| 67 | unsigned cap_chunksize, chunksize; |
| 68 | mp_size_t i; |
| 69 | |
| 70 | /* Set entire result to 111..1 */ |
| 71 | i = BITS_TO_LIMBS (nbits) - 1; |
| 72 | rp[i] = GMP_NUMB_MAX >> (GMP_NUMB_BITS - (nbits % GMP_NUMB_BITS)) % GMP_NUMB_BITS; |
| 73 | for (i = i - 1; i >= 0; i--) |
| 74 | rp[i] = GMP_NUMB_MAX; |
| 75 | |
| 76 | _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL); |
| 77 | cap_chunksize = nbits / (ranm % 4 + 1); |
| 78 | cap_chunksize += cap_chunksize == 0; /* make it at least 1 */ |
| 79 | |
| 80 | bi = nbits; |
| 81 | |
| 82 | for (;;) |
| 83 | { |
| 84 | _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL); |
| 85 | chunksize = 1 + ranm % cap_chunksize; |
| 86 | bi = (bi < chunksize) ? 0 : bi - chunksize; |
| 87 | |
| 88 | if (bi == 0) |
| 89 | break; /* low chunk is ...1 */ |
| 90 | |
| 91 | rp[bi / GMP_NUMB_BITS] ^= CNST_LIMB (1) << bi % GMP_NUMB_BITS; |
| 92 | |
| 93 | _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL); |
| 94 | chunksize = 1 + ranm % cap_chunksize; |
| 95 | bi = (bi < chunksize) ? 0 : bi - chunksize; |
| 96 | |
| 97 | mpn_incr_u (rp + bi / GMP_NUMB_BITS, CNST_LIMB (1) << bi % GMP_NUMB_BITS); |
| 98 | |
| 99 | if (bi == 0) |
| 100 | break; /* low chunk is ...0 */ |
| 101 | } |
| 102 | } |