Raven Core  3.0.0
P2P Digital Currency
crypto_hash.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <iostream>
7 
8 #include "bench.h"
9 #include "bloom.h"
10 #include "hash.h"
11 #include "random.h"
12 #include "uint256.h"
13 #include "utiltime.h"
14 #include "crypto/ripemd160.h"
15 #include "crypto/sha1.h"
16 #include "crypto/sha256.h"
17 #include "crypto/sha512.h"
18 
19 /* Number of bytes to hash per iteration */
20 static const uint64_t BUFFER_SIZE = 1000*1000;
21 
22 static void RIPEMD160(benchmark::State& state)
23 {
24  uint8_t hash[CRIPEMD160::OUTPUT_SIZE];
25  std::vector<uint8_t> in(BUFFER_SIZE,0);
26  while (state.KeepRunning())
27  CRIPEMD160().Write(in.data(), in.size()).Finalize(hash);
28 }
29 
30 static void SHA1(benchmark::State& state)
31 {
32  uint8_t hash[CSHA1::OUTPUT_SIZE];
33  std::vector<uint8_t> in(BUFFER_SIZE,0);
34  while (state.KeepRunning())
35  CSHA1().Write(in.data(), in.size()).Finalize(hash);
36 }
37 
38 static void SHA256(benchmark::State& state)
39 {
40  uint8_t hash[CSHA256::OUTPUT_SIZE];
41  std::vector<uint8_t> in(BUFFER_SIZE,0);
42  while (state.KeepRunning())
43  CSHA256().Write(in.data(), in.size()).Finalize(hash);
44 }
45 
46 static void SHA256_32b(benchmark::State& state)
47 {
48  std::vector<uint8_t> in(32,0);
49  while (state.KeepRunning()) {
50  for (int i = 0; i < 1000000; i++) {
51  CSHA256().Write(in.data(), in.size()).Finalize(in.data());
52  }
53  }
54 }
55 
56 static void SHA512(benchmark::State& state)
57 {
58  uint8_t hash[CSHA512::OUTPUT_SIZE];
59  std::vector<uint8_t> in(BUFFER_SIZE,0);
60  while (state.KeepRunning())
61  CSHA512().Write(in.data(), in.size()).Finalize(hash);
62 }
63 
64 static void SipHash_32b(benchmark::State& state)
65 {
66  uint256 x;
67  while (state.KeepRunning()) {
68  for (int i = 0; i < 1000000; i++) {
69  *((uint64_t*)x.begin()) = SipHashUint256(0, i, x);
70  }
71  }
72 }
73 
74 static void FastRandom_32bit(benchmark::State& state)
75 {
76  FastRandomContext rng(true);
77  uint32_t x = 0;
78  while (state.KeepRunning()) {
79  for (int i = 0; i < 1000000; i++) {
80  x += rng.rand32();
81  }
82  }
83 }
84 
85 static void FastRandom_1bit(benchmark::State& state)
86 {
87  FastRandomContext rng(true);
88  uint32_t x = 0;
89  while (state.KeepRunning()) {
90  for (int i = 0; i < 1000000; i++) {
91  x += rng.randbool();
92  }
93  }
94 }
95 
96 BENCHMARK(RIPEMD160);
97 BENCHMARK(SHA1);
98 BENCHMARK(SHA256);
99 BENCHMARK(SHA512);
100 
101 BENCHMARK(SHA256_32b);
102 BENCHMARK(SipHash_32b);
103 BENCHMARK(FastRandom_32bit);
104 BENCHMARK(FastRandom_1bit);
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:202
BENCHMARK(RIPEMD160)
CSHA1 & Write(const unsigned char *data, size_t len)
Definition: sha1.cpp:155
bool KeepRunning()
Definition: bench.cpp:44
unsigned char * begin()
Definition: uint256.h:57
static const size_t OUTPUT_SIZE
Definition: sha512.h:21
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:21
Fast randomness source.
Definition: random.h:45
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:248
256-bit opaque blob.
Definition: uint256.h:123
CSHA512 & Write(const unsigned char *data, size_t len)
Definition: sha512.cpp:160
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: hash.cpp:173
static const size_t OUTPUT_SIZE
Definition: sha256.h:22
A hasher class for SHA1.
Definition: sha1.h:13
A hasher class for SHA-512.
Definition: sha512.h:13
static const size_t OUTPUT_SIZE
Definition: sha1.h:21
A hasher class for SHA-256.
Definition: sha256.h:14
A hasher class for RIPEMD-160.
Definition: ripemd160.h:13