diff --git a/CMakeLists.txt b/CMakeLists.txt index 214ba3a..5db8382 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,8 +5,22 @@ message(STATUS "Target architectures: ${CMAKE_TARGET_ARCHITECTURES}") project(cryptonite_hash C ASM) +set(LINK_TYPE SHARED) # Add a CMake parameter for choosing a desired Python version -set(CRYPTONIGHT_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling cryptonite_hash library") +option(MAKE_CLIB "Builds a shared library for C") +option(MAKE_CLIB_STATIC "Builds a static library for C") +if(MAKE_CLIB OR MAKE_CLIB_STATIC) + add_definitions(-DCLIB) + if(MAKE_CLIB_STATIC) + set(MAKE_CLIB ON) + set(LINK_TYPE STATIC) + message(STATUS "Building static library for C Lib") + else() + message(STATUS "Building shared library for C Lib") + endif() +else() + set(CRYPTONIGHT_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling cryptonite_hash library") +endif() # Set a default build configuration if none is specified. 'Release' produces the fastest binaries if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) @@ -23,6 +37,7 @@ find_package(PythonLibs ${CRYPTONIGHT_PYTHON_VERSION} REQUIRED) # Include path for Python header files include_directories(${PYTHON_INCLUDE_DIR}) + # include all subdirs at source directory include_directories(${PROJECT_SOURCE_DIR}/.) include_directories(${PROJECT_SOURCE_DIR}/crypto) @@ -74,7 +89,27 @@ else() endif() -# Create the binding library + +if(MAKE_CLIB) + # Create the binding library for C + add_library(cryptonite_hash ${LINK_TYPE} + #${PYBIND11_HEADERS} + ${CRYPTO_HEADERS} + ${COMPAT_HEADERS} + ${CRYPTO_SOURCES} + ${ASM} + compat.h + miner.h + cryptonite_hash.h + cryptonite_hash.c + cryptolite_hash.c + sysinfos.c + # ... extra files go here ... +) +set_target_properties(cryptonite_hash PROPERTIES PREFIX "lib") +configure_file(cryptonite_hash_Capi.h cryptonite_hash_Capi.h COPYONLY) +else() +# Create the binding library for python add_library(cryptonite_hash SHARED #${PYBIND11_HEADERS} ${CRYPTO_HEADERS} @@ -90,9 +125,10 @@ add_library(cryptonite_hash SHARED cryptonitehashmodule.c # ... extra files go here ... ) - # Don't add a 'lib' prefix to the shared library set_target_properties(cryptonite_hash PROPERTIES PREFIX "") +endif() + if (WIN32) if (MSVC) @@ -104,20 +140,24 @@ if (WIN32) elseif(MINGW) set_target_properties(cryptonite_hash PROPERTIES COMPILE_FLAGS "-O2 -D_REENTRANT -fmerge-all-constants") endif() - - # .PYD file extension on Windows - set_target_properties(cryptonite_hash PROPERTIES SUFFIX ".pyd") - - # Link against the Python shared library - target_link_libraries(cryptonite_hash ${PYTHON_LIBRARY}) - # Strip unnecessary sections of the binary on MINGW - if (MINGW) - if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) - message(STATUS "Add strip post-build command") - add_custom_command(TARGET cryptonite_hash POST_BUILD COMMAND strip -p --strip-debug --strip-unneeded ${PROJECT_BINARY_DIR}/cryptonite_hash.pyd) - endif() - endif() + if(NOT MAKE_CLIB) + # .PYD file extension on Windows + set_target_properties(cryptonite_hash PROPERTIES SUFFIX ".pyd") + + # Link against the Python shared library + target_link_libraries(cryptonite_hash ${PYTHON_LIBRARY}) + # Strip unnecessary sections of the binary on MINGW + if (MINGW) + if(LINK_TYPE EQUAL STATIC) + set_target_properties(cryptonite_hash PROPERTIES SUFFIX ".a") + endif() + if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) + message(STATUS "Add strip post-build command") + add_custom_command(TARGET cryptonite_hash POST_BUILD COMMAND strip -p --strip-debug --strip-unneeded ${PROJECT_BINARY_DIR}/cryptonite_hash.pyd) + endif() + endif() + endif() elseif (UNIX) # It's quite common to have multiple copies of the same Python version # installed on one's system. E.g.: one copy from the OS and another copy @@ -133,20 +173,31 @@ elseif (UNIX) # import time. set_target_properties(cryptonite_hash PROPERTIES COMPILE_FLAGS "-O2 -Wno-pointer-sign -Wno-pointer-to-int-cast") - # .SO file extension on Linux/Mac OS - set_target_properties(cryptonite_hash PROPERTIES SUFFIX ".so") - - # Strip unnecessary sections of the binary on Linux/Mac OS - message(STATUS "Add strip post-build command") - if(APPLE) - set_target_properties(cryptonite_hash PROPERTIES MACOSX_RPATH ".") - set_target_properties(cryptonite_hash PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip") - if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) - add_custom_command(TARGET cryptonite_hash POST_BUILD COMMAND strip -u -r ${PROJECT_BINARY_DIR}/cryptonite_hash.so) - endif() + + if(${LINK_TYPE} MATCHES SHARED) + set_target_properties(cryptonite_hash PROPERTIES SUFFIX ".so") else() - if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) - add_custom_command(TARGET cryptonite_hash POST_BUILD COMMAND strip ${PROJECT_BINARY_DIR}/cryptonite_hash.so) - endif() + set_target_properties(cryptonite_hash PROPERTIES SUFFIX ".a") endif() -endif() \ No newline at end of file + if(NOT MAKE_CLIB) + # Strip unnecessary sections of the binary on Linux/Mac OS + message(STATUS "Add strip post-build command") + if(APPLE) + set_target_properties(cryptonite_hash PROPERTIES MACOSX_RPATH ".") + set_target_properties(cryptonite_hash PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip") + if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) + add_custom_command(TARGET cryptonite_hash POST_BUILD COMMAND strip -u -r ${PROJECT_BINARY_DIR}/cryptonite_hash.so) + endif() + else() + if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) + add_custom_command(TARGET cryptonite_hash POST_BUILD COMMAND strip ${PROJECT_BINARY_DIR}/cryptonite_hash.so) + endif() + endif() + endif() +endif() + + +if(MAKE_CLIB) + add_executable(LibTestOut Libtest.c) + target_link_libraries(LibTestOut cryptonite_hash) +endif() diff --git a/Libtest.c b/Libtest.c new file mode 100644 index 0000000..d7e2e10 --- /dev/null +++ b/Libtest.c @@ -0,0 +1,24 @@ +#include +#include "cryptonite_hash.h" +// gcc -oLibTest Libtest.c + +int main(){ + char test[] = "This is a test"; + unsigned char out[32]; + cryptonight_hash(out, test, sizeof(test)-1, 0); + printf("String is: %s\n", test); + puts("Hash is: "); + for(int i=0; i<32;i++){ + printf("%02x", out[i]); + } + puts("\n"); + unsigned char correct[] = {0xa0,0x84,0xf0,0x1d,0x14,0x37,0xa0,0x9c,0x69,0x85,0x40,0x1b,0x60,0xd4,0x35,0x54,0xae,0x10,0x58,0x02,0xc5,0xf5,0xd8,0xa9,0xb3,0x25,0x36,0x49,0xc0,0xbe,0x66,0x05}; + for(int i=0; i<32; i++){ + if(out[i] != correct[i]){ + puts("Fail"); + return -1; + } + } + puts("Test passed"); + return 0; +} diff --git a/README.md b/README.md index 6a8e713..c169638 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,15 @@ Copyright (c) 2017, Sumokoin.org -This a python-wrapper lib to give cryptonight hashing functions for Sumo Easy Miner +This a python-wrapper lib with cryptonight hashing functions To complile on Linux: cd /path/to/cryptonight-hash-lib cmake . - make \ No newline at end of file + make + +The library now has C functions. To compile with C: + cd /path/to/cryptonight-hash-lib + cmake . <-DMAKE_CLIB_STATIC=ON/-DMAKE_CLIB=ON> + make diff --git a/cryptonite_hash.c b/cryptonite_hash.c index 335cdd3..eb21562 100644 --- a/cryptonite_hash.c +++ b/cryptonite_hash.c @@ -311,8 +311,8 @@ void cryptonight_hash_ctx_aes_ni(void* output, const void* input, int len, struc //bool aes_ni_supported = false; - -void cryptonight_hash(void* output, const void* input, const int aes_ni_supported) { +#ifndef CLIB +void cryptonight_hash(void* output, const void* input,const int aes_ni_supported) { struct cryptonight_ctx *ctx = (struct cryptonight_ctx*)malloc(sizeof(struct cryptonight_ctx)); if (aes_ni_supported) { @@ -324,7 +324,20 @@ void cryptonight_hash(void* output, const void* input, const int aes_ni_supporte } free(ctx); } - +#else +void cryptonight_hash(void* output, const void* input, unsigned int length,const int aes_ni_supported) { + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*)malloc(sizeof(struct cryptonight_ctx)); + + if (aes_ni_supported) { + cryptonight_hash_ctx_aes_ni(output, input, length, ctx); + } + else + { + cryptonight_hash_ctx(output, input, length, ctx); + } + free(ctx); +} +#endif int scanhash_cryptonight(char* pdata, uint32_t target, uint32_t max_nonce, uint64_t* hashes_done) @@ -364,4 +377,4 @@ int scanhash_cryptonight(char* pdata, uint32_t target, *hashes_done = n - first_nonce + 1; return 0; -} \ No newline at end of file +} diff --git a/cryptonite_hash.h b/cryptonite_hash.h index 708cfbd..033dbef 100644 --- a/cryptonite_hash.h +++ b/cryptonite_hash.h @@ -4,7 +4,8 @@ #include #include -void cryptonight_hash(void* output, const void* input, const int aes_ni_supported); +#ifndef CLIB +void cryptonight_hash(void* output, const void* input,const int aes_ni_supported); int scanhash_cryptonight(char* pdata, uint32_t target, uint32_t max_nonce, uint64_t* hashes_done); void cryptolight_hash(void* output, const void* input, const int aes_ni_supported); @@ -13,5 +14,12 @@ int scanhash_cryptolight(char* pdata, uint32_t target, uint32_t max_nonce, uint6 bool has_aes_ni(void); void bestcpu_feature(char *outbuf, int maxsz); float cpu_temp(int core); +#else -#endif /* CRYPTONIGHT_H */ \ No newline at end of file +void cryptonight_hash(void* output, const void* input,unsigned int length,const int aes_ni_supported); +bool has_aes_ni(void); +void bestcpu_feature(char *outbuf, int maxsz); +float cpu_temp(int core); +#endif + +#endif /* CRYPTONIGHT_H */ diff --git a/cryptonite_hash_Capi.h b/cryptonite_hash_Capi.h new file mode 100644 index 0000000..026df52 --- /dev/null +++ b/cryptonite_hash_Capi.h @@ -0,0 +1,12 @@ +#ifndef CRYPTONITE_HASH_CAPI_H +#define CRYPTONITE_HASH_CAPI_H + +void cryptonight_hash(void* output, const void* input,unsigned int length,const int aes_ni_supported); +bool has_aes_ni(void); +void bestcpu_feature(char *outbuf, int maxsz); +float cpu_temp(int core); + + + + +#endif