gateconvert 0.1.2

The library to convert Gate circuit from/to foreign logic format.
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
// aiger.rs - AIGER conversion module

#![cfg_attr(docsrs, feature(doc_cfg))]
//! Module to conversion between Gate circuit and the AIGER logic format.

use crate::gatesim::*;
use flussab::DeferredWriter;
use flussab_aiger::aig::*;
use flussab_aiger::*;

use std::collections::HashMap;
use std::fmt::Debug;
use std::io::{self, Read, Write};

use crate::AssignEntry;

/// Converts circuit to AIGER format.
///
/// Function writes Gate circuit logic in AIGER format to `out`. `circuit` is circuit
/// to convert. `state_len` is length of state that represents in AIGER as latches.
///
/// The circuit inputs are organized in form: `[state,inputs]`.
/// The circuit outputs are organized in form: `[state,outputs]`.
///
/// `binmode` sets mode used while writing to AIGER mode - if true then use binary mode,
/// otherwise textual mode.
pub fn to_aiger(
    circuit: &Circuit<usize>,
    state_len: usize,
    out: impl Write,
    binmode: bool,
) -> io::Result<()> {
    let input_len = circuit.input_len();
    let output_len = circuit.outputs().len();
    let outputs = circuit.outputs();
    assert!(state_len <= input_len);
    assert!(state_len <= output_len);
    // convert to OrderedAig
    // in circuit - states are first.
    // in AIGER - states are next after inputs.
    let mut wires2lits = (0..state_len)
        .map(|x| 2 * (input_len - state_len + x) + 2)
        .chain((0..input_len - state_len).map(|x| 2 * x + 2))
        .collect::<Vec<_>>();
    let mut var_index = input_len;
    let mut and_gate_count = 0;
    for g in circuit.gates() {
        let (count, v) = if g.func == GateFunc::Xor {
            (3, var_index + 2)
        } else {
            (1, var_index)
        };
        wires2lits.push(2 * v + 2);
        and_gate_count += count;
        var_index += count;
    }
    let mut and_gates = Vec::with_capacity(and_gate_count);
    for (i, g) in circuit.gates().iter().enumerate() {
        let wire = i + input_len;
        match g.func {
            GateFunc::And => {
                and_gates.push(OrderedAndGate {
                    inputs: [wires2lits[g.i0], wires2lits[g.i1]],
                });
            }
            GateFunc::Nor => {
                and_gates.push(OrderedAndGate {
                    inputs: [wires2lits[g.i0] + 1, wires2lits[g.i1] + 1],
                });
            }
            GateFunc::Nimpl => {
                and_gates.push(OrderedAndGate {
                    inputs: [wires2lits[g.i0], wires2lits[g.i1] + 1],
                });
            }
            GateFunc::Xor => {
                // xor(a,b) = and(!and(a,b), !and(!a, !b))
                let prev0 = wires2lits[wire] - 4;
                let prev1 = prev0 + 2;
                and_gates.push(OrderedAndGate {
                    inputs: [wires2lits[g.i0], wires2lits[g.i1]],
                });
                and_gates.push(OrderedAndGate {
                    inputs: [wires2lits[g.i0] + 1, wires2lits[g.i1] + 1],
                });
                and_gates.push(OrderedAndGate {
                    inputs: [prev0 + 1, prev1 + 1],
                });
            }
        }
    }
    let ord_aig = OrderedAig {
        max_var_index: var_index,
        input_count: input_len - state_len,
        latches: (0..state_len)
            .map(|i| OrderedLatch {
                next_state: wires2lits[outputs[i].0] + usize::from(outputs[i].1),
                initialization: Some(false),
            })
            .collect::<Vec<_>>(),
        outputs: (state_len..output_len)
            .map(|i| wires2lits[outputs[i].0] + usize::from(outputs[i].1))
            .collect::<Vec<_>>(),
        bad_state_properties: vec![],
        invariant_constraints: vec![],
        justice_properties: vec![],
        fairness_constraints: vec![],
        and_gates,
        symbols: vec![],
        comment: None,
    };
    let mut dwriter = DeferredWriter::from_write(out);
    if binmode {
        let mut writer = binary::Writer::new(dwriter);
        writer.write_ordered_aig(&ord_aig);
        writer.check_io_error()
    } else {
        let writer = ascii::Writer::new(&mut dwriter);
        writer.write_ordered_aig(&ord_aig);
        writer.check_io_error()
    }
}

