logicaffeine_verify/
certificate.rs1use std::collections::hash_map::DefaultHasher;
11use std::hash::{Hash, Hasher};
12
13use crate::ir::VerifyExpr;
14use serde::{Deserialize, Serialize};
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct ProofCertificate {
19 pub claim: VerifyClaim,
20 pub steps: Vec<ProofStep>,
21 pub axioms: Vec<String>,
22 pub checkable: bool,
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
27pub enum VerifyClaim {
28 Equivalent {
29 description: String,
30 },
31 Safe {
32 property: String,
33 bound: Option<u32>,
34 },
35 Unsafe {
36 counterexample: String,
37 },
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct ProofStep {
43 pub rule: String,
44 pub premises: Vec<usize>,
45 pub conclusion: String,
46 pub formula: Option<VerifyExpr>,
48 pub claim_binding: u64,
50}
51
52fn digest_claim(claim: &VerifyClaim) -> u64 {
54 let mut hasher = DefaultHasher::new();
55 match claim {
56 VerifyClaim::Equivalent { description } => {
57 "Equivalent".hash(&mut hasher);
58 description.hash(&mut hasher);
59 }
60 VerifyClaim::Safe { property, bound } => {
61 "Safe".hash(&mut hasher);
62 property.hash(&mut hasher);
63 bound.hash(&mut hasher);
64 }
65 VerifyClaim::Unsafe { counterexample } => {
66 "Unsafe".hash(&mut hasher);
67 counterexample.hash(&mut hasher);
68 }
69 }
70 hasher.finish()
71}
72
73pub fn digest_formula(expr: &VerifyExpr) -> u64 {
75 let mut hasher = DefaultHasher::new();
76 let serialized = serde_json::to_string(expr).unwrap_or_default();
77 serialized.hash(&mut hasher);
78 hasher.finish()
79}
80
81fn expected_conclusion_keyword(claim: &VerifyClaim) -> &'static str {
83 match claim {
84 VerifyClaim::Equivalent { .. } => "UNSAT",
85 VerifyClaim::Safe { .. } => "Verified",
86 VerifyClaim::Unsafe { .. } => "SAT",
87 }
88}
89
90pub fn generate_equivalence_certificate(
92 a_desc: &str,
93 b_desc: &str,
94 is_equivalent: bool,
95) -> ProofCertificate {
96 let claim = if is_equivalent {
97 VerifyClaim::Equivalent {
98 description: format!("{} ≡ {}", a_desc, b_desc),
99 }
100 } else {
101 VerifyClaim::Unsafe {
102 counterexample: format!("{} ≢ {}", a_desc, b_desc),
103 }
104 };
105
106 let binding = digest_claim(&claim);
107
108 let xor_formula = VerifyExpr::not(VerifyExpr::iff(
109 VerifyExpr::var(a_desc),
110 VerifyExpr::var(b_desc),
111 ));
112
113 ProofCertificate {
114 claim,
115 steps: vec![ProofStep {
116 rule: "Z3-SAT-check".into(),
117 premises: vec![],
118 conclusion: if is_equivalent {
119 "UNSAT: no distinguishing assignment exists".into()
120 } else {
121 "SAT: distinguishing assignment found".into()
122 },
123 formula: Some(xor_formula),
124 claim_binding: binding,
125 }],
126 axioms: vec!["propositional-logic".into(), "Z3-soundness".into()],
127 checkable: true,
128 }
129}
130
131pub fn generate_safety_certificate(property_desc: &str, k: u32, is_safe: bool) -> ProofCertificate {
133 let claim = if is_safe {
134 VerifyClaim::Safe {
135 property: property_desc.into(),
136 bound: Some(k),
137 }
138 } else {
139 VerifyClaim::Unsafe {
140 counterexample: format!("Counterexample at depth {}", k),
141 }
142 };
143
144 let binding = digest_claim(&claim);
145
146 let safety_formula = VerifyExpr::apply("G", vec![VerifyExpr::var(property_desc)]);
147
148 ProofCertificate {
149 claim,
150 steps: vec![ProofStep {
151 rule: if is_safe { "k-induction" } else { "BMC" }.into(),
152 premises: vec![],
153 conclusion: format!("Verified at depth k={}", k),
154 formula: Some(safety_formula),
155 claim_binding: binding,
156 }],
157 axioms: vec!["k-induction-soundness".into()],
158 checkable: true,
159 }
160}
161
162pub fn verify_certificate(cert: &ProofCertificate) -> bool {
172 if cert.steps.is_empty() {
173 return false;
174 }
175 if !cert.checkable {
176 return false;
177 }
178
179 let claim_digest = digest_claim(&cert.claim);
180
181 for (i, step) in cert.steps.iter().enumerate() {
182 for &premise in &step.premises {
184 if premise >= i {
185 return false;
186 }
187 }
188
189 if step.claim_binding != 0 && step.claim_binding != claim_digest {
191 return false;
192 }
193
194 if step.claim_binding != 0 && step.formula.is_none() {
197 return false;
198 }
199 }
200
201 let final_step = cert.steps.last().unwrap();
203 let expected_keyword = expected_conclusion_keyword(&cert.claim);
204 if !final_step.conclusion.contains(expected_keyword) {
205 return false;
206 }
207
208 true
209}