@@ -1111,12 +1111,31 @@ static int secp256k1_ecmult_multi_simple_var(const secp256k1_ecmult_context *ctx
11111111 return 1 ;
11121112}
11131113
1114+ /* Compute the number of batches and the batch size given the maximum batch size and the
1115+ * total number of points */
1116+ static int secp256k1_ecmult_multi_batch_size_helper (size_t * n_batches , size_t * n_batch_points , size_t max_n_batch_points , size_t n ) {
1117+ if (max_n_batch_points == 0 ) {
1118+ return 0 ;
1119+ }
1120+ if (max_n_batch_points > ECMULT_MAX_POINTS_PER_BATCH ) {
1121+ max_n_batch_points = ECMULT_MAX_POINTS_PER_BATCH ;
1122+ }
1123+ if (n == 0 ) {
1124+ * n_batches = 0 ;
1125+ * n_batch_points = 0 ;
1126+ return 1 ;
1127+ }
1128+ /* Compute ceil(n/max_n_batch_points) and ceil(n/n_batches) */
1129+ * n_batches = 1 + (n - 1 ) / max_n_batch_points ;
1130+ * n_batch_points = 1 + (n - 1 ) / * n_batches ;
1131+ return 1 ;
1132+ }
1133+
11141134typedef int (* secp256k1_ecmult_multi_func )(const secp256k1_ecmult_context * , secp256k1_scratch * , secp256k1_gej * , const secp256k1_scalar * , secp256k1_ecmult_multi_callback cb , void * , size_t );
11151135static 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 ) {
11161136 size_t i ;
11171137
11181138 int (* f )(const secp256k1_ecmult_context * , secp256k1_scratch * , secp256k1_gej * , const secp256k1_scalar * , secp256k1_ecmult_multi_callback cb , void * , size_t , size_t );
1119- size_t max_points ;
11201139 size_t n_batches ;
11211140 size_t n_batch_points ;
11221141
@@ -1133,24 +1152,17 @@ static int secp256k1_ecmult_multi_var(const secp256k1_ecmult_context *ctx, secp2
11331152 return secp256k1_ecmult_multi_simple_var (ctx , r , inp_g_sc , cb , cbdata , n );
11341153 }
11351154
1136- max_points = secp256k1_pippenger_max_points (scratch );
1137- if (max_points == 0 ) {
1155+ /* Compute the batch sizes for pippenger given a scratch space. If it's greater than a threshold
1156+ * use pippenger. Otherwise use strauss */
1157+ if (!secp256k1_ecmult_multi_batch_size_helper (& n_batches , & n_batch_points , secp256k1_pippenger_max_points (scratch ), n )) {
11381158 return 0 ;
1139- } else if (max_points > ECMULT_MAX_POINTS_PER_BATCH ) {
1140- max_points = ECMULT_MAX_POINTS_PER_BATCH ;
11411159 }
1142- n_batches = (n + max_points - 1 )/max_points ;
1143- n_batch_points = (n + n_batches - 1 )/n_batches ;
1144-
11451160 if (n_batch_points >= ECMULT_PIPPENGER_THRESHOLD ) {
11461161 f = secp256k1_ecmult_pippenger_batch ;
11471162 } else {
1148- max_points = secp256k1_strauss_max_points (scratch );
1149- if (max_points == 0 ) {
1163+ if (!secp256k1_ecmult_multi_batch_size_helper (& n_batches , & n_batch_points , secp256k1_strauss_max_points (scratch ), n )) {
11501164 return 0 ;
11511165 }
1152- n_batches = (n + max_points - 1 )/max_points ;
1153- n_batch_points = (n + n_batches - 1 )/n_batches ;
11541166 f = secp256k1_ecmult_strauss_batch ;
11551167 }
11561168 for (i = 0 ; i < n_batches ; i ++ ) {
0 commit comments