lib-q-sca-test 0.0.6

Side-channel validation harnesses (TVLA, statistical timing) for libQ PQC crates
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
//! Structured evaluation reports for side-channel self-certification.
//!
//! A self-certification run produces an auditable artifact: a set of
//! [`EvaluationReport`] entries (one per screened path) wrapped in a
//! [`SelfCertReport`] that records the capture [`EnvironmentInfo`]. Both serialize
//! to JSON (machine-readable evidence) and Markdown (human review).
//!
//! This module records measurements and verdicts; it does not assert
//! certification-grade resistance. See [`docs/sca-self-certification.md`] for the
//! methodology, gates, and the boundary between self-certification and accredited
//! laboratory evaluation.
//!
//! [`docs/sca-self-certification.md`]: ../../docs/sca-self-certification.md

use std::time::{
    SystemTime,
    UNIX_EPOCH,
};

/// Outcome of a single leakage screen against the configured threshold.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Verdict {
    /// Test statistic is finite and below the absolute threshold.
    Pass,
    /// Test statistic is finite and at or above the absolute threshold.
    Fail,
    /// Statistic could not be computed (too few samples, zero variance, non-finite).
    Inconclusive,
}

impl Verdict {
    /// Lowercase identifier used in JSON and Markdown output.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Verdict::Pass => "pass",
            Verdict::Fail => "fail",
            Verdict::Inconclusive => "inconclusive",
        }
    }

    /// Derive a verdict from an optional test statistic and an absolute threshold.
    #[must_use]
    pub fn from_statistic(statistic: Option<f64>, abs_threshold: f64) -> Self {
        match statistic {
            Some(t) if t.is_finite() => {
                if t.abs() < abs_threshold {
                    Verdict::Pass
                } else {
                    Verdict::Fail
                }
            }
            _ => Verdict::Inconclusive,
        }
    }
}

/// Measurement channel that produced a screen's samples.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Channel {
    /// Software wall-clock timing (`std::time::Instant`); subject to scheduler noise.
    WallClockTiming,
    /// Externally acquired traces (power, EM, or cycle counts) ingested from files.
    IngestedTrace,
}

impl Channel {
    /// Lowercase identifier used in JSON and Markdown output.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Channel::WallClockTiming => "wall_clock_timing",
            Channel::IngestedTrace => "ingested_trace",
        }
    }
}

/// Capture-time environment metadata recorded alongside every report.
///
/// Fields are limited to values obtainable without `unsafe` or external crates so
/// the harness keeps `#![forbid(unsafe_code)]`. High-fidelity CPU/microarchitecture
/// identification belongs in the acquisition rig that feeds the
/// [`Channel::IngestedTrace`] path.
#[derive(Clone, Debug)]
pub struct EnvironmentInfo {
    /// Target operating system (`std::env::consts::OS`).
    pub os: &'static str,
    /// Target architecture (`std::env::consts::ARCH`).
    pub arch: &'static str,
    /// Target pointer width in bits.
    pub pointer_width: u32,
    /// `lib-q-sca-test` crate version.
    pub harness_version: &'static str,
    /// Seconds since the Unix epoch at capture time (`0` if the clock is before epoch).
    pub timestamp_unix: u64,
}

impl EnvironmentInfo {
    /// Capture the current build/runtime environment.
    #[must_use]
    pub fn capture() -> Self {
        let timestamp_unix = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        Self {
            os: std::env::consts::OS,
            arch: std::env::consts::ARCH,
            pointer_width: (usize::BITS),
            harness_version: env!("CARGO_PKG_VERSION"),
            timestamp_unix,
        }
    }

    fn to_json(&self) -> String {
        format!(
            "{{\"os\":{},\"arch\":{},\"pointer_width\":{},\"harness_version\":{},\"timestamp_unix\":{}}}",
            json_string(self.os),
            json_string(self.arch),
            self.pointer_width,
            json_string(self.harness_version),
            self.timestamp_unix,
        )
    }
}

/// A single leakage screen result.
#[derive(Clone, Debug)]
pub struct EvaluationReport {
    /// Identifier of the path under test (e.g. `"lib-q-ml-kem:decapsulate"`).
    pub target: String,
    /// Measurement channel.
    pub channel: Channel,
    /// Samples per class (fixed and random each contribute this many).
    pub samples_per_class: usize,
    /// Welch *t*-statistic, if computable.
    pub t_statistic: Option<f64>,
    /// Absolute threshold applied to `|t|` (TVLA default `4.5`).
    pub abs_t_threshold: f64,
    /// Verdict derived from `t_statistic` and `abs_t_threshold`.
    pub verdict: Verdict,
    /// Free-form notes (methodology caveats, class construction, etc.).
    pub notes: String,
}

