Wrapping every expression with % 2 ** 32 to get a C-like behavior
Firstly, the expensive modulo operation % 2**32 can be replaced by a cheap bitwise and: &amp; 2**32-1. Make sure to familiarize yourself with operator precedence, because it's not intuitive when it comes to bitwise operators (if you haven't done that yet).

As a next step, you could use the following properties to optimize the equations:


(a % m + b % m) % m = (a + b) % m
(a &amp; n ^ b &amp; n) &amp; n = (a ^ b) &amp; n


That allows you get rid of many of the &amp; 2**32-1 operations at the cost of bigger intermediate results.  If you want to make sure to get the highest speed, do some perfs. In any case, it will make the code much more readable.