// bench1.cpp - originally written and placed in the public domain by Wei Dai
// CryptoPP::Test namespace added by JW in February 2017
#include "cryptlib.h"
#include "bench.h"
#include "validate.h"
#include "cpu.h"
#include "drbg.h"
#include "factory.h"
#include "algparam.h"
#include "argnames.h"
#include "smartptr.h"
#include "stdcpp.h"
#if CRYPTOPP_MSC_VERSION
# pragma warning(disable: 4355)
#endif
#if CRYPTOPP_MSC_VERSION
# pragma warning(disable: 4505 4355)
#endif
NAMESPACE_BEGIN(CryptoPP)
NAMESPACE_BEGIN(Test)
#ifdef CLOCKS_PER_SEC
const double CLOCK_TICKS_PER_SECOND = (double)CLOCKS_PER_SEC;
#elif defined(CLK_TCK)
const double CLOCK_TICKS_PER_SECOND = (double)CLK_TCK;
#else
const double CLOCK_TICKS_PER_SECOND = 1000000.0;
#endif
const byte defaultKey[] = "0123456789" // 168 + NULL
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
"00000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000";
double g_allocatedTime = 0.0, g_hertz = 0.0, g_logTotal = 0.0;
unsigned int g_logCount = 0;
time_t g_testBegin, g_testEnd;
void OutputResultBytes(const char *name, const char *provider, double length, double timeTaken)
{
// Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
StreamState ss(std::cout);
// Coverity finding
if (length < 0.000001f) length = 0.000001f;
if (timeTaken < 0.000001f) timeTaken = 0.000001f;
double mbs = length / timeTaken / (1024*1024);
std::cout << "\n
| " << name << " | " << provider;
std::cout << std::setiosflags(std::ios::fixed);
std::cout << " | " << std::setprecision(0) << std::setiosflags(std::ios::fixed) << mbs;
if (g_hertz > 1.0f)
{
const double cpb = timeTaken * g_hertz / length;
if (cpb < 24.0f)
std::cout << " | " << std::setprecision(2) << std::setiosflags(std::ios::fixed) << cpb;
else
std::cout << " | " << std::setprecision(1) << std::setiosflags(std::ios::fixed) << cpb;
}
g_logTotal += log(mbs);
g_logCount++;
}
void OutputResultKeying(double iterations, double timeTaken)
{
// Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
StreamState ss(std::cout);
// Coverity finding
if (iterations < 0.000001f) iterations = 0.000001f;
if (timeTaken < 0.000001f) timeTaken = 0.000001f;
std::cout << " | " << std::setprecision(3) << std::setiosflags(std::ios::fixed) << (1000*1000*timeTaken/iterations);
// Coverity finding
if (g_hertz > 1.0f)
std::cout << " | " << std::setprecision(0) << std::setiosflags(std::ios::fixed) << timeTaken * g_hertz / iterations;
}
void OutputResultOperations(const char *name, const char *provider, const char *operation, bool pc, unsigned long iterations, double timeTaken)
{
// Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
StreamState ss(std::cout);
// Coverity finding
if (!iterations) iterations++;
if (timeTaken < 0.000001f) timeTaken = 0.000001f;
std::cout << "\n |
| " << name << " " << operation << (pc ? " with precomputation" : "");
std::cout << " | " << provider;
std::cout << " | " << std::setprecision(2) << std::setiosflags(std::ios::fixed) << (1000*timeTaken/iterations);
// Coverity finding
if (g_hertz > 1.0f)
{
const double t = timeTaken * g_hertz / iterations / 1000000;
std::cout << " | " << std::setprecision(2) << std::setiosflags(std::ios::fixed) << t;
}
g_logTotal += log(iterations/timeTaken);
g_logCount++;
}
/*
void BenchMark(const char *name, BlockTransformation &cipher, double timeTotal)
{
const int BUF_SIZE = RoundUpToMultipleOf(2048U, cipher.OptimalNumberOfParallelBlocks() * cipher.BlockSize());
AlignedSecByteBlock buf(BUF_SIZE);
buf.SetMark(16);
const int nBlocks = BUF_SIZE / cipher.BlockSize();
unsigned long i=0, blocks=1;
double timeTaken;
clock_t start = ::clock();
do
{
blocks *= 2;
for (; i(&rng);
if (cipher != NULLPTR)
{
const size_t size = cipher->DefaultKeyLength();
if (cipher->IsResynchronizable())
cipher->SetKeyWithIV(buf, size, buf+size);
else
cipher->SetKey(buf, size);
}
unsigned long long blocks = 1;
double timeTaken;
clock_t start = ::clock();
do
{
rng.GenerateBlock(buf, buf.size());
blocks++;
timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
} while (timeTaken < timeTotal);
std::string provider = rng.AlgorithmProvider();
OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
}
// Hack, but we probably need a KeyedRandomNumberGenerator interface
// and a few methods to generalize keying a RNG. X917RNG, Hash_DRBG,
// HMAC_DRBG, AES/CFB RNG and a few others could use it. "A few others"
// includes BLAKE2, ChaCha and Poly1305 when used as a RNG.
void BenchMark(const char *name, NIST_DRBG &rng, double timeTotal)
{
const int BUF_SIZE = 2048U;
AlignedSecByteBlock buf(BUF_SIZE);
Test::GlobalRNG().GenerateBlock(buf, BUF_SIZE);
buf.SetMark(16);
rng.IncorporateEntropy(buf, rng.MinEntropyLength());
unsigned long long blocks = 1;
double timeTaken;
clock_t start = ::clock();
do
{
rng.GenerateBlock(buf, buf.size());
blocks++;
timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
} while (timeTaken < timeTotal);
std::string provider = rng.AlgorithmProvider();
OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
}
template
void BenchMarkByNameKeyLess(const char *factoryName, const char *displayName = NULLPTR, const NameValuePairs ¶ms = g_nullNameValuePairs)
{
CRYPTOPP_UNUSED(params);
std::string name = factoryName;
if (displayName)
name = displayName;
member_ptr obj(ObjectFactoryRegistry::Registry().CreateObject(factoryName));
BenchMark(name.c_str(), *obj, g_allocatedTime);
}
void AddHtmlHeader()
{
// HTML5
std::cout << "";
std::cout << "\n";
std::cout << "\n";
std::cout << "\n";
std::cout << "\nSpeed Comparison of Popular Crypto Algorithms";
std::cout << "\n";
std::cout << "\n";
std::cout << "\n";
std::cout << "\nCrypto++ " << CRYPTOPP_VERSION / 100;
std::cout << '.' << (CRYPTOPP_VERSION % 100) / 10 << '.' << CRYPTOPP_VERSION % 10 << " Benchmarks";
std::cout << "\nHere are speed benchmarks for some commonly used cryptographic algorithms. ";
if (g_hertz > 1.0f)
std::cout << "\nCPU frequency of the test platform is " << g_hertz << " Hz. ";
else
std::cout << "\nCPU frequency of the test platform was not provided. " << std::endl;
}
void AddHtmlFooter()
{
std::cout << "\n";
std::cout << "\n" << std::endl;
}
void BenchmarkWithCommand(int argc, const char* const argv[])
{
std::string command(argv[1]);
float runningTime(argc >= 3 ? Test::StringToValue(argv[2]) : 1.0f);
float cpuFreq(argc >= 4 ? Test::StringToValue(argv[3])*float(1e9) : 0.0f);
std::string algoName(argc >= 5 ? argv[4] : "");
if (command == "b") // All benchmarks
Benchmark(Test::All, runningTime, cpuFreq);
else if (command == "b3") // Public key algorithms
Test::Benchmark(Test::PublicKey, runningTime, cpuFreq);
else if (command == "b2") // Shared key algorithms
Test::Benchmark(Test::SharedKey, runningTime, cpuFreq);
else if (command == "b1") // Unkeyed algorithms
Test::Benchmark(Test::Unkeyed, runningTime, cpuFreq);
}
void Benchmark(Test::TestClass suites, double t, double hertz)
{
g_allocatedTime = t;
g_hertz = hertz;
AddHtmlHeader();
g_testBegin = ::time(NULLPTR);
if (static_cast(suites) == 0 || static_cast(suites) > TestLast)
suites = Test::All;
// Unkeyed algorithms
if (suites & Test::Unkeyed)
{
std::cout << "\n ";
Benchmark1(t, hertz);
}
// Shared key algorithms
if (suites & Test::SharedKey)
{
std::cout << "\n ";
Benchmark2(t, hertz);
}
// Public key algorithms
if (suites & Test::PublicKey)
{
std::cout << "\n ";
Benchmark3(t, hertz);
}
g_testEnd = ::time(NULLPTR);
{
StreamState state(std::cout);
std::cout << "\nThroughput Geometric Average: " << std::setiosflags(std::ios::fixed);
std::cout << std::exp(g_logTotal/(g_logCount > 0.0f ? g_logCount : 1.0f)) << std::endl;
}
std::cout << "\n Test started at " << TimeToString(g_testBegin);
std::cout << "\n Test ended at " << TimeToString(g_testEnd);
std::cout << std::endl;
AddHtmlFooter();
}
void Benchmark1(double t, double hertz)
{
g_allocatedTime = t;
g_hertz = hertz;
const char *cpb;
if (g_hertz > 1.0f)
cpb = " Cycles Per Byte";
else
cpb = "";
std::cout << "\n";
std::cout << "\n";
std::cout << "";
std::cout << "\n";
std::cout << "\n| Algorithm | Provider | MiB/Second" << cpb;
std::cout << "\n | ";
{
#ifdef NONBLOCKING_RNG_AVAILABLE
BenchMarkByNameKeyLess("NonblockingRng");
#endif
#ifdef OS_RNG_AVAILABLE
BenchMarkByNameKeyLess("AutoSeededRandomPool");
BenchMarkByNameKeyLess("AutoSeededX917RNG(AES)");
#endif
BenchMarkByNameKeyLess("MT19937");
#if (CRYPTOPP_BOOL_X86)
if (HasPadlockRNG())
BenchMarkByNameKeyLess("PadlockRNG");
#endif
#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
if (HasRDRAND())
BenchMarkByNameKeyLess("RDRAND");
if (HasRDSEED())
BenchMarkByNameKeyLess("RDSEED");
#endif
BenchMarkByNameKeyLess("AES/OFB RNG");
BenchMarkByNameKeyLess("Hash_DRBG(SHA1)");
BenchMarkByNameKeyLess("Hash_DRBG(SHA256)");
BenchMarkByNameKeyLess("HMAC_DRBG(SHA1)");
BenchMarkByNameKeyLess("HMAC_DRBG(SHA256)");
}
std::cout << "\n";
{
BenchMarkByNameKeyLess("CRC32");
BenchMarkByNameKeyLess("CRC32C");
BenchMarkByNameKeyLess("Adler32");
BenchMarkByNameKeyLess("MD5");
BenchMarkByNameKeyLess("SHA-1");
BenchMarkByNameKeyLess("SHA-256");
BenchMarkByNameKeyLess("SHA-512");
BenchMarkByNameKeyLess("SHA3-224");
BenchMarkByNameKeyLess("SHA3-256");
BenchMarkByNameKeyLess("SHA3-384");
BenchMarkByNameKeyLess("SHA3-512");
BenchMarkByNameKeyLess("Keccak-224");
BenchMarkByNameKeyLess("Keccak-256");
BenchMarkByNameKeyLess("Keccak-384");
BenchMarkByNameKeyLess("Keccak-512");
BenchMarkByNameKeyLess("Tiger");
BenchMarkByNameKeyLess("Whirlpool");
BenchMarkByNameKeyLess("RIPEMD-160");
BenchMarkByNameKeyLess("RIPEMD-320");
BenchMarkByNameKeyLess("RIPEMD-128");
BenchMarkByNameKeyLess("RIPEMD-256");
BenchMarkByNameKeyLess("SM3");
BenchMarkByNameKeyLess("BLAKE2s");
BenchMarkByNameKeyLess("BLAKE2b");
}
std::cout << "\n " << std::endl;
}
NAMESPACE_END // Test
NAMESPACE_END // CryptoPP
| |