snapkitty
cryptography
python
marlborg-worm / src /strain_monitor.rs
SNAPKITTYWEST's picture
push from SNAPKITTYWEST/marlborg-worm
75619b0 verified
Raw History Blame Contribute Delete
4.81 kB
//
// Copyright (c) 2026 BEL ESPRIT D ACCORD TRUST HOLDINGS INC
// All rights reserved.
use std::time::{Instant, Duration};
use std::sync::RwLock;
/// Cognitive strain monitor implementing the ICP entropy model.
/// Tracks system entropy against the HyperKitty bound (H <= 0.20 nats)
/// and provides real-time strain assessment.
pub struct CognitiveStrainMonitor {
h_safe: f64,
scaling_factor: u64,
h_base: f64,
update_interval: Duration,
state: RwLock<StrainState>,
}
#[derive(Debug, Clone, Copy)]
struct StrainState {
h_trunc: f64,
h_hash_removed: bool,
delta_rules: u64,
h_cog: f64,
icp: f64,
strain: f64,
timestamp: Instant,
}
impl CognitiveStrainMonitor {
pub fn new() -> Self {
Self {
h_safe: 0.20,
scaling_factor: 10_000,
h_base: 0.10,
update_interval: Duration::from_millis(100),
state: RwLock::new(StrainState {
h_trunc: 0.0,
h_hash_removed: false,
delta_rules: 0,
h_cog: 0.10,
icp: 0.0,
strain: 0.0,
timestamp: Instant::now(),
}),
}
}
pub fn update_truncation(&self, ops: u64) {
let epsilon = 168.0 / 10_088_352.0; // ~1.665e-5 nats/operation
let mut state = self.state.write().unwrap();
state.h_trunc += ops as f64 * epsilon;
state.timestamp = Instant::now();
}
pub fn set_hash_status(&self, removed: bool) {
let mut state = self.state.write().unwrap();
state.h_hash_removed = removed;
state.timestamp = Instant::now();
}
pub fn update_marlborg_growth(&self, new_rules: u64) {
let mut state = self.state.write().unwrap();
state.delta_rules += new_rules;
state.timestamp = Instant::now();
}
pub fn compute_strain(&self) -> f64 {
let mut state = self.state.write().unwrap();
let h_hash = if state.h_hash_removed { 0.04 } else { 0.0 };
let h_marlborg = state.delta_rules as f64 * 0.0005;
state.h_cog = self.h_base + state.h_trunc + h_hash + h_marlborg;
state.icp = if state.h_cog > self.h_safe { state.h_cog - self.h_safe } else { 0.0 };
state.strain = if self.h_safe > 0.0 { state.icp / self.h_safe } else { 0.0 };
state.timestamp = Instant::now();
state.strain
}
pub fn get_entropy_fixed(&self) -> u64 {
let state = self.state.read().unwrap();
(state.h_cog * self.scaling_factor as f64) as u64
}
pub fn get_s_bh_fixed(&self) -> u64 {
(self.h_safe * self.scaling_factor as f64) as u64
}
pub fn is_overflow(&self) -> bool {
self.get_entropy_fixed() > self.get_s_bh_fixed()
}
pub fn is_critical(&self, threshold: f64) -> bool {
self.compute_strain() > threshold
}
pub fn get_status(&self) -> String {
let strain = self.compute_strain() * 100.0;
match strain {
s if s < 50.0 => format!("HEALTHY: {:.1}% strain", s),
s if s < 70.0 => format!("WARNING: {:.1}% strain", s),
s if s < 85.0 => format!("HIGH: {:.1}% strain", s),
_ => format!("CRITICAL: {:.1}% strain", strain),
}
}
/// Compute max safe operations before entropy exceeds bound
pub fn max_safe_ops(&self, delta_rules: u64) -> u64 {
let epsilon = 168.0 / 10_088_352.0;
let h_hash = if self.state.read().unwrap().h_hash_removed { 0.04 } else { 0.0 };
let h_marlborg = delta_rules as f64 * 0.0005;
let remaining = self.h_safe - self.h_base - h_hash - h_marlborg;
if remaining <= 0.0 {
return 0;
}
(remaining / epsilon) as u64
}
}
fn main() {
let monitor = CognitiveStrainMonitor::new();
println!("=== Marlborg-Wormhole Strain Monitor ===");
println!("S_BH (fixed): {}", monitor.get_s_bh_fixed());
println!("Initial: {}", monitor.get_status());
println!("Max safe ops (0 rules): {}", monitor.max_safe_ops(0));
println!("Max safe ops (100 rules): {}", monitor.max_safe_ops(100));
println!("Max safe ops (440 rules): {}", monitor.max_safe_ops(440));
// Simulate normal operation
for _ in 0..100 {
monitor.update_truncation(120);
}
println!("\nAfter 12K ops: {}", monitor.get_status());
println!("Overflow: {}", monitor.is_overflow());
// Simulate attack
monitor.set_hash_status(true);
monitor.update_marlborg_growth(50);
println!("\nAfter hash removal + 50 rules: {}", monitor.get_status());
println!("Overflow: {}", monitor.is_overflow());
}