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
//! Differential test for the CDCL solver against brute force.
//!
//! The BDD and SAT engines are compared in `differential.rs`, but when they disagree that
//! test cannot say *which* engine is wrong. This one has an oracle the solver cannot share
//! code with: for a small number of variables, enumerate every assignment and check the
//! clauses directly. It also checks a returned model actually satisfies the clauses, which
//! catches a solver that reports `Sat` for the wrong reason.
use verif::sat::{self, Lit, Sat};
struct Lcg(u64);
impl Lcg {
fn next(&mut self) -> u64 {
self.0 = self.0.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
self.0 >> 11
}
fn below(&mut self, n: u64) -> u64 {
self.next() % n
}
}
fn brute(clauses: &[Vec<u32>], nvars: u32) -> bool {
for m in 0u32..(1u32 << nvars) {
let ok = clauses.iter().all(|c| {
c.iter().any(|&l| {
let v = l >> 1;
let sign = l & 1 == 1;
let val = (m >> v) & 1 == 1;
val != sign
})
});
if ok {
return true;
}
}
false
}
#[test]
fn solver_agrees_with_brute_force_on_random_cnfs() {
let mut r = Lcg(0xdead_beef_1234_5678);
let mut sat_seen = 0u32;
let mut unsat_seen = 0u32;
for trial in 0..2000 {
let nvars = 1 + r.below(6) as u32; // 1..=6 vars, so 2..=64 assignments
let nclauses = 1 + r.below(14) as usize;
let mut clauses: Vec<Vec<u32>> = Vec::new();
for _ in 0..nclauses {
let k = 1 + r.below(3) as usize;
let mut c: Vec<u32> = Vec::new();
for _ in 0..k {
let v = r.below(nvars as u64) as u32;
let sign = r.below(2) as u32;
let l = (v << 1) | sign;
if !c.contains(&l) && !c.contains(&(l ^ 1)) {
c.push(l);
}
}
if c.is_empty() {
continue;
}
clauses.push(c);
}
let expect = brute(&clauses, nvars);
// Encode: one SAT variable per CNF variable, clauses via add_clause.
let (got, model) = sat::query(|cnf| {
// Materialise the input bits so variable indices line up with `v`.
// `input_bit(0, v)` is input key `v`, which is what `Model::bit(v)` reads.
let vars: Vec<Lit> = (0..nvars).map(|v| cnf.input_bit(0, v)).collect();
for c in &clauses {
let lits: Vec<Lit> = c
.iter()
.map(|&l| {
let base = vars[(l >> 1) as usize];
if l & 1 == 1 {
base.not()
} else {
base
}
})
.collect();
cnf.add_clause(&lits);
}
cnf.true_lit()
});
match got {
Sat::Unknown => continue, // honest, and the caps are far away here
Sat::Sat => {
sat_seen += 1;
assert!(expect, "trial {trial}: claimed Sat for an unsatisfiable CNF");
// The model must actually satisfy every clause.
for (i, c) in clauses.iter().enumerate() {
let ok = c.iter().any(|&l| {
let v = l >> 1;
let sign = l & 1 == 1;
let bit = model.bit(v).unwrap_or(false);
bit != sign
});
assert!(ok, "trial {trial}: model does not satisfy clause {i}");
}
}
Sat::Unsat => {
unsat_seen += 1;
if expect {
let mut txt = String::new();
for c in &clauses {
txt.push_str(" (");
for (i, &l) in c.iter().enumerate() {
if i > 0 { txt.push_str(" | "); }
if l & 1 == 1 { txt.push('!'); }
txt.push_str(&format!("x{}", l >> 1));
}
txt.push_str(")\n");
}
panic!("trial {trial}: claimed Unsat for a satisfiable CNF (nvars={nvars}):\n{txt}");
}
}
}
}
assert!(sat_seen > 100, "expected a healthy share of SAT cases, got {sat_seen}");
assert!(unsat_seen > 100, "expected a healthy share of UNSAT cases, got {unsat_seen}");
}