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