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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Deterministic safety-witness composition for the FM 2026 baseline.
//!
//! This is a closest-prior-art baseline implementation, not a GCC novelty
//! claim. Version 1 accepts bounded ASCII AIGER safety witnesses, coalesces
//! variables explicitly mapped to the same model input or latch, keeps private
//! variables disjoint, hash-conses gates, and conjoins safety and constraint
//! semantics. Liveness and comment-based mappings fail closed.

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

pub const COMPOSED_WITNESS_BASELINE_VERSION: u32 = 1;
pub const MAX_COMPOSED_WITNESSES: usize = 64;
pub const MAX_COMPOSED_AIGER_BYTES: usize = 16 * 1024 * 1024;
const MAX_VARIABLES: usize = 2_000_000;

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

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

impl Error for ComposedWitnessError {}

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

#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
enum VariableKind {
    Input,
    Latch,
    Gate,
}

#[derive(Clone, Debug)]
struct Latch {
    current: usize,
    next: usize,
    reset: usize,
}

#[derive(Clone, Debug)]
struct Gate {
    output: usize,
    left: usize,
    right: usize,
}

#[derive(Clone, Debug)]
struct Aag {
    max_variable: usize,
    inputs: Vec<usize>,
    latches: Vec<Latch>,
    outputs: Vec<usize>,
    bads: Vec<usize>,
    constraints: Vec<usize>,
    gates: Vec<Gate>,
    input_names: BTreeMap<usize, String>,
    latch_names: BTreeMap<usize, String>,
}

fn parse_number(value: Option<&str>, field: &str) -> Result<usize, ComposedWitnessError> {
    let text = value.ok_or_else(|| reject(format!("missing AIGER {field}")))?;
    if text.is_empty() || (text.len() > 1 && text.starts_with('0')) {
        return Err(reject(format!("noncanonical AIGER {field}")));
    }
    text.parse()
        .map_err(|_| reject(format!("invalid AIGER {field}")))
}

fn take_literal(
    lines: &mut std::str::Lines<'_>,
    field: &str,
) -> Result<usize, ComposedWitnessError> {
    let line = lines
        .next()
        .ok_or_else(|| reject(format!("truncated AIGER {field}")))?;
    let mut fields = line.split_whitespace();
    let literal = parse_number(fields.next(), field)?;
    if fields.next().is_some() {
        return Err(reject(format!("AIGER {field} has trailing fields")));
    }
    Ok(literal)
}

