30
30
# endif
31
31
#else
32
32
/* 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
42
44
#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].
43
59
#endif
44
60
45
61
#ifdef USE_ENDOMORPHISM
@@ -311,7 +327,12 @@ static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, const
311
327
/* get the generator */
312
328
secp256k1_gej_set_ge (& gj , & secp256k1_ge_const_g );
313
329
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
+ }
315
336
316
337
/* precompute the tables with odd multiples */
317
338
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
321
342
secp256k1_gej g_128j ;
322
343
int i ;
323
344
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 );
325
349
326
350
/* calculate 2^128*generator */
327
351
g_128j = gj ;
@@ -338,15 +362,15 @@ static void secp256k1_ecmult_context_clone(secp256k1_ecmult_context *dst,
338
362
if (src -> pre_g == NULL ) {
339
363
dst -> pre_g = NULL ;
340
364
} 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 ) );
342
366
dst -> pre_g = (secp256k1_ge_storage (* )[])checked_malloc (cb , size );
343
367
memcpy (dst -> pre_g , src -> pre_g , size );
344
368
}
345
369
#ifdef USE_ENDOMORPHISM
346
370
if (src -> pre_g_128 == NULL ) {
347
371
dst -> pre_g_128 = NULL ;
348
372
} 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 ) );
350
374
dst -> pre_g_128 = (secp256k1_ge_storage (* )[])checked_malloc (cb , size );
351
375
memcpy (dst -> pre_g_128 , src -> pre_g_128 , size );
352
376
}
0 commit comments