init
This commit is contained in:
commit
10ff2123ac
22 changed files with 1430 additions and 0 deletions
41
src/uniformity/chi_byte.rs
Normal file
41
src/uniformity/chi_byte.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use std::collections::HashMap;
|
||||
use statrs::distribution::{ChiSquared, ContinuousCDF};
|
||||
|
||||
use crate::Onod;
|
||||
|
||||
impl Onod {
|
||||
|
||||
/// ChiByte randomness test
|
||||
/// Evaluates the uniformity of byte values across the data and returns a p-value.
|
||||
pub fn chi_byte(samples: &[u8]) -> f64 {
|
||||
|
||||
if samples.is_empty() {
|
||||
return 0.0; // Default to perfect randomness for empty data
|
||||
}
|
||||
|
||||
// Count occurrences of each byte value (0-255)
|
||||
let mut counts = HashMap::new();
|
||||
for &byte in samples {
|
||||
*counts.entry(byte).or_insert(0) += 1;
|
||||
}
|
||||
|
||||
// Calculate expected count assuming uniform distribution
|
||||
let expected_count = samples.len() as f64 / 256.0;
|
||||
|
||||
// Calculate chi-squared statistic
|
||||
let mut chi_squared_stat = 0.0;
|
||||
for i in 0..256 {
|
||||
let observed = *counts.get(&(i as u8)).unwrap_or(&0) as f64;
|
||||
let diff = observed - expected_count;
|
||||
chi_squared_stat += (diff * diff) / expected_count;
|
||||
}
|
||||
|
||||
// Use chi-squared distribution to calculate p-value
|
||||
let degrees_of_freedom = 256.0 - 1.0; // 256 possible byte values - 1
|
||||
let chi_squared_dist = ChiSquared::new(degrees_of_freedom).expect("Failed to create ChiSquared distribution");
|
||||
let p_value = 1.0 - chi_squared_dist.cdf(chi_squared_stat);
|
||||
|
||||
p_value
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue