chiark / gitweb /
Ignore mlib-bin.
[mLib] / unihash.c
1 /* -*-c-*-
2  *
3  * $Id: unihash.c,v 1.2 2003/12/14 14:45:30 mdw Exp $
4  *
5  * Simple and efficient universal hashing for hashtables
6  *
7  * (c) 2003 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the mLib utilities library.
13  *
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.
18  * 
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.
23  * 
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,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: unihash.c,v $
33  * Revision 1.2  2003/12/14 14:45:30  mdw
34  * Test universal hashing and fix bugs.
35  *
36  * Revision 1.1  2003/10/12 14:43:24  mdw
37  * Universal hashing.
38  *
39  */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include <assert.h>
44 #include <stdlib.h>
45
46 #include "unihash.h"
47
48 /*----- Main code ---------------------------------------------------------*/
49
50 /* --- @unihash_setkey@ --- *
51  *
52  * Arguments:   @unihash_info *i@ = where to store the precomputed tables
53  *              @uint32 k@ = the key to set, randomly chosen
54  *
55  * Returns:     ---
56  *
57  * Use:         Calculates the tables required for efficient hashing.
58  */
59
60 static uint32 mul(uint32 x, uint32 y)
61 {
62   uint32 z = 0;
63   while (y) {
64     if (y & 1) z ^= x;
65     if (x & (1 << 31))
66       x = U32(x << 1) ^ UNIHASH_POLY;
67     else
68       x = U32(x << 1);
69     y = U32(y >> 1);
70   }
71   return (z);
72 }
73
74 void unihash_setkey(unihash_info *i, uint32 k)
75 {
76   size_t a;
77   size_t b;
78   uint32 x = 1;
79
80   for (a = 0; a < UNIHASH_NBATCH; a++) {
81     x = mul(x, k);
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);
87     }
88   }
89 }
90
91 /* --- @unihash_hash@ --- *
92  *
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
97  *
98  * Returns:     Hash of data so far.
99  *
100  * Use:         Hashes data.  Call this as many times as needed.  
101  */
102
103 uint32 unihash_hash(const unihash_info *i, uint32 a,
104                     const void *p, size_t sz)
105 {
106   const octet *pp = p;
107
108   assert(UNIHASH_NBATCH == 4);
109
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)]);
113
114 #define BYTEMULT(u, x) i->s[u][0][x]
115
116   /* --- Do the main bulk in batches of %$n$% bytes --- *
117    *
118    * We have %$a$% and %$m_{n-1}, \ldots, m_1, m_0$%; we want
119    *
120    * %$a' = (a + m_{n-1}) k^n + m_{n-2} k^{n-1} + \cdots + m_1 k^2 + m_0 k$%
121    */
122
123   while (sz >= UNIHASH_NBATCH) {
124     a ^= *pp++;
125     a = FULLMULT(3, a);
126     a ^= BYTEMULT(2, *pp++);
127     a ^= BYTEMULT(1, *pp++);
128     a ^= BYTEMULT(0, *pp++);
129     sz -= UNIHASH_NBATCH;
130   }
131
132   /* --- The tail end is a smaller batch --- */
133
134   switch (sz) {
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++);
140     batch_0: break;
141   }
142
143   return (a);
144 }
145
146 /* --- @unihash@ --- *
147  *
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
151  *
152  * Returns:     The hash value computed.
153  *
154  * Use:         All-in-one hashing function.  No faster than using the
155  *              separate calls, but more convenient.
156  */
157
158 uint32 unihash(const unihash_info *i, const void *p, size_t sz)
159 {
160   return (UNIHASH(i, p, sz));
161 }
162
163 /*----- Test rig ----------------------------------------------------------*/
164
165 #ifdef TEST_RIG
166
167 #include "testrig.h"
168
169 static int verify(dstr *v)
170 {
171   unihash_info ui;
172   uint32 k;
173   uint32 h, hh;
174   size_t n;
175   int i, c;
176   const char *p;
177   int ok = 1;
178
179   static const int step[] = { 0, 1, 5, 6, 7, 8, 23, -1 };
180
181   /* --- Set up for using this key --- */
182
183   k = *(uint32 *)v[0].buf;
184   h = *(uint32 *)v[2].buf;
185   unihash_setkey(&ui, k);
186
187   /* --- Hash the data a lot --- */
188
189   for (i = 0; step[i] >= 0; i++) {
190     c = step[i];
191     if (!c)
192       hh = unihash(&ui, v[1].buf, v[1].len);
193     else {
194       hh = UNIHASH_INIT(&ui);
195       p = v[1].buf;
196       n = v[1].len;
197       while (n) {
198         if (c > n) c = n;
199         hh = unihash_hash(&ui, hh, p, c);
200         p += c;
201         n -= c;
202       }
203     }
204     if (h != hh) {
205       ok = 0;
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);
212     }
213   }
214   return (ok);
215 }
216
217 static const test_chunk tests[] = {
218   { "hash", verify, { &type_uint32, &type_string, &type_uint32 } },
219   { 0, 0, { 0 } }
220 };
221
222 int main(int argc, char *argv[])
223 {
224   test_run(argc, argv, tests, "unihash.in");
225   return (0);
226 }
227
228 #endif
229
230 /*----- That's all, folks -------------------------------------------------*/