Fabcoin Core  0.16.2
P2P Digital Currency
crypto_hash.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2017 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 <iostream>
6 
7 #include <bench/bench.h>
8 #include <bloom.h>
9 #include <hash.h>
10 #include <random.h>
11 #include <uint256.h>
12 #include <utiltime.h>
13 #include <crypto/ripemd160.h>
14 #include <crypto/sha1.h>
15 #include <crypto/sha256.h>
16 #include <crypto/sha512.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::State& state)
22 {
23  uint8_t hash[CRIPEMD160::OUTPUT_SIZE];
24  std::vector<uint8_t> in(BUFFER_SIZE,0);
25  while (state.KeepRunning())
26  CRIPEMD160().Write(in.data(), in.size()).Finalize(hash);
27 }
28 
29 static void SHA1(benchmark::State& state)
30 {
31  uint8_t hash[CSHA1::OUTPUT_SIZE];
32  std::vector<uint8_t> in(BUFFER_SIZE,0);
33  while (state.KeepRunning())
34  CSHA1().Write(in.data(), in.size()).Finalize(hash);
35 }
36 
37 static void SHA256(benchmark::State& state)
38 {
39  uint8_t hash[CSHA256::OUTPUT_SIZE];
40  std::vector<uint8_t> in(BUFFER_SIZE,0);
41  while (state.KeepRunning())
42  CSHA256().Write(in.data(), in.size()).Finalize(hash);
43 }
44 
45 static void SHA256_32b(benchmark::State& state)
46 {
47  std::vector<uint8_t> in(32,0);
48  while (state.KeepRunning()) {
49  CSHA256()
50  .Write(in.data(), in.size())
51  .Finalize(in.data());
52  }
53 }
54 
55 static void SHA512(benchmark::State& state)
56 {
57  uint8_t hash[CSHA512::OUTPUT_SIZE];
58  std::vector<uint8_t> in(BUFFER_SIZE,0);
59  while (state.KeepRunning())
60  CSHA512().Write(in.data(), in.size()).Finalize(hash);
61 }
62 
63 static void SipHash_32b(benchmark::State& state)
64 {
65  uint256 x;
66  uint64_t k1 = 0;
67  while (state.KeepRunning()) {
68  *((uint64_t*)x.begin()) = SipHashUint256(0, ++k1, x);
69  }
70 }
71 
72 static void FastRandom_32bit(benchmark::State& state)
73 {
74  FastRandomContext rng(true);
75  uint32_t x = 0;
76  while (state.KeepRunning()) {
77  x += rng.rand32();
78  }
79 }
80 
81 static void FastRandom_1bit(benchmark::State& state)
82 {
83  FastRandomContext rng(true);
84  uint32_t x = 0;
85  while (state.KeepRunning()) {
86  x += rng.randbool();
87  }
88 }
89 
90 BENCHMARK(RIPEMD160, 440);
91 BENCHMARK(SHA1, 570);
92 BENCHMARK(SHA256, 340);
93 BENCHMARK(SHA512, 330);
94 
95 BENCHMARK(SHA256_32b, 4700 * 1000);
96 BENCHMARK(SipHash_32b, 40 * 1000 * 1000);
97 BENCHMARK(FastRandom_32bit, 110 * 1000 * 1000);
98 BENCHMARK(FastRandom_1bit, 440 * 1000 * 1000);
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:201
CSHA1 & Write(const unsigned char *data, size_t len)
Definition: sha1.cpp:154
SHA-256 message digest.
Definition: sha.h:39
RIPEMD-160 message digest.
Definition: ripemd.h:17
bool KeepRunning()
Definition: bench.h:70
unsigned char * begin()
Definition: uint256.h:65
#define x(i)
static const size_t OUTPUT_SIZE
Definition: sha512.h:20
BENCHMARK(RIPEMD160, 440)
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
SHA-512 message digest.
Definition: sha.h:69
Fast randomness source.
Definition: random.h:44
SHA-1 message digest.
Definition: sha.h:25
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
256-bit opaque blob.
Definition: uint256.h:132
CSHA512 & Write(const unsigned char *data, size_t len)
Definition: sha512.cpp:159
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: hash.cpp:169
#define k1
Definition: ripemd.cpp:19
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
A hasher class for SHA1.
Definition: sha1.h:12
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
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12