Skip to content

Commit ce6df7d

Browse files
committed
bench: Add SHA256 implementation specific benchmarks
1 parent 5f72417 commit ce6df7d

File tree

1 file changed

+121
-6
lines changed

1 file changed

+121
-6
lines changed

src/bench/crypto_hash.cpp

Lines changed: 121 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <crypto/siphash.h>
1414
#include <hash.h>
1515
#include <random.h>
16+
#include <tinyformat.h>
1617
#include <uint256.h>
1718

1819
/* Number of bytes to hash per iteration */
@@ -36,13 +37,48 @@ static void SHA1(benchmark::Bench& bench)
3637
});
3738
}
3839

39-
static void SHA256(benchmark::Bench& bench)
40+
static void SHA256_STANDARD(benchmark::Bench& bench)
4041
{
42+
bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::STANDARD)));
4143
uint8_t hash[CSHA256::OUTPUT_SIZE];
4244
std::vector<uint8_t> in(BUFFER_SIZE,0);
4345
bench.batch(in.size()).unit("byte").run([&] {
4446
CSHA256().Write(in.data(), in.size()).Finalize(hash);
4547
});
48+
SHA256AutoDetect();
49+
}
50+
51+
static void SHA256_SSE4(benchmark::Bench& bench)
52+
{
53+
bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4)));
54+
uint8_t hash[CSHA256::OUTPUT_SIZE];
55+
std::vector<uint8_t> in(BUFFER_SIZE,0);
56+
bench.batch(in.size()).unit("byte").run([&] {
57+
CSHA256().Write(in.data(), in.size()).Finalize(hash);
58+
});
59+
SHA256AutoDetect();
60+
}
61+
62+
static void SHA256_AVX2(benchmark::Bench& bench)
63+
{
64+
bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_AVX2)));
65+
uint8_t hash[CSHA256::OUTPUT_SIZE];
66+
std::vector<uint8_t> in(BUFFER_SIZE,0);
67+
bench.batch(in.size()).unit("byte").run([&] {
68+
CSHA256().Write(in.data(), in.size()).Finalize(hash);
69+
});
70+
SHA256AutoDetect();
71+
}
72+
73+
static void SHA256_SHANI(benchmark::Bench& bench)
74+
{
75+
bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_SHANI)));
76+
uint8_t hash[CSHA256::OUTPUT_SIZE];
77+
std::vector<uint8_t> in(BUFFER_SIZE,0);
78+
bench.batch(in.size()).unit("byte").run([&] {
79+
CSHA256().Write(in.data(), in.size()).Finalize(hash);
80+
});
81+
SHA256AutoDetect();
4682
}
4783

4884
static void SHA3_256_1M(benchmark::Bench& bench)
@@ -54,22 +90,92 @@ static void SHA3_256_1M(benchmark::Bench& bench)
5490
});
5591
}
5692

57-
static void SHA256_32b(benchmark::Bench& bench)
93+
static void SHA256_32b_STANDARD(benchmark::Bench& bench)
94+
{
95+
bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::STANDARD)));
96+
std::vector<uint8_t> in(32,0);
97+
bench.batch(in.size()).unit("byte").run([&] {
98+
CSHA256()
99+
.Write(in.data(), in.size())
100+
.Finalize(in.data());
101+
});
102+
SHA256AutoDetect();
103+
}
104+
105+
static void SHA256_32b_SSE4(benchmark::Bench& bench)
106+
{
107+
bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4)));
108+
std::vector<uint8_t> in(32,0);
109+
bench.batch(in.size()).unit("byte").run([&] {
110+
CSHA256()
111+
.Write(in.data(), in.size())
112+
.Finalize(in.data());
113+
});
114+
SHA256AutoDetect();
115+
}
116+
117+
static void SHA256_32b_AVX2(benchmark::Bench& bench)
118+
{
119+
bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_AVX2)));
120+
std::vector<uint8_t> in(32,0);
121+
bench.batch(in.size()).unit("byte").run([&] {
122+
CSHA256()
123+
.Write(in.data(), in.size())
124+
.Finalize(in.data());
125+
});
126+
SHA256AutoDetect();
127+
}
128+
129+
static void SHA256_32b_SHANI(benchmark::Bench& bench)
58130
{
131+
bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_SHANI)));
59132
std::vector<uint8_t> in(32,0);
60133
bench.batch(in.size()).unit("byte").run([&] {
61134
CSHA256()
62135
.Write(in.data(), in.size())
63136
.Finalize(in.data());
64137
});
138+
SHA256AutoDetect();
139+
}
140+
141+
static void SHA256D64_1024_STANDARD(benchmark::Bench& bench)
142+
{
143+
bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::STANDARD)));
144+
std::vector<uint8_t> in(64 * 1024, 0);
145+
bench.batch(in.size()).unit("byte").run([&] {
146+
SHA256D64(in.data(), in.data(), 1024);
147+
});
148+
SHA256AutoDetect();
149+
}
150+
151+
static void SHA256D64_1024_SSE4(benchmark::Bench& bench)
152+
{
153+
bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4)));
154+
std::vector<uint8_t> in(64 * 1024, 0);
155+
bench.batch(in.size()).unit("byte").run([&] {
156+
SHA256D64(in.data(), in.data(), 1024);
157+
});
158+
SHA256AutoDetect();
159+
}
160+
161+
static void SHA256D64_1024_AVX2(benchmark::Bench& bench)
162+
{
163+
bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_AVX2)));
164+
std::vector<uint8_t> in(64 * 1024, 0);
165+
bench.batch(in.size()).unit("byte").run([&] {
166+
SHA256D64(in.data(), in.data(), 1024);
167+
});
168+
SHA256AutoDetect();
65169
}
66170