fn parse_aag(bytes: &[u8], witness: bool) -> Result<Aag, ComposedWitnessError> {
    if bytes.is_empty()
        || bytes.len() > MAX_COMPOSED_AIGER_BYTES
        || !bytes.is_ascii()
        || bytes.contains(&b'\r')
        || bytes.last() != Some(&b'\n')
    {
        return Err(reject(
            "ASCII AIGER bytes are noncanonical or outside limits",
        ));
    }
    let text = std::str::from_utf8(bytes).map_err(|_| reject("ASCII AIGER is not UTF-8"))?;
    let mut lines = text.lines();
    let mut header = lines
        .next()
        .ok_or_else(|| reject("ASCII AIGER is empty"))?
        .split_whitespace();
    if header.next() != Some("aag") {
        return Err(reject("only ASCII AIGER is supported"));
    }
    let max_variable = parse_number(header.next(), "maximum variable")?;
    let input_count = parse_number(header.next(), "input count")?;
    let latch_count = parse_number(header.next(), "latch count")?;
    let output_count = parse_number(header.next(), "output count")?;
    let gate_count = parse_number(header.next(), "AND count")?;
    let mut extensions = Vec::new();
    for value in header {
        extensions.push(parse_number(Some(value), "extended count")?);
    }
    if extensions.len() > 4 {
        return Err(reject("ASCII AIGER header has trailing fields"));
    }
    extensions.resize(4, 0);
    let [bad_count, constraint_count, justice_count, fairness_count] =
        extensions.try_into().unwrap();
    if max_variable > MAX_VARIABLES
        || input_count
            .checked_add(latch_count)
            .and_then(|count| count.checked_add(gate_count))
            != Some(max_variable)
    {
        return Err(reject("ASCII AIGER dimensions are invalid"));
    }
    if witness && (justice_count != 0 || fairness_count != 0) {
        return Err(reject("v1 does not compose liveness witnesses"));
    }

    let mut inputs = Vec::with_capacity(input_count);
    for index in 0..input_count {
        inputs.push(take_literal(&mut lines, &format!("input {index}"))?);
    }
    let mut latches = Vec::with_capacity(latch_count);
    for index in 0..latch_count {
        let line = lines
            .next()
            .ok_or_else(|| reject(format!("truncated AIGER latch {index}")))?;
        let mut fields = line.split_whitespace();
        let current = parse_number(fields.next(), "latch current")?;
        let next = parse_number(fields.next(), "latch next")?;
        let reset = match fields.next() {
            Some(value) => parse_number(Some(value), "latch reset")?,
            None => 0,
        };
        if fields.next().is_some() {
            return Err(reject("AIGER latch has trailing fields"));
        }
        latches.push(Latch {
            current,
            next,
            reset,
        });
    }
    let mut outputs = Vec::with_capacity(output_count);
    for index in 0..output_count {
        outputs.push(take_literal(&mut lines, &format!("output {index}"))?);
    }
    let mut bads = Vec::with_capacity(bad_count);
    for index in 0..bad_count {
        bads.push(take_literal(&mut lines, &format!("bad {index}"))?);
    }
    let mut constraints = Vec::with_capacity(constraint_count);
    for index in 0..constraint_count {
        constraints.push(take_literal(&mut lines, &format!("constraint {index}"))?);
    }
    if justice_count != 0 || fairness_count != 0 {
        return Err(reject("v1 does not parse AIGER liveness sections"));
    }
    let mut gates = Vec::with_capacity(gate_count);
    for index in 0..gate_count {
        let line = lines
            .next()
            .ok_or_else(|| reject(format!("truncated AIGER AND {index}")))?;
        let mut fields = line.split_whitespace();
        let output = parse_number(fields.next(), "AND output")?;
        let left = parse_number(fields.next(), "AND left")?;
        let right = parse_number(fields.next(), "AND right")?;
        if fields.next().is_some() {
            return Err(reject("AIGER AND has trailing fields"));
        }
        gates.push(Gate {
            output,
            left,
            right,
        });
    }

    let mut input_names = BTreeMap::new();
    let mut latch_names = BTreeMap::new();
    let mut comments = Vec::new();
    let mut in_comments = false;
    for line in lines {
        if in_comments {
            comments.push(line.to_string());
            continue;
        }
        if line == "c" {
            in_comments = true;
            continue;
        }
        let Some((kind_index, name)) = line.split_once(' ') else {
            return Err(reject("invalid AIGER symbol line"));
        };
        let (kind, index) = kind_index.split_at(1);
        let index = parse_number(Some(index), "symbol index")?;
        match kind {
            "i" if index < input_count => {
                if input_names.insert(index, name.to_string()).is_some() {
                    return Err(reject("duplicate AIGER input symbol"));
                }
            }
            "l" if index < latch_count => {
                if latch_names.insert(index, name.to_string()).is_some() {
                    return Err(reject("duplicate AIGER latch symbol"));
                }
            }
            "o" if index < output_count => {}
            "b" if index < bad_count => {}
            "c" if index < constraint_count => {}
            _ => return Err(reject("unsupported or invalid AIGER symbol")),
        }
    }
    if witness
        && comments.iter().any(|line| {
            line.split_whitespace()
                .next()
                .is_some_and(|word| word == "MAPPING" || word == "INTERVENTION")
        })
    {
        return Err(reject("v1 rejects comment-based witness mappings"));
    }

    let circuit = Aag {
        max_variable,
        inputs,
        latches,
        outputs,
        bads,
        constraints,
        gates,
        input_names,
        latch_names,
    };
    validate_aag(&circuit)?;
    Ok(circuit)
}

