Go to the documentation of this file.00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef LIBECC_RNG_H
00030 #define LIBECC_RNG_H
00031
00032 #include <libecc/bitset.h>
00033
00034 namespacelibecc {
00035
00036 classrng {
00037 public:
00038 static unsigned int const S_pool_size = 521;
00039
00040 private:
00041 uint32_t M_pool[S_pool_size / 32 + 1];
00042 bitset<512> M_out;
00043 unsigned int M_out_cnt;
00044
00045 classbit_iterator {
00046 private:
00047 uint32_t* M_ptr;
00048 uint32_t M_head_mask;
00049 public:
00050 bit_iterator(uint32_t* pool, int bit) : M_ptr(pool + bit / 32), M_head_mask(1 << bit % 32) { }
00051 void toggle(void) const{ *M_ptr ^= M_head_mask; }
00052 void set(void) { *M_ptr |= M_head_mask; }
00053 void clear(void) { *M_ptr &= ~M_head_mask; }
00054 uint32_t increment_and_test(uint32_t* pool)
00055 {
00056 M_head_mask <<= 1;
00057 if (M_head_mask == 0)
00058 {
00059 M_head_mask = 1;
00060 ++M_ptr;
00061 }
00062 else if (M_head_mask == (1 << (S_pool_size % 32)) && M_ptr - pool == S_pool_size / 32)
00063 {
00064 M_ptr = pool;
00065 M_head_mask = 1;
00066 }
00067 return (*M_ptr & M_head_mask);
00068 }
00069 };
00070
00071 uint32_t* M_entropy_ptr;
00072 uint32_t const* M_entropy_ptr_end;
00073
00074 bit_iterator M_head;
00075 bit_iterator M_fbp1;
00076 bit_iterator M_fbp2;
00077 bit_iterator M_fbp3;
00078 bit_iterator M_fbp4;
00079 bit_iterator M_fbp5;
00080 bit_iterator M_fbp6;
00081 bit_iterator M_fbp7;
00082 bit_iterator M_fbp8;
00083 bit_iterator M_fbp9;
00084
00085 public:
00089 typedef bitset<S_pool_size> pool_type;
00090 rng(pool_type const& seed);
00091
00092 void generate_512_bits(void);
00094 bitset<512> const& get_512_bits(void) const{ return M_out; }
00095
00096 void add_entropy(uint32_t const* noise, unsigned int number_of_ints);
00097
00103 template<unsigned int n>
00104 void add_entropy(bitset<n> const noise)
00105 {
00106 add_entropy(reinterpret_cast<uint32_t const*>(noise.digits_ptr()), bitset<n>::digits * sizeof(bitset_digit_t) / 4);
00107 }
00108 };
00109
00110 }
00111
00112 #endif // LIBECC_RNG_H