3 * Simple and efficient universal hashing for hashtables
5 * (c) 2003 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
35 /*----- Main code ---------------------------------------------------------*/
37 /* --- @unihash_setkey@ --- *
39 * Arguments: @unihash_info *i@ = where to store the precomputed tables
40 * @uint32 k@ = the key to set, randomly chosen
44 * Use: Calculates the tables required for efficient hashing.
47 static uint32 mul(uint32 x, uint32 y)
53 x = U32(x << 1) ^ UNIHASH_POLY;
61 void unihash_setkey(unihash_info *i, uint32 k)
67 for (a = 0; a < UNIHASH_NBATCH; a++) {
69 for (b = 0; b < 256; b++) {
70 i->s[a][0][b] = mul(x, b << 0);
71 i->s[a][1][b] = mul(x, b << 8);
72 i->s[a][2][b] = mul(x, b << 16);
73 i->s[a][3][b] = mul(x, b << 24);
78 /* --- @unihash_hash@ --- *
80 * Arguments: @const unihash_info *i@ = pointer to precomputed table
81 * @uint32 a@ = @i->[0][0][1]@ or value from previous call
82 * @const void *p@ = pointer to data to hash
83 * @size_t sz@ = size of the data
85 * Returns: Hash of data so far.
87 * Use: Hashes data. Call this as many times as needed.
90 uint32 unihash_hash(const unihash_info *i, uint32 a,
91 const void *p, size_t sz)
95 assert(UNIHASH_NBATCH == 4);
97 #define FULLMULT(u, x) \
98 (i->s[u][0][U8((x) >> 0)] ^ i->s[u][1][U8((x) >> 8)] ^ \
99 i->s[u][2][U8((x) >> 16)] ^ i->s[u][3][U8((x) >> 24)]);
101 #define BYTEMULT(u, x) i->s[u][0][x]
103 /* --- Do the main bulk in batches of %$n$% bytes --- *
105 * We have %$a$% and %$m_{n-1}, \ldots, m_1, m_0$%; we want
107 * %$a' = (a + m_{n-1}) k^n + m_{n-2} k^{n-1} + \cdots + m_1 k^2 + m_0 k$%
110 while (sz >= UNIHASH_NBATCH) {
113 a ^= BYTEMULT(2, *pp++);
114 a ^= BYTEMULT(1, *pp++);
115 a ^= BYTEMULT(0, *pp++);
116 sz -= UNIHASH_NBATCH;
119 /* --- The tail end is a smaller batch --- */
122 case 3: a ^= *pp++; a = FULLMULT(2, a); goto batch_2;
123 case 2: a ^= *pp++; a = FULLMULT(1, a); goto batch_1;
124 case 1: a ^= *pp++; a = FULLMULT(0, a); goto batch_0;
125 batch_2: a ^= BYTEMULT(1, *pp++);
126 batch_1: a ^= BYTEMULT(0, *pp++);
133 /* --- @unihash@ --- *
135 * Arguments: @const unihash_info *i@ = precomputed tables
136 * @const void *p@ = pointer to data to hash
137 * @size_t sz@ = size of the data
139 * Returns: The hash value computed.
141 * Use: All-in-one hashing function. No faster than using the
142 * separate calls, but more convenient.
145 uint32 unihash(const unihash_info *i, const void *p, size_t sz)
147 return (UNIHASH(i, p, sz));
150 /*----- Test rig ----------------------------------------------------------*/
156 static int verify(dstr *v)
166 static const int step[] = { 0, 1, 5, 6, 7, 8, 23, -1 };
168 /* --- Set up for using this key --- */
170 k = *(uint32 *)v[0].buf;
171 h = *(uint32 *)v[2].buf;
172 unihash_setkey(&ui, k);
174 /* --- Hash the data a lot --- */
176 for (i = 0; step[i] >= 0; i++) {
179 hh = unihash(&ui, v[1].buf, v[1].len);
181 hh = UNIHASH_INIT(&ui);
186 hh = unihash_hash(&ui, hh, p, c);
193 fprintf(stderr, "\nunihash failed\n");
194 fprintf(stderr, " key = %08lx\n", (unsigned long)k);
195 fprintf(stderr, " data = %s\n", v[1].buf);
196 fprintf(stderr, " step = %d\n", step[i]);
197 fprintf(stderr, " expected = %08lx\n", (unsigned long)h);
198 fprintf(stderr, " computed = %08lx\n", (unsigned long)hh);
204 static const test_chunk tests[] = {
205 { "hash", verify, { &type_uint32, &type_string, &type_uint32 } },
209 int main(int argc, char *argv[])
211 test_run(argc, argv, tests, "unihash.in");
217 /*----- That's all, folks -------------------------------------------------*/