Removing code for unused hash functions.

This commit is contained in:
Robin Sommer 2011-04-01 16:05:19 -07:00
parent 4677174aa4
commit 03c0d587a4
7 changed files with 14 additions and 355 deletions

13
CHANGES
View file

@ -1,3 +1,16 @@
1.6-dev.71 Fri Apr 1 16:06:33 PDT 2011
- Removing code for the following no longer supported functionality.
* Trace rewriting.
* DFA state expiration in regexp engine.
* Active mapping.
* Unused hash functions.
(Robin Sommer)
- Fixing crashes when SSL is not configured correctly. (Robin Sommer)
1.6-dev.66 Tue Mar 29 21:52:01 PDT 2011 1.6-dev.66 Tue Mar 29 21:52:01 PDT 2011
- Initial btest setup (Don Appleman and Robin Sommer) - Initial btest setup (Don Appleman and Robin Sommer)

View file

@ -1 +1 @@
1.6-dev.66 1.6-dev.71

View file

@ -349,7 +349,6 @@ set(bro_SRCS
Timer.cc Timer.cc
Traverse.cc Traverse.cc
Trigger.cc Trigger.cc
TwoWise.cc
Type.cc Type.cc
UDP.cc UDP.cc
Val.cc Val.cc

View file

@ -21,202 +21,14 @@
#include "Hash.h" #include "Hash.h"
// Define *one* of the following as the universal hash function family to use.
// #define USE_DIETZFELBINGER // TwoWise
#define USE_H3
// #define USE_UHASH_CW
// #define USE_UMAC_NH
int hash_cnt_all = 0, hash_cnt_uhash = 0;
#if defined(USE_DIETZFELBINGER)
#include "TwoWise.h"
const TwoWise* two_wise = 0;
#elif defined(USE_H3)
#include "H3.h" #include "H3.h"
const H3<hash_t, UHASH_KEY_SIZE>* h3; const H3<hash_t, UHASH_KEY_SIZE>* h3;
#elif defined(USE_UHASH_CW)
// The Carter-Wegman family of universal hash functions.
// f(x) = (sum(a_i * x_i) mod p) mod N
// where p is a prime number between N and 2N.
// Here N = 2^32.
class UHashCW {
typedef uint32 word_t;
public:
UHashCW(int arg_max_key_size)
{
max_num_words = (arg_max_key_size + sizeof(word_t) - 1) /
sizeof(word_t);
a = new word_t[max_num_words + 1];
x = new word_t[max_num_words + 1];
for ( int i = 0; i < max_num_words + 1; ++i )
a[i] = rand32bit();
b = rand64bit();
}
~UHashCW()
{
delete [] a;
delete [] x;
}
uint32 hash(int len, const u_char* data) const
{
int xlen = (len + sizeof(word_t) - 1) / sizeof(word_t);
ASSERT(xlen <= max_num_words);
x[xlen] = 0;
x[xlen-1] = 0; // pad with 0
memcpy(static_cast<void *>(x), data, len);
uint64 h = b;
for ( int i = 0; i < xlen; ++i )
h += (static_cast<uint64>(x[i]) * a[i]);
h += static_cast<uint64>(len) * a[xlen];
// h = h % kPrime
//
// Here we use a trick given that h is a Mersenne prime:
//
// Let K = 2^61. Let h = a * K + b.
// Thus, h = a * (K-1) + (a + b).
h = (h & kPrime) + (h >> 61);
if ( h >= kPrime )
h -= kPrime;
// h = h % 2^32
return static_cast<uint32>(0xffffffffUL & h);
}
protected:
static const uint64 kPrime = (static_cast<uint64>(1) << 61) - 1;
int max_num_words;
word_t* a;
uint64 b;
word_t* x;
};
const UHashCW* uhash_cw = 0;
#elif defined(USE_UMAC_NH)
// Use the NH hash function proposed in UMAC.
// (See http://www.cs.ucdavis.edu/~rogaway/umac/)
//
// Essentially, it is computed as:
//
// H = (x_0 +_16 k_0) * (x_1 +_16 k_1) +
// (x_2 +_16 k_2) * (x_3 +_16 k_3) + ...
//
// where {k_i} are keys for universal hashing,
// {x_i} are data words, and +_16 means plus mod 2^16.
//
// This is faster than UHASH_CW because no modulo operation
// is needed. But note that it is 2^-16 universal, while the
// other universal functions are (almost) 2^-32 universal.
//
// Note: UMAC now has a code release under a BSD-like license, and we may want
// to consider using it instead of our home-grown code.
#ifndef DEBUG
#error "UMAC/NH is experimental code."
#endif
class UMacNH {
// NH uses 16-bit words
typedef uint16 word_t;
public:
UMacNH(int arg_max_key_size)
{
max_num_words = (arg_max_key_size + sizeof(word_t) - 1) /
sizeof(word_t);
// Make max_num_words 2n+1
if ( max_num_words % 2 == 0 )
++max_num_words;
a = new word_t[max_num_words + 1];
x = new word_t[max_num_words + 1];
for ( int i = 0; i < max_num_words + 1; ++i )
a[i] = rand16bit();
}
~UMacNH()
{
delete [] a;
delete [] x;
}
uint32 hash(int len, const u_char* data) const
{
int xlen = (len + sizeof(word_t) - 1) / sizeof(word_t);
if ( xlen % 2 == 0 )
++xlen;
ASSERT(xlen <= max_num_words);
x[xlen] = len;
x[xlen-1] = 0; // pad with 0
if ( xlen >= 2 )
x[xlen-2] = 0;
memcpy(static_cast<void *>(x), data, len);
uint32 h = 0;
for ( int i = 0; i <= xlen; i += 2 )
h += (static_cast<uint32>(x[i] + a[i]) *
static_cast<uint32>(x[i+1] + a[i+1]));
return h;
}
protected:
int max_num_words;
word_t* a;
word_t* x;
};
const UMacNH* umac_nh = 0;
#else
#ifdef DEBUG
#error "No universal hash function is used."
#endif
#endif
void init_hash_function() void init_hash_function()
{ {
// Make sure we have already called init_random_seed(). // Make sure we have already called init_random_seed().
ASSERT(hmac_key_set); ASSERT(hmac_key_set);
// Both Dietzfelbinger and H3 use random() to generate keys
// -- is it strong enough?
#if defined(USE_DIETZFELBINGER)
two_wise = new TwoWise((UHASH_KEY_SIZE + 3) >> 2);
#elif defined(USE_H3)
h3 = new H3<hash_t, UHASH_KEY_SIZE>(); h3 = new H3<hash_t, UHASH_KEY_SIZE>();
#elif defined(USE_UHASH_CW)
uhash_cw = new UHashCW(UHASH_KEY_SIZE);
#elif defined(USE_UMAC_NH)
umac_nh = new UMacNH(UHASH_KEY_SIZE);
#endif
} }
HashKey::HashKey(bro_int_t i) HashKey::HashKey(bro_int_t i)
@ -354,24 +166,11 @@ void* HashKey::CopyKey(const void* k, int s) const
hash_t HashKey::HashBytes(const void* bytes, int size) hash_t HashKey::HashBytes(const void* bytes, int size)
{ {
++hash_cnt_all;
if ( size <= UHASH_KEY_SIZE ) if ( size <= UHASH_KEY_SIZE )
{ {
const uint8* b = reinterpret_cast<const uint8*>(bytes); const uint8* b = reinterpret_cast<const uint8*>(bytes);
++hash_cnt_uhash;
#if defined(USE_DIETZFELBINGER)
return two_wise->Hash(size, b);
#elif defined(USE_H3)
// H3 doesn't check if size is zero // H3 doesn't check if size is zero
return ( size == 0 ) ? 0 : (*h3)(bytes, size); return ( size == 0 ) ? 0 : (*h3)(bytes, size);
#elif defined(USE_UHASH_CW)
return uhash_cw->hash(size, b);
#elif defined(USE_UMAC_NH)
return umac_nh->hash(size, b);
#else
--hash_cnt_uhash;
#endif
} }
// Fall back to HMAC/MD5 for longer data (which is usually rare). // Fall back to HMAC/MD5 for longer data (which is usually rare).

View file

@ -86,7 +86,6 @@ protected:
int size, hash; int size, hash;
}; };
extern int hash_cnt_all, hash_cnt_uhash;
extern void init_hash_function(); extern void init_hash_function();
#endif #endif

View file

@ -1,59 +0,0 @@
/* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
// $Id: TwoWise.cc 1386 2005-09-14 21:42:13Z vern $
//
// Implementation of 2-wise independent hash functions. Contributed
// by Yin Zhang.
//
#include <stdlib.h>
#include "TwoWise.h"
TwoWise::TwoWise(int arg_dim)
{
dim = arg_dim;
int n = dim > 2 ? dim : 2;
a = new uint64[n];
b = new uint64[n];
c = new uint32[n];
for ( int i = 0; i < n; ++i )
{
a[i] = rand64bit() & ~(1ULL);
b[i] = rand64bit() & ~(1ULL);
c[i] = 0;
}
a0 = a[0];
b0 = b[0];
a1 = a[1];
b1 = b[1];
}
TwoWise::~TwoWise()
{
delete[] a;
delete[] b;
delete[] c;
}
void TwoWise::TestSpeed(uint32 N)
{
uint32 x = 0, i;
double start_time = current_time();
for ( i = 0; i < N; ++i )
x ^= Hash(i);
double end_time = current_time();
double time0 = end_time - start_time;
start_time = current_time();
for ( i = 0; i < N; ++i )
x ^= Hash(i, i);
end_time = current_time();
double time1 = end_time - start_time;
fprintf(stderr, "time0=%.6f time1=%.6f x=%u\n",
time0, time1, x);
}

View file

@ -1,92 +0,0 @@
/* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
// $Id: TwoWise.h 2809 2006-04-23 20:26:07Z vern $
//
// Implementation of 2-wise independent hash functions. Contributed
// by Yin Zhang.
//
#ifndef twowise_h
#define twowise_h
#include "util.h"
typedef union {
uint64 as_int64;
uint32 as_int32s[2];
uint32 as_int16s[4];
} int64views;
#ifdef WORDS_BIGENDIAN
#define TOP32BITS(h) h.as_int32s[0]
#else
#define TOP32BITS(h) h.as_int32s[1]
#endif
typedef union {
uint32 as_int32;
uint16 as_int16s[2];
uint16 as_int8s[4];
} int32views;
class TwoWise {
public:
TwoWise(int dim = 0);
~TwoWise();
uint32 Hash(uint32 k) const
{
int64views h;
h.as_int64 = a0*k + b0;
return TOP32BITS(h);
}
uint32 Hash(uint32 k0, uint32 k1) const
{
int64views h;
h.as_int64 = (a0*k0+b0) ^ (a1*k1+b1);
return TOP32BITS(h);
}
uint32 Hash(const uint32* k) const
{
int64views h;
h.as_int64 = (a0*k[0]+b0);
for ( int i = 1; i < dim; ++i )
h.as_int64 ^= (a[i]*k[i] + b[i]);
return TOP32BITS(h);
}
uint32 Hash(int size, const uint8* data) const
{
if ( size == 0 )
return 0;
// Copy data to c to resolve any potential alignment problem.
int num_words = (size + 3) >> 2;
c[num_words - 1] = 0; // pad with 0
memcpy(c, data, size);
int64views h;
h.as_int64 = (a0*c[0]+b0);
for ( int i = 1; i < num_words; ++i )
h.as_int64 ^= (a[i]*c[i] + b[i]);
return TOP32BITS(h);
}
void TestSpeed(uint32 N = 1000000);
private:
// Coefficients in Dietzfelbinger scheme.
uint64 a0, b0, a1, b1; // for 1-d and 2-d case
uint64 *a, *b; // for N-d case
uint32 *c;
int dim;
};
#endif // twowise_h