Fast SHA-1 hash implementation in x86 assembly

After doing something for the first time, doing it again is much easier. The MD5 and SHA-1 hash algorithms are quite similar in structure, and having written MD5 in x86 assembly recently, applying that knowledge to implement SHA-1 in x86 was a breeze.

Source code

The code comes in a number of parts:

Files:

To use this code, compile it on Linux with one of these commands:

Then run the executable with ./sha1test.

Licensing: This code is copyrighted and is not open source. Please contact me if you wish to use or copy the code.

Benchmark results

An informal benchmark on Intel Core 2 Quad Q6600 2.40 GHz (using a single core), Ubuntu 10.04, GCC 4.4.3 gives these numbers:

In conclusion, my best x86 code is 23% faster than my best C code compiled with GCC.

x86-64 version

All the C files work correctly without modification on x86-64. I made minimal changes to the assembly code only to adapt to the calling convention and change the 32-bit constants to signed numbers. The usage instructions are exactly the same. Here are the files:

An informal benchmark on Intel Core 2 Quad Q6600 2.40 GHz (using a single core), Ubuntu 10.04, GCC 4.4.3 gives these numbers:

Compared to x86-32 mode, the C code performs better but the assembly code performs worse.

Notes

More info

Related



Feedback

Question? Comment? Contact me

ProjectNayuki: Like, comment, follow updates on Facebook