-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpearsonb.c
206 lines (154 loc) · 4.41 KB
/
pearsonb.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// This is free and unencumbered software released into the public domain.
#include "pearsonb.h"
// Christopher Wellons' triple32 from https://github.com/skeeto/hash-prospector
// published under The Unlicense
#define permute32(in) \
in ^= in >> 17; \
in *= 0xed5ad4bb; \
in ^= in >> 11; \
in *= 0xac4c1b51; \
in ^= in >> 15; \
in *= 0x31848bab; \
in ^= in >> 14
// David Stafford's Mix13 from http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html
// the author clarified via eMail that this of his work is released to the public domain
#define permute64(in) \
in ^= (in >> 30); \
in *= 0xbf58476d1ce4e5b9; \
in ^= (in >> 27); \
in *= 0x94d049bb133111eb; \
in ^= (in >> 31)
#define dec1(in) \
in--
#define dec2(in) \
dec1(in); \
dec1(in)
#define dec3(in) \
dec2(in); \
dec1(in)
#define dec4(in) \
dec3(in); \
dec1(in)
#define hash_round(hash, in, part) \
hash##part ^= in; \
dec##part(hash##part); \
permute64(hash##part)
void pearson_hash_256 (uint8_t *out, const uint8_t *in, size_t len) {
uint64_t *current;
current = (uint64_t*)in;
uint64_t org_len = len;
uint64_t hash1 = 0;
uint64_t hash2 = 0;
uint64_t hash3 = 0;
uint64_t hash4 = 0;
while (len > 7) {
// digest words little endian first
hash_round(hash, le64toh(*current), 1);
hash_round(hash, le64toh(*current), 2);
hash_round(hash, le64toh(*current), 3);
hash_round(hash, le64toh(*current), 4);
current++;
len-=8;
}
// handle the rest
hash1 = ~hash1;
hash2 = ~hash2;
hash3 = ~hash3;
hash4 = ~hash4;
while(len) {
// byte-wise, no endianess
hash_round(hash, *(uint8_t*)current, 1);
hash_round(hash, *(uint8_t*)current, 2);
hash_round(hash, *(uint8_t*)current, 3);
hash_round(hash, *(uint8_t*)current, 4);
current = (uint64_t*)((uint8_t*)current + 1);
len--;
}
// digest length
hash1 = ~hash1;
hash2 = ~hash2;
hash3 = ~hash3;
hash4 = ~hash4;
hash_round(hash, org_len, 1);
hash_round(hash, org_len, 2);
hash_round(hash, org_len, 3);
hash_round(hash, org_len, 4);
// hash string is stored big endian, the natural way to read
uint64_t *o;
o = (uint64_t*)out;
*o = htobe64(hash4);
o++;
*o = htobe64(hash3);
o++;
*o = htobe64(hash2);
o++;
*o = htobe64(hash1);
}
void pearson_hash_128 (uint8_t *out, const uint8_t *in, size_t len) {
uint64_t *current;
current = (uint64_t*)in;
uint64_t org_len = len;
uint64_t hash1 = 0;
uint64_t hash2 = 0;
while (len > 7) {
// digest words little endian first
hash_round(hash, le64toh(*current), 1);
hash_round(hash, le64toh(*current), 2);
current++;
len-=8;
}
// handle the rest
hash1 = ~hash1;
hash2 = ~hash2;
while(len) {
// byte-wise, no endianess
hash_round(hash, *(uint8_t*)current, 1);
hash_round(hash, *(uint8_t*)current, 2);
current = (uint64_t*)((uint8_t*)current + 1);
len--;
}
// digest length
hash1 = ~hash1;
hash2 = ~hash2;
hash_round(hash, org_len, 1);
hash_round(hash, org_len, 2);
// hash string is stored big endian, the natural way to read
uint64_t *o;
o = (uint64_t*)out;
*o = htobe64(hash2);
o++;
*o = htobe64(hash1);
}
uint64_t pearson_hash_64 (const uint8_t *in, size_t len) {
uint64_t *current;
current = (uint64_t*)in;
uint64_t org_len = len;
uint64_t hash1 = 0;
while(len > 7) {
// digest words little endian first
hash_round(hash, le64toh(*current), 1);
current++;
len-=8;
}
// handle the rest
hash1 = ~hash1;
while(len) {
// byte-wise, no endianess
hash_round(hash, *(uint8_t*)current, 1);
current = (uint64_t*)((uint8_t*)current + 1);
len--;
}
// digest length
hash1 = ~hash1;
hash_round(hash, org_len, 1);
// caller is responsible for storing it the big endian way to memory (if ever)
return hash1;
}
uint32_t pearson_hash_32 (const uint8_t *in, size_t len) {
return pearson_hash_64(in, len);
}
uint16_t pearson_hash_16 (const uint8_t *in, size_t len) {
return pearson_hash_64(in, len);
}
void pearson_hash_init (void) {
}