Restructuring a bit

This commit is contained in:
Bill Cox
2014-12-10 13:03:19 -05:00
parent 2d037b7635
commit 1006254e2b
15 changed files with 140 additions and 190 deletions

View File

Binary file not shown.

View File

@@ -1,4 +1,4 @@
all: infnoise infnoise-v1 healthcheck findlongest entcheck hex2bin
all: infnoise infnoise-v1
infnoise: infnoise.c infnoise.h healthcheck.c writeentropy.c Keccak/KeccakF-1600-reference.c Keccak/brg_endian.h
gcc -Wall -std=c99 -O3 -I Keccak -o infnoise infnoise.c healthcheck.c writeentropy.c Keccak/KeccakF-1600-reference.c -lftdi -lm -lrt
@@ -6,17 +6,5 @@ infnoise: infnoise.c infnoise.h healthcheck.c writeentropy.c Keccak/KeccakF-1600
infnoise-v1: infnoise.c infnoise.h healthcheck.c writeentropy.c Keccak/KeccakF-1600-reference.c Keccak/brg_endian.h
gcc -Wall -std=c99 -O3 -DVERSION1 -I Keccak -o infnoise-v1 infnoise.c healthcheck.c writeentropy.c Keccak/KeccakF-1600-reference.c -lftdi -lm -lrt
healthcheck: healthcheck.c
gcc -Wall -std=c99 -O3 -D TEST_HEALTHCHECK -o healthcheck healthcheck.c -lm -lrt
entcheck: entcheck.c
gcc -Wall -std=c99 -O3 -o entcheck entcheck.c -lm -lrt
findlongest: findlongest.c
gcc -Wall -std=c99 -O3 -o findlongest findlongest.c
hex2bin: hex2bin.c
gcc -Wall -std=c99 -O3 -o hex2bin hex2bin.c
clean:
rm -f healthcheck infnoise infnoise-v1 findlongest hex2bin
rm -f infnoise infnoise-v1

View File

Binary file not shown.

View File

Binary file not shown.

View File

