Skip to content

Commit a484e00

Browse files
committed
Merge bitcoin-core#566: Enable context creation in preallocated memory
0522caa Explain caller's obligations for preallocated memory (Tim Ruffing) 238305f Move _preallocated functions to separate header (Tim Ruffing) 695feb6 Export _preallocated functions (Tim Ruffing) 814cc78 Add tests for contexts in preallocated memory (Tim Ruffing) ba12dd0 Check arguments of _preallocated functions (Tim Ruffing) 5feadde Support cloning a context into preallocated memory (Tim Ruffing) c4fd5da Switch to a single malloc call (Tim Ruffing) ef020de Add size constants for preallocated memory (Tim Ruffing) 1bf7c05 Prepare for manual memory management in preallocated memory (Tim Ruffing) Pull request description: @apoelstra This builds on bitcoin-core#557. Manually managing memory is always a pain in the ass in some way. I tried to keep the pain manageable. I'm open to suggestions to make this less ugly or error-prone. to do: * tests * export functions ACKs for commit 0522ca: Tree-SHA512: 8ddb5b70219b6f095e780a9812d2387ab2a7f399803ce4101e27da504b479a61ebe08b6380568c7ba6f1e73d7d0b1f58a3c0a66fa0fdec7a64cd0740e156ce38
2 parents 36698dc + 0522caa commit a484e00

File tree

12 files changed

+428
-94
lines changed

12 files changed

+428
-94
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ else
88
JNI_LIB =
99
endif
1010
include_HEADERS = include/secp256k1.h
11+
include_HEADERS += include/secp256k1_preallocated.h
1112
noinst_HEADERS =
1213
noinst_HEADERS += src/scalar.h
1314
noinst_HEADERS += src/scalar_4x64.h

include/secp256k1.h

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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,11 @@ typedef int (*secp256k1_nonce_function)(
186188
*/
187189
SECP256K1_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).
192+
*
193+
* This function uses malloc to allocate memory. It is guaranteed that malloc is
194+
* called at most once for every call of this function. If you need to avoid dynamic
195+
* memory allocation entirely, see the functions in secp256k1_preallocated.h.
190196
*
191197
* Returns: a newly created context object.
192198
* In: flags: which parts of the context to initialize.
@@ -197,7 +203,11 @@ SECP256K1_API secp256k1_context* secp256k1_context_create(
197203
unsigned int flags
198204
) SECP256K1_WARN_UNUSED_RESULT;
199205

