use verif::smt::{self, Shift, 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 concrete_shl(w: u8, x: u64, n: u64) {
let mask = (1u64 << w) - 1;
let x = x & mask;
let n = n & mask;
smt::with_fresh(|| {
let got = smt::shift_var(Shift::Left, smt::word(x, w), smt::word(n, w));
let got = smt::evaluate(got, |_| None);
let want = if n >= w as u64 { 0 } else { (x << n) & mask };
assert_eq!(got, Some(want), "{w}-bit {x} << {n}");
});
}
fn concrete_lshr(w: u8, x: u64, n: u64) {
let mask = (1u64 << w) - 1;
let x = x & mask;
let n = n & mask;
smt::with_fresh(|| {
let got = smt::shift_var(Shift::LogicalRight, smt::word(x, w), smt::word(n, w));
let got = smt::evaluate(got, |_| None);
let want = if n >= w as u64 { 0 } else { x >> n };
assert_eq!(got, Some(want), "{w}-bit {x} >> {n}");
});
}
#[test]
fn left_shift_matches_rust_on_boundaries() {
for w in [2u8, 4, 8, 16] {
let max = (1u64 << w) - 1;
for (x, n) in [
(0, 0),
(0, max),
(1, 0),
(1, 1),
(max, 0),
(max, 1),
(max, w as u64 - 1),
(max, w as u64),
(max, w as u64 + 1),
(max, max),
(max / 2, 1),
(max / 3, 2),
(1u64 << (w - 1), 1),
] {
concrete_shl(w, x, n);
}
}
}
#[test]
fn logical_right_shift_matches_rust_on_boundaries() {
for w in [2u8, 4, 8, 16] {
let max = (1u64 << w) - 1;
for (x, n) in [
(0, 0),
(0, max),
(1, 0),
(1, 1),
(max, 0),
(max, 1),
(max, w as u64 - 1),
(max, w as u64),
(max, max),
(max / 2, 1),
(1u64 << (w - 1), 1),
(1u64 << (w - 1), w as u64 - 1),
] {
concrete_lshr(w, x, n);
}
}
}
#[test]
fn every_power_of_two_amount_agrees_with_rust() {
for w in [4u8, 8, 16] {
let max = (1u64 << w) - 1;
let mut i = 0u32;
while (1u32 << i) < w as u32 {
let amt = 1u64 << i;
concrete_shl(w, max, amt);
concrete_shl(w, 1, amt);
concrete_lshr(w, max, amt);
i += 1;
}
}
}
#[test]
fn barrel_agrees_with_the_constant_path_symbolically() {
on_big_stack(|| {
for k in [0u32, 1, 2, 3] {
smt::with_fresh(|| {
let x = smt::var(4);
let n = smt::word(k as u64, 4);
let via_barrel = smt::shift_var(Shift::Left, x, n);
let via_const = smt::shl(x, k);
assert_eq!(
smt::prove(smt::eq(via_barrel, via_const)),
Verdict::Valid,
"x << {k} must agree through both paths"
);
});
}
});
}
#[test]
fn a_wider_shift_amount_is_read_at_its_own_width() {
for value_w in [1u8, 4, 8, 16, 32, 64] {
for amount_w in [8u8, 16, 32, 64] {
if amount_w < value_w {
continue;
}
on_big_stack(move || {
let vmask = if value_w >= 64 { u64::MAX } else { (1u64 << value_w) - 1 };
let amask = if amount_w >= 64 { u64::MAX } else { (1u64 << amount_w) - 1 };
for x in [0u64, 1, 3, vmask, vmask & 0xAA] {
for n in [0u64, 1, value_w as u64 - 1, value_w as u64, value_w as u64 + 1, amask] {
let xm = x & vmask;
smt::with_fresh(|| {
let xv = smt::word(xm, value_w);
let nv = smt::word(n & amask, amount_w);
let shl = smt::evaluate(smt::shift_var(Shift::Left, xv, nv), |_| None);
let want = if n >= value_w as u64 { 0 } else { (xm << n) & vmask };
assert_eq!(
shl, Some(want),
"{value_w}-bit {xm} << {n} with a {amount_w}-bit amount"
);
let lshr =
smt::evaluate(smt::shift_var(Shift::LogicalRight, xv, nv), |_| None);
let want = if n >= value_w as u64 { 0 } else { xm >> n };
assert_eq!(
lshr, Some(want),
"{value_w}-bit {xm} >> {n} with a {amount_w}-bit amount"
);
});
}
}
});
}
}
}
#[test]
fn a_wider_shift_amount_is_read_at_its_own_width_symbolically() {
for value_w in [4u8, 8, 16] {
for amount_w in [16u8, 32, 64] {
if amount_w <= value_w {
continue;
}
on_big_stack(move || {
for k in [0u32, 1, 2, 3] {
if k >= value_w as u32 {
continue;
}
smt::with_fresh(|| {
let x = smt::var(value_w);
let n = smt::word(k as u64, amount_w);
let via_barrel = smt::shift_var(Shift::Left, x, n);
let via_const = smt::shl(x, k);
assert_eq!(
smt::prove(smt::eq(via_barrel, via_const)),
Verdict::Valid,
"x << {k} at {value_w} bits with a {amount_w}-bit amount must \
agree through both paths"
);
});
}
});
}
}
}
#[test]
fn a_variable_shift_is_not_the_identity() {
on_big_stack(|| {
smt::with_fresh(|| {
let x = smt::var(4);
let n = smt::var(4);
let shifted = smt::shift_var(Shift::Left, x, n);
let in_range = smt::ult_const(n, 4);
let differs = smt::and(in_range, smt::not(smt::eq(shifted, x)));
match smt::find_model(differs) {
Verdict::Counterexample(_) => {}
other => panic!(
"`x << n != x` must have a model; an identity shifter makes it \
unsatisfiable, got: {other}"
),
}
let same = smt::and(in_range, smt::eq(shifted, x));
assert_ne!(
smt::prove(same),
Verdict::Valid,
"`x << n == x` must not be a theorem, or the shift does nothing"
);
});
});
}
#[test]
fn shift_left_by_the_width_is_the_defined_zero() {
for w in [4u8, 8, 16] {
let max = (1u64 << w) - 1;
concrete_shl(w, max, w as u64);
concrete_shl(w, 1, w as u64);
}
}
#[test]
fn arithmetic_right_shift_replicates_the_sign_bit() {
for w in [4u8, 8, 16] {
let max = (1u64 << w) - 1;
for (x, n) in [(max, 0u64), (max, 1), (max, w as u64 - 1), (1, 1), (0, max)] {
smt::with_fresh(|| {
let got =
smt::shift_var(Shift::ArithmeticRight, smt::word(x, w), smt::word(n, w));
let got = smt::evaluate(got, |_| None);
let signed = if x >> (w - 1) == 1 {
(x as i64) - (1i64 << w)
} else {
x as i64
};
let want = if n >= w as u64 {
if signed < 0 {
max
} else {
0
}
} else {
((signed >> n) as u64) & max
};
assert_eq!(got, Some(want), "{w}-bit {x} >> {n} (arithmetic)");
});
}
}
}