CPP   34

crypto hash cpp

Guest on 6th July 2022 06:37:59 PM

  1. // Copyright (c) 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. #include <bench/bench.h>
  7. #include <bloom.h>
  8. #include <hash.h>
  9. #include <random.h>
  10. #include <uint256.h>
  11. #include <utiltime.h>
  12. #include <crypto/ripemd160.h>
  13. #include <crypto/sha1.h>
  14. #include <crypto/sha256.h>
  15. #include <crypto/sha512.h>
  16.  
  17. /* Number of bytes to hash per iteration */
  18. static const uint64_t BUFFER_SIZE = 1000*1000;
  19.  
  20. static void RIPEMD160(benchmark::State& state)
  21. {
  22.     uint8_t hash[CRIPEMD160::OUTPUT_SIZE];
  23.     std::vector<uint8_t> in(BUFFER_SIZE,0);
  24.     while (state.KeepRunning())
  25.         CRIPEMD160().Write(in.data(), in.size()).Finalize(hash);
  26. }
  27.  
  28. static void SHA1(benchmark::State& state)
  29. {
  30.     uint8_t hash[CSHA1::OUTPUT_SIZE];
  31.     std::vector<uint8_t> in(BUFFER_SIZE,0);
  32.     while (state.KeepRunning())
  33.         CSHA1().Write(in.data(), in.size()).Finalize(hash);
  34. }
  35.  
  36. static void SHA256(benchmark::State& state)
  37. {
  38.     uint8_t hash[CSHA256::OUTPUT_SIZE];
  39.     std::vector<uint8_t> in(BUFFER_SIZE,0);
  40.     while (state.KeepRunning())
  41.         CSHA256().Write(in.data(), in.size()).Finalize(hash);
  42. }
  43.  
  44. static void SHA256_32b(benchmark::State& state)
  45. {
  46.     std::vector<uint8_t> in(32,0);
  47.     while (state.KeepRunning()) {
  48.         CSHA256()
  49.             .Write(in.data(), in.size())
  50.             .Finalize(in.data());
  51.     }
  52. }
  53.  
  54. static void SHA256D64_1024(benchmark::State& state)
  55. {
  56.     std::vector<uint8_t> in(64 * 1024, 0);
  57.     while (state.KeepRunning()) {
  58.         SHA256D64(in.data(), in.data(), 1024);
  59.     }
  60. }
  61.  
  62. static void SHA512(benchmark::State& state)
  63. {
  64.     uint8_t hash[CSHA512::OUTPUT_SIZE];
  65.     std::vector<uint8_t> in(BUFFER_SIZE,0);
  66.     while (state.KeepRunning())
  67.         CSHA512().Write(in.data(), in.size()).Finalize(hash);
  68. }
  69.  
  70. static void SipHash_32b(benchmark::State& state)
  71. {
  72.     uint256 x;
  73.     uint64_t k1 = 0;
  74.     while (state.KeepRunning()) {
  75.         *((uint64_t*)x.begin()) = SipHashUint256(0, ++k1, x);
  76.     }
  77. }
  78.  
  79. static void FastRandom_32bit(benchmark::State& state)
  80. {
  81.     FastRandomContext rng(true);
  82.     uint32_t x = 0;
  83.     while (state.KeepRunning()) {
  84.         x += rng.rand32();
  85.     }
  86. }
  87.  
  88. static void FastRandom_1bit(benchmark::State& state)
  89. {
  90.     FastRandomContext rng(true);
  91.     uint32_t x = 0;
  92.     while (state.KeepRunning()) {
  93.         x += rng.randbool();
  94.     }
  95. }
  96.  
  97. BENCHMARK(RIPEMD160, 440);
  98. BENCHMARK(SHA1, 570);
  99. BENCHMARK(SHA256, 340);
  100. BENCHMARK(SHA512, 330);
  101.  
  102. BENCHMARK(SHA256_32b, 4700 * 1000);
  103. BENCHMARK(SipHash_32b, 40 * 1000 * 1000);
  104. BENCHMARK(SHA256D64_1024, 7400);
  105. BENCHMARK(FastRandom_32bit, 110 * 1000 * 1000);
  106. BENCHMARK(FastRandom_1bit, 440 * 1000 * 1000);

Raw Paste


Login or Register to edit or fork this paste. It's free.