3 * $Id: maurer.c,v 1.3 2000/08/16 17:56:59 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 2000/08/16 17:56:59 mdw
34 * (more): Remove spurious function.
36 * Revision 1.2 2000/08/11 21:34:59 mdw
37 * New restartable interface to Maurer testing.
39 * Revision 1.1 2000/06/17 11:29:49 mdw
40 * Maurer's universal statistical test.
44 /*----- Header files ------------------------------------------------------*/
51 #include <mLib/alloc.h>
52 #include <mLib/bits.h>
56 /*----- Data structures ---------------------------------------------------*/
58 typedef struct bitscan
{
64 /*----- Main code ---------------------------------------------------------*/
66 /* --- @maurer_init@ --- *
68 * Arguments: @maurer_ctx *m@ = pointer to a context to initialize
69 * @unsigned l@ = number of bits to take at a time
71 * Returns: Minimum recommended amount of data to test.
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.
78 unsigned long maurer_init(maurer_ctx
*m
, unsigned l
)
82 assert(((void)"(maurer_init): chunks larger than 16 bits not supported",
91 m
->t
= xmalloc(sizeof(unsigned long) << l
);
92 for (i
= 0; i
< (1 << l
); i
++)
94 return (((1000ul << l
) * l
+ 7)/8);
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
104 * Returns: Nonzero if some more bits arrived.
106 * Use: Reads some bits from a buffer.
109 static int bits(maurer_ctx
*m
, const octet
**p
, const octet
*q
, unsigned *x
)
111 while (m
->b
< m
->l
) {
114 m
->a
|= U8(*(*p
)++) << m
->b
;
117 *x
= m
->a
& ((1 << m
->l
) - 1);
124 /* --- @maurer_test@ --- *
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
132 * Use: Scans a buffer of data and updates the testing context.
135 void maurer_test(maurer_ctx
*m
, const void *buf
, size_t sz
)
137 const octet
*p
= buf
, *l
= p
+ sz
;
138 unsigned long q
= 10 << m
->l
;
141 /* --- Initialize the table --- */
144 if (!bits(m
, &p
, l
, &x
))
149 /* --- Update the statistic --- */
151 while (bits(m
, &p
, l
, &x
)) {
152 m
->x
+= log(m
->n
- m
->t
[x
]);
157 /* --- @maurer_done@ --- *
159 * Arguments: @maurer_ctx *m@ = pointer to a Maurer testing context
161 * Returns: The statistic %$Z_u = (X_u - \mu)/\sigma$%, which should be
162 * normally distributed with a mean of 0 and variance of 1.
164 * Use: Returns the result of Maurer's universal statistical test.
165 * Also frees the memory allocated during initialization.
167 * If insufficient data was received, the value @HUGE_VAL@ is
171 double maurer_done(maurer_ctx
*m
)
173 double x
, mu
, c
, sigma
;
174 unsigned long q
= 10 << m
->l
, k
;
176 /* --- Table for means and variances --- */
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 }
197 /* --- Check for bogus requests --- */
204 /* --- Do the maths --- *
206 * This produces %$X_u$%, which should be normally distributed with a mean
207 * and variance we can compute.
211 x
= m
->x
/(k
* log(2));
213 /* --- Now translate this into %$Z_u$% distributed as %$N(0, 1)$% --- */
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
);
220 /* --- Done (phew!) --- */
227 /* --- @maurer@ --- *
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
233 * Returns: The statistic %$Z_u$%.
235 * Use: Simple interface to Maurer's universal statistical test. For
236 * large %$l$% you should use the more complicated restartable
240 double maurer(const octet
*p
, size_t sz
, unsigned l
)
245 maurer_test(&m
, p
, sz
);
246 return (maurer_done(&m
));
249 /*----- That's all, folks -------------------------------------------------*/