Force subkeys to be sorted in structured keys.
[u/mdw/catacomb] / maurer.c
CommitLineData
39be2708 1/* -*-c-*-
2 *
d2ae13e0 3 * $Id: maurer.c,v 1.3 2000/08/16 17:56:59 mdw Exp $
39be2708 4 *
5 * Maurer's universal statistical test
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb 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 * Catacomb 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 Catacomb; 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: maurer.c,v $
d2ae13e0 33 * Revision 1.3 2000/08/16 17:56:59 mdw
34 * (more): Remove spurious function.
35 *
e3dc2d22 36 * Revision 1.2 2000/08/11 21:34:59 mdw
37 * New restartable interface to Maurer testing.
38 *
39be2708 39 * Revision 1.1 2000/06/17 11:29:49 mdw
40 * Maurer's universal statistical test.
41 *
42 */
43
44/*----- Header files ------------------------------------------------------*/
45
46#include <assert.h>
47#include <math.h>
48#include <stdio.h>
49#include <stdlib.h>
50
51#include <mLib/alloc.h>
52#include <mLib/bits.h>
53
54#include "maurer.h"
55
56/*----- Data structures ---------------------------------------------------*/
57
58typedef struct bitscan {
59 const octet *p;
60 unsigned b;
61 uint32 a;
62} bitscan;
63
64/*----- Main code ---------------------------------------------------------*/
65
e3dc2d22 66/* --- @maurer_init@ --- *
39be2708 67 *
e3dc2d22 68 * Arguments: @maurer_ctx *m@ = pointer to a context to initialize
39be2708 69 * @unsigned l@ = number of bits to take at a time
70 *
e3dc2d22 71 * Returns: Minimum recommended amount of data to test.
72 *
73 * Use: Initializes a Maurer testing context. Note that @l@ values
74 * larger than 16 are not supported, and values less than 6 can
75 * give bizarre results.
76 */
77
78unsigned long maurer_init(maurer_ctx *m, unsigned l)
79{
80 size_t i;
81
82 assert(((void)"(maurer_init): chunks larger than 16 bits not supported",
83 0 < l && l <= 16));
84
85 m->l = l;
86 m->a = 0;
87 m->b = 0;
88 m->n = 0;
89 m->x = 0;
90
91 m->t = xmalloc(sizeof(unsigned long) << l);
92 for (i = 0; i < (1 << l); i++)
93 m->t[i] = 0;
94 return (((1000ul << l) * l + 7)/8);
95}
96
97/* --- @bits@ --- *
98 *
99 * Arguments: @maurer_ctx *m@ = pointer to testing context
100 * @const octet **p@ = address of a buffer pointer
101 * @const octet *q@ = limit of the buffer pointer
102 * @unsigned *x@ = address to store next chunk
103 *
104 * Returns: Nonzero if some more bits arrived.
105 *
106 * Use: Reads some bits from a buffer.
107 */
108
109static int bits(maurer_ctx *m, const octet **p, const octet *q, unsigned *x)
110{
111 while (m->b < m->l) {
112 if (*p >= q)
113 return (0);
114 m->a |= U8(*(*p)++) << m->b;
115 m->b += 8;
116 }
117 *x = m->a & ((1 << m->l) - 1);
118 m->a >>= m->l;
119 m->b -= m->l;
120 m->n++;
121 return (1);
122}
123
124/* --- @maurer_test@ --- *
125 *
126 * Arguments: @maurer_ctx *m@ = pointer to a Maurer testing context
127 * @const void *buf@ = pointer to a buffer of data
128 * @size_t sz@ = size of the buffer
129 *
130 * Returns: ---
131 *
132 * Use: Scans a buffer of data and updates the testing context.
133 */
134
135void maurer_test(maurer_ctx *m, const void *buf, size_t sz)
136{
137 const octet *p = buf, *l = p + sz;
138 unsigned long q = 10 << m->l;
139 unsigned x;
140
141 /* --- Initialize the table --- */
142
143 while (m->n < q) {
144 if (!bits(m, &p, l, &x))
145 return;
146 m->t[x] = m->n;
147 }
148
149 /* --- Update the statistic --- */
150
151 while (bits(m, &p, l, &x)) {
152 m->x += log(m->n - m->t[x]);
153 m->t[x] = m->n;
154 }
155}
156
157/* --- @maurer_done@ --- *
158 *
159 * Arguments: @maurer_ctx *m@ = pointer to a Maurer testing context
160 *
39be2708 161 * Returns: The statistic %$Z_u = (X_u - \mu)/\sigma$%, which should be
e3dc2d22 162 * normally distributed with a mean of 0 and variance of 1.
39be2708 163 *
e3dc2d22 164 * Use: Returns the result of Maurer's universal statistical test.
165 * Also frees the memory allocated during initialization.
39be2708 166 *
e3dc2d22 167 * If insufficient data was received, the value @HUGE_VAL@ is
168 * returned.
39be2708 169 */
170
e3dc2d22 171double maurer_done(maurer_ctx *m)
39be2708 172{
e3dc2d22 173 double x, mu, c, sigma;
174 unsigned long q = 10 << m->l, k;
39be2708 175
176 /* --- Table for means and variances --- */
177
178 struct { double mu, var_1; } vtab[] = {
179 { 0.7326495, 0.690 },
180 { 1.5374383, 1.338 },
181 { 2.4016068, 1.901 },
182 { 3.3112247, 2.358 },
183 { 4.2534266, 2.705 },
184 { 5.2177052, 2.945 },
185 { 6.1962507, 3.125 },
186 { 7.1836656, 3.238 },
187 { 8.1764248, 3.311 },
188 { 9.1723243, 3.356 },
189 { 10.170032 , 3.384 },
190 { 11.168765 , 3.401 },
191 { 12.168070 , 3.410 },
192 { 13.167693 , 3.416 },
193 { 14.167488 , 3.419 },
194 { 15.167379 , 3.421 }
195 };
196
e3dc2d22 197 /* --- Check for bogus requests --- */
39be2708 198
e3dc2d22 199 if (m->n < q) {
200 x = HUGE_VAL;
201 goto done;
39be2708 202 }
203
e3dc2d22 204 /* --- Do the maths --- *
39be2708 205 *
206 * This produces %$X_u$%, which should be normally distributed with a mean
207 * and variance we can compute.
208 */
209
e3dc2d22 210 k = m->n - q;
211 x = m->x/(k * log(2));
39be2708 212
213 /* --- Now translate this into %$Z_u$% distributed as %$N(0, 1)$% --- */
214
e3dc2d22 215 mu = vtab[m->l - 1].mu;
216 c = 0.7 - 0.8/m->l + (1.6 + 12.8/m->l) * pow(k, -4.0/m->l);
217 sigma = sqrt(c * c * vtab[m->l - 1].var_1 / k);
39be2708 218 x = (x - mu)/sigma;
219
220 /* --- Done (phew!) --- */
221
e3dc2d22 222done:
223 xfree(m->t);
39be2708 224 return (x);
225}
226
e3dc2d22 227/* --- @maurer@ --- *
228 *
229 * Arguments: @const octet *p@ = pointer to a buffer of data
230 * @size_t sz@ = size of the buffer
231 * @unsigned l@ = number of bits to take at a time
232 *
233 * Returns: The statistic %$Z_u$%.
234 *
235 * Use: Simple interface to Maurer's universal statistical test. For
236 * large %$l$% you should use the more complicated restartable
237 * interface.
238 */
239
240double maurer(const octet *p, size_t sz, unsigned l)
241{
242 maurer_ctx m;
243
244 maurer_init(&m, l);
245 maurer_test(&m, p, sz);
246 return (maurer_done(&m));
247}
248
39be2708 249/*----- That's all, folks -------------------------------------------------*/