Skip to content

Commit b0917f3

Browse files
committed
Merge remote-tracking branch 'upstream/master' into temp-merge-835
2 parents 81052ca + 9e5939d commit b0917f3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1738
-1067
lines changed

.travis.yml

+10-13
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,32 @@ compiler:
1717
- gcc
1818
env:
1919
global:
20-
- WIDEMUL=auto BIGNUM=auto ENDOMORPHISM=no STATICPRECOMPUTATION=yes ECMULTGENPRECISION=auto ASM=no BUILD=check EXTRAFLAGS= HOST= ECDH=no RECOVERY=no EXPERIMENTAL=no CTIMETEST=yes BENCH=yes ITERS=2 GENERATOR=no RANGEPROOF=no WHITELIST=no SCHNORRSIG=no MUSIG=no
20+
- WIDEMUL=auto BIGNUM=auto STATICPRECOMPUTATION=yes ECMULTGENPRECISION=auto ASM=no BUILD=check WITH_VALGRIND=yes RUN_VALGRIND=no EXTRAFLAGS= HOST= ECDH=no RECOVERY=no SCHNORRSIG=no EXPERIMENTAL=no CTIMETEST=yes BENCH=yes ITERS=2 GENERATOR=no RANGEPROOF=no WHITELIST=no SCHNORRSIG=no MUSIG=no
2121
matrix:
2222
- WIDEMUL=int64 EXPERIMENTAL=yes RANGEPROOF=yes WHITELIST=yes GENERATOR=yes SCHNORRSIG=yes MUSIG=yes
2323
- WIDEMUL=int128 EXPERIMENTAL=yes RANGEPROOF=yes WHITELIST=yes GENERATOR=yes SCHNORRSIG=yes MUSIG=yes
2424
- WIDEMUL=int64 RECOVERY=yes
2525
- WIDEMUL=int64 ECDH=yes EXPERIMENTAL=yes SCHNORRSIG=yes
26-
- WIDEMUL=int64 ENDOMORPHISM=yes
2726
- WIDEMUL=int128
2827
- WIDEMUL=int128 RECOVERY=yes EXPERIMENTAL=yes SCHNORRSIG=yes
29-
- WIDEMUL=int128 ENDOMORPHISM=yes
30-
- WIDEMUL=int128 ENDOMORPHISM=yes ECDH=yes EXPERIMENTAL=yes SCHNORRSIG=yes MUSIG=yes
28+
- WIDEMUL=int128 ECDH=yes EXPERIMENTAL=yes SCHNORRSIG=yes MUSIG=yes
3129
- WIDEMUL=int128 ASM=x86_64
32-
- WIDEMUL=int128 ENDOMORPHISM=yes ASM=x86_64
3330
- BIGNUM=no
34-
- BIGNUM=no ENDOMORPHISM=yes RECOVERY=yes EXPERIMENTAL=yes SCHNORRSIG=yes MUSIG=yes
31+
- BIGNUM=no RECOVERY=yes EXPERIMENTAL=yes SCHNORRSIG=yes MUSIG=yes
3532
- BIGNUM=no STATICPRECOMPUTATION=no
36-
- BUILD=distcheck CTIMETEST= BENCH=
33+
- BUILD=distcheck WITH_VALGRIND=no CTIMETEST=no BENCH=no
3734
- CPPFLAGS=-DDETERMINISTIC
38-
- CFLAGS=-O0 CTIMETEST=
35+
- CFLAGS=-O0 CTIMETEST=no
36+
- CFLAGS="-fsanitize=undefined -fno-omit-frame-pointer" LDFLAGS="-fsanitize=undefined -fno-omit-frame-pointer" UBSAN_OPTIONS="print_stacktrace=1:halt_on_error=1" BIGNUM=no ASM=x86_64 ECDH=yes RECOVERY=yes EXPERIMENTAL=yes SCHNORRSIG=yes CTIMETEST=no
3937
- ECMULTGENPRECISION=2
4038
- ECMULTGENPRECISION=8
41-
- VALGRIND=yes ENDOMORPHISM=yes BIGNUM=no ASM=x86_64 EXPERIMENTAL=yes ECDH=yes RECOVERY=yes EXTRAFLAGS="--disable-openssl-tests" CPPFLAGS=-DVALGRIND BUILD=
42-
- VALGRIND=yes BIGNUM=no ASM=x86_64 EXPERIMENTAL=yes ECDH=yes RECOVERY=yes EXTRAFLAGS="--disable-openssl-tests" CPPFLAGS=-DVALGRIND BUILD=
39+
- RUN_VALGRIND=yes BIGNUM=no ASM=x86_64 ECDH=yes RECOVERY=yes EXPERIMENTAL=yes SCHNORRSIG=yes EXTRAFLAGS="--disable-openssl-tests" BUILD=
4340
matrix:
4441
fast_finish: true
4542
include:
4643
- compiler: clang
4744
os: linux
48-
env: HOST=i686-linux-gnu ENDOMORPHISM=yes
45+
env: HOST=i686-linux-gnu
4946
addons:
5047
apt:
5148
packages:
@@ -65,7 +62,7 @@ matrix:
6562
- libtool-bin
6663
- libc6-dbg:i386
6764
- compiler: gcc
68-
env: HOST=i686-linux-gnu ENDOMORPHISM=yes
65+
env: HOST=i686-linux-gnu
6966
os: linux
7067
addons:
7168
apt:
@@ -87,7 +84,7 @@ matrix:
8784
- libc6-dbg:i386
8885
# S390x build (big endian system)
8986
- compiler: gcc
90-
env: HOST=s390x-unknown-linux-gnu ECDH=yes RECOVERY=yes EXPERIMENTAL=yes CTIMETEST=
87+
env: HOST=s390x-unknown-linux-gnu ECDH=yes RECOVERY=yes EXPERIMENTAL=yes SCHNORRSIG=yes CTIMETEST=
9188
arch: s390x
9289

