guarded-continuation-checker 0.32.0

Proof-carrying bounded verification for embedded firmware and RTL, powered by CQ-SAT
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
//! Bounded-equivalent AIGER export for sampled controller and plant composition.

use std::collections::{BTreeMap, BTreeSet};
use std::error::Error;
use std::fmt;

use crate::aiger_obligation::AigerTransition;
use crate::controller_plant::{ControllerPlantWiring, compose_controller_plant_direct};

pub const CONTROLLER_PLANT_AIGER_EXPORT_VERSION: u32 = 1;
pub const MAX_EXPORT_HORIZON: usize = 62;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ControllerPlantAigerExport {
    pub version: u32,
    pub horizon: usize,
    pub bad_plant_output: usize,
    pub external_plant_inputs: Vec<usize>,
    pub bytes: Vec<u8>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ControllerPlantAigerMultiExport {
    pub version: u32,
    pub horizon: usize,
    pub bad_plant_outputs: Vec<usize>,
    pub external_plant_inputs: Vec<usize>,
    pub bytes: Vec<u8>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ControllerPlantAigerExportError(pub String);

impl fmt::Display for ControllerPlantAigerExportError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.0)
    }
}

impl Error for ControllerPlantAigerExportError {}

fn reject(message: impl Into<String>) -> ControllerPlantAigerExportError {
    ControllerPlantAigerExportError(message.into())
}

#[derive(Default)]
struct Builder {
    inputs: Vec<usize>,
    latches: Vec<(usize, usize)>,
    ands: Vec<(usize, usize, usize)>,
    next_variable: usize,
}

impl Builder {
    fn variable(&mut self) -> usize {
        self.next_variable += 1;
        self.next_variable * 2
    }

    fn input(&mut self) -> usize {
        let literal = self.variable();
        self.inputs.push(literal);
        literal
    }

    fn latch(&mut self) -> usize {
        let literal = self.variable();
        self.latches.push((literal, 0));
        literal
    }

    fn and(&mut self, left: usize, right: usize) -> usize {
        if left == 0 || right == 0 {
            return 0;
        }
        if left == 1 {
            return right;
        }
        if right == 1 || left == right {
            return left;
        }
        if left == (right ^ 1) {
            return 0;
        }
        let output = self.variable();
        self.ands.push((output, left, right));
        output
    }

    fn or(&mut self, left: usize, right: usize) -> usize {
        self.and(left ^ 1, right ^ 1) ^ 1
    }

    fn xor(&mut self, left: usize, right: usize) -> usize {
        let one = self.and(left, right ^ 1);
        let two = self.and(left ^ 1, right);
        self.or(one, two)
    }

    fn mux(&mut self, select: usize, when_true: usize, when_false: usize) -> usize {
        let yes = self.and(select, when_true);
        let no = self.and(select ^ 1, when_false);
        self.or(yes, no)
    }

    fn conjunction(&mut self, literals: impl IntoIterator<Item = usize>) -> usize {
        literals
            .into_iter()
            .fold(1, |left, right| self.and(left, right))
    }

    fn set_latch_next(
        &mut self,
        current: usize,
        next: usize,
    ) -> Result<(), ControllerPlantAigerExportError> {
        let entry = self
            .latches
            .iter_mut()
            .find(|entry| entry.0 == current)
            .ok_or_else(|| reject("export latch is missing"))?;
        entry.1 = next;
        Ok(())
    }

    fn finish(self, output: usize) -> Vec<u8> {
        let mut text = format!(
            "aag {} {} {} 1 {}\n",
            self.next_variable,
            self.inputs.len(),
            self.latches.len(),
            self.ands.len()
        );
        for literal in self.inputs {
            text.push_str(&format!("{literal}\n"));
        }
        for (current, next) in self.latches {
            text.push_str(&format!("{current} {next} 0\n"));
        }
        text.push_str(&format!("{output}\n"));
        for (output, left, right) in self.ands {
            text.push_str(&format!("{output} {left} {right}\n"));
        }
        text.into_bytes()
    }

    fn finish_bads(self, bads: &[usize]) -> Vec<u8> {
        let mut text = format!(
            "aag {} {} {} 0 {} {} 0 0 0\n",
            self.next_variable,
            self.inputs.len(),
            self.latches.len(),
            self.ands.len(),
            bads.len(),
        );
        for literal in self.inputs {
            text.push_str(&format!("{literal}\n"));
        }
        for (current, next) in self.latches {
            text.push_str(&format!("{current} {next} 0\n"));
        }
        for bad in bads {
            text.push_str(&format!("{bad}\n"));
        }
        for (output, left, right) in self.ands {
            text.push_str(&format!("{output} {left} {right}\n"));
        }
        text.into_bytes()
    }
}

