33 if (!data || len == 0)
35 for (i = 0; i < 256; i++)
37 for (i = 0; i < len; i++)
39 for (i = 0; i < 256; i++) {
41 double p = (double)counts[i] / (
double)len;
51 if (!data || len == 0)
53 for (i = 0; i < len; i++) {
54 unsigned int b = data[i];
56 b = b - ((b >> 1) & 0x55u);
57 b = (b & 0x33u) + ((b >> 2) & 0x33u);
58 b = (b + (b >> 4)) & 0x0Fu;
61 return (
double)ones / ((double)len * 8.0);
67 double expected, chi = 0.0;
68 if (!data || len == 0)
70 for (i = 0; i < 256; i++)
72 for (i = 0; i < len; i++)
74 expected = (double)len / 256.0;
75 for (i = 0; i < 256; i++) {
76 double diff = (double)counts[i] - expected;
77 chi += (diff * diff) / expected;
84 if (df == 0 || chi <= 0.0)
90 z = (t - (1.0 - 2.0 / (9.0 * d))) / sqrt(2.0 / (9.0 * d));
91 return 0.5 * erfc(z / sqrt(2.0));
96 if (!samples || !out || count == 0 || sample_len == 0 || max_out == 0)
98 n = (sample_len < max_out) ? sample_len : max_out;
99 for (pos = 0; pos < n; pos++) {
101 size_t i, distinct = 0;
102 double h = 0.0, chi = 0.0;
103 for (i = 0; i < 256; i++)
105 for (i = 0; i < count; i++)
107 counts[samples[i][pos]]++;
108 for (i = 0; i < 256; i++)
110 double p = (double)counts[i] / (
double)count;
118 double expected = (double)count / (
double)distinct;
119 for (i = 0; i < 256; i++)
121 double diff = (double)counts[i] - expected;
122 chi += (diff * diff) / expected;
135 if (!samples || !out || count == 0 || sample_len == 0 || max_out == 0)
137 nbits = sample_len * 8;
140 for (bit = 0; bit < nbits; bit++) {
141 size_t byte = bit / 8;
142 unsigned mask = (unsigned)(0x80u >> (bit % 8));
145 for (i = 0; i < count; i++)
146 if (samples[i] && (samples[i][
byte] & mask))
148 frac = (double)ones / (
double)count;
150 z = ((double)ones - (double)count / 2.0) / (sqrt((
double)count) / 2.0);
151 out[bit].
ones = ones;
154 out[bit].
p_value = erfc(fabs(z) / sqrt(2.0));
160 size_t nbits, i, runs = 1;
163 if (!data || len == 0)
169 if (fabs(pi - 0.5) >= 2.0 / sqrt((
double)nbits))
171 prev = (data[0] & 0x80u) ? 1 : 0;
172 for (i = 1; i < nbits; i++) {
173 int b = (data[i / 8] & (unsigned)(0x80u >> (i % 8))) ? 1 : 0;
178 z = fabs((
double)runs - 2.0 * (
double)nbits * pi * (1.0 - pi)) /
179 (2.0 * sqrt(2.0 * (
double)nbits) * pi * (1.0 - pi));
180 return erfc(z / sqrt(2.0));
184 size_t nbits, i, best = 0, cur = 1;
186 if (!data || len == 0)
189 prev = (data[0] & 0x80u) ? 1 : 0;
191 for (i = 1; i < nbits; i++) {
192 int b = (data[i / 8] & (unsigned)(0x80u >> (i % 8))) ? 1 : 0;
double p_value
upper-tail p-value of chi_square
double chi_square
statistic against a uniform spread over the values seen
size_t ones
samples whose bit is set
size_t distinct
distinct byte values seen at this position
double p_value
two-sided p-value of z
double fraction
ones / sample count (0.5 is ideal)
double shannon
bits of entropy at this position (0 .
double z
z-score of the imbalance against a fair coin
size_t n_entropy_positions(const unsigned char *const *samples, size_t count, size_t sample_len, N_ENTROPY_POS *out, size_t max_out)
Per-POSITION analysis of a set of equal-length samples.
size_t n_entropy_bits(const unsigned char *const *samples, size_t count, size_t sample_len, N_ENTROPY_BIT *out, size_t max_out)
Per-BIT analysis of a set of equal-length samples.
size_t n_entropy_longest_run(const unsigned char *data, size_t len)
Length of the longest run of identical bits in the sample.
double n_entropy_shannon(const unsigned char *data, size_t len)
Shannon entropy of the byte sample, in bits per byte (0.0 .
double n_entropy_monobit(const unsigned char *data, size_t len)
Fraction of set bits in the sample (0.0 .
double n_entropy_chi_square(const unsigned char *data, size_t len)
Chi-square statistic of the byte histogram against a uniform distribution over 256 values.
double n_entropy_chi_square_p(double chi, size_t df)
Upper-tail p-value of a chi-square statistic (Wilson-Hilferty).
double n_entropy_runs_p(const unsigned char *data, size_t len)
Runs test over the sample's bit stream (NIST SP 800-22 style).
per-bit statistics across a set of equal-length samples
per-position statistics across a set of equal-length samples
Randomness/entropy metrics for byte samples (token-randomness analysis).