Skip to content

Commit 8269dec

Browse files
committed
Merge commits '8ae56e33 75ce488 4866178 446d28d 253f90c ec3aaa5 0440945 7688a4f be8d9c2 ' into temp-merge-965
2 parents 5d2df05 + be8d9c2 commit 8269dec

23 files changed

+580
-218
lines changed

.cirrus.yml

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
env:
2-
WIDEMUL: auto
2+
### compiler options
3+
HOST:
4+
# Specific warnings can be disabled with -Wno-error=foo.
5+
# -pedantic-errors is not equivalent to -Werror=pedantic and thus not implied by -Werror according to the GCC manual.
6+
WERROR_CFLAGS: -Werror -pedantic-errors
7+
MAKEFLAGS: -j2
8+
BUILD: check
9+
### secp256k1 config
310
STATICPRECOMPUTATION: yes
411
ECMULTGENPRECISION: auto
512
ASM: no
6-
BUILD: check
13+
WIDEMUL: auto
714
WITH_VALGRIND: yes
815
EXTRAFLAGS:
9-
HOST:
16+
### secp256k1 modules
1017
ECDH: no
1118
RECOVERY: no
1219
SCHNORRSIG: no
@@ -16,12 +23,11 @@ env:
1623
WHITELIST: no
1724
MUSIG: no
1825
ECDSAADAPTOR: no
19-
EXPERIMENTAL: no
20-
CTIMETEST: yes
21-
BENCH: yes
26+
### test options
2227
TEST_ITERS:
28+
BENCH: yes
2329
BENCH_ITERS: 2
24-
MAKEFLAGS: -j2
30+
CTIMETEST: yes
2531

2632
cat_logs_snippet: &CAT_LOGS
2733
always:
@@ -340,3 +346,23 @@ task:
340346
- ./ci/cirrus.sh
341347
<< : *CAT_LOGS
342348

349+
task:
350+
name: "C++ -fpermissive"
351+
container:
352+
dockerfile: ci/linux-debian.Dockerfile
353+
cpu: 1
354+
memory: 1G
355+
env:
356+
# ./configure correctly errors out when given CC=g++.
357+
# We hack around this by passing CC=g++ only to make.
358+
CC: gcc
359+
MAKEFLAGS: -j2 CC=g++ CFLAGS=-fpermissive
360+
WERROR_CFLAGS:
361+
EXPERIMENTAL: yes
362+
ECDH: yes
363+
RECOVERY: yes
364+
SCHNORRSIG: yes
365+
<< : *MERGE_BASE
366+
test_script:
367+
- ./ci/cirrus.sh
368+
<< : *CAT_LOGS

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ aclocal.m4
2525
autom4te.cache/
2626
config.log
2727
config.status
28+
conftest*
2829
*.tar.gz
2930
*.la
3031
libtool

Makefile.am

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
ACLOCAL_AMFLAGS = -I build-aux/m4
22

3+
# AM_CFLAGS will be automatically prepended to CFLAGS by Automake when compiling some foo
4+
# which does not have an explicit foo_CFLAGS variable set.
5+
AM_CFLAGS = $(SECP_CFLAGS)
6+
37
lib_LTLIBRARIES = libsecp256k1.la
48
include_HEADERS = include/secp256k1.h
59
include_HEADERS += include/secp256k1_preallocated.h
@@ -131,10 +135,10 @@ CPPFLAGS_FOR_BUILD +=-I$(top_srcdir) -I$(builddir)/src
131135
gen_context_OBJECTS = gen_context.o
132136
gen_context_BIN = gen_context$(BUILD_EXEEXT)
133137
gen_%.o: src/gen_%.c src/libsecp256k1-config.h
134-
$(CC_FOR_BUILD) $(DEFS) $(CPPFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD) -c $< -o $@
138+
$(CC_FOR_BUILD) $(DEFS) $(CPPFLAGS_FOR_BUILD) $(SECP_CFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD) -c $< -o $@
135139

136140
$(gen_context_BIN): $(gen_context_OBJECTS)
137-
$(CC_FOR_BUILD) $(CFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) $^ -o $@
141+
$(CC_FOR_BUILD) $(SECP_CFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) $^ -o $@
138142

139143
$(libsecp256k1_la_OBJECTS): src/ecmult_static_context.h
140144
$(tests_OBJECTS): src/ecmult_static_context.h

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Features:
1717
* Suitable for embedded systems.
1818
* Optional module for public key recovery.
1919
* Optional module for ECDH key exchange.
20+
* Optional module for Schnorr signatures according to [BIP-340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki) (experimental).
2021
* Optional module for ECDSA adaptor signatures (experimental).
2122

2223
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.

build-aux/m4/bitcoin_secp.m4

+16
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,19 @@ if test x"$has_valgrind" != x"yes"; then
8282
AC_CHECK_HEADER([valgrind/memcheck.h], [has_valgrind=yes; AC_DEFINE(HAVE_VALGRIND,1,[Define this symbol if valgrind is installed])])
8383
fi
8484
])
85+
86+
dnl SECP_TRY_APPEND_CFLAGS(flags, VAR)
87+
dnl Append flags to VAR if CC accepts them.
88+
AC_DEFUN([SECP_TRY_APPEND_CFLAGS], [
89+
AC_MSG_CHECKING([if ${CC} supports $1])
90+
SECP_TRY_APPEND_CFLAGS_saved_CFLAGS="$CFLAGS"
91+
CFLAGS="$1 $CFLAGS"
92+
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])], [flag_works=yes], [flag_works=no])
93+
AC_MSG_RESULT($flag_works)
94+
CFLAGS="$SECP_TRY_APPEND_CFLAGS_saved_CFLAGS"
95+
if test x"$flag_works" = x"yes"; then
96+
$2="$$2 $1"
97+
fi
98+
unset flag_works
99+
AC_SUBST($2)
100+
])

ci/linux-debian.Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ RUN apt-get install --no-install-recommends --no-upgrade -y \
1313
git ca-certificates \
1414
make automake libtool pkg-config dpkg-dev valgrind qemu-user \
1515
gcc clang llvm libc6-dbg \
16+
g++ \
1617
gcc-i686-linux-gnu libc6-dev-i386-cross libc6-dbg:i386 libubsan1:i386 libasan5:i386 \
1718
gcc-s390x-linux-gnu libc6-dev-s390x-cross libc6-dbg:s390x \
1819
gcc-arm-linux-gnueabihf libc6-dev-armhf-cross libc6-dbg:armhf \

configure.ac

+62-58
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ AH_TOP([#define LIBSECP256K1_CONFIG_H])
88
AH_BOTTOM([#endif /*LIBSECP256K1_CONFIG_H*/])
99
AM_INIT_AUTOMAKE([foreign subdir-objects])
1010

11-
# Set -g if CFLAGS are not already set, which matches the default autoconf
12-
# behavior (see PROG_CC in the Autoconf manual) with the exception that we don't
13-
# set -O2 here because we set it in any case (see further down).
14-
: ${CFLAGS="-g"}
1511
LT_INIT
1612

1713
# Make the compilation flags quiet unless V=1 is used.
@@ -42,8 +38,8 @@ AM_PROG_AS
4238
case $host_os in
4339
*darwin*)
4440
if test x$cross_compiling != xyes; then
45-
AC_PATH_PROG([BREW],brew,)
46-
if test x$BREW != x; then
41+
AC_CHECK_PROG([BREW], brew, brew)
42+
if test x$BREW = xbrew; then
4743
# These Homebrew packages may be keg-only, meaning that they won't be found
4844
# in expected paths because they may conflict with system files. Ask
4945
# Homebrew where each one is located, then adjust paths accordingly.
@@ -58,10 +54,10 @@ case $host_os in
5854
VALGRIND_CPPFLAGS="-I$valgrind_prefix/include"
5955
fi
6056
else
61-
AC_PATH_PROG([PORT],port,)
57+
AC_CHECK_PROG([PORT], port, port)
6258
# If homebrew isn't installed and macports is, add the macports default paths
6359
# as a last resort.
64-
if test x$PORT != x; then
60+
if test x$PORT = xport; then
6561
CPPFLAGS="$CPPFLAGS -isystem /opt/local/include"
6662
LDFLAGS="$LDFLAGS -L/opt/local/lib"
6763
fi
@@ -70,35 +66,41 @@ case $host_os in
7066
;;
7167
esac
7268

73-
CFLAGS="-W $CFLAGS"
74-
75-
warn_CFLAGS="-std=c89 -pedantic -Wall -Wextra -Wcast-align -Wnested-externs -Wshadow -Wstrict-prototypes -Wundef -Wno-unused-function -Wno-long-long -Wno-overlength-strings"
76-
saved_CFLAGS="$CFLAGS"
77-
CFLAGS="$warn_CFLAGS $CFLAGS"
78-
AC_MSG_CHECKING([if ${CC} supports ${warn_CFLAGS}])
79-
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
80-
[ AC_MSG_RESULT([yes]) ],
81-
[ AC_MSG_RESULT([no])
82-
CFLAGS="$saved_CFLAGS"
83-
])
84-
85-
saved_CFLAGS="$CFLAGS"
86-
CFLAGS="-Wconditional-uninitialized $CFLAGS"
87-
AC_MSG_CHECKING([if ${CC} supports -Wconditional-uninitialized])
88-
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
89-
[ AC_MSG_RESULT([yes]) ],
90-
[ AC_MSG_RESULT([no])
91-
CFLAGS="$saved_CFLAGS"
92-
])
93-
94-
saved_CFLAGS="$CFLAGS"
95-
CFLAGS="-fvisibility=hidden $CFLAGS"
96-
AC_MSG_CHECKING([if ${CC} supports -fvisibility=hidden])
97-
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
98-
[ AC_MSG_RESULT([yes]) ],
99-
[ AC_MSG_RESULT([no])
100-
CFLAGS="$saved_CFLAGS"
101-
])
69+
# Try if some desirable compiler flags are supported and append them to SECP_CFLAGS.
70+
#
71+
# These are our own flags, so we append them to our own SECP_CFLAGS variable (instead of CFLAGS) as
72+
# recommended in the automake manual (Section "Flag Variables Ordering"). CFLAGS belongs to the user
73+
# and we are not supposed to touch it. In the Makefile, we will need to ensure that SECP_CFLAGS
74+
# is prepended to CFLAGS when invoking the compiler so that the user always has the last word (flag).
75+
#
76+
# Another advantage of not touching CFLAGS is that the contents of CFLAGS will be picked up by
77+
# libtool for compiling helper executables. For example, when compiling for Windows, libtool will
78+
# generate entire wrapper executables (instead of simple wrapper scripts as on Unix) to ensure
79+
# proper operation of uninstalled programs linked by libtool against the uninstalled shared library.
80+
# These executables are compiled from C source file for which our flags may not be appropriate,
81+
# e.g., -std=c89 flag has lead to undesirable warnings in the past.
82+
#
83+
# TODO We should analogously not touch CPPFLAGS and LDFLAGS but currently there are no issues.
84+
AC_DEFUN([SECP_TRY_APPEND_DEFAULT_CFLAGS], [
85+
# Try to append -Werror=unknown-warning-option to CFLAGS temporarily. Otherwise clang will
86+
# not error out if it gets unknown warning flags and the checks here will always succeed
87+
# no matter if clang knows the flag or not.
88+
SECP_TRY_APPEND_DEFAULT_CFLAGS_saved_CFLAGS="$CFLAGS"
89+
SECP_TRY_APPEND_CFLAGS([-Werror=unknown-warning-option], CFLAGS)
90+
91+
SECP_TRY_APPEND_CFLAGS([-std=c89 -pedantic -Wno-long-long -Wnested-externs -Wshadow -Wstrict-prototypes -Wundef], $1) # GCC >= 3.0, -Wlong-long is implied by -pedantic.
92+
SECP_TRY_APPEND_CFLAGS([-Wno-overlength-strings], $1) # GCC >= 4.2, -Woverlength-strings is implied by -pedantic.
93+
SECP_TRY_APPEND_CFLAGS([-Wall], $1) # GCC >= 2.95 and probably many other compilers
94+
SECP_TRY_APPEND_CFLAGS([-Wno-unused-function], $1) # GCC >= 3.0, -Wunused-function is implied by -Wall.
95+
SECP_TRY_APPEND_CFLAGS([-Wextra], $1) # GCC >= 3.4, this is the newer name of -W, which we don't use because older GCCs will warn about unused functions.
96+
SECP_TRY_APPEND_CFLAGS([-Wcast-align], $1) # GCC >= 2.95
97+
SECP_TRY_APPEND_CFLAGS([-Wcast-align=strict], $1) # GCC >= 8.0
98+
SECP_TRY_APPEND_CFLAGS([-Wconditional-uninitialized], $1) # Clang >= 3.0 only
99+
SECP_TRY_APPEND_CFLAGS([-fvisibility=hidden], $1) # GCC >= 4.0
100+
101+
CFLAGS="$SECP_TRY_APPEND_DEFAULT_CFLAGS_saved_CFLAGS"
102+
])
103+
SECP_TRY_APPEND_DEFAULT_CFLAGS(SECP_CFLAGS)
102104

