Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_entropy.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
26
27#ifndef __NILOREA_ENTROPY_HEADER
28#define __NILOREA_ENTROPY_HEADER
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#include <stddef.h>
35
40
44double n_entropy_shannon(const unsigned char* data, size_t len);
45
48double n_entropy_monobit(const unsigned char* data, size_t len);
49
53double n_entropy_chi_square(const unsigned char* data, size_t len);
54
63double n_entropy_chi_square_p(double chi, size_t df);
64
66typedef struct N_ENTROPY_POS {
67 size_t distinct;
68 double shannon;
69 double chi_square;
70 double p_value;
72
74typedef struct N_ENTROPY_BIT {
75 size_t ones;
76 double fraction;
77 double z;
78 double p_value;
80
94size_t n_entropy_positions(const unsigned char* const* samples, size_t count, size_t sample_len, N_ENTROPY_POS* out, size_t max_out);
95
108size_t n_entropy_bits(const unsigned char* const* samples, size_t count, size_t sample_len, N_ENTROPY_BIT* out, size_t max_out);
109
117double n_entropy_runs_p(const unsigned char* data, size_t len);
118
121size_t n_entropy_longest_run(const unsigned char* data, size_t len);
122
126
127#ifdef __cplusplus
128}
129#endif
130
131/* #ifndef __NILOREA_ENTROPY_HEADER */
132#endif
double p_value
upper-tail p-value of chi_square
Definition n_entropy.h:70
double chi_square
statistic against a uniform spread over the values seen
Definition n_entropy.h:69
size_t ones
samples whose bit is set
Definition n_entropy.h:75
size_t distinct
distinct byte values seen at this position
Definition n_entropy.h:67
double p_value
two-sided p-value of z
Definition n_entropy.h:78
double fraction
ones / sample count (0.5 is ideal)
Definition n_entropy.h:76
double shannon
bits of entropy at this position (0 .
Definition n_entropy.h:68
double z
z-score of the imbalance against a fair coin
Definition n_entropy.h:77
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.
Definition n_entropy.c:94
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.
Definition n_entropy.c:133
size_t n_entropy_longest_run(const unsigned char *data, size_t len)
Length of the longest run of identical bits in the sample.
Definition n_entropy.c:183
double n_entropy_shannon(const unsigned char *data, size_t len)
Shannon entropy of the byte sample, in bits per byte (0.0 .
Definition n_entropy.c:29
double n_entropy_monobit(const unsigned char *data, size_t len)
Fraction of set bits in the sample (0.0 .
Definition n_entropy.c:48
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.
Definition n_entropy.c:64
double n_entropy_chi_square_p(double chi, size_t df)
Upper-tail p-value of a chi-square statistic (Wilson-Hilferty).
Definition n_entropy.c:82
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).
Definition n_entropy.c:159
per-bit statistics across a set of equal-length samples
Definition n_entropy.h:74
per-position statistics across a set of equal-length samples
Definition n_entropy.h:66