8 #ifndef HASHING_UTIL_H_
9 #define HASHING_UTIL_H_
11 #include "../hashing_includes.h"
14 typedef uint16_t TABLEID_T;
17 #define MAX_TABLE_SIZE_BYTES sizeof(TABLEID_T)
18 #define DUMMY_ENTRY_SERVER 0x00
19 #define DUMMY_ENTRY_CLIENT 0xFF
21 #define USE_LUBY_RACKOFF
25 uint32_t*** hf_values;
36 uint32_t* address_used;
42 static const uint32_t SELECT_BITS[33] = \
43 {0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F, \
44 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF, \
45 0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, \
46 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, \
50 static const uint32_t SELECT_BITS_INV[33] = \
51 {0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFC, 0xFFFFFFF8, 0xFFFFFFF0, 0xFFFFFFE0, 0xFFFFFFC0, 0xFFFFFF80, \
52 0xFFFFFF00, 0xFFFFFE00, 0xFFFFFC00, 0xFFFFF800, 0xFFFFF000, 0xFFFFE000, 0xFFFFC000, 0xFFFF8000, \
53 0xFFFF0000, 0xFFFE0000, 0xFFFC0000, 0xFFF80000, 0xFFF00000, 0xFFE00000, 0xFFC00000, 0xFF800000, \
54 0xFF000000, 0xFE000000, 0xFC000000, 0xF8000000, 0xF0000000, 0xE0000000, 0xC0000000, 0x80000000, \
57 static const uint8_t BYTE_SELECT_BITS_INV[8] = {0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01};
60 static void init_hashing_state(
hs_t* hs, uint32_t nelements, uint32_t inbitlen, uint32_t nbins,
62 uint32_t i, j, nrndbytes;
63 hs->nhashfuns = nhashfuns;
64 hs->nelements = nelements;
67 hs->inbitlen = inbitlen;
68 hs->addrbitlen = min((uint32_t) ceil_log2(nbins), inbitlen);
70 #ifdef USE_LUBY_RACKOFF
71 hs->outbitlen = hs->inbitlen - hs->addrbitlen+1;
73 hs->outbitlen = inbitlen;
80 hs->inbytelen = ceil_divide(hs->inbitlen, 8);
81 hs->addrbytelen = ceil_divide(hs->addrbitlen, 8);
82 hs->outbytelen = ceil_divide(hs->outbitlen, 8);
84 hs->nhfvals = ceil_divide(hs->outbytelen, MAX_TABLE_SIZE_BYTES);
87 nrndbytes = (1<<(8*MAX_TABLE_SIZE_BYTES)) *
sizeof(uint32_t);
93 hs->hf_values = (uint32_t***) malloc(
sizeof(uint32_t**) * hs->nhashfuns);
94 for(i = 0; i < hs->nhashfuns; i++) {
95 hs->hf_values[i] = (uint32_t**) malloc(
sizeof(uint32_t*) * hs->nhfvals);
97 for(j = 0; j < hs->nhfvals; j++) {
98 hs->hf_values[i][j] = (uint32_t*) malloc(nrndbytes);
99 assert(hs->hf_values[i][j]);
100 gen_rnd_bytes(prf_state, (uint8_t*) hs->hf_values[i][j], nrndbytes);
104 hs->address_used = (uint32_t*) calloc(nbins,
sizeof(uint32_t));
105 hs->mask = 0xFFFFFFFF >> hs->addrbitlen;
106 if(hs->inbytelen <
sizeof(uint32_t)) {
107 hs->mask >>= (
sizeof(uint32_t) * 8 - hs->inbitlen - hs->addrbitlen);
111 static void free_hashing_state(
hs_t* hs) {
113 for(i = 0; i < hs->nhashfuns; i++) {
114 for(j = 0; j < hs->nhfvals; j++) {
115 free(hs->hf_values[i][j]);
117 free(hs->hf_values[i]);
119 free(hs->address_used);
130 inline void hashElement(uint8_t* element, uint32_t* address, uint8_t* val,
hs_t* hs) {
135 #ifdef USE_LUBY_RACKOFF
138 TABLEID_T hfmaskaddr;
140 L = *((uint32_t*) element) & SELECT_BITS[hs->addrbitlen];
142 R = (*((uint32_t*) element) & SELECT_BITS_INV[hs->addrbitlen]) >> (hs->addrbitlen);
159 hfmaskaddr = R *
sizeof(uint32_t);
161 for(i = 0; i < hs->nhashfuns; i++) {
165 for(j = 0; j < hs->nhfvals; j++) {
169 address[i] = (L ^ *((hs->hf_values[i][j]+hfmaskaddr))) % hs->nbins;
176 #ifndef TEST_UTILIZATION
178 *((uint32_t*) val) = R;
183 if(hs->inbitlen >
sizeof(uint32_t) * 8) {
185 memcpy(val + (
sizeof(uint32_t) - (hs->addrbitlen >>3)), element +
sizeof(uint32_t), hs->outbytelen - (
sizeof(uint32_t) - (hs->addrbitlen >>3)));
190 val[hs->outbytelen-1] &= (BYTE_SELECT_BITS_INV[hs->outbitlen & 0x03]);
207 for(uint64_t i = 0; i < NUM_HASH_FUNCTIONS; i++) {
208 address[i] = ((*((uint32_t*) element+i) ^ HF_MASKS[i]) & SELECT_BITS[hs->addrbitlen]) % hs->nbins;
210 #ifndef TEST_UTILIZATION
211 *((uint32_t*) val) = (*((uint32_t*) element) & SELECT_BITS_INV[hs->addrbitlen]) >> (hs->addrbitlen);
214 if(hs->outbytelen >=
sizeof(uint32_t))
215 memcpy(val + (
sizeof(uint32_t) - hs->addrbytelen), element +
sizeof(uint32_t), hs->outbytelen -
sizeof(uint32_t));
221 inline void domain_hashing(uint32_t nelements, uint8_t* elements, uint32_t elebytelen, uint8_t* result,
222 uint32_t resultbytelen,
crypto* crypt) {
224 uint8_t *eleptr, *resultptr, *hash_buf;
230 cout <<
"Hashing " << nelements <<
" elements from " << elebytelen <<
" bytes into " << resultbytelen <<
" bytes" << endl;
232 hash_buf = (uint8_t*) calloc(crypt->get_hash_bytes(),
sizeof(uint8_t));
233 for(i = 0; i < nelements; i++, resultptr+=resultbytelen, eleptr+=elebytelen) {
234 memcpy(hash_buf, eleptr, elebytelen);
235 crypt->hash(resultptr, resultbytelen, hash_buf, elebytelen);
240 inline void domain_hashing(uint32_t nelements, uint8_t** elements, uint32_t* elebytelens, uint8_t* result,
241 uint32_t resultbytelen,
crypto* crypt) {
248 cout <<
"Hashing " << nelements <<
" elements from " << elebytelens <<
" bytes into " << resultbytelen <<
" bytes" << endl;
251 for(i = 0; i < nelements; i++, resultptr+=resultbytelen) {
253 crypt->hash(resultptr, resultbytelen, elements[i], elebytelens[i]);
Definition: hashing_util.h:23