8#ifndef CRYPTOPP_MERSENNE_TWISTER_H
9#define CRYPTOPP_MERSENNE_TWISTER_H
30template <
unsigned int K,
unsigned int M,
unsigned int N,
unsigned int F, word32 S>
34 CRYPTOPP_STATIC_CONSTEXPR
const char* StaticAlgorithmName() {
return (S==5489 ?
"MT19937ar" : (S==4537 ?
"MT19937" :
"MT19937x")); }
63 for (
size_t i=0; i<length; ++i)
66 temp[0] = temp[0] | input[i];
83 for (
size_t i=0; i < size/4; i++, output += 4)
85 temp[0] = NextMersenneWord();
86 std::memcpy(output, temp+0, 4);
94 temp[0] = NextMersenneWord();
97 case 3: output[2] = CRYPTOPP_GET_BYTE_AS_BYTE(temp[0], 1);
98 case 2: output[1] = CRYPTOPP_GET_BYTE_AS_BYTE(temp[0], 2);
99 case 1: output[0] = CRYPTOPP_GET_BYTE_AS_BYTE(temp[0], 3);
break;
111 const word32 range = max-min;
112 if (range == 0xffffffffL)
113 return NextMersenneWord();
119 value =
Crop(NextMersenneWord(), maxBits);
120 }
while (value > range);
144 for (
unsigned int i = 1; i < N+1; i++)
145 m_state[i] =
word32(F * (m_state[i-1] ^ (m_state[i-1] >> 30)) + i);
154 if (m_idx >= N) { Twist(); }
156 word32 temp = m_state[m_idx++];
158 temp ^= (temp >> 11);
159 temp ^= (temp << 7) & 0x9D2C5680;
160 temp ^= (temp << 15) & 0xEFC60000;
162 return temp ^ (temp >> 18);
168 static const word32 magic[2]={0x0UL, K};
172 for (kk=0;kk<N-M;kk++)
174 temp = (m_state[kk] & 0x80000000)|(m_state[kk+1] & 0x7FFFFFFF);
175 m_state[kk] = m_state[kk+M] ^ (temp >> 1) ^ magic[temp & 0x1UL];
180 temp = (m_state[kk] & 0x80000000)|(m_state[kk+1] & 0x7FFFFFFF);
181 m_state[kk] = m_state[kk+(M-N)] ^ (temp >> 1) ^ magic[temp & 0x1UL];
184 temp = (m_state[N-1] & 0x80000000)|(m_state[0] & 0x7FFFFFFF);
185 m_state[N-1] = m_state[M-1] ^ (temp >> 1) ^ magic[temp & 0x1UL];
209#if CRYPTOPP_DOXYGEN_PROCESSING
223#if CRYPTOPP_DOXYGEN_PROCESSING
Fixed size stack-based SecBlock.
Original MT19937 generator provided in the ACM paper.
Updated MT19937 generator adapted to provide an array for initialization.
Mersenne Twister class for Monte-Carlo simulations.
void IncorporateEntropy(const byte *input, size_t length)
Update RNG state with additional unpredictable values.
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
void DiscardBytes(size_t n)
Generate and discard n bytes.
bool CanIncorporateEntropy() const
Determines if a generator can accept additional entropy.
MersenneTwister(word32 seed=S)
Construct a Mersenne Twister.
word32 GenerateWord32(word32 min=0, word32 max=0xffffffffL)
Generate a random 32-bit word in the range min to max, inclusive.
Interface for random number generators.
unsigned int word32
32-bit unsigned datatype
Abstract base classes that provide a uniform interface to this library.
Utility functions for the Crypto++ library.
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
void SecureWipeArray(T *buf, size_t n)
Sets each element of an array to 0.
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
T Crop(T value, size_t bits)
Truncates the value to the specified number of bits.
Crypto++ library namespace.
Classes and functions for secure memory allocations.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.