88#include < iostream>
99#include < merklecpp.h>
1010
11- # define HSZ 32
12- # define PRNTSZ 3
11+ constexpr size_t HSZ = 32 ;
12+ constexpr size_t PRNTSZ = 3 ;
1313
1414#ifdef HAVE_EVERCRYPT
1515# include < Hacl_Hash.h>
@@ -47,7 +47,7 @@ typedef merkle::TreeT<32, sha256_evercrypt> EverCryptFullTree;
4747#endif
4848
4949#ifdef HAVE_OPENSSL
50- typedef merkle::TreeT<32 , merkle::sha256_openssl> OpenSSLFullTree ;
50+ using OpenSSLFullTree = merkle::TreeT<32 , merkle::sha256_openssl>;
5151#endif
5252
5353template <
@@ -68,11 +68,11 @@ void compare_roots(
6868 if (mt1_root != mt2_root)
6969 {
7070 std::cout << mt1.num_leaves () << " : " << mt1_root.to_string ()
71- << " != " << mt2_root.to_string () << std::endl ;
72- std::cout << " mt1: " << std::endl ;
73- std::cout << mt1.to_string (PRNTSZ) << std::endl ;
74- std::cout << name << " : " << std::endl ;
75- std::cout << mt2.to_string (PRNTSZ) << std::endl ;
71+ << " != " << mt2_root.to_string () << ' \n ' ;
72+ std::cout << " mt1: " << ' \n ' ;
73+ std::cout << mt1.to_string (PRNTSZ) << ' \n ' ;
74+ std::cout << name << " : " << ' \n ' ;
75+ std::cout << mt2.to_string (PRNTSZ) << ' \n ' ;
7676 throw std::runtime_error (" root hash mismatch" );
7777 }
7878}
@@ -87,7 +87,8 @@ void compare_compression_hashes()
8787 const size_t root_interval = 128 ;
8888#endif
8989
90- size_t total_inserts = 0 , total_roots = 0 ;
90+ size_t total_inserts = 0 ;
91+ size_t total_roots = 0 ;
9192
9293 for (size_t k = 0 ; k < num_trees; k++)
9394 {
@@ -129,7 +130,7 @@ void compare_compression_hashes()
129130
130131 std::cout << num_trees << " trees, " << total_inserts << " inserts, "
131132 << total_roots << " roots with SHA256 compression function: OK"
132- << std::endl ;
133+ << ' \n ' ;
133134}
134135
135136#if defined(HAVE_OPENSSL) && defined(HAVE_EVERCRYPT)
@@ -143,7 +144,8 @@ void compare_full_hashes()
143144 const size_t root_interval = 128 ;
144145# endif
145146
146- size_t total_inserts = 0 , total_roots = 0 ;
147+ size_t total_inserts = 0 ;
148+ size_t total_roots = 0 ;
147149
148150 for (size_t k = 0 ; k < num_trees; k++)
149151 {
@@ -184,7 +186,7 @@ void compare_full_hashes()
184186 }
185187
186188 std::cout << num_trees << " trees, " << total_inserts << " inserts, "
187- << total_roots << " roots with full SHA256: OK" << std::endl ;
189+ << total_roots << " roots with full SHA256: OK" << ' \n ' ;
188190}
189191#endif
190192
@@ -197,21 +199,23 @@ void bench(
197199 size_t j = 0 ;
198200 auto start = std::chrono::high_resolution_clock::now ();
199201 T mt;
200- for (auto & h : hashes)
202+ for (const auto & h : hashes)
201203 {
202204 mt.insert (h);
203205 if ((j++ % root_interval) == 0 )
206+ {
204207 mt.root ();
208+ }
205209 }
206210 mt.root ();
207211 auto stop = std::chrono::high_resolution_clock::now ();
208- double seconds =
209- std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count () /
212+ const double seconds =
213+ static_cast < double >( std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count () ) /
210214 1e9 ;
211215 std::cout << std::left << std::setw (10 ) << name << " : "
212216 << mt.statistics .num_insert << " insertions, "
213217 << mt.statistics .num_root << " roots in " << seconds << " sec"
214- << std::endl ;
218+ << ' \n ' ;
215219}
216220
217221template <typename T, size_t HASH_SIZE>
@@ -223,21 +227,23 @@ void benchT(
223227 size_t j = 0 ;
224228 auto start = std::chrono::high_resolution_clock::now ();
225229 T mt;
226- for (auto & h : hashes)
230+ for (const auto & h : hashes)
227231 {
228232 mt.insert (h);
229233 if ((j++ % root_interval) == 0 )
234+ {
230235 mt.root ();
236+ }
231237 }
232238 mt.root ();
233239 auto stop = std::chrono::high_resolution_clock::now ();
234- double seconds =
235- std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count () /
240+ const double seconds =
241+ static_cast < double >( std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count () ) /
236242 1e9 ;
237243 std::cout << std::left << std::setw (10 ) << name << " : "
238244 << mt.statistics .num_insert << " insertions, "
239245 << mt.statistics .num_root << " roots in " << seconds << " sec"
240- << std::endl ;
246+ << ' \n ' ;
241247}
242248
243249#ifdef HAVE_EVERCRYPT
@@ -269,7 +275,7 @@ void bench_evercrypt(
269275 1e9 ;
270276 std::cout << std::left << std::setw (10 ) << name << " : " << num_inserts
271277 << " insertions, " << num_roots << " roots in " << seconds << " sec"
272- << std::endl ;
278+ << ' \n ' ;
273279 mt_free_hash (ec_root);
274280 mt_free (ec_mt);
275281}
@@ -280,7 +286,7 @@ int main()
280286 try
281287 {
282288 // std::srand(0);
283- std::srand (std::time (0 ));
289+ std::srand (std::time (nullptr ));
284290
285291 compare_compression_hashes ();
286292
@@ -292,36 +298,36 @@ int main()
292298 const size_t num_leaves = 128 * 1024 ;
293299 const size_t root_interval = 128 ;
294300#else
295- const size_t num_leaves = 16 * 1024 * 1024 ;
301+ const size_t num_leaves = static_cast < size_t >( 16 ) * 1024 * 1024 ;
296302 const size_t root_interval = 1024 ;
297303#endif
298304
299305 auto hashes = make_hashes (num_leaves);
300306
301307 std::cout << " --- merklecpp trees with SHA256 compression function: "
302- << std::endl ;
308+ << ' \n ' ;
303309
304310 bench<merkle::Tree>(hashes, " merklecpp" , root_interval);
305311
306312#ifdef HAVE_EVERCRYPT
307313 bench<EverCryptTree>(hashes, " EverCrypt" , root_interval);
308314#endif
309315
310- std::cout << " --- merklecpp trees with full SHA256: " << std::endl ;
316+ std::cout << " --- merklecpp trees with full SHA256: " << ' \n ' ;
311317
312318#ifdef HAVE_OPENSSL
313319 bench<OpenSSLFullTree>(hashes, " OpenSSL" , root_interval);
314320#endif
315321
316322#ifdef HAVE_OPENSSL
317323 {
318- std::cout << " --- merklecpp trees with full SHA384: " << std::endl ;
324+ std::cout << " --- merklecpp trees with full SHA384: " << ' \n ' ;
319325 auto hashes384 = make_hashesT<48 >(num_leaves);
320326 benchT<merkle::Tree384, 48 >(hashes384, " OpenSSL" , root_interval);
321327 }
322328
323329 {
324- std::cout << " --- merklecpp trees with full SHA512: " << std::endl ;
330+ std::cout << " --- merklecpp trees with full SHA512: " << ' \n ' ;
325331 auto hashes512 = make_hashesT<64 >(num_leaves);
326332 benchT<merkle::Tree512, 64 >(hashes512, " OpenSSL" , root_interval);
327333 }
@@ -340,10 +346,10 @@ int main()
340346 }
341347
342348 std::cout << " --- EverCrypt trees with SHA256 compression function: "
343- << std::endl ;
349+ << ' \n ' ;
344350 bench_evercrypt<mt_sha256_compress>(ec_hashes, " EverCrypt" , root_interval);
345351
346- std::cout << " --- EverCrypt trees with full SHA256: " << std::endl ;
352+ std::cout << " --- EverCrypt trees with full SHA256: " << ' \n ' ;
347353 bench_evercrypt<mt_sha256_evercrypt>(ec_hashes, " EverCrypt" , root_interval);
348354
349355 for (auto h : ec_hashes)
@@ -352,12 +358,12 @@ int main()
352358 }
353359 catch (std::exception& ex)
354360 {
355- std::cout << " Error: " << ex.what () << std::endl ;
361+ std::cout << " Error: " << ex.what () << ' \n ' ;
356362 return 1 ;
357363 }
358364 catch (...)
359365 {
360- std::cout << " Error" << std::endl ;
366+ std::cout << " Error" << ' \n ' ;
361367 return 1 ;
362368 }
363369
0 commit comments