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 8e68782

Browse files
committedNov 6, 2021
ecmult: add function to compute optimal scratch space size
1 parent e1b1e04 commit 8e68782

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed
 

‎src/ecmult_impl.h

+16
Original file line numberDiff line numberDiff line change
@@ -975,4 +975,20 @@ static int secp256k1_ecmult_multi_var(const secp256k1_callback* error_callback,
975975
return 1;
976976
}
977977

978+
/**
979+
* Returns the optimal scratch space size for a given number of points
980+
* excluding base point G.
981+
*/
982+
static size_t secp256k1_ecmult_multi_scratch_size(size_t n_points) {
983+
if (n_points > ECMULT_MAX_POINTS_PER_BATCH) {
984+
n_points = ECMULT_MAX_POINTS_PER_BATCH;
985+
}
986+
if (n_points >= ECMULT_PIPPENGER_THRESHOLD) {
987+
int bucket_window = secp256k1_pippenger_bucket_window(n_points);
988+
return secp256k1_pippenger_scratch_size(n_points, bucket_window);
989+
} else {
990+
return secp256k1_strauss_scratch_size(n_points);
991+
}
992+
}
993+
978994
#endif /* SECP256K1_ECMULT_IMPL_H */

‎src/tests.c

+12-8
Original file line numberDiff line numberDiff line change
@@ -4271,14 +4271,8 @@ void test_ecmult_multi_batching(void) {
42714271
secp256k1_scratch_destroy(&ctx->error_callback, scratch);
42724272

42734273
for(i = 1; i <= n_points; i++) {
4274-
if (i >= ECMULT_PIPPENGER_THRESHOLD) {
4275-
int bucket_window = secp256k1_pippenger_bucket_window(i);
4276-
size_t scratch_size = secp256k1_pippenger_scratch_size(i, bucket_window);
4277-
scratch = secp256k1_scratch_create(&ctx->error_callback, scratch_size);
4278-
} else {
4279-
size_t scratch_size = secp256k1_strauss_scratch_size(i);
4280-
scratch = secp256k1_scratch_create(&ctx->error_callback, scratch_size);
4281-
}
4274+
size_t scratch_size = secp256k1_ecmult_multi_scratch_size(i);
4275+
scratch = secp256k1_scratch_create(&ctx->error_callback, scratch_size);
42824276
CHECK(secp256k1_ecmult_multi_var(&ctx->error_callback, scratch, &r, &scG, ecmult_multi_callback, &data, n_points));
42834277
secp256k1_gej_add_var(&r, &r, &r2, NULL);
42844278
CHECK(secp256k1_gej_is_infinity(&r));
@@ -4288,6 +4282,15 @@ void test_ecmult_multi_batching(void) {
42884282
free(pt);
42894283
}
42904284

4285+
void test_ecmult_multi_scratch_size(void) {
4286+
/* test ECMULT_MAX_POINTS_PER_BATCH limit */
4287+
size_t n_points = ECMULT_MAX_POINTS_PER_BATCH + 1;
4288+
size_t scratch_size = secp256k1_ecmult_multi_scratch_size(n_points);
4289+
int expected_bucket_window = secp256k1_pippenger_bucket_window(n_points - 1);
4290+
size_t expected_scratch_size = secp256k1_pippenger_scratch_size(n_points - 1, expected_bucket_window);
4291+
CHECK(expected_scratch_size == scratch_size);
4292+
}
4293+
42914294
void run_ecmult_multi_tests(void) {
42924295
secp256k1_scratch *scratch;
42934296

@@ -4311,6 +4314,7 @@ void run_ecmult_multi_tests(void) {
43114314

43124315
test_ecmult_multi_batch_size_helper();
43134316
test_ecmult_multi_batching();
4317+
test_ecmult_multi_scratch_size();
43144318
}
43154319

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

0 commit comments

Comments
 (0)
Please sign in to comment.