From ff7be00f6f29503d6ee0d533f9538d651224cd38 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 10 May 2017 15:06:01 +0300 Subject: [PATCH] Fix test. --- test/cryptonight/CMakeLists.txt | 33 +++------ test/cryptonight/bmi2/CMakeLists.txt | 3 - test/cryptonight/cryptonight.c | 103 +++++++++++---------------- test/cryptonight/cryptonight32.c | 95 ------------------------ 4 files changed, 52 insertions(+), 182 deletions(-) delete mode 100644 test/cryptonight/bmi2/CMakeLists.txt delete mode 100644 test/cryptonight/cryptonight32.c diff --git a/test/cryptonight/CMakeLists.txt b/test/cryptonight/CMakeLists.txt index f15ca5fdb..4ebbbcc9b 100644 --- a/test/cryptonight/CMakeLists.txt +++ b/test/cryptonight/CMakeLists.txt @@ -1,7 +1,12 @@ set(SOURCES + cryptonight.c + ../../options.h ../../algo/cryptonight/cryptonight.h - ../../algo/cryptonight/cryptonight_common.c - ../../algo/cryptonight/cryptonight_av4_softaes.c + ../../algo/cryptonight/cryptonight.c + ../../algo/cryptonight/cryptonight_av1_aesni.c + ../../algo/cryptonight/cryptonight_av2_aesni_double.c + ../../algo/cryptonight/cryptonight_av3_softaes.c + ../../algo/cryptonight/cryptonight_av4_softaes_double.c ../../crypto/c_keccak.c ../../crypto/c_blake256.c ../../crypto/c_groestl.c @@ -10,32 +15,14 @@ set(SOURCES ../../crypto/soft_aes.c ) -if (CMAKE_SIZEOF_VOID_P EQUAL 8) - add_subdirectory(bmi2) - - add_executable(cryptonight_app ${SOURCES} - cryptonight.c - ../../algo/cryptonight/cryptonight_av1_aesni.c - ../../algo/cryptonight/cryptonight_av2_aesni_stak.c - ../../algo/cryptonight/cryptonight_av5_aesni_experimental.c - ) - - target_link_libraries(cryptonight_app unity cryptonight_av3_aesni_bmi2) -else() - add_executable(cryptonight_app ${SOURCES} - cryptonight32.c - ../../algo/cryptonight/cryptonight_av1_aesni32.c - ) - - target_link_libraries(cryptonight_app unity) -endif() - - +add_executable(cryptonight_app ${SOURCES}) +target_link_libraries(cryptonight_app unity) include_directories(../..) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -fno-strict-aliasing") set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2") add_definitions(-DBUILD_TEST) +add_definitions(-DXMRIG_NO_AEON) add_test(cryptonight_test cryptonight_app) diff --git a/test/cryptonight/bmi2/CMakeLists.txt b/test/cryptonight/bmi2/CMakeLists.txt deleted file mode 100644 index 785709a9f..000000000 --- a/test/cryptonight/bmi2/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -mbmi2") -include_directories(../../..) -add_library(cryptonight_av3_aesni_bmi2 STATIC ../../../algo/cryptonight/cryptonight_av3_aesni_bmi2.c) diff --git a/test/cryptonight/cryptonight.c b/test/cryptonight/cryptonight.c index 73c0c582f..bcc0db304 100644 --- a/test/cryptonight/cryptonight.c +++ b/test/cryptonight/cryptonight.c @@ -1,14 +1,25 @@ #include #include #include -#include #include +#include -const static char input1[76] = { - 0x03, 0x05, 0xA0, 0xDB, 0xD6, 0xBF, 0x05, 0xCF, 0x16, 0xE5, 0x03, 0xF3, 0xA6, 0x6F, 0x78, 0x00, 0x7C, 0xBF, 0x34, - 0x14, 0x43, 0x32, 0xEC, 0xBF, 0xC2, 0x2E, 0xD9, 0x5C, 0x87, 0x00, 0x38, 0x3B, 0x30, 0x9A, 0xCE, 0x19, 0x23, 0xA0, - 0x96, 0x4B, 0x00, 0x00, 0x00, 0x08, 0xBA, 0x93, 0x9A, 0x62, 0x72, 0x4C, 0x0D, 0x75, 0x81, 0xFC, 0xE5, 0x76, 0x1E, - 0x9D, 0x8A, 0x0E, 0x6A, 0x1C, 0x3F, 0x92, 0x4F, 0xDD, 0x84, 0x93, 0xD1, 0x11, 0x56, 0x49, 0xC0, 0x5E, 0xB6, 0x01 +#include "options.h" +#include "algo/cryptonight/cryptonight.h" + +bool opt_double_hash = false; + +const static char input1[152] = { + 0x03, 0x05, 0xA0, 0xDB, 0xD6, 0xBF, 0x05, 0xCF, 0x16, 0xE5, 0x03, 0xF3, 0xA6, 0x6F, 0x78, 0x00, + 0x7C, 0xBF, 0x34, 0x14, 0x43, 0x32, 0xEC, 0xBF, 0xC2, 0x2E, 0xD9, 0x5C, 0x87, 0x00, 0x38, 0x3B, + 0x30, 0x9A, 0xCE, 0x19, 0x23, 0xA0, 0x96, 0x4B, 0x00, 0x00, 0x00, 0x08, 0xBA, 0x93, 0x9A, 0x62, + 0x72, 0x4C, 0x0D, 0x75, 0x81, 0xFC, 0xE5, 0x76, 0x1E, 0x9D, 0x8A, 0x0E, 0x6A, 0x1C, 0x3F, 0x92, + 0x4F, 0xDD, 0x84, 0x93, 0xD1, 0x11, 0x56, 0x49, 0xC0, 0x5E, 0xB6, 0x01, + 0x01, 0x00, 0xFB, 0x8E, 0x8A, 0xC8, 0x05, 0x89, 0x93, 0x23, 0x37, 0x1B, 0xB7, 0x90, 0xDB, 0x19, + 0x21, 0x8A, 0xFD, 0x8D, 0xB8, 0xE3, 0x75, 0x5D, 0x8B, 0x90, 0xF3, 0x9B, 0x3D, 0x55, 0x06, 0xA9, + 0xAB, 0xCE, 0x4F, 0xA9, 0x12, 0x24, 0x45, 0x00, 0x00, 0x00, 0x00, 0xEE, 0x81, 0x46, 0xD4, 0x9F, + 0xA9, 0x3E, 0xE7, 0x24, 0xDE, 0xB5, 0x7D, 0x12, 0xCB, 0xC6, 0xC6, 0xF3, 0xB9, 0x24, 0xD9, 0x46, + 0x12, 0x7C, 0x7A, 0x97, 0x41, 0x8F, 0x93, 0x48, 0x82, 0x8F, 0x0F, 0x02, }; const static char input2[] = "This is a test"; @@ -16,16 +27,16 @@ const static char input3[] = "Lorem ipsum dolor sit amet, consectetur adipiscing void cryptonight_av1_aesni(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_av2_aesni_stak(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_av3_aesni_bmi2(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_av4_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_av5_aesni_experimental(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); +void cryptonight_av2_aesni_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); +void cryptonight_av3_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); +void cryptonight_av4_softaes_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -static char hash[32]; -#define RESULT1 "1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd080100" -#define RESULT2 "a084f01d1437a09c6985401b60d43554ae105802c5f5d8a9b3253649c0be6605" -#define RESULT3 "0bbe54bd26caa92a1d436eec71cbef02560062fa689fe14d7efcf42566b411cf" +static char hash[64]; +#define RESULT1 "1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd080100" +#define RESULT1_DOUBLE "1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd0801001b606a3f4a07d6489a1bcd07697bd16696b61c8ae982f61a90160f4e52828a7f" +#define RESULT2 "a084f01d1437a09c6985401b60d43554ae105802c5f5d8a9b3253649c0be6605" +#define RESULT3 "0bbe54bd26caa92a1d436eec71cbef02560062fa689fe14d7efcf42566b411cf" static char *bin2hex(const unsigned char *p, size_t len) @@ -43,24 +54,24 @@ static char *bin2hex(const unsigned char *p, size_t len) } -static void * create_ctx() { - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) malloc(sizeof(struct cryptonight_ctx)); - ctx->memory = (uint8_t *) malloc(MEMORY); +static void * create_ctx(int ratio) { + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) _mm_malloc(sizeof(struct cryptonight_ctx), 16); + ctx->memory = (uint8_t *) _mm_malloc(MEMORY * ratio, 16); return ctx; } static void free_ctx(struct cryptonight_ctx *ctx) { - free(ctx->memory); - free(ctx); + _mm_free(ctx->memory); + _mm_free(ctx); } void test_cryptonight_av1_should_CalcHash(void) { - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(); + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(1); - cryptonight_av1_aesni(input1, sizeof(input1), &hash, ctx); + cryptonight_av1_aesni(input1, 76, &hash, ctx); TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); cryptonight_av1_aesni(input2, strlen(input2), &hash, ctx); @@ -75,16 +86,10 @@ void test_cryptonight_av1_should_CalcHash(void) { void test_cryptonight_av2_should_CalcHash(void) { - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(); + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(2); - cryptonight_av2_aesni_stak(input1, sizeof(input1), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); - - cryptonight_av2_aesni_stak(input2, strlen(input2), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT2, bin2hex(hash, 32)); - - cryptonight_av2_aesni_stak(input3, strlen(input3), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT3, bin2hex(hash, 32)); + cryptonight_av2_aesni_double(input1, 76, &hash, ctx); + TEST_ASSERT_EQUAL_STRING(RESULT1_DOUBLE, bin2hex(hash, 64)); free_ctx(ctx); } @@ -92,15 +97,15 @@ void test_cryptonight_av2_should_CalcHash(void) void test_cryptonight_av3_should_CalcHash(void) { - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(); + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(1); - cryptonight_av3_aesni_bmi2(input1, sizeof(input1), &hash, ctx); + cryptonight_av3_softaes(input1, 76, &hash, ctx); TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); - cryptonight_av3_aesni_bmi2(input2, strlen(input2), &hash, ctx); + cryptonight_av3_softaes(input2, strlen(input2), &hash, ctx); TEST_ASSERT_EQUAL_STRING(RESULT2, bin2hex(hash, 32)); - cryptonight_av3_aesni_bmi2(input3, strlen(input3), &hash, ctx); + cryptonight_av3_softaes(input3, strlen(input3), &hash, ctx); TEST_ASSERT_EQUAL_STRING(RESULT3, bin2hex(hash, 32)); free_ctx(ctx); @@ -109,33 +114,10 @@ void test_cryptonight_av3_should_CalcHash(void) void test_cryptonight_av4_should_CalcHash(void) { - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(); + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(2); - cryptonight_av4_softaes(input1, sizeof(input1), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); - - cryptonight_av4_softaes(input2, strlen(input2), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT2, bin2hex(hash, 32)); - - cryptonight_av4_softaes(input3, strlen(input3), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT3, bin2hex(hash, 32)); - - free_ctx(ctx); -} - - -void test_cryptonight_av5_should_CalcHash(void) -{ - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(); - - cryptonight_av5_aesni_experimental(input1, sizeof(input1), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); - - cryptonight_av5_aesni_experimental(input2, strlen(input2), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT2, bin2hex(hash, 32)); - - cryptonight_av5_aesni_experimental(input3, strlen(input3), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT3, bin2hex(hash, 32)); + cryptonight_av4_softaes_double(input1, 76, &hash, ctx); + TEST_ASSERT_EQUAL_STRING(RESULT1_DOUBLE, bin2hex(hash, 64)); free_ctx(ctx); } @@ -149,7 +131,6 @@ int main(void) RUN_TEST(test_cryptonight_av2_should_CalcHash); RUN_TEST(test_cryptonight_av3_should_CalcHash); RUN_TEST(test_cryptonight_av4_should_CalcHash); - RUN_TEST(test_cryptonight_av5_should_CalcHash); return UNITY_END(); } diff --git a/test/cryptonight/cryptonight32.c b/test/cryptonight/cryptonight32.c deleted file mode 100644 index 997d8ef92..000000000 --- a/test/cryptonight/cryptonight32.c +++ /dev/null @@ -1,95 +0,0 @@ -#include -#include -#include -#include - - -void cryptonight_av1_aesni32(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx); -void cryptonight_av4_legacy(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx); - - -char *bin2hex(const unsigned char *p, size_t len) -{ - int i; - char *s = malloc((len * 2) + 1); - if (!s) - return NULL; - - for (i = 0; i < len; i++) - sprintf(s + (i * 2), "%02x", (unsigned int) p[i]); - - return s; -} - -bool hex2bin(unsigned char *p, const char *hexstr, size_t len) -{ - char hex_byte[3]; - char *ep; - - hex_byte[2] = '\0'; - - while (*hexstr && len) { - if (!hexstr[1]) { - return false; - } - hex_byte[0] = hexstr[0]; - hex_byte[1] = hexstr[1]; - *p = (unsigned char) strtol(hex_byte, &ep, 16); - if (*ep) { - return false; - } - p++; - hexstr += 2; - len--; - } - - return (len == 0 && *hexstr == 0) ? true : false; -} - - -void test_cryptonight_av1_32_should_CalcHash(void) { - char hash[32]; - char data[76]; - - hex2bin((unsigned char *) &data, "0305a0dbd6bf05cf16e503f3a66f78007cbf34144332ecbfc22ed95c8700383b309ace1923a0964b00000008ba939a62724c0d7581fce5761e9d8a0e6a1c3f924fdd8493d1115649c05eb601", 76); - - uint8_t *memory = (uint8_t *) malloc(MEMORY); - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*)malloc(sizeof(struct cryptonight_ctx)); - - cryptonight_av1_aesni32(&hash, data, memory, ctx); - - free(memory); - free(ctx); - - TEST_ASSERT_EQUAL_STRING("1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd080100", bin2hex(hash, 32)); -} - - -void test_cryptonight_av4_should_CalcHash(void) -{ - char hash[32]; - char data[76]; - - hex2bin((unsigned char *) &data, "0305a0dbd6bf05cf16e503f3a66f78007cbf34144332ecbfc22ed95c8700383b309ace1923a0964b00000008ba939a62724c0d7581fce5761e9d8a0e6a1c3f924fdd8493d1115649c05eb601", 76); - - uint8_t *memory = (uint8_t *) malloc(MEMORY); - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*)malloc(sizeof(struct cryptonight_ctx)); - - cryptonight_av4_legacy(&hash, data, memory, ctx); - - free(memory); - free(ctx); - - TEST_ASSERT_EQUAL_STRING("1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd080100", bin2hex(hash, 32)); -} - - -int main(void) -{ - UNITY_BEGIN(); - - RUN_TEST(test_cryptonight_av1_32_should_CalcHash); - RUN_TEST(test_cryptonight_av4_should_CalcHash); - - return UNITY_END(); -}