-
Notifications
You must be signed in to change notification settings - Fork 14
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
crypto: speed up secp256k1/field.py #160
Conversation
# n[0] = 977 * 16 = 15632 | ||
# n[1] = 64 * 16 = 1024 | ||
# That is to say (2^26 * 1024) + 15632 = 68719492368 | ||
primePartBy16 = 68719492368 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having this as a named value is needed by Cython.
b = ByteArray(0, length=32) | ||
b = bytearray(32) | ||
self.putBytes(b) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not passing a ByteArray
allows optimizing the putBytes
method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
omg. Tests from 21.40s to 4.33s
Will try this on windows asap.
f = FieldVal.fromHex("0abc").add(1) | ||
f = FieldVal.fromHex("abc").add(1) | ||
so that: | ||
f = 0x0abc + 1 | ||
f = 0xabc + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to show that a missing hex zero is not a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an amazing improvement. I'm learning cython on the fly, and it's been a while since I've thought in C, so bear with me while I take a couple passes at this. A couple of questions for now.
-
Should we be checking in our .c files?
-
There are some official recommendations for distributing though PyPI. Does Poetry take care of all of this?
Thank you.
I don't think so. From the docs:
So the user is supposed to have a C compiler but somehow not Cython, that is listed in the dev dependencies? It doesn't seem likely.
Huh? Why should they not want to use it?
We have dependency check and resolution now, thanks to Poetry. This line of reasoning is outdated. Then they go on detailing lots of unneeded complexity. No thanks.
Yes, it does. Just run |
I tried to
If there is no compiler, it must fail gracefully and fall back to pure-Python. |
Agreed. This needs Poetry support though, so I created an issue in their project. |
Here's a build.py that works. https://github.com/buck54321/tinydecred/blob/teknico/decred/build.py |
That's swell! Thanks, putting it in. |
This speeds up most of
crypto/secp256k1/field.py
by ~100x (!) using Cython.The file
crypto/secp256k1/field.pxd
contains type definitions that are used by Cython to generatefield.c
, a 23K lines C extension that in turn gets compiled to a dynamic library in a platform-dependent format (.so
on Unix-like,.dll
on Windows).The dynamic library is placed in the same directory and is loaded automatically by the Python interpreter at execution time.
The command to generate the C extension and build the dynamic library in development is:
Poetry has undocumented support for building C extensions, so that the
poetry build
command builds the library and includes it into the created, platform-dependent, installable wheel archive.Cython has been added to the development dependencies of the
decred
library.Only tested on Linux, let me know if it works correctly on Windows and macOs.
Based on #158.