a6f9ae2a51c0247a5e68c14c82b39b693425b537
[mLib] / hash / unihash.c
1 /* -*-c-*-
2 *
3 * Simple and efficient universal hashing for hashtables
4 *
5 * (c) 2003 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the mLib utilities library.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <assert.h>
31 #include <stdlib.h>
32
33 #include "unihash.h"
34
35 /*----- Main code ---------------------------------------------------------*/
36
37 /* --- @unihash_setkey@ --- *
38 *
39 * Arguments: @unihash_info *i@ = where to store the precomputed tables
40 * @uint32 k@ = the key to set, randomly chosen
41 *
42 * Returns: ---
43 *
44 * Use: Calculates the tables required for efficient hashing.
45 */
46
47 static uint32 mul(uint32 x, uint32 y)
48 {
49 uint32 z = 0;
50 while (y) {
51 if (y & 1) z ^= x;
52 if (x & (1 << 31))
53 x = U32(x << 1) ^ UNIHASH_POLY;
54 else
55 x = U32(x << 1);
56 y = U32(y >> 1);
57 }
58 return (z);
59 }
60
61 void unihash_setkey(unihash_info *i, uint32 k)
62 {
63 size_t a;
64 size_t b;
65 uint32 x = 1;
66
67 for (a = 0; a < UNIHASH_NBATCH; a++) {
68 x = mul(x, k);
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);
74 }
75 }
76 }
77
78 /* --- @unihash_hash@ --- *
79 *
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
84 *
85 * Returns: Hash of data so far.
86 *
87 * Use: Hashes data. Call this as many times as needed.
88 */
89
90 uint32 unihash_hash(const unihash_info *i, uint32 a,
91 const void *p, size_t sz)
92 {
93 const octet *pp = p;
94
95 assert(UNIHASH_NBATCH == 4);
96
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)]);
100
101 #define BYTEMULT(u, x) i->s[u][0][x]
102
103 /* --- Do the main bulk in batches of %$n$% bytes --- *
104 *
105 * We have %$a$% and %$m_{n-1}, \ldots, m_1, m_0$%; we want
106 *
107 * %$a' = (a + m_{n-1}) k^n + m_{n-2} k^{n-1} + \cdots + m_1 k^2 + m_0 k$%
108 */
109
110 while (sz >= UNIHASH_NBATCH) {
111 a ^= *pp++;
112 a = FULLMULT(3, a);
113 a ^= BYTEMULT(2, *pp++);
114 a ^= BYTEMULT(1, *pp++);
115 a ^= BYTEMULT(0, *pp++);
116 sz -= UNIHASH_NBATCH;
117 }
118
119 /* --- The tail end is a smaller batch --- */
120
121 switch (sz) {
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++);
127 batch_0: break;
128 }
129
130 return (a);
131 }
132
133 /* --- @unihash@ --- *
134 *
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
138 *
139 * Returns: The hash value computed.
140 *
141 * Use: All-in-one hashing function. No faster than using the
142 * separate calls, but more convenient.
143 */
144
145 uint32 unihash(const unihash_info *i, const void *p, size_t sz)
146 { return (UNIHASH(i, p, sz)); }
147
148 /*----- That's all, folks -------------------------------------------------*/