Skip to content

Commit c2b028a

Browse files
committed
scratch space: thread error_callback into all scratch space functions
Use it when checking magic bytes
1 parent 0be1a4a commit c2b028a

File tree

9 files changed

+130
-104
lines changed

9 files changed

+130
-104
lines changed

include/secp256k1.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,13 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT secp256k1_scratch_space* secp256k1_sc
295295
/** Destroy a secp256k1 scratch space.
296296
*
297297
* The pointer may not be used afterwards.
298-
* Args: scratch: space to destroy
298+
* Args: ctx: a secp256k1 context object.
299+
* scratch: space to destroy
299300
*/
300301
SECP256K1_API void secp256k1_scratch_space_destroy(
302+
const secp256k1_context* ctx,
301303
secp256k1_scratch_space* scratch
302-
);
304+
) SECP256K1_ARG_NONNULL(1);
303305

304306
/** Parse a variable-length public key into the pubkey object.
305307
*

src/bench_ecmult.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static void bench_ecmult(void* arg) {
6464
size_t iter;
6565

6666
for (iter = 0; iter < iters; ++iter) {
67-
data->ecmult_multi(&data->ctx->ecmult_ctx, data->scratch, &data->output[iter], data->includes_g ? &data->scalars[data->offset1] : NULL, bench_callback, arg, count - includes_g);
67+
data->ecmult_multi(&data->ctx->error_callback, &data->ctx->ecmult_ctx, data->scratch, &data->output[iter], data->includes_g ? &data->scalars[data->offset1] : NULL, bench_callback, arg, count - includes_g);
6868
data->offset1 = (data->offset1 + count) % POINTS;
6969
data->offset2 = (data->offset2 + count - 1) % POINTS;
7070
}
@@ -154,7 +154,7 @@ int main(int argc, char **argv) {
154154
} else if(have_flag(argc, argv, "simple")) {
155155
printf("Using simple algorithm:\n");
156156
data.ecmult_multi = secp256k1_ecmult_multi_var;
157-
secp256k1_scratch_space_destroy(data.scratch);
157+
secp256k1_scratch_space_destroy(data.ctx, data.scratch);
158158
data.scratch = NULL;
159159
} else {
160160
fprintf(stderr, "%s: unrecognized argument '%s'.\n", argv[0], argv[1]);
@@ -193,10 +193,10 @@ int main(int argc, char **argv) {
193193
run_test(&data, i << p, 1);
194194
}
195195
}
196-
secp256k1_context_destroy(data.ctx);
197196
if (data.scratch != NULL) {
198-
secp256k1_scratch_space_destroy(data.scratch);
197+
secp256k1_scratch_space_destroy(data.ctx, data.scratch);
199198
}
199+
secp256k1_context_destroy(data.ctx);
200200
free(data.scalars);
201201
free(data.pubkeys);
202202
free(data.seckeys);

src/ecmult.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ typedef int (secp256k1_ecmult_multi_callback)(secp256k1_scalar *sc, secp256k1_ge
4343
* 0 if there is not enough scratch space for a single point or
4444
* callback returns 0
4545
*/
46-
static int secp256k1_ecmult_multi_var(const secp256k1_ecmult_context *ctx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n);
46+
static int secp256k1_ecmult_multi_var(const secp256k1_callback* error_callback, const secp256k1_ecmult_context *ctx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n);
4747

4848
#endif /* SECP256K1_ECMULT_H */

src/ecmult_impl.h

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ static size_t secp256k1_strauss_scratch_size(size_t n_points) {
648648
return n_points*point_size;
649649
}
650650

651-
static int secp256k1_ecmult_strauss_batch(const secp256k1_ecmult_context *ctx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n_points, size_t cb_offset) {
651+
static int secp256k1_ecmult_strauss_batch(const secp256k1_callback* error_callback, const secp256k1_ecmult_context *ctx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n_points, size_t cb_offset) {
652652
secp256k1_gej* points;
653653
secp256k1_scalar* scalars;
654654
struct secp256k1_strauss_state state;
@@ -659,41 +659,41 @@ static int secp256k1_ecmult_strauss_batch(const secp256k1_ecmult_context *ctx, s
659659
return 1;
660660
}
661661

662-
if (!secp256k1_scratch_allocate_frame(scratch, secp256k1_strauss_scratch_size(n_points), STRAUSS_SCRATCH_OBJECTS)) {
662+
if (!secp256k1_scratch_allocate_frame(error_callback, scratch, secp256k1_strauss_scratch_size(n_points), STRAUSS_SCRATCH_OBJECTS)) {
663663
return 0;
664664
}
665-
points = (secp256k1_gej*)secp256k1_scratch_alloc(scratch, n_points * sizeof(secp256k1_gej));
666-
scalars = (secp256k1_scalar*)secp256k1_scratch_alloc(scratch, n_points * sizeof(secp256k1_scalar));
667-
state.prej = (secp256k1_gej*)secp256k1_scratch_alloc(scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_gej));
668-
state.zr = (secp256k1_fe*)secp256k1_scratch_alloc(scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_fe));
665+
points = (secp256k1_gej*)secp256k1_scratch_alloc(error_callback, scratch, n_points * sizeof(secp256k1_gej));
666+
scalars = (secp256k1_scalar*)secp256k1_scratch_alloc(error_callback, scratch, n_points * sizeof(secp256k1_scalar));
667+
state.prej = (secp256k1_gej*)secp256k1_scratch_alloc(error_callback, scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_gej));
668+
state.zr = (secp256k1_fe*)secp256k1_scratch_alloc(error_callback, scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_fe));
669669
#ifdef USE_ENDOMORPHISM
670-
state.pre_a = (secp256k1_ge*)secp256k1_scratch_alloc(scratch, n_points * 2 * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge));
670+
state.pre_a = (secp256k1_ge*)secp256k1_scratch_alloc(error_callback, scratch, n_points * 2 * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge));
671671
state.pre_a_lam = state.pre_a + n_points * ECMULT_TABLE_SIZE(WINDOW_A);
672672
#else
673-
state.pre_a = (secp256k1_ge*)secp256k1_scratch_alloc(scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge));
673+
state.pre_a = (secp256k1_ge*)secp256k1_scratch_alloc(error_callback, scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge));
674674
#endif
675-
state.ps = (struct secp256k1_strauss_point_state*)secp256k1_scratch_alloc(scratch, n_points * sizeof(struct secp256k1_strauss_point_state));
675+
state.ps = (struct secp256k1_strauss_point_state*)secp256k1_scratch_alloc(error_callback, scratch, n_points * sizeof(struct secp256k1_strauss_point_state));
676676

677677
for (i = 0; i < n_points; i++) {
678678
secp256k1_ge point;
679679
if (!cb(&scalars[i], &point, i+cb_offset, cbdata)) {
680-
secp256k1_scratch_deallocate_frame(scratch);
680+
secp256k1_scratch_deallocate_frame(error_callback, scratch);
681681
return 0;
682682
}
683683
secp256k1_gej_set_ge(&points[i], &point);
684684
}
685685
secp256k1_ecmult_strauss_wnaf(ctx, &state, r, n_points, points, scalars, inp_g_sc);
686-
secp256k1_scratch_deallocate_frame(scratch);
686+
secp256k1_scratch_deallocate_frame(error_callback, scratch);
687687
return 1;
688688
}
689689

