@@ -33,9 +33,10 @@ extern "C" {
33
33
* verification).
34
34
*
35
35
* 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
37
37
* 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.
39
40
*
40
41
* Regarding randomization, either do it once at creation time (in which case
41
42
* 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)(
163
164
#define SECP256K1_FLAGS_BIT_CONTEXT_SIGN (1 << 9)
164
165
#define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8)
165
166
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. */
167
169
#define SECP256K1_CONTEXT_VERIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_VERIFY)
168
170
#define SECP256K1_CONTEXT_SIGN (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_SIGN)
169
171
#define SECP256K1_CONTEXT_NONE (SECP256K1_FLAGS_TYPE_CONTEXT)
@@ -186,7 +188,7 @@ typedef int (*secp256k1_nonce_function)(
186
188
*/
187
189
SECP256K1_API extern const secp256k1_context * secp256k1_context_no_precomp ;
188
190
189
- /** Create a secp256k1 context object.
191
+ /** Create a secp256k1 context object (in dynamically allocated memory) .
190
192
*
191
193
* This function uses malloc to allocate memory. It is guaranteed that malloc is
192
194
* called at most once for every call of this function.
@@ -200,7 +202,7 @@ SECP256K1_API secp256k1_context* secp256k1_context_create(
200
202
unsigned int flags
201
203
) SECP256K1_WARN_UNUSED_RESULT ;
202
204
203
- /** Copies a secp256k1 context object.
205
+ /** Copy a secp256k1 context object (into dynamically allocated memory) .
204
206
*
205
207
* This function uses malloc to allocate memory. It is guaranteed that malloc is
206
208
* called at most once for every call of this function.
@@ -212,14 +214,97 @@ SECP256K1_API secp256k1_context* secp256k1_context_clone(
212
214
const secp256k1_context * ctx
213
215
) SECP256K1_ARG_NONNULL (1 ) SECP256K1_WARN_UNUSED_RESULT ;
214
216
215
- /** Destroy a secp256k1 context object.
217
+ /** Destroy a secp256k1 context object (created in dynamically allocated memory) .
216
218
*
217
219
* 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
219
229
*/
220
230
SECP256K1_API void secp256k1_context_destroy (
221
231
secp256k1_context * ctx
222
232
);
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
+ );
223
308
224
309
/** Set a callback function to be called when an illegal argument is passed to
225
310
* 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(
642
727
* contexts not initialized for signing; then it will have no effect and return 1.
643
728
*
644
729
* 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.
646
732
*/
647
733
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize (
648
734
secp256k1_context * ctx ,
0 commit comments