Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

# bench.cpp

    00001 // bench.cpp - written and placed in the public domain by Wei Dai
    00002
    00003 #include "bench.h"
    00004 #include "crc.h"
    00005 #include "adler32.h"
    00006 #include "md2.h"
    00007 #include "md5.h"
    00008 #include "md5mac.h"
    00009 #include "sha.h"
    00010 #include "haval.h"
    00011 #include "tiger.h"
    00012 #include "ripemd.h"
    00013 #include "panama.h"
    00014 #include "whrlpool.h"
    00015 #include "idea.h"
    00016 #include "des.h"
    00017 #include "rc2.h"
    00018 #include "arc4.h"
    00019 #include "rc5.h"
    00020 #include "blowfish.h"
    00021 #include "wake.h"
    00022 #include "3way.h"
    00023 #include "safer.h"
    00024 #include "gost.h"
    00025 #include "shark.h"
    00026 #include "cast.h"
    00027 #include "square.h"
    00028 #include "skipjack.h"
    00029 #include "seal.h"
    00030 #include "rc6.h"
    00031 #include "mars.h"
    00032 #include "rijndael.h"
    00033 #include "twofish.h"
    00034 #include "serpent.h"
    00035 #include "shacal2.h"
    00036 #include "camellia.h"
    00037 #include "hmac.h"
    00038 #include "xormac.h"
    00039 #include "cbcmac.h"
    00040 #include "dmac.h"
    00041 #include "ttmac.h"
    00042 #include "blumshub.h"
    00043 #include "rng.h"
    00044 #include "files.h"
    00045 #include "hex.h"
    00046 #include "modes.h"
    00047 #include "mdc.h"
    00048 #include "lubyrack.h"
    00049 #include "tea.h"
    00050
    00051 #include <time.h>
    00052 #include <math.h>
    00053 #include <iostream>
    00054 #include <iomanip>
    00055
    00056 USING_NAMESPACE(CryptoPP)
    00057 USING_NAMESPACE(std)
    00058
    00059 #ifdef CLOCKS_PER_SEC
    00060 const double CLOCK_TICKS_PER_SECOND = (double)CLOCKS_PER_SEC;
    00061 #elif defined(CLK_TCK)
    00062 const double CLOCK_TICKS_PER_SECOND = (double)CLK_TCK;
    00063 #else
    00064 const double CLOCK_TICKS_PER_SECOND = 1000000.0;
    00065 #endif
    00066
    00067 double logtotal = 0;
    00068 unsigned int logcount = 0;
    00069
    00070 static const byte *const key=(byte *)"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
    00071
    00072 void OutputResultBytes(const char *name, double length, double timeTaken)
    00073 {
    00074         double mbs = length / timeTaken / (1024*1024);
    00075         cout << "<TR><TH>" << name;
    00076         cout << "<TD>" << setprecision(3) << length / (1024*1024);
    00077         cout << setiosflags(ios::fixed);
    00078         cout << "<TD>" << setprecision(3) << timeTaken;
    00079         cout << "<TD>" << setprecision(3) << mbs << endl;
    00080         cout << resetiosflags(ios::fixed);
    00081         logtotal += log(mbs);
    00082         logcount++;
    00083 }
    00084
    00085 void OutputResultOperations(const char *name, const char *operation, bool pc, unsigned long iterations, double timeTaken)
    00086 {
    00087         cout << "<TR><TH>" << name << " " << operation << (pc ? " with precomputation" : "");
    00088         cout << "<TD>" << iterations;
    00089         cout << setiosflags(ios::fixed);
    00090         cout << "<TD>" << setprecision(3) << timeTaken;
    00091         cout << "<TD>" << setprecision(2) << (1000*timeTaken/iterations) << endl;
    00092         cout << resetiosflags(ios::fixed);
    00093
    00094         logtotal += log(iterations/timeTaken);
    00095         logcount++;
    00096 }
    00097
    00098 void BenchMark(const char *name, BlockTransformation &cipher, double timeTotal)
    00099 {
    00100         const int BUF_SIZE = RoundDownToMultipleOf(1024U, cipher.OptimalNumberOfParallelBlocks() * cipher.BlockSize());
    00101         SecByteBlock buf(BUF_SIZE);
    00102         const int nBlocks = BUF_SIZE / cipher.BlockSize();
    00103         clock_t start = clock();
    00104
    00105         unsigned long i=0, blocks=1;
    00106         double timeTaken;
    00107         do
    00108         {
    00109                 blocks *= 2;
    00110                 for (; i<blocks; i++)
    00111                         cipher.ProcessAndXorMultipleBlocks(buf, NULL, buf, nBlocks);
    00112                 timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
    00113         }
    00114         while (timeTaken < 2.0/3*timeTotal);
    00115
    00116         OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
    00117 }
    00118
    00119 void BenchMark(const char *name, StreamTransformation &cipher, double timeTotal)
    00120 {
    00121         const int BUF_SIZE=1024;
    00122         SecByteBlock buf(BUF_SIZE);
    00123         clock_t start = clock();
    00124
    00125         unsigned long i=0, blocks=1;
    00126         double timeTaken;
    00127         do
    00128         {
    00129                 blocks *= 2;
    00130                 for (; i<blocks; i++)
    00131                         cipher.ProcessString(buf, BUF_SIZE);
    00132                 timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
    00133         }
    00134         while (timeTaken < 2.0/3*timeTotal);
    00135
    00136         OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
    00137 }
    00138
    00139 void BenchMark(const char *name, HashTransformation &ht, double timeTotal)
    00140 {
    00141         const int BUF_SIZE=1024;
    00142         SecByteBlock buf(BUF_SIZE);
    00143         LC_RNG rng(time(NULL));
    00144         rng.GenerateBlock(buf, BUF_SIZE);
    00145         clock_t start = clock();
    00146
    00147         unsigned long i=0, blocks=1;
    00148         double timeTaken;
    00149         do
    00150         {
    00151                 blocks *= 2;
    00152                 for (; i<blocks; i++)
    00153                         ht.Update(buf, BUF_SIZE);
    00154                 timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
    00155         }
    00156         while (timeTaken < 2.0/3*timeTotal);
    00157
    00158         OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
    00159 }
    00160
    00161 void BenchMark(const char *name, BufferedTransformation &bt, double timeTotal)
    00162 {
    00163         const int BUF_SIZE=1024;
    00164         SecByteBlock buf(BUF_SIZE);
    00165         LC_RNG rng(time(NULL));
    00166         rng.GenerateBlock(buf, BUF_SIZE);
    00167         clock_t start = clock();
    00168
    00169         unsigned long i=0, blocks=1;
    00170         double timeTaken;
    00171         do
    00172         {
    00173                 blocks *= 2;
    00174                 for (; i<blocks; i++)
    00175                         bt.Put(buf, BUF_SIZE);
    00176                 timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
    00177         }
    00178         while (timeTaken < 2.0/3*timeTotal);
    00179
    00180         OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
    00181 }
    00182
    00183 //VC60 workaround: compiler bug triggered without the extra dummy parameters
    00184 template <class T>
    00185 void BenchMarkKeyed(const char *name, double timeTotal, T *x=NULL)
    00186 {
    00187         T c;
    00188         c.SetKey(key, c.DefaultKeyLength(), MakeParameters(Name::IV(), key, false));
    00189         BenchMark(name, c, timeTotal);
    00190 }
    00191
    00192 //VC60 workaround: compiler bug triggered without the extra dummy parameters
    00193 template <class T>
    00194 void BenchMarkKeyedVariable(const char *name, double timeTotal, unsigned int keyLength, T *x=NULL)
    00195 {
    00196         T c;
    00197         c.SetKey(key, keyLength, MakeParameters(Name::IV(), key, false));
    00198         BenchMark(name, c, timeTotal);
    00199 }
    00200
    00201 //VC60 workaround: compiler bug triggered without the extra dummy parameters
    00202 template <class T>
    00203 void BenchMarkKeyless(const char *name, double timeTotal, T *x=NULL)
    00204 {
    00205         T c;
    00206         BenchMark(name, c, timeTotal);
    00207 }
    00208
    00209 void BenchmarkAll(double t)
    00210 {
    00211 #if 1
    00212         logtotal = 0;
    00213         logcount = 0;
    00214
    00215         cout << "<TABLE border=1><COLGROUP><COL align=left><COL align=right><COL align=right><COL align=right>" << endl;
    00216         cout << "<THEAD><TR><TH>Algorithm<TH>Megabytes(2^20 bytes) Processed<TH>Time Taken<TH>MB/Second\n<TBODY>" << endl;
    00217
    00218         BenchMarkKeyless<CRC32>("CRC-32", t);
    00219         BenchMarkKeyless<Adler32>("Adler-32", t);
    00220         BenchMarkKeyless<MD2>("MD2", t);
    00221         BenchMarkKeyless<MD5>("MD5", t);
    00222         BenchMarkKeyless<SHA>("SHA-1", t);
    00223         BenchMarkKeyless<SHA256>("SHA-256", t);
    00224 #ifdef WORD64_AVAILABLE
    00225         BenchMarkKeyless<SHA512>("SHA-512", t);
    00226 #endif
    00227         BenchMarkKeyless<HAVAL3>("HAVAL (pass=3)", t);
    00228         BenchMarkKeyless<HAVAL4>("HAVAL (pass=4)", t);
    00229         BenchMarkKeyless<HAVAL5>("HAVAL (pass=5)", t);
    00230 #ifdef WORD64_AVAILABLE
    00231         BenchMarkKeyless<Tiger>("Tiger", t);
    00232 #endif
    00233         BenchMarkKeyless<RIPEMD160>("RIPE-MD160", t);
    00234         BenchMarkKeyless<PanamaHash<LittleEndian> >("Panama Hash (little endian)", t);
    00235         BenchMarkKeyless<PanamaHash<BigEndian> >("Panama Hash (big endian)", t);
    00236 #ifdef WORD64_AVAILABLE
    00237         BenchMarkKeyless<Whirlpool>("Whirlpool", t);
    00238 #endif
    00239         BenchMarkKeyed<MDC<MD5>::Encryption>("MDC/MD5", t);
    00240         BenchMarkKeyed<LR<MD5>::Encryption>("Luby-Rackoff/MD5", t);
    00241         BenchMarkKeyed<DES::Encryption>("DES", t);
    00242         BenchMarkKeyed<DES_XEX3::Encryption>("DES-XEX3", t);
    00243         BenchMarkKeyed<DES_EDE3::Encryption>("DES-EDE3", t);
    00244         BenchMarkKeyed<IDEA::Encryption>("IDEA", t);
    00245         BenchMarkKeyed<RC2::Encryption>("RC2", t);
    00246         BenchMarkKeyed<RC5::Encryption>("RC5 (r=16)", t);
    00247         BenchMarkKeyed<Blowfish::Encryption>("Blowfish", t);
    00248         BenchMarkKeyed<ThreeWayDecryption>("3-WAY", t);
    00249         BenchMarkKeyed<TEA::Encryption>("TEA", t);
    00250         BenchMarkKeyedVariable<SAFER_SK::Encryption>("SAFER (r=8)", t, 8);
    00251         BenchMarkKeyed<GOST::Encryption>("GOST", t);
    00252 #ifdef WORD64_AVAILABLE
    00253         BenchMarkKeyed<SHARK::Encryption>("SHARK (r=6)", t);
    00254 #endif
    00255         BenchMarkKeyed<CAST128::Encryption>("CAST-128", t);
    00256         BenchMarkKeyed<CAST256::Encryption>("CAST-256", t);
    00257         BenchMarkKeyed<Square::Encryption>("Square", t);
    00258         BenchMarkKeyed<SKIPJACK::Encryption>("SKIPJACK", t);
    00259         BenchMarkKeyed<RC6::Encryption>("RC6", t);
    00260         BenchMarkKeyed<MARS::Encryption>("MARS", t);
    00261         BenchMarkKeyedVariable<Rijndael::Encryption>("Rijndael (128-bit key)", t, 16);
    00262         BenchMarkKeyedVariable<Rijndael::Encryption>("Rijndael (192-bit key)", t, 24);
    00263         BenchMarkKeyedVariable<Rijndael::Encryption>("Rijndael (256-bit key)", t, 32);
    00264         BenchMarkKeyedVariable<CTR_Mode<Rijndael>::Encryption>("Rijndael (128) CTR", t, 16);
    00265         BenchMarkKeyedVariable<OFB_Mode<Rijndael>::Encryption>("Rijndael (128) OFB", t, 16);
    00266         BenchMarkKeyedVariable<CFB_Mode<Rijndael>::Encryption>("Rijndael (128) CFB", t, 16);
    00267         BenchMarkKeyedVariable<CBC_Mode<Rijndael>::Encryption>("Rijndael (128) CBC", t, 16);
    00268         BenchMarkKeyed<Twofish::Encryption>("Twofish", t);
    00269         BenchMarkKeyed<Serpent::Encryption>("Serpent", t);
    00270         BenchMarkKeyed<ARC4>("ARC4", t);
    00271         BenchMarkKeyed<SEAL<BigEndian>::Encryption>("SEAL-3.0-BE", t);
    00272         BenchMarkKeyed<SEAL<LittleEndian>::Encryption>("SEAL-3.0-LE", t);
    00273         BenchMarkKeyed<WAKE_CFB<BigEndian>::Encryption>("WAKE-CFB-BE", t);
    00274         BenchMarkKeyed<WAKE_CFB<LittleEndian>::Encryption>("WAKE-CFB-LE", t);
    00275         BenchMarkKeyed<WAKE_OFB<BigEndian>::Encryption>("WAKE-OFB-BE", t);
    00276         BenchMarkKeyed<WAKE_OFB<LittleEndian>::Encryption>("WAKE-OFB-LE", t);
    00277         BenchMarkKeyed<PanamaCipher<LittleEndian>::Encryption>("Panama Cipher (little endian)", t);
    00278         BenchMarkKeyed<PanamaCipher<BigEndian>::Encryption>("Panama Cipher (big endian)", t);
    00279         BenchMarkKeyedVariable<SHACAL2::Encryption>("SHACAL-2 (128-bit key)", t, 16);
    00280         BenchMarkKeyedVariable<SHACAL2::Encryption>("SHACAL-2 (512-bit key)", t, 64);
    00281 #ifdef WORD64_AVAILABLE
    00282         BenchMarkKeyedVariable<Camellia::Encryption>("Camellia (128-bit key)", t, 16);
    00283         BenchMarkKeyedVariable<Camellia::Encryption>("Camellia (192-bit key)", t, 24);
    00284         BenchMarkKeyedVariable<Camellia::Encryption>("Camellia (256-bit key)", t, 32);
    00285 #endif
    00286         BenchMarkKeyed<MD5MAC>("MD5-MAC", t);
    00287         BenchMarkKeyed<XMACC<MD5> >("XMACC/MD5", t);
    00288         BenchMarkKeyed<HMAC<MD5> >("HMAC/MD5", t);
    00289         BenchMarkKeyed<TTMAC>("Two-Track-MAC", t);
    00290         BenchMarkKeyed<CBC_MAC<Rijndael> >("CBC-MAC/Rijndael", t);
    00291         BenchMarkKeyed<DMAC<Rijndael> >("DMAC/Rijndael", t);
    00292
    00293         {
    00294                 Integer p("CB6C,B8CE,6351,164F,5D0C,0C9E,9E31,E231,CF4E,D551,CBD0,E671,5D6A,7B06,D8DF,C4A7h");
    00295                 Integer q("FD2A,8594,A132,20CC,4E6D,DE77,3AAA,CF15,CD9E,E447,8592,FF46,CC77,87BE,9876,A2AFh");
    00296                 Integer s("63239752671357255800299643604761065219897634268887145610573595874544114193025997412441121667211431");
    00297                 BlumBlumShub c(p, q, s);
    00298                 BenchMark("BlumBlumShub 512", c, t);
    00299         }
    00300         {
    00301                 Integer p("FD2A,8594,A132,20CC,4E6D,DE77,3AAA,CF15,CD9E,E447,8592,FF46,CC77,87BE,9876,9E2C,"
    00302                                   "8572,64C3,4CF4,188A,44D4,2130,1135,7982,6FF6,EDD3,26F0,5FAA,BAF4,A81E,7ADC,B80Bh");
    00303                 Integer q("C8B9,5797,B349,6BA3,FD72,F2C0,A796,8A65,EE0F,B4BA,272F,4FEE,4DB1,06D5,ECEB,7142,"
    00304                                   "E8A8,E5A8,6BF9,A32F,BA37,BACC,8A75,8A6B,2DCE,D6EC,B515,980A,4BB1,08FB,6F2C,2383h");
    00305                 Integer s("3578,8F00,2965,71A4,4382,699F,45FD,3922,8238,241B,CEBA,0543,3443,E8D9,12FB,AC46,"
    00306                                   "7EC4,8505,EC9E,7EE8,5A23,9B2A,B615,D0C4,9448,F23A,ADEE,E850,1A7A,CA30,0B5B,A408,"
    00307                                   "D936,21BA,844E,BDD6,7848,3D1E,9137,CC87,DAA5,773B,D45A,C8BB,5392,1393,108B,6992,"
    00308                                   "74E3,C5E2,C235,A321,0111,3BA4,BAB4,1A2F,17EE,C371,DE67,01C9,0F3D,907A,B252,9BDDh");
    00309                 BlumBlumShub c(p, q, s);
    00310                 BenchMark("BlumBlumShub 1024", c, t);
    00311         }
    00312         {
    00313                 Integer p("EB56,978A,7BA7,B5D9,1383,4611,94F5,4766,FCEF,CF41,958A,FC41,43D0,839F,C56B,B568,"
    00314                                   "4ED3,9E5A,BABB,5ACE,8B11,CEBC,88A2,7C12,FFEE,E6E8,CF0A,E231,5BC2,DEDE,80B7,32F6,"
    00315                                   "340E,D8A6,B7DE,C779,7EE5,0E16,9C88,FC9F,2A0E,EE6C,7D47,C5F2,6B06,EB8C,F1C8,2E67,"
    00316                                   "5B82,8C28,4FB8,542F,2874,C355,CEEE,7A54,1B06,A8AB,8B66,6A5C,9DB2,72B8,74F3,7BC7h");
    00317                 Integer q("EB6B,3645,4591,8343,7331,7CAC,B02E,4BB9,DEF5,8EDC,1772,DB9B,9571,5FAB,1CDD,4FB1,"
    00318                                   "7B9A,07CD,E715,D448,F552,CBBD,D387,C037,DE70,6661,F360,D0E8,D42E,292A,9321,DDCB,"
    00319                                   "0BF9,C514,BFAC,3F2C,C06E,DF64,A9B8,50D6,AC4F,B9E4,014B,5624,2B40,A0D4,5D0B,6DD4,"
    00320                                   "0989,D00E,0268,99AB,21DB,0BB4,DB38,84DA,594F,575F,95AC,1B70,45E4,96C8,C6AD,CE67h");
    00321                 Integer s("C75A,8A0D,E231,295F,C08A,1716,8611,D5EC,E9EF,B565,90EC,58C0,57D0,DA7D,C6E6,DB00,"
    00322                                   "2282,1CA7,EA31,D64E,768C,0B19,8563,36DF,2226,F4EC,74A4,2844,2E8D,37E8,53DC,0172,"
    00323                                   "5F56,8CF9,B444,CA02,78B3,17AF,7C78,D320,16AE,AC3D,B97F,7259,1B8F,9C84,6A16,B878,"
    00324                                   "0595,70BB,9C52,18B5,9100,9C1F,E85A,4035,06F3,5F38,7462,F01D,0462,BFBC,A4CD,4A45,"
    00325                                   "3A77,E7F8,DED1,D6EF,CEF7,0937,CD3F,3AF1,4F88,932D,6D4B,002C,3735,304C,C5D3,B88A,"
    00326                                   "B57B,24B6,5346,9B46,5153,B7ED,B216,C181,B1C6,C52E,CD2B,E0AA,B1BB,0A93,C92E,4F79,"
    00327                                   "4931,E303,7C8F,A408,8ACF,56CD,6EC0,76A2,5015,6BA4,4C50,C44D,53B9,E168,5F84,B381,"
    00328                                   "2514,10B2,00E5,B4D1,4156,A2FE,0BF6,6F33,0A1B,91C6,31B8,1C90,02F1,FB1F,C494,8B65h");
    00329                 BlumBlumShub c(p, q, s);
    00330                 BenchMark("BlumBlumShub 2048", c, t);
    00331         }
    00332         cout << "</TABLE>" << endl;
    00333
    00334         BenchmarkAll2(t);
    00335
    00336         cout << "Throughput Geometric Average: " << setiosflags(ios::fixed) << exp(logtotal/logcount) << endl;
    00337
    00338         time_t endTime = time(NULL);
    00339         cout << "\nTest ended at " << asctime(localtime(&endTime));
    00340 #endif
    00341 }