Skip to content

Commit

Permalink
Merge #896: [PoW][RandomX] Cherry picked fixes from offical randomx r…
Browse files Browse the repository at this point in the history
…epo - Part 2

7fedd67 Update cherry picked code to match this implementation (codeofalltrades)
ab7897c Cherry picked commit 'Add a missing function to calculate a batch of hashes' from tevador/RandomX@01381cc (codeofalltrades)
9bc9a3d Cherry picked commit 'Fix symbol collisions with blake2b' from tevador/RandomX@7567cef (codeofalltrades)
08d2607 Cherry picked commit 'Fixes for cmake build with visual studio' from tevador/RandomX@01914b4 (codeofalltrades)
8a9c139 Cherry picked commit 'Optimized Argon2 (SSSE3/AVX2)' from tevador/RandomX@900a936 (codeofalltrades)
86aea92 Cherry picked commit 'Fix function names for clang on Apple' from tevador/RandomX@e43267f (codeofalltrades)
ad53bda Cherry picked commit 'virtual_memory: add MAP_JIT on macOS' from tevador/RandomX@7c172f7 (codeofalltrades)
f027b97 Cherry picked commit 'Fix compilation and JIT support on NetBSD:' from tevador/RandomX@708a4e5 (codeofalltrades)
f3d70c3 Cherry picked commit 'fix potential use-after-free when reallocating cache' from tevador/RandomX@32ab5de (codeofalltrades)
2fc4053 Cherry picked commit 'fix test 92 not failing properly on GCC/amd64' from tevador/RandomX@148b923 (codeofalltrades)
c6a8be7 Cherry picked commit 'Preserve floating point state when calling randomx_calculate_hash' from tevador/RandomX@6a764e9 (codeofalltrades)
6cb9858 Cherry picked commit 'Add a missing function to calculate a batch of hashes' from tevador/RandomX@01381cc (codeofalltrades)

Pull request description:

  ### Problem ###
  Keep randomx code current

  ### Solution ###
  Cherry picked fixes from offical randomx repo
  Add a missing function to calculate a batch of hashes - tevador/RandomX@01381cc
  Fix symbol collisions with blake2b - tevador/RandomX@7567cef
  Fixes for cmake build with visual studio - tevador/RandomX@01914b4
  Optimized Argon2 (SSSE3/AVX2) - tevador/RandomX@900a936
  Fix function names for clang on Apple - tevador/RandomX@e43267f
  virtual_memory: add MAP_JIT on macOS - tevador/RandomX@7c172f7
  Fix compilation and JIT support on NetBSD - tevador/RandomX@708a4e5
  fix potential use-after-free when reallocating cache - tevador/RandomX@32ab5de
  fix test 92 not failing properly on GCC/amd64 - tevador/RandomX@148b923
  Preserve floating point state when calling randomx_calculate_hash - tevador/RandomX@6a764e9
  Add a missing function to calculate a batch of hashes - tevador/RandomX@01381cc

  ### Unit Testing Results ###
  Start the wallet
  Start mining randomx
  Verify you can spend mined coins

Tree-SHA512: 15d4d1ad9fcc8f90a2486b291406b75e4ef118edd0274bd684b431da67b881152107d8e9a4782e7a65b70ab4da8d246e40b1c499b28f85615dc7e9d0927a3fed
  • Loading branch information
codeofalltrades committed Feb 4, 2021
2 parents d93942b + 7fedd67 commit 8bd686d
Show file tree
Hide file tree
Showing 13 changed files with 348 additions and 237 deletions.
135 changes: 67 additions & 68 deletions src/crypto/randomx/argon2.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* Argon2 input parameter restrictions
*/

/* Minimum and maximum number of lanes (degree of parallelism) */
/* Minimum and maximum number of lanes (degree of parallelism) */
#define ARGON2_MIN_LANES UINT32_C(1)
#define ARGON2_MAX_LANES UINT32_C(0xFFFFFF)

