3 * $Id: unihash.c,v 1.2 2003/12/14 14:45:30 mdw Exp $
5 * Simple and efficient universal hashing for hashtables
7 * (c) 2003 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2003/12/14 14:45:30 mdw
34 * Test universal hashing and fix bugs.
36 * Revision 1.1 2003/10/12 14:43:24 mdw
41 /*----- Header files ------------------------------------------------------*/
48 /*----- Main code ---------------------------------------------------------*/
50 /* --- @unihash_setkey@ --- *
52 * Arguments: @unihash_info *i@ = where to store the precomputed tables
53 * @uint32 k@ = the key to set, randomly chosen
57 * Use: Calculates the tables required for efficient hashing.
60 static uint32 mul(uint32 x, uint32 y)
66 x = U32(x << 1) ^ UNIHASH_POLY;
74 void unihash_setkey(unihash_info *i, uint32 k)
80 for (a = 0; a < UNIHASH_NBATCH; a++) {
82 for (b = 0; b < 256; b++) {
83 i->s[a][0][b] = mul(x, b << 0);
84 i->s[a][1][b] = mul(x, b << 8);
85 i->s[a][2][b] = mul(x, b << 16);
86 i->s[a][3][b] = mul(x, b << 24);
91 /* --- @unihash_hash@ --- *
93 * Arguments: @const unihash_info *i@ = pointer to precomputed table
94 * @uint32 a@ = @i->[0][0][1]@ or value from previous call
95 * @const void *p@ = pointer to data to hash
96 * @size_t sz@ = size of the data
98 * Returns: Hash of data so far.
100 * Use: Hashes data. Call this as many times as needed.
103 uint32 unihash_hash(const unihash_info *i, uint32 a,
104 const void *p, size_t sz)
108 assert(UNIHASH_NBATCH == 4);
110 #define FULLMULT(u, x) \
111 (i->s[u][0][U8((x) >> 0)] ^ i->s[u][1][U8((x) >> 8)] ^ \
112 i->s[u][2][U8((x) >> 16)] ^ i->s[u][3][U8((x) >> 24)]);
114 #define BYTEMULT(u, x) i->s[u][0][x]
116 /* --- Do the main bulk in batches of %$n$% bytes --- *
118 * We have %$a$% and %$m_{n-1}, \ldots, m_1, m_0$%; we want
120 * %$a' = (a + m_{n-1}) k^n + m_{n-2} k^{n-1} + \cdots + m_1 k^2 + m_0 k$%
123 while (sz >= UNIHASH_NBATCH) {
126 a ^= BYTEMULT(2, *pp++);
127 a ^= BYTEMULT(1, *pp++);
128 a ^= BYTEMULT(0, *pp++);
129 sz -= UNIHASH_NBATCH;
132 /* --- The tail end is a smaller batch --- */
135 case 3: a ^= *pp++; a = FULLMULT(2, a); goto batch_2;
136 case 2: a ^= *pp++; a = FULLMULT(1, a); goto batch_1;
137 case 1: a ^= *pp++; a = FULLMULT(0, a); goto batch_0;
138 batch_2: a ^= BYTEMULT(1, *pp++);
139 batch_1: a ^= BYTEMULT(0, *pp++);
146 /* --- @unihash@ --- *
148 * Arguments: @const unihash_info *i@ = precomputed tables
149 * @const void *p@ = pointer to data to hash
150 * @size_t sz@ = size of the data
152 * Returns: The hash value computed.
154 * Use: All-in-one hashing function. No faster than using the
155 * separate calls, but more convenient.
158 uint32 unihash(const unihash_info *i, const void *p, size_t sz)
160 return (UNIHASH(i, p, sz));
163 /*----- Test rig ----------------------------------------------------------*/
169 static int verify(dstr *v)
179 static const int step[] = { 0, 1, 5, 6, 7, 8, 23, -1 };
181 /* --- Set up for using this key --- */
183 k = *(uint32 *)v[0].buf;
184 h = *(uint32 *)v[2].buf;
185 unihash_setkey(&ui, k);
187 /* --- Hash the data a lot --- */
189 for (i = 0; step[i] >= 0; i++) {
192 hh = unihash(&ui, v[1].buf, v[1].len);
194 hh = UNIHASH_INIT(&ui);
199 hh = unihash_hash(&ui, hh, p, c);
206 fprintf(stderr, "\nunihash failed\n");
207 fprintf(stderr, " key = %08lx\n", (unsigned long)k);
208 fprintf(stderr, " data = %s\n", v[1].buf);
209 fprintf(stderr, " step = %d\n", step[i]);
210 fprintf(stderr, " expected = %08lx\n", (unsigned long)h);
211 fprintf(stderr, " computed = %08lx\n", (unsigned long)hh);
217 static const test_chunk tests[] = {
218 { "hash", verify, { &type_uint32, &type_string, &type_uint32 } },
222 int main(int argc, char *argv[])
224 test_run(argc, argv, tests, "unihash.in");
230 /*----- That's all, folks -------------------------------------------------*/