Skip to content

Commit 87bddb7

Browse files
committed
Squashed 'src/secp256k1/' changes from ad2028f..b0210a9
b0210a9 Merge pull request bitcoin-core#135 ee3eb4b Fix a memory leak and add a number of small tests. 4d879a3 Merge pull request bitcoin-core#134 d5e8362 Merge pull request bitcoin-core#127 7b92cf6 Merge pull request bitcoin-core#132 0bf70a5 Merge pull request bitcoin-core#133 29ae131 Make scalar_add_bit test's overflow detection exact 9048def Avoid undefined shift behaviour efb7d4b Use constant-time conditional moves instead of byte slicing d220062 Merge pull request bitcoin-core#131 82f9254 Fix typo 601ca04 Merge pull request bitcoin-core#129 35399e0 Bugfix: b is restricted, not r c35ff1e Convert lambda splitter to pure scalar code. cc604e9 Avoid division when decomposing scalars ff8746d Add secp256k1_scalar_mul_shift_var bd313f7 Merge pull request bitcoin-core#119 276f987 Merge pull request bitcoin-core#124 25d125e Merge pull request bitcoin-core#126 24b3c65 Add a test case for ECDSA recomputing infinity 32600e5 Add a test for r >= order signature handling 4d4eeea Make secp256k1_fe_mul_inner use the r != property be82e92 Require that r and b are different for field multiplication. 597128d Make num optional 659b554 Make constant initializers independent from num 0af5b47 Merge pull request bitcoin-core#120 e2e8a36 Merge pull request bitcoin-core#117 c76be9e Remove unused num functions 4285a98 Move lambda-splitting code to scalar. f24041d Switch all EC/ECDSA logic from num to scalar 6794be6 Add scalar splitting functions d1502eb Add secp256k1_scalar_inverse_var which delegates to GMP b5c9ee7 Make test_point_times_order test meaningful again 0b73059 Switch wnaf splitting from num-based to scalar-based 1e6c77c Generalize secp256k1_scalar_get_bits 5213207 Add secp256k1_scalar_add_bit 3c0ae43 Merge pull request bitcoin-core#122 6e05287 Do signature recovery/verification with 4 possible recid case e3d692f Explain why no y=0 check is necessary for doubling f7dc1c6 Optimize doubling: secp256k1 has no y=0 point 666d3b5 Merge pull request bitcoin-core#121 2a54f9b Correct typo in comment 9d64145 Merge pull request bitcoin-core#114 99f0728 Fix secp256k1_num_set_bin handling of 0 d907ebc Add bounds checking to field element setters bb2cd94 Merge pull request bitcoin-core#116 665775b Don't split the g factor when not using endomorphism 9431d6b Merge pull request bitcoin-core#115 e2274c5 build: osx: attempt to work with homebrew keg-only packages git-subtree-dir: src/secp256k1 git-subtree-split: b0210a95da433e048a11d298efbcc14eb423c95f
1 parent d48555b commit 87bddb7

30 files changed

+1241
-773
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ env:
1818
- FIELD=64bit ENDOMORPHISM=yes
1919
- FIELD=32bit
2020
- FIELD=32bit ENDOMORPHISM=yes
21+
- BIGNUM=none
22+
- BIGNUM=none ENDOMORPHISM=yes
2123
- BUILD=distcheck
2224
- EXTRAFLAGS=CFLAGS=-DDETERMINISTIC
2325
before_script: ./autogen.sh

Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ bench_sign_LDFLAGS = -static
6868
bench_inv_SOURCES = src/bench_inv.c
6969
bench_inv_LDADD = $(COMMON_LIB) $(SECP_LIBS)
7070
bench_inv_LDFLAGS = -static
71+
bench_inv_CPPFLAGS = $(SECP_INCLUDES)
7172
endif
7273

7374
if USE_TESTS
7475
noinst_PROGRAMS += tests
7576
tests_SOURCES = src/tests.c
76-
tests_CPPFLAGS = -DVERIFY $(SECP_TEST_INCLUDES)
77+
tests_CPPFLAGS = -DVERIFY $(SECP_INCLUDES) $(SECP_TEST_INCLUDES)
7778
tests_LDADD = $(COMMON_LIB) $(SECP_LIBS) $(SECP_TEST_LIBS)
7879
tests_LDFLAGS = -static
7980
TESTS = tests

