LCOV - code coverage report
Current view: top level - src - saltedhasher.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 6 6 100.0 %
Date: 2025-02-23 09:33:43 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright (c) 2019 The Dash 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             : #ifndef PIVX_SALTEDHASHER_H
       6             : #define PIVX_SALTEDHASHER_H
       7             : 
       8             : #include "crypto/siphash.h"
       9             : #include "uint256.h"
      10             : 
      11             : /** Helper classes for std::unordered_map and std::unordered_set hashing */
      12             : 
      13             : template<typename T> struct SaltedHasherImpl;
      14             : 
      15             : template<typename N>
      16             : struct SaltedHasherImpl<std::pair<uint256, N>>
      17             : {
      18             :     static std::size_t CalcHash(const std::pair<uint256, N>& v, uint64_t k0, uint64_t k1)
      19             :     {
      20             :         return SipHashUint256Extra(k0, k1, v.first, (uint32_t) v.second);
      21             :     }
      22             : };
      23             : 
      24             : template<typename N>
      25             : struct SaltedHasherImpl<std::pair<N, uint256>>
      26             : {
      27       30810 :     static std::size_t CalcHash(const std::pair<N, uint256>& v, uint64_t k0, uint64_t k1)
      28             :     {
      29       30810 :         return SipHashUint256Extra(k0, k1, v.second, (uint32_t) v.first);
      30             :     }
      31             : };
      32             : 
      33             : template<>
      34             : struct SaltedHasherImpl<uint256>
      35             : {
      36      349527 :     static std::size_t CalcHash(const uint256& v, uint64_t k0, uint64_t k1)
      37             :     {
      38      349527 :         return SipHashUint256(k0, k1, v);
      39             :     }
      40             : };
      41             : 
      42             : struct SaltedHasherBase
      43             : {
      44             :     /** Salt */
      45             :     const uint64_t k0, k1;
      46             : 
      47             :     SaltedHasherBase();
      48             : };
      49             : 
      50             : /* Allows each instance of unordered maps/sest to have their own salt */
      51             : template<typename T, typename S>
      52             : struct SaltedHasher
      53             : {
      54             :     S s;
      55             :     std::size_t operator()(const T& v) const
      56             :     {
      57             :         return SaltedHasherImpl<T>::CalcHash(v, s.k0, s.k1);
      58             :     }
      59             : };
      60             : 
      61             : /* Allows to use a static salt for all instances. The salt is a random value set at startup
      62             :  * (through static initialization)
      63             :  */
      64             : struct StaticSaltedHasher
      65             : {
      66             :     static SaltedHasherBase s;
      67             : 
      68             :     template<typename T>
      69      380337 :     std::size_t operator()(const T& v) const
      70             :     {
      71      380337 :         return SaltedHasherImpl<T>::CalcHash(v, s.k0, s.k1);
      72             :     }
      73             : };
      74             : 
      75             : #endif // PIVX_SALTEDHASHER_H

Generated by: LCOV version 1.14