voirs-evaluation 0.1.0-rc.1

Quality evaluation and assessment framework for VoiRS
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
//! Privacy-Preserving Evaluation Framework
//!
//! Secure evaluation of speech synthesis quality while protecting sensitive audio data
//! using differential privacy, homomorphic encryption, and secure multi-party computation.
//!
//! # Features
//!
//! - **Differential Privacy**: Add calibrated noise to evaluation metrics to protect individual samples
//! - **Secure Aggregation**: Compute aggregate metrics without exposing individual results
//! - **Data Anonymization**: Remove personally identifiable information from audio metadata
//! - **Federated Evaluation**: Evaluate models across distributed datasets without data sharing
//! - **Audit Logging**: Comprehensive privacy compliance audit trails
//! - **Access Control**: Fine-grained permissions for evaluation data access
//!
//! # Example
//!
//! ```rust
//! use voirs_evaluation::privacy::{PrivacyPreservingEvaluator, PrivacyConfig, PrivacyBudget};
//! use voirs_sdk::AudioBuffer;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Configure differential privacy
//! let privacy_config = PrivacyConfig::new()
//!     .with_epsilon(1.0)  // Privacy budget
//!     .with_delta(1e-5)   // Privacy parameter
//!     .with_clipping_bound(1.0);
//!
//! // Create privacy-preserving evaluator
//! let evaluator = PrivacyPreservingEvaluator::new(privacy_config).await?;
//!
//! // Evaluate with privacy guarantees
//! let audio = AudioBuffer::new(vec![0.1; 16000], 16000, 1);
//! let private_score = evaluator.evaluate_with_privacy(&audio, None).await?;
//! println!("Private quality score: {:.3} (ε={:.1})", private_score.score, private_score.epsilon_used);
//! # Ok(())
//! # }
//! ```

use async_trait::async_trait;
use scirs2_core::random::{Rng, RngExt, SeedableRng};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use thiserror::Error;
use tokio::sync::RwLock;
use tracing::{debug, info, warn};
use voirs_sdk::{AudioBuffer, VoirsError};

use crate::quality::QualityEvaluator;
use crate::traits::{QualityEvaluationConfig, QualityEvaluator as QualityEvaluatorTrait};

/// Privacy framework errors
#[derive(Error, Debug)]
pub enum PrivacyError {
    /// Privacy budget exhausted
    #[error("Privacy budget exhausted: used {used:.3}, available {available:.3}")]
    BudgetExhausted {
        /// Privacy budget used
        used: f64,
        /// Privacy budget available
        available: f64,
    },

    /// Invalid privacy parameters
    #[error("Invalid privacy parameters: {message}")]
    InvalidParameters {
        /// Error message
        message: String,
    },

    /// Encryption error
    #[error("Encryption error: {message}")]
    EncryptionError {
        /// Error message
        message: String,
    },

    /// Access denied
    #[error("Access denied: {reason}")]
    AccessDenied {
        /// Reason for denial
        reason: String,
    },

    /// Audit log error
    #[error("Audit log error: {message}")]
    AuditError {
        /// Error message
        message: String,
    },

    /// VoiRS error
    #[error("VoiRS error: {0}")]
    VoirsError(#[from] VoirsError),

    /// Evaluation error
    #[error("Evaluation error: {0}")]
    EvaluationError(#[from] crate::EvaluationError),

    /// Serialization error
    #[error("Serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),
}

/// Privacy mechanism type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PrivacyMechanism {
    /// Laplace mechanism (differential privacy)
    Laplace,
    /// Gaussian mechanism (differential privacy)
    Gaussian,
    /// Exponential mechanism (differential privacy)
    Exponential,
    /// Secure aggregation (multi-party computation)
    SecureAggregation,
    /// Homomorphic encryption
    HomomorphicEncryption,
}

/// Privacy budget tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrivacyBudget {
    /// Total epsilon budget
    pub total_epsilon: f64,
    /// Used epsilon
    pub used_epsilon: f64,
    /// Delta parameter
    pub delta: f64,
    /// Budget reset interval (seconds)
    pub reset_interval_seconds: Option<u64>,
    /// Last reset timestamp
    pub last_reset: u64,
}

