File size: 5,979 Bytes
9425aed | 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 | //! Execution Receipt Generation and Verification
//!
//! Cryptographically sealed execution receipts for provenance
use crate::{Job, Result, ShadowRpgError};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sha2::{Sha256, Digest};
use uuid::Uuid;
/// Execution receipt with cryptographic seal
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionReceipt {
pub receipt_id: Uuid,
pub job_id: Uuid,
pub job_name: String,
pub source_hash: String,
pub compilation_hash: String,
pub target_backend: String,
pub submitted_at: DateTime<Utc>,
pub started_at: Option<DateTime<Utc>>,
pub completed_at: Option<DateTime<Utc>>,
pub status: String,
pub result_hash: Option<String>,
pub metadata: serde_json::Value,
pub seal: String,
}
impl ExecutionReceipt {
pub fn from_job(job: &Job, result_data: Option<&str>) -> Self {
let receipt_id = Uuid::new_v4();
let result_hash = result_data.map(|data| Self::hash_data(data));
let source_hash = Self::hash_data(&job.source_code);
let metadata = serde_json::json!({
"priority": format!("{:?}", job.priority),
"language": job.source_language,
"shots": job.shots,
});
let seal = Self::compute_seal(
receipt_id,
job.job_id,
&source_hash,
job.compilation_hash.as_deref().unwrap_or(""),
&job.target_backend,
result_hash.as_deref(),
);
Self {
receipt_id,
job_id: job.job_id,
job_name: job.job_name.clone(),
source_hash,
compilation_hash: job.compilation_hash.clone().unwrap_or_default(),
target_backend: job.target_backend.clone(),
submitted_at: job.submitted_at,
started_at: job.started_at,
completed_at: job.completed_at,
status: format!("{:?}", job.status),
result_hash,
metadata,
seal,
}
}
fn hash_data(data: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(data.as_bytes());
format!("{:x}", hasher.finalize())
}
fn compute_seal(
receipt_id: Uuid,
job_id: Uuid,
source_hash: &str,
compilation_hash: &str,
target_backend: &str,
result_hash: Option<&str>,
) -> String {
let mut hasher = Sha256::new();
hasher.update(receipt_id.as_bytes());
hasher.update(job_id.as_bytes());
hasher.update(source_hash.as_bytes());
hasher.update(compilation_hash.as_bytes());
hasher.update(target_backend.as_bytes());
if let Some(rh) = result_hash {
hasher.update(rh.as_bytes());
}
format!("{:x}", hasher.finalize())
}
pub fn verify_seal(&self) -> bool {
let computed = Self::compute_seal(
self.receipt_id,
self.job_id,
&self.source_hash,
&self.compilation_hash,
&self.target_backend,
self.result_hash.as_deref(),
);
computed == self.seal
}
pub fn to_json(&self) -> Result<String> {
serde_json::to_string_pretty(self)
.map_err(|e| ShadowRpgError::Serialization(format!("Failed to serialize receipt: {}", e)))
}
pub fn from_json(json: &str) -> Result<Self> {
serde_json::from_str(json)
.map_err(|e| ShadowRpgError::Serialization(format!("Failed to deserialize receipt: {}", e)))
}
}
/// Receipt chain for audit trail
#[derive(Debug)]
pub struct ReceiptChain {
receipts: Vec<ExecutionReceipt>,
}
impl ReceiptChain {
pub fn new() -> Self {
Self {
receipts: Vec::new(),
}
}
pub fn add(&mut self, receipt: ExecutionReceipt) -> Result<()> {
if !receipt.verify_seal() {
return Err(ShadowRpgError::Receipt("Receipt seal verification failed".to_string()));
}
self.receipts.push(receipt);
Ok(())
}
pub fn verify_all(&self) -> bool {
self.receipts.iter().all(|r| r.verify_seal())
}
pub fn len(&self) -> usize {
self.receipts.len()
}
pub fn is_empty(&self) -> bool {
self.receipts.is_empty()
}
pub fn get(&self, index: usize) -> Option<&ExecutionReceipt> {
self.receipts.get(index)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::JobPriority;
#[test]
fn test_receipt_seal() {
let job = Job::new("test".to_string(), "code".to_string(), "qasm2".to_string(), "sim".to_string())
.with_priority(JobPriority::Normal);
let receipt = ExecutionReceipt::from_job(&job, Some("result_data"));
assert!(receipt.verify_seal());
}
#[test]
fn test_receipt_json_roundtrip() {
let job = Job::new("test".to_string(), "code".to_string(), "qasm2".to_string(), "sim".to_string());
let receipt = ExecutionReceipt::from_job(&job, None);
let json = receipt.to_json().unwrap();
let restored = ExecutionReceipt::from_json(&json).unwrap();
assert_eq!(receipt.receipt_id, restored.receipt_id);
assert!(restored.verify_seal());
}
#[test]
fn test_receipt_chain() {
let mut chain = ReceiptChain::new();
let job1 = Job::new("j1".to_string(), "c1".to_string(), "qasm2".to_string(), "sim".to_string());
let receipt1 = ExecutionReceipt::from_job(&job1, None);
chain.add(receipt1).unwrap();
assert_eq!(chain.len(), 1);
assert!(chain.verify_all());
}
}
// Made with Bob
|