From 8b277ff53bc81b59fdf41ec3aed29e37ab96a118 Mon Sep 17 00:00:00 2001 From: randogoth Date: Sun, 5 Jan 2025 15:37:42 +0200 Subject: [PATCH] sequential & bandpass --- src/bin/main.rs | 17 +---------------- src/lib.rs | 46 +++++----------------------------------------- 2 files changed, 6 insertions(+), 57 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 3668ad4..5efefcd 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -28,7 +28,6 @@ fn main() -> Result<(), Box> { } let quick = args.contains(&String::from("-q")); - let pp = args.contains(&String::from("-p")); let camera_index: usize = args[1].parse().expect("Failed to parse camera index as a number"); let length: usize = args[2].parse().expect("Failed to parse entropy length as a number"); @@ -72,21 +71,7 @@ fn main() -> Result<(), Box> { let grayscale_data = frame_to_grayscale(&data); - let entropy: Vec = if pp { - let mut low = 10; - let mut high = 245; - - if ocelli.is_covered(&grayscale_data, 50) { - low = 3; - high = 252; - } - - if let Some(last) = last_frame { - ocelli.whiten(&ocelli.tune_and_prune(&grayscale_data, &last, low, high)) - } else { - Vec::new() - } - } else if quick { + let entropy: Vec = if quick { ocelli.whiten(&ocelli.pick_and_flip(&grayscale_data, frame_count as usize)) } else { if ocelli.is_covered(&grayscale_data, 50) { diff --git a/src/lib.rs b/src/lib.rs index e328e9a..3fb6d22 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,7 +50,7 @@ impl Ocelli { } - pub fn pick_and_flip(&self, data: &[u8], current_frame_index: usize) -> Vec { + pub fn pick_and_flip(&self, data: &[u8], low: u8, high: u8, current_frame_index: usize) -> Vec { // Extracts the least significant bit (LSB) of each pixel brightness, flipping it based on the frame index. // Generates entropy by combining these bits into bytes. // Algorithm is a simplified version of R. Li, "A True Random Number Generator algorithm from @@ -59,7 +59,7 @@ impl Ocelli { let mut entropy = Vec::new(); for &pixel in data { - if (2..=253).contains(&pixel) { // filter bias + if (low..=high).contains(&pixel) { // filter bias let mut lsb = pixel & 1; if current_frame_index % 2 == 0 { // flip bits lsb ^= 1; @@ -71,20 +71,6 @@ impl Ocelli { self.bits_to_bytes(&entropy) } - pub fn tune_and_prune(&self, current: &Vec, previous: &Vec, low: u8, high: u8) -> Vec { - // Extracts the least significant bit (LSB) of each pixel brightness while filtering out bias - // It's a combination of methods from the other two entropy extraction algorithms - let mut entropy = Vec::new(); - - for (&c, &p) in current.iter().zip(previous.iter()) { - if c != p && (low..=high).contains(&c) { - entropy.push(c & 1); - } - } - - self.bits_to_bytes(&entropy) - } - pub fn shannon(&self, data: &Vec) -> f64 { // Calculates the Shannon entropy of a given byte vector to measure its randomness. // Uses a frequency map to compute probabilities and their contributions to entropy. @@ -178,6 +164,8 @@ pub extern "C" fn chop_and_tack( pub extern "C" fn pick_and_flip( data_ptr: *const u8, data_len: usize, + low: u8, + high: u8, current_frame_index: usize, result_ptr: *mut u8, result_len: &mut usize, @@ -185,31 +173,7 @@ pub extern "C" fn pick_and_flip( let data = unsafe { slice::from_raw_parts(data_ptr, data_len) }; let ocelli = Ocelli; - let result = ocelli.pick_and_flip(data, current_frame_index); - - unsafe { - let result_slice = slice::from_raw_parts_mut(result_ptr, result.len()); - result_slice.copy_from_slice(&result); - *result_len = result.len(); - } -} - -#[no_mangle] -pub extern "C" fn tune_and_prune( - current_ptr: *const u8, - current_len: usize, - previous_ptr: *const u8, - previous_len: usize, - low: u8, - high: u8, - result_ptr: *mut u8, - result_len: &mut usize, -) { - let current = unsafe { slice::from_raw_parts(current_ptr, current_len) }; - let previous = unsafe { slice::from_raw_parts(previous_ptr, previous_len) }; - - let ocelli = Ocelli; - let result = ocelli.tune_and_prune(¤t.to_vec(), &previous.to_vec(), low, high); + let result = ocelli.pick_and_flip(data, low, high, current_frame_index); unsafe { let result_slice = slice::from_raw_parts_mut(result_ptr, result.len());