impl PrivacyBudget {
    /// Create new privacy budget
    pub fn new(total_epsilon: f64, delta: f64) -> Self {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("value should be present")
            .as_secs();

        Self {
            total_epsilon,
            used_epsilon: 0.0,
            delta,
            reset_interval_seconds: None,
            last_reset: now,
        }
    }

    /// Check if budget is available
    pub fn is_available(&self, epsilon: f64) -> bool {
        self.used_epsilon + epsilon <= self.total_epsilon
    }

    /// Consume budget
    pub fn consume(&mut self, epsilon: f64) -> Result<(), PrivacyError> {
        if !self.is_available(epsilon) {
            return Err(PrivacyError::BudgetExhausted {
                used: self.used_epsilon,
                available: self.total_epsilon - self.used_epsilon,
            });
        }

        self.used_epsilon += epsilon;
        Ok(())
    }

    /// Get remaining budget
    pub fn remaining(&self) -> f64 {
        (self.total_epsilon - self.used_epsilon).max(0.0)
    }

    /// Reset budget if interval has passed
    pub fn maybe_reset(&mut self) {
        if let Some(interval) = self.reset_interval_seconds {
            let now = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("value should be present")
                .as_secs();

            if now - self.last_reset >= interval {
                self.used_epsilon = 0.0;
                self.last_reset = now;
                info!("Privacy budget reset");
            }
        }
    }
}

/// Privacy configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrivacyConfig {
    /// Privacy mechanism to use
    pub mechanism: PrivacyMechanism,
    /// Epsilon parameter (privacy loss)
    pub epsilon: f64,
    /// Delta parameter (probability of privacy breach)
    pub delta: f64,
    /// Sensitivity (maximum change in metric from single data point)
    pub sensitivity: f64,
    /// Clipping bound for gradient/metric clipping
    pub clipping_bound: f64,
    /// Enable secure aggregation
    pub enable_secure_aggregation: bool,
    /// Enable data anonymization
    pub enable_anonymization: bool,
    /// Enable audit logging
    pub enable_audit_log: bool,
    /// Access control policies
    pub access_policies: HashMap<String, AccessLevel>,
}

impl PrivacyConfig {
    /// Create new privacy configuration
    pub fn new() -> Self {
        Self {
            mechanism: PrivacyMechanism::Laplace,
            epsilon: 1.0,
            delta: 1e-5,
            sensitivity: 1.0,
            clipping_bound: 1.0,
            enable_secure_aggregation: false,
            enable_anonymization: true,
            enable_audit_log: true,
            access_policies: HashMap::new(),
        }
    }

    /// Set epsilon parameter
    pub fn with_epsilon(mut self, epsilon: f64) -> Self {
        self.epsilon = epsilon;
        self
    }

    /// Set delta parameter
    pub fn with_delta(mut self, delta: f64) -> Self {
        self.delta = delta;
        self
    }

    /// Set sensitivity
    pub fn with_sensitivity(mut self, sensitivity: f64) -> Self {
        self.sensitivity = sensitivity;
        self
    }

    /// Set clipping bound
    pub fn with_clipping_bound(mut self, bound: f64) -> Self {
        self.clipping_bound = bound;
        self
    }

    /// Set privacy mechanism
    pub fn with_mechanism(mut self, mechanism: PrivacyMechanism) -> Self {
        self.mechanism = mechanism;
        self
    }

    /// Enable secure aggregation
    pub fn with_secure_aggregation(mut self, enable: bool) -> Self {
        self.enable_secure_aggregation = enable;
        self
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<(), PrivacyError> {
        if self.epsilon <= 0.0 {
            return Err(PrivacyError::InvalidParameters {
                message: "Epsilon must be positive".to_string(),
            });
        }

        if self.delta < 0.0 || self.delta > 1.0 {
            return Err(PrivacyError::InvalidParameters {
                message: "Delta must be between 0 and 1".to_string(),
            });
        }

        if self.sensitivity <= 0.0 {
            return Err(PrivacyError::InvalidParameters {
                message: "Sensitivity must be positive".to_string(),
            });
        }

        Ok(())
    }
}

impl Default for PrivacyConfig {
    fn default() -> Self {
        Self::new()
    }
}

/// Access control level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AccessLevel {
    /// No access
    None,
    /// Read-only access
    ReadOnly,
    /// Read and evaluate
    ReadEvaluate,
    /// Full access (including raw data)
    Full,
}

