tacet 0.4.2

Detect timing side channels in cryptographic code
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
//! Stationarity tracking for drift detection (implementation-guide ยง2.3).
//!
//! Uses reservoir sampling to maintain approximate quantiles per time window,
//! enabling detection of timing distribution drift during measurement.

use rand::{Rng, SeedableRng};
use rand_xoshiro::Xoshiro256PlusPlus;

/// Number of time windows for stationarity analysis.
const NUM_WINDOWS: usize = 10;

/// Samples per window for reservoir sampling.
const SAMPLES_PER_WINDOW: usize = 100;

/// Tracks timing samples across time windows for stationarity analysis.
///
/// Uses reservoir sampling to maintain a fixed-size sample per window,
/// allowing O(1) memory usage regardless of total sample count.
#[derive(Debug, Clone)]
pub struct StationarityTracker {
    /// Reservoir samples for each window.
    windows: [[f64; SAMPLES_PER_WINDOW]; NUM_WINDOWS],
    /// Count of samples seen per window.
    counts: [usize; NUM_WINDOWS],
    /// Total samples expected (for window assignment).
    expected_total: usize,
    /// Total samples seen so far.
    samples_seen: usize,
    /// RNG for reservoir sampling.
    rng: Xoshiro256PlusPlus,
}

/// Result of stationarity analysis.
#[derive(Debug, Clone, Copy)]
pub struct StationarityResult {
    /// Ratio of max to min window median.
    pub ratio: f64,
    /// Whether stationarity check passed.
    pub ok: bool,
    /// Whether median drift was detected.
    pub median_drift_ok: bool,
    /// Whether variance drift was detected.
    pub variance_drift_ok: bool,
}

impl Default for StationarityResult {
    fn default() -> Self {
        Self {
            ratio: 1.0,
            ok: true,
            median_drift_ok: true,
            variance_drift_ok: true,
        }
    }
}

impl StationarityTracker {
    /// Create a new stationarity tracker.
    ///
    /// # Arguments
    /// * `expected_total` - Expected total number of samples (for window assignment)
    /// * `seed` - Random seed for reservoir sampling
    pub fn new(expected_total: usize, seed: u64) -> Self {
        Self {
            windows: [[0.0; SAMPLES_PER_WINDOW]; NUM_WINDOWS],
            counts: [0; NUM_WINDOWS],
            expected_total: expected_total.max(1),
            samples_seen: 0,
            rng: Xoshiro256PlusPlus::seed_from_u64(seed),
        }
    }

    /// Push a timing sample.
    pub fn push(&mut self, time_ns: f64) {
        // Determine which window this sample belongs to
        let window_idx = (self.samples_seen * NUM_WINDOWS) / self.expected_total;
        let window_idx = window_idx.min(NUM_WINDOWS - 1);

        let count = self.counts[window_idx];

        if count < SAMPLES_PER_WINDOW {
            // Fill the reservoir
            self.windows[window_idx][count] = time_ns;
        } else {
            // Reservoir sampling: replace with probability SAMPLES_PER_WINDOW / (count + 1)
            let j = self.rng.random_range(0..=count);
            if j < SAMPLES_PER_WINDOW {
                self.windows[window_idx][j] = time_ns;
            }
        }

        self.counts[window_idx] += 1;
        self.samples_seen += 1;
    }

    /// Update expected total (call when sample count changes).
    pub fn set_expected_total(&mut self, expected_total: usize) {
        self.expected_total = expected_total.max(1);
    }

    /// Compute stationarity metrics.
    ///
    /// Returns `None` if insufficient samples for analysis.
    pub fn compute(&self) -> Option<StationarityResult> {
        // Need at least 2 windows with samples
        let active_windows: Vec<usize> =
            (0..NUM_WINDOWS).filter(|&i| self.counts[i] >= 10).collect();

        if active_windows.len() < 2 {
            return None;
        }

        // Compute median and variance for each active window
        let mut window_medians = Vec::with_capacity(active_windows.len());
        let mut window_iqrs = Vec::with_capacity(active_windows.len());
        let mut window_variances = Vec::with_capacity(active_windows.len());

        for &i in &active_windows {
            let n = self.counts[i].min(SAMPLES_PER_WINDOW);
            let mut samples: Vec<f64> = self.windows[i][..n].to_vec();
            samples.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

            let median = samples[n / 2];
            let q1 = samples[n / 4];
            let q3 = samples[(n * 3) / 4];
            let iqr = q3 - q1;

            let mean: f64 = samples.iter().sum::<f64>() / n as f64;
            let variance: f64 = samples.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / n as f64;

            window_medians.push(median);
            window_iqrs.push(iqr);
            window_variances.push(variance);
        }

        // Compute global median for threshold
        let global_median = {
            let sum: f64 = window_medians.iter().sum();
            sum / window_medians.len() as f64
        };

        // Check 1: Median drift (implementation-guide ยง2.3)
        let max_median = window_medians
            .iter()
            .cloned()
            .fold(f64::NEG_INFINITY, f64::max);
        let min_median = window_medians.iter().cloned().fold(f64::INFINITY, f64::min);
        let avg_iqr: f64 = window_iqrs.iter().sum::<f64>() / window_iqrs.len() as f64;

        let drift_floor = 0.05 * global_median;
        let threshold = (2.0 * avg_iqr).max(drift_floor);
        let median_drift_ok = (max_median - min_median) <= threshold;

        // Check 2: Variance monotonic drift (>50% change first to last)
        let first_var = window_variances[0];
        let last_var = *window_variances.last().unwrap();
        let var_drift_ratio = if first_var > 1e-12 {
            last_var / first_var
        } else {
            1.0
        };
        let variance_drift_ok = (0.5..=2.0).contains(&var_drift_ratio);

        // Compute stationarity ratio
        let ratio = if min_median > 1e-12 {
            max_median / min_median
        } else {
            1.0
        };

        Some(StationarityResult {
            ratio,
            ok: median_drift_ok && variance_drift_ok,
            median_drift_ok,
            variance_drift_ok,
        })
    }
}

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

    #[test]
    fn test_stationary_data() {
        let mut tracker = StationarityTracker::new(1000, 42);

        // Push stationary data (constant with small noise)
        for i in 0..1000 {
            let noise = (i % 10) as f64 * 0.1;
            tracker.push(100.0 + noise);
        }

        let result = tracker.compute().unwrap();
        assert!(result.ok, "Stationary data should pass");
        assert!(
            (result.ratio - 1.0).abs() < 0.1,
            "Ratio should be close to 1"
        );
    }

    #[test]
    fn test_drifting_data() {
        let mut tracker = StationarityTracker::new(1000, 42);

        // Push drifting data (linear increase)
        for i in 0..1000 {
            // Significant drift: 100 -> 200 over the measurement
            let time = 100.0 + (i as f64) * 0.1;
            tracker.push(time);
        }

        let result = tracker.compute().unwrap();
        assert!(!result.ok, "Drifting data should fail");
        assert!(result.ratio > 1.5, "Ratio should show significant drift");
    }

    #[test]
    fn test_variance_change() {
        let mut tracker = StationarityTracker::new(1000, 42);
        let mut rng = Xoshiro256PlusPlus::seed_from_u64(123);

        // First half: low variance
        for _ in 0..500 {
            let noise: f64 = rng.random_range(-1.0..1.0);
            tracker.push(100.0 + noise);
        }

        // Second half: high variance (10x)
        for _ in 0..500 {
            let noise: f64 = rng.random_range(-10.0..10.0);
            tracker.push(100.0 + noise);
        }

        let result = tracker.compute().unwrap();
        assert!(
            !result.variance_drift_ok,
            "Variance change should be detected"
        );
    }

    #[test]
    fn test_insufficient_samples() {
        let tracker = StationarityTracker::new(1000, 42);
        assert!(
            tracker.compute().is_none(),
            "Should return None with no samples"
        );
    }

    #[test]
    fn test_reservoir_sampling_coverage() {
        let mut tracker = StationarityTracker::new(10000, 42);

        // Push many samples to test reservoir sampling
        for i in 0..10000 {
            tracker.push(100.0 + (i % 100) as f64);
        }

        let result = tracker.compute();
        assert!(result.is_some(), "Should have enough samples");
    }

    #[test]
    fn test_step_change() {
        // Sudden jump mid-measurement (e.g., CPU throttle)
        let mut tracker = StationarityTracker::new(1000, 42);

        for _ in 0..500 {
            tracker.push(100.0);
        }
        for _ in 0..500 {
            tracker.push(200.0); // Sudden 2x jump
        }

        let result = tracker.compute().unwrap();
        assert!(!result.ok, "Step change should fail stationarity");
        assert!(!result.median_drift_ok, "Median drift should be detected");
    }

    #[test]
    fn test_variance_boundary_pass() {
        // Variance ratio exactly at boundary (2.0) should pass
        let mut tracker = StationarityTracker::new(1000, 42);
        let mut rng = Xoshiro256PlusPlus::seed_from_u64(456);

        // First half: variance ~1
        for _ in 0..500 {
            let noise: f64 = rng.random_range(-1.0..1.0);
            tracker.push(100.0 + noise);
        }

        // Second half: variance ~2 (ratio = 2.0, should still pass)
        for _ in 0..500 {
            let noise: f64 = rng.random_range(-1.414..1.414); // sqrt(2) range
            tracker.push(100.0 + noise);
        }

        let result = tracker.compute().unwrap();
        // With stochastic data, just check it's not failing dramatically
        assert!(result.median_drift_ok, "No median drift expected");
    }

    #[test]
    fn test_median_drift_without_variance_change() {
        // Gradual shift without variance change
        let mut tracker = StationarityTracker::new(1000, 42);
        let mut rng = Xoshiro256PlusPlus::seed_from_u64(789);

        for i in 0..1000 {
            let base = 100.0 + (i as f64) * 0.05; // Slow drift
            let noise: f64 = rng.random_range(-1.0..1.0);
            tracker.push(base + noise);
        }

        let result = tracker.compute().unwrap();
        // Should detect median drift
        assert!(
            !result.median_drift_ok || result.ratio > 1.3,
            "Should detect gradual drift"
        );
    }

    #[test]
    fn test_high_iqr_threshold() {
        // When IQR is high, median drift threshold should increase
        let mut tracker = StationarityTracker::new(1000, 42);
        let mut rng = Xoshiro256PlusPlus::seed_from_u64(321);

        // High variance data with small drift should pass
        for i in 0..1000 {
            let base = 100.0 + (i as f64) * 0.01; // Very small drift
            let noise: f64 = rng.random_range(-20.0..20.0); // High noise
            tracker.push(base + noise);
        }

        let result = tracker.compute().unwrap();
        // High IQR should make threshold lenient
        assert!(
            result.median_drift_ok,
            "Small drift with high IQR should pass"
        );
    }

    #[test]
    fn test_minimum_samples_per_window() {
        // Test with exactly enough samples (10 per window minimum)
        let mut tracker = StationarityTracker::new(200, 42);

        for i in 0..200 {
            tracker.push(100.0 + (i % 5) as f64);
        }

        let result = tracker.compute();
        assert!(
            result.is_some(),
            "Should compute with 20 samples per window"
        );
    }

    #[test]
    fn test_set_expected_total() {
        let mut tracker = StationarityTracker::new(100, 42);

        // Initially expect 100 samples
        for i in 0..50 {
            tracker.push(100.0 + (i % 5) as f64);
        }

        // Update expectation
        tracker.set_expected_total(200);

        // Continue pushing
        for i in 0..50 {
            tracker.push(100.0 + (i % 5) as f64);
        }

        let result = tracker.compute();
        assert!(result.is_some(), "Should handle expected_total update");
    }

    #[test]
    fn test_near_zero_values() {
        let mut tracker = StationarityTracker::new(1000, 42);

        // Very small positive values
        for i in 0..1000 {
            tracker.push(0.001 + (i % 10) as f64 * 0.0001);
        }

        let result = tracker.compute().unwrap();
        assert!(result.ok, "Near-zero positive values should work");
    }

    #[test]
    fn test_identical_values() {
        // All identical values (zero variance)
        let mut tracker = StationarityTracker::new(1000, 42);

        for _ in 0..1000 {
            tracker.push(100.0);
        }

        let result = tracker.compute().unwrap();
        assert!(result.ok, "Identical values should pass");
        assert!(
            (result.ratio - 1.0).abs() < 1e-10,
            "Ratio should be exactly 1.0"
        );
    }
}