z3py

Defining constraints in Z3 using Boolean operators

血红的双手。 提交于 2019-12-02 05:24:40
问题 Let's say, I want to restrict each character of the string to the charset: [a-zA-Z0-9_] using Z3 constraints, can I use a boolean operator to specify that? As an example: input = [BitVec("input%s" % i, 8) for i in range(10)] for i in range(10): s.add(input[i] >= 0x30 and input[i] <= 0x39) s.add(input[i] >= 0x41 and input[i] <= 0x5A) s.add(input[i] >= 0x61 and input[i] <= 0x7A) Is this correct? Any other efficient way to define constraints? Usually in Python, I could do something like: import

z3 fails with this system of equations

 ̄綄美尐妖づ 提交于 2019-12-02 03:37:57
Over the years I keep track of solving technology - and I maintain a blog post about applying them to a specific puzzle - the "crossing ladders". To get to the point, I accidentally found out about z3, and tried putting it to use in the specific problem. I used the Python bindings, and wrote this: $ cat laddersZ3.py #!/usr/bin/env python from z3 import * a = Int('a') b = Int('b') c = Int('c') d = Int('d') e = Int('e') f = Int('f') solve( a>0, a<200, b>0, b<200, c>0, c<200, d>0, d<200, e>0, e<200, f>0, f<200, (e+f)**2 + d**2 == 119**2, (e+f)**2 + c**2 == 70**2, e**2 + 30**2 == a**2, f**2 + 30*

Nonzero vector in quantifier

非 Y 不嫁゛ 提交于 2019-12-02 03:37:39
I want to verify a formula of the form: Exists p . ForAll x != 0 . f(x, p) > 0 An implementation (that isn't working) is the following: def f0(x0, x1, x, y): return x1 ** 2 * y + x0 ** 2 * x s = Solver() x0, x1 = Reals('x0 x1') p0, p1 = Reals('p0 p1') s.add(Exists([p0, p1], ForAll([x0, x1], f0(x0, x1, p0, p1) > 0 ) )) #s.add(Or(x0 != 0, x1 != 0)) while s.check() == sat: m = s.model() m.evaluate(x0, model_completion=True) m.evaluate(x1, model_completion=True) m.evaluate(p0, model_completion=True) m.evaluate(p1, model_completion=True) print m s.add(Or(x0 != m[x0], x1 != m[x1])) The formula isn't

Defining constraints in Z3 using Boolean operators

为君一笑 提交于 2019-12-02 03:33:25
Let's say, I want to restrict each character of the string to the charset: [a-zA-Z0-9_] using Z3 constraints, can I use a boolean operator to specify that? As an example: input = [BitVec("input%s" % i, 8) for i in range(10)] for i in range(10): s.add(input[i] >= 0x30 and input[i] <= 0x39) s.add(input[i] >= 0x41 and input[i] <= 0x5A) s.add(input[i] >= 0x61 and input[i] <= 0x7A) Is this correct? Any other efficient way to define constraints? Usually in Python, I could do something like: import string charset = string.uppercase + string.lowercase + string.digits + "_" for i in charset: ... Can

Unsatisfiable Cores in Z3 Python

坚强是说给别人听的谎言 提交于 2019-12-01 21:54:34
问题 I am working with the Python API of Z3 in an attempt to include support for it in a research tool that I am writing. I have a question regarding extracting the unsatisfiable core using the Python interface. I have the following simple query: (set-option :produce-unsat-cores true) (assert (! (not (= (_ bv0 32) (_ bv0 32))) :named __constraint0)) (check-sat) (get-unsat-core) (exit) Running this query through the z3 executable (for Z3 4.1 ), I receive the expected result: unsat (__constraint0)

How get a a value from a Lambda expression?

房东的猫 提交于 2019-12-01 18:52:02
I'm experimenting with z3 in python. I have the following model: (set-option :produce-models true) (set-logic QF_AUFBV ) (declare-fun a () (Array (_ BitVec 32) (_ BitVec 8) ) ) (declare-fun another () (Array (_ BitVec 32) (_ BitVec 8) ) ) (assert (and (= false (= (_ bv77 32) (concat (select a (_ bv3 32) ) (concat (select a (_ bv2 32) ) (concat (select a (_ bv1 32) ) (select a (_ bv0 32) ) ) ) ) ) ) (= false (= (_ bv12 32) (concat (select another (_ bv3 32) ) (concat (select another (_ bv2 32) ) (concat (select another (_ bv1 32) ) (select another (_ bv0 32) ) ) ) ) ) ) ) ) I can load it and

How to model signed integer with BitVector?

对着背影说爱祢 提交于 2019-12-01 04:45:52
问题 Suppose that a is an integer of 8-bit of value 254 . If a is a signed integer, it is actually considered -2 . In contrary, if a is unsigned, it remains 254 . I am trying to model this signed/unsigned integer problem with BitVector theory with Z3, but it seems BitVector doesnt allow this. Is this true? Then any idea on how to model this in Z3py? Thanks so much. 回答1: Z3 has APIs for the signed and unsigned interpretations. For example, in the C API, Z3_mk_bvslt creates the signed less than, and

Sum of all the bits in a Bit Vector of Z3

落爺英雄遲暮 提交于 2019-12-01 02:10:00
Given a bit vector in Z3 , I am wondering how can I sum up each individual bit of this vector? E.g., a = BitVecVal(3, 2) sum_all_bit(a) = 2 Is there any pre-implemented APIs/functions that support this? Thank you! Nikolaj Bjorner It isn't part of the bit-vector operations. You can create an expression as follows: def sub(b): n = b.size() bits = [ Extract(i, i, b) for i in range(n) ] bvs = [ Concat(BitVecVal(0, n - 1), b) for b in bits ] nb = reduce(lambda a, b: a + b, bvs) return nb print sub(BitVecVal(4,7)) Of course, log(n) bits for the result will suffice if you prefer. The page: https:/

How to print z3 solver results print(s.model()) in order?

≡放荡痞女 提交于 2019-11-29 17:25:15
Suppose I have a list of 10 variables v = [Real('v_%s' % (i+1)) for i in range(10)] and I want to add a simple constraint like this s = Solver() for i in range(10): s.add(v[i] == i) if s.check() == sat: print(s.model()) So a satisfying model is v_1 = 0, v_2 = 1 .... v_10 = 9 . However the output of print(s.model()) is totoally unordered which makes me confused when I have lots of variables in a bigger model. For this example, the output of my computer is v_5, v_7, v_4, v_2, v_1, v_3, v_6, v_8, v_9, v_10 , but I want to ouput the variables of this model in order like v_1, v_2, ..., v_10 . Can

K-out-of-N constraint in Z3Py

徘徊边缘 提交于 2019-11-27 23:40:05
I am using the Python bindings for the Z3 theorem prover (Z3Py). I have N boolean variables, x1,..,xN. I want to express the constraint that exactly K out of N of them should be true. How can I do that, in Z3Py? Is there any built-in support for that? I checked the online documentation, but the Z3Py docs don't have any mention of any API for that. For one-out-of-N constraints, I know I can separately express that at least one is true (assert Or(x1,..,xN)) and that at most one is true (assert Not(And(xi,xj)) for all i,j). I also know of other ways to manually express the 1-out-of-N and K-out-of