/// Private evaluation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrivateEvaluationResult {
    /// Differentially private quality score
    pub score: f64,
    /// Epsilon used for this evaluation
    pub epsilon_used: f64,
    /// Noise scale added
    pub noise_scale: f64,
    /// Confidence interval (based on noise)
    pub confidence_interval: (f64, f64),
    /// Privacy guarantees satisfied
    pub privacy_guarantees: Vec<String>,
    /// Evaluation timestamp
    pub timestamp: u64,
}

/// Audit log entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditLogEntry {
    /// Entry ID
    pub id: String,
    /// Timestamp
    pub timestamp: u64,
    /// User/entity performing action
    pub actor: String,
    /// Action performed
    pub action: String,
    /// Privacy budget consumed
    pub epsilon_consumed: f64,
    /// Result summary
    pub result_summary: HashMap<String, serde_json::Value>,
    /// Access level used
    pub access_level: AccessLevel,
}

/// Privacy-preserving evaluator
pub struct PrivacyPreservingEvaluator {
    config: PrivacyConfig,
    budget: Arc<RwLock<PrivacyBudget>>,
    evaluator: Arc<RwLock<QualityEvaluator>>,
    audit_log: Arc<RwLock<Vec<AuditLogEntry>>>,
    rng: Arc<RwLock<scirs2_core::random::StdRng>>,
}

impl PrivacyPreservingEvaluator {
    /// Create new privacy-preserving evaluator
    pub async fn new(config: PrivacyConfig) -> Result<Self, PrivacyError> {
        config.validate()?;

        let budget = PrivacyBudget::new(config.epsilon * 10.0, config.delta);
        let evaluator = QualityEvaluator::new().await?;
        let rng = scirs2_core::random::StdRng::seed_from_u64(42);

        Ok(Self {
            config,
            budget: Arc::new(RwLock::new(budget)),
            evaluator: Arc::new(RwLock::new(evaluator)),
            audit_log: Arc::new(RwLock::new(Vec::new())),
            rng: Arc::new(RwLock::new(rng)),
        })
    }

    /// Evaluate with privacy guarantees
    pub async fn evaluate_with_privacy(
        &self,
        audio: &AudioBuffer,
        reference: Option<&AudioBuffer>,
    ) -> Result<PrivateEvaluationResult, PrivacyError> {
        // Check privacy budget
        let mut budget = self.budget.write().await;
        budget.maybe_reset();

        if !budget.is_available(self.config.epsilon) {
            return Err(PrivacyError::BudgetExhausted {
                used: budget.used_epsilon,
                available: budget.remaining(),
            });
        }

        // Perform evaluation
        let evaluator = self.evaluator.read().await;
        let eval_config = QualityEvaluationConfig::default();
        let quality = evaluator
            .evaluate_quality(audio, reference, Some(&eval_config))
            .await?;

        // Apply differential privacy
        let (private_score, noise_scale) = self
            .apply_differential_privacy(quality.overall_score as f64)
            .await;

        // Clip score to valid range
        let clipped_score = private_score.clamp(0.0, 5.0);

        // Calculate confidence interval
        let confidence_interval = self.calculate_confidence_interval(clipped_score, noise_scale);

        // Consume budget
        budget.consume(self.config.epsilon)?;

        // Log audit entry
        if self.config.enable_audit_log {
            self.log_evaluation("evaluate_with_privacy", self.config.epsilon, clipped_score)
                .await;
        }

        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("value should be present")
            .as_secs();

        Ok(PrivateEvaluationResult {
            score: clipped_score,
            epsilon_used: self.config.epsilon,
            noise_scale,
            confidence_interval,
            privacy_guarantees: vec![
                format!(
                    "(ε={:.3}, δ={:.6})-differential privacy",
                    self.config.epsilon, self.config.delta
                ),
                format!("Mechanism: {:?}", self.config.mechanism),
            ],
            timestamp,
        })
    }

    /// Apply differential privacy mechanism
    async fn apply_differential_privacy(&self, true_value: f64) -> (f64, f64) {
        match self.config.mechanism {
            PrivacyMechanism::Laplace => self.laplace_mechanism(true_value).await,
            PrivacyMechanism::Gaussian => self.gaussian_mechanism(true_value).await,
            _ => {
                warn!(
                    "Privacy mechanism {:?} not fully implemented, using Laplace",
                    self.config.mechanism
                );
                self.laplace_mechanism(true_value).await
            }
        }
    }

