Skip to content

Commit cf66f23

Browse files
refactor: Add helper function secp256k1_context_is_proper()
1 parent 5c789dc commit cf66f23

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/secp256k1.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ static const secp256k1_context secp256k1_context_static_ = {
7575
const secp256k1_context *secp256k1_context_static = &secp256k1_context_static_;
7676
const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_static_;
7777

78+
/* Helper function that determines if a context is proper, i.e., is not the static context or a copy thereof.
79+
*
80+
* This is intended for "context" functions such as secp256k1_context_clone. Function which need specific
81+
* features of a context should still check for these features directly. For example, a function that needs
82+
* ecmult_gen should directly check for the existence of the ecmult_gen context. */
83+
static int secp256k1_context_is_proper(const secp256k1_context* ctx) {
84+
return secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx);
85+
}
86+
7887
void secp256k1_selftest(void) {
7988
if (!secp256k1_selftest_passes()) {
8089
secp256k1_callback_call(&default_error_callback, "self test failed");
@@ -171,6 +180,9 @@ void secp256k1_context_destroy(secp256k1_context* ctx) {
171180
}
172181

173182
void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
183+
/* We compare pointers instead of checking secp256k1_context_is_proper() here
184+
because setting callbacks is allowed on *copies* of the static context:
185+
it's harmless and makes testing easier. */
174186
ARG_CHECK_NO_RETURN(ctx != secp256k1_context_static);
175187
if (fun == NULL) {
176188
fun = secp256k1_default_illegal_callback_fn;
@@ -180,6 +192,9 @@ void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(
180192
}
181193

182194
void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
195+
/* We compare pointers instead of checking secp256k1_context_is_proper() here
196+
because setting callbacks is allowed on *copies* of the static context:
197+
it's harmless and makes testing easier. */
183198
ARG_CHECK_NO_RETURN(ctx != secp256k1_context_static);
184199
if (fun == NULL) {
185200
fun = secp256k1_default_error_callback_fn;

0 commit comments

Comments
 (0)