-
Notifications
You must be signed in to change notification settings - Fork 12
/
KeysetTest.cpp
139 lines (94 loc) · 2.81 KB
/
KeysetTest.cpp
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
#include "KeysetTest.h"
#include "Platform.h"
#include "Random.h"
#include <map>
#include <set>
//-----------------------------------------------------------------------------
// Generate all keys of up to N bytes containing two non-zero bytes
void TwoBytesKeygen ( int maxlen, KeyCallback & c, char *name )
{
//----------
// Compute # of keys
int keycount = 0;
for(int i = 2; i <= maxlen; i++) keycount += (int)chooseK(i,2);
keycount *= 255*255;
for(int i = 2; i <= maxlen; i++) keycount += i*255;
if (name)
snprintf(name,1024,"Keyset 'TwoBytes' - up-to-%d-byte keys, %d total keys",
maxlen, keycount);
c.reserve(keycount);
//----------
// Add all keys with one non-zero byte
uint8_t key[256];
memset(key,0,256);
for(int keylen = 2; keylen <= maxlen; keylen++)
for(int byteA = 0; byteA < keylen; byteA++)
{
for(int valA = 1; valA <= 255; valA++)
{
key[byteA] = (uint8_t)valA;
c(key,keylen);
}
key[byteA] = 0;
}
//----------
// Add all keys with two non-zero bytes
for(int keylen = 2; keylen <= maxlen; keylen++)
for(int byteA = 0; byteA < keylen-1; byteA++)
for(int byteB = byteA+1; byteB < keylen; byteB++)
{
for(int valA = 1; valA <= 255; valA++)
{
key[byteA] = (uint8_t)valA;
for(int valB = 1; valB <= 255; valB++)
{
key[byteB] = (uint8_t)valB;
c(key,keylen);
}
key[byteB] = 0;
}
key[byteA] = 0;
}
}
//-----------------------------------------------------------------------------
template< typename hashtype >
void DumpCollisionMap ( CollisionMap<hashtype,ByteVec> & cmap )
{
typedef CollisionMap<hashtype,ByteVec> cmap_t;
for(typename cmap_t::iterator it = cmap.begin(); it != cmap.end(); ++it)
{
const hashtype & hash = (*it).first;
printf("### Hash - ");
printbytes(&hash,sizeof(hashtype));
printf("\n"); // nl ok
std::vector<ByteVec> & keys = (*it).second;
for(int i = 0; i < (int)keys.size(); i++)
{
ByteVec & key = keys[i];
printf("# Key - ");
printbytes(&key[0],(int)key.size());
printf("\n"); // nl ok
}
}
}
// test code
template<typename hashtype>
void ReportCollisions ( hashfunc<hashtype> hash )
{
printf("# Hashing keyset\n");
std::vector<uint128_t> hashes;
HashCallback<uint128_t> c(hash,hashes);
TwoBytesKeygen(20,c,NULL);
printf("# %d hashes\n",(int)hashes.size());
printf("# Finding collisions\n");
HashSet<uint128_t> collisions;
FindCollisions(hashes,collisions,1000);
printf("# %d collisions\n",(int)collisions.size());
printf("# Mapping collisions\n");
CollisionMap<uint128_t,ByteVec> cmap;
CollisionCallback<uint128_t> c2(hash,collisions,cmap);
TwoBytesKeygen(20,c2,NULL);
printf("# Dumping collisions\n");
DumpCollisionMap(cmap);
}
/* vim: set sts=2 sw=2 et: */