103105
###
104106
### Define config arguments
@@ -253,10 +255,14 @@ AM_CONDITIONAL([VALGRIND_ENABLED],[test "$enable_valgrind" = "yes"])
253255

254256
if test x"$enable_coverage" = x"yes"; then
255257
AC_DEFINE(COVERAGE, 1, [Define this symbol to compile out all VERIFY code])
256-
CFLAGS="-O0 --coverage $CFLAGS"
258+
SECP_CFLAGS="-O0 --coverage $SECP_CFLAGS"
257259
LDFLAGS="--coverage $LDFLAGS"
258260
else
259-
CFLAGS="-O2 $CFLAGS"
261+
# Most likely the CFLAGS already contain -O2 because that is autoconf's default.
262+
# We still add it here because passing it twice is not an issue, and handling
263+
# this case would just add unnecessary complexity (see #896).
264+
SECP_CFLAGS="-O2 $SECP_CFLAGS"
265+
SECP_CFLAGS_FOR_BUILD="-O2 $SECP_CFLAGS_FOR_BUILD"
260266
fi
261267

262268
AC_MSG_CHECKING([for __builtin_popcount])
@@ -403,6 +409,9 @@ if test x"$enable_valgrind" = x"yes"; then
403409
SECP_INCLUDES="$SECP_INCLUDES $VALGRIND_CPPFLAGS"
404410
fi
405411

412+
# Add -Werror and similar flags passed from the outside (for testing, e.g., in CI)
413+
SECP_CFLAGS="$SECP_CFLAGS $WERROR_CFLAGS"
414+
406415
# Handle static precomputation (after everything which modifies CFLAGS and friends)
407416
if test x"$use_ecmult_static_precomputation" != x"no"; then
408417
if test x"$cross_compiling" = x"no"; then
@@ -412,8 +421,9 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
412421
fi
413422
# If we're not cross-compiling, simply use the same compiler for building the static precompation code.
414423
CC_FOR_BUILD="$CC"
415-
CFLAGS_FOR_BUILD="$CFLAGS"
416424
CPPFLAGS_FOR_BUILD="$CPPFLAGS"
425+
SECP_CFLAGS_FOR_BUILD="$SECP_CFLAGS"
426+
CFLAGS_FOR_BUILD="$CFLAGS"
417427
LDFLAGS_FOR_BUILD="$LDFLAGS"
418428
else
419429
AX_PROG_CC_FOR_BUILD
@@ -423,42 +433,32 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
423433
cross_compiling=no
424434
SAVE_CC="$CC"
425435
CC="$CC_FOR_BUILD"
426-
SAVE_CFLAGS="$CFLAGS"
427-
CFLAGS="$CFLAGS_FOR_BUILD"
428436
SAVE_CPPFLAGS="$CPPFLAGS"
429437
CPPFLAGS="$CPPFLAGS_FOR_BUILD"
438+
SAVE_CFLAGS="$CFLAGS"
439+
CFLAGS="$CFLAGS_FOR_BUILD"
430440
SAVE_LDFLAGS="$LDFLAGS"
431441
LDFLAGS="$LDFLAGS_FOR_BUILD"
432442

433-
warn_CFLAGS_FOR_BUILD="-Wall -Wextra -Wno-unused-function"
434-
saved_CFLAGS="$CFLAGS"
435-
CFLAGS="$warn_CFLAGS_FOR_BUILD $CFLAGS"
436-
AC_MSG_CHECKING([if native ${CC_FOR_BUILD} supports ${warn_CFLAGS_FOR_BUILD}])
437-
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
438-
[ AC_MSG_RESULT([yes]) ],
439-
[ AC_MSG_RESULT([no])
440-
CFLAGS="$saved_CFLAGS"
441-
])
443+
SECP_TRY_APPEND_DEFAULT_CFLAGS(SECP_CFLAGS_FOR_BUILD)
442444

443445
AC_MSG_CHECKING([for working native compiler: ${CC_FOR_BUILD}])
444446
AC_RUN_IFELSE(
445447
[AC_LANG_PROGRAM([], [])],
446448
[working_native_cc=yes],
447449
[working_native_cc=no],[:])
448450

449-
CFLAGS_FOR_BUILD="$CFLAGS"
450-
451451
# Restore the environment
452452
cross_compiling=$save_cross_compiling
453453
CC="$SAVE_CC"
454-
CFLAGS="$SAVE_CFLAGS"
455454
CPPFLAGS="$SAVE_CPPFLAGS"
455+
CFLAGS="$SAVE_CFLAGS"
456456
LDFLAGS="$SAVE_LDFLAGS"
457457

