LCOV - code coverage report
Current view: top level - src/util - hasher.h Coverage Total Hit
Test: fuzz_coverage.info Lines: 91.7 % 12 11
Test Date: 2025-12-13 04:17:47 Functions: - 0 0
Branches: 50.0 % 4 2

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2019-present 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                 :             : #ifndef BITCOIN_UTIL_HASHER_H
       6                 :             : #define BITCOIN_UTIL_HASHER_H
       7                 :             : 
       8                 :             : #include <crypto/common.h>
       9                 :             : #include <crypto/siphash.h>
      10                 :             : #include <primitives/transaction.h>
      11                 :             : #include <span.h>
      12                 :             : #include <uint256.h>
      13                 :             : 
      14                 :             : #include <concepts>
      15                 :             : #include <cstdint>
      16                 :             : #include <cstring>
      17                 :             : 
      18                 :             : class SaltedUint256Hasher
      19                 :             : {
      20                 :             :     const PresaltedSipHasher m_hasher;
      21                 :             : 
      22                 :             : public:
      23                 :             :     SaltedUint256Hasher();
      24                 :             : 
      25                 :        2103 :     size_t operator()(const uint256& hash) const
      26                 :             :     {
      27                 :        2103 :         return m_hasher(hash);
      28                 :             :     }
      29                 :             : };
      30                 :             : 
      31                 :             : class SaltedTxidHasher
      32                 :             : {
      33                 :             :     const PresaltedSipHasher m_hasher;
      34                 :             : 
      35                 :             : public:
      36                 :             :     SaltedTxidHasher();
      37                 :             : 
      38                 :    58377127 :     size_t operator()(const Txid& txid) const
      39                 :             :     {
      40                 :    58377127 :         return m_hasher(txid.ToUint256());
      41                 :             :     }
      42                 :             : };
      43                 :             : 
      44                 :             : class SaltedWtxidHasher
      45                 :             : {
      46                 :             :     const PresaltedSipHasher m_hasher;
      47                 :             : 
      48                 :             : public:
      49                 :             :     SaltedWtxidHasher();
      50                 :             : 
      51                 :     5447361 :     size_t operator()(const Wtxid& wtxid) const
      52                 :             :     {
      53                 :     5447361 :         return m_hasher(wtxid.ToUint256());
      54                 :             :     }
      55                 :             : };
      56                 :             : 
      57                 :             : class SaltedOutpointHasher
      58                 :             : {
      59                 :             :     const PresaltedSipHasher m_hasher;
      60                 :             : 
      61                 :             : public:
      62                 :             :     SaltedOutpointHasher(bool deterministic = false);
      63                 :             : 
      64                 :             :     /**
      65                 :             :      * Having the hash noexcept allows libstdc++'s unordered_map to recalculate
      66                 :             :      * the hash during rehash, so it does not have to cache the value. This
      67                 :             :      * reduces node's memory by sizeof(size_t). The required recalculation has
      68                 :             :      * a slight performance penalty (around 1.6%), but this is compensated by
      69                 :             :      * memory savings of about 9% which allow for a larger dbcache setting.
      70                 :             :      *
      71                 :             :      * @see https://gcc.gnu.org/onlinedocs/gcc-13.2.0/libstdc++/manual/manual/unordered_associative.html
      72                 :             :      */
      73                 :   595934021 :     size_t operator()(const COutPoint& id) const noexcept
      74                 :             :     {
      75                 :   595934021 :         return m_hasher(id.hash.ToUint256(), id.n);
      76                 :             :     }
      77                 :             : };
      78                 :             : 
      79                 :             : struct FilterHeaderHasher {
      80         [ #  # ]:           0 :     size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
      81                 :             : };
      82                 :             : 
      83                 :             : /**
      84                 :             :  * We're hashing a nonce into the entries themselves, so we don't need extra
      85                 :             :  * blinding in the set hash computation.
      86                 :             :  *
      87                 :             :  * This may exhibit platform endian dependent behavior but because these are
      88                 :             :  * nonced hashes (random) and this state is only ever used locally it is safe.
      89                 :             :  * All that matters is local consistency.
      90                 :             :  */
      91                 :             : class SignatureCacheHasher
      92                 :             : {
      93                 :             : public:
      94                 :             :     template <uint8_t hash_select>
      95                 :      500299 :     uint32_t operator()(const uint256& key) const
      96                 :             :     {
      97                 :             :         static_assert(hash_select <8, "SignatureCacheHasher only has 8 hashes available.");
      98                 :             :         uint32_t u;
      99                 :      500299 :         std::memcpy(&u, key.begin()+4*hash_select, 4);
     100                 :             :         return u;
     101                 :             :     }
     102                 :             : };
     103                 :             : 
     104                 :             : struct BlockHasher
     105                 :             : {
     106                 :             :     // this used to call `GetCheapHash()` in uint256, which was later moved; the
     107                 :             :     // cheap hash function simply calls ReadLE64() however, so the end result is
     108                 :             :     // identical
     109         [ +  + ]:     3060020 :     size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
     110                 :             : };
     111                 :             : 
     112                 :             : class SaltedSipHasher
     113                 :             : {
     114                 :             : private:
     115                 :             :     /** Salt */
     116                 :             :     const uint64_t m_k0, m_k1;
     117                 :             : 
     118                 :             : public:
     119                 :             :     SaltedSipHasher();
     120                 :             : 
     121                 :             :     size_t operator()(const std::span<const unsigned char>& script) const;
     122                 :             : };
     123                 :             : 
     124                 :             : #endif // BITCOIN_UTIL_HASHER_H
        

Generated by: LCOV version 2.0-1