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