fn mapped(
    literal: usize,
    map: &BTreeMap<usize, usize>,
) -> Result<usize, ControllerPlantAigerExportError> {
    if literal < 2 {
        return Ok(literal);
    }
    map.get(&(literal / 2))
        .copied()
        .map(|base| base ^ (literal & 1))
        .ok_or_else(|| reject("export references an unmapped AIGER literal"))
}

fn append_gates(
    builder: &mut Builder,
    model: &AigerTransition,
    map: &mut BTreeMap<usize, usize>,
) -> Result<(), ControllerPlantAigerExportError> {
    for gate in &model.ands {
        let left = mapped(gate.left, map)?;
        let right = mapped(gate.right, map)?;
        let output = builder.and(left, right);
        map.insert(gate.output / 2, output);
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn build_bounded_controller_plant_aag(
    controller: &AigerTransition,
    plant: &AigerTransition,
    wiring: &ControllerPlantWiring,
    initial_controller_state: usize,
    initial_plant_state: usize,
    bad_plant_outputs: &[usize],
    horizon: usize,
) -> Result<(Vec<usize>, Builder, Vec<usize>), ControllerPlantAigerExportError> {
    controller
        .validate()
        .map_err(|error| reject(error.to_string()))?;
    plant
        .validate()
        .map_err(|error| reject(error.to_string()))?;
    if initial_controller_state != 0 || initial_plant_state != 0 {
        return Err(reject("AIGER export v1 requires all-zero initial states"));
    }
    if horizon > MAX_EXPORT_HORIZON
        || bad_plant_outputs.is_empty()
        || bad_plant_outputs.len() > 64
        || bad_plant_outputs
            .iter()
            .any(|&output| output >= plant.outputs.len())
        || bad_plant_outputs
            .iter()
            .copied()
            .collect::<BTreeSet<_>>()
            .len()
            != bad_plant_outputs.len()
    {
        return Err(reject("AIGER export query is outside limits"));
    }
    for &bad_plant_output in bad_plant_outputs {
        compose_controller_plant_direct(
            controller,
            plant,
            wiring,
            initial_controller_state,
            initial_plant_state,
            bad_plant_output,
            horizon,
        )
        .map_err(|error| reject(error.to_string()))?;
    }

    let action_inputs = wiring
        .plant_action_inputs
        .iter()
        .copied()
        .collect::<BTreeSet<_>>();
    let external_plant_inputs = (0..plant.inputs.len())
        .filter(|index| !action_inputs.contains(index))
        .collect::<Vec<_>>();
    let mut builder = Builder::default();
    let external_literals = external_plant_inputs
        .iter()
        .map(|_| builder.input())
        .collect::<Vec<_>>();
    let controller_latches = controller
        .latches
        .iter()
        .map(|_| builder.latch())
        .collect::<Vec<_>>();
    let plant_latches = plant
        .latches
        .iter()
        .map(|_| builder.latch())
        .collect::<Vec<_>>();
    let counter = (0..6).map(|_| builder.latch()).collect::<Vec<_>>();

    let mut sensor_map = BTreeMap::new();
    for (&declared, &literal) in plant.inputs.iter().zip(std::iter::repeat(&0)) {
        sensor_map.insert(declared / 2, literal);
    }
    for (latch, &literal) in plant.latches.iter().zip(&plant_latches) {
        sensor_map.insert(latch.current / 2, literal);
    }
    append_gates(&mut builder, plant, &mut sensor_map)?;
    let sensors = wiring
        .plant_sensor_outputs
        .iter()
        .map(|&index| mapped(plant.outputs[index], &sensor_map))
        .collect::<Result<Vec<_>, _>>()?;

    let mut controller_map = BTreeMap::new();
    for (&index, &literal) in wiring.controller_sensor_inputs.iter().zip(&sensors) {
        controller_map.insert(controller.inputs[index] / 2, literal);
    }
    for &declared in &controller.inputs {
        controller_map.entry(declared / 2).or_insert(0);
    }
    for (latch, &literal) in controller.latches.iter().zip(&controller_latches) {
        controller_map.insert(latch.current / 2, literal);
    }
    append_gates(&mut builder, controller, &mut controller_map)?;
    let actions = wiring
        .controller_action_outputs
        .iter()
        .map(|&index| mapped(controller.outputs[index], &controller_map))
        .collect::<Result<Vec<_>, _>>()?;

    let mut plant_map = BTreeMap::new();
    for (external_index, &literal) in external_plant_inputs.iter().zip(&external_literals) {
        plant_map.insert(plant.inputs[*external_index] / 2, literal);
    }
    for (&input_index, &literal) in wiring.plant_action_inputs.iter().zip(&actions) {
        plant_map.insert(plant.inputs[input_index] / 2, literal);
    }
    for (latch, &literal) in plant.latches.iter().zip(&plant_latches) {
        plant_map.insert(latch.current / 2, literal);
    }
    append_gates(&mut builder, plant, &mut plant_map)?;

    let completed_value = horizon + 1;
    let equality_bits = counter
        .iter()
        .enumerate()
        .map(|(bit, &literal)| {
            if completed_value >> bit & 1 == 1 {
                literal
            } else {
                literal ^ 1
            }
        })
        .collect::<Vec<_>>();
    let done = builder.conjunction(equality_bits);

    for ((latch, &current), source) in controller
        .latches
        .iter()
        .zip(&controller_latches)
        .zip(std::iter::repeat(&controller_map))
    {
        let next = mapped(latch.next, source)?;
        let frozen = builder.mux(done, current, next);
        builder.set_latch_next(current, frozen)?;
    }
    for (latch, &current) in plant.latches.iter().zip(&plant_latches) {
        let next = mapped(latch.next, &plant_map)?;
        let frozen = builder.mux(done, current, next);
        builder.set_latch_next(current, frozen)?;
    }
    let mut carry = 1;
    for &current in &counter {
        let incremented = builder.xor(current, carry);
        carry = builder.and(current, carry);
        let next = builder.mux(done, current, incremented);
        builder.set_latch_next(current, next)?;
    }
    let mut bads = Vec::with_capacity(bad_plant_outputs.len());
    for &bad_plant_output in bad_plant_outputs {
        let source_bad = mapped(plant.outputs[bad_plant_output], &plant_map)?;
        bads.push(builder.and(source_bad, done ^ 1));
    }

    Ok((external_plant_inputs, builder, bads))
}

/// Exports one sampled controller/plant property as an unbounded AIGER safety model.
///
/// Frames `0..=horizon` retain the original bad predicate. The next state after
/// the final checked frame is an absorbing completed state whose bad output is
/// false, making unbounded safety equivalent to the bounded source query.
#[allow(clippy::too_many_arguments)]
pub fn export_bounded_controller_plant_aag(
    controller: &AigerTransition,
    plant: &AigerTransition,
    wiring: &ControllerPlantWiring,
    initial_controller_state: usize,
    initial_plant_state: usize,
    bad_plant_output: usize,
    horizon: usize,
) -> Result<ControllerPlantAigerExport, ControllerPlantAigerExportError> {
    let (external_plant_inputs, builder, bads) = build_bounded_controller_plant_aag(
        controller,
        plant,
        wiring,
        initial_controller_state,
        initial_plant_state,
        &[bad_plant_output],
        horizon,
    )?;

    Ok(ControllerPlantAigerExport {
        version: CONTROLLER_PLANT_AIGER_EXPORT_VERSION,
        horizon,
        bad_plant_output,
        external_plant_inputs,
        bytes: builder.finish(bads[0]),
    })
}

/// Exports several sampled properties in one AIGER 1.9 model with explicit bad
/// sections. Transition logic and the checked horizon are shared exactly.
#[allow(clippy::too_many_arguments)]
pub fn export_bounded_controller_plant_multi_aag(
    controller: &AigerTransition,
    plant: &AigerTransition,
    wiring: &ControllerPlantWiring,
    initial_controller_state: usize,
    initial_plant_state: usize,
    bad_plant_outputs: &[usize],
    horizon: usize,
) -> Result<ControllerPlantAigerMultiExport, ControllerPlantAigerExportError> {
    let (external_plant_inputs, builder, bads) = build_bounded_controller_plant_aag(
        controller,
        plant,
        wiring,
        initial_controller_state,
        initial_plant_state,
        bad_plant_outputs,
        horizon,
    )?;
    Ok(ControllerPlantAigerMultiExport {
        version: CONTROLLER_PLANT_AIGER_EXPORT_VERSION,
        horizon,
        bad_plant_outputs: bad_plant_outputs.to_vec(),
        external_plant_inputs,
        bytes: builder.finish_bads(&bads),
    })
}