Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 83fb087

Browse files
committedJun 16, 2019
Add function to compute optimal ecmult_multi scratch space size for a given number of points
1 parent 2b3e262 commit 83fb087

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed
 

‎src/ecmult_impl.h

+16
Original file line numberDiff line numberDiff line change
@@ -1323,4 +1323,20 @@ static int secp256k1_ecmult_multi_var(const secp256k1_callback* error_callback,
13231323
return 1;
13241324
}
13251325

1326+
/**
1327+
* Returns the optimal scratch space size for a given number of points
1328+
* excluding base point G.
1329+
*/
1330+
static size_t secp256k1_ecmult_multi_scratch_size(size_t n_points) {
1331+
if (n_points > ECMULT_MAX_POINTS_PER_BATCH) {
1332+
n_points = ECMULT_MAX_POINTS_PER_BATCH;
1333+
}
1334+
if (n_points >= ECMULT_PIPPENGER_THRESHOLD) {
1335+
int bucket_window = secp256k1_pippenger_bucket_window(n_points);
1336+
return secp256k1_pippenger_scratch_size(n_points, bucket_window);
1337+
} else {
1338+
return secp256k1_strauss_scratch_size(n_points);
1339+
}
1340+
}
1341+
13261342
#endif /* SECP256K1_ECMULT_IMPL_H */

‎src/tests.c

+12-3
Original file line numberDiff line numberDiff line change
@@ -3154,11 +3154,10 @@ void test_ecmult_multi_batching(void) {
31543154

31553155
for(i = 1; i <= n_points; i++) {
31563156
if (i >= ECMULT_PIPPENGER_THRESHOLD) {
3157-
int bucket_window = secp256k1_pippenger_bucket_window(i);
3158-
size_t scratch_size = secp256k1_pippenger_scratch_size(i, bucket_window);
3157+
size_t scratch_size = secp256k1_ecmult_multi_scratch_size(i);
31593158
scratch = secp256k1_scratch_create(&ctx->error_callback, scratch_size);
31603159
} else {
3161-
size_t scratch_size = secp256k1_strauss_scratch_size(i);
3160+
size_t scratch_size = secp256k1_ecmult_multi_scratch_size(i);
31623161
scratch = secp256k1_scratch_create(&ctx->error_callback, scratch_size);
31633162
}
31643163
CHECK(secp256k1_ecmult_multi_var(&ctx->error_callback, &ctx->ecmult_ctx, scratch, &r, &scG, ecmult_multi_callback, &data, n_points));
@@ -3170,6 +3169,15 @@ void test_ecmult_multi_batching(void) {
31703169
free(pt);
31713170
}
31723171

3172+
void test_ecmult_multi_scratch_size(void) {
3173+
/* test ECMULT_MAX_POINTS_PER_BATCH limit */
3174+
size_t n_points = ECMULT_MAX_POINTS_PER_BATCH + 1;
3175+
size_t scratch_size = secp256k1_ecmult_multi_scratch_size(n_points);
3176+
int expected_bucket_window = secp256k1_pippenger_bucket_window(n_points - 1);
3177+
size_t expected_scratch_size = secp256k1_pippenger_scratch_size(n_points - 1, expected_bucket_window);
3178+
CHECK(expected_scratch_size == scratch_size);
3179+
}
3180+
31733181
void run_ecmult_multi_tests(void) {
31743182
secp256k1_scratch *scratch;
31753183

@@ -3193,6 +3201,7 @@ void run_ecmult_multi_tests(void) {
31933201

31943202
test_ecmult_multi_batch_size_helper();
31953203
test_ecmult_multi_batching();
3204+
test_ecmult_multi_scratch_size();
31963205
}
31973206

31983207
void test_wnaf(const secp256k1_scalar *number, int w) {

0 commit comments

Comments
 (0)
Please sign in to comment.