use verif::exhaustive::for_all_u16;
use verif::prelude::*;
use verif::smt::{self, Verdict};
const ADC_MAX: u32 = 4095;
const PWM_MIN: u32 = 1000;
const PWM_MAX: u32 = 2000;
const RATE_LIMIT: u32 = 50;
pub const fn map_reading(raw: u32) -> u32 {
let clamped = if raw > ADC_MAX { ADC_MAX } else { raw };
PWM_MIN + (clamped * (PWM_MAX - PWM_MIN)) / ADC_MAX
}
pub const fn rate_limit(current: u32, target: u32) -> u32 {
if target > current {
let step = target - current;
if step > RATE_LIMIT {
current + RATE_LIMIT
} else {
target
}
} else {
let step = current - target;
if step > RATE_LIMIT {
current - RATE_LIMIT
} else {
target
}
}
}
pub fn the_bound_is_refutable_at(width: u8) -> bool {
let raw = var(width);
matches!(
smt::find_model((raw * 1000u32).gt_val(4_095_000)),
Verdict::Counterexample(_)
)
}
fn prove_output_bound() -> Verdict {
smt::with_fresh(|| {
let raw = var(22);
let max = word(ADC_MAX as u64, 22);
let clamped = ite(raw.gt(max), max, raw);
let scaled = clamped * 1000u32;
assert!(
the_bound_is_refutable_at(22),
"the bound is vacuous at 22 bits: the unclamped product cannot exceed \
4 095 000, so the implication carries no information"
);
prove_implies(clamped.le(max), scaled.le_val(4_095_000))
})
}
fn prove_rate_limit_bound() -> Verdict {
smt::with_fresh(|| {
let current = var(12);
let target = var(12);
let limit = word(RATE_LIMIT as u64, 12);
let next = rate_limit_symbolic(current, target, limit);
let delta = ite(next.ge(current), next - current, current - next);
delta.le(limit).prove()
})
}
fn rate_limit_symbolic(current: Var, target: Var, limit: Var) -> Var {
let up = target.gt(current);
let raw_step = ite(up, target - current, current - target);
let clamped_step = ite(raw_step.gt(limit), limit, raw_step);
ite(up, current + clamped_step, current - clamped_step)
}
fn proofs_by_enumeration() -> bool {
for_all_u16(|raw| {
if raw > ADC_MAX as u16 {
return true; }
let out = map_reading(raw as u32);
(PWM_MIN..=PWM_MAX).contains(&out)
})
}
fn clamp_holds_for_every_u16() -> bool {
for_all_u16(|raw| {
let out = map_reading(raw as u32);
(PWM_MIN..=PWM_MAX).contains(&out)
})
}
fn rate_limit_bound_by_enumeration() -> bool {
verif::exhaustive::for_all_u8_2(|c, t| {
let next = rate_limit(c as u32, t as u32);
let delta = next.abs_diff(c as u32);
delta <= RATE_LIMIT
})
}
fn map_is_monotone() -> bool {
verif::exhaustive::for_all_u16(|a| {
let b = a.wrapping_add(1);
if b <= a {
return true;
}
map_reading(a as u32) <= map_reading(b as u32)
})
}
fn main() {
let mut failures = 0;
let v = prove_output_bound();
println!("[symbolic] map_reading output bound : {}", v);
if !v.is_proved() {
failures += 1;
}
let v = prove_rate_limit_bound();
println!("[symbolic] rate_limit step bound : {}", v);
if !v.is_proved() {
failures += 1;
}
let ok = clamp_holds_for_every_u16();
println!("[exhaustive] clamp holds for all 65536 : {}", ok);
if !ok {
failures += 1;
}
let ok = proofs_by_enumeration();
println!("[exhaustive] output in [PWM_MIN, PWM_MAX] : {}", ok);
if !ok {
failures += 1;
}
let ok = rate_limit_bound_by_enumeration();
println!("[exhaustive] rate limit for all u8 pairs : {}", ok);
if !ok {
failures += 1;
}
let ok = map_is_monotone();
println!("[exhaustive] mapping is monotone : {}", ok);
if !ok {
failures += 1;
}
let cert = smt::with_fresh(|| {
let raw = var(16);
let max = word(ADC_MAX as u64, 16);
Certificate::discharge(implies(raw.le(max), raw.le(max)))
});
match cert {
Some(c) => {
let v = Verified::from_certificate(map_reading(2048), c);
match v {
Some(v) => println!("[kernel] certificate accepted, value = {}", v.get()),
None => println!("[kernel] certificate was stale, refused"),
}
}
None => {
println!("[kernel] certificate rejected");
failures += 1;
}
}
let bad = smt::with_fresh(|| {
let raw = var(12);
raw.lt_val(ADC_MAX as u64).prove()
});
match bad {
Verdict::Counterexample(c) => {
let witness = c.value_of(0).unwrap();
println!(
"[refuted] raw < ADC_MAX is false, witness raw = {}",
witness
);
}
other => {
println!("[refuted] expected a refutation, got {}", other);
failures += 1;
}
}
let hard = smt::with_fresh(|| {
let a = var(12);
let b = var(12);
let c = var(12);
((a * b) * c).eq(a * (b * c)).prove()
});
println!("[honesty] (a*b)*c == a*(b*c) at 12 bits : {}", hard);
assert!(
matches!(hard, Verdict::Valid | Verdict::Unknown),
"engine must never refute a true statement"
);
println!();
if failures == 0 {
println!("all obligations discharged");
} else {
println!("{} obligation(s) FAILED", failures);
std::process::exit(1);
}
}