Skip to content

Commit 86540e9

Browse files
committed
tests: add test for deprecated flags and rm them from run_context
1 parent caa0ad6 commit 86540e9

File tree

2 files changed

+71
-96
lines changed

2 files changed

+71
-96
lines changed

src/secp256k1.c

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
} \
5858
} while(0)
5959

60+
/* Note that whenever you change the context struct, you must also change the
61+
* context_eq function. */
6062
struct secp256k1_context_struct {
6163
secp256k1_ecmult_gen_context ecmult_gen_ctx;
6264
secp256k1_callback illegal_callback;

src/tests.c

+69-96
Original file line numberDiff line numberDiff line change
@@ -146,22 +146,47 @@ void run_selftest_tests(void) {
146146
secp256k1_selftest();
147147
}
148148

149+
int ecmult_gen_context_eq(const secp256k1_ecmult_gen_context *a, const secp256k1_ecmult_gen_context *b) {
150+
return a->built == b->built
151+
&& secp256k1_scalar_eq(&a->blind, &b->blind)
152+
&& secp256k1_gej_eq_var(&a->initial, &b->initial);
153+
}
154+
155+
int context_eq(const secp256k1_context *a, const secp256k1_context *b) {
156+
return a->declassify == b->declassify
157+
&& ecmult_gen_context_eq(&a->ecmult_gen_ctx, &b->ecmult_gen_ctx)
158+
&& a->illegal_callback.fn == b->illegal_callback.fn
159+
&& a->illegal_callback.data == b->illegal_callback.
160+
data
161+
&& a->error_callback.fn == b->error_callback.fn
162+
&& a->error_callback.data == b->error_callback.data;
163+
}
164+
165+
void test_deprecated_flags(void) {
166+
unsigned int flags[] = { SECP256K1_CONTEXT_SIGN,
167+
SECP256K1_CONTEXT_VERIFY,
168+
SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY };
169+
int i;
170+
/* Check that a context created with any of the flags in the flags array is
171+
* identical to the NONE context. */
172+
for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++) {
173+
secp256k1_context *tmp_ctx;
174+
CHECK(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE) == secp256k1_context_preallocated_size(flags[i]));
175+
tmp_ctx = secp256k1_context_create(flags[i]);
176+
CHECK(context_eq(ctx, tmp_ctx));
177+
secp256k1_context_destroy(tmp_ctx);
178+
}
179+
}
180+
149181
void run_context_tests(int use_prealloc) {
150182
secp256k1_pubkey pubkey;
151183
secp256k1_pubkey zero_pubkey;
152184
secp256k1_ecdsa_signature sig;
153185
unsigned char ctmp[32];
154186
int32_t ecount;
155187
int32_t ecount2;
156-
secp256k1_context *none;
157-
secp256k1_context *sign;
158-
secp256k1_context *vrfy;
159-
secp256k1_context *both;
160188
secp256k1_context *sttc;
161-
void *none_prealloc = NULL;
162-
void *sign_prealloc = NULL;
163-
void *vrfy_prealloc = NULL;
164-
void *both_prealloc = NULL;
189+
void *ctx_prealloc = NULL;
165190
void *sttc_prealloc = NULL;
166191

167192
secp256k1_gej pubj;
@@ -173,45 +198,32 @@ void run_context_tests(int use_prealloc) {
173198
CHECK(secp256k1_context_no_precomp == secp256k1_context_static);
174199

175200
if (use_prealloc) {
176-
none_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
177-
sign_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN));
178-
vrfy_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY));
179-
both_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY));
201+
ctx_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
202+
CHECK(ctx_prealloc != NULL);
203+
ctx = secp256k1_context_preallocated_create(ctx_prealloc, SECP256K1_CONTEXT_NONE);
180204
sttc_prealloc = malloc(secp256k1_context_preallocated_clone_size(secp256k1_context_static));
181-
CHECK(none_prealloc != NULL);
182-
CHECK(sign_prealloc != NULL);
183-
CHECK(vrfy_prealloc != NULL);
184-
CHECK(both_prealloc != NULL);
185205
CHECK(sttc_prealloc != NULL);
186-
none = secp256k1_context_preallocated_create(none_prealloc, SECP256K1_CONTEXT_NONE);
187-
sign = secp256k1_context_preallocated_create(sign_prealloc, SECP256K1_CONTEXT_SIGN);
188-
vrfy = secp256k1_context_preallocated_create(vrfy_prealloc, SECP256K1_CONTEXT_VERIFY);
189-
both = secp256k1_context_preallocated_create(both_prealloc, SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
190206
sttc = secp256k1_context_preallocated_clone(secp256k1_context_static, sttc_prealloc);
191207
} else {
192-
none = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
193-
sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
194-
vrfy = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
195-
both = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
196208
sttc = secp256k1_context_clone(secp256k1_context_static);
209+
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
197210
}
198211

