Skip to content

Commit b4725ec

Browse files
Copilotachamayou
andcommitted
Fix remaining clang-tidy errors in OpenSSL code paths
The previous build didn't include -DOPENSSL=ON, so clang-tidy errors in #ifdef HAVE_OPENSSL blocks were missed. - time_large_trees.cpp: Add braces, const, static_cast for SHA384/512 blocks - compare_hash_functions.cpp: Replace #define with constexpr, typedef with using, std::endl with '\n', split declarations, add braces/const, fix narrowing conversions, use nullptr, fix implicit widening - merklecpp.h: Add const to EVP_Digest return values, replace typedef with using for Hash384/Path384/Tree384/Hash512/Path512/Tree512 Co-authored-by: achamayou <4016369+achamayou@users.noreply.github.com>
1 parent 9b34a8c commit b4725ec

File tree

3 files changed

+56
-46
lines changed

3 files changed

+56
-46
lines changed

merklecpp.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,7 +2052,7 @@ namespace merkle
20522052
memcpy(&block[32], r.bytes, 32);
20532053

20542054
const EVP_MD* md = EVP_sha256();
2055-
int rc =
2055+
const int rc =
20562056
EVP_Digest(&block[0], sizeof(block), out.bytes, nullptr, md, nullptr);
20572057
if (rc != 1)
20582058
{
@@ -2074,7 +2074,7 @@ namespace merkle
20742074
memcpy(&block[48], r.bytes, 48);
20752075

20762076
const EVP_MD* md = EVP_sha384();
2077-
int rc =
2077+
const int rc =
20782078
EVP_Digest(&block[0], sizeof(block), out.bytes, nullptr, md, nullptr);
20792079
if (rc != 1)
20802080
{
@@ -2096,7 +2096,7 @@ namespace merkle
20962096
memcpy(&block[64], r.bytes, 64);
20972097

20982098
const EVP_MD* md = EVP_sha512();
2099-
int rc =
2099+
const int rc =
21002100
EVP_Digest(&block[0], sizeof(block), out.bytes, nullptr, md, nullptr);
21012101
if (rc != 1)
21022102
{
@@ -2105,22 +2105,22 @@ namespace merkle
21052105
}
21062106

21072107
/// @brief Type of hashes in the SHA384 tree type
2108-
typedef HashT<48> Hash384;
2108+
using Hash384 = HashT<48>;
21092109

21102110
/// @brief Type of paths in the SHA384 tree type
2111-
typedef PathT<48, sha384_openssl> Path384;
2111+
using Path384 = PathT<48, sha384_openssl>;
21122112

21132113
/// @brief SHA384 tree with OpenSSL hash function
2114-
typedef TreeT<48, sha384_openssl> Tree384;
2114+
using Tree384 = TreeT<48, sha384_openssl>;
21152115

21162116
/// @brief Type of hashes in the SHA512 tree type
2117-
typedef HashT<64> Hash512;
2117+
using Hash512 = HashT<64>;
21182118

21192119
/// @brief Type of paths in the SHA512 tree type
2120-
typedef PathT<64, sha512_openssl> Path512;
2120+
using Path512 = PathT<64, sha512_openssl>;
21212121

21222122
/// @brief SHA512 tree with OpenSSL hash function
2123-
typedef TreeT<64, sha512_openssl> Tree512;
2123+
using Tree512 = TreeT<64, sha512_openssl>;
21242124
#endif
21252125

21262126
/// @brief Type of hashes in the default tree type

test/compare_hash_functions.cpp

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
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

5353
template <
@@ -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

217221
template <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

test/time_large_trees.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ int main()
5959
{
6060
mt384.insert(h);
6161
if ((j384++ % root_interval) == 0)
62+
{
6263
mt384.root();
64+
}
6365
}
6466
mt384.root();
6567
auto stop384 = std::chrono::high_resolution_clock::now();
66-
double seconds384 =
67-
std::chrono::duration_cast<std::chrono::nanoseconds>(stop384 - start384)
68-
.count() /
68+
const double seconds384 =
69+
static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>(stop384 - start384)
70+
.count()) /
6971
1e9;
7072
std::cout << "SHA384: " << mt384.statistics.to_string() << " in "
7173
<< seconds384 << " sec" << '\n';
@@ -81,13 +83,15 @@ int main()
8183
{
8284
mt512.insert(h);
8385
if ((j512++ % root_interval) == 0)
86+
{
8487
mt512.root();
88+
}
8589
}
8690
mt512.root();
8791
auto stop512 = std::chrono::high_resolution_clock::now();
88-
double seconds512 =
89-
std::chrono::duration_cast<std::chrono::nanoseconds>(stop512 - start512)
90-
.count() /
92+
const double seconds512 =
93+
static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>(stop512 - start512)
94+
.count()) /
9195
1e9;
9296
std::cout << "SHA512: " << mt512.statistics.to_string() << " in "
9397
<< seconds512 << " sec" << '\n';

0 commit comments

Comments
 (0)