Bitcoin Core  21.99.0
P2P Digital Currency
crypto_hash.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2020 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 <crypto/muhash.h>
8 #include <crypto/ripemd160.h>
9 #include <crypto/sha1.h>
10 #include <crypto/sha256.h>
11 #include <crypto/sha3.h>
12 #include <crypto/sha512.h>
13 #include <crypto/siphash.h>
14 #include <hash.h>
15 #include <random.h>
16 #include <uint256.h>
17 
18 /* Number of bytes to hash per iteration */
19 static const uint64_t BUFFER_SIZE = 1000*1000;
20 
21 static void RIPEMD160(benchmark::Bench& bench)
22 {
23  uint8_t hash[CRIPEMD160::OUTPUT_SIZE];
24  std::vector<uint8_t> in(BUFFER_SIZE,0);
25  bench.batch(in.size()).unit("byte").run([&] {
26  CRIPEMD160().Write(in.data(), in.size()).Finalize(hash);
27  });
28 }
29 
30 static void SHA1(benchmark::Bench& bench)
31 {
32  uint8_t hash[CSHA1::OUTPUT_SIZE];
33  std::vector<uint8_t> in(BUFFER_SIZE,0);
34  bench.batch(in.size()).unit("byte").run([&] {
35  CSHA1().Write(in.data(), in.size()).Finalize(hash);
36  });
37 }
38 
39 static void SHA256(benchmark::Bench& bench)
40 {
41  uint8_t hash[CSHA256::OUTPUT_SIZE];
42  std::vector<uint8_t> in(BUFFER_SIZE,0);
43  bench.batch(in.size()).unit("byte").run([&] {
44  CSHA256().Write(in.data(), in.size()).Finalize(hash);
45  });
46 }
47 
48 static void SHA3_256_1M(benchmark::Bench& bench)
49 {
50  uint8_t hash[SHA3_256::OUTPUT_SIZE];
51  std::vector<uint8_t> in(BUFFER_SIZE,0);
52  bench.batch(in.size()).unit("byte").run([&] {
53  SHA3_256().Write(in).Finalize(hash);
54  });
55 }
56 
57 static void SHA256_32b(benchmark::Bench& bench)
58 {
59  std::vector<uint8_t> in(32,0);
60  bench.batch(in.size()).unit("byte").run([&] {
61  CSHA256()
62  .Write(in.data(), in.size())
63  .Finalize(in.data());
64  });
65 }
66 
67 static void SHA256D64_1024(benchmark::Bench& bench)
68 {
69  std::vector<uint8_t> in(64 * 1024, 0);
70  bench.batch(in.size()).unit("byte").run([&] {
71  SHA256D64(in.data(), in.data(), 1024);
72  });
73 }
74 
75 static void SHA512(benchmark::Bench& bench)
76 {
77  uint8_t hash[CSHA512::OUTPUT_SIZE];
78  std::vector<uint8_t> in(BUFFER_SIZE,0);
79  bench.batch(in.size()).unit("byte").run([&] {
80  CSHA512().Write(in.data(), in.size()).Finalize(hash);
81  });
82 }
83 
84 static void SipHash_32b(benchmark::Bench& bench)
85 {
86  uint256 x;
87  uint64_t k1 = 0;
88  bench.run([&] {
89  *((uint64_t*)x.begin()) = SipHashUint256(0, ++k1, x);
90  });
91 }
92 
94 {
95  FastRandomContext rng(true);
96  bench.run([&] {
97  rng.rand32();
98  });
99 }
100 
102 {
103  FastRandomContext rng(true);
104  bench.run([&] {
105  rng.randbool();
106  });
107 }
108 
109 static void MuHash(benchmark::Bench& bench)
110 {
111  MuHash3072 acc;
112  unsigned char key[32] = {0};
113  int i = 0;
114  bench.run([&] {
115  key[0] = ++i;
116  acc *= MuHash3072(key);
117  });
118 }
119 
120 static void MuHashMul(benchmark::Bench& bench)
121 {
122  MuHash3072 acc;
123  FastRandomContext rng(true);
124  MuHash3072 muhash{rng.randbytes(32)};
125 
126  bench.run([&] {
127  acc *= muhash;
128  });
129 }
130 
131 static void MuHashDiv(benchmark::Bench& bench)
132 {
133  MuHash3072 acc;
134  FastRandomContext rng(true);
135  MuHash3072 muhash{rng.randbytes(32)};
136 
137  for (size_t i = 0; i < bench.epochIterations(); ++i) {
138  acc *= muhash;
139  }
140 
141  bench.run([&] {
142  acc /= muhash;
143  });
144 }
145 
147 {
148  MuHash3072 acc;
149  FastRandomContext rng(true);
150  std::vector<unsigned char> key{rng.randbytes(32)};
151 
152  bench.run([&] {
153  MuHash3072{key};
154  });
155 }
156 
158 BENCHMARK(SHA1);
162 
168 
SHA3_256::OUTPUT_SIZE
static constexpr size_t OUTPUT_SIZE
Definition: sha3.h:33
ankerl::nanobench::Bench::batch
ANKERL_NANOBENCH(NODISCARD) std Bench & batch(T b) noexcept
Sets the batch size.
MuHashMul
static void MuHashMul(benchmark::Bench &bench)
Definition: crypto_hash.cpp:120
CSHA256::Write
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:637
CRIPEMD160
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
ankerl::nanobench::Bench::epochIterations
Bench & epochIterations(uint64_t numIters) noexcept
Sets exactly the number of iterations for each epoch.
sha1.h
SHA3_256
Definition: sha3.h:16
RIPEMD160
static void RIPEMD160(benchmark::Bench &bench)
Definition: crypto_hash.cpp:21
CRIPEMD160::Finalize
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:273
FastRandomContext::randbool
bool randbool() noexcept
Generate a random boolean.
Definition: random.h:211
uint256.h
MuHash
static void MuHash(benchmark::Bench &bench)
Definition: crypto_hash.cpp:109
sha512.h
SipHash_32b
static void SipHash_32b(benchmark::Bench &bench)
Definition: crypto_hash.cpp:84
FastRandomContext::randbytes
std::vector< unsigned char > randbytes(size_t len)
Generate random bytes.
Definition: random.cpp:626
CRIPEMD160::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
CSHA1::Finalize
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha1.cpp:180
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:614
BENCHMARK
BENCHMARK(RIPEMD160)
SHA256D64
void SHA256D64(unsigned char *out, const unsigned char *in, size_t blocks)
Compute multiple double-SHA256's of 64-byte blobs.
Definition: sha256.cpp:687
CSHA512::Finalize
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha512.cpp:185
FastRandom_32bit
static void FastRandom_32bit(benchmark::Bench &bench)
Definition: crypto_hash.cpp:93
muhash.h
siphash.h
random.h
SHA256D64_1024
static void SHA256D64_1024(benchmark::Bench &bench)
Definition: crypto_hash.cpp:67
CSHA512::OUTPUT_SIZE
static constexpr size_t OUTPUT_SIZE
Definition: sha512.h:20
SipHashUint256
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.cpp:94
CSHA1::Write
CSHA1 & Write(const unsigned char *data, size_t len)
Definition: sha1.cpp:154
FastRandomContext::rand32
uint32_t rand32() noexcept
Generate a random 32-bit integer.
Definition: random.h:205
SHA256_32b
static void SHA256_32b(benchmark::Bench &bench)
Definition: crypto_hash.cpp:57
CRIPEMD160::Write
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1181
ankerl::nanobench::Bench::unit
Bench & unit(char const *unit)
Sets the operation unit.
uint256
256-bit opaque blob.
Definition: uint256.h:124
CSHA256::Finalize
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:663
SHA1
static void SHA1(benchmark::Bench &bench)
Definition: crypto_hash.cpp:30
SHA3_256::Write
SHA3_256 & Write(Span< const unsigned char > data)
Definition: sha3.cpp:111
bench.h
FastRandom_1bit
static void FastRandom_1bit(benchmark::Bench &bench)
Definition: crypto_hash.cpp:101
CSHA512::Write
CSHA512 & Write(const unsigned char *data, size_t len)
Definition: sha512.cpp:159
SHA3_256_1M
static void SHA3_256_1M(benchmark::Bench &bench)
Definition: crypto_hash.cpp:48
ripemd160.h
CSHA1
A hasher class for SHA1.
Definition: sha1.h:12
CSHA256::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
sha256.h
SHA256
static void SHA256(benchmark::Bench &bench)
Definition: crypto_hash.cpp:39
SHA3_256::Finalize
SHA3_256 & Finalize(Span< unsigned char > output)
Definition: sha3.cpp:141
CSHA1::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: sha1.h:20
CSHA512
A hasher class for SHA-512.
Definition: sha512.h:12
CSHA256
A hasher class for SHA-256.
Definition: sha256.h:13
SHA512
static void SHA512(benchmark::Bench &bench)
Definition: crypto_hash.cpp:75
MuHashPrecompute
static void MuHashPrecompute(benchmark::Bench &bench)
Definition: crypto_hash.cpp:146
sha3.h
k1
static const unsigned char k1[32]
Definition: chacha_poly_aead.cpp:19
MuHashDiv
static void MuHashDiv(benchmark::Bench &bench)
Definition: crypto_hash.cpp:131
BUFFER_SIZE
static const uint64_t BUFFER_SIZE
Definition: crypto_hash.cpp:19
base_blob::begin
unsigned char * begin()
Definition: uint256.h:58
FastRandomContext
Fast randomness source.
Definition: random.h:119
MuHash3072
A class representing MuHash sets.
Definition: muhash.h:94