diff options
author | lloyd <[email protected]> | 2008-09-30 22:41:49 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-09-30 22:41:49 +0000 |
commit | 13d08cbe978c4cd0de01aa0120c39470508cbbcb (patch) | |
tree | ff93739131cbca0dbdf23a31cd4b7611faf5aa6e /src/math/bigint/big_rand.cpp | |
parent | 8854fe339f2e1f81091ba65c042824e8cc62cbbc (diff) |
Rearrange BigInt directories:
math/bigint - BigInt implementation
math/numbertheory - Math stuff built on top of BigInt
Coming soon: math/gfp (parts of pk/ecdsa)
Update deps in the pk files
Diffstat (limited to 'src/math/bigint/big_rand.cpp')
-rw-r--r-- | src/math/bigint/big_rand.cpp | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/math/bigint/big_rand.cpp b/src/math/bigint/big_rand.cpp new file mode 100644 index 000000000..055873642 --- /dev/null +++ b/src/math/bigint/big_rand.cpp @@ -0,0 +1,59 @@ +/************************************************* +* BigInt Random Generation Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/bigint.h> +#include <botan/parsing.h> + +namespace Botan { + +/************************************************* +* Construct a BigInt of a specific form * +*************************************************/ +BigInt::BigInt(NumberType type, u32bit bits) + { + set_sign(Positive); + + if(type == Power2) + set_bit(bits); + else + throw Invalid_Argument("BigInt(NumberType): Unknown type"); + } + +/************************************************* +* Randomize this number * +*************************************************/ +void BigInt::randomize(RandomNumberGenerator& rng, + u32bit bitsize) + { + set_sign(Positive); + + if(bitsize == 0) + clear(); + else + { + SecureVector<byte> array((bitsize + 7) / 8); + rng.randomize(array, array.size()); + if(bitsize % 8) + array[0] &= 0xFF >> (8 - (bitsize % 8)); + array[0] |= 0x80 >> ((bitsize % 8) ? (8 - bitsize % 8) : 0); + binary_decode(array, array.size()); + } + } + +/************************************************* +* Generate a random integer within given range * +*************************************************/ +BigInt BigInt::random_integer(RandomNumberGenerator& rng, + const BigInt& min, const BigInt& max) + { + BigInt range = max - min; + + if(range <= 0) + throw Invalid_Argument("random_integer: invalid min/max values"); + + return (min + (BigInt(rng, range.bits() + 2) % range)); + } + +} |