fn validate_aag(circuit: &Aag) -> Result<(), ComposedWitnessError> {
    let mut kinds = vec![None; circuit.max_variable + 1];
    for (index, &literal) in circuit.inputs.iter().enumerate() {
        let expected = 2 * (index + 1);
        if literal != expected {
            return Err(reject("AIGER inputs are not consecutively indexed"));
        }
        kinds[literal / 2] = Some(VariableKind::Input);
    }
    for (index, latch) in circuit.latches.iter().enumerate() {
        let expected = 2 * (circuit.inputs.len() + index + 1);
        if latch.current != expected {
            return Err(reject("AIGER latches are not consecutively indexed"));
        }
        kinds[latch.current / 2] = Some(VariableKind::Latch);
    }
    let variable_limit = circuit.max_variable * 2 + 1;
    for (index, gate) in circuit.gates.iter().enumerate() {
        let expected = 2 * (circuit.inputs.len() + circuit.latches.len() + index + 1);
        if gate.output != expected
            || gate.left / 2 >= gate.output / 2
            || gate.right / 2 >= gate.output / 2
        {
            return Err(reject("AIGER AND gates are not canonical and topological"));
        }
        kinds[gate.output / 2] = Some(VariableKind::Gate);
    }
    for literal in circuit
        .latches
        .iter()
        .flat_map(|latch| [latch.next, latch.reset])
        .chain(circuit.outputs.iter().copied())
        .chain(circuit.bads.iter().copied())
        .chain(circuit.constraints.iter().copied())
        .chain(
            circuit
                .gates
                .iter()
                .flat_map(|gate| [gate.left, gate.right]),
        )
    {
        if literal > variable_limit || (literal >= 2 && kinds[literal / 2].is_none()) {
            return Err(reject("AIGER references an undefined literal"));
        }
    }
    Ok(())
}

fn mapped_literal(name: Option<&String>) -> Result<Option<usize>, ComposedWitnessError> {
    let Some(name) = name else {
        return Ok(None);
    };
    let Some(value) = name.strip_prefix("= ") else {
        return Ok(None);
    };
    let literal = parse_number(Some(value), "mapped model literal")?;
    if !literal.is_multiple_of(2) || literal == 0 {
        return Err(reject(
            "mapped model literal must be positive and unnegated",
        ));
    }
    Ok(Some(literal))
}

fn model_kinds(model: &Aag) -> Vec<Option<VariableKind>> {
    let mut kinds = vec![None; model.max_variable + 1];
    for &literal in &model.inputs {
        kinds[literal / 2] = Some(VariableKind::Input);
    }
    for latch in &model.latches {
        kinds[latch.current / 2] = Some(VariableKind::Latch);
    }
    for gate in &model.gates {
        kinds[gate.output / 2] = Some(VariableKind::Gate);
    }
    kinds
}

fn translate(literal: usize, variables: &[usize]) -> Result<usize, ComposedWitnessError> {
    if literal < 2 {
        return Ok(literal);
    }
    let mapped = variables
        .get(literal / 2)
        .copied()
        .filter(|value| *value != 0)
        .ok_or_else(|| reject("witness literal was not mapped"))?;
    Ok(mapped * 2 + literal % 2)
}