67-
static void SHA256D64_1024(benchmark::Bench& bench)
171+
static void SHA256D64_1024_SHANI(benchmark::Bench& bench)
68172
{
173+
bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_SHANI)));
69174
std::vector<uint8_t> in(64 * 1024, 0);
70175
bench.batch(in.size()).unit("byte").run([&] {
71176
SHA256D64(in.data(), in.data(), 1024);
72177
});
178+
SHA256AutoDetect();
73179
}
74180

75181
static void SHA512(benchmark::Bench& bench)
@@ -152,13 +258,22 @@ static void MuHashPrecompute(benchmark::Bench& bench)
152258

153259
BENCHMARK(BenchRIPEMD160, benchmark::PriorityLevel::HIGH);
154260
BENCHMARK(SHA1, benchmark::PriorityLevel::HIGH);
155-
BENCHMARK(SHA256, benchmark::PriorityLevel::HIGH);
261+
BENCHMARK(SHA256_STANDARD, benchmark::PriorityLevel::HIGH);
262+
BENCHMARK(SHA256_SSE4, benchmark::PriorityLevel::HIGH);
263+
BENCHMARK(SHA256_AVX2, benchmark::PriorityLevel::HIGH);
264+
BENCHMARK(SHA256_SHANI, benchmark::PriorityLevel::HIGH);
156265
BENCHMARK(SHA512, benchmark::PriorityLevel::HIGH);
157266
BENCHMARK(SHA3_256_1M, benchmark::PriorityLevel::HIGH);
158267

159-
BENCHMARK(SHA256_32b, benchmark::PriorityLevel::HIGH);
268+
BENCHMARK(SHA256_32b_STANDARD, benchmark::PriorityLevel::HIGH);
269+
BENCHMARK(SHA256_32b_SSE4, benchmark::PriorityLevel::HIGH);
270+
BENCHMARK(SHA256_32b_AVX2, benchmark::PriorityLevel::HIGH);
271+
BENCHMARK(SHA256_32b_SHANI, benchmark::PriorityLevel::HIGH);
160272
BENCHMARK(SipHash_32b, benchmark::PriorityLevel::HIGH);
161-
BENCHMARK(SHA256D64_1024, benchmark::PriorityLevel::HIGH);
273+
BENCHMARK(SHA256D64_1024_STANDARD, benchmark::PriorityLevel::HIGH);
274+
BENCHMARK(SHA256D64_1024_SSE4, benchmark::PriorityLevel::HIGH);
275+
BENCHMARK(SHA256D64_1024_AVX2, benchmark::PriorityLevel::HIGH);
276+
BENCHMARK(SHA256D64_1024_SHANI, benchmark::PriorityLevel::HIGH);
162277
BENCHMARK(FastRandom_32bit, benchmark::PriorityLevel::HIGH);
163278
BENCHMARK(FastRandom_1bit, benchmark::PriorityLevel::HIGH);
164279

0 commit comments

Comments
 (0)