Skip to content

Commit 9e5626d

Browse files
committed
Update libsecp256k1 subtree to latest master
2 parents c2c4dba + 52380bf commit 9e5626d

39 files changed

+1586
-947
lines changed

src/secp256k1/.travis.yml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,29 @@ 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 SCHNORRSIG=no EXPERIMENTAL=no CTIMETEST=yes BENCH=yes ITERS=2
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
2121
matrix:
2222
- WIDEMUL=int64 RECOVERY=yes
2323
- WIDEMUL=int64 ECDH=yes EXPERIMENTAL=yes SCHNORRSIG=yes
24-
- WIDEMUL=int64 ENDOMORPHISM=yes
2524
- WIDEMUL=int128
2625
- WIDEMUL=int128 RECOVERY=yes EXPERIMENTAL=yes SCHNORRSIG=yes
27-
- WIDEMUL=int128 ENDOMORPHISM=yes
28-
- WIDEMUL=int128 ENDOMORPHISM=yes ECDH=yes EXPERIMENTAL=yes SCHNORRSIG=yes
26+
- WIDEMUL=int128 ECDH=yes EXPERIMENTAL=yes SCHNORRSIG=yes
2927
- WIDEMUL=int128 ASM=x86_64
30-
- WIDEMUL=int128 ENDOMORPHISM=yes ASM=x86_64
3128
- BIGNUM=no
32-
- BIGNUM=no ENDOMORPHISM=yes RECOVERY=yes EXPERIMENTAL=yes SCHNORRSIG=yes
29+
- BIGNUM=no RECOVERY=yes EXPERIMENTAL=yes SCHNORRSIG=yes
3330
- BIGNUM=no STATICPRECOMPUTATION=no
34-
- BUILD=distcheck CTIMETEST= BENCH=
31+
- BUILD=distcheck WITH_VALGRIND=no CTIMETEST=no BENCH=no
3532
- CPPFLAGS=-DDETERMINISTIC
36-
- CFLAGS=-O0 CTIMETEST=
33+
- CFLAGS=-O0 CTIMETEST=no
3734
- ECMULTGENPRECISION=2
3835
- ECMULTGENPRECISION=8
39-
- VALGRIND=yes ENDOMORPHISM=yes BIGNUM=no ASM=x86_64 EXPERIMENTAL=yes ECDH=yes RECOVERY=yes EXTRAFLAGS="--disable-openssl-tests" CPPFLAGS=-DVALGRIND BUILD=
40-
- VALGRIND=yes BIGNUM=no ASM=x86_64 EXPERIMENTAL=yes ECDH=yes RECOVERY=yes EXTRAFLAGS="--disable-openssl-tests" CPPFLAGS=-DVALGRIND BUILD=
36+
- RUN_VALGRIND=yes BIGNUM=no ASM=x86_64 EXPERIMENTAL=yes ECDH=yes RECOVERY=yes EXTRAFLAGS="--disable-openssl-tests" BUILD=
4137
matrix:
4238
fast_finish: true
4339
include:
4440
- compiler: clang
4541
os: linux
46-
env: HOST=i686-linux-gnu ENDOMORPHISM=yes
42+
env: HOST=i686-linux-gnu
4743
addons:
4844
apt:
4945
packages:
@@ -63,7 +59,7 @@ matrix:
6359
- libtool-bin
6460
- libc6-dbg:i386
6561
- compiler: gcc
66-
env: HOST=i686-linux-gnu ENDOMORPHISM=yes
62+
env: HOST=i686-linux-gnu
6763
os: linux
6864
addons:
6965
apt:

src/secp256k1/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)

src/secp256k1/configure.ac

