-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsiphash-cpp17.hpp
145 lines (117 loc) · 3.46 KB
/
siphash-cpp17.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <cstdint>
#include <cstring>
#include <string>
#include "codeforces-bit.hpp"
#if defined(_WIN32)
#define CODEFORCES 1
#endif
// utility function that fills a buffer with random bytes from more reliable
// sources (usually provided by the os).
// supports: linux, windows
// does not care about any errors that may happen. use at your own risk
#if defined(__linux__)
#include <sys/random.h>
void fill_random_bytes(void *buffer, size_t len) {
char *byte_buf = (char *)buffer;
while (len != 0) {
ssize_t count = getrandom(byte_buf, len, 0);
if (count > 0) {
len -= count;
byte_buf += count;
}
}
}
#elif defined(_WIN32) // defined(__linux__)
#if defined(CODEFORCES)
// clang-format off
#include <windows.h>
#include <wincrypt.h>
// clang-format on
void fill_random_bytes(void *buffer, size_t len) {
HCRYPTPROV prov;
CryptAcquireContext(&prov, 0, 0, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
CryptGenRandom(prov, len, (BYTE *)buffer);
CryptReleaseContext(prov, 0);
}
#else // defined(CODEFORCES)
// clang-format off
#include <windows.h>
#include <bcrypt.h>
// clang-format on
void fill_random_bytes(void *buffer, size_t len) {
BCryptGenRandom(NULL, (PUCHAR)buffer, len, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
}
#endif // defined(CODEFORCES)
#endif // defined(_WIN32)
namespace hash_lib {
template <size_t C, size_t D> class SipHash {
public:
SipHash() = delete;
SipHash(uint64_t k0, uint64_t k1)
: v0(k0 ^ V0INIT), v1(k1 ^ V1INIT), v2(k0 ^ V2INIT), v3(k1 ^ V3INIT) {}
void update(uint64_t block) {
v3 ^= block;
for (size_t i = 0; i < C; i++) {
round();
}
v0 ^= block;
}
uint64_t finalize() {
v2 ^= 0xff;
for (size_t i = 0; i < D; i++) {
round();
}
return v0 ^ v1 ^ v2 ^ v3;
}
private:
void round() {
v0 += v1;
v2 += v3;
v1 = std::rotl(v1, 13);
v3 = std::rotl(v3, 16);
v1 ^= v0;
v3 ^= v2;
v0 = std::rotl(v0, 32);
v2 += v1;
v0 += v3;
v1 = std::rotl(v1, 17);
v3 = std::rotl(v3, 21);
v1 ^= v2;
v3 ^= v0;
v2 = std::rotl(v2, 32);
}
static constexpr uint64_t V0INIT = 0x736f6d6570736575ULL;
static constexpr uint64_t V1INIT = 0x646f72616e646f6dULL;
static constexpr uint64_t V2INIT = 0x6c7967656e657261ULL;
static constexpr uint64_t V3INIT = 0x7465646279746573ULL;
uint64_t v0, v1, v2, v3;
};
// wrapper for SipHash, intended as a replacement for std::hash
template <size_t C, size_t D> class SipHasher {
public:
SipHasher() {
uint64_t k[2];
fill_random_bytes(&k, sizeof(k));
new (this) SipHasher(k[0], k[1]);
}
SipHasher(uint64_t k0_, uint64_t k1_) : k0(k0_), k1(k1_) {}
size_t operator()(const std::string &str) const {
SipHash<C, D> hasher(k0, k1);
size_t blocks = str.size() / 8;
for (size_t i = 0; i < blocks; i++) {
uint64_t block = 0;
std::memcpy(&block, str.data() + i * 8, 8);
hasher.update(block);
}
uint64_t final_block = 0;
std::memcpy(&final_block, str.data() + blocks * 8,
str.size() - blocks * 8);
final_block |= uint64_t(str.size()) << 56;
hasher.update(final_block);
return hasher.finalize();
}
private:
uint64_t k0, k1;
};
} // namespace hash_lib