@@ -1,107 +0,0 @@
#=============================================================================#
# dieharder version 3.31.1 Copyright 2003 Robert G. Brown #
#=============================================================================#
rng_name |rands/second| Seed |
stdin_input_raw| 1.29e+06 |2741031046|
#=============================================================================#
test_name |ntup| tsamples |psamples| p-value |Assessment
#=============================================================================#
diehard_birthdays| 0| 100| 100|0.76830635| PASSED
diehard_operm5| 0| 1000000| 100|0.84259177| PASSED
diehard_rank_32x32| 0| 40000| 100|0.87856030| PASSED
diehard_rank_6x8| 0| 100000| 100|0.17758822| PASSED
diehard_bitstream| 0| 2097152| 100|0.27682190| PASSED
diehard_opso| 0| 2097152| 100|0.75657485| PASSED
diehard_oqso| 0| 2097152| 100|0.61013263| PASSED
diehard_dna| 0| 2097152| 100|0.82299285| PASSED
diehard_count_1s_str| 0| 256000| 100|0.58812627| PASSED
diehard_count_1s_byt| 0| 256000| 100|0.85029476| PASSED
diehard_parking_lot| 0| 12000| 100|0.46927986| PASSED
diehard_2dsphere| 2| 8000| 100|0.35425371| PASSED
diehard_3dsphere| 3| 4000| 100|0.54705086| PASSED
diehard_squeeze| 0| 100000| 100|0.79386624| PASSED
diehard_sums| 0| 100| 100|0.20751981| PASSED
diehard_runs| 0| 100000| 100|0.52401798| PASSED
diehard_runs| 0| 100000| 100|0.16458722| PASSED
diehard_craps| 0| 200000| 100|0.88129827| PASSED
diehard_craps| 0| 200000| 100|0.17363726| PASSED
marsaglia_tsang_gcd| 0| 10000000| 100|0.80622749| PASSED
marsaglia_tsang_gcd| 0| 10000000| 100|0.31741708| PASSED
sts_monobit| 1| 100000| 100|0.34128611| PASSED
sts_runs| 2| 100000| 100|0.84186019| PASSED
sts_serial| 1| 100000| 100|0.66820324| PASSED
sts_serial| 2| 100000| 100|0.66744999| PASSED
sts_serial| 3| 100000| 100|0.70300557| PASSED
sts_serial| 3| 100000| 100|0.98651823| PASSED
sts_serial| 4| 100000| 100|0.03816395| PASSED
sts_serial| 4| 100000| 100|0.07400626| PASSED
sts_serial| 5| 100000| 100|0.64038207| PASSED
sts_serial| 5| 100000| 100|0.86875581| PASSED
sts_serial| 6| 100000| 100|0.29142055| PASSED
sts_serial| 6| 100000| 100|0.27275411| PASSED
sts_serial| 7| 100000| 100|0.80877707| PASSED
sts_serial| 7| 100000| 100|0.11611644| PASSED
sts_serial| 8| 100000| 100|0.51047937| PASSED
sts_serial| 8| 100000| 100|0.42766871| PASSED
sts_serial| 9| 100000| 100|0.12591562| PASSED
sts_serial| 9| 100000| 100|0.28185368| PASSED
sts_serial| 10| 100000| 100|0.62684314| PASSED
sts_serial| 10| 100000| 100|0.99357055| PASSED
sts_serial| 11| 100000| 100|0.27264910| PASSED
sts_serial| 11| 100000| 100|0.08907883| PASSED
sts_serial| 12| 100000| 100|0.98158775| PASSED
sts_serial| 12| 100000| 100|0.82797045| PASSED
sts_serial| 13| 100000| 100|0.94423572| PASSED
sts_serial| 13| 100000| 100|0.77459300| PASSED
sts_serial| 14| 100000| 100|0.28142429| PASSED
sts_serial| 14| 100000| 100|0.22769792| PASSED
sts_serial| 15| 100000| 100|0.66232537| PASSED
sts_serial| 15| 100000| 100|0.64061862| PASSED
sts_serial| 16| 100000| 100|0.66075117| PASSED
sts_serial| 16| 100000| 100|0.49874691| PASSED
rgb_bitdist| 1| 100000| 100|0.81450638| PASSED
rgb_bitdist| 2| 100000| 100|0.96078811| PASSED
rgb_bitdist| 3| 100000| 100|0.47230648| PASSED
rgb_bitdist| 4| 100000| 100|0.34697395| PASSED
rgb_bitdist| 5| 100000| 100|0.99674273| WEAK
rgb_bitdist| 6| 100000| 100|0.84286874| PASSED
rgb_bitdist| 7| 100000| 100|0.62795402| PASSED
rgb_bitdist| 8| 100000| 100|0.90212316| PASSED
rgb_bitdist| 9| 100000| 100|0.91127505| PASSED
rgb_bitdist| 10| 100000| 100|0.20152947| PASSED
rgb_bitdist| 11| 100000| 100|0.99620421| WEAK
rgb_bitdist| 12| 100000| 100|0.73148016| PASSED
rgb_minimum_distance| 2| 10000| 1000|0.01565523| PASSED
rgb_minimum_distance| 3| 10000| 1000|0.26698601| PASSED
rgb_minimum_distance| 4| 10000| 1000|0.62153807| PASSED
rgb_minimum_distance| 5| 10000| 1000|0.72159195| PASSED
rgb_permutations| 2| 100000| 100|0.98324442| PASSED
rgb_permutations| 3| 100000| 100|0.51682926| PASSED
rgb_permutations| 4| 100000| 100|0.54064304| PASSED
rgb_permutations| 5| 100000| 100|0.71867228| PASSED
rgb_lagged_sum| 0| 1000000| 100|0.85838888| PASSED
rgb_lagged_sum| 1| 1000000| 100|0.79501958| PASSED
rgb_lagged_sum| 2| 1000000| 100|0.34616098| PASSED
rgb_lagged_sum| 3| 1000000| 100|0.09757801| PASSED
rgb_lagged_sum| 4| 1000000| 100|0.69399757| PASSED
rgb_lagged_sum| 5| 1000000| 100|0.13322803| PASSED
rgb_lagged_sum| 6| 1000000| 100|0.98994494| PASSED
rgb_lagged_sum| 7| 1000000| 100|0.50697674| PASSED
rgb_lagged_sum| 8| 1000000| 100|0.97855245| PASSED
rgb_lagged_sum| 9| 1000000| 100|0.53261621| PASSED
rgb_lagged_sum| 10| 1000000| 100|0.89414918| PASSED
rgb_lagged_sum| 11| 1000000| 100|0.50076536| PASSED
rgb_lagged_sum| 12| 1000000| 100|0.92937628| PASSED
rgb_lagged_sum| 13| 1000000| 100|0.85361490| PASSED
I killed lagged_sum since my battery was dying. Continuing with the rest...
dab_monobit2| 12| 65000000| 1|0.56975971| PASSED
rgb_kstest_test| 0| 10000| 1000|0.02726165| PASSED
dab_bytedistrib| 0| 51200000| 1|0.76718402| PASSED
dab_dct| 256| 50000| 1|0.61173005| PASSED
dab_filltree| 32| 15000000| 1|0.81071169| PASSED
dab_filltree| 32| 15000000| 1|0.01415494| PASSED
dab_filltree2| 0| 5000000| 1|0.82385351| PASSED
dab_filltree2| 1| 5000000| 1|0.21870952| PASSED
dab_monobit2| 12| 65000000| 1|0.46336064| PASSED