212+
test_deprecated_flags();
213+
199214
memset(&zero_pubkey, 0, sizeof(zero_pubkey));
200215

201216
ecount = 0;
202217
ecount2 = 10;
203218
secp256k1_context_set_illegal_callback(sttc, counting_illegal_callback_fn, &ecount);
204-
secp256k1_context_set_illegal_callback(sign, counting_illegal_callback_fn, &ecount2);
219+
secp256k1_context_set_illegal_callback(ctx, counting_illegal_callback_fn, &ecount2);
205220
/* set error callback (to a function that still aborts in case malloc() fails in secp256k1_context_clone() below) */
206-
secp256k1_context_set_error_callback(sign, secp256k1_default_illegal_callback_fn, NULL);
207-
CHECK(sign->error_callback.fn != vrfy->error_callback.fn);
208-
CHECK(sign->error_callback.fn == secp256k1_default_illegal_callback_fn);
221+
secp256k1_context_set_error_callback(ctx, secp256k1_default_illegal_callback_fn, NULL);
222+
CHECK(ctx->error_callback.fn != sttc->error_callback.fn);
223+
CHECK(ctx->error_callback.fn == secp256k1_default_illegal_callback_fn);
209224

210225
/* check if sizes for cloning are consistent */
211-
CHECK(secp256k1_context_preallocated_clone_size(none) == secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
212-
CHECK(secp256k1_context_preallocated_clone_size(sign) == secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN));
213-
CHECK(secp256k1_context_preallocated_clone_size(vrfy) == secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY));
214-
CHECK(secp256k1_context_preallocated_clone_size(both) == secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY));
226+
CHECK(secp256k1_context_preallocated_clone_size(ctx) == secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
215227
CHECK(secp256k1_context_preallocated_clone_size(sttc) >= sizeof(secp256k1_context));
216228

217229
/*** clone and destroy all of them to make sure cloning was complete ***/
@@ -220,88 +232,61 @@ void run_context_tests(int use_prealloc) {
220232

221233
if (use_prealloc) {
222234
/* clone into a non-preallocated context and then again into a new preallocated one. */
223-
ctx_tmp = none; none = secp256k1_context_clone(none); secp256k1_context_preallocated_destroy(ctx_tmp);
224-
free(none_prealloc); none_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(none_prealloc != NULL);
225-
ctx_tmp = none; none = secp256k1_context_preallocated_clone(none, none_prealloc); secp256k1_context_destroy(ctx_tmp);
226-
227-
ctx_tmp = sign; sign = secp256k1_context_clone(sign); secp256k1_context_preallocated_destroy(ctx_tmp);
228-
free(sign_prealloc); sign_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN)); CHECK(sign_prealloc != NULL);
229-
ctx_tmp = sign; sign = secp256k1_context_preallocated_clone(sign, sign_prealloc); secp256k1_context_destroy(ctx_tmp);
230-
231-
ctx_tmp = vrfy; vrfy = secp256k1_context_clone(vrfy); secp256k1_context_preallocated_destroy(ctx_tmp);
232-
free(vrfy_prealloc); vrfy_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY)); CHECK(vrfy_prealloc != NULL);
233-
ctx_tmp = vrfy; vrfy = secp256k1_context_preallocated_clone(vrfy, vrfy_prealloc); secp256k1_context_destroy(ctx_tmp);
234-
235-
ctx_tmp = both; both = secp256k1_context_clone(both); secp256k1_context_preallocated_destroy(ctx_tmp);
236-
free(both_prealloc); both_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); CHECK(both_prealloc != NULL);
237-
ctx_tmp = both; both = secp256k1_context_preallocated_clone(both, both_prealloc); secp256k1_context_destroy(ctx_tmp);
235+
ctx_tmp = ctx; ctx = secp256k1_context_clone(ctx); secp256k1_context_preallocated_destroy(ctx_tmp);
236+
free(ctx_prealloc); ctx_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(ctx_prealloc != NULL);
237+
ctx_tmp = ctx; ctx = secp256k1_context_preallocated_clone(ctx, ctx_prealloc); secp256k1_context_destroy(ctx_tmp);
238238
} else {
239239
/* clone into a preallocated context and then again into a new non-preallocated one. */
240240
void *prealloc_tmp;
241241

242242
prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(prealloc_tmp != NULL);
243-
ctx_tmp = none; none = secp256k1_context_preallocated_clone(none, prealloc_tmp); secp256k1_context_destroy(ctx_tmp);
244-
ctx_tmp = none; none = secp256k1_context_clone(none); secp256k1_context_preallocated_destroy(ctx_tmp);
245-
free(prealloc_tmp);
246-
247-
prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN)); CHECK(prealloc_tmp != NULL);
248-
ctx_tmp = sign; sign = secp256k1_context_preallocated_clone(sign, prealloc_tmp); secp256k1_context_destroy(ctx_tmp);
249-
ctx_tmp = sign; sign = secp256k1_context_clone(sign); secp256k1_context_preallocated_destroy(ctx_tmp);
250-
free(prealloc_tmp);
251-
252-
prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY)); CHECK(prealloc_tmp != NULL);
253-
ctx_tmp = vrfy; vrfy = secp256k1_context_preallocated_clone(vrfy, prealloc_tmp); secp256k1_context_destroy(ctx_tmp);
254-
ctx_tmp = vrfy; vrfy = secp256k1_context_clone(vrfy); secp256k1_context_preallocated_destroy(ctx_tmp);
255-
free(prealloc_tmp);
256-
257-
prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); CHECK(prealloc_tmp != NULL);
258-
ctx_tmp = both; both = secp256k1_context_preallocated_clone(both, prealloc_tmp); secp256k1_context_destroy(ctx_tmp);
259-
ctx_tmp = both; both = secp256k1_context_clone(both); secp256k1_context_preallocated_destroy(ctx_tmp);
243+
ctx_tmp = ctx; ctx = secp256k1_context_preallocated_clone(ctx, prealloc_tmp); secp256k1_context_destroy(ctx_tmp);
244+
ctx_tmp = ctx; ctx = secp256k1_context_clone(ctx); secp256k1_context_preallocated_destroy(ctx_tmp);
260245
free(prealloc_tmp);
261246
}
262247
}
263248