Lines changed: 17 additions & 14 deletions
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,11 +116,6 @@ 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],
@@ -164,8 +159,7 @@ AC_ARG_WITH([asm], [AS_HELP_STRING([--with-asm=x86_64|arm|no|auto],
164159
AC_ARG_WITH([ecmult-window], [AS_HELP_STRING([--with-ecmult-window=SIZE|auto],
165160
[window size for ecmult precomputation for verification, specified as integer in range [2..24].]
166161
[Larger values result in possibly better performance at the cost of an exponentially larger precomputed table.]
167-
[The table will store 2^(SIZE-2) * 64 bytes of data but can be larger in memory due to platform-specific padding and alignment.]
168-
[If the endomorphism optimization is enabled, two tables of this size are used instead of only one.]
162+
[The table will store 2^(SIZE-1) * 64 bytes of data but can be larger in memory due to platform-specific padding and alignment.]
169163
["auto" is a reasonable setting for desktop machines (currently 15). [default=auto]]
170164
)],
171165
[req_ecmult_window=$withval], [req_ecmult_window=auto])
@@ -178,7 +172,21 @@ AC_ARG_WITH([ecmult-gen-precision], [AS_HELP_STRING([--with-ecmult-gen-precision
178172
)],
179173
[req_ecmult_gen_precision=$withval], [req_ecmult_gen_precision=auto])
180174

181-
AC_CHECK_HEADER([valgrind/memcheck.h], [enable_valgrind=yes], [enable_valgrind=no], [])
175+
AC_ARG_WITH([valgrind], [AS_HELP_STRING([--with-valgrind=yes|no|auto],
176+
[Build with extra checks for running inside Valgrind [default=auto]]
177+
)],
178+
[req_valgrind=$withval], [req_valgrind=auto])
179+
180+
if test x"$req_valgrind" = x"no"; then
181+
enable_valgrind=no
182+
else
183+
AC_CHECK_HEADER([valgrind/memcheck.h], [enable_valgrind=yes], [
184+
if test x"$req_valgrind" = x"yes"; then
185+
AC_MSG_ERROR([Valgrind support explicitly requested but valgrind/memcheck.h header not available])
186+
fi
187+
enable_valgrind=no
188+
], [])
189+
fi
182190
AM_CONDITIONAL([VALGRIND_ENABLED],[test "$enable_valgrind" = "yes"])
183191

184192
if test x"$enable_coverage" = x"yes"; then
@@ -415,10 +423,6 @@ if test x"$set_bignum" = x"gmp"; then
415423
SECP_INCLUDES="$SECP_INCLUDES $GMP_CPPFLAGS"
416424
fi
417425

418-
if test x"$use_endomorphism" = x"yes"; then
419-
AC_DEFINE(USE_ENDOMORPHISM, 1, [Define this symbol to use endomorphism optimization])
420-
fi
421-
422426
if test x"$set_precomp" = x"yes"; then
423427
AC_DEFINE(USE_ECMULT_STATIC_PRECOMPUTATION, 1, [Define this symbol to use a statically generated ecmult table])
424428
fi
@@ -500,7 +504,6 @@ AC_OUTPUT
500504

501505
echo
502506
echo "Build Options:"
503-
echo " with endomorphism = $use_endomorphism"
504507
echo " with ecmult precomp = $set_precomp"
505508
echo " with external callbacks = $use_external_default_callbacks"
506509
echo " with benchmarks = $use_benchmark"

src/secp256k1/contrib/travis.sh

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,28 @@ 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-schnorrsig="$SCHNORRSIG" \
21+
--with-valgrind="$WITH_VALGRIND" \
2122
--host="$HOST" $EXTRAFLAGS
2223

2324
if [ -n "$BUILD" ]
2425
then
2526
make -j2 "$BUILD"
2627
fi
27-
if [ -n "$VALGRIND" ]
28+
if [ "$RUN_VALGRIND" = "yes" ]
2829
then
2930
make -j2
3031
# 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)
3132
valgrind --error-exitcode=42 ./tests 16
3233
valgrind --error-exitcode=42 ./exhaustive_tests
3334
fi
34-
if [ -n "$BENCH" ]
35+
if [ "$BENCH" = "yes" ]
3536
then
36-
if [ -n "$VALGRIND" ]
37+
if [ "$RUN_VALGRIND" = "yes" ]
3738
then
3839
# Using the local `libtool` because on macOS the system's libtool has nothing to do with GNU libtool
3940
EXEC='./libtool --mode=execute valgrind --error-exitcode=42'
@@ -56,8 +57,12 @@ then
5657
then
5758
$EXEC ./bench_ecdh >> bench.log 2>&1
5859
fi
60+
if [ "$SCHNORRSIG" = "yes" ]
61+
then
62+
$EXEC ./bench_schnorrsig >> bench.log 2>&1
63+
fi
5964
fi
60-
if [ -n "$CTIMETEST" ]
65+
if [ "$CTIMETEST" = "yes" ]
6166
then
6267
./libtool --mode=execute valgrind --error-exitcode=42 ./valgrind_ctime_test > valgrind_ctime_test.log 2>&1
6368
fi
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Define field size and field
2+
P = 2^256 - 2^32 - 977
3+
F = GF(P)
4+
BETA = F(0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee)
5+
6+
assert(BETA != F(1) and BETA^3 == F(1))
7+
8+
orders_done = set()
9+
results = {}
10+
first = True
11+
for b in range(1, P):
12+
# There are only 6 curves (up to isomorphism) of the form y^2=x^3+B. Stop once we have tried all.
13+
if len(orders_done) == 6:
14+
break
15+
16+
E = EllipticCurve(F, [0, b])
17+
print("Analyzing curve y^2 = x^3 + %i" % b)
18+
n = E.order()
19+
# Skip curves with an order we've already tried
20+
if n in orders_done:
21+
print("- Isomorphic to earlier curve")
22+
continue
23+
orders_done.add(n)
24+
# Skip curves isomorphic to the real secp256k1
25+
if n.is_pseudoprime():
26+
print(" - Isomorphic to secp256k1")
27+
continue
28+
29+
print("- Finding subgroups")
30+
31+
# Find what prime subgroups exist
32+
for f, _ in n.factor():
33+
print("- Analyzing subgroup of order %i" % f)
34+
# Skip subgroups of order >1000
35+
if f < 4 or f > 1000:
36+
print(" - Bad size")
37+
continue
38+
39+
# Iterate over X coordinates until we find one that is on the curve, has order f,
40+
# and for which curve isomorphism exists that maps it to X coordinate 1.
41+
for x in range(1, P):
42+
# Skip X coordinates not on the curve, and construct the full point otherwise.
43+
if not E.is_x_coord(x):
44+
continue
45+
G = E.lift_x(F(x))
46+
47+
print(" - Analyzing (multiples of) point with X=%i" % x)
48+
49+
# Skip points whose order is not a multiple of f. Project the point to have
50+
# order f otherwise.
51+
if (G.order() % f):
52+
print(" - Bad order")
53+
continue
54+
G = G * (G.order() // f)
55+
56+
# Find lambda for endomorphism. Skip if none can be found.
57+
lam = None
58+
for l in Integers(f)(1).nth_root(3, all=True):
59+
if int(l)*G == E(BETA*G[0], G[1]):
60+
lam = int(l)
61+
break
62+
if lam is None:
63+
print(" - No endomorphism for this subgroup")
64+
break
65+
66+
# Now look for an isomorphism of the curve that gives this point an X
67+
# coordinate equal to 1.
68+
# If (x,y) is on y^2 = x^3 + b, then (a^2*x, a^3*y) is on y^2 = x^3 + a^6*b.
69+
# So look for m=a^2=1/x.
70+
m = F(1)/G[0]
71+
if not m.is_square():
72+
print(" - No curve isomorphism maps it to a point with X=1")
73+
continue
74+
a = m.sqrt()
75+
rb = a^6*b
76+
RE = EllipticCurve(F, [0, rb])
77+
78+
# Use as generator twice the image of G under the above isormorphism.
79+
# This means that generator*(1/2 mod f) will have X coordinate 1.
80+
RG = RE(1, a^3*G[1]) * 2
81+
# And even Y coordinate.
82+
if int(RG[1]) % 2:
83+
RG = -RG
84+
assert(RG.order() == f)
85+
assert(lam*RG == RE(BETA*RG[0], RG[1]))
86+
87+
# We have found curve RE:y^2=x^3+rb with generator RG of order f. Remember it
88+
results[f] = {"b": rb, "G": RG, "lambda": lam}
89+
print(" - Found solution")
90+
break
91+
92+
print("")
93+
94+
print("")
95+
print("")
96+
print("/* To be put in src/group_impl.h: */")
97+
first = True
98+
for f in sorted(results.keys()):
99+
b = results[f]["b"]
100+
G = results[f]["G"]
101+
print("# %s EXHAUSTIVE_TEST_ORDER == %i" % ("if" if first else "elif", f))
102+
first = False
103+
print("static const secp256k1_ge secp256k1_ge_const_g = SECP256K1_GE_CONST(")
104+
print(" 0x%08x, 0x%08x, 0x%08x, 0x%08x," % tuple((int(G[0]) >> (32 * (7 - i))) & 0xffffffff for i in range(4)))
105+
print(" 0x%08x, 0x%08x, 0x%08x, 0x%08x," % tuple((int(G[0]) >> (32 * (7 - i))) & 0xffffffff for i in range(4, 8)))
106+
print(" 0x%08x, 0x%08x, 0x%08x, 0x%08x," % tuple((int(G[1]) >> (32 * (7 - i))) & 0xffffffff for i in range(4)))
107+
print(" 0x%08x, 0x%08x, 0x%08x, 0x%08x" % tuple((int(G[1]) >> (32 * (7 - i))) & 0xffffffff for i in range(4, 8)))
108+
print(");")
109+
print("static const secp256k1_fe secp256k1_fe_const_b = SECP256K1_FE_CONST(")
110+
print(" 0x%08x, 0x%08x, 0x%08x, 0x%08x," % tuple((int(b) >> (32 * (7 - i))) & 0xffffffff for i in range(4)))
111+
print(" 0x%08x, 0x%08x, 0x%08x, 0x%08x" % tuple((int(b) >> (32 * (7 - i))) & 0xffffffff for i in range(4, 8)))
112+
print(");")
113+
print("# else")
114+
print("# error No known generator for the specified exhaustive test group order.")
115+
print("# endif")
116+
117+
print("")
118+
print("")
119+
print("/* To be put in src/scalar_impl.h: */")
120+
first = True
121+
for f in sorted(results.keys()):
122+
lam = results[f]["lambda"]
123+
print("# %s EXHAUSTIVE_TEST_ORDER == %i" % ("if" if first else "elif", f))
124+
first = False
125+
print("# define EXHAUSTIVE_TEST_LAMBDA %i" % lam)
126+
print("# else")
127+
print("# error No known lambda for the specified exhaustive test group order.")
128+
print("# endif")
129+
print("")

src/secp256k1/src/assumptions.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef SECP256K1_ASSUMPTIONS_H
88
#define SECP256K1_ASSUMPTIONS_H
99

10+
#include <limits.h>
11+
1012
#include "util.h"
1113

1214
/* This library, like most software, relies on a number of compiler implementation defined (but not undefined)
@@ -19,7 +21,11 @@ struct secp256k1_assumption_checker {
1921
allowed. */
2022
int dummy_array[(
2123
/* Bytes are 8 bits. */
22-
CHAR_BIT == 8 &&
24+
(CHAR_BIT == 8) &&
25+
26+
/* No integer promotion for uint32_t. This ensures that we can multiply uintXX_t values where XX >= 32
27+
without signed overflow, which would be undefined behaviour. */
28+
(UINT_MAX <= UINT32_MAX) &&
2329

2430
/* Conversions from unsigned to signed outside of the bounds of the signed type are
2531
implementation-defined. Verify that they function as reinterpreting the lower

src/secp256k1/src/basic-config.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#undef USE_ASM_X86_64
1313
#undef USE_ECMULT_STATIC_PRECOMPUTATION
14-
#undef USE_ENDOMORPHISM
1514
#undef USE_EXTERNAL_ASM
1615
#undef USE_EXTERNAL_DEFAULT_CALLBACKS
1716
#undef USE_FIELD_INV_BUILTIN

src/secp256k1/src/bench_internal.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ void bench_scalar_mul(void* arg, int iters) {
117117
}
118118
}
119119

120-
#ifdef USE_ENDOMORPHISM
121120
void bench_scalar_split(void* arg, int iters) {
122121
int i, j = 0;
123122
bench_inv *data = (bench_inv*)arg;
@@ -128,7 +127,6 @@ void bench_scalar_split(void* arg, int iters) {
128127
}
129128
CHECK(j <= iters);
130129
}
131-
#endif
132130

133131
void bench_scalar_inverse(void* arg, int iters) {
134132
int i, j = 0;
@@ -397,9 +395,7 @@ int main(int argc, char **argv) {
397395
if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "negate")) run_benchmark("scalar_negate", bench_scalar_negate, bench_setup, NULL, &data, 10, iters*100);
398396
if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "sqr")) run_benchmark("scalar_sqr", bench_scalar_sqr, bench_setup, NULL, &data, 10, iters*10);
399397
if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "mul")) run_benchmark("scalar_mul", bench_scalar_mul, bench_setup, NULL, &data, 10, iters*10);
400-
#ifdef USE_ENDOMORPHISM
401398
if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "split")) run_benchmark("scalar_split", bench_scalar_split, bench_setup, NULL, &data, 10, iters);
402-
#endif
403399
if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "inverse")) run_benchmark("scalar_inverse", bench_scalar_inverse, bench_setup, NULL, &data, 10, 2000);
404400
if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "inverse")) run_benchmark("scalar_inverse_var", bench_scalar_inverse_var, bench_setup, NULL, &data, 10, 2000);
405401

src/secp256k1/src/ecmult.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
typedef struct {
1616
/* For accelerating the computation of a*P + b*G: */
1717
secp256k1_ge_storage (*pre_g)[]; /* odd multiples of the generator */
18-
#ifdef USE_ENDOMORPHISM
1918
secp256k1_ge_storage (*pre_g_128)[]; /* odd multiples of 2^128*generator */
20-
#endif
2119
} secp256k1_ecmult_context;
2220

2321
static const size_t SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;

0 commit comments

Comments
 (0)