Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_entropy.c
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
27#include "nilorea/n_entropy.h"
28#include "nilorea/n_log.h"
29
30#include <math.h>
31#include <string.h>
32
33static int failures = 0;
34
35static void approx(const char* label, double got, double want, double tol) {
36 if (fabs(got - want) > tol) {
37 n_log(LOG_ERR, "%s: got %.4f, expected %.4f", label, got, want);
38 failures++;
39 }
40}
41
42int main(void) {
43 unsigned char uni[256];
44 unsigned char same[64];
45 unsigned char z[8];
46 unsigned char f[8];
47 unsigned char half[8];
48 unsigned char two[4] = {'A', 'B', 'A', 'B'};
49 int i;
51
52 /* a uniform byte histogram: max entropy, zero chi-square, balanced bits */
53 for (i = 0; i < 256; i++)
54 uni[i] = (unsigned char)i;
55 approx("uniform shannon", n_entropy_shannon(uni, 256), 8.0, 0.0001);
56 approx("uniform chi", n_entropy_chi_square(uni, 256), 0.0, 0.0001);
57 approx("uniform monobit", n_entropy_monobit(uni, 256), 0.5, 0.01);
58
59 /* a single repeated value: zero entropy, large chi-square */
60 memset(same, 'A', sizeof(same));
61 approx("same shannon", n_entropy_shannon(same, sizeof(same)), 0.0, 0.0001);
62 if (n_entropy_chi_square(same, sizeof(same)) <= 255.0) {
63 n_log(LOG_ERR, "same chi-square should be large");
64 failures++;
65 }
66
67 /* monobit on known bit patterns */
68 memset(z, 0x00, sizeof(z));
69 memset(f, 0xFF, sizeof(f));
70 memset(half, 0x0F, sizeof(half));
71 approx("zeros monobit", n_entropy_monobit(z, sizeof(z)), 0.0, 0.0001);
72 approx("ones monobit", n_entropy_monobit(f, sizeof(f)), 1.0, 0.0001);
73 approx("half monobit", n_entropy_monobit(half, sizeof(half)), 0.5, 0.0001);
74
75 /* two equally likely symbols: exactly one bit of entropy */
76 approx("two-symbol shannon", n_entropy_shannon(two, 4), 1.0, 0.0001);
77
78 /* empty / NULL samples are zero, not a crash */
79 approx("empty shannon", n_entropy_shannon(NULL, 0), 0.0, 0.0);
80 approx("empty chi", n_entropy_chi_square(z, 0), 0.0, 0.0);
81 approx("empty monobit", n_entropy_monobit(NULL, 0), 0.0, 0.0);
82
83 if (failures) {
84 n_log(LOG_ERR, "ex_entropy: %d failure(s)", failures);
85 return 1;
86 }
87 n_log(LOG_NOTICE, "ex_entropy: all checks passed");
88 return 0;
89}
static int failures
int main(void)
static void approx(const char *label, double got, double want, double tol)
Definition ex_entropy.c:35
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
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
Randomness/entropy metrics for byte samples (token-randomness analysis)
Generic log system.