There is no such thing as a good hash function for universal hashes. Depending on the context different criteria determine the quality of a hash. To test your hash function using data drawn from the same distribution that you expect it works on. When looking at a hash function on 64-bit longs, the default hash function is excellent if the input values are drawn uniformly from all possible long values.
There are different ways to test the distribution, collision, and performance properties of non-cryptographic hash functions. The probability is not a good idea when it comes to hash collisions or distribution. For this, several libraries utilize linear cryptoanalysis or other methods.
The SMHasher is a test suite designed to test the distribution, collision, and performance properties of the non-cryptographic hash function. It aims to be the DieHarder of hash testing and does a pretty good job of finding flaws with several popular hashes.
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 |
template < typename hashtype > void test ( hashfunc<hashtype> hash, HashInfo * info ) { const int hashbits = sizeof(hashtype) * 8; printf("-------------------------------------------------------------------------------n"); printf("--- Testing %s (%s)nn",info->name,info->desc); //----------------------------------------------------------------------------- // Sanity tests if(g_testSanity || g_testAll) { printf("[[[ Sanity Tests ]]]nn"); VerificationTest(hash,hashbits,info->verification,true); SanityTest(hash,hashbits); AppendedZeroesTest(hash,hashbits); printf("n"); } //----------------------------------------------------------------------------- // Speed tests if(g_testSpeed || g_testAll) { printf("[[[ Speed Tests ]]]nn"); BulkSpeedTest(info->hash,info->verification); printf("n"); for(int i = 1; i < 32; i++) { double cycles; TinySpeedTest(hashfunc<hashtype>(info->hash),sizeof(hashtype),i,info->verification,true,cycles); } printf("n"); } //----------------------------------------------------------------------------- // Differential tests if(g_testDiff || g_testAll) { printf("[[[ Differential Tests ]]]nn"); bool result = true; bool dumpCollisions = false; result &= DiffTest< Blob<64>, hashtype >(hash,5,1000,dumpCollisions); result &= DiffTest< Blob<128>, hashtype >(hash,4,1000,dumpCollisions); result &= DiffTest< Blob<256>, hashtype >(hash,3,1000,dumpCollisions); if(!result) printf("*********FAIL*********n"); printf("n"); } //----------------------------------------------------------------------------- // Differential-distribution tests if(g_testDiffDist /*|| g_testAll*/) { printf("[[[ Differential Distribution Tests ]]]nn"); bool result = true; result &= DiffDistTest2<uint64_t,hashtype>(hash); printf("n"); } |
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition