Unify context by creating an alloc / init two phase approach #913

issue rickmark opened this issue on March 26, 2021
  1. rickmark commented at 7:13 AM on March 26, 2021: none

    Instead of having two different systems for allocation, we should move to an alloc/init model. We should use a fixed size context (the largest possible size) since this is sharable. (maybe this is a compiler flag?)

    Use uninitialized static memory or call malloc yourself, this is the self managed model. You would do this for self managed: secp256k1_context* ctx = malloc(sizeof(secp256k1_context)); Then call secp256k1_context_initialize(ctx, flags);

    This is consistent with how every other operation handles memory. The caller provides it.

    An alternative would be to provide an allocator to the initialize function.

    A deprecated convenance function using malloc / free to provide back compatibility can be used as well

  2. real-or-random commented at 8:10 AM on March 26, 2021: contributor

    Instead of having two different systems for allocation, we should move to an alloc/init model.

    Am I right that this is essentially equivalent to dropping the normal context creation functions and keeping the prealloc functions (with nicer names)?

    We should use a fixed size context (the largest possible size) since this is sharable. (maybe this is a compiler flag?)

    I don't exactly understand what you're saying. Can you elaborate? A problem with a large context is that some users (embedded) won't like this.

  3. sipa commented at 12:17 AM on May 1, 2021: contributor

    The difficulty with what you propose here is that the size of the context is unknown, and vary wildly based on configuration (and probably, version). By several orders of magnitude really.

    But, we do already have a system for building contexts in user-provided space. It's a bit less convenient than your suggestion, but I believe it does everything you need.

    Is there a particular reason why that can't be used?

  4. elichai commented at 11:19 AM on May 2, 2021: contributor

    To expand on @sipa the current way would look like this:

    const int flags = SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN;
    void* ptr = malloc(secp256k1_context_preallocated_size(flags));
    if (ptr == NULL) {....}
    secp256k1_context* ctx = secp256k1_context_preallocated_create(ptr, flags);
    

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/secp256k1. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-14 15:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me