PIVX Core  5.6.99
P2P Digital Currency
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 PIVX_SALTEDHASHER_H
6 #define PIVX_SALTEDHASHER_H
7 
8 #include "crypto/siphash.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 // PIVX_SALTEDHASHER_H
256-bit opaque blob.
Definition: uint256.h:138
#define T(expected, seed, data)
#define S(x0, x1, x2, x3, cb, r)
Definition: jh.c:494
Definition: uint256.h:212
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, uint32_t extra)
Definition: siphash.cpp:134
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.cpp:94
const uint64_t k1
Definition: saltedhasher.h:45
const uint64_t k0
Salt.
Definition: saltedhasher.h:45
std::size_t operator()(const T &v) const
Definition: saltedhasher.h:55
static std::size_t CalcHash(const std::pair< N, uint256 > &v, uint64_t k0, uint64_t k1)
Definition: saltedhasher.h:27
static std::size_t CalcHash(const std::pair< uint256, N > &v, uint64_t k0, uint64_t k1)
Definition: saltedhasher.h:18
static std::size_t CalcHash(const uint256 &v, uint64_t k0, uint64_t k1)
Definition: saltedhasher.h:36
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:69
static SaltedHasherBase s
Definition: saltedhasher.h:66