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
//! Block bootstrap resampling for time series data.
//!
//! This module implements block bootstrap resampling which preserves
//! the autocorrelation structure of timing measurements. This is critical
//! for accurate covariance estimation when measurements have temporal
//! dependencies.

extern crate alloc;

use alloc::vec::Vec;

use rand::Rng;

use crate::math;
use crate::types::TimingSample;

/// Counter-based RNG seed generation using SplitMix64.
///
/// This is a stateless PRF that generates deterministic, well-distributed
/// seeds from a base seed and counter. Using this instead of simple addition
/// provides better statistical properties and avoids sequential correlation.
///
/// # Arguments
///
/// * `base_seed` - Base random seed
/// * `counter` - Iteration counter (0, 1, 2, ...)
///
/// # Returns
///
/// A 64-bit seed suitable for initializing an RNG.
///
/// # Performance
///
/// ~2-3ns per call (negligible compared to ~100ns for RNG initialization).
#[inline]
pub fn counter_rng_seed(base_seed: u64, counter: u64) -> u64 {
    // SplitMix64: high-quality 64-bit hash function
    // See: https://xoshiro.di.unimi.it/splitmix64.c
    let mut z = base_seed.wrapping_add(counter.wrapping_mul(0x9e3779b97f4a7c15));
    z = (z ^ (z >> 30)).wrapping_mul(0xbf58476d1ce4e5b9);
    z = (z ^ (z >> 27)).wrapping_mul(0x94d049bb133111eb);
    z ^ (z >> 31)
}

/// Compute the optimal block size for block bootstrap using n^(1/3) scaling.
///
/// The block length follows Politis & Romano (1994) and Kunsch (1989):
/// l(n) = O(n^(1/3)) balances bias-variance tradeoff for stationary data.
///
/// The constant 1.3 is an engineering default that works well empirically.
/// At n=30k, this gives ~40 blocks (vs ~173 with sqrt(n)), preserving
/// autocorrelation structure better.
///
/// # Arguments
///
/// * `n` - Number of samples in the original data
///
/// # Returns
///
/// The recommended block size, minimum 1.
pub fn compute_block_size(n: usize) -> usize {
    let block_size = math::ceil(1.3 * math::cbrt(n as f64)) as usize;
    block_size.max(1)
}

/// Perform block bootstrap resampling into an existing buffer.
///
/// This is an optimized version of `block_bootstrap_resample` that writes
/// into a preallocated buffer instead of allocating a new Vec. This eliminates
/// allocation overhead in hot loops.
///
/// # Arguments
///
/// * `data` - Slice of timing measurements for one class
/// * `block_size` - Size of contiguous blocks to resample
/// * `rng` - Random number generator
/// * `out` - Output buffer (must have same length as `data`)
///
/// # Panics
///
/// Panics if `out.len() != data.len()`.
///
/// # Performance
///
/// ~2-3× faster than allocating version when called repeatedly with
/// the same buffer, due to eliminating allocator overhead and using
/// fast `copy_from_slice` memcpy.
pub fn block_bootstrap_resample_into<R: Rng>(
    data: &[f64],
    block_size: usize,
    rng: &mut R,
    out: &mut [f64],
) {
    assert_eq!(
        out.len(),
        data.len(),
        "Output buffer must have same length as input data"
    );

    if data.is_empty() {
        return;
    }

    let n = data.len();
    let block_size = block_size.max(1).min(n);

    // Number of possible starting positions for blocks
    let num_block_starts = n.saturating_sub(block_size) + 1;
    if num_block_starts == 0 {
        out.copy_from_slice(data);
        return;
    }

    let mut pos = 0;

    // Sample blocks until we fill the output buffer
    while pos < n {
        // Random starting position for this block
        let start = rng.random_range(0..num_block_starts);
        let block_end = (start + block_size).min(n);
        let block_len = block_end - start;

        // How much can we copy without overflowing output?
        let copy_len = block_len.min(n - pos);

        // Fast memcpy of the block
        out[pos..pos + copy_len].copy_from_slice(&data[start..start + copy_len]);

        pos += copy_len;
    }
}

