BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
crypto_hash.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2018 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 <iostream>
6 
7 #include <bench/bench.h>
8 #include <bloom.h>
9 #include <hash.h>
10 #include <random.h>
11 #include <uint256.h>
12 #include <utiltime.h>
13 #include <crypto/ripemd160.h>
14 #include <crypto/sha1.h>
15 #include <crypto/sha256.h>
16 #include <crypto/sha512.h>
17 #include <crypto/sha3.h>
18 #include <crypto/sha3512.h>
19 
20 /* Number of bytes to hash per iteration */
21 static const uint64_t BUFFER_SIZE = 1000*1000;
22 
23 static void RIPEMD160(benchmark::State& state)
24 {
25  uint8_t hash[CRIPEMD160::OUTPUT_SIZE];
26  std::vector<uint8_t> in(BUFFER_SIZE,0);
27  while (state.KeepRunning())
28  CRIPEMD160().Write(in.data(), in.size()).Finalize(hash);
29 }
30 
31 static void SHA1(benchmark::State& state)
32 {
33  uint8_t hash[CSHA1::OUTPUT_SIZE];
34  std::vector<uint8_t> in(BUFFER_SIZE,0);
35  while (state.KeepRunning())
36  CSHA1().Write(in.data(), in.size()).Finalize(hash);
37 }
38 
39 static void SHA256(benchmark::State& state)
40 {
41  uint8_t hash[CSHA256::OUTPUT_SIZE];
42  std::vector<uint8_t> in(BUFFER_SIZE,0);
43  while (state.KeepRunning())
44  CSHA256().Write(in.data(), in.size()).Finalize(hash);
45 }
46 
47 static void SHA3(benchmark::State& state) // SHA3-256
48 {
49  uint8_t hash[CSHA3::OUTPUT_SIZE];
50  std::vector<uint8_t> in(BUFFER_SIZE,0);
51  while (state.KeepRunning())
52  CSHA3().Write(in.data(), in.size()).Finalize(hash);
53 }
54 
55 static void SHA256_32b(benchmark::State& state)
56 {
57  std::vector<uint8_t> in(32,0);
58  while (state.KeepRunning()) {
59  CSHA256()
60  .Write(in.data(), in.size())
61  .Finalize(in.data());
62  }
63 }
64 
65 static void SHA3_32b(benchmark::State& state)
66 {
67  std::vector<uint8_t> in(32,0);
68  while (state.KeepRunning()) {
69  CSHA3()
70  .Write(in.data(), in.size())
71  .Finalize(in.data());
72  }
73 }
74 
75 static void SHA256D64_1024(benchmark::State& state)
76 {
77  std::vector<uint8_t> in(64 * 1024, 0);
78  while (state.KeepRunning()) {
79  SHA256D64(in.data(), in.data(), 1024);
80  }
81 }
82 
83 /* TODO
84 static void SHA3D64_1024(benchmark::State& state)
85 {
86  std::vector<uint8_t> in(64 * 1024, 0);
87  while (state.KeepRunning()) {
88  SHA3D64(in.data(), in.data(), 1024);
89  }
90 }
91 */
92 
93 static void SHA512(benchmark::State& state)
94 {
95  uint8_t hash[CSHA512::OUTPUT_SIZE];
96  std::vector<uint8_t> in(BUFFER_SIZE,0);
97  while (state.KeepRunning())
98  CSHA512().Write(in.data(), in.size()).Finalize(hash);
99 }
100 
101 static void SHA3512(benchmark::State& state)
102 {
103  uint8_t hash[CSHA3512::OUTPUT_SIZE];
104  std::vector<uint8_t> in(BUFFER_SIZE,0);
105  while (state.KeepRunning())
106  CSHA3512().Write(in.data(), in.size()).Finalize(hash);
107 }
108 
109 static void SipHash_32b(benchmark::State& state)
110 {
111  uint256 x;
112  uint64_t k1 = 0;
113  while (state.KeepRunning()) {
114  *((uint64_t*)x.begin()) = SipHashUint256(0, ++k1, x);
115  }
116 }
117 
118 static void FastRandom_32bit(benchmark::State& state)
119 {
120  FastRandomContext rng(true);
121  while (state.KeepRunning()) {
122  rng.rand32();
123  }
124 }
125 
126 static void FastRandom_1bit(benchmark::State& state)
127 {
128  FastRandomContext rng(true);
129  while (state.KeepRunning()) {
130  rng.randbool();
131  }
132 }
133 
134 BENCHMARK(RIPEMD160, 440);
135 BENCHMARK(SHA1, 570);
136 BENCHMARK(SHA256, 340);
137 BENCHMARK(SHA512, 330);
138 
139 BENCHMARK(SHA256_32b, 4700 * 1000);
140 BENCHMARK(SipHash_32b, 40 * 1000 * 1000);
141 BENCHMARK(SHA256D64_1024, 7400);
142 BENCHMARK(FastRandom_32bit, 110 * 1000 * 1000);
143 BENCHMARK(FastRandom_1bit, 440 * 1000 * 1000);
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:648
CSHA3512 & Write(const unsigned char *data, size_t len)
Definition: sha3512.cpp:30
CSHA1 & Write(const unsigned char *data, size_t len)
Definition: sha1.cpp:154
static const size_t OUTPUT_SIZE
Definition: sha3512.h:23
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: hash.cpp:168
bool KeepRunning()
Definition: bench.h:70
unsigned char * begin()
Definition: uint256.h:56
static const size_t OUTPUT_SIZE
Definition: sha512.h:20
BENCHMARK(RIPEMD160, 440)
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
Fast randomness source.
Definition: random.h:45
A hasher class for SHA3-512.
Definition: sha3512.h:14
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
A hasher class for SHA3-256.
Definition: sha3.h:14
256-bit opaque blob.
Definition: uint256.h:122
CSHA512 & Write(const unsigned char *data, size_t len)
Definition: sha512.cpp:159
CSHA3 & Write(const unsigned char *data, size_t len)
Definition: sha3.cpp:30
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
A hasher class for SHA1.
Definition: sha1.h:12
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 const size_t OUTPUT_SIZE
Definition: sha1.h:20
A hasher class for SHA-256.
Definition: sha256.h:13
static const size_t OUTPUT_SIZE
Definition: sha3.h:23
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12