/// Compose safety witness circuits using the repeated Theorem 1 construction
/// from FM 2026. The result is canonical ASCII AIGER 1.9.
pub fn compose_safety_witnesses_v1(
    model_bytes: &[u8],
    witness_bytes: &[&[u8]],
) -> Result<Vec<u8>, ComposedWitnessError> {
    if witness_bytes.len() < 2 || witness_bytes.len() > MAX_COMPOSED_WITNESSES {
        return Err(reject("composed witness count must be in 2..=64"));
    }
    let model = parse_aag(model_bytes, false)?;
    let model_kinds = model_kinds(&model);
    let witnesses = witness_bytes
        .iter()
        .map(|bytes| parse_aag(bytes, true))
        .collect::<Result<Vec<_>, _>>()?;

    let mut variable_maps = witnesses
        .iter()
        .map(|witness| vec![0; witness.max_variable + 1])
        .collect::<Vec<_>>();
    let mut shared = BTreeMap::<(VariableKind, usize), usize>::new();
    let mut input_symbols = BTreeMap::<usize, String>::new();
    let mut latch_symbols = BTreeMap::<usize, String>::new();
    let mut next_variable = 1usize;

    for (witness_index, witness) in witnesses.iter().enumerate() {
        let has_mapping = witness
            .input_names
            .values()
            .chain(witness.latch_names.values())
            .any(|name| name.starts_with("= "));
        for (index, &literal) in witness.inputs.iter().enumerate() {
            let mapping = if has_mapping {
                mapped_literal(witness.input_names.get(&index))?
            } else {
                model.inputs.get(index).copied()
            };
            let variable = assign_variable(
                VariableKind::Input,
                mapping,
                &model_kinds,
                &mut shared,
                &mut next_variable,
            )?;
            variable_maps[witness_index][literal / 2] = variable;
            let name = mapping.map_or_else(
                || {
                    format!(
                        "w{witness_index}:{}",
                        witness
                            .input_names
                            .get(&index)
                            .map(String::as_str)
                            .unwrap_or("private-input")
                    )
                },
                |mapped| format!("= {mapped}"),
            );
            input_symbols.entry(variable).or_insert(name);
        }
    }
    let input_count = next_variable - 1;

    for (witness_index, witness) in witnesses.iter().enumerate() {
        let has_mapping = witness
            .input_names
            .values()
            .chain(witness.latch_names.values())
            .any(|name| name.starts_with("= "));
        for (index, latch) in witness.latches.iter().enumerate() {
            let mapping = if has_mapping {
                mapped_literal(witness.latch_names.get(&index))?
            } else {
                model.latches.get(index).map(|latch| latch.current)
            };
            let variable = assign_variable(
                VariableKind::Latch,
                mapping,
                &model_kinds,
                &mut shared,
                &mut next_variable,
            )?;
            variable_maps[witness_index][latch.current / 2] = variable;
            let name = mapping.map_or_else(
                || {
                    format!(
                        "w{witness_index}:{}",
                        witness
                            .latch_names
                            .get(&index)
                            .map(String::as_str)
                            .unwrap_or("private-latch")
                    )
                },
                |mapped| format!("= {mapped}"),
            );
            latch_symbols.entry(variable).or_insert(name);
        }
    }
    let latch_count = next_variable - 1 - input_count;

    let mut gate_by_operands = BTreeMap::<(usize, usize), usize>::new();
    let mut gates = Vec::<Gate>::new();
    for (witness_index, witness) in witnesses.iter().enumerate() {
        for gate in &witness.gates {
            let mut left = translate(gate.left, &variable_maps[witness_index])?;
            let mut right = translate(gate.right, &variable_maps[witness_index])?;
            if left > right {
                std::mem::swap(&mut left, &mut right);
            }
            let variable = if let Some(&variable) = gate_by_operands.get(&(left, right)) {
                variable
            } else {
                let variable = next_variable;
                next_variable = next_variable
                    .checked_add(1)
                    .ok_or_else(|| reject("composed witness variable overflow"))?;
                if variable > MAX_VARIABLES {
                    return Err(reject("composed witness exceeds variable limit"));
                }
                gate_by_operands.insert((left, right), variable);
                gates.push(Gate {
                    output: variable * 2,
                    left,
                    right,
                });
                variable
            };
            variable_maps[witness_index][gate.output / 2] = variable;
        }
    }

    let mut latch_definitions = BTreeMap::<usize, (usize, usize)>::new();
    for (witness_index, witness) in witnesses.iter().enumerate() {
        for latch in &witness.latches {
            let variable = variable_maps[witness_index][latch.current / 2];
            let definition = (
                translate(latch.next, &variable_maps[witness_index])?,
                translate(latch.reset, &variable_maps[witness_index])?,
            );
            if let Some(previous) = latch_definitions.insert(variable, definition)
                && previous != definition
            {
                return Err(reject("shared witness latch definitions disagree"));
            }
        }
    }
    if latch_definitions.len() != latch_count {
        return Err(reject("composed witness latch definitions are incomplete"));
    }

    let mut bads = BTreeSet::new();
    let mut constraints = BTreeSet::new();
    for (witness_index, witness) in witnesses.iter().enumerate() {
        let safety = if witness.bads.is_empty() {
            &witness.outputs
        } else {
            &witness.bads
        };
        if safety.is_empty() {
            return Err(reject("witness has no safety property"));
        }
        for &literal in safety {
            bads.insert(translate(literal, &variable_maps[witness_index])?);
        }
        for &literal in &witness.constraints {
            constraints.insert(translate(literal, &variable_maps[witness_index])?);
        }
    }

    let mut output = String::new();
    output.push_str(&format!(
        "aag {} {} {} 0 {} {} {} 0 0\n",
        next_variable - 1,
        input_count,
        latch_count,
        gates.len(),
        bads.len(),
        constraints.len()
    ));
    for variable in 1..=input_count {
        output.push_str(&format!("{}\n", variable * 2));
    }
    for variable in (input_count + 1)..=(input_count + latch_count) {
        let (next, reset) = latch_definitions[&variable];
        output.push_str(&format!("{} {next} {reset}\n", variable * 2));
    }
    for literal in &bads {
        output.push_str(&format!("{literal}\n"));
    }
    for literal in &constraints {
        output.push_str(&format!("{literal}\n"));
    }
    for gate in &gates {
        output.push_str(&format!("{} {} {}\n", gate.output, gate.left, gate.right));
    }
    for (index, variable) in (1..=input_count).enumerate() {
        if let Some(name) = input_symbols.get(&variable) {
            output.push_str(&format!("i{index} {name}\n"));
        }
    }
    for (index, variable) in ((input_count + 1)..=(input_count + latch_count)).enumerate() {
        if let Some(name) = latch_symbols.get(&variable) {
            output.push_str(&format!("l{index} {name}\n"));
        }
    }
    output.push_str("c\nGCC FM 2026 composed-witness baseline v1\n");
    Ok(output.into_bytes())
}

