Skip to content

Add missing API for BV #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions z3/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,34 @@ impl<'ctx> BV<'ctx> {
Some(unsafe { Self::wrap(ctx, ast) })
}

pub fn from_bit_vec(ctx: &'ctx Context, bit_vec: Vec<bool>) -> Option<BV<'ctx>> {
let ast = unsafe {
let size = bit_vec.len() as u32;
let bits = bit_vec.as_ptr();

let numeral_ptr = Z3_mk_bv_numeral(ctx.z3_ctx, size, bits);
if numeral_ptr.is_null() {
return None;
}

numeral_ptr
};
Some(unsafe { Self::wrap(ctx, ast) })
}

pub fn from_byte_vec(ctx: &'ctx Context, byte_vec: Vec<u8>) -> Option<BV<'ctx>> {
let mut bit_vec = Vec::with_capacity(byte_vec.len() * 8);
for item in byte_vec {
for offset in 0..8 {
let mask = 1 << offset;
let bit = item & mask != 0;
bit_vec.push(bit);
}
}

Self::from_bit_vec(ctx, bit_vec)
}

pub fn new_const<S: Into<Symbol>>(ctx: &'ctx Context, name: S, sz: u32) -> BV<'ctx> {
let sort = Sort::bitvector(ctx, sz);
unsafe {
Expand Down
43 changes: 43 additions & 0 deletions z3/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2002,3 +2002,46 @@ fn test_model_iter() {
vec![ast::Dynamic::new_const(&ctx, "a", &Sort::int(&ctx))]
);
}

#[test]
fn test_bitvector_from_bit_vec() {
let cfg = Config::new();
let ctx = Context::new(&cfg);

// 0b01010101
let bit_vec = vec![true, false, true, false, true, false, true, false];

let a = ast::BV::new_const(&ctx, "a", 8);
let b = ast::BV::from_bit_vec(&ctx, bit_vec).unwrap();

let solver = Solver::new(&ctx);
solver.assert(&a._eq(&b));
assert_eq!(solver.check(), SatResult::Sat);

let model = solver.get_model().unwrap();
let av = model.eval(&a, true).unwrap().to_string();
assert_eq!(av, "#x55".to_string());
}

#[test]
fn test_bitvector_from_byte_vec() {
let cfg = Config::new();
let ctx = Context::new(&cfg);

// 0x55AA
// 85 = 0x55 = 01010101
// 170 = 0xAA = 10101010
// The array should be ordered like this (at least that is how it works in rust internally)
let byte_vec = vec![170, 85];

let a = ast::BV::new_const(&ctx, "a", 16);
let b = ast::BV::from_byte_vec(&ctx, byte_vec).unwrap();

let solver = Solver::new(&ctx);
solver.assert(&a._eq(&b));
assert_eq!(solver.check(), SatResult::Sat);

let model = solver.get_model().unwrap();
let av = model.eval(&a, true).unwrap().to_string();
assert_eq!(av, "#x55aa".to_string());
}
Loading