Skip to content

Commit d48555b

Browse files
committed
Squashed 'src/secp256k1/' content from commit ad2028f
git-subtree-dir: src/secp256k1 git-subtree-split: ad2028f9890ca40bdd32055aa0fe5c1c9af0e485
0 parents  commit d48555b

Some content is hidden

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

55 files changed

+8393
-0
lines changed

.gitignore

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
bench_inv
2+
bench_sign
3+
bench_verify
4+
tests
5+
*.exe
6+
*.so
7+
*.a
8+
!.gitignore
9+
10+
Makefile
11+
configure
12+
.libs/
13+
Makefile.in
14+
aclocal.m4
15+
autom4te.cache/
16+
config.log
17+
config.status
18+
*.tar.gz
19+
*.la
20+
libtool
21+
.deps/
22+
.dirstamp
23+
build-aux/
24+
*.lo
25+
*.o
26+
*~
27+
src/libsecp256k1-config.h
28+
src/libsecp256k1-config.h.in
29+
m4/libtool.m4
30+
m4/ltoptions.m4
31+
m4/ltsugar.m4
32+
m4/ltversion.m4
33+
m4/lt~obsolete.m4
34+
src/stamp-h1
35+
libsecp256k1.pc

.travis.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: cpp
2+
compiler: gcc
3+
install:
4+
- sudo apt-get install -qq libssl-dev
5+
- if [ "$BIGNUM" = "gmp" -o "$BIGNUM" = "auto" -o "$FIELD" = "gmp" ]; then sudo apt-get install -qq libgmp-dev; fi
6+
- if [ "$FIELD" = "64bit_asm" ]; then sudo apt-get install -qq yasm; fi
7+
env:
8+
global:
9+
- FIELD=auto BIGNUM=auto SCALAR=auto ENDOMORPHISM=no BUILD=check EXTRAFLAGS=
10+
matrix:
11+
- SCALAR=32bit
12+
- SCALAR=64bit
13+
- FIELD=gmp
14+
- FIELD=gmp ENDOMORPHISM=yes
15+
- FIELD=64bit_asm
16+
- FIELD=64bit_asm ENDOMORPHISM=yes
17+
- FIELD=64bit
18+
- FIELD=64bit ENDOMORPHISM=yes
19+
- FIELD=32bit
20+
- FIELD=32bit ENDOMORPHISM=yes
21+
- BUILD=distcheck
22+
- EXTRAFLAGS=CFLAGS=-DDETERMINISTIC
23+
before_script: ./autogen.sh
24+
script: ./configure --enable-endomorphism=$ENDOMORPHISM --with-field=$FIELD --with-bignum=$BIGNUM --with-scalar=$SCALAR $EXTRAFLAGS && make -j2 $BUILD
25+
os: linux

COPYING

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2013 Pieter Wuille
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

