aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-evidence / src / verify.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Config A bundle verification. |
| 17 | //! |
| 18 | //! `errors` means the capture itself cannot be trusted as reference evidence. |
| 19 | //! `m0_open` lists what still keeps the M0 freeze from closing even when the |
| 20 | //! capture is sound (for example, an untested native-boot rollback). |
| 21 | |
| 22 | use crate::canon::{checksum, CHECKSUM_FIELD}; |
| 23 | use serde_json::Value; |
| 24 | |
| 25 | /// Output tokens below this measure start-up latency, not decode speed. |
| 26 | pub const MIN_OUTPUT_TOKENS: u64 = 32; |
| 27 | pub const MIN_SAMPLES: usize = 3; |
| 28 | |
| 29 | #[derive(Debug, Default)] |
| 30 | pub struct Report { |
| 31 | pub errors: Vec<String>, |
| 32 | pub m0_open: Vec<String>, |
| 33 | } |
| 34 | |
| 35 | fn nonempty(v: &Value) -> bool { |
| 36 | v.as_str().is_some_and(|s| !s.trim().is_empty()) |
| 37 | } |
| 38 | |
| 39 | fn is_hex(v: &Value, len: usize) -> bool { |
| 40 | v.as_str().is_some_and(|s| { |
| 41 | s.len() == len |
| 42 | && s.bytes() |
| 43 | .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) |
| 44 | }) |
| 45 | } |
| 46 | |
| 47 | pub fn check(bundle: &Value) -> Report { |
| 48 | let mut r = Report::default(); |
| 49 | let mut err = |m: &str| r.errors.push(m.to_string()); |
| 50 | |
| 51 | if bundle[CHECKSUM_FIELD].as_str() != Some(checksum(bundle).as_str()) { |
| 52 | err("bundle checksum mismatch"); |
| 53 | } |
| 54 | if bundle["schema_version"] != crate::SCHEMA_VERSION { |
| 55 | err("unsupported evidence schema"); |
| 56 | } |
| 57 | let id = &bundle["config_identity"]; |
| 58 | if ![ |
| 59 | "host", |
| 60 | "kernel", |
| 61 | "architecture", |
| 62 | "os_distribution", |
| 63 | "kernel_command_line", |
| 64 | ] |
| 65 | .iter() |
| 66 | .all(|k| nonempty(&id[*k])) |
| 67 | { |
| 68 | err("incomplete host identity"); |
| 69 | } |
| 70 | let capsule = &bundle["machine_capsule"]; |
| 71 | if capsule["firmware"]["uefi_present"] != true { |
| 72 | err("UEFI was not observed"); |
| 73 | } |
| 74 | let accel = capsule["accelerators"].as_array(); |
| 75 | if !accel.is_some_and(|a| { |
| 76 | !a.is_empty() |
| 77 | && a.iter() |
| 78 | .all(|g| nonempty(&g["pci_bus_id"]) && nonempty(&g["vendor_device"])) |
| 79 | }) { |
| 80 | err("GPU identity was not observed"); |
| 81 | } |
| 82 | let cores = capsule["cpu"]["cores"].as_u64().unwrap_or(0); |
| 83 | let part_sum: u64 = capsule["cpu"]["parts"] |
| 84 | .as_object() |
| 85 | .map(|m| m.values().filter_map(Value::as_u64).sum()) |
| 86 | .unwrap_or(0); |
| 87 | if cores == 0 || part_sum != cores { |
| 88 | err("CPU core inventory missing or inconsistent with core types"); |
| 89 | } |
| 90 | if capsule["memory"]["total_kb"].as_u64().unwrap_or(0) == 0 { |
| 91 | err("memory size missing"); |
| 92 | } |
| 93 | if capsule["block_devices"] |
| 94 | .as_array() |
| 95 | .is_none_or(Vec::is_empty) |
| 96 | { |
| 97 | err("block device inventory missing"); |
| 98 | } |
| 99 | let model = &bundle["baseline_model"]; |
| 100 | if !is_hex(&model["sha256"], 64) || model["size_bytes"].as_u64().unwrap_or(0) == 0 { |
| 101 | err("baseline model digest or size missing"); |
| 102 | } |
| 103 | match bundle["workspace_repositories"].as_array() { |
| 104 | Some(repos) if !repos.is_empty() => { |
| 105 | for repo in repos { |
| 106 | let name = repo["name"].as_str().unwrap_or("?"); |
| 107 | if !is_hex(&repo["commit"], 40) { |
| 108 | err(&format!("repository {name} has no commit")); |
| 109 | } |
| 110 | if repo["dirty"] != false { |
| 111 | err(&format!( |
| 112 | "repository {name} was dirty at capture; the freeze is not reproducible" |
| 113 | )); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | _ => err("workspace commit inventory missing"), |
| 118 | } |
| 119 | let samples = bundle["reference_workload"]["samples"] |
| 120 | .as_array() |
| 121 | .cloned() |
| 122 | .unwrap_or_default(); |
| 123 | let complete = samples.iter().all(|s| { |
| 124 | s["ttft_ms"].as_f64().unwrap_or(0.0) > 0.0 |
| 125 | && s["output_tokens"].as_u64().unwrap_or(0) >= MIN_OUTPUT_TOKENS |
| 126 | && s["decode_tokens_per_second"].as_f64().unwrap_or(0.0) > 0.0 |
| 127 | }); |
| 128 | if samples.len() < MIN_SAMPLES || !complete { |
| 129 | err(&format!( |
| 130 | "{MIN_SAMPLES}+ inference samples with at least {MIN_OUTPUT_TOKENS} output tokens and a measured decode rate required" |
| 131 | )); |
| 132 | } |
| 133 | |
| 134 | let recovery = &bundle["recovery_procedure"]; |
| 135 | if recovery["status"] != "verified" { |
| 136 | r.m0_open.push(format!( |
| 137 | "recovery procedure is {}", |
| 138 | recovery["status"].as_str().unwrap_or("unknown") |
| 139 | )); |
| 140 | } |
| 141 | if recovery["native_boot_rollback_tested"] != true { |
| 142 | r.m0_open |
| 143 | .push("native boot rollback has not been tested on Machine 1".into()); |
| 144 | } |
| 145 | r |
| 146 | } |
| 147 | |
| 148 | #[cfg(test)] |
| 149 | mod tests { |
| 150 | use super::*; |
| 151 | use crate::canon::seal; |
| 152 | use serde_json::json; |
| 153 | |
| 154 | fn good() -> Value { |
| 155 | let sample = |i| json!({"index": i, "ttft_ms": 80.0, "total_ms": 2080.0, "output_tokens": 256, "decode_tokens_per_second": 127.5}); |
| 156 | json!({ |
| 157 | "schema_version": crate::SCHEMA_VERSION, |
| 158 | "config_identity": {"host": "spark", "kernel": "7.0", "architecture": "aarch64", "os_distribution": "Ubuntu", "kernel_command_line": "ro"}, |
| 159 | "machine_capsule": { |
| 160 | "firmware": {"uefi_present": true}, |
| 161 | "accelerators": [{"pci_bus_id": "000f:01:00.0", "vendor_device": "10de:2e12"}], |
| 162 | "cpu": {"cores": 20, "parts": {"0xd85": 10, "0xd87": 10}}, |
| 163 | "memory": {"total_kb": 127598748}, |
| 164 | "block_devices": [{"name": "nvme0n1", "size_bytes": 4000000000000u64}] |
| 165 | }, |
| 166 | "baseline_model": {"sha256": "3f5a22426976ab26cfe84dba63c1d08391717abb1af893e10f1b2968d862dcc1", "size_bytes": 807694368}, |
| 167 | "workspace_repositories": [{"name": "aienos", "commit": "f6350b548cccc53ec03825021112be75c2b30667", "dirty": false}], |
| 168 | "reference_workload": {"samples": [sample(0), sample(1), sample(2)]}, |
| 169 | "recovery_procedure": {"status": "documented_only", "native_boot_rollback_tested": false} |
| 170 | }) |
| 171 | } |
| 172 | |
| 173 | #[test] |
| 174 | fn sound_capture_passes_but_m0_stays_open_until_rollback_is_tested() { |
| 175 | let r = check(&seal(good())); |
| 176 | assert!(r.errors.is_empty(), "{:?}", r.errors); |
| 177 | assert_eq!(r.m0_open.len(), 2); |
| 178 | } |
| 179 | |
| 180 | #[test] |
| 181 | fn tampering_dirty_repos_and_toy_workloads_are_rejected() { |
| 182 | let mut tampered = seal(good()); |
| 183 | tampered["machine_capsule"]["cpu"]["cores"] = json!(21); |
| 184 | assert!(check(&tampered) |
| 185 | .errors |
| 186 | .iter() |
| 187 | .any(|e| e.contains("checksum"))); |
| 188 | |
| 189 | let mut dirty = good(); |
| 190 | dirty["workspace_repositories"][0]["dirty"] = json!(true); |
| 191 | assert!(check(&seal(dirty)) |
| 192 | .errors |
| 193 | .iter() |
| 194 | .any(|e| e.contains("dirty"))); |
| 195 | |
| 196 | let mut toy = good(); |
| 197 | toy["reference_workload"]["samples"][1]["output_tokens"] = json!(2); |
| 198 | assert!(check(&seal(toy)) |
| 199 | .errors |
| 200 | .iter() |
| 201 | .any(|e| e.contains("output tokens"))); |
| 202 | |
| 203 | let mut mismatched = good(); |
| 204 | mismatched["machine_capsule"]["cpu"]["parts"] = json!({"0xd85": 10}); |
| 205 | assert!(check(&seal(mismatched)) |
| 206 | .errors |
| 207 | .iter() |
| 208 | .any(|e| e.contains("CPU"))); |
| 209 | } |
| 210 | |
| 211 | #[test] |
| 212 | fn verified_recovery_closes_m0() { |
| 213 | let mut closed = good(); |
| 214 | closed["recovery_procedure"] = |
| 215 | json!({"status": "verified", "native_boot_rollback_tested": true}); |
| 216 | let r = check(&seal(closed)); |
| 217 | assert!(r.errors.is_empty() && r.m0_open.is_empty()); |
| 218 | } |
| 219 | } |