200-
/** Copies a secp256k1 context object.
206+
/** Copy a secp256k1 context object (into dynamically allocated memory).
207+
*
208+
* This function uses malloc to allocate memory. It is guaranteed that malloc is
209+
* called at most once for every call of this function. If you need to avoid dynamic
210+
* memory allocation entirely, see the functions in secp256k1_preallocated.h.
201211
*
202212
* Returns: a newly created context object.
203213
* Args: ctx: an existing context to copy (cannot be NULL)
@@ -206,10 +216,18 @@ SECP256K1_API secp256k1_context* secp256k1_context_clone(
206216
const secp256k1_context* ctx
207217
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
208218

209-
/** Destroy a secp256k1 context object.
219+
/** Destroy a secp256k1 context object (created in dynamically allocated memory).
210220
*
211221
* The context pointer may not be used afterwards.
212-
* Args: ctx: an existing context to destroy (cannot be NULL)
222+
*
223+
* The context to destroy must have been created using secp256k1_context_create
224+
* or secp256k1_context_clone. If the context has instead been created using
225+
* secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone, the
226+
* behaviour is undefined. In that case, secp256k1_context_preallocated_destroy must
227+
* be used instead.
228+
*
229+
* Args: ctx: an existing context to destroy, constructed using
230+
* secp256k1_context_create or secp256k1_context_clone
213231
*/
214232
SECP256K1_API void secp256k1_context_destroy(
215233
secp256k1_context* ctx
@@ -636,7 +654,8 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
636654
* contexts not initialized for signing; then it will have no effect and return 1.
637655
*
638656
* You should call this after secp256k1_context_create or
639-
* secp256k1_context_clone, and may call this repeatedly afterwards.
657+
* secp256k1_context_clone (and secp256k1_context_preallocated_create or
658+
* secp256k1_context_clone, resp.), and you may call this repeatedly afterwards.
640659
*/
641660
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(
642661
secp256k1_context* ctx,

include/secp256k1_preallocated.h

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#ifndef SECP256K1_PREALLOCATED_H
2+
#define SECP256K1_PREALLOCATED_H
3+
4+
#include "secp256k1.h"
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
/* The module provided by this header file is intended for settings in which it
11+
* is not possible or desirable to rely on dynamic memory allocation. It provides
12+
* functions for creating, cloning, and destroying secp256k1 context objects in a
13+
* contiguous fixed-size block of memory provided by the caller.
14+
*
15+
* Context objects created by functions in this module can be used like contexts
16+
* objects created by functions in secp256k1.h, i.e., they can be passed to any
17+
* API function that excepts a context object (see secp256k1.h for details). The
18+
* only exception is that context objects created by functions in this module
19+
* must be destroyed using secp256k1_context_preallocated_destroy (in this
20+
* module) instead of secp256k1_context_destroy (in secp256k1.h).
21+
*
22+
* It is guaranteed that functions in by this module will not call malloc or its
23+
* friends realloc, calloc, and free.
24+
*/
25+
26+
/** Determine the memory size of a secp256k1 context object to be created in
27+
* caller-provided memory.
28+
*
29+
* The purpose of this function is to determine how much memory must be provided
30+
* to secp256k1_context_preallocated_create.
31+
*
32+
* Returns: the required size of the caller-provided memory block
33+
* In: flags: which parts of the context to initialize.
34+
*/
35+
SECP256K1_API size_t secp256k1_context_preallocated_size(
36+
unsigned int flags
37+
) SECP256K1_WARN_UNUSED_RESULT;
38+
39+
/** Create a secp256k1 context object in caller-provided memory.
40+
*
41+
* The caller must provide a pointer to a rewritable contiguous block of memory
42+
* of size at least secp256k1_context_preallocated_size(flags) bytes, suitably
43+
* aligned to hold an object of any type.
44+
*
45+
* The block of memory is exclusively owned by the created context object during
46+
* the lifetime of this context object, which begins with the call to this
47+
* function and ends when a call to secp256k1_context_preallocated_destroy
48+
* (which destroys the context object again) returns. During the lifetime of the
49+
* context object, the caller is obligated not to access this block of memory,
50+
* i.e., the caller may not read or write the memory, e.g., by copying the memory
51+
* contents to a different location or trying to create a second context object
52+
* in the memory. In simpler words, the prealloc pointer (or any pointer derived
53+
* from it) should not be used during the lifetime of the context object.
54+
*
55+
* Returns: a newly created context object.
56+
* In: prealloc: a pointer to a rewritable contiguous block of memory of
57+
* size at least secp256k1_context_preallocated_size(flags)
58+
* bytes, as detailed above (cannot be NULL)
59+
* flags: which parts of the context to initialize.
60+
*
61+
* See also secp256k1_context_randomize (in secp256k1.h)
62+
* and secp256k1_context_preallocated_destroy.
63+
*/
64+
SECP256K1_API secp256k1_context* secp256k1_context_preallocated_create(
65+
void* prealloc,
66+
unsigned int flags
67+
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
68+
69+
/** Determine the memory size of a secp256k1 context object to be copied into
70+
* caller-provided memory.
71+
*
72+
* Returns: the required size of the caller-provided memory block.
73+
* In: ctx: an existing context to copy (cannot be NULL)
74+
*/
75+
SECP256K1_API size_t secp256k1_context_preallocated_clone_size(
76+
const secp256k1_context* ctx
77+
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
78+
79+
/** Copy a secp256k1 context object into caller-provided memory.
80+
*
81+
* The caller must provide a pointer to a rewritable contiguous block of memory
82+
* of size at least secp256k1_context_preallocated_size(flags) bytes, suitably
83+
* aligned to hold an object of any type.
84+
*
85+
* The block of memory is exclusively owned by the created context object during
86+
* the lifetime of this context object, see the description of
87+
* secp256k1_context_preallocated_create for details.
88+
*
89+
* Returns: a newly created context object.
90+
* Args: ctx: an existing context to copy (cannot be NULL)
91+
* In: prealloc: a pointer to a rewritable contiguous block of memory of
92+
* size at least secp256k1_context_preallocated_size(flags)
93+
* bytes, as detailed above (cannot be NULL)
94+
*/
95+
SECP256K1_API secp256k1_context* secp256k1_context_preallocated_clone(
96+
const secp256k1_context* ctx,
97+
void* prealloc
98+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_WARN_UNUSED_RESULT;
99+
100+
/** Destroy a secp256k1 context object that has been created in
101+
* caller-provided memory.
102+
*
103+
* The context pointer may not be used afterwards.
104+
*
105+
* The context to destroy must have been created using
106+
* secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone.
107+
* If the context has instead been created using secp256k1_context_create or
108+
* secp256k1_context_clone, the behaviour is undefined. In that case,
109+
* secp256k1_context_destroy must be used instead.
110+
*
111+
* If required, it is the responsibility of the caller to deallocate the block
112+
* of memory properly after this function returns, e.g., by calling free on the
113+
* preallocated pointer given to secp256k1_context_preallocated_create or
114+
* secp256k1_context_preallocated_clone.
115+
*
116+
* Args: ctx: an existing context to destroy, constructed using
117+
* secp256k1_context_preallocated_create or
118+
* secp256k1_context_preallocated_clone (cannot be NULL)
119+
*/
120+
SECP256K1_API void secp256k1_context_preallocated_destroy(
121+
secp256k1_context* ctx
122+
);
123+
124+
#ifdef __cplusplus
125+
}
126+
#endif
127+
128+
#endif /* SECP256K1_PREALLOCATED_H */

src/ecmult.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ typedef struct {
2020
#endif
2121
} secp256k1_ecmult_context;
2222

23+
static const size_t SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;
2324
static void secp256k1_ecmult_context_init(secp256k1_ecmult_context *ctx);
24-
static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, const secp256k1_callback *cb);
25-
static void secp256k1_ecmult_context_clone(secp256k1_ecmult_context *dst,
26-
const secp256k1_ecmult_context *src, const secp256k1_callback *cb);
25+
static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, void **prealloc);
26+
static void secp256k1_ecmult_context_finalize_memcpy(secp256k1_ecmult_context *dst, const secp256k1_ecmult_context *src);
2727
static void secp256k1_ecmult_context_clear(secp256k1_ecmult_context *ctx);
2828
static int secp256k1_ecmult_context_is_built(const secp256k1_ecmult_context *ctx);
2929

src/ecmult_gen.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ typedef struct {
2828
secp256k1_gej initial;
2929
} secp256k1_ecmult_gen_context;
3030

31+
static const size_t SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
3132
static void secp256k1_ecmult_gen_context_init(secp256k1_ecmult_gen_context* ctx);
32-
static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context* ctx, const secp256k1_callback* cb);
33-
static void secp256k1_ecmult_gen_context_clone(secp256k1_ecmult_gen_context *dst,
34-
const secp256k1_ecmult_gen_context* src, const secp256k1_callback* cb);
33+
static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context* ctx, void **prealloc);
34+
static void secp256k1_ecmult_gen_context_finalize_memcpy(secp256k1_ecmult_gen_context *dst, const secp256k1_ecmult_gen_context* src);
3535
static void secp256k1_ecmult_gen_context_clear(secp256k1_ecmult_gen_context* ctx);
3636
static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context* ctx);
3737