Makefile.am

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
ACLOCAL_AMFLAGS = -I build-aux/m4
2+
3+
lib_LTLIBRARIES = libsecp256k1.la
4+
if USE_ASM
5+
COMMON_LIB = libsecp256k1_common.la
6+
else
7+
COMMON_LIB =
8+
endif
9+
noinst_LTLIBRARIES = $(COMMON_LIB)
10+
include_HEADERS = include/secp256k1.h
11+
noinst_HEADERS =
12+
noinst_HEADERS += src/scalar.h
13+
noinst_HEADERS += src/scalar_4x64.h
14+
noinst_HEADERS += src/scalar_8x32.h
15+
noinst_HEADERS += src/scalar_impl.h
16+
noinst_HEADERS += src/scalar_4x64_impl.h
17+
noinst_HEADERS += src/scalar_8x32_impl.h
18+
noinst_HEADERS += src/group.h
19+
noinst_HEADERS += src/group_impl.h
20+
noinst_HEADERS += src/num_gmp.h
21+
noinst_HEADERS += src/num_gmp_impl.h
22+
noinst_HEADERS += src/ecdsa.h
23+
noinst_HEADERS += src/ecdsa_impl.h
24+
noinst_HEADERS += src/eckey.h
25+
noinst_HEADERS += src/eckey_impl.h
26+
noinst_HEADERS += src/ecmult.h
27+
noinst_HEADERS += src/ecmult_impl.h
28+
noinst_HEADERS += src/ecmult_gen.h
29+
noinst_HEADERS += src/ecmult_gen_impl.h
30+
noinst_HEADERS += src/num.h
31+
noinst_HEADERS += src/num_impl.h
32+
noinst_HEADERS += src/field_10x26.h
33+
noinst_HEADERS += src/field_10x26_impl.h
34+
noinst_HEADERS += src/field_5x52.h
35+
noinst_HEADERS += src/field_5x52_impl.h
36+
noinst_HEADERS += src/field_5x52_int128_impl.h
37+
noinst_HEADERS += src/field_5x52_asm_impl.h
38+
noinst_HEADERS += src/java/org_bitcoin_NativeSecp256k1.h
39+
noinst_HEADERS += src/util.h
40+
noinst_HEADERS += src/testrand.h
41+
noinst_HEADERS += src/testrand_impl.h
42+
noinst_HEADERS += src/field_gmp.h
43+
noinst_HEADERS += src/field_gmp_impl.h
44+
noinst_HEADERS += src/field.h
45+
noinst_HEADERS += src/field_impl.h
46+
47+
pkgconfigdir = $(libdir)/pkgconfig
48+
pkgconfig_DATA = libsecp256k1.pc
49+
50+
if USE_ASM
51+
libsecp256k1_common_la_SOURCES = src/field_5x52_asm.asm
52+
endif
53+
54+
libsecp256k1_la_SOURCES = src/secp256k1.c
55+
libsecp256k1_la_CPPFLAGS = -I$(top_srcdir)/include $(SECP_INCLUDES)
56+
libsecp256k1_la_LIBADD = $(COMMON_LIB) $(SECP_LIBS)
57+
58+
59+
noinst_PROGRAMS =
60+
if USE_BENCHMARK
61+
noinst_PROGRAMS += bench_verify bench_sign bench_inv
62+
bench_verify_SOURCES = src/bench_verify.c
63+
bench_verify_LDADD = libsecp256k1.la $(SECP_LIBS)
64+
bench_verify_LDFLAGS = -static
65+
bench_sign_SOURCES = src/bench_sign.c
66+
bench_sign_LDADD = libsecp256k1.la $(SECP_LIBS)
67+
bench_sign_LDFLAGS = -static
68+
bench_inv_SOURCES = src/bench_inv.c
69+
bench_inv_LDADD = $(COMMON_LIB) $(SECP_LIBS)
70+
bench_inv_LDFLAGS = -static
71+
endif
72+
73+
if USE_TESTS
74+
noinst_PROGRAMS += tests
75+
tests_SOURCES = src/tests.c
76+
tests_CPPFLAGS = -DVERIFY $(SECP_TEST_INCLUDES)
77+
tests_LDADD = $(COMMON_LIB) $(SECP_LIBS) $(SECP_TEST_LIBS)
78+
tests_LDFLAGS = -static
79+
TESTS = tests
80+
endif
81+
82+
EXTRA_DIST = autogen.sh nasm_lt.sh
83+
84+
#x86_64 only
85+
if USE_ASM
86+
.asm.lo:
87+
$(LIBTOOL) --mode=compile --tag YASM $(srcdir)/nasm_lt.sh $(YASM) -f $(YASM_BINFMT) $(YAFLAGS) -I$(srcdir) -I. $< -o $@
88+
endif

