3030# endif
3131#else
3232/* optimal for 128-bit and 256-bit exponents. */
33- #define WINDOW_A 5
34- /** larger numbers may result in slightly better performance, at the cost of
35- exponentially larger precomputed tables. */
36- #ifdef USE_ENDOMORPHISM
37- /** Two tables for window size 15: 1.375 MiB. */
38- #define WINDOW_G 15
39- #else
40- /** One table for window size 16: 1.375 MiB. */
41- #define WINDOW_G 16
33+ # define WINDOW_A 5
34+ /** Larger values for ECMULT_WINDOW_SIZE result in possibly better
35+ * performance at the cost of an exponentially larger precomputed
36+ * table. The exact table size is
37+ * (1 << (WINDOW_G - 2)) * sizeof(secp256k1_ge_storage) bytes,
38+ * where sizeof(secp256k1_ge_storage) is typically 64 bytes but can
39+ * be larger due to platform-specific padding and alignment.
40+ * If the endomorphism optimization is enabled (USE_ENDOMORMPHSIM)
41+ * two tables of this size are used instead of only one.
42+ */
43+ # define WINDOW_G ECMULT_WINDOW_SIZE
4244#endif
45+
46+ /* Noone will ever need more than a window size of 24. The code might
47+ * be correct for larger values of ECMULT_WINDOW_SIZE but this is not
48+ * not tested.
49+ *
50+ * The following limitations are known, and there are probably more:
51+ * If WINDOW_G > 27 and size_t has 32 bits, then the code is incorrect
52+ * because the size of the memory object that we allocate (in bytes)
53+ * will not fit in a size_t.
54+ * If WINDOW_G > 31 and int has 32 bits, then the code is incorrect
55+ * because certain expressions will overflow.
56+ */
57+ #if ECMULT_WINDOW_SIZE < 2 || ECMULT_WINDOW_SIZE > 24
58+ # error Set ECMULT_WINDOW_SIZE to an integer in range [2..24].
4359#endif
4460
4561#ifdef USE_ENDOMORPHISM
@@ -311,7 +327,12 @@ static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, const
311327 /* get the generator */
312328 secp256k1_gej_set_ge (& gj , & secp256k1_ge_const_g );
313329
314- ctx -> pre_g = (secp256k1_ge_storage (* )[])checked_malloc (cb , sizeof ((* ctx -> pre_g )[0 ]) * ECMULT_TABLE_SIZE (WINDOW_G ));
330+ {
331+ size_t size = sizeof ((* ctx -> pre_g )[0 ]) * ((size_t )ECMULT_TABLE_SIZE (WINDOW_G ));
332+ /* check for overflow */
333+ VERIFY_CHECK (size / sizeof ((* ctx -> pre_g )[0 ]) == ((size_t )ECMULT_TABLE_SIZE (WINDOW_G )));
334+ ctx -> pre_g = (secp256k1_ge_storage (* )[])checked_malloc (cb , size );
335+ }
315336
316337 /* precompute the tables with odd multiples */
317338 secp256k1_ecmult_odd_multiples_table_storage_var (ECMULT_TABLE_SIZE (WINDOW_G ), * ctx -> pre_g , & gj );
@@ -321,7 +342,10 @@ static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, const
321342 secp256k1_gej g_128j ;
322343 int i ;
323344
324- ctx -> pre_g_128 = (secp256k1_ge_storage (* )[])checked_malloc (cb , sizeof ((* ctx -> pre_g_128 )[0 ]) * ECMULT_TABLE_SIZE (WINDOW_G ));
345+ size_t size = sizeof ((* ctx -> pre_g_128 )[0 ]) * ((size_t ) ECMULT_TABLE_SIZE (WINDOW_G ));
346+ /* check for overflow */
347+ VERIFY_CHECK (size / sizeof ((* ctx -> pre_g_128 )[0 ]) == ((size_t )ECMULT_TABLE_SIZE (WINDOW_G )));
348+ ctx -> pre_g_128 = (secp256k1_ge_storage (* )[])checked_malloc (cb , size );
325349
326350 /* calculate 2^128*generator */
327351 g_128j = gj ;
@@ -338,15 +362,15 @@ static void secp256k1_ecmult_context_clone(secp256k1_ecmult_context *dst,
338362 if (src -> pre_g == NULL ) {
339363 dst -> pre_g = NULL ;
340364 } else {
341- size_t size = sizeof ((* dst -> pre_g )[0 ]) * ECMULT_TABLE_SIZE (WINDOW_G );
365+ size_t size = sizeof ((* dst -> pre_g )[0 ]) * (( size_t ) ECMULT_TABLE_SIZE (WINDOW_G ) );
342366 dst -> pre_g = (secp256k1_ge_storage (* )[])checked_malloc (cb , size );
343367 memcpy (dst -> pre_g , src -> pre_g , size );
344368 }
345369#ifdef USE_ENDOMORPHISM
346370 if (src -> pre_g_128 == NULL ) {
347371 dst -> pre_g_128 = NULL ;
348372 } else {
349- size_t size = sizeof ((* dst -> pre_g_128 )[0 ]) * ECMULT_TABLE_SIZE (WINDOW_G );
373+ size_t size = sizeof ((* dst -> pre_g_128 )[0 ]) * (( size_t ) ECMULT_TABLE_SIZE (WINDOW_G ) );
350374 dst -> pre_g_128 = (secp256k1_ge_storage (* )[])checked_malloc (cb , size );
351375 memcpy (dst -> pre_g_128 , src -> pre_g_128 , size );
352376 }
0 commit comments