9390
# We use this to install macOS dependencies instead of the built in `homebrew` plugin,

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Features:
1616
* Very efficient implementation.
1717
* Suitable for embedded systems.
1818
* Optional module for public key recovery.
19-
* Optional module for ECDH key exchange (experimental).
19+
* Optional module for ECDH key exchange.
2020

2121
Experimental features have not received enough scrutiny to satisfy the standard of quality of this library but are made available for testing and review by the community. The APIs of these features should not be considered stable.
2222

@@ -48,7 +48,7 @@ Implementation details
4848
* Use wNAF notation for point multiplicands.
4949
* Use a much larger window for multiples of G, using precomputed multiples.
5050
* Use Shamir's trick to do the multiplication with the public key and the generator simultaneously.
51-
* Optionally (off by default) use secp256k1's efficiently-computable endomorphism to split the P multiplicand into 2 half-sized ones.
51+
* Use secp256k1's efficiently-computable endomorphism to split the P multiplicand into 2 half-sized ones.
5252
* Point multiplication for signing
5353
* Use a precomputed table of multiples of powers of 16 multiplied with the generator, so general multiplication becomes a series of additions.
5454
* Intended to be completely free of timing sidechannels for secret-key operations (on reasonable hardware/toolchains)

build-aux/m4/bitcoin_secp.m4