Expand Down Expand Up @@ -95,63 +95,63 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/* Error codes */
typedef enum Argon2_ErrorCodes {
ARGON2_OK = 0,
ARGON2_OK = 0,

ARGON2_OUTPUT_PTR_NULL = -1,
ARGON2_OUTPUT_PTR_NULL = -1,

ARGON2_OUTPUT_TOO_SHORT = -2,
ARGON2_OUTPUT_TOO_LONG = -3,
ARGON2_OUTPUT_TOO_SHORT = -2,
ARGON2_OUTPUT_TOO_LONG = -3,

ARGON2_PWD_TOO_SHORT = -4,
ARGON2_PWD_TOO_LONG = -5,
ARGON2_PWD_TOO_SHORT = -4,
ARGON2_PWD_TOO_LONG = -5,

ARGON2_SALT_TOO_SHORT = -6,
ARGON2_SALT_TOO_LONG = -7,
ARGON2_SALT_TOO_SHORT = -6,
ARGON2_SALT_TOO_LONG = -7,

ARGON2_AD_TOO_SHORT = -8,
ARGON2_AD_TOO_LONG = -9,
ARGON2_AD_TOO_SHORT = -8,
ARGON2_AD_TOO_LONG = -9,

ARGON2_SECRET_TOO_SHORT = -10,
ARGON2_SECRET_TOO_LONG = -11,
ARGON2_SECRET_TOO_SHORT = -10,
ARGON2_SECRET_TOO_LONG = -11,

ARGON2_TIME_TOO_SMALL = -12,
ARGON2_TIME_TOO_LARGE = -13,
ARGON2_TIME_TOO_SMALL = -12,
ARGON2_TIME_TOO_LARGE = -13,

ARGON2_MEMORY_TOO_LITTLE = -14,
ARGON2_MEMORY_TOO_MUCH = -15,
ARGON2_MEMORY_TOO_LITTLE = -14,
ARGON2_MEMORY_TOO_MUCH = -15,

ARGON2_LANES_TOO_FEW = -16,
ARGON2_LANES_TOO_MANY = -17,
ARGON2_LANES_TOO_FEW = -16,
ARGON2_LANES_TOO_MANY = -17,

ARGON2_PWD_PTR_MISMATCH = -18, /* NULL ptr with non-zero length */
ARGON2_SALT_PTR_MISMATCH = -19, /* NULL ptr with non-zero length */
ARGON2_SECRET_PTR_MISMATCH = -20, /* NULL ptr with non-zero length */
ARGON2_AD_PTR_MISMATCH = -21, /* NULL ptr with non-zero length */
ARGON2_PWD_PTR_MISMATCH = -18, /* NULL ptr with non-zero length */
ARGON2_SALT_PTR_MISMATCH = -19, /* NULL ptr with non-zero length */
ARGON2_SECRET_PTR_MISMATCH = -20, /* NULL ptr with non-zero length */
ARGON2_AD_PTR_MISMATCH = -21, /* NULL ptr with non-zero length */

ARGON2_MEMORY_ALLOCATION_ERROR = -22,
ARGON2_MEMORY_ALLOCATION_ERROR = -22,

ARGON2_FREE_MEMORY_CBK_NULL = -23,
ARGON2_ALLOCATE_MEMORY_CBK_NULL = -24,
ARGON2_FREE_MEMORY_CBK_NULL = -23,
ARGON2_ALLOCATE_MEMORY_CBK_NULL = -24,

ARGON2_INCORRECT_PARAMETER = -25,
ARGON2_INCORRECT_TYPE = -26,
ARGON2_INCORRECT_PARAMETER = -25,
ARGON2_INCORRECT_TYPE = -26,

ARGON2_OUT_PTR_MISMATCH = -27,
ARGON2_OUT_PTR_MISMATCH = -27,

ARGON2_THREADS_TOO_FEW = -28,
ARGON2_THREADS_TOO_MANY = -29,
ARGON2_THREADS_TOO_FEW = -28,
ARGON2_THREADS_TOO_MANY = -29,

ARGON2_MISSING_ARGS = -30,
ARGON2_MISSING_ARGS = -30,

ARGON2_ENCODING_FAIL = -31,
ARGON2_ENCODING_FAIL = -31,

ARGON2_DECODING_FAIL = -32,
ARGON2_DECODING_FAIL = -32,

ARGON2_THREAD_FAIL = -33,
ARGON2_THREAD_FAIL = -33,

ARGON2_DECODING_LENGTH_FAIL = -34,
ARGON2_DECODING_LENGTH_FAIL = -34,

ARGON2_VERIFY_MISMATCH = -35
ARGON2_VERIFY_MISMATCH = -35
} argon2_error_codes;

/* Memory allocator types --- for external allocation */
Expand Down Expand Up @@ -186,46 +186,46 @@ typedef void(*deallocate_fptr)(uint8_t *memory, size_t bytes_to_allocate);
Argon2_Context(out,8,pwd,32,salt,16,NULL,0,NULL,0,5,1<<20,4,4,NULL,NULL,true,false,false,false)
*/
typedef struct Argon2_Context {
uint8_t *out; /* output array */
uint32_t outlen; /* digest length */
uint8_t *out; /* output array */
uint32_t outlen; /* digest length */

uint8_t *pwd; /* password array */
uint32_t pwdlen; /* password length */
uint8_t *pwd; /* password array */
uint32_t pwdlen; /* password length */

uint8_t *salt; /* salt array */
uint32_t saltlen; /* salt length */
uint8_t *salt; /* salt array */
uint32_t saltlen; /* salt length */

uint8_t *secret; /* key array */
uint32_t secretlen; /* key length */
uint8_t *secret; /* key array */
uint32_t secretlen; /* key length */

uint8_t *ad; /* associated data array */
uint32_t adlen; /* associated data length */
uint8_t *ad; /* associated data array */
uint32_t adlen; /* associated data length */

uint32_t t_cost; /* number of passes */
uint32_t m_cost; /* amount of memory requested (KB) */
uint32_t lanes; /* number of lanes */
uint32_t threads; /* maximum number of threads */
uint32_t t_cost; /* number of passes */
uint32_t m_cost; /* amount of memory requested (KB) */
uint32_t lanes; /* number of lanes */
uint32_t threads; /* maximum number of threads */

uint32_t version; /* version number */
uint32_t version; /* version number */

allocate_fptr allocate_cbk; /* pointer to memory allocator */
deallocate_fptr free_cbk; /* pointer to memory deallocator */
allocate_fptr allocate_cbk; /* pointer to memory allocator */
deallocate_fptr free_cbk; /* pointer to memory deallocator */

uint32_t flags; /* array of bool options */
uint32_t flags; /* array of bool options */
} argon2_context;

/* Argon2 primitive type */
typedef enum Argon2_type {
Argon2_d = 0,
Argon2_i = 1,
Argon2_id = 2
Argon2_d = 0,
Argon2_i = 1,
Argon2_id = 2
} argon2_type;

/* Version of the algorithm */
typedef enum Argon2_version {
ARGON2_VERSION_10 = 0x10,
ARGON2_VERSION_13 = 0x13,
ARGON2_VERSION_NUMBER = ARGON2_VERSION_13
ARGON2_VERSION_10 = 0x10,
ARGON2_VERSION_13 = 0x13,
ARGON2_VERSION_NUMBER = ARGON2_VERSION_13
} argon2_version;

//Argon2 instance - forward declaration
Expand All @@ -236,7 +236,7 @@ typedef struct Argon2_position_t argon2_position_t;

//Argon2 implementation function
typedef void randomx_argon2_impl(const argon2_instance_t* instance,
argon2_position_t position);
argon2_position_t position);

#if defined(__cplusplus)
extern "C" {
Expand All @@ -251,17 +251,16 @@ extern "C" {
* @pre all block pointers must be valid
*/
void randomx_argon2_fill_segment_ref(const argon2_instance_t* instance,
argon2_position_t position);
argon2_position_t position);

randomx_argon2_impl *randomx_argon2_impl_ssse3();
randomx_argon2_impl *randomx_argon2_impl_ssse3();

#if defined (__AVX2__)
randomx_argon2_impl *randomx_argon2_impl_avx2();
randomx_argon2_impl *randomx_argon2_impl_avx2();
#else
inline randomx_argon2_impl *randomx_argon2_impl_avx2() { return NULL; }
inline randomx_argon2_impl *randomx_argon2_impl_avx2() { return NULL; }
#endif

#if defined(__cplusplus)
}
#endif

#endif
2 changes: 1 addition & 1 deletion src/crypto/randomx/argon2_avx2.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "crypto/randomx/argon2.h"

void randomx_argon2_fill_segment_avx2(const argon2_instance_t* instance,
argon2_position_t position);
argon2_position_t position);

