Is there an efficient way to find Hamming Distance between two BitVec() in Z3? That is, two equal length BitVectors that differ by a certain number of bits in their respective positions. I am trying to use some Z3-APIs from here.
This is what I have tried until now:
V_1, V_2 = BitVecs('V_1 V_2',bit_length) #bit_length varies from 1 to 9.
s.add(Sum([ZeroExt(int(ceil(log(bit_length)/log(2))+1), Extract(i,i,(V_1 ^ V_2))) for i in range(bit_length) ]) == 9)
Now, above constraint should give "SAT" only when BitVecs V_1 & V_2 differ at 9 bit-positions. However, it also gives SAT when V_1='0', V_2='1' & V_1='00', V_2='10'
I'm afraid I'm over-complicating my constraint. Is there a simple way to find HD between two BitVecs?
I am a beginner in the field of SAT-solving and SMT solvers. I am currently trying out Z3 to learn and would appreciate any help in this regard. Thanks in advance!
FYI - for the above code this is the output I get when I run in loop to check with 0 < bit_length < 10. V_1, V_2 values are represented in binary for readability:
Sat, bit_length = 1,
V_1 -> 0
V_2 -> 1
Sat, bit_length = 2,
V_1 -> 00
V_2 -> 10
NotSat, bit_length = 3,
NotSat, bit_length = 4,
NotSat, bit_length = 5,
NotSat, bit_length = 6,
NotSat, bit_length = 7,
NotSat, bit_length = 8,
Sat, bit_length = 9,
V_1 -> 111011101
V_2 -> 000100010
UPDATE:
After debugging my constraints using simplify(), I got it working with ZeroExt(int(ceil(log(bit_length)/log(2))+ <HD_value>), )
That is, the above s.add() changed to:
s.add(Sum([ZeroExt(int(ceil(log(bit_length)/log(2))+9), Extract(i,i,(V_1 ^ V_2))) for i in range(bit_length) ]) == 9)
Nonetheless, I would continue to explore if I can find a better method. Feel free to post your answers if you know a better approach. Thanks!