
Boost : 
From: Mark Borgerding (mborgerding_at_[hidden])
Date: 20000224 11:02:32
I did some more work on the entropy pool/ clock() timing rng.
I had about 3MB of data from the clock() method. I put it through a
entropypool churning process described in my earlier email. I used a
32 bit CRC to churn the pool.
For those that are just tuning in, the idea was to get a value from
clock(), count in a tight loop until clock() changes, and use the lsb
of the count as a random bit. Eight bits of entropy are gathered this
way and then mixed into an entropy pool via a CRC. A byte of the
entropy pool is then used as a true random byte.
The following is a program that writes random bytes to stdout.
I analyzed the results with diehard and they looked really good.
Granted, I could not run all the tests, because some of them require >
10MB of data.
The abbreviated diehard results are after the program listing. Note to
interpretation: pvalues for random data should be uniformly
distributed on [0,1)
Of course, now that I've done all this, I'm having doubts about its
utility. I'm not sure this method would be strong enough in cases that
need cryptographic random numbers (i.e. key generation). And it is
probably too slow to be used in applications where a PRNG would
suffice. What does that leave?
.... but I put some serious time into this, so by golly  I'm gonna
tell you about it.
/*
The CRC32 code is based upon work by Charles Michael Heard. He did
not affix any copyright to his original code. He should be contacted
before this is used for anything more than proofofconcept.
*/
#include <stdlib.h>
#include <time.h>
#include <unistd.h> // this is needed for write(), not required by the
algo
unsigned char true_rand();
int main(int argc, char ** argv)
{
size_t num = ~0;
if (argc > 1)
num = atoi(argv[1]);
for (size_t i=0;i<num;++i){
unsigned char c = true_rand();
write(1,&c,1);
}
return 0;
}
const unsigned long polynomial = 0x04c11db7;
unsigned long crc_table[256];
void gen_crc_table()
{
int i, j;
unsigned long crc_accum;
for ( i = 0; i < 256; i++ ){
crc_accum = ( i << 24 );
for ( j = 0; j < 8; j++ ){
if ( crc_accum & 0x80000000L )
crc_accum = ( crc_accum << 1 ) ^
polynomial;
else
crc_accum = ( crc_accum << 1 );
}
crc_table[i] = crc_accum;
}
}
inline
unsigned long update_crc(unsigned long crc_accum,const void *
pData,unsigned long data_blk_size)
{
const char * data_blk_ptr = static_cast<const char *>(pData);
int i;
static bool init=false;
if (!init){
gen_crc_table();
init = true;
}
while (data_blk_size){
i = (( crc_accum >> 24) ^ *data_blk_ptr++ ) & 0xff;
crc_accum = ( crc_accum << 8 ) ^ crc_table[i];
}
return crc_accum;
}
inline
unsigned char churn(const void * pData=0,size_t bytes=0) {
static unsigned long ent=0; //entropy pool
ent = update_crc(static_cast<unsigned long>(1),&ent,sizeof(ent
));
if (bytes && pData)
ent = update_crc(ent,pData,bytes);
return (~ent)&0xff;
}
unsigned char true_rand()
{
unsigned char result = 0;
for (int i=0;i<8;++i)
{
unsigned int counter=0;
clock_t start = clock();
do
{
++counter;// keep counting until the clock
changes
}while (start == clock());
result <<= 1;
// left shift 1 bit
result = (counter&1);
// put the LSB from the counter into the LSB of the
result
}
return churn(&result,1);
}
Here are the diehard test results for a 3MB file generated by the above
program on a linux box (Pentium 233, kernel 2.2.14,g++ 2.95.2,glibc
2.1.1).
If anyone is interested in getting the file or installing diehard (the
precompiled binaries didn't work for me), contact me (mborgerding @
acm.org) and I will help out.
Birthday spacings:
The 9 pvalues were
.650702 .352101 .372355 .322484 .871127
.593715 .926910 .815100 .929238
A KSTEST for the 9 pvalues yields .817223
BINARY RANK TEST for 6x8 matrices
TEST SUMMARY, 25 tests on 100,000 random 6x8 matrices
These should be 25 uniform [0,1] random variables:
.034846 .668624 .867341 .322676 .063906
.573906 .616073 .756396 .259063 .225803
.004042 .917611 .375742 .014019 .593909
.101089 .832640 .527469 .372356 .176146
.205358 .746577 .293803 .314605 .721505
brank test summary for rand.dat
The KS test for those 25 supposed UNI's yields
KS pvalue= .723423
THE BITSTREAM TEST
BITSTREAM test results forrand.dat
tst no 1: 142557 missing words, 1.51 sigmas from mean, pvalue=
.93489
tst no 2: 141747 missing words, .38 sigmas from mean, pvalue=
.35224
tst no 3: 142267 missing words, .84 sigmas from mean, pvalue=
.79833
tst no 4: 142453 missing words, 1.27 sigmas from mean, pvalue=
.89800
tst no 5: 142019 missing words, .26 sigmas from mean, pvalue=
.60112
tst no 6: 141123 missing words, 1.84 sigmas from mean, pvalue=
.03309
tst no 7: 142181 missing words, .63 sigmas from mean, pvalue=
.73720
tst no 8: 141680 missing words, .54 sigmas from mean, pvalue=
.29604
tst no 9: 142044 missing words, .31 sigmas from mean, pvalue=
.62349
tst no 10: 142355 missing words, 1.04 sigmas from mean, pvalue=
.85113
tst no 11: 141741 missing words, .39 sigmas from mean, pvalue=
.34705
tst no 12: 141922 missing words, .03 sigmas from mean, pvalue=
.51181
tst no 13: 142160 missing words, .59 sigmas from mean, pvalue=
.72096
tst no 14: 141871 missing words, .09 sigmas from mean, pvalue=
.46432
tst no 15: 141482 missing words, 1.00 sigmas from mean, pvalue=
.15904
Results for COUNTTHE1's in successive bytes:
chisquare equiv normal pvalue
byte stream for rand.dat 2566.34 .938 .825937
PARKING LOT TEST
DPARK: result of ten tests on file rand.dat
Of 12,000 tries, the average no. of successes
should be 3523 with sigma=21.9
Successes: 3518 zscore: .228 pvalue: .409702
Successes: 3522 zscore: .046 pvalue: .481790
Successes: 3508 zscore: .685 pvalue: .246694
Successes: 3520 zscore: .137 pvalue: .445521
Successes: 3510 zscore: .594 pvalue: .276387
Successes: 3525 zscore: .091 pvalue: .536382
Successes: 3532 zscore: .411 pvalue: .659449
Successes: 3515 zscore: .365 pvalue: .357445
Successes: 3529 zscore: .274 pvalue: .607947
Successes: 3539 zscore: .731 pvalue: .767486
square size avg. no. parked sample sigma
100. 3521.800 9.231
KSTEST for the above 10: p= .697954
This is the MINIMUM DISTANCE test
for random integers in the file rand.dat
Sample no. d^2 avg equiv uni
5 .5021 .2219 .396247
10 .8606 .4462 .578929
15 .0062 .4712 .006250
20 .0754 .5142 .072955
25 2.5621 .6595 .923843
30 .5615 .6905 .431277
35 1.9744 .7447 .862526
40 .9322 .7300 .608140
45 .0691 .7822 .067082
50 .3137 .7939 .270420
55 4.0357 .8903 .982682
60 .1584 .9124 .147175
3DSPHERES test for file rand.dat pvalue= .598040
OVERLAPPING SUMS
Test no. 1 pvalue .226889
Test no. 2 pvalue .148903
Test no. 3 pvalue .894253
Test no. 4 pvalue .922038
Test no. 5 pvalue .102995
Test no. 6 pvalue .792611
Test no. 7 pvalue .143711
Test no. 8 pvalue .819030
Test no. 9 pvalue .655024
Test no. 10 pvalue .588361
Results of the OSUM test for rand.dat
KSTEST on the above 10 pvalues: .307924
The RUNS test for file rand.dat
Up and down runs in a sample of 10000
_________________________________________________
Run test for rand.dat :
runs up; ks test for 10 p's: .575118
runs down; ks test for 10 p's: .444705
Run test for rand.dat :
runs up; ks test for 10 p's: .018677
runs down; ks test for 10 p's: .285316
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk