From 856f880a6080e8bbc575ae420754986604b9ad2e Mon Sep 17 00:00:00 2001 From: Tim Ruffing Date: Thu, 1 Nov 2018 15:15:44 +0100 Subject: [PATCH] Rename _prealloc functions to _preallocated --- Makefile.am | 2 +- ...k1_prealloc.h => secp256k1_preallocated.h} | 30 +++---- src/ecmult.h | 2 +- src/ecmult_gen.h | 2 +- src/ecmult_gen_impl.h | 6 +- src/ecmult_impl.h | 8 +- src/gen_context.c | 6 +- src/secp256k1.c | 36 ++++---- src/tests.c | 84 +++++++++---------- src/util.h | 2 +- 10 files changed, 89 insertions(+), 89 deletions(-) rename include/{secp256k1_prealloc.h => secp256k1_preallocated.h} (72%) diff --git a/Makefile.am b/Makefile.am index bf719a855f..9f7970e872 100644 --- a/Makefile.am +++ b/Makefile.am @@ -8,7 +8,7 @@ else JNI_LIB = endif include_HEADERS = include/secp256k1.h -include_HEADERS += include/secp256k1_prealloc.h +include_HEADERS += include/secp256k1_preallocated.h noinst_HEADERS = noinst_HEADERS += src/scalar.h noinst_HEADERS += src/scalar_4x64.h diff --git a/include/secp256k1_prealloc.h b/include/secp256k1_preallocated.h similarity index 72% rename from include/secp256k1_prealloc.h rename to include/secp256k1_preallocated.h index bfaa5c01a9..b692e0be6e 100644 --- a/include/secp256k1_prealloc.h +++ b/include/secp256k1_preallocated.h @@ -1,5 +1,5 @@ -#ifndef SECP256K1_PREALLOC_H -#define SECP256K1_PREALLOC_H +#ifndef SECP256K1_PREALLOCACTED_H +#define SECP256K1_PREALLOCACTED_H #include "secp256k1.h" @@ -11,12 +11,12 @@ extern "C" { * caller-provided memory. * * The purpose of this function is to determine how much memory must be provided - * to secp256k1_context_prealloc_create. + * to secp256k1_context_preallocated_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( +SECP256K1_API size_t secp256k1_context_preallocated_size( unsigned int flags ) SECP256K1_WARN_UNUSED_RESULT; @@ -24,14 +24,14 @@ SECP256K1_API size_t secp256k1_context_prealloc_size( * * 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) + * size at least secp256k1_context_preallocated_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( +SECP256K1_API secp256k1_context* secp256k1_context_preallocated_create( void* prealloc, unsigned int flags ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT; @@ -40,12 +40,12 @@ SECP256K1_API secp256k1_context* secp256k1_context_prealloc_create( * 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. + * to secp256k1_context_preallocated_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( +SECP256K1_API size_t secp256k1_context_preallocated_clone_size( const secp256k1_context* ctx ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT; @@ -54,11 +54,11 @@ SECP256K1_API size_t secp256k1_context_prealloc_clone_size( * 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) + * size at least secp256k1_context_preallocated_size(flags) * bytes, suitably aligned to hold an object of any type * (cannot be NULL) */ -SECP256K1_API secp256k1_context* secp256k1_context_prealloc_clone( +SECP256K1_API secp256k1_context* secp256k1_context_preallocated_clone( const secp256k1_context* ctx, void* prealloc ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_WARN_UNUSED_RESULT; @@ -69,16 +69,16 @@ SECP256K1_API secp256k1_context* secp256k1_context_prealloc_clone( * 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. + * secp256k1_context_preallocated_create or secp256k1_context_preallocated_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_context_preallocated_create or + * secp256k1_context_preallocated_clone (cannot be NULL) */ -SECP256K1_API void secp256k1_context_prealloc_destroy( +SECP256K1_API void secp256k1_context_preallocated_destroy( secp256k1_context* ctx ); @@ -86,4 +86,4 @@ SECP256K1_API void secp256k1_context_prealloc_destroy( } #endif -#endif /* SECP256K1_PREALLOC_H */ +#endif /* SECP256K1_PREALLOCATED_H */ diff --git a/src/ecmult.h b/src/ecmult.h index adf7477b92..848c1ac329 100644 --- a/src/ecmult.h +++ b/src/ecmult.h @@ -20,7 +20,7 @@ typedef struct { #endif } secp256k1_ecmult_context; -static size_t secp256k1_ecmult_context_prealloc_size(void); +static size_t secp256k1_ecmult_context_preallocated_size(void); static void secp256k1_ecmult_context_init(secp256k1_ecmult_context *ctx); static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, void **prealloc); static void secp256k1_ecmult_context_finalize_memcpy(secp256k1_ecmult_context *dst, const secp256k1_ecmult_context *src); diff --git a/src/ecmult_gen.h b/src/ecmult_gen.h index 4ad1afa891..b39e5086ba 100644 --- a/src/ecmult_gen.h +++ b/src/ecmult_gen.h @@ -28,7 +28,7 @@ typedef struct { secp256k1_gej initial; } secp256k1_ecmult_gen_context; -static size_t secp256k1_ecmult_gen_context_prealloc_size(void); +static size_t secp256k1_ecmult_gen_context_preallocated_size(void); static void secp256k1_ecmult_gen_context_init(secp256k1_ecmult_gen_context* ctx); static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context* ctx, void **prealloc); static void secp256k1_ecmult_gen_context_finalize_memcpy(secp256k1_ecmult_gen_context *dst, const secp256k1_ecmult_gen_context* src); diff --git a/src/ecmult_gen_impl.h b/src/ecmult_gen_impl.h index d2ea9e12aa..a9e3d86329 100644 --- a/src/ecmult_gen_impl.h +++ b/src/ecmult_gen_impl.h @@ -16,7 +16,7 @@ #include "ecmult_static_context.h" #endif -static size_t secp256k1_ecmult_gen_context_prealloc_size(void) { +static size_t secp256k1_ecmult_gen_context_preallocated_size(void) { #ifndef USE_ECMULT_STATIC_PRECOMPUTATION return ROUND_TO_ALIGN(sizeof(*((secp256k1_ecmult_gen_context*) NULL)->prec)); #else @@ -34,7 +34,7 @@ static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx secp256k1_gej gj; secp256k1_gej nums_gej; int i, j; - size_t const prealloc_size = secp256k1_ecmult_gen_context_prealloc_size(); + size_t const prealloc_size = secp256k1_ecmult_gen_context_preallocated_size(); void* const base = *prealloc; #endif @@ -42,7 +42,7 @@ static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx return; } #ifndef USE_ECMULT_STATIC_PRECOMPUTATION - ctx->prec = (secp256k1_ge_storage (*)[64][16])manual_alloc(prealloc, prealloc_size, base, prealloc_size); + ctx->prec = (secp256k1_ge_storage (*)[64][16])manual_malloc(prealloc, prealloc_size, base, prealloc_size); /* get the generator */ secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g); diff --git a/src/ecmult_impl.h b/src/ecmult_impl.h index 5ac10d00ca..cc8fd7895b 100644 --- a/src/ecmult_impl.h +++ b/src/ecmult_impl.h @@ -254,7 +254,7 @@ static void secp256k1_ecmult_odd_multiples_table_storage_var(const int n, secp25 } \ } while(0) -static size_t secp256k1_ecmult_context_prealloc_size(void) { +static size_t secp256k1_ecmult_context_preallocated_size(void) { int ret = ROUND_TO_ALIGN(sizeof((*((secp256k1_ecmult_context*) NULL)->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G)); #ifdef USE_ENDOMORPHISM ret += ROUND_TO_ALIGN(sizeof((*((secp256k1_ecmult_context*) NULL)->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G)); @@ -272,7 +272,7 @@ static void secp256k1_ecmult_context_init(secp256k1_ecmult_context *ctx) { static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, void **prealloc) { secp256k1_gej gj; void* const base = *prealloc; - size_t const prealloc_size = secp256k1_ecmult_context_prealloc_size(); + size_t const prealloc_size = secp256k1_ecmult_context_preallocated_size(); if (ctx->pre_g != NULL) { return; @@ -281,7 +281,7 @@ static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, void * /* get the generator */ secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g); - ctx->pre_g = (secp256k1_ge_storage (*)[])manual_alloc(prealloc, sizeof((*ctx->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G), base, prealloc_size); + ctx->pre_g = (secp256k1_ge_storage (*)[])manual_malloc(prealloc, sizeof((*ctx->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G), base, prealloc_size); /* precompute the tables with odd multiples */ secp256k1_ecmult_odd_multiples_table_storage_var(ECMULT_TABLE_SIZE(WINDOW_G), *ctx->pre_g, &gj); @@ -291,7 +291,7 @@ static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, void * secp256k1_gej g_128j; int i; - ctx->pre_g_128 = (secp256k1_ge_storage (*)[])manual_alloc(prealloc, sizeof((*ctx->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G), base, prealloc_size); + ctx->pre_g_128 = (secp256k1_ge_storage (*)[])manual_malloc(prealloc, sizeof((*ctx->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G), base, prealloc_size); /* calculate 2^128*generator */ g_128j = gj; diff --git a/src/gen_context.c b/src/gen_context.c index 732278b5e3..5635ec7e0c 100644 --- a/src/gen_context.c +++ b/src/gen_context.c @@ -47,7 +47,7 @@ int main(int argc, char **argv) { fprintf(fp, "#define SC SECP256K1_GE_STORAGE_CONST\n"); fprintf(fp, "static const secp256k1_ge_storage secp256k1_ecmult_static_context[64][16] = {\n"); - base = checked_malloc(&default_error_callback, secp256k1_ecmult_gen_context_prealloc_size()); + base = checked_malloc(&default_error_callback, secp256k1_ecmult_gen_context_preallocated_size()); prealloc = base; secp256k1_ecmult_gen_context_init(&ctx); secp256k1_ecmult_gen_context_build(&ctx, &prealloc); @@ -70,10 +70,10 @@ int main(int argc, char **argv) { fprintf(fp,"};\n"); secp256k1_ecmult_gen_context_clear(&ctx); free(base); - + fprintf(fp, "#undef SC\n"); fprintf(fp, "#endif\n"); fclose(fp); - + return 0; } diff --git a/src/secp256k1.c b/src/secp256k1.c index e4565a74db..7b868a84fe 100644 --- a/src/secp256k1.c +++ b/src/secp256k1.c @@ -5,7 +5,7 @@ **********************************************************************/ #include "include/secp256k1.h" -#include "include/secp256k1_prealloc.h" +#include "include/secp256k1_preallocated.h" #include "util.h" #include "num_impl.h" @@ -65,7 +65,7 @@ static secp256k1_context secp256k1_context_no_precomp_ = { }; secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_no_precomp_; -size_t secp256k1_context_prealloc_size(unsigned int flags) { +size_t secp256k1_context_preallocated_size(unsigned int flags) { size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context)); if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) { @@ -76,34 +76,34 @@ size_t secp256k1_context_prealloc_size(unsigned int flags) { } if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) { - ret += secp256k1_ecmult_gen_context_prealloc_size(); + ret += secp256k1_ecmult_gen_context_preallocated_size(); } if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) { - ret += secp256k1_ecmult_context_prealloc_size(); + ret += secp256k1_ecmult_context_preallocated_size(); } return ret; } -size_t secp256k1_context_prealloc_size_for_clone(const secp256k1_context* ctx) { +size_t secp256k1_context_preallocated_size_for_clone(const secp256k1_context* ctx) { size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context)); VERIFY_CHECK(ctx != NULL); if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) { - ret += secp256k1_ecmult_gen_context_prealloc_size(); + ret += secp256k1_ecmult_gen_context_preallocated_size(); } if (secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx)) { - ret += secp256k1_ecmult_context_prealloc_size(); + ret += secp256k1_ecmult_context_preallocated_size(); } return ret; } -secp256k1_context* secp256k1_context_prealloc_create(void* prealloc, unsigned int flags) { +secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigned int flags) { void* const base = prealloc; size_t prealloc_size; secp256k1_context* ret; VERIFY_CHECK(prealloc != NULL); - prealloc_size = secp256k1_context_prealloc_size(flags); - ret = (secp256k1_context*)manual_alloc(&prealloc, sizeof(secp256k1_context), base, prealloc_size); + prealloc_size = secp256k1_context_preallocated_size(flags); + ret = (secp256k1_context*)manual_malloc(&prealloc, sizeof(secp256k1_context), base, prealloc_size); ret->illegal_callback = default_illegal_callback; ret->error_callback = default_error_callback; @@ -128,9 +128,9 @@ secp256k1_context* secp256k1_context_prealloc_create(void* prealloc, unsigned in } secp256k1_context* secp256k1_context_create(unsigned int flags) { - size_t const prealloc_size = secp256k1_context_prealloc_size(flags); + size_t const prealloc_size = secp256k1_context_preallocated_size(flags); secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size); - if (EXPECT(secp256k1_context_prealloc_create(ctx, flags) == NULL, 0)) { + if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) { free(ctx); return NULL; } @@ -138,13 +138,13 @@ secp256k1_context* secp256k1_context_create(unsigned int flags) { return ctx; } -secp256k1_context* secp256k1_context_prealloc_clone(const secp256k1_context* ctx, void* prealloc) { +secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) { size_t prealloc_size; secp256k1_context* ret; VERIFY_CHECK(ctx != NULL); ARG_CHECK(prealloc != NULL); - prealloc_size = secp256k1_context_prealloc_size_for_clone(ctx); + prealloc_size = secp256k1_context_preallocated_size_for_clone(ctx); ret = (secp256k1_context*)prealloc; memcpy(ret, ctx, prealloc_size); secp256k1_ecmult_gen_context_finalize_memcpy(&ret->ecmult_gen_ctx, &ctx->ecmult_gen_ctx); @@ -157,13 +157,13 @@ secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) { size_t prealloc_size; VERIFY_CHECK(ctx != NULL); - prealloc_size = secp256k1_context_prealloc_size_for_clone(ctx); + prealloc_size = secp256k1_context_preallocated_size_for_clone(ctx); ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size); - ret = secp256k1_context_prealloc_clone(ctx, ret); + ret = secp256k1_context_preallocated_clone(ctx, ret); return ret; } -void secp256k1_context_prealloc_destroy(secp256k1_context* ctx) { +void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) { if (ctx != NULL) { secp256k1_ecmult_context_clear(&ctx->ecmult_ctx); secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx); @@ -172,7 +172,7 @@ void secp256k1_context_prealloc_destroy(secp256k1_context* ctx) { void secp256k1_context_destroy(secp256k1_context* ctx) { if (ctx != NULL) { - secp256k1_context_prealloc_destroy(ctx); + secp256k1_context_preallocated_destroy(ctx); free(ctx); } } diff --git a/src/tests.c b/src/tests.c index 96ccd850a5..7106639e0b 100644 --- a/src/tests.c +++ b/src/tests.c @@ -16,7 +16,7 @@ #include "secp256k1.c" #include "include/secp256k1.h" -#include "include/secp256k1_prealloc.h" +#include "include/secp256k1_preallocated.h" #include "testrand_impl.h" #ifdef ENABLE_OPENSSL_TESTS @@ -138,7 +138,7 @@ void random_scalar_order(secp256k1_scalar *num) { } while(1); } -void run_context_tests(int use_prealloc) { +void run_context_tests(int use_preallocated) { secp256k1_pubkey pubkey; secp256k1_pubkey zero_pubkey; secp256k1_ecdsa_signature sig; @@ -159,19 +159,19 @@ void run_context_tests(int use_prealloc) { secp256k1_scalar msg, key, nonce; secp256k1_scalar sigr, sigs; - if (use_prealloc) { - none_prealloc = malloc(secp256k1_context_prealloc_size(SECP256K1_CONTEXT_NONE)); - sign_prealloc = malloc(secp256k1_context_prealloc_size(SECP256K1_CONTEXT_SIGN)); - vrfy_prealloc = malloc(secp256k1_context_prealloc_size(SECP256K1_CONTEXT_VERIFY)); - both_prealloc = malloc(secp256k1_context_prealloc_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); + if (use_preallocated) { + none_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); + sign_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN)); + vrfy_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY)); + both_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); CHECK(none_prealloc != NULL); CHECK(sign_prealloc != NULL); CHECK(vrfy_prealloc != NULL); CHECK(both_prealloc != NULL); - none = secp256k1_context_prealloc_create(none_prealloc, SECP256K1_CONTEXT_NONE); - sign = secp256k1_context_prealloc_create(sign_prealloc, SECP256K1_CONTEXT_SIGN); - vrfy = secp256k1_context_prealloc_create(vrfy_prealloc, SECP256K1_CONTEXT_VERIFY); - both = secp256k1_context_prealloc_create(both_prealloc, SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); + none = secp256k1_context_preallocated_create(none_prealloc, SECP256K1_CONTEXT_NONE); + sign = secp256k1_context_preallocated_create(sign_prealloc, SECP256K1_CONTEXT_SIGN); + vrfy = secp256k1_context_preallocated_create(vrfy_prealloc, SECP256K1_CONTEXT_VERIFY); + both = secp256k1_context_preallocated_create(both_prealloc, SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); } else { none = secp256k1_context_create(SECP256K1_CONTEXT_NONE); sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); @@ -192,45 +192,45 @@ void run_context_tests(int use_prealloc) { { secp256k1_context *ctx_tmp; - if (use_prealloc) { + if (use_preallocated) { /* clone into a non-preallocated context and then again into a new preallocated one. */ - ctx_tmp = none; none = secp256k1_context_clone(none); secp256k1_context_prealloc_destroy(ctx_tmp); - free(none_prealloc); none_prealloc = malloc(secp256k1_context_prealloc_size(SECP256K1_CONTEXT_NONE)); CHECK(none_prealloc != NULL); - ctx_tmp = none; none = secp256k1_context_prealloc_clone(none, none_prealloc); secp256k1_context_destroy(ctx_tmp); + ctx_tmp = none; none = secp256k1_context_clone(none); secp256k1_context_preallocated_destroy(ctx_tmp); + free(none_prealloc); none_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(none_prealloc != NULL); + ctx_tmp = none; none = secp256k1_context_preallocated_clone(none, none_prealloc); secp256k1_context_destroy(ctx_tmp); - ctx_tmp = sign; sign = secp256k1_context_clone(sign); secp256k1_context_prealloc_destroy(ctx_tmp); - free(sign_prealloc); sign_prealloc = malloc(secp256k1_context_prealloc_size(SECP256K1_CONTEXT_SIGN)); CHECK(sign_prealloc != NULL); - ctx_tmp = sign; sign = secp256k1_context_prealloc_clone(sign, sign_prealloc); secp256k1_context_destroy(ctx_tmp); + ctx_tmp = sign; sign = secp256k1_context_clone(sign); secp256k1_context_preallocated_destroy(ctx_tmp); + free(sign_prealloc); sign_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN)); CHECK(sign_prealloc != NULL); + ctx_tmp = sign; sign = secp256k1_context_preallocated_clone(sign, sign_prealloc); secp256k1_context_destroy(ctx_tmp); - ctx_tmp = vrfy; vrfy = secp256k1_context_clone(vrfy); secp256k1_context_prealloc_destroy(ctx_tmp); - free(vrfy_prealloc); vrfy_prealloc = malloc(secp256k1_context_prealloc_size(SECP256K1_CONTEXT_VERIFY)); CHECK(vrfy_prealloc != NULL); - ctx_tmp = vrfy; vrfy = secp256k1_context_prealloc_clone(vrfy, vrfy_prealloc); secp256k1_context_destroy(ctx_tmp); + ctx_tmp = vrfy; vrfy = secp256k1_context_clone(vrfy); secp256k1_context_preallocated_destroy(ctx_tmp); + free(vrfy_prealloc); vrfy_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY)); CHECK(vrfy_prealloc != NULL); + ctx_tmp = vrfy; vrfy = secp256k1_context_preallocated_clone(vrfy, vrfy_prealloc); secp256k1_context_destroy(ctx_tmp); - ctx_tmp = both; both = secp256k1_context_clone(both); secp256k1_context_prealloc_destroy(ctx_tmp); - free(both_prealloc); both_prealloc = malloc(secp256k1_context_prealloc_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); CHECK(both_prealloc != NULL); - ctx_tmp = both; both = secp256k1_context_prealloc_clone(both, both_prealloc); secp256k1_context_destroy(ctx_tmp); + ctx_tmp = both; both = secp256k1_context_clone(both); secp256k1_context_preallocated_destroy(ctx_tmp); + free(both_prealloc); both_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); CHECK(both_prealloc != NULL); + ctx_tmp = both; both = secp256k1_context_preallocated_clone(both, both_prealloc); secp256k1_context_destroy(ctx_tmp); } else { /* clone into a preallocated context and then again into a new non-preallocated one. */ void *prealloc_tmp; - prealloc_tmp = malloc(secp256k1_context_prealloc_size(SECP256K1_CONTEXT_NONE)); CHECK(prealloc_tmp != NULL); - ctx_tmp = none; none = secp256k1_context_prealloc_clone(none, prealloc_tmp); secp256k1_context_destroy(ctx_tmp); - ctx_tmp = none; none = secp256k1_context_clone(none); secp256k1_context_prealloc_destroy(ctx_tmp); + prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(prealloc_tmp != NULL); + ctx_tmp = none; none = secp256k1_context_preallocated_clone(none, prealloc_tmp); secp256k1_context_destroy(ctx_tmp); + ctx_tmp = none; none = secp256k1_context_clone(none); secp256k1_context_preallocated_destroy(ctx_tmp); free(prealloc_tmp); - prealloc_tmp = malloc(secp256k1_context_prealloc_size(SECP256K1_CONTEXT_SIGN)); CHECK(prealloc_tmp != NULL); - ctx_tmp = sign; sign = secp256k1_context_prealloc_clone(sign, prealloc_tmp); secp256k1_context_destroy(ctx_tmp); - ctx_tmp = sign; sign = secp256k1_context_clone(sign); secp256k1_context_prealloc_destroy(ctx_tmp); + prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN)); CHECK(prealloc_tmp != NULL); + ctx_tmp = sign; sign = secp256k1_context_preallocated_clone(sign, prealloc_tmp); secp256k1_context_destroy(ctx_tmp); + ctx_tmp = sign; sign = secp256k1_context_clone(sign); secp256k1_context_preallocated_destroy(ctx_tmp); free(prealloc_tmp); - prealloc_tmp = malloc(secp256k1_context_prealloc_size(SECP256K1_CONTEXT_VERIFY)); CHECK(prealloc_tmp != NULL); - ctx_tmp = vrfy; vrfy = secp256k1_context_prealloc_clone(vrfy, prealloc_tmp); secp256k1_context_destroy(ctx_tmp); - ctx_tmp = vrfy; vrfy = secp256k1_context_clone(vrfy); secp256k1_context_prealloc_destroy(ctx_tmp); + prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY)); CHECK(prealloc_tmp != NULL); + ctx_tmp = vrfy; vrfy = secp256k1_context_preallocated_clone(vrfy, prealloc_tmp); secp256k1_context_destroy(ctx_tmp); + ctx_tmp = vrfy; vrfy = secp256k1_context_clone(vrfy); secp256k1_context_preallocated_destroy(ctx_tmp); free(prealloc_tmp); - prealloc_tmp = malloc(secp256k1_context_prealloc_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); CHECK(prealloc_tmp != NULL); - ctx_tmp = both; both = secp256k1_context_prealloc_clone(both, prealloc_tmp); secp256k1_context_destroy(ctx_tmp); - ctx_tmp = both; both = secp256k1_context_clone(both); secp256k1_context_prealloc_destroy(ctx_tmp); + prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); CHECK(prealloc_tmp != NULL); + ctx_tmp = both; both = secp256k1_context_preallocated_clone(both, prealloc_tmp); secp256k1_context_destroy(ctx_tmp); + ctx_tmp = both; both = secp256k1_context_clone(both); secp256k1_context_preallocated_destroy(ctx_tmp); free(prealloc_tmp); } } @@ -301,11 +301,11 @@ void run_context_tests(int use_prealloc) { CHECK(secp256k1_ecdsa_sig_verify(&both->ecmult_ctx, &sigr, &sigs, &pub, &msg)); /* cleanup */ - if (use_prealloc) { - secp256k1_context_prealloc_destroy(none); - secp256k1_context_prealloc_destroy(sign); - secp256k1_context_prealloc_destroy(vrfy); - secp256k1_context_prealloc_destroy(both); + if (use_preallocated) { + secp256k1_context_preallocated_destroy(none); + secp256k1_context_preallocated_destroy(sign); + secp256k1_context_preallocated_destroy(vrfy); + secp256k1_context_preallocated_destroy(both); free(none_prealloc); free(sign_prealloc); free(vrfy_prealloc); @@ -318,7 +318,7 @@ void run_context_tests(int use_prealloc) { } /* Defined as no-op. */ secp256k1_context_destroy(NULL); - secp256k1_context_prealloc_destroy(NULL); + secp256k1_context_preallocated_destroy(NULL); } diff --git a/src/util.h b/src/util.h index 1827c1e4a5..e15b3ad9a9 100644 --- a/src/util.h +++ b/src/util.h @@ -95,7 +95,7 @@ static SECP256K1_INLINE void *checked_realloc(const secp256k1_callback* cb, void #define ROUND_TO_ALIGN(size) (((size + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT) -static SECP256K1_INLINE void *manual_alloc(void** prealloc_ptr, size_t alloc_size, void* base, size_t max_size) { +static SECP256K1_INLINE void *manual_malloc(void** prealloc_ptr, size_t alloc_size, void* base, size_t max_size) { size_t aligned_alloc_size = ROUND_TO_ALIGN(alloc_size); void* ret = *prealloc_ptr; CHECK((unsigned char*)*prealloc_ptr != NULL);