1 // Copyright (c) 2016 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.
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()) {
49 for (int i
= 0; i
< 1000000; i
++) {
50 CSHA256().Write(in
.data(), in
.size()).Finalize(in
.data());
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
)
66 while (state
.KeepRunning()) {
67 for (int i
= 0; i
< 1000000; i
++) {
68 *((uint64_t*)x
.begin()) = SipHashUint256(0, i
, x
);
73 static void FastRandom_32bit(benchmark::State
& state
)
75 FastRandomContext
rng(true);
77 while (state
.KeepRunning()) {
78 for (int i
= 0; i
< 1000000; i
++) {
84 static void FastRandom_1bit(benchmark::State
& state
)
86 FastRandomContext
rng(true);
88 while (state
.KeepRunning()) {
89 for (int i
= 0; i
< 1000000; i
++) {
100 BENCHMARK(SHA256_32b
);
101 BENCHMARK(SipHash_32b
);
102 BENCHMARK(FastRandom_32bit
);
103 BENCHMARK(FastRandom_1bit
);