├── .gitignore ├── CHANGELOG.md ├── win ├── app.ico ├── app.rc ├── cpu_win.c ├── xmrig_win.c └── memory_win.c ├── test ├── unity │ └── CMakeLists.txt ├── CMakeLists.txt └── cryptonight │ ├── bmi2 │ └── CMakeLists.txt │ ├── CMakeLists.txt │ ├── cryptonight32.c │ └── cryptonight.c ├── crypto ├── hash.h ├── c_keccak.h ├── hash.c ├── c_jh.h ├── c_blake256.h ├── c_groestl.h ├── c_skein.h ├── c_keccak.c ├── groestl_tables.h └── skein_port.h ├── compat ├── jansson │ ├── CMakeLists.txt │ ├── utf.h │ ├── strbuffer.h │ ├── LICENSE │ ├── jansson_config.h │ ├── memory.c │ ├── error.c │ ├── strbuffer.c │ ├── jansson_private.h │ ├── strconv.c │ ├── utf.c │ ├── hashtable.h │ ├── jansson_private_config.h │ ├── hashtable_seed.c │ └── hashtable.c └── winansi.h ├── donate.h ├── utils ├── summary.h ├── threads.h ├── applog.h ├── summary.c └── applog.c ├── stats.h ├── cpu.h ├── version.h ├── compat.h ├── util.h ├── persistent_memory.h ├── xmrig.h ├── unix ├── cpu_unix.c ├── xmrig_unix.c └── memory_unix.c ├── memory.c ├── algo └── cryptonight │ ├── cryptonight.h │ ├── cryptonight_av1_aesni.c │ ├── cryptonight_av3_softaes.c │ ├── cryptonight_av4_softaes_double.c │ ├── cryptonight_av2_aesni_double.c │ ├── cryptonight.c │ ├── cryptonight_softaes.h │ └── cryptonight_aesni.h ├── options.h ├── stratum.h ├── cpu.c ├── CMakeLists.txt ├── stats.c ├── README.md ├── util.c └── elist.h /.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # v0.7.0 2 | - First AEON release. 3 | -------------------------------------------------------------------------------- /win/app.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmrig/xmrig-aeon/HEAD/win/app.ico -------------------------------------------------------------------------------- /test/unity/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library(unity STATIC unity.c) 2 | target_include_directories(unity PUBLIC .) 3 | -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project("xmrig-test" C) 2 | cmake_minimum_required(VERSION 3.0) 3 | 4 | include(CTest) 5 | 6 | add_subdirectory(unity) 7 | add_subdirectory(cryptonight) -------------------------------------------------------------------------------- /crypto/hash.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | typedef unsigned char BitSequence; 4 | typedef unsigned long long DataLength; 5 | typedef enum {SUCCESS = 0, FAIL = 1, BAD_HASHLEN = 2} HashReturn; 6 | -------------------------------------------------------------------------------- /test/cryptonight/bmi2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -mbmi2") 2 | include_directories(../../..) 3 | add_library(cryptonight_av3_aesni_bmi2 STATIC ../../../algo/cryptonight/cryptonight_av3_aesni_bmi2.c) 4 | -------------------------------------------------------------------------------- /crypto/c_keccak.h: -------------------------------------------------------------------------------- 1 | // keccak.h 2 | // 19-Nov-11 Markku-Juhani O. Saarinen 3 | 4 | #ifndef KECCAK_H 5 | #define KECCAK_H 6 | 7 | #include 8 | #include 9 | 10 | #ifndef KECCAK_ROUNDS 11 | #define KECCAK_ROUNDS 24 12 | #endif 13 | 14 | #ifndef ROTL64 15 | #define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y)))) 16 | #endif 17 | 18 | // compute a keccak hash (md) of given byte length from "in" 19 | int keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen); 20 | 21 | // update the state 22 | void keccakf(uint64_t st[25], int norounds); 23 | 24 | void keccak1600(const uint8_t *in, int inlen, uint8_t *md); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /compat/jansson/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 2.8) 2 | project (jansson C) 3 | 4 | add_definitions(-DHAVE_CONFIG_H) 5 | 6 | # Add the lib sources. 7 | file(GLOB JANSSON_SRC *.c) 8 | 9 | set(JANSSON_HDR_PRIVATE 10 | ${CMAKE_CURRENT_SOURCE_DIR}/hashtable.h 11 | ${CMAKE_CURRENT_SOURCE_DIR}/jansson_private.h 12 | ${CMAKE_CURRENT_SOURCE_DIR}/strbuffer.h 13 | ${CMAKE_CURRENT_SOURCE_DIR}/utf.h 14 | ${CMAKE_CURRENT_SOURCE_DIR}/jansson_private_config.h) 15 | 16 | set(JANSSON_HDR_PUBLIC 17 | ${CMAKE_CURRENT_SOURCE_DIR}/jansson_config.h 18 | ${CMAKE_CURRENT_SOURCE_DIR}/jansson.h) 19 | 20 | add_library(jansson STATIC 21 | ${JANSSON_SRC} 22 | ${JANSSON_HDR_PRIVATE} 23 | ${JANSSON_HDR_PUBLIC}) 24 | -------------------------------------------------------------------------------- /crypto/hash.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2013 The Cryptonote developers 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "hash-ops.h" 10 | #include "c_keccak.h" 11 | 12 | void hash_permutation(union hash_state *state) { 13 | keccakf((uint64_t*)state, 24); 14 | } 15 | 16 | void hash_process(union hash_state *state, const uint8_t *buf, size_t count) { 17 | keccak1600(buf, count, (uint8_t*)state); 18 | } 19 | 20 | void cn_fast_hash(const void *data, size_t length, char *hash) { 21 | union hash_state state; 22 | hash_process(&state, data, length); 23 | memcpy(hash, &state, HASH_SIZE); 24 | } 25 | -------------------------------------------------------------------------------- /crypto/c_jh.h: -------------------------------------------------------------------------------- 1 | /*This program gives the 64-bit optimized bitslice implementation of JH using ANSI C 2 | 3 | -------------------------------- 4 | Performance 5 | 6 | Microprocessor: Intel CORE 2 processor (Core 2 Duo Mobile T6600 2.2GHz) 7 | Operating System: 64-bit Ubuntu 10.04 (Linux kernel 2.6.32-22-generic) 8 | Speed for long message: 9 | 1) 45.8 cycles/byte compiler: Intel C++ Compiler 11.1 compilation option: icc -O2 10 | 2) 56.8 cycles/byte compiler: gcc 4.4.3 compilation option: gcc -O3 11 | 12 | -------------------------------- 13 | Last Modified: January 16, 2011 14 | */ 15 | #pragma once 16 | 17 | #include "hash.h" 18 | 19 | HashReturn jh_hash(int hashbitlen, const BitSequence *data, DataLength databitlen, BitSequence *hashval); 20 | -------------------------------------------------------------------------------- /compat/jansson/utf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2016 Petri Lehtinen 3 | * 4 | * Jansson is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #ifndef UTF_H 9 | #define UTF_H 10 | 11 | #ifdef HAVE_CONFIG_H 12 | #include 13 | #endif 14 | 15 | #ifdef HAVE_STDINT_H 16 | #include 17 | #endif 18 | 19 | int utf8_encode(int32_t codepoint, char *buffer, size_t *size); 20 | 21 | size_t utf8_check_first(char byte); 22 | size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint); 23 | const char *utf8_iterate(const char *buffer, size_t size, int32_t *codepoint); 24 | 25 | int utf8_check_string(const char *string, size_t length); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /compat/winansi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ANSI emulation wrappers 3 | */ 4 | #ifdef WIN32 5 | #include 6 | #include 7 | #include 8 | 9 | #define isatty(fd) _isatty(fd) 10 | #define fileno(fd) _fileno(fd) 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | int winansi_fputs(const char *str, FILE *stream); 16 | int winansi_printf(const char *format, ...); 17 | int winansi_fprintf(FILE *stream, const char *format, ...); 18 | int winansi_vfprintf(FILE *stream, const char *format, va_list list); 19 | #ifdef __cplusplus 20 | } 21 | #endif 22 | 23 | #undef fputs 24 | #undef fprintf 25 | #undef vfprintf 26 | 27 | #define fputs winansi_fputs 28 | #define printf winansi_printf 29 | #define fprintf winansi_fprintf 30 | #define vfprintf winansi_vfprintf 31 | 32 | #endif -------------------------------------------------------------------------------- /compat/jansson/strbuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2016 Petri Lehtinen 3 | * 4 | * Jansson is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #ifndef STRBUFFER_H 9 | #define STRBUFFER_H 10 | 11 | #include 12 | 13 | typedef struct { 14 | char *value; 15 | size_t length; /* bytes used */ 16 | size_t size; /* bytes allocated */ 17 | } strbuffer_t; 18 | 19 | int strbuffer_init(strbuffer_t *strbuff); 20 | void strbuffer_close(strbuffer_t *strbuff); 21 | 22 | void strbuffer_clear(strbuffer_t *strbuff); 23 | 24 | const char *strbuffer_value(const strbuffer_t *strbuff); 25 | 26 | /* Steal the value and close the strbuffer */ 27 | char *strbuffer_steal_value(strbuffer_t *strbuff); 28 | 29 | int strbuffer_append_byte(strbuffer_t *strbuff, char byte); 30 | int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size); 31 | 32 | char strbuffer_pop(strbuffer_t *strbuff); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /win/app.rc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../version.h" 3 | 4 | IDI_ICON1 ICON DISCARDABLE "app.ico" 5 | 6 | VS_VERSION_INFO VERSIONINFO 7 | FILEVERSION APP_VER_MAJOR,APP_VER_MINOR,APP_VER_BUILD,APP_VER_REV 8 | PRODUCTVERSION APP_VER_MAJOR,APP_VER_MINOR,APP_VER_BUILD,APP_VER_REV 9 | FILEFLAGSMASK 0x3fL 10 | #ifdef _DEBUG 11 | FILEFLAGS VS_FF_DEBUG 12 | #else 13 | FILEFLAGS 0x0L 14 | #endif 15 | FILEOS VOS__WINDOWS32 16 | FILETYPE VFT_APP 17 | FILESUBTYPE 0x0L 18 | BEGIN 19 | BLOCK "StringFileInfo" 20 | BEGIN 21 | BLOCK "000004b0" 22 | BEGIN 23 | VALUE "CompanyName", APP_SITE 24 | VALUE "FileDescription", APP_DESC 25 | VALUE "FileVersion", APP_VERSION 26 | VALUE "LegalCopyright", APP_COPYRIGHT 27 | VALUE "OriginalFilename", "xmrig-aeon.exe" 28 | VALUE "ProductName", APP_NAME 29 | VALUE "ProductVersion", APP_VERSION 30 | END 31 | END 32 | BLOCK "VarFileInfo" 33 | BEGIN 34 | VALUE "Translation", 0x0, 1200 35 | END 36 | END 37 | 38 | -------------------------------------------------------------------------------- /compat/jansson/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009-2014 Petri Lehtinen 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /donate.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __DONATE_H__ 25 | #define __DONATE_H__ 26 | 27 | #define DONATE_LEVEL 5 28 | 29 | #endif /* __DONATE_H__ */ 30 | -------------------------------------------------------------------------------- /utils/summary.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __SUMMARY_H__ 25 | #define __SUMMARY_H__ 26 | 27 | void print_summary(); 28 | 29 | #endif /* __SUMMARY_H__ */ 30 | -------------------------------------------------------------------------------- /test/cryptonight/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(SOURCES 2 | ../../algo/cryptonight/cryptonight.h 3 | ../../algo/cryptonight/cryptonight_common.c 4 | ../../algo/cryptonight/cryptonight_av4_softaes.c 5 | ../../crypto/c_keccak.c 6 | ../../crypto/c_blake256.c 7 | ../../crypto/c_groestl.c 8 | ../../crypto/c_jh.c 9 | ../../crypto/c_skein.c 10 | ../../crypto/soft_aes.c 11 | ) 12 | 13 | if (CMAKE_SIZEOF_VOID_P EQUAL 8) 14 | add_subdirectory(bmi2) 15 | 16 | add_executable(cryptonight_app ${SOURCES} 17 | cryptonight.c 18 | ../../algo/cryptonight/cryptonight_av1_aesni.c 19 | ../../algo/cryptonight/cryptonight_av2_aesni_stak.c 20 | ../../algo/cryptonight/cryptonight_av5_aesni_experimental.c 21 | ) 22 | 23 | target_link_libraries(cryptonight_app unity cryptonight_av3_aesni_bmi2) 24 | else() 25 | add_executable(cryptonight_app ${SOURCES} 26 | cryptonight32.c 27 | ../../algo/cryptonight/cryptonight_av1_aesni32.c 28 | ) 29 | 30 | target_link_libraries(cryptonight_app unity) 31 | endif() 32 | 33 | 34 | 35 | include_directories(../..) 36 | 37 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -fno-strict-aliasing") 38 | set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2") 39 | add_definitions(-DBUILD_TEST) 40 | 41 | add_test(cryptonight_test cryptonight_app) 42 | -------------------------------------------------------------------------------- /crypto/c_blake256.h: -------------------------------------------------------------------------------- 1 | #ifndef _BLAKE256_H_ 2 | #define _BLAKE256_H_ 3 | 4 | #include 5 | 6 | typedef struct { 7 | uint32_t h[8], s[4], t[2]; 8 | int buflen, nullt; 9 | uint8_t buf[64]; 10 | } state; 11 | 12 | typedef struct { 13 | state inner; 14 | state outer; 15 | } hmac_state; 16 | 17 | void blake256_init(state *); 18 | void blake224_init(state *); 19 | 20 | void blake256_update(state *, const uint8_t *, uint64_t); 21 | void blake224_update(state *, const uint8_t *, uint64_t); 22 | 23 | void blake256_final(state *, uint8_t *); 24 | void blake224_final(state *, uint8_t *); 25 | 26 | void blake256_hash(uint8_t *, const uint8_t *, uint64_t); 27 | void blake224_hash(uint8_t *, const uint8_t *, uint64_t); 28 | 29 | /* HMAC functions: */ 30 | 31 | void hmac_blake256_init(hmac_state *, const uint8_t *, uint64_t); 32 | void hmac_blake224_init(hmac_state *, const uint8_t *, uint64_t); 33 | 34 | void hmac_blake256_update(hmac_state *, const uint8_t *, uint64_t); 35 | void hmac_blake224_update(hmac_state *, const uint8_t *, uint64_t); 36 | 37 | void hmac_blake256_final(hmac_state *, uint8_t *); 38 | void hmac_blake224_final(hmac_state *, uint8_t *); 39 | 40 | void hmac_blake256_hash(uint8_t *, const uint8_t *, uint64_t, const uint8_t *, uint64_t); 41 | void hmac_blake224_hash(uint8_t *, const uint8_t *, uint64_t, const uint8_t *, uint64_t); 42 | 43 | #endif /* _BLAKE256_H_ */ 44 | -------------------------------------------------------------------------------- /stats.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __STATS_H__ 25 | #define __STATS_H__ 26 | 27 | #include 28 | #include 29 | 30 | 31 | void stats_init(); 32 | void stats_set_target(uint32_t new_target); 33 | void stats_share_result(bool success); 34 | void stats_add_hashes(int thr_id, struct timeval *tv_start, unsigned long hashes_done); 35 | 36 | 37 | #endif /* __STATS_H__ */ 38 | -------------------------------------------------------------------------------- /compat/jansson/jansson_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2016 Petri Lehtinen 3 | * 4 | * Jansson is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | * 7 | * 8 | * This file specifies a part of the site-specific configuration for 9 | * Jansson, namely those things that affect the public API in 10 | * jansson.h. 11 | * 12 | * The configure script copies this file to jansson_config.h and 13 | * replaces @var@ substitutions by values that fit your system. If you 14 | * cannot run the configure script, you can do the value substitution 15 | * by hand. 16 | */ 17 | 18 | #ifndef JANSSON_CONFIG_H 19 | #define JANSSON_CONFIG_H 20 | 21 | /* If your compiler supports the inline keyword in C, JSON_INLINE is 22 | defined to `inline', otherwise empty. In C++, the inline is always 23 | supported. */ 24 | #ifdef __cplusplus 25 | #define JSON_INLINE inline 26 | #else 27 | #define JSON_INLINE inline 28 | #endif 29 | 30 | /* If your compiler supports the `long long` type and the strtoll() 31 | library function, JSON_INTEGER_IS_LONG_LONG is defined to 1, 32 | otherwise to 0. */ 33 | #define JSON_INTEGER_IS_LONG_LONG 1 34 | 35 | /* If locale.h and localeconv() are available, define to 1, 36 | otherwise to 0. */ 37 | #define JSON_HAVE_LOCALECONV 1 38 | 39 | /* Maximum recursion depth for parsing JSON input. 40 | This limits the depth of e.g. array-within-array constructions. */ 41 | #define JSON_PARSER_MAX_DEPTH 2048 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /cpu.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __CPU_H__ 25 | #define __CPU_H__ 26 | 27 | struct cpu_info { 28 | int count; 29 | int flags; 30 | char brand[48]; 31 | }; 32 | 33 | extern struct cpu_info cpu_info; 34 | 35 | 36 | enum cpu_flags { 37 | CPU_FLAG_X86_64 = 1, 38 | CPU_FLAG_AES = 2, 39 | CPU_FLAG_BMI2 = 4 40 | }; 41 | 42 | 43 | 44 | void cpu_init(); 45 | int get_optimal_threads_count(); 46 | int affine_to_cpu_mask(int id, unsigned long mask); 47 | 48 | #endif /* __CPU_H__ */ 49 | -------------------------------------------------------------------------------- /version.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __VERSION_H__ 25 | #define __VERSION_H__ 26 | 27 | #define APP_ID "xmrig" 28 | #define APP_NAME "XMRig" 29 | #define APP_DESC "AEON CPU miner" 30 | #define APP_VERSION "0.7.0" 31 | #define APP_DOMAIN "xmrig.com" 32 | #define APP_SITE "www.xmrig.com" 33 | #define APP_COPYRIGHT "Copyright (C) 2016-2017 xmrig.com" 34 | 35 | #define APP_VER_MAJOR 0 36 | #define APP_VER_MINOR 7 37 | #define APP_VER_BUILD 0 38 | #define APP_VER_REV 0 39 | 40 | #endif /* __VERSION_H__ */ 41 | -------------------------------------------------------------------------------- /compat/jansson/memory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2016 Petri Lehtinen 3 | * Copyright (c) 2011-2012 Basile Starynkevitch 4 | * 5 | * Jansson is free software; you can redistribute it and/or modify it 6 | * under the terms of the MIT license. See LICENSE for details. 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | #include "jansson.h" 13 | #include "jansson_private.h" 14 | 15 | /* C89 allows these to be macros */ 16 | #undef malloc 17 | #undef free 18 | 19 | /* memory function pointers */ 20 | static json_malloc_t do_malloc = malloc; 21 | static json_free_t do_free = free; 22 | 23 | void *jsonp_malloc(size_t size) 24 | { 25 | if(!size) 26 | return NULL; 27 | 28 | return (*do_malloc)(size); 29 | } 30 | 31 | void jsonp_free(void *ptr) 32 | { 33 | if(!ptr) 34 | return; 35 | 36 | (*do_free)(ptr); 37 | } 38 | 39 | char *jsonp_strdup(const char *str) 40 | { 41 | return jsonp_strndup(str, strlen(str)); 42 | } 43 | 44 | char *jsonp_strndup(const char *str, size_t len) 45 | { 46 | char *new_str; 47 | 48 | new_str = jsonp_malloc(len + 1); 49 | if(!new_str) 50 | return NULL; 51 | 52 | memcpy(new_str, str, len); 53 | new_str[len] = '\0'; 54 | return new_str; 55 | } 56 | 57 | void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn) 58 | { 59 | do_malloc = malloc_fn; 60 | do_free = free_fn; 61 | } 62 | 63 | void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn) 64 | { 65 | if (malloc_fn) 66 | *malloc_fn = do_malloc; 67 | if (free_fn) 68 | *free_fn = do_free; 69 | } 70 | -------------------------------------------------------------------------------- /compat.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __COMPAT_H__ 25 | #define __COMPAT_H__ 26 | 27 | #define unlikely(expr) (__builtin_expect(!!(expr), 0)) 28 | #define likely(expr) (__builtin_expect(!!(expr), 1)) 29 | 30 | #ifdef WIN32 31 | 32 | #include 33 | 34 | #define sleep(secs) Sleep((secs) * 1000) 35 | 36 | enum { 37 | PRIO_PROCESS = 0, 38 | }; 39 | 40 | static inline int setpriority(int which, int who, int prio) 41 | { 42 | return -!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE); 43 | } 44 | 45 | #endif /* WIN32 */ 46 | 47 | #endif /* __COMPAT_H__ */ 48 | -------------------------------------------------------------------------------- /util.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __UTIL_H__ 25 | #define __UTIL_H__ 26 | 27 | #include 28 | 29 | 30 | json_t *json_decode(const char *s); 31 | 32 | char *bin2hex(const unsigned char *p, size_t len); 33 | bool hex2bin(unsigned char *p, const char *hexstr, size_t len); 34 | 35 | struct thread_q *tq_new(void); 36 | void tq_free(struct thread_q *tq); 37 | bool tq_push(struct thread_q *tq, void *data); 38 | void *tq_pop(struct thread_q *tq, const struct timespec *abstime); 39 | void tq_freeze(struct thread_q *tq); 40 | void tq_thaw(struct thread_q *tq); 41 | 42 | 43 | #endif /* __UTIL_H__ */ 44 | -------------------------------------------------------------------------------- /crypto/c_groestl.h: -------------------------------------------------------------------------------- 1 | #ifndef __hash_h 2 | #define __hash_h 3 | /* 4 | #include "crypto_uint8.h" 5 | #include "crypto_uint32.h" 6 | #include "crypto_uint64.h" 7 | #include "crypto_hash.h" 8 | 9 | typedef crypto_uint8 uint8_t; 10 | typedef crypto_uint32 uint32_t; 11 | typedef crypto_uint64 uint64_t; 12 | */ 13 | #include 14 | 15 | #include "hash.h" 16 | 17 | /* some sizes (number of bytes) */ 18 | #define ROWS 8 19 | #define LENGTHFIELDLEN ROWS 20 | #define COLS512 8 21 | 22 | #define SIZE512 (ROWS*COLS512) 23 | 24 | #define ROUNDS512 10 25 | #define HASH_BIT_LEN 256 26 | 27 | #define ROTL32(v, n) ((((v)<<(n))|((v)>>(32-(n))))&li_32(ffffffff)) 28 | 29 | 30 | #define li_32(h) 0x##h##u 31 | #define EXT_BYTE(var,n) ((uint8_t)((uint32_t)(var) >> (8*n))) 32 | #define u32BIG(a) \ 33 | ((ROTL32(a,8) & li_32(00FF00FF)) | \ 34 | (ROTL32(a,24) & li_32(FF00FF00))) 35 | 36 | 37 | /* NIST API begin */ 38 | typedef struct { 39 | uint32_t chaining[SIZE512/sizeof(uint32_t)]; /* actual state */ 40 | uint32_t block_counter1, 41 | block_counter2; /* message block counter(s) */ 42 | BitSequence buffer[SIZE512]; /* data buffer */ 43 | int buf_ptr; /* data buffer pointer */ 44 | int bits_in_last_byte; /* no. of message bits in last byte of 45 | data buffer */ 46 | } groestlHashState; 47 | 48 | /*void Init(hashState*); 49 | void Update(hashState*, const BitSequence*, DataLength); 50 | void Final(hashState*, BitSequence*); */ 51 | void groestl(const BitSequence*, DataLength, BitSequence*); 52 | /* NIST API end */ 53 | 54 | /* 55 | int crypto_hash(unsigned char *out, 56 | const unsigned char *in, 57 | unsigned long long len); 58 | */ 59 | 60 | #endif /* __hash_h */ 61 | -------------------------------------------------------------------------------- /persistent_memory.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __PERSISTENT_MEMORY_H__ 25 | #define __PERSISTENT_MEMORY_H__ 26 | 27 | #include 28 | 29 | 30 | enum memory_flags { 31 | MEMORY_HUGEPAGES_AVAILABLE = 1, 32 | MEMORY_HUGEPAGES_ENABLED = 2, 33 | MEMORY_LOCK = 4 34 | }; 35 | 36 | 37 | #define MEMORY 2097152 38 | 39 | 40 | extern char *persistent_memory; 41 | extern int persistent_memory_flags; 42 | 43 | 44 | const char * persistent_memory_allocate(); 45 | void persistent_memory_free(); 46 | void * persistent_calloc(size_t num, size_t size); 47 | void * create_persistent_ctx(int thr_id); 48 | 49 | 50 | #endif /* __PERSISTENT_MEMORY_H__ */ 51 | -------------------------------------------------------------------------------- /compat/jansson/error.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "jansson_private.h" 3 | 4 | void jsonp_error_init(json_error_t *error, const char *source) 5 | { 6 | if(error) 7 | { 8 | error->text[0] = '\0'; 9 | error->line = -1; 10 | error->column = -1; 11 | error->position = 0; 12 | if(source) 13 | jsonp_error_set_source(error, source); 14 | else 15 | error->source[0] = '\0'; 16 | } 17 | } 18 | 19 | void jsonp_error_set_source(json_error_t *error, const char *source) 20 | { 21 | size_t length; 22 | 23 | if(!error || !source) 24 | return; 25 | 26 | length = strlen(source); 27 | if(length < JSON_ERROR_SOURCE_LENGTH) 28 | strncpy(error->source, source, length + 1); 29 | else { 30 | size_t extra = length - JSON_ERROR_SOURCE_LENGTH + 4; 31 | strncpy(error->source, "...", 3); 32 | strncpy(error->source + 3, source + extra, length - extra + 1); 33 | } 34 | } 35 | 36 | void jsonp_error_set(json_error_t *error, int line, int column, 37 | size_t position, const char *msg, ...) 38 | { 39 | va_list ap; 40 | 41 | va_start(ap, msg); 42 | jsonp_error_vset(error, line, column, position, msg, ap); 43 | va_end(ap); 44 | } 45 | 46 | void jsonp_error_vset(json_error_t *error, int line, int column, 47 | size_t position, const char *msg, va_list ap) 48 | { 49 | if(!error) 50 | return; 51 | 52 | if(error->text[0] != '\0') { 53 | /* error already set */ 54 | return; 55 | } 56 | 57 | error->line = line; 58 | error->column = column; 59 | error->position = (int)position; 60 | 61 | vsnprintf(error->text, JSON_ERROR_TEXT_LENGTH, msg, ap); 62 | error->text[JSON_ERROR_TEXT_LENGTH - 1] = '\0'; 63 | } 64 | -------------------------------------------------------------------------------- /utils/threads.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __THREADS_H__ 25 | #define __THREADS_H__ 26 | 27 | #if defined(WIN32) && defined(USE_NATIVE_THREADS) 28 | # include 29 | # define MUTEX CRITICAL_SECTION 30 | # define MUTEX_INIT(mutex) InitializeCriticalSection(&mutex) 31 | # define MUTEX_LOCK(mutex) EnterCriticalSection(&mutex) 32 | # define MUTEX_UNLOCK(mutex) LeaveCriticalSection(&mutex) 33 | #else 34 | # include 35 | # define MUTEX pthread_mutex_t 36 | # define MUTEX_INIT(mutex) pthread_mutex_init(&mutex, NULL) 37 | # define MUTEX_LOCK(mutex) pthread_mutex_lock(&mutex) 38 | # define MUTEX_UNLOCK(mutex) pthread_mutex_unlock(&mutex) 39 | #endif 40 | 41 | #endif /* __THREADS_H__ */ 42 | -------------------------------------------------------------------------------- /xmrig.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __XMRIG_H__ 25 | #define __XMRIG_H__ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #define unlikely(expr) (__builtin_expect(!!(expr), 0)) 34 | #define likely(expr) (__builtin_expect(!!(expr), 1)) 35 | 36 | 37 | struct thr_info { 38 | int id; 39 | pthread_t pth; 40 | struct thread_q *q; 41 | }; 42 | 43 | 44 | struct work_restart { 45 | volatile unsigned long restart; 46 | char padding[128 - sizeof(unsigned long)]; 47 | }; 48 | 49 | 50 | struct work; 51 | 52 | 53 | extern struct thr_info *thr_info; 54 | extern struct work_restart *work_restart; 55 | extern void os_specific_init(); 56 | 57 | #endif /* __XMRIG_H__ */ 58 | -------------------------------------------------------------------------------- /win/cpu_win.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #include 25 | #include 26 | 27 | #include "cpu.h" 28 | 29 | 30 | struct cpu_info cpu_info = { 0 }; 31 | void cpu_init_common(); 32 | 33 | 34 | void cpu_init() { 35 | SYSTEM_INFO sysinfo; 36 | GetSystemInfo(&sysinfo); 37 | 38 | cpu_info.count = sysinfo.dwNumberOfProcessors; 39 | 40 | cpu_init_common(); 41 | } 42 | 43 | 44 | int get_optimal_threads_count(int mining_algo) { 45 | int count = cpu_info.count / 2; 46 | return count < 1 ? 1 : count; 47 | } 48 | 49 | 50 | int affine_to_cpu_mask(int id, unsigned long mask) 51 | { 52 | if (id == -1) { 53 | SetProcessAffinityMask(GetCurrentProcess(), mask); 54 | } 55 | else { 56 | SetThreadAffinityMask(GetCurrentThread(), mask); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /unix/cpu_unix.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #include "cpu.h" 29 | 30 | 31 | struct cpu_info cpu_info = { 0 }; 32 | void cpu_init_common(); 33 | 34 | 35 | void cpu_init() { 36 | cpu_info.count = sysconf(_SC_NPROCESSORS_CONF); 37 | 38 | cpu_init_common(); 39 | } 40 | 41 | 42 | int get_optimal_threads_count() { 43 | int count = cpu_info.count / 2; 44 | return count < 1 ? 1 : count; 45 | } 46 | 47 | 48 | int affine_to_cpu_mask(int id, unsigned long mask) 49 | { 50 | cpu_set_t set; 51 | CPU_ZERO(&set); 52 | 53 | for (unsigned i = 0; i < cpu_info.count; i++) { 54 | if (mask & (1UL << i)) { 55 | CPU_SET(i, &set); 56 | } 57 | } 58 | 59 | if (id == -1) { 60 | sched_setaffinity(0, sizeof(&set), &set); 61 | } else { 62 | pthread_setaffinity_np(pthread_self(), sizeof(&set), &set); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /crypto/c_skein.h: -------------------------------------------------------------------------------- 1 | #ifndef _SKEIN_H_ 2 | #define _SKEIN_H_ 1 3 | /************************************************************************** 4 | ** 5 | ** Interface declarations and internal definitions for Skein hashing. 6 | ** 7 | ** Source code author: Doug Whiting, 2008. 8 | ** 9 | ** This algorithm and source code is released to the public domain. 10 | ** 11 | *************************************************************************** 12 | ** 13 | ** The following compile-time switches may be defined to control some 14 | ** tradeoffs between speed, code size, error checking, and security. 15 | ** 16 | ** The "default" note explains what happens when the switch is not defined. 17 | ** 18 | ** SKEIN_DEBUG -- make callouts from inside Skein code 19 | ** to examine/display intermediate values. 20 | ** [default: no callouts (no overhead)] 21 | ** 22 | ** SKEIN_ERR_CHECK -- how error checking is handled inside Skein 23 | ** code. If not defined, most error checking 24 | ** is disabled (for performance). Otherwise, 25 | ** the switch value is interpreted as: 26 | ** 0: use assert() to flag errors 27 | ** 1: return SKEIN_FAIL to flag errors 28 | ** 29 | ***************************************************************************/ 30 | #include "skein_port.h" /* get platform-specific definitions */ 31 | 32 | typedef enum 33 | { 34 | SKEIN_SUCCESS = 0, /* return codes from Skein calls */ 35 | SKEIN_FAIL = 1, 36 | SKEIN_BAD_HASHLEN = 2 37 | } 38 | SkeinHashReturn; 39 | 40 | typedef size_t SkeinDataLength; /* bit count type */ 41 | typedef u08b_t SkeinBitSequence; /* bit stream type */ 42 | 43 | /* "all-in-one" call */ 44 | SkeinHashReturn skein_hash(int hashbitlen, const SkeinBitSequence *data, 45 | SkeinDataLength databitlen, SkeinBitSequence *hashval); 46 | 47 | #endif /* ifndef _SKEIN_H_ */ 48 | -------------------------------------------------------------------------------- /memory.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #include 25 | 26 | #include "persistent_memory.h" 27 | #include "options.h" 28 | #include "algo/cryptonight/cryptonight.h" 29 | 30 | static size_t offset = 0; 31 | 32 | 33 | void * persistent_calloc(size_t num, size_t size) { 34 | void *mem = &persistent_memory[offset]; 35 | offset += (num * size); 36 | 37 | memset(mem, 0, num * size); 38 | 39 | return mem; 40 | } 41 | 42 | 43 | void * create_persistent_ctx(int thr_id) { 44 | struct cryptonight_ctx *ctx = NULL; 45 | 46 | if (!opt_double_hash) { 47 | const size_t offset = MEMORY * (thr_id + 1); 48 | 49 | ctx = (struct cryptonight_ctx *) &persistent_memory[offset + MEMORY_LITE]; 50 | ctx->memory = &persistent_memory[offset]; 51 | return ctx; 52 | } 53 | 54 | ctx = (struct cryptonight_ctx *) &persistent_memory[MEMORY - sizeof(struct cryptonight_ctx) * (thr_id + 1)]; 55 | ctx->memory = &persistent_memory[MEMORY * (thr_id + 1)]; 56 | 57 | return ctx; 58 | } 59 | -------------------------------------------------------------------------------- /algo/cryptonight/cryptonight.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __CRYPTONIGHT_H__ 25 | #define __CRYPTONIGHT_H__ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #define MEMORY 2097152 /* 2 MiB */ 32 | #define MEMORY_LITE 1048576 /* 1 MiB */ 33 | 34 | struct cryptonight_ctx { 35 | uint8_t state0[200] __attribute__((aligned(16))); 36 | uint8_t state1[200] __attribute__((aligned(16))); 37 | uint8_t* memory __attribute__((aligned(16))); 38 | }; 39 | 40 | 41 | extern void (* const extra_hashes[4])(const void *, size_t, char *); 42 | 43 | bool cryptonight_init(int variant); 44 | int scanhash_cryptonight(int thr_id, uint32_t *hash, uint32_t *restrict blob, size_t blob_size, uint32_t target, uint32_t max_nonce, unsigned long *restrict hashes_done, struct cryptonight_ctx *restrict ctx); 45 | int scanhash_cryptonight_double(int thr_id, uint32_t *hash, uint8_t *restrict blob, size_t blob_size, uint32_t target, uint32_t max_nonce, unsigned long *restrict hashes_done, struct cryptonight_ctx *restrict ctx); 46 | 47 | #endif /* __CRYPTONIGHT_H__ */ 48 | -------------------------------------------------------------------------------- /options.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __OPTIONS_H__ 25 | #define __OPTIONS_H__ 26 | 27 | #include 28 | #include 29 | 30 | #ifndef ARRAY_SIZE 31 | # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 32 | #endif 33 | 34 | 35 | enum xmr_algo_variant { 36 | AEON_AV0_AUTO, 37 | AEON_AV1_AESNI, 38 | AEON_AV2_AESNI_DOUBLE, 39 | AEON_AV3_SOFT_AES, 40 | AEON_AV4_SOFT_AES_DOUBLE, 41 | AEON_AV_MAX 42 | }; 43 | 44 | 45 | extern bool opt_colors; 46 | extern bool opt_keepalive; 47 | extern bool opt_background; 48 | extern bool opt_double_hash; 49 | extern char *opt_url; 50 | extern char *opt_backup_url; 51 | extern char *opt_userpass; 52 | extern char *opt_user; 53 | extern char *opt_pass; 54 | extern int opt_n_threads; 55 | extern int opt_algo_variant; 56 | extern int opt_retry_pause; 57 | extern int opt_retries; 58 | extern int opt_donate_level; 59 | extern int64_t opt_affinity; 60 | 61 | void parse_cmdline(int argc, char *argv[]); 62 | void show_usage_and_exit(int status); 63 | void show_version_and_exit(void); 64 | 65 | extern void proper_exit(int reason); 66 | 67 | 68 | #endif /* __OPTIONS_H__ */ 69 | -------------------------------------------------------------------------------- /win/xmrig_win.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #include 25 | 26 | #include "options.h" 27 | #include "cpu.h" 28 | #include "utils/applog.h" 29 | 30 | 31 | BOOL WINAPI ConsoleHandler(DWORD dwType) 32 | { 33 | switch (dwType) { 34 | case CTRL_C_EVENT: 35 | applog(LOG_WARNING, "CTRL_C_EVENT received, exiting"); 36 | proper_exit(0); 37 | break; 38 | 39 | case CTRL_BREAK_EVENT: 40 | applog(LOG_WARNING, "CTRL_BREAK_EVENT received, exiting"); 41 | proper_exit(0); 42 | break; 43 | 44 | default: 45 | return false; 46 | } 47 | 48 | return true; 49 | } 50 | 51 | 52 | void proper_exit(int reason) { 53 | if (opt_background) { 54 | HWND hcon = GetConsoleWindow(); 55 | if (hcon) { 56 | // unhide parent command line windows 57 | ShowWindow(hcon, SW_SHOWMINNOACTIVE); 58 | } 59 | } 60 | 61 | exit(reason); 62 | } 63 | 64 | 65 | void os_specific_init() 66 | { 67 | if (opt_affinity != -1) { 68 | affine_to_cpu_mask(-1, opt_affinity); 69 | } 70 | 71 | SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler, TRUE); 72 | 73 | if (opt_background) { 74 | HWND hcon = GetConsoleWindow(); 75 | if (hcon) { 76 | // this method also hide parent command line window 77 | ShowWindow(hcon, SW_HIDE); 78 | } else { 79 | HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); 80 | CloseHandle(h); 81 | FreeConsole(); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /unix/xmrig_unix.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include "options.h" 30 | #include "cpu.h" 31 | #include "utils/applog.h" 32 | 33 | 34 | static void signal_handler(int sig) 35 | { 36 | switch (sig) { 37 | case SIGHUP: 38 | applog(LOG_WARNING, "SIGHUP received"); 39 | break; 40 | 41 | case SIGINT: 42 | applog(LOG_WARNING, "SIGINT received, exiting"); 43 | proper_exit(0); 44 | break; 45 | 46 | case SIGTERM: 47 | applog(LOG_WARNING, "SIGTERM received, exiting"); 48 | proper_exit(0); 49 | break; 50 | } 51 | } 52 | 53 | 54 | void proper_exit(int reason) { 55 | exit(reason); 56 | } 57 | 58 | 59 | void os_specific_init() 60 | { 61 | if (opt_affinity != -1) { 62 | affine_to_cpu_mask(-1, opt_affinity); 63 | } 64 | 65 | if (opt_background) { 66 | int i = fork(); 67 | if (i < 0) { 68 | exit(1); 69 | } 70 | 71 | if (i > 0) { 72 | exit(0); 73 | } 74 | 75 | i = setsid(); 76 | 77 | if (i < 0) { 78 | applog(LOG_ERR, "setsid() failed (errno = %d)", errno); 79 | } 80 | 81 | i = chdir("/"); 82 | if (i < 0) { 83 | applog(LOG_ERR, "chdir() failed (errno = %d)", errno); 84 | } 85 | 86 | signal(SIGHUP, signal_handler); 87 | signal(SIGTERM, signal_handler); 88 | } 89 | 90 | signal(SIGINT, signal_handler); 91 | } 92 | -------------------------------------------------------------------------------- /utils/applog.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __APPLOG_H__ 25 | #define __APPLOG_H__ 26 | 27 | enum { 28 | LOG_ERR, 29 | LOG_WARNING, 30 | LOG_NOTICE, 31 | LOG_INFO, 32 | LOG_DEBUG, 33 | LOG_BLUE = 0x10 34 | }; 35 | 36 | #define CL_N "\x1B[0m" 37 | #define CL_RED "\x1B[31m" 38 | #define CL_GRN "\x1B[32m" 39 | #define CL_YLW "\x1B[33m" 40 | #define CL_BLU "\x1B[34m" 41 | #define CL_MAG "\x1B[35m" 42 | #define CL_CYN "\x1B[36m" 43 | 44 | #define CL_BLK "\x1B[22;30m" /* black */ 45 | #define CL_RD2 "\x1B[22;31m" /* red */ 46 | #define CL_GR2 "\x1B[22;32m" /* green */ 47 | #define CL_BRW "\x1B[22;33m" /* brown */ 48 | #define CL_BL2 "\x1B[22;34m" /* blue */ 49 | #define CL_MA2 "\x1B[22;35m" /* magenta */ 50 | #define CL_CY2 "\x1B[22;36m" /* cyan */ 51 | #define CL_SIL "\x1B[22;37m" /* gray */ 52 | 53 | #ifdef WIN32 54 | #define CL_GRY "\x1B[01;30m" /* dark gray */ 55 | #else 56 | #define CL_GRY "\x1B[90m" /* dark gray selectable in putty */ 57 | #endif 58 | #define CL_LRD "\x1B[01;31m" /* light red */ 59 | #define CL_LGR "\x1B[01;32m" /* light green */ 60 | #define CL_YL2 "\x1B[01;33m" /* yellow */ 61 | #define CL_LBL "\x1B[01;34m" /* light blue */ 62 | #define CL_LMA "\x1B[01;35m" /* light magenta */ 63 | #define CL_LCY "\x1B[01;36m" /* light cyan */ 64 | 65 | #define CL_WHT "\x1B[01;37m" /* white */ 66 | 67 | #define OPT_COLOR(color, text) (opt_colors ? (color text CL_N) : text) 68 | 69 | 70 | void applog_init(); 71 | void applog(int prio, const char *fmt, ...); 72 | void applog_notime(int prio, const char *fmt, ...); 73 | 74 | #endif /* __APPLOG_H__ */ 75 | -------------------------------------------------------------------------------- /unix/memory_unix.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __MEMORY_H__ 25 | #define __MEMORY_H__ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include "persistent_memory.h" 32 | #include "options.h" 33 | #include "utils/applog.h" 34 | 35 | 36 | char *persistent_memory; 37 | int persistent_memory_flags = 0; 38 | 39 | 40 | const char * persistent_memory_allocate() { 41 | const int size = MEMORY * (opt_n_threads + 1); 42 | persistent_memory_flags |= MEMORY_HUGEPAGES_AVAILABLE; 43 | 44 | persistent_memory = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0); 45 | 46 | if (persistent_memory == MAP_FAILED) { 47 | persistent_memory = _mm_malloc(size, 4096); 48 | return persistent_memory; 49 | } 50 | 51 | persistent_memory_flags |= MEMORY_HUGEPAGES_ENABLED; 52 | 53 | if (madvise(persistent_memory, size, MADV_RANDOM | MADV_WILLNEED) != 0) { 54 | applog(LOG_ERR, "madvise failed"); 55 | } 56 | 57 | if (mlock(persistent_memory, size) == 0) { 58 | persistent_memory_flags |= MEMORY_LOCK; 59 | } 60 | 61 | return persistent_memory; 62 | } 63 | 64 | 65 | void persistent_memory_free() { 66 | const int size = MEMORY * (opt_n_threads + 1); 67 | 68 | if (persistent_memory_flags & MEMORY_HUGEPAGES_ENABLED) { 69 | if (persistent_memory_flags & MEMORY_LOCK) { 70 | munlock(persistent_memory, size); 71 | } 72 | 73 | munmap(persistent_memory, size); 74 | } 75 | else { 76 | _mm_free(persistent_memory); 77 | } 78 | } 79 | 80 | 81 | #endif /* __MEMORY_H__ */ 82 | -------------------------------------------------------------------------------- /stratum.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __STRATUM_H__ 25 | #define __STRATUM_H__ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | 32 | /** 33 | * 128tx exploit. 34 | * 35 | * Max blob size is 84 (75 fixed + 9 variable), aligned to 96. 36 | * https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. 37 | */ 38 | struct work { 39 | uint32_t blob[21] __attribute__((aligned(16))); 40 | size_t blob_size __attribute__((aligned(16))); 41 | uint32_t target __attribute__((aligned(16))); 42 | uint32_t hash[8] __attribute__((aligned(16))); 43 | char job_id[64] __attribute__((aligned(16))); 44 | }; 45 | 46 | 47 | struct stratum_ctx { 48 | char *url; 49 | 50 | CURL *curl; 51 | char *curl_url; 52 | char curl_err_str[CURL_ERROR_SIZE]; 53 | curl_socket_t sock; 54 | size_t sockbuf_size; 55 | char *sockbuf; 56 | pthread_mutex_t sock_lock; 57 | bool ready; 58 | 59 | char id[64]; 60 | 61 | struct work work; 62 | struct work g_work; 63 | time_t g_work_time; 64 | pthread_mutex_t work_lock; 65 | }; 66 | 67 | 68 | bool stratum_send_line(struct stratum_ctx *sctx, char *s); 69 | bool stratum_socket_full(struct stratum_ctx *sctx, int timeout); 70 | char *stratum_recv_line(struct stratum_ctx *sctx); 71 | bool stratum_connect(struct stratum_ctx *sctx, const char *url); 72 | void stratum_disconnect(struct stratum_ctx *sctx); 73 | bool stratum_authorize(struct stratum_ctx *sctx, const char *user, const char *pass); 74 | bool stratum_handle_method(struct stratum_ctx *sctx, const char *s); 75 | bool stratum_handle_response(char *buf); 76 | bool stratum_keepalived(struct stratum_ctx *sctx); 77 | 78 | #endif /* __STRATUM_H__ */ 79 | -------------------------------------------------------------------------------- /test/cryptonight/cryptonight32.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | void cryptonight_av1_aesni32(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx); 8 | void cryptonight_av4_legacy(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx); 9 | 10 | 11 | char *bin2hex(const unsigned char *p, size_t len) 12 | { 13 | int i; 14 | char *s = malloc((len * 2) + 1); 15 | if (!s) 16 | return NULL; 17 | 18 | for (i = 0; i < len; i++) 19 | sprintf(s + (i * 2), "%02x", (unsigned int) p[i]); 20 | 21 | return s; 22 | } 23 | 24 | bool hex2bin(unsigned char *p, const char *hexstr, size_t len) 25 | { 26 | char hex_byte[3]; 27 | char *ep; 28 | 29 | hex_byte[2] = '\0'; 30 | 31 | while (*hexstr && len) { 32 | if (!hexstr[1]) { 33 | return false; 34 | } 35 | hex_byte[0] = hexstr[0]; 36 | hex_byte[1] = hexstr[1]; 37 | *p = (unsigned char) strtol(hex_byte, &ep, 16); 38 | if (*ep) { 39 | return false; 40 | } 41 | p++; 42 | hexstr += 2; 43 | len--; 44 | } 45 | 46 | return (len == 0 && *hexstr == 0) ? true : false; 47 | } 48 | 49 | 50 | void test_cryptonight_av1_32_should_CalcHash(void) { 51 | char hash[32]; 52 | char data[76]; 53 | 54 | hex2bin((unsigned char *) &data, "0305a0dbd6bf05cf16e503f3a66f78007cbf34144332ecbfc22ed95c8700383b309ace1923a0964b00000008ba939a62724c0d7581fce5761e9d8a0e6a1c3f924fdd8493d1115649c05eb601", 76); 55 | 56 | uint8_t *memory = (uint8_t *) malloc(MEMORY); 57 | struct cryptonight_ctx *ctx = (struct cryptonight_ctx*)malloc(sizeof(struct cryptonight_ctx)); 58 | 59 | cryptonight_av1_aesni32(&hash, data, memory, ctx); 60 | 61 | free(memory); 62 | free(ctx); 63 | 64 | TEST_ASSERT_EQUAL_STRING("1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd080100", bin2hex(hash, 32)); 65 | } 66 | 67 | 68 | void test_cryptonight_av4_should_CalcHash(void) 69 | { 70 | char hash[32]; 71 | char data[76]; 72 | 73 | hex2bin((unsigned char *) &data, "0305a0dbd6bf05cf16e503f3a66f78007cbf34144332ecbfc22ed95c8700383b309ace1923a0964b00000008ba939a62724c0d7581fce5761e9d8a0e6a1c3f924fdd8493d1115649c05eb601", 76); 74 | 75 | uint8_t *memory = (uint8_t *) malloc(MEMORY); 76 | struct cryptonight_ctx *ctx = (struct cryptonight_ctx*)malloc(sizeof(struct cryptonight_ctx)); 77 | 78 | cryptonight_av4_legacy(&hash, data, memory, ctx); 79 | 80 | free(memory); 81 | free(ctx); 82 | 83 | TEST_ASSERT_EQUAL_STRING("1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd080100", bin2hex(hash, 32)); 84 | } 85 | 86 | 87 | int main(void) 88 | { 89 | UNITY_BEGIN(); 90 | 91 | RUN_TEST(test_cryptonight_av1_32_should_CalcHash); 92 | RUN_TEST(test_cryptonight_av4_should_CalcHash); 93 | 94 | return UNITY_END(); 95 | } 96 | -------------------------------------------------------------------------------- /algo/cryptonight/cryptonight_av1_aesni.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2017 fireice-uk 8 | * Copyright 2016-2017 XMRig 9 | * 10 | * 11 | * This program is free software: you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License as published by 13 | * the Free Software Foundation, either version 3 of the License, or 14 | * (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License 22 | * along with this program. If not, see . 23 | */ 24 | 25 | #include 26 | #include 27 | 28 | #include "cryptonight.h" 29 | #include "cryptonight_aesni.h" 30 | #include "crypto/c_keccak.h" 31 | 32 | 33 | void cryptonight_av1_aesni(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx) 34 | { 35 | keccak((const uint8_t *) input, size, ctx->state0, 200); 36 | 37 | cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); 38 | 39 | const uint8_t* l0 = ctx->memory; 40 | uint64_t* h0 = (uint64_t*) ctx->state0; 41 | 42 | uint64_t al0 = h0[0] ^ h0[4]; 43 | uint64_t ah0 = h0[1] ^ h0[5]; 44 | __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); 45 | 46 | uint64_t idx0 = h0[0] ^ h0[4]; 47 | 48 | for (size_t i = 0; __builtin_expect(i < 0x40000, 1); i++) { 49 | __m128i cx; 50 | cx = _mm_load_si128((__m128i *) &l0[idx0 & 0xFFFF0]); 51 | cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); 52 | 53 | _mm_store_si128((__m128i *) &l0[idx0 & 0xFFFF0], _mm_xor_si128(bx0, cx)); 54 | idx0 = EXTRACT64(cx); 55 | bx0 = cx; 56 | 57 | uint64_t hi, lo, cl, ch; 58 | cl = ((uint64_t*) &l0[idx0 & 0xFFFF0])[0]; 59 | ch = ((uint64_t*) &l0[idx0 & 0xFFFF0])[1]; 60 | lo = _umul128(idx0, cl, &hi); 61 | 62 | al0 += hi; 63 | ah0 += lo; 64 | 65 | ((uint64_t*)&l0[idx0 & 0xFFFF0])[0] = al0; 66 | ((uint64_t*)&l0[idx0 & 0xFFFF0])[1] = ah0; 67 | 68 | ah0 ^= ch; 69 | al0 ^= cl; 70 | idx0 = al0; 71 | } 72 | 73 | cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state0); 74 | 75 | keccakf(h0, 24); 76 | extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); 77 | } 78 | -------------------------------------------------------------------------------- /algo/cryptonight/cryptonight_av3_softaes.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2017 fireice-uk 8 | * Copyright 2016-2017 XMRig 9 | * 10 | * 11 | * This program is free software: you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License as published by 13 | * the Free Software Foundation, either version 3 of the License, or 14 | * (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License 22 | * along with this program. If not, see . 23 | */ 24 | 25 | #include 26 | #include 27 | 28 | #include "cryptonight.h" 29 | #include "cryptonight_softaes.h" 30 | #include "crypto/c_keccak.h" 31 | 32 | 33 | void cryptonight_av3_softaes(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx) 34 | { 35 | keccak((const uint8_t *) input, size, ctx->state0, 200); 36 | 37 | cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); 38 | 39 | const uint8_t* l0 = ctx->memory; 40 | uint64_t* h0 = (uint64_t*) ctx->state0; 41 | 42 | uint64_t al0 = h0[0] ^ h0[4]; 43 | uint64_t ah0 = h0[1] ^ h0[5]; 44 | __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); 45 | 46 | uint64_t idx0 = h0[0] ^ h0[4]; 47 | 48 | for (size_t i = 0; __builtin_expect(i < 0x40000, 1); i++) { 49 | __m128i cx; 50 | cx = _mm_load_si128((__m128i *)&l0[idx0 & 0xFFFF0]); 51 | cx = soft_aesenc(cx, _mm_set_epi64x(ah0, al0)); 52 | 53 | _mm_store_si128((__m128i *)&l0[idx0 & 0xFFFF0], _mm_xor_si128(bx0, cx)); 54 | idx0 = EXTRACT64(cx); 55 | bx0 = cx; 56 | 57 | uint64_t hi, lo, cl, ch; 58 | cl = ((uint64_t*)&l0[idx0 & 0xFFFF0])[0]; 59 | ch = ((uint64_t*)&l0[idx0 & 0xFFFF0])[1]; 60 | lo = _umul128(idx0, cl, &hi); 61 | 62 | al0 += hi; 63 | ah0 += lo; 64 | 65 | ((uint64_t*)&l0[idx0 & 0xFFFF0])[0] = al0; 66 | ((uint64_t*)&l0[idx0 & 0xFFFF0])[1] = ah0; 67 | 68 | ah0 ^= ch; 69 | al0 ^= cl; 70 | idx0 = al0; 71 | } 72 | 73 | cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state0); 74 | 75 | keccakf(h0, 24); 76 | extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); 77 | } 78 | -------------------------------------------------------------------------------- /cpu.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | #include "cpu.h" 28 | 29 | 30 | #define VENDOR_ID (0) 31 | #define PROCESSOR_INFO (1) 32 | #define CACHE_TLB_DESCRIPTOR (2) 33 | #define EXTENDED_FEATURES (7) 34 | #define PROCESSOR_BRAND_STRING_1 (0x80000002) 35 | #define PROCESSOR_BRAND_STRING_2 (0x80000003) 36 | #define PROCESSOR_BRAND_STRING_3 (0x80000004) 37 | 38 | #define EAX_Reg (0) 39 | #define EBX_Reg (1) 40 | #define ECX_Reg (2) 41 | #define EDX_Reg (3) 42 | 43 | 44 | static inline void cpuid(int level, int output[4]) { 45 | int a, b, c, d; 46 | __cpuid_count(level, 0, a, b, c, d); 47 | 48 | output[0] = a; 49 | output[1] = b; 50 | output[2] = c; 51 | output[3] = d; 52 | } 53 | 54 | 55 | static void cpu_brand_string(char* s) { 56 | int cpu_info[4] = { 0 }; 57 | cpuid(VENDOR_ID, cpu_info); 58 | 59 | if (cpu_info[EAX_Reg] >= 4) { 60 | for (int i = 0; i < 4; i++) { 61 | cpuid(0x80000002 + i, cpu_info); 62 | memcpy(s, cpu_info, sizeof(cpu_info)); 63 | s += 16; 64 | } 65 | } 66 | } 67 | 68 | 69 | static bool has_aes_ni() 70 | { 71 | int cpu_info[4] = { 0 }; 72 | cpuid(PROCESSOR_INFO, cpu_info); 73 | 74 | return cpu_info[ECX_Reg] & bit_AES; 75 | } 76 | 77 | 78 | static bool has_bmi2() { 79 | int cpu_info[4] = { 0 }; 80 | cpuid(EXTENDED_FEATURES, cpu_info); 81 | 82 | return cpu_info[EBX_Reg] & bit_BMI2; 83 | } 84 | 85 | 86 | void cpu_init_common() { 87 | cpu_brand_string(cpu_info.brand); 88 | 89 | # ifdef __x86_64__ 90 | cpu_info.flags |= CPU_FLAG_X86_64; 91 | # endif 92 | 93 | if (has_aes_ni()) { 94 | cpu_info.flags |= CPU_FLAG_AES; 95 | } 96 | 97 | if (has_bmi2()) { 98 | cpu_info.flags |= CPU_FLAG_BMI2; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /compat/jansson/strbuffer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2016 Petri Lehtinen 3 | * 4 | * Jansson is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #ifndef _GNU_SOURCE 9 | #define _GNU_SOURCE 10 | #endif 11 | 12 | #include 13 | #include 14 | #include "jansson_private.h" 15 | #include "strbuffer.h" 16 | 17 | #define STRBUFFER_MIN_SIZE 16 18 | #define STRBUFFER_FACTOR 2 19 | #define STRBUFFER_SIZE_MAX ((size_t)-1) 20 | 21 | int strbuffer_init(strbuffer_t *strbuff) 22 | { 23 | strbuff->size = STRBUFFER_MIN_SIZE; 24 | strbuff->length = 0; 25 | 26 | strbuff->value = jsonp_malloc(strbuff->size); 27 | if(!strbuff->value) 28 | return -1; 29 | 30 | /* initialize to empty */ 31 | strbuff->value[0] = '\0'; 32 | return 0; 33 | } 34 | 35 | void strbuffer_close(strbuffer_t *strbuff) 36 | { 37 | if(strbuff->value) 38 | jsonp_free(strbuff->value); 39 | 40 | strbuff->size = 0; 41 | strbuff->length = 0; 42 | strbuff->value = NULL; 43 | } 44 | 45 | void strbuffer_clear(strbuffer_t *strbuff) 46 | { 47 | strbuff->length = 0; 48 | strbuff->value[0] = '\0'; 49 | } 50 | 51 | const char *strbuffer_value(const strbuffer_t *strbuff) 52 | { 53 | return strbuff->value; 54 | } 55 | 56 | char *strbuffer_steal_value(strbuffer_t *strbuff) 57 | { 58 | char *result = strbuff->value; 59 | strbuff->value = NULL; 60 | return result; 61 | } 62 | 63 | int strbuffer_append_byte(strbuffer_t *strbuff, char byte) 64 | { 65 | return strbuffer_append_bytes(strbuff, &byte, 1); 66 | } 67 | 68 | int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size) 69 | { 70 | if(size >= strbuff->size - strbuff->length) 71 | { 72 | size_t new_size; 73 | char *new_value; 74 | 75 | /* avoid integer overflow */ 76 | if (strbuff->size > STRBUFFER_SIZE_MAX / STRBUFFER_FACTOR 77 | || size > STRBUFFER_SIZE_MAX - 1 78 | || strbuff->length > STRBUFFER_SIZE_MAX - 1 - size) 79 | return -1; 80 | 81 | new_size = max(strbuff->size * STRBUFFER_FACTOR, 82 | strbuff->length + size + 1); 83 | 84 | new_value = jsonp_malloc(new_size); 85 | if(!new_value) 86 | return -1; 87 | 88 | memcpy(new_value, strbuff->value, strbuff->length); 89 | 90 | jsonp_free(strbuff->value); 91 | strbuff->value = new_value; 92 | strbuff->size = new_size; 93 | } 94 | 95 | memcpy(strbuff->value + strbuff->length, data, size); 96 | strbuff->length += size; 97 | strbuff->value[strbuff->length] = '\0'; 98 | 99 | return 0; 100 | } 101 | 102 | char strbuffer_pop(strbuffer_t *strbuff) 103 | { 104 | if(strbuff->length > 0) { 105 | char c = strbuff->value[--strbuff->length]; 106 | strbuff->value[strbuff->length] = '\0'; 107 | return c; 108 | } 109 | else 110 | return '\0'; 111 | } 112 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(xmrig C) 3 | 4 | set(HEADERS 5 | compat.h 6 | algo/cryptonight/cryptonight.h 7 | algo/cryptonight/cryptonight_aesni.h 8 | elist.h 9 | xmrig.h 10 | version.h 11 | options.h 12 | cpu.h 13 | persistent_memory.h 14 | stratum.h 15 | stats.h 16 | util.h 17 | donate.h 18 | ) 19 | 20 | set(HEADERS_CRYPTO 21 | crypto/c_groestl.h 22 | crypto/c_blake256.h 23 | crypto/c_jh.h 24 | crypto/c_skein.h 25 | ) 26 | 27 | set(HEADERS_COMPAT 28 | compat/winansi.h 29 | ) 30 | 31 | set(HEADERS_UTILS 32 | utils/applog.h 33 | utils/threads.h 34 | utils/summary.h 35 | ) 36 | 37 | set(SOURCES 38 | xmrig.c 39 | algo/cryptonight/cryptonight.c 40 | algo/cryptonight/cryptonight_av1_aesni.c 41 | algo/cryptonight/cryptonight_av2_aesni_double.c 42 | algo/cryptonight/cryptonight_av3_softaes.c 43 | algo/cryptonight/cryptonight_av4_softaes_double.c 44 | util.c 45 | options.c 46 | cpu.c 47 | stratum.c 48 | stats.c 49 | memory.c 50 | ) 51 | 52 | set(SOURCES_CRYPTO 53 | crypto/c_keccak.c 54 | crypto/c_groestl.c 55 | crypto/c_blake256.c 56 | crypto/c_jh.c 57 | crypto/c_skein.c 58 | crypto/soft_aes.c 59 | ) 60 | 61 | set(SOURCES_UTILS 62 | utils/applog.c 63 | utils/summary.c 64 | ) 65 | 66 | if (WIN32) 67 | set(SOURCES_OS win/cpu_win.c win/memory_win.c win/xmrig_win.c win/app.rc compat/winansi.c) 68 | set(EXTRA_LIBS ws2_32) 69 | add_definitions(/D_WIN32_WINNT=0x600) 70 | else() 71 | set(SOURCES_OS unix/cpu_unix.c unix/memory_unix.c unix/xmrig_unix.c) 72 | set(EXTRA_LIBS pthread) 73 | endif() 74 | 75 | include_directories(.) 76 | add_definitions(/DUSE_NATIVE_THREADS) 77 | add_definitions(/D_GNU_SOURCE) 78 | 79 | if ("${CMAKE_BUILD_TYPE}" STREQUAL "") 80 | set(CMAKE_BUILD_TYPE Release) 81 | endif() 82 | 83 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wno-pointer-to-int-cast") 84 | set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") 85 | #set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -gdwarf-2") 86 | #set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fprofile-generate") 87 | #set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fprofile-use -fprofile-correction") 88 | 89 | if (WIN32) 90 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static") 91 | endif() 92 | 93 | include_directories(compat/jansson) 94 | add_subdirectory(compat/jansson) 95 | 96 | find_package(CURL REQUIRED) 97 | 98 | if (CURL_FOUND) 99 | include_directories(${CURL_INCLUDE_DIRS}) 100 | add_definitions(/DCURL_STATICLIB) 101 | link_directories(${CURL_LIBRARIES}) 102 | endif() 103 | 104 | if (CMAKE_SIZEOF_VOID_P EQUAL 8) 105 | add_executable(xmrig-aeon ${HEADERS} ${HEADERS_CRYPTO} ${SOURCES} ${SOURCES_CRYPTO} ${HEADERS_UTILS} ${SOURCES_UTILS} ${HEADERS_COMPAT} ${SOURCES_COMPAT} ${SOURCES_OS}) 106 | target_link_libraries(xmrig-aeon jansson curl ${EXTRA_LIBS}) 107 | else() 108 | add_executable(xmrig-aeon32 ${HEADERS} ${HEADERS_CRYPTO} ${SOURCES} ${SOURCES_CRYPTO} ${HEADERS_UTILS} ${SOURCES_UTILS} ${HEADERS_COMPAT} ${SOURCES_COMPAT} ${SOURCES_OS}) 109 | target_link_libraries(xmrig-aeon32 jansson curl ${EXTRA_LIBS}) 110 | endif() 111 | 112 | source_group("HEADERS" FILES ${HEADERS}) 113 | -------------------------------------------------------------------------------- /compat/jansson/jansson_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2016 Petri Lehtinen 3 | * 4 | * Jansson is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #ifndef JANSSON_PRIVATE_H 9 | #define JANSSON_PRIVATE_H 10 | 11 | #include 12 | #include "jansson.h" 13 | #include "hashtable.h" 14 | #include "strbuffer.h" 15 | 16 | #define container_of(ptr_, type_, member_) \ 17 | ((type_ *)((char *)ptr_ - offsetof(type_, member_))) 18 | 19 | /* On some platforms, max() may already be defined */ 20 | #ifndef max 21 | #define max(a, b) ((a) > (b) ? (a) : (b)) 22 | #endif 23 | 24 | /* va_copy is a C99 feature. In C89 implementations, it's sometimes 25 | available as __va_copy. If not, memcpy() should do the trick. */ 26 | #ifndef va_copy 27 | #ifdef __va_copy 28 | #define va_copy __va_copy 29 | #else 30 | #define va_copy(a, b) memcpy(&(a), &(b), sizeof(va_list)) 31 | #endif 32 | #endif 33 | 34 | typedef struct { 35 | json_t json; 36 | hashtable_t hashtable; 37 | int visited; 38 | } json_object_t; 39 | 40 | typedef struct { 41 | json_t json; 42 | size_t size; 43 | size_t entries; 44 | json_t **table; 45 | int visited; 46 | } json_array_t; 47 | 48 | typedef struct { 49 | json_t json; 50 | char *value; 51 | size_t length; 52 | } json_string_t; 53 | 54 | typedef struct { 55 | json_t json; 56 | double value; 57 | } json_real_t; 58 | 59 | typedef struct { 60 | json_t json; 61 | json_int_t value; 62 | } json_integer_t; 63 | 64 | #define json_to_object(json_) container_of(json_, json_object_t, json) 65 | #define json_to_array(json_) container_of(json_, json_array_t, json) 66 | #define json_to_string(json_) container_of(json_, json_string_t, json) 67 | #define json_to_real(json_) container_of(json_, json_real_t, json) 68 | #define json_to_integer(json_) container_of(json_, json_integer_t, json) 69 | 70 | /* Create a string by taking ownership of an existing buffer */ 71 | json_t *jsonp_stringn_nocheck_own(const char *value, size_t len); 72 | 73 | /* Error message formatting */ 74 | void jsonp_error_init(json_error_t *error, const char *source); 75 | void jsonp_error_set_source(json_error_t *error, const char *source); 76 | void jsonp_error_set(json_error_t *error, int line, int column, 77 | size_t position, const char *msg, ...); 78 | void jsonp_error_vset(json_error_t *error, int line, int column, 79 | size_t position, const char *msg, va_list ap); 80 | 81 | /* Locale independent string<->double conversions */ 82 | int jsonp_strtod(strbuffer_t *strbuffer, double *out); 83 | int jsonp_dtostr(char *buffer, size_t size, double value, int prec); 84 | 85 | /* Wrappers for custom memory functions */ 86 | void* jsonp_malloc(size_t size); 87 | void jsonp_free(void *ptr); 88 | char *jsonp_strndup(const char *str, size_t length); 89 | char *jsonp_strdup(const char *str); 90 | char *jsonp_strndup(const char *str, size_t len); 91 | 92 | 93 | /* Windows compatibility */ 94 | #if defined(_WIN32) || defined(WIN32) 95 | # if defined(_MSC_VER) /* MS compiller */ 96 | # if (_MSC_VER < 1900) && !defined(snprintf) /* snprintf not defined yet & not introduced */ 97 | # define snprintf _snprintf 98 | # endif 99 | # if (_MSC_VER < 1500) && !defined(vsnprintf) /* vsnprintf not defined yet & not introduced */ 100 | # define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a) 101 | # endif 102 | # else /* Other Windows compiller, old definition */ 103 | # define snprintf _snprintf 104 | # define vsnprintf _vsnprintf 105 | # endif 106 | #endif 107 | 108 | #endif 109 | -------------------------------------------------------------------------------- /win/memory_win.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef __MEMORY_H__ 25 | #define __MEMORY_H__ 26 | 27 | #include 28 | #include "options.h" 29 | #include "persistent_memory.h" 30 | 31 | 32 | char *persistent_memory; 33 | int persistent_memory_flags = 0; 34 | 35 | 36 | /***************************************************************** 37 | SetLockPagesPrivilege: a function to obtain or 38 | release the privilege of locking physical pages. 39 | 40 | Inputs: 41 | 42 | HANDLE hProcess: Handle for the process for which the 43 | privilege is needed 44 | 45 | BOOL bEnable: Enable (TRUE) or disable? 46 | 47 | Return value: TRUE indicates success, FALSE failure. 48 | 49 | *****************************************************************/ 50 | /** 51 | * AWE Example: https://msdn.microsoft.com/en-us/library/windows/desktop/aa366531(v=vs.85).aspx 52 | * Creating a File Mapping Using Large Pages: https://msdn.microsoft.com/en-us/library/aa366543(VS.85).aspx 53 | */ 54 | static BOOL SetLockPagesPrivilege(HANDLE hProcess, BOOL bEnable) { 55 | struct { 56 | DWORD Count; 57 | LUID_AND_ATTRIBUTES Privilege[1]; 58 | } Info; 59 | 60 | HANDLE Token; 61 | 62 | if (OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES, &Token) != TRUE) { 63 | return FALSE; 64 | } 65 | 66 | Info.Count = 1; 67 | Info.Privilege[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0; 68 | 69 | if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &(Info.Privilege[0].Luid)) != TRUE) { 70 | return FALSE; 71 | } 72 | 73 | if (AdjustTokenPrivileges(Token, FALSE, (PTOKEN_PRIVILEGES) &Info, 0, NULL, NULL) != TRUE) { 74 | return FALSE; 75 | } 76 | 77 | if (GetLastError() != ERROR_SUCCESS) { 78 | return FALSE; 79 | } 80 | 81 | CloseHandle(Token); 82 | 83 | return TRUE; 84 | } 85 | 86 | 87 | const char * persistent_memory_allocate() { 88 | const int size = MEMORY * (opt_n_threads + 1); 89 | 90 | if (SetLockPagesPrivilege(GetCurrentProcess(), TRUE)) { 91 | persistent_memory_flags |= MEMORY_HUGEPAGES_AVAILABLE; 92 | } 93 | 94 | persistent_memory = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE); 95 | if (!persistent_memory) { 96 | persistent_memory = _mm_malloc(size, 16); 97 | } 98 | else { 99 | persistent_memory_flags |= MEMORY_HUGEPAGES_ENABLED; 100 | } 101 | 102 | return persistent_memory; 103 | } 104 | 105 | 106 | void persistent_memory_free() { 107 | if (persistent_memory_flags & MEMORY_HUGEPAGES_ENABLED) { 108 | VirtualFree(persistent_memory, 0, MEM_RELEASE); 109 | } 110 | else { 111 | _mm_free(persistent_memory); 112 | } 113 | } 114 | 115 | #endif /* __MEMORY_H__ */ 116 | -------------------------------------------------------------------------------- /compat/jansson/strconv.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #ifdef __MINGW32__ 7 | #undef __NO_ISOCEXT /* ensure stdlib.h will declare prototypes for mingw own 'strtod' replacement, called '__strtod' */ 8 | #endif 9 | #include "jansson_private.h" 10 | #include "strbuffer.h" 11 | 12 | /* need jansson_private_config.h to get the correct snprintf */ 13 | #ifdef HAVE_CONFIG_H 14 | #include 15 | #endif 16 | 17 | #ifdef __MINGW32__ 18 | #define strtod __strtod 19 | #endif 20 | 21 | #if JSON_HAVE_LOCALECONV 22 | #include 23 | 24 | /* 25 | - This code assumes that the decimal separator is exactly one 26 | character. 27 | 28 | - If setlocale() is called by another thread between the call to 29 | localeconv() and the call to sprintf() or strtod(), the result may 30 | be wrong. setlocale() is not thread-safe and should not be used 31 | this way. Multi-threaded programs should use uselocale() instead. 32 | */ 33 | 34 | static void to_locale(strbuffer_t *strbuffer) 35 | { 36 | const char *point; 37 | char *pos; 38 | 39 | point = localeconv()->decimal_point; 40 | if(*point == '.') { 41 | /* No conversion needed */ 42 | return; 43 | } 44 | 45 | pos = strchr(strbuffer->value, '.'); 46 | if(pos) 47 | *pos = *point; 48 | } 49 | 50 | static void from_locale(char *buffer) 51 | { 52 | const char *point; 53 | char *pos; 54 | 55 | point = localeconv()->decimal_point; 56 | if(*point == '.') { 57 | /* No conversion needed */ 58 | return; 59 | } 60 | 61 | pos = strchr(buffer, *point); 62 | if(pos) 63 | *pos = '.'; 64 | } 65 | #endif 66 | 67 | int jsonp_strtod(strbuffer_t *strbuffer, double *out) 68 | { 69 | double value; 70 | char *end; 71 | 72 | #if JSON_HAVE_LOCALECONV 73 | to_locale(strbuffer); 74 | #endif 75 | 76 | errno = 0; 77 | value = strtod(strbuffer->value, &end); 78 | assert(end == strbuffer->value + strbuffer->length); 79 | 80 | if((value == HUGE_VAL || value == -HUGE_VAL) && errno == ERANGE) { 81 | /* Overflow */ 82 | return -1; 83 | } 84 | 85 | *out = value; 86 | return 0; 87 | } 88 | 89 | int jsonp_dtostr(char *buffer, size_t size, double value, int precision) 90 | { 91 | int ret; 92 | char *start, *end; 93 | size_t length; 94 | 95 | if (precision == 0) 96 | precision = 17; 97 | 98 | ret = snprintf(buffer, size, "%.*g", precision, value); 99 | if(ret < 0) 100 | return -1; 101 | 102 | length = (size_t)ret; 103 | if(length >= size) 104 | return -1; 105 | 106 | #if JSON_HAVE_LOCALECONV 107 | from_locale(buffer); 108 | #endif 109 | 110 | /* Make sure there's a dot or 'e' in the output. Otherwise 111 | a real is converted to an integer when decoding */ 112 | if(strchr(buffer, '.') == NULL && 113 | strchr(buffer, 'e') == NULL) 114 | { 115 | if(length + 3 >= size) { 116 | /* No space to append ".0" */ 117 | return -1; 118 | } 119 | buffer[length] = '.'; 120 | buffer[length + 1] = '0'; 121 | buffer[length + 2] = '\0'; 122 | length += 2; 123 | } 124 | 125 | /* Remove leading '+' from positive exponent. Also remove leading 126 | zeros from exponents (added by some printf() implementations) */ 127 | start = strchr(buffer, 'e'); 128 | if(start) { 129 | start++; 130 | end = start + 1; 131 | 132 | if(*start == '-') 133 | start++; 134 | 135 | while(*end == '0') 136 | end++; 137 | 138 | if(end != start) { 139 | memmove(start, end, length - (size_t)(end - buffer)); 140 | length -= (size_t)(end - start); 141 | } 142 | } 143 | 144 | return (int)length; 145 | } 146 | -------------------------------------------------------------------------------- /crypto/c_keccak.c: -------------------------------------------------------------------------------- 1 | // keccak.c 2 | // 19-Nov-11 Markku-Juhani O. Saarinen 3 | // A baseline Keccak (3rd round) implementation. 4 | 5 | #include 6 | #include 7 | 8 | #define HASH_DATA_AREA 136 9 | #define KECCAK_ROUNDS 24 10 | 11 | #ifndef ROTL64 12 | #define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y)))) 13 | #endif 14 | 15 | const uint64_t keccakf_rndc[24] = 16 | { 17 | 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 18 | 0x8000000080008000, 0x000000000000808b, 0x0000000080000001, 19 | 0x8000000080008081, 0x8000000000008009, 0x000000000000008a, 20 | 0x0000000000000088, 0x0000000080008009, 0x000000008000000a, 21 | 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 22 | 0x8000000000008003, 0x8000000000008002, 0x8000000000000080, 23 | 0x000000000000800a, 0x800000008000000a, 0x8000000080008081, 24 | 0x8000000000008080, 0x0000000080000001, 0x8000000080008008 25 | }; 26 | 27 | const int keccakf_rotc[24] = 28 | { 29 | 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 30 | 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44 31 | }; 32 | 33 | const int keccakf_piln[24] = 34 | { 35 | 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 36 | 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1 37 | }; 38 | 39 | // update the state with given number of rounds 40 | 41 | void keccakf(uint64_t st[25], int rounds) 42 | { 43 | int i, j, round; 44 | uint64_t t, bc[5]; 45 | 46 | for (round = 0; round < rounds; ++round) { 47 | 48 | // Theta 49 | bc[0] = st[0] ^ st[5] ^ st[10] ^ st[15] ^ st[20]; 50 | bc[1] = st[1] ^ st[6] ^ st[11] ^ st[16] ^ st[21]; 51 | bc[2] = st[2] ^ st[7] ^ st[12] ^ st[17] ^ st[22]; 52 | bc[3] = st[3] ^ st[8] ^ st[13] ^ st[18] ^ st[23]; 53 | bc[4] = st[4] ^ st[9] ^ st[14] ^ st[19] ^ st[24]; 54 | 55 | for (i = 0; i < 5; ++i) { 56 | t = bc[(i + 4) % 5] ^ ROTL64(bc[(i + 1) % 5], 1); 57 | st[i ] ^= t; 58 | st[i + 5] ^= t; 59 | st[i + 10] ^= t; 60 | st[i + 15] ^= t; 61 | st[i + 20] ^= t; 62 | } 63 | 64 | // Rho Pi 65 | t = st[1]; 66 | for (i = 0; i < 24; ++i) { 67 | bc[0] = st[keccakf_piln[i]]; 68 | st[keccakf_piln[i]] = ROTL64(t, keccakf_rotc[i]); 69 | t = bc[0]; 70 | } 71 | 72 | // Chi 73 | for (j = 0; j < 25; j += 5) { 74 | bc[0] = st[j ]; 75 | bc[1] = st[j + 1]; 76 | bc[2] = st[j + 2]; 77 | bc[3] = st[j + 3]; 78 | bc[4] = st[j + 4]; 79 | st[j ] ^= (~bc[1]) & bc[2]; 80 | st[j + 1] ^= (~bc[2]) & bc[3]; 81 | st[j + 2] ^= (~bc[3]) & bc[4]; 82 | st[j + 3] ^= (~bc[4]) & bc[0]; 83 | st[j + 4] ^= (~bc[0]) & bc[1]; 84 | } 85 | 86 | // Iota 87 | st[0] ^= keccakf_rndc[round]; 88 | } 89 | } 90 | 91 | // compute a keccak hash (md) of given byte length from "in" 92 | typedef uint64_t state_t[25]; 93 | 94 | void keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen) 95 | { 96 | state_t st; 97 | uint8_t temp[144]; 98 | int i, rsiz, rsizw; 99 | 100 | rsiz = sizeof(state_t) == mdlen ? HASH_DATA_AREA : 200 - 2 * mdlen; 101 | rsizw = rsiz / 8; 102 | 103 | memset(st, 0, sizeof(st)); 104 | 105 | for ( ; inlen >= rsiz; inlen -= rsiz, in += rsiz) { 106 | for (i = 0; i < rsizw; i++) 107 | st[i] ^= ((uint64_t *) in)[i]; 108 | keccakf(st, KECCAK_ROUNDS); 109 | } 110 | 111 | // last block and padding 112 | memcpy(temp, in, inlen); 113 | temp[inlen++] = 1; 114 | memset(temp + inlen, 0, rsiz - inlen); 115 | temp[rsiz - 1] |= 0x80; 116 | 117 | for (i = 0; i < rsizw; i++) 118 | st[i] ^= ((uint64_t *) temp)[i]; 119 | 120 | keccakf(st, KECCAK_ROUNDS); 121 | 122 | memcpy(md, st, mdlen); 123 | } 124 | -------------------------------------------------------------------------------- /utils/summary.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #include "options.h" 25 | #include "applog.h" 26 | #include "version.h" 27 | #include "persistent_memory.h" 28 | #include "cpu.h" 29 | 30 | 31 | static void print_memory() { 32 | const char *t1 = (persistent_memory_flags & MEMORY_HUGEPAGES_AVAILABLE) ? OPT_COLOR(CL_LGR, "available") : OPT_COLOR(CL_LRD, "unavailable"); 33 | const char *t2 = (persistent_memory_flags & MEMORY_HUGEPAGES_ENABLED) ? OPT_COLOR(CL_LGR, "enabled") : OPT_COLOR(CL_LRD, "disabled"); 34 | 35 | if (opt_colors) { 36 | applog_notime(LOG_INFO, CL_LGR " * " CL_WHT "HUGE PAGES: %s, %s", t1, t2); 37 | } 38 | else { 39 | applog_notime(LOG_INFO, " * HUGE PAGES: %s, %s", t1, t2); 40 | } 41 | } 42 | 43 | 44 | static void print_cpu() { 45 | const char *t1 = (cpu_info.flags & CPU_FLAG_X86_64) ? OPT_COLOR(CL_LGR, "x86_64") : OPT_COLOR(CL_LRD, "-x86_64"); 46 | const char *t2 = (cpu_info.flags & CPU_FLAG_AES) ? OPT_COLOR(CL_LGR, "AES-NI") : OPT_COLOR(CL_LRD, "-AES-NI"); 47 | 48 | if (opt_colors) { 49 | applog_notime(LOG_INFO, CL_LGR " * " CL_WHT "CPU: %s", cpu_info.brand); 50 | applog_notime(LOG_INFO, CL_LGR " * " CL_WHT "CPU FEATURES: %s %s", t1, t2); 51 | } 52 | else { 53 | applog_notime(LOG_INFO, " * CPU: %s", cpu_info.brand); 54 | applog_notime(LOG_INFO, " * CPU FEATURES: %s %s", t1, t2); 55 | } 56 | } 57 | 58 | 59 | static void print_threads() { 60 | if (opt_colors) { 61 | applog_notime(LOG_INFO, CL_LGR " * " CL_WHT "THREADS: " CL_WHT "%d" CL_WHT ", av=%d, donate=%d%%", opt_n_threads, opt_algo_variant, opt_donate_level); 62 | } 63 | else { 64 | applog_notime(LOG_INFO, " * THREADS: %d, av=%d, donate=%d%%", opt_n_threads, opt_algo_variant, opt_donate_level); 65 | } 66 | } 67 | 68 | 69 | static void print_stratum() { 70 | if (opt_colors) { 71 | applog_notime(LOG_INFO, CL_LGR " * " CL_WHT "STRATUM URL: " CL_LCY "%s", opt_url); 72 | 73 | if (opt_backup_url) { 74 | applog_notime(LOG_INFO, CL_LGR " * " CL_WHT "FAILOVER URL: " CL_LCY "%s", opt_backup_url); 75 | } 76 | else { 77 | applog_notime(LOG_INFO, CL_LGR " * " CL_WHT "FAILOVER URL: " CL_LRD "none"); 78 | } 79 | } 80 | else { 81 | applog_notime(LOG_INFO, " * STRATUM URL: %s", opt_url); 82 | applog_notime(LOG_INFO, " * FAILOVER URL: %s", opt_backup_url ? opt_backup_url : "none"); 83 | } 84 | } 85 | 86 | 87 | void print_summary() { 88 | if (opt_colors) { 89 | applog_notime(LOG_INFO, CL_LGR " * " CL_WHT APP_NAME " " APP_VERSION " " CL_LCY APP_SITE); 90 | } 91 | else { 92 | applog_notime(LOG_INFO, " * " APP_NAME " " APP_VERSION " " APP_SITE); 93 | } 94 | 95 | print_memory(); 96 | print_cpu(); 97 | print_threads(); 98 | print_stratum(); 99 | } 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /utils/applog.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #include "xmrig.h" 25 | #include "applog.h" 26 | #include "threads.h" 27 | #include 28 | #include 29 | 30 | #ifdef WIN32 31 | # include "compat/winansi.h" 32 | #endif 33 | 34 | #include "options.h" 35 | 36 | 37 | MUTEX applog_mutex; 38 | 39 | 40 | void applog_init() 41 | { 42 | MUTEX_INIT(applog_mutex); 43 | } 44 | 45 | 46 | void applog(int prio, const char *fmt, ...) 47 | { 48 | if (opt_background) { 49 | return; 50 | } 51 | 52 | va_list ap; 53 | va_start(ap, fmt); 54 | 55 | struct tm tm; 56 | struct tm *tm_p; 57 | time_t now = time(NULL); 58 | 59 | MUTEX_LOCK(applog_mutex); 60 | tm_p = localtime(&now); 61 | memcpy(&tm, tm_p, sizeof(tm)); 62 | MUTEX_UNLOCK(applog_mutex); 63 | 64 | const char* color = ""; 65 | 66 | if (opt_colors) { 67 | switch (prio) { 68 | case LOG_ERR: color = CL_RED; break; 69 | case LOG_WARNING: color = CL_YLW; break; 70 | case LOG_NOTICE: color = CL_WHT; break; 71 | case LOG_INFO: color = ""; break; 72 | case LOG_DEBUG: color = CL_GRY; break; 73 | 74 | case LOG_BLUE: 75 | prio = LOG_NOTICE; 76 | color = CL_CYN; 77 | break; 78 | } 79 | } 80 | 81 | const int len = 64 + strlen(fmt) + 2; 82 | char *f = alloca(len); 83 | 84 | sprintf(f, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s\n", 85 | tm.tm_year + 1900, 86 | tm.tm_mon + 1, 87 | tm.tm_mday, 88 | tm.tm_hour, 89 | tm.tm_min, 90 | tm.tm_sec, 91 | color, 92 | fmt, 93 | opt_colors ? CL_N : "" 94 | ); 95 | 96 | MUTEX_LOCK(applog_mutex); 97 | vfprintf(stderr, f, ap); 98 | fflush(stderr); 99 | MUTEX_UNLOCK(applog_mutex); 100 | 101 | va_end(ap); 102 | } 103 | 104 | 105 | void applog_notime(int prio, const char *fmt, ...) 106 | { 107 | if (opt_background) { 108 | return; 109 | } 110 | 111 | va_list ap; 112 | va_start(ap, fmt); 113 | 114 | const char* color = ""; 115 | 116 | if (opt_colors) { 117 | switch (prio) { 118 | case LOG_ERR: color = CL_RED; break; 119 | case LOG_WARNING: color = CL_YLW; break; 120 | case LOG_NOTICE: color = CL_WHT; break; 121 | case LOG_INFO: color = ""; break; 122 | case LOG_DEBUG: color = CL_GRY; break; 123 | 124 | case LOG_BLUE: 125 | prio = LOG_NOTICE; 126 | color = CL_CYN; 127 | break; 128 | } 129 | } 130 | 131 | const int len = 64 + strlen(fmt) + 2; 132 | char *f = alloca(len); 133 | 134 | sprintf(f, "%s%s%s\n", 135 | color, 136 | fmt, 137 | opt_colors ? CL_N : "" 138 | ); 139 | 140 | MUTEX_LOCK(applog_mutex); 141 | vfprintf(stderr, f, ap); 142 | fflush(stderr); 143 | MUTEX_UNLOCK(applog_mutex); 144 | 145 | va_end(ap); 146 | } 147 | -------------------------------------------------------------------------------- /algo/cryptonight/cryptonight_av4_softaes_double.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2017 fireice-uk 8 | * Copyright 2016-2017 XMRig 9 | * 10 | * 11 | * This program is free software: you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License as published by 13 | * the Free Software Foundation, either version 3 of the License, or 14 | * (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License 22 | * along with this program. If not, see . 23 | */ 24 | 25 | #include 26 | #include 27 | 28 | #include "cryptonight.h" 29 | #include "cryptonight_softaes.h" 30 | #include "crypto/c_keccak.h" 31 | 32 | 33 | void cryptonight_av4_softaes_double(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx) 34 | { 35 | keccak((const uint8_t *) input, size, ctx->state0, 200); 36 | keccak((const uint8_t *) input + size, size, ctx->state1, 200); 37 | 38 | const uint8_t* l0 = ctx->memory; 39 | const uint8_t* l1 = ctx->memory + MEMORY_LITE; 40 | uint64_t* h0 = (uint64_t*) ctx->state0; 41 | uint64_t* h1 = (uint64_t*) ctx->state1; 42 | 43 | cn_explode_scratchpad((__m128i*) h0, (__m128i*) l0); 44 | cn_explode_scratchpad((__m128i*) h1, (__m128i*) l1); 45 | 46 | uint64_t al0 = h0[0] ^ h0[4]; 47 | uint64_t al1 = h1[0] ^ h1[4]; 48 | uint64_t ah0 = h0[1] ^ h0[5]; 49 | uint64_t ah1 = h1[1] ^ h1[5]; 50 | 51 | __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); 52 | __m128i bx1 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); 53 | 54 | uint64_t idx0 = h0[0] ^ h0[4]; 55 | uint64_t idx1 = h1[0] ^ h1[4]; 56 | 57 | for (size_t i = 0; __builtin_expect(i < 0x40000, 1); i++) { 58 | __m128i cx0 = _mm_load_si128((__m128i *) &l0[idx0 & 0xFFFF0]); 59 | __m128i cx1 = _mm_load_si128((__m128i *) &l1[idx1 & 0xFFFF0]); 60 | 61 | cx0 = soft_aesenc(cx0, _mm_set_epi64x(ah0, al0)); 62 | cx1 = soft_aesenc(cx1, _mm_set_epi64x(ah1, al1)); 63 | 64 | _mm_store_si128((__m128i *) &l0[idx0 & 0xFFFF0], _mm_xor_si128(bx0, cx0)); 65 | _mm_store_si128((__m128i *) &l1[idx1 & 0xFFFF0], _mm_xor_si128(bx1, cx1)); 66 | 67 | idx0 = EXTRACT64(cx0); 68 | idx1 = EXTRACT64(cx1); 69 | 70 | bx0 = cx0; 71 | bx1 = cx1; 72 | 73 | uint64_t hi, lo, cl, ch; 74 | cl = ((uint64_t*) &l0[idx0 & 0xFFFF0])[0]; 75 | ch = ((uint64_t*) &l0[idx0 & 0xFFFF0])[1]; 76 | lo = _umul128(idx0, cl, &hi); 77 | 78 | al0 += hi; 79 | ah0 += lo; 80 | 81 | ((uint64_t*) &l0[idx0 & 0xFFFF0])[0] = al0; 82 | ((uint64_t*) &l0[idx0 & 0xFFFF0])[1] = ah0; 83 | 84 | ah0 ^= ch; 85 | al0 ^= cl; 86 | idx0 = al0; 87 | 88 | cl = ((uint64_t*) &l1[idx1 & 0xFFFF0])[0]; 89 | ch = ((uint64_t*) &l1[idx1 & 0xFFFF0])[1]; 90 | lo = _umul128(idx1, cl, &hi); 91 | 92 | al1 += hi; 93 | ah1 += lo; 94 | 95 | ((uint64_t*) &l1[idx1 & 0xFFFF0])[0] = al1; 96 | ((uint64_t*) &l1[idx1 & 0xFFFF0])[1] = ah1; 97 | 98 | ah1 ^= ch; 99 | al1 ^= cl; 100 | idx1 = al1; 101 | } 102 | 103 | cn_implode_scratchpad((__m128i*) l0, (__m128i*) h0); 104 | cn_implode_scratchpad((__m128i*) l1, (__m128i*) h1); 105 | 106 | keccakf(h0, 24); 107 | keccakf(h1, 24); 108 | 109 | extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); 110 | extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, (char*) output + 32); 111 | } 112 | -------------------------------------------------------------------------------- /algo/cryptonight/cryptonight_av2_aesni_double.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2017 fireice-uk 8 | * Copyright 2016-2017 XMRig 9 | * 10 | * 11 | * This program is free software: you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License as published by 13 | * the Free Software Foundation, either version 3 of the License, or 14 | * (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License 22 | * along with this program. If not, see . 23 | */ 24 | 25 | #include 26 | #include 27 | 28 | #include "cryptonight.h" 29 | #include "cryptonight_aesni.h" 30 | #include "crypto/c_keccak.h" 31 | 32 | 33 | void cryptonight_av2_aesni_double(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx) 34 | { 35 | keccak((const uint8_t *) input, size, ctx->state0, 200); 36 | keccak((const uint8_t *) input + size, size, ctx->state1, 200); 37 | 38 | const uint8_t* l0 = ctx->memory; 39 | const uint8_t* l1 = ctx->memory + MEMORY_LITE; 40 | uint64_t* h0 = (uint64_t*) ctx->state0; 41 | uint64_t* h1 = (uint64_t*) ctx->state1; 42 | 43 | cn_explode_scratchpad((__m128i*) h0, (__m128i*) l0); 44 | cn_explode_scratchpad((__m128i*) h1, (__m128i*) l1); 45 | 46 | uint64_t al0 = h0[0] ^ h0[4]; 47 | uint64_t al1 = h1[0] ^ h1[4]; 48 | uint64_t ah0 = h0[1] ^ h0[5]; 49 | uint64_t ah1 = h1[1] ^ h1[5]; 50 | 51 | __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); 52 | __m128i bx1 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); 53 | 54 | uint64_t idx0 = h0[0] ^ h0[4]; 55 | uint64_t idx1 = h1[0] ^ h1[4]; 56 | 57 | for (size_t i = 0; __builtin_expect(i < 0x40000, 1); i++) { 58 | __m128i cx0 = _mm_load_si128((__m128i *) &l0[idx0 & 0xFFFF0]); 59 | __m128i cx1 = _mm_load_si128((__m128i *) &l1[idx1 & 0xFFFF0]); 60 | 61 | cx0 = _mm_aesenc_si128(cx0, _mm_set_epi64x(ah0, al0)); 62 | cx1 = _mm_aesenc_si128(cx1, _mm_set_epi64x(ah1, al1)); 63 | 64 | _mm_store_si128((__m128i *) &l0[idx0 & 0xFFFF0], _mm_xor_si128(bx0, cx0)); 65 | _mm_store_si128((__m128i *) &l1[idx1 & 0xFFFF0], _mm_xor_si128(bx1, cx1)); 66 | 67 | idx0 = EXTRACT64(cx0); 68 | idx1 = EXTRACT64(cx1); 69 | 70 | bx0 = cx0; 71 | bx1 = cx1; 72 | 73 | uint64_t hi, lo, cl, ch; 74 | cl = ((uint64_t*) &l0[idx0 & 0xFFFF0])[0]; 75 | ch = ((uint64_t*) &l0[idx0 & 0xFFFF0])[1]; 76 | lo = _umul128(idx0, cl, &hi); 77 | 78 | al0 += hi; 79 | ah0 += lo; 80 | 81 | ((uint64_t*) &l0[idx0 & 0xFFFF0])[0] = al0; 82 | ((uint64_t*) &l0[idx0 & 0xFFFF0])[1] = ah0; 83 | 84 | ah0 ^= ch; 85 | al0 ^= cl; 86 | idx0 = al0; 87 | 88 | cl = ((uint64_t*) &l1[idx1 & 0xFFFF0])[0]; 89 | ch = ((uint64_t*) &l1[idx1 & 0xFFFF0])[1]; 90 | lo = _umul128(idx1, cl, &hi); 91 | 92 | al1 += hi; 93 | ah1 += lo; 94 | 95 | ((uint64_t*) &l1[idx1 & 0xFFFF0])[0] = al1; 96 | ((uint64_t*) &l1[idx1 & 0xFFFF0])[1] = ah1; 97 | 98 | ah1 ^= ch; 99 | al1 ^= cl; 100 | idx1 = al1; 101 | } 102 | 103 | cn_implode_scratchpad((__m128i*) l0, (__m128i*) h0); 104 | cn_implode_scratchpad((__m128i*) l1, (__m128i*) h1); 105 | 106 | keccakf(h0, 24); 107 | keccakf(h1, 24); 108 | 109 | extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); 110 | extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, (char*) output + 32); 111 | } 112 | -------------------------------------------------------------------------------- /stats.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #include "stats.h" 29 | #include "options.h" 30 | #include "utils/applog.h" 31 | #include "persistent_memory.h" 32 | 33 | 34 | static unsigned long accepted_count = 0L; 35 | static unsigned long rejected_count = 0L; 36 | static double *thr_hashrates; 37 | static double *thr_times; 38 | static uint32_t target = 0; 39 | 40 | pthread_mutex_t stats_lock; 41 | 42 | 43 | static int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y); 44 | 45 | 46 | /** 47 | * @brief stats_init 48 | */ 49 | void stats_init() { 50 | pthread_mutex_init(&stats_lock, NULL); 51 | 52 | thr_hashrates = (double *) persistent_calloc(opt_n_threads, sizeof(double)); 53 | thr_times = (double *) persistent_calloc(opt_n_threads, sizeof(double)); 54 | } 55 | 56 | 57 | 58 | /** 59 | * @brief stats_set_target 60 | * @param target 61 | */ 62 | void stats_set_target(uint32_t new_target) 63 | { 64 | target = new_target; 65 | 66 | applog(LOG_DEBUG, "Pool set diff to %g", ((double) 0xffffffff) / target); 67 | } 68 | 69 | 70 | /** 71 | * @brief stats_share_result 72 | * @param result 73 | */ 74 | void stats_share_result(bool success) 75 | { 76 | double hashrate = 0.0; 77 | 78 | pthread_mutex_lock(&stats_lock); 79 | 80 | for (int i = 0; i < opt_n_threads; i++) { 81 | if (thr_times[i] > 0) { 82 | hashrate += thr_hashrates[i] / thr_times[i]; 83 | } 84 | } 85 | 86 | success ? accepted_count++ : rejected_count++; 87 | pthread_mutex_unlock(&stats_lock); 88 | 89 | applog(LOG_INFO, "accepted: %lu/%lu (%.2f%%), %.2f H/s at diff %g", 90 | accepted_count, accepted_count + rejected_count, 91 | 100. * accepted_count / (accepted_count + rejected_count), hashrate, 92 | (((double) 0xffffffff) / target)); 93 | } 94 | 95 | 96 | void stats_add_hashes(int thr_id, struct timeval *tv_start, unsigned long hashes_done) 97 | { 98 | struct timeval tv_end, diff; 99 | 100 | /* record scanhash elapsed time */ 101 | gettimeofday(&tv_end, NULL); 102 | timeval_subtract(&diff, &tv_end, tv_start); 103 | 104 | if (diff.tv_usec || diff.tv_sec) { 105 | pthread_mutex_lock(&stats_lock); 106 | thr_hashrates[thr_id] = hashes_done; 107 | thr_times[thr_id] = (diff.tv_sec + 1e-6 * diff.tv_usec); 108 | pthread_mutex_unlock(&stats_lock); 109 | } 110 | } 111 | 112 | 113 | /* Subtract the `struct timeval' values X and Y, 114 | storing the result in RESULT. 115 | Return 1 if the difference is negative, otherwise 0. */ 116 | static int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y) 117 | { 118 | /* Perform the carry for the later subtraction by updating Y. */ 119 | if (x->tv_usec < y->tv_usec) { 120 | int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; 121 | y->tv_usec -= 1000000 * nsec; 122 | y->tv_sec += nsec; 123 | } 124 | if (x->tv_usec - y->tv_usec > 1000000) { 125 | int nsec = (x->tv_usec - y->tv_usec) / 1000000; 126 | y->tv_usec += 1000000 * nsec; 127 | y->tv_sec -= nsec; 128 | } 129 | 130 | /* Compute the time remaining to wait. 131 | * `tv_usec' is certainly positive. */ 132 | result->tv_sec = x->tv_sec - y->tv_sec; 133 | result->tv_usec = x->tv_usec - y->tv_usec; 134 | 135 | /* Return 1 if result is negative. */ 136 | return x->tv_sec < y->tv_sec; 137 | } 138 | -------------------------------------------------------------------------------- /compat/jansson/utf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2016 Petri Lehtinen 3 | * 4 | * Jansson is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #include 9 | #include "utf.h" 10 | 11 | int utf8_encode(int32_t codepoint, char *buffer, size_t *size) 12 | { 13 | if(codepoint < 0) 14 | return -1; 15 | else if(codepoint < 0x80) 16 | { 17 | buffer[0] = (char)codepoint; 18 | *size = 1; 19 | } 20 | else if(codepoint < 0x800) 21 | { 22 | buffer[0] = 0xC0 + ((codepoint & 0x7C0) >> 6); 23 | buffer[1] = 0x80 + ((codepoint & 0x03F)); 24 | *size = 2; 25 | } 26 | else if(codepoint < 0x10000) 27 | { 28 | buffer[0] = 0xE0 + ((codepoint & 0xF000) >> 12); 29 | buffer[1] = 0x80 + ((codepoint & 0x0FC0) >> 6); 30 | buffer[2] = 0x80 + ((codepoint & 0x003F)); 31 | *size = 3; 32 | } 33 | else if(codepoint <= 0x10FFFF) 34 | { 35 | buffer[0] = 0xF0 + ((codepoint & 0x1C0000) >> 18); 36 | buffer[1] = 0x80 + ((codepoint & 0x03F000) >> 12); 37 | buffer[2] = 0x80 + ((codepoint & 0x000FC0) >> 6); 38 | buffer[3] = 0x80 + ((codepoint & 0x00003F)); 39 | *size = 4; 40 | } 41 | else 42 | return -1; 43 | 44 | return 0; 45 | } 46 | 47 | size_t utf8_check_first(char byte) 48 | { 49 | unsigned char u = (unsigned char)byte; 50 | 51 | if(u < 0x80) 52 | return 1; 53 | 54 | if(0x80 <= u && u <= 0xBF) { 55 | /* second, third or fourth byte of a multi-byte 56 | sequence, i.e. a "continuation byte" */ 57 | return 0; 58 | } 59 | else if(u == 0xC0 || u == 0xC1) { 60 | /* overlong encoding of an ASCII byte */ 61 | return 0; 62 | } 63 | else if(0xC2 <= u && u <= 0xDF) { 64 | /* 2-byte sequence */ 65 | return 2; 66 | } 67 | 68 | else if(0xE0 <= u && u <= 0xEF) { 69 | /* 3-byte sequence */ 70 | return 3; 71 | } 72 | else if(0xF0 <= u && u <= 0xF4) { 73 | /* 4-byte sequence */ 74 | return 4; 75 | } 76 | else { /* u >= 0xF5 */ 77 | /* Restricted (start of 4-, 5- or 6-byte sequence) or invalid 78 | UTF-8 */ 79 | return 0; 80 | } 81 | } 82 | 83 | size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint) 84 | { 85 | size_t i; 86 | int32_t value = 0; 87 | unsigned char u = (unsigned char)buffer[0]; 88 | 89 | if(size == 2) 90 | { 91 | value = u & 0x1F; 92 | } 93 | else if(size == 3) 94 | { 95 | value = u & 0xF; 96 | } 97 | else if(size == 4) 98 | { 99 | value = u & 0x7; 100 | } 101 | else 102 | return 0; 103 | 104 | for(i = 1; i < size; i++) 105 | { 106 | u = (unsigned char)buffer[i]; 107 | 108 | if(u < 0x80 || u > 0xBF) { 109 | /* not a continuation byte */ 110 | return 0; 111 | } 112 | 113 | value = (value << 6) + (u & 0x3F); 114 | } 115 | 116 | if(value > 0x10FFFF) { 117 | /* not in Unicode range */ 118 | return 0; 119 | } 120 | 121 | else if(0xD800 <= value && value <= 0xDFFF) { 122 | /* invalid code point (UTF-16 surrogate halves) */ 123 | return 0; 124 | } 125 | 126 | else if((size == 2 && value < 0x80) || 127 | (size == 3 && value < 0x800) || 128 | (size == 4 && value < 0x10000)) { 129 | /* overlong encoding */ 130 | return 0; 131 | } 132 | 133 | if(codepoint) 134 | *codepoint = value; 135 | 136 | return 1; 137 | } 138 | 139 | const char *utf8_iterate(const char *buffer, size_t bufsize, int32_t *codepoint) 140 | { 141 | size_t count; 142 | int32_t value; 143 | 144 | if(!bufsize) 145 | return buffer; 146 | 147 | count = utf8_check_first(buffer[0]); 148 | if(count <= 0) 149 | return NULL; 150 | 151 | if(count == 1) 152 | value = (unsigned char)buffer[0]; 153 | else 154 | { 155 | if(count > bufsize || !utf8_check_full(buffer, count, &value)) 156 | return NULL; 157 | } 158 | 159 | if(codepoint) 160 | *codepoint = value; 161 | 162 | return buffer + count; 163 | } 164 | 165 | int utf8_check_string(const char *string, size_t length) 166 | { 167 | size_t i; 168 | 169 | for(i = 0; i < length; i++) 170 | { 171 | size_t count = utf8_check_first(string[i]); 172 | if(count == 0) 173 | return 0; 174 | else if(count > 1) 175 | { 176 | if(count > length - i) 177 | return 0; 178 | 179 | if(!utf8_check_full(&string[i], count, NULL)) 180 | return 0; 181 | 182 | i += count - 1; 183 | } 184 | } 185 | 186 | return 1; 187 | } 188 | -------------------------------------------------------------------------------- /compat/jansson/hashtable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2016 Petri Lehtinen 3 | * 4 | * This library is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #ifndef HASHTABLE_H 9 | #define HASHTABLE_H 10 | 11 | #include 12 | #include "jansson.h" 13 | 14 | struct hashtable_list { 15 | struct hashtable_list *prev; 16 | struct hashtable_list *next; 17 | }; 18 | 19 | /* "pair" may be a bit confusing a name, but think of it as a 20 | key-value pair. In this case, it just encodes some extra data, 21 | too */ 22 | struct hashtable_pair { 23 | struct hashtable_list list; 24 | struct hashtable_list ordered_list; 25 | size_t hash; 26 | json_t *value; 27 | char key[1]; 28 | }; 29 | 30 | struct hashtable_bucket { 31 | struct hashtable_list *first; 32 | struct hashtable_list *last; 33 | }; 34 | 35 | typedef struct hashtable { 36 | size_t size; 37 | struct hashtable_bucket *buckets; 38 | size_t order; /* hashtable has pow(2, order) buckets */ 39 | struct hashtable_list list; 40 | struct hashtable_list ordered_list; 41 | } hashtable_t; 42 | 43 | 44 | #define hashtable_key_to_iter(key_) \ 45 | (&(container_of(key_, struct hashtable_pair, key)->ordered_list)) 46 | 47 | 48 | /** 49 | * hashtable_init - Initialize a hashtable object 50 | * 51 | * @hashtable: The (statically allocated) hashtable object 52 | * 53 | * Initializes a statically allocated hashtable object. The object 54 | * should be cleared with hashtable_close when it's no longer used. 55 | * 56 | * Returns 0 on success, -1 on error (out of memory). 57 | */ 58 | int hashtable_init(hashtable_t *hashtable); 59 | 60 | /** 61 | * hashtable_close - Release all resources used by a hashtable object 62 | * 63 | * @hashtable: The hashtable 64 | * 65 | * Destroys a statically allocated hashtable object. 66 | */ 67 | void hashtable_close(hashtable_t *hashtable); 68 | 69 | /** 70 | * hashtable_set - Add/modify value in hashtable 71 | * 72 | * @hashtable: The hashtable object 73 | * @key: The key 74 | * @serial: For addition order of keys 75 | * @value: The value 76 | * 77 | * If a value with the given key already exists, its value is replaced 78 | * with the new value. Value is "stealed" in the sense that hashtable 79 | * doesn't increment its refcount but decreases the refcount when the 80 | * value is no longer needed. 81 | * 82 | * Returns 0 on success, -1 on failure (out of memory). 83 | */ 84 | int hashtable_set(hashtable_t *hashtable, const char *key, json_t *value); 85 | 86 | /** 87 | * hashtable_get - Get a value associated with a key 88 | * 89 | * @hashtable: The hashtable object 90 | * @key: The key 91 | * 92 | * Returns value if it is found, or NULL otherwise. 93 | */ 94 | void *hashtable_get(hashtable_t *hashtable, const char *key); 95 | 96 | /** 97 | * hashtable_del - Remove a value from the hashtable 98 | * 99 | * @hashtable: The hashtable object 100 | * @key: The key 101 | * 102 | * Returns 0 on success, or -1 if the key was not found. 103 | */ 104 | int hashtable_del(hashtable_t *hashtable, const char *key); 105 | 106 | /** 107 | * hashtable_clear - Clear hashtable 108 | * 109 | * @hashtable: The hashtable object 110 | * 111 | * Removes all items from the hashtable. 112 | */ 113 | void hashtable_clear(hashtable_t *hashtable); 114 | 115 | /** 116 | * hashtable_iter - Iterate over hashtable 117 | * 118 | * @hashtable: The hashtable object 119 | * 120 | * Returns an opaque iterator to the first element in the hashtable. 121 | * The iterator should be passed to hashtable_iter_* functions. 122 | * The hashtable items are not iterated over in any particular order. 123 | * 124 | * There's no need to free the iterator in any way. The iterator is 125 | * valid as long as the item that is referenced by the iterator is not 126 | * deleted. Other values may be added or deleted. In particular, 127 | * hashtable_iter_next() may be called on an iterator, and after that 128 | * the key/value pair pointed by the old iterator may be deleted. 129 | */ 130 | void *hashtable_iter(hashtable_t *hashtable); 131 | 132 | /** 133 | * hashtable_iter_at - Return an iterator at a specific key 134 | * 135 | * @hashtable: The hashtable object 136 | * @key: The key that the iterator should point to 137 | * 138 | * Like hashtable_iter() but returns an iterator pointing to a 139 | * specific key. 140 | */ 141 | void *hashtable_iter_at(hashtable_t *hashtable, const char *key); 142 | 143 | /** 144 | * hashtable_iter_next - Advance an iterator 145 | * 146 | * @hashtable: The hashtable object 147 | * @iter: The iterator 148 | * 149 | * Returns a new iterator pointing to the next element in the 150 | * hashtable or NULL if the whole hastable has been iterated over. 151 | */ 152 | void *hashtable_iter_next(hashtable_t *hashtable, void *iter); 153 | 154 | /** 155 | * hashtable_iter_key - Retrieve the key pointed by an iterator 156 | * 157 | * @iter: The iterator 158 | */ 159 | void *hashtable_iter_key(void *iter); 160 | 161 | /** 162 | * hashtable_iter_value - Retrieve the value pointed by an iterator 163 | * 164 | * @iter: The iterator 165 | */ 166 | void *hashtable_iter_value(void *iter); 167 | 168 | /** 169 | * hashtable_iter_set - Set the value pointed by an iterator 170 | * 171 | * @iter: The iterator 172 | * @value: The value to set 173 | */ 174 | void hashtable_iter_set(void *iter, json_t *value); 175 | 176 | #endif 177 | -------------------------------------------------------------------------------- /compat/jansson/jansson_private_config.h: -------------------------------------------------------------------------------- 1 | /* jansson_private_config.h. Generated from jansson_private_config.h.in by configure. */ 2 | /* jansson_private_config.h.in. Generated from configure.ac by autoheader. */ 3 | 4 | /* Define to 1 if gcc's __atomic builtins are available */ 5 | #define HAVE_ATOMIC_BUILTINS 1 6 | 7 | /* Define to 1 if you have the `close' function. */ 8 | #define HAVE_CLOSE 1 9 | 10 | /* Define to 1 if you have the header file. */ 11 | /* #undef HAVE_DLFCN_H */ 12 | 13 | /* Define to 1 if you have the header file. */ 14 | /* #undef HAVE_ENDIAN_H */ 15 | 16 | /* Define to 1 if you have the header file. */ 17 | #define HAVE_FCNTL_H 1 18 | 19 | /* Define to 1 if you have the `getpid' function. */ 20 | #define HAVE_GETPID 1 21 | 22 | /* Define to 1 if you have the `gettimeofday' function. */ 23 | #define HAVE_GETTIMEOFDAY 1 24 | 25 | /* Define to 1 if you have the header file. */ 26 | #define HAVE_INTTYPES_H 1 27 | 28 | /* Define to 1 if you have the `localeconv' function. */ 29 | #define HAVE_LOCALECONV 1 30 | 31 | /* Define to 1 if you have the header file. */ 32 | #define HAVE_LOCALE_H 1 33 | 34 | /* Define to 1 if the system has the type 'long long int'. */ 35 | #define HAVE_LONG_LONG_INT 1 36 | 37 | /* Define to 1 if you have the header file. */ 38 | #define HAVE_MEMORY_H 1 39 | 40 | /* Define to 1 if you have the `open' function. */ 41 | #define HAVE_OPEN 1 42 | 43 | /* Define to 1 if you have the `read' function. */ 44 | #define HAVE_READ 1 45 | 46 | /* Define to 1 if you have the header file. */ 47 | #define HAVE_SCHED_H 1 48 | 49 | /* Define to 1 if you have the `sched_yield' function. */ 50 | #define HAVE_SCHED_YIELD 1 51 | 52 | /* Define to 1 if you have the header file. */ 53 | #define HAVE_STDINT_H 1 54 | 55 | /* Define to 1 if you have the header file. */ 56 | #define HAVE_STDLIB_H 1 57 | 58 | /* Define to 1 if you have the header file. */ 59 | #define HAVE_STRINGS_H 1 60 | 61 | /* Define to 1 if you have the header file. */ 62 | #define HAVE_STRING_H 1 63 | 64 | /* Define to 1 if you have the `strtoll' function. */ 65 | #define HAVE_STRTOLL 1 66 | 67 | /* Define to 1 if gcc's __sync builtins are available */ 68 | #define HAVE_SYNC_BUILTINS 1 69 | 70 | /* Define to 1 if you have the header file. */ 71 | #define HAVE_SYS_PARAM_H 1 72 | 73 | /* Define to 1 if you have the header file. */ 74 | #define HAVE_SYS_STAT_H 1 75 | 76 | /* Define to 1 if you have the header file. */ 77 | #define HAVE_SYS_TIME_H 1 78 | 79 | /* Define to 1 if you have the header file. */ 80 | #define HAVE_SYS_TYPES_H 1 81 | 82 | /* Define to 1 if you have the header file. */ 83 | #define HAVE_UNISTD_H 1 84 | 85 | /* Define to 1 if the system has the type 'unsigned long long int'. */ 86 | #define HAVE_UNSIGNED_LONG_LONG_INT 1 87 | 88 | /* Number of buckets new object hashtables contain is 2 raised to this power. 89 | E.g. 3 -> 2^3 = 8. */ 90 | #define INITIAL_HASHTABLE_ORDER 3 91 | 92 | /* Define to the sub-directory where libtool stores uninstalled libraries. */ 93 | #define LT_OBJDIR ".libs/" 94 | 95 | /* Name of package */ 96 | #define PACKAGE "jansson" 97 | 98 | /* Define to the address where bug reports for this package should be sent. */ 99 | #define PACKAGE_BUGREPORT "petri@digip.org" 100 | 101 | /* Define to the full name of this package. */ 102 | #define PACKAGE_NAME "jansson" 103 | 104 | /* Define to the full name and version of this package. */ 105 | #define PACKAGE_STRING "jansson 2.9" 106 | 107 | /* Define to the one symbol short name of this package. */ 108 | #define PACKAGE_TARNAME "jansson" 109 | 110 | /* Define to the home page for this package. */ 111 | #define PACKAGE_URL "" 112 | 113 | /* Define to the version of this package. */ 114 | #define PACKAGE_VERSION "2.9" 115 | 116 | /* Define to 1 if you have the ANSI C header files. */ 117 | #define STDC_HEADERS 1 118 | 119 | /* Define to 1 if /dev/urandom should be used for seeding the hash function */ 120 | #define USE_URANDOM 1 121 | 122 | /* Define to 1 if CryptGenRandom should be used for seeding the hash function 123 | */ 124 | #define USE_WINDOWS_CRYPTOAPI 1 125 | 126 | /* Version number of package */ 127 | #define VERSION "2.9" 128 | 129 | /* Define for Solaris 2.5.1 so the uint32_t typedef from , 130 | , or is not used. If the typedef were allowed, the 131 | #define below would cause a syntax error. */ 132 | /* #undef _UINT32_T */ 133 | 134 | /* Define for Solaris 2.5.1 so the uint8_t typedef from , 135 | , or is not used. If the typedef were allowed, the 136 | #define below would cause a syntax error. */ 137 | /* #undef _UINT8_T */ 138 | 139 | /* Define to `__inline__' or `__inline' if that's what the C compiler 140 | calls it, or to nothing if 'inline' is not supported under any name. */ 141 | #ifndef __cplusplus 142 | /* #undef inline */ 143 | #endif 144 | 145 | /* Define to the type of a signed integer type of width exactly 32 bits if 146 | such a type exists and the standard includes do not define it. */ 147 | /* #undef int32_t */ 148 | 149 | /* Define to the type of an unsigned integer type of width exactly 16 bits if 150 | such a type exists and the standard includes do not define it. */ 151 | /* #undef uint16_t */ 152 | 153 | /* Define to the type of an unsigned integer type of width exactly 32 bits if 154 | such a type exists and the standard includes do not define it. */ 155 | /* #undef uint32_t */ 156 | 157 | /* Define to the type of an unsigned integer type of width exactly 8 bits if 158 | such a type exists and the standard includes do not define it. */ 159 | /* #undef uint8_t */ 160 | -------------------------------------------------------------------------------- /test/cryptonight/cryptonight.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | const static char input1[76] = { 8 | 0x03, 0x05, 0xA0, 0xDB, 0xD6, 0xBF, 0x05, 0xCF, 0x16, 0xE5, 0x03, 0xF3, 0xA6, 0x6F, 0x78, 0x00, 0x7C, 0xBF, 0x34, 9 | 0x14, 0x43, 0x32, 0xEC, 0xBF, 0xC2, 0x2E, 0xD9, 0x5C, 0x87, 0x00, 0x38, 0x3B, 0x30, 0x9A, 0xCE, 0x19, 0x23, 0xA0, 10 | 0x96, 0x4B, 0x00, 0x00, 0x00, 0x08, 0xBA, 0x93, 0x9A, 0x62, 0x72, 0x4C, 0x0D, 0x75, 0x81, 0xFC, 0xE5, 0x76, 0x1E, 11 | 0x9D, 0x8A, 0x0E, 0x6A, 0x1C, 0x3F, 0x92, 0x4F, 0xDD, 0x84, 0x93, 0xD1, 0x11, 0x56, 0x49, 0xC0, 0x5E, 0xB6, 0x01 12 | }; 13 | 14 | const static char input2[] = "This is a test"; 15 | const static char input3[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus pellentesque metus."; 16 | 17 | 18 | void cryptonight_av1_aesni(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); 19 | void cryptonight_av2_aesni_stak(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); 20 | void cryptonight_av3_aesni_bmi2(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); 21 | void cryptonight_av4_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); 22 | void cryptonight_av5_aesni_experimental(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); 23 | 24 | 25 | static char hash[32]; 26 | #define RESULT1 "1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd080100" 27 | #define RESULT2 "a084f01d1437a09c6985401b60d43554ae105802c5f5d8a9b3253649c0be6605" 28 | #define RESULT3 "0bbe54bd26caa92a1d436eec71cbef02560062fa689fe14d7efcf42566b411cf" 29 | 30 | 31 | static char *bin2hex(const unsigned char *p, size_t len) 32 | { 33 | char *s = malloc((len * 2) + 1); 34 | if (!s) { 35 | return NULL; 36 | } 37 | 38 | for (int i = 0; i < len; i++) { 39 | sprintf(s + (i * 2), "%02x", (unsigned int) p[i]); 40 | } 41 | 42 | return s; 43 | } 44 | 45 | 46 | static void * create_ctx() { 47 | struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) malloc(sizeof(struct cryptonight_ctx)); 48 | ctx->memory = (uint8_t *) malloc(MEMORY); 49 | 50 | return ctx; 51 | } 52 | 53 | 54 | static void free_ctx(struct cryptonight_ctx *ctx) { 55 | free(ctx->memory); 56 | free(ctx); 57 | } 58 | 59 | 60 | void test_cryptonight_av1_should_CalcHash(void) { 61 | struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(); 62 | 63 | cryptonight_av1_aesni(input1, sizeof(input1), &hash, ctx); 64 | TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); 65 | 66 | cryptonight_av1_aesni(input2, strlen(input2), &hash, ctx); 67 | TEST_ASSERT_EQUAL_STRING(RESULT2, bin2hex(hash, 32)); 68 | 69 | cryptonight_av1_aesni(input3, strlen(input3), &hash, ctx); 70 | TEST_ASSERT_EQUAL_STRING(RESULT3, bin2hex(hash, 32)); 71 | 72 | free_ctx(ctx); 73 | } 74 | 75 | 76 | void test_cryptonight_av2_should_CalcHash(void) 77 | { 78 | struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(); 79 | 80 | cryptonight_av2_aesni_stak(input1, sizeof(input1), &hash, ctx); 81 | TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); 82 | 83 | cryptonight_av2_aesni_stak(input2, strlen(input2), &hash, ctx); 84 | TEST_ASSERT_EQUAL_STRING(RESULT2, bin2hex(hash, 32)); 85 | 86 | cryptonight_av2_aesni_stak(input3, strlen(input3), &hash, ctx); 87 | TEST_ASSERT_EQUAL_STRING(RESULT3, bin2hex(hash, 32)); 88 | 89 | free_ctx(ctx); 90 | } 91 | 92 | 93 | void test_cryptonight_av3_should_CalcHash(void) 94 | { 95 | struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(); 96 | 97 | cryptonight_av3_aesni_bmi2(input1, sizeof(input1), &hash, ctx); 98 | TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); 99 | 100 | cryptonight_av3_aesni_bmi2(input2, strlen(input2), &hash, ctx); 101 | TEST_ASSERT_EQUAL_STRING(RESULT2, bin2hex(hash, 32)); 102 | 103 | cryptonight_av3_aesni_bmi2(input3, strlen(input3), &hash, ctx); 104 | TEST_ASSERT_EQUAL_STRING(RESULT3, bin2hex(hash, 32)); 105 | 106 | free_ctx(ctx); 107 | } 108 | 109 | 110 | void test_cryptonight_av4_should_CalcHash(void) 111 | { 112 | struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(); 113 | 114 | cryptonight_av4_softaes(input1, sizeof(input1), &hash, ctx); 115 | TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); 116 | 117 | cryptonight_av4_softaes(input2, strlen(input2), &hash, ctx); 118 | TEST_ASSERT_EQUAL_STRING(RESULT2, bin2hex(hash, 32)); 119 | 120 | cryptonight_av4_softaes(input3, strlen(input3), &hash, ctx); 121 | TEST_ASSERT_EQUAL_STRING(RESULT3, bin2hex(hash, 32)); 122 | 123 | free_ctx(ctx); 124 | } 125 | 126 | 127 | void test_cryptonight_av5_should_CalcHash(void) 128 | { 129 | struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(); 130 | 131 | cryptonight_av5_aesni_experimental(input1, sizeof(input1), &hash, ctx); 132 | TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); 133 | 134 | cryptonight_av5_aesni_experimental(input2, strlen(input2), &hash, ctx); 135 | TEST_ASSERT_EQUAL_STRING(RESULT2, bin2hex(hash, 32)); 136 | 137 | cryptonight_av5_aesni_experimental(input3, strlen(input3), &hash, ctx); 138 | TEST_ASSERT_EQUAL_STRING(RESULT3, bin2hex(hash, 32)); 139 | 140 | free_ctx(ctx); 141 | } 142 | 143 | 144 | int main(void) 145 | { 146 | UNITY_BEGIN(); 147 | 148 | RUN_TEST(test_cryptonight_av1_should_CalcHash); 149 | RUN_TEST(test_cryptonight_av2_should_CalcHash); 150 | RUN_TEST(test_cryptonight_av3_should_CalcHash); 151 | RUN_TEST(test_cryptonight_av4_should_CalcHash); 152 | RUN_TEST(test_cryptonight_av5_should_CalcHash); 153 | 154 | return UNITY_END(); 155 | } 156 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # XMRig for AEON 2 | :exclamation: :exclamation: :exclamation: **This repository now deprecated, please use [XMRig](https://github.com/xmrig/xmrig) with option `-a cryptonight-lite`.** :exclamation: :exclamation: :exclamation: 3 | 4 | XMRig is high performance [AEON](http://www.aeon.cash/) CPU miner, with the official full Windows support. 5 | Based on cpuminer-multi with heavy optimizations/rewrites and removing a lot of legacy code. 6 | 7 | #### Table of contents 8 | * [Features](#features) 9 | * [Download](#download) 10 | * [Usage](#usage) 11 | * [Algorithm variations](#algorithm-variations) 12 | * [Build](#build) 13 | * [Common Issues](#common-issues) 14 | * [Other information](#other-information) 15 | * [Donations](#Donations) 16 | 17 | ## Features 18 | * High performance (1100+ H/s on i7 6700). 19 | * Official Windows support. 20 | * Small Windows executable, only 430 KB without dependencies. 21 | * Support for backup (failover) mining server. 22 | * keepalived support. 23 | * Command line options compatible with cpuminer. 24 | * It's open source software. 25 | 26 | ## Download 27 | * Binary releases: https://github.com/xmrig/xmrig-aeon/releases 28 | * Git tree: https://github.com/xmrig/xmrig-aeon.git 29 | * Clone with `git clone https://github.com/xmrig/xmrig-aeon.git` 30 | 31 | ## Usage 32 | ### Basic example 33 | ``` 34 | xmrig.exe -o mine.aeon-pool.com:3333 -u YOUR_WALLET -p x 35 | ``` 36 | 37 | ### Options 38 | ``` 39 | -o, --url=URL URL of mining server 40 | -b, --backup-url=URL URL of backup mining server 41 | -O, --userpass=U:P username:password pair for mining server 42 | -u, --user=USERNAME username for mining server 43 | -p, --pass=PASSWORD password for mining server 44 | -t, --threads=N number of miner threads 45 | -v, --av=N algorithm variation, 0 auto select 46 | -k, --keepalive send keepalived for prevent timeout (need pool support) 47 | -r, --retries=N number of times to retry before switch to backup server (default: 5) 48 | -R, --retry-pause=N time to pause between retries (default: 5) 49 | --cpu-affinity set process affinity to cpu core(s), mask 0x3 for cores 0 and 1 50 | --no-color disable colored output 51 | --donate-level=N donate level, default 5% (5 minutes in 100 minutes) 52 | -B, --background run the miner in the background 53 | -c, --config=FILE load a JSON-format configuration file 54 | -h, --help display this help and exit 55 | -V, --version output version information and exit 56 | ``` 57 | 58 | ## Algorithm variations 59 | * `--av=1` Single hash hardware AES. 60 | * `--av=2` Double hash hardware AES. 61 | * `--av=3` Single hash software AES implementation. 62 | * `--av=4` Double hash software AES implementation. 63 | 64 | ### Single vs Double 65 | Single hash variations required 1 MB CPU L3 cache for one thread for optimal performance, double hash required 2MB per thread. 66 | Double hash usually better choice, less CPU usage and a fairly high hashrate. 67 | 68 | Example with Intel i7 6700: 69 | 70 | | AV | Threads | H/s | CPU usage | 71 | |----------|---------|------|-----------| 72 | | `--av=1` | 4 | 600 | 50% | 73 | | `--av=2` | 4 | 1000 | 50% | 74 | | `--av=1` | 8 | 1150 | 100% | 75 | 76 | ## Build 77 | ### Ubuntu (Debian-based distros) 78 | ``` 79 | sudo apt-get install git build-essential cmake libcurl4-openssl-dev 80 | git clone https://github.com/xmrig/xmrig-aeon.git 81 | cd xmrig-aeon 82 | mkdir build 83 | cd build 84 | cmake .. -DCMAKE_BUILD_TYPE=Release 85 | make 86 | ``` 87 | 88 | ### Windows 89 | It's complicated, you need [MSYS2](http://www.msys2.org/), custom libcurl build, and of course CMake too. 90 | 91 | Necessary MSYS2 packages: 92 | ``` 93 | pacman -Sy 94 | pacman -S mingw-w64-x86_64-gcc 95 | pacman -S make 96 | pacman -S mingw-w64-x86_64-cmake 97 | pacman -S mingw-w64-x86_64-pkg-config 98 | ``` 99 | Configure options for libcurl: 100 | ``` 101 | ./configure --disable-shared --enable-optimize --enable-threaded-resolver --disable-libcurl-option --disable-ares --disable-rt --disable-ftp --disable-file --disable-ldap --disable-ldaps --disable-rtsp --disable-dict --disable-telnet --disable-tftp --disable-pop3 --disable-imap --disable-smb --disable-smtp --disable-gopher --disable-manual --disable-ipv6 --disable-sspi --disable-crypto-auth --disable-ntlm-wb --disable-tls-srp --disable-unix-sockets --without-zlib --without-winssl --without-ssl --without-libssh2 --without-nghttp2 --disable-cookies --without-ca-bundle --without-librtmp 102 | ``` 103 | CMake options: 104 | ``` 105 | cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DCURL_INCLUDE_DIR="c:\\curl-7.53.1\include" -DCURL_LIBRARY="c:\\curl-7.53.1\lib\.libs" 106 | ``` 107 | 108 | ## Common Issues 109 | ### HUGE PAGES unavailable 110 | * Run XMRig as Administrator. 111 | * Enable SeLockMemoryPrivilege. For Windows 7 pro, or Windows 8 and above see [this article](https://msdn.microsoft.com/en-gb/library/ms190730.aspx). 112 | 113 | ## Other information 114 | * No HTTP support, only stratum protocol support. 115 | * No TLS support. 116 | * Default donation 5% (5 minutes in 100 minutes) can be reduced to 1% via command line option `--donate-level`. 117 | 118 | Please note performance is highly dependent on system load. The numbers above are obtained on an idle system. Tasks heavily using a processor cache, such as video playback, can greatly degrade hashrate. Optimal number of threads depends on the size of the L3 cache of a processor, 1 thread requires 1 or 2 MB of cache. 119 | 120 | ### Maximum performance checklist 121 | * Idle operating system. 122 | * Do not exceed optimal thread count. 123 | * Use modern CPUs with AES-NI instructuon set. 124 | * Try setup optimal cpu affinity. 125 | * Enable fast memory (Large/Huge pages). 126 | 127 | ## Donations 128 | * XMR: `48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD` 129 | * BTC: `1P7ujsXeX7GxQwHNnJsRMgAdNkFZmNVqJT` 130 | 131 | ## Contacts 132 | * support@xmrig.com 133 | * [reddit](https://www.reddit.com/user/XMRig/) 134 | -------------------------------------------------------------------------------- /crypto/groestl_tables.h: -------------------------------------------------------------------------------- 1 | #ifndef __tables_h 2 | #define __tables_h 3 | 4 | 5 | const uint32_t T[512] = {0xa5f432c6, 0xc6a597f4, 0x84976ff8, 0xf884eb97, 0x99b05eee, 0xee99c7b0, 0x8d8c7af6, 0xf68df78c, 0xd17e8ff, 0xff0de517, 0xbddc0ad6, 0xd6bdb7dc, 0xb1c816de, 0xdeb1a7c8, 0x54fc6d91, 0x915439fc 6 | , 0x50f09060, 0x6050c0f0, 0x3050702, 0x2030405, 0xa9e02ece, 0xcea987e0, 0x7d87d156, 0x567dac87, 0x192bcce7, 0xe719d52b, 0x62a613b5, 0xb56271a6, 0xe6317c4d, 0x4de69a31, 0x9ab559ec, 0xec9ac3b5 7 | , 0x45cf408f, 0x8f4505cf, 0x9dbca31f, 0x1f9d3ebc, 0x40c04989, 0x894009c0, 0x879268fa, 0xfa87ef92, 0x153fd0ef, 0xef15c53f, 0xeb2694b2, 0xb2eb7f26, 0xc940ce8e, 0x8ec90740, 0xb1de6fb, 0xfb0bed1d 8 | , 0xec2f6e41, 0x41ec822f, 0x67a91ab3, 0xb3677da9, 0xfd1c435f, 0x5ffdbe1c, 0xea256045, 0x45ea8a25, 0xbfdaf923, 0x23bf46da, 0xf7025153, 0x53f7a602, 0x96a145e4, 0xe496d3a1, 0x5bed769b, 0x9b5b2ded 9 | , 0xc25d2875, 0x75c2ea5d, 0x1c24c5e1, 0xe11cd924, 0xaee9d43d, 0x3dae7ae9, 0x6abef24c, 0x4c6a98be, 0x5aee826c, 0x6c5ad8ee, 0x41c3bd7e, 0x7e41fcc3, 0x206f3f5, 0xf502f106, 0x4fd15283, 0x834f1dd1 10 | , 0x5ce48c68, 0x685cd0e4, 0xf4075651, 0x51f4a207, 0x345c8dd1, 0xd134b95c, 0x818e1f9, 0xf908e918, 0x93ae4ce2, 0xe293dfae, 0x73953eab, 0xab734d95, 0x53f59762, 0x6253c4f5, 0x3f416b2a, 0x2a3f5441 11 | , 0xc141c08, 0x80c1014, 0x52f66395, 0x955231f6, 0x65afe946, 0x46658caf, 0x5ee27f9d, 0x9d5e21e2, 0x28784830, 0x30286078, 0xa1f8cf37, 0x37a16ef8, 0xf111b0a, 0xa0f1411, 0xb5c4eb2f, 0x2fb55ec4 12 | , 0x91b150e, 0xe091c1b, 0x365a7e24, 0x2436485a, 0x9bb6ad1b, 0x1b9b36b6, 0x3d4798df, 0xdf3da547, 0x266aa7cd, 0xcd26816a, 0x69bbf54e, 0x4e699cbb, 0xcd4c337f, 0x7fcdfe4c, 0x9fba50ea, 0xea9fcfba 13 | , 0x1b2d3f12, 0x121b242d, 0x9eb9a41d, 0x1d9e3ab9, 0x749cc458, 0x5874b09c, 0x2e724634, 0x342e6872, 0x2d774136, 0x362d6c77, 0xb2cd11dc, 0xdcb2a3cd, 0xee299db4, 0xb4ee7329, 0xfb164d5b, 0x5bfbb616 14 | , 0xf601a5a4, 0xa4f65301, 0x4dd7a176, 0x764decd7, 0x61a314b7, 0xb76175a3, 0xce49347d, 0x7dcefa49, 0x7b8ddf52, 0x527ba48d, 0x3e429fdd, 0xdd3ea142, 0x7193cd5e, 0x5e71bc93, 0x97a2b113, 0x139726a2 15 | , 0xf504a2a6, 0xa6f55704, 0x68b801b9, 0xb96869b8, 0x0, 0x0, 0x2c74b5c1, 0xc12c9974, 0x60a0e040, 0x406080a0, 0x1f21c2e3, 0xe31fdd21, 0xc8433a79, 0x79c8f243, 0xed2c9ab6, 0xb6ed772c 16 | , 0xbed90dd4, 0xd4beb3d9, 0x46ca478d, 0x8d4601ca, 0xd9701767, 0x67d9ce70, 0x4bddaf72, 0x724be4dd, 0xde79ed94, 0x94de3379, 0xd467ff98, 0x98d42b67, 0xe82393b0, 0xb0e87b23, 0x4ade5b85, 0x854a11de 17 | , 0x6bbd06bb, 0xbb6b6dbd, 0x2a7ebbc5, 0xc52a917e, 0xe5347b4f, 0x4fe59e34, 0x163ad7ed, 0xed16c13a, 0xc554d286, 0x86c51754, 0xd762f89a, 0x9ad72f62, 0x55ff9966, 0x6655ccff, 0x94a7b611, 0x119422a7 18 | , 0xcf4ac08a, 0x8acf0f4a, 0x1030d9e9, 0xe910c930, 0x60a0e04, 0x406080a, 0x819866fe, 0xfe81e798, 0xf00baba0, 0xa0f05b0b, 0x44ccb478, 0x7844f0cc, 0xbad5f025, 0x25ba4ad5, 0xe33e754b, 0x4be3963e 19 | , 0xf30eaca2, 0xa2f35f0e, 0xfe19445d, 0x5dfeba19, 0xc05bdb80, 0x80c01b5b, 0x8a858005, 0x58a0a85, 0xadecd33f, 0x3fad7eec, 0xbcdffe21, 0x21bc42df, 0x48d8a870, 0x7048e0d8, 0x40cfdf1, 0xf104f90c 20 | , 0xdf7a1963, 0x63dfc67a, 0xc1582f77, 0x77c1ee58, 0x759f30af, 0xaf75459f, 0x63a5e742, 0x426384a5, 0x30507020, 0x20304050, 0x1a2ecbe5, 0xe51ad12e, 0xe12effd, 0xfd0ee112, 0x6db708bf, 0xbf6d65b7 21 | , 0x4cd45581, 0x814c19d4, 0x143c2418, 0x1814303c, 0x355f7926, 0x26354c5f, 0x2f71b2c3, 0xc32f9d71, 0xe13886be, 0xbee16738, 0xa2fdc835, 0x35a26afd, 0xcc4fc788, 0x88cc0b4f, 0x394b652e, 0x2e395c4b 22 | , 0x57f96a93, 0x93573df9, 0xf20d5855, 0x55f2aa0d, 0x829d61fc, 0xfc82e39d, 0x47c9b37a, 0x7a47f4c9, 0xacef27c8, 0xc8ac8bef, 0xe73288ba, 0xbae76f32, 0x2b7d4f32, 0x322b647d, 0x95a442e6, 0xe695d7a4 23 | , 0xa0fb3bc0, 0xc0a09bfb, 0x98b3aa19, 0x199832b3, 0xd168f69e, 0x9ed12768, 0x7f8122a3, 0xa37f5d81, 0x66aaee44, 0x446688aa, 0x7e82d654, 0x547ea882, 0xabe6dd3b, 0x3bab76e6, 0x839e950b, 0xb83169e 24 | , 0xca45c98c, 0x8cca0345, 0x297bbcc7, 0xc729957b, 0xd36e056b, 0x6bd3d66e, 0x3c446c28, 0x283c5044, 0x798b2ca7, 0xa779558b, 0xe23d81bc, 0xbce2633d, 0x1d273116, 0x161d2c27, 0x769a37ad, 0xad76419a 25 | , 0x3b4d96db, 0xdb3bad4d, 0x56fa9e64, 0x6456c8fa, 0x4ed2a674, 0x744ee8d2, 0x1e223614, 0x141e2822, 0xdb76e492, 0x92db3f76, 0xa1e120c, 0xc0a181e, 0x6cb4fc48, 0x486c90b4, 0xe4378fb8, 0xb8e46b37 26 | , 0x5de7789f, 0x9f5d25e7, 0x6eb20fbd, 0xbd6e61b2, 0xef2a6943, 0x43ef862a, 0xa6f135c4, 0xc4a693f1, 0xa8e3da39, 0x39a872e3, 0xa4f7c631, 0x31a462f7, 0x37598ad3, 0xd337bd59, 0x8b8674f2, 0xf28bff86 27 | , 0x325683d5, 0xd532b156, 0x43c54e8b, 0x8b430dc5, 0x59eb856e, 0x6e59dceb, 0xb7c218da, 0xdab7afc2, 0x8c8f8e01, 0x18c028f, 0x64ac1db1, 0xb16479ac, 0xd26df19c, 0x9cd2236d, 0xe03b7249, 0x49e0923b 28 | , 0xb4c71fd8, 0xd8b4abc7, 0xfa15b9ac, 0xacfa4315, 0x709faf3, 0xf307fd09, 0x256fa0cf, 0xcf25856f, 0xafea20ca, 0xcaaf8fea, 0x8e897df4, 0xf48ef389, 0xe9206747, 0x47e98e20, 0x18283810, 0x10182028 29 | , 0xd5640b6f, 0x6fd5de64, 0x888373f0, 0xf088fb83, 0x6fb1fb4a, 0x4a6f94b1, 0x7296ca5c, 0x5c72b896, 0x246c5438, 0x3824706c, 0xf1085f57, 0x57f1ae08, 0xc7522173, 0x73c7e652, 0x51f36497, 0x975135f3 30 | , 0x2365aecb, 0xcb238d65, 0x7c8425a1, 0xa17c5984, 0x9cbf57e8, 0xe89ccbbf, 0x21635d3e, 0x3e217c63, 0xdd7cea96, 0x96dd377c, 0xdc7f1e61, 0x61dcc27f, 0x86919c0d, 0xd861a91, 0x85949b0f, 0xf851e94 31 | , 0x90ab4be0, 0xe090dbab, 0x42c6ba7c, 0x7c42f8c6, 0xc4572671, 0x71c4e257, 0xaae529cc, 0xccaa83e5, 0xd873e390, 0x90d83b73, 0x50f0906, 0x6050c0f, 0x103f4f7, 0xf701f503, 0x12362a1c, 0x1c123836 32 | , 0xa3fe3cc2, 0xc2a39ffe, 0x5fe18b6a, 0x6a5fd4e1, 0xf910beae, 0xaef94710, 0xd06b0269, 0x69d0d26b, 0x91a8bf17, 0x17912ea8, 0x58e87199, 0x995829e8, 0x2769533a, 0x3a277469, 0xb9d0f727, 0x27b94ed0 33 | , 0x384891d9, 0xd938a948, 0x1335deeb, 0xeb13cd35, 0xb3cee52b, 0x2bb356ce, 0x33557722, 0x22334455, 0xbbd604d2, 0xd2bbbfd6, 0x709039a9, 0xa9704990, 0x89808707, 0x7890e80, 0xa7f2c133, 0x33a766f2 34 | , 0xb6c1ec2d, 0x2db65ac1, 0x22665a3c, 0x3c227866, 0x92adb815, 0x15922aad, 0x2060a9c9, 0xc9208960, 0x49db5c87, 0x874915db, 0xff1ab0aa, 0xaaff4f1a, 0x7888d850, 0x5078a088, 0x7a8e2ba5, 0xa57a518e 35 | , 0x8f8a8903, 0x38f068a, 0xf8134a59, 0x59f8b213, 0x809b9209, 0x980129b, 0x1739231a, 0x1a173439, 0xda751065, 0x65daca75, 0x315384d7, 0xd731b553, 0xc651d584, 0x84c61351, 0xb8d303d0, 0xd0b8bbd3 36 | , 0xc35edc82, 0x82c31f5e, 0xb0cbe229, 0x29b052cb, 0x7799c35a, 0x5a77b499, 0x11332d1e, 0x1e113c33, 0xcb463d7b, 0x7bcbf646, 0xfc1fb7a8, 0xa8fc4b1f, 0xd6610c6d, 0x6dd6da61, 0x3a4e622c, 0x2c3a584e}; 37 | 38 | #endif /* __tables_h */ 39 | -------------------------------------------------------------------------------- /util.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "util.h" 31 | #include "elist.h" 32 | #include "utils/applog.h" 33 | 34 | 35 | struct tq_ent { 36 | void *data; 37 | struct list_head q_node; 38 | }; 39 | 40 | 41 | struct thread_q { 42 | struct list_head q; 43 | bool frozen; 44 | pthread_mutex_t mutex; 45 | pthread_cond_t cond; 46 | }; 47 | 48 | 49 | json_t *json_decode(const char *s) 50 | { 51 | json_error_t err; 52 | json_t *val = json_loads(s, 0, &err); 53 | 54 | if (!val) { 55 | applog(LOG_ERR, "JSON decode failed(%d): %s", err.line, err.text); 56 | } 57 | 58 | return val; 59 | } 60 | 61 | 62 | /** 63 | * @brief bin2hex 64 | * @param p 65 | * @param len 66 | * @return 67 | */ 68 | char *bin2hex(const unsigned char *p, size_t len) 69 | { 70 | char *s = malloc((len * 2) + 1); 71 | if (!s) { 72 | return NULL; 73 | } 74 | 75 | for (int i = 0; i < len; i++) { 76 | sprintf(s + (i * 2), "%02x", (unsigned int) p[i]); 77 | } 78 | 79 | return s; 80 | } 81 | 82 | 83 | /** 84 | * @brief hex2bin 85 | * @param p 86 | * @param hexstr 87 | * @param len 88 | * @return 89 | */ 90 | bool hex2bin(unsigned char *p, const char *hexstr, size_t len) 91 | { 92 | char hex_byte[3]; 93 | char *ep; 94 | 95 | hex_byte[2] = '\0'; 96 | 97 | while (*hexstr && len) { 98 | if (!hexstr[1]) { 99 | applog(LOG_ERR, "hex2bin str truncated"); 100 | return false; 101 | } 102 | 103 | hex_byte[0] = hexstr[0]; 104 | hex_byte[1] = hexstr[1]; 105 | *p = (unsigned char) strtol(hex_byte, &ep, 16); 106 | if (*ep) { 107 | applog(LOG_ERR, "hex2bin failed on '%s'", hex_byte); 108 | return false; 109 | } 110 | 111 | p++; 112 | hexstr += 2; 113 | len--; 114 | } 115 | 116 | return (len == 0 && *hexstr == 0) ? true : false; 117 | } 118 | 119 | 120 | /** 121 | * @brief tq_new 122 | * @return 123 | */ 124 | struct thread_q *tq_new(void) 125 | { 126 | struct thread_q *tq; 127 | 128 | tq = calloc(1, sizeof(*tq)); 129 | if (!tq) 130 | return NULL; 131 | 132 | INIT_LIST_HEAD(&tq->q); 133 | pthread_mutex_init(&tq->mutex, NULL); 134 | pthread_cond_init(&tq->cond, NULL); 135 | 136 | return tq; 137 | } 138 | 139 | 140 | /** 141 | * @brief tq_free 142 | * @param tq 143 | */ 144 | void tq_free(struct thread_q *tq) 145 | { 146 | struct tq_ent *ent, *iter; 147 | 148 | if (!tq) 149 | return; 150 | 151 | list_for_each_entry_safe(ent, iter, &tq->q, q_node) { 152 | list_del(&ent->q_node); 153 | free(ent); 154 | } 155 | 156 | pthread_cond_destroy(&tq->cond); 157 | pthread_mutex_destroy(&tq->mutex); 158 | 159 | memset(tq, 0, sizeof(*tq)); /* poison */ 160 | free(tq); 161 | } 162 | 163 | 164 | /** 165 | * @brief tq_freezethaw 166 | * @param tq 167 | * @param frozen 168 | */ 169 | static void tq_freezethaw(struct thread_q *tq, bool frozen) 170 | { 171 | pthread_mutex_lock(&tq->mutex); 172 | 173 | tq->frozen = frozen; 174 | 175 | pthread_cond_signal(&tq->cond); 176 | pthread_mutex_unlock(&tq->mutex); 177 | } 178 | 179 | 180 | /** 181 | * @brief tq_freeze 182 | * @param tq 183 | */ 184 | void tq_freeze(struct thread_q *tq) 185 | { 186 | tq_freezethaw(tq, true); 187 | } 188 | 189 | 190 | /** 191 | * @brief tq_thaw 192 | * @param tq 193 | */ 194 | void tq_thaw(struct thread_q *tq) 195 | { 196 | tq_freezethaw(tq, false); 197 | } 198 | 199 | 200 | /** 201 | * @brief tq_push 202 | * @param tq 203 | * @param data 204 | * @return 205 | */ 206 | bool tq_push(struct thread_q *tq, void *data) 207 | { 208 | struct tq_ent *ent; 209 | bool rc = true; 210 | 211 | ent = calloc(1, sizeof(*ent)); 212 | if (!ent) 213 | return false; 214 | 215 | ent->data = data; 216 | INIT_LIST_HEAD(&ent->q_node); 217 | 218 | pthread_mutex_lock(&tq->mutex); 219 | 220 | if (!tq->frozen) { 221 | list_add_tail(&ent->q_node, &tq->q); 222 | } else { 223 | free(ent); 224 | rc = false; 225 | } 226 | 227 | pthread_cond_signal(&tq->cond); 228 | pthread_mutex_unlock(&tq->mutex); 229 | 230 | return rc; 231 | } 232 | 233 | 234 | /** 235 | * @brief tq_pop 236 | * @param tq 237 | * @param abstime 238 | * @return 239 | */ 240 | void *tq_pop(struct thread_q *tq, const struct timespec *abstime) 241 | { 242 | struct tq_ent *ent; 243 | void *rval = NULL; 244 | int rc; 245 | 246 | pthread_mutex_lock(&tq->mutex); 247 | 248 | if (!list_empty(&tq->q)) 249 | goto pop; 250 | 251 | if (abstime) 252 | rc = pthread_cond_timedwait(&tq->cond, &tq->mutex, abstime); 253 | else 254 | rc = pthread_cond_wait(&tq->cond, &tq->mutex); 255 | if (rc) 256 | goto out; 257 | if (list_empty(&tq->q)) 258 | goto out; 259 | 260 | pop: 261 | ent = list_entry(tq->q.next, struct tq_ent, q_node); 262 | rval = ent->data; 263 | 264 | list_del(&ent->q_node); 265 | free(ent); 266 | 267 | out: 268 | pthread_mutex_unlock(&tq->mutex); 269 | return rval; 270 | } 271 | -------------------------------------------------------------------------------- /algo/cryptonight/cryptonight.c: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #ifndef BUILD_TEST 30 | # include "xmrig.h" 31 | #endif 32 | 33 | #include "crypto/c_groestl.h" 34 | #include "crypto/c_blake256.h" 35 | #include "crypto/c_jh.h" 36 | #include "crypto/c_skein.h" 37 | #include "cryptonight.h" 38 | #include "options.h" 39 | 40 | 41 | const static char test_input[152] = { 42 | 0x01, 0x00, 0xFB, 0x8E, 0x8A, 0xC8, 0x05, 0x89, 0x93, 0x23, 0x37, 0x1B, 0xB7, 0x90, 0xDB, 0x19, 43 | 0x21, 0x8A, 0xFD, 0x8D, 0xB8, 0xE3, 0x75, 0x5D, 0x8B, 0x90, 0xF3, 0x9B, 0x3D, 0x55, 0x06, 0xA9, 44 | 0xAB, 0xCE, 0x4F, 0xA9, 0x12, 0x24, 0x45, 0x00, 0x00, 0x00, 0x00, 0xEE, 0x81, 0x46, 0xD4, 0x9F, 45 | 0xA9, 0x3E, 0xE7, 0x24, 0xDE, 0xB5, 0x7D, 0x12, 0xCB, 0xC6, 0xC6, 0xF3, 0xB9, 0x24, 0xD9, 0x46, 46 | 0x12, 0x7C, 0x7A, 0x97, 0x41, 0x8F, 0x93, 0x48, 0x82, 0x8F, 0x0F, 0x02, 47 | 0x03, 0x05, 0xA0, 0xDB, 0xD6, 0xBF, 0x05, 0xCF, 0x16, 0xE5, 0x03, 0xF3, 0xA6, 0x6F, 0x78, 0x00, 48 | 0x7C, 0xBF, 0x34, 0x14, 0x43, 0x32, 0xEC, 0xBF, 0xC2, 0x2E, 0xD9, 0x5C, 0x87, 0x00, 0x38, 0x3B, 49 | 0x30, 0x9A, 0xCE, 0x19, 0x23, 0xA0, 0x96, 0x4B, 0x00, 0x00, 0x00, 0x08, 0xBA, 0x93, 0x9A, 0x62, 50 | 0x72, 0x4C, 0x0D, 0x75, 0x81, 0xFC, 0xE5, 0x76, 0x1E, 0x9D, 0x8A, 0x0E, 0x6A, 0x1C, 0x3F, 0x92, 51 | 0x4F, 0xDD, 0x84, 0x93, 0xD1, 0x11, 0x56, 0x49, 0xC0, 0x5E, 0xB6, 0x01 52 | }; 53 | 54 | const static char test_output[64] = { 55 | 0x28, 0xA2, 0x2B, 0xAD, 0x3F, 0x93, 0xD1, 0x40, 0x8F, 0xCA, 0x47, 0x2E, 0xB5, 0xAD, 0x1C, 0xBE, 56 | 0x75, 0xF2, 0x1D, 0x05, 0x3C, 0x8C, 0xE5, 0xB3, 0xAF, 0x10, 0x5A, 0x57, 0x71, 0x3E, 0x21, 0xDD, 57 | 0x36, 0x95, 0xB4, 0xB5, 0x3B, 0xB0, 0x03, 0x58, 0xB0, 0xAD, 0x38, 0xDC, 0x16, 0x0F, 0xEB, 0x9E, 58 | 0x00, 0x4E, 0xEC, 0xE0, 0x9B, 0x83, 0xA7, 0x2E, 0xF6, 0xBA, 0x98, 0x64, 0xD3, 0x51, 0x0C, 0x88, 59 | }; 60 | 61 | 62 | void cryptonight_av1_aesni(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); 63 | void cryptonight_av2_aesni_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); 64 | void cryptonight_av3_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); 65 | void cryptonight_av4_softaes_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); 66 | 67 | 68 | void (*cryptonight_hash_ctx)(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx) = NULL; 69 | 70 | 71 | static bool self_test() { 72 | char output[64]; 73 | 74 | struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) _mm_malloc(sizeof(struct cryptonight_ctx), 16); 75 | ctx->memory = (uint8_t *) _mm_malloc(MEMORY, 16); 76 | 77 | 78 | cryptonight_hash_ctx(test_input, 76, output, ctx); 79 | 80 | _mm_free(ctx->memory); 81 | _mm_free(ctx); 82 | 83 | return memcmp(output, test_output, (opt_double_hash ? 64 : 32)) == 0; 84 | } 85 | 86 | 87 | bool cryptonight_init(int variant) 88 | { 89 | switch (variant) { 90 | case AEON_AV1_AESNI: 91 | cryptonight_hash_ctx = cryptonight_av1_aesni; 92 | break; 93 | 94 | case AEON_AV2_AESNI_DOUBLE: 95 | opt_double_hash = true; 96 | cryptonight_hash_ctx = cryptonight_av2_aesni_double; 97 | break; 98 | 99 | case AEON_AV3_SOFT_AES: 100 | cryptonight_hash_ctx = cryptonight_av3_softaes; 101 | break; 102 | 103 | case AEON_AV4_SOFT_AES_DOUBLE: 104 | opt_double_hash = true; 105 | cryptonight_hash_ctx = cryptonight_av4_softaes_double; 106 | break; 107 | 108 | default: 109 | break; 110 | } 111 | 112 | return self_test(); 113 | } 114 | 115 | 116 | static inline void do_blake_hash(const void* input, size_t len, char* output) { 117 | blake256_hash((uint8_t*)output, input, len); 118 | } 119 | 120 | 121 | static inline void do_groestl_hash(const void* input, size_t len, char* output) { 122 | groestl(input, len * 8, (uint8_t*)output); 123 | } 124 | 125 | 126 | static inline void do_jh_hash(const void* input, size_t len, char* output) { 127 | jh_hash(32 * 8, input, 8 * len, (uint8_t*)output); 128 | } 129 | 130 | 131 | static inline void do_skein_hash(const void* input, size_t len, char* output) { 132 | skein_hash(8 * 32, input, 8 * len, (uint8_t*)output); 133 | } 134 | 135 | 136 | void (* const extra_hashes[4])(const void *, size_t, char *) = {do_blake_hash, do_groestl_hash, do_jh_hash, do_skein_hash}; 137 | 138 | 139 | #ifndef BUILD_TEST 140 | int scanhash_cryptonight(int thr_id, uint32_t *hash, uint32_t *restrict blob, size_t blob_size, uint32_t target, uint32_t max_nonce, unsigned long *restrict hashes_done, struct cryptonight_ctx *restrict ctx) { 141 | uint32_t *nonceptr = (uint32_t*) (((char*) blob) + 39); 142 | 143 | do { 144 | cryptonight_hash_ctx(blob, blob_size, hash, ctx); 145 | (*hashes_done)++; 146 | 147 | if (unlikely(hash[7] < target)) { 148 | return 1; 149 | } 150 | 151 | (*nonceptr)++; 152 | } while (likely(((*nonceptr) < max_nonce && !work_restart[thr_id].restart))); 153 | 154 | return 0; 155 | } 156 | 157 | 158 | int scanhash_cryptonight_double(int thr_id, uint32_t *hash, uint8_t *restrict blob, size_t blob_size, uint32_t target, uint32_t max_nonce, unsigned long *restrict hashes_done, struct cryptonight_ctx *restrict ctx) { 159 | int rc = 0; 160 | uint32_t *nonceptr0 = (uint32_t*) (((char*) blob) + 39); 161 | uint32_t *nonceptr1 = (uint32_t*) (((char*) blob) + 39 + blob_size); 162 | 163 | do { 164 | cryptonight_hash_ctx(blob, blob_size, hash, ctx); 165 | (*hashes_done) += 2; 166 | 167 | if (unlikely(hash[7] < target)) { 168 | return rc |= 1; 169 | } 170 | 171 | if (unlikely(hash[15] < target)) { 172 | return rc |= 2; 173 | } 174 | 175 | if (rc) { 176 | break; 177 | } 178 | 179 | (*nonceptr0)++; 180 | (*nonceptr1)++; 181 | } while (likely(((*nonceptr0) < max_nonce && !work_restart[thr_id].restart))); 182 | 183 | return rc; 184 | } 185 | #endif 186 | -------------------------------------------------------------------------------- /crypto/skein_port.h: -------------------------------------------------------------------------------- 1 | #ifndef _SKEIN_PORT_H_ 2 | #define _SKEIN_PORT_H_ 3 | 4 | #include 5 | #include 6 | 7 | #ifndef RETURN_VALUES 8 | # define RETURN_VALUES 9 | # if defined( DLL_EXPORT ) 10 | # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER ) 11 | # define VOID_RETURN __declspec( dllexport ) void __stdcall 12 | # define INT_RETURN __declspec( dllexport ) int __stdcall 13 | # elif defined( __GNUC__ ) 14 | # define VOID_RETURN __declspec( __dllexport__ ) void 15 | # define INT_RETURN __declspec( __dllexport__ ) int 16 | # else 17 | # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers 18 | # endif 19 | # elif defined( DLL_IMPORT ) 20 | # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER ) 21 | # define VOID_RETURN __declspec( dllimport ) void __stdcall 22 | # define INT_RETURN __declspec( dllimport ) int __stdcall 23 | # elif defined( __GNUC__ ) 24 | # define VOID_RETURN __declspec( __dllimport__ ) void 25 | # define INT_RETURN __declspec( __dllimport__ ) int 26 | # else 27 | # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers 28 | # endif 29 | # elif defined( __WATCOMC__ ) 30 | # define VOID_RETURN void __cdecl 31 | # define INT_RETURN int __cdecl 32 | # else 33 | # define VOID_RETURN void 34 | # define INT_RETURN int 35 | # endif 36 | #endif 37 | 38 | /* These defines are used to declare buffers in a way that allows 39 | faster operations on longer variables to be used. In all these 40 | defines 'size' must be a power of 2 and >= 8 41 | 42 | dec_unit_type(size,x) declares a variable 'x' of length 43 | 'size' bits 44 | 45 | dec_bufr_type(size,bsize,x) declares a buffer 'x' of length 'bsize' 46 | bytes defined as an array of variables 47 | each of 'size' bits (bsize must be a 48 | multiple of size / 8) 49 | 50 | ptr_cast(x,size) casts a pointer to a pointer to a 51 | varaiable of length 'size' bits 52 | */ 53 | 54 | #define ui_type(size) uint##size##_t 55 | #define dec_unit_type(size,x) typedef ui_type(size) x 56 | #define dec_bufr_type(size,bsize,x) typedef ui_type(size) x[bsize / (size >> 3)] 57 | #define ptr_cast(x,size) ((ui_type(size)*)(x)) 58 | 59 | typedef unsigned int uint_t; /* native unsigned integer */ 60 | typedef uint8_t u08b_t; /* 8-bit unsigned integer */ 61 | typedef uint64_t u64b_t; /* 64-bit unsigned integer */ 62 | 63 | #ifndef RotL_64 64 | #define RotL_64(x,N) (((x) << (N)) | ((x) >> (64-(N)))) 65 | #endif 66 | 67 | /* 68 | * Skein is "natively" little-endian (unlike SHA-xxx), for optimal 69 | * performance on x86 CPUs. The Skein code requires the following 70 | * definitions for dealing with endianness: 71 | * 72 | * SKEIN_NEED_SWAP: 0 for little-endian, 1 for big-endian 73 | * Skein_Put64_LSB_First 74 | * Skein_Get64_LSB_First 75 | * Skein_Swap64 76 | * 77 | * If SKEIN_NEED_SWAP is defined at compile time, it is used here 78 | * along with the portable versions of Put64/Get64/Swap64, which 79 | * are slow in general. 80 | * 81 | * Otherwise, an "auto-detect" of endianness is attempted below. 82 | * If the default handling doesn't work well, the user may insert 83 | * platform-specific code instead (e.g., for big-endian CPUs). 84 | * 85 | */ 86 | #ifndef SKEIN_NEED_SWAP /* compile-time "override" for endianness? */ 87 | 88 | #define IS_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */ 89 | #define IS_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */ 90 | 91 | #if BYTE_ORDER == LITTLE_ENDIAN && !defined(PLATFORM_BYTE_ORDER) 92 | # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN 93 | #endif 94 | 95 | #if BYTE_ORDER == BIG_ENDIAN && !defined(PLATFORM_BYTE_ORDER) 96 | # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN 97 | #endif 98 | 99 | /* special handler for IA64, which may be either endianness (?) */ 100 | /* here we assume little-endian, but this may need to be changed */ 101 | #if defined(__ia64) || defined(__ia64__) || defined(_M_IA64) 102 | # define PLATFORM_MUST_ALIGN (1) 103 | #ifndef PLATFORM_BYTE_ORDER 104 | # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN 105 | #endif 106 | #endif 107 | 108 | #ifndef PLATFORM_MUST_ALIGN 109 | # define PLATFORM_MUST_ALIGN (0) 110 | #endif 111 | 112 | 113 | #if PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN 114 | /* here for big-endian CPUs */ 115 | #define SKEIN_NEED_SWAP (1) 116 | #elif PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN 117 | /* here for x86 and x86-64 CPUs (and other detected little-endian CPUs) */ 118 | #define SKEIN_NEED_SWAP (0) 119 | #if PLATFORM_MUST_ALIGN == 0 /* ok to use "fast" versions? */ 120 | #define Skein_Put64_LSB_First(dst08,src64,bCnt) memcpy(dst08,src64,bCnt) 121 | #define Skein_Get64_LSB_First(dst64,src08,wCnt) memcpy(dst64,src08,8*(wCnt)) 122 | #endif 123 | #else 124 | #error "Skein needs endianness setting!" 125 | #endif 126 | 127 | #endif /* ifndef SKEIN_NEED_SWAP */ 128 | 129 | /* 130 | ****************************************************************** 131 | * Provide any definitions still needed. 132 | ****************************************************************** 133 | */ 134 | #ifndef Skein_Swap64 /* swap for big-endian, nop for little-endian */ 135 | #if SKEIN_NEED_SWAP 136 | #define Skein_Swap64(w64) \ 137 | ( (( ((u64b_t)(w64)) & 0xFF) << 56) | \ 138 | (((((u64b_t)(w64)) >> 8) & 0xFF) << 48) | \ 139 | (((((u64b_t)(w64)) >>16) & 0xFF) << 40) | \ 140 | (((((u64b_t)(w64)) >>24) & 0xFF) << 32) | \ 141 | (((((u64b_t)(w64)) >>32) & 0xFF) << 24) | \ 142 | (((((u64b_t)(w64)) >>40) & 0xFF) << 16) | \ 143 | (((((u64b_t)(w64)) >>48) & 0xFF) << 8) | \ 144 | (((((u64b_t)(w64)) >>56) & 0xFF) ) ) 145 | #else 146 | #define Skein_Swap64(w64) (w64) 147 | #endif 148 | #endif /* ifndef Skein_Swap64 */ 149 | 150 | 151 | #ifndef Skein_Put64_LSB_First 152 | void Skein_Put64_LSB_First(u08b_t *dst,const u64b_t *src,size_t bCnt) 153 | #ifdef SKEIN_PORT_CODE /* instantiate the function code here? */ 154 | { /* this version is fully portable (big-endian or little-endian), but slow */ 155 | size_t n; 156 | 157 | for (n=0;n>3] >> (8*(n&7))); 159 | } 160 | #else 161 | ; /* output only the function prototype */ 162 | #endif 163 | #endif /* ifndef Skein_Put64_LSB_First */ 164 | 165 | 166 | #ifndef Skein_Get64_LSB_First 167 | void Skein_Get64_LSB_First(u64b_t *dst,const u08b_t *src,size_t wCnt) 168 | #ifdef SKEIN_PORT_CODE /* instantiate the function code here? */ 169 | { /* this version is fully portable (big-endian or little-endian), but slow */ 170 | size_t n; 171 | 172 | for (n=0;n<8*wCnt;n+=8) 173 | dst[n/8] = (((u64b_t) src[n ]) ) + 174 | (((u64b_t) src[n+1]) << 8) + 175 | (((u64b_t) src[n+2]) << 16) + 176 | (((u64b_t) src[n+3]) << 24) + 177 | (((u64b_t) src[n+4]) << 32) + 178 | (((u64b_t) src[n+5]) << 40) + 179 | (((u64b_t) src[n+6]) << 48) + 180 | (((u64b_t) src[n+7]) << 56) ; 181 | } 182 | #else 183 | ; /* output only the function prototype */ 184 | #endif 185 | #endif /* ifndef Skein_Get64_LSB_First */ 186 | 187 | #endif /* ifndef _SKEIN_PORT_H_ */ 188 | -------------------------------------------------------------------------------- /compat/jansson/hashtable_seed.c: -------------------------------------------------------------------------------- 1 | /* Generate sizeof(uint32_t) bytes of as random data as possible to seed 2 | the hash function. 3 | */ 4 | 5 | #ifdef HAVE_CONFIG_H 6 | #include 7 | #endif 8 | 9 | #include 10 | #include 11 | 12 | #ifdef HAVE_STDINT_H 13 | #include 14 | #endif 15 | 16 | #ifdef HAVE_FCNTL_H 17 | #include 18 | #endif 19 | 20 | #ifdef HAVE_SCHED_H 21 | #include 22 | #endif 23 | 24 | #ifdef HAVE_UNISTD_H 25 | #include 26 | #endif 27 | 28 | #ifdef HAVE_SYS_STAT_H 29 | #include 30 | #endif 31 | 32 | #ifdef HAVE_SYS_TIME_H 33 | #include 34 | #endif 35 | 36 | #ifdef HAVE_SYS_TYPES_H 37 | #include 38 | #endif 39 | 40 | #if defined(_WIN32) 41 | /* For GetModuleHandle(), GetProcAddress() and GetCurrentProcessId() */ 42 | #include 43 | #endif 44 | 45 | #include "jansson.h" 46 | 47 | 48 | static uint32_t buf_to_uint32(char *data) { 49 | size_t i; 50 | uint32_t result = 0; 51 | 52 | for (i = 0; i < sizeof(uint32_t); i++) 53 | result = (result << 8) | (unsigned char)data[i]; 54 | 55 | return result; 56 | } 57 | 58 | 59 | 60 | /* /dev/urandom */ 61 | #if !defined(_WIN32) && defined(USE_URANDOM) 62 | static int seed_from_urandom(uint32_t *seed) { 63 | /* Use unbuffered I/O if we have open(), close() and read(). Otherwise 64 | fall back to fopen() */ 65 | 66 | char data[sizeof(uint32_t)]; 67 | int ok; 68 | 69 | #if defined(HAVE_OPEN) && defined(HAVE_CLOSE) && defined(HAVE_READ) 70 | int urandom; 71 | urandom = open("/dev/urandom", O_RDONLY); 72 | if (urandom == -1) 73 | return 1; 74 | 75 | ok = read(urandom, data, sizeof(uint32_t)) == sizeof(uint32_t); 76 | close(urandom); 77 | #else 78 | FILE *urandom; 79 | 80 | urandom = fopen("/dev/urandom", "rb"); 81 | if (!urandom) 82 | return 1; 83 | 84 | ok = fread(data, 1, sizeof(uint32_t), urandom) == sizeof(uint32_t); 85 | fclose(urandom); 86 | #endif 87 | 88 | if (!ok) 89 | return 1; 90 | 91 | *seed = buf_to_uint32(data); 92 | return 0; 93 | } 94 | #endif 95 | 96 | /* Windows Crypto API */ 97 | #if defined(_WIN32) && defined(USE_WINDOWS_CRYPTOAPI) 98 | #include 99 | 100 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv, LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags); 101 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer); 102 | typedef BOOL (WINAPI *CRYPTRELEASECONTEXT)(HCRYPTPROV hProv, DWORD dwFlags); 103 | 104 | static int seed_from_windows_cryptoapi(uint32_t *seed) 105 | { 106 | HINSTANCE hAdvAPI32 = NULL; 107 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; 108 | CRYPTGENRANDOM pCryptGenRandom = NULL; 109 | CRYPTRELEASECONTEXT pCryptReleaseContext = NULL; 110 | HCRYPTPROV hCryptProv = 0; 111 | BYTE data[sizeof(uint32_t)]; 112 | int ok; 113 | 114 | hAdvAPI32 = GetModuleHandle(TEXT("advapi32.dll")); 115 | if(hAdvAPI32 == NULL) 116 | return 1; 117 | 118 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(hAdvAPI32, "CryptAcquireContextA"); 119 | if (!pCryptAcquireContext) 120 | return 1; 121 | 122 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(hAdvAPI32, "CryptGenRandom"); 123 | if (!pCryptGenRandom) 124 | return 1; 125 | 126 | pCryptReleaseContext = (CRYPTRELEASECONTEXT)GetProcAddress(hAdvAPI32, "CryptReleaseContext"); 127 | if (!pCryptReleaseContext) 128 | return 1; 129 | 130 | if (!pCryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) 131 | return 1; 132 | 133 | ok = pCryptGenRandom(hCryptProv, sizeof(uint32_t), data); 134 | pCryptReleaseContext(hCryptProv, 0); 135 | 136 | if (!ok) 137 | return 1; 138 | 139 | *seed = buf_to_uint32((char *)data); 140 | return 0; 141 | } 142 | #endif 143 | 144 | /* gettimeofday() and getpid() */ 145 | static int seed_from_timestamp_and_pid(uint32_t *seed) { 146 | #ifdef HAVE_GETTIMEOFDAY 147 | /* XOR of seconds and microseconds */ 148 | struct timeval tv; 149 | gettimeofday(&tv, NULL); 150 | *seed = (uint32_t)tv.tv_sec ^ (uint32_t)tv.tv_usec; 151 | #else 152 | /* Seconds only */ 153 | *seed = (uint32_t)time(NULL); 154 | #endif 155 | 156 | /* XOR with PID for more randomness */ 157 | #if defined(_WIN32) 158 | *seed ^= (uint32_t)GetCurrentProcessId(); 159 | #elif defined(HAVE_GETPID) 160 | *seed ^= (uint32_t)getpid(); 161 | #endif 162 | 163 | return 0; 164 | } 165 | 166 | static uint32_t generate_seed() { 167 | uint32_t seed; 168 | int done = 0; 169 | 170 | #if !defined(_WIN32) && defined(USE_URANDOM) 171 | if (!done && seed_from_urandom(&seed) == 0) 172 | done = 1; 173 | #endif 174 | 175 | #if defined(_WIN32) && defined(USE_WINDOWS_CRYPTOAPI) 176 | if (!done && seed_from_windows_cryptoapi(&seed) == 0) 177 | done = 1; 178 | #endif 179 | 180 | if (!done) { 181 | /* Fall back to timestamp and PID if no better randomness is 182 | available */ 183 | seed_from_timestamp_and_pid(&seed); 184 | } 185 | 186 | /* Make sure the seed is never zero */ 187 | if (seed == 0) 188 | seed = 1; 189 | 190 | return seed; 191 | } 192 | 193 | 194 | volatile uint32_t hashtable_seed = 0; 195 | 196 | #if defined(HAVE_ATOMIC_BUILTINS) && (defined(HAVE_SCHED_YIELD) || !defined(_WIN32)) 197 | static volatile char seed_initialized = 0; 198 | 199 | void json_object_seed(size_t seed) { 200 | uint32_t new_seed = (uint32_t)seed; 201 | 202 | if (hashtable_seed == 0) { 203 | if (__atomic_test_and_set(&seed_initialized, __ATOMIC_RELAXED) == 0) { 204 | /* Do the seeding ourselves */ 205 | if (new_seed == 0) 206 | new_seed = generate_seed(); 207 | 208 | __atomic_store_n(&hashtable_seed, new_seed, __ATOMIC_RELEASE); 209 | } else { 210 | /* Wait for another thread to do the seeding */ 211 | do { 212 | #ifdef HAVE_SCHED_YIELD 213 | sched_yield(); 214 | #endif 215 | } while(__atomic_load_n(&hashtable_seed, __ATOMIC_ACQUIRE) == 0); 216 | } 217 | } 218 | } 219 | #elif defined(HAVE_SYNC_BUILTINS) && (defined(HAVE_SCHED_YIELD) || !defined(_WIN32)) 220 | void json_object_seed(size_t seed) { 221 | uint32_t new_seed = (uint32_t)seed; 222 | 223 | if (hashtable_seed == 0) { 224 | if (new_seed == 0) { 225 | /* Explicit synchronization fences are not supported by the 226 | __sync builtins, so every thread getting here has to 227 | generate the seed value. 228 | */ 229 | new_seed = generate_seed(); 230 | } 231 | 232 | do { 233 | if (__sync_bool_compare_and_swap(&hashtable_seed, 0, new_seed)) { 234 | /* We were the first to seed */ 235 | break; 236 | } else { 237 | /* Wait for another thread to do the seeding */ 238 | #ifdef HAVE_SCHED_YIELD 239 | sched_yield(); 240 | #endif 241 | } 242 | } while(hashtable_seed == 0); 243 | } 244 | } 245 | #elif defined(_WIN32) 246 | static long seed_initialized = 0; 247 | void json_object_seed(size_t seed) { 248 | uint32_t new_seed = (uint32_t)seed; 249 | 250 | if (hashtable_seed == 0) { 251 | if (InterlockedIncrement(&seed_initialized) == 1) { 252 | /* Do the seeding ourselves */ 253 | if (new_seed == 0) 254 | new_seed = generate_seed(); 255 | 256 | hashtable_seed = new_seed; 257 | } else { 258 | /* Wait for another thread to do the seeding */ 259 | do { 260 | SwitchToThread(); 261 | } while (hashtable_seed == 0); 262 | } 263 | } 264 | } 265 | #else 266 | /* Fall back to a thread-unsafe version */ 267 | void json_object_seed(size_t seed) { 268 | uint32_t new_seed = (uint32_t)seed; 269 | 270 | if (hashtable_seed == 0) { 271 | if (new_seed == 0) 272 | new_seed = generate_seed(); 273 | 274 | hashtable_seed = new_seed; 275 | } 276 | } 277 | #endif 278 | -------------------------------------------------------------------------------- /elist.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2016-2017 XMRig 8 | * 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef _LINUX_LIST_H 25 | #define _LINUX_LIST_H 26 | 27 | /* 28 | * Simple doubly linked list implementation. 29 | * 30 | * Some of the internal functions ("__xxx") are useful when 31 | * manipulating whole lists rather than single entries, as 32 | * sometimes we already know the next/prev entries and we can 33 | * generate better code by using them directly rather than 34 | * using the generic single-entry routines. 35 | */ 36 | 37 | struct list_head { 38 | struct list_head *next, *prev; 39 | }; 40 | 41 | #define LIST_HEAD_INIT(name) { &(name), &(name) } 42 | 43 | #define LIST_HEAD(name) \ 44 | struct list_head name = LIST_HEAD_INIT(name) 45 | 46 | #define INIT_LIST_HEAD(ptr) do { \ 47 | (ptr)->next = (ptr); (ptr)->prev = (ptr); \ 48 | } while (0) 49 | 50 | /* 51 | * Insert a new entry between two known consecutive entries. 52 | * 53 | * This is only for internal list manipulation where we know 54 | * the prev/next entries already! 55 | */ 56 | static inline void __list_add(struct list_head *new, 57 | struct list_head *prev, 58 | struct list_head *next) 59 | { 60 | next->prev = new; 61 | new->next = next; 62 | new->prev = prev; 63 | prev->next = new; 64 | } 65 | 66 | /** 67 | * list_add - add a new entry 68 | * @new: new entry to be added 69 | * @head: list head to add it after 70 | * 71 | * Insert a new entry after the specified head. 72 | * This is good for implementing stacks. 73 | */ 74 | static inline void list_add(struct list_head *new, struct list_head *head) 75 | { 76 | __list_add(new, head, head->next); 77 | } 78 | 79 | /** 80 | * list_add_tail - add a new entry 81 | * @new: new entry to be added 82 | * @head: list head to add it before 83 | * 84 | * Insert a new entry before the specified head. 85 | * This is useful for implementing queues. 86 | */ 87 | static inline void list_add_tail(struct list_head *new, struct list_head *head) 88 | { 89 | __list_add(new, head->prev, head); 90 | } 91 | 92 | /* 93 | * Delete a list entry by making the prev/next entries 94 | * point to each other. 95 | * 96 | * This is only for internal list manipulation where we know 97 | * the prev/next entries already! 98 | */ 99 | static inline void __list_del(struct list_head *prev, struct list_head *next) 100 | { 101 | next->prev = prev; 102 | prev->next = next; 103 | } 104 | 105 | /** 106 | * list_del - deletes entry from list. 107 | * @entry: the element to delete from the list. 108 | * Note: list_empty on entry does not return true after this, the entry is in an undefined state. 109 | */ 110 | static inline void list_del(struct list_head *entry) 111 | { 112 | __list_del(entry->prev, entry->next); 113 | entry->next = (void *) 0; 114 | entry->prev = (void *) 0; 115 | } 116 | 117 | /** 118 | * list_del_init - deletes entry from list and reinitialize it. 119 | * @entry: the element to delete from the list. 120 | */ 121 | static inline void list_del_init(struct list_head *entry) 122 | { 123 | __list_del(entry->prev, entry->next); 124 | INIT_LIST_HEAD(entry); 125 | } 126 | 127 | /** 128 | * list_move - delete from one list and add as another's head 129 | * @list: the entry to move 130 | * @head: the head that will precede our entry 131 | */ 132 | static inline void list_move(struct list_head *list, struct list_head *head) 133 | { 134 | __list_del(list->prev, list->next); 135 | list_add(list, head); 136 | } 137 | 138 | /** 139 | * list_move_tail - delete from one list and add as another's tail 140 | * @list: the entry to move 141 | * @head: the head that will follow our entry 142 | */ 143 | static inline void list_move_tail(struct list_head *list, 144 | struct list_head *head) 145 | { 146 | __list_del(list->prev, list->next); 147 | list_add_tail(list, head); 148 | } 149 | 150 | /** 151 | * list_empty - tests whether a list is empty 152 | * @head: the list to test. 153 | */ 154 | static inline int list_empty(struct list_head *head) 155 | { 156 | return head->next == head; 157 | } 158 | 159 | static inline void __list_splice(struct list_head *list, 160 | struct list_head *head) 161 | { 162 | struct list_head *first = list->next; 163 | struct list_head *last = list->prev; 164 | struct list_head *at = head->next; 165 | 166 | first->prev = head; 167 | head->next = first; 168 | 169 | last->next = at; 170 | at->prev = last; 171 | } 172 | 173 | /** 174 | * list_splice - join two lists 175 | * @list: the new list to add. 176 | * @head: the place to add it in the first list. 177 | */ 178 | static inline void list_splice(struct list_head *list, struct list_head *head) 179 | { 180 | if (!list_empty(list)) 181 | __list_splice(list, head); 182 | } 183 | 184 | /** 185 | * list_splice_init - join two lists and reinitialise the emptied list. 186 | * @list: the new list to add. 187 | * @head: the place to add it in the first list. 188 | * 189 | * The list at @list is reinitialised 190 | */ 191 | static inline void list_splice_init(struct list_head *list, 192 | struct list_head *head) 193 | { 194 | if (!list_empty(list)) { 195 | __list_splice(list, head); 196 | INIT_LIST_HEAD(list); 197 | } 198 | } 199 | 200 | /** 201 | * list_entry - get the struct for this entry 202 | * @ptr: the &struct list_head pointer. 203 | * @type: the type of the struct this is embedded in. 204 | * @member: the name of the list_struct within the struct. 205 | */ 206 | #define list_entry(ptr, type, member) \ 207 | ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) 208 | 209 | /** 210 | * list_for_each - iterate over a list 211 | * @pos: the &struct list_head to use as a loop counter. 212 | * @head: the head for your list. 213 | */ 214 | #define list_for_each(pos, head) \ 215 | for (pos = (head)->next; pos != (head); \ 216 | pos = pos->next) 217 | /** 218 | * list_for_each_prev - iterate over a list backwards 219 | * @pos: the &struct list_head to use as a loop counter. 220 | * @head: the head for your list. 221 | */ 222 | #define list_for_each_prev(pos, head) \ 223 | for (pos = (head)->prev; pos != (head); \ 224 | pos = pos->prev) 225 | 226 | /** 227 | * list_for_each_safe - iterate over a list safe against removal of list entry 228 | * @pos: the &struct list_head to use as a loop counter. 229 | * @n: another &struct list_head to use as temporary storage 230 | * @head: the head for your list. 231 | */ 232 | #define list_for_each_safe(pos, n, head) \ 233 | for (pos = (head)->next, n = pos->next; pos != (head); \ 234 | pos = n, n = pos->next) 235 | 236 | /** 237 | * list_for_each_entry - iterate over list of given type 238 | * @pos: the type * to use as a loop counter. 239 | * @head: the head for your list. 240 | * @member: the name of the list_struct within the struct. 241 | */ 242 | #define list_for_each_entry(pos, head, member) \ 243 | for (pos = list_entry((head)->next, typeof(*pos), member); \ 244 | &pos->member != (head); \ 245 | pos = list_entry(pos->member.next, typeof(*pos), member)) 246 | 247 | /** 248 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry 249 | * @pos: the type * to use as a loop counter. 250 | * @n: another type * to use as temporary storage 251 | * @head: the head for your list. 252 | * @member: the name of the list_struct within the struct. 253 | */ 254 | #define list_for_each_entry_safe(pos, n, head, member) \ 255 | for (pos = list_entry((head)->next, typeof(*pos), member), \ 256 | n = list_entry(pos->member.next, typeof(*pos), member); \ 257 | &pos->member != (head); \ 258 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) 259 | 260 | /** 261 | * list_for_each_entry_continue - iterate over list of given type 262 | * continuing after existing point 263 | * @pos: the type * to use as a loop counter. 264 | * @head: the head for your list. 265 | * @member: the name of the list_struct within the struct. 266 | */ 267 | #define list_for_each_entry_continue(pos, head, member) \ 268 | for (pos = list_entry(pos->member.next, typeof(*pos), member), \ 269 | prefetch(pos->member.next); \ 270 | &pos->member != (head); \ 271 | pos = list_entry(pos->member.next, typeof(*pos), member), \ 272 | prefetch(pos->member.next)) 273 | 274 | #endif 275 | -------------------------------------------------------------------------------- /algo/cryptonight/cryptonight_softaes.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2017 fireice-uk 8 | * Copyright 2016-2017 XMRig 9 | * 10 | * 11 | * This program is free software: you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License as published by 13 | * the Free Software Foundation, either version 3 of the License, or 14 | * (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License 22 | * along with this program. If not, see . 23 | */ 24 | 25 | #ifndef __CRYPTONIGHT_SOFTAES_H__ 26 | #define __CRYPTONIGHT_SOFTAES_H__ 27 | 28 | #include 29 | 30 | extern __m128i soft_aesenc(__m128i in, __m128i key); 31 | extern __m128i soft_aeskeygenassist(__m128i key, uint8_t rcon); 32 | 33 | 34 | // This will shift and xor tmp1 into itself as 4 32-bit vals such as 35 | // sl_xor(a1 a2 a3 a4) = a1 (a2^a1) (a3^a2^a1) (a4^a3^a2^a1) 36 | inline __m128i sl_xor(__m128i tmp1) 37 | { 38 | __m128i tmp4; 39 | tmp4 = _mm_slli_si128(tmp1, 0x04); 40 | tmp1 = _mm_xor_si128(tmp1, tmp4); 41 | tmp4 = _mm_slli_si128(tmp4, 0x04); 42 | tmp1 = _mm_xor_si128(tmp1, tmp4); 43 | tmp4 = _mm_slli_si128(tmp4, 0x04); 44 | tmp1 = _mm_xor_si128(tmp1, tmp4); 45 | return tmp1; 46 | } 47 | 48 | 49 | inline void aes_genkey_sub(__m128i* xout0, __m128i* xout2, uint8_t rcon) 50 | { 51 | __m128i xout1 = soft_aeskeygenassist(*xout2, rcon); 52 | xout1 = _mm_shuffle_epi32(xout1, 0xFF); // see PSHUFD, set all elems to 4th elem 53 | *xout0 = sl_xor(*xout0); 54 | *xout0 = _mm_xor_si128(*xout0, xout1); 55 | xout1 = soft_aeskeygenassist(*xout0, 0x00); 56 | xout1 = _mm_shuffle_epi32(xout1, 0xAA); // see PSHUFD, set all elems to 3rd elem 57 | *xout2 = sl_xor(*xout2); 58 | *xout2 = _mm_xor_si128(*xout2, xout1); 59 | } 60 | 61 | 62 | inline void aes_round(__m128i key, __m128i* x0, __m128i* x1, __m128i* x2, __m128i* x3, __m128i* x4, __m128i* x5, __m128i* x6, __m128i* x7) 63 | { 64 | *x0 = soft_aesenc(*x0, key); 65 | *x1 = soft_aesenc(*x1, key); 66 | *x2 = soft_aesenc(*x2, key); 67 | *x3 = soft_aesenc(*x3, key); 68 | *x4 = soft_aesenc(*x4, key); 69 | *x5 = soft_aesenc(*x5, key); 70 | *x6 = soft_aesenc(*x6, key); 71 | *x7 = soft_aesenc(*x7, key); 72 | } 73 | 74 | 75 | inline void aes_genkey(const __m128i* memory, __m128i* k0, __m128i* k1, __m128i* k2, __m128i* k3, __m128i* k4, __m128i* k5, __m128i* k6, __m128i* k7, __m128i* k8, __m128i* k9) 76 | { 77 | __m128i xout0 = _mm_load_si128(memory); 78 | __m128i xout2 = _mm_load_si128(memory + 1); 79 | *k0 = xout0; 80 | *k1 = xout2; 81 | 82 | aes_genkey_sub(&xout0, &xout2, 0x1); 83 | *k2 = xout0; 84 | *k3 = xout2; 85 | 86 | aes_genkey_sub(&xout0, &xout2, 0x2); 87 | *k4 = xout0; 88 | *k5 = xout2; 89 | 90 | aes_genkey_sub(&xout0, &xout2, 0x4); 91 | *k6 = xout0; 92 | *k7 = xout2; 93 | 94 | aes_genkey_sub(&xout0, &xout2, 0x8); 95 | *k8 = xout0; 96 | *k9 = xout2; 97 | } 98 | 99 | 100 | inline void cn_explode_scratchpad(const __m128i* input, __m128i* output) 101 | { 102 | // This is more than we have registers, compiler will assign 2 keys on the stack 103 | __m128i xin0, xin1, xin2, xin3, xin4, xin5, xin6, xin7; 104 | __m128i k0, k1, k2, k3, k4, k5, k6, k7, k8, k9; 105 | 106 | aes_genkey(input, &k0, &k1, &k2, &k3, &k4, &k5, &k6, &k7, &k8, &k9); 107 | 108 | xin0 = _mm_load_si128(input + 4); 109 | xin1 = _mm_load_si128(input + 5); 110 | xin2 = _mm_load_si128(input + 6); 111 | xin3 = _mm_load_si128(input + 7); 112 | xin4 = _mm_load_si128(input + 8); 113 | xin5 = _mm_load_si128(input + 9); 114 | xin6 = _mm_load_si128(input + 10); 115 | xin7 = _mm_load_si128(input + 11); 116 | 117 | for (size_t i = 0; i < MEMORY_LITE / sizeof(__m128i); i += 8) { 118 | aes_round(k0, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 119 | aes_round(k1, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 120 | aes_round(k2, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 121 | aes_round(k3, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 122 | aes_round(k4, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 123 | aes_round(k5, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 124 | aes_round(k6, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 125 | aes_round(k7, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 126 | aes_round(k8, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 127 | aes_round(k9, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 128 | 129 | _mm_store_si128(output + i + 0, xin0); 130 | _mm_store_si128(output + i + 1, xin1); 131 | _mm_store_si128(output + i + 2, xin2); 132 | _mm_store_si128(output + i + 3, xin3); 133 | _mm_store_si128(output + i + 4, xin4); 134 | _mm_store_si128(output + i + 5, xin5); 135 | _mm_store_si128(output + i + 6, xin6); 136 | _mm_store_si128(output + i + 7, xin7); 137 | } 138 | } 139 | 140 | 141 | inline void cn_implode_scratchpad(const __m128i* input, __m128i* output) 142 | { 143 | // This is more than we have registers, compiler will assign 2 keys on the stack 144 | __m128i xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7; 145 | __m128i k0, k1, k2, k3, k4, k5, k6, k7, k8, k9; 146 | 147 | aes_genkey(output + 2, &k0, &k1, &k2, &k3, &k4, &k5, &k6, &k7, &k8, &k9); 148 | 149 | xout0 = _mm_load_si128(output + 4); 150 | xout1 = _mm_load_si128(output + 5); 151 | xout2 = _mm_load_si128(output + 6); 152 | xout3 = _mm_load_si128(output + 7); 153 | xout4 = _mm_load_si128(output + 8); 154 | xout5 = _mm_load_si128(output + 9); 155 | xout6 = _mm_load_si128(output + 10); 156 | xout7 = _mm_load_si128(output + 11); 157 | 158 | for (size_t i = 0; __builtin_expect(i < MEMORY_LITE / sizeof(__m128i), 1); i += 8) 159 | { 160 | xout0 = _mm_xor_si128(_mm_load_si128(input + i + 0), xout0); 161 | xout1 = _mm_xor_si128(_mm_load_si128(input + i + 1), xout1); 162 | xout2 = _mm_xor_si128(_mm_load_si128(input + i + 2), xout2); 163 | xout3 = _mm_xor_si128(_mm_load_si128(input + i + 3), xout3); 164 | xout4 = _mm_xor_si128(_mm_load_si128(input + i + 4), xout4); 165 | xout5 = _mm_xor_si128(_mm_load_si128(input + i + 5), xout5); 166 | xout6 = _mm_xor_si128(_mm_load_si128(input + i + 6), xout6); 167 | xout7 = _mm_xor_si128(_mm_load_si128(input + i + 7), xout7); 168 | 169 | aes_round(k0, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 170 | aes_round(k1, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 171 | aes_round(k2, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 172 | aes_round(k3, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 173 | aes_round(k4, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 174 | aes_round(k5, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 175 | aes_round(k6, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 176 | aes_round(k7, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 177 | aes_round(k8, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 178 | aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 179 | } 180 | 181 | _mm_store_si128(output + 4, xout0); 182 | _mm_store_si128(output + 5, xout1); 183 | _mm_store_si128(output + 6, xout2); 184 | _mm_store_si128(output + 7, xout3); 185 | _mm_store_si128(output + 8, xout4); 186 | _mm_store_si128(output + 9, xout5); 187 | _mm_store_si128(output + 10, xout6); 188 | _mm_store_si128(output + 11, xout7); 189 | } 190 | 191 | 192 | #if defined(__x86_64__) 193 | # define EXTRACT64(X) _mm_cvtsi128_si64(X) 194 | 195 | inline uint64_t _umul128(uint64_t a, uint64_t b, uint64_t* hi) 196 | { 197 | unsigned __int128 r = (unsigned __int128) a * (unsigned __int128) b; 198 | *hi = r >> 64; 199 | return (uint64_t) r; 200 | } 201 | #elif defined(__i386__) 202 | # define HI32(X) \ 203 | _mm_srli_si128((X), 4) 204 | 205 | 206 | # define EXTRACT64(X) \ 207 | ((uint64_t)(uint32_t)_mm_cvtsi128_si32(X) | \ 208 | ((uint64_t)(uint32_t)_mm_cvtsi128_si32(HI32(X)) << 32)) 209 | 210 | inline uint64_t _umul128(uint64_t multiplier, uint64_t multiplicand, uint64_t *product_hi) { 211 | // multiplier = ab = a * 2^32 + b 212 | // multiplicand = cd = c * 2^32 + d 213 | // ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d 214 | uint64_t a = multiplier >> 32; 215 | uint64_t b = multiplier & 0xFFFFFFFF; 216 | uint64_t c = multiplicand >> 32; 217 | uint64_t d = multiplicand & 0xFFFFFFFF; 218 | 219 | //uint64_t ac = a * c; 220 | uint64_t ad = a * d; 221 | //uint64_t bc = b * c; 222 | uint64_t bd = b * d; 223 | 224 | uint64_t adbc = ad + (b * c); 225 | uint64_t adbc_carry = adbc < ad ? 1 : 0; 226 | 227 | // multiplier * multiplicand = product_hi * 2^64 + product_lo 228 | uint64_t product_lo = bd + (adbc << 32); 229 | uint64_t product_lo_carry = product_lo < bd ? 1 : 0; 230 | *product_hi = (a * c) + (adbc >> 32) + (adbc_carry << 32) + product_lo_carry; 231 | 232 | return product_lo; 233 | } 234 | #endif 235 | 236 | 237 | #endif /* __CRYPTONIGHT_SOFTAES_H__ */ 238 | -------------------------------------------------------------------------------- /compat/jansson/hashtable.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2016 Petri Lehtinen 3 | * 4 | * This library is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #if HAVE_CONFIG_H 9 | #include 10 | #endif 11 | 12 | #include 13 | #include 14 | 15 | #if HAVE_STDINT_H 16 | #include 17 | #endif 18 | 19 | #include /* for JSON_INLINE */ 20 | #include "jansson_private.h" /* for container_of() */ 21 | #include "hashtable.h" 22 | 23 | #ifndef INITIAL_HASHTABLE_ORDER 24 | #define INITIAL_HASHTABLE_ORDER 3 25 | #endif 26 | 27 | typedef struct hashtable_list list_t; 28 | typedef struct hashtable_pair pair_t; 29 | typedef struct hashtable_bucket bucket_t; 30 | 31 | extern volatile uint32_t hashtable_seed; 32 | 33 | /* Implementation of the hash function */ 34 | #include "lookup3.h" 35 | 36 | #define list_to_pair(list_) container_of(list_, pair_t, list) 37 | #define ordered_list_to_pair(list_) container_of(list_, pair_t, ordered_list) 38 | #define hash_str(key) ((size_t)hashlittle((key), strlen(key), hashtable_seed)) 39 | 40 | static JSON_INLINE void list_init(list_t *list) 41 | { 42 | list->next = list; 43 | list->prev = list; 44 | } 45 | 46 | static JSON_INLINE void list_insert(list_t *list, list_t *node) 47 | { 48 | node->next = list; 49 | node->prev = list->prev; 50 | list->prev->next = node; 51 | list->prev = node; 52 | } 53 | 54 | static JSON_INLINE void list_remove(list_t *list) 55 | { 56 | list->prev->next = list->next; 57 | list->next->prev = list->prev; 58 | } 59 | 60 | static JSON_INLINE int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket) 61 | { 62 | return bucket->first == &hashtable->list && bucket->first == bucket->last; 63 | } 64 | 65 | static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket, 66 | list_t *list) 67 | { 68 | if(bucket_is_empty(hashtable, bucket)) 69 | { 70 | list_insert(&hashtable->list, list); 71 | bucket->first = bucket->last = list; 72 | } 73 | else 74 | { 75 | list_insert(bucket->first, list); 76 | bucket->first = list; 77 | } 78 | } 79 | 80 | static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket, 81 | const char *key, size_t hash) 82 | { 83 | list_t *list; 84 | pair_t *pair; 85 | 86 | if(bucket_is_empty(hashtable, bucket)) 87 | return NULL; 88 | 89 | list = bucket->first; 90 | while(1) 91 | { 92 | pair = list_to_pair(list); 93 | if(pair->hash == hash && strcmp(pair->key, key) == 0) 94 | return pair; 95 | 96 | if(list == bucket->last) 97 | break; 98 | 99 | list = list->next; 100 | } 101 | 102 | return NULL; 103 | } 104 | 105 | /* returns 0 on success, -1 if key was not found */ 106 | static int hashtable_do_del(hashtable_t *hashtable, 107 | const char *key, size_t hash) 108 | { 109 | pair_t *pair; 110 | bucket_t *bucket; 111 | size_t index; 112 | 113 | index = hash & hashmask(hashtable->order); 114 | bucket = &hashtable->buckets[index]; 115 | 116 | pair = hashtable_find_pair(hashtable, bucket, key, hash); 117 | if(!pair) 118 | return -1; 119 | 120 | if(&pair->list == bucket->first && &pair->list == bucket->last) 121 | bucket->first = bucket->last = &hashtable->list; 122 | 123 | else if(&pair->list == bucket->first) 124 | bucket->first = pair->list.next; 125 | 126 | else if(&pair->list == bucket->last) 127 | bucket->last = pair->list.prev; 128 | 129 | list_remove(&pair->list); 130 | list_remove(&pair->ordered_list); 131 | json_decref(pair->value); 132 | 133 | jsonp_free(pair); 134 | hashtable->size--; 135 | 136 | return 0; 137 | } 138 | 139 | static void hashtable_do_clear(hashtable_t *hashtable) 140 | { 141 | list_t *list, *next; 142 | pair_t *pair; 143 | 144 | for(list = hashtable->list.next; list != &hashtable->list; list = next) 145 | { 146 | next = list->next; 147 | pair = list_to_pair(list); 148 | json_decref(pair->value); 149 | jsonp_free(pair); 150 | } 151 | } 152 | 153 | static int hashtable_do_rehash(hashtable_t *hashtable) 154 | { 155 | list_t *list, *next; 156 | pair_t *pair; 157 | size_t i, index, new_size, new_order; 158 | struct hashtable_bucket *new_buckets; 159 | 160 | new_order = hashtable->order + 1; 161 | new_size = hashsize(new_order); 162 | 163 | new_buckets = jsonp_malloc(new_size * sizeof(bucket_t)); 164 | if(!new_buckets) 165 | return -1; 166 | 167 | jsonp_free(hashtable->buckets); 168 | hashtable->buckets = new_buckets; 169 | hashtable->order = new_order; 170 | 171 | for(i = 0; i < hashsize(hashtable->order); i++) 172 | { 173 | hashtable->buckets[i].first = hashtable->buckets[i].last = 174 | &hashtable->list; 175 | } 176 | 177 | list = hashtable->list.next; 178 | list_init(&hashtable->list); 179 | 180 | for(; list != &hashtable->list; list = next) { 181 | next = list->next; 182 | pair = list_to_pair(list); 183 | index = pair->hash % new_size; 184 | insert_to_bucket(hashtable, &hashtable->buckets[index], &pair->list); 185 | } 186 | 187 | return 0; 188 | } 189 | 190 | 191 | int hashtable_init(hashtable_t *hashtable) 192 | { 193 | size_t i; 194 | 195 | hashtable->size = 0; 196 | hashtable->order = INITIAL_HASHTABLE_ORDER; 197 | hashtable->buckets = jsonp_malloc(hashsize(hashtable->order) * sizeof(bucket_t)); 198 | if(!hashtable->buckets) 199 | return -1; 200 | 201 | list_init(&hashtable->list); 202 | list_init(&hashtable->ordered_list); 203 | 204 | for(i = 0; i < hashsize(hashtable->order); i++) 205 | { 206 | hashtable->buckets[i].first = hashtable->buckets[i].last = 207 | &hashtable->list; 208 | } 209 | 210 | return 0; 211 | } 212 | 213 | void hashtable_close(hashtable_t *hashtable) 214 | { 215 | hashtable_do_clear(hashtable); 216 | jsonp_free(hashtable->buckets); 217 | } 218 | 219 | int hashtable_set(hashtable_t *hashtable, const char *key, json_t *value) 220 | { 221 | pair_t *pair; 222 | bucket_t *bucket; 223 | size_t hash, index; 224 | 225 | /* rehash if the load ratio exceeds 1 */ 226 | if(hashtable->size >= hashsize(hashtable->order)) 227 | if(hashtable_do_rehash(hashtable)) 228 | return -1; 229 | 230 | hash = hash_str(key); 231 | index = hash & hashmask(hashtable->order); 232 | bucket = &hashtable->buckets[index]; 233 | pair = hashtable_find_pair(hashtable, bucket, key, hash); 234 | 235 | if(pair) 236 | { 237 | json_decref(pair->value); 238 | pair->value = value; 239 | } 240 | else 241 | { 242 | /* offsetof(...) returns the size of pair_t without the last, 243 | flexible member. This way, the correct amount is 244 | allocated. */ 245 | 246 | size_t len = strlen(key); 247 | if(len >= (size_t)-1 - offsetof(pair_t, key)) { 248 | /* Avoid an overflow if the key is very long */ 249 | return -1; 250 | } 251 | 252 | pair = jsonp_malloc(offsetof(pair_t, key) + len + 1); 253 | if(!pair) 254 | return -1; 255 | 256 | pair->hash = hash; 257 | strncpy(pair->key, key, len + 1); 258 | pair->value = value; 259 | list_init(&pair->list); 260 | list_init(&pair->ordered_list); 261 | 262 | insert_to_bucket(hashtable, bucket, &pair->list); 263 | list_insert(&hashtable->ordered_list, &pair->ordered_list); 264 | 265 | hashtable->size++; 266 | } 267 | return 0; 268 | } 269 | 270 | void *hashtable_get(hashtable_t *hashtable, const char *key) 271 | { 272 | pair_t *pair; 273 | size_t hash; 274 | bucket_t *bucket; 275 | 276 | hash = hash_str(key); 277 | bucket = &hashtable->buckets[hash & hashmask(hashtable->order)]; 278 | 279 | pair = hashtable_find_pair(hashtable, bucket, key, hash); 280 | if(!pair) 281 | return NULL; 282 | 283 | return pair->value; 284 | } 285 | 286 | int hashtable_del(hashtable_t *hashtable, const char *key) 287 | { 288 | size_t hash = hash_str(key); 289 | return hashtable_do_del(hashtable, key, hash); 290 | } 291 | 292 | void hashtable_clear(hashtable_t *hashtable) 293 | { 294 | size_t i; 295 | 296 | hashtable_do_clear(hashtable); 297 | 298 | for(i = 0; i < hashsize(hashtable->order); i++) 299 | { 300 | hashtable->buckets[i].first = hashtable->buckets[i].last = 301 | &hashtable->list; 302 | } 303 | 304 | list_init(&hashtable->list); 305 | list_init(&hashtable->ordered_list); 306 | hashtable->size = 0; 307 | } 308 | 309 | void *hashtable_iter(hashtable_t *hashtable) 310 | { 311 | return hashtable_iter_next(hashtable, &hashtable->ordered_list); 312 | } 313 | 314 | void *hashtable_iter_at(hashtable_t *hashtable, const char *key) 315 | { 316 | pair_t *pair; 317 | size_t hash; 318 | bucket_t *bucket; 319 | 320 | hash = hash_str(key); 321 | bucket = &hashtable->buckets[hash & hashmask(hashtable->order)]; 322 | 323 | pair = hashtable_find_pair(hashtable, bucket, key, hash); 324 | if(!pair) 325 | return NULL; 326 | 327 | return &pair->ordered_list; 328 | } 329 | 330 | void *hashtable_iter_next(hashtable_t *hashtable, void *iter) 331 | { 332 | list_t *list = (list_t *)iter; 333 | if(list->next == &hashtable->ordered_list) 334 | return NULL; 335 | return list->next; 336 | } 337 | 338 | void *hashtable_iter_key(void *iter) 339 | { 340 | pair_t *pair = ordered_list_to_pair((list_t *)iter); 341 | return pair->key; 342 | } 343 | 344 | void *hashtable_iter_value(void *iter) 345 | { 346 | pair_t *pair = ordered_list_to_pair((list_t *)iter); 347 | return pair->value; 348 | } 349 | 350 | void hashtable_iter_set(void *iter, json_t *value) 351 | { 352 | pair_t *pair = ordered_list_to_pair((list_t *)iter); 353 | 354 | json_decref(pair->value); 355 | pair->value = value; 356 | } 357 | -------------------------------------------------------------------------------- /algo/cryptonight/cryptonight_aesni.h: -------------------------------------------------------------------------------- 1 | /* XMRig 2 | * Copyright 2010 Jeff Garzik 3 | * Copyright 2012-2014 pooler 4 | * Copyright 2014 Lucas Jones 5 | * Copyright 2014-2016 Wolf9466 6 | * Copyright 2016 Jay D Dee 7 | * Copyright 2017 fireice-uk 8 | * Copyright 2016-2017 XMRig 9 | * 10 | * 11 | * This program is free software: you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License as published by 13 | * the Free Software Foundation, either version 3 of the License, or 14 | * (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License 22 | * along with this program. If not, see . 23 | */ 24 | 25 | #ifndef __CRYPTONIGHT_AESNI_H__ 26 | #define __CRYPTONIGHT_AESNI_H__ 27 | 28 | #include 29 | 30 | 31 | #define aes_genkey_sub(imm8) \ 32 | __m128i xout1 = _mm_aeskeygenassist_si128(*xout2, (imm8)); \ 33 | xout1 = _mm_shuffle_epi32(xout1, 0xFF); \ 34 | *xout0 = sl_xor(*xout0); \ 35 | *xout0 = _mm_xor_si128(*xout0, xout1); \ 36 | xout1 = _mm_aeskeygenassist_si128(*xout0, 0x00);\ 37 | xout1 = _mm_shuffle_epi32(xout1, 0xAA); \ 38 | *xout2 = sl_xor(*xout2); \ 39 | *xout2 = _mm_xor_si128(*xout2, xout1); \ 40 | 41 | 42 | // This will shift and xor tmp1 into itself as 4 32-bit vals such as 43 | // sl_xor(a1 a2 a3 a4) = a1 (a2^a1) (a3^a2^a1) (a4^a3^a2^a1) 44 | inline __m128i sl_xor(__m128i tmp1) 45 | { 46 | __m128i tmp4; 47 | tmp4 = _mm_slli_si128(tmp1, 0x04); 48 | tmp1 = _mm_xor_si128(tmp1, tmp4); 49 | tmp4 = _mm_slli_si128(tmp4, 0x04); 50 | tmp1 = _mm_xor_si128(tmp1, tmp4); 51 | tmp4 = _mm_slli_si128(tmp4, 0x04); 52 | tmp1 = _mm_xor_si128(tmp1, tmp4); 53 | return tmp1; 54 | } 55 | 56 | 57 | inline void aes_genkey_sub1(__m128i* xout0, __m128i* xout2) 58 | { 59 | aes_genkey_sub(0x1) 60 | } 61 | 62 | 63 | inline void aes_genkey_sub2(__m128i* xout0, __m128i* xout2) 64 | { 65 | aes_genkey_sub(0x2) 66 | } 67 | 68 | 69 | inline void aes_genkey_sub4(__m128i* xout0, __m128i* xout2) 70 | { 71 | aes_genkey_sub(0x4) 72 | } 73 | 74 | 75 | inline void aes_genkey_sub8(__m128i* xout0, __m128i* xout2) 76 | { 77 | aes_genkey_sub(0x8) 78 | } 79 | 80 | 81 | inline void aes_round(__m128i key, __m128i* x0, __m128i* x1, __m128i* x2, __m128i* x3, __m128i* x4, __m128i* x5, __m128i* x6, __m128i* x7) 82 | { 83 | *x0 = _mm_aesenc_si128(*x0, key); 84 | *x1 = _mm_aesenc_si128(*x1, key); 85 | *x2 = _mm_aesenc_si128(*x2, key); 86 | *x3 = _mm_aesenc_si128(*x3, key); 87 | *x4 = _mm_aesenc_si128(*x4, key); 88 | *x5 = _mm_aesenc_si128(*x5, key); 89 | *x6 = _mm_aesenc_si128(*x6, key); 90 | *x7 = _mm_aesenc_si128(*x7, key); 91 | } 92 | 93 | 94 | inline void aes_genkey(const __m128i* memory, __m128i* k0, __m128i* k1, __m128i* k2, __m128i* k3, __m128i* k4, __m128i* k5, __m128i* k6, __m128i* k7, __m128i* k8, __m128i* k9) 95 | { 96 | __m128i xout0 = _mm_load_si128(memory); 97 | __m128i xout2 = _mm_load_si128(memory + 1); 98 | *k0 = xout0; 99 | *k1 = xout2; 100 | 101 | aes_genkey_sub1(&xout0, &xout2); 102 | *k2 = xout0; 103 | *k3 = xout2; 104 | 105 | aes_genkey_sub2(&xout0, &xout2); 106 | *k4 = xout0; 107 | *k5 = xout2; 108 | 109 | aes_genkey_sub4(&xout0, &xout2); 110 | *k6 = xout0; 111 | *k7 = xout2; 112 | 113 | aes_genkey_sub8(&xout0, &xout2); 114 | *k8 = xout0; 115 | *k9 = xout2; 116 | } 117 | 118 | 119 | inline void cn_explode_scratchpad(const __m128i* input, __m128i* output) 120 | { 121 | // This is more than we have registers, compiler will assign 2 keys on the stack 122 | __m128i xin0, xin1, xin2, xin3, xin4, xin5, xin6, xin7; 123 | __m128i k0, k1, k2, k3, k4, k5, k6, k7, k8, k9; 124 | 125 | aes_genkey(input, &k0, &k1, &k2, &k3, &k4, &k5, &k6, &k7, &k8, &k9); 126 | 127 | xin0 = _mm_load_si128(input + 4); 128 | xin1 = _mm_load_si128(input + 5); 129 | xin2 = _mm_load_si128(input + 6); 130 | xin3 = _mm_load_si128(input + 7); 131 | xin4 = _mm_load_si128(input + 8); 132 | xin5 = _mm_load_si128(input + 9); 133 | xin6 = _mm_load_si128(input + 10); 134 | xin7 = _mm_load_si128(input + 11); 135 | 136 | for (size_t i = 0; __builtin_expect(i < MEMORY_LITE / sizeof(__m128i), 1); i += 8) { 137 | aes_round(k0, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 138 | aes_round(k1, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 139 | aes_round(k2, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 140 | aes_round(k3, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 141 | aes_round(k4, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 142 | aes_round(k5, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 143 | aes_round(k6, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 144 | aes_round(k7, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 145 | aes_round(k8, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 146 | aes_round(k9, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); 147 | 148 | _mm_store_si128(output + i + 0, xin0); 149 | _mm_store_si128(output + i + 1, xin1); 150 | _mm_store_si128(output + i + 2, xin2); 151 | _mm_store_si128(output + i + 3, xin3); 152 | _mm_store_si128(output + i + 4, xin4); 153 | _mm_store_si128(output + i + 5, xin5); 154 | _mm_store_si128(output + i + 6, xin6); 155 | _mm_store_si128(output + i + 7, xin7); 156 | } 157 | } 158 | 159 | 160 | inline void cn_implode_scratchpad(const __m128i* input, __m128i* output) 161 | { 162 | // This is more than we have registers, compiler will assign 2 keys on the stack 163 | __m128i xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7; 164 | __m128i k0, k1, k2, k3, k4, k5, k6, k7, k8, k9; 165 | 166 | aes_genkey(output + 2, &k0, &k1, &k2, &k3, &k4, &k5, &k6, &k7, &k8, &k9); 167 | 168 | xout0 = _mm_load_si128(output + 4); 169 | xout1 = _mm_load_si128(output + 5); 170 | xout2 = _mm_load_si128(output + 6); 171 | xout3 = _mm_load_si128(output + 7); 172 | xout4 = _mm_load_si128(output + 8); 173 | xout5 = _mm_load_si128(output + 9); 174 | xout6 = _mm_load_si128(output + 10); 175 | xout7 = _mm_load_si128(output + 11); 176 | 177 | for (size_t i = 0; __builtin_expect(i < MEMORY_LITE / sizeof(__m128i), 1); i += 8) 178 | { 179 | xout0 = _mm_xor_si128(_mm_load_si128(input + i + 0), xout0); 180 | xout1 = _mm_xor_si128(_mm_load_si128(input + i + 1), xout1); 181 | xout2 = _mm_xor_si128(_mm_load_si128(input + i + 2), xout2); 182 | xout3 = _mm_xor_si128(_mm_load_si128(input + i + 3), xout3); 183 | xout4 = _mm_xor_si128(_mm_load_si128(input + i + 4), xout4); 184 | xout5 = _mm_xor_si128(_mm_load_si128(input + i + 5), xout5); 185 | xout6 = _mm_xor_si128(_mm_load_si128(input + i + 6), xout6); 186 | xout7 = _mm_xor_si128(_mm_load_si128(input + i + 7), xout7); 187 | 188 | aes_round(k0, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 189 | aes_round(k1, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 190 | aes_round(k2, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 191 | aes_round(k3, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 192 | aes_round(k4, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 193 | aes_round(k5, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 194 | aes_round(k6, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 195 | aes_round(k7, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 196 | aes_round(k8, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 197 | aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); 198 | } 199 | 200 | _mm_store_si128(output + 4, xout0); 201 | _mm_store_si128(output + 5, xout1); 202 | _mm_store_si128(output + 6, xout2); 203 | _mm_store_si128(output + 7, xout3); 204 | _mm_store_si128(output + 8, xout4); 205 | _mm_store_si128(output + 9, xout5); 206 | _mm_store_si128(output + 10, xout6); 207 | _mm_store_si128(output + 11, xout7); 208 | } 209 | 210 | 211 | #if defined(__x86_64__) 212 | # define EXTRACT64(X) _mm_cvtsi128_si64(X) 213 | 214 | inline uint64_t _umul128(uint64_t a, uint64_t b, uint64_t* hi) 215 | { 216 | unsigned __int128 r = (unsigned __int128) a * (unsigned __int128) b; 217 | *hi = r >> 64; 218 | return (uint64_t) r; 219 | } 220 | #elif defined(__i386__) 221 | # define HI32(X) \ 222 | _mm_srli_si128((X), 4) 223 | 224 | 225 | # define EXTRACT64(X) \ 226 | ((uint64_t)(uint32_t)_mm_cvtsi128_si32(X) | \ 227 | ((uint64_t)(uint32_t)_mm_cvtsi128_si32(HI32(X)) << 32)) 228 | 229 | inline uint64_t _umul128(uint64_t multiplier, uint64_t multiplicand, uint64_t *product_hi) { 230 | // multiplier = ab = a * 2^32 + b 231 | // multiplicand = cd = c * 2^32 + d 232 | // ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d 233 | uint64_t a = multiplier >> 32; 234 | uint64_t b = multiplier & 0xFFFFFFFF; 235 | uint64_t c = multiplicand >> 32; 236 | uint64_t d = multiplicand & 0xFFFFFFFF; 237 | 238 | //uint64_t ac = a * c; 239 | uint64_t ad = a * d; 240 | //uint64_t bc = b * c; 241 | uint64_t bd = b * d; 242 | 243 | uint64_t adbc = ad + (b * c); 244 | uint64_t adbc_carry = adbc < ad ? 1 : 0; 245 | 246 | // multiplier * multiplicand = product_hi * 2^64 + product_lo 247 | uint64_t product_lo = bd + (adbc << 32); 248 | uint64_t product_lo_carry = product_lo < bd ? 1 : 0; 249 | *product_hi = (a * c) + (adbc >> 32) + (adbc_carry << 32) + product_lo_carry; 250 | 251 | return product_lo; 252 | } 253 | #endif 254 | 255 | 256 | #endif /* __CRYPTONIGHT_AESNI_H__ */ 257 | --------------------------------------------------------------------------------