Bit vector expression extension in Z3 - z3

I have a 32bit bit vector expression. Somehow I want to do a signed or unsigned extension on this expression to a 64bit bit vector. Is there any API I can use?

For sign extension:
Z3_ast Z3_API Z3_mk_sign_ext(__in Z3_context c, __in unsigned i, __in Z3_ast t1);
https://github.com/Z3Prover/z3/blob/master/src/api/z3_api.h#L2826
For unsigned extension:
Z3_ast Z3_API Z3_mk_zero_ext(__in Z3_context c, __in unsigned i, __in Z3_ast t1);
https://github.com/Z3Prover/z3/blob/master/src/api/z3_api.h#L2838
These functions are also available in bindings for Python, C#, Java

Related

What is the right way to perform 64-bit arithmetic?

I've found primitives for 64-bit floating point and 32-bit signed integer operations. I've not found primitives for 64-bit unsigned integer operations. Without those, what is the correct way to deal with 64-bit uint arithmetic on Agda?
It seems to be present on 2.5.4. (Exactly one version above the one I was using.)
Added support for built-in 64-bit machine words.
These are defined in Agda.Builtin.Word and come with two primitive operations to convert to and from natural numbers.
Word64 : Set
primWord64ToNat : Word64 → Nat
primWord64FromNat : Nat → Word64
Converting to a natural number is the trivial embedding, and converting from a natural number gives you the remainder modulo 2^64. The proofs of these theorems are not primitive, but can be defined in a library using primTrustMe.
Basic arithmetic operations can be defined on Word64 by converting to natural numbers, peforming the corresponding operation, and then converting back. The compiler will optimise these to use 64-bit arithmetic. For instance,
addWord : Word64 → Word64 → Word64
addWord a b = primWord64FromNat (primWord64ToNat a + primWord64ToNat b)
subWord : Word64 → Word64 → Word64
subWord a b = primWord64FromNat (primWord64ToNat a + 18446744073709551616 - primWord64ToNat b)
These compiles (in the GHC backend) to addition and subtraction on Data.Word.Word64

What does this symbol mean: ∧?

I need to calculate this equation using Delphi programming language
z = (Rot(y ∧ n1 , K2) ∧ K1 ) ⊕ n2
Where:
K1, K2, n1, n2, y are 96-bits binary values
I just want to know what does this symbol means "∧", and how to us it in Delphi?
It might be bitwise AND.
The ⊕ could be exclusive or XOR in Delphi.
The tricky bit might be the ROT operation which rotates the bits of a variable. There is no ROT operation but there is shl and shr for left and right shift. See Delphi Expressions
To make things even harder you don't have a native 96 bit datatype. LongInt is 4 bytes = 32 bit. You will need to use an array if you need to represent the fill 96 bit.

How to convert a variable's value to an int in a model?

When using a model object, I call func_decl get_func_decl (unsigned i) to get the value assigned to a certain function (variable). The problem I am having is taking the output of that (which is a func_decl) and converting it to int.
For example, if the model is {x |-> 4, y |-> 12, z |-> 6}, I would like to get the actual int values of those 3 variables (4, 12 and 6).
Z3 provides the function Z3_get_numeral_int for this purpose:
Z3_bool Z3_API Z3_get_numeral_int(__in Z3_context c, __in Z3_ast v, __out int* i);
Note that the last parameter is a point to an integer that will be filled with the right value if the call succeeds (it will fail for non-numerals or numerals which are not representable as an int).
There are also other functions called Z3_get_numeral_* to obtain values of different types, e.g., uint64 etc.
This method works for constants (i.e., func_decls of arity 0). To get the entries of a (non-zero arity) function definition, the following function should be used:
Z3_func_entry Z3_API Z3_func_interp_get_entry(__in Z3_context c, __in Z3_func_interp f, unsigned i);

How to read smtlib2 strings using Z3 C++ api?

I want to create an expr object from a given SMTLIB2 file. I can see a Z3_parse_smtlib_string function in the C examples. Is there a wrapper for that in the expr class?
The Z3 C++ API does not explicitly provide this functionality as part of the expr class. However, the C++ API can be used alongside the C API, i.e., the function Z3_parse_smtlib_string (or ..._file) can be used to achieve this. Note that this function returns a Z3_ast, which must be converted to an expr object to get back to the C++ "world".
A simple example:
#include <z3++.h>
...
context ctx;
Z3_ast a = Z3_parse_smtlib2_file(ctx, "test.smt2", 0, 0, 0, 0, 0, 0);
expr e(ctx, a);
std::cout << "Result = " << e << std::endl;
Since the Z3_parse_smtlib2_* functions do not perform error checking, no exception will be thrown upon errors. This can be achieved by calls to context::check_error().

Z3 C API manage Z3_VAR_AST

I try to write a custom print for a Z3_ast of Z3 in C, but I do not know how to manage the Z3_ast_kind of Z3_VAR_AST and Z3_FUNC_DECL_AST, I only know how to print the Z3_sort of Z3_VAR_AST (Z3_get_sort), about this variable value I have no ideas ???. And about the Z3_FUNC_DECL_AST, I couldn't find any accessors can get the function name, number of parameters and the parameters. Could you guys please help me? Cheers
I suggest you take a look at the file 'python/z3printer.py' in the Z3 distribution. It defines a custom pretty printer in Python. The Z3 python API is just a layer on top of the C API. So, it should be straightforward to convert this printer in C.
Regarding Z3_VAR_AST, the function
unsigned Z3_API Z3_get_index_value(__in Z3_context c, __in Z3_ast a);
returns the de-Brujin index for the variable. The meaning of the index is explained here: http://en.wikipedia.org/wiki/De_Bruijn_index
The variable names are stored in the quantifier AST. Note that, the names are irrelevant for Z3. They are stored just to make the output nice. The code in z3printer.py will keeps a stack with the variable names.
Regarding Z3_FUNC_DECL_AST, it is easier to handle than Z3_VAR_AST. ASTs of this kind are actually Z3_func_decl. Then the following APIs can be used to extract the information you want:
Z3_symbol Z3_API Z3_get_decl_name(__in Z3_context c, __in Z3_func_decl d);
Z3_decl_kind Z3_API Z3_get_decl_kind(__in Z3_context c, __in Z3_func_decl d);
unsigned Z3_API Z3_get_domain_size(__in Z3_context c, __in Z3_func_decl d);
unsigned Z3_API Z3_get_arity(__in Z3_context c, __in Z3_func_decl d);
Z3_sort Z3_API Z3_get_domain(__in Z3_context c, __in Z3_func_decl d, __in unsigned i);
Again, the file z3printer.py uses all these functions.

Resources