From a43ee04d8619cdb4bbb84dab60002fd9c987ee60 Mon Sep 17 00:00:00 2001 From: Stepan Snigirev Date: Mon, 17 Jan 2022 11:12:57 +0100 Subject: [PATCH] Add a secp256k1 makefile and building instructions (#24) --- .gitmodules | 3 + secp256k1/Makefile | 90 ++++++++++++++++++++++++++ secp256k1/README.md | 58 +++++++++++++++++ secp256k1/config/libsecp256k1-config.h | 44 +++++++++++++ secp256k1/secp256k1-zkp | 1 + 5 files changed, 196 insertions(+) create mode 100644 .gitmodules create mode 100644 secp256k1/Makefile create mode 100644 secp256k1/README.md create mode 100644 secp256k1/config/libsecp256k1-config.h create mode 160000 secp256k1/secp256k1-zkp diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..fa05e9e --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "secp256k1/secp256k1-zkp"] + path = secp256k1/secp256k1-zkp + url = https://github.com/ElementsProject/secp256k1-zkp.git diff --git a/secp256k1/Makefile b/secp256k1/Makefile new file mode 100644 index 0000000..45b93bc --- /dev/null +++ b/secp256k1/Makefile @@ -0,0 +1,90 @@ +CROSS_DLL ?= 0 +TARGET = libsecp256k1 +ifeq ($(CROSS_DLL),1) +PLATFORM = windows +ARCH = amd64 +else ifeq ($(OS),Windows_NT) +PLATFORM = windows +ARCH = amd64 +else +PLATFORM = $(shell uname -s | tr A-Z a-z) +ARCH = $(shell uname -m) +endif + +# Paths +LIB_DIR = secp256k1-zkp +BUILD_DIR = build + +# Tools +ifeq ($(PLATFORM),windows) +TOOLCHAIN_PREFIX ?= x86_64-w64-mingw32- +else +TOOLCHAIN_PREFIX ?= +endif + +CC := $(TOOLCHAIN_PREFIX)gcc +ifeq ($(OS),Windows_NT) +MKDIR_P = mkdir +RM_R = rmdir /s /q +else +MKDIR_P = mkdir -p +RM_R = rm -r +endif + +# C sources +C_SOURCES = $(addprefix $(LIB_DIR)/src/,\ + secp256k1.c \ + ) + +# C includes +C_INCLUDES = \ + $(LIB_DIR) \ + $(LIB_DIR)/src \ + config + +# C defines +C_DEFS = \ + HAVE_CONFIG_H + +ifeq ($(PLATFORM),windows) +C_DEFS += _WIN32 +endif + +OBJS := $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o))) +vpath %.c $(sort $(dir $(C_SOURCES))) + +DEPS := $(OBJS:.o=.d) + +ifeq ($(PLATFORM),windows) +CFLAGS = -O2 -std=c99 -MMD -MP -Werror -Wno-unused-function \ + $(addprefix -I,$(C_INCLUDES)) $(addprefix -D,$(C_DEFS)) +else +CFLAGS = -fPIC -O2 -Werror -Wno-unused-function \ + $(addprefix -I,$(C_INCLUDES)) $(addprefix -D,$(C_DEFS)) +endif + +ifeq ($(PLATFORM),windows) +LDFLAGS = -shared -s \ + -Wl,--subsystem,windows,--out-implib,$(BUILD_DIR)/$(TARGET).a +EXT = .dll +else ifeq ($(PLATFORM),darwin) +LDFLAGS = -dynamiclib +EXT = .dylib +else +LDFLAGS = -shared +EXT = .so +endif + +$(BUILD_DIR)/$(TARGET)_$(PLATFORM)_$(ARCH)$(EXT): $(OBJS) Makefile + $(CC) $(OBJS) -o $@ $(LDFLAGS) + +$(BUILD_DIR)/%.o: %.c Makefile + $(MKDIR_P) "$(dir $@)" + $(CC) $(CFLAGS) -c $< -o $@ + +.PHONY: clean + +clean: + $(RM_R) "$(BUILD_DIR)" + +-include $(DEPS) diff --git a/secp256k1/README.md b/secp256k1/README.md new file mode 100644 index 0000000..ae0d585 --- /dev/null +++ b/secp256k1/README.md @@ -0,0 +1,58 @@ +# Building secp256k1 for embit + +If you don't want to use prebuilt binary packaged with `embit` you can build it yourself. + +We are using **libsecp256k1** fork - +[**secp256k1-zkp**](https://github.com/ElementsProject/secp256k1-zkp). + +# Building the library under target platform + +To build the library run: + +```sh +make +``` + +To clean build directory use: + +```shell +make clean +``` + +# Cross-compiling Windows DLL + +## Toolchain install + +### Linux + +In the console type: + +```shell +sudo apt-get install gcc-mingw-w64-x86-64 g++-mingw-w64-x86-64 wine64 +``` + +### Mac + +Assuming that [Homebrew](https://brew.sh/) package manager is installed, in the console type: + +```shell +brew install mingw-w64 +brew install --cask xquartz +brew install --cask wine-stable +``` + +### Windows + +Assuming that [Chocolatey](https://chocolatey.org/) package manager is installed, in the **Powershell** type: + +```shell +choco install mingw make +``` + +## Building the library + +To build the Windows DLL and the companion library from other platforms run: + +```shell +make CROSS_DLL=1 +``` diff --git a/secp256k1/config/libsecp256k1-config.h b/secp256k1/config/libsecp256k1-config.h new file mode 100644 index 0000000..c700251 --- /dev/null +++ b/secp256k1/config/libsecp256k1-config.h @@ -0,0 +1,44 @@ +#ifndef SECP256K1_CONFIG_H +#define SECP256K1_CONFIG_H + +#undef USE_ASM_X86_64 +#undef USE_ECMULT_STATIC_PRECOMPUTATION +#undef USE_ENDOMORPHISM +#undef USE_EXTERNAL_ASM +#undef USE_EXTERNAL_DEFAULT_CALLBACKS +#undef USE_FIELD_10X26 +#undef USE_FIELD_5X52 +#undef USE_FIELD_INV_BUILTIN +#undef USE_FIELD_INV_NUM +#undef USE_NUM_GMP +#undef USE_NUM_NONE +#undef USE_SCALAR_4X64 +#undef USE_SCALAR_8X32 +#undef USE_SCALAR_INV_BUILTIN +#undef USE_SCALAR_INV_NUM +#undef ECMULT_WINDOW_SIZE + +#define ENABLE_MODULE_ECDH 1 +#define ENABLE_MODULE_RECOVERY 1 +#define ENABLE_MODULE_GENERATOR 1 +#define ENABLE_MODULE_RANGEPROOF 1 +#define ENABLE_MODULE_SURJECTIONPROOF 1 +#define ENABLE_MODULE_MUSIG 1 +#define ENABLE_MODULE_EXTRAKEYS 1 +#define ENABLE_MODULE_SCHNORRSIG 1 + + +#define USE_NUM_NONE 1 +#define USE_FIELD_INV_BUILTIN 1 +#define USE_SCALAR_INV_BUILTIN 1 +#define USE_FIELD_10X26 1 +#define USE_SCALAR_8X32 1 + +#define ECMULT_GEN_PREC_BITS 4 +#define ECMULT_WINDOW_SIZE 4 + +#define HAVE_STDINT_H 1 +#define HAVE_STDLIB_H 1 +#define HAVE_STRING_H 1 + +#endif /* SECP256K1_CONFIG_H */ diff --git a/secp256k1/secp256k1-zkp b/secp256k1/secp256k1-zkp new file mode 160000 index 0000000..d9560e0 --- /dev/null +++ b/secp256k1/secp256k1-zkp @@ -0,0 +1 @@ +Subproject commit d9560e0af78d9059bba0c4845a310387abfa4e5e