stwo-gpu 2.0.0

GPU-accelerated Circle STARK prover and verifier — ObelyZK fork of STWO with CUDA/Metal backend
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
//! Confidential Computing Mode Management for NVIDIA GPUs
//!
//! This module provides functionality to query and configure CC mode on
//! NVIDIA H100/H200/B200 GPUs.
//!
//! # CC Mode States
//!
//! - **Off**: Confidential Computing disabled
//! - **On**: CC enabled for production use
//! - **DevTools**: CC enabled with debugging capabilities

use super::{CcMode, TeeError, TeeResult};
use std::process::Command;

/// Query the current CC mode of a GPU
pub fn query_cc_mode(device_id: u32) -> TeeResult<CcMode> {
    // Try nvidia-smi first for CC mode
    if let Ok(mode) = query_cc_mode_nvidia_smi(device_id) {
        return Ok(mode);
    }

    // Try nvtrust-rs style query
    if let Ok(mode) = query_cc_mode_nvtrust(device_id) {
        return Ok(mode);
    }

    // Fallback: Check GPU driver sysfs
    query_cc_mode_sysfs(device_id)
}

/// Query CC mode via nvidia-smi
fn query_cc_mode_nvidia_smi(device_id: u32) -> TeeResult<CcMode> {
    let output = Command::new("nvidia-smi")
        .args([
            "-i",
            &device_id.to_string(),
            "--query-gpu=cc_mode",
            "--format=csv,noheader",
        ])
        .output()
        .map_err(|e| TeeError::DriverError(format!("nvidia-smi failed: {}", e)))?;

    if !output.status.success() {
        // CC mode query might not be available on older drivers
        return Err(TeeError::DriverError(
            "nvidia-smi cc_mode query not supported".into(),
        ));
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    parse_cc_mode(stdout.trim())
}

/// Query CC mode via nvtrust tool
#[allow(unused_variables)]
fn query_cc_mode_nvtrust(device_id: u32) -> TeeResult<CcMode> {
    // Try the official nvidia-smi conf-compute command
    let output = Command::new("nvidia-smi")
        .args(["conf-compute", "-gcs"])
        .output()
        .map_err(|e| TeeError::DriverError(format!("nvidia-smi conf-compute failed: {}", e)))?;

    if output.status.success() {
        let stdout = String::from_utf8_lossy(&output.stdout);

        // Parse the output for CC mode
        for line in stdout.lines() {
            if line.contains("CC Mode") || line.contains("Confidential Compute") {
                if line.contains("ON") || line.contains("Enabled") {
                    return Ok(CcMode::On);
                } else if line.contains("DEVTOOLS") || line.contains("DevTools") {
                    return Ok(CcMode::DevTools);
                } else if line.contains("OFF") || line.contains("Disabled") {
                    return Ok(CcMode::Off);
                }
            }
        }
    }

    Err(TeeError::DriverError("Could not parse CC mode".into()))
}

/// Query CC mode via sysfs
fn query_cc_mode_sysfs(device_id: u32) -> TeeResult<CcMode> {
    // Check sysfs for CC mode
    let _sysfs_path = format!(
        "/sys/bus/pci/devices/*/nvidia/{}/conf_compute_mode",
        device_id
    );

    // Use glob to find the actual path
    if let Ok(entries) = std::fs::read_dir("/sys/bus/pci/devices") {
        for entry in entries.flatten() {
            let cc_path = entry.path().join(format!("nvidia/{}/conf_compute_mode", device_id));
            if cc_path.exists() {
                if let Ok(mode_str) = std::fs::read_to_string(&cc_path) {
                    return parse_cc_mode(mode_str.trim());
                }
            }
        }
    }

    // Default to Off if we can't determine
    tracing::warn!(
        device_id = device_id,
        "Could not determine CC mode, assuming Off"
    );
    Ok(CcMode::Off)
}

/// Parse CC mode from string
fn parse_cc_mode(s: &str) -> TeeResult<CcMode> {
    let s_lower = s.to_lowercase();

    if s_lower.contains("on") || s_lower.contains("enabled") || s == "1" {
        Ok(CcMode::On)
    } else if s_lower.contains("devtools") || s_lower.contains("dev") || s == "2" {
        Ok(CcMode::DevTools)
    } else if s_lower.contains("off") || s_lower.contains("disabled") || s == "0" {
        Ok(CcMode::Off)
    } else {
        Err(TeeError::DriverError(format!(
            "Unknown CC mode: {}",
            s
        )))
    }
}

/// Set CC mode on a GPU (requires reboot)
///
/// # Warning
///
/// Changing CC mode requires a GPU reset or system reboot.
/// This should only be done during system setup, not during operation.
pub fn set_cc_mode(device_id: u32, mode: CcMode) -> TeeResult<()> {
    let mode_str = match mode {
        CcMode::Off => "off",
        CcMode::On => "on",
        CcMode::DevTools => "devtools",
    };

    // Use nvidia-smi conf-compute
    let output = Command::new("nvidia-smi")
        .args([
            "conf-compute",
            "-scc",
            mode_str,
            "-i",
            &device_id.to_string(),
        ])
        .output()
        .map_err(|e| TeeError::DriverError(format!("Failed to set CC mode: {}", e)))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(TeeError::DriverError(format!(
            "Failed to set CC mode: {}",
            stderr
        )));
    }

    tracing::info!(
        device_id = device_id,
        mode = %mode,
        "CC mode set (reboot required)"
    );

    Ok(())
}

