Bitcoin ABC  0.22.13
P2P Digital Currency
crypto_hash.cpp
Go to the documentation of this file.
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.
4 
5 #include <bench/bench.h>
6 #include <crypto/ripemd160.h>
7 #include <crypto/sha1.h>
8 #include <crypto/sha256.h>
9 #include <crypto/sha512.h>
10 #include <crypto/siphash.h>
11 #include <hash.h>
12 #include <random.h>
13 #include <uint256.h>
14 
15 #include <string>
16 
17 /* Number of bytes to hash per iteration */
18 static const uint64_t BUFFER_SIZE = 1000 * 1000;
19 
20 static void RIPEMD160(benchmark::State &state) {
21  uint8_t hash[CRIPEMD160::OUTPUT_SIZE];
22  std::vector<uint8_t> in(BUFFER_SIZE, 0);
23  while (state.KeepRunning()) {
24  CRIPEMD160().Write(in.data(), in.size()).Finalize(hash);
25  }
26 }
27 
28 static void SHA1(benchmark::State &state) {
29  uint8_t hash[CSHA1::OUTPUT_SIZE];
30  std::vector<uint8_t> in(BUFFER_SIZE, 0);
31  while (state.KeepRunning()) {
32  CSHA1().Write(in.data(), in.size()).Finalize(hash);
33  }
34 }
35 
36 static void SHA256(benchmark::State &state) {
37  uint8_t hash[CSHA256::OUTPUT_SIZE];
38  std::vector<uint8_t> in(BUFFER_SIZE, 0);
39  while (state.KeepRunning()) {
40  CSHA256().Write(in.data(), in.size()).Finalize(hash);
41  }
42 }
43 
44 static void SHA256_32b(benchmark::State &state) {
45  std::vector<uint8_t> in(32, 0);
46  while (state.KeepRunning()) {
47  CSHA256().Write(in.data(), in.size()).Finalize(in.data());
48  }
49 }
50 
51 static void SHA256D64_1024(benchmark::State &state) {
52  std::vector<uint8_t> in(64 * 1024, 0);
53  while (state.KeepRunning()) {
54  SHA256D64(in.data(), in.data(), 1024);
55  }
56 }
57 
58 static void SHA512(benchmark::State &state) {
59  uint8_t hash[CSHA512::OUTPUT_SIZE];
60  std::vector<uint8_t> in(BUFFER_SIZE, 0);
61  while (state.KeepRunning()) {
62  CSHA512().Write(in.data(), in.size()).Finalize(hash);
63  }
64 }
65 
66 static void SipHash_32b(benchmark::State &state) {
67  uint256 x;
68  uint64_t k1 = 0;
69  while (state.KeepRunning()) {
70  uint64_t hash64 = SipHashUint256(0, ++k1, x);
71  std::memcpy(x.begin(), &hash64, sizeof(hash64));
72  }
73 }
74 
75 static void FastRandom_32bit(benchmark::State &state) {
76  FastRandomContext rng(true);
77  while (state.KeepRunning()) {
78  rng.rand32();
79  }
80 }
81 
82 static void FastRandom_1bit(benchmark::State &state) {
83  FastRandomContext rng(true);
84  while (state.KeepRunning()) {
85  rng.randbool();
86  }
87 }
88 
89 BENCHMARK(RIPEMD160, 440);
90 BENCHMARK(SHA1, 570);
91 BENCHMARK(SHA256, 340);
92 BENCHMARK(SHA512, 330);
93 
94 BENCHMARK(SHA256_32b, 4700 * 1000);
95 BENCHMARK(SipHash_32b, 40 * 1000 * 1000);
97 BENCHMARK(FastRandom_32bit, 110 * 1000 * 1000);
98 BENCHMARK(FastRandom_1bit, 440 * 1000 * 1000);
CSHA512 & Write(const uint8_t *data, size_t len)
Definition: sha512.cpp:248
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.cpp:99
static void RIPEMD160(benchmark::State &state)
Definition: crypto_hash.cpp:20
bool KeepRunning()
Definition: bench.h:71
static const uint64_t BUFFER_SIZE
Definition: crypto_hash.cpp:18
static void SipHash_32b(benchmark::State &state)
Definition: crypto_hash.cpp:66
CRIPEMD160 & Write(const uint8_t *data, size_t len)
Definition: ripemd160.cpp:286
static void FastRandom_1bit(benchmark::State &state)
Definition: crypto_hash.cpp:82
BENCHMARK(RIPEMD160, 440)
static void SHA256(benchmark::State &state)
Definition: crypto_hash.cpp:36
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:19
Fast randomness source.
Definition: random.h:113
static void SHA512(benchmark::State &state)
Definition: crypto_hash.cpp:58
uint8_t * begin()
Definition: uint256.h:76
static constexpr size_t OUTPUT_SIZE
Definition: sha512.h:19
static const uint8_t k1[32]
void SHA256D64(uint8_t *out, const uint8_t *in, size_t blocks)
Compute multiple double-SHA256&#39;s of 64-byte blobs.
Definition: sha256.cpp:866
uint32_t rand32() noexcept
Generate a random 32-bit integer.
Definition: random.h:204
256-bit opaque blob.
Definition: uint256.h:120
static void SHA256D64_1024(benchmark::State &state)
Definition: crypto_hash.cpp:51
CSHA256 & Write(const uint8_t *data, size_t len)
Definition: sha256.cpp:819
static const size_t OUTPUT_SIZE
Definition: sha256.h:20
A hasher class for SHA1.
Definition: sha1.h:12
bool randbool() noexcept
Generate a random boolean.
Definition: random.h:213
static void FastRandom_32bit(benchmark::State &state)
Definition: crypto_hash.cpp:75
CSHA1 & Write(const uint8_t *data, size_t len)
Definition: sha1.cpp:154
A hasher class for SHA-512.
Definition: sha512.h:12
static const size_t OUTPUT_SIZE
Definition: sha1.h:19
A hasher class for SHA-256.
Definition: sha256.h:13
static void SHA256_32b(benchmark::State &state)
Definition: crypto_hash.cpp:44
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
static void SHA1(benchmark::State &state)
Definition: crypto_hash.cpp:28