458458
if test x"$working_native_cc" = x"no"; then
459459
AC_MSG_RESULT([no])
460460
set_precomp=no
461-
m4_define([please_set_for_build], [Please set CC_FOR_BUILD, CFLAGS_FOR_BUILD, CPPFLAGS_FOR_BUILD, and/or LDFLAGS_FOR_BUILD.])
461+
m4_define([please_set_for_build], [Please set CC_FOR_BUILD, CPPFLAGS_FOR_BUILD, CFLAGS_FOR_BUILD, and/or LDFLAGS_FOR_BUILD.])
462462
if test x"$use_ecmult_static_precomputation" = x"yes"; then
463463
AC_MSG_ERROR([native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build])
464464
else
@@ -471,8 +471,9 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
471471
fi
472472

473473
AC_SUBST(CC_FOR_BUILD)
474-
AC_SUBST(CFLAGS_FOR_BUILD)
475474
AC_SUBST(CPPFLAGS_FOR_BUILD)
475+
AC_SUBST(SECP_CFLAGS_FOR_BUILD)
476+
AC_SUBST(CFLAGS_FOR_BUILD)
476477
AC_SUBST(LDFLAGS_FOR_BUILD)
477478
else
478479
set_precomp=no
@@ -626,6 +627,7 @@ AC_SUBST(SECP_INCLUDES)
626627
AC_SUBST(SECP_LIBS)
627628
AC_SUBST(SECP_TEST_LIBS)
628629
AC_SUBST(SECP_TEST_INCLUDES)
630+
AC_SUBST(SECP_CFLAGS)
629631
AM_CONDITIONAL([ENABLE_COVERAGE], [test x"$enable_coverage" = x"yes"])
630632
AM_CONDITIONAL([USE_TESTS], [test x"$use_tests" != x"no"])
631633
AM_CONDITIONAL([USE_EXHAUSTIVE_TESTS], [test x"$use_exhaustive_tests" != x"no"])
@@ -679,13 +681,15 @@ fi
679681
echo
680682
echo " valgrind = $enable_valgrind"
681683
echo " CC = $CC"
682-
echo " CFLAGS = $CFLAGS"
683684
echo " CPPFLAGS = $CPPFLAGS"
685+
echo " SECP_CFLAGS = $SECP_CFLAGS"
686+
echo " CFLAGS = $CFLAGS"
684687
echo " LDFLAGS = $LDFLAGS"
685688
echo
686689
if test x"$set_precomp" = x"yes"; then
687690
echo " CC_FOR_BUILD = $CC_FOR_BUILD"
688-
echo " CFLAGS_FOR_BUILD = $CFLAGS_FOR_BUILD"
689691
echo " CPPFLAGS_FOR_BUILD = $CPPFLAGS_FOR_BUILD"
692+
echo " SECP_CFLAGS_FOR_BUILD = $SECP_CFLAGS_FOR_BUILD"
693+
echo " CFLAGS_FOR_BUILD = $CFLAGS_FOR_BUILD"
690694
echo " LDFLAGS_FOR_BUILD = $LDFLAGS_FOR_BUILD"
691695
fi

contrib/lax_der_parsing.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_
120120
/* Copy R value */
121121
if (rlen > 32) {
122122
overflow = 1;
123-
} else {
123+
} else if (rlen) {
124124
memcpy(tmpsig + 32 - rlen, input + rpos, rlen);
125125
}
126126

@@ -132,7 +132,7 @@ int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_
132132
/* Copy S value */
133133
if (slen > 32) {
134134
overflow = 1;
135-
} else {
135+
} else if (slen) {
136136
memcpy(tmpsig + 64 - slen, input + spos, slen);
137137
}
138138

contrib/lax_der_privatekey_parsing.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *out32, co
4444
if (end < privkey+2 || privkey[0] != 0x04 || privkey[1] > 0x20 || end < privkey+2+privkey[1]) {
4545
return 0;
4646
}
47-
memcpy(out32 + 32 - privkey[1], privkey + 2, privkey[1]);
47+
if (privkey[1]) memcpy(out32 + 32 - privkey[1], privkey + 2, privkey[1]);
4848
if (!secp256k1_ec_seckey_verify(ctx, out32)) {
4949
memset(out32, 0, 32);
5050
return 0;

0 commit comments

Comments
 (0)