build-aux/m4/bitcoin_secp.m4

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ fi
7878
dnl
7979
AC_DEFUN([SECP_GMP_CHECK],[
8080
if test x"$has_gmp" != x"yes"; then
81-
AC_CHECK_HEADER(gmp.h,[AC_CHECK_LIB(gmp, __gmpz_init,[has_gmp=yes; GMP_LIBS=-lgmp; AC_DEFINE(HAVE_LIBGMP,1,[Define this symbol if libgmp is installed])])])
81+
CPPFLAGS_TEMP="$CPPFLAGS"
82+
CPPFLAGS="$GMP_CPPFLAGS $CPPFLAGS"
83+
LIBS_TEMP="$LIBS"
84+
LIBS="$GMP_LIBS $LIBS"
85+
AC_CHECK_HEADER(gmp.h,[AC_CHECK_LIB(gmp, __gmpz_init,[has_gmp=yes; GMP_LIBS="$GMP_LIBS -lgmp"; AC_DEFINE(HAVE_LIBGMP,1,[Define this symbol if libgmp is installed])])])
86+
CPPFLAGS="$CPPFLAGS_TEMP"
87+
LIBS="$LIBS_TEMP"
8288
fi
8389
if test x"$set_field" = x"gmp" && test x"$has_gmp" != x"yes"; then
8490
AC_MSG_ERROR([$set_field field support explicitly requested but libgmp was not found])

configure.ac

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,35 @@ case $host in
3333
esac
3434

3535
case $host_os in
36-
darwin*)
37-
CPPFLAGS="$CPPFLAGS -I/opt/local/include"
38-
LDFLAGS="$LDFLAGS -L/opt/local/lib"
39-
;;
36+
*darwin*)
37+
if test x$cross_compiling != xyes; then
38+
AC_PATH_PROG([BREW],brew,)
39+
if test x$BREW != x; then
40+
dnl These Homebrew packages may be keg-only, meaning that they won't be found
41+
dnl in expected paths because they may conflict with system files. Ask
42+
dnl Homebrew where each one is located, then adjust paths accordingly.
43+
44+
openssl_prefix=`$BREW --prefix openssl 2>/dev/null`
45+
gmp_prefix=`$BREW --prefix gmp 2>/dev/null`
46+
if test x$openssl_prefix != x; then
47+
PKG_CONFIG_PATH="$openssl_prefix/lib/pkgconfig:$PKG_CONFIG_PATH"
48+
export PKG_CONFIG_PATH
49+
fi
50+
if test x$gmp_prefix != x; then
51+
GMP_CPPFLAGS="-I$gmp_prefix/include"
52+
GMP_LIBS="-L$gmp_prefix/lib"
53+
fi
54+
else
55+
AC_PATH_PROG([PORT],port,)
56+
dnl if homebrew isn't installed and macports is, add the macports default paths
57+
dnl as a last resort.
58+
if test x$PORT != x; then
59+
CPPFLAGS="$CPPFLAGS -isystem /opt/local/include"
60+
LDFLAGS="$LDFLAGS -L/opt/local/lib"
61+
fi
62+
fi
63+
fi
64+
;;
4065
esac
4166

4267
CFLAGS="$CFLAGS -W"
@@ -70,7 +95,7 @@ AC_ARG_ENABLE(endomorphism,
7095
AC_ARG_WITH([field], [AS_HELP_STRING([--with-field=gmp|64bit|64bit_asm|32bit|auto],
7196
[Specify Field Implementation. Default is auto])],[req_field=$withval], [req_field=auto])
7297

73-
AC_ARG_WITH([bignum], [AS_HELP_STRING([--with-bignum=gmp|auto],
98+
AC_ARG_WITH([bignum], [AS_HELP_STRING([--with-bignum=gmp|none|auto],
7499
[Specify Bignum Implementation. Default is auto])],[req_bignum=$withval], [req_bignum=auto])
75100

76101
AC_ARG_WITH([scalar], [AS_HELP_STRING([--with-scalar=64bit|32bit|auto],
@@ -154,16 +179,15 @@ if test x"$req_bignum" = x"auto"; then
154179
fi
155180

156181
if test x"$set_bignum" = x; then
157-
AC_MSG_ERROR([no working bignum implementation found])
182+
set_bignum=none
158183
fi
159184
else
160185
set_bignum=$req_bignum
161186
case $set_bignum in
162187
gmp)
163188
SECP_GMP_CHECK
164189
;;
165-
openssl)
166-
SECP_OPENSSL_CHECK
190+
none)
167191
;;
168192
*)
169193
AC_MSG_ERROR([invalid bignum implementation selection])
@@ -196,9 +220,15 @@ esac
196220
# select bignum implementation
197221
case $set_bignum in
198222
gmp)
199-
AC_DEFINE(HAVE_LIBGMP,1,[Define this symbol if libgmp is installed])
200-
AC_DEFINE(USE_NUM_GMP, 1, [Define this symbol to use the gmp implementation])
201-
AC_DEFINE(USE_FIELD_INV_NUM, 1, [Define this symbol to use the USE_FIELD_INV_NUM implementation])
223+
AC_DEFINE(HAVE_LIBGMP, 1, [Define this symbol if libgmp is installed])
224+
AC_DEFINE(USE_NUM_GMP, 1, [Define this symbol to use the gmp implementation for num])
225+
AC_DEFINE(USE_FIELD_INV_NUM, 1, [Define this symbol to use the num-based field inverse implementation])
226+
AC_DEFINE(USE_SCALAR_INV_NUM, 1, [Define this symbol to use the num-based scalar inverse implementation])
227+
;;
228+
none)
229+
AC_DEFINE(USE_NUM_NONE, 1, [Define this symbol to use no num implementation])
230+
AC_DEFINE(USE_FIELD_INV_BUILTIN, 1, [Define this symbol to use the native field inverse implementation])
231+
AC_DEFINE(USE_SCALAR_INV_BUILTIN, 1, [Define this symbol to use the native scalar inverse implementation])
202232
;;
203233
*)
204234
AC_MSG_ERROR([invalid bignum implementation])
@@ -236,10 +266,11 @@ fi
236266