690690
/* Wrapper for secp256k1_ecmult_multi_func interface */
691-
static int secp256k1_ecmult_strauss_batch_single(const secp256k1_ecmult_context *actx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n) {
692-
return secp256k1_ecmult_strauss_batch(actx, scratch, r, inp_g_sc, cb, cbdata, n, 0);
691+
static int secp256k1_ecmult_strauss_batch_single(const secp256k1_callback* error_callback, const secp256k1_ecmult_context *actx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n) {
692+
return secp256k1_ecmult_strauss_batch(error_callback, actx, scratch, r, inp_g_sc, cb, cbdata, n, 0);
693693
}
694694

695-
static size_t secp256k1_strauss_max_points(secp256k1_scratch *scratch) {
696-
return secp256k1_scratch_max_allocation(scratch, STRAUSS_SCRATCH_OBJECTS) / secp256k1_strauss_scratch_size(1);
695+
static size_t secp256k1_strauss_max_points(const secp256k1_callback* error_callback, secp256k1_scratch *scratch) {
696+
return secp256k1_scratch_max_allocation(error_callback, scratch, STRAUSS_SCRATCH_OBJECTS) / secp256k1_strauss_scratch_size(1);
697697
}
698698

699699
/** Convert a number to WNAF notation.
@@ -985,7 +985,7 @@ static size_t secp256k1_pippenger_scratch_size(size_t n_points, int bucket_windo
985985
return (sizeof(secp256k1_gej) << bucket_window) + sizeof(struct secp256k1_pippenger_state) + entries * entry_size;
986986
}
987987

988-
static int secp256k1_ecmult_pippenger_batch(const secp256k1_ecmult_context *ctx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n_points, size_t cb_offset) {
988+
static int secp256k1_ecmult_pippenger_batch(const secp256k1_callback* error_callback, const secp256k1_ecmult_context *ctx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n_points, size_t cb_offset) {
989989
/* Use 2(n+1) with the endomorphism, n+1 without, when calculating batch
990990
* sizes. The reason for +1 is that we add the G scalar to the list of
991991
* other scalars. */
@@ -1010,15 +1010,15 @@ static int secp256k1_ecmult_pippenger_batch(const secp256k1_ecmult_context *ctx,
10101010
}
10111011

10121012
bucket_window = secp256k1_pippenger_bucket_window(n_points);
1013-
if (!secp256k1_scratch_allocate_frame(scratch, secp256k1_pippenger_scratch_size(n_points, bucket_window), PIPPENGER_SCRATCH_OBJECTS)) {
1013+
if (!secp256k1_scratch_allocate_frame(error_callback, scratch, secp256k1_pippenger_scratch_size(n_points, bucket_window), PIPPENGER_SCRATCH_OBJECTS)) {
10141014
return 0;
10151015
}
1016-
points = (secp256k1_ge *) secp256k1_scratch_alloc(scratch, entries * sizeof(*points));
1017-
scalars = (secp256k1_scalar *) secp256k1_scratch_alloc(scratch, entries * sizeof(*scalars));
1018-
state_space = (struct secp256k1_pippenger_state *) secp256k1_scratch_alloc(scratch, sizeof(*state_space));
1019-
state_space->ps = (struct secp256k1_pippenger_point_state *) secp256k1_scratch_alloc(scratch, entries * sizeof(*state_space->ps));
1020-
state_space->wnaf_na = (int *) secp256k1_scratch_alloc(scratch, entries*(WNAF_SIZE(bucket_window+1)) * sizeof(int));
1021-
buckets = (secp256k1_gej *) secp256k1_scratch_alloc(scratch, sizeof(*buckets) << bucket_window);
1016+
points = (secp256k1_ge *) secp256k1_scratch_alloc(error_callback, scratch, entries * sizeof(*points));
1017+
scalars = (secp256k1_scalar *) secp256k1_scratch_alloc(error_callback, scratch, entries * sizeof(*scalars));
1018+
state_space = (struct secp256k1_pippenger_state *) secp256k1_scratch_alloc(error_callback, scratch, sizeof(*state_space));
1019+
state_space->ps = (struct secp256k1_pippenger_point_state *) secp256k1_scratch_alloc(error_callback, scratch, entries * sizeof(*state_space->ps));
1020+
state_space->wnaf_na = (int *) secp256k1_scratch_alloc(error_callback, scratch, entries*(WNAF_SIZE(bucket_window+1)) * sizeof(int));
1021+
buckets = (secp256k1_gej *) secp256k1_scratch_alloc(error_callback, scratch, (1<<bucket_window) * sizeof(*buckets));
10221022

10231023
if (inp_g_sc != NULL) {
10241024
scalars[0] = *inp_g_sc;
@@ -1032,7 +1032,7 @@ static int secp256k1_ecmult_pippenger_batch(const secp256k1_ecmult_context *ctx,
10321032

10331033
while (point_idx < n_points) {
10341034
if (!cb(&scalars[idx], &points[idx], point_idx + cb_offset, cbdata)) {
1035-
secp256k1_scratch_deallocate_frame(scratch);
1035+
secp256k1_scratch_deallocate_frame(error_callback, scratch);
10361036
return 0;
10371037
}
10381038
idx++;
@@ -1056,22 +1056,22 @@ static int secp256k1_ecmult_pippenger_batch(const secp256k1_ecmult_context *ctx,
10561056
for(i = 0; i < 1<<bucket_window; i++) {
10571057
secp256k1_gej_clear(&buckets[i]);
10581058
}
1059-
secp256k1_scratch_deallocate_frame(scratch);
1059+
secp256k1_scratch_deallocate_frame(error_callback, scratch);
10601060
return 1;
10611061
}
10621062

10631063
/* Wrapper for secp256k1_ecmult_multi_func interface */
1064-
static int secp256k1_ecmult_pippenger_batch_single(const secp256k1_ecmult_context *actx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n) {
1065-
return secp256k1_ecmult_pippenger_batch(actx, scratch, r, inp_g_sc, cb, cbdata, n, 0);
1064+
static int secp256k1_ecmult_pippenger_batch_single(const secp256k1_callback* error_callback, const secp256k1_ecmult_context *actx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n) {
1065+
return secp256k1_ecmult_pippenger_batch(error_callback, actx, scratch, r, inp_g_sc, cb, cbdata, n, 0);
10661066
}
10671067

10681068
/**
10691069
* Returns the maximum number of points in addition to G that can be used with
10701070
* a given scratch space. The function ensures that fewer points may also be
10711071
* used.
10721072
*/
1073-
static size_t secp256k1_pippenger_max_points(secp256k1_scratch *scratch) {
1074-
size_t max_alloc = secp256k1_scratch_max_allocation(scratch, PIPPENGER_SCRATCH_OBJECTS);
1073+
static size_t secp256k1_pippenger_max_points(const secp256k1_callback* error_callback, secp256k1_scratch *scratch) {
1074+
size_t max_alloc = secp256k1_scratch_max_allocation(error_callback, scratch, PIPPENGER_SCRATCH_OBJECTS);
10751075
int bucket_window;
10761076
size_t res = 0;
10771077

@@ -1153,11 +1153,11 @@ static int secp256k1_ecmult_multi_batch_size_helper(size_t *n_batches, size_t *n
11531153
return 1;
11541154
}
11551155

1156-
typedef int (*secp256k1_ecmult_multi_func)(const secp256k1_ecmult_context*, secp256k1_scratch*, secp256k1_gej*, const secp256k1_scalar*, secp256k1_ecmult_multi_callback cb, void*, size_t);
1157-
static int secp256k1_ecmult_multi_var(const secp256k1_ecmult_context *ctx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n) {
1156+
typedef int (*secp256k1_ecmult_multi_func)(const secp256k1_callback* error_callback, const secp256k1_ecmult_context*, secp256k1_scratch*, secp256k1_gej*, const secp256k1_scalar*, secp256k1_ecmult_multi_callback cb, void*, size_t);
1157+
static int secp256k1_ecmult_multi_var(const secp256k1_callback* error_callback, const secp256k1_ecmult_context *ctx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n) {
11581158
size_t i;
11591159

1160-
int (*f)(const secp256k1_ecmult_context*, secp256k1_scratch*, secp256k1_gej*, const secp256k1_scalar*, secp256k1_ecmult_multi_callback cb, void*, size_t, size_t);
1160+
int (*f)(const secp256k1_callback* error_callback, const secp256k1_ecmult_context*, secp256k1_scratch*, secp256k1_gej*, const secp256k1_scalar*, secp256k1_ecmult_multi_callback cb, void*, size_t, size_t);
11611161
size_t n_batches;
11621162
size_t n_batch_points;
11631163

@@ -1178,13 +1178,13 @@ static int secp256k1_ecmult_multi_var(const secp256k1_ecmult_context *ctx, secp2
11781178
* a threshold use Pippenger's algorithm. Otherwise use Strauss' algorithm.
11791179
* As a first step check if there's enough space for Pippenger's algo (which requires less space
11801180
* than Strauss' algo) and if not, use the simple algorithm. */
1181-
if (!secp256k1_ecmult_multi_batch_size_helper(&n_batches, &n_batch_points, secp256k1_pippenger_max_points(scratch), n)) {
1181+
if (!secp256k1_ecmult_multi_batch_size_helper(&n_batches, &n_batch_points, secp256k1_pippenger_max_points(error_callback, scratch), n)) {
11821182
return secp256k1_ecmult_multi_simple_var(ctx, r, inp_g_sc, cb, cbdata, n);
11831183
}
11841184
if (n_batch_points >= ECMULT_PIPPENGER_THRESHOLD) {
11851185
f = secp256k1_ecmult_pippenger_batch;
11861186
} else {
1187-
if (!secp256k1_ecmult_multi_batch_size_helper(&n_batches, &n_batch_points, secp256k1_strauss_max_points(scratch), n)) {
1187+
if (!secp256k1_ecmult_multi_batch_size_helper(&n_batches, &n_batch_points, secp256k1_strauss_max_points(error_callback, scratch), n)) {
11881188
return secp256k1_ecmult_multi_simple_var(ctx, r, inp_g_sc, cb, cbdata, n);
11891189
}
11901190
f = secp256k1_ecmult_strauss_batch;
@@ -1193,7 +1193,7 @@ static int secp256k1_ecmult_multi_var(const secp256k1_ecmult_context *ctx, secp2
11931193
size_t nbp = n < n_batch_points ? n : n_batch_points;
11941194
size_t offset = n_batch_points*i;
11951195
secp256k1_gej tmp;
1196-
if (!f(ctx, scratch, &tmp, i == 0 ? inp_g_sc : NULL, cb, cbdata, nbp, offset)) {
1196+
if (!f(error_callback, ctx, scratch, &tmp, i == 0 ? inp_g_sc : NULL, cb, cbdata, nbp, offset)) {
11971197
return 0;
11981198
}
11991199
secp256k1_gej_add_var(r, r, &tmp, NULL);

src/scratch.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ typedef struct secp256k1_scratch_space_struct {
2323

2424
static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t max_size);
2525

26-
static void secp256k1_scratch_destroy(secp256k1_scratch* scratch);
26+
static void secp256k1_scratch_destroy(const secp256k1_callback* error_callback, secp256k1_scratch* scratch);
2727

2828
/** Attempts to allocate a new stack frame with `n` available bytes. Returns 1 on success, 0 on failure */
29-
static int secp256k1_scratch_allocate_frame(secp256k1_scratch* scratch, size_t n, size_t objects);
29+
static int secp256k1_scratch_allocate_frame(const secp256k1_callback* error_callback, secp256k1_scratch* scratch, size_t n, size_t objects);
3030

3131
/** Deallocates a stack frame */
32-
static void secp256k1_scratch_deallocate_frame(secp256k1_scratch* scratch);
32+
static void secp256k1_scratch_deallocate_frame(const secp256k1_callback* error_callback, secp256k1_scratch* scratch);
3333

3434
/** Returns the maximum allocation the scratch space will allow */
35-
static size_t secp256k1_scratch_max_allocation(const secp256k1_scratch* scratch, size_t n_objects);
35+
static size_t secp256k1_scratch_max_allocation(const secp256k1_callback* error_callback, const secp256k1_scratch* scratch, size_t n_objects);
3636

3737
/** Returns a pointer into the most recently allocated frame, or NULL if there is insufficient available space */
38-
static void *secp256k1_scratch_alloc(secp256k1_scratch* scratch, size_t n);
38+
static void *secp256k1_scratch_alloc(const secp256k1_callback* error_callback, secp256k1_scratch* scratch, size_t n);
3939

4040
#endif

0 commit comments

Comments
 (0)