+25-2
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,39 @@ if test x"$has_libcrypto" = x"yes" && test x"$has_openssl_ec" = x; then
3636
CPPFLAGS_TEMP="$CPPFLAGS"
3737
CPPFLAGS="$CRYPTO_CPPFLAGS $CPPFLAGS"
3838
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
39+
#include <openssl/bn.h>
3940
#include <openssl/ec.h>
4041
#include <openssl/ecdsa.h>
4142
#include <openssl/obj_mac.h>]],[[
42-
EC_KEY *eckey = EC_KEY_new_by_curve_name(NID_secp256k1);
43-
ECDSA_sign(0, NULL, 0, NULL, NULL, eckey);
43+
# if OPENSSL_VERSION_NUMBER < 0x10100000L
44+
void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) {(void)sig->r; (void)sig->s;}
45+
# endif
46+
47+
unsigned int zero = 0;
48+
const unsigned char *zero_ptr = (unsigned char*)&zero;
49+
EC_KEY_free(EC_KEY_new_by_curve_name(NID_secp256k1));
50+
EC_KEY *eckey = EC_KEY_new();
51+
EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp256k1);
52+
EC_KEY_set_group(eckey, group);
53+
ECDSA_sign(0, NULL, 0, NULL, &zero, eckey);
4454
ECDSA_verify(0, NULL, 0, NULL, 0, eckey);
55+
o2i_ECPublicKey(&eckey, &zero_ptr, 0);
56+
d2i_ECPrivateKey(&eckey, &zero_ptr, 0);
57+
EC_KEY_check_key(eckey);
4558
EC_KEY_free(eckey);
59+
EC_GROUP_free(group);
4660
ECDSA_SIG *sig_openssl;
4761
sig_openssl = ECDSA_SIG_new();
62+
d2i_ECDSA_SIG(&sig_openssl, &zero_ptr, 0);
63+
i2d_ECDSA_SIG(sig_openssl, NULL);
64+
ECDSA_SIG_get0(sig_openssl, NULL, NULL);
4865
ECDSA_SIG_free(sig_openssl);
66+
const BIGNUM *bignum = BN_value_one();
67+
BN_is_negative(bignum);
68+
BN_num_bits(bignum);
69+
if (sizeof(zero) >= BN_num_bytes(bignum)) {
70+
BN_bn2bin(bignum, (unsigned char*)&zero);
71+
}
4972
]])],[has_openssl_ec=yes],[has_openssl_ec=no])
5073
AC_MSG_RESULT([$has_openssl_ec])
5174
CPPFLAGS="$CPPFLAGS_TEMP"

configure.ac

+24-22
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ esac
6767

6868
CFLAGS="-W $CFLAGS"
6969

70-
warn_CFLAGS="-std=c89 -pedantic -Wall -Wextra -Wcast-align -Wnested-externs -Wshadow -Wstrict-prototypes -Wno-unused-function -Wno-long-long -Wno-overlength-strings"
70+
warn_CFLAGS="-std=c89 -pedantic -Wall -Wextra -Wcast-align -Wnested-externs -Wshadow -Wstrict-prototypes -Wundef -Wno-unused-function -Wno-long-long -Wno-overlength-strings"
7171
saved_CFLAGS="$CFLAGS"
7272
CFLAGS="$warn_CFLAGS $CFLAGS"
7373
AC_MSG_CHECKING([if ${CC} supports ${warn_CFLAGS}])
@@ -116,18 +116,13 @@ AC_ARG_ENABLE(exhaustive_tests,
116116
[use_exhaustive_tests=$enableval],
117117
[use_exhaustive_tests=yes])
118118

119-
AC_ARG_ENABLE(endomorphism,
120-
AS_HELP_STRING([--enable-endomorphism],[enable endomorphism [default=no]]),
121-
[use_endomorphism=$enableval],
122-
[use_endomorphism=no])
123-
124119
AC_ARG_ENABLE(ecmult_static_precomputation,
125120
AS_HELP_STRING([--enable-ecmult-static-precomputation],[enable precomputed ecmult table for signing [default=auto]]),
126121
[use_ecmult_static_precomputation=$enableval],
127122
[use_ecmult_static_precomputation=auto])
128123

129124
AC_ARG_ENABLE(module_ecdh,
130-
AS_HELP_STRING([--enable-module-ecdh],[enable ECDH shared secret computation (experimental)]),
125+
AS_HELP_STRING([--enable-module-ecdh],[enable ECDH shared secret computation]),
131126
[enable_module_ecdh=$enableval],
132127
[enable_module_ecdh=no])
133128

