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

Optimize usage of secp256k1 #5418

Open
solardiz opened this issue Jan 6, 2024 · 1 comment
Open

Optimize usage of secp256k1 #5418

solardiz opened this issue Jan 6, 2024 · 1 comment

Comments

@solardiz
Copy link
Member

solardiz commented Jan 6, 2024

A comment in secp256k1/secp256k1.h says:

/** Opaque data structure that holds context information (precomputed tables etc.).
 *
 *  The purpose of context structures is to cache large precomputed data tables
 *  that are expensive to construct, and also to maintain the randomization data
 *  for blinding.
 *
 *  Do not create a new context object for each operation, as construction is
 *  far slower than all other API calls (~100 times slower than an ECDSA
 *  verification).
 *
 *  A constructed context can safely be used from multiple threads
 *  simultaneously, but API call that take a non-const pointer to a context
 *  need exclusive access to it. In particular this is the case for
 *  secp256k1_context_destroy and secp256k1_context_randomize.
 *
 *  Regarding randomization, either do it once at creation time (in which case
 *  you do not need any locking for the other calls), or use a read-write lock.
 */
typedef struct secp256k1_context_struct secp256k1_context;

In bitshares_fmt_plug.c, electrum_fmt_plug.c, opencl_electrum_modern_fmt_plug.c, we currently recreate the context for each use. We should optimize these formats to use either one shared context structure, or per-thread ones, but not recreated/destroyed each time.

@solardiz
Copy link
Member Author

solardiz commented Jan 6, 2024

OTOH, in all of those places we use SECP256K1_CONTEXT_NONE, which may skip much of the initialization, and maybe our uses are such that they don't require nor trigger further initialization. If so, the performance impact may not be that bad, but following the comment's advice is probably desirable anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant