forked from rurban/smhasher
-
Notifications
You must be signed in to change notification settings - Fork 10
/
KeysetTest.h
584 lines (407 loc) · 14.1 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//-----------------------------------------------------------------------------
// 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 );
static void printKey(const void* key, size_t len)
{
const unsigned char* const p = (const unsigned char*)key;
size_t s;
printf("\n0x");
for (s=0; s<len; s++) printf("%02X", p[s]);
printf("\n ");
for (s=0; s<len; s+=8) printf("%-16zu", s);
}
//-----------------------------------------------------------------------------
// Keyset 'Prng'
template< typename hashtype >
void Prn_gen (int nbRn, pfHash hash, std::vector<hashtype> & hashes )
{
assert(nbRn < 0);
printf("Generating %i random numbers : \n", nbRn);
hashtype hcopy;
memset(&hcopy, 0, sizeof(hcopy));
// a generated random number becomes the input for the next one
for (int i=0; i< nbRn; i++) {
hashtype h;
hash(&hcopy, sizeof(hcopy), 0, &h);
hashes.push_back(h);
memcpy(&hcopy, &h, sizeof(h));
}
}
template< typename hashtype >
bool PrngTest ( hashfunc<hashtype> hash,
bool testColl, bool testDist, bool drawDiagram )
{
if (sizeof(hashtype) < 8) {
printf("The PRNG test is designed for hashes >= 64-bit \n");
return true;
}
//----------
std::vector<hashtype> hashes;
Prn_gen(32 << 20, hash, hashes);
//----------
bool result = true;
result &= TestHashList(hashes,drawDiagram,testColl,testDist);
return result;
}
//-----------------------------------------------------------------------------
// Keyset 'Perlin Noise' - X,Y coordinates on input & seed
template< typename hashtype >
void PerlinNoiseTest (int Xbits, int Ybits,
int inputLen, int step,
pfHash hash, std::vector<hashtype> & hashes )
{
assert(0 < Ybits && Ybits < 31);
assert(0 < Xbits && Xbits < 31);
assert(inputLen*8 > Xbits); // enough space to run the test
int const xMax = (1 << Xbits);
int const yMax = (1 << Ybits);
assert(Xbits + Ybits < 31);
#define INPUT_LEN_MAX 256
assert(inputLen <= INPUT_LEN_MAX);
char key[INPUT_LEN_MAX] = {0};
printf("Testing %i coordinates (L%i) : \n", xMax * yMax, inputLen);
for(int x = 0; x < xMax; x++) {
memcpy(key, &x, inputLen); // Note : only works with Little Endian
for (int y=0; y < yMax; y++) {
hashtype h;
hash(key, inputLen, y, &h);
hashes.push_back(h);
}
}
}
template< typename hashtype >
bool PerlinNoise ( hashfunc<hashtype> hash, int inputLen,
bool testColl, bool testDist, bool drawDiagram )
{
//----------
std::vector<hashtype> hashes;
PerlinNoiseTest(12, 12, inputLen, 1, hash,hashes);
//----------
bool result = true;
result &= TestHashList(hashes,drawDiagram,testColl,testDist);
printf("\n");
return result;
}
//-----------------------------------------------------------------------------
// Keyset 'Combination' - all possible combinations of input blocks
template< typename hashtype, class blocktype >
void CombinationKeygenRecurse ( blocktype * key, int len, int maxlen,
blocktype * blocks, int blockcount,
pfHash hash, std::vector<hashtype> & hashes )
{
if(len == maxlen) return; // end recursion
for(int i = 0; i < blockcount; i++)
{
key[len] = blocks[i];
//if(len == maxlen-1)
{
hashtype h;
hash(key, (len+1) * sizeof(blocktype), 0, &h);
hashes.push_back(h);
}
//else
{
CombinationKeygenRecurse(key,len+1,maxlen,blocks,blockcount,hash,hashes);
}
}
}
typedef struct { char c[16]; } block16;
typedef struct { char c[32]; } block32;
typedef struct { char c[64]; } block64;
typedef struct { char c[128]; } block128;
template< typename hashtype, typename blocktype >
bool CombinationKeyTest ( hashfunc<hashtype> hash, int maxlen, blocktype* 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;
blocktype * key = new blocktype[maxlen];
CombinationKeygenRecurse(key,0,maxlen,blocks,blockcount,hash,hashes);
delete [] key;
printf("%d keys\n",(int)hashes.size());
//----------
bool result = true;
result &= TestHashList(hashes,drawDiagram,testColl,testDist);
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,drawDiagram,testColl,testDist);
printf("\n");
return result;
}
//-----------------------------------------------------------------------------
// Keyset 'Sparse' - generate all possible N-bit keys with up to K bits set
static void printSparseKey(const void* buffer, size_t size)
{
const char* const p = (const char*)buffer;
size_t s;
printf("bits:");
for (s=0; s<size; s++) {
int b;
for (b=0; b<8; b++) {
if ((p[s] >> b) & 1)
printf(" %2u.%2i,", (unsigned)s, b);
}
}
}
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)
{
hashes.resize(1);
hash(&k,sizeof(keytype),0,&hashes[0]);
}
SparseKeygenRecurse(hash,0,setbits,inclusive,k,hashes);
printf("%d keys\n",(int)hashes.size());
bool result = true;
result &= TestHashList<hashtype>(hashes,drawDiagram,testColl,testDist);
printf("\n");
return result;
}
//-----------------------------------------------------------------------------
// Keyset 'Window' - 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, int windowbits,
bool testCollision, bool testDistribution, bool drawDiagram )
{
const int keybits = sizeof(keytype) * 8;
// calc keycount to expect min. 0.5 collisions: EstimateNbCollisions, except for 64++bit.
// there limit to 2^25 = 33554432 keys
int keycount = 1 << windowbits;
while (EstimateNbCollisions(keycount, sizeof(hashtype) * 8) < 0.5 && windowbits < 25) {
if ((int)log2(2.0 * keycount) < 0) // overflow
break;
keycount *= 2;
windowbits = (int)log2(1.0 * keycount);
//printf (" enlarge windowbits to %d (%d keys)\n", windowbits, keycount);
//fflush (NULL);
}
std::vector<hashtype> hashes;
hashes.resize(keycount);
bool result = true;
int testcount = keybits;
printf("Keyset 'Window' - %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, drawDiagram, testCollision, testDistribution,
/* do not test high/low bits (to not clobber the screen) */
false, false);
//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,drawDiagram);
printf("\n");
delete [] key;
delete [] cycle;
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,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,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 = 200*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,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,drawDiagram);
printf("\n");
return result;
}
//-----------------------------------------------------------------------------