Skip to content

Commit ef020de

Browse files
Add size constants for preallocated memory
1 parent 1bf7c05 commit ef020de

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

src/ecmult.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ 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);
2425
static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, const secp256k1_callback *cb);
2526
static void secp256k1_ecmult_context_clone(secp256k1_ecmult_context *dst,

src/ecmult_gen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ 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);
3233
static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context* ctx, const secp256k1_callback* cb);
3334
static void secp256k1_ecmult_gen_context_clone(secp256k1_ecmult_gen_context *dst,

src/ecmult_gen_impl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@
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
}

src/ecmult_impl.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <string.h>
1111
#include <stdint.h>
1212

13+
#include "util.h"
1314
#include "group.h"
1415
#include "scalar.h"
1516
#include "ecmult.h"
@@ -310,6 +311,13 @@ static void secp256k1_ecmult_odd_multiples_table_storage_var(const int n, secp25
310311
} \
311312
} while(0)
312313

314+
static const size_t SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE =
315+
ROUND_TO_ALIGN(sizeof((*((secp256k1_ecmult_context*) NULL)->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G))
316+
#ifdef USE_ENDOMORPHISM
317+
+ ROUND_TO_ALIGN(sizeof((*((secp256k1_ecmult_context*) NULL)->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G))
318+
#endif
319+
;
320+
313321
static void secp256k1_ecmult_context_init(secp256k1_ecmult_context *ctx) {
314322
ctx->pre_g = NULL;
315323
#ifdef USE_ENDOMORPHISM
@@ -442,7 +450,7 @@ static int secp256k1_ecmult_wnaf(int *wnaf, int len, const secp256k1_scalar *a,
442450
CHECK(carry == 0);
443451
while (bit < 256) {
444452
CHECK(secp256k1_scalar_get_bits(&s, bit++, 1) == 0);
445-
}
453+
}
446454
#endif
447455
return last_set_bit + 1;
448456
}

src/secp256k1.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ static const secp256k1_context secp256k1_context_no_precomp_ = {
6464
};
6565
const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_no_precomp_;
6666

67+
size_t secp256k1_context_preallocated_size(unsigned int flags) {
68+
size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context));
69+
70+
if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
71+
secp256k1_callback_call(&default_illegal_callback,
72+
"Invalid flags");
73+
return 0;
74+
}
75+
76+
if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) {
77+
ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
78+
}
79+
if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) {
80+
ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;
81+
}
82+
return ret;
83+
}
84+
6785
secp256k1_context* secp256k1_context_create(unsigned int flags) {
6886
secp256k1_context* ret = (secp256k1_context*)checked_malloc(&default_error_callback, sizeof(secp256k1_context));
6987
ret->illegal_callback = default_illegal_callback;

0 commit comments

Comments
 (0)