Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added C support #10

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 82 additions & 31 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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}
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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()
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()
24 changes: 24 additions & 0 deletions Libtest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#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;
}
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
21 changes: 17 additions & 4 deletions cryptonite_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -364,4 +377,4 @@ int scanhash_cryptonight(char* pdata, uint32_t target,
*hashes_done = n - first_nonce + 1;
return 0;

}
}
12 changes: 10 additions & 2 deletions cryptonite_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <stdbool.h>
#include <stdint.h>

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);
Expand All @@ -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 */
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 */
12 changes: 12 additions & 0 deletions cryptonite_hash_Capi.h
Original file line number Diff line number Diff line change
@@ -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