I'm running the following test in Z3/Python:
def test_converting_word_into_byte_array():
bytes_in_word = 4
word_size = 8 * bytes_in_word
word = BitVec('word', word_size)
word_in_bytes = Array('bytes(word)', BitVecSort(word_size), BitVecSort(8))
read = BitVec('read', word_size)
pointer = BitVecVal(0, word_size)
answer_array = Array('final(word)', BitVecSort(word_size), BitVecSort(8))
solver = Solver()
solver.add(word == BitVecVal(2, word_size))
for byte in range(bytes_in_word):
solver.add(Select(word_in_bytes, byte) == Extract(word_size - 1 - 8 * byte, word_size - 1 - 7 - 8 * byte, word))
new_array = Lambda([read],
If(
And(ULE(pointer, read), ULE(read, pointer + bytes_in_word - 1)),
Select(word_in_bytes, read - pointer),
Select(K(BitVecSort(word_size), BitVecVal(0, 8)), read)))
solver.add(answer_array == new_array)
assert str(solver.check()) == "sat"
print(solver.model())
While the solution is satisfactory, ultimately the solver model seems wrong:
[final(word) = Lambda(k!0, 2),
bytes(word) = Lambda(k!0, If(k!0 == 3, 2, 0)),
word = 2]
Question
Why is final(word)
taking on the value of 2
when it should be equivalent to bytes(word)
due to how the If
condition is set up?