/// AIGER error enumeration.
#[derive(thiserror::Error, Debug)]
pub enum AIGERError {
    /// If parse error.
    #[error("AIGER parse error {0}")]
    ParseError(#[from] ParseError),
    /// If encountered cycle in AIGER logic.
    #[error("Cycles in AIGER")]
    CyclesInAIGER,
    /// If gate have wrong output.
    #[error("AndGate bad output")]
    AndGateBadOutput,
    /// If latch have bad state.
    #[error("Latch bad state")]
    LatchBadState,
    /// If bad input.
    #[error("Bad input")]
    BadInput,
}

fn from_aiger_int(
    aig: &Aig<usize>,
) -> Result<(Circuit<usize>, Vec<(usize, AssignEntry)>), AIGERError> {
    use gategen2::boolvar::*;
    use gategen2::dynintvar::*;
    let state_len = aig.latches.len();
    let all_input_len = aig.inputs.len() + aig.latches.len();
    // exprs - input and latches initialized, rest is empty - initialized by false
    let mut exprs = (0..all_input_len)
        .map(|_| BoolVarSys::var())
        .chain((0..aig.and_gates.len()).map(|_| BoolVarSys::from(false)))
        .collect::<Vec<_>>();
    let mut expr_map = HashMap::<usize, usize>::new();
    for (i, l) in aig.latches.iter().enumerate() {
        let lo = l.state;
        if (lo & 1) == 0 && !expr_map.contains_key(&lo) {
            expr_map.insert(lo, i);
        } else {
            return Err(AIGERError::LatchBadState);
        }
    }
    for (i, input) in aig.inputs.iter().enumerate() {
        let ino = *input;
        if (ino & 1) == 0 && !expr_map.contains_key(&ino) {
            expr_map.insert(ino, i + state_len);
        } else {
            return Err(AIGERError::BadInput);
        }
    }
    for (i, g) in aig.and_gates.iter().enumerate() {
        let go = g.output;
        if (go & 1) == 0 && !expr_map.contains_key(&go) {
            expr_map.insert(go, all_input_len + i);
        } else {
            return Err(AIGERError::AndGateBadOutput);
        }
    }
    let and_resolve = |l: usize| {
        let lpos = l & !1;
        if l < 2 {
            None
        } else if let Some(x) = expr_map.get(&lpos) {
            if *x >= all_input_len {
                Some((*x - all_input_len, &aig.and_gates[*x - all_input_len]))
            } else {
                None
            }
        } else {
            panic!("Unexpected literal");
        }
    };
    #[derive(Clone)]
    struct StackEntry {
        way: usize,
        lit: usize,
    }
    // visited nodes in graph
    let mut visited = vec![false; aig.max_var_index];
    // path_visited - to detect cycles
    let mut path_visited = vec![false; aig.max_var_index];
    let mut stack = vec![];
    // XOR subpart gates will be skipped - if they are part of other path then included
    // automatically. Any negation propagation, constant assignments will be done
    // automatically gategen.
    let outputs = aig
        .latches
        .iter()
        .map(|latch| latch.next_state)
        .chain(aig.outputs.iter().copied())
        .collect::<Vec<_>>();
    for ol in &outputs {
        stack.push(StackEntry { way: 0, lit: *ol });
        while !stack.is_empty() {
            let top = stack.last_mut().unwrap();
            let and_gate = and_resolve(top.lit);
            let avar = top.lit >> 1;

            if let Some((and_idx, and_gate)) = and_gate {
                // check if XOR or Equal
                let gi0and = and_resolve(and_gate.inputs[0]);
                let gi1and = and_resolve(and_gate.inputs[1]);
                // and_data - default data for AND gate
                let and_data = (and_gate.inputs[0], and_gate.inputs[1], false);
                // check and resolve XOR construction:
                // xor(a,b) = and(!and(a,b), !and(!a, !b))
                let (gi0l, gi1l, is_xor) =
                    if (and_gate.inputs[0] & 1) != 0 && (and_gate.inputs[1] & 1) != 0 {
                        if let Some((_, gi0and)) = gi0and {
                            if let Some((_, gi1and)) = gi1and {
                                // compare results
                                if (gi0and.inputs[0] == (gi1and.inputs[0] ^ 1)
                                    && gi0and.inputs[1] == (gi1and.inputs[1] ^ 1))
                                    || (gi0and.inputs[0] == (gi1and.inputs[1] ^ 1)
                                        && gi0and.inputs[1] == (gi1and.inputs[0] ^ 1))
                                {
                                    // if XOR
                                    (gi0and.inputs[0], gi0and.inputs[1], true)
                                } else {
                                    and_data
                                }
                            } else {
                                and_data
                            }
                        } else {
                            and_data
                        }
                    } else {
                        and_data
                    };

                let way = top.way;
                if way == 0 {
                    if !path_visited[avar - 1] {
                        path_visited[avar - 1] = true;
                    } else {
                        return Err(AIGERError::CyclesInAIGER);
                    }
                    if !visited[avar - 1] {
                        visited[avar - 1] = true;
                    } else {
                        path_visited[avar - 1] = false;
                        stack.pop();
                        continue;
                    }
                    // first argument
                    top.way += 1;
                    stack.push(StackEntry { way: 0, lit: gi0l });
                } else if way == 1 {
                    // second argument
                    top.way += 1;
                    stack.push(StackEntry { way: 0, lit: gi1l });
                } else {
                    // get expressions for gate arguments
                    let gi0expr = if gi0l < 2 {
                        BoolVarSys::from(gi0l == 1)
                    } else if let Some(x) = expr_map.get(&(gi0l & !1)) {
                        &exprs[*x] ^ ((gi0l & 1) != 0)
                    } else {
                        panic!("Unexpected literal");
                    };
                    let gi1expr = if gi1l < 2 {
                        BoolVarSys::from(gi1l == 1)
                    } else if let Some(x) = expr_map.get(&(gi1l & !1)) {
                        &exprs[*x] ^ ((gi1l & 1) != 0)
                    } else {
                        panic!("Unexpected literal");
                    };
                    // set expression to exprs
                    exprs[all_input_len + and_idx] = if is_xor {
                        gi0expr ^ gi1expr
                    } else {
                        gi0expr & gi1expr
                    };
                    path_visited[avar - 1] = false;
                    stack.pop();
                }
            } else {
                // if constant
                if avar >= 1 {
                    path_visited[avar - 1] = false;
                }
                stack.pop();
            }
        }
    }
    // map: entry: (literal, Some(value), circuit_output_count)
    let mut ocount = 0;
    let aiger_out_map = outputs
        .iter()
        .map(|l| {
            let lpos = *l & !1;
            let expr = if *l < 2 {
                BoolVarSys::from(*l == 1)
            } else if let Some(x) = expr_map.get(&lpos) {
                &exprs[*x] ^ ((l & 1) != 0)
            } else {
                panic!("Unexpected literal");
            };
            if let Some(v) = expr.value() {
                (l, Some(v), ocount)
            } else {
                let old_ocount = ocount;
                ocount += 1;
                (l, None, old_ocount)
            }
        })
        .collect::<Vec<_>>();
    let filtered_outputs = outputs
        .iter()
        .filter_map(|l| {
            let lpos = *l & !1;
            let expr = if *l < 2 {
                BoolVarSys::from(*l == 1)
            } else if let Some(x) = expr_map.get(&lpos) {
                &exprs[*x] ^ ((l & 1) != 0)
            } else {
                panic!("Unexpected literal");
            };
            // just choose only not constant expressions
            if expr.value().is_none() {
                Some(expr)
            } else {
                None
            }
        })
        .collect::<Vec<_>>();
    let outint = if !filtered_outputs.is_empty() {
        UDynVarSys::from_iter(filtered_outputs.into_iter())
    } else {
        UDynVarSys::var(0)
    };

    // generate circuit with assign map
    let (circuit, assign_map) =
        outint.to_translated_circuit_with_map(exprs.iter().take(all_input_len).cloned());
    // collect for aiger_map: first are AIGER latches and AIGER inputs
    let aiger_map = aig
        .latches
        .iter()
        .map(|latch| latch.state)
        .chain(aig.inputs.iter().copied())
        .enumerate()
        .map(|(i, l)| {
            // map AIGER latches and inputs
            (
                l,
                if let Some(newidx) = assign_map[i] {
                    AssignEntry::Var(newidx, false)
                } else {
                    AssignEntry::NoMap
                },
            )
        })
        .chain(
            // main next states of latches and outputs
            aiger_out_map.into_iter().map(|(l, cb, circ_out_idx)| {
                if let Some(c) = cb {
                    // constant
                    (*l, AssignEntry::Value(c))
                } else {
                    let (o, n) = circuit.outputs()[circ_out_idx];
                    (*l, AssignEntry::Var(o, n))
                }
            }),
        )
        .collect::<Vec<_>>();
    Ok((circuit, aiger_map))
}

// return: circuit, map for AIGER variables (input, latches and output)
// format of AIGER map: (AIGER literal, AIGER Entry)

/// Converts AIGER logic to Gate circuit.
///
/// `input` is read stream with AIGER logic. Function returns Gate circuit with its mapping.
/// Mapping in form: key - original variable in AIGER logic, value - assignment in circuit.
///
/// `binmode` sets mode used while writing to AIGER mode - if true then use binary mode,
/// otherwise textual mode.
pub fn from_aiger(
    input: impl Read,
    binmode: bool,
) -> Result<(Circuit<usize>, Vec<(usize, AssignEntry)>), AIGERError> {
    use gategen2::boolvar::*;
    let aig = if binmode {
        let parser = binary::Parser::<usize>::from_read(input, binary::Config::default())?;
        parser.parse()?.into()
    } else {
        let parser = ascii::Parser::<usize>::from_read(input, ascii::Config::default())?;
        parser.parse()?
    };
    callsys(|| from_aiger_int(&aig))
}