| 1 |
/* This code was taken from http://www.fourmilab.ch/random/ where it states that: |
| 2 |
|
| 3 |
This software is in the public domain. Permission to use, copy, modify, and distribute |
| 4 |
this software and its documentation for any purpose and without fee is hereby granted, |
| 5 |
without any conditions or restrictions. This software is provided “as is” without |
| 6 |
express or implied warranty. */ |
| 7 |
|
| 8 |
/* |
| 9 |
|
| 10 |
Apply various randomness tests to a stream of bytes |
| 11 |
|
| 12 |
by John Walker -- September 1996 |
| 13 |
http://www.fourmilab.ch/ |
| 14 |
|
| 15 |
*/ |
| 16 |
|
| 17 |
#include <math.h> |
| 18 |
|
| 19 |
#define FALSE 0 |
| 20 |
#define TRUE 1 |
| 21 |
|
| 22 |
#define log2of10 3.32192809488736234787 |
| 23 |
|
| 24 |
static int binary = FALSE; /* Treat input as a bitstream */ |
| 25 |
|
| 26 |
static long ccount[256], /* Bins to count occurrences of values */ |
| 27 |
totalc = 0; /* Total bytes counted */ |
| 28 |
static double prob[256]; /* Probabilities per bin for entropy */ |
| 29 |
|
| 30 |
/* RT_LOG2 -- Calculate log to the base 2 */ |
| 31 |
|
| 32 |
static double rt_log2(double x) |
| 33 |
{ |
| 34 |
return log2of10 * log10(x); |
| 35 |
} |
| 36 |
|
| 37 |
#define MONTEN 6 /* Bytes used as Monte Carlo |
| 38 |
co-ordinates. This should be no more |
| 39 |
bits than the mantissa of your |
| 40 |
"double" floating point type. */ |
| 41 |
|
| 42 |
static int mp, sccfirst; |
| 43 |
static unsigned int monte[MONTEN]; |
| 44 |
static long inmont, mcount; |
| 45 |
static double cexp, incirc, montex, montey, montepi, |
| 46 |
scc, sccun, sccu0, scclast, scct1, scct2, scct3, |
| 47 |
ent, chisq, datasum; |
| 48 |
|
| 49 |
/* RT_INIT -- Initialise random test counters. */ |
| 50 |
|
| 51 |
void rt_init(int binmode) |
| 52 |
{ |
| 53 |
int i; |
| 54 |
|
| 55 |
binary = binmode; /* Set binary / byte mode */ |
| 56 |
|
| 57 |
/* Initialise for calculations */ |
| 58 |
|
| 59 |
ent = 0.0; /* Clear entropy accumulator */ |
| 60 |
chisq = 0.0; /* Clear Chi-Square */ |
| 61 |
datasum = 0.0; /* Clear sum of bytes for arithmetic mean */ |
| 62 |
|
| 63 |
mp = 0; /* Reset Monte Carlo accumulator pointer */ |
| 64 |
mcount = 0; /* Clear Monte Carlo tries */ |
| 65 |
inmont = 0; /* Clear Monte Carlo inside count */ |
| 66 |
incirc = 65535.0 * 65535.0;/* In-circle distance for Monte Carlo */ |
| 67 |
|
| 68 |
sccfirst = TRUE; /* Mark first time for serial correlation */ |
| 69 |
scct1 = scct2 = scct3 = 0.0; /* Clear serial correlation terms */ |
| 70 |
|
| 71 |
incirc = pow(pow(256.0, (double) (MONTEN / 2)) - 1, 2.0); |
| 72 |
|
| 73 |
for (i = 0; i < 256; i++) { |
| 74 |
ccount[i] = 0; |
| 75 |
} |
| 76 |
totalc = 0; |
| 77 |
} |
| 78 |
|
| 79 |
/* RT_ADD -- Add one or more bytes to accumulation. */ |
| 80 |
|
| 81 |
void rt_add(void *buf, int bufl) |
| 82 |
{ |
| 83 |
unsigned char *bp = (unsigned char *)buf; |
| 84 |
int oc, c, bean; |
| 85 |
|
| 86 |
while (bean = 0, (bufl-- > 0)) { |
| 87 |
oc = *bp++; |
| 88 |
|
| 89 |
do { |
| 90 |
if (binary) { |
| 91 |
c = !!(oc & 0x80); |
| 92 |
} else { |
| 93 |
c = oc; |
| 94 |
} |
| 95 |
ccount[c]++; /* Update counter for this bin */ |
| 96 |
totalc++; |
| 97 |
|
| 98 |
/* Update inside / outside circle counts for Monte Carlo |
| 99 |
computation of PI */ |
| 100 |
|
| 101 |
if (bean == 0) { |
| 102 |
monte[mp++] = oc; /* Save character for Monte Carlo */ |
| 103 |
if (mp >= MONTEN) { /* Calculate every MONTEN character */ |
| 104 |
int mj; |
| 105 |
|
| 106 |
mp = 0; |
| 107 |
mcount++; |
| 108 |
montex = montey = 0; |
| 109 |
for (mj = 0; mj < MONTEN / 2; mj++) { |
| 110 |
montex = (montex * 256.0) + monte[mj]; |
| 111 |
montey = (montey * 256.0) + monte[(MONTEN / 2) + mj]; |
| 112 |
} |
| 113 |
if ((montex * montex + montey * montey) <= incirc) { |
| 114 |
inmont++; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/* Update calculation of serial correlation coefficient */ |
| 120 |
|
| 121 |
sccun = c; |
| 122 |
if (sccfirst) { |
| 123 |
sccfirst = FALSE; |
| 124 |
scclast = 0; |
| 125 |
sccu0 = sccun; |
| 126 |
} else { |
| 127 |
scct1 = scct1 + scclast * sccun; |
| 128 |
} |
| 129 |
scct2 = scct2 + sccun; |
| 130 |
scct3 = scct3 + (sccun * sccun); |
| 131 |
scclast = sccun; |
| 132 |
oc <<= 1; |
| 133 |
} while (binary && (++bean < 8)); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/* RT_END -- Complete calculation and return results. */ |
| 138 |
|
| 139 |
void rt_end(double *r_ent, double *r_chisq, double *r_mean, |
| 140 |
double *r_montepicalc, double *r_scc) |
| 141 |
{ |
| 142 |
int i; |
| 143 |
|
| 144 |
/* Complete calculation of serial correlation coefficient */ |
| 145 |
|
| 146 |
scct1 = scct1 + scclast * sccu0; |
| 147 |
scct2 = scct2 * scct2; |
| 148 |
scc = totalc * scct3 - scct2; |
| 149 |
if (scc == 0.0) { |
| 150 |
scc = -100000; |
| 151 |
} else { |
| 152 |
scc = (totalc * scct1 - scct2) / scc; |
| 153 |
} |
| 154 |
|
| 155 |
/* Scan bins and calculate probability for each bin and |
| 156 |
Chi-Square distribution. The probability will be reused |
| 157 |
in the entropy calculation below. While we're at it, |
| 158 |
we sum of all the data which will be used to compute the |
| 159 |
mean. */ |
| 160 |
|
| 161 |
cexp = totalc / (binary ? 2.0 : 256.0); /* Expected count per bin */ |
| 162 |
for (i = 0; i < (binary ? 2 : 256); i++) { |
| 163 |
double a = ccount[i] - cexp;; |
| 164 |
|
| 165 |
prob[i] = ((double) ccount[i]) / totalc; |
| 166 |
chisq += (a * a) / cexp; |
| 167 |
datasum += ((double) i) * ccount[i]; |
| 168 |
} |
| 169 |
|
| 170 |
/* Calculate entropy */ |
| 171 |
|
| 172 |
for (i = 0; i < (binary ? 2 : 256); i++) { |
| 173 |
if (prob[i] > 0.0) { |
| 174 |
ent += prob[i] * rt_log2(1 / prob[i]); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/* Calculate Monte Carlo value for PI from percentage of hits |
| 179 |
within the circle */ |
| 180 |
|
| 181 |
montepi = 4.0 * (((double) inmont) / mcount); |
| 182 |
|
| 183 |
/* Return results through arguments */ |
| 184 |
|
| 185 |
*r_ent = ent; |
| 186 |
*r_chisq = chisq; |
| 187 |
*r_mean = datasum / totalc; |
| 188 |
*r_montepicalc = montepi; |
| 189 |
*r_scc = scc; |
| 190 |
} |