Bitcoin Core  0.20.99
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/ripemd160.h>
8 #include <crypto/sha1.h>
9 #include <crypto/sha256.h>
10 #include <crypto/sha3.h>
11 #include <crypto/sha512.h>
12 #include <crypto/siphash.h>
13 #include <hash.h>
14 #include <random.h>
15 #include <uint256.h>
16 
17 /* Number of bytes to hash per iteration */
18 static const uint64_t BUFFER_SIZE = 1000*1000;
19 
20 static void RIPEMD160(benchmark::Bench& bench)
21 {
22  uint8_t hash[CRIPEMD160::OUTPUT_SIZE];
23  std::vector<uint8_t> in(BUFFER_SIZE,0);
24  bench.batch(in.size()).unit("byte").run([&] {
25  CRIPEMD160().Write(in.data(), in.size()).Finalize(hash);
26  });
27 }
28 
29 static void SHA1(benchmark::Bench& bench)
30 {
31  uint8_t hash[CSHA1::OUTPUT_SIZE];
32  std::vector<uint8_t> in(BUFFER_SIZE,0);
33  bench.batch(in.size()).unit("byte").run([&] {
34  CSHA1().Write(in.data(), in.size()).Finalize(hash);
35  });
36 }
37 
38 static void SHA256(benchmark::Bench& bench)
39 {
40  uint8_t hash[CSHA256::OUTPUT_SIZE];
41  std::vector<uint8_t> in(BUFFER_SIZE,0);
42  bench.batch(in.size()).unit("byte").run([&] {
43  CSHA256().Write(in.data(), in.size()).Finalize(hash);
44  });
45 }
46 
47 static void SHA3_256_1M(benchmark::Bench& bench)
48 {
49  uint8_t hash[SHA3_256::OUTPUT_SIZE];
50  std::vector<uint8_t> in(BUFFER_SIZE,0);
51  bench.batch(in.size()).unit("byte").run([&] {
52  SHA3_256().Write(in).Finalize(hash);
53  });
54 }
55 
56 static void SHA256_32b(benchmark::Bench& bench)
57 {
58  std::vector<uint8_t> in(32,0);
59  bench.batch(in.size()).unit("byte").run([&] {
60  CSHA256()
61  .Write(in.data(), in.size())
62  .Finalize(in.data());
63  });
64 }
65 
66 static void SHA256D64_1024(benchmark::Bench& bench)
67 {
68  std::vector<uint8_t> in(64 * 1024, 0);
69  bench.batch(in.size()).unit("byte").run([&] {
70  SHA256D64(in.data(), in.data(), 1024);
71  });
72 }
73 
74 static void SHA512(benchmark::Bench& bench)
75 {
76  uint8_t hash[CSHA512::OUTPUT_SIZE];
77  std::vector<uint8_t> in(BUFFER_SIZE,0);
78  bench.batch(in.size()).unit("byte").run([&] {
79  CSHA512().Write(in.data(), in.size()).Finalize(hash);
80  });
81 }
82 
83 static void SipHash_32b(benchmark::Bench& bench)
84 {
85  uint256 x;
86  uint64_t k1 = 0;
87  bench.run([&] {
88  *((uint64_t*)x.begin()) = SipHashUint256(0, ++k1, x);
89  });
90 }
91 
93 {
94  FastRandomContext rng(true);
95  bench.run([&] {
96  rng.rand32();
97  });
98 }
99 
101 {
102  FastRandomContext rng(true);
103  bench.run([&] {
104  rng.randbool();
105  });
106 }
107 
109 BENCHMARK(SHA1);
113 
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:637
BENCHMARK(RIPEMD160)
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.cpp:94
SHA3_256 & Write(Span< const unsigned char > data)
Definition: sha3.cpp:111
CSHA1 & Write(const unsigned char *data, size_t len)
Definition: sha1.cpp:154
static void SHA1(benchmark::Bench &bench)
Definition: crypto_hash.cpp:29
Definition: sha3.h:16
static void SipHash_32b(benchmark::Bench &bench)
Definition: crypto_hash.cpp:83
static constexpr size_t OUTPUT_SIZE
Definition: sha3.h:33
SHA3_256 & Finalize(Span< unsigned char > output)
Definition: sha3.cpp:141
static const uint64_t BUFFER_SIZE
Definition: crypto_hash.cpp:18
static void FastRandom_1bit(benchmark::Bench &bench)
unsigned char * begin()
Definition: uint256.h:58
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
static void RIPEMD160(benchmark::Bench &bench)
Definition: crypto_hash.cpp:20
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
Fast randomness source.
Definition: random.h:119
static const unsigned char k1[32]
static void SHA3_256_1M(benchmark::Bench &bench)
Definition: crypto_hash.cpp:47
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:205
256-bit opaque blob.
Definition: uint256.h:124
static void SHA256D64_1024(benchmark::Bench &bench)
Definition: crypto_hash.cpp:66
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:211
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:583
static void SHA512(benchmark::Bench &bench)
Definition: crypto_hash.cpp:74
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::Bench &bench)
Definition: crypto_hash.cpp:56
static void SHA256(benchmark::Bench &bench)
Definition: crypto_hash.cpp:38
ANKERL_NANOBENCH(NODISCARD) std Bench & batch(T b) noexcept
Sets the batch size.
static void FastRandom_32bit(benchmark::Bench &bench)
Definition: crypto_hash.cpp:92
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12