impl EvaluationReport {
    /// Build a report, deriving the [`Verdict`] from the statistic and threshold.
    #[must_use]
    pub fn new(
        target: impl Into<String>,
        channel: Channel,
        samples_per_class: usize,
        t_statistic: Option<f64>,
        abs_t_threshold: f64,
        notes: impl Into<String>,
    ) -> Self {
        let verdict = Verdict::from_statistic(t_statistic, abs_t_threshold);
        Self {
            target: target.into(),
            channel,
            samples_per_class,
            t_statistic,
            abs_t_threshold,
            verdict,
            notes: notes.into(),
        }
    }

    /// Serialize this entry to a single-line JSON object.
    #[must_use]
    pub fn to_json(&self) -> String {
        format!(
            "{{\"target\":{},\"channel\":{},\"samples_per_class\":{},\"t_statistic\":{},\"abs_t_threshold\":{},\"verdict\":{},\"notes\":{}}}",
            json_string(&self.target),
            json_string(self.channel.as_str()),
            self.samples_per_class,
            json_number(self.t_statistic),
            json_f64(self.abs_t_threshold),
            json_string(self.verdict.as_str()),
            json_string(&self.notes),
        )
    }

    /// Render this entry as a Markdown table row (matching [`SelfCertReport::to_markdown`]).
    #[must_use]
    pub fn to_markdown_row(&self) -> String {
        let t = match self.t_statistic {
            Some(v) if v.is_finite() => format!("{v:.4}"),
            _ => "n/a".to_string(),
        };
        format!(
            "| `{}` | {} | {} | {} | {:.2} | {} |",
            self.target,
            self.channel.as_str(),
            self.samples_per_class,
            t,
            self.abs_t_threshold,
            self.verdict.as_str(),
        )
    }
}

/// A battery of [`EvaluationReport`] entries with shared capture environment.
#[derive(Clone, Debug)]
pub struct SelfCertReport {
    /// Capture environment shared by every entry.
    pub environment: EnvironmentInfo,
    /// Individual screen results.
    pub reports: Vec<EvaluationReport>,
}

impl SelfCertReport {
    /// Create an empty battery capturing the current environment.
    #[must_use]
    pub fn new() -> Self {
        Self {
            environment: EnvironmentInfo::capture(),
            reports: Vec::new(),
        }
    }

    /// Append a screen result.
    pub fn push(&mut self, report: EvaluationReport) {
        self.reports.push(report);
    }

    /// `true` only if every entry returned [`Verdict::Pass`].
    ///
    /// An [`Verdict::Inconclusive`] entry is **not** a pass: a self-certification run
    /// that could not compute a statistic has not demonstrated the property.
    #[must_use]
    pub fn all_pass(&self) -> bool {
        !self.reports.is_empty() && self.reports.iter().all(|r| r.verdict == Verdict::Pass)
    }

    /// Number of entries with the given verdict.
    #[must_use]
    pub fn count(&self, verdict: Verdict) -> usize {
        self.reports.iter().filter(|r| r.verdict == verdict).count()
    }

    /// Serialize the battery to a single-line JSON object.
    #[must_use]
    pub fn to_json(&self) -> String {
        let mut entries = String::new();
        for (idx, report) in self.reports.iter().enumerate() {
            if idx > 0 {
                entries.push(',');
            }
            entries.push_str(&report.to_json());
        }
        format!(
            "{{\"schema\":\"libq.sca.self-cert.v1\",\"environment\":{},\"reports\":[{}]}}",
            self.environment.to_json(),
            entries,
        )
    }

