/* * BigInt Random Generation * (C) 1999-2007 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ #include #include #include namespace Botan { /* * Randomize this number */ void BigInt::randomize(RandomNumberGenerator& rng, size_t bitsize, bool set_high_bit) { set_sign(Positive); if(bitsize == 0) { clear(); } else { secure_vector array = rng.random_vec(round_up(bitsize, 8) / 8); // Always cut unwanted bits if(bitsize % 8) array[0] &= 0xFF >> (8 - (bitsize % 8)); // Set the highest bit if wanted if (set_high_bit) array[0] |= 0x80 >> ((bitsize % 8) ? (8 - bitsize % 8) : 0); binary_decode(array); } } /* * Generate a random integer within given range */ BigInt BigInt::random_integer(RandomNumberGenerator& rng, const BigInt& min, const BigInt& max) { BigInt delta_upper_bound = max - min - 1; if(delta_upper_bound < 0) throw Invalid_Argument("random_integer: invalid min/max values"); // Choose x in [0, delta_upper_bound] BigInt x; do { auto bitsize = delta_upper_bound.bits(); x.randomize(rng, bitsize, false); } while(x > delta_upper_bound); return min + x; } }