Skip to content

Commit 2843768

Browse files
Switch to a single malloc call
1 parent e979d09 commit 2843768

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

src/ecmult.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ typedef struct {
2222

2323
static size_t secp256k1_ecmult_context_prealloc_size(void);
2424
static void secp256k1_ecmult_context_init(secp256k1_ecmult_context *ctx);
25-
static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, const secp256k1_callback *cb);
25+
static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, void **prealloc);
2626
static void secp256k1_ecmult_context_clone(secp256k1_ecmult_context *dst,
2727
const secp256k1_ecmult_context *src, const secp256k1_callback *cb);
2828
static void secp256k1_ecmult_context_clear(secp256k1_ecmult_context *ctx);

src/ecmult_gen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ typedef struct {
3030

3131
static size_t secp256k1_ecmult_gen_context_prealloc_size(void);
3232
static void secp256k1_ecmult_gen_context_init(secp256k1_ecmult_gen_context* ctx);
33-
static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context* ctx, const secp256k1_callback* cb);
33+
static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context* ctx, void **prealloc);
3434
static void secp256k1_ecmult_gen_context_clone(secp256k1_ecmult_gen_context *dst,
3535
const secp256k1_ecmult_gen_context* src, const secp256k1_callback* cb);
3636
static void secp256k1_ecmult_gen_context_clear(secp256k1_ecmult_gen_context* ctx);

src/ecmult_gen_impl.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,20 @@ static void secp256k1_ecmult_gen_context_init(secp256k1_ecmult_gen_context *ctx)
2828
ctx->prec = NULL;
2929
}
3030

31-
static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx, const secp256k1_callback* cb) {
31+
static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx, void **prealloc) {
3232
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
3333
secp256k1_ge prec[1024];
3434
secp256k1_gej gj;
3535
secp256k1_gej nums_gej;
3636
int i, j;
37+
size_t ctx_size = secp256k1_ecmult_gen_context_prealloc_size();
3738
#endif
3839

3940
if (ctx->prec != NULL) {
4041
return;
4142
}
4243
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
43-
ctx->prec = (secp256k1_ge_storage (*)[64][16])checked_malloc(cb, sizeof(*ctx->prec));
44+
ctx->prec = (secp256k1_ge_storage (*)[64][16])manual_alloc(prealloc, ctx_size, *prealloc, ctx_size);
4445

4546
/* get the generator */
4647
secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
@@ -95,7 +96,7 @@ static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx
9596
}
9697
}
9798
#else
98-
(void)cb;
99+
(void)prealloc;
99100
ctx->prec = (secp256k1_ge_storage (*)[64][16])secp256k1_ecmult_static_context;
100101
#endif
101102
secp256k1_ecmult_gen_blind(ctx, NULL);
@@ -123,9 +124,6 @@ static void secp256k1_ecmult_gen_context_clone(secp256k1_ecmult_gen_context *dst
123124
}
124125