src/ecmult_gen_impl.h

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,40 @@
77
#ifndef SECP256K1_ECMULT_GEN_IMPL_H
88
#define SECP256K1_ECMULT_GEN_IMPL_H
99

10+
#include "util.h"
1011
#include "scalar.h"
1112
#include "group.h"
1213
#include "ecmult_gen.h"
1314
#include "hash_impl.h"
1415
#ifdef USE_ECMULT_STATIC_PRECOMPUTATION
1516
#include "ecmult_static_context.h"
1617
#endif
18+
19+
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
20+
static const size_t SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE = ROUND_TO_ALIGN(sizeof(*((secp256k1_ecmult_gen_context*) NULL)->prec));
21+
#else
22+
static const size_t SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE = 0;
23+
#endif
24+
1725
static void secp256k1_ecmult_gen_context_init(secp256k1_ecmult_gen_context *ctx) {
1826
ctx->prec = NULL;
1927
}
2028

21-
static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx, const secp256k1_callback* cb) {
29+
static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx, void **prealloc) {
2230
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
2331
secp256k1_ge prec[1024];
2432
secp256k1_gej gj;
2533
secp256k1_gej nums_gej;
2634
int i, j;
35+
size_t const prealloc_size = SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
36+
void* const base = *prealloc;
2737
#endif
2838

2939
if (ctx->prec != NULL) {
3040
return;
3141
}
3242
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
33-
ctx->prec = (secp256k1_ge_storage (*)[64][16])checked_malloc(cb, sizeof(*ctx->prec));
43+
ctx->prec = (secp256k1_ge_storage (*)[64][16])manual_alloc(prealloc, prealloc_size, base, prealloc_size);
3444

3545
/* get the generator */
3646
secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
@@ -85,7 +95,7 @@ static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx
8595
}
8696
}
8797
#else
88-
(void)cb;
98+
(void)prealloc;
8999
ctx->prec = (secp256k1_ge_storage (*)[64][16])secp256k1_ecmult_static_context;
90100
#endif
91101
secp256k1_ecmult_gen_blind(ctx, NULL);
@@ -95,27 +105,18 @@ static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_cont
95105
return ctx->prec != NULL;
96106
}
97107

