Skip to content

Commit

Permalink
configure.ac: support --with-system-secp256k1
Browse files Browse the repository at this point in the history
Libwally-core, in its stock configuration, compiles and statically links
against a private copy of libsecp256k1_zkp. However, Gentoo unbundles
libsecp256k1_zkp and instead links libwally-core against a system-wide
shared libsecp256k1_zkp with headers in /usr/include/secp256k1_zkp and a
libsecp256k1_zkp.pc that specifies the relevant CFLAGS and LIBS.

Implement a --with-system-secp256k1 configure option to allow compiling
and linking against a system-installed libsecp256k1. Pass the user-
specified package name (or 'libsecp256k1' or 'libsecp256k1_zkp' by
default, depending on --enable-standard-secp) to PKG_CHECK_MODULES to
find the CFLAGS and LIBS of a system-installed libsecp256k1. Call
AC_CHECK_FUNCS to assert that the required modules are present in the
system-installed libsecp256k1.

If the user does not specify --with-system-secp256k1 (or specifies
--without-system-secp256k1), then the pre-existing behavior is
preserved: namely, the build will use the bundled copy of
libsecp256k1_zkp.

Adjust several #include directives to reference secp256k1 headers in the
configured header search path rather than explicitly specifying paths to
the private copies. In the case of using the bundled libsecp256k1_zkp,
'src/secp256k1/include' is added to the header search path; otherwise,
the necessary CFLAGS, if any, are taken from pkg-config.
  • Loading branch information
whitslack committed Sep 1, 2023
1 parent 4b1b985 commit b745937
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 19 deletions.
47 changes: 43 additions & 4 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,40 @@ fi
#
# libsecp256k1
#
AC_ARG_WITH([system-secp256k1],
[AS_HELP_STRING([[--with-system-secp256k1[=PKG]]],
[build using system-installed libsecp256k1 instead of bundled, passing PKG (default: libsecp256k1 or libsecp256k1_zkp, depending on --enable-standard-secp) to pkg-config (default: no)])],
[AS_IF([test "x$withval" = xyes],
[AM_COND_IF([BUILD_STANDARD_SECP], [with_system_secp256k1=libsecp256k1], [with_system_secp256k1=libsecp256k1_zkp])])],
[with_system_secp256k1=no])
AM_CONDITIONAL([LINK_SYSTEM_SECP256K1], [test "x$with_system_secp256k1" != xno])
AM_COND_IF([LINK_SYSTEM_SECP256K1], [
saved_LIBS=$LIBS
PKG_CHECK_MODULES([libsecp256k1], [$with_system_secp256k1])
LIBS="$libsecp256k1_LIBS $LIBS"
missing_modules=
AC_DEFUN([CHECK_MODULE], [
AC_CHECK_FUNCS([$2], [], [missing_modules="${missing_modules} $1"])
])
CHECK_MODULE([ecdh], [secp256k1_ecdh])
CHECK_MODULE([extrakeys], [secp256k1_xonly_pubkey_parse])
CHECK_MODULE([recovery], [secp256k1_ecdsa_recover])
CHECK_MODULE([schnorrsig], [secp256k1_schnorrsig_verify])
AM_COND_IF([BUILD_STANDARD_SECP], [], [
CHECK_MODULE([ecdsa-s2c], [secp256k1_ecdsa_s2c_sign])
])
AM_COND_IF([BUILD_ELEMENTS], [
CHECK_MODULE([generator], [secp256k1_generator_parse])
CHECK_MODULE([rangeproof], [secp256k1_rangeproof_verify])
CHECK_MODULE([surjectionproof], [secp256k1_surjectionproof_initialize])
CHECK_MODULE([whitelist], [secp256k1_whitelist_sign])
])
AS_IF([test -n "${missing_modules}"], [
AC_MSG_ERROR([system-installed $with_system_secp256k1 does not support these required modules:${missing_modules}])
])
LIBS=$saved_LIBS
], [
libsecp256k1_CFLAGS='-I$(top_srcdir)/src/secp256k1/include'
# FIXME: This is needed to force libtool to use all object files from secp.
# We can only build secp properly by recursively invoking
# configure/make, and can't include it as a noinst_ library. Libtool
Expand All @@ -277,8 +311,10 @@ fi
# Because libtool both intercepts -Wl and arbitrarily re-orders its
# command line inputs, we have to concoct a single expression to
# enforce linking that cannot be split, hence the below expression.
LIBADD_SECP256K1="-Wl,secp256k1/src/libsecp256k1_la-secp256k1.${OBJEXT},secp256k1/src/libsecp256k1_precomputed_la-precomputed_ecmult_gen.${OBJEXT},secp256k1/src/libsecp256k1_precomputed_la-precomputed_ecmult.${OBJEXT}"
AC_SUBST([LIBADD_SECP256K1])
libsecp256k1_LIBS="-Wl,secp256k1/src/libsecp256k1_la-secp256k1.${OBJEXT},secp256k1/src/libsecp256k1_precomputed_la-precomputed_ecmult_gen.${OBJEXT},secp256k1/src/libsecp256k1_precomputed_la-precomputed_ecmult.${OBJEXT}"
])
AC_SUBST([libsecp256k1_CFLAGS])
AC_SUBST([libsecp256k1_LIBS])