/// Perform block bootstrap resampling on timing measurements.
///
/// Resamples measurements from a single class (fixed or random) while
/// preserving autocorrelation structure. Blocks of consecutive measurements
/// are sampled with replacement.
///
/// # Arguments
///
/// * `data` - Slice of timing measurements for one class
/// * `block_size` - Size of contiguous blocks to resample
/// * `rng` - Random number generator
///
/// # Returns
///
/// A new vector of resampled measurements with the same length as input.
///
/// # Algorithm
///
/// 1. Divide data into overlapping blocks of size `block_size`
/// 2. Sample blocks with replacement until we have n samples
/// 3. Truncate to exactly n samples
pub fn block_bootstrap_resample<R: Rng>(data: &[f64], block_size: usize, rng: &mut R) -> Vec<f64> {
    if data.is_empty() {
        return Vec::new();
    }

    let n = data.len();
    let block_size = block_size.max(1).min(n);

    // Number of possible starting positions for blocks
    let num_block_starts = n.saturating_sub(block_size) + 1;
    if num_block_starts == 0 {
        return data.to_vec();
    }

    let mut result = Vec::with_capacity(n);

    // Sample blocks until we have enough data
    while result.len() < n {
        // Random starting position for this block
        let start = rng.random_range(0..num_block_starts);
        let end = (start + block_size).min(n);

        // Add the block to our resampled data
        for &value in data.iter().take(end).skip(start) {
            if result.len() >= n {
                break;
            }
            result.push(value);
        }
    }

    result
}

/// Perform stratified block bootstrap on interleaved measurements.
///
/// When measurements alternate between fixed and random classes,
/// this function resamples within each class separately to preserve
/// both the autocorrelation structure and class separation.
///
/// # Arguments
///
/// * `fixed_data` - Measurements for the fixed input class
/// * `random_data` - Measurements for the random input class
/// * `block_size` - Size of contiguous blocks to resample
/// * `rng` - Random number generator
///
/// # Returns
///
/// A tuple of (resampled_fixed, resampled_random) vectors.
#[cfg(test)]
pub fn stratified_block_bootstrap<R: Rng>(
    fixed_data: &[f64],
    random_data: &[f64],
    block_size: usize,
    rng: &mut R,
) -> (Vec<f64>, Vec<f64>) {
    let resampled_fixed = block_bootstrap_resample(fixed_data, block_size, rng);
    let resampled_random = block_bootstrap_resample(random_data, block_size, rng);

    (resampled_fixed, resampled_random)
}

/// Perform joint block bootstrap resampling on interleaved measurements.
///
/// This preserves temporal pairing between fixed and random samples, which
/// captures the cross-covariance Cov(q_F, q_R) > 0 from common-mode noise.
/// Joint resampling gives the correct (smaller) Var(Δ), improving statistical
/// power compared to independent resampling.
///
/// # Arguments
///
/// * `interleaved` - Samples in measurement order, each tagged with its class
/// * `block_size` - Size of contiguous blocks to resample
/// * `rng` - Random number generator
///
/// # Returns
///
/// A new vector of resampled `TimingSample`s with the same length as input.
///
/// # Algorithm
///
/// 1. Sample blocks of consecutive measurements (preserving F/R pairing)
/// 2. The class labels travel with their timing values
/// 3. After resampling, caller splits by class and computes quantile difference
#[allow(dead_code)]
pub fn block_bootstrap_resample_joint<R: Rng>(
    interleaved: &[TimingSample],
    block_size: usize,
    rng: &mut R,
) -> Vec<TimingSample> {
    if interleaved.is_empty() {
        return Vec::new();
    }

    let n = interleaved.len();
    let block_size = block_size.max(1).min(n);

    // Number of possible starting positions for blocks
    let num_block_starts = n.saturating_sub(block_size) + 1;
    if num_block_starts == 0 {
        return interleaved.to_vec();
    }

    let mut result = Vec::with_capacity(n);

    // Sample blocks until we have enough data
    while result.len() < n {
        // Random starting position for this block
        let start = rng.random_range(0..num_block_starts);
        let end = (start + block_size).min(n);

        // Add the entire block (preserving class labels)
        for sample in interleaved.iter().take(end).skip(start) {
            if result.len() >= n {
                break;
            }
            result.push(*sample);
        }
    }

    result
}

