3 * $Id: unihash.h,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 #ifndef MLIB_UNIHASH_H
42 #define MLIB_UNIHASH_H
49 /*----- Concept -----------------------------------------------------------*
51 * Let %$\gf{q}$% be a finite field. Choose an arbitrary %$k \inr \gf{q}$%.
52 * Let %$M$% be a message. Injectively pad %$M$% and split it into blocks
53 * $m_{n-1}, m_{n-2}, \ldots, m_2, m_1, m_0$% in %$\gf{q}%.
56 * %$H_k(M) = k^{n+1} + \sum_{0\le i<n} m_i k^{i+1}.$%
58 * Note that %$H_0(M) = 0$% for all messages %$M$%.
60 * If we deal with messages at most %$\ell$% blocks long then %$H_k(\cdot)$%
61 * is %$(\ell + 1)/q$%-almost universal. Moreover, if %$q = 2^f$% then
62 * %$H_k(\cdot)$% is %$(\ell + 1)/q$%-almost XOR-universal.
64 * Proof. Let %$A$% and %$B$% be two messages, represented by
65 * %$a_{n-1}, \ldots, a_0$% and %$b_{m-1}, \ldots, b_0$% respectively; and
66 * choose any %$\delta \in \gf{q}$%. We must bound the probability that
68 * %$k^{n+1} + a_{n-1} k^{n} + \cdots + a_1 k^2 + a_0 k - {}$%
69 * %$k^{m+1} - b_{m-1} k^{m} - \cdots - b_1 k^2 - b_0 k = \delta$%.
71 * Firstly, we claim that if %$A$% and %$B$% are distinct, there is some
72 * nonzero coefficient of %$k$%. For if %$n \ne m$% then, without loss of
73 * generality, let %$n > m$%, and hence the coefficient of %$k_n$% is
74 * nonzero. Alternatively, if %$n = m$% then there must be some
75 * %$i \in \{ 0, \ldots, n - 1 \}$% with %$a_i \ne b_i$%, for otherwise the
76 * messages would be identical; but then the coefficient of %$k^{i+1}$% is
77 * %$a_i - b_i \ne 0$%.
79 * Hence we have a polynomial equation with degree at most %$\ell + 1$%;
80 * there must be at most %$\ell + 1$% solutions for %$k$%; but we choose
81 * %$k$% at random from a set of %$q$%; so the equation is true with
82 * probability at most %$(\ell + 1)/q$%.
84 * This function can be used as a simple MAC with provable security against
85 * computationally unbounded adversaries. Simply XOR the hash with a random
86 * string indexed from a large random pad by some nonce sent with the
87 * message. The probability of a forgery attempt being successful is then
88 * %$(\ell + 1)/2^t$%, where %$t$% is the tag length and %$\ell$% is the
89 * longest message permitted.
92 /*----- Practicalities ----------------------------------------------------*
94 * We work in %$\gf{2^32}$%, represented as a field of polynomials modulo
95 * %$\texttt{104c11db7}_x$% (this is the standard CRC-32 polynomial). Our
98 * The choice of a 32-bit hash is made for pragmatic reasons: we're never
99 * likely to actually want all 32 bits for a real hashtable anyway. The
100 * truncation result is needed to keep us afloat with smaller tables.
102 * We compute hashes using a slightly unrolled version of Horner's rule,
103 * using the recurrence:
105 * %$a_{i+b} = (a_i + m_i) k^b + m_{i+1} k^{b-1} + \cdots + m_{i+b-1} k$%
107 * which involves one full-width multiply and %$b - 1$% one-byte multiplies;
108 * the latter may be efficiently computed using a table lookup. Start with
111 * We precompute tables %$S[\cdot][\cdot][\cdot]$%, where
113 * %$S[u][v][w] = k^{u+1} x^{8v} w$%
114 * for %$0 \le u < b$%, %$0 \le v < 4$%, %$0 \le w < 256)$%.
116 * A one-byte multiply is one lookup; a full-width multiply is four lookups
117 * and three XORs. The processing required is then %$b + 3$% lookups and
118 * %$b + 3$% XORs per batch, or %$(b + 3)/b$% lookups and XORs per byte, at
119 * the expense of %$4 b$% kilobytes of tables. This compares relatively
120 * favorably with CRC32. Indeed, in tests, this implementation with $b = 4$%
121 * is faster than a 32-bit CRC.
124 /*----- Header files ------------------------------------------------------*/
132 /*----- Data structures ---------------------------------------------------*/
134 #define UNIHASH_NBATCH 4
135 #define UNIHASH_POLY 0x04c11db7 /* From CRC32 */
137 typedef struct unihash_info
{
138 uint32 s
[UNIHASH_NBATCH
][4][256]; /* S-tables as described */
141 /*----- Functions provided ------------------------------------------------*/
143 /* --- @unihash_setkey@ --- *
145 * Arguments: @unihash_info *i@ = where to store the precomputed tables
146 * @uint32 k@ = the key to set, randomly chosen
150 * Use: Calculates the tables required for efficient hashing.
153 extern void unihash_setkey(unihash_info */
*i*/
, uint32
/*k*/);
155 /* --- @unihash_hash@ --- *
157 * Arguments: @const unihash_info *i@ = pointer to precomputed table
158 * @uint32 a@ = @UNIHASH_INIT(i)@ or value from previous call
159 * @const void *p@ = pointer to data to hash
160 * @size_t sz@ = size of the data
162 * Returns: Hash of data so far.
164 * Use: Hashes data. Call this as many times as needed.
167 #define UNIHASH_INIT(i) ((i)->s[0][0][1]) /* %$k$% */
169 extern uint32
unihash_hash(const unihash_info */
*i*/
, uint32
/*a*/,
170 const void */
*p*/
, size_t /*sz*/);
172 /* --- @unihash@ --- *
174 * Arguments: @const unihash_info *i@ = precomputed tables
175 * @const void *p@ = pointer to data to hash
176 * @size_t sz@ = size of the data
178 * Returns: The hash value computed.
180 * Use: All-in-one hashing function. No faster than using the
181 * separate calls, but more convenient.
184 #define UNIHASH(i, p, sz) (unihash_hash((i), UNIHASH_INIT((i)), (p), (sz)))
186 extern uint32
unihash(const unihash_info */
*i*/
,
187 const void */
*p*/
, size_t /*sz*/);
189 /*----- That's all, folks -------------------------------------------------*/