98-
static void secp256k1_ecmult_gen_context_clone(secp256k1_ecmult_gen_context *dst,
99-
const secp256k1_ecmult_gen_context *src, const secp256k1_callback* cb) {
100-
if (src->prec == NULL) {
101-
dst->prec = NULL;
102-
} else {
108+
static void secp256k1_ecmult_gen_context_finalize_memcpy(secp256k1_ecmult_gen_context *dst, const secp256k1_ecmult_gen_context *src) {
103109
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
104-
dst->prec = (secp256k1_ge_storage (*)[64][16])checked_malloc(cb, sizeof(*dst->prec));
105-
memcpy(dst->prec, src->prec, sizeof(*dst->prec));
110+
if (src->prec != NULL) {
111+
/* We cast to void* first to suppress a -Wcast-align warning. */
112+
dst->prec = (secp256k1_ge_storage (*)[64][16])(void*)((unsigned char*)dst + ((unsigned char*)src->prec - (unsigned char*)src));
113+
}
106114
#else
107-
(void)cb;
108-
dst->prec = src->prec;
115+
(void)dst, (void)src;
109116
#endif
110-
dst->initial = src->initial;
111-
dst->blind = src->blind;
112-
}
113117
}
114118

115119
static void secp256k1_ecmult_gen_context_clear(secp256k1_ecmult_gen_context *ctx) {
116-
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
117-
free(ctx->prec);
118-
#endif
119120
secp256k1_scalar_clear(&ctx->blind);
120121
secp256k1_gej_clear(&ctx->initial);
121122
ctx->prec = NULL;

0 commit comments

Comments
 (0)