Blowfish in Python?

Tim Peters tim_one at
Sun Feb 27 05:06:04 EST 2000

[Markus Stenberg]
> ...
>  speed was horrendous. >
> I think the main reason was the fact that I had to use _long ints_ for
> calculations, as the normal ints are signed, and apparently the bitwise
> operators do not work as advertised when bit32 is set (=number is
> negative). Or that's my theory anyway - my implementation worked with
> long's and did not work with ints.

Try to whittle this down.  Negative ints are nothing special to the bitwise
ops, so if they "don't work as advertised" it would be a (very surprising!)
bug in the implementation.  For example, here's the implementation of int &:

static PyObject *
int_and(v, w)
	PyIntObject *v;
	PyIntObject *w;
	register long a, b;
	a = v->ob_ival;
	b = w->ob_ival;
	return PyInt_FromLong(a & b);

They're all like that -- they can't screw up unless C screws up.

> ...
> C implementations [of Blowfish] routinely go to megabytes/second level;

And are routinely highly optimized too.  Once you get "&" to work <wink>,
one machine instruction in C is going to turn into a couple of function
calls in Python.

> my Python-with-longs implementation was kilobytes/second, to be precise,
> 2-10k/s depending on the machine.

Using ints will get you the biggest bang for the buck at this point, but
it's never going to be *fast* in pure Python 1.5.2.

not-implying-it-will-be-faster-in-later-versions-either<wink>-ly y'rs  - tim

More information about the Python-list mailing list