2012-12-14 88 views
5

我正在瀏覽Z3py,並對如何在特定情況下使用API​​有疑問。下面的代碼是我最終希望使用Z3的簡化版本。我的一些問題是: 1.有沒有辦法像下面的變量'a'一樣創建任意長度的數組值? 2.您可以通過Z3py訪問循環中的數組中的每個元素嗎? 3.是否可以將結果分配回原始變量或是需要新變量?轉換爲CNF會自動添加一個唯一的ID嗎? (即a + = b)學習Z3py - 是否支持數組和循環

總體而言,我迷失在如何將Z3py API應用於如下解決方案依賴於'b'的算法。感謝您的幫助或提示。

import sys 
import struct 

a = "\xff\x33\x01\x88\x02\x11\x03\x55" 
b = sys.stdin.read(1) #a byte of user input - value 'a' is good 
c = '' 
d = '' 

for x in range(len(a)): 
    c += struct.pack('B', int(a[x].encode('hex'), 16)^int(b.encode('hex'), 16)) 

print c.encode('hex') 

second = '\x23\x23' 
x = 0 
while x < len(c): 
    d += struct.pack('h', int(c[x:x+1].encode('hex'), 16)^int(second.encode('hex'), 16)) 
    x += 2 

print d.encode('hex') 

if d == '\xbd\x23\x43\x23\x40\x23\x41\x23': 
    print "Good" 
else: 
    print "Bad" 
+0

我想檢查我是否理解這個問題。看來你想用Z3Py來解決這個問題:給定'a'和'd',找到'b',這樣你就可以打印出好的。是嗎? –

+0

是的,這就對了。 – daybreak

回答

6

我們可以通過編寫一個產生Z3表達式的Python程序來實現這一點。我們在這個程序中使用Python循環和列表(我們也可以使用數組),但是這些列表包含Z3「符號」表達式而不是Python值。結果列表d是包含b的Z3表達式的列表。然後,我們要求Z3找到一個b,使得d的元素與字符串'\xbd\x23\x43\x23\x40\x23\x41\x23'中的字符「相等」。這裏是代碼:

from z3 import * 

def str2array(a): 
    """ 
    Convert a string into a list of Z3 bit-vector values of size 8 
    """ 
    return [ BitVecVal(int(a[x].encode('hex'), 16), 8) for x in range(len(a)) ] 

a = str2array("\xff\x33\x01\x88\x02\x11\x03\x55") 
# 'a' is a list of Z3 bitvector constants. 
print "a:", a 
# The elements of 'a' may look like Python integers but they are Z3 expressions. 
# We can use the method sexpr() to get these values in SMT 2.0 syntax. 
print [ a_i.sexpr() for a_i in a ] 

# b is a Z3 symbolic variable. 
b = BitVec('b', 8) 

# We compute a list 'c' of Z3 expressions from 'a' and 'b'. 
# We use Python list comprehensions but we can also use a for-loop. 
c = [ a_i^b for a_i in a ] 

print "c:", c 

second = '\x23\x23' 
# We convert 'second' in a Z3 bit-vector value of size 16 
second = BitVecVal(int(second.encode('hex'), 16), 16) 
print "second:", second 

# The Z3 operation Concat concatenates two or more bit-vector expressions. 
d = [] 
x = 0 
while x < len(c): 
    # c[x] is a Bit-vector of size 8, second is a Bit-vector of size 16. 
    # In Z3, we have to do the "casting" ourselves. 
    # We use ZeroExt(8, c[x]) to convert c[x] into a Bit-vector of size 16, 
    # by adding 0-bits. 
    d.append(ZeroExt(8, c[x])^second) 
    x += 2 
print "d:", d 

goal = str2array('\xbd\x23\x43\x23\x40\x23\x41\x23') 
print "goal:", goal 
# Note that, len(goal) == 2*len(d) 
# We can use Concat to concatenate adjacent elements. 

# We want each element of d[i] == Concat(goal[2*i], goal[2*i+1]) 
# We can use Z3 to find the 'b' that will satisfy these constraints 

s = Solver() 
# 's' is a Z3 solver object 
i = 0 
while i < len(d): 
    s.add(d[i] == Concat(goal[2*i+1], goal[2*i])) 
    i += 1 
print s 
# Now, 's' contains a set of equational constraints. 
print s.check() 
print s.model() 
+0

感謝您的代碼並對其進行評論。它已經清除了很多我的問題。是否有理由不能看出「a」或97是解決約束條件的b值? – daybreak

+0

我更正了程序。在Z3編碼示例時,我犯了兩個錯誤。首先,在我使用的第一個編碼中,操作'c [x:x + 1]'實際上返回了'c'的一個字符。其次,當用十六進制編碼短整數時,首先放置最重要的位。例如,我將'9149'編碼爲'x23xBD'而不是'xBDx23'。進行這些更正後,Z3找到解97。 –