264249
/* Verify that the error callback makes it across the clone. */
265-
CHECK(sign->error_callback.fn != vrfy->error_callback.fn);
266-
CHECK(sign->error_callback.fn == secp256k1_default_illegal_callback_fn);
250+
CHECK(ctx->error_callback.fn != sttc->error_callback.fn);
251+
CHECK(ctx->error_callback.fn == secp256k1_default_illegal_callback_fn);
267252
/* And that it resets back to default. */
268-
secp256k1_context_set_error_callback(sign, NULL, NULL);
269-
CHECK(vrfy->error_callback.fn == sign->error_callback.fn);
253+
secp256k1_context_set_error_callback(ctx, NULL, NULL);
254+
CHECK(ctx->error_callback.fn == sttc->error_callback.fn);
270255

271256
/*** attempt to use them ***/
272257
random_scalar_order_test(&msg);
273258
random_scalar_order_test(&key);
274-
secp256k1_ecmult_gen(&both->ecmult_gen_ctx, &pubj, &key);
259+
secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pubj, &key);
275260
secp256k1_ge_set_gej(&pub, &pubj);
276261

277262
/* Verify context-type checking illegal-argument errors. */
278263
memset(ctmp, 1, 32);
279264
CHECK(secp256k1_ec_pubkey_create(sttc, &pubkey, ctmp) == 0);
280265
CHECK(ecount == 1);
281266
VG_UNDEF(&pubkey, sizeof(pubkey));
282-
CHECK(secp256k1_ec_pubkey_create(sign, &pubkey, ctmp) == 1);
267+
CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, ctmp) == 1);
283268
VG_CHECK(&pubkey, sizeof(pubkey));
284269
CHECK(secp256k1_ecdsa_sign(sttc, &sig, ctmp, ctmp, NULL, NULL) == 0);
285270
CHECK(ecount == 2);
286271
VG_UNDEF(&sig, sizeof(sig));
287-
CHECK(secp256k1_ecdsa_sign(sign, &sig, ctmp, ctmp, NULL, NULL) == 1);
272+
CHECK(secp256k1_ecdsa_sign(ctx, &sig, ctmp, ctmp, NULL, NULL) == 1);
288273
VG_CHECK(&sig, sizeof(sig));
289274
CHECK(ecount2 == 10);
290-
CHECK(secp256k1_ecdsa_verify(sign, &sig, ctmp, &pubkey) == 1);
275+
CHECK(secp256k1_ecdsa_verify(ctx, &sig, ctmp, &pubkey) == 1);
291276
CHECK(ecount2 == 10);
292277
CHECK(secp256k1_ecdsa_verify(sttc, &sig, ctmp, &pubkey) == 1);
293278
CHECK(ecount == 2);
294-
CHECK(secp256k1_ec_pubkey_tweak_add(sign, &pubkey, ctmp) == 1);
279+
CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp) == 1);
295280
CHECK(ecount2 == 10);
296281
CHECK(secp256k1_ec_pubkey_tweak_add(sttc, &pubkey, ctmp) == 1);
297282
CHECK(ecount == 2);
298-
CHECK(secp256k1_ec_pubkey_tweak_mul(sign, &pubkey, ctmp) == 1);
283+
CHECK(secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey, ctmp) == 1);
299284
CHECK(ecount2 == 10);
300285
CHECK(secp256k1_ec_pubkey_negate(sttc, &pubkey) == 1);
301286
CHECK(ecount == 2);
302-
CHECK(secp256k1_ec_pubkey_negate(sign, &pubkey) == 1);
287+
CHECK(secp256k1_ec_pubkey_negate(ctx, &pubkey) == 1);
303288
CHECK(ecount == 2);
304-
CHECK(secp256k1_ec_pubkey_negate(sign, NULL) == 0);
289+
CHECK(secp256k1_ec_pubkey_negate(ctx, NULL) == 0);
305290
CHECK(ecount2 == 11);
306291
CHECK(secp256k1_ec_pubkey_negate(sttc, &zero_pubkey) == 0);
307292
CHECK(ecount == 3);
@@ -311,49 +296,37 @@ void run_context_tests(int use_prealloc) {
311296
CHECK(ecount == 3);
312297
CHECK(secp256k1_context_randomize(sttc, NULL) == 1);
313298
CHECK(ecount == 3);
314-
CHECK(secp256k1_context_randomize(sign, ctmp) == 1);
299+
CHECK(secp256k1_context_randomize(ctx, ctmp) == 1);
315300
CHECK(ecount2 == 11);
316-
CHECK(secp256k1_context_randomize(sign, NULL) == 1);
301+
CHECK(secp256k1_context_randomize(ctx, NULL) == 1);
317302
CHECK(ecount2 == 11);
318303
secp256k1_context_set_illegal_callback(sttc, NULL, NULL);
319-
secp256k1_context_set_illegal_callback(sign, NULL, NULL);
304+
secp256k1_context_set_illegal_callback(ctx, NULL, NULL);
320305

321306
/* obtain a working nonce */
322307
do {
323308
random_scalar_order_test(&nonce);
324-
} while(!secp256k1_ecdsa_sig_sign(&both->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL));
309+
} while(!secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL));
325310

