Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

crypto_hash.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016 The Bitcoin Core developers
2 // Copyright (c) 2018-2020 The Dash Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <iostream>
7 
8 #include <bench/bench.h>
9 #include <bloom.h>
10 #include <hash.h>
11 #include <random.h>
12 #include <uint256.h>
13 #include <utiltime.h>
14 #include <crypto/ripemd160.h>
15 #include <crypto/sha1.h>
16 #include <crypto/sha256.h>
17 #include <crypto/sha512.h>
18 
19 /* Number of bytes to hash per iteration */
20 static const uint64_t BUFFER_SIZE = 1000*1000;
21 
22 static void HASH_RIPEMD160(benchmark::State& state)
23 {
24  uint8_t hash[CRIPEMD160::OUTPUT_SIZE];
25  std::vector<uint8_t> in(BUFFER_SIZE,0);
26  while (state.KeepRunning())
27  CRIPEMD160().Write(in.data(), in.size()).Finalize(hash);
28 }
29 
30 static void HASH_SHA1(benchmark::State& state)
31 {
32  uint8_t hash[CSHA1::OUTPUT_SIZE];
33  std::vector<uint8_t> in(BUFFER_SIZE,0);
34  while (state.KeepRunning())
35  CSHA1().Write(in.data(), in.size()).Finalize(hash);
36 }
37 
38 static void HASH_SHA256(benchmark::State& state)
39 {
40  uint8_t hash[CSHA256::OUTPUT_SIZE];
41  std::vector<uint8_t> in(BUFFER_SIZE,0);
42  while (state.KeepRunning())
43  CSHA256().Write(in.data(), in.size()).Finalize(hash);
44 }
45 
47 {
48  std::vector<uint8_t> in(32,0);
49  while (state.KeepRunning()) {
50  for (int i = 0; i < 1000000; i++) {
51  CSHA256().Write(in.data(), in.size()).Finalize(in.data());
52  }
53  }
54 }
55 
56 static void HASH_DSHA256(benchmark::State& state)
57 {
58  uint8_t hash[CSHA256::OUTPUT_SIZE];
59  std::vector<uint8_t> in(BUFFER_SIZE,0);
60  while (state.KeepRunning())
61  CHash256().Write(in.data(), in.size()).Finalize(hash);
62 }
63 
65 {
66  std::vector<uint8_t> in(32,0);
67  while (state.KeepRunning()) {
68  for (int i = 0; i < 1000000; i++) {
69  CHash256().Write(in.data(), in.size()).Finalize(in.data());
70  }
71  }
72 }
73 
75 {
76  std::vector<uint8_t> in(64 * 1024, 0);
77  while (state.KeepRunning()) {
78  SHA256D64(in.data(), in.data(), 1024);
79  }
80 }
81 
82 static void HASH_SHA512(benchmark::State& state)
83 {
84  uint8_t hash[CSHA512::OUTPUT_SIZE];
85  std::vector<uint8_t> in(BUFFER_SIZE,0);
86  while (state.KeepRunning())
87  CSHA512().Write(in.data(), in.size()).Finalize(hash);
88 }
89 
91 {
92  uint256 x;
93  while (state.KeepRunning()) {
94  for (int i = 0; i < 1000000; i++) {
95  *((uint64_t*)x.begin()) = SipHashUint256(0, i, x);
96  }
97  }
98 }
99 
101 {
102  FastRandomContext rng(true);
103  uint32_t x = 0;
104  while (state.KeepRunning()) {
105  for (int i = 0; i < 1000000; i++) {
106  x += rng.rand32();
107  }
108  }
109 }
110 
112 {
113  FastRandomContext rng(true);
114  uint32_t x = 0;
115  while (state.KeepRunning()) {
116  for (int i = 0; i < 1000000; i++) {
117  x += rng.randbool();
118  }
119  }
120 }
121 
123 {
124  std::vector<uint8_t> in(32,0);
125  while (state.KeepRunning())
126  CHash256().Write(in.data(), in.size()).Finalize(in.data());
127 }
128 
130 {
131  std::vector<uint8_t> in(80,0);
132  while (state.KeepRunning())
133  CHash256().Write(in.data(), in.size()).Finalize(in.data());
134 }
135 
137 {
138  std::vector<uint8_t> in(128,0);
139  while (state.KeepRunning())
140  CHash256().Write(in.data(), in.size()).Finalize(in.data());
141 }
142 
144 {
145  std::vector<uint8_t> in(512,0);
146  while (state.KeepRunning())
147  CHash256().Write(in.data(), in.size()).Finalize(in.data());
148 }
149 
151 {
152  std::vector<uint8_t> in(1024,0);
153  while (state.KeepRunning())
154  CHash256().Write(in.data(), in.size()).Finalize(in.data());
155 }
156 
158 {
159  std::vector<uint8_t> in(2048,0);
160  while (state.KeepRunning())
161  CHash256().Write(in.data(), in.size()).Finalize(in.data());
162 }
163 
164 static void HASH_X11(benchmark::State& state)
165 {
166  uint256 hash;
167  std::vector<uint8_t> in(BUFFER_SIZE,0);
168  while (state.KeepRunning())
169  hash = HashX11(in.begin(), in.end());
170 }
171 
173 {
174  uint256 hash;
175  std::vector<uint8_t> in(32,0);
176  while (state.KeepRunning())
177  hash = HashX11(in.begin(), in.end());
178 }
179 
181 {
182  uint256 hash;
183  std::vector<uint8_t> in(80,0);
184  while (state.KeepRunning())
185  hash = HashX11(in.begin(), in.end());
186 }
187 
189 {
190  uint256 hash;
191  std::vector<uint8_t> in(128,0);
192  while (state.KeepRunning())
193  hash = HashX11(in.begin(), in.end());
194 }
195 
197 {
198  uint256 hash;
199  std::vector<uint8_t> in(512,0);
200  while (state.KeepRunning())
201  hash = HashX11(in.begin(), in.end());
202 }
203 
205 {
206  uint256 hash;
207  std::vector<uint8_t> in(1024,0);
208  while (state.KeepRunning())
209  hash = HashX11(in.begin(), in.end());
210 }
211 
213 {
214  uint256 hash;
215  std::vector<uint8_t> in(2048,0);
216  while (state.KeepRunning())
217  hash = HashX11(in.begin(), in.end());
218 }
219 
226 
230 BENCHMARK(HASH_SHA256D64_1024/*, 7400*/);
231 
static void HASH_DSHA256_1024b_single(benchmark::State &state)
static void HASH_X11_0032b_single(benchmark::State &state)
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:648
static void HASH_DSHA256_0032b_single(benchmark::State &state)
CSHA1 & Write(const unsigned char *data, size_t len)
Definition: sha1.cpp:154
static void HASH_DSHA256_0032b(benchmark::State &state)
Definition: crypto_hash.cpp:64
static void HASH_DSHA256(benchmark::State &state)
Definition: crypto_hash.cpp:56
uint32_t rand32(uint32_t nMax)
Definition: random.h:117
static void HASH_X11_0080b_single(benchmark::State &state)
static void HASH_SipHash_0032b(benchmark::State &state)
Definition: crypto_hash.cpp:90
CHash256 & Write(const unsigned char *data, size_t len)
Definition: hash.h:47
static void HASH_SHA512(benchmark::State &state)
Definition: crypto_hash.cpp:82
static void HASH_DSHA256_2048b_single(benchmark::State &state)
BENCHMARK(HASH_RIPEMD160)
static void HASH_DSHA256_0128b_single(benchmark::State &state)
bool KeepRunning()
Definition: bench.cpp:39
static void HASH_X11_0512b_single(benchmark::State &state)
static void HASH_DSHA256_0512b_single(benchmark::State &state)
A hasher class for Bitcoin&#39;s 256-bit hash (double SHA-256).
Definition: hash.h:35
static const uint64_t BUFFER_SIZE
Definition: crypto_hash.cpp:20
unsigned char * begin()
Definition: uint256.h:57
static void FastRandom_1bit(benchmark::State &state)
static const size_t OUTPUT_SIZE
Definition: sha512.h:20
static void HASH_SHA256_0032b(benchmark::State &state)
Definition: crypto_hash.cpp:46
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
Fast randomness source.
Definition: random.h:48
static void HASH_SHA256D64_1024(benchmark::State &state)
Definition: crypto_hash.cpp:74
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
256-bit opaque blob.
Definition: uint256.h:123
bool randbool()
Generate a random boolean.
Definition: random.h:135
static void HASH_X11(benchmark::State &state)
static void HASH_DSHA256_0080b_single(benchmark::State &state)
CSHA512 & Write(const unsigned char *data, size_t len)
Definition: sha512.cpp:159
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: hash.cpp:168
static void HASH_X11_2048b_single(benchmark::State &state)
static void HASH_X11_1024b_single(benchmark::State &state)
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
A hasher class for SHA1.
Definition: sha1.h:12
static void FastRandom_32bit(benchmark::State &state)
static void HASH_SHA256(benchmark::State &state)
Definition: crypto_hash.cpp:38
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:698
A hasher class for SHA-512.
Definition: sha512.h:12
static void HASH_SHA1(benchmark::State &state)
Definition: crypto_hash.cpp:30
static void HASH_RIPEMD160(benchmark::State &state)
Definition: crypto_hash.cpp:22
static const size_t OUTPUT_SIZE
Definition: sha1.h:20
A hasher class for SHA-256.
Definition: sha256.h:13
static void HASH_X11_0128b_single(benchmark::State &state)
uint256 HashX11(const T1 pbegin, const T1 pend)
Definition: hash.h:302
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
Released under the MIT license