#
# Python facilities
Expand Down Expand Up @@ -410,8 +446,11 @@ export ARFLAGS
export AR_FLAGS
export LD
export LDFLAGS
ac_configure_args="${ac_configure_args} --disable-shared --with-pic --with-bignum=no --enable-experimental --enable-module-ecdh --enable-module-recovery --enable-module-ecdsa-s2c --enable-module-rangeproof --enable-module-surjectionproof --enable-module-whitelist --enable-module-generator --enable-module-extrakeys --enable-module-schnorrsig ${secp256k1_test_opt} --enable-exhaustive-tests=no --enable-benchmark=no --disable-dependency-tracking ${secp_asm}"
AC_CONFIG_SUBDIRS([src/secp256k1])

AM_COND_IF([LINK_SYSTEM_SECP256K1], [], [
ac_configure_args="${ac_configure_args} --disable-shared --with-pic --with-bignum=no --enable-experimental --enable-module-ecdh --enable-module-recovery --enable-module-ecdsa-s2c --enable-module-rangeproof --enable-module-surjectionproof --enable-module-whitelist --enable-module-generator --enable-module-extrakeys --enable-module-schnorrsig ${secp256k1_test_opt} --enable-exhaustive-tests=no --enable-benchmark=no --disable-dependency-tracking ${secp_asm}"
AC_CONFIG_SUBDIRS([src/secp256k1])
])


AC_OUTPUT
10 changes: 6 additions & 4 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ if USE_SWIG_PYTHON
noinst_LTLIBRARIES += libswig_python.la
libswig_python_la_SOURCES = swig_python/swig_python_wrap.c

libswig_python_la_CFLAGS = -I$(top_srcdir) $(AM_CFLAGS) $(SWIG_PYTHON_CPPFLAGS) $(SWIG_WARN_CFLAGS) $(NOALIAS_CFLAGS)
libswig_python_la_CFLAGS = -I$(top_srcdir) $(libsecp256k1_CFLAGS) $(AM_CFLAGS) $(SWIG_PYTHON_CPPFLAGS) $(SWIG_WARN_CFLAGS) $(NOALIAS_CFLAGS)
if PYTHON_MANYLINUX
else
libswig_python_la_LIBADD = $(PYTHON_LIBS)
Expand Down Expand Up @@ -83,7 +83,7 @@ noinst_LTLIBRARIES += libswig_java.la
libswig_java_la_SOURCES = \
swig_java/swig_java_wrap.c

