Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

saltedhasher.h
Go to the documentation of this file.
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 SALTEDHASHER_H
6 #define SALTEDHASHER_H
7 
8 #include <hash.h>
9 #include <uint256.h>
10 
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  static std::size_t CalcHash(const std::pair<N, uint256>& v, uint64_t k0, uint64_t k1)
28  {
29  return SipHashUint256Extra(k0, k1, v.second, (uint32_t) v.first);
30  }
31 };
32 
33 template<>
35 {
36  static std::size_t CalcHash(const uint256& v, uint64_t k0, uint64_t k1)
37  {
38  return SipHashUint256(k0, k1, v);
39  }
40 };
41 
43 {
45  const uint64_t k0, k1;
46 
48 };
49 
50 /* Allows each instance of unordered maps/sest to have their own salt */
51 template<typename T, typename S>
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  */
65 {
67 
68  template<typename T>
69  std::size_t operator()(const T& v) const
70  {
72  }
73 };
74 
75 #endif//SALTEDHASHER_H
Helper classes for std::unordered_map and std::unordered_set hashing.
Definition: saltedhasher.h:13
std::size_t operator()(const T &v) const
Definition: saltedhasher.h:55
Definition: box.hpp:161
static std::size_t CalcHash(const uint256 &v, uint64_t k0, uint64_t k1)
Definition: saltedhasher.h:36
std::size_t operator()(const T &v) const
Definition: saltedhasher.h:69
std::size_t size_t
Definition: bits.hpp:21
#define S(x0, x1, x2, x3, cb, r)
Definition: jh.c:494
static const unsigned char k1[32]
const uint64_t k0
Salt.
Definition: saltedhasher.h:45
256-bit opaque blob.
Definition: uint256.h:123
static std::size_t CalcHash(const std::pair< uint256, N > &v, uint64_t k0, uint64_t k1)
Definition: saltedhasher.h:18
const uint64_t k1
Definition: saltedhasher.h:45
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: hash.cpp:168
static std::size_t CalcHash(const std::pair< N, uint256 > &v, uint64_t k0, uint64_t k1)
Definition: saltedhasher.h:27
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, uint32_t extra)
Definition: hash.cpp:208
static SaltedHasherBase s
Definition: saltedhasher.h:66
Released under the MIT license