use verif::arith::prove::{self, Verdict as EnumVerdict};
use verif::logic::{verify_checked_add, verify_saturating_add_bounds};
use verif::primitives::{
verify_accumulator_no_overflow, verify_bounded_sub_is_total, verify_safe_add_is_exact,
verify_saturating_add_monotone,
};
use verif::smt;
fn both_prove(name: &str, enumerated: EnumVerdict, symbolic: smt::Verdict) {
assert!(
enumerated.is_proved(),
"{}: enumeration did not prove it: {:?}",
name,
enumerated
);
assert!(
symbolic.is_proved(),
"{}: symbolic engine did not prove it: {}",
name,
symbolic
);
}
#[test]
fn checked_add_agrees() {
both_prove(
"checked_add_is_total",
prove::checked_add_is_total(),
verify_checked_add(),
);
}
#[test]
fn saturating_add_bounds_agree() {
both_prove(
"saturating_add_bounds",
prove::saturating_add_bounds(),
verify_saturating_add_bounds(),
);
}
#[test]
fn safe_add_is_exact_agrees_at_8_and_16() {
for w in [8u8, 16] {
both_prove(
&format!("safe_add_is_exact(w={})", w),
prove::safe_add_is_exact(w),
verify_safe_add_is_exact(w),
);
}
}
#[test]
fn accumulator_no_overflow_agrees_on_a_12_bit_domain() {
both_prove(
"accumulator_no_overflow(12, 1000)",
prove::accumulator_no_overflow(12, 1000),
verify_accumulator_no_overflow(12, 1000),
);
}
#[test]
fn saturating_add_monotone_agrees() {
both_prove(
"saturating_add_monotone(8, 7)",
prove::saturating_add_monotone(8, 7),
verify_saturating_add_monotone(8, 7),
);
}
#[test]
fn the_32_bit_domain_is_where_the_symbolic_engine_earns_its_keep() {
for w in [8u8, 16] {
assert!(prove::safe_add_is_exact(w).is_proved(), "enumeration at {}", w);
}
assert_eq!(
prove::safe_add_is_exact(32),
EnumVerdict::Unknown,
"2^64 cases is past the enumeration budget; it must decline, not guess"
);
assert!(
verif::primitives::verify_safe_add_is_exact(32).is_proved(),
"the symbolic engine must prove at 32 what enumeration cannot reach"
);
assert_eq!(prove::sub_self_is_zero(33), EnumVerdict::Unknown);
}
#[test]
fn bounded_sub_refinement_is_proved_at_every_width_each_engine_can_reach() {
for (min, max) in [(0u64, 255u64), (10, 100), (5, 200)] {
assert!(
prove::bounded_sub_is_total(min, max).is_proved(),
"enumeration must prove the 8-bit instance ({}, {})",
min,
max
);
assert!(
verify_bounded_sub_is_total(min, max).is_proved(),
"smt must prove the 32-bit instance ({}, {})",
min,
max
);
}
}
#[test]
fn the_full_range_sub_is_only_the_symbolic_engine() {
assert!(verify_bounded_sub_is_total(0, u32::MAX as u64).is_proved());
assert!(verify_bounded_sub_is_total(1, u32::MAX as u64).is_proved());
assert!(verify_bounded_sub_is_total(5, u32::MAX as u64).is_proved());
}
#[test]
fn a_false_accumulator_bound_is_refuted_by_both() {
for (w, max) in [(8u8, 200u64), (8, 128), (8, 255)] {
let symbolic = verify_accumulator_no_overflow(w, max);
assert!(
matches!(symbolic, smt::Verdict::Counterexample(_)),
"width {} max {} must be refuted with a witness, got {}",
w,
max,
symbolic
);
let enumerated = prove::accumulator_no_overflow(w, max);
assert!(
!enumerated.is_proved(),
"the enumeration must not prove the false bound (w={}, max={})",
w,
max
);
}
}
#[test]
fn a_deliberately_false_obligation_is_not_proved() {
let enumerated = prove::accumulator_no_overflow(8, 200);
match enumerated {
EnumVerdict::Counterexample(w) => {
assert_eq!(w.len, 2);
let sum = w.a.wrapping_add(w.b) & 0xFF;
assert!(sum < w.a, "the witness must actually overflow");
}
other => panic!("expected a counterexample, got {:?}", other),
}
}
#[test]
fn the_two_verdict_types_agree_on_what_proved_means() {
assert!(EnumVerdict::Valid.is_proved());
assert!(smt::Verdict::Valid.is_proved());
assert!(!EnumVerdict::Unknown.is_proved());
assert!(!smt::Verdict::Unknown.is_proved());
assert!(!EnumVerdict::Unknown.is_decided());
assert!(!smt::Verdict::Unknown.is_decided());
}