LCOV - code coverage report
Current view: top level - src/util - hasher.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 5 9 55.6 %
Date: 2023-11-10 23:46:46 Functions: 3 12 25.0 %
Branches: 1 2 50.0 %

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

Generated by: LCOV version 1.14