@@ -194,8 +189,7 @@ AC_ARG_WITH([asm], [AS_HELP_STRING([--with-asm=x86_64|arm|no|auto],
194189
AC_ARG_WITH([ecmult-window], [AS_HELP_STRING([--with-ecmult-window=SIZE|auto],
195190
[window size for ecmult precomputation for verification, specified as integer in range [2..24].]
196191
[Larger values result in possibly better performance at the cost of an exponentially larger precomputed table.]
197-
[The table will store 2^(SIZE-2) * 64 bytes of data but can be larger in memory due to platform-specific padding and alignment.]
198-
[If the endomorphism optimization is enabled, two tables of this size are used instead of only one.]
192+
[The table will store 2^(SIZE-1) * 64 bytes of data but can be larger in memory due to platform-specific padding and alignment.]
199193
["auto" is a reasonable setting for desktop machines (currently 15). [default=auto]]
200194
)],
201195
[req_ecmult_window=$withval], [req_ecmult_window=auto])
@@ -208,7 +202,21 @@ AC_ARG_WITH([ecmult-gen-precision], [AS_HELP_STRING([--with-ecmult-gen-precision
208202
)],
209203
[req_ecmult_gen_precision=$withval], [req_ecmult_gen_precision=auto])
210204

211-
AC_CHECK_HEADER([valgrind/memcheck.h], [enable_valgrind=yes], [enable_valgrind=no], [])
205+
AC_ARG_WITH([valgrind], [AS_HELP_STRING([--with-valgrind=yes|no|auto],
206+
[Build with extra checks for running inside Valgrind [default=auto]]
207+
)],
208+
[req_valgrind=$withval], [req_valgrind=auto])
209+
210+
if test x"$req_valgrind" = x"no"; then
211+
enable_valgrind=no
212+
else
213+
AC_CHECK_HEADER([valgrind/memcheck.h], [enable_valgrind=yes], [
214+
if test x"$req_valgrind" = x"yes"; then
215+
AC_MSG_ERROR([Valgrind support explicitly requested but valgrind/memcheck.h header not available])
216+
fi
217+
enable_valgrind=no
218+
], [])
219+
fi
212220
AM_CONDITIONAL([VALGRIND_ENABLED],[test "$enable_valgrind" = "yes"])
213221

214222
if test x"$enable_coverage" = x"yes"; then
@@ -429,8 +437,8 @@ esac
429437

430438
if test x"$use_tests" = x"yes"; then
431439
SECP_OPENSSL_CHECK
432-
if test x"$has_openssl_ec" = x"yes"; then
433-
if test x"$enable_openssl_tests" != x"no"; then
440+
if test x"$enable_openssl_tests" != x"no" && test x"$has_openssl_ec" = x"yes"; then
441+
enable_openssl_tests=yes
434442
AC_DEFINE(ENABLE_OPENSSL_TESTS, 1, [Define this symbol if OpenSSL EC functions are available])
435443
SECP_TEST_INCLUDES="$SSL_CFLAGS $CRYPTO_CFLAGS $CRYPTO_CPPFLAGS"
436444
SECP_TEST_LIBS="$CRYPTO_LIBS"
@@ -440,27 +448,24 @@ if test x"$use_tests" = x"yes"; then
440448
SECP_TEST_LIBS="$SECP_TEST_LIBS -lgdi32"
441449
;;
442450
esac
443-
fi
444451
else
445452
if test x"$enable_openssl_tests" = x"yes"; then
446453
AC_MSG_ERROR([OpenSSL tests requested but OpenSSL with EC support is not available])
447454
fi
455+
enable_openssl_tests=no
448456
fi
449457
else
450458
if test x"$enable_openssl_tests" = x"yes"; then
451459
AC_MSG_ERROR([OpenSSL tests requested but tests are not enabled])
452460
fi
461+
enable_openssl_tests=no
453462
fi
454463

455464
if test x"$set_bignum" = x"gmp"; then
456465
SECP_LIBS="$SECP_LIBS $GMP_LIBS"
457466
SECP_INCLUDES="$SECP_INCLUDES $GMP_CPPFLAGS"
458467
fi
459468

460-
if test x"$use_endomorphism" = x"yes"; then
461-
AC_DEFINE(USE_ENDOMORPHISM, 1, [Define this symbol to use endomorphism optimization])
462-
fi
463-
464469
if test x"$set_precomp" = x"yes"; then
465470
AC_DEFINE(USE_ECMULT_STATIC_PRECOMPUTATION, 1, [Define this symbol to use a statically generated ecmult table])
466471
fi
@@ -520,7 +525,6 @@ if test x"$enable_experimental" = x"yes"; then
520525
AC_MSG_NOTICE([******])
521526
AC_MSG_NOTICE([WARNING: experimental build])
522527
AC_MSG_NOTICE([Experimental features do not have stable APIs or properties, and may not be safe for production use.])
523-
AC_MSG_NOTICE([Building ECDH module: $enable_module_ecdh])
524528
AC_MSG_NOTICE([Building NUMS generator module: $enable_module_generator])
525529
AC_MSG_NOTICE([Building range proof module: $enable_module_rangeproof])
526530
AC_MSG_NOTICE([Building key whitelisting module: $enable_module_whitelist])
@@ -552,9 +556,6 @@ if test x"$enable_experimental" = x"yes"; then
552556
fi
553557
fi
554558
else
555-
if test x"$enable_module_ecdh" = x"yes"; then
556-
AC_MSG_ERROR([ECDH module is experimental. Use --enable-experimental to allow.])
557-
fi
558559
if test x"$enable_module_musig" = x"yes"; then
559560
AC_MSG_ERROR([MuSig module is experimental. Use --enable-experimental to allow.])
560561
fi
@@ -614,10 +615,11 @@ AC_OUTPUT
614615

615616
echo
616617
echo "Build Options:"
617-
echo " with endomorphism = $use_endomorphism"
618618
echo " with ecmult precomp = $set_precomp"
619619
echo " with external callbacks = $use_external_default_callbacks"
620620
echo " with benchmarks = $use_benchmark"
621+
echo " with tests = $use_tests"
622+
echo " with openssl tests = $enable_openssl_tests"
621623
echo " with coverage = $enable_coverage"
622624
echo " module ecdh = $enable_module_ecdh"
623625
echo " module recovery = $enable_module_recovery"

contrib/travis.sh

+11-6
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,29 @@ then
1313
fi
1414

1515
./configure \
16-
--enable-experimental="$EXPERIMENTAL" --enable-endomorphism="$ENDOMORPHISM" \
16+
--enable-experimental="$EXPERIMENTAL" \
1717
--with-test-override-wide-multiply="$WIDEMUL" --with-bignum="$BIGNUM" --with-asm="$ASM" \
1818
--enable-ecmult-static-precomputation="$STATICPRECOMPUTATION" --with-ecmult-gen-precision="$ECMULTGENPRECISION" \
1919
--enable-module-ecdh="$ECDH" --enable-module-recovery="$RECOVERY" \
2020
--enable-module-rangeproof="$RANGEPROOF" --enable-module-whitelist="$WHITELIST" --enable-module-generator="$GENERATOR" \
21-
--enable-module-schnorrsig="$SCHNORRSIG" --enable-module-musig="$MUSIG" \
21+
--enable-module-schnorrsig="$SCHNORRSIG" --enable-module-musig="$MUSIG"\
22+
--with-valgrind="$WITH_VALGRIND" \
2223
--host="$HOST" $EXTRAFLAGS
2324

2425
if [ -n "$BUILD" ]
2526
then
2627
make -j2 "$BUILD"
2728
fi
28-
if [ -n "$VALGRIND" ]
29+
if [ "$RUN_VALGRIND" = "yes" ]
2930
then
3031
make -j2
3132
# the `--error-exitcode` is required to make the test fail if valgrind found errors, otherwise it'll return 0 (http://valgrind.org/docs/manual/manual-core.html)
3233
valgrind --error-exitcode=42 ./tests 16
3334
valgrind --error-exitcode=42 ./exhaustive_tests
3435
fi
35-
if [ -n "$BENCH" ]
36+
if [ "$BENCH" = "yes" ]
3637
then
37-
if [ -n "$VALGRIND" ]
38+
if [ "$RUN_VALGRIND" = "yes" ]
3839
then
3940
# Using the local `libtool` because on macOS the system's libtool has nothing to do with GNU libtool
4041
EXEC='./libtool --mode=execute valgrind --error-exitcode=42'
@@ -57,8 +58,12 @@ then
5758
then
5859
$EXEC ./bench_ecdh >> bench.log 2>&1
5960
fi
61+
if [ "$SCHNORRSIG" = "yes" ]
62+
then
63+
$EXEC ./bench_schnorrsig >> bench.log 2>&1
64+
fi
6065
fi
61-
if [ -n "$CTIMETEST" ]
66+
if [ "$CTIMETEST" = "yes" ]
6267
then
6368
./libtool --mode=execute valgrind --error-exitcode=42 ./valgrind_ctime_test > valgrind_ctime_test.log 2>&1
6469
fi

0 commit comments

Comments
 (0)