Skip to content

Instantly share code, notes, and snippets.

@TeWu
Created January 2, 2021 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TeWu/7a7c57d4f556455d2c1285b587b3f349 to your computer and use it in GitHub Desktop.
Save TeWu/7a7c57d4f556455d2c1285b587b3f349 to your computer and use it in GitHub Desktop.
Z3 incorrect ignore model in solver
from z3 import *
inp_length = 2
vec = ''
for i in range(0,inp_length):
vec += f"inp[{i}] " # inp[0] inp[1]
inp = BitVecs(vec, 8)
solver = Solver()
for i in range(0,2):
solver.add(inp[i] & 0b111111110 == 0) # Allow only first bit to be one
while solver.check() == z3.sat:
model = solver.model()
# solver.add([i != model[i] for i in inp]) # <-- Incorrect
solver.add(Or( # <-- Correct
[inp[i] != model[inp[i]] for i in range(0,inp_length)]
))
print(model)
## Output with correct line uncommented:
## [inp[1] = 0, inp[0] = 0]
## [inp[0] = 1, inp[1] = 1]
## [inp[0] = 1, inp[1] = 0]
## [inp[0] = 0, inp[1] = 1]
## Output with incorrect line uncommented:
## [inp[1] = 0, inp[0] = 0]
## [inp[0] = 1, inp[1] = 1]
@TeWu
Copy link
Author

TeWu commented Jan 2, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment