1 // Copyright (c) 2016-2017 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 #include <bench/bench.h>
13 #include <crypto/ripemd160.h>
14 #include <crypto/sha1.h>
15 #include <crypto/sha256.h>
16 #include <crypto/sha512.h>
18 /* Number of bytes to hash per iteration */
19 static const uint64_t BUFFER_SIZE
= 1000*1000;
21 static void RIPEMD160(benchmark::State
& state
)
23 uint8_t hash
[CRIPEMD160::OUTPUT_SIZE
];
24 std::vector
<uint8_t> in(BUFFER_SIZE
,0);
25 while (state
.KeepRunning())
26 CRIPEMD160().Write(in
.data(), in
.size()).Finalize(hash
);
29 static void SHA1(benchmark::State
& state
)
31 uint8_t hash
[CSHA1::OUTPUT_SIZE
];
32 std::vector
<uint8_t> in(BUFFER_SIZE
,0);
33 while (state
.KeepRunning())
34 CSHA1().Write(in
.data(), in
.size()).Finalize(hash
);
37 static void SHA256(benchmark::State
& state
)
39 uint8_t hash
[CSHA256::OUTPUT_SIZE
];
40 std::vector
<uint8_t> in(BUFFER_SIZE
,0);
41 while (state
.KeepRunning())
42 CSHA256().Write(in
.data(), in
.size()).Finalize(hash
);
45 static void SHA256_32b(benchmark::State
& state
)
47 std::vector
<uint8_t> in(32,0);
48 while (state
.KeepRunning()) {
50 .Write(in
.data(), in
.size())
55 static void SHA512(benchmark::State
& state
)
57 uint8_t hash
[CSHA512::OUTPUT_SIZE
];
58 std::vector
<uint8_t> in(BUFFER_SIZE
,0);
59 while (state
.KeepRunning())
60 CSHA512().Write(in
.data(), in
.size()).Finalize(hash
);
63 static void SipHash_32b(benchmark::State
& state
)
67 while (state
.KeepRunning()) {
68 *((uint64_t*)x
.begin()) = SipHashUint256(0, ++k1
, x
);
72 static void FastRandom_32bit(benchmark::State
& state
)
74 FastRandomContext
rng(true);
76 while (state
.KeepRunning()) {
81 static void FastRandom_1bit(benchmark::State
& state
)
83 FastRandomContext
rng(true);
85 while (state
.KeepRunning()) {
90 BENCHMARK(RIPEMD160
, 440);
92 BENCHMARK(SHA256
, 340);
93 BENCHMARK(SHA512
, 330);
95 BENCHMARK(SHA256_32b
, 4700 * 1000);
96 BENCHMARK(SipHash_32b
, 40 * 1000 * 1000);
97 BENCHMARK(FastRandom_32bit
, 110 * 1000 * 1000);
98 BENCHMARK(FastRandom_1bit
, 440 * 1000 * 1000);