From c259a7cbc0d93fe70dd3fea3d26ab95dbbf1a618 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sat, 13 Sep 2014 17:19:26 +0200 Subject: [PATCH] Set precomputation table late and unset early. Set the global pointer to the precomputation table only after initializing it completely, and unset it before doing any uninitialization. This causes fail-fast behavior in case of race conditions between initialization and operations using it. --- src/ecmult_impl.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/ecmult_impl.h b/src/ecmult_impl.h index c09cfec..1b575aa 100644 --- a/src/ecmult_impl.h +++ b/src/ecmult_impl.h @@ -94,8 +94,8 @@ static void secp256k1_ecmult_start(void) { if (secp256k1_ecmult_consts != NULL) return; + // Allocate the precomputation table. secp256k1_ecmult_consts_t *ret = (secp256k1_ecmult_consts_t*)malloc(sizeof(secp256k1_ecmult_consts_t)); - secp256k1_ecmult_consts = ret; // get the generator const secp256k1_ge_t *g = &secp256k1_ge_consts->g; @@ -109,14 +109,17 @@ static void secp256k1_ecmult_start(void) { // precompute the tables with odd multiples secp256k1_ecmult_table_precomp_ge(ret->pre_g, &gj, WINDOW_G); secp256k1_ecmult_table_precomp_ge(ret->pre_g_128, &g_128j, WINDOW_G); + + // Set the global pointer to the precomputation table. + secp256k1_ecmult_consts = ret; } static void secp256k1_ecmult_gen_start(void) { if (secp256k1_ecmult_gen_consts != NULL) return; + // Allocate the precomputation table. secp256k1_ecmult_gen_consts_t *ret = (secp256k1_ecmult_gen_consts_t*)malloc(sizeof(secp256k1_ecmult_gen_consts_t)); - secp256k1_ecmult_gen_consts = ret; // get the generator const secp256k1_ge_t *g = &secp256k1_ge_consts->g; @@ -169,6 +172,9 @@ static void secp256k1_ecmult_gen_start(void) { ret->prec[j][k][i] = raw[k]; } } + + // Set the global pointer to the precomputation table. + secp256k1_ecmult_gen_consts = ret; } static void secp256k1_ecmult_stop(void) { @@ -176,8 +182,8 @@ static void secp256k1_ecmult_stop(void) { return; secp256k1_ecmult_consts_t *c = (secp256k1_ecmult_consts_t*)secp256k1_ecmult_consts; - free(c); secp256k1_ecmult_consts = NULL; + free(c); } static void secp256k1_ecmult_gen_stop(void) { @@ -185,8 +191,8 @@ static void secp256k1_ecmult_gen_stop(void) { return; secp256k1_ecmult_gen_consts_t *c = (secp256k1_ecmult_gen_consts_t*)secp256k1_ecmult_gen_consts; - free(c); secp256k1_ecmult_gen_consts = NULL; + free(c); } /** Convert a number to WNAF notation. The number becomes represented by sum(2^i * wnaf[i], i=0..bits),