use verif::smt::{self, Verdict};
fn on_big_stack<F: FnOnce() + Send + 'static>(f: F) {
std::thread::Builder::new()
.stack_size(64 * 1024 * 1024)
.spawn(f)
.expect("spawn")
.join()
.expect("the query panicked");
}
fn exhaustive_evaluate(w: u8, x: u64, c: u64) {
let mask = if w >= 64 { u64::MAX } else { (1u64 << w) - 1 };
let x = x & mask;
let c = c & mask;
let want = x.wrapping_mul(c) & mask;
for (label, swapped) in [("x * c", false), ("c * x", true)] {
let got = smt::with_fresh(|| {
let (a, b) = if swapped {
(smt::word(c, w), smt::word(x, w))
} else {
(smt::word(x, w), smt::word(c, w))
};
smt::evaluate(smt::mul(a, b), |_| None)
});
assert_eq!(got, Some(want), "{w}-bit {label} = {x} * {c} (term folder)");
}
}
#[test]
fn the_term_folder_multiplies_exactly() {
for w in [1u8, 4, 8] {
let max = (1u64 << w) - 1;
for c in 0..=max {
for x in 0..=max {
exhaustive_evaluate(w, x, c);
}
}
}
}
#[test]
fn the_blast_constant_arm_agrees_with_the_schoolbook_arm() {
on_big_stack(|| {
for w in [4u8, 8] {
let max = (1u64 << w) - 1;
for c in 0..=max {
smt::with_fresh(|| {
let x = smt::var(w);
let y = smt::var(w);
let pinned = smt::eq(y, smt::word(c, w));
let schoolbook = smt::mul(x, y);
let shifted = smt::mul(x, smt::word(c, w));
let agree = smt::eq(schoolbook, shifted);
assert_eq!(
smt::prove(smt::or(smt::not(pinned), agree)),
Verdict::Valid,
"{w}-bit multiply by {c}: the two blast arms disagree"
);
});
}
}
});
}
#[test]
fn the_blast_constant_arm_matches_an_independent_shift_sum() {
on_big_stack(|| {
for w in [4u8, 8] {
let max = (1u64 << w) - 1;
for c in 0..=max {
smt::with_fresh(|| {
let x = smt::var(w);
let mut independent = smt::word(0, w);
for k in 0..w {
if (c >> k) & 1 == 1 {
independent = smt::add(independent, smt::shl(x, k as u32));
}
}
assert_eq!(
smt::prove(smt::eq(smt::mul(x, smt::word(c, w)), independent)),
Verdict::Valid,
"{w}-bit multiply by {c}: the arm disagrees with the shift sum"
);
});
}
}
});
}
#[test]
fn the_blast_constant_arm_is_not_the_identity() {
on_big_stack(|| {
smt::with_fresh(|| {
let x = smt::var(4);
assert!(
matches!(
smt::prove(smt::eq(smt::mul(x, smt::word(3, 4)), x)),
Verdict::Counterexample(_)
),
"x * 3 must not be x"
);
assert!(
matches!(
smt::prove(smt::eq(
smt::mul(x, smt::word(2, 4)),
smt::add(x, smt::word(2, 4))
)),
Verdict::Counterexample(_)
),
"x * 2 must not be x + 2"
);
});
});
}
#[test]
fn the_blast_constant_arm_is_exact_at_usize_width() {
on_big_stack(|| {
for c in [2u64, 4, 8, 256, 65536] {
smt::with_fresh(|| {
let x = smt::var(64);
let mut independent = smt::word(0, 64);
for k in 0..64 {
if (c >> k) & 1 == 1 {
independent = smt::add(independent, smt::shl(x, k as u32));
}
}
assert_eq!(
smt::prove(smt::eq(smt::mul(x, smt::word(c, 64)), independent)),
Verdict::Valid,
"64-bit multiply by {c} is a single shift"
);
});
}
for c in [17u64, 1000, 0xFFFF] {
smt::with_fresh(|| {
let x = smt::var(64);
let mut independent = smt::word(0, 64);
for k in 0..64 {
if (c >> k) & 1 == 1 {
independent = smt::add(independent, smt::shl(x, k as u32));
}
}
assert!(
!matches!(
smt::prove(smt::eq(smt::mul(x, smt::word(c, 64)), independent)),
Verdict::Counterexample(_)
),
"64-bit multiply by {c}: the arm must never be refuted"
);
});
}
});
}