- Main Page
- Namespaces
- Classes
- Files

# bench.cpp

    00001 // bench.cpp - written and placed in the public domain by Wei Dai
    00002
    00003 #define _CRT_SECURE_NO_DEPRECATE
    00004
    00005 #include "bench.h"
    00006 #include "aes.h"
    00007 #include "blumshub.h"
    00008 #include "rng.h"
    00009 #include "files.h"
    00010 #include "hex.h"
    00011 #include "modes.h"
    00012 #include "factory.h"
    00013
    00014 #include <time.h>
    00015 #include <math.h>
    00016 #include <iostream>
    00017 #include <iomanip>
    00018
    00019 USING_NAMESPACE(CryptoPP)
    00020 USING_NAMESPACE(std)
    00021
    00022 #ifdef CLOCKS_PER_SEC
    00023 const double CLOCK_TICKS_PER_SECOND = (double)CLOCKS_PER_SEC;
    00024 #elif defined(CLK_TCK)
    00025 const double CLOCK_TICKS_PER_SECOND = (double)CLK_TCK;
    00026 #else
    00027 const double CLOCK_TICKS_PER_SECOND = 1000000.0;
    00028 #endif
    00029
    00030 double logtotal = 0, g_allocatedTime, g_hertz;
    00031 unsigned int logcount = 0;
    00032
    00033 static const byte *const key=(byte *)"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
    00034
    00035 void OutputResultBytes(const char *name, double length, double timeTaken)
    00036 {
    00037         double mbs = length / timeTaken / (1024*1024);
    00038         cout << "\n<TR><TH>" << name;
    00039 //      cout << "<TD>" << setprecision(3) << length / (1024*1024);
    00040         cout << setiosflags(ios::fixed);
    00041 //      cout << "<TD>" << setprecision(3) << timeTaken;
    00042         cout << "<TD>" << setprecision(0) << setiosflags(ios::fixed) << mbs;
    00043         if (g_hertz)
    00044                 cout << "<TD>" << setprecision(1) << setiosflags(ios::fixed) << timeTaken * g_hertz / length;
    00045         cout << resetiosflags(ios::fixed);
    00046         logtotal += log(mbs);
    00047         logcount++;
    00048 }
    00049
    00050 void OutputResultKeying(double iterations, double timeTaken)
    00051 {
    00052         cout << "<TD>" << setprecision(3) << setiosflags(ios::fixed) << (1000*1000*timeTaken/iterations);
    00053         if (g_hertz)
    00054                 cout << "<TD>" << setprecision(0) << setiosflags(ios::fixed) << timeTaken * g_hertz / iterations;
    00055 }
    00056
    00057 void OutputResultOperations(const char *name, const char *operation, bool pc, unsigned long iterations, double timeTaken)
    00058 {
    00059         cout << "\n<TR><TH>" << name << " " << operation << (pc ? " with precomputation" : "");
    00060 //      cout << "<TD>" << iterations;
    00061 //      cout << setiosflags(ios::fixed);
    00062 //      cout << "<TD>" << setprecision(3) << timeTaken;
    00063         cout << "<TD>" << setprecision(2) << setiosflags(ios::fixed) << (1000*timeTaken/iterations);
    00064         if (g_hertz)
    00065                 cout << "<TD>" << setprecision(2) << setiosflags(ios::fixed) << timeTaken * g_hertz / iterations / 1000000;
    00066         cout << resetiosflags(ios::fixed);
    00067
    00068         logtotal += log(iterations/timeTaken);
    00069         logcount++;
    00070 }
    00071
    00072 /*
    00073 void BenchMark(const char *name, BlockTransformation &cipher, double timeTotal)
    00074 {
    00075         const int BUF_SIZE = RoundUpToMultipleOf(2048U, cipher.OptimalNumberOfParallelBlocks() * cipher.BlockSize());
    00076         AlignedSecByteBlock buf(BUF_SIZE);
    00077         const int nBlocks = BUF_SIZE / cipher.BlockSize();
    00078         clock_t start = clock();
    00079
    00080         unsigned long i=0, blocks=1;
    00081         double timeTaken;
    00082         do
    00083         {
    00084                 blocks *= 2;
    00085                 for (; i<blocks; i++)
    00086                         cipher.ProcessAndXorMultipleBlocks(buf, NULL, buf, nBlocks);
    00087                 timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
    00088         }
    00089         while (timeTaken < 2.0/3*timeTotal);
    00090
    00091         OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
    00092 }
    00093 */
    00094
    00095 void BenchMark(const char *name, StreamTransformation &cipher, double timeTotal)
    00096 {
    00097         const int BUF_SIZE=RoundUpToMultipleOf(2048U, cipher.OptimalBlockSize());
    00098         AlignedSecByteBlock buf(BUF_SIZE);
    00099         clock_t start = clock();
    00100
    00101         unsigned long i=0, blocks=1;
    00102         double timeTaken;
    00103         do
    00104         {
    00105                 blocks *= 2;
    00106                 for (; i<blocks; i++)
    00107                         cipher.ProcessString(buf, BUF_SIZE);
    00108                 timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
    00109         }
    00110         while (timeTaken < 2.0/3*timeTotal);
    00111
    00112         OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
    00113 }
    00114
    00115 void BenchMark(const char *name, HashTransformation &ht, double timeTotal)
    00116 {
    00117         const int BUF_SIZE=2048U;
    00118         AlignedSecByteBlock buf(BUF_SIZE);
    00119         LC_RNG rng((word32)time(NULL));
    00120         rng.GenerateBlock(buf, BUF_SIZE);
    00121         clock_t start = clock();
    00122
    00123         unsigned long i=0, blocks=1;
    00124         double timeTaken;
    00125         do
    00126         {
    00127                 blocks *= 2;
    00128                 for (; i<blocks; i++)
    00129                         ht.Update(buf, BUF_SIZE);
    00130                 timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
    00131         }
    00132         while (timeTaken < 2.0/3*timeTotal);
    00133
    00134         OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
    00135 }
    00136
    00137 void BenchMark(const char *name, BufferedTransformation &bt, double timeTotal)
    00138 {
    00139         const int BUF_SIZE=2048U;
    00140         AlignedSecByteBlock buf(BUF_SIZE);
    00141         LC_RNG rng((word32)time(NULL));
    00142         rng.GenerateBlock(buf, BUF_SIZE);
    00143         clock_t start = clock();
    00144
    00145         unsigned long i=0, blocks=1;
    00146         double timeTaken;
    00147         do
    00148         {
    00149                 blocks *= 2;
    00150                 for (; i<blocks; i++)
    00151                         bt.Put(buf, BUF_SIZE);
    00152                 timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
    00153         }
    00154         while (timeTaken < 2.0/3*timeTotal);
    00155
    00156         OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
    00157 }
    00158
    00159 void BenchMarkKeying(SimpleKeyingInterface &c, size_t keyLength, const NameValuePairs &params)
    00160 {
    00161         unsigned long iterations = 0;
    00162         clock_t start = clock();
    00163         double timeTaken;
    00164         do
    00165         {
    00166                 for (unsigned int i=0; i<1024; i++)
    00167                         c.SetKey(key, keyLength, params);
    00168                 timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
    00169                 iterations += 1024;
    00170         }
    00171         while (timeTaken < g_allocatedTime);
    00172
    00173         OutputResultKeying(iterations, timeTaken);
    00174 }
    00175
    00176 //VC60 workaround: compiler bug triggered without the extra dummy parameters
    00177 // on VC60 also needs to be named differently from BenchMarkByName
    00178 template <class T_FactoryOutput, class T_Interface>
    00179 void BenchMarkByName2(const char *factoryName, size_t keyLength = 0, const char *displayName=NULL, const NameValuePairs &params = g_nullNameValuePairs, T_FactoryOutput *x=NULL, T_Interface *y=NULL)
    00180 {
    00181         std::string name = factoryName;
    00182         if (displayName)
    00183                 name = displayName;
    00184         else if (keyLength)
    00185                 name += " (" + IntToString(keyLength * 8) + "-bit key)";
    00186
    00187         std::auto_ptr<T_FactoryOutput> obj(ObjectFactoryRegistry<T_FactoryOutput>::Registry().CreateObject(factoryName));
    00188         if (!keyLength)
    00189                 keyLength = obj->DefaultKeyLength();
    00190         obj->SetKey(key, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(key, obj->IVSize()), false)));
    00191         BenchMark(name.c_str(), *static_cast<T_Interface *>(obj.get()), g_allocatedTime);
    00192         BenchMarkKeying(*obj, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(key, obj->IVSize()), false)));
    00193 }
    00194
    00195 //VC60 workaround: compiler bug triggered without the extra dummy parameters
    00196 template <class T_FactoryOutput>
    00197 void BenchMarkByName(const char *factoryName, size_t keyLength = 0, const char *displayName=NULL, const NameValuePairs &params = g_nullNameValuePairs, T_FactoryOutput *x=NULL)
    00198 {
    00199         BenchMarkByName2<T_FactoryOutput, T_FactoryOutput>(factoryName, keyLength, displayName, params, x, x);
    00200 }
    00201
    00202 template <class T>
    00203 void BenchMarkByNameKeyLess(const char *factoryName, const char *displayName=NULL, const NameValuePairs &params = g_nullNameValuePairs, T *x=NULL)
    00204 {
    00205         std::string name = factoryName;
    00206         if (displayName)
    00207                 name = displayName;
    00208
    00209         std::auto_ptr<T> obj(ObjectFactoryRegistry<T>::Registry().CreateObject(factoryName));
    00210         BenchMark(name.c_str(), *obj, g_allocatedTime);
    00211 }
    00212
    00213 void BenchmarkAll(double t, double hertz)
    00214 {
    00215 #if 1
    00216         logtotal = 0;
    00217         logcount = 0;
    00218         g_allocatedTime = t;
    00219         g_hertz = hertz;
    00220
    00221         const char *cpb, *cpk;
    00222         if (g_hertz)
    00223         {
    00224                 cpb = "<TH>Cycles Per Byte";
    00225                 cpk = "<TH>Cycles to<br>Setup Key and IV";
    00226                 cout << "CPU frequency of the test platform is " << g_hertz << " Hz.\n";
    00227         }
    00228         else
    00229         {
    00230                 cpb = cpk = "";
    00231                 cout << "CPU frequency of the test platform was not provided.\n";
    00232         }
    00233
    00234         cout << "<TABLE border=1><COLGROUP><COL align=left><COL align=right><COL align=right><COL align=right><COL align=right>" << endl;
    00235         cout << "<THEAD><TR><TH>Algorithm<TH>MiB/Second" << cpb << "<TH>Microseconds to<br>Setup Key and IV" << cpk << endl;
    00236
    00237         cout << "\n<TBODY style=\"background: yellow\">";
    00238         BenchMarkByName2<AuthenticatedSymmetricCipher, StreamTransformation>("AES/GCM", 0, "AES/GCM (2K tables)", MakeParameters(Name::TableSize(), 2048));
    00239         BenchMarkByName2<AuthenticatedSymmetricCipher, StreamTransformation>("AES/GCM", 0, "AES/GCM (64K tables)", MakeParameters(Name::TableSize(), 64*1024));
    00240         BenchMarkByName2<AuthenticatedSymmetricCipher, StreamTransformation>("AES/CCM");
    00241         BenchMarkByName2<AuthenticatedSymmetricCipher, StreamTransformation>("AES/EAX");
    00242
    00243         cout << "\n<TBODY style=\"background: white\">";
    00244         BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES) (2K tables)", MakeParameters(Name::TableSize(), 2048));
    00245         BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES) (64K tables)", MakeParameters(Name::TableSize(), 64*1024));
    00246         BenchMarkByName<MessageAuthenticationCode>("VMAC(AES)-64");
    00247         BenchMarkByName<MessageAuthenticationCode>("VMAC(AES)-128");
    00248         BenchMarkByName<MessageAuthenticationCode>("HMAC(SHA-1)");
    00249         BenchMarkByName<MessageAuthenticationCode>("Two-Track-MAC");
    00250         BenchMarkByName<MessageAuthenticationCode>("CMAC(AES)");
    00251         BenchMarkByName<MessageAuthenticationCode>("DMAC(AES)");
    00252
    00253         cout << "\n<TBODY style=\"background: yellow\">";
    00254         BenchMarkByNameKeyLess<HashTransformation>("CRC32");
    00255         BenchMarkByNameKeyLess<HashTransformation>("Adler32");
    00256         BenchMarkByNameKeyLess<HashTransformation>("MD5");
    00257         BenchMarkByNameKeyLess<HashTransformation>("SHA-1");
    00258         BenchMarkByNameKeyLess<HashTransformation>("SHA-256");
    00259         BenchMarkByNameKeyLess<HashTransformation>("SHA-512");
    00260         BenchMarkByNameKeyLess<HashTransformation>("Tiger");
    00261         BenchMarkByNameKeyLess<HashTransformation>("Whirlpool");
    00262         BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-160");
    00263         BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-320");
    00264         BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-128");
    00265         BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-256");
    00266
    00267         cout << "\n<TBODY style=\"background: white\">";
    00268         BenchMarkByName<SymmetricCipher>("Panama-LE");
    00269         BenchMarkByName<SymmetricCipher>("Panama-BE");
    00270         BenchMarkByName<SymmetricCipher>("Salsa20");
    00271         BenchMarkByName<SymmetricCipher>("Salsa20", 0, "Salsa20/12", MakeParameters(Name::Rounds(), 12));
    00272         BenchMarkByName<SymmetricCipher>("Salsa20", 0, "Salsa20/8", MakeParameters(Name::Rounds(), 8));
    00273         BenchMarkByName<SymmetricCipher>("Sosemanuk");
    00274         BenchMarkByName<SymmetricCipher>("MARC4");
    00275         BenchMarkByName<SymmetricCipher>("SEAL-3.0-LE");
    00276         BenchMarkByName<SymmetricCipher>("WAKE-OFB-LE");
    00277
    00278         cout << "\n<TBODY style=\"background: yellow\">";
    00279         BenchMarkByName<SymmetricCipher>("AES/CTR", 16);
    00280         BenchMarkByName<SymmetricCipher>("AES/CTR", 24);
    00281         BenchMarkByName<SymmetricCipher>("AES/CTR", 32);
    00282         BenchMarkByName<SymmetricCipher>("AES/CBC", 16);
    00283         BenchMarkByName<SymmetricCipher>("AES/CBC", 24);
    00284         BenchMarkByName<SymmetricCipher>("AES/CBC", 32);
    00285         BenchMarkByName<SymmetricCipher>("AES/OFB", 16);
    00286         BenchMarkByName<SymmetricCipher>("AES/CFB", 16);
    00287         BenchMarkByName<SymmetricCipher>("AES/ECB", 16);
    00288         BenchMarkByName<SymmetricCipher>("Camellia/CTR", 16);
    00289         BenchMarkByName<SymmetricCipher>("Camellia/CTR", 32);
    00290         BenchMarkByName<SymmetricCipher>("Twofish/CTR");
    00291         BenchMarkByName<SymmetricCipher>("Serpent/CTR");
    00292         BenchMarkByName<SymmetricCipher>("CAST-256/CTR");
    00293         BenchMarkByName<SymmetricCipher>("RC6/CTR");
    00294         BenchMarkByName<SymmetricCipher>("MARS/CTR");
    00295         BenchMarkByName<SymmetricCipher>("SHACAL-2/CTR", 16);
    00296         BenchMarkByName<SymmetricCipher>("SHACAL-2/CTR", 64);
    00297         BenchMarkByName<SymmetricCipher>("DES/CTR");
    00298         BenchMarkByName<SymmetricCipher>("DES-XEX3/CTR");
    00299         BenchMarkByName<SymmetricCipher>("DES-EDE3/CTR");
    00300         BenchMarkByName<SymmetricCipher>("IDEA/CTR");
    00301         BenchMarkByName<SymmetricCipher>("RC5/CTR", 0, "RC5 (r=16)");
    00302         BenchMarkByName<SymmetricCipher>("Blowfish/CTR");
    00303         BenchMarkByName<SymmetricCipher>("TEA/CTR");
    00304         BenchMarkByName<SymmetricCipher>("XTEA/CTR");
    00305         BenchMarkByName<SymmetricCipher>("CAST-128/CTR");
    00306         BenchMarkByName<SymmetricCipher>("SKIPJACK/CTR");
    00307         BenchMarkByName<SymmetricCipher>("SEED/CTR", 0, "SEED/CTR (1/2 K table)");
    00308         cout << "</TABLE>" << endl;
    00309
    00310         BenchmarkAll2(t, hertz);
    00311
    00312         cout << "Throughput Geometric Average: " << setiosflags(ios::fixed) << exp(logtotal/logcount) << endl;
    00313
    00314         time_t endTime = time(NULL);
    00315         cout << "\nTest ended at " << asctime(localtime(&endTime));
    00316 #endif
    00317 }