/// Check if GPU supports Confidential Computing
pub fn supports_cc(device_id: u32) -> TeeResult<bool> {
    // Query GPU name
    let output = Command::new("nvidia-smi")
        .args([
            "-i",
            &device_id.to_string(),
            "--query-gpu=name",
            "--format=csv,noheader",
        ])
        .output()
        .map_err(|e| TeeError::DriverError(format!("nvidia-smi failed: {}", e)))?;

    if !output.status.success() {
        return Err(TeeError::DriverError("Could not query GPU name".into()));
    }

    let name = String::from_utf8_lossy(&output.stdout);
    let name_upper = name.to_uppercase();

    // Only Hopper (H100, H200) and Blackwell (B200) support CC
    let supported = name_upper.contains("H100")
        || name_upper.contains("H200")
        || name_upper.contains("B200")
        || name_upper.contains("B100");

    Ok(supported)
}

/// Get CC settings (detailed configuration)
pub fn query_cc_settings(device_id: u32) -> TeeResult<CcSettings> {
    let output = Command::new("nvidia-smi")
        .args(["conf-compute", "-gcs", "-i", &device_id.to_string()])
        .output()
        .map_err(|e| TeeError::DriverError(format!("nvidia-smi conf-compute failed: {}", e)))?;

    if !output.status.success() {
        return Err(TeeError::DriverError(
            "Failed to query CC settings".into(),
        ));
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    parse_cc_settings(&stdout)
}

/// CC Settings from the GPU
#[derive(Debug, Clone)]
pub struct CcSettings {
    /// Current CC mode
    pub mode: CcMode,
    /// CC mode that will take effect after reset
    pub pending_mode: Option<CcMode>,
    /// Protected PCIe enabled
    pub ppcie_enabled: bool,
    /// Multi-GPU mode
    pub multi_gpu_mode: Option<String>,
    /// Driver version
    pub driver_version: String,
    /// VBIOS version
    pub vbios_version: String,
}

fn parse_cc_settings(output: &str) -> TeeResult<CcSettings> {
    let mut settings = CcSettings {
        mode: CcMode::Off,
        pending_mode: None,
        ppcie_enabled: false,
        multi_gpu_mode: None,
        driver_version: String::new(),
        vbios_version: String::new(),
    };

    for line in output.lines() {
        let line = line.trim();

        if line.contains("CC Mode") || line.contains("Current CC Mode") {
            if line.contains("ON") {
                settings.mode = CcMode::On;
            } else if line.contains("DEVTOOLS") {
                settings.mode = CcMode::DevTools;
            }
        } else if line.contains("Pending CC Mode") {
            if line.contains("ON") {
                settings.pending_mode = Some(CcMode::On);
            } else if line.contains("DEVTOOLS") {
                settings.pending_mode = Some(CcMode::DevTools);
            } else if line.contains("OFF") {
                settings.pending_mode = Some(CcMode::Off);
            }
        } else if line.contains("PPCIE") || line.contains("Protected PCIe") {
            settings.ppcie_enabled = line.contains("Enabled") || line.contains("ON");
        } else if line.contains("Multi-GPU") {
            if let Some(mode) = line.split(':').nth(1) {
                settings.multi_gpu_mode = Some(mode.trim().to_string());
            }
        } else if line.contains("Driver Version") {
            if let Some(version) = line.split(':').nth(1) {
                settings.driver_version = version.trim().to_string();
            }
        } else if line.contains("VBIOS") {
            if let Some(version) = line.split(':').nth(1) {
                settings.vbios_version = version.trim().to_string();
            }
        }
    }

    Ok(settings)
}

/// Verify the GPU is properly configured for CC
pub fn verify_cc_configuration(device_id: u32) -> TeeResult<CcVerificationResult> {
    let mut result = CcVerificationResult::default();

    // Check if GPU supports CC
    result.gpu_supported = supports_cc(device_id)?;

    if !result.gpu_supported {
        return Ok(result);
    }

    // Check current CC mode
    result.cc_mode = query_cc_mode(device_id)?;
    result.cc_enabled = matches!(result.cc_mode, CcMode::On | CcMode::DevTools);

    // Check driver version
    if let Ok(settings) = query_cc_settings(device_id) {
        result.driver_version = settings.driver_version;
        result.ppcie_enabled = settings.ppcie_enabled;

        // Verify driver version is compatible (r550+)
        if let Some(major) = result.driver_version.split('.').next() {
            if let Ok(major_num) = major.parse::<u32>() {
                result.driver_compatible = major_num >= 550;
            }
        }
    }

    Ok(result)
}

/// Result of CC configuration verification
#[derive(Debug, Clone, Default)]
pub struct CcVerificationResult {
    /// GPU supports CC
    pub gpu_supported: bool,
    /// CC is currently enabled
    pub cc_enabled: bool,
    /// Current CC mode
    pub cc_mode: CcMode,
    /// PPCIE enabled
    pub ppcie_enabled: bool,
    /// Driver version
    pub driver_version: String,
    /// Driver is compatible (r550+)
    pub driver_compatible: bool,
}

impl Default for CcMode {
    fn default() -> Self {
        CcMode::Off
    }
}

impl CcVerificationResult {
    /// Check if configuration is ready for production
    pub fn is_production_ready(&self) -> bool {
        self.gpu_supported
            && self.cc_enabled
            && self.cc_mode == CcMode::On
            && self.driver_compatible
    }

    /// Get human-readable status
    pub fn status_message(&self) -> String {
        if !self.gpu_supported {
            return "GPU does not support Confidential Computing (need H100/H200/B200)".into();
        }

        if !self.cc_enabled {
            return format!(
                "CC mode is {}, needs to be enabled via: nvidia-smi conf-compute -scc on",
                self.cc_mode
            );
        }

        if !self.driver_compatible {
            return format!(
                "Driver version {} is not compatible, need r550 or later",
                self.driver_version
            );
        }

        if self.cc_mode == CcMode::DevTools {
            return "CC mode is DevTools (debugging enabled) - use 'on' for production".into();
        }

        "Configuration is production ready".into()
    }
}

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

    #[test]
    fn test_parse_cc_mode() {
        assert_eq!(parse_cc_mode("on").unwrap(), CcMode::On);
        assert_eq!(parse_cc_mode("ON").unwrap(), CcMode::On);
        assert_eq!(parse_cc_mode("Enabled").unwrap(), CcMode::On);
        assert_eq!(parse_cc_mode("1").unwrap(), CcMode::On);

        assert_eq!(parse_cc_mode("off").unwrap(), CcMode::Off);
        assert_eq!(parse_cc_mode("OFF").unwrap(), CcMode::Off);
        assert_eq!(parse_cc_mode("Disabled").unwrap(), CcMode::Off);
        assert_eq!(parse_cc_mode("0").unwrap(), CcMode::Off);

        assert_eq!(parse_cc_mode("devtools").unwrap(), CcMode::DevTools);
        assert_eq!(parse_cc_mode("DEVTOOLS").unwrap(), CcMode::DevTools);
        assert_eq!(parse_cc_mode("2").unwrap(), CcMode::DevTools);
    }

    #[test]
    fn test_verification_result() {
        let mut result = CcVerificationResult::default();
        assert!(!result.is_production_ready());

        result.gpu_supported = true;
        result.cc_enabled = true;
        result.cc_mode = CcMode::On;
        result.driver_compatible = true;

        assert!(result.is_production_ready());
    }
}