3 * $Id: unihash.c,v 1.3 2004/04/08 01:36:13 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 /*----- Header files ------------------------------------------------------*/
37 /*----- Main code ---------------------------------------------------------*/
39 /* --- @unihash_setkey@ --- *
41 * Arguments: @unihash_info *i@ = where to store the precomputed tables
42 * @uint32 k@ = the key to set, randomly chosen
46 * Use: Calculates the tables required for efficient hashing.
49 static uint32 mul(uint32 x, uint32 y)
55 x = U32(x << 1) ^ UNIHASH_POLY;
63 void unihash_setkey(unihash_info *i, uint32 k)
69 for (a = 0; a < UNIHASH_NBATCH; a++) {
71 for (b = 0; b < 256; b++) {
72 i->s[a][0][b] = mul(x, b << 0);
73 i->s[a][1][b] = mul(x, b << 8);
74 i->s[a][2][b] = mul(x, b << 16);
75 i->s[a][3][b] = mul(x, b << 24);
80 /* --- @unihash_hash@ --- *
82 * Arguments: @const unihash_info *i@ = pointer to precomputed table
83 * @uint32 a@ = @i->[0][0][1]@ or value from previous call
84 * @const void *p@ = pointer to data to hash
85 * @size_t sz@ = size of the data
87 * Returns: Hash of data so far.
89 * Use: Hashes data. Call this as many times as needed.
92 uint32 unihash_hash(const unihash_info *i, uint32 a,
93 const void *p, size_t sz)
97 assert(UNIHASH_NBATCH == 4);
99 #define FULLMULT(u, x) \
100 (i->s[u][0][U8((x) >> 0)] ^ i->s[u][1][U8((x) >> 8)] ^ \
101 i->s[u][2][U8((x) >> 16)] ^ i->s[u][3][U8((x) >> 24)]);
103 #define BYTEMULT(u, x) i->s[u][0][x]
105 /* --- Do the main bulk in batches of %$n$% bytes --- *
107 * We have %$a$% and %$m_{n-1}, \ldots, m_1, m_0$%; we want
109 * %$a' = (a + m_{n-1}) k^n + m_{n-2} k^{n-1} + \cdots + m_1 k^2 + m_0 k$%
112 while (sz >= UNIHASH_NBATCH) {
115 a ^= BYTEMULT(2, *pp++);
116 a ^= BYTEMULT(1, *pp++);
117 a ^= BYTEMULT(0, *pp++);
118 sz -= UNIHASH_NBATCH;
121 /* --- The tail end is a smaller batch --- */
124 case 3: a ^= *pp++; a = FULLMULT(2, a); goto batch_2;
125 case 2: a ^= *pp++; a = FULLMULT(1, a); goto batch_1;
126 case 1: a ^= *pp++; a = FULLMULT(0, a); goto batch_0;
127 batch_2: a ^= BYTEMULT(1, *pp++);
128 batch_1: a ^= BYTEMULT(0, *pp++);
135 /* --- @unihash@ --- *
137 * Arguments: @const unihash_info *i@ = precomputed tables
138 * @const void *p@ = pointer to data to hash
139 * @size_t sz@ = size of the data
141 * Returns: The hash value computed.
143 * Use: All-in-one hashing function. No faster than using the
144 * separate calls, but more convenient.
147 uint32 unihash(const unihash_info *i, const void *p, size_t sz)
149 return (UNIHASH(i, p, sz));
152 /*----- Test rig ----------------------------------------------------------*/
158 static int verify(dstr *v)
168 static const int step[] = { 0, 1, 5, 6, 7, 8, 23, -1 };
170 /* --- Set up for using this key --- */
172 k = *(uint32 *)v[0].buf;
173 h = *(uint32 *)v[2].buf;
174 unihash_setkey(&ui, k);
176 /* --- Hash the data a lot --- */
178 for (i = 0; step[i] >= 0; i++) {
181 hh = unihash(&ui, v[1].buf, v[1].len);
183 hh = UNIHASH_INIT(&ui);
188 hh = unihash_hash(&ui, hh, p, c);
195 fprintf(stderr, "\nunihash failed\n");
196 fprintf(stderr, " key = %08lx\n", (unsigned long)k);
197 fprintf(stderr, " data = %s\n", v[1].buf);
198 fprintf(stderr, " step = %d\n", step[i]);
199 fprintf(stderr, " expected = %08lx\n", (unsigned long)h);
200 fprintf(stderr, " computed = %08lx\n", (unsigned long)hh);
206 static const test_chunk tests[] = {
207 { "hash", verify, { &type_uint32, &type_string, &type_uint32 } },
211 int main(int argc, char *argv[])
213 test_run(argc, argv, tests, "unihash.in");
219 /*----- That's all, folks -------------------------------------------------*/