-1

My task is:

input 10023678 (as a decimal number)

output 10023678 (as a hex number:)

I have solved this using the SMT-LIB formula in the bit-vector logic. But I would love to know how can I do it in python using the "import z3"


.smt2 INPUT CODE:


(set-logic QF_BV)
(set-option :produce-models true)
(declare-fun x () (_ BitVec 32))

(assert (= ((_ extract 3 3) x) ((_ extract 3 3 ) (bvnot(bvxor x x)))))
(assert (= ((_ extract 4 4) x) ((_ extract 4 4) (bvnot(bvxor x x)))))
(assert (= ((_ extract 5 5) x) ((_ extract 5 5) (bvnot(bvxor x x)))))
(assert (= ((_ extract 6 6) x) ((_ extract 6 6) (bvnot(bvxor x x)))))
(assert (= ((_ extract 9 9) x) ((_ extract 9 9) (bvnot(bvxor x x)))))
(assert (= ((_ extract 10 10) x) ((_ extract 10 10) (bvnot(bvxor x x)))))
(assert (= ((_ extract 12 12) x) ((_ extract 12 12) (bvnot(bvxor x x)))))
(assert (= ((_ extract 13 13) x) ((_ extract 13 13) (bvnot(bvxor x x)))))
(assert (= ((_ extract 17 17) x) ((_ extract 17 17) (bvnot(bvxor x x)))))
(assert (= ((_ extract 28 28) x) ((_ extract 28 28) (bvnot(bvxor x x)))))

(check-sat)
(get-model)
(exit)

OUTPUT:


sat
(
  (define-fun x () (_ BitVec 32)
    #x10023678)
)

1 Answers1

0

Your question is very confusing: You're saying "convert input 10023678 to output.." But there's no input to the SMTLib program you've posted. You've merely asserted some constraints, and z3 gave you a solution that satisfied those constraints.

Ignoring that aside, it's relatively easy to translate the given code to Z3py. Note that (bvxor x x) is the 0 bit-vector for any x: If you xor a number with itself, you'll simply get 0. Then, bvnot (bvxor x x) is simply the bit-vector that consists of all 1's. So, the calls to extract on the right handside always return the single 1-bit vector containing 1. With this in mind, the solution in z3py would be:

from z3 import *

x = BitVec('x', 32)

s = Solver()
def is_set(i):
    s.add(Extract(i, i, x) == BitVecVal(1, 1))

is_set( 3)
is_set( 4)
is_set( 5)
is_set( 6)
is_set( 9)
is_set(10)
is_set(12)
is_set(13)
is_set(17)
is_set(28)

print(s.check())
print(hex(s.model()[x].as_long()))

When I run this, I get:

sat
0x10023678

Hope this gets you started!

alias
  • 28,120
  • 2
  • 23
  • 40
  • Thank you the task is to convert my student id number 10023678 in the same number in hex. So task is to produce an SMT formula in the bit vector logic (QF_BV) which has a single variable 'x' and as only solution the 32 bit-vector identical to my student number if printed in hexadecimal encoding. (The challenge is to use exactly the single variable 'x' and no constants (neither 'true', 'false', 'bit0', 'bit1', '#b...', '#x...', nor '(_ bv... )' etc. are allowed)) – Fredery_Drell Jan 24 '21 at 18:22
  • If you want the solution to be unique, then you'll have to add a `is_zero` function and call it on all the other positions to make sure they are `0`. But otherwise, looks like you've got a solution for this really funny exercise. – alias Jan 24 '21 at 18:30
  • Thank you, my exercise is already solved in smt2 in Z3. But, I was wondering if there would be a way where the program search for the bits positions itself. So you don't need to type "is_set( 3) is_set( 4)"...also also dont need to search for the bits... – Fredery_Drell Jan 25 '21 at 19:34
  • To do so, you'd need to find a way to characterize what would force `x` to be equal to `0x10023678`. A simple thing to do would be to assert that `x` is `0x10023678`, but that kind of beats the whole purpose of this (rather silly) exercise. You can express the number `0x10023678` with some mathematical expression (is it the root of an equation? Is it the multiplication of multiple numbers? Or anything else), and ask z3 to solve that. But the whole thing is rather pointless from a practical perspective. – alias Jan 25 '21 at 19:43
  • Yes you are right. Thank you very much for helping and brainstorming with me. – Fredery_Drell Jan 26 '21 at 10:09
  • Glad it helped you. In stack-overflow, you indicate an answer was helpful to you by upvoting it and/or accepting it. (See the little checkmark next to the answer itself.) – alias Jan 26 '21 at 15:45