Python vs. Perl, which is better to learn?
James J. Besemer
jb at cascade-sys.com
Fri May 10 03:49:18 EDT 2002
Andrew Dalke wrote:
> > I pondered for a while adding the necessary masking operations but
> > decided it was not sane.
I meant my first idea was to add the necessary masking ops to INTEGERS to
prevent overflow and I suspect you'll agree that would have been silly, even
though it might have worked. I certainly did not mean to cast aspersions on
Python.
Then I tried using longs but it wasn't working right off, so I gave up. It
was cheaper to write off the investment to date and switch to C, which free
off the web site.
> Here's the (completely untested and probably incorrect Python but
> close enough for the gist)
[Now with my bug-fixes]
>
> def encipher(v, k):
> low32bits = 2L**32-1
> y = v & low32bits
> z = (v>>32) & low32bits
> sum = 0L
> delta = 0x9E3779B9L
> a = k & low32bits
> b = (k>>32) & low32bits
> c = (k>>64) & low32bits
> d = (k>>96) & low32bits
>
> for i in range(32):
> sum += delta
> sum &= low32bits
> y += (z << 4)+a ^ z+sum ^ (z >> 5)+b
> y &= low32bits
> z += (y << 4)+c ^ y+sum ^ (y >> 5)+d
> z &= low32bits
>
> return ( z << 32 ) | y
>
> Seems reasonable enough to me, and not very insane.
You're right. Aside from a few bugs, I don't see why it wouldn't work. Your
variables z, b and d needed to be shifted so the data is in the lower 32
bits. Delta also needed to be long, as it affects the result of "sum+=".
Return result was undefined. Since + is done before ^ your translation of
"+=" materially affected the computation.
I tried it with those changes and it appears to work.
Regards
--jb
--
James J. Besemer 503-280-0838 voice
http://cascade-sys.com 503-280-0375 fax
mailto:jb at cascade-sys.com
More information about the Python-list
mailing list