Skip to content

Commit

Permalink
fixup! Export functions for preallocated memory
Browse files Browse the repository at this point in the history
  • Loading branch information
real-or-random committed Oct 26, 2018
1 parent de5ee80 commit 864da3a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 75 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ else
JNI_LIB =
endif
include_HEADERS = include/secp256k1.h
include_HEADERS += include/secp256k1_prealloc.h
noinst_HEADERS =
noinst_HEADERS += src/scalar.h
noinst_HEADERS += src/scalar_4x64.h
Expand Down
75 changes: 0 additions & 75 deletions include/secp256k1.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,81 +225,6 @@ SECP256K1_API void secp256k1_context_destroy(
secp256k1_context* ctx
);

/** Determine the memory size of a secp256k1 context object to be created in
* caller-provided memory.
*
* The purpose of this function is to determine how much memory must be provided
* to secp256k1_context_prealloc_create.
*
* Returns: the required size of the caller-provided memory block
* In: flags: which parts of the context to initialize.
*/
SECP256K1_API size_t secp256k1_context_prealloc_size(
unsigned int flags
) SECP256K1_WARN_UNUSED_RESULT;

/** Create a secp256k1 context object in caller-provided memory.
*
* Returns: a newly created context object.
* In: prealloc: a pointer to a rewritable contiguous block of memory of
* size at least secp256k1_context_prealloc_size(flags)
* bytes, suitably aligned to hold an object of any type
* (cannot be NULL)
* flags: which parts of the context to initialize.
*
* See also secp256k1_context_randomize.
*/
SECP256K1_API secp256k1_context* secp256k1_context_prealloc_create(
void* prealloc,
unsigned int flags
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;

/** Determine the memory size of a secp256k1 context object to be copied into
* caller-provided memory.
*
* The purpose of this function is to determine how much memory must be provided
* to secp256k1_context_prealloc_clone when copying the context ctx.
*
* Returns: the required size of the caller-provided memory block.
* In: ctx: an existing context to copy (cannot be NULL)
*/
SECP256K1_API size_t secp256k1_context_prealloc_clone_size(
const secp256k1_context* ctx
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;

/** Copy a secp256k1 context object into caller-provided memory.
*
* Returns: a newly created context object.
* Args: ctx: an existing context to copy (cannot be NULL)
* In: prealloc: a pointer to a rewritable contiguous block of memory of
* size at least secp256k1_context_prealloc_size(flags)
* bytes, suitably aligned to hold an object of any type
* (cannot be NULL)
*/
SECP256K1_API secp256k1_context* secp256k1_context_prealloc_clone(
const secp256k1_context* ctx,
void* prealloc
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_WARN_UNUSED_RESULT;

/** Destroy a secp256k1 context object that has been created in
* caller-provided memory.
*
* The context pointer may not be used afterwards.
*
* The context to destroy must have been created using
* secp256k1_context_prealloc_create or secp256k1_context_prealloc_clone.
* If the context has instead been created using secp256k1_context_create or
* secp256k1_context_clone, the behaviour is undefined. In that case,
* secp256k1_context_destroy must be used instead.
*
* Args: ctx: an existing context to destroy, constructed using
* secp256k1_context_prealloc_create or
* secp256k1_context_prealloc_clone (cannot be NULL)
*/
SECP256K1_API void secp256k1_context_prealloc_destroy(
secp256k1_context* ctx
);

/** Set a callback function to be called when an illegal argument is passed to
* an API call. It will only trigger for violations that are mentioned
* explicitly in the header.
Expand Down
89 changes: 89 additions & 0 deletions include/secp256k1_prealloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#ifndef SECP256K1_PREALLOC_H
#define SECP256K1_PREALLOC_H

#include "secp256k1.h"

#ifdef __cplusplus
extern "C" {
#endif

/** Determine the memory size of a secp256k1 context object to be created in
* caller-provided memory.
*
* The purpose of this function is to determine how much memory must be provided
* to secp256k1_context_prealloc_create.
*
* Returns: the required size of the caller-provided memory block
* In: flags: which parts of the context to initialize.
*/
SECP256K1_API size_t secp256k1_context_prealloc_size(
unsigned int flags
) SECP256K1_WARN_UNUSED_RESULT;

/** Create a secp256k1 context object in caller-provided memory.
*
* Returns: a newly created context object.
* In: prealloc: a pointer to a rewritable contiguous block of memory of
* size at least secp256k1_context_prealloc_size(flags)
* bytes, suitably aligned to hold an object of any type
* (cannot be NULL)
* flags: which parts of the context to initialize.
*
* See also secp256k1_context_randomize.
*/
SECP256K1_API secp256k1_context* secp256k1_context_prealloc_create(
void* prealloc,
unsigned int flags
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;

/** Determine the memory size of a secp256k1 context object to be copied into
* caller-provided memory.
*
* The purpose of this function is to determine how much memory must be provided
* to secp256k1_context_prealloc_clone when copying the context ctx.
*
* Returns: the required size of the caller-provided memory block.
* In: ctx: an existing context to copy (cannot be NULL)
*/
SECP256K1_API size_t secp256k1_context_prealloc_clone_size(
const secp256k1_context* ctx
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;

/** Copy a secp256k1 context object into caller-provided memory.
*
* Returns: a newly created context object.
* Args: ctx: an existing context to copy (cannot be NULL)
* In: prealloc: a pointer to a rewritable contiguous block of memory of
* size at least secp256k1_context_prealloc_size(flags)
* bytes, suitably aligned to hold an object of any type
* (cannot be NULL)
*/
SECP256K1_API secp256k1_context* secp256k1_context_prealloc_clone(
const secp256k1_context* ctx,
void* prealloc
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_WARN_UNUSED_RESULT;

/** Destroy a secp256k1 context object that has been created in
* caller-provided memory.
*
* The context pointer may not be used afterwards.
*
* The context to destroy must have been created using
* secp256k1_context_prealloc_create or secp256k1_context_prealloc_clone.
* If the context has instead been created using secp256k1_context_create or
* secp256k1_context_clone, the behaviour is undefined. In that case,
* secp256k1_context_destroy must be used instead.
*
* Args: ctx: an existing context to destroy, constructed using
* secp256k1_context_prealloc_create or
* secp256k1_context_prealloc_clone (cannot be NULL)
*/
SECP256K1_API void secp256k1_context_prealloc_destroy(
secp256k1_context* ctx
);

#ifdef __cplusplus
}
#endif

#endif /* SECP256K1_PREALLOC_H */
1 change: 1 addition & 0 deletions src/secp256k1.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
**********************************************************************/

#include "include/secp256k1.h"
#include "include/secp256k1_prealloc.h"

#include "util.h"
#include "num_impl.h"
Expand Down
1 change: 1 addition & 0 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "secp256k1.c"
#include "include/secp256k1.h"
#include "include/secp256k1_prealloc.h"
#include "testrand_impl.h"

#ifdef ENABLE_OPENSSL_TESTS
Expand Down

0 comments on commit 864da3a

Please sign in to comment.