Skip to content

Commit

Permalink
Slightly speed up BadSeed testing, and make output slightly friendlier
Browse files Browse the repository at this point in the history
  • Loading branch information
fwojcik committed Dec 13, 2021
1 parent 6a9092a commit 4de7b3c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
15 changes: 7 additions & 8 deletions tests/BadSeedsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ static bool TestSecret ( const HashInfo* info, const uint64_t secret ) {
static hashtype zero;
pfHash hash = info->hash;
uint8_t key[128];
HashSet<hashtype> collisions_dummy;
// Currently *only* seeds going through Hash_Seed_init() can be
// wider than 32 bits!!
if (!Hash_Seed_init (hash, secret) && (secret > UINT64_C(0xffffffff)))
Expand All @@ -91,14 +92,14 @@ static bool TestSecret ( const HashInfo* info, const uint64_t secret ) {
else
hashes.push_back(h);
}
if (!TestHashList(hashes, false, true, false, false, false, false)) {
if (FindCollisions(hashes, collisions_dummy, 0, false) > 0) {
printf("Confirmed bad seed 0x%" PRIx64 " for len %d ", secret, len);
#if !defined __clang__ && !defined _MSC_VER
printf("=> hashes: ");
for (hashtype x : hashes) printf ("%lx ", x);
#endif
printf ("\n");
TestHashList(hashes, false);
TestHashList(hashes, true, true, false, false, false, true);
result = false;
}
}
Expand Down Expand Up @@ -132,6 +133,7 @@ static void TestSecretRangeThread ( const HashInfo* info, const uint64_t hi,
#endif
pfHash hash = info->hash;
std::vector<hashtype> hashes;
HashSet<hashtype> collisions_dummy;
int fails = 0;
hashes.resize(4);
result = true;
Expand Down Expand Up @@ -166,6 +168,7 @@ static void TestSecretRangeThread ( const HashInfo* info, const uint64_t hi,
uint8_t key[64]; // for crc32_pclmul, otherwie we would need only 16 byte
memset(&key, x, sizeof(key));
hash(key, 16, seed, &h);
hashes.push_back(h);
if (h == 0 && x == 0) {
bool known_seed = (std::find(secrets.begin(), secrets.end(), seed) != secrets.end());
{
Expand All @@ -177,17 +180,13 @@ static void TestSecretRangeThread ( const HashInfo* info, const uint64_t hi,
else
printf("\nNew broken seed 0x%" PRIx64 " => 0 with key[16] of all %d bytes\n", seed, x);
}
hashes.push_back(h);
fails++;
result = false;
if (!known_seed)
newresult = true;
}
else {
hashes.push_back(h);
}
}
if (!TestHashList(hashes, false, true, false, false, false, false)) {
if (FindCollisions(hashes, collisions_dummy, 0, false) > 0) {
#if NCPU > 1
std::lock_guard<std::mutex> lock(print_mutex);
#endif
Expand All @@ -198,7 +197,7 @@ static void TestSecretRangeThread ( const HashInfo* info, const uint64_t hi,
printf("\nNew bad seed 0x%" PRIx64 "\n", seed);
fails++;
if (!known_seed && (fails < 32)) // don't print too many lines
TestHashList(hashes, false);
TestHashList(hashes, true, true, false, false, false, true);
result = false;
if (!known_seed)
newresult = true;
Expand Down
9 changes: 6 additions & 3 deletions util/Analyze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ static void plot ( double n )
// Sort the hash list, count the total number of collisions and return
// the first N collisions for further processing
template< typename hashtype >
static unsigned int FindCollisions ( std::vector<hashtype> & hashes,
unsigned int FindCollisions ( std::vector<hashtype> & hashes,
HashSet<hashtype> & collisions,
int maxCollisions = 1000,
bool drawDiagram = false)
int maxCollisions,
bool drawDiagram)
{
unsigned int collcount = 0;
blobsort(hashes.begin(),hashes.end());
Expand Down Expand Up @@ -258,6 +258,8 @@ static unsigned int FindCollisions ( std::vector<hashtype> & hashes,
#endif
}

INSTANTIATE(FindCollisions, HASHTYPELIST);

//-----------------------------------------------------------------------------
// If threshHBits is 0, then this tallies the total number of
// collisions across all given hashes for each bit window in the range
Expand Down Expand Up @@ -417,6 +419,7 @@ static int PrintCollisions ( HashSet<hashtype> & collisions )
printhex(&hash, sizeof(hashtype));
printf("\n");
}
printf("\n");
return 0;
}

Expand Down
6 changes: 6 additions & 0 deletions util/Analyze.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
*/
#include <vector>

template < typename hashtype >
unsigned int FindCollisions ( std::vector<hashtype> & hashes,
HashSet<hashtype> & collisions,
int maxCollisions = 1000,
bool drawDiagram = false);

template < typename hashtype >
bool TestHashList ( std::vector<hashtype> & hashes, bool drawDiagram,
bool testCollision = true, bool testDist = true,
Expand Down

0 comments on commit 4de7b3c

Please sign in to comment.