Crypto++ 8.2
Free C&
bench1.cpp
1// bench1.cpp - originally written and placed in the public domain by Wei Dai
2// CryptoPP::Test namespace added by JW in February 2017
3
4#include "cryptlib.h"
5#include "bench.h"
6#include "validate.h"
7
8#include "cpu.h"
9#include "factory.h"
10#include "algparam.h"
11#include "argnames.h"
12#include "smartptr.h"
13#include "stdcpp.h"
14
15#include "osrng.h"
16#include "drbg.h"
17#include "darn.h"
18#include "mersenne.h"
19#include "rdrand.h"
20#include "padlkrng.h"
21
22#include <iostream>
23#include <iomanip>
24#include <sstream>
25
26#if CRYPTOPP_MSC_VERSION
27# pragma warning(disable: 4355)
28#endif
29
30#if CRYPTOPP_MSC_VERSION
31# pragma warning(disable: 4505 4355)
32#endif
33
34NAMESPACE_BEGIN(CryptoPP)
35NAMESPACE_BEGIN(Test)
36
37#ifdef CLOCKS_PER_SEC
38const double CLOCK_TICKS_PER_SECOND = (double)CLOCKS_PER_SEC;
39#elif defined(CLK_TCK)
40const double CLOCK_TICKS_PER_SECOND = (double)CLK_TCK;
41#else
42const double CLOCK_TICKS_PER_SECOND = 1000000.0;
43#endif
44
45extern const byte defaultKey[] = "0123456789" // 168 + NULL
46 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
47 "00000000000000000000000000000000000000000000000000000"
48 "00000000000000000000000000000000000000000000000000000";
49
50double g_allocatedTime = 0.0, g_hertz = 0.0, g_logTotal = 0.0;
51unsigned int g_logCount = 0;
52time_t g_testBegin, g_testEnd;
53
54inline std::string HertzToString(double hertz)
55{
56 std::ostringstream oss;
57 oss.precision(3);
58
59 if (hertz >= 0.999e+9)
60 oss << hertz / 1e+9 << " GHz";
61 else if (hertz >= 0.999e+6)
62 oss << hertz / 1e+6 << " MHz";
63 else if (hertz >= 0.999e+3)
64 oss << hertz / 1e+3 << " KHz";
65 else
66 oss << hertz << " Hz";
67
68 return oss.str();
69}
70
71void OutputResultBytes(const char *name, const char *provider, double length, double timeTaken)
72{
73 std::ostringstream oss;
74
75 // Coverity finding
76 if (length < 0.000001f) length = 0.000001f;
77 if (timeTaken < 0.000001f) timeTaken = 0.000001f;
78
79 double mbs = length / timeTaken / (1024*1024);
80 oss << "\n<TR><TD>" << name << "<TD>" << provider;
81 oss << std::setiosflags(std::ios::fixed);
82 oss << "<TD>" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << mbs;
83 if (g_hertz > 1.0f)
84 {
85 const double cpb = timeTaken * g_hertz / length;
86 if (cpb < 24.0f)
87 oss << "<TD>" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << cpb;
88 else
89 oss << "<TD>" << std::setprecision(1) << std::setiosflags(std::ios::fixed) << cpb;
90 }
91 g_logTotal += log(mbs);
92 g_logCount++;
93
94 std::cout << oss.str();
95}
96
97void OutputResultKeying(double iterations, double timeTaken)
98{
99 std::ostringstream oss;
100
101 // Coverity finding
102 if (iterations < 0.000001f) iterations = 0.000001f;
103 if (timeTaken < 0.000001f) timeTaken = 0.000001f;
104
105 oss << "<TD>" << std::setprecision(3) << std::setiosflags(std::ios::fixed) << (1000*1000*timeTaken/iterations);
106
107 // Coverity finding
108 if (g_hertz > 1.0f)
109 oss << "<TD>" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << timeTaken * g_hertz / iterations;
110
111 std::cout << oss.str();
112}
113
114void OutputResultOperations(const char *name, const char *provider, const char *operation, bool pc, unsigned long iterations, double timeTaken)
115{
116 CRYPTOPP_UNUSED(provider);
117 std::ostringstream oss;
118
119 // Coverity finding
120 if (!iterations) iterations++;
121 if (timeTaken < 0.000001f) timeTaken = 0.000001f;
122
123 oss << "\n<TR><TD>" << name << " " << operation << (pc ? " with precomputation" : "");
124 //oss << "<TD>" << provider;
125 oss << "<TD>" << std::setprecision(3) << std::setiosflags(std::ios::fixed) << (1000*timeTaken/iterations);
126
127 // Coverity finding
128 if (g_hertz > 1.0f)
129 {
130 const double t = timeTaken * g_hertz / iterations / 1000000;
131 oss << "<TD>" << std::setprecision(3) << std::setiosflags(std::ios::fixed) << t;
132 }
133
134 g_logTotal += log(iterations/timeTaken);
135 g_logCount++;
136
137 std::cout << oss.str();
138}
139
140/*
141void BenchMark(const char *name, BlockTransformation &cipher, double timeTotal)
142{
143 const int BUF_SIZE = RoundUpToMultipleOf(2048U, cipher.OptimalNumberOfParallelBlocks() * cipher.BlockSize());
144 AlignedSecByteBlock buf(BUF_SIZE);
145 buf.SetMark(16);
146
147 const int nBlocks = BUF_SIZE / cipher.BlockSize();
148 unsigned long i=0, blocks=1;
149 double timeTaken;
150
151 clock_t start = ::clock();
152 do
153 {
154 blocks *= 2;
155 for (; i<blocks; i++)
156 cipher.ProcessAndXorMultipleBlocks(buf, NULLPTR, buf, nBlocks);
157 timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
158 }
159 while (timeTaken < 2.0/3*timeTotal);
160
161 OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
162}
163*/
164
165void BenchMark(const char *name, StreamTransformation &cipher, double timeTotal)
166{
167 const int BUF_SIZE=RoundUpToMultipleOf(2048U, cipher.OptimalBlockSize());
168 AlignedSecByteBlock buf(BUF_SIZE);
169 Test::GlobalRNG().GenerateBlock(buf, BUF_SIZE);
170 buf.SetMark(16);
171
172 unsigned long i=0, blocks=1;
173 double timeTaken;
174
175 clock_t start = ::clock();
176 do
177 {
178 blocks *= 2;
179 for (; i<blocks; i++)
180 cipher.ProcessString(buf, BUF_SIZE);
181 timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
182 }
183 while (timeTaken < 2.0/3*timeTotal);
184
185 std::string provider = cipher.AlgorithmProvider();
186 OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
187}
188
189void BenchMark(const char *name, HashTransformation &ht, double timeTotal)
190{
191 const int BUF_SIZE=2048U;
192 AlignedSecByteBlock buf(BUF_SIZE);
193 Test::GlobalRNG().GenerateBlock(buf, BUF_SIZE);
194 buf.SetMark(16);
195
196 unsigned long i=0, blocks=1;
197 double timeTaken;
198
199 clock_t start = ::clock();
200 do
201 {
202 blocks *= 2;
203 for (; i<blocks; i++)
204 ht.Update(buf, BUF_SIZE);
205 timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
206 }
207 while (timeTaken < 2.0/3*timeTotal);
208
209 std::string provider = ht.AlgorithmProvider();
210 OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
211}
212
213void BenchMark(const char *name, BufferedTransformation &bt, double timeTotal)
214{
215 const int BUF_SIZE=2048U;
216 AlignedSecByteBlock buf(BUF_SIZE);
217 Test::GlobalRNG().GenerateBlock(buf, BUF_SIZE);
218 buf.SetMark(16);
219
220 unsigned long i=0, blocks=1;
221 double timeTaken;
222
223 clock_t start = ::clock();
224 do
225 {
226 blocks *= 2;
227 for (; i<blocks; i++)
228 bt.Put(buf, BUF_SIZE);
229 timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
230 }
231 while (timeTaken < 2.0/3*timeTotal);
232
233 std::string provider = bt.AlgorithmProvider();
234 OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
235}
236
237void BenchMark(const char *name, RandomNumberGenerator &rng, double timeTotal)
238{
239 const int BUF_SIZE = 2048U;
240 AlignedSecByteBlock buf(BUF_SIZE);
241 Test::GlobalRNG().GenerateBlock(buf, BUF_SIZE);
242 buf.SetMark(16);
243
244 SymmetricCipher * cipher = dynamic_cast<SymmetricCipher*>(&rng);
245 if (cipher != NULLPTR)
246 {
247 const size_t size = cipher->DefaultKeyLength();
248 if (cipher->IsResynchronizable())
249 cipher->SetKeyWithIV(buf, size, buf+size);
250 else
251 cipher->SetKey(buf, size);
252 }
253
254 unsigned long long blocks = 1;
255 double timeTaken;
256
257 clock_t start = ::clock();
258 do
259 {
260 rng.GenerateBlock(buf, buf.size());
261 blocks++;
262 timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
263 } while (timeTaken < timeTotal);
264
265 std::string provider = rng.AlgorithmProvider();
266 OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
267}
268
269// Hack, but we probably need a KeyedRandomNumberGenerator interface
270// and a few methods to generalize keying a RNG. X917RNG, Hash_DRBG,
271// HMAC_DRBG, AES/CFB RNG and a few others could use it. "A few others"
272// includes BLAKE2, ChaCha and Poly1305 when used as a RNG.
273void BenchMark(const char *name, NIST_DRBG &rng, double timeTotal)
274{
275 const int BUF_SIZE = 2048U;
276 AlignedSecByteBlock buf(BUF_SIZE);
277 Test::GlobalRNG().GenerateBlock(buf, BUF_SIZE);
278 buf.SetMark(16);
279
280 rng.IncorporateEntropy(buf, rng.MinEntropyLength());
281 unsigned long long blocks = 1;
282 double timeTaken;
283
284 clock_t start = ::clock();
285 do
286 {
287 rng.GenerateBlock(buf, buf.size());
288 blocks++;
289 timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
290 } while (timeTaken < timeTotal);
291
292 std::string provider = rng.AlgorithmProvider();
293 OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
294}
295
296template <class T>
297void BenchMarkByNameKeyLess(const char *factoryName, const char *displayName = NULLPTR, const NameValuePairs &params = g_nullNameValuePairs)
298{
299 CRYPTOPP_UNUSED(params);
300 std::string name = factoryName;
301 if (displayName)
302 name = displayName;
303
304 member_ptr<T> obj(ObjectFactoryRegistry<T>::Registry().CreateObject(factoryName));
305 BenchMark(name.c_str(), *obj, g_allocatedTime);
306}
307
308void AddHtmlHeader()
309{
310 std::ostringstream oss;
311
312 // HTML5
313 oss << "<!DOCTYPE HTML>";
314 oss << "\n<HTML lang=\"en\">";
315
316 oss << "\n<HEAD>";
317 oss << "\n<META charset=\"UTF-8\">";
318 oss << "\n<TITLE>Speed Comparison of Popular Crypto Algorithms</TITLE>";
319 oss << "\n<STYLE>\n table {border-collapse: collapse;}";
320 oss << "\n table, th, td, tr {border: 1px solid black;}\n</STYLE>";
321 oss << "\n</HEAD>";
322
323 oss << "\n<BODY>";
324
325 oss << "\n<H1><A href=\"http://www.cryptopp.com\">Crypto++</A> " << CRYPTOPP_VERSION / 100;
326 oss << '.' << (CRYPTOPP_VERSION % 100) / 10 << '.' << CRYPTOPP_VERSION % 10 << " Benchmarks</H1>";
327
328 oss << "\n<P>Here are speed benchmarks for some commonly used cryptographic algorithms.</P>";
329
330 if (g_hertz > 1.0f)
331 oss << "\n<P>CPU frequency of the test platform is " << HertzToString(g_hertz) << ".</P>";
332 else
333 oss << "\n<P>CPU frequency of the test platform was not provided.</P>" << std::endl;
334
335 std::cout << oss.str();
336}
337
338void AddHtmlFooter()
339{
340 std::ostringstream oss;
341 oss << "\n</BODY>\n</HTML>\n";
342 std::cout << oss.str();
343}
344
345void BenchmarkWithCommand(int argc, const char* const argv[])
346{
347 std::string command(argv[1]);
348 float runningTime(argc >= 3 ? Test::StringToValue<float, true>(argv[2]) : 1.0f);
349 float cpuFreq(argc >= 4 ? Test::StringToValue<float, true>(argv[3])*float(1e9) : 0.0f);
350 std::string algoName(argc >= 5 ? argv[4] : "");
351
352 if (command == "b") // All benchmarks
353 Benchmark(Test::All, runningTime, cpuFreq);
354 else if (command == "b3") // Public key algorithms
355 Test::Benchmark(Test::PublicKey, runningTime, cpuFreq);
356 else if (command == "b2") // Shared key algorithms
357 Test::Benchmark(Test::SharedKey, runningTime, cpuFreq);
358 else if (command == "b1") // Unkeyed algorithms
359 Test::Benchmark(Test::Unkeyed, runningTime, cpuFreq);
360}
361
362void Benchmark(Test::TestClass suites, double t, double hertz)
363{
364 g_allocatedTime = t;
365 g_hertz = hertz;
366
367 AddHtmlHeader();
368
369 g_testBegin = ::time(NULLPTR);
370
371 if (static_cast<int>(suites) == 0 || static_cast<int>(suites) > TestLast)
372 suites = Test::All;
373
374 // Unkeyed algorithms
375 if (suites & Test::Unkeyed)
376 {
377 std::cout << "\n<BR>";
378 Benchmark1(t, hertz);
379 }
380
381 // Shared key algorithms
382 if (suites & Test::SharedKey)
383 {
384 std::cout << "\n<BR>";
385 Benchmark2(t, hertz);
386 }
387
388 // Public key algorithms
389 if (suites & Test::PublicKey)
390 {
391 std::cout << "\n<BR>";
392 Benchmark3(t, hertz);
393 }
394
395 g_testEnd = ::time(NULLPTR);
396
397 std::ostringstream oss;
398 oss << "\n<P>Throughput Geometric Average: " << std::setiosflags(std::ios::fixed);
399 oss << std::exp(g_logTotal/(g_logCount > 0.0f ? g_logCount : 1.0f)) << std::endl;
400
401 oss << "\n<P>Test started at " << TimeToString(g_testBegin);
402 oss << "\n<BR>Test ended at " << TimeToString(g_testEnd);
403 oss << "\n";
404 std::cout << oss.str();
405
406 AddHtmlFooter();
407}
408
409void Benchmark1(double t, double hertz)
410{
411 g_allocatedTime = t;
412 g_hertz = hertz;
413
414 const char *cpb;
415 if (g_hertz > 1.0f)
416 cpb = "<TH>Cycles/Byte";
417 else
418 cpb = "";
419
420 std::cout << "\n<TABLE>";
421
422 std::cout << "\n<COLGROUP><COL style=\"text-align: left;\"><COL style=\"text-align: right;\">";
423 std::cout << "<COL style=\"text-align: right;\">";
424 std::cout << "\n<THEAD style=\"background: #F0F0F0\">";
425 std::cout << "\n<TR><TH>Algorithm<TH>Provider<TH>MiB/Second" << cpb;
426
427 std::cout << "\n<TBODY style=\"background: white;\">";
428 {
429#ifdef NONBLOCKING_RNG_AVAILABLE
430 BenchMarkByNameKeyLess<RandomNumberGenerator>("NonblockingRng");
431#endif
432#ifdef OS_RNG_AVAILABLE
433 BenchMarkByNameKeyLess<RandomNumberGenerator>("AutoSeededRandomPool");
434 BenchMarkByNameKeyLess<RandomNumberGenerator>("AutoSeededX917RNG(AES)");
435#endif
436 BenchMarkByNameKeyLess<RandomNumberGenerator>("MT19937");
437#if (CRYPTOPP_BOOL_X86) && !defined(CRYPTOPP_DISABLE_ASM)
438 if (HasPadlockRNG())
439 BenchMarkByNameKeyLess<RandomNumberGenerator>("PadlockRNG");
440#endif
441#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64) && !defined(CRYPTOPP_DISABLE_ASM)
442 if (HasRDRAND())
443 BenchMarkByNameKeyLess<RandomNumberGenerator>("RDRAND");
444 if (HasRDSEED())
445 BenchMarkByNameKeyLess<RandomNumberGenerator>("RDSEED");
446#endif
447#if (CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64) && !defined(CRYPTOPP_DISABLE_ASM)
448 if (HasDARN())
449 BenchMarkByNameKeyLess<RandomNumberGenerator>("DARN");
450#endif
451 BenchMarkByNameKeyLess<RandomNumberGenerator>("AES/OFB RNG");
452 BenchMarkByNameKeyLess<NIST_DRBG>("Hash_DRBG(SHA1)");
453 BenchMarkByNameKeyLess<NIST_DRBG>("Hash_DRBG(SHA256)");
454 BenchMarkByNameKeyLess<NIST_DRBG>("HMAC_DRBG(SHA1)");
455 BenchMarkByNameKeyLess<NIST_DRBG>("HMAC_DRBG(SHA256)");
456 }
457
458 std::cout << "\n<TBODY style=\"background: yellow;\">";
459 {
460 BenchMarkByNameKeyLess<HashTransformation>("CRC32");
461 BenchMarkByNameKeyLess<HashTransformation>("CRC32C");
462 BenchMarkByNameKeyLess<HashTransformation>("Adler32");
463 BenchMarkByNameKeyLess<HashTransformation>("MD5");
464 BenchMarkByNameKeyLess<HashTransformation>("SHA-1");
465 BenchMarkByNameKeyLess<HashTransformation>("SHA-256");
466 BenchMarkByNameKeyLess<HashTransformation>("SHA-512");
467 BenchMarkByNameKeyLess<HashTransformation>("SHA3-224");
468 BenchMarkByNameKeyLess<HashTransformation>("SHA3-256");
469 BenchMarkByNameKeyLess<HashTransformation>("SHA3-384");
470 BenchMarkByNameKeyLess<HashTransformation>("SHA3-512");
471 BenchMarkByNameKeyLess<HashTransformation>("Keccak-224");
472 BenchMarkByNameKeyLess<HashTransformation>("Keccak-256");
473 BenchMarkByNameKeyLess<HashTransformation>("Keccak-384");
474 BenchMarkByNameKeyLess<HashTransformation>("Keccak-512");
475 BenchMarkByNameKeyLess<HashTransformation>("Tiger");
476 BenchMarkByNameKeyLess<HashTransformation>("Whirlpool");
477 BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-160");
478 BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-320");
479 BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-128");
480 BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-256");
481 BenchMarkByNameKeyLess<HashTransformation>("SM3");
482 BenchMarkByNameKeyLess<HashTransformation>("BLAKE2s");
483 BenchMarkByNameKeyLess<HashTransformation>("BLAKE2b");
484 }
485
486 std::cout << "\n</TABLE>" << std::endl;
487}
488
489NAMESPACE_END // Test
490NAMESPACE_END // CryptoPP
Classes for working with NameValuePairs.
Standard names for retrieving values by name when working with NameValuePairs.
virtual std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition: cryptlib.h:608
SecBlock using AllocatorWithCleanup<byte, true> typedef.
Definition: secblock.h:1062
Interface for buffered transformations.
Definition: cryptlib.h:1599
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition: cryptlib.h:1620
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:1085
virtual void Update(const byte *input, size_t length)=0
Updates a hash with additional input.
Interface for NIST DRBGs from SP 800-90A.
Definition: drbg.h:25
virtual void IncorporateEntropy(const byte *input, size_t length)=0
Update RNG state with additional unpredictable values.
virtual void GenerateBlock(byte *output, size_t size)=0
Generate random array of bytes.
virtual unsigned int MinEntropyLength() const =0
Provides the minimum entropy size.
Interface for retrieving values given their names.
Definition: cryptlib.h:294
Object factory registry.
Definition: factory.h:43
Interface for random number generators.
Definition: cryptlib.h:1384
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: cryptlib.cpp:311
void SetKeyWithIV(const byte *key, size_t length, const byte *iv, size_t ivLength)
Sets or reset the key of this object.
Definition: cryptlib.cpp:69
bool IsResynchronizable() const
Determines if the object can be resynchronized.
Definition: cryptlib.h:712
virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params=g_nullNameValuePairs)
Sets or reset the key of this object.
Definition: cryptlib.cpp:58
virtual size_t DefaultKeyLength() const =0
Returns default key length.
Interface for the data processing portion of stream ciphers.
Definition: cryptlib.h:918
void ProcessString(byte *inoutString, size_t length)
Encrypt or decrypt a string of bytes.
Definition: cryptlib.h:1032
virtual unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this cipher.
Definition: cryptlib.h:944
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode.
Definition: cryptlib.h:1259
Pointer that overloads operator ->
Definition: smartptr.h:37
Functions for CPU features and intrinsics.
bool HasPadlockRNG()
Determines Padlock RNG availability.
Definition: cpu.h:269
bool HasRDRAND()
Determines RDRAND availability.
Definition: cpu.h:247
bool HasDARN()
Determine if a PowerPC processor has DARN available.
Definition: cpu.h:722
bool HasRDSEED()
Determines RDSEED availability.
Definition: cpu.h:258
Abstract base classes that provide a uniform interface to this library.
Classes for DARN RNG.
Classes for NIST DRBGs from SP 800-90A.
Classes and functions for registering and locating library objects.
Class file for Mersenne Twister.
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
Definition: misc.h:1085
Crypto++ library namespace.
Namespace containing testing and benchmark classes.
Definition: cryptlib.h:547
Classes for access to the operating system's random number generators.
Classes for VIA Padlock RNG.
Classes for RDRAND and RDSEED.
Classes for automatic resource management.
Common C++ header files.