/// Perform joint block bootstrap resampling into an existing buffer.
///
/// This is an optimized version that writes into a preallocated buffer.
/// Used in parallel bootstrap for better performance.
///
/// # Arguments
///
/// * `interleaved` - Samples in measurement order, each tagged with its class
/// * `block_size` - Size of contiguous blocks to resample
/// * `rng` - Random number generator
/// * `out` - Output buffer (must have same length as `interleaved`)
///
/// # Panics
///
/// Panics if `out.len() != interleaved.len()`.
pub fn block_bootstrap_resample_joint_into<R: Rng>(
    interleaved: &[TimingSample],
    block_size: usize,
    rng: &mut R,
    out: &mut [TimingSample],
) {
    assert_eq!(
        out.len(),
        interleaved.len(),
        "Output buffer must have same length as input data"
    );

    if interleaved.is_empty() {
        return;
    }

    let n = interleaved.len();
    let block_size = block_size.max(1).min(n);

    // Number of possible starting positions for blocks
    let num_block_starts = n.saturating_sub(block_size) + 1;
    if num_block_starts == 0 {
        out.copy_from_slice(interleaved);
        return;
    }

    let mut pos = 0;

    // Sample blocks until we fill the output buffer
    while pos < n {
        // Random starting position for this block
        let start = rng.random_range(0..num_block_starts);
        let block_end = (start + block_size).min(n);
        let block_len = block_end - start;

        // How much can we copy without overflowing output?
        let copy_len = block_len.min(n - pos);

        // Copy the block (preserving class labels)
        out[pos..pos + copy_len].copy_from_slice(&interleaved[start..start + copy_len]);

        pos += copy_len;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::SeedableRng;
    use rand_xoshiro::Xoshiro256PlusPlus;

    #[test]
    fn test_block_size_computation() {
        // Formula: ceil(1.3 * n^(1/3))
        // n=100:   1.3 * 100^(1/3) = 1.3 * 4.6416 = 6.03 -> ceil = 7
        // n=10000: 1.3 * 10000^(1/3) = 1.3 * 21.544 = 28.007 -> ceil = 29
        // n=30000: 1.3 * 30000^(1/3) = 1.3 * 31.072 = 40.39 -> ceil = 41
        assert_eq!(compute_block_size(100), 7);
        assert_eq!(compute_block_size(10000), 29);
        assert_eq!(compute_block_size(30000), 41);
        assert_eq!(compute_block_size(1), 2); // 1.3 * 1^(1/3) = 1.3 -> ceil = 2
        assert_eq!(compute_block_size(0), 1); // Minimum is 1
    }

    #[test]
    fn test_bootstrap_preserves_length() {
        let data: Vec<f64> = (0..100).map(|x| x as f64).collect();
        let mut rng = Xoshiro256PlusPlus::seed_from_u64(42);

        let resampled = block_bootstrap_resample(&data, 10, &mut rng);
        assert_eq!(resampled.len(), data.len());
    }

    #[test]
    fn test_bootstrap_samples_from_data() {
        let data: Vec<f64> = (0..100).map(|x| x as f64).collect();
        let mut rng = Xoshiro256PlusPlus::seed_from_u64(42);

        let resampled = block_bootstrap_resample(&data, 10, &mut rng);

        // All resampled values should be from original data
        for val in &resampled {
            assert!(data.contains(val));
        }
    }

    #[test]
    fn test_empty_data() {
        let data: Vec<f64> = vec![];
        let mut rng = Xoshiro256PlusPlus::seed_from_u64(42);

        let resampled = block_bootstrap_resample(&data, 10, &mut rng);
        assert!(resampled.is_empty());
    }

    #[test]
    fn test_stratified_bootstrap() {
        let fixed: Vec<f64> = (0..50).map(|x| x as f64).collect();
        let random: Vec<f64> = (50..100).map(|x| x as f64).collect();
        let mut rng = Xoshiro256PlusPlus::seed_from_u64(42);

        let (resampled_fixed, resampled_random) =
            stratified_block_bootstrap(&fixed, &random, 7, &mut rng);

        assert_eq!(resampled_fixed.len(), fixed.len());
        assert_eq!(resampled_random.len(), random.len());

        // Fixed resamples should come from fixed data
        for val in &resampled_fixed {
            assert!(fixed.contains(val));
        }

        // Random resamples should come from random data
        for val in &resampled_random {
            assert!(random.contains(val));
        }
    }
}