问题
I'm working with Z3 in Python and am trying to figure out how to do String operations. In general, I've played around with z3.String
as the object, doing things like str1 + str2 == 'hello world'
. However, I have been unable to accomplish the following behavior:
solver.add(str1[1] ^ str1[2] == 12) # -- or --
solver.add(str1[1] ^ str1[2] == str2[1])
So basically add the constraint that character 1 xor character 2 equals 12. My understanding is that the string is defined as a sequence of 8-bit BitVectors under the hood, and BitVectors should be able to be xor'd.
Thanks!
回答1:
So far I don't expose ways to access characters with a function. You would have to define auxiliary functions and axioms that capture extraction. The operator [] extracts a sub-sequence, which is of length 1 if the index is within bounds.
Here is a way to access the elements:
from z3 import *
nth = Function('nth', StringSort(), IntSort(), BitVecSort(8))
k = Int('k')
str1, str2, s = Strings('str1 str2 s')
s = Solver()
s.add(ForAll([str1, k], Implies(And(0 <= k, k < Length(str1)), Unit(nth(str1, k)) == str1[k])))
s.add( ((nth(str1, 1)) ^ (nth(str2, 2))) == 12)
来源:https://stackoverflow.com/questions/35991948/z3-string-char-xor