From 7aff1e4cb053394db57c2814d5fe1e6493e0cc75 Mon Sep 17 00:00:00 2001 From: watcherhd Date: Sat, 26 Nov 2011 14:19:43 +0000 Subject: Project folders rename part 2 git-svn-id: http://miranda-plugins.googlecode.com/svn/trunk@214 e753b5eb-9565-29b2-b5c5-2cc6f99dfbcb --- CryptoPP/crypto/bench.cpp | 344 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 344 insertions(+) create mode 100644 CryptoPP/crypto/bench.cpp (limited to 'CryptoPP/crypto/bench.cpp') diff --git a/CryptoPP/crypto/bench.cpp b/CryptoPP/crypto/bench.cpp new file mode 100644 index 0000000..bbaa1e5 --- /dev/null +++ b/CryptoPP/crypto/bench.cpp @@ -0,0 +1,344 @@ +// bench.cpp - written and placed in the public domain by Wei Dai + +#define _CRT_SECURE_NO_DEPRECATE + +#include "bench.h" +#include "crc.h" +#include "adler32.h" +#include "idea.h" +#include "des.h" +#include "rc5.h" +#include "blowfish.h" +#include "wake.h" +#include "cast.h" +#include "seal.h" +#include "rc6.h" +#include "mars.h" +#include "twofish.h" +#include "serpent.h" +#include "skipjack.h" +#include "cbcmac.h" +#include "dmac.h" +#include "aes.h" +#include "blumshub.h" +#include "rng.h" +#include "files.h" +#include "hex.h" +#include "modes.h" +#include "factory.h" + +#include +#include +#include +#include + +USING_NAMESPACE(CryptoPP) +USING_NAMESPACE(std) + +#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 + +double logtotal = 0, g_allocatedTime, g_hertz; +unsigned int logcount = 0; + +static const byte *const key=(byte *)"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; + +void OutputResultBytes(const char *name, double length, double timeTaken) +{ + double mbs = length / timeTaken / (1024*1024); + cout << "\n" << name; +// cout << "" << setprecision(3) << length / (1024*1024); + cout << setiosflags(ios::fixed); +// cout << "" << setprecision(3) << timeTaken; + cout << "" << setprecision(0) << setiosflags(ios::fixed) << mbs; + if (g_hertz) + cout << "" << setprecision(1) << setiosflags(ios::fixed) << timeTaken * g_hertz / length; + cout << resetiosflags(ios::fixed); + logtotal += log(mbs); + logcount++; +} + +void OutputResultKeying(double iterations, double timeTaken) +{ + cout << "" << setprecision(3) << setiosflags(ios::fixed) << (1000*1000*timeTaken/iterations); + if (g_hertz) + cout << "" << setprecision(0) << setiosflags(ios::fixed) << timeTaken * g_hertz / iterations; +} + +void OutputResultOperations(const char *name, const char *operation, bool pc, unsigned long iterations, double timeTaken) +{ + cout << "\n" << name << " " << operation << (pc ? " with precomputation" : ""); +// cout << "" << iterations; +// cout << setiosflags(ios::fixed); +// cout << "" << setprecision(3) << timeTaken; + cout << "" << setprecision(2) << setiosflags(ios::fixed) << (1000*timeTaken/iterations); + if (g_hertz) + cout << "" << setprecision(2) << setiosflags(ios::fixed) << timeTaken * g_hertz / iterations / 1000000; + cout << resetiosflags(ios::fixed); + + logtotal += log(iterations/timeTaken); + logcount++; +} + +void BenchMark(const char *name, BlockTransformation &cipher, double timeTotal) +{ + const int BUF_SIZE = RoundUpToMultipleOf(2048U, cipher.OptimalNumberOfParallelBlocks() * cipher.BlockSize()); + AlignedSecByteBlock buf(BUF_SIZE); + const int nBlocks = BUF_SIZE / cipher.BlockSize(); + clock_t start = clock(); + + unsigned long i=0, blocks=1; + double timeTaken; + do + { + blocks *= 2; + for (; i +void BenchMarkKeyed(const char *name, double timeTotal, const NameValuePairs ¶ms = g_nullNameValuePairs, T *x=NULL) +{ + T c; + c.SetKey(key, c.DefaultKeyLength(), CombinedNameValuePairs(params, MakeParameters(Name::IV(), key, false))); + BenchMark(name, c, timeTotal); + BenchMarkKeying(c, c.DefaultKeyLength(), CombinedNameValuePairs(params, MakeParameters(Name::IV(), key, false))); +} + +//VC60 workaround: compiler bug triggered without the extra dummy parameters +template +void BenchMarkKeyedVariable(const char *name, double timeTotal, unsigned int keyLength, const NameValuePairs ¶ms = g_nullNameValuePairs, T *x=NULL) +{ + T c; + c.SetKey(key, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), key, false))); + BenchMark(name, c, timeTotal); + BenchMarkKeying(c, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), key, false))); +} + +//VC60 workaround: compiler bug triggered without the extra dummy parameters +template +void BenchMarkKeyless(const char *name, double timeTotal, T *x=NULL) +{ + T c; + BenchMark(name, c, timeTotal); +} + +//VC60 workaround: compiler bug triggered without the extra dummy parameters +template +void BenchMarkByName(const char *factoryName, size_t keyLength = 0, const char *displayName=NULL, const NameValuePairs ¶ms = g_nullNameValuePairs, T *x=NULL) +{ + std::string name = factoryName; + if (displayName) + name = displayName; + else if (keyLength) + name += " (" + IntToString(keyLength * 8) + "-bit key)"; + + std::auto_ptr obj(ObjectFactoryRegistry::Registry().CreateObject(factoryName)); + if (!keyLength) + keyLength = obj->DefaultKeyLength(); + obj->SetKey(key, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), key, false))); + BenchMark(name.c_str(), *obj, g_allocatedTime); + BenchMarkKeying(*obj, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), key, false))); +} + +template +void BenchMarkByNameKeyLess(const char *factoryName, const char *displayName=NULL, const NameValuePairs ¶ms = g_nullNameValuePairs, T *x=NULL) +{ + std::string name = factoryName; + if (displayName) + name = displayName; + + std::auto_ptr obj(ObjectFactoryRegistry::Registry().CreateObject(factoryName)); + BenchMark(name.c_str(), *obj, g_allocatedTime); +} + +void BenchmarkAll(double t, double hertz) +{ +#if 1 + logtotal = 0; + logcount = 0; + g_allocatedTime = t; + g_hertz = hertz; + + const char *cpb, *cpk; + if (g_hertz) + { + cpb = "Cycles Per Byte"; + cpk = "Cycles to
Setup Key and IV"; + cout << "CPU frequency of the test platform is " << g_hertz << " Hz.\n"; + } + else + { + cpb = cpk = ""; + cout << "CPU frequency of the test platform was not provided.\n"; + } + + cout << "" << endl; + cout << ""; + BenchMarkByName("VMAC(AES)-64"); + BenchMarkByName("VMAC(AES)-128"); + BenchMarkByName("HMAC(SHA-1)"); + BenchMarkByName("Two-Track-MAC"); + BenchMarkKeyed >("CBC-MAC/AES", t); + BenchMarkKeyed >("DMAC/AES", t); + + cout << "\n"; + BenchMarkKeyless("CRC-32", t); + BenchMarkKeyless("Adler-32", t); + BenchMarkByNameKeyLess("MD5"); + BenchMarkByNameKeyLess("SHA-1"); + BenchMarkByNameKeyLess("SHA-256"); +#ifdef WORD64_AVAILABLE + BenchMarkByNameKeyLess("SHA-512"); + BenchMarkByNameKeyLess("Tiger"); + BenchMarkByNameKeyLess("Whirlpool"); +#endif + BenchMarkByNameKeyLess("RIPEMD-160"); + BenchMarkByNameKeyLess("RIPEMD-320"); + BenchMarkByNameKeyLess("RIPEMD-128"); + BenchMarkByNameKeyLess("RIPEMD-256"); + + cout << "\n"; + BenchMarkByName("Panama-LE"); + BenchMarkByName("Panama-BE"); + BenchMarkByName("Salsa20"); + BenchMarkByName("Salsa20", 0, "Salsa20/12", MakeParameters(Name::Rounds(), 12)); + BenchMarkByName("Salsa20", 0, "Salsa20/8", MakeParameters(Name::Rounds(), 8)); + BenchMarkByName("Sosemanuk"); + BenchMarkByName("MARC4"); + BenchMarkKeyed::Encryption>("SEAL-3.0-BE", t); + BenchMarkKeyed::Encryption>("SEAL-3.0-LE", t); + BenchMarkKeyed::Encryption>("WAKE-OFB-BE", t); + BenchMarkKeyed::Encryption>("WAKE-OFB-LE", t); + + cout << "\n"; + BenchMarkByName("AES/ECB", 16); + BenchMarkByName("AES/ECB", 24); + BenchMarkByName("AES/ECB", 32); + BenchMarkByName("AES/CTR", 16); + BenchMarkByName("AES/OFB", 16); + BenchMarkByName("AES/CFB", 16); + BenchMarkByName("AES/CBC", 16); + BenchMarkByName("Camellia/ECB", 16); + BenchMarkByName("Camellia/ECB", 32); + BenchMarkKeyed("Twofish", t); + BenchMarkKeyed("Serpent", t); + BenchMarkKeyed("CAST-256", t); + BenchMarkKeyed("RC6", t); + BenchMarkKeyed("MARS", t); + BenchMarkByName("SHACAL-2/ECB", 16); + BenchMarkByName("SHACAL-2/ECB", 64); + BenchMarkKeyed("DES", t); + BenchMarkKeyed("DES-XEX3", t); + BenchMarkKeyed("DES-EDE3", t); + BenchMarkKeyed("IDEA", t); + BenchMarkKeyed("RC5 (r=16)", t); + BenchMarkKeyed("Blowfish", t); + BenchMarkByName("TEA/ECB"); + BenchMarkByName("XTEA/ECB"); + BenchMarkKeyed("CAST-128", t); + BenchMarkKeyed("SKIPJACK", t); + cout << "
AlgorithmMiB/Second" << cpb << "Microseconds to
Setup Key and IV" << cpk << endl; + + cout << "\n
" << endl; + + BenchmarkAll2(t, hertz); + + cout << "Throughput Geometric Average: " << setiosflags(ios::fixed) << exp(logtotal/logcount) << endl; + + time_t endTime = time(NULL); + cout << "\nTest ended at " << asctime(localtime(&endTime)); +#endif +} -- cgit v1.2.3