tacet-core 0.4.2

Core statistical analysis for timing side-channel detection (no_std)
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
//! Condition drift detection for Gate 4 (spec ยง3.5.4).
//!
//! Detects when measurement conditions change between calibration and the
//! adaptive loop, which can invalidate the covariance estimate and cause
//! false positives or negatives.

use alloc::string::String;
use alloc::vec::Vec;

use crate::statistics::StatsSnapshot;

/// Snapshot of measurement statistics at a point in time.
///
/// Captures per-class statistics for comparison between calibration
/// and post-test phases.
#[derive(Debug, Clone)]
pub struct CalibrationSnapshot {
    /// Statistics for baseline class.
    pub baseline: StatsSnapshot,
    /// Statistics for sample class.
    pub sample: StatsSnapshot,
}

impl CalibrationSnapshot {
    /// Create a new calibration snapshot from per-class statistics.
    pub fn new(baseline: StatsSnapshot, sample: StatsSnapshot) -> Self {
        Self { baseline, sample }
    }
}

/// Detected drift between calibration and post-test statistics.
///
/// Contains per-class drift metrics that indicate how much measurement
/// conditions changed during the test.
#[derive(Debug, Clone, Copy)]
pub struct ConditionDrift {
    /// Variance ratio for baseline class: post_variance / cal_variance.
    /// Values far from 1.0 indicate variance changed significantly.
    pub variance_ratio_baseline: f64,

    /// Variance ratio for sample class.
    pub variance_ratio_sample: f64,

    /// Absolute change in lag-1 autocorrelation for baseline class.
    pub autocorr_change_baseline: f64,

    /// Absolute change in lag-1 autocorrelation for sample class.
    pub autocorr_change_sample: f64,

    /// Mean drift in standard deviations for baseline class:
    /// |post_mean - cal_mean| / cal_std_dev
    pub mean_drift_baseline: f64,

    /// Mean drift in standard deviations for sample class.
    pub mean_drift_sample: f64,
}

impl ConditionDrift {
    /// Compute drift between calibration and post-test snapshots.
    ///
    /// # Arguments
    ///
    /// * `cal` - Statistics snapshot from calibration phase
    /// * `post` - Statistics snapshot from full test run
    ///
    /// # Returns
    ///
    /// A `ConditionDrift` struct with per-class drift metrics.
    pub fn compute(cal: &CalibrationSnapshot, post: &CalibrationSnapshot) -> Self {
        Self {
            variance_ratio_baseline: compute_variance_ratio(
                cal.baseline.variance,
                post.baseline.variance,
            ),
            variance_ratio_sample: compute_variance_ratio(
                cal.sample.variance,
                post.sample.variance,
            ),
            autocorr_change_baseline: libm::fabs(
                post.baseline.autocorr_lag1 - cal.baseline.autocorr_lag1,
            ),
            autocorr_change_sample: libm::fabs(
                post.sample.autocorr_lag1 - cal.sample.autocorr_lag1,
            ),
            mean_drift_baseline: compute_mean_drift(
                cal.baseline.mean,
                post.baseline.mean,
                cal.baseline.variance,
            ),
            mean_drift_sample: compute_mean_drift(
                cal.sample.mean,
                post.sample.mean,
                cal.sample.variance,
            ),
        }
    }

    /// Check if drift exceeds thresholds.
    ///
    /// Returns `true` if any drift metric exceeds its threshold, indicating
    /// measurement conditions changed significantly.
    pub fn is_significant(&self, thresholds: &DriftThresholds) -> bool {
        // Variance drift check (either direction)
        if self.variance_ratio_baseline > thresholds.max_variance_ratio
            || self.variance_ratio_baseline < thresholds.min_variance_ratio
        {
            return true;
        }
        if self.variance_ratio_sample > thresholds.max_variance_ratio
            || self.variance_ratio_sample < thresholds.min_variance_ratio
        {
            return true;
        }

        // Autocorrelation change check
        if self.autocorr_change_baseline > thresholds.max_autocorr_change {
            return true;
        }
        if self.autocorr_change_sample > thresholds.max_autocorr_change {
            return true;
        }

        // Mean drift check
        if self.mean_drift_baseline > thresholds.max_mean_drift_sigmas {
            return true;
        }
        if self.mean_drift_sample > thresholds.max_mean_drift_sigmas {
            return true;
        }

        false
    }