326311
/* try signing */
327-
CHECK(secp256k1_ecdsa_sig_sign(&sign->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL));
328-
CHECK(secp256k1_ecdsa_sig_sign(&both->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL));
312+
CHECK(secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL));
329313

330314
/* try verifying */
331315
CHECK(secp256k1_ecdsa_sig_verify(&sigr, &sigs, &pub, &msg));
332-
CHECK(secp256k1_ecdsa_sig_verify(&sigr, &sigs, &pub, &msg));
333316

334317
/* cleanup */
335318
if (use_prealloc) {
336-
secp256k1_context_preallocated_destroy(none);
337-
secp256k1_context_preallocated_destroy(sign);
338-
secp256k1_context_preallocated_destroy(vrfy);
339-
secp256k1_context_preallocated_destroy(both);
319+
secp256k1_context_preallocated_destroy(ctx);
340320
secp256k1_context_preallocated_destroy(sttc);
341-
free(none_prealloc);
342-
free(sign_prealloc);
343-
free(vrfy_prealloc);
344-
free(both_prealloc);
321+
free(ctx_prealloc);
345322
free(sttc_prealloc);
346323
} else {
347-
secp256k1_context_destroy(none);
348-
secp256k1_context_destroy(sign);
349-
secp256k1_context_destroy(vrfy);
350-
secp256k1_context_destroy(both);
324+
secp256k1_context_destroy(ctx);
351325
secp256k1_context_destroy(sttc);
352326
}
353327
/* Defined as no-op. */
354328
secp256k1_context_destroy(NULL);
355329
secp256k1_context_preallocated_destroy(NULL);
356-
357330
}
358331

359332
void run_scratch_tests(void) {

0 commit comments

Comments
 (0)