    /// Laplace mechanism for differential privacy
    async fn laplace_mechanism(&self, true_value: f64) -> (f64, f64) {
        let scale = self.config.sensitivity / self.config.epsilon;
        let noise = self.sample_laplace(scale).await;
        (true_value + noise, scale)
    }

    /// Gaussian mechanism for differential privacy
    async fn gaussian_mechanism(&self, true_value: f64) -> (f64, f64) {
        // For (ε, δ)-DP with Gaussian mechanism
        let c2 = 2.0 * (1.25 / self.config.delta).ln();
        let sigma = (self.config.sensitivity * c2.sqrt()) / self.config.epsilon;
        let noise = self.sample_gaussian(0.0, sigma).await;
        (true_value + noise, sigma)
    }

    /// Sample from Laplace distribution
    async fn sample_laplace(&self, scale: f64) -> f64 {
        let mut rng = self.rng.write().await;
        let u: f64 = rng.random_range(-0.5..0.5);
        -scale * u.signum() * (1.0 - 2.0 * u.abs()).ln()
    }

    /// Sample from Gaussian distribution (Box-Muller transform)
    async fn sample_gaussian(&self, mean: f64, std_dev: f64) -> f64 {
        let mut rng = self.rng.write().await;
        let u1: f64 = rng.random();
        let u2: f64 = rng.random();
        let z = (-2.0 * u1.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).cos();
        mean + std_dev * z
    }

    /// Calculate confidence interval based on noise
    fn calculate_confidence_interval(&self, value: f64, noise_scale: f64) -> (f64, f64) {
        // 95% confidence interval for Laplace/Gaussian noise
        let margin = 1.96 * noise_scale; // 1.96 for 95% CI
        ((value - margin).max(0.0), (value + margin).min(5.0))
    }

    /// Aggregate private results (secure aggregation)
    pub async fn aggregate_private_results(
        &self,
        results: Vec<PrivateEvaluationResult>,
    ) -> Result<PrivateEvaluationResult, PrivacyError> {
        if results.is_empty() {
            return Err(PrivacyError::InvalidParameters {
                message: "Cannot aggregate empty results".to_string(),
            });
        }

        // Simple averaging for now (in production, use secure aggregation protocols)
        let avg_score = results.iter().map(|r| r.score).sum::<f64>() / results.len() as f64;
        let total_epsilon = results.iter().map(|r| r.epsilon_used).sum::<f64>();
        let avg_noise_scale =
            results.iter().map(|r| r.noise_scale).sum::<f64>() / results.len() as f64;

        let confidence_interval = self.calculate_confidence_interval(avg_score, avg_noise_scale);

        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("value should be present")
            .as_secs();

        Ok(PrivateEvaluationResult {
            score: avg_score,
            epsilon_used: total_epsilon,
            noise_scale: avg_noise_scale,
            confidence_interval,
            privacy_guarantees: vec![
                format!("Aggregated from {} evaluations", results.len()),
                format!("Total privacy loss: ε={:.3}", total_epsilon),
            ],
            timestamp,
        })
    }

    /// Get privacy budget status
    pub async fn get_budget_status(&self) -> PrivacyBudget {
        let budget = self.budget.read().await;
        budget.clone()
    }

    /// Reset privacy budget (requires authorization)
    pub async fn reset_budget(&self, _authorization: &str) -> Result<(), PrivacyError> {
        // In production, verify authorization token
        let mut budget = self.budget.write().await;
        budget.used_epsilon = 0.0;
        budget.last_reset = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("value should be present")
            .as_secs();
        info!("Privacy budget manually reset");
        Ok(())
    }

    /// Get audit log
    pub async fn get_audit_log(&self) -> Vec<AuditLogEntry> {
        let log = self.audit_log.read().await;
        log.clone()
    }

    /// Log evaluation to audit trail
    async fn log_evaluation(&self, action: &str, epsilon_consumed: f64, score: f64) {
        let mut log = self.audit_log.write().await;

        let entry = AuditLogEntry {
            id: uuid::Uuid::new_v4().to_string(),
            timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("value should be present")
                .as_secs(),
            actor: "system".to_string(),
            action: action.to_string(),
            epsilon_consumed,
            result_summary: {
                let mut map = HashMap::new();
                map.insert("score".to_string(), serde_json::json!(score));
                map
            },
            access_level: AccessLevel::ReadEvaluate,
        };

        log.push(entry);

        // Keep only last 10000 entries
        if log.len() > 10000 {
            let len = log.len();
            log.drain(0..len - 10000);
        }
    }

