Revamp makefile

This commit is contained in:
Pieter Wuille 2013-04-07 00:37:06 +02:00
parent 6baf08dfe4
commit f0c89aadc6
6 changed files with 182 additions and 87 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
obj/*
bench
tests
*.so
*.a
config.mk

View File

@ -1,91 +1,39 @@
FLAGS_COMMON:=-Wall -Wno-unused -fPIC -std=c99 $(shell CC=$(CC) YASM=$(YASM) ./configure)
FLAGS_PROD:=-DNDEBUG -O2 -march=native include config.mk
FLAGS_DEBUG:=-DVERIFY -ggdb3 -O1
FLAGS_TEST:=-DVERIFY -ggdb3 -O2 -march=native
SECP256K1_FILES := src/num.h src/field.h src/field_5x52.h src/group.h src/ecmult.h src/ecdsa.h \ FILES := src/*.h src/impl/*.h
src/impl/*.h
JAVA_FILES := src/java/org_bitcoin_NativeSecp256k1.h src/java/org_bitcoin_NativeSecp256k1.c JAVA_FILES := src/java/org_bitcoin_NativeSecp256k1.h src/java/org_bitcoin_NativeSecp256k1.c
ifndef CONF
CONF := gmp
endif
OBJS := OBJS :=
default: all ifeq ($(USE_ASM), 1)
OBJS := $(OBJS) obj/field_5x52_asm.o
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
endif endif
all: src/*.c src/*.asm src/*.h include/*.h default: tests libsecp256k1.a libsecp256k1.so
+make CONF=openssl all-openssl ./tests
+make CONF=gmp all-gmp
+make CONF=gmpgmp all-gmpgmp
+make CONF=gmp32 all-gmp32
+make CONF=gmpasm all-gmpasm
clean: clean:
+make CONF=openssl clean-openssl rm -rf obj/*.o bench tests *.a *.so config.mk
+make CONF=gmp clean-gmp
+make CONF=gmp32 clean-gmp32
+make CONF=gmpasm clean-gmpasm
bench-any: bench-$(CONF) obj/field_5x52_asm.o: src/field_5x52_asm.asm
tests-any: tests-$(CONF) $(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): bench: $(FILES) src/bench.c $(OBJS)
rm -f bench-$(CONF) tests-$(CONF) libsecp256k1-$(CONF).a libjavasecp256k1-$(CONF).so obj/* $(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 tests: $(FILES) src/tests.c $(OBJS)
$(CC) $(FLAGS_COMMON) $(FLAGS_PROD) src/secp256k1.c -c -o obj/secp256k1-$(CONF).o $(CC) -std=c99 $(CFLAGS) $(CFLAGS_EXTRA) -DVERIFY -O1 src/tests.c $(OBJS) $(LDFLAGS_EXTRA) -o tests
bench-$(CONF): $(OBJS) $(SECP256K1_FILES) src/bench.c libsecp256k1.a: obj/secp256k1.o $(OBJS)
$(CC) $(FLAGS_COMMON) $(FLAGS_PROD) src/bench.c $(LIBS) $(OBJS) -o bench-$(CONF) $(AR) -rs $@ $(OBJS) obj/secp256k1.o
tests-$(CONF): $(OBJS) $(SECP256K1_FILES) src/tests.c libsecp256k1.so: obj/secp256k1.o $(OBJS)
$(CC) $(FLAGS_COMMON) $(FLAGS_TEST) src/tests.c $(LIBS) $(OBJS) -o tests-$(CONF) $(CC) -std=c99 $(LDFLAGS_EXTRA) $(OBJS) obj/secp256k1.o -shared -o libsecp256k1.so
libsecp256k1-$(CONF).a: $(OBJS) obj/secp256k1-$(CONF).o libjavasecp256k1.so: $(OBJS) obj/secp256k1.o $(JAVA_FILES)
$(AR) -rs $@ $(OBJS) obj/secp256k1-$(CONF).o $(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
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

136
configure vendored Executable file
View File

@ -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 - <<EOF
BITS 64
GLOBAL testyasm
ALIGN 32
testyasm:
xor r9,r9
EOF
if [ "$?" = 0 ]; then
$CC $CFLAGS -std=c99 -x c -c - -o /tmp/secp256k1-$$-2.o 2>/dev/null <<EOF
void __attribute__ ((sysv_abi)) testyasm(void);
int main() {
testyasm();
return 0;
}
EOF
$CC $CFLAGS -std=c99 /tmp/secp256k1-$$-2.o /tmp/secp256k1-$$.o -o /dev/null 2>/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 <<EOF
#include <openssl/bn.h>
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 <<EOF
#include <gmp.h>
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 <<EOF
#include <stdint.h>
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

View File

@ -12,14 +12,14 @@
* normality. * normality.
*/ */
#ifdef USE_FIELD_GMP #if defined(USE_FIELD_GMP)
#include "field_gmp.h" #include "field_gmp.h"
#else #elif defined(USE_FIELD_10X26)
#ifdef USE_FIELD_10X26
#include "field_10x26.h" #include "field_10x26.h"
#else #elif defined(USE_FIELD_5X52)
#include "field_5x52.h" #include "field_5x52.h"
#endif #else
#error "Please select field implementation"
#endif #endif
typedef struct { typedef struct {

View File

@ -1,14 +1,14 @@
#ifndef _SECP256K1_FIELD_IMPL_H_ #ifndef _SECP256K1_FIELD_IMPL_H_
#define _SECP256K1_FIELD_IMPL_H_ #define _SECP256K1_FIELD_IMPL_H_
#ifdef USE_FIELD_GMP #if defined(USE_FIELD_GMP)
#include "field_gmp.h" #include "field_gmp.h"
#else #elif defined(USE_FIELD_10X26)
#ifdef USE_FIELD_10X26
#include "field_10x26.h" #include "field_10x26.h"
#else #elif defined(USE_FIELD_5X52)
#include "field_5x52.h" #include "field_5x52.h"
#endif #else
#error "Please select field implementation"
#endif #endif
void static secp256k1_fe_get_hex(char *r, int *rlen, const secp256k1_fe_t *a) { 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) { void static secp256k1_fe_inv_var(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
#if defined(USE_FIELD_INV_BUILTIN) #if defined(USE_FIELD_INV_BUILTIN)
secp256k1_fe_inv(r, a); secp256k1_fe_inv(r, a);
#else #elif defined(USE_FIELD_INV_NUM)
unsigned char b[32]; unsigned char b[32];
secp256k1_fe_t c = *a; secp256k1_fe_t c = *a;
secp256k1_fe_normalize(&c); 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_get_bin(b, 32, &n);
secp256k1_num_free(&n); secp256k1_num_free(&n);
secp256k1_fe_set_b32(r, b); secp256k1_fe_set_b32(r, b);
#else
#error "Please select field inverse implementation"
#endif #endif
} }

View File

@ -6,10 +6,12 @@
#include "../num.h" #include "../num.h"
#include "../field.h" #include "../field.h"
#ifdef USE_FIELD_5X52_ASM #if defined(USE_FIELD_5X52_ASM)
#include "field_5x52_asm.h" #include "field_5x52_asm.h"
#else #elif defined(USE_FIELD_5X52_INT128)
#include "field_5x52_int128.h" #include "field_5x52_int128.h"
#else
#error "Please select field_5x52 implementation"
#endif #endif
/** Implements arithmetic modulo FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F, /** Implements arithmetic modulo FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F,