Bitcoin ABC  0.24.7
P2P Digital Currency
crypto_hash.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016 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 <bench/bench.h>
6 #include <crypto/ripemd160.h>
7 #include <crypto/sha1.h>
8 #include <crypto/sha256.h>
9 #include <crypto/sha3.h>
10 #include <crypto/sha512.h>
11 #include <crypto/siphash.h>
12 #include <hash.h>
13 #include <random.h>
14 #include <uint256.h>
15 
16 #include <string>
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  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 static void SHA1(benchmark::Bench &bench) {
29  uint8_t hash[CSHA1::OUTPUT_SIZE];
30  std::vector<uint8_t> in(BUFFER_SIZE, 0);
31  bench.batch(in.size()).unit("byte").run(
32  [&] { CSHA1().Write(in.data(), in.size()).Finalize(hash); });
33 }
34 
35 static void SHA256(benchmark::Bench &bench) {
36  uint8_t hash[CSHA256::OUTPUT_SIZE];
37  std::vector<uint8_t> in(BUFFER_SIZE, 0);
38  bench.batch(in.size()).unit("byte").run(
39  [&] { CSHA256().Write(in.data(), in.size()).Finalize(hash); });
40 }
41 
42 static void SHA3_256_1M(benchmark::Bench &bench) {
43  uint8_t hash[SHA3_256::OUTPUT_SIZE];
44  std::vector<uint8_t> in(BUFFER_SIZE, 0);
45  bench.batch(in.size()).unit("byte").run(
46  [&] { SHA3_256().Write(in).Finalize(hash); });
47 }
48 
49 static void SHA256_32b(benchmark::Bench &bench) {
50  std::vector<uint8_t> in(32, 0);
51  bench.batch(in.size()).unit("byte").run(
52  [&] { CSHA256().Write(in.data(), in.size()).Finalize(in.data()); });
53 }
54 
55 static void SHA256D64_1024(benchmark::Bench &bench) {
56  std::vector<uint8_t> in(64 * 1024, 0);
57  bench.batch(in.size()).unit("byte").run(
58  [&] { SHA256D64(in.data(), in.data(), 1024); });
59 }
60 
61 static void SHA512(benchmark::Bench &bench) {
62  uint8_t hash[CSHA512::OUTPUT_SIZE];
63  std::vector<uint8_t> in(BUFFER_SIZE, 0);
64  bench.batch(in.size()).unit("byte").run(
65  [&] { CSHA512().Write(in.data(), in.size()).Finalize(hash); });
66 }
67 
68 static void SipHash_32b(benchmark::Bench &bench) {
69  uint256 x;
70  uint64_t k1 = 0;
71  bench.run([&] {
72  uint64_t hash64 = SipHashUint256(0, ++k1, x);
73  std::memcpy(x.begin(), &hash64, sizeof(hash64));
74  });
75 }
76 
77 static void FastRandom_32bit(benchmark::Bench &bench) {
78  FastRandomContext rng(true);
79  bench.run([&] { rng.rand32(); });
80 }
81 
82 static void FastRandom_1bit(benchmark::Bench &bench) {
83  FastRandomContext rng(true);
84  bench.run([&] { rng.randbool(); });
85 }
86 
92 
SHA3_256::OUTPUT_SIZE
static constexpr size_t OUTPUT_SIZE
Definition: sha3.h:33
SHA3_256::Finalize
SHA3_256 & Finalize(Span< uint8_t > output)
Definition: sha3.cpp:232
ankerl::nanobench::Bench::batch
ANKERL_NANOBENCH(NODISCARD) std Bench & batch(T b) noexcept
Sets the batch size.
SHA256D64
void SHA256D64(uint8_t *out, const uint8_t *in, size_t blocks)
Compute multiple double-SHA256's of 64-byte blobs.
Definition: sha256.cpp:866
CRIPEMD160
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
k1
static const uint8_t k1[32]
Definition: chacha_poly_aead.cpp:20
sha1.h
SHA3_256
Definition: sha3.h:16
RIPEMD160
static void RIPEMD160(benchmark::Bench &bench)
Definition: crypto_hash.cpp:21
FastRandomContext::randbool
bool randbool() noexcept
Generate a random boolean.
Definition: random.h:229
uint256.h
sha512.h
SipHash_32b
static void SipHash_32b(benchmark::Bench &bench)
Definition: crypto_hash.cpp:68
CRIPEMD160::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:19
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:583
BENCHMARK
BENCHMARK(RIPEMD160)
CSHA1::Finalize
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha1.cpp:179
FastRandom_32bit
static void FastRandom_32bit(benchmark::Bench &bench)
Definition: crypto_hash.cpp:77
siphash.h
random.h
SHA256D64_1024
static void SHA256D64_1024(benchmark::Bench &bench)
Definition: crypto_hash.cpp:55
CSHA256::Finalize
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha256.cpp:844
CSHA512::OUTPUT_SIZE
static constexpr size_t OUTPUT_SIZE
Definition: sha512.h:19
SipHashUint256
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.cpp:99
FastRandomContext::rand32
uint32_t rand32() noexcept
Generate a random 32-bit integer.
Definition: random.h:220
SHA256_32b
static void SHA256_32b(benchmark::Bench &bench)
Definition: crypto_hash.cpp:49
CSHA512::Finalize
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha512.cpp:273
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
ankerl::nanobench::Bench::unit
Bench & unit(char const *unit)
Sets the operation unit.
CRIPEMD160::Write
CRIPEMD160 & Write(const uint8_t *data, size_t len)
Definition: ripemd160.cpp:286
uint256
256-bit opaque blob.
Definition: uint256.h:127
CRIPEMD160::Finalize
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:311
SHA1
static void SHA1(benchmark::Bench &bench)
Definition: crypto_hash.cpp:28
CSHA1::Write
CSHA1 & Write(const uint8_t *data, size_t len)
Definition: sha1.cpp:154
bench.h
base_blob::begin
uint8_t * begin()
Definition: uint256.h:83
FastRandom_1bit
static void FastRandom_1bit(benchmark::Bench &bench)
Definition: crypto_hash.cpp:82
SHA3_256_1M
static void SHA3_256_1M(benchmark::Bench &bench)
Definition: crypto_hash.cpp:42
ripemd160.h
CSHA1
A hasher class for SHA1.
Definition: sha1.h:12
CSHA256::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: sha256.h:20
sha256.h
SHA256
static void SHA256(benchmark::Bench &bench)
Definition: crypto_hash.cpp:35
CSHA1::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: sha1.h:19
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:61
CSHA256::Write
CSHA256 & Write(const uint8_t *data, size_t len)
Definition: sha256.cpp:819
sha3.h
BUFFER_SIZE
static const uint64_t BUFFER_SIZE
Definition: crypto_hash.cpp:19
CSHA512::Write
CSHA512 & Write(const uint8_t *data, size_t len)
Definition: sha512.cpp:248
FastRandomContext
Fast randomness source.
Definition: random.h:129
SHA3_256::Write
SHA3_256 & Write(Span< const uint8_t > data)
Definition: sha3.cpp:202