I'm trying to port the following C loop to Python:
for (int32_t i = 32; --i >= 0;) {
v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
sum -= delta;
v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum >> 11 & 3]);
}
The problem is that the these integers are 32-bit unsigned and the code above relies on integer wrapping. My first attempt was to introduce the U32
class (see here), but after porting this code to Python 3, the solution got so ugly (see here) that I started to looking for other ones. My best idea at the moment is to wrap every expression with % 2 ** 32
, so that it wraps around. Here's how this looks:
for _ in range(32):
v0 = (
v0
+
(
(
(v1 << 4) % 2**32 ^ (v1 >> 5) % 2**32
) % 2**32
+
v1 ^ (sum_ + k[sum_ & 3]) % 2**32
) % 2**32
) % 2**32
sum_ = (sum_ - delta) % 2 ** 32
v1 = (
v1
+
(
(
(
(v0 << 4) % 2**32 ^ (v0 >> 5) % 2**32
) % 2**32 + v0
) % 2**32
^
(sum_ + k[(sum_ >> 11) % 2**32 & 3]) % 2**32
) % 2**32
) % 2**32
It looks strange and I'm not sure it's the best idea. What do you think of it? Is it something that needs improving? If so, how?