README.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
libsecp256k1
2+
============
3+
4+
[![Build Status](https://travis-ci.org/bitcoin/secp256k1.svg?branch=master)](https://travis-ci.org/bitcoin/secp256k1)
5+
6+
Optimized C library for EC operations on curve secp256k1.
7+
8+
This library is experimental, so use at your own risk.
9+
10+
Features:
11+
* Low-level field and group operations on secp256k1.
12+
* ECDSA signing/verification and key generation.
13+
* Adding/multiplying private/public keys.
14+
* Serialization/parsing of private keys, public keys, signatures.
15+
* Very efficient implementation.
16+
17+
Implementation details
18+
----------------------
19+
20+
* General
21+
* Avoid dynamic memory usage almost everywhere.
22+
* Field operations
23+
* Optimized implementation of arithmetic modulo the curve's field size (2^256 - 0x1000003D1).
24+
* Using 5 52-bit limbs (including hand-optimized assembly for x86_64, by Diederik Huys).
25+
* Using 10 26-bit limbs.
26+
* Using GMP.
27+
* Field inverses and square roots using a sliding window over blocks of 1s (by Peter Dettman).
28+
* Scalar operations
29+
* Optimized implementation without data-dependent branches of arithmetic modulo the curve's order.
30+
* Using 4 64-bit limbs (relying on __int128 support in the compiler).
31+
* Using 8 32-bit limbs.
32+
* Group operations
33+
* Point addition formula specifically simplified for the curve equation (y^2 = x^3 + 7).
34+
* Use addition between points in Jacobian and affine coordinates where possible.
35+
* Use a unified addition/doubling formula where necessary to avoid data-dependent branches.
36+
* Point multiplication for verification (a*P + b*G).
37+
* Use wNAF notation for point multiplicands.
38+
* Use a much larger window for multiples of G, using precomputed multiples.
39+
* Use Shamir's trick to do the multiplication with the public key and the generator simultaneously.
40+
* Optionally use secp256k1's efficiently-computable endomorphism to split the multiplicands into 4 half-sized ones first.
41+
* Point multiplication for signing
42+
* Use a precomputed table of multiples of powers of 16 multiplied with the generator, so general multiplication becomes a series of additions.
43+
* Slice the precomputed table in memory per byte, so memory access to the table becomes uniform.
44+
* No data-dependent branches
45+
* The precomputed tables add and eventually subtract points for which no known scalar (private key) is known, preventing even an attacker with control over the private key used to control the data internally.
46+
47+
Build steps
48+
-----------
49+
50+
libsecp256k1 is built using autotools:
51+
52+
$ ./autogen.sh
53+
$ ./configure
54+
$ make
55+
$ sudo make install # optional

TODO

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* Unit tests for fieldelem/groupelem, including ones intended to
2+
trigger fieldelem's boundary cases.
3+
* Complete constant-time operations for signing/keygen

autogen.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
set -e
3+
autoreconf -if --warnings=all

build-aux/m4/bitcoin_secp.m4

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
dnl libsecp25k1 helper checks
2+
AC_DEFUN([SECP_INT128_CHECK],[
3+
has_int128=$ac_cv_type___int128
4+
if test x"$has_int128" != x"yes" && test x"$set_field" = x"64bit"; then
5+
AC_MSG_ERROR([$set_field field support explicitly requested but is not compatible with this host])
6+
fi
7+
if test x"$has_int128" != x"yes" && test x"$set_scalar" = x"64bit"; then
8+
AC_MSG_ERROR([$set_scalar scalar support explicitly requested but is not compatible with this host])
9+
fi
10+
])
11+
12+
dnl
13+
AC_DEFUN([SECP_64BIT_ASM_CHECK],[
14+
if test x"$host_cpu" == x"x86_64"; then
15+
AC_CHECK_PROG(YASM, yasm, yasm)
16+
else
17+
if test x"$set_field" = x"64bit_asm"; then
18+
AC_MSG_ERROR([$set_field field support explicitly requested but is not compatible with this host])
19+
fi
20+
fi
21+
if test x$YASM = x; then
22+
if test x"$set_field" = x"64bit_asm"; then
23+
AC_MSG_ERROR([$set_field field support explicitly requested but yasm was not found])
24+
fi
25+
has_64bit_asm=no
26+
else
27+
case x"$host_os" in
28+
xdarwin*)
29+
YASM_BINFMT=macho64
30+
;;
31+
x*-gnux32)
32+
YASM_BINFMT=elfx32
33+
;;
34+
*)
35+
YASM_BINFMT=elf64
36+
;;
37+
esac
38+
if $YASM -f help | grep -q $YASM_BINFMT; then
39+
has_64bit_asm=yes
40+
else
41+
if test x"$set_field" = x"64bit_asm"; then
42+
AC_MSG_ERROR([$set_field field support explicitly requested but yasm doesn't support $YASM_BINFMT format])
43+
fi
44+
AC_MSG_WARN([yasm too old for $YASM_BINFMT format])
45+
has_64bit_asm=no
46+
fi
47+
fi
48+
])
49+
50+
dnl
51+
AC_DEFUN([SECP_OPENSSL_CHECK],[
52+
if test x"$use_pkgconfig" = x"yes"; then
53+
: #NOP
54+
m4_ifdef([PKG_CHECK_MODULES],[
55+
PKG_CHECK_MODULES([CRYPTO], [libcrypto], [has_libcrypto=yes; AC_DEFINE(HAVE_LIBCRYPTO,1,[Define this symbol if libcrypto is installed])],[has_libcrypto=no])
56+
: #NOP
57+
])
58+
else
59+
AC_CHECK_HEADER(openssl/crypto.h,[AC_CHECK_LIB(crypto, main,[has_libcrypto=yes; CRYPTO_LIBS=-lcrypto; AC_DEFINE(HAVE_LIBCRYPTO,1,[Define this symbol if libcrypto is installed])]
60+
)])
61+
LIBS=
62+
fi
63+
if test x"$has_libcrypto" == x"yes" && test x"$has_openssl_ec" = x; then
64+
AC_MSG_CHECKING(for EC functions in libcrypto)
65+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
66+
#include <openssl/ec.h>
67+
#include <openssl/ecdsa.h>
68+
#include <openssl/obj_mac.h>]],[[
69+
EC_KEY *eckey = EC_KEY_new_by_curve_name(NID_secp256k1);
70+
ECDSA_sign(0, NULL, 0, NULL, NULL, eckey);
71+
ECDSA_verify(0, NULL, 0, NULL, 0, eckey);
72+
EC_KEY_free(eckey);
73+
]])],[has_openssl_ec=yes],[has_openssl_ec=no])
74+
AC_MSG_RESULT([$has_openssl_ec])
75+
fi
76+
])
77+
78+
dnl
79+
AC_DEFUN([SECP_GMP_CHECK],[
80+
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])])])
82+
fi
83+
if test x"$set_field" = x"gmp" && test x"$has_gmp" != x"yes"; then
84+
AC_MSG_ERROR([$set_field field support explicitly requested but libgmp was not found])
85+
fi
86+
if test x"$set_bignum" = x"gmp" && test x"$has_gmp" != x"yes"; then
87+
AC_MSG_ERROR([$set_bignum field support explicitly requested but libgmp was not found])
88+
fi
89+
])
90+

0 commit comments

Comments
 (0)