mutaig 0.4.2

Mutable AIGs designed for equivalence checking.
Documentation
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Extract a SAT formula from a previously generated [`Miter`].
//!
//! To prove combinational equivalence checking (CEC) between two circuits `a` and `b`:
//! - generate miter from `a` and `b` with [`Miter::new`]
//! - extract CNF from the miter with methods of [`Cnf`]
//! - check that the CNF is **UNSAT** with a SAT solver.
//!
//! If the resulting CNF is SAT, it means that the two circuits are **not equivalent**.
//!
//! The core methods to extract a SAT formula are:
//! - [`Miter::extract_cnf_node`] if you want to prove the equivalence of two internal nodes.
//!   Once the nodes are proven equivalent, merge them using [`Miter::merge`].
//! - [`Miter::extract_cnf`] if you wat to prove the equivalence of the whole circuits.
//!
//! Note that you can also try to merge obviously equivalent internal nodes with
//! [`Miter::mergeable`] then [`Miter::merge`].
//!
//! [`Miter`]: crate::miter::Miter
//! [`Miter::new`]: crate::miter::Miter::new
//! [`Miter::extract_cnf_node`]: crate::miter::Miter::extract_cnf_node
//! [`Miter::extract_cnf`]: crate::miter::Miter::extract_cnf
//! [`Miter::merge`]: crate::miter::Miter::merge
//! [`Miter::mergeable`]: crate::miter::Miter::mergeable

use std::{collections::HashMap, num::TryFromIntError, ops::Not};

use crate::{AigEdge, AigNode, NodeId, Result, miter::MiterError};

/// A SAT literal.
///
/// Note that all AIG nodes do not correspond to a SAT literal.
/// For example, [`AigNode::False`] node do not map to any literal, but rather is omitted
/// as false boolean variables can be removed from a clause without changing the problem.
/// Clauses that contain a true boolean variable (ie a complemented edge to [`AigNode::False`] node)
/// are obviously true and don't need to be emitted.
///
/// These cases are handled by the internal `LitRes` data structure.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Lit(i64);

impl Not for Lit {
    type Output = Self;

    fn not(self) -> Self::Output {
        Lit(-self.0)
    }
}

impl From<i64> for Lit {
    fn from(value: i64) -> Self {
        if value == 0 {
            panic!("Tried to create a Lit from 0. 0 is not a valid literal in DIMACS format.");
        }
        Lit(value)
    }
}

impl TryFrom<NodeId> for Lit {
    type Error = TryFromIntError;

    fn try_from(value: NodeId) -> std::result::Result<Self, Self::Error> {
        Ok(Lit::from(i64::try_from(value)?))
    }
}

