ordeal_cert/certificate.rs
1//! The v1 certificate pair and the model type (TR-075: extracted from
2//! `ordeal::solver`, which re-exports them unchanged). [`Certificate`] is
3//! the CNF + LRAT pair; [`Certificate::recheck`] runs the trusted
4//! `ordeal_lrat::check` over it, exactly as the solver does before it
5//! returns `Unsat`.
6
7/// A machine-checkable UNSAT certificate — a **self-contained, portable proof
8/// object**.
9///
10/// The solver (untrusted) emits an LRAT proof; the `ordeal-lrat` checker
11/// (the only trusted component) validated exactly these bytes against
12/// [`cnf`](Certificate::cnf) before this value was constructed. Because the
13/// certificate carries *both halves* — the DIMACS CNF and the LRAT refutation —
14/// a consumer can re-establish the UNSAT verdict **with zero trust in ordeal**
15/// by calling [`recheck`](Certificate::recheck) (or running `ordeal_lrat::check`
16/// directly). That independent re-check is the whole point: an UNSAT is
17/// believable because *you* can validate the proof, not because the solver says
18/// so. This is what a translation validator (synth) needs to turn a proof-less
19/// solver verdict into checkable evidence.
20#[derive(Clone, Debug, Default)]
21pub struct Certificate {
22 /// The checker-validated textual LRAT proof bytes.
23 pub lrat: Vec<u8>,
24 /// The exact DIMACS CNF clause set the LRAT proof refutes — the other half
25 /// of the checkable pair. Together with [`lrat`](Certificate::lrat) it is a
26 /// complete, independently-verifiable proof of unsatisfiability.
27 pub cnf: Vec<Vec<i32>>,
28}
29
30/// Why an independent [`Certificate::recheck`] could not confirm the proof.
31#[derive(Debug)]
32pub enum CertificateError {
33 /// The LRAT bytes were not valid UTF-8 (only possible for a hand-built
34 /// certificate; ones ordeal emits are always text).
35 NotText,
36 /// The trusted `ordeal-lrat` checker rejected the LRAT proof against the
37 /// carried CNF.
38 Rejected(ordeal_lrat::CheckError),
39}
40
41impl std::fmt::Display for CertificateError {
42 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43 match self {
44 CertificateError::NotText => write!(f, "LRAT certificate is not valid UTF-8"),
45 CertificateError::Rejected(e) => write!(f, "checker rejected certificate: {e:?}"),
46 }
47 }
48}
49
50impl std::error::Error for CertificateError {}
51
52impl Certificate {
53 /// The LRAT proof as text, if the bytes are valid UTF-8 (ordeal-emitted
54 /// certificates always are).
55 pub fn lrat_text(&self) -> Option<&str> {
56 std::str::from_utf8(&self.lrat).ok()
57 }
58
59 /// Independently re-validate this certificate with the trusted `ordeal-lrat`
60 /// checker — the *same* check ordeal ran internally before returning
61 /// `Unsat`, reproducible by any consumer with no faith in the (untrusted)
62 /// solver. `Ok(())` ⟺ the LRAT proof refutes [`cnf`](Certificate::cnf), so
63 /// the conjunction really is unsatisfiable.
64 pub fn recheck(&self) -> Result<(), CertificateError> {
65 let text = self.lrat_text().ok_or(CertificateError::NotText)?;
66 ordeal_lrat::check(&self.cnf, text).map_err(CertificateError::Rejected)
67 }
68}
69
70/// A satisfying assignment (counterexample) for a SAT query.
71///
72/// Each entry binds a variable name to the concrete bitvector value (as a
73/// `u128`, zero-extended for widths below 128) that witnesses satisfiability.
74#[derive(Clone, Debug, Default)]
75pub struct Model {
76 /// Variable-name → value assignments.
77 pub assignments: Vec<(String, u128)>,
78}