    /// Get a human-readable description of the most significant drift.
    pub fn description(&self, thresholds: &DriftThresholds) -> String {
        let mut issues = Vec::new();

        if self.variance_ratio_baseline > thresholds.max_variance_ratio {
            issues.push(alloc::format!(
                "baseline variance increased {:.1}x",
                self.variance_ratio_baseline
            ));
        } else if self.variance_ratio_baseline < thresholds.min_variance_ratio {
            issues.push(alloc::format!(
                "baseline variance decreased to {:.1}x",
                self.variance_ratio_baseline
            ));
        }

        if self.variance_ratio_sample > thresholds.max_variance_ratio {
            issues.push(alloc::format!(
                "sample variance increased {:.1}x",
                self.variance_ratio_sample
            ));
        } else if self.variance_ratio_sample < thresholds.min_variance_ratio {
            issues.push(alloc::format!(
                "sample variance decreased to {:.1}x",
                self.variance_ratio_sample
            ));
        }

        if self.autocorr_change_baseline > thresholds.max_autocorr_change {
            issues.push(alloc::format!(
                "baseline autocorrelation changed by {:.2}",
                self.autocorr_change_baseline
            ));
        }

        if self.autocorr_change_sample > thresholds.max_autocorr_change {
            issues.push(alloc::format!(
                "sample autocorrelation changed by {:.2}",
                self.autocorr_change_sample
            ));
        }

        if self.mean_drift_baseline > thresholds.max_mean_drift_sigmas {
            issues.push(alloc::format!(
                "baseline mean drifted {:.1}\u{03C3}",
                self.mean_drift_baseline
            ));
        }

        if self.mean_drift_sample > thresholds.max_mean_drift_sigmas {
            issues.push(alloc::format!(
                "sample mean drifted {:.1}\u{03C3}",
                self.mean_drift_sample
            ));
        }

        if issues.is_empty() {
            String::from("no significant drift")
        } else {
            issues.join(", ")
        }
    }
}

/// Thresholds for condition drift detection.
///
/// These control when Gate 6 (ConditionsChanged) triggers.
#[derive(Debug, Clone, Copy)]
pub struct DriftThresholds {
    /// Maximum allowed variance ratio (post/cal). Default: 2.0
    pub max_variance_ratio: f64,

    /// Minimum allowed variance ratio (post/cal). Default: 0.5
    pub min_variance_ratio: f64,

    /// Maximum allowed change in lag-1 autocorrelation. Default: 0.3
    pub max_autocorr_change: f64,

    /// Maximum allowed mean drift in standard deviations. Default: 3.0
    pub max_mean_drift_sigmas: f64,
}

impl Default for DriftThresholds {
    fn default() -> Self {
        Self {
            max_variance_ratio: 2.0,
            min_variance_ratio: 0.5,
            max_autocorr_change: 0.3,
            max_mean_drift_sigmas: 3.0,
        }
    }
}

/// Compute variance ratio, handling edge cases.
fn compute_variance_ratio(cal_variance: f64, post_variance: f64) -> f64 {
    if cal_variance < 1e-15 {
        // If calibration variance was essentially zero, any change is infinite
        // But if post is also zero, call it 1.0 (no change)
        if post_variance < 1e-15 {
            1.0
        } else {
            f64::INFINITY
        }
    } else {
        post_variance / cal_variance
    }
}

