Skip to content

Commit 314a61d

Browse files
committed
Merge bitcoin#553: add static context object which has no capabilities
40fde61 prevent attempts to modify `secp256k1_context_no_precomp` (Andrew Poelstra) ed7c084 add static context object which has no capabilities (Andrew Poelstra) Pull request description: Tree-SHA512: a843ed7ba00a00a46eec3146ce428d4b49eb440af766f44d731b1f51553d08de8cc9a0af5ed114d0dfdca6f4bf4a2ede4dbd6a37d6bd818b81630089424a0ba5
2 parents 1086fda + 40fde61 commit 314a61d

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

include/secp256k1.h

+7
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ typedef int (*secp256k1_nonce_function)(
179179
#define SECP256K1_TAG_PUBKEY_HYBRID_EVEN 0x06
180180
#define SECP256K1_TAG_PUBKEY_HYBRID_ODD 0x07
181181

182+
/** A simple secp256k1 context object with no precomputed tables. These are useful for
183+
* type serialization/parsing functions which require a context object to maintain
184+
* API consistency, but currently do not require expensive precomputations or dynamic
185+
* allocations.
186+
*/
187+
SECP256K1_API extern const secp256k1_context *secp256k1_context_no_precomp;
188+
182189
/** Create a secp256k1 context object.
183190
*
184191
* Returns: a newly created context object.

src/secp256k1.c

+12
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ struct secp256k1_context_struct {
5656
secp256k1_callback error_callback;
5757
};
5858

59+
static const secp256k1_context secp256k1_context_no_precomp_ = {
60+
{ 0 },
61+
{ 0 },
62+
{ default_illegal_callback_fn, 0 },
63+
{ default_error_callback_fn, 0 }
64+
};
65+
const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_no_precomp_;
66+
5967
secp256k1_context* secp256k1_context_create(unsigned int flags) {
6068
secp256k1_context* ret = (secp256k1_context*)checked_malloc(&default_error_callback, sizeof(secp256k1_context));
6169
ret->illegal_callback = default_illegal_callback;
@@ -91,6 +99,7 @@ secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
9199
}
92100

93101
void secp256k1_context_destroy(secp256k1_context* ctx) {
102+
CHECK(ctx != secp256k1_context_no_precomp);
94103
if (ctx != NULL) {
95104
secp256k1_ecmult_context_clear(&ctx->ecmult_ctx);
96105
secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
@@ -100,6 +109,7 @@ void secp256k1_context_destroy(secp256k1_context* ctx) {
100109
}
101110

102111
void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
112+
CHECK(ctx != secp256k1_context_no_precomp);
103113
if (fun == NULL) {
104114
fun = default_illegal_callback_fn;
105115
}
@@ -108,6 +118,7 @@ void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(
108118
}
109119

110120
void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
121+
CHECK(ctx != secp256k1_context_no_precomp);
111122
if (fun == NULL) {
112123
fun = default_error_callback_fn;
113124
}
@@ -559,6 +570,7 @@ int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey
559570

560571
int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *seed32) {
561572
VERIFY_CHECK(ctx != NULL);
573+
CHECK(ctx != secp256k1_context_no_precomp);
562574
ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
563575
secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
564576
return 1;

src/tests.c

+1
Original file line numberDiff line numberDiff line change
@@ -3599,6 +3599,7 @@ void run_ec_pubkey_parse_test(void) {
35993599
ecount = 0;
36003600
VG_UNDEF(&pubkey, sizeof(pubkey));
36013601
CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeyc, 65) == 1);
3602+
CHECK(secp256k1_ec_pubkey_parse(secp256k1_context_no_precomp, &pubkey, pubkeyc, 65) == 1);
36023603
VG_CHECK(&pubkey, sizeof(pubkey));
36033604
CHECK(ecount == 0);
36043605
VG_UNDEF(&ge, sizeof(ge));

0 commit comments

Comments
 (0)