From f0c89aadc6aeec18104f6b3e8f874156b85b55ee Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sun, 7 Apr 2013 00:37:06 +0200 Subject: [PATCH] Revamp makefile --- .gitignore | 7 +++ Makefile | 96 +++++++---------------------- configure | 136 ++++++++++++++++++++++++++++++++++++++++++ src/field.h | 10 ++-- src/impl/field.h | 14 +++-- src/impl/field_5x52.h | 6 +- 6 files changed, 182 insertions(+), 87 deletions(-) create mode 100644 .gitignore create mode 100755 configure diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b683f77 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +obj/* +bench +tests +*.so +*.a +config.mk + diff --git a/Makefile b/Makefile index a1ab3fd..3080b50 100644 --- a/Makefile +++ b/Makefile @@ -1,91 +1,39 @@ -FLAGS_COMMON:=-Wall -Wno-unused -fPIC -std=c99 -FLAGS_PROD:=-DNDEBUG -O2 -march=native -FLAGS_DEBUG:=-DVERIFY -ggdb3 -O1 -FLAGS_TEST:=-DVERIFY -ggdb3 -O2 -march=native +$(shell CC=$(CC) YASM=$(YASM) ./configure) +include config.mk -SECP256K1_FILES := src/num.h src/field.h src/field_5x52.h src/group.h src/ecmult.h src/ecdsa.h \ - src/impl/*.h +FILES := src/*.h src/impl/*.h JAVA_FILES := src/java/org_bitcoin_NativeSecp256k1.h src/java/org_bitcoin_NativeSecp256k1.c -ifndef CONF -CONF := gmp -endif - OBJS := -default: all - -ifeq ($(CONF), gmpgmp) -FLAGS_COMMON := $(FLAGS_COMMON) -DUSE_NUM_GMP -DUSE_FIELD_GMP -LIBS := -lgmp -SECP256K1_FILES := $(SECP256K1_FILES) src/num_gmp.h src/field_gmp.h -else -ifeq ($(CONF), gmp32) -FLAGS_COMMON := $(FLAGS_COMMON) -DUSE_NUM_GMP -DUSE_FIELD_10X26 -LIBS := -lgmp -SECP256K1_FILES := $(SECP256K1_FILES) src/num_gmp.h src/field_10x26.h -else -ifeq ($(CONF), openssl) -FLAGS_COMMON := $(FLAGS_COMMON) -DUSE_NUM_OPENSSL -DUSE_FIELD_INV_BUILTIN -LIBS := -lcrypto -SECP256K1_FILES := $(SECP256K1_FILES) src/num_openssl.h src/field_5x52.h -else -ifeq ($(CONF), gmp) -FLAGS_COMMON := $(FLAGS_COMMON) -DUSE_NUM_GMP -LIBS := -lgmp -SECP256K1_FILES := $(SECP256K1_FILES) src/num_gmp.h src/field_5x52.h -else -ifeq ($(CONF), gmpasm) -FLAGS_COMMON := $(FLAGS_COMMON) -DUSE_NUM_GMP -DUSE_FIELD_5X52_ASM -LIBS := -lgmp -OBJS := $(OBJS) obj/field_5x52_asm.o -SECP256K1_FILES := $(SECP256K1_FILES) src/num_gmp.h src/field_5x52.h - -obj/field_5x52_asm.o: src/field_5x52_asm.asm - yasm -f elf64 -o obj/field_5x52_asm.o src/field_5x52_asm.asm -else -error -endif -endif -endif -endif +ifeq ($(USE_ASM), 1) + OBJS := $(OBJS) obj/field_5x52_asm.o endif -all: src/*.c src/*.asm src/*.h include/*.h - +make CONF=openssl all-openssl - +make CONF=gmp all-gmp - +make CONF=gmpgmp all-gmpgmp - +make CONF=gmp32 all-gmp32 - +make CONF=gmpasm all-gmpasm +default: tests libsecp256k1.a libsecp256k1.so + ./tests clean: - +make CONF=openssl clean-openssl - +make CONF=gmp clean-gmp - +make CONF=gmp32 clean-gmp32 - +make CONF=gmpasm clean-gmpasm + rm -rf obj/*.o bench tests *.a *.so config.mk -bench-any: bench-$(CONF) -tests-any: tests-$(CONF) +obj/field_5x52_asm.o: src/field_5x52_asm.asm + $(YASM) -f elf64 -o obj/field_5x52_asm.o src/field_5x52_asm.asm -all-$(CONF): bench-$(CONF) tests-$(CONF) libsecp256k1-$(CONF).a +obj/secp256k1.o: $(FILES) src/secp256k1.c include/secp256k1.h + $(CC) -fPIC -std=c99 $(CFLAGS) $(CFLAGS_EXTRA) -DNDEBUG -O2 src/secp256k1.c -c -o obj/secp256k1.o -clean-$(CONF): - rm -f bench-$(CONF) tests-$(CONF) libsecp256k1-$(CONF).a libjavasecp256k1-$(CONF).so obj/* +bench: $(FILES) src/bench.c $(OBJS) + $(CC) -fPIC -std=c99 $(CFLAGS) $(CFLAGS_EXTRA) -DNDEBUG -O2 src/bench.c $(OBJS) $(LDFLAGS_EXTRA) -o bench -obj/secp256k1-$(CONF).o: $(SECP256K1_FILES) src/secp256k1.c include/secp256k1.h - $(CC) $(FLAGS_COMMON) $(FLAGS_PROD) src/secp256k1.c -c -o obj/secp256k1-$(CONF).o +tests: $(FILES) src/tests.c $(OBJS) + $(CC) -std=c99 $(CFLAGS) $(CFLAGS_EXTRA) -DVERIFY -O1 src/tests.c $(OBJS) $(LDFLAGS_EXTRA) -o tests -bench-$(CONF): $(OBJS) $(SECP256K1_FILES) src/bench.c - $(CC) $(FLAGS_COMMON) $(FLAGS_PROD) src/bench.c $(LIBS) $(OBJS) -o bench-$(CONF) +libsecp256k1.a: obj/secp256k1.o $(OBJS) + $(AR) -rs $@ $(OBJS) obj/secp256k1.o -tests-$(CONF): $(OBJS) $(SECP256K1_FILES) src/tests.c - $(CC) $(FLAGS_COMMON) $(FLAGS_TEST) src/tests.c $(LIBS) $(OBJS) -o tests-$(CONF) +libsecp256k1.so: obj/secp256k1.o $(OBJS) + $(CC) -std=c99 $(LDFLAGS_EXTRA) $(OBJS) obj/secp256k1.o -shared -o libsecp256k1.so -libsecp256k1-$(CONF).a: $(OBJS) obj/secp256k1-$(CONF).o - $(AR) -rs $@ $(OBJS) obj/secp256k1-$(CONF).o - -libjavasecp256k1-$(CONF).so: $(OBJS) obj/secp256k1-$(CONF).o $(JAVA_FILES) - $(CC) $(FLAGS_COMMON) $(FLAGS_PROD) -I. src/java/org_bitcoin_NativeSecp256k1.c $(LIBS) $(OBJS) obj/secp256k1-$(CONF).o -shared -o libjavasecp256k1-$(CONF).so - -java: libjavasecp256k1-$(CONF).so +libjavasecp256k1.so: $(OBJS) obj/secp256k1.o $(JAVA_FILES) + $(CC) -fPIC -std=c99 $(CFLAGS) $(CFLAGS_EXTRA) -DNDEBUG -O2 -I. src/java/org_bitcoin_NativeSecp256k1.c $(LDFLAGS_EXTRA) $(OBJS) obj/secp256k1.o -shared -o libjavasecp256k1.so diff --git a/configure b/configure new file mode 100755 index 0000000..0bfd8e6 --- /dev/null +++ b/configure @@ -0,0 +1,136 @@ +#!/bin/sh + +if test -f config.mk; then + exit 0 +fi + +if test -z "$CC"; then + CC=cc +fi + +if test -z "$YASM"; then + YASM=yasm +fi + +# test yasm +$YASM -f elf64 -o /tmp/secp256k1-$$.o - </dev/null </dev/null + if [ "$?" = 0 ]; then + HAVE_YASM=1 + fi + rm -rf /tmp/secp256k1-$$-2.o /tmp/secp256k1-$$.o +fi + +# test openssl +HAVE_OPENSSL=0 +$CC $CFLAGS -std=c99 -x c - -o /dev/null -lcrypto 2>/dev/null < +int main() { + BN_CTX *ctx = BN_CTX_new(); + BN_CTX_free(ctx); + return 0; +} +EOF +if [ "$?" = 0 ]; then + HAVE_OPENSSL=1 +fi + +# test gmp +HAVE_GMP=0 +$CC $CFLAGS -std=c99 -x c - -o /dev/null -lgmp 2>/dev/null < +int main() { + mpz_t n; + mpz_init(n); + mpz_clear(n); + return 0; +} +EOF +if [ "$?" = 0 ]; then + HAVE_GMP=1 +fi + +# test __int128 +HAVE_INT128=0 +$CC $CFLAGS -std=c99 -x c - -o /dev/null 2>/dev/null < +int main() { + __int128 x = 0; + return 0; +} +EOF +if [ "$?" = 0 ]; then + HAVE_INT128=1 +fi + +for arg in "$@"; do + case "$arg" in + --no-yasm) + HAVE_YASM=0 + ;; + --no-gmp) + HAVE_GMP=0 + ;; + --no-openssl) + HAVE_OPENSSL=0 + ;; + esac +done + +LINK_OPENSSL=0 +LINK_GMP=0 +USE_ASM=0 + +# select field implementation +if [ "$HAVE_YASM" = "1" ]; then + CFLAGS_FIELD="-DUSE_FIELD_5X52 -DUSE_FIELD_5X52_ASM" + USE_ASM=1 +elif [ "$HAVE_INT128" = "1" ]; then + CFLAGS_FIELD="-DUSE_FIELD_5X52 -DUSE_FIELD_5X52_INT128" +elif [ "$HAVE_GMP" = "1" ]; then + CFLAGS_FIELD="-DUSE_FIELD_GMP" + LINK_GMP=1 +else + CFLAGS_FIELD="-DUSE_FIELD_10X26" +fi + +# select num implementation +if [ "$HAVE_GMP" = "1" ]; then + CFLAGS_NUM="-DUSE_NUM_GMP -DUSE_FIELD_INV_NUM" + LINK_GMP=1 +elif [ "$HAVE_OPENSSL" = "1" ]; then + CFLAGS_NUM="-DUSE_NUM_OPENSSL -DUSE_FIELD_INV_BUILTIN" + LINK_OPENSSL=1 +else + echo "No usable num implementation found" >&2 + exit 1 +fi + +CFLAGS_EXTRA="$CFLAGS_FIELD $CFLAGS_NUM" +LDFLAGS_EXTRA="" +if [ "$LINK_GMP" = "1" ]; then + LDFLAGS_EXTRA="-lgmp" +fi +if [ "$LINK_OPENSSL" = "1" ]; then + LDFLAGS_EXTRA="-lcrypto" +fi + +echo "CC=$CC" > config.mk +echo "YASM=$YASM" >>config.mk +echo "CFLAGS_EXTRA=$CFLAGS_EXTRA" >> config.mk +echo "LDFLAGS_EXTRA=$LDFLAGS_EXTRA" >> config.mk +echo "USE_ASM=$USE_ASM" >>config.mk diff --git a/src/field.h b/src/field.h index 604873e..039d572 100644 --- a/src/field.h +++ b/src/field.h @@ -12,14 +12,14 @@ * normality. */ -#ifdef USE_FIELD_GMP +#if defined(USE_FIELD_GMP) #include "field_gmp.h" -#else -#ifdef USE_FIELD_10X26 +#elif defined(USE_FIELD_10X26) #include "field_10x26.h" -#else +#elif defined(USE_FIELD_5X52) #include "field_5x52.h" -#endif +#else +#error "Please select field implementation" #endif typedef struct { diff --git a/src/impl/field.h b/src/impl/field.h index 1ba0b00..a0112fd 100644 --- a/src/impl/field.h +++ b/src/impl/field.h @@ -1,14 +1,14 @@ #ifndef _SECP256K1_FIELD_IMPL_H_ #define _SECP256K1_FIELD_IMPL_H_ -#ifdef USE_FIELD_GMP +#if defined(USE_FIELD_GMP) #include "field_gmp.h" -#else -#ifdef USE_FIELD_10X26 +#elif defined(USE_FIELD_10X26) #include "field_10x26.h" -#else +#elif defined(USE_FIELD_5X52) #include "field_5x52.h" -#endif +#else +#error "Please select field implementation" #endif void static secp256k1_fe_get_hex(char *r, int *rlen, const secp256k1_fe_t *a) { @@ -123,7 +123,7 @@ void static secp256k1_fe_inv(secp256k1_fe_t *r, const secp256k1_fe_t *a) { void static secp256k1_fe_inv_var(secp256k1_fe_t *r, const secp256k1_fe_t *a) { #if defined(USE_FIELD_INV_BUILTIN) secp256k1_fe_inv(r, a); -#else +#elif defined(USE_FIELD_INV_NUM) unsigned char b[32]; secp256k1_fe_t c = *a; secp256k1_fe_normalize(&c); @@ -135,6 +135,8 @@ void static secp256k1_fe_inv_var(secp256k1_fe_t *r, const secp256k1_fe_t *a) { secp256k1_num_get_bin(b, 32, &n); secp256k1_num_free(&n); secp256k1_fe_set_b32(r, b); +#else +#error "Please select field inverse implementation" #endif } diff --git a/src/impl/field_5x52.h b/src/impl/field_5x52.h index f8757c2..ff1e498 100644 --- a/src/impl/field_5x52.h +++ b/src/impl/field_5x52.h @@ -6,10 +6,12 @@ #include "../num.h" #include "../field.h" -#ifdef USE_FIELD_5X52_ASM +#if defined(USE_FIELD_5X52_ASM) #include "field_5x52_asm.h" -#else +#elif defined(USE_FIELD_5X52_INT128) #include "field_5x52_int128.h" +#else +#error "Please select field_5x52 implementation" #endif /** Implements arithmetic modulo FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F,