Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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
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
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
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
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
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
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
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
70 uint256 x;
71 uint64_t k1 = 0;
72 bench.run([&] {
74 std::memcpy(x.begin(), &hash64, sizeof(hash64));
75 });
76}
77
79 FastRandomContext rng(true);
80 bench.run([&] { rng.rand32(); });
81}
82
84 FastRandomContext rng(true);
85 bench.run([&] { rng.randbool(); });
86}
87
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
100 FastRandomContext rng(true);
101 MuHash3072 muhash{rng.randbytes(32)};
102
103 bench.run([&] { acc *= muhash; });
104}
105
108 FastRandomContext rng(true);
109 MuHash3072 muhash{rng.randbytes(32)};
110
111 bench.run([&] { acc /= muhash; });
112}
113
116 FastRandomContext rng(true);
117 std::vector<uint8_t> key{rng.randbytes(32)};
118
119 bench.run([&] { MuHash3072{key}; });
120}
121
127
133
#define BENCHMARK(n)
Definition bench.h:65
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)
void Finalize(uint8_t hash[OUTPUT_SIZE])
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
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
uint8_t * begin()
Definition uint256.h:85
256-bit opaque blob.
Definition uint256.h:129
static void RIPEMD160(benchmark::Bench &bench)
static void SipHash_32b(benchmark::Bench &bench)
static void FastRandom_1bit(benchmark::Bench &bench)
static void MuHashPrecompute(benchmark::Bench &bench)
static const uint64_t BUFFER_SIZE
static void SHA256_32b(benchmark::Bench &bench)
static void FastRandom_32bit(benchmark::Bench &bench)
static void MuHashMul(benchmark::Bench &bench)
static void SHA256D64_1024(benchmark::Bench &bench)
static void SHA3_256_1M(benchmark::Bench &bench)
static void SHA1(benchmark::Bench &bench)
static void SHA512(benchmark::Bench &bench)
static void SHA256(benchmark::Bench &bench)
static void MuHashDiv(benchmark::Bench &bench)
static void MuHash(benchmark::Bench &bench)
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
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