Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Z3py - do a roll

Tags:

python

z3

smt

Is there a recommended way of doing a bitwise roll to either left or right by any amount?

For example with a byte - 0x57 rolr 3 = 0xEA.

I have not found any "roll" operation in the Z3py docs. I was thinking about using a BitVecs for each bit but that doesn't seem efficient/probably won't work. Any advice is appreciated, thanks.

Edit: Thanks for the answers so far. This is more of an API question because I suck at it right now. Heres what I have as a starting point.

def roll(bt):
count = BitVecVal(int('0x03', 16), 8)
s.add(bt == (bt << count | bt >> (8 - count)) & 0xFF)

a = BitVec('a', 8)
s = Solver()
roll(a)
s.add(a == BitVecVal(int('0xEA', 16), 8))
s.check()

This prints out nothing and model is not available.

like image 632
daybreak Avatar asked Mar 11 '26 23:03

daybreak


1 Answers

You can do a rotate like this:

size = 0x100  # size of the bitvector

rotated = (x << n) | (x >> (size - n)) & (size - 1)
like image 187
Eric Avatar answered Mar 13 '26 11:03

Eric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!