Skip to content

Commit 8a901f9

Browse files
committed
Squashed 'src/secp256k1/' content from commit 452d8e4
git-subtree-dir: src/secp256k1 git-subtree-split: 452d8e4
0 parents  commit 8a901f9

Some content is hidden

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

89 files changed

+22858
-0
lines changed

.gitignore

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
bench_inv
2+
bench_ecdh
3+
bench_ecmult
4+
bench_sign
5+
bench_verify
6+
bench_schnorr_verify
7+
bench_recover
8+
bench_internal
9+
tests
10+
exhaustive_tests
11+
gen_context
12+
*.exe
13+
*.so
14+
*.a
15+
!.gitignore
16+
17+
Makefile
18+
configure
19+
.libs/
20+
Makefile.in
21+
aclocal.m4
22+
autom4te.cache/
23+
config.log
24+
config.status
25+
*.tar.gz
26+
*.la
27+
libtool
28+
.deps/
29+
.dirstamp
30+
*.lo
31+
*.o
32+
*~
33+
src/libsecp256k1-config.h
34+
src/libsecp256k1-config.h.in
35+
src/ecmult_static_context.h
36+
build-aux/config.guess
37+
build-aux/config.sub
38+
build-aux/depcomp
39+
build-aux/install-sh
40+
build-aux/ltmain.sh
41+
build-aux/m4/libtool.m4
42+
build-aux/m4/lt~obsolete.m4
43+
build-aux/m4/ltoptions.m4
44+
build-aux/m4/ltsugar.m4
45+
build-aux/m4/ltversion.m4
46+
build-aux/missing
47+
build-aux/compile
48+
build-aux/test-driver
49+
src/stamp-h1
50+
libsecp256k1.pc

.travis.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
language: c
2+
sudo: false
3+
addons:
4+
apt:
5+
packages: libgmp-dev
6+
compiler:
7+
- clang
8+
- gcc
9+
cache:
10+
directories:
11+
- src/java/guava/
12+
env:
13+
global:
14+
- FIELD=auto BIGNUM=auto SCALAR=auto ENDOMORPHISM=no STATICPRECOMPUTATION=yes ASM=no BUILD=check EXTRAFLAGS= HOST= ECDH=no RECOVERY=no EXPERIMENTAL=no JNI=no
15+
- GUAVA_URL=https://search.maven.org/remotecontent?filepath=com/google/guava/guava/18.0/guava-18.0.jar GUAVA_JAR=src/java/guava/guava-18.0.jar
16+
matrix:
17+
- SCALAR=32bit RECOVERY=yes
18+
- SCALAR=32bit FIELD=32bit ECDH=yes EXPERIMENTAL=yes
19+
- SCALAR=64bit
20+
- FIELD=64bit RECOVERY=yes
21+
- FIELD=64bit ENDOMORPHISM=yes
22+
- FIELD=64bit ENDOMORPHISM=yes ECDH=yes EXPERIMENTAL=yes
23+
- FIELD=64bit ASM=x86_64
24+
- FIELD=64bit ENDOMORPHISM=yes ASM=x86_64
25+
- FIELD=32bit ENDOMORPHISM=yes
26+
- BIGNUM=no
27+
- BIGNUM=no ENDOMORPHISM=yes RECOVERY=yes EXPERIMENTAL=yes
28+
- BIGNUM=no STATICPRECOMPUTATION=no
29+
- BUILD=distcheck
30+
- EXTRAFLAGS=CPPFLAGS=-DDETERMINISTIC
31+
- EXTRAFLAGS=CFLAGS=-O0
32+
- BUILD=check-java JNI=yes ECDH=yes EXPERIMENTAL=yes
33+
matrix:
34+
fast_finish: true
35+
include:
36+
- compiler: clang
37+
env: HOST=i686-linux-gnu ENDOMORPHISM=yes
38+
addons:
39+
apt:
40+
packages:
41+
- gcc-multilib
42+
- libgmp-dev:i386
43+
- compiler: clang
44+
env: HOST=i686-linux-gnu
45+
addons:
46+
apt:
47+
packages:
48+
- gcc-multilib
49+
- compiler: gcc
50+
env: HOST=i686-linux-gnu ENDOMORPHISM=yes
51+
addons:
52+
apt:
53+
packages:
54+
- gcc-multilib
55+
- compiler: gcc
56+
env: HOST=i686-linux-gnu
57+
addons:
58+
apt:
59+
packages:
60+
- gcc-multilib
61+
- libgmp-dev:i386
62+
before_install: mkdir -p `dirname $GUAVA_JAR`
63+
install: if [ ! -f $GUAVA_JAR ]; then wget $GUAVA_URL -O $GUAVA_JAR; fi
64+
before_script: ./autogen.sh
65+
script:
66+
- if [ -n "$HOST" ]; then export USE_HOST="--host=$HOST"; fi
67+
- if [ "x$HOST" = "xi686-linux-gnu" ]; then export CC="$CC -m32"; fi
68+
- ./configure --enable-experimental=$EXPERIMENTAL --enable-endomorphism=$ENDOMORPHISM --with-field=$FIELD --with-bignum=$BIGNUM --with-scalar=$SCALAR --enable-ecmult-static-precomputation=$STATICPRECOMPUTATION --enable-module-ecdh=$ECDH --enable-module-recovery=$RECOVERY --enable-jni=$JNI $EXTRAFLAGS $USE_HOST && make -j2 $BUILD
69+
os: linux

