stat
This is a quick program I wrote to test the various
algorithms I've written for this page. It is not a real statistical analysis
program, in fact, it only checks the distribution of bits. It is enough
to make sure your program is producing a decent pseudo-random sequence which
does not tend to produce more 1's, for instance, or has a tendency to
produce long strings of 0's, but certainly does not guarantee that your
generator is secure.
Running
This program takes ascii text input, which should consist entirely of
the characters '1' and '0'. It also needs a command line argument specifying
the 'dimension' -- the length of bit sequences to do analysis on. For
instance, a dimension of 1 would give statistics on the number of 1's
versus the number of 0's, a dimension of 2 would give information on all
of the possible two-bit combinations 00, 01, 10, 11. Dimension 3 is
000 001 010 011 100 101 110 111, etc.
Since it uses the console for input, this program is best used using
your operating system's redirection. In DOS and unix, the pipe is handy:
Though redirection is useful as well:
The program will produce output like this:
Or maybe this:
Or, if your algorithm is poor, something like this:
Programmers should have no difficulty converting this program to take
other sorts of input, indeed, they should have no difficulty rewriting
the program entirely.
When doing bit lengths of more than 1, it does not overlap the sequences,
that is:
This program could calculate a standard deviation and check to see
if the distribution is within bounds. This may come in a future version.
This is the code, which you can also just download here. It should have no difficulty compiling on any ansi capable compiler. /* C Implementation of Tom 7's 'stat' program. This code is distributed under the GNU public license; see http://www.gnu.org/copyleft/gpl.html. This takes one parameter on the command line which is the 'dimension' of bits to check for distribution in. Tom 7 http://tom7.org/ */ #include <stdio.h> main (int argc, char ** argv) { int * data, len,n,score,x,c; if (argc<2) { printf("%s length\n",argv[0]); exit(-1); } len = atoi(argv[1]); if (!(data = (int*)malloc((1<<len)*sizeof (int)))) { printf("No memory.\n"); exit(-1); } for (n=0;n<(1<<len);n++) data[n]=0; while (1) { n=len; score=0; while (n--) { if (EOF == (c=getc(stdin))) goto gone; score |= (c-'0')<<n; } data[score]++; } gone: score=0; for (n=0;n<(1<<len);n++) { if (score < data[n]) score=data[n]; } for (n=0;n<(1<<len);n++) { for (x=len;x--;) putchar ('0' + ((n&(1<<x)?1:0) )); printf (": %d :", data[n]); for (x=(20.*(data[n]/(float)score));x--;) putchar('*'); putchar('\n'); } free (data); } Back to Tom's Cryptography Thingie.
|