fn assign_variable(
    kind: VariableKind,
    mapping: Option<usize>,
    model_kinds: &[Option<VariableKind>],
    shared: &mut BTreeMap<(VariableKind, usize), usize>,
    next_variable: &mut usize,
) -> Result<usize, ComposedWitnessError> {
    if let Some(literal) = mapping {
        if model_kinds.get(literal / 2).copied().flatten() != Some(kind) {
            return Err(reject("witness mapping kind does not match model"));
        }
        if let Some(&variable) = shared.get(&(kind, literal)) {
            return Ok(variable);
        }
    }
    let variable = *next_variable;
    *next_variable = next_variable
        .checked_add(1)
        .ok_or_else(|| reject("composed witness variable overflow"))?;
    if let Some(literal) = mapping {
        shared.insert((kind, literal), variable);
    }
    Ok(variable)
}

#[cfg(test)]
mod tests {
    use super::*;

    const MODEL: &[u8] =
        b"aag 3 1 1 1 1\n2\n4 6 0\n4\n6 4 2\ni0 sensor\nl0 state\no0 bad\nc\nmodel\n";
    const WITNESS: &[u8] = b"aag 3 1 1 1 1\n2\n4 6 0\n4\n6 4 2\ni0 = 2\nl0 = 4\no0 invariant\nc\nWITNESS o0 model.aag\n";

    #[test]
    fn self_composition_coalesces_shared_state_and_gates() {
        let first = compose_safety_witnesses_v1(MODEL, &[WITNESS, WITNESS]).unwrap();
        let second = compose_safety_witnesses_v1(MODEL, &[WITNESS, WITNESS]).unwrap();
        assert_eq!(first, second);
        let composed = parse_aag(&first, true).unwrap();
        assert_eq!(composed.inputs.len(), 1);
        assert_eq!(composed.latches.len(), 1);
        assert_eq!(composed.gates.len(), 1);
        assert_eq!(composed.bads.len(), 1);
        assert_eq!(composed.input_names[&0], "= 2");
        assert_eq!(composed.latch_names[&0], "= 4");
    }

    #[test]
    fn private_variables_remain_disjoint() {
        let witness =
            b"aag 4 2 1 1 1\n2\n4\n6 6 0\n8\n8 6 4\ni0 = 2\ni1 helper\nl0 = 4\nc\nprivate\n";
        let composed = compose_safety_witnesses_v1(MODEL, &[witness, witness]).unwrap();
        let parsed = parse_aag(&composed, true).unwrap();
        assert_eq!(parsed.inputs.len(), 3);
        assert_eq!(parsed.latches.len(), 1);
    }

    #[test]
    fn unsupported_and_hostile_witnesses_fail_closed() {
        let mapping = b"aag 3 1 1 1 1\n2\n4 6 0\n4\n6 4 2\nc\nMAPPING 1\n2 2\n";
        assert!(compose_safety_witnesses_v1(MODEL, &[mapping, WITNESS]).is_err());
        let symbol_start = WITNESS
            .windows(3)
            .position(|bytes| bytes == b"i0 ")
            .unwrap();
        for end in 0..symbol_start {
            assert!(compose_safety_witnesses_v1(MODEL, &[&WITNESS[..end], WITNESS]).is_err());
        }
        let wrong_kind = b"aag 3 1 1 1 1\n2\n4 6 0\n4\n6 4 2\ni0 = 4\nl0 = 4\nc\nwrong kind\n";
        assert!(compose_safety_witnesses_v1(MODEL, &[wrong_kind, WITNESS]).is_err());
    }
}