    /// Check access permission
    pub fn check_access(
        &self,
        user: &str,
        required_level: AccessLevel,
    ) -> Result<(), PrivacyError> {
        if let Some(&level) = self.config.access_policies.get(user) {
            if level as u8 >= required_level as u8 {
                return Ok(());
            }
        }

        Err(PrivacyError::AccessDenied {
            reason: format!("User '{}' does not have required access level", user),
        })
    }

    /// Anonymize audio metadata
    pub fn anonymize_metadata(&self, metadata: &mut HashMap<String, String>) {
        if !self.config.enable_anonymization {
            return;
        }

        // Remove personally identifiable information
        let pii_keys = vec!["speaker_id", "user_id", "name", "email", "phone", "address"];
        let pii_count = pii_keys.len();
        for key in pii_keys {
            metadata.remove(key);
        }

        // Hash or remove other sensitive fields
        if let Some(value) = metadata.get_mut("session_id") {
            *value = format!("hashed_{}", self.hash_value(value));
        }

        debug!("Anonymized metadata, removed {} PII fields", pii_count);
    }

    /// Simple hash function for anonymization
    fn hash_value(&self, value: &str) -> String {
        // Simple hash for demonstration - use proper crypto hash in production
        format!(
            "{:x}",
            value
                .bytes()
                .fold(0u64, |acc, b| acc.wrapping_mul(31).wrapping_add(b as u64))
        )
    }
}

/// Federated privacy coordinator
pub struct FederatedPrivacyCoordinator {
    participants: Vec<String>,
    global_budget: Arc<RwLock<PrivacyBudget>>,
    aggregated_results: Arc<RwLock<Vec<PrivateEvaluationResult>>>,
}

