Hopefully fix test_rand breakage

This commit is contained in:
Viktor Dukhovni
2013-10-16 22:04:01 -04:00
parent 1d84562886
commit 1364508cbf

View File

@@ -36,6 +36,8 @@
#include <config.h> #include <config.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <roken.h> #include <roken.h>
#include <getarg.h> #include <getarg.h>
@@ -153,8 +155,9 @@ main(int argc, char **argv)
unsigned bits[8]; unsigned bits[8];
size_t bit, i; size_t bit, i;
double res; double res;
double slen = sqrt((double)len);
memset(bits, 0, sizeof(bit)); memset(bits, 0, sizeof(bits));
memset(bytes, 0, sizeof(bytes)); memset(bytes, 0, sizeof(bytes));
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
@@ -169,25 +172,39 @@ main(int argc, char **argv)
} }
} }
/*
* The count for each bit value has a mean of n*p = len/2,
* and a standard deviation of sqrt(n*p*q) ~ sqrt(len/4).
* Normalizing by dividing by "n*p", we get a mean of 1 and
* a standard deviation of sqrt(q/n*p) = 1/sqrt(len).
*
* We tolerate 5-sigma events (1 in ~2 million builds will
* erroneously report RNG failure when the RNG is unbiased).
*/
for (bit = 0; bit < 8; bit++) { for (bit = 0; bit < 8; bit++) {
res = slen * fabs(1.0 - 2 * (double)bits[bit] / len);
res = 1.0 - (((double)(bits[bit]) / (double)len) * 2); if (res > 5)
if (res > 0.005) errx(1, "head%d vs tail%d: %.1f-sigma (%d of %d)",
errx(1, "head%u vs tail%u > 0.5%% %lf == %d of %d", bit, bit, res, bits[bit], len);
(unsigned)bit, (unsigned)bit, res, len, bits[bit]); printf("head vs tails bit%d: %f-sigma\n", bit, res);
printf("head vs tails bit%u: %lf\n", (unsigned)bit, res);
} }
/*
* The count of each byte value has a mean of n*p = len/256,
* and a standard deviation of sqrt(n*p*q) ~ sqrt(len/256).
* Normalizing by dividing by "n*p", we get a mean of 1 and
* a standard deviation of sqrt(q/n*p) ~ 16/sqrt(len).
*
* We tolerate 5-sigma events (1 in ~2 million builds will
* erroneously report RNG failure when the RNG is unbiased).
*/
for (i = 0; i < 256; i++) { for (i = 0; i < 256; i++) {
res = 1.0 - (((double)(bytes[i]) / (double)len) * 256); res = (slen / 16) * fabs(1.0 - 256 * (double)bytes[i] / len);
if (res > 0.005) if (res > 5)
errx(1, "byte %u > 0.5%%%% %lf", errx(1, "byte %d: %.1f-sigma (%d of %d)",
(unsigned)i, res); i, res, bytes[i], len);
printf("byte %u: %lf\n", (unsigned)i, res); printf("byte %d: %f-sigma\n", i, res);
} }
} }
free(buffer); free(buffer);