impl Lit {
    pub fn get_idx(&self) -> i64 {
        self.0
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LitRes {
    False,
    True,
    Lit(Lit),
}

impl Not for LitRes {
    type Output = Self;

    fn not(self) -> Self::Output {
        match self {
            LitRes::False => LitRes::True,
            LitRes::True => LitRes::False,
            LitRes::Lit(lit) => LitRes::Lit(!lit),
        }
    }
}

impl From<Lit> for LitRes {
    fn from(value: Lit) -> Self {
        LitRes::Lit(value)
    }
}

/// A SAT clause.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Clause(Vec<Lit>);

impl Clause {
    /// A new empty clause.
    pub fn new() -> Self {
        Clause(Vec::new())
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn get_lits(&self) -> Vec<Lit> {
        self.0.clone()
    }

    /// Returns the true SAT clause once we got rid of `True` and `False` literals.
    /// If there is a `True`, then the Clause is obviously satisfied, so we return None.
    /// `False` literals are omitted, and real literals are added to the clause.
    /// If the clause is empty (lits were only `False`), None is returned.
    fn from_lit_res(lits: Vec<LitRes>) -> Option<Clause> {
        let mut literals = Vec::new();

        for lit_res in lits {
            match lit_res {
                LitRes::True => return None,
                LitRes::False => (),
                LitRes::Lit(lit) => literals.push(lit),
            }
        }

        if literals.is_empty() {
            None
        } else {
            Some(Clause(literals))
        }
    }
}

impl From<Vec<Lit>> for Clause {
    fn from(value: Vec<Lit>) -> Self {
        Clause(value)
    }
}

/// A SAT CNF that can be passed to a SAT solver.
///
/// It provides useful methods to create a miter such as [`add_xor`], [`add_xor_whose_output_is_true`],
/// and [`add_or_whose_output_is_true`], which can be used to finish the construction of the miter.
///
/// [`add_xor`]: Cnf::add_xor
/// [`add_xor_whose_output_is_true`]: Cnf::add_xor_whose_output_is_true
/// [`add_or_whose_output_is_true`]: Cnf::add_or_whose_output_is_true
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cnf(Vec<Clause>);

impl Cnf {
    /// A new empty CNF.
    pub fn new() -> Self {
        Cnf(Vec::new())
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn get_clauses(&self) -> Vec<Clause> {
        self.0.clone()
    }

    /// Add the given clause to the CNF.
    pub fn add_clause(&mut self, clause: Clause) {
        self.0.push(clause);
    }

    /// Add the given clause to the CNF, else does nothing.
    pub fn add_clause_if(&mut self, clause: Option<Clause>) {
        if let Some(c) = clause {
            self.add_clause(c);
        }
    }

    /// Add clauses induced by the node.
    ///
    /// Latches outputs are considered as pseudo-inputs, and their inputs as pseudo-outputs.
    pub fn add_clauses_node(
        &mut self,
        node: &AigNode,
        litmap: &HashMap<NodeId, Lit>,
    ) -> Result<()> {
        match node {
            AigNode::And {
                id, fanin0, fanin1, ..
            } => {
                let a = fanin0.get_literal_res(litmap)?;
                let b = fanin1.get_literal_res(litmap)?;
                let z = LitRes::from(*litmap.get(id).ok_or(MiterError::UnmappedNodeToLit(*id))?);

                self.add_clause_if(Clause::from_lit_res(vec![a, !z]));
                self.add_clause_if(Clause::from_lit_res(vec![b, !z]));
                self.add_clause_if(Clause::from_lit_res(vec![!a, !b, z]));
            }
            // The other nodes do not induce any clause, they only generate literals
            // Latches are treated as pseudo-outputs/inputs see in miter.
            _ => (),
        }
        Ok(())
    }

    /// Add clauses that encode `z = XOR(a, b)`.
    ///
    /// - a is the literal associated with the `fanin0`
    /// - b is the literal associated with the `fanin1`
    /// - z is the literal of the XOR gate itself
    ///
    /// If you are creating a miter, you should create z with the [`fresh_lit()`] method.
    ///
    /// [`fresh_lit()`]: crate::miter::Miter::fresh_lit
    pub fn add_xor(&mut self, a: Lit, b: Lit, z: Lit) {
        self.add_clause(Clause::from(vec![a, b, !z]));
        self.add_clause(Clause::from(vec![a, !b, z]));
        self.add_clause(Clause::from(vec![!a, b, z]));
        self.add_clause(Clause::from(vec![!a, !b, !z]));
    }

    /// Add clauses that encode `XOR(a, b) = true`.
    ///
    /// This is the function to use if you want to compare two internal nodes of the miter for AIGs `a` and `b`:
    /// - a is the literal associated with the node from aig `a`
    /// - b is the literal associated with the node from aig `b`
    pub fn add_xor_whose_output_is_true(&mut self, a: Lit, b: Lit) {
        self.add_clause(Clause::from(vec![a, b]));
        self.add_clause(Clause::from(vec![!a, !b]));
    }

    /// Add a xor between the two pseudo outputs a and b.
    /// They are the fanins of some latches which should be equivalent.
    pub fn add_xor_pseudo_output(
        &mut self,
        a: AigEdge,
        litmap_a: &HashMap<NodeId, Lit>,
        b: AigEdge,
        litmap_b: &HashMap<NodeId, Lit>,
        z: Lit,
    ) -> Result<()> {
        let a = a.get_literal_res(litmap_a)?;
        let b = b.get_literal_res(litmap_b)?;
        let z = LitRes::from(z);

        self.add_clause_if(Clause::from_lit_res(vec![a, b, !z]));
        self.add_clause_if(Clause::from_lit_res(vec![a, !b, z]));
        self.add_clause_if(Clause::from_lit_res(vec![!a, b, z]));
        self.add_clause_if(Clause::from_lit_res(vec![!a, !b, !z]));

        Ok(())
    }

    /// Add clauses that encode `OR(inputs) = true`.
    ///
    /// This is the last node of the miter to compare two circuits.
    /// We assume that the output is true, which means there are at least a pair of outputs
    /// which differ for the same set of inputs:
    /// - if this is possible (ie the CNF is SAT), then circuits are not equivalent
    /// - if the CNF is UNSAT, then circuits are equivalent.
    pub fn add_or_whose_output_is_true(&mut self, inputs: Vec<Lit>) {
        self.add_clause(Clause::from(inputs));
    }

    /// Returns a string using DIMACS format to represent CNF.
    pub fn to_dimacs(&self) -> String {
        let var_max = self
            .0
            .iter()
            .flat_map(|clause| &clause.0)
            .map(|lit| lit.0.abs())
            .max()
            .unwrap_or(0);

        // Pre-calculate approximate size to reduce allocations
        let estimated_size = 30 + self.0.len() * 20; // rough estimate
        let mut dimacs = String::with_capacity(estimated_size);

        // Add header
        dimacs.push_str("p cnf ");
        dimacs.push_str(&var_max.to_string());
        dimacs.push(' ');
        dimacs.push_str(&self.0.len().to_string());
        dimacs.push('\n');

        // Add clauses
        for clause in &self.0 {
            for Lit(val) in &clause.0 {
                dimacs.push_str(&val.to_string());
                dimacs.push(' ');
            }
            dimacs.push('0');
            dimacs.push('\n');
        }

        dimacs
    }
}

impl AigEdge {
    fn get_literal_res(&self, litmap: &HashMap<NodeId, Lit>) -> Result<LitRes> {
        let lit = if *self.get_node().borrow() == AigNode::False {
            LitRes::False
        } else {
            let id = self.get_node().borrow().get_id();
            LitRes::from(*litmap.get(&id).ok_or(MiterError::UnmappedNodeToLit(id))?)
        };
        Ok(if self.get_complement() { !lit } else { lit })
    }
}

#[cfg(test)]
mod test {
    use std::{cell::RefCell, rc::Rc};

    use super::*;

    #[test]
    fn not_lit_test() {
        let l1 = Lit(1);
        assert_eq!(!l1, Lit(-1));
    }

    #[test]
    fn not_lit_res_test() {
        let ltrue = LitRes::True;
        let lfalse = LitRes::False;
        let l1 = LitRes::Lit(Lit(1));
        assert_eq!(!lfalse, ltrue);
        assert_eq!(!l1, LitRes::Lit(!Lit(1)));
    }

    #[test]
    fn clause_from_lit_res_test() {
        let ltrue = LitRes::True;
        let lfalse = LitRes::False;
        let l1 = Lit(1);
        let l2 = Lit(2);
        let lr1 = LitRes::Lit(l1);
        let lr2 = LitRes::Lit(l2);

        assert!(Clause::from_lit_res(vec![lfalse, lfalse]).is_none());
        assert!(Clause::from_lit_res(vec![lfalse, ltrue, lr1]).is_none());
        assert_eq!(
            Clause::from_lit_res(vec![lr1, lfalse, lr2]).unwrap(),
            Clause(vec![l1, l2])
        );
    }

    #[test]
    fn add_clause_test() {
        let l1 = Lit(1);
        let l2 = Lit(2);
        let c = Clause::from(vec![l1, l2]);

        let mut cnf = Cnf::new();

        cnf.add_clause(c.clone());
        assert_eq!(cnf.0, vec![c.clone()]);

        cnf.add_clause_if(Some(c.clone()));
        assert_eq!(cnf.0, vec![c.clone(), c.clone()]);

        cnf.add_clause_if(None);
        assert_eq!(cnf.0, vec![c.clone(), c.clone()]);
    }

    #[test]
    #[should_panic]
    fn invalid_lit_from_test() {
        _ = Lit::from(0);
    }

    #[test]
    #[should_panic]
    fn invalid_lit_tryfrom_test() {
        _ = Lit::try_from(0 as NodeId);
    }

    #[test]
    fn empty_to_dimacs_test() {
        let cnf = Cnf::new();
        assert_eq!(cnf.to_dimacs(), "p cnf 0 0\n");
    }

    #[test]
    fn to_dimacs_test() {
        let mut cnf = Cnf::new();
        cnf.add_clause(Clause::from(vec![Lit(1)]));
        cnf.add_clause(Clause::from(vec![Lit(-1), Lit(2)]));
        assert_eq!(cnf.to_dimacs(), "p cnf 2 2\n1 0\n-1 2 0\n");

        cnf.add_clause(Clause::from(vec![Lit(5), Lit(-4), Lit(2)]));
        assert_eq!(cnf.to_dimacs(), "p cnf 5 3\n1 0\n-1 2 0\n5 -4 2 0\n");

        cnf.add_clause(Clause::from(vec![Lit(-6)]));
        assert_eq!(cnf.to_dimacs(), "p cnf 6 4\n1 0\n-1 2 0\n5 -4 2 0\n-6 0\n")
    }

    #[test]
    fn get_literal_res_test() {
        let nf = Rc::new(RefCell::new(AigNode::False));
        let mut litmap = HashMap::new();
        assert_eq!(
            AigEdge::new(nf.clone(), false)
                .get_literal_res(&litmap)
                .unwrap(),
            LitRes::False
        );
        assert_eq!(
            AigEdge::new(nf.clone(), true)
                .get_literal_res(&litmap)
                .unwrap(),
            LitRes::True
        );
        let i1 = Rc::new(RefCell::new(AigNode::Input(1)));
        litmap.insert(1, Lit::from(1));
        assert_eq!(
            AigEdge::new(i1.clone(), false)
                .get_literal_res(&litmap)
                .unwrap(),
            LitRes::Lit(Lit::from(1))
        );
        assert_eq!(
            AigEdge::new(i1.clone(), true)
                .get_literal_res(&litmap)
                .unwrap(),
            LitRes::Lit(Lit::from(-1))
        );
    }
}