#if defined(__AVX2__)
randomx_argon2_impl* randomx_argon2_impl_avx2() {
Expand Down
124 changes: 62 additions & 62 deletions src/crypto/randomx/blake2/blake2.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,72 +42,72 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
extern "C" {
#endif

enum blake2b_constant {
BLAKE2B_BLOCKBYTES = 128,
BLAKE2B_OUTBYTES = 64,
BLAKE2B_KEYBYTES = 64,
BLAKE2B_SALTBYTES = 16,
BLAKE2B_PERSONALBYTES = 16
};
enum blake2b_constant {
BLAKE2B_BLOCKBYTES = 128,
BLAKE2B_OUTBYTES = 64,
BLAKE2B_KEYBYTES = 64,
BLAKE2B_SALTBYTES = 16,
BLAKE2B_PERSONALBYTES = 16
};

#pragma pack(push, 1)
typedef struct __blake2b_param {
uint8_t digest_length; /* 1 */
uint8_t key_length; /* 2 */
uint8_t fanout; /* 3 */
uint8_t depth; /* 4 */
uint32_t leaf_length; /* 8 */
uint64_t node_offset; /* 16 */
uint8_t node_depth; /* 17 */
uint8_t inner_length; /* 18 */
uint8_t reserved[14]; /* 32 */
uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
} blake2b_param;
typedef struct __blake2b_param {
uint8_t digest_length; /* 1 */
uint8_t key_length; /* 2 */
uint8_t fanout; /* 3 */
uint8_t depth; /* 4 */
uint32_t leaf_length; /* 8 */
uint64_t node_offset; /* 16 */
uint8_t node_depth; /* 17 */
uint8_t inner_length; /* 18 */
uint8_t reserved[14]; /* 32 */
uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
} blake2b_param;
#pragma pack(pop)

typedef struct __blake2b_state {
uint64_t h[8];
uint64_t t[2];
uint64_t f[2];
uint8_t buf[BLAKE2B_BLOCKBYTES];
unsigned buflen;
unsigned outlen;
uint8_t last_node;
} blake2b_state;

/* Ensure param structs have not been wrongly padded */
/* Poor man's static_assert */
enum {
blake2_size_check_0 = 1 / !!(CHAR_BIT == 8),
blake2_size_check_2 =
1 / !!(sizeof(blake2b_param) == sizeof(uint64_t) * CHAR_BIT)
};

//randomx namespace
#define blake2b_init randomx_blake2b_init
#define blake2b_init_key randomx_blake2b_init_key
#define blake2b_init_param randomx_blake2b_init_param
#define blake2b_update randomx_blake2b_update
#define blake2b_final randomx_blake2b_final
#define blake2b randomx_blake2b
#define blake2b_long randomx_blake2b_long

/* Streaming API */
int blake2b_init(blake2b_state *S, size_t outlen);
int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key,
size_t keylen);
int blake2b_init_param(blake2b_state *S, const blake2b_param *P);
int blake2b_update(blake2b_state *S, const void *in, size_t inlen);
int blake2b_final(blake2b_state *S, void *out, size_t outlen);

/* Simple API */
int blake2b(void *out, size_t outlen, const void *in, size_t inlen,
const void *key, size_t keylen);

/* Argon2 Team - Begin Code */
int rxa2_blake2b_long(void *out, size_t outlen, const void *in, size_t inlen);
/* Argon2 Team - End Code */
typedef struct __blake2b_state {
uint64_t h[8];
uint64_t t[2];
uint64_t f[2];
uint8_t buf[BLAKE2B_BLOCKBYTES];
unsigned buflen;
unsigned outlen;
uint8_t last_node;
} blake2b_state;

/* Ensure param structs have not been wrongly padded */
/* Poor man's static_assert */
enum {
blake2_size_check_0 = 1 / !!(CHAR_BIT == 8),
blake2_size_check_2 =
1 / !!(sizeof(blake2b_param) == sizeof(uint64_t) * CHAR_BIT)
};

//randomx namespace
#define blake2b_init randomx_blake2b_init
#define blake2b_init_key randomx_blake2b_init_key
#define blake2b_init_param randomx_blake2b_init_param
#define blake2b_update randomx_blake2b_update
#define blake2b_final randomx_blake2b_final
#define blake2b randomx_blake2b
#define blake2b_long randomx_blake2b_long

/* Streaming API */
int blake2b_init(blake2b_state *S, size_t outlen);
int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key,
size_t keylen);
int blake2b_init_param(blake2b_state *S, const blake2b_param *P);
int blake2b_update(blake2b_state *S, const void *in, size_t inlen);
int blake2b_final(blake2b_state *S, void *out, size_t outlen);

/* Simple API */
int blake2b(void *out, size_t outlen, const void *in, size_t inlen,
const void *key, size_t keylen);

/* Argon2 Team - Begin Code */
int blake2b_long(void *out, size_t outlen, const void *in, size_t inlen);
/* Argon2 Team - End Code */

#if defined(__cplusplus)
}
Expand Down
Loading

0 comments on commit 8bd686d

Please sign in to comment.