2ee12a1892917b21a0ac917595ee9b59a458306d
[crypto_lab3.git] / src / Rand.h
1 #ifndef RAND_H
2 #define RAND_H
3
4 #include <gmpxx.h>
5
6 class Rand
7 {
8 public:
9 /**
10 * Returns a random number with a maximum size of 'sizeBits'.
11 * If 'forceSize' is true the size is exactly 'sizeBits', so the highest bit will always be 1.
12 *
13 * For example, for 'sizeBits' = 12 and 'forceSize' = true, the returned number will be in the following range:
14 * [2^11, 2^12-1]
15 *
16 * For 'sizeBits' = 12 and 'forceSize' = false, the returned number will be in the following range:
17 * [0, 2^12-1]
18 */
19 static mpz_class randSize(int sizeBits, bool forceSize = true);
20
21 /**
22 * Return a random number n such as:
23 * 'from' <= n <= 'to'
24 */
25 static mpz_class randLimits(const mpz_class& from, const mpz_class& to);
26
27 static mpz_class randPrime(const int size_bits);
28
29 private:
30 /**
31 * Read a rand from "/dev/urandom" of size 'size' in byte to the given array 'to'.
32 */
33 static void readRand(char* to, int size);
34 };
35
36 #endif