chiark / gitweb /
Fix distribution.
[mLib] / unihash.h
CommitLineData
8fe3c82b 1/* -*-c-*-
2 *
573eadb5 3 * $Id: unihash.h,v 1.2 2003/12/14 14:45:30 mdw Exp $
8fe3c82b 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.h,v $
573eadb5 33 * Revision 1.2 2003/12/14 14:45:30 mdw
34 * Test universal hashing and fix bugs.
35 *
8fe3c82b 36 * Revision 1.1 2003/10/12 14:43:24 mdw
37 * Universal hashing.
38 *
39 */
40
41#ifndef MLIB_UNIHASH_H
42#define MLIB_UNIHASH_H
43
44#ifdef __cplusplus
45 extern "C" {
46#endif
47
48
49/*----- Concept -----------------------------------------------------------*
50 *
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}%.
54 * Then we compute
55 *
573eadb5 56 * %$H_k(M) = k^{n+1} + \sum_{0\le i<n} m_i k^{i+1}.$%
8fe3c82b 57 *
58 * Note that %$H_0(M) = 0$% for all messages %$M$%.
59 *
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.
63 *
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
67 *
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$%.
70 *
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$%.
78 *
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$%.
83 *
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
573eadb5 88 * %$(\ell + 1)/2^t$%, where %$t$% is the tag length and %$\ell$% is the
89 * longest message permitted.
8fe3c82b 90 */
91
92/*----- Practicalities ----------------------------------------------------*
93 *
94 * We work in %$\gf{2^32}$%, represented as a field of polynomials modulo
573eadb5 95 * %$\texttt{104c11db7}_x$% (this is the standard CRC-32 polynomial). Our
96 * blocks are bytes.
8fe3c82b 97 *
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.
101 *
102 * We compute hashes using a slightly unrolled version of Horner's rule,
103 * using the recurrence:
104 *
105 * %$a_{i+b} = (a_i + m_i) k^b + m_{i+1} k^{b-1} + \cdots + m_{i+b-1} k$%
106 *
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
109 * %$a_0 = k$%.
110 *
111 * We precompute tables %$S[\cdot][\cdot][\cdot]$%, where
112 *
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)$%.
115 *
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.
122 */
123
124/*----- Header files ------------------------------------------------------*/
125
126#include <stddef.h>
127
128#ifndef MLIB_BITS_H
129# include "bits.h"
130#endif
131
132/*----- Data structures ---------------------------------------------------*/
133
134#define UNIHASH_NBATCH 4
135#define UNIHASH_POLY 0x04c11db7 /* From CRC32 */
136
137typedef struct unihash_info {
138 uint32 s[UNIHASH_NBATCH][4][256]; /* S-tables as described */
139} unihash_info;
140
141/*----- Functions provided ------------------------------------------------*/
142
143/* --- @unihash_setkey@ --- *
144 *
145 * Arguments: @unihash_info *i@ = where to store the precomputed tables
146 * @uint32 k@ = the key to set, randomly chosen
147 *
148 * Returns: ---
149 *
150 * Use: Calculates the tables required for efficient hashing.
151 */
152
153extern void unihash_setkey(unihash_info */*i*/, uint32 /*k*/);
154
155/* --- @unihash_hash@ --- *
156 *
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
161 *
573eadb5 162 * Returns: Hash of data so far.
8fe3c82b 163 *
164 * Use: Hashes data. Call this as many times as needed.
165 */
166
167#define UNIHASH_INIT(i) ((i)->s[0][0][1]) /* %$k$% */
168
169extern uint32 unihash_hash(const unihash_info */*i*/, uint32 /*a*/,
170 const void */*p*/, size_t /*sz*/);
171
172/* --- @unihash@ --- *
173 *
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
177 *
178 * Returns: The hash value computed.
179 *
180 * Use: All-in-one hashing function. No faster than using the
181 * separate calls, but more convenient.
182 */
183
184#define UNIHASH(i, p, sz) (unihash_hash((i), UNIHASH_INIT((i)), (p), (sz)))
185
186extern uint32 unihash(const unihash_info */*i*/,
187 const void */*p*/, size_t /*sz*/);
188
189/*----- That's all, folks -------------------------------------------------*/
190
191#ifdef __cplusplus
192 }
193#endif
194
195#endif