Skip to content

Commit 93c433c

Browse files
Make WINDOW_G configurable
This makes WINDOW_G a configurable value in the range of [3..24]. The upper limit of 24 is a defensive choice. The code is probably correct for values up to 33 but those larger values yield in huge tables (>= 256MiB), which are i) unlikely to be really beneficial in practice and ii) increasingly difficult to test.
1 parent aa15154 commit 93c433c

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

configure.ac

+37
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ AC_ARG_WITH([scalar], [AS_HELP_STRING([--with-scalar=64bit|32bit|auto],
151151
AC_ARG_WITH([asm], [AS_HELP_STRING([--with-asm=x86_64|arm|no|auto]
152152
[Specify assembly optimizations to use. Default is auto (experimental: arm)])],[req_asm=$withval], [req_asm=auto])
153153

154+
# Default is window size 16 (or window size 15 with endomorphism) which needs 1.375 MiB. */
155+
AC_ARG_WITH([ecmult-window], [AS_HELP_STRING([--with-ecmult-window=SIZE|auto],
156+
[window size for ecmult precomputation for verification, specified as integer in range [3..24].]
157+
[Larger values result in possibly better performance at the cost of an exponentially larger precomputed table.]
158+
[The table will store 2^(SIZE-2) * 64 bytes of data but can be larger in memory due]
159+
[to platform-specific padding and alignment. "auto" is a reasonable setting for desktop machines]
160+
[(currently 15 if the endomorphism optimization is disabled and 16 if it is enabled). [default=auto]]
161+
)],
162+
[req_ecmult_window=$withval], [req_ecmult_window=auto])
163+
154164
AC_CHECK_TYPES([__int128])
155165

156166
if test x"$enable_coverage" = x"yes"; then
@@ -387,6 +397,32 @@ case $set_scalar in
387397
;;
388398
esac
389399

400+
#set ecmult window size
401+
if test x"$req_ecmult_window" = x"auto"; then
402+
if test x"$use_endomorphism" = x"yes"; then
403+
set_ecmult_window=16
404+
else
405+
set_ecmult_window=15
406+
fi
407+
else
408+
set_ecmult_window=$req_ecmult_window
409+
fi
410+
411+
error_window_size=['window size for ecmult precomputation not an integer in range [3..24] or "auto"']
412+
case $set_ecmult_window in
413+
''|*[[!0-9]]*)
414+
# no valid integer
415+
AC_MSG_ERROR($error_window_size)
416+
;;
417+
*)
418+
if test "$set_ecmult_window" -lt 3 -o "$set_ecmult_window" -gt 24 ; then
419+
# not in range
420+
AC_MSG_ERROR($error_window_size)
421+
fi
422+
AC_DEFINE_UNQUOTED(ECMULT_WINDOW_SIZE, $set_ecmult_window, [Set window size for ecmult precomputation])
423+
;;
424+
esac
425+
390426
if test x"$use_tests" = x"yes"; then
391427
SECP_OPENSSL_CHECK
392428
if test x"$has_openssl_ec" = x"yes"; then
@@ -516,6 +552,7 @@ echo " asm = $set_asm"
516552
echo " bignum = $set_bignum"
517553
echo " field = $set_field"
518554
echo " scalar = $set_scalar"
555+
echo " ecmult window size = $set_ecmult_window"
519556
echo
520557
echo " CC = $CC"
521558
echo " CFLAGS = $CFLAGS"

src/basic-config.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define USE_SCALAR_INV_BUILTIN 1
2828
#define USE_FIELD_10X26 1
2929
#define USE_SCALAR_8X32 1
30+
#define ECMULT_WINDOW_SIZE 15
3031

3132
#endif /* USE_BASIC_CONFIG */
3233

src/ecmult_impl.h

+20-9
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,27 @@
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+
*/
41+
# ifdef USE_ENDOMORPHISM
42+
# define WINDOW_G ((ECMULT_WINDOW_SIZE)-1)
43+
# else
44+
# define WINDOW_G (ECMULT_WINDOW_SIZE)
45+
# endif
4246
#endif
47+
48+
/* Noone will ever need more than a window size of 24.
49+
* (The code probably works with window sizes up to 33 but
50+
* it is not tested for it. For WINDOW_G >= 34, the
51+
* expansion of ECMULT_TABLE_SIZE(WINDOW_G) will overflow. */
52+
#if ECMULT_WINDOW_SIZE < 3 || ECMULT_WINDOW_SIZE > 24
53+
# error Set ECMULT_WINDOW_SIZE to an integer in range [3..24].
4354
#endif
4455

4556
#ifdef USE_ENDOMORPHISM

0 commit comments

Comments
 (0)