secp256k1 icon indicating copy to clipboard operation
secp256k1 copied to clipboard

Unify context by creating an alloc / init two phase approach

Open rickmark opened this issue 3 years ago • 3 comments

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

rickmark avatar Mar 26 '21 07:03 rickmark

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.

real-or-random avatar Mar 26 '21 08:03 real-or-random

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?

sipa avatar May 01 '21 00:05 sipa

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);

elichai avatar May 02 '21 11:05 elichai