32 bit left rotates where n > 1: a hack

Christopher Oliver oliver at fritz.co.traverse.com
Sat Jan 24 20:28:29 UTC 1998


   Disclaimer: this is the stuff of geek teen idols and video
   games; it would draw a barely stiffled yawn from even a com-
   puter science novice, so if you're expecting deep thoughts,
   read no further.

That being said, this hack might prove useful to someone, so
here goes:

  Many current cryptographic methods such as SQUARE, Blowfish,
RIPEMD-160, and HAVAL use word rotation as one of the central
operations.  The most obvious expression:

	(a bitShift: n) bitOr: (a BitShift: -n)

for some fixed n is quite slow.  In an early attempt to speed
up SQUARE, I wrote three methods to handle the rotations which
happened to be on eight bit boundaries.  These methods broke
the word into four bytes with digitAt: and used digitAt:put:
into a four byte Integer to assemble the properly rotated
value.  Since each rotate was used only once per pass, I did
not copy the Integer before munging.

I found in trying to hack together a quick version of RIPEMD-
160 that one can actually reduce the execution by about at
least a quarter and rotate by an arbitrary amount by adding to
the expression given first a bitAnd: so that the left shift
stays within four bytes.  Hence, my best effort looks like:

	((a bitAnd: m) bitShift: n) bitOr: (a BitShift: -n)

where n is a number greater than zero, and m is 2^(32 - n) - 1.
It seems cheapest when (a bitAnd: m) is a SmallInteger.  I
welcome any improvements to this technique.

At this time, I'm seeking the cheapest non-VM hack for addition
of from two to four 32 bit Integers.  Even with my shift hack,
hash code seems painfully slow.  I'd like to get a RIPEMD-160
compress to around 16ms per block on a 133MHz Pentium.

Happy hacking,

-- 
Christopher Oliver                     Traverse Communications
Systems Coordinator                    223 Grandview Pkwy, Suite 108
oliver at traverse.com                    Traverse City, Michigan, 49684
   (define magic (lambda (f) (lambda (x) x)))
   (define (more-magic n) (lambda (f) (lambda (x) (f ((n f) x)))))





More information about the Squeak-dev mailing list