pyca / ed25519

Optimized version of the reference implementation of Ed25519
Creative Commons Zero v1.0 Universal
34 stars 22 forks source link

Manual bit unpacking is slow #16

Closed Ivoz closed 11 years ago

Ivoz commented 11 years ago

I have added a simple profiling script to show call profiles of the main functions.

This allowed me to see that the manual bit unpacking performed by the likes of

return sum(2 ** i * bit(h, i) for i in range(2 * b))

Was causing a major slowdown, I thought it seemed to be 1/5th of the time for a signature validation.

With the help of a cool dude called ssbr in #python, I've managed to replace generators like the one above with C-speed unpacking using the struct module.

This resulted in 30% faster in signature validation and 50% faster signature generation.

There are two functions which still use by-the-bit packing, encodeint and encodepoint; only encodepoint is still contributing a small noticeable amount of time to runtime. It's now otherwise purely taken up by the edwards/scalarmult functions. You still might get a noticeable speed increase by optimizing these two encoding functions.

Lastly, bit twiddling is hard.

pyca

(ed25519)ivo@ivosung ed25519$ python science.py 
Time generate
0.168344974518

Time create signature
0.423187017441

Time verify signature
0.906836032867

Ivoz

(ed25519)ivo@ivosung ed25519$ python science.py 
Time generate
0.118165969849

Time create signature
0.175889015198

Time verify signature
0.677781820297
alex commented 11 years ago

Can you split the unrelated changes like range -> xrange into a seperate patch so there's less here to review?