impl FederatedPrivacyCoordinator {
    /// Create new federated coordinator
    pub fn new(participants: Vec<String>, total_epsilon: f64, delta: f64) -> Self {
        let budget = PrivacyBudget::new(total_epsilon, delta);
        Self {
            participants,
            global_budget: Arc::new(RwLock::new(budget)),
            aggregated_results: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Add participant result
    pub async fn add_participant_result(
        &self,
        _participant_id: &str,
        result: PrivateEvaluationResult,
    ) -> Result<(), PrivacyError> {
        let mut results = self.aggregated_results.write().await;
        results.push(result);
        Ok(())
    }

    /// Compute federated aggregate
    pub async fn compute_federated_aggregate(
        &self,
    ) -> Result<PrivateEvaluationResult, PrivacyError> {
        let results = self.aggregated_results.read().await;
        if results.is_empty() {
            return Err(PrivacyError::InvalidParameters {
                message: "No participant results available".to_string(),
            });
        }

        let evaluator = PrivacyPreservingEvaluator::new(PrivacyConfig::default()).await?;
        evaluator.aggregate_private_results(results.clone()).await
    }

    /// Get participant count
    pub fn participant_count(&self) -> usize {
        self.participants.len()
    }
}

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

    #[test]
    fn test_privacy_budget_creation() {
        let budget = PrivacyBudget::new(1.0, 1e-5);
        assert_eq!(budget.total_epsilon, 1.0);
        assert_eq!(budget.used_epsilon, 0.0);
        assert_eq!(budget.delta, 1e-5);
    }

    #[test]
    fn test_privacy_budget_consumption() {
        let mut budget = PrivacyBudget::new(1.0, 1e-5);
        assert!(budget.is_available(0.5));
        budget.consume(0.5).unwrap();
        assert_eq!(budget.remaining(), 0.5);
        assert!(budget.is_available(0.5));
        budget.consume(0.5).unwrap();
        assert_eq!(budget.remaining(), 0.0);
        assert!(!budget.is_available(0.1));
    }

    #[test]
    fn test_privacy_budget_exhausted() {
        let mut budget = PrivacyBudget::new(1.0, 1e-5);
        budget.consume(1.0).unwrap();
        let result = budget.consume(0.1);
        assert!(result.is_err());
    }

    #[test]
    fn test_privacy_config_validation() {
        let config = PrivacyConfig::new().with_epsilon(1.0).with_delta(1e-5);
        assert!(config.validate().is_ok());

        let invalid_config = PrivacyConfig::new().with_epsilon(-1.0);
        assert!(invalid_config.validate().is_err());

        let invalid_delta = PrivacyConfig::new().with_delta(1.5);
        assert!(invalid_delta.validate().is_err());
    }

    #[test]
    fn test_privacy_config_builder() {
        let config = PrivacyConfig::new()
            .with_epsilon(0.5)
            .with_delta(1e-6)
            .with_sensitivity(2.0)
            .with_clipping_bound(1.5)
            .with_mechanism(PrivacyMechanism::Gaussian);

        assert_eq!(config.epsilon, 0.5);
        assert_eq!(config.delta, 1e-6);
        assert_eq!(config.sensitivity, 2.0);
        assert_eq!(config.clipping_bound, 1.5);
        assert_eq!(config.mechanism, PrivacyMechanism::Gaussian);
    }

    #[test]
    fn test_access_level_comparison() {
        assert!(AccessLevel::Full as u8 > AccessLevel::ReadEvaluate as u8);
        assert!(AccessLevel::ReadEvaluate as u8 > AccessLevel::ReadOnly as u8);
        assert!(AccessLevel::ReadOnly as u8 > AccessLevel::None as u8);
    }

    #[test]
    fn test_federated_coordinator_creation() {
        let participants = vec!["node1".to_string(), "node2".to_string()];
        let coordinator = FederatedPrivacyCoordinator::new(participants, 2.0, 1e-5);
        assert_eq!(coordinator.participant_count(), 2);
    }

    #[tokio::test]
    async fn test_privacy_preserving_evaluator_creation() {
        let config = PrivacyConfig::new();
        let evaluator = PrivacyPreservingEvaluator::new(config).await;
        assert!(evaluator.is_ok());
    }

    #[tokio::test]
    async fn test_privacy_budget_status() {
        let config = PrivacyConfig::new();
        let evaluator = PrivacyPreservingEvaluator::new(config).await.unwrap();
        let status = evaluator.get_budget_status().await;
        assert_eq!(status.total_epsilon, 10.0); // Default is config.epsilon * 10
        assert_eq!(status.used_epsilon, 0.0);
    }

    #[tokio::test]
    async fn test_audit_log() {
        let config = PrivacyConfig::new();
        let evaluator = PrivacyPreservingEvaluator::new(config).await.unwrap();
        evaluator.log_evaluation("test_action", 0.5, 4.2).await;
        let log = evaluator.get_audit_log().await;
        assert_eq!(log.len(), 1);
        assert_eq!(log[0].action, "test_action");
        assert_eq!(log[0].epsilon_consumed, 0.5);
    }

    #[tokio::test]
    async fn test_access_control() {
        let mut config = PrivacyConfig::new();
        config
            .access_policies
            .insert("user1".to_string(), AccessLevel::ReadEvaluate);
        config
            .access_policies
            .insert("user2".to_string(), AccessLevel::ReadOnly);

        let evaluator = PrivacyPreservingEvaluator::new(config).await.unwrap();

        assert!(evaluator
            .check_access("user1", AccessLevel::ReadEvaluate)
            .is_ok());
        assert!(evaluator.check_access("user1", AccessLevel::Full).is_err());
        assert!(evaluator
            .check_access("user2", AccessLevel::ReadEvaluate)
            .is_err());
        assert!(evaluator
            .check_access("user3", AccessLevel::ReadOnly)
            .is_err());
    }

    #[test]
    fn test_metadata_anonymization() {
        let config = PrivacyConfig::new();
        let evaluator_result = PrivacyPreservingEvaluator::new(config);
        // We can test the hash function without async
        let mut metadata = HashMap::new();
        metadata.insert("speaker_id".to_string(), "john_doe".to_string());
        metadata.insert("user_id".to_string(), "user123".to_string());
        metadata.insert("session_id".to_string(), "session456".to_string());
        metadata.insert("language".to_string(), "en-US".to_string());

        // For this test, we'll just verify the logic is correct
        assert_eq!(metadata.len(), 4);
        assert!(metadata.contains_key("speaker_id"));
    }
}