Skip to content

Commit

Permalink
Make WINDOW_G configurable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
real-or-random committed Mar 7, 2019
1 parent aa15154 commit 673889e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
26 changes: 26 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ AC_ARG_WITH([scalar], [AS_HELP_STRING([--with-scalar=64bit|32bit|auto],
AC_ARG_WITH([asm], [AS_HELP_STRING([--with-asm=x86_64|arm|no|auto]
[Specify assembly optimizations to use. Default is auto (experimental: arm)])],[req_asm=$withval], [req_asm=auto])

# Default is window size 16 (or window size 15 with endomorphism) which needs 1.375 MiB. */
AC_ARG_WITH([ecmult-window], [AS_HELP_STRING([--with-ecmult-window=SIZE],
[window size for ecmult precomputation for verification, specified as integer in range [3..24].]
[Larger values result in possibly better performance at the cost of an exponentially larger precomputed table.]
[The table will store 2^(SIZE-2) * 64 bytes of data but can be larger in memory due]
[to platform-specific padding and alignment. [default=16]]
)],
[set_ecmult_window=$withval], [set_ecmult_window=16])

AC_CHECK_TYPES([__int128])

if test x"$enable_coverage" = x"yes"; then
Expand Down Expand Up @@ -387,6 +396,22 @@ case $set_scalar in
;;
esac

#set ecmult window size
m4_define([error_window_size], [[window size for ecmult precomputation not an integer in range [3..24]]])
case $set_ecmult_window in
''|*[[!0-9]]*)
# no valid integer
AC_MSG_ERROR([error_window_size])
;;
*)
if test "$set_ecmult_window" -lt 3 -o "$set_ecmult_window" -gt 24 ; then
# not in range
AC_MSG_ERROR([error_window_size])
fi
;;
esac
AC_DEFINE_UNQUOTED(ECMULT_WINDOW_SIZE, $set_ecmult_window, [Set window size for ecmult precomputation])

if test x"$use_tests" = x"yes"; then
SECP_OPENSSL_CHECK
if test x"$has_openssl_ec" = x"yes"; then
Expand Down Expand Up @@ -516,6 +541,7 @@ echo " asm = $set_asm"
echo " bignum = $set_bignum"
echo " field = $set_field"
echo " scalar = $set_scalar"
echo " ecmult window size = $set_ecmult_window"
echo
echo " CC = $CC"
echo " CFLAGS = $CFLAGS"
Expand Down
29 changes: 20 additions & 9 deletions src/ecmult_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,27 @@
# endif
#else
/* optimal for 128-bit and 256-bit exponents. */
#define WINDOW_A 5
/** larger numbers may result in slightly better performance, at the cost of
exponentially larger precomputed tables. */
#ifdef USE_ENDOMORPHISM
/** Two tables for window size 15: 1.375 MiB. */
#define WINDOW_G 15
#else
/** One table for window size 16: 1.375 MiB. */
#define WINDOW_G 16
# define WINDOW_A 5
/** Larger values for ECMULT_WINDOW_SIZE result in possibly better
* performance at the cost of an exponentially larger precomputed
* table. The exact table size is
* (1 << (WINDOW_G - 2)) * sizeof(secp256k1_ge_storage) bytes,
* where sizeof(secp256k1_ge_storage) is typically 64 bytes but can
* be larger due to platform-specific padding and alignment.
*/
# ifdef USE_ENDOMORPHISM
# define WINDOW_G ((ECMULT_WINDOW_SIZE)-1)
# else
# define WINDOW_G (ECMULT_WINDOW_SIZE)
# endif
#endif

/* Noone will ever need more than a window size of 24.
* (The code probably works with window sizes up to 33 but
* it is not tested for it. For WINDOW_G >= 34, the
* expansion of ECMULT_TABLE_SIZE(WINDOW_G) will overflow. */
#if ECMULT_WINDOW_SIZE < 3 || ECMULT_WINDOW_SIZE > 24
# error Set ECMULT_WINDOW_SIZE to an integer in range [3..24].
#endif

#ifdef USE_ENDOMORPHISM
Expand Down

0 comments on commit 673889e

Please sign in to comment.