125126
static void secp256k1_ecmult_gen_context_clear(secp256k1_ecmult_gen_context *ctx) {
126-
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
127-
free(ctx->prec);
128-
#endif
129127
secp256k1_scalar_clear(&ctx->blind);
130128
secp256k1_gej_clear(&ctx->initial);
131129
ctx->prec = NULL;

src/ecmult_impl.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,10 @@ static void secp256k1_ecmult_context_init(secp256k1_ecmult_context *ctx) {
269269
#endif
270270
}
271271

272-
static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, const secp256k1_callback *cb) {
272+
static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, void **prealloc) {
273273
secp256k1_gej gj;
274+
void* const base = *prealloc;
275+
size_t const prealloc_size = secp256k1_ecmult_context_prealloc_size();
274276

275277
if (ctx->pre_g != NULL) {
276278
return;
@@ -279,7 +281,7 @@ static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, const
279281
/* get the generator */
280282
secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
281283

282-
ctx->pre_g = (secp256k1_ge_storage (*)[])checked_malloc(cb, sizeof((*ctx->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G));
284+
ctx->pre_g = (secp256k1_ge_storage (*)[])manual_alloc(prealloc, sizeof((*ctx->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G), base, prealloc_size);
283285

284286
/* precompute the tables with odd multiples */
285287
secp256k1_ecmult_odd_multiples_table_storage_var(ECMULT_TABLE_SIZE(WINDOW_G), *ctx->pre_g, &gj);
@@ -289,7 +291,7 @@ static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, const
289291
secp256k1_gej g_128j;
290292
int i;
291293

292-
ctx->pre_g_128 = (secp256k1_ge_storage (*)[])checked_malloc(cb, sizeof((*ctx->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G));
294+
ctx->pre_g_128 = (secp256k1_ge_storage (*)[])manual_alloc(prealloc, sizeof((*ctx->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G), base, prealloc_size);
293295

294296
/* calculate 2^128*generator */
295297
g_128j = gj;
@@ -326,10 +328,6 @@ static int secp256k1_ecmult_context_is_built(const secp256k1_ecmult_context *ctx
326328
}
327329

328330
static void secp256k1_ecmult_context_clear(secp256k1_ecmult_context *ctx) {
329-
free(ctx->pre_g);
330-
#ifdef USE_ENDOMORPHISM
331-
free(ctx->pre_g_128);
332-
#endif
333331
secp256k1_ecmult_context_init(ctx);
334332
}
335333

src/secp256k1.c

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,43 @@ size_t secp256k1_context_prealloc_size(unsigned int flags) {
8383
return ret;
8484
}
8585

86-
secp256k1_context* secp256k1_context_create(unsigned int flags) {
87-
secp256k1_context* ret = (secp256k1_context*)checked_malloc(&default_error_callback, sizeof(secp256k1_context));
86+
secp256k1_context* secp256k1_context_prealloc_create(void* prealloc, unsigned int flags) {
87+
void* const base = prealloc;
88+
size_t const ctx_size = secp256k1_context_prealloc_size(flags);
89+
secp256k1_context* ret = (secp256k1_context*)manual_alloc(&prealloc, sizeof(secp256k1_context), base, ctx_size);
90+
8891
ret->illegal_callback = default_illegal_callback;
8992
ret->error_callback = default_error_callback;
9093

9194
if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
9295
secp256k1_callback_call(&ret->illegal_callback,
9396
"Invalid flags");
94-
free(ret);
97+
/* Unreachable */
9598
return NULL;
9699
}
97100

98101
secp256k1_ecmult_context_init(&ret->ecmult_ctx);
99102
secp256k1_ecmult_gen_context_init(&ret->ecmult_gen_ctx);
100103

101104
if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) {
102-
secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &ret->error_callback);
105+
secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &prealloc);
103106
}
104107
if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) {
105-
secp256k1_ecmult_context_build(&ret->ecmult_ctx, &ret->error_callback);
108+
secp256k1_ecmult_context_build(&ret->ecmult_ctx, &prealloc);
106109
}
107110

108-
return ret;
111+
return (secp256k1_context*) ret;
112+
}
113+
114+
secp256k1_context* secp256k1_context_create(unsigned int flags) {
115+
size_t ctx_size = secp256k1_context_prealloc_size(flags);
116+
secp256k1_context* ctx = (secp256k1_context*) checked_malloc(&default_error_callback, ctx_size);
117+
if (secp256k1_context_prealloc_create(ctx, flags) == NULL) {
118+
free(ctx);
119+
return NULL;
120+
}
121+
122+
return ctx;
109123
}
110124

111125
secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
@@ -117,11 +131,16 @@ secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
117131
return ret;
118132
}
119133

120-
void secp256k1_context_destroy(secp256k1_context* ctx) {
134+
void secp256k1_context_prealloc_destroy(secp256k1_context* ctx) {
121135
if (ctx != NULL) {
122136
secp256k1_ecmult_context_clear(&ctx->ecmult_ctx);
123137
secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
138+
}
139+
}
124140

141+
void secp256k1_context_destroy(secp256k1_context* ctx) {
142+
if (ctx != NULL) {
143+
secp256k1_context_prealloc_destroy(ctx);
125144
free(ctx);
126145
}
127146
}

0 commit comments

Comments
 (0)