View File

@@ -51,6 +51,17 @@ static uint32_t inmEvenMisfires, inmOddMisfires;
static bool inmPrevEven, inmPrevOdd;
static bool inmDebug;
// Print the tables of statistics.
void inmDumpStats(void) {
uint32_t i;
for(i = 0; i < 1 << inmN; i++) {
//if(inmOnes[i] > 0 || inmZeros[i] > 0) {
printf("%x onesEven:%u zerosEven:%u onesOdd:%u zerosOdd:%u\n",
i, inmOnesEven[i], inmZerosEven[i], inmOnesOdd[i], inmZerosOdd[i]);
//}
}
}
// Free memory used by the health check.
void inmHealthCheckStop(void) {
if(inmOnesEven != NULL) {
@@ -289,17 +300,6 @@ bool inmEntropyOnTarget(uint32_t entropy, uint32_t numBits) {
#ifdef TEST_HEALTHCHECK
// Print the tables of statistics.
static void inmDumpStats(void) {
uint32_t i;
for(i = 0; i < 1 << inmN; i++) {
//if(inmOnes[i] > 0 || inmZeros[i] > 0) {
printf("%x onesEven:%u zerosEven:%u onesOdd:%u zerosOdd:%u\n",
i, inmOnesEven[i], inmZerosEven[i], inmOnesOdd[i], inmZerosOdd[i]);
//}
}
}
// Compare the ability to predict with 1 fewer bits and see how much less accurate we are.
static void checkLSBStatsForNBits(uint8_t N) {
uint32_t i, j;

View File

@@ -108,7 +108,7 @@ static uint32_t extractBytes(uint8_t *bytes, uint8_t *inBuf, bool raw) {
uint8_t evenBit = (val >> COMP2) & 1;
uint8_t oddBit = (val >> COMP1) & 1;
bool even = j & 1; // Use the even bit if j is odd
uint8_t bit = even? oddBit : evenBit;
uint8_t bit = even? evenBit : oddBit;
byte = (byte << 1) | bit;
// This is a good place to feed the bit from the INM to the health checker.
uint8_t addr = extractAddress(val);

View File

@@ -16,5 +16,6 @@ bool inmEntropyOnTarget(uint32_t entropy, uint32_t bits);
void inmWriteEntropyStart(uint32_t bufLen, bool debug);
void inmWriteEntropyToPool(uint8_t *bytes, uint32_t length, uint32_t entropy);
void inmWaitForPoolToHaveRoom(void);
void inmDumpStats(void);
extern double inmK, inmExpectedEntropyPerBit;

19
software/tools/Makefile Normal file
View File

@@ -0,0 +1,19 @@
all: healthcheck findlongest entcheck hex2bin flipbits
healthcheck: ../healthcheck.c
gcc -Wall -std=c99 -O3 -D TEST_HEALTHCHECK -o healthcheck ../healthcheck.c -lm -lrt
entcheck: entcheck.c
gcc -Wall -std=c99 -O3 -o entcheck entcheck.c -lm -lrt
findlongest: findlongest.c
gcc -Wall -std=c99 -O3 -o findlongest findlongest.c
hex2bin: hex2bin.c
gcc -Wall -std=c99 -O3 -o hex2bin hex2bin.c
flipbits: flipbits.c
gcc -Wall -std=c99 -O3 -o flipbits flipbits.c
clean:
rm -f healthcheck findlongest entcheck hex2bin flipbits

View File

@@ -1,23 +1,22 @@
/*
Measure the entropy level of an input sample.
*/
// Measure the entropy level of an input sample.
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "infnoise.h"
#define INM_MIN_DATA 80000
#define INM_MIN_SAMPLE_SIZE 100
#define INM_MAX_SEQUENCE 20
#define INM_MAX_COUNT (1 << 14)
static uint8_t inmN;
static uint8_t inmNumStreams;
static uint32_t inmPrevBits;
static uint32_t inmNumBitsSampled;
static uint32_t *inmOnes, *inmZeros;
static uint32_t **inmOnes, **inmZeros;
// The total probability of generating the string of states we did is
// 1/(2^inmNumBitsOfEntropy * inmCurrentProbability).
static uint32_t inmNumBitsOfEntropy;
@@ -27,16 +26,6 @@ static bool inmPrevBit;
static uint32_t inmTotalOnes, inmTotalZeros;
static bool inmDebug;
// Free memory used by the entropy check.
void inmEntCheckStop(void) {
if(inmOnes != NULL) {
free(inmOnes);
}
if(inmZeros != NULL) {
free(inmZeros);
}
}
// Reset the statistics.
static void resetStats(void) {
inmNumBitsSampled = 0;
@@ -49,7 +38,7 @@ static void resetStats(void) {
// Initialize the entropy check. N is the number of bits used to predict the next bit.
// At least 8 bits must be used, and no more than 30. In general, we should use bits
// large enough so that INM output will be uncorrelated with bits N samples back in time.
bool inmEntCheckStart(uint8_t N, bool debug) {
bool inmEntCheckStart(uint8_t N, uint8_t numStreams, bool debug) {
if(N < 1 || N > 30) {
return false;
}
@@ -57,26 +46,36 @@ bool inmEntCheckStart(uint8_t N, bool debug) {
inmNumBitsOfEntropy = 0;
inmCurrentProbability = 1.0;
inmN = N;
inmNumStreams = numStreams;
inmPrevBits = 0;
inmOnes = calloc(1u << N, sizeof(uint32_t));
inmZeros = calloc(1u << N, sizeof(uint32_t));
inmOnes = calloc(numStreams, sizeof(uint32_t *));
inmZeros = calloc(numStreams, sizeof(uint32_t *));
if(inmOnes == NULL || inmZeros == NULL) {
return false;
}
uint8_t i;
for(i = 0; i < numStreams; i++) {
inmOnes[i] = calloc(1u << N, sizeof(uint32_t));
inmZeros[i] = calloc(1u << N, sizeof(uint32_t));
if(inmOnes[i] == NULL || inmZeros[i] == NULL) {
return false;
}
}
inmTotalBits = 0;
inmPrevBit = false;
resetStats();
if(inmOnes == NULL || inmZeros == NULL) {
inmEntCheckStop();
return false;
}
return true;
}
// If running continuously, it is possible to start overflowing the 32-bit counters for
// zeros and ones. Check for this, and scale the stats if needed.
static void scaleStats(void) {
uint32_t i;
for(i = 0; i < (1 << inmN); i++) {
inmZeros[i] >>= 1;
inmOnes[i] >>= 1;
uint32_t i, j;
for(i = 0; i < inmNumStreams; i++) {
for(j = 0; j < (1 << inmN); j++) {
inmZeros[i][j] >>= 1;
inmOnes[i][j] >>= 1;
}
}
}
@@ -101,6 +100,7 @@ static void scaleZeroOneCounts(void) {
// This should be called for each bit generated.
bool inmEntCheckAddBit(bool bit) {
uint8_t stream = inmTotalBits % inmNumStreams;
inmTotalBits++;
inmPrevBits = (inmPrevBits << 1) & ((1 << inmN)-1);
if(inmPrevBit) {
@@ -114,17 +114,19 @@ bool inmEntCheckAddBit(bool bit) {
inmTotalZeros++;
}
}
uint32_t zeros, ones;
zeros = inmZeros[inmPrevBits];
ones = inmOnes[inmPrevBits];
uint32_t total = zeros + ones;
uint32_t *zeros = inmZeros[stream];
uint32_t *ones = inmOnes[stream];
uint32_t numZeros, numOnes;
numZeros = zeros[inmPrevBits];
numOnes = ones[inmPrevBits];
uint32_t total = numZeros + numOnes;
if(bit) {
if(ones != 0) {
inmCurrentProbability *= (double)ones/total;
if(numOnes != 0) {
inmCurrentProbability *= (double)numOnes/total;
}
} else {
if(zeros != 0) {
inmCurrentProbability *= (double)zeros/total;
if(numZeros != 0) {
inmCurrentProbability *= (double)numZeros/total;
}
}
while(inmCurrentProbability <= 0.5) {
@@ -134,13 +136,13 @@ bool inmEntCheckAddBit(bool bit) {
//printf("probability:%f\n", inmCurrentProbability);
inmNumBitsSampled++;
if(bit) {
inmOnes[inmPrevBits]++;
if(inmOnes[inmPrevBits] == INM_MAX_COUNT) {
ones[inmPrevBits]++;
if(ones[inmPrevBits] == INM_MAX_COUNT) {
scaleStats();
}
} else {
inmZeros[inmPrevBits]++;
if(inmZeros[inmPrevBits] == INM_MAX_COUNT) {
zeros[inmPrevBits]++;
if(zeros[inmPrevBits] == INM_MAX_COUNT) {
scaleStats();
}
}
@@ -157,31 +159,62 @@ double inmEntCheckEstimateEntropyPerBit(void) {
// Print the tables of statistics.
static void inmDumpStats(void) {
uint32_t i;
for(i = 0; i < 1 << inmN; i++) {
//if(inmOnes[i] > 0 || inmZeros[i] > 0) {
printf("%x ones:%u zeros:%u\n", i, inmOnes[i], inmZeros[i]);
//}
uint32_t i, j;
for(i = 0; i < inmNumStreams; i++) {
printf("*************************************** stream %u\n", i);
for(j = 0; j < 1 << inmN; j++) {
printf("%x ones:%u zeros:%u\n", j, inmOnes[i][j], inmZeros[i][j]);
}
}
}
// Print usage and exit
static void usage(void) {
fprintf(stderr, "Usage: entcheck [options]\n"
" -N numBits -- Use N previous bits to predict the next bit\n"
" -s numStreams -- Use s streams to predict the next bit\n"
"\n"
"entcheck simply uses the previous N bits (16 by default) to predict the next bit.\n"
"It estimates the entropy based on 'surprise', or log2 of the probability of seeing\n"
"the string of 0's and 1's. Sometimes some bits are special, such as the output of\n"
"an 8 bit DAC, where we want different tables depending on which bit we're predicting.\n"
"Set numStreams to the DAC width in this case.\n");
exit(1);
}
int main(int argc, char **argv) {
uint8_t N = 16;
if(argc == 2) {
N = atoi(argv[1]);
uint8_t N = 12;
uint8_t numStreams = 2;
uint32_t i;
for(i = 1; i < argc; i++) {
if(!strcmp(argv[i], "-N")) {
i++;
if(i == argc) {
usage();
}
N = atoi(argv[i]);
if(N == 0) {
usage();
}
} else if(!strcmp(argv[i], "-s")) {
i++;
if(i == argc) {
usage();
}
numStreams = atoi(argv[i]);
if(numStreams == 0 || numStreams > 32) {
usage();
}
} else {
usage();
}
}
if (N <= 0 || argc > 2) {
printf("Usage: entcheck [numBits]\n"
"\n"
"entcheck simply uses the previous numBits (16 by default) to predict the next bit.\n"
"It estimates the entropy based on 'surprise', or log2 of the probability of seeing\n"
"the string of 0's and 1's.\n");
if(!inmEntCheckStart(N, numStreams, true)) {
fprintf(stderr, "Unable to allocate memory\n");
return 1;
}
inmEntCheckStart(N, true);
int value = getchar();
while(value != EOF) {
int i;
for(i = 0; i < 8; i++) {
inmEntCheckAddBit(value & 1);
value >>= 1;
@@ -196,6 +229,5 @@ int main(int argc, char **argv) {
if(inmDebug) {
inmDumpStats();
}
inmEntCheckStop();
return 0;
}

17
software/tools/flipbits.c Normal file
View File

@@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int value = getchar();
while(value != EOF) {
int i, revVal = 0;
for(i = 0; i < 8; i++) {
revVal <<= 1;
revVal |= value & 1;
value >>= 1;
}
putchar(revVal);
value = getchar();
}
return 0;
}