Table of Contents
ENTROPY: randomness metrics for byte samples
Structures
| Name | |
|---|---|
| struct | N_ENTROPY_POS per-position statistics across a set of equal-length samples |
| struct | N_ENTROPY_BIT per-bit statistics across a set of equal-length samples |
Functions
| Name | |
|---|---|
| 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_runs_p(const unsigned char * data, size_t len) Runs test over the sample's bit stream (NIST SP 800-22 style). |
| 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. |
| double | n_entropy_monobit(const unsigned char * data, size_t len) Fraction of set bits in the sample (0.0 . |
| 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_chi_square_p(double chi, size_t df) Upper-tail p-value of a chi-square statistic (Wilson-Hilferty). |
| 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. |
| 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. |
Functions Documentation
function n_entropy_shannon
double n_entropy_shannon(
const unsigned char * data,
size_t len
)
Shannon entropy of the byte sample, in bits per byte (0.0 .
. 8.0). 0 means a single repeated value; 8 means a perfectly uniform byte histogram. Returns 0.0 for an empty/NULL sample.
function n_entropy_runs_p
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).
Counts the transitions between runs of equal bits and scores them against what a fair coin would produce. It catches what monobit cannot: a stream with exactly half its bits set but arranged as 0000...1111 passes monobit and fails this. Returns the two-sided p-value (near 1.0 is unremarkable), or 0.0 when the sample is empty or its monobit fraction is too skewed for the test to apply.
function n_entropy_positions
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.
Parameters:
- samples array of
countpointers, each tosample_lenbytes - count number of samples (positions need several to mean anything)
- sample_len length of every sample
- out array receiving one entry per position
- max_out capacity of out
Return: the number of positions written, or 0 on a bad argument
An aggregate metric over concatenated tokens hides the finding that matters: a token can look random overall while its third character is always one of two values, or its counter half increments. This reports each byte position separately, so the caller can name WHICH position is predictable.
function n_entropy_monobit
double n_entropy_monobit(
const unsigned char * data,
size_t len
)
Fraction of set bits in the sample (0.0 .
. 1.0); 0.5 is the ideal for random data (the FIPS monobit idea). Returns 0.0 for an empty/NULL sample.
function n_entropy_longest_run
size_t n_entropy_longest_run(
const unsigned char * data,
size_t len
)
Length of the longest run of identical bits in the sample.
A long run in a token set is a stuck field; returns 0 for an empty sample.
function n_entropy_chi_square_p
double n_entropy_chi_square_p(
double chi,
size_t df
)
Upper-tail p-value of a chi-square statistic (Wilson-Hilferty).
How surprising chi is for df degrees of freedom if the data really were uniform: near 1.0 means unremarkable, near 0.0 means the distribution is skewed far beyond chance. The Wilson-Hilferty cube-root transform is used rather than the exact incomplete gamma: it is accurate to a few thousandths for df >= 3, which is well inside what a verdict needs, and it is a dozen lines instead of a numerics library. Returns 1.0 for df == 0 or a non-positive statistic.
function n_entropy_chi_square
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.
Near 255 (the degrees of freedom) for random data; large for skewed data. Returns 0.0 for an empty/NULL sample.
function n_entropy_bits
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.
Parameters:
- samples array of
countpointers, each tosample_lenbytes - count number of samples
- sample_len length of every sample
- out array receiving one entry per bit (sample_len * 8 of them)
- max_out capacity of out
Return: the number of bits written, or 0 on a bad argument
The bit-level counterpart of n_entropy_positions: it finds a bit that is stuck, or biased, across the sample set, which is how a truncated counter or a fixed-prefix identifier shows up.
Modules
- AABB3D
- ACCEPT_POOL
- ALLEGRO4
- ALLEGRO5
- ANIM_LIB
- ASTAR
- ATLAS_LIB
- AVRO
- CLOCK_SYNC
- COMMONS
- CONFIGFILE
- CYPHER_BASE64
- CYPHER_VIGENERE
- DEAD_RECKONING
- ENUMS
- EXCEPTIONS
- GAME_ENV
- GIT
- GUI
- HASH_TABLE
- ISOMETRIC_ENGINE
- LIST
- LOG
- LOGNODUP
- LZ4
- NETWORKING
- NETWORK_MSG
- N_COOKIES
- N_DIFF
- N_DIGEST
- N_DISK_CACHE
- N_DNS
- N_ECC
- N_ENTROPY
- N_FILES
- N_FLUIDS
- N_GUI_COLOR
- N_GUI_COLORWHEEL
- N_GUI_CURVE
- N_GUI_GRID
- N_GUI_POPUP
- N_HEX
- N_HTTP3
- N_HTTP_COOKIE
- N_HTTP_FORM
- N_HTTP_MIME
- N_HTTP_REQ
- N_HTTP_RESP
- N_HTTP_ROUTER
- N_HTTP_SERVER
- N_ICC
- N_JSON
- N_KAFKA
- N_LINALG
- N_MODULE
- N_OPENSSL_COMPAT
- N_PRETTY
- N_PTH
- N_QUERY
- N_RANDOM
- N_SHA256
- N_STR
- N_TIFF
- N_TIME
- N_USER
- N_X509
- N_ZIP
- PARTICLES
- PCRE
- PHYSICS
- SIGNALS
- STACK
- THREADS
- TRAJECTORY
- TREE
- ZLIB