COPYING

Lines changed: 19 additions & 0 deletions
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

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
ACLOCAL_AMFLAGS = -I build-aux/m4
2+
3+
lib_LTLIBRARIES = libsecp256k1.la
4+
if USE_JNI
5+
JNI_LIB = libsecp256k1_jni.la
6+
noinst_LTLIBRARIES = $(JNI_LIB)
7+
else
8+
JNI_LIB =
9+
endif
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_low.h
16+
noinst_HEADERS += src/scalar_impl.h
17+
noinst_HEADERS += src/scalar_4x64_impl.h
18+
noinst_HEADERS += src/scalar_8x32_impl.h
19+
noinst_HEADERS += src/scalar_low_impl.h
20+
noinst_HEADERS += src/group.h
21+
noinst_HEADERS += src/group_impl.h
22+
noinst_HEADERS += src/num_gmp.h
23+
noinst_HEADERS += src/num_gmp_impl.h
24+
noinst_HEADERS += src/ecdsa.h
25+
noinst_HEADERS += src/ecdsa_impl.h
26+
noinst_HEADERS += src/eckey.h
27+
noinst_HEADERS += src/eckey_impl.h
28+
noinst_HEADERS += src/ecmult.h
29+
noinst_HEADERS += src/ecmult_impl.h
30+
noinst_HEADERS += src/ecmult_const.h
31+
noinst_HEADERS += src/ecmult_const_impl.h
32+
noinst_HEADERS += src/ecmult_gen.h
33+
noinst_HEADERS += src/ecmult_gen_impl.h
34+
noinst_HEADERS += src/num.h
35+
noinst_HEADERS += src/num_impl.h
36+
noinst_HEADERS += src/field_10x26.h
37+
noinst_HEADERS += src/field_10x26_impl.h
38+
noinst_HEADERS += src/field_5x52.h
39+
noinst_HEADERS += src/field_5x52_impl.h
40+
noinst_HEADERS += src/field_5x52_int128_impl.h
41+
noinst_HEADERS += src/field_5x52_asm_impl.h
42+
noinst_HEADERS += src/java/org_bitcoin_NativeSecp256k1.h
43+
noinst_HEADERS += src/java/org_bitcoin_Secp256k1Context.h
44+
noinst_HEADERS += src/util.h
45+
noinst_HEADERS += src/scratch.h
46+
noinst_HEADERS += src/scratch_impl.h
47+
noinst_HEADERS += src/testrand.h
48+
noinst_HEADERS += src/testrand_impl.h
49+
noinst_HEADERS += src/hash.h
50+
noinst_HEADERS += src/hash_impl.h
51+
noinst_HEADERS += src/field.h
52+
noinst_HEADERS += src/field_impl.h
53+
noinst_HEADERS += src/bench.h
54+
noinst_HEADERS += contrib/lax_der_parsing.h
55+
noinst_HEADERS += contrib/lax_der_parsing.c
56+
noinst_HEADERS += contrib/lax_der_privatekey_parsing.h
57+
noinst_HEADERS += contrib/lax_der_privatekey_parsing.c
58+
59+
if USE_EXTERNAL_ASM
60+
COMMON_LIB = libsecp256k1_common.la
61+
noinst_LTLIBRARIES = $(COMMON_LIB)
62+
else
63+
COMMON_LIB =
64+
endif
65+
66+
pkgconfigdir = $(libdir)/pkgconfig
67+
pkgconfig_DATA = libsecp256k1.pc
68+
69+
if USE_EXTERNAL_ASM
70+
if USE_ASM_ARM
71+
libsecp256k1_common_la_SOURCES = src/asm/field_10x26_arm.s
72+
endif
73+
endif
74+
75+
libsecp256k1_la_SOURCES = src/secp256k1.c
76+
libsecp256k1_la_CPPFLAGS = -DSECP256K1_BUILD -I$(top_srcdir)/include -I$(top_srcdir)/src $(SECP_INCLUDES)
77+
libsecp256k1_la_LIBADD = $(JNI_LIB) $(SECP_LIBS) $(COMMON_LIB)
78+
79+
libsecp256k1_jni_la_SOURCES = src/java/org_bitcoin_NativeSecp256k1.c src/java/org_bitcoin_Secp256k1Context.c
80+
libsecp256k1_jni_la_CPPFLAGS = -DSECP256K1_BUILD $(JNI_INCLUDES)
81+
82+
noinst_PROGRAMS =
83+
if USE_BENCHMARK
84+
noinst_PROGRAMS += bench_verify bench_sign bench_internal bench_ecmult
85+
bench_verify_SOURCES = src/bench_verify.c
86+
bench_verify_LDADD = libsecp256k1.la $(SECP_LIBS) $(SECP_TEST_LIBS) $(COMMON_LIB)
87+
bench_sign_SOURCES = src/bench_sign.c
88+
bench_sign_LDADD = libsecp256k1.la $(SECP_LIBS) $(SECP_TEST_LIBS) $(COMMON_LIB)
89+
bench_internal_SOURCES = src/bench_internal.c
90+
bench_internal_LDADD = $(SECP_LIBS) $(COMMON_LIB)
91+
bench_internal_CPPFLAGS = -DSECP256K1_BUILD $(SECP_INCLUDES)
92+
bench_ecmult_SOURCES = src/bench_ecmult.c
93+
bench_ecmult_LDADD = $(SECP_LIBS) $(COMMON_LIB)
94+
bench_ecmult_CPPFLAGS = -DSECP256K1_BUILD $(SECP_INCLUDES)
95+
endif
96+
97+
TESTS =
98+
if USE_TESTS
99+
noinst_PROGRAMS += tests
100+
tests_SOURCES = src/tests.c
101+
tests_CPPFLAGS = -DSECP256K1_BUILD -I$(top_srcdir)/src -I$(top_srcdir)/include $(SECP_INCLUDES) $(SECP_TEST_INCLUDES)
102+
if !ENABLE_COVERAGE
103+
tests_CPPFLAGS += -DVERIFY
104+
endif
105+
tests_LDADD = $(SECP_LIBS) $(SECP_TEST_LIBS) $(COMMON_LIB)
106+
tests_LDFLAGS = -static
107+
TESTS += tests
108+
endif
109+
110+
if USE_EXHAUSTIVE_TESTS
111+
noinst_PROGRAMS += exhaustive_tests
112+
exhaustive_tests_SOURCES = src/tests_exhaustive.c
113+
exhaustive_tests_CPPFLAGS = -DSECP256K1_BUILD -I$(top_srcdir)/src $(SECP_INCLUDES)
114+
if !ENABLE_COVERAGE
115+
exhaustive_tests_CPPFLAGS += -DVERIFY
116+
endif
117+
exhaustive_tests_LDADD = $(SECP_LIBS)
118+
exhaustive_tests_LDFLAGS = -static
119+
TESTS += exhaustive_tests
120+
endif
121+
122+
JAVAROOT=src/java
123+
JAVAORG=org/bitcoin
124+
JAVA_GUAVA=$(srcdir)/$(JAVAROOT)/guava/guava-18.0.jar
125+
CLASSPATH_ENV=CLASSPATH=$(JAVA_GUAVA)
126+
JAVA_FILES= \
127+
$(JAVAROOT)/$(JAVAORG)/NativeSecp256k1.java \
128+
$(JAVAROOT)/$(JAVAORG)/NativeSecp256k1Test.java \
129+
$(JAVAROOT)/$(JAVAORG)/NativeSecp256k1Util.java \
130+
$(JAVAROOT)/$(JAVAORG)/Secp256k1Context.java
131+
132+
if USE_JNI
133+
134+
$(JAVA_GUAVA):
135+
@echo Guava is missing. Fetch it via: \
136+
wget https://search.maven.org/remotecontent?filepath=com/google/guava/guava/18.0/guava-18.0.jar -O $(@)
137+
@false
138+
139+
.stamp-java: $(JAVA_FILES)
140+
@echo Compiling $^
141+
$(AM_V_at)$(CLASSPATH_ENV) javac $^
142+
@touch $@
143+
144+
if USE_TESTS
145+
146+
check-java: libsecp256k1.la $(JAVA_GUAVA) .stamp-java
147+
$(AM_V_at)java -Djava.library.path="./:./src:./src/.libs:.libs/" -cp "$(JAVA_GUAVA):$(JAVAROOT)" $(JAVAORG)/NativeSecp256k1Test
148+
149+
endif
150+
endif
151+
152+
if USE_ECMULT_STATIC_PRECOMPUTATION
153+
CPPFLAGS_FOR_BUILD +=-I$(top_srcdir)
154+
CFLAGS_FOR_BUILD += -Wall -Wextra -Wno-unused-function
155+
156+
gen_context_OBJECTS = gen_context.o
157+
gen_context_BIN = gen_context$(BUILD_EXEEXT)
158+
gen_%.o: src/gen_%.c
159+
$(CC_FOR_BUILD) $(CPPFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD) -c $< -o $@
160+
161+
$(gen_context_BIN): $(gen_context_OBJECTS)
162+
$(CC_FOR_BUILD) $^ -o $@
163+
164+
$(libsecp256k1_la_OBJECTS): src/ecmult_static_context.h
165+
$(tests_OBJECTS): src/ecmult_static_context.h
166+
$(bench_internal_OBJECTS): src/ecmult_static_context.h
167+
$(bench_ecmult_OBJECTS): src/ecmult_static_context.h
168+
169+
src/ecmult_static_context.h: $(gen_context_BIN)
170+
./$(gen_context_BIN)
171+
172+
CLEANFILES = $(gen_context_BIN) src/ecmult_static_context.h $(JAVAROOT)/$(JAVAORG)/*.class .stamp-java
173+
endif
174+
175+
EXTRA_DIST = autogen.sh src/gen_context.c src/basic-config.h $(JAVA_FILES)
176+
177+
if ENABLE_MODULE_ECDH
178+
include src/modules/ecdh/Makefile.am.include
179+
endif
180+
181+
if ENABLE_MODULE_RECOVERY
182+
include src/modules/recovery/Makefile.am.include
183+
endif

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
libsecp256k1
2+
============
3+
4+
[![Build Status](https://travis-ci.org/bitcoin-core/secp256k1.svg?branch=master)](https://travis-ci.org/bitcoin-core/secp256k1)
5+
6+
Optimized C library for EC operations on curve secp256k1.
7+
8+
This library is a work in progress and is being used to research best practices. Use at your own risk.
9+
10+
Features:
11+
* secp256k1 ECDSA signing/verification and key generation.
12+
* Adding/multiplying private/public keys.
13+
* Serialization/parsing of private keys, public keys, signatures.
14+
* Constant time, constant memory access signing and pubkey generation.
15+
* Derandomized DSA (via RFC6979 or with a caller provided function.)
16+
* Very efficient implementation.
17+
18+
Implementation details
19+
----------------------
20+
21+
* General
22+
* No runtime heap allocation.
23+
* Extensive testing infrastructure.
24+
* Structured to facilitate review and analysis.
25+
* Intended to be portable to any system with a C89 compiler and uint64_t support.
26+
* Expose only higher level interfaces to minimize the API surface and improve application security. ("Be difficult to use insecurely.")
27+
* Field operations
28+
* Optimized implementation of arithmetic modulo the curve's field size (2^256 - 0x1000003D1).
29+
* Using 5 52-bit limbs (including hand-optimized assembly for x86_64, by Diederik Huys).
30+
* Using 10 26-bit limbs.
31+
* Field inverses and square roots using a sliding window over blocks of 1s (by Peter Dettman).
32+
* Scalar operations
33+
* Optimized implementation without data-dependent branches of arithmetic modulo the curve's order.
34+
* Using 4 64-bit limbs (relying on __int128 support in the compiler).
35+
* Using 8 32-bit limbs.
36+
* Group operations
37+
* Point addition formula specifically simplified for the curve equation (y^2 = x^3 + 7).
38+
* Use addition between points in Jacobian and affine coordinates where possible.
39+
* Use a unified addition/doubling formula where necessary to avoid data-dependent branches.
40+
* Point/x comparison without a field inversion by comparison in the Jacobian coordinate space.
41+
* Point multiplication for verification (a*P + b*G).
42+
* Use wNAF notation for point multiplicands.
43+
* Use a much larger window for multiples of G, using precomputed multiples.
44+
* Use Shamir's trick to do the multiplication with the public key and the generator simultaneously.
45+
* Optionally (off by default) use secp256k1's efficiently-computable endomorphism to split the P multiplicand into 2 half-sized ones.
46+
* Point multiplication for signing
47+
* Use a precomputed table of multiples of powers of 16 multiplied with the generator, so general multiplication becomes a series of additions.
48+
* Access the table with branch-free conditional moves so memory access is uniform.
49+
* No data-dependent branches
50+
* 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.
51+
52+
Build steps
53+
-----------
54+
55+
libsecp256k1 is built using autotools:
56+
57+
$ ./autogen.sh
58+
$ ./configure
59+
$ make
60+
$ ./tests
61+
$ sudo make install # optional

TODO

Lines changed: 3 additions & 0 deletions
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

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

0 commit comments

Comments
 (0)