openentropy-core 0.12.3

Core entropy harvesting library — hardware noise sources, raw or SHA-256 conditioned
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
//! # Chaos Theory Analysis
//!
//! Methods for applying chaos theory analysis to QRNG output.
//! These functions help distinguish genuine quantum randomness from
//! deterministic or structured behavior in sampled byte streams.

use flate2::Compression;
use flate2::write::ZlibEncoder;
use serde::Serialize;
use std::f64::consts::LN_2;

use crate::math;

#[derive(Debug, Clone, Serialize)]
pub struct HurstResult {
    pub hurst_exponent: f64,
    pub is_valid: bool,
    pub r_squared: f64,
    pub rs_values: Vec<(usize, f64)>,
}

#[derive(Debug, Clone, Serialize)]
pub struct BootstrapHurstResult {
    pub observed_hurst: f64,
    pub mean_surrogate_hurst: f64,
    pub std_surrogate_hurst: f64,
    pub p_value: f64,
    pub ci_lower: f64,
    pub ci_upper: f64,
    pub n_surrogates: usize,
    pub is_significant: bool,
    pub is_valid: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct RollingHurstWindow {
    pub offset: usize,
    pub hurst: f64,
    pub r_squared: f64,
    pub is_valid: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct RollingHurstResult {
    pub windows: Vec<RollingHurstWindow>,
    pub mean_hurst: f64,
    pub std_hurst: f64,
    pub min_hurst: f64,
    pub max_hurst: f64,
    pub window_size: usize,
    pub step: usize,
    pub is_valid: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct DfaResult {
    pub alpha: f64,
    pub r_squared: f64,
    pub fluctuations: Vec<(usize, f64)>,
    pub order: usize,
    pub is_valid: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct BiEntropyResult {
    pub bien: f64,
    pub tbien: f64,
    pub derivative_entropies: Vec<f64>,
    pub num_derivatives: usize,
    pub is_valid: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct EpiplexityResult {
    pub compression_ratio: f64,       // compressed_size / raw_size
    pub structural_info: f64,         // 1.0 - compression_ratio (epiplexity)
    pub remaining_entropy: f64,       // compression_ratio (unpredictability)
    pub delta_compression_ratio: f64, // compress(diff(data)) / diff_size
    pub is_valid: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct SampleEntropyResult {
    pub sample_entropy: f64,
    pub m: usize,
    pub r: f64,
    pub count_a: u64,
    pub count_b: u64,
    pub actual_samples: usize,
    pub is_valid: bool,
}

fn sample_std_dev(data: &[f64]) -> f64 {
    let n = data.len();
    if n < 2 {
        return 0.0;
    }

    let mean = data.iter().sum::<f64>() / n as f64;
    let variance = data
        .iter()
        .map(|x| {
            let d = x - mean;
            d * d
        })
        .sum::<f64>()
        / (n as f64 - 1.0);

    variance.sqrt()
}

fn percentile(sorted: &[f64], p: f64) -> f64 {
    if sorted.is_empty() {
        return f64::NAN;
    }

    let idx = (p / 100.0 * (sorted.len() - 1) as f64).round() as usize;
    sorted[idx.min(sorted.len() - 1)]
}

fn log_spaced_windows(min: usize, max: usize, count: usize) -> Vec<usize> {
    if max <= min || count < 2 {
        return vec![min, max];
    }

    let log_min = (min as f64).ln();
    let log_max = (max as f64).ln();
    (0..count)
        .map(|i| {
            let t = i as f64 / (count - 1) as f64;
            (log_min + t * (log_max - log_min)).exp().round() as usize
        })
        .collect::<std::collections::BTreeSet<_>>()
        .into_iter()
        .collect()
}

fn subsample_to_limit(data: &[u8], max_samples: usize) -> Vec<f64> {
    let data_f64: Vec<f64> = data.iter().map(|&x| x as f64).collect();
    if data_f64.len() > max_samples {
        let step = (data_f64.len() / max_samples).max(1);
        data_f64
            .iter()
            .step_by(step)
            .take(max_samples)
            .copied()
            .collect()
    } else {
        data_f64
    }
}

pub fn sample_entropy(data: &[u8], m: usize, r: f64) -> SampleEntropyResult {
    const MAX_SAMPLES: usize = 5000;

    if m == 0 || data.len() <= m + 1 {
        return SampleEntropyResult {
            sample_entropy: f64::NAN,
            m,
            r,
            count_a: 0,
            count_b: 0,
            actual_samples: data.len().min(MAX_SAMPLES),
            is_valid: false,
        };
    }

    let sampled = subsample_to_limit(data, MAX_SAMPLES);
    if sampled.len() <= m + 1 {
        return SampleEntropyResult {
            sample_entropy: f64::NAN,
            m,
            r,
            count_a: 0,
            count_b: 0,
            actual_samples: sampled.len(),
            is_valid: false,
        };
    }

    let std_dev = sample_std_dev(&sampled);
    if std_dev == 0.0 || !r.is_finite() || r <= 0.0 {
        return SampleEntropyResult {
            sample_entropy: f64::NAN,
            m,
            r,
            count_a: 0,
            count_b: 0,
            actual_samples: sampled.len(),
            is_valid: false,
        };
    }

    let n = sampled.len();
    let n_m = n - m;
    let n_m1 = n - m - 1;
    let mut count_b: u64 = 0;
    let mut count_a: u64 = 0;

    for i in 0..n_m {
        for j in 0..n_m {
            if i == j {
                continue;
            }

            let mut matches_m = true;
            for k in 0..m {
                if (sampled[i + k] - sampled[j + k]).abs() >= r {
                    matches_m = false;
                    break;
                }
            }

            if !matches_m {
                continue;
            }

            count_b += 1;

            if i < n_m1 && j < n_m1 && (sampled[i + m] - sampled[j + m]).abs() < r {
                count_a += 1;
            }
        }
    }

    if count_b == 0 || count_a == 0 {
        return SampleEntropyResult {
            sample_entropy: f64::NAN,
            m,
            r,
            count_a,
            count_b,
            actual_samples: sampled.len(),
            is_valid: false,
        };
    }

    let sampen = -((count_a as f64) / (count_b as f64)).ln();
    SampleEntropyResult {
        sample_entropy: sampen,
        m,
        r,
        count_a,
        count_b,
        actual_samples: sampled.len(),
        is_valid: sampen.is_finite(),
    }
}

pub fn sample_entropy_default(data: &[u8]) -> SampleEntropyResult {
    const DEFAULT_M: usize = 2;
    const MAX_SAMPLES: usize = 5000;

    if data.len() <= DEFAULT_M + 1 {
        return SampleEntropyResult {
            sample_entropy: f64::NAN,
            m: DEFAULT_M,
            r: f64::NAN,
            count_a: 0,
            count_b: 0,
            actual_samples: data.len().min(MAX_SAMPLES),
            is_valid: false,
        };
    }

    let sampled = subsample_to_limit(data, MAX_SAMPLES);
    let std_dev = sample_std_dev(&sampled);
    if std_dev == 0.0 {
        return SampleEntropyResult {
            sample_entropy: f64::NAN,
            m: DEFAULT_M,
            r: 0.0,
            count_a: 0,
            count_b: 0,
            actual_samples: sampled.len(),
            is_valid: false,
        };
    }

    sample_entropy(data, DEFAULT_M, 0.2 * std_dev)
}

#[allow(dead_code)]
fn shannon_entropy_binary(data: &[u8]) -> f64 {
    if data.is_empty() {
        return 0.0;
    }

    let mut ones = 0usize;
    let total_bits = data.len() * 8;
    for &byte in data {
        ones += byte.count_ones() as usize;
    }

    let p1 = ones as f64 / total_bits as f64;
    let p0 = 1.0 - p1;

    let h0 = if p0 <= 0.0 {
        0.0
    } else {
        -p0 * (p0.ln() / LN_2)
    };
    let h1 = if p1 <= 0.0 {
        0.0
    } else {
        -p1 * (p1.ln() / LN_2)
    };
    h0 + h1
}

pub fn hurst_exponent(data: &[u8]) -> HurstResult {
    if data.len() < 100 {
        return HurstResult {
            hurst_exponent: f64::NAN,
            is_valid: false,
            r_squared: 0.0,
            rs_values: vec![],
        };
    }

    let n = data.len();
    let min_window = 10usize;
    let max_window = n / 4;
    if max_window <= min_window {
        return HurstResult {
            hurst_exponent: f64::NAN,
            is_valid: false,
            r_squared: 0.0,
            rs_values: vec![],
        };
    }

    let step = ((max_window - min_window) / 20).max(1);
    let mut window_sizes = Vec::new();
    let mut rs_means = Vec::new();
    let mut rs_values = Vec::new();

    for window_size in (min_window..max_window).step_by(step) {
        let mut rs_for_size = Vec::new();

        for start in (0..=(n - window_size)).step_by(window_size) {
            let segment = &data[start..start + window_size];
            let segment_f64: Vec<f64> = segment.iter().map(|&x| x as f64).collect();

            let mean = segment_f64.iter().sum::<f64>() / window_size as f64;

            let mut cumulative = 0.0;
            let mut min_cum = 0.0;
            let mut max_cum = 0.0;
            for &value in &segment_f64 {
                cumulative += value - mean;
                if cumulative < min_cum {
                    min_cum = cumulative;
                }
                if cumulative > max_cum {
                    max_cum = cumulative;
                }
            }

            let range = max_cum - min_cum;
            let std_dev = sample_std_dev(&segment_f64);

            if std_dev > 1e-10 {
                rs_for_size.push(range / std_dev);
            }
        }

        if !rs_for_size.is_empty() {
            let mean_rs = rs_for_size.iter().sum::<f64>() / rs_for_size.len() as f64;
            window_sizes.push(window_size as f64);
            rs_means.push(mean_rs);
            rs_values.push((window_size, mean_rs));
        }
    }

    if rs_values.len() < 5 {
        return HurstResult {
            hurst_exponent: f64::NAN,
            is_valid: false,
            r_squared: 0.0,
            rs_values,
        };
    }

    let log_n: Vec<f64> = window_sizes.iter().map(|&v| v.ln()).collect();
    let log_rs: Vec<f64> = rs_means.iter().map(|&v| v.ln()).collect();
    let (slope, _intercept, r_squared) = math::linear_regression(&log_n, &log_rs);

    HurstResult {
        hurst_exponent: slope,
        is_valid: slope.is_finite(),
        r_squared,
        rs_values,
    }
}

pub fn rolling_hurst(data: &[u8], window_size: usize, step: usize) -> RollingHurstResult {
    let invalid = || RollingHurstResult {
        windows: vec![],
        mean_hurst: f64::NAN,
        std_hurst: f64::NAN,
        min_hurst: f64::NAN,
        max_hurst: f64::NAN,
        window_size,
        step,
        is_valid: false,
    };

    if window_size == 0 || step == 0 || data.len() < window_size {
        return invalid();
    }

    let mut windows = Vec::new();
    let mut offset = 0usize;
    while offset + window_size <= data.len() {
        let result = hurst_exponent(&data[offset..offset + window_size]);
        windows.push(RollingHurstWindow {
            offset,
            hurst: result.hurst_exponent,
            r_squared: result.r_squared,
            is_valid: result.is_valid,
        });
        offset += step;
    }

    let valid_hursts: Vec<f64> = windows
        .iter()
        .filter_map(|w| {
            if w.is_valid && w.hurst.is_finite() {
                Some(w.hurst)
            } else {
                None
            }
        })
        .collect();

    if valid_hursts.is_empty() {
        return RollingHurstResult {
            windows,
            mean_hurst: f64::NAN,
            std_hurst: f64::NAN,
            min_hurst: f64::NAN,
            max_hurst: f64::NAN,
            window_size,
            step,
            is_valid: false,
        };
    }

    let mean_hurst = valid_hursts.iter().sum::<f64>() / valid_hursts.len() as f64;
    let std_hurst = sample_std_dev(&valid_hursts);
    let min_hurst = valid_hursts.iter().copied().fold(f64::INFINITY, f64::min);
    let max_hurst = valid_hursts
        .iter()
        .copied()
        .fold(f64::NEG_INFINITY, f64::max);

    RollingHurstResult {
        windows,
        mean_hurst,
        std_hurst,
        min_hurst,
        max_hurst,
        window_size,
        step,
        is_valid: true,
    }
}

pub fn rolling_hurst_default(data: &[u8]) -> RollingHurstResult {
    rolling_hurst(data, 512, 64)
}

pub fn bootstrap_hurst(data: &[u8], n_bootstrap: usize) -> BootstrapHurstResult {
    let invalid = || BootstrapHurstResult {
        observed_hurst: f64::NAN,
        mean_surrogate_hurst: f64::NAN,
        std_surrogate_hurst: f64::NAN,
        p_value: f64::NAN,
        ci_lower: f64::NAN,
        ci_upper: f64::NAN,
        n_surrogates: 0,
        is_significant: false,
        is_valid: false,
    };

    let observed = hurst_exponent(data);
    if !observed.is_valid || n_bootstrap == 0 {
        return invalid();
    }

    let mut state = data
        .iter()
        .fold(0u64, |acc, &b| acc.wrapping_mul(31).wrapping_add(b as u64));

    let mut surrogate_hursts = Vec::with_capacity(n_bootstrap);
    for _ in 0..n_bootstrap {
        let mut surrogate = data.to_vec();
        for i in (1..surrogate.len()).rev() {
            state = state
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            let j = (state as usize) % (i + 1);
            surrogate.swap(i, j);
        }

        let result = hurst_exponent(&surrogate);
        if result.is_valid {
            surrogate_hursts.push(result.hurst_exponent);
        }
    }

    if surrogate_hursts.is_empty() {
        return invalid();
    }

    let mean_surrogate_hurst = surrogate_hursts.iter().sum::<f64>() / surrogate_hursts.len() as f64;
    let std_surrogate_hurst = sample_std_dev(&surrogate_hursts);

    let mut sorted = surrogate_hursts.clone();
    sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
    let ci_lower = percentile(&sorted, 2.5);
    let ci_upper = percentile(&sorted, 97.5);

    let ge_count = surrogate_hursts
        .iter()
        .filter(|&&h| h >= observed.hurst_exponent)
        .count();
    let p_value = ge_count as f64 / surrogate_hursts.len() as f64;

    BootstrapHurstResult {
        observed_hurst: observed.hurst_exponent,
        mean_surrogate_hurst,
        std_surrogate_hurst,
        p_value,
        ci_lower,
        ci_upper,
        n_surrogates: surrogate_hursts.len(),
        is_significant: observed.hurst_exponent < ci_lower || observed.hurst_exponent > ci_upper,
        is_valid: true,
    }
}

pub fn bootstrap_hurst_default(data: &[u8]) -> BootstrapHurstResult {
    bootstrap_hurst(data, 200)
}

pub fn dfa(data: &[u8], order: usize) -> DfaResult {
    if data.len() < 40 || order != 1 {
        return DfaResult {
            alpha: f64::NAN,
            r_squared: 0.0,
            fluctuations: vec![],
            order,
            is_valid: false,
        };
    }

    let data_f64: Vec<f64> = data.iter().map(|&x| x as f64).collect();
    let mean = data_f64.iter().sum::<f64>() / data_f64.len() as f64;

    let mut integrated = Vec::with_capacity(data_f64.len());
    let mut cumulative = 0.0;
    for &v in &data_f64 {
        cumulative += v - mean;
        integrated.push(cumulative);
    }

    let min_window = 10usize;
    let max_window = integrated.len() / 4;
    if max_window <= min_window {
        return DfaResult {
            alpha: f64::NAN,
            r_squared: 0.0,
            fluctuations: vec![],
            order,
            is_valid: false,
        };
    }

    let window_sizes = log_spaced_windows(min_window, max_window, 20);
    let mut fluctuations = Vec::new();
    let mut log_n = Vec::new();
    let mut log_f = Vec::new();

    for &window_size in &window_sizes {
        let n_windows = integrated.len() / window_size;
        if n_windows < 2 {
            continue;
        }

        let t: Vec<f64> = (0..window_size).map(|i| i as f64).collect();
        let mut rms_values = Vec::with_capacity(n_windows);

        for w in 0..n_windows {
            let start = w * window_size;
            let end = start + window_size;
            let segment = &integrated[start..end];

            let (slope, intercept, _r2) = math::linear_regression(&t, segment);
            if !slope.is_finite() || !intercept.is_finite() {
                continue;
            }

            let mse = segment
                .iter()
                .enumerate()
                .map(|(i, &y)| {
                    let trend = slope * i as f64 + intercept;
                    let d = y - trend;
                    d * d
                })
                .sum::<f64>()
                / window_size as f64;

            let rms = mse.sqrt();
            if rms.is_finite() {
                rms_values.push(rms);
            }
        }

        if !rms_values.is_empty() {
            let f_n = rms_values.iter().sum::<f64>() / rms_values.len() as f64;
            if f_n > 1e-12 {
                fluctuations.push((window_size, f_n));
                log_n.push((window_size as f64).ln());
                log_f.push(f_n.ln());
            }
        }
    }

    if fluctuations.len() < 4 {
        return DfaResult {
            alpha: f64::NAN,
            r_squared: 0.0,
            fluctuations,
            order,
            is_valid: false,
        };
    }

    let (alpha, _intercept, r_squared) = math::linear_regression(&log_n, &log_f);
    let is_valid = alpha.is_finite();

    DfaResult {
        alpha,
        r_squared,
        fluctuations,
        order,
        is_valid,
    }
}

pub fn dfa_default(data: &[u8]) -> DfaResult {
    dfa(data, 1)
}

fn bytes_to_bits(data: &[u8]) -> Vec<bool> {
    let mut bits = Vec::with_capacity(data.len() * 8);
    for &byte in data {
        for bit_index in 0..8 {
            bits.push(((byte >> bit_index) & 1) == 1);
        }
    }
    bits
}

fn binary_derivative(bits: &[bool]) -> Vec<bool> {
    if bits.len() < 2 {
        return vec![];
    }

    let mut derivative = Vec::with_capacity(bits.len() - 1);
    for i in 0..(bits.len() - 1) {
        derivative.push(bits[i] ^ bits[i + 1]);
    }
    derivative
}

fn shannon_entropy_bits(bits: &[bool]) -> f64 {
    if bits.is_empty() {
        return 0.0;
    }

    let ones = bits.iter().filter(|&&bit| bit).count();
    let p = ones as f64 / bits.len() as f64;
    if p <= 0.0 || p >= 1.0 {
        return 0.0;
    }

    let q = 1.0 - p;
    -p * (p.ln() / LN_2) - q * (q.ln() / LN_2)
}

pub fn bientropy(data: &[u8]) -> BiEntropyResult {
    if data.len() < 2 {
        return BiEntropyResult {
            bien: 0.0,
            tbien: 0.0,
            derivative_entropies: vec![],
            num_derivatives: 0,
            is_valid: false,
        };
    }

    let mut bien_sum = 0.0;
    let mut tbien_sum = 0.0;
    let mut chunk_count = 0usize;
    let mut derivative_sums: Vec<f64> = vec![];
    let mut derivative_counts: Vec<usize> = vec![];

    for chunk in data.chunks(32) {
        let bits = bytes_to_bits(chunk);
        if bits.len() < 2 {
            continue;
        }

        let max_k = 20usize.min(bits.len() - 1);
        let mut current = bits;
        let mut entropies = Vec::with_capacity(max_k + 1);

        for k in 0..=max_k {
            let h = shannon_entropy_bits(&current);
            entropies.push(h);

            if derivative_sums.len() <= k {
                derivative_sums.push(0.0);
                derivative_counts.push(0);
            }
            derivative_sums[k] += h;
            derivative_counts[k] += 1;

            if k < max_k {
                current = binary_derivative(&current);
            }
        }

        let mut bien_weighted = 0.0;
        let mut tbien_weighted = 0.0;
        let mut tbien_weight_total = 0.0;
        for (k, &h) in entropies.iter().enumerate() {
            let bien_weight = 2f64.powi(k as i32);
            let tbien_weight = (k as f64 + 2.0).log2();
            bien_weighted += bien_weight * h;
            tbien_weighted += tbien_weight * h;
            tbien_weight_total += tbien_weight;
        }

        let bien_normalizer = 2f64.powi((max_k + 1) as i32) - 1.0;
        let chunk_bien = if bien_normalizer > 0.0 {
            bien_weighted / bien_normalizer
        } else {
            0.0
        };
        let chunk_tbien = if tbien_weight_total > 0.0 {
            tbien_weighted / tbien_weight_total
        } else {
            0.0
        };

        bien_sum += chunk_bien;
        tbien_sum += chunk_tbien;
        chunk_count += 1;
    }

    if chunk_count == 0 {
        return BiEntropyResult {
            bien: 0.0,
            tbien: 0.0,
            derivative_entropies: vec![],
            num_derivatives: 0,
            is_valid: false,
        };
    }

    let derivative_entropies: Vec<f64> = derivative_sums
        .iter()
        .zip(derivative_counts.iter())
        .map(|(&sum, &count)| if count > 0 { sum / count as f64 } else { 0.0 })
        .collect();

    BiEntropyResult {
        bien: bien_sum / chunk_count as f64,
        tbien: tbien_sum / chunk_count as f64,
        num_derivatives: derivative_entropies.len(),
        derivative_entropies,
        is_valid: true,
    }
}

fn compress_bytes(data: &[u8]) -> usize {
    use std::io::Write;
    let mut encoder = ZlibEncoder::new(Vec::new(), Compression::best());
    let _ = encoder.write_all(data);
    encoder.finish().map(|v| v.len()).unwrap_or(data.len())
}

pub fn epiplexity(data: &[u8]) -> EpiplexityResult {
    // Guard: insufficient data
    if data.len() < 20 {
        return EpiplexityResult {
            compression_ratio: 0.0,
            structural_info: 0.0,
            remaining_entropy: 0.0,
            delta_compression_ratio: 0.0,
            is_valid: false,
        };
    }

    // Compression ratio of raw data
    let compressed_size = compress_bytes(data) as f64;
    let raw_size = data.len() as f64;
    let compression_ratio = compressed_size / raw_size;

    // Structural information (epiplexity) = 1 - compression_ratio
    let structural_info = 1.0 - compression_ratio;

    // Remaining entropy = compression_ratio (unpredictability)
    let remaining_entropy = compression_ratio;

    // Delta compression: differences between adjacent bytes
    let delta: Vec<u8> = data.windows(2).map(|w| w[1].wrapping_sub(w[0])).collect();

    let delta_compressed_size = compress_bytes(&delta) as f64;
    let delta_size = delta.len() as f64;
    let delta_compression_ratio = if delta_size > 0.0 {
        delta_compressed_size / delta_size
    } else {
        0.0
    };

    EpiplexityResult {
        compression_ratio,
        structural_info,
        remaining_entropy,
        delta_compression_ratio,
        is_valid: true,
    }
}

fn time_delay_embedding(data: &[f64], m: usize, tau: usize) -> Vec<Vec<f64>> {
    let n = data.len();
    if n < m * tau {
        return vec![];
    }
    let num_vectors = n - (m - 1) * tau;
    (0..num_vectors)
        .map(|i| (0..m).map(|j| data[i + j * tau]).collect())
        .collect()
}

#[derive(Debug, Clone, Serialize)]
pub struct LyapunovResult {
    pub lyapunov_exponent: f64,
    pub divergence_curve: Vec<f64>,
    pub embedding_dim: usize,
    pub delay: usize,
    pub r_squared: f64,
    pub is_valid: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct CorrelationDimResult {
    pub dimension: f64,
    pub r_squared: f64,
    pub embedding_dim_used: usize,
    pub num_points_used: usize,
    pub is_valid: bool,
}

pub fn lyapunov_exponent(data: &[u8]) -> LyapunovResult {
    const EMBEDDING_DIM: usize = 3;
    const TAU: usize = 1;
    const MAX_ITER: usize = 50;
    const THEILER_WINDOW: usize = 3;

    let invalid = || LyapunovResult {
        lyapunov_exponent: f64::NAN,
        divergence_curve: vec![],
        embedding_dim: EMBEDDING_DIM,
        delay: TAU,
        r_squared: 0.0,
        is_valid: false,
    };

    if data.len() < 500 {
        return invalid();
    }

    let data_f64: Vec<f64> = data.iter().map(|&b| b as f64).collect();
    let sampled: Vec<f64> = if data_f64.len() > 5000 {
        let step = (data_f64.len() / 5000).max(1);
        data_f64.iter().step_by(step).take(5000).copied().collect()
    } else {
        data_f64
    };

    let embedded = time_delay_embedding(&sampled, EMBEDDING_DIM, TAU);
    if embedded.len() < 100 {
        return invalid();
    }

    let reference_indices: Vec<usize> = if embedded.len() > 200 {
        let step = (embedded.len() / 200).max(1);
        (0..embedded.len()).step_by(step).take(200).collect()
    } else {
        (0..embedded.len()).collect()
    };

    let mut divergence_sums = vec![0.0; MAX_ITER];
    let mut divergence_counts = vec![0usize; MAX_ITER];

    for &i in &reference_indices {
        let mut nearest_neighbor = None;
        let mut min_distance = f64::INFINITY;

        for j in 0..embedded.len() {
            if i.abs_diff(j) <= THEILER_WINDOW {
                continue;
            }
            let d = math::euclidean_distance(&embedded[i], &embedded[j]);
            if d <= 1e-10 {
                continue;
            }
            if d < min_distance {
                min_distance = d;
                nearest_neighbor = Some(j);
            }
        }

        let Some(j) = nearest_neighbor else {
            continue;
        };

        for step in 0..MAX_ITER {
            if i + step >= embedded.len() || j + step >= embedded.len() {
                break;
            }
            let d = math::euclidean_distance(&embedded[i + step], &embedded[j + step]);
            if d > 1e-10 {
                divergence_sums[step] += d.ln();
                divergence_counts[step] += 1;
            }
        }
    }

    let mut steps = Vec::new();
    let mut mean_log_divergence = Vec::new();
    for step in 0..MAX_ITER {
        if divergence_counts[step] >= 5 {
            steps.push(step as f64);
            mean_log_divergence.push(divergence_sums[step] / divergence_counts[step] as f64);
        }
    }

    if steps.len() < 5 {
        return invalid();
    }

    let fit_len = steps.len().min(15);
    let (slope, _intercept, r_squared) =
        math::linear_regression(&steps[..fit_len], &mean_log_divergence[..fit_len]);
    if !slope.is_finite() {
        return invalid();
    }

    LyapunovResult {
        lyapunov_exponent: slope,
        divergence_curve: mean_log_divergence,
        embedding_dim: EMBEDDING_DIM,
        delay: TAU,
        r_squared,
        is_valid: true,
    }
}

pub fn correlation_dimension(data: &[u8]) -> CorrelationDimResult {
    const MAX_EMBEDDING: usize = 8;
    const SUBSAMPLE_MAX: usize = 500;
    const NUM_RADII: usize = 15;

    if data.len() < 1000 {
        return CorrelationDimResult {
            dimension: f64::NAN,
            r_squared: 0.0,
            embedding_dim_used: 0,
            num_points_used: 0,
            is_valid: false,
        };
    }

    let data_f64: Vec<f64> = data.iter().map(|&b| b as f64).collect();
    let subsampled: Vec<f64> = if data_f64.len() > SUBSAMPLE_MAX {
        let step = (data_f64.len() / SUBSAMPLE_MAX).max(1);
        data_f64
            .iter()
            .step_by(step)
            .take(SUBSAMPLE_MAX)
            .copied()
            .collect()
    } else {
        data_f64
    };

    let mut accepted = Vec::new();

    for m in 2..=MAX_EMBEDDING {
        let embedded = time_delay_embedding(&subsampled, m, 1);
        if embedded.len() < 10 {
            continue;
        }

        let n = embedded.len();
        let total_pairs = n * (n - 1) / 2;
        if total_pairs == 0 {
            continue;
        }

        let mut distances = Vec::with_capacity(total_pairs);
        for i in 0..n {
            for j in (i + 1)..n {
                distances.push(math::euclidean_distance(&embedded[i], &embedded[j]));
            }
        }

        distances.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        if distances.len() < 100 {
            continue;
        }

        let r_min = distances[distances.len() / 100];
        let r_max = distances[distances.len() / 2];
        if r_min <= 0.0 || r_max <= r_min {
            continue;
        }

        let mut log_r = Vec::with_capacity(NUM_RADII);
        let mut log_c = Vec::with_capacity(NUM_RADII);
        let ratio = r_max / r_min;

        for k in 0..NUM_RADII {
            let r = r_min * ratio.powf(k as f64 / (NUM_RADII - 1) as f64);
            let count = distances.partition_point(|&d| d < r);
            let c = count as f64 / total_pairs as f64;
            if c > 0.0 {
                log_r.push(r.ln());
                log_c.push(c.ln());
            }
        }

        if log_r.len() < 2 {
            continue;
        }

        let (slope, _intercept, r_squared) = math::linear_regression(&log_r, &log_c);
        if r_squared > 0.9 && slope.is_finite() && slope > 0.0 {
            accepted.push((m, slope, r_squared));
        }
    }

    if accepted.is_empty() {
        return CorrelationDimResult {
            dimension: f64::NAN,
            r_squared: 0.0,
            embedding_dim_used: 0,
            num_points_used: subsampled.len(),
            is_valid: false,
        };
    }

    let take_n = accepted.len().min(3);
    let avg_d2 = accepted[accepted.len() - take_n..]
        .iter()
        .map(|(_, slope, _)| *slope)
        .sum::<f64>()
        / take_n as f64;

    let (last_m, _last_slope, last_r2) = accepted[accepted.len() - 1];

    CorrelationDimResult {
        dimension: avg_d2,
        r_squared: last_r2,
        embedding_dim_used: last_m,
        num_points_used: subsampled.len(),
        is_valid: true,
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct RqaResult {
    pub recurrence_rate: f64,
    pub determinism: f64,
    pub laminarity: f64,
    pub avg_diagonal_length: f64,
    pub max_diagonal_length: usize,
    pub embedding_dim: usize,
    pub delay: usize,
    pub threshold: f64,
    pub num_points_used: usize,
    pub is_valid: bool,
}

pub fn rqa(data: &[u8], embedding_dim: usize, delay: usize, threshold: f64) -> RqaResult {
    const SUBSAMPLE_MAX: usize = 1000;

    let invalid = || RqaResult {
        recurrence_rate: 0.0,
        determinism: 0.0,
        laminarity: 0.0,
        avg_diagonal_length: 0.0,
        max_diagonal_length: 0,
        embedding_dim,
        delay,
        threshold,
        num_points_used: 0,
        is_valid: false,
    };

    if data.is_empty()
        || embedding_dim == 0
        || delay == 0
        || !threshold.is_finite()
        || threshold <= 0.0
    {
        return invalid();
    }

    let data_f64: Vec<f64> = data.iter().map(|&b| b as f64).collect();
    let embedded = time_delay_embedding(&data_f64, embedding_dim, delay);
    if embedded.len() < 2 {
        return invalid();
    }

    let vectors: Vec<Vec<f64>> = if embedded.len() > SUBSAMPLE_MAX {
        let step = (embedded.len() / SUBSAMPLE_MAX).max(1);
        embedded
            .iter()
            .step_by(step)
            .take(SUBSAMPLE_MAX)
            .cloned()
            .collect()
    } else {
        embedded
    };

    let n = vectors.len();
    if n < 2 {
        return invalid();
    }

    let mut total_recurrence_points = 0usize;
    let mut diag_points_in_lines = 0usize;
    let mut diag_line_count = 0usize;
    let mut max_diagonal_length = 0usize;
    let mut vertical_points_in_lines = 0usize;

    let mut prev_diag_lengths = vec![0usize; n + 1];
    let mut curr_diag_lengths = vec![0usize; n + 1];
    let mut vertical_lengths = vec![0usize; n];

    for i in 0..n {
        curr_diag_lengths[0] = 0;

        for j in 0..n {
            let recurrence =
                i != j && math::euclidean_distance(&vectors[i], &vectors[j]) < threshold;

            if recurrence {
                total_recurrence_points += 1;

                let diag_len = prev_diag_lengths[j] + 1;
                curr_diag_lengths[j + 1] = diag_len;
                if diag_len == 2 {
                    diag_points_in_lines += 2;
                    diag_line_count += 1;
                    max_diagonal_length = max_diagonal_length.max(2);
                } else if diag_len > 2 {
                    diag_points_in_lines += 1;
                    max_diagonal_length = max_diagonal_length.max(diag_len);
                }

                let vert_len = vertical_lengths[j] + 1;
                vertical_lengths[j] = vert_len;
                if vert_len == 2 {
                    vertical_points_in_lines += 2;
                } else if vert_len > 2 {
                    vertical_points_in_lines += 1;
                }
            } else {
                curr_diag_lengths[j + 1] = 0;
                vertical_lengths[j] = 0;
            }
        }

        std::mem::swap(&mut prev_diag_lengths, &mut curr_diag_lengths);
    }

    let total_pairs = n * (n - 1);
    let recurrence_rate = if total_pairs > 0 {
        total_recurrence_points as f64 / total_pairs as f64
    } else {
        0.0
    };

    let determinism = if total_recurrence_points > 0 {
        diag_points_in_lines as f64 / total_recurrence_points as f64
    } else {
        0.0
    };

    let laminarity = if total_recurrence_points > 0 {
        vertical_points_in_lines as f64 / total_recurrence_points as f64
    } else {
        0.0
    };

    let avg_diagonal_length = if diag_line_count > 0 {
        diag_points_in_lines as f64 / diag_line_count as f64
    } else {
        0.0
    };

    RqaResult {
        recurrence_rate,
        determinism,
        laminarity,
        avg_diagonal_length,
        max_diagonal_length,
        embedding_dim,
        delay,
        threshold,
        num_points_used: n,
        is_valid: true,
    }
}

pub fn rqa_default(data: &[u8]) -> RqaResult {
    rqa(data, 3, 1, 10.0)
}

/// Aggregated chaos theory analysis results for a data stream.
#[derive(Debug, Clone, Serialize)]
pub struct ChaosAnalysis {
    pub hurst: HurstResult,
    pub lyapunov: LyapunovResult,
    pub correlation_dimension: CorrelationDimResult,
    pub bientropy: BiEntropyResult,
    pub epiplexity: EpiplexityResult,
    pub sample_entropy: SampleEntropyResult,
    pub dfa: DfaResult,
    pub rqa: RqaResult,
    pub bootstrap_hurst: BootstrapHurstResult,
    pub rolling_hurst: RollingHurstResult,
}

/// Run all 5 chaos theory analysis methods on the given data.
///
/// This is a separate entry point from `full_analysis()` - chaos methods are
/// computationally expensive (O(n^2)) and should only be run when explicitly requested.
pub fn chaos_analysis(data: &[u8]) -> ChaosAnalysis {
    ChaosAnalysis {
        hurst: hurst_exponent(data),
        lyapunov: lyapunov_exponent(data),
        correlation_dimension: correlation_dimension(data),
        bientropy: bientropy(data),
        epiplexity: epiplexity(data),
        sample_entropy: sample_entropy_default(data),
        dfa: dfa_default(data),
        rqa: rqa_default(data),
        bootstrap_hurst: bootstrap_hurst_default(data),
        rolling_hurst: rolling_hurst_default(data),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn random_data_seeded(n: usize, seed: u64) -> Vec<u8> {
        let mut data = Vec::with_capacity(n);
        let mut state: u64 = seed;
        for _ in 0..n {
            state = state
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            data.push((state >> 33) as u8);
        }
        data
    }

    #[test]
    fn test_hurst_random() {
        let data = random_data_seeded(10000, 0xdeadbeef);
        let result = hurst_exponent(&data);

        assert!(result.is_valid);
        assert!(result.hurst_exponent >= 0.35 && result.hurst_exponent <= 0.65);
    }

    #[test]
    fn test_hurst_constant() {
        let data = vec![42u8; 1000];
        let result = hurst_exponent(&data);

        assert!(!result.is_valid);
    }

    #[test]
    fn test_rolling_hurst_random_seeded() {
        let data = random_data_seeded(10000, 0xdeadbeef);
        let result = rolling_hurst(&data, 1000, 100);

        assert!(result.is_valid);
        assert_eq!(result.windows.len(), 91);
        for window in &result.windows {
            assert!(window.is_valid);
            assert!(window.hurst >= 0.3 && window.hurst <= 0.7);
        }
    }

    #[test]
    fn test_rolling_hurst_data_too_short() {
        let data = random_data_seeded(500, 0x12345678);
        let result = rolling_hurst(&data, 1000, 100);

        assert!(!result.is_valid);
        assert!(result.windows.is_empty());
    }

    #[test]
    fn test_rolling_hurst_empty_invalid() {
        let data: &[u8] = &[];
        let result = rolling_hurst_default(data);

        assert!(!result.is_valid);
        assert!(result.windows.is_empty());
    }

    #[test]
    fn test_rolling_hurst_mean_consistency() {
        let data = random_data_seeded(10000, 0xcafebabe);
        let result = rolling_hurst_default(&data);
        assert!(result.is_valid);

        let valid_hursts: Vec<f64> = result
            .windows
            .iter()
            .filter(|w| w.is_valid)
            .map(|w| w.hurst)
            .collect();

        let expected_mean = valid_hursts.iter().sum::<f64>() / valid_hursts.len() as f64;
        assert!((result.mean_hurst - expected_mean).abs() < 1e-12);
    }

    #[test]
    fn test_bootstrap_hurst_random_deterministic() {
        let data = random_data_seeded(5000, 0xdeadbeef);
        let result_a = bootstrap_hurst(&data, 200);
        let result_b = bootstrap_hurst(&data, 200);

        assert!(result_a.is_valid);
        assert_eq!(result_a.n_surrogates, 200);
        assert_eq!(result_a.p_value, result_b.p_value);
        assert_eq!(result_a.mean_surrogate_hurst, result_b.mean_surrogate_hurst);
        assert_eq!(result_a.ci_lower, result_b.ci_lower);
        assert_eq!(result_a.ci_upper, result_b.ci_upper);
    }

    #[test]
    fn test_bootstrap_hurst_correlated_significant() {
        let data: Vec<u8> = (0..5000).map(|i| (i / 20) as u8).collect();
        let result = bootstrap_hurst(&data, 200);

        assert!(result.is_valid);
        assert!(result.is_significant);
    }

    #[test]
    fn test_bootstrap_hurst_empty_invalid() {
        let result = bootstrap_hurst_default(&[]);

        assert!(!result.is_valid);
        assert_eq!(result.n_surrogates, 0);
    }

    #[test]
    fn test_bootstrap_hurst_too_short_invalid() {
        let data = random_data_seeded(50, 0x12345678);
        let result = bootstrap_hurst_default(&data);

        assert!(!result.is_valid);
        assert_eq!(result.n_surrogates, 0);
    }

    #[test]
    fn test_dfa_random() {
        let data = random_data_seeded(10000, 0xdeadbeef);
        let result = dfa(&data, 1);

        assert!(result.is_valid);
        assert!(result.alpha >= 0.3 && result.alpha <= 0.7);
    }

    #[test]
    fn test_dfa_constant() {
        let data = vec![42u8; 10000];
        let result = dfa_default(&data);

        assert!(!result.is_valid);
    }

    #[test]
    fn test_dfa_empty() {
        let data: &[u8] = &[];
        let result = dfa_default(data);

        assert!(!result.is_valid);
        assert!(result.fluctuations.is_empty());
    }

    #[test]
    fn test_dfa_too_short() {
        let data = random_data_seeded(20, 0x12345678);
        let result = dfa_default(&data);

        assert!(!result.is_valid);
    }

    #[test]
    fn test_bientropy_random() {
        let data = random_data_seeded(10000, 0xdeadbeef);
        let result = bientropy(&data);

        assert!(result.is_valid);
        assert!(result.bien > 0.90);
        assert!(result.tbien > 0.90);
    }

    #[test]
    fn test_bientropy_constant() {
        let data = vec![0u8; 1000];
        let result = bientropy(&data);

        assert!(result.is_valid);
        assert!(result.bien < 0.05);
    }

    #[test]
    fn test_bientropy_alternating() {
        let data = vec![0xAAu8; 1000];
        let result = bientropy(&data);

        assert!(result.is_valid);
        assert!(result.bien < 0.50);
    }

    #[test]
    fn test_epiplexity_random() {
        let data = random_data_seeded(10000, 0xdeadbeef);
        let result = epiplexity(&data);

        assert!(result.is_valid);
        assert!(
            result.compression_ratio > 0.95,
            "Random data should have high compression ratio (low compressibility)"
        );
    }

    #[test]
    fn test_epiplexity_patterned() {
        // Repeating [0, 1, 2, 3] pattern — highly compressible
        let data: Vec<u8> = (0..10000u32).map(|i| (i % 4) as u8).collect();
        let result = epiplexity(&data);

        assert!(result.is_valid);
        assert!(
            result.compression_ratio < 0.10,
            "Patterned data should have low compression ratio (high compressibility)"
        );
    }

    #[test]
    fn test_lyapunov_random() {
        let data = random_data_seeded(10000, 0xdeadbeef);
        let result = lyapunov_exponent(&data);

        assert!(result.is_valid);
        assert!(result.lyapunov_exponent.abs() < 0.3);
    }

    #[test]
    fn test_lyapunov_logistic() {
        let mut x = 0.1f64;
        let mut data = Vec::with_capacity(5000);
        for _ in 0..5000 {
            x = 4.0 * x * (1.0 - x);
            data.push((x * 255.0) as u8);
        }

        let result = lyapunov_exponent(&data);
        assert!(result.is_valid);
        assert!(
            result.lyapunov_exponent > 0.3,
            "expected logistic Lyapunov > 0.3, got {}",
            result.lyapunov_exponent
        );
    }

    #[test]
    fn test_lyapunov_short() {
        let data = random_data_seeded(100, 0x12345678);
        let result = lyapunov_exponent(&data);

        assert!(!result.is_valid);
    }

    #[test]
    fn test_corrdim_random() {
        let data = random_data_seeded(5000, 0xdeadbeef);
        let result = correlation_dimension(&data);
        assert!(
            result.is_valid,
            "correlation_dimension should be valid for 5000 random bytes"
        );
        assert!(
            result.dimension > 2.0,
            "random data should have D2 > 2.0, got {}",
            result.dimension
        );
    }

    #[test]
    fn test_corrdim_short() {
        let data = random_data_seeded(500, 0x12345678);
        let result = correlation_dimension(&data);
        assert!(
            !result.is_valid,
            "correlation_dimension should be invalid for 500 bytes (below 1000 minimum)"
        );
    }

    #[test]
    fn test_rqa_random_low_recurrence() {
        let data = random_data_seeded(5000, 0xdeadbeef);
        let result = rqa_default(&data);

        assert!(result.is_valid);
        assert!(
            result.recurrence_rate < 0.1,
            "random data should have low RR, got {}",
            result.recurrence_rate
        );
    }

    #[test]
    fn test_rqa_periodic_high_recurrence_and_determinism() {
        let data: Vec<u8> = (0..5000).map(|i| (i % 10) as u8).collect();
        let result = rqa_default(&data);

        assert!(result.is_valid);
        assert!(
            result.recurrence_rate > 0.2,
            "periodic data should have high RR, got {}",
            result.recurrence_rate
        );
        assert!(
            result.determinism > 0.8,
            "periodic data should have high DET, got {}",
            result.determinism
        );
    }

    #[test]
    fn test_rqa_constant_near_full_recurrence() {
        let data = vec![42u8; 5000];
        let result = rqa_default(&data);

        assert!(result.is_valid);
        assert!(
            result.recurrence_rate > 0.99,
            "constant data should have RR near 1.0, got {}",
            result.recurrence_rate
        );
    }

    #[test]
    fn test_rqa_empty_invalid() {
        let data: &[u8] = &[];
        let result = rqa_default(data);

        assert!(!result.is_valid);
    }

    #[test]
    fn test_sample_entropy_random() {
        let data = random_data_seeded(5000, 0xdeadbeef);
        let result = sample_entropy_default(&data);
        assert!(result.is_valid);
        assert!(result.sample_entropy > 0.0);
    }

    #[test]
    fn test_sample_entropy_periodic_lower_than_random() {
        let random = random_data_seeded(5000, 0xdeadbeef);
        let periodic: Vec<u8> = (0..5000).map(|i| (i % 4) as u8).collect();

        let random_result = sample_entropy_default(&random);
        let periodic_result = sample_entropy_default(&periodic);

        assert!(random_result.is_valid);
        assert!(periodic_result.is_valid);
        assert!(periodic_result.sample_entropy < random_result.sample_entropy);
    }

    #[test]
    fn test_sample_entropy_constant_invalid() {
        let data = vec![42u8; 5000];
        let result = sample_entropy_default(&data);
        assert!(!result.is_valid);
    }

    #[test]
    fn test_sample_entropy_empty_invalid() {
        let data: &[u8] = &[];
        let result = sample_entropy_default(data);
        assert!(!result.is_valid);
    }

    #[test]
    fn test_sample_entropy_single_byte_invalid() {
        let data: &[u8] = &[42];
        let result = sample_entropy_default(data);
        assert!(!result.is_valid);
    }

    #[test]
    fn test_empty_input() {
        let data: &[u8] = &[];
        let h = hurst_exponent(data);
        let l = lyapunov_exponent(data);
        let c = correlation_dimension(data);
        let b = bientropy(data);
        let e = epiplexity(data);
        assert!(!h.is_valid);
        assert!(!l.is_valid);
        assert!(!c.is_valid);
        assert!(!b.is_valid);
        assert!(!e.is_valid);
    }

    #[test]
    fn test_single_byte() {
        let data: &[u8] = &[42];
        let h = hurst_exponent(data);
        let l = lyapunov_exponent(data);
        let c = correlation_dimension(data);
        let e = epiplexity(data);
        assert!(!h.is_valid);
        assert!(!l.is_valid);
        assert!(!c.is_valid);
        assert!(!e.is_valid);
    }

    #[test]
    fn test_chaos_analysis_orchestrator() {
        let data = random_data_seeded(5000, 0xdeadbeef);
        let result = chaos_analysis(&data);
        assert!(result.hurst.is_valid);
        assert!(result.lyapunov.is_valid);
        assert!(result.correlation_dimension.is_valid);
        assert!(result.bientropy.is_valid);
        assert!(result.epiplexity.is_valid);
        assert!(result.sample_entropy.is_valid);
        assert!(result.dfa.is_valid);
        assert!(result.rqa.is_valid);
        assert!(result.bootstrap_hurst.is_valid);
        assert!(result.rolling_hurst.is_valid);
    }

    #[test]
    fn test_chaos_analysis_serializes() {
        let data = random_data_seeded(5000, 0xdeadbeef);
        let result = chaos_analysis(&data);
        let json = serde_json::to_string(&result).expect("serialization failed");
        assert!(json.contains("sample_entropy"));
        assert!(json.contains("dfa"));
    }

    #[test]
    fn test_performance_100k() {
        let data = random_data_seeded(100_000, 0xCAFEBABE);
        let _result = chaos_analysis(&data);
    }
}