Skip to content

Commit

Permalink
Multiple (cross-)build related fixes and additions (Chia-Network#24)
Browse files Browse the repository at this point in the history
* Fix compilation errors if libsodium is not used/found

"#if BLSALLOC == sodium" for some reason evaluates to true and thus sodium
is used even though it is not present.

* Use WCGR for SEED when cross compiling for Windows

/dev/[u]dev is not present on Windows.

* Use lower-case include for <Wincrypt.h>

When cross-compiling on Debian Stretch, compilation fails because Wincrypt.h
can't be found because includes are case-sensitive on Linux.

* Use CMAKE_AR and CMAKE_C_OUTPUT_EXTENSION when combining .a libraries

Required for cross-compilation.

* Installation of lib files and headers

* Make messageHash const in Sign and SignPrehashed
  • Loading branch information
codablock authored and mariano54 committed Sep 19, 2018
1 parent 9b86637 commit 98323d0
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 18 deletions.
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ find_package(sodium)
if (SODIUM_FOUND)
message(STATUS "Found libsodium")
message(STATUS "Sodium include dir = ${sodium_INCLUDE_DIR}")
set(BLSALLOC "sodium" CACHE STRING "")
set(BLSALLOC_SODIUM "1" CACHE STRING "")
include_directories(${sodium_INCLUDE_DIR})
endif()

Expand All @@ -43,13 +43,15 @@ set(FP_PRIME 381 CACHE INTEGER "")

IF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(DSEED "UDEV" CACHE STRING "")
set(STBIN "OFF" CACHE STRING "")
set(FP_QNRES "off" CACHE STRING "")
ELSEIF (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(SEED "WCGR" CACHE STRING "")
set(FP_QNRES "on" CACHE STRING "")
ELSE()
set(DSEED "DEV" CACHE STRING "")
set(STBIN "OFF" CACHE STRING "")
set(FP_QNRES "on" CACHE STRING "")
ENDIF()
set(STBIN "OFF" CACHE STRING "")

set(FP_METHD "INTEG;INTEG;INTEG;MONTY;LOWER;SLIDE" CACHE STRING "")
set(COMP "-O3 -funroll-loops -fomit-frame-pointer -finline-small-functions -march=native -mtune=native" CACHE STRING "")
Expand Down
2 changes: 1 addition & 1 deletion contrib/relic/src/rand/relic_rand_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
#undef DOUBLE

#include <windows.h>
#include <Wincrypt.h>
#include <wincrypt.h>

#elif SEED == RDRND

Expand Down
14 changes: 9 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ find_library(GMP_NAME NAMES libgmp.a gmp)
find_library(SODIUM_NAME NAMES libsodium.a sodium)

set(LIBRARIES_TO_COMBINE
COMMAND mkdir ${OPREFIX}$<TARGET_NAME:blstmp> || true && cd ${OPREFIX}$<TARGET_NAME:blstmp> && ar -x $<TARGET_FILE:blstmp>
COMMAND mkdir ${OPREFIX}$<TARGET_NAME:relic_s> || true && cd ${OPREFIX}$<TARGET_NAME:relic_s> && ar -x $<TARGET_FILE:relic_s>
COMMAND mkdir ${OPREFIX}$<TARGET_NAME:blstmp> || true && cd ${OPREFIX}$<TARGET_NAME:blstmp> && ${CMAKE_AR} -x $<TARGET_FILE:blstmp>
COMMAND mkdir ${OPREFIX}$<TARGET_NAME:relic_s> || true && cd ${OPREFIX}$<TARGET_NAME:relic_s> && ${CMAKE_AR} -x $<TARGET_FILE:relic_s>
)

if (GMP_FOUND)
list(APPEND LIBRARIES_TO_COMBINE COMMAND mkdir ${OPREFIX}gmp || true && cd ${OPREFIX}gmp && ar -x ${GMP_NAME})
list(APPEND LIBRARIES_TO_COMBINE COMMAND mkdir ${OPREFIX}gmp || true && cd ${OPREFIX}gmp && ${CMAKE_AR} -x ${GMP_NAME})
endif()
if (SODIUM_FOUND)
list(APPEND LIBRARIES_TO_COMBINE COMMAND mkdir ${OPREFIX}sodium || true && cd ${OPREFIX}sodium && ar -x ${SODIUM_NAME})
list(APPEND LIBRARIES_TO_COMBINE COMMAND mkdir ${OPREFIX}sodium || true && cd ${OPREFIX}sodium && ${CMAKE_AR} -x ${SODIUM_NAME})
endif()

add_custom_target(combined_custom
${LIBRARIES_TO_COMBINE}
COMMAND ar -rs ${C_LIB} ${OPREFIX}*/*.o
COMMAND ${CMAKE_AR} -rs ${C_LIB} ${OPREFIX}*/*${CMAKE_C_OUTPUT_EXTENSION}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
DEPENDS blstmp relic_s
)
Expand All @@ -59,6 +59,10 @@ set_target_properties(combined
IMPORTED_LOCATION ${C_LIB}
)

file(GLOB includes "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${includes} DESTINATION include/chiabls)
install(FILES ${C_LIB} DESTINATION lib)

add_executable(runtest test.cpp)
add_executable(runbench test-bench.cpp)

Expand Down
4 changes: 2 additions & 2 deletions src/bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ bool BLS::Init() {
std::cout << "ep_param_set_any_pairf() failed";
return false;
}
#if BLSALLOC == sodium
#if BLSALLOC_SODIUM
if (libsodium::sodium_init() < 0) {
std::cout << "libsodium init failed";
return false;
Expand All @@ -53,7 +53,7 @@ void BLS::AssertInitialized() {
if (!relic::core_get()) {
throw std::string("Library not initialized properly. Call BLS::Init()");
}
#if BLSALLOC == sodium
#if BLSALLOC_SODIUM
if (libsodium::sodium_init() < 0) {
throw std::string("Libsodium initialization failed.");
}
Expand Down
4 changes: 2 additions & 2 deletions src/blsprivatekey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ void BLSPrivateKey::Serialize(uint8_t* buffer) const {
bn_write_bin(buffer, BLSPrivateKey::PRIVATE_KEY_SIZE, *keydata);
}

BLSSignature BLSPrivateKey::Sign(uint8_t *msg, size_t len) const {
BLSSignature BLSPrivateKey::Sign(const uint8_t *msg, size_t len) const {
BLS::AssertInitialized();
uint8_t messageHash[BLS::MESSAGE_HASH_LEN];
BLSUtil::Hash256(messageHash, msg, len);
return SignPrehashed(messageHash);
}

BLSSignature BLSPrivateKey::SignPrehashed(uint8_t *messageHash) const {
BLSSignature BLSPrivateKey::SignPrehashed(const uint8_t *messageHash) const {
BLS::AssertInitialized();
relic::g2_t sig, point;

Expand Down
4 changes: 2 additions & 2 deletions src/blsprivatekey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ class BLSPrivateKey {
void Serialize(uint8_t* buffer) const;

// Sign a message
BLSSignature Sign(uint8_t *msg, size_t len) const;
BLSSignature SignPrehashed(uint8_t *hash) const;
BLSSignature Sign(const uint8_t *msg, size_t len) const;
BLSSignature SignPrehashed(const uint8_t *hash) const;

private:
// Don't allow public construction, force static methods
Expand Down
6 changes: 3 additions & 3 deletions src/blsutil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <gmp.h>
#endif

#if BLSALLOC == sodium
#if BLSALLOC_SODIUM
namespace libsodium {
#include "sodium/utils.h"
#include "sodium/core.h"
Expand Down Expand Up @@ -80,7 +80,7 @@ class BLSUtil {
*/
template<class T>
static T* SecAlloc(size_t numTs) {
#if BLSALLOC
#if BLSALLOC_SODIUM
return static_cast<T*>(libsodium::sodium_malloc
(sizeof(T) * numTs));
#else
Expand All @@ -92,7 +92,7 @@ class BLSUtil {
* Frees memory allocated using SecAlloc.
*/
static void SecFree(void* ptr) {
#if BLSALLOC
#if BLSALLOC_SODIUM
libsodium::sodium_free(ptr);
#else
free(ptr);
Expand Down

0 comments on commit 98323d0

Please sign in to comment.