diff options
| author | Jeffrey Walton <noloader@gmail.com> | 2017-05-05 19:00:17 -0400 |
|---|---|---|
| committer | Jeffrey Walton <noloader@gmail.com> | 2017-05-05 19:00:17 -0400 |
| commit | 9225ca09cb33711278cfb9d5cf8d87fdb2c1c460 (patch) | |
| tree | b1d2cd77475220bbdd005bcd825256ab12fbf48d /validat1.cpp | |
| parent | c1377b29550ddd336dde7285c82aff86bec8e7fa (diff) | |
| download | cryptopp-git-9225ca09cb33711278cfb9d5cf8d87fdb2c1c460.tar.gz | |
Updated MersenneTwister tests
The tests now include the first 10 elements of the sequence to ensure a properly implemented algorithm and endianess correctness.
Diffstat (limited to 'validat1.cpp')
| -rw-r--r-- | validat1.cpp | 106 |
1 files changed, 93 insertions, 13 deletions
diff --git a/validat1.cpp b/validat1.cpp index 15743f12..2dc48572 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -45,6 +45,7 @@ #include "osrng.h"
#include "drbg.h"
#include "rdrand.h"
+#include "mersenne.h"
#include "zdeflate.h"
#include "smartptr.h"
#include "channels.h"
@@ -72,7 +73,9 @@ bool ValidateAll(bool thorough) pass=TestAutoSeeded() && pass;
pass=TestAutoSeededX917() && pass;
// pass=TestSecRandom() && pass;
-
+#if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
+ pass=TestMersenne() && pass;
+#endif
#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
pass=TestRDRAND() && pass;
pass=TestRDSEED() && pass;
@@ -96,7 +99,6 @@ bool ValidateAll(bool thorough) // Additional tests due to no coverage
pass=TestGzip() && pass;
pass=TestZinflate() && pass;
- pass=TestMersenne() && pass;
pass=TestDefaultEncryptor() && pass;
pass=TestDefaultEncryptorWithMAC() && pass;
pass=TestLegacyEncryptor() && pass;
@@ -449,7 +451,7 @@ bool TestOS_RNG() }
else
std::cout << "passed:";
- std::cout << " " << total << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << std::endl;
+ std::cout << " " << total << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
}
else
std::cout << "\nNo operating system provided blocking random number generator, skipping test." << std::endl;
@@ -474,7 +476,7 @@ bool TestOS_RNG() }
else
std::cout << "passed:";
- std::cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << std::endl;
+ std::cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
}
else
std::cout << "\nNo operating system provided nonblocking random number generator, skipping test." << std::endl;
@@ -511,7 +513,7 @@ bool TestAutoSeeded() }
else
std::cout << "passed:";
- std::cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << std::endl;
+ std::cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
try
{
@@ -575,7 +577,7 @@ bool TestAutoSeededX917() }
else
std::cout << "passed:";
- std::cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << std::endl;
+ std::cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
try
{
@@ -621,6 +623,90 @@ bool TestAutoSeededX917() }
#endif // NO_OS_DEPENDENCE
+#if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
+bool TestMersenne()
+{
+ std::cout << "\nTesting Mersenne Twister...\n\n";
+
+ static const unsigned int ENTROPY_SIZE = 32;
+ bool equal = true, generate = true, discard = true, incorporate = false;
+
+ // First 10; http://create.stephan-brumme.com/mersenne-twister/
+ word32 result[10], expected[10] = {0xD091BB5C, 0x22AE9EF6,
+ 0xE7E1FAEE, 0xD5C31F79, 0x2082352C, 0xF807B7DF, 0xE9D30005,
+ 0x3895AFE1, 0xA1E24BBA, 0x4EE4092B};
+
+ MT19937ar prng;
+ prng.GenerateBlock(reinterpret_cast<byte*>(result), sizeof(result));
+ equal = (0 == ::memcmp(result, expected, sizeof(expected)));
+
+ if (equal)
+ {
+ std::cout << "passed:";
+ }
+ else
+ {
+ std::cout << "FAILED:";
+ equal = false;
+ }
+ std::cout << " Expected sequence from MT19937ar (2002 version)\n";
+
+ MeterFilter meter(new Redirector(TheBitBucket()));
+ RandomNumberSource test(prng, 100000, true, new Deflator(new Redirector(meter)));
+
+ if (meter.GetTotalBytes() < 100000)
+ {
+ std::cout << "FAILED:";
+ generate = false;
+ }
+ else
+ std::cout << "passed:";
+ std::cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
+
+ try
+ {
+ prng.DiscardBytes(100000);
+ }
+ catch(const Exception&)
+ {
+ discard = false;
+ }
+
+ if (!discard)
+ std::cout << "FAILED:";
+ else
+ std::cout << "passed:";
+ std::cout << " discarded 10000 bytes\n";
+
+ try
+ {
+ if(prng.CanIncorporateEntropy())
+ {
+ SecByteBlock entropy(ENTROPY_SIZE);
+ OS_GenerateRandomBlock(false, entropy, entropy.SizeInBytes());
+
+ prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
+ prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
+ prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
+ prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
+
+ incorporate = true;
+ }
+ }
+ catch(const Exception& /*ex*/)
+ {
+ }
+
+ if (!incorporate)
+ std::cout << "FAILED:";
+ else
+ std::cout << "passed:";
+ std::cout << " IncorporateEntropy with " << 4*ENTROPY_SIZE << " bytes" << std::endl;
+
+ return equal && generate && discard && incorporate;
+}
+#endif
+
#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
bool TestRDRAND()
{
@@ -657,7 +743,7 @@ bool TestRDRAND() // Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
StreamState ss(std::cout);
std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(6);
- std::cout << " Maurer Randomness Test returned value " << mv << std::endl;
+ std::cout << " Maurer Randomness Test returned value " << mv << "\n";
if (meter.GetTotalBytes() < SIZE)
{
@@ -691,9 +777,6 @@ bool TestRDRAND() (void)rdrand.CanIncorporateEntropy();
rdrand.IncorporateEntropy(NULLPTR, 0);
- if (!(entropy && compress && discard))
- std::cout.flush();
-
return entropy && compress && discard;
}
#endif
@@ -769,9 +852,6 @@ bool TestRDSEED() (void)rdseed.CanIncorporateEntropy();
rdseed.IncorporateEntropy(NULLPTR, 0);
- if (!(entropy && compress && discard))
- std::cout.flush();
-
return entropy && compress && discard;
}
#endif
|