237267
if test x"$set_field" = x"gmp" || test x"$set_bignum" = x"gmp"; then
238268
SECP_LIBS="$SECP_LIBS $GMP_LIBS"
269+
SECP_INCLUDES="$SECP_INCLUDES $GMP_CPPFLAGS"
239270
fi
240271

241272
if test x"$use_endomorphism" = x"yes"; then
242-
AC_DEFINE(USE_ENDOMORPHISM, 1, [Define this symbol to use endomorphism])
273+
AC_DEFINE(USE_ENDOMORPHISM, 1, [Define this symbol to use endomorphism optimization])
243274
fi
244275

245276
AC_MSG_NOTICE([Using field implementation: $set_field])
@@ -256,4 +287,10 @@ AC_SUBST(YASM_BINFMT)
256287
AM_CONDITIONAL([USE_ASM], [test x"$set_field" == x"64bit_asm"])
257288
AM_CONDITIONAL([USE_TESTS], [test x"$use_tests" != x"no"])
258289
AM_CONDITIONAL([USE_BENCHMARK], [test x"$use_benchmark" != x"no"])
290+
291+
dnl make sure nothing new is exported so that we don't break the cache
292+
PKGCONFIG_PATH_TEMP="$PKG_CONFIG_PATH"
293+
unset PKG_CONFIG_PATH
294+
PKG_CONFIG_PATH="$PKGCONFIG_PATH_TEMP"
295+
259296
AC_OUTPUT

include/secp256k1.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,6 @@ extern "C" {
1414
# endif
1515
# endif
1616

17-
# if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
18-
# if SECP256K1_GNUC_PREREQ(3,0)
19-
# define SECP256K1_RESTRICT __restrict__
20-
# elif (defined(_MSC_VER) && _MSC_VER >= 1400)
21-
# define SECP256K1_RESTRICT __restrict
22-
# else
23-
# define SECP256K1_RESTRICT
24-
# endif
25-
# else
26-
# define SECP256K1_RESTRICT restrict
27-
# endif
28-
2917
# if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
3018
# if SECP256K1_GNUC_PREREQ(2,7)
3119
# define SECP256K1_INLINE __inline__

src/ecdsa.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@
77
#ifndef _SECP256K1_ECDSA_
88
#define _SECP256K1_ECDSA_
99

10-
#include "num.h"
10+
#include "scalar.h"
11+
#include "group.h"
12+
13+
static void secp256k1_ecsda_start(void);
14+
static void secp256k1_ecdsa_stop(void);
1115

1216
typedef struct {
13-
secp256k1_num_t r, s;
17+
secp256k1_scalar_t r, s;
1418
} secp256k1_ecdsa_sig_t;
1519

1620
static int secp256k1_ecdsa_sig_parse(secp256k1_ecdsa_sig_t *r, const unsigned char *sig, int size);
1721
static int secp256k1_ecdsa_sig_serialize(unsigned char *sig, int *size, const secp256k1_ecdsa_sig_t *a);
18-
static int secp256k1_ecdsa_sig_verify(const secp256k1_ecdsa_sig_t *sig, const secp256k1_ge_t *pubkey, const secp256k1_num_t *message);
22+
static int secp256k1_ecdsa_sig_verify(const secp256k1_ecdsa_sig_t *sig, const secp256k1_ge_t *pubkey, const secp256k1_scalar_t *message);
1923
static int secp256k1_ecdsa_sig_sign(secp256k1_ecdsa_sig_t *sig, const secp256k1_scalar_t *seckey, const secp256k1_scalar_t *message, const secp256k1_scalar_t *nonce, int *recid);
20-
static int secp256k1_ecdsa_sig_recover(const secp256k1_ecdsa_sig_t *sig, secp256k1_ge_t *pubkey, const secp256k1_num_t *message, int recid);
21-
static void secp256k1_ecdsa_sig_set_rs(secp256k1_ecdsa_sig_t *sig, const secp256k1_num_t *r, const secp256k1_num_t *s);
24+
static int secp256k1_ecdsa_sig_recover(const secp256k1_ecdsa_sig_t *sig, secp256k1_ge_t *pubkey, const secp256k1_scalar_t *message, int recid);
25+
static void secp256k1_ecdsa_sig_set_rs(secp256k1_ecdsa_sig_t *sig, const secp256k1_scalar_t *r, const secp256k1_scalar_t *s);
2226

2327
#endif

0 commit comments

Comments
 (0)