3 * $Id: maurer.c,v 1.4 2004/04/08 01:36:15 mdw Exp $
5 * Maurer's universal statistical test
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/alloc.h>
38 #include <mLib/bits.h>
42 /*----- Data structures ---------------------------------------------------*/
44 typedef struct bitscan {
50 /*----- Main code ---------------------------------------------------------*/
52 /* --- @maurer_init@ --- *
54 * Arguments: @maurer_ctx *m@ = pointer to a context to initialize
55 * @unsigned l@ = number of bits to take at a time
57 * Returns: Minimum recommended amount of data to test.
59 * Use: Initializes a Maurer testing context. Note that @l@ values
60 * larger than 16 are not supported, and values less than 6 can
61 * give bizarre results.
64 unsigned long maurer_init(maurer_ctx *m, unsigned l)
68 assert(((void)"(maurer_init): chunks larger than 16 bits not supported",
77 m->t = xmalloc(sizeof(unsigned long) << l);
78 for (i = 0; i < (1 << l); i++)
80 return (((1000ul << l) * l + 7)/8);
85 * Arguments: @maurer_ctx *m@ = pointer to testing context
86 * @const octet **p@ = address of a buffer pointer
87 * @const octet *q@ = limit of the buffer pointer
88 * @unsigned *x@ = address to store next chunk
90 * Returns: Nonzero if some more bits arrived.
92 * Use: Reads some bits from a buffer.
95 static int bits(maurer_ctx *m, const octet **p, const octet *q, unsigned *x)
100 m->a |= U8(*(*p)++) << m->b;
103 *x = m->a & ((1 << m->l) - 1);
110 /* --- @maurer_test@ --- *
112 * Arguments: @maurer_ctx *m@ = pointer to a Maurer testing context
113 * @const void *buf@ = pointer to a buffer of data
114 * @size_t sz@ = size of the buffer
118 * Use: Scans a buffer of data and updates the testing context.
121 void maurer_test(maurer_ctx *m, const void *buf, size_t sz)
123 const octet *p = buf, *l = p + sz;
124 unsigned long q = 10 << m->l;
127 /* --- Initialize the table --- */
130 if (!bits(m, &p, l, &x))
135 /* --- Update the statistic --- */
137 while (bits(m, &p, l, &x)) {
138 m->x += log(m->n - m->t[x]);
143 /* --- @maurer_done@ --- *
145 * Arguments: @maurer_ctx *m@ = pointer to a Maurer testing context
147 * Returns: The statistic %$Z_u = (X_u - \mu)/\sigma$%, which should be
148 * normally distributed with a mean of 0 and variance of 1.
150 * Use: Returns the result of Maurer's universal statistical test.
151 * Also frees the memory allocated during initialization.
153 * If insufficient data was received, the value @HUGE_VAL@ is
157 double maurer_done(maurer_ctx *m)
159 double x, mu, c, sigma;
160 unsigned long q = 10 << m->l, k;
162 /* --- Table for means and variances --- */
164 struct { double mu, var_1; } vtab[] = {
165 { 0.7326495, 0.690 },
166 { 1.5374383, 1.338 },
167 { 2.4016068, 1.901 },
168 { 3.3112247, 2.358 },
169 { 4.2534266, 2.705 },
170 { 5.2177052, 2.945 },
171 { 6.1962507, 3.125 },
172 { 7.1836656, 3.238 },
173 { 8.1764248, 3.311 },
174 { 9.1723243, 3.356 },
175 { 10.170032 , 3.384 },
176 { 11.168765 , 3.401 },
177 { 12.168070 , 3.410 },
178 { 13.167693 , 3.416 },
179 { 14.167488 , 3.419 },
180 { 15.167379 , 3.421 }
183 /* --- Check for bogus requests --- */
190 /* --- Do the maths --- *
192 * This produces %$X_u$%, which should be normally distributed with a mean
193 * and variance we can compute.
197 x = m->x/(k * log(2));
199 /* --- Now translate this into %$Z_u$% distributed as %$N(0, 1)$% --- */
201 mu = vtab[m->l - 1].mu;
202 c = 0.7 - 0.8/m->l + (1.6 + 12.8/m->l) * pow(k, -4.0/m->l);
203 sigma = sqrt(c * c * vtab[m->l - 1].var_1 / k);
206 /* --- Done (phew!) --- */
213 /* --- @maurer@ --- *
215 * Arguments: @const octet *p@ = pointer to a buffer of data
216 * @size_t sz@ = size of the buffer
217 * @unsigned l@ = number of bits to take at a time
219 * Returns: The statistic %$Z_u$%.
221 * Use: Simple interface to Maurer's universal statistical test. For
222 * large %$l$% you should use the more complicated restartable
226 double maurer(const octet *p, size_t sz, unsigned l)
231 maurer_test(&m, p, sz);
232 return (maurer_done(&m));
235 /*----- That's all, folks -------------------------------------------------*/