8 #ifndef CRYPTOPP_MERSENNE_TWISTER_H 9 #define CRYPTOPP_MERSENNE_TWISTER_H 28 template <
unsigned int K,
unsigned int M,
unsigned int N,
unsigned int F, word32 S>
32 CRYPTOPP_STATIC_CONSTEXPR
const char* StaticAlgorithmName() {
return (S==5489 ?
"MT19937ar" : (S==4537 ?
"MT19937" :
"MT19937x")); }
55 ::memcpy(&temp, input,
STDMIN(
sizeof(temp), length));
73 for (
size_t i=0; i < size/4; i++, output += 4)
75 temp = NextMersenneWord();
76 memcpy(output, &temp, 4);
88 temp = NextMersenneWord();
91 case 3: output[2] = CRYPTOPP_GET_BYTE_AS_BYTE(temp, 1);
92 case 2: output[1] = CRYPTOPP_GET_BYTE_AS_BYTE(temp, 2);
93 case 1: output[0] = CRYPTOPP_GET_BYTE_AS_BYTE(temp, 3);
break;
108 const word32 range = max-min;
109 if (range == 0xffffffffL)
110 return NextMersenneWord();
116 value =
Crop(NextMersenneWord(), maxBits);
117 }
while (value > range);
136 void Reset(word32 seed)
142 for (
unsigned int i = 1; i < N+1; i++)
143 m_state[i] = word32(F * (m_state[i-1] ^ (m_state[i-1] >> 30)) + i);
150 word32 NextMersenneWord()
152 if (m_idx >= N) { Twist(); }
154 word32 temp = m_state[m_idx++];
156 temp ^= (temp >> 11);
157 temp ^= (temp << 7) & 0x9D2C5680;
158 temp ^= (temp << 15) & 0xEFC60000;
160 return temp ^ (temp >> 18);
166 static const word32 magic[2]={0x0UL, K};
170 for (kk=0;kk<N-M;kk++)
172 temp = (m_state[kk] & 0x80000000)|(m_state[kk+1] & 0x7FFFFFFF);
173 m_state[kk] = m_state[kk+M] ^ (temp >> 1) ^ magic[temp & 0x1UL];
178 temp = (m_state[kk] & 0x80000000)|(m_state[kk+1] & 0x7FFFFFFF);
179 m_state[kk] = m_state[kk+(M-N)] ^ (temp >> 1) ^ magic[temp & 0x1UL];
182 temp = (m_state[N-1] & 0x80000000)|(m_state[0] & 0x7FFFFFFF);
183 m_state[N-1] = m_state[M-1] ^ (temp >> 1) ^ magic[temp & 0x1UL];
207 #if CRYPTOPP_DOXYGEN_PROCESSING 219 #if CRYPTOPP_DOXYGEN_PROCESSING 227 #endif // CRYPTOPP_MERSENNE_TWISTER_H Utility functions for the Crypto++ library.
Mersenne Twister class for Monte-Carlo simulations.
Abstract base classes that provide a uniform interface to this library.
Interface for random number generators.
MersenneTwister(word32 seed=S)
Construct a Mersenne Twister.
Classes and functions for secure memory allocations.
void SecureWipeArray(T *buf, size_t n)
Sets each element of an array to 0.
bool CanIncorporateEntropy() const
Determines if a generator can accept additional entropy.
T Crop(T value, size_t bits)
Truncates the value to the specified number of bits.
void DiscardBytes(size_t n)
Generate and discard n bytes.
Original MT19937 generator provided in the ACM paper.
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Updated MT19937 generator adapted to provide an array for initialization.
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
Crypto++ library namespace.
word32 GenerateWord32(word32 min=0, word32 max=0xffffffffL)
Generate a random 32-bit word in the range min to max, inclusive.
void IncorporateEntropy(const byte *input, size_t length)
Update RNG state with additional unpredictable values.
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.