Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up by fast unpacking #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Speed up by fast unpacking #17

wants to merge 2 commits into from

Conversation

Ivoz
Copy link

@Ivoz Ivoz commented Oct 6, 2013

I 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

ed25519.py Outdated
a = 2 ** (b - 2) \
+ (reduce(lambda a, x: a << 64 | x,
struct.unpack('<QQQQ', h[:b // 8])[::-1], 0)
& (2**((b-2) - 3) - 1) << 3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think we should move the reduce(...) bit into it's own function, also I think it makes sense to unroll the shift-and-or ops in the reduce and just write them out, it's only 4 ops, and I think it'd be easier to read.

@Ivoz Ivoz mentioned this pull request Oct 6, 2013
3 tasks
Replace manual bit unpacking of integers with
C-speed unpacking using the struct module.

Conflicts:
	ed25519.py
Add a small comment noting generation of public key
@Ivoz
Copy link
Author

Ivoz commented Oct 6, 2013

rebased onto xrange commit

@dstufft
Copy link
Member

dstufft commented Oct 6, 2013

Strange, tests didn't seem to run on this?

@Ivoz
Copy link
Author

Ivoz commented Oct 6, 2013

@Ivoz
Copy link
Author

Ivoz commented Nov 6, 2013

Is there any wont to merge this?

Base automatically changed from master to main February 13, 2021 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants