github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpn/generic/random2.c (about) 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.h" 33 #include "gmp-impl.h" 34 35 static void gmp_rrandomb (mp_ptr, gmp_randstate_t, mp_bitcnt_t); 36 37 /* Ask _gmp_rand for 32 bits per call unless that's more than a limb can hold. 38 Thus, we get the same random number sequence in the common cases. 39 FIXME: We should always generate the same random number sequence! */ 40 #if GMP_NUMB_BITS < 32 41 #define BITS_PER_RANDCALL GMP_NUMB_BITS 42 #else 43 #define BITS_PER_RANDCALL 32 44 #endif 45 46 void 47 mpn_random2 (mp_ptr rp, mp_size_t n) 48 { 49 gmp_randstate_ptr rstate = RANDS; 50 int bit_pos; /* bit number of least significant bit where 51 next bit field to be inserted */ 52 mp_limb_t ran, ranm; /* buffer for random bits */ 53 54 /* FIXME: Is n==0 supposed to be allowed? */ 55 ASSERT (n >= 0); 56 57 _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL); 58 ran = ranm; 59 60 /* Start off at a random bit position in the most significant limb. */ 61 bit_pos = ran % GMP_NUMB_BITS; 62 63 gmp_rrandomb (rp, rstate, n * GMP_NUMB_BITS - bit_pos); 64 } 65 66 static void 67 gmp_rrandomb (mp_ptr rp, gmp_randstate_t rstate, mp_bitcnt_t nbits) 68 { 69 mp_bitcnt_t bi; 70 mp_limb_t ranm; /* buffer for random bits */ 71 unsigned cap_chunksize, chunksize; 72 mp_size_t i; 73 74 /* Set entire result to 111..1 */ 75 i = BITS_TO_LIMBS (nbits) - 1; 76 rp[i] = GMP_NUMB_MAX >> (GMP_NUMB_BITS - (nbits % GMP_NUMB_BITS)) % GMP_NUMB_BITS; 77 for (i = i - 1; i >= 0; i--) 78 rp[i] = GMP_NUMB_MAX; 79 80 _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL); 81 cap_chunksize = nbits / (ranm % 4 + 1); 82 cap_chunksize += cap_chunksize == 0; /* make it at least 1 */ 83 84 bi = nbits; 85 86 for (;;) 87 { 88 _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL); 89 chunksize = 1 + ranm % cap_chunksize; 90 bi = (bi < chunksize) ? 0 : bi - chunksize; 91 92 if (bi == 0) 93 break; /* low chunk is ...1 */ 94 95 rp[bi / GMP_NUMB_BITS] ^= CNST_LIMB (1) << bi % GMP_NUMB_BITS; 96 97 _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL); 98 chunksize = 1 + ranm % cap_chunksize; 99 bi = (bi < chunksize) ? 0 : bi - chunksize; 100 101 mpn_incr_u (rp + bi / GMP_NUMB_BITS, CNST_LIMB (1) << bi % GMP_NUMB_BITS); 102 103 if (bi == 0) 104 break; /* low chunk is ...0 */ 105 } 106 }