Bitcoin ABC 0.30.5
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/muhash.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#include <string>
18
19/* Number of bytes to hash per iteration */
20static const uint64_t BUFFER_SIZE = 1000 * 1000;
21
22static void RIPEMD160(benchmark::Bench &bench) {
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
29static void SHA1(benchmark::Bench &bench) {
30 uint8_t hash[CSHA1::OUTPUT_SIZE];
31 std::vector<uint8_t> in(BUFFER_SIZE, 0);
32 bench.batch(in.size()).unit("byte").run(
33 [&] { CSHA1().Write(in.data(), in.size()).Finalize(hash); });
34}
35
36static void SHA256(benchmark::Bench &bench) {
37 uint8_t hash[CSHA256::OUTPUT_SIZE];
38 std::vector<uint8_t> in(BUFFER_SIZE, 0);
39 bench.batch(in.size()).unit("byte").run(
40 [&] { CSHA256().Write(in.data(), in.size()).Finalize(hash); });
41}
42
43static void SHA3_256_1M(benchmark::Bench &bench) {
44 uint8_t hash[SHA3_256::OUTPUT_SIZE];
45 std::vector<uint8_t> in(BUFFER_SIZE, 0);
46 bench.batch(in.size()).unit("byte").run(
47 [&] { SHA3_256().Write(in).Finalize(hash); });
48}
49
50static void SHA256_32b(benchmark::Bench &bench) {
51 std::vector<uint8_t> in(32, 0);
52 bench.batch(in.size()).unit("byte").run(
53 [&] { CSHA256().Write(in.data(), in.size()).Finalize(in.data()); });
54}
55
56static void SHA256D64_1024(benchmark::Bench &bench) {
57 std::vector<uint8_t> in(64 * 1024, 0);
58 bench.batch(in.size()).unit("byte").run(
59 [&] { SHA256D64(in.data(), in.data(), 1024); });
60}
61
62static void SHA512(benchmark::Bench &bench) {
63 uint8_t hash[CSHA512::OUTPUT_SIZE];
64 std::vector<uint8_t> in(BUFFER_SIZE, 0);
65 bench.batch(in.size()).unit("byte").run(
66 [&] { CSHA512().Write(in.data(), in.size()).Finalize(hash); });
67}
68
69static void SipHash_32b(benchmark::Bench &bench) {
70 uint256 x;
71 uint64_t k1 = 0;
72 bench.run([&] {
73 uint64_t hash64 = SipHashUint256(0, ++k1, x);
74 std::memcpy(x.begin(), &hash64, sizeof(hash64));
75 });
76}
77
79 FastRandomContext rng(true);
80 bench.run([&] { rng.rand32(); });
81}
82
83static void FastRandom_1bit(benchmark::Bench &bench) {
84 FastRandomContext rng(true);
85 bench.run([&] { rng.randbool(); });
86}
87
88static void MuHash(benchmark::Bench &bench) {
89 MuHash3072 acc;
90 uint8_t key[32] = {0};
91 uint32_t i = 0;
92 bench.run([&] {
93 key[0] = ++i & 0xFF;
94 acc *= MuHash3072(key);
95 });
96}
97
98static void MuHashMul(benchmark::Bench &bench) {
99 MuHash3072 acc;
100 FastRandomContext rng(true);
101 MuHash3072 muhash{rng.randbytes(32)};
102
103 bench.run([&] { acc *= muhash; });
104}
105
106static void MuHashDiv(benchmark::Bench &bench) {
107 MuHash3072 acc;
108 FastRandomContext rng(true);
109 MuHash3072 muhash{rng.randbytes(32)};
110
111 bench.run([&] { acc /= muhash; });
112}
113
115 MuHash3072 acc;
116 FastRandomContext rng(true);
117 std::vector<uint8_t> key{rng.randbytes(32)};
118
119 bench.run([&] { MuHash3072{key}; });
120}
121
127
133
static const uint8_t k1[32]
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
CRIPEMD160 & Write(const uint8_t *data, size_t len)
Definition: ripemd160.cpp:288
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:313
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:19
A hasher class for SHA1.
Definition: sha1.h:12
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha1.cpp:183
CSHA1 & Write(const uint8_t *data, size_t len)
Definition: sha1.cpp:158
static const size_t OUTPUT_SIZE
Definition: sha1.h:19
A hasher class for SHA-256.
Definition: sha256.h:13
CSHA256 & Write(const uint8_t *data, size_t len)
Definition: sha256.cpp:819
static const size_t OUTPUT_SIZE
Definition: sha256.h:20
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha256.cpp:844
A hasher class for SHA-512.
Definition: sha512.h:12
CSHA512 & Write(const uint8_t *data, size_t len)
Definition: sha512.cpp:248
static constexpr size_t OUTPUT_SIZE
Definition: sha512.h:19
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha512.cpp:273
Fast randomness source.
Definition: random.h:156
uint32_t rand32() noexcept
Generate a random 32-bit integer.
Definition: random.h:247
std::vector< uint8_t > randbytes(size_t len)
Generate random bytes.
Definition: random.cpp:690
bool randbool() noexcept
Generate a random boolean.
Definition: random.h:256
A class representing MuHash sets.
Definition: muhash.h:96
Definition: sha3.h:16
SHA3_256 & Finalize(Span< uint8_t > output)
Definition: sha3.cpp:232
SHA3_256 & Write(Span< const uint8_t > data)
Definition: sha3.cpp:202
static constexpr size_t OUTPUT_SIZE
Definition: sha3.h:33
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
ANKERL_NANOBENCH(NODISCARD) std Bench & batch(T b) noexcept
Sets the batch size.
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
Bench & unit(char const *unit)
Sets the operation unit.
uint8_t * begin()
Definition: uint256.h:85
256-bit opaque blob.
Definition: uint256.h:129
static void RIPEMD160(benchmark::Bench &bench)
Definition: crypto_hash.cpp:22
static void SipHash_32b(benchmark::Bench &bench)
Definition: crypto_hash.cpp:69
static void FastRandom_1bit(benchmark::Bench &bench)
Definition: crypto_hash.cpp:83
static void MuHashPrecompute(benchmark::Bench &bench)
static const uint64_t BUFFER_SIZE
Definition: crypto_hash.cpp:20
static void SHA256_32b(benchmark::Bench &bench)
Definition: crypto_hash.cpp:50
static void FastRandom_32bit(benchmark::Bench &bench)
Definition: crypto_hash.cpp:78
static void MuHashMul(benchmark::Bench &bench)
Definition: crypto_hash.cpp:98
static void SHA256D64_1024(benchmark::Bench &bench)
Definition: crypto_hash.cpp:56
static void SHA3_256_1M(benchmark::Bench &bench)
Definition: crypto_hash.cpp:43
static void SHA1(benchmark::Bench &bench)
Definition: crypto_hash.cpp:29
static void SHA512(benchmark::Bench &bench)
Definition: crypto_hash.cpp:62
static void SHA256(benchmark::Bench &bench)
Definition: crypto_hash.cpp:36
static void MuHashDiv(benchmark::Bench &bench)
BENCHMARK(RIPEMD160)
static void MuHash(benchmark::Bench &bench)
Definition: crypto_hash.cpp:88
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
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.cpp:99