@@ -33,9 +33,10 @@ extern "C" {
3333 * verification).
3434 *
3535 * A constructed context can safely be used from multiple threads
36- * simultaneously, but API call that take a non-const pointer to a context
36+ * simultaneously, but API calls that take a non-const pointer to a context
3737 * need exclusive access to it. In particular this is the case for
38- * secp256k1_context_destroy and secp256k1_context_randomize.
38+ * secp256k1_context_destroy, secp256k1_context_preallocated_destroy,
39+ * and secp256k1_context_randomize.
3940 *
4041 * Regarding randomization, either do it once at creation time (in which case
4142 * you do not need any locking for the other calls), or use a read-write lock.
@@ -163,7 +164,8 @@ typedef int (*secp256k1_nonce_function)(
163164#define SECP256K1_FLAGS_BIT_CONTEXT_SIGN (1 << 9)
164165#define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8)
165166
166- /** Flags to pass to secp256k1_context_create. */
167+ /** Flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and
168+ * secp256k1_context_preallocated_create. */
167169#define SECP256K1_CONTEXT_VERIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_VERIFY)
168170#define SECP256K1_CONTEXT_SIGN (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_SIGN)
169171#define SECP256K1_CONTEXT_NONE (SECP256K1_FLAGS_TYPE_CONTEXT)
@@ -186,7 +188,7 @@ typedef int (*secp256k1_nonce_function)(
186188 */
187189SECP256K1_API extern const secp256k1_context * secp256k1_context_no_precomp ;
188190
189- /** Create a secp256k1 context object.
191+ /** Create a secp256k1 context object (in dynamically allocated memory) .
190192 *
191193 * This function uses malloc to allocate memory. It is guaranteed that malloc is
192194 * called at most once for every call of this function.
@@ -200,7 +202,7 @@ SECP256K1_API secp256k1_context* secp256k1_context_create(
200202 unsigned int flags
201203) SECP256K1_WARN_UNUSED_RESULT ;
202204
203- /** Copies a secp256k1 context object.
205+ /** Copy a secp256k1 context object (into dynamically allocated memory) .
204206 *
205207 * This function uses malloc to allocate memory. It is guaranteed that malloc is
206208 * called at most once for every call of this function.
@@ -212,14 +214,97 @@ SECP256K1_API secp256k1_context* secp256k1_context_clone(
212214 const secp256k1_context * ctx
213215) SECP256K1_ARG_NONNULL (1 ) SECP256K1_WARN_UNUSED_RESULT ;
214216
215- /** Destroy a secp256k1 context object.
217+ /** Destroy a secp256k1 context object (created in dynamically allocated memory) .
216218 *
217219 * The context pointer may not be used afterwards.
218- * Args: ctx: an existing context to destroy (cannot be NULL)
220+ *
221+ * The context to destroy must have been created using secp256k1_context_create
222+ * or secp256k1_context_clone. If the context has instead been created using
223+ * secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone, the
224+ * behaviour is undefined. In that case, secp256k1_context_preallocated_destroy must
225+ * be used instead.
226+ *
227+ * Args: ctx: an existing context to destroy, constructed using
228+ * secp256k1_context_create or secp256k1_context_clone
219229 */
220230SECP256K1_API void secp256k1_context_destroy (
221231 secp256k1_context * ctx
222232);
233+ /** Determine the memory size of a secp256k1 context object to be created in
234+ * caller-provided memory.
235+ *
236+ * The purpose of this function is to determine how much memory must be provided
237+ * to secp256k1_context_preallocated_create.
238+ *
239+ * Returns: the required size of the caller-provided memory block
240+ * In: flags: which parts of the context to initialize.
241+ */
242+
243+ SECP256K1_API size_t secp256k1_context_preallocated_size (
244+ unsigned int flags
245+ ) SECP256K1_WARN_UNUSED_RESULT ;
246+
247+ /** Create a secp256k1 context object in caller-provided memory.
248+ *
249+ * Returns: a newly created context object.
250+ * In: prealloc: a pointer to a rewritable contiguous block of memory of
251+ * size at least secp256k1_context_preallocated_size(flags)
252+ * bytes, suitably aligned to hold an object of any type
253+ * (cannot be NULL)
254+ * flags: which parts of the context to initialize.
255+ *
256+ * See also secp256k1_context_randomize.
257+ */
258+ SECP256K1_API secp256k1_context * secp256k1_context_preallocated_create (
259+ void * prealloc ,
260+ unsigned int flags
261+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_WARN_UNUSED_RESULT ;
262+
263+ /** Determine the memory size of a secp256k1 context object to be copied into
264+ * caller-provided memory.
265+ *
266+ * The purpose of this function is to determine how much memory must be provided
267+ * to secp256k1_context_preallocated_clone when copying the context ctx.
268+ *
269+ * Returns: the required size of the caller-provided memory block.
270+ * In: ctx: an existing context to copy (cannot be NULL)
271+ */
272+ SECP256K1_API size_t secp256k1_context_preallocated_clone_size (
273+ const secp256k1_context * ctx
274+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_WARN_UNUSED_RESULT ;
275+
276+ /** Copy a secp256k1 context object into caller-provided memory.
277+ *
278+ * Returns: a newly created context object.
279+ * Args: ctx: an existing context to copy (cannot be NULL)
280+ * In: prealloc: a pointer to a rewritable contiguous block of memory of
281+ * size at least secp256k1_context_preallocated_size(flags)
282+ * bytes, suitably aligned to hold an object of any type
283+ * (cannot be NULL)
284+ */
285+ SECP256K1_API secp256k1_context * secp256k1_context_preallocated_clone (
286+ const secp256k1_context * ctx ,
287+ void * prealloc
288+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_WARN_UNUSED_RESULT ;
289+
290+ /** Destroy a secp256k1 context object that has been created in
291+ * caller-provided memory.
292+ *
293+ * The context pointer may not be used afterwards.
294+ *
295+ * The context to destroy must have been created using
296+ * secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone.
297+ * If the context has instead been created using secp256k1_context_create or
298+ * secp256k1_context_clone, the behaviour is undefined. In that case,
299+ * secp256k1_context_destroy must be used instead.
300+ *
301+ * Args: ctx: an existing context to destroy, constructed using
302+ * secp256k1_context_preallocated_create or
303+ * secp256k1_context_preallocated_clone (cannot be NULL)
304+ */
305+ SECP256K1_API void secp256k1_context_preallocated_destroy (
306+ secp256k1_context * ctx
307+ );
223308
224309/** Set a callback function to be called when an illegal argument is passed to
225310 * an API call. It will only trigger for violations that are mentioned
@@ -642,7 +727,8 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
642727 * contexts not initialized for signing; then it will have no effect and return 1.
643728 *
644729 * You should call this after secp256k1_context_create or
645- * secp256k1_context_clone, and may call this repeatedly afterwards.
730+ * secp256k1_context_clone (and secp256k1_context_preallocated_create or
731+ * secp256k1_context_clone, resp.), and you may call this repeatedly afterwards.
646732 */
647733SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize (
648734 secp256k1_context * ctx ,
0 commit comments