/// Compute mean drift in standard deviations.
fn compute_mean_drift(cal_mean: f64, post_mean: f64, cal_variance: f64) -> f64 {
    let cal_std = libm::sqrt(cal_variance);
    if cal_std < 1e-15 {
        // If calibration had zero variance, any mean change is significant
        // But if means are equal, call it 0.0
        if libm::fabs(post_mean - cal_mean) < 1e-15 {
            0.0
        } else {
            f64::INFINITY
        }
    } else {
        libm::fabs(post_mean - cal_mean) / cal_std
    }
}

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

    fn make_snapshot(mean: f64, variance: f64, autocorr: f64) -> StatsSnapshot {
        StatsSnapshot {
            mean,
            variance,
            autocorr_lag1: autocorr,
            count: 1000,
        }
    }

    #[test]
    fn test_no_drift() {
        let cal = CalibrationSnapshot::new(
            make_snapshot(100.0, 25.0, 0.3),
            make_snapshot(100.0, 25.0, 0.3),
        );
        let post = CalibrationSnapshot::new(
            make_snapshot(100.0, 25.0, 0.3),
            make_snapshot(100.0, 25.0, 0.3),
        );

        let drift = ConditionDrift::compute(&cal, &post);
        let thresholds = DriftThresholds::default();

        assert!(!drift.is_significant(&thresholds));
        assert!(libm::fabs(drift.variance_ratio_baseline - 1.0) < 1e-10);
        assert!(libm::fabs(drift.variance_ratio_sample - 1.0) < 1e-10);
    }

    #[test]
    fn test_variance_increase_detected() {
        let cal = CalibrationSnapshot::new(
            make_snapshot(100.0, 25.0, 0.3),
            make_snapshot(100.0, 25.0, 0.3),
        );
        let post = CalibrationSnapshot::new(
            make_snapshot(100.0, 75.0, 0.3), // 3x variance increase
            make_snapshot(100.0, 25.0, 0.3),
        );

        let drift = ConditionDrift::compute(&cal, &post);
        let thresholds = DriftThresholds::default();

        assert!(drift.is_significant(&thresholds));
        assert!(libm::fabs(drift.variance_ratio_baseline - 3.0) < 1e-10);
    }

    #[test]
    fn test_variance_decrease_detected() {
        let cal = CalibrationSnapshot::new(
            make_snapshot(100.0, 100.0, 0.3),
            make_snapshot(100.0, 100.0, 0.3),
        );
        let post = CalibrationSnapshot::new(
            make_snapshot(100.0, 25.0, 0.3), // 4x variance decrease
            make_snapshot(100.0, 100.0, 0.3),
        );

        let drift = ConditionDrift::compute(&cal, &post);
        let thresholds = DriftThresholds::default();

        assert!(drift.is_significant(&thresholds));
        assert!(libm::fabs(drift.variance_ratio_baseline - 0.25) < 1e-10);
    }

    #[test]
    fn test_autocorr_change_detected() {
        let cal = CalibrationSnapshot::new(
            make_snapshot(100.0, 25.0, 0.1),
            make_snapshot(100.0, 25.0, 0.1),
        );
        let post = CalibrationSnapshot::new(
            make_snapshot(100.0, 25.0, 0.6), // 0.5 autocorr change
            make_snapshot(100.0, 25.0, 0.1),
        );

        let drift = ConditionDrift::compute(&cal, &post);
        let thresholds = DriftThresholds::default();

        assert!(drift.is_significant(&thresholds));
        assert!(libm::fabs(drift.autocorr_change_baseline - 0.5) < 1e-10);
    }

    #[test]
    fn test_mean_drift_detected() {
        let cal = CalibrationSnapshot::new(
            make_snapshot(100.0, 25.0, 0.3), // std = 5
            make_snapshot(100.0, 25.0, 0.3),
        );
        let post = CalibrationSnapshot::new(
            make_snapshot(120.0, 25.0, 0.3), // 20/5 = 4 sigma drift
            make_snapshot(100.0, 25.0, 0.3),
        );

        let drift = ConditionDrift::compute(&cal, &post);
        let thresholds = DriftThresholds::default();

        assert!(drift.is_significant(&thresholds));
        assert!(libm::fabs(drift.mean_drift_baseline - 4.0) < 1e-10);
    }

    #[test]
    fn test_small_drift_allowed() {
        let cal = CalibrationSnapshot::new(
            make_snapshot(100.0, 25.0, 0.3),
            make_snapshot(100.0, 25.0, 0.3),
        );
        let post = CalibrationSnapshot::new(
            make_snapshot(102.0, 30.0, 0.35), // Small changes within thresholds
            make_snapshot(98.0, 20.0, 0.25),
        );

        let drift = ConditionDrift::compute(&cal, &post);
        let thresholds = DriftThresholds::default();

        assert!(!drift.is_significant(&thresholds));
    }

    #[test]
    fn test_description() {
        let cal = CalibrationSnapshot::new(
            make_snapshot(100.0, 25.0, 0.1),
            make_snapshot(100.0, 25.0, 0.1),
        );
        let post = CalibrationSnapshot::new(
            make_snapshot(100.0, 75.0, 0.1), // 3x variance
            make_snapshot(100.0, 25.0, 0.1),
        );

        let drift = ConditionDrift::compute(&cal, &post);
        let thresholds = DriftThresholds::default();
        let desc = drift.description(&thresholds);

        assert!(desc.contains("baseline variance increased"));
    }

    #[test]
    fn test_custom_thresholds() {
        let cal = CalibrationSnapshot::new(
            make_snapshot(100.0, 25.0, 0.3),
            make_snapshot(100.0, 25.0, 0.3),
        );
        let post = CalibrationSnapshot::new(
            make_snapshot(100.0, 60.0, 0.3), // 2.4x variance
            make_snapshot(100.0, 25.0, 0.3),
        );

        let drift = ConditionDrift::compute(&cal, &post);

        // Default threshold (2.0) would trigger
        let default_thresholds = DriftThresholds::default();
        assert!(drift.is_significant(&default_thresholds));

        // Relaxed threshold (3.0) would not trigger
        let relaxed_thresholds = DriftThresholds {
            max_variance_ratio: 3.0,
            ..Default::default()
        };
        assert!(!drift.is_significant(&relaxed_thresholds));
    }
}