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