From 490d36e65ac24e34e3021c2a0947384aee138c88 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Wed, 13 Apr 2016 21:05:14 -0400 Subject: Root commit for new Compass repository This is the Alpha version of ComPASS, originally developed sometime in 2014. --- cryptopp562/bench.cpp | 343 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 343 insertions(+) create mode 100644 cryptopp562/bench.cpp (limited to 'cryptopp562/bench.cpp') diff --git a/cryptopp562/bench.cpp b/cryptopp562/bench.cpp new file mode 100644 index 0000000..34c0d61 --- /dev/null +++ b/cryptopp562/bench.cpp @@ -0,0 +1,343 @@ +// bench.cpp - written and placed in the public domain by Wei Dai + +#define _CRT_SECURE_NO_DEPRECATE + +#include "bench.h" +#include "validate.h" +#include "aes.h" +#include "blumshub.h" +#include "files.h" +#include "hex.h" +#include "modes.h" +#include "factory.h" +#include "cpu.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(cipher), timeTotal); +} + +void BenchMark(const char *name, HashTransformation &ht, double timeTotal) +{ + const int BUF_SIZE=2048U; + AlignedSecByteBlock buf(BUF_SIZE); + GlobalRNG().GenerateBlock(buf, BUF_SIZE); + clock_t start = clock(); + + unsigned long i=0, blocks=1; + double timeTaken; + do + { + blocks *= 2; + for (; i +void BenchMarkByName2(const char *factoryName, size_t keyLength = 0, const char *displayName=NULL, const NameValuePairs ¶ms = g_nullNameValuePairs, T_FactoryOutput *x=NULL, T_Interface *y=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(), ConstByteArrayParameter(key, obj->IVSize()), false))); + BenchMark(name.c_str(), *static_cast(obj.get()), g_allocatedTime); + BenchMarkKeying(*obj, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(key, obj->IVSize()), false))); +} + +//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_FactoryOutput *x=NULL) +{ + BenchMarkByName2(factoryName, keyLength, displayName, params, x, x); +} + +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 << ""; +#if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE + if (HasCLMUL()) + BenchMarkByName2("AES/GCM", 0, "AES/GCM"); + else +#endif + { + BenchMarkByName2("AES/GCM", 0, "AES/GCM (2K tables)", MakeParameters(Name::TableSize(), 2048)); + BenchMarkByName2("AES/GCM", 0, "AES/GCM (64K tables)", MakeParameters(Name::TableSize(), 64*1024)); + } + BenchMarkByName2("AES/CCM"); + BenchMarkByName2("AES/EAX"); + + cout << "\n"; +#if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE + if (HasCLMUL()) + BenchMarkByName2("AES/GCM", 0, "GMAC(AES)"); + else +#endif + { + BenchMarkByName2("AES/GCM", 0, "GMAC(AES) (2K tables)", MakeParameters(Name::TableSize(), 2048)); + BenchMarkByName2("AES/GCM", 0, "GMAC(AES) (64K tables)", MakeParameters(Name::TableSize(), 64*1024)); + } + BenchMarkByName("VMAC(AES)-64"); + BenchMarkByName("VMAC(AES)-128"); + BenchMarkByName("HMAC(SHA-1)"); + BenchMarkByName("Two-Track-MAC"); + BenchMarkByName("CMAC(AES)"); + BenchMarkByName("DMAC(AES)"); + + cout << "\n"; + BenchMarkByNameKeyLess("CRC32"); + BenchMarkByNameKeyLess("Adler32"); + BenchMarkByNameKeyLess("MD5"); + BenchMarkByNameKeyLess("SHA-1"); + BenchMarkByNameKeyLess("SHA-256"); + BenchMarkByNameKeyLess("SHA-512"); + BenchMarkByNameKeyLess("SHA-3-224"); + BenchMarkByNameKeyLess("SHA-3-256"); + BenchMarkByNameKeyLess("SHA-3-384"); + BenchMarkByNameKeyLess("SHA-3-512"); + BenchMarkByNameKeyLess("Tiger"); + BenchMarkByNameKeyLess("Whirlpool"); + 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"); + BenchMarkByName("SEAL-3.0-LE"); + BenchMarkByName("WAKE-OFB-LE"); + + cout << "\n"; + BenchMarkByName("AES/CTR", 16); + BenchMarkByName("AES/CTR", 24); + BenchMarkByName("AES/CTR", 32); + BenchMarkByName("AES/CBC", 16); + BenchMarkByName("AES/CBC", 24); + BenchMarkByName("AES/CBC", 32); + BenchMarkByName("AES/OFB", 16); + BenchMarkByName("AES/CFB", 16); + BenchMarkByName("AES/ECB", 16); + BenchMarkByName("Camellia/CTR", 16); + BenchMarkByName("Camellia/CTR", 32); + BenchMarkByName("Twofish/CTR"); + BenchMarkByName("Serpent/CTR"); + BenchMarkByName("CAST-256/CTR"); + BenchMarkByName("RC6/CTR"); + BenchMarkByName("MARS/CTR"); + BenchMarkByName("SHACAL-2/CTR", 16); + BenchMarkByName("SHACAL-2/CTR", 64); + BenchMarkByName("DES/CTR"); + BenchMarkByName("DES-XEX3/CTR"); + BenchMarkByName("DES-EDE3/CTR"); + BenchMarkByName("IDEA/CTR"); + BenchMarkByName("RC5/CTR", 0, "RC5 (r=16)"); + BenchMarkByName("Blowfish/CTR"); + BenchMarkByName("TEA/CTR"); + BenchMarkByName("XTEA/CTR"); + BenchMarkByName("CAST-128/CTR"); + BenchMarkByName("SKIPJACK/CTR"); + BenchMarkByName("SEED/CTR", 0, "SEED/CTR (1/2 K table)"); + 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