    /// Serialize the battery to a human-readable Markdown summary.
    #[must_use]
    pub fn to_markdown(&self) -> String {
        let mut out = String::new();
        out.push_str("# libQ side-channel self-certification report\n\n");
        out.push_str(&format!(
            "- Schema: `libq.sca.self-cert.v1`\n- OS/arch: `{}`/`{}` ({}-bit)\n- Harness: `lib-q-sca-test {}`\n- Timestamp (Unix): `{}`\n\n",
            self.environment.os,
            self.environment.arch,
            self.environment.pointer_width,
            self.environment.harness_version,
            self.environment.timestamp_unix,
        ));
        out.push_str(&format!(
            "Summary: {} pass, {} fail, {} inconclusive ({} total).\n\n",
            self.count(Verdict::Pass),
            self.count(Verdict::Fail),
            self.count(Verdict::Inconclusive),
            self.reports.len(),
        ));
        out.push_str("| Target | Channel | Samples/class | \\|t\\| stat | Threshold | Verdict |\n");
        out.push_str("|--------|---------|--------------:|----------:|----------:|---------|\n");
        for report in &self.reports {
            out.push_str(&report.to_markdown_row());
            out.push('\n');
        }
        out.push_str(
            "\nWall-clock timing screens are pre-laboratory regression evidence, not an \
             independent side-channel evaluation. See `docs/sca-self-certification.md`.\n",
        );
        out
    }
}

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

/// Escape and quote a string for JSON output.
fn json_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for ch in s.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

/// Render an `f64` as a JSON number, or `null` when non-finite.
fn json_f64(value: f64) -> String {
    if value.is_finite() {
        format!("{value}")
    } else {
        "null".to_string()
    }
}

/// Render an optional `f64` as a JSON number, or `null` when absent/non-finite.
fn json_number(value: Option<f64>) -> String {
    match value {
        Some(v) => json_f64(v),
        None => "null".to_string(),
    }
}

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

    #[test]
    fn verdict_thresholding() {
        assert_eq!(Verdict::from_statistic(Some(1.0), 4.5), Verdict::Pass);
        assert_eq!(Verdict::from_statistic(Some(-1.0), 4.5), Verdict::Pass);
        assert_eq!(Verdict::from_statistic(Some(4.5), 4.5), Verdict::Fail);
        assert_eq!(Verdict::from_statistic(Some(9.0), 4.5), Verdict::Fail);
        assert_eq!(Verdict::from_statistic(None, 4.5), Verdict::Inconclusive);
        assert_eq!(
            Verdict::from_statistic(Some(f64::NAN), 4.5),
            Verdict::Inconclusive
        );
    }

    #[test]
    fn report_json_is_well_formed_single_line() {
        let r = EvaluationReport::new(
            "lib-q-ml-kem:decapsulate",
            Channel::WallClockTiming,
            1024,
            Some(2.5),
            4.5,
            "fixed vs random dk/ct",
        );
        let json = r.to_json();
        assert!(json.starts_with('{') && json.ends_with('}'));
        assert!(!json.contains('\n'));
        assert!(json.contains("\"verdict\":\"pass\""));
        assert!(json.contains("\"channel\":\"wall_clock_timing\""));
    }

    #[test]
    fn non_finite_statistic_serializes_as_null() {
        let r = EvaluationReport::new("x", Channel::IngestedTrace, 0, Some(f64::INFINITY), 4.5, "");
        assert!(r.to_json().contains("\"t_statistic\":null"));
        assert_eq!(r.verdict, Verdict::Inconclusive);
    }

    #[test]
    fn json_string_escapes_control_and_quotes() {
        let s = json_string("a\"b\\c\nd\te");
        assert_eq!(s, "\"a\\\"b\\\\c\\nd\\te\"");
    }

    #[test]
    fn battery_all_pass_requires_nonempty_and_all_pass() {
        let mut battery = SelfCertReport::new();
        assert!(!battery.all_pass(), "empty battery is not a pass");
        battery.push(EvaluationReport::new(
            "a",
            Channel::WallClockTiming,
            8,
            Some(1.0),
            4.5,
            "",
        ));
        assert!(battery.all_pass());
        battery.push(EvaluationReport::new(
            "b",
            Channel::WallClockTiming,
            8,
            None,
            4.5,
            "",
        ));
        assert!(!battery.all_pass(), "inconclusive entry blocks all_pass");
        assert_eq!(battery.count(Verdict::Inconclusive), 1);
    }

    #[test]
    fn battery_markdown_and_json_render() {
        let mut battery = SelfCertReport::new();
        battery.push(EvaluationReport::new(
            "lib-q-ml-dsa:sign",
            Channel::WallClockTiming,
            512,
            Some(3.1),
            4.5,
            "fixed vs random signing key",
        ));
        let md = battery.to_markdown();
        assert!(md.contains("self-certification report"));
        assert!(md.contains("lib-q-ml-dsa:sign"));
        let json = battery.to_json();
        assert!(json.contains("\"schema\":\"libq.sca.self-cert.v1\""));
        assert!(json.contains("\"reports\":["));
    }
}