Z3 String/Char xor?

跟風遠走 提交于 2019-12-11 11:12:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!