qssh 0.5.0

Post-quantum secure shell with NIST PQC algorithms (Falcon, SPHINCS+, ML-KEM), configurable security tiers, and quantum-resistant protocol design
Documentation
// Tao Signal Integration for QSSH
// Cherry-picked from Drista's blockchain monitoring

use serde::{Serialize, Deserialize};
use tokio::sync::mpsc;
use std::sync::Arc;

/// Tao signals from quantum blockchain
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TaoSignal {
    /// Quantum coherence measurement
    Coherence {
        timestamp: u64,
        coherence_time: f64, // microseconds
        fidelity: f64, // 0.0 to 1.0
        qubit_count: u32,
    },
    
    /// Entanglement event
    Entanglement {
        timestamp: u64,
        bell_state: String,
        correlation: f64,
        distance: f64, // km
    },
    
    /// Quantum phase transition
    PhaseTransition {
        timestamp: u64,
        old_phase: String,
        new_phase: String,
        order_parameter: f64,
    },
    
    /// Harmonic resonance detection
    HarmonicResonance {
        timestamp: u64,
        frequency: f64, // Hz
        amplitude: f64,
        q_factor: f64,
        harmonics: Vec<f64>,
    },
    
    /// Quantum error syndrome
    ErrorSyndrome {
        timestamp: u64,
        error_type: String,
        syndrome_bits: Vec<u8>,
        correction_applied: bool,
    },
}

/// Tao signal monitor for QSSH
pub struct TaoMonitor {
    signals: mpsc::Sender<TaoSignal>,
    receiver: Arc<tokio::sync::Mutex<mpsc::Receiver<TaoSignal>>>,
}

impl TaoMonitor {
    pub fn new() -> Self {
        let (tx, rx) = mpsc::channel(1000);
        Self {
            signals: tx,
            receiver: Arc::new(tokio::sync::Mutex::new(rx)),
        }
    }
    
    /// Emit a Tao signal
    pub async fn emit(&self, signal: TaoSignal) {
        if let Err(e) = self.signals.send(signal).await {
            log::error!("Failed to emit Tao signal: {}", e);
        }
    }
    
    /// Subscribe to Tao signals
    pub async fn subscribe(&self) -> TaoSignalStream {
        TaoSignalStream {
            receiver: self.receiver.clone(),
        }
    }
    
    /// Process quantum measurement into Tao signal
    pub async fn process_measurement(&self, measurement: QuantumMeasurement) {
        let signal = match measurement {
            QuantumMeasurement::Coherence { t2_star, fidelity, qubits } => {
                TaoSignal::Coherence {
                    timestamp: current_timestamp(),
                    coherence_time: t2_star,
                    fidelity,
                    qubit_count: qubits,
                }
            },
            QuantumMeasurement::BellState { state, correlation } => {
                TaoSignal::Entanglement {
                    timestamp: current_timestamp(),
                    bell_state: state,
                    correlation,
                    distance: 0.0, // Would come from hardware
                }
            },
            QuantumMeasurement::PhaseTransition { from, to, parameter } => {
                TaoSignal::PhaseTransition {
                    timestamp: current_timestamp(),
                    old_phase: from,
                    new_phase: to,
                    order_parameter: parameter,
                }
            }
        };
        
        self.emit(signal).await;
    }
}

/// Stream of Tao signals
pub struct TaoSignalStream {
    receiver: Arc<tokio::sync::Mutex<mpsc::Receiver<TaoSignal>>>,
}

impl TaoSignalStream {
    pub async fn next(&mut self) -> Option<TaoSignal> {
        let mut rx = self.receiver.lock().await;
        rx.recv().await
    }
}

/// Quantum measurements that generate Tao signals
pub enum QuantumMeasurement {
    Coherence {
        t2_star: f64,
        fidelity: f64,
        qubits: u32,
    },
    BellState {
        state: String,
        correlation: f64,
    },
    PhaseTransition {
        from: String,
        to: String,
        parameter: f64,
    },
}

/// Integration with QSSH protocol
impl super::QsshSession {
    /// Monitor Tao signals during session
    pub async fn monitor_tao_signals(&mut self, monitor: Arc<TaoMonitor>) {
        // Subscribe to signals
        let mut stream = monitor.subscribe().await;
        
        tokio::spawn(async move {
            while let Some(signal) = stream.next().await {
                match signal {
                    TaoSignal::Coherence { coherence_time, fidelity, .. } => {
                        if fidelity < 0.9 || coherence_time < 10.0 {
                            log::warn!("Low quantum coherence detected: {}μs @ {:.2}% fidelity", 
                                     coherence_time, fidelity * 100.0);
                        }
                    },
                    TaoSignal::ErrorSyndrome { error_type, correction_applied, .. } => {
                        if !correction_applied {
                            log::error!("Uncorrected quantum error: {}", error_type);
                        }
                    },
                    TaoSignal::HarmonicResonance { frequency, amplitude, .. } => {
                        log::info!("Harmonic resonance at {}Hz (amplitude: {})", 
                                 frequency, amplitude);
                    },
                    _ => {}
                }
            }
        });
    }
}

/// Harmonic frequency analyzer
pub struct HarmonicAnalyzer {
    sample_rate: f64,
    window_size: usize,
}

impl HarmonicAnalyzer {
    pub fn new(sample_rate: f64) -> Self {
        Self {
            sample_rate,
            window_size: 1024,
        }
    }
    
    /// Analyze quantum signal for harmonics
    pub fn analyze(&self, signal: &[f64]) -> Vec<f64> {
        // FFT to find frequency components
        // In production, use rustfft or similar
        let fundamental = self.find_fundamental(signal);
        
        // Find harmonics
        let mut harmonics = vec![fundamental];
        for n in 2..=8 {
            harmonics.push(fundamental * n as f64);
        }
        
        harmonics
    }
    
    fn find_fundamental(&self, signal: &[f64]) -> f64 {
        // Simplified - find dominant frequency
        // In production, use proper FFT
        440.0 // A4 note as placeholder
    }
}

fn current_timestamp() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs()
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[tokio::test]
    async fn test_tao_monitor() {
        let monitor = TaoMonitor::new();
        
        // Emit test signal
        monitor.emit(TaoSignal::Coherence {
            timestamp: current_timestamp(),
            coherence_time: 50.0,
            fidelity: 0.95,
            qubit_count: 5,
        }).await;
        
        // Subscribe and receive
        let mut stream = monitor.subscribe().await;
        let signal = stream.next().await;
        
        assert!(matches!(signal, Some(TaoSignal::Coherence { .. })));
    }
}