Skip to content

Commit 5336791

Browse files
committed
fixup! fix tests by plugging memory leaks
1 parent 8874315 commit 5336791

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

src/modules/bppp/bppp_rangeproof_impl.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ static int secp256k1_bppp_rangeproof_verify_impl(
784784
secp256k1_scalar e, q_sqrt, q, q_inv, x, y, t;
785785
/* To be re-used as c_vec later */
786786
secp256k1_scalar t_pows[8];
787+
int res;
787788

788789
/* Check proof sizes*/
789790
if (proof_len != 33 * 4 + (65 * n_rounds) + 64) {
@@ -936,6 +937,7 @@ static int secp256k1_bppp_rangeproof_verify_impl(
936937
num_points = 6 + g_offset;
937938

938939
if (!secp256k1_ecmult_multi_var(&ctx->error_callback, scratch, &c_commj, NULL, secp256k1_bppp_verify_cb, (void*) &cb_data, num_points)) {
940+
secp256k1_scratch_apply_checkpoint(&ctx->error_callback, scratch, scratch_checkpoint);
939941
return 0;
940942
}
941943

@@ -949,7 +951,7 @@ static int secp256k1_bppp_rangeproof_verify_impl(
949951
secp256k1_scalar_clear(&t_pows[6]);
950952
secp256k1_scalar_clear(&t_pows[7]);
951953

952-
return secp256k1_bppp_rangeproof_norm_product_verify(
954+
res = secp256k1_bppp_rangeproof_norm_product_verify(
953955
ctx,
954956
scratch,
955957
&proof[33*4],
@@ -964,6 +966,8 @@ static int secp256k1_bppp_rangeproof_verify_impl(
964966
&c_comm
965967
);
966968
}
969+
secp256k1_scratch_apply_checkpoint(&ctx->error_callback, scratch, scratch_checkpoint);
970+
return res;
967971
}
968972

969973
#endif

src/modules/bppp/tests_impl.h

+14-14
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ static void test_bppp_generators_api(void) {
9090
}
9191

9292
static void test_bppp_generators_fixed(void) {
93-
secp256k1_context* secp_ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
94-
secp256k1_bppp_generators *gens = secp256k1_bppp_generators_create(secp_ctx, 11);
93+
secp256k1_bppp_generators *gens = secp256k1_bppp_generators_create(ctx, 11);
9594
unsigned char gens_ser[330];
9695
const unsigned char fixed_first_11[330] = {
9796
0x0b, 0xb3, 0x4d, 0x5f, 0xa6, 0xb8, 0xf3, 0xd1, 0x38, 0x49, 0xce, 0x51, 0x91, 0xb7, 0xf6, 0x76, 0x18, 0xfe, 0x5b, 0xd1, 0x2a, 0x88, 0xb2, 0x0e, 0xac, 0x33, 0x89, 0x45, 0x66, 0x7f, 0xb3, 0x30, 0x56,
@@ -108,14 +107,14 @@ static void test_bppp_generators_fixed(void) {
108107
size_t len;
109108

110109
len = 330;
111-
CHECK(secp256k1_bppp_generators_serialize(secp_ctx, gens, gens_ser, &len));
110+
CHECK(secp256k1_bppp_generators_serialize(ctx, gens, gens_ser, &len));
112111
CHECK(memcmp(gens_ser, fixed_first_11, sizeof(fixed_first_11)) == 0);
113112

114113
len = sizeof(gens_ser);
115-
CHECK(secp256k1_bppp_generators_serialize(secp_ctx, gens, gens_ser, &len));
114+
CHECK(secp256k1_bppp_generators_serialize(ctx, gens, gens_ser, &len));
116115
CHECK(memcmp(gens_ser, fixed_first_11, sizeof(fixed_first_11)) == 0);
117116

118-
secp256k1_bppp_generators_destroy(secp_ctx, gens);
117+
secp256k1_bppp_generators_destroy(ctx, gens);
119118
}
120119

121120
static void test_bppp_tagged_hash(void) {
@@ -559,39 +558,40 @@ void rangeproof_test(size_t digit_base, size_t num_bits, uint64_t value, uint64_
559558
size_t n = num_digits > digit_base ? num_digits : digit_base;
560559
size_t res;
561560
secp256k1_pedersen_commitment commit;
562-
secp256k1_context* secp_ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
563561
const unsigned char blind[32] = "help me! i'm bliiiiiiiiiiiiiiind";
564562
const unsigned char nonce[32] = "nonce? non ce n'est vrai amirite";
565563
/* Extra commit is a Joan Shelley lyric */
566564
const unsigned char extra_commit[] = "Shock of teal blue beneath clouds gathering, and the light of empty black on the waves at the horizon";
567565
const size_t extra_commit_len = sizeof(extra_commit);
568566
secp256k1_sha256 transcript;
569-
const secp256k1_bppp_generators *gs = secp256k1_bppp_generators_create(secp_ctx, n + 8);
570-
secp256k1_scratch *scratch = secp256k1_scratch_space_create(secp_ctx, 1000*1000); /* shouldn't need much */
567+
secp256k1_bppp_generators *gs = secp256k1_bppp_generators_create(ctx, n + 8);
568+
secp256k1_scratch *scratch = secp256k1_scratch_space_create(ctx, 1000*1000); /* shouldn't need much */
571569
unsigned char proof[1000];
572570
plen = 1000;
573571
asset_genp = *secp256k1_generator_h;
574-
CHECK(secp256k1_pedersen_commit(secp_ctx, &commit, blind, value, &asset_genp));
575-
secp256k1_bppp_generators_serialize(secp_ctx, gs, proof, &plen);
572+
CHECK(secp256k1_pedersen_commit(ctx, &commit, blind, value, &asset_genp));
573+
secp256k1_bppp_generators_serialize(ctx, gs, proof, &plen);
576574
plen = 1000;
577575
secp256k1_sha256_initialize(&transcript);
578576

579577

580-
res = secp256k1_bppp_rangeproof_prove(secp_ctx, scratch, gs, &asset_genp, proof, &plen, num_bits, digit_base, value, min_value, &commit, blind, nonce, extra_commit, extra_commit_len);
578+
res = secp256k1_bppp_rangeproof_prove(ctx, scratch, gs, &asset_genp, proof, &plen, num_bits, digit_base, value, min_value, &commit, blind, nonce, extra_commit, extra_commit_len);
581579
CHECK(res == 1);
582580

583-
res = secp256k1_bppp_rangeproof_verify(secp_ctx, scratch, gs, &asset_genp, proof, plen, num_bits, digit_base, min_value, &commit, extra_commit, extra_commit_len);
581+
res = secp256k1_bppp_rangeproof_verify(ctx, scratch, gs, &asset_genp, proof, plen, num_bits, digit_base, min_value, &commit, extra_commit, extra_commit_len);
584582
CHECK(res == 1);
585583

586584
proof[plen - 1] ^= 1;
587-
res = secp256k1_bppp_rangeproof_verify(secp_ctx, scratch, gs, &asset_genp, proof, plen, num_bits, digit_base, min_value, &commit, extra_commit, extra_commit_len);
585+
res = secp256k1_bppp_rangeproof_verify(ctx, scratch, gs, &asset_genp, proof, plen, num_bits, digit_base, min_value, &commit, extra_commit, extra_commit_len);
588586
CHECK(res == 0);
587+
588+
secp256k1_scratch_space_destroy(ctx, scratch);
589+
secp256k1_bppp_generators_destroy(ctx, gs);
589590
}
590591

591592
void run_bppp_tests(void) {
592593
/* Update the global context for all bppp tests*/
593594
size_t i;
594-
ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
595595
test_log_exp();
596596
test_norm_util_helpers();
597597
test_bppp_generators_api();

0 commit comments

Comments
 (0)