libswig_java_la_CFLAGS = -I$(top_srcdir) $(AM_CFLAGS) $(SWIG_JAVA_CPPFLAGS) $(SWIG_WARN_CFLAGS)
libswig_java_la_CFLAGS = -I$(top_srcdir) $(libsecp256k1_CFLAGS) $(AM_CFLAGS) $(SWIG_JAVA_CPPFLAGS) $(SWIG_WARN_CFLAGS)

SWIG_JOPT = $(SWIG_JAVA_OPT) -outdir swig_java -noproxy -package com.blockstream.libwally

Expand Down Expand Up @@ -205,10 +205,12 @@ libwallycore_la_LDFLAGS += -no-undefined
endif
endif # SHARED_BUILD_ENABLED

libwallycore_la_CFLAGS = -I$(top_srcdir) -I$(srcdir)/ccan -DWALLY_CORE_BUILD=1 $(AM_CFLAGS)
libwallycore_la_LIBADD = $(LIBADD_SECP256K1) $(noinst_LTLIBRARIES)
libwallycore_la_CFLAGS = -I$(top_srcdir) -I$(srcdir)/ccan $(libsecp256k1_CFLAGS) -DWALLY_CORE_BUILD=1 $(AM_CFLAGS)
libwallycore_la_LIBADD = $(libsecp256k1_LIBS) $(noinst_LTLIBRARIES)

if !LINK_SYSTEM_SECP256K1
SUBDIRS = secp256k1
endif

TESTS =
noinst_PROGRAMS =
Expand Down
4 changes: 2 additions & 2 deletions src/ecdh.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "internal.h"
#include <include/wally_crypto.h>
#include "secp256k1/include/secp256k1.h"
#include "secp256k1/include/secp256k1_ecdh.h"
#include <secp256k1.h>
#include <secp256k1_ecdh.h>

int wally_ecdh(const unsigned char *pub_key, size_t pub_key_len,
const unsigned char *priv_key, size_t priv_key_len,
Expand Down
8 changes: 4 additions & 4 deletions src/elements.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#include <include/wally_crypto.h>
#include <include/wally_symmetric.h>
#include <include/wally_transaction.h>
#include "secp256k1/include/secp256k1_generator.h"
#include "secp256k1/include/secp256k1_rangeproof.h"
#include "src/secp256k1/include/secp256k1_surjectionproof.h"
#include "src/secp256k1/include/secp256k1_whitelist.h"
#include <secp256k1_generator.h>
#include <secp256k1_rangeproof.h>
#include <secp256k1_surjectionproof.h>
#include <secp256k1_whitelist.h>


static const unsigned char LABEL_STR[] = {
Expand Down
8 changes: 4 additions & 4 deletions src/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif /* BUILD_ELEMENTS */
#endif /* WALLY_ABI_NO_ELEMENTS */
#include "secp256k1/include/secp256k1.h"
#include "secp256k1/include/secp256k1_recovery.h"
#include "secp256k1/include/secp256k1_extrakeys.h"
#include <secp256k1.h>
#include <secp256k1_recovery.h>
#include <secp256k1_extrakeys.h>
#ifndef BUILD_STANDARD_SECP
#include "secp256k1/include/secp256k1_ecdsa_s2c.h"
#include <secp256k1_ecdsa_s2c.h>
#endif
#include <config.h>
#if defined(HAVE_MEMSET_S)
Expand Down
2 changes: 1 addition & 1 deletion src/sign.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "internal.h"
#include <include/wally_crypto.h>
#include "script_int.h"
#include "secp256k1/include/secp256k1_schnorrsig.h"
#include <secp256k1_schnorrsig.h>
#include "ccan/ccan/build_assert/build_assert.h"

#define EC_FLAGS_TYPES (EC_FLAG_ECDSA | EC_FLAG_SCHNORR)
Expand Down

0 comments on commit b745937

Please sign in to comment.