Fabcoin Core  0.16.2
P2P Digital Currency
mersenne.h
Go to the documentation of this file.
1 // mersenne.h - written and placed in public domain by Jeffrey Walton.
2 // Copyright assigned to Crypto++ project.
3 
9 #ifndef CRYPTOPP_MERSENNE_TWISTER_H
10 #define CRYPTOPP_MERSENNE_TWISTER_H
11 
12 #include "cryptlib.h"
13 #include "secblock.h"
14 #include "misc.h"
15 
17 
18 template <unsigned int K, unsigned int M, unsigned int N, unsigned int F, unsigned long S>
32 {
33 public:
38  MersenneTwister(unsigned long seed = S) : m_seed(seed), m_idx(N)
39  {
40  m_state[0] = seed;
41  for (unsigned int i = 1; i < N+1; i++)
42  m_state[i] = word32(F * (m_state[i-1] ^ (m_state[i-1] >> 30)) + i);
43  }
44 
52  void GenerateBlock(byte *output, size_t size)
53  {
54  // Handle word32 size blocks
55  word32 temp;
56  for (size_t i=0; i < size/4; i++, output += 4)
57  {
58 #if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS) && defined(IS_LITTLE_ENDIAN)
59  *((word32*)output) = ByteReverse(NextMersenneWord());
60 #elif defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS)
61  *((word32*)output) = NextMersenneWord();
62 #else
63  temp = NextMersenneWord();
64  output[3] = CRYPTOPP_GET_BYTE_AS_BYTE(temp, 0);
65  output[2] = CRYPTOPP_GET_BYTE_AS_BYTE(temp, 1);
66  output[1] = CRYPTOPP_GET_BYTE_AS_BYTE(temp, 2);
67  output[0] = CRYPTOPP_GET_BYTE_AS_BYTE(temp, 3);
68 #endif
69  }
70 
71  // No tail bytes
72  if (size%4 == 0)
73  {
74  // Wipe temp
75  *((volatile word32*)&temp) = 0;
76  return;
77  }
78 
79  // Handle tail bytes
80  temp = NextMersenneWord();
81  switch (size%4)
82  {
83  case 3: output[2] = CRYPTOPP_GET_BYTE_AS_BYTE(temp, 1); /* fall through */
84  case 2: output[1] = CRYPTOPP_GET_BYTE_AS_BYTE(temp, 2); /* fall through */
85  case 1: output[0] = CRYPTOPP_GET_BYTE_AS_BYTE(temp, 3); break;
86 
87  default: CRYPTOPP_ASSERT(0); ;;
88  }
89 
90  // Wipe temp
91  *((volatile word32*)&temp) = 0;
92  }
93 
99  {
100  const word32 range = max-min;
101  if (range == 0xffffffffL)
102  return NextMersenneWord();
103 
104  const int maxBits = BitPrecision(range);
105  word32 value;
106 
107  do{
108  value = Crop(NextMersenneWord(), maxBits);
109  } while (value > range);
110 
111  return value+min;
112  }
113 
120  void DiscardBytes(size_t n)
121  {
122  for(size_t i=0; i < RoundUpToMultipleOf(n, 4U); i++)
123  NextMersenneWord();
124  }
125 
126 protected:
127 
133  {
134  if (m_idx >= N) { Twist(); }
135 
136  word32 temp = m_state[m_idx++];
137 
138  temp ^= (temp >> 11);
139  temp ^= (temp << 7) & 0x9D2C5680; // 0x9D2C5680 (2636928640)
140  temp ^= (temp << 15) & 0xEFC60000; // 0xEFC60000 (4022730752)
141 
142  return temp ^ (temp >> 18);
143  }
144 
146  void Twist()
147  {
148  static const unsigned long magic[2]={0x0UL, K};
149  word32 kk, temp;
150 
151  CRYPTOPP_ASSERT(N >= M);
152  for (kk=0;kk<N-M;kk++)
153  {
154  temp = (m_state[kk] & 0x80000000)|(m_state[kk+1] & 0x7FFFFFFF);
155  m_state[kk] = m_state[kk+M] ^ (temp >> 1) ^ magic[temp & 0x1UL];
156  }
157 
158  for (;kk<N-1;kk++)
159  {
160  temp = (m_state[kk] & 0x80000000)|(m_state[kk+1] & 0x7FFFFFFF);
161  m_state[kk] = m_state[kk+(M-N)] ^ (temp >> 1) ^ magic[temp & 0x1UL];
162  }
163 
164  temp = (m_state[N-1] & 0x80000000)|(m_state[0] & 0x7FFFFFFF);
165  m_state[N-1] = m_state[M-1] ^ (temp >> 1) ^ magic[temp & 0x1UL];
166 
167  // Reset index
168  m_idx = 0;
169 
170  // Wipe temp
171  *((volatile word32*)&temp) = 0;
172  }
173 
174 private:
175 
179  unsigned int m_seed;
181  unsigned int m_idx;
182 };
183 
190 #if CRYPTOPP_DOXYGEN_PROCESSING
191 class MT19937 : public MersenneTwister<0x9908B0DF /*2567483615*/, 397, 624, 0x10DCD /*69069*/, 4537> {};
192 #else
193 typedef MersenneTwister<0x9908B0DF /*2567483615*/, 397, 624, 0x10DCD /*69069*/, 4537> MT19937;
194 #endif
195 
203 #if CRYPTOPP_DOXYGEN_PROCESSING
204 class MT19937ar : public MersenneTwister<0x9908B0DF /*2567483615*/, 397, 624, 0x6C078965 /*1812433253*/, 5489> {};
205 #else
206 typedef MersenneTwister<0x9908B0DF /*2567483615*/, 397, 624, 0x6C078965 /*1812433253*/, 5489> MT19937ar;
207 #endif
208 
210 
211 #endif // CRYPTOPP_MERSENNE_TWISTER_H
212 
uint8_t byte
Definition: Common.h:57
Utility functions for the Crypto++ library.
void Twist()
Performs the twist operaton on the state array.
Definition: mersenne.h:146
MersenneTwister< 0x9908B0DF, 397, 624, 0x10DCD, 4537 > MT19937
Definition: mersenne.h:193
FixedSizeSecBlock< word32, N+1 > m_state
32-bit word state array of size N
Definition: mersenne.h:177
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
Mersenne Twister class for Monte-Carlo simulations.
Definition: mersenne.h:31
unsigned int m_idx
the current index into the state array
Definition: mersenne.h:181
Abstract base classes that provide a uniform interface to this library.
Interface for random number generators.
Definition: cryptlib.h:1188
ExecStats::duration min
Definition: ExecStats.cpp:35
Classes and functions for secure memory allocations.
ExecStats::duration max
Definition: ExecStats.cpp:36
T Crop(T value, size_t bits)
Truncates the value to the specified number of bits.
Definition: misc.h:737
void DiscardBytes(size_t n)
Generate and discard n bytes.
Definition: mersenne.h:120
unsigned int m_seed
the value used to seed the generator
Definition: mersenne.h:179
#define F(x, y, z)
Definition: Hash.cpp:79
Original MT19937 generator provided in the ACM paper.
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: mersenne.h:52
#define CRYPTOPP_ASSERT(exp)
Definition: trap.h:92
word32 NextMersenneWord()
Returns the next 32-bit word from the state array.
Definition: mersenne.h:132
MersenneTwister< 0x9908B0DF, 397, 624, 0x6C078965, 5489 > MT19937ar
Definition: mersenne.h:206
#define UL(i)
Updated MT19937 generator adapted to provide an array for initialization.
uint8_t const size_t const size
Definition: sha3.h:20
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
Definition: misc.h:905
#define NAMESPACE_END
Definition: config.h:201
#define S(a)
Definition: mars.cpp:50
#define CRYPTOPP_GET_BYTE_AS_BYTE(x, y)
Definition: misc.h:615
unsigned int word32
Definition: config.h:231
word32 GenerateWord32(word32 min=0, word32 max=0xffffffffL)
Generate a random 32-bit word in the range min to max, inclusive.
Definition: mersenne.h:98
byte ByteReverse(byte value)
Reverses bytes in a 8-bit value.
Definition: misc.h:1663
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
Definition: misc.h:654
MersenneTwister(unsigned long seed=S)
Construct a Mersenne Twister.
Definition: mersenne.h:38