-
Notifications
You must be signed in to change notification settings - Fork 1
/
KeysetTest.h
439 lines (293 loc) · 10.4 KB
/
KeysetTest.h
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//-----------------------------------------------------------------------------
// Keyset tests generate various sorts of difficult-to-hash keysets and compare
// the distribution and collision frequency of the hash results against an
// ideal random distribution
// The sanity checks are also in this cpp/h
#pragma once
#include "Types.h"
#include "Stats.h"
#include "Random.h" // for rand_p
#include <algorithm> // for std::swap
#include <assert.h>
//-----------------------------------------------------------------------------
// Sanity tests
bool VerificationTest ( pfHash hash, const int hashbits, uint32_t expected, bool verbose );
bool SanityTest ( pfHash hash, const int hashbits );
void AppendedZeroesTest ( pfHash hash, const int hashbits );
//-----------------------------------------------------------------------------
// Keyset 'Combination' - all possible combinations of input blocks
template< typename hashtype >
void CombinationKeygenRecurse ( uint32_t * key, int len, int maxlen,
uint32_t * blocks, int blockcount,
pfHash hash, std::vector<hashtype> & hashes )
{
if(len == maxlen) return;
for(int i = 0; i < blockcount; i++)
{
key[len] = blocks[i];
//if(len == maxlen-1)
{
hashtype h;
hash(key,(len+1) * sizeof(uint32_t),0,&h);
hashes.push_back(h);
}
//else
{
CombinationKeygenRecurse(key,len+1,maxlen,blocks,blockcount,hash,hashes);
}
}
}
template< typename hashtype >
bool CombinationKeyTest ( hashfunc<hashtype> hash, int maxlen, uint32_t * blocks, int blockcount, bool testColl, bool testDist, bool drawDiagram )
{
printf("Keyset 'Combination' - up to %d blocks from a set of %d - ",maxlen,blockcount);
//----------
std::vector<hashtype> hashes;
uint32_t * key = new uint32_t[maxlen];
CombinationKeygenRecurse<hashtype>(key,0,maxlen,blocks,blockcount,hash,hashes);
delete [] key;
printf("%d keys\n",(int)hashes.size());
//----------
bool result = true;
result &= TestHashList<hashtype>(hashes,testColl,testDist,drawDiagram);
printf("\n");
return result;
}
//----------------------------------------------------------------------------
// Keyset 'Permutation' - given a set of 32-bit blocks, generate keys
// consisting of all possible permutations of those blocks
template< typename hashtype >
void PermutationKeygenRecurse ( pfHash hash, uint32_t * blocks, int blockcount, int k, std::vector<hashtype> & hashes )
{
if(k == blockcount-1)
{
hashtype h;
hash(blocks,blockcount * sizeof(uint32_t),0,&h);
hashes.push_back(h);
return;
}
for(int i = k; i < blockcount; i++)
{
std::swap(blocks[k],blocks[i]);
PermutationKeygenRecurse(hash,blocks,blockcount,k+1,hashes);
std::swap(blocks[k],blocks[i]);
}
}
template< typename hashtype >
bool PermutationKeyTest ( hashfunc<hashtype> hash, uint32_t * blocks, int blockcount, bool testColl, bool testDist, bool drawDiagram )
{
printf("Keyset 'Permutation' - %d blocks - ",blockcount);
//----------
std::vector<hashtype> hashes;
PermutationKeygenRecurse<hashtype>(hash,blocks,blockcount,0,hashes);
printf("%d keys\n",(int)hashes.size());
//----------
bool result = true;
result &= TestHashList<hashtype>(hashes,testColl,testDist,drawDiagram);
printf("\n");
return result;
}
//-----------------------------------------------------------------------------
// Keyset 'Sparse' - generate all possible N-bit keys with up to K bits set
template < typename keytype, typename hashtype >
void SparseKeygenRecurse ( pfHash hash, int start, int bitsleft, bool inclusive, keytype & k, std::vector<hashtype> & hashes )
{
const int nbytes = sizeof(keytype);
const int nbits = nbytes * 8;
hashtype h;
for(int i = start; i < nbits; i++)
{
flipbit(&k,nbytes,i);
if(inclusive || (bitsleft == 1))
{
hash(&k,sizeof(keytype),0,&h);
hashes.push_back(h);
}
if(bitsleft > 1)
{
SparseKeygenRecurse(hash,i+1,bitsleft-1,inclusive,k,hashes);
}
flipbit(&k,nbytes,i);
}
}
//----------
template < int keybits, typename hashtype >
bool SparseKeyTest ( hashfunc<hashtype> hash, const int setbits, bool inclusive, bool testColl, bool testDist, bool drawDiagram )
{
printf("Keyset 'Sparse' - %d-bit keys with %s %d bits set - ",keybits, inclusive ? "up to" : "exactly", setbits);
typedef Blob<keybits> keytype;
std::vector<hashtype> hashes;
keytype k;
memset(&k,0,sizeof(k));
if(inclusive)
{
hashtype h;
hash(&k,sizeof(keytype),0,&h);
hashes.push_back(h);
}
SparseKeygenRecurse(hash,0,setbits,inclusive,k,hashes);
printf("%d keys\n",(int)hashes.size());
bool result = true;
result &= TestHashList<hashtype>(hashes,testColl,testDist,drawDiagram);
printf("\n");
return result;
}
//-----------------------------------------------------------------------------
// Keyset 'Windows' - for all possible N-bit windows of a K-bit key, generate
// all possible keys with bits set in that window
template < typename keytype, typename hashtype >
bool WindowedKeyTest ( hashfunc<hashtype> hash, const int windowbits, bool testCollision, bool testDistribution, bool drawDiagram )
{
const int keybits = sizeof(keytype) * 8;
const int keycount = 1 << windowbits;
std::vector<hashtype> hashes;
hashes.resize(keycount);
bool result = true;
int testcount = keybits;
printf("Keyset 'Windowed' - %3d-bit key, %3d-bit window - %d tests, %d keys per test\n",keybits,windowbits,testcount,keycount);
for(int j = 0; j <= testcount; j++)
{
int minbit = j;
keytype key;
for(int i = 0; i < keycount; i++)
{
key = i;
//key = key << minbit;
lrot(&key,sizeof(keytype),minbit);
hash(&key,sizeof(keytype),0,&hashes[i]);
}
printf("Window at %3d - ",j);
result &= TestHashList(hashes,testCollision,testDistribution,drawDiagram);
//printf("\n");
}
return result;
}
//-----------------------------------------------------------------------------
// Keyset 'Cyclic' - generate keys that consist solely of N repetitions of M
// bytes.
// (This keyset type is designed to make MurmurHash2 fail)
template < typename hashtype >
bool CyclicKeyTest ( pfHash hash, int cycleLen, int cycleReps, const int keycount, bool drawDiagram )
{
printf("Keyset 'Cyclic' - %d cycles of %d bytes - %d keys\n",cycleReps,cycleLen,keycount);
Rand r(483723);
std::vector<hashtype> hashes;
hashes.resize(keycount);
int keyLen = cycleLen * cycleReps;
uint8_t * cycle = new uint8_t[cycleLen + 16];
uint8_t * key = new uint8_t[keyLen];
//----------
for(int i = 0; i < keycount; i++)
{
r.rand_p(cycle,cycleLen);
*(uint32_t*)cycle = f3mix(i ^ 0x746a94f1);
for(int j = 0; j < keyLen; j++)
{
key[j] = cycle[j % cycleLen];
}
hash(key,keyLen,0,&hashes[i]);
}
//----------
bool result = true;
result &= TestHashList(hashes,true,true,drawDiagram);
printf("\n");
delete [] cycle;
delete [] key;
return result;
}
//-----------------------------------------------------------------------------
// Keyset 'TwoBytes' - generate all keys up to length N with two non-zero bytes
void TwoBytesKeygen ( int maxlen, KeyCallback & c );
template < typename hashtype >
bool TwoBytesTest2 ( pfHash hash, int maxlen, bool drawDiagram )
{
std::vector<hashtype> hashes;
HashCallback<hashtype> c(hash,hashes);
TwoBytesKeygen(maxlen,c);
bool result = true;
result &= TestHashList(hashes,true,true,drawDiagram);
printf("\n");
return result;
}
//-----------------------------------------------------------------------------
// Keyset 'Text' - generate all keys of the form "prefix"+"core"+"suffix",
// where "core" consists of all possible combinations of the given character
// set of length N.
template < typename hashtype >
bool TextKeyTest ( hashfunc<hashtype> hash, const char * prefix, const char * coreset, const int corelen, const char * suffix, bool drawDiagram )
{
const int prefixlen = (int)strlen(prefix);
const int suffixlen = (int)strlen(suffix);
const int corecount = (int)strlen(coreset);
const int keybytes = prefixlen + corelen + suffixlen;
const int keycount = (int)pow(double(corecount),double(corelen));
printf("Keyset 'Text' - keys of form \"%s[",prefix);
for(int i = 0; i < corelen; i++) printf("X");
printf("]%s\" - %d keys\n",suffix,keycount);
uint8_t * key = new uint8_t[keybytes+1];
key[keybytes] = 0;
memcpy(key,prefix,prefixlen);
memcpy(key+prefixlen+corelen,suffix,suffixlen);
//----------
std::vector<hashtype> hashes;
hashes.resize(keycount);
for(int i = 0; i < keycount; i++)
{
int t = i;
for(int j = 0; j < corelen; j++)
{
key[prefixlen+j] = coreset[t % corecount]; t /= corecount;
}
hash(key,keybytes,0,&hashes[i]);
}
//----------
bool result = true;
result &= TestHashList(hashes,true,true,drawDiagram);
printf("\n");
delete [] key;
return result;
}
//-----------------------------------------------------------------------------
// Keyset 'Zeroes' - keys consisting of all zeroes, differing only in length
// We reuse one block of empty bytes, otherwise the RAM cost is enormous.
template < typename hashtype >
bool ZeroKeyTest ( pfHash hash, bool drawDiagram )
{
int keycount = 64*1024;
printf("Keyset 'Zeroes' - %d keys\n",keycount);
unsigned char * nullblock = new unsigned char[keycount];
memset(nullblock,0,keycount);
//----------
std::vector<hashtype> hashes;
hashes.resize(keycount);
for(int i = 0; i < keycount; i++)
{
hash(nullblock,i,0,&hashes[i]);
}
bool result = true;
result &= TestHashList(hashes,true,true,drawDiagram);
printf("\n");
delete [] nullblock;
return result;
}
//-----------------------------------------------------------------------------
// Keyset 'Seed' - hash "the quick brown fox..." using different seeds
template < typename hashtype >
bool SeedTest ( pfHash hash, int keycount, bool drawDiagram )
{
printf("Keyset 'Seed' - %d keys\n",keycount);
const char * text = "The quick brown fox jumps over the lazy dog";
const int len = (int)strlen(text);
//----------
std::vector<hashtype> hashes;
hashes.resize(keycount);
for(int i = 0; i < keycount; i++)
{
hash(text,len,i,&hashes[i]);
}
bool result = true;
result &= TestHashList(hashes,true,true,drawDiagram);
printf("\n");
return result;
}
//-----------------------------------------------------------------------------