Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

refactor: segregate x11 hashing #6303

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,6 @@ crypto_libbitcoin_crypto_base_a_SOURCES = \
crypto/pkcs5_pbkdf2_hmac_sha512.cpp \
crypto/pkcs5_pbkdf2_hmac_sha512.h \
crypto/ripemd160.cpp \
crypto/aes_helper.c \
crypto/ripemd160.h \
crypto/sha1.cpp \
crypto/sha1.h \
Expand Down Expand Up @@ -655,29 +654,30 @@ crypto_libbitcoin_crypto_avx2_a_SOURCES = crypto/sha256_avx2.cpp

# x11
crypto_libbitcoin_crypto_base_a_SOURCES += \
crypto/blake.c \
crypto/bmw.c \
crypto/cubehash.c \
crypto/echo.c \
crypto/groestl.c \
crypto/jh.c \
crypto/keccak.c \
crypto/luffa.c \
crypto/shavite.c \
crypto/simd.c \
crypto/skein.c \
crypto/sph_blake.h \
crypto/sph_bmw.h \
crypto/sph_cubehash.h \
crypto/sph_echo.h \
crypto/sph_groestl.h \
crypto/sph_jh.h \
crypto/sph_keccak.h \
crypto/sph_luffa.h \
crypto/sph_shavite.h \
crypto/sph_simd.h \
crypto/sph_skein.h \
crypto/sph_types.h
crypto/x11/aes_helper.c \
crypto/x11/blake.c \
crypto/x11/bmw.c \
crypto/x11/cubehash.c \
crypto/x11/echo.c \
crypto/x11/groestl.c \
crypto/x11/jh.c \
crypto/x11/keccak.c \
crypto/x11/luffa.c \
crypto/x11/shavite.c \
crypto/x11/simd.c \
crypto/x11/skein.c \
crypto/x11/sph_blake.h \
crypto/x11/sph_bmw.h \
crypto/x11/sph_cubehash.h \
crypto/x11/sph_echo.h \
crypto/x11/sph_groestl.h \
crypto/x11/sph_jh.h \
crypto/x11/sph_keccak.h \
crypto/x11/sph_luffa.h \
crypto/x11/sph_shavite.h \
crypto/x11/sph_simd.h \
crypto/x11/sph_skein.h \
crypto/x11/sph_types.h

crypto_libbitcoin_crypto_x86_shani_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
crypto_libbitcoin_crypto_x86_shani_a_CPPFLAGS = $(AM_CPPFLAGS)
Expand Down Expand Up @@ -707,6 +707,7 @@ libbitcoin_consensus_a_SOURCES = \
consensus/validation.h \
hash.cpp \
hash.h \
hash_x11.h \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to add hash_x11.h to test/util/data/non-backported.txt to let extra linter and clang-format run on this file

prevector.h \
primitives/block.cpp \
primitives/block.h \
Expand Down
1 change: 1 addition & 0 deletions src/bench/crypto_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <crypto/sha512.h>
#include <crypto/siphash.h>
#include <hash.h>
#include <hash_x11.h>
#include <random.h>
#include <uint256.h>

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/crypto/sph_blake.h → src/crypto/x11/sph_blake.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extern "C"{
#endif

#include <stddef.h>
#include <crypto/sph_types.h>
#include "sph_types.h"

/**
* Output size (in bits) for BLAKE-224.
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/sph_bmw.h → src/crypto/x11/sph_bmw.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C"{
#endif

#include <stddef.h>
#include <crypto/sph_types.h>
#include "sph_types.h"

/**
* Output size (in bits) for BMW-224.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extern "C"{
#endif

#include <stddef.h>
#include <crypto/sph_types.h>
#include "sph_types.h"

/**
* Output size (in bits) for CubeHash-224.
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/sph_echo.h → src/crypto/x11/sph_echo.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C"{
#endif

#include <stddef.h>
#include <crypto/sph_types.h>
#include "sph_types.h"

/**
* Output size (in bits) for ECHO-224.
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/sph_groestl.h → src/crypto/x11/sph_groestl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extern "C"{
#endif

#include <stddef.h>
#include <crypto/sph_types.h>
#include "sph_types.h"

/**
* Output size (in bits) for Groestl-224.
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/sph_jh.h → src/crypto/x11/sph_jh.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C"{
#endif

#include <stddef.h>
#include <crypto/sph_types.h>
#include "sph_types.h"

/**
* Output size (in bits) for JH-224.
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/sph_keccak.h → src/crypto/x11/sph_keccak.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C"{
#endif

#include <stddef.h>
#include <crypto/sph_types.h>
#include "sph_types.h"

/**
* Output size (in bits) for Keccak-224.
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/sph_luffa.h → src/crypto/x11/sph_luffa.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C"{
#endif

#include <stddef.h>
#include <crypto/sph_types.h>
#include "sph_types.h"

/**
* Output size (in bits) for Luffa-224.
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/sph_shavite.h → src/crypto/x11/sph_shavite.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#define SPH_SHAVITE_H__

#include <stddef.h>
#include <crypto/sph_types.h>
#include "sph_types.h"

#ifdef __cplusplus
extern "C"{
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/sph_simd.h → src/crypto/x11/sph_simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C"{
#endif

#include <stddef.h>
#include <crypto/sph_types.h>
#include "sph_types.h"

/**
* Output size (in bits) for SIMD-224.
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/sph_skein.h → src/crypto/x11/sph_skein.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extern "C"{
#endif

#include <stddef.h>
#include <crypto/sph_types.h>
#include "sph_types.h"

#if SPH_64

Expand Down
File renamed without changes.
79 changes: 0 additions & 79 deletions src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@
#include <uint256.h>
#include <version.h>

#include <crypto/sph_blake.h>
#include <crypto/sph_bmw.h>
#include <crypto/sph_groestl.h>
#include <crypto/sph_jh.h>
#include <crypto/sph_keccak.h>
#include <crypto/sph_skein.h>
#include <crypto/sph_luffa.h>
#include <crypto/sph_cubehash.h>
#include <crypto/sph_shavite.h>
#include <crypto/sph_simd.h>
#include <crypto/sph_echo.h>

#include <vector>

typedef uint256 ChainCode;
Expand Down Expand Up @@ -253,71 +241,4 @@ unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vData

void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);

/* ----------- Dash Hash ------------------------------------------------ */
template<typename T1>
inline uint256 HashX11(const T1 pbegin, const T1 pend)

{
sph_blake512_context ctx_blake;
sph_bmw512_context ctx_bmw;
sph_groestl512_context ctx_groestl;
sph_jh512_context ctx_jh;
sph_keccak512_context ctx_keccak;
sph_skein512_context ctx_skein;
sph_luffa512_context ctx_luffa;
sph_cubehash512_context ctx_cubehash;
sph_shavite512_context ctx_shavite;
sph_simd512_context ctx_simd;
sph_echo512_context ctx_echo;
static unsigned char pblank[1];

uint512 hash[11];

sph_blake512_init(&ctx_blake);
sph_blake512 (&ctx_blake, (pbegin == pend ? pblank : static_cast<const void*>(&pbegin[0])), (pend - pbegin) * sizeof(pbegin[0]));
sph_blake512_close(&ctx_blake, static_cast<void*>(&hash[0]));

sph_bmw512_init(&ctx_bmw);
sph_bmw512 (&ctx_bmw, static_cast<const void*>(&hash[0]), 64);
sph_bmw512_close(&ctx_bmw, static_cast<void*>(&hash[1]));

sph_groestl512_init(&ctx_groestl);
sph_groestl512 (&ctx_groestl, static_cast<const void*>(&hash[1]), 64);
sph_groestl512_close(&ctx_groestl, static_cast<void*>(&hash[2]));

sph_skein512_init(&ctx_skein);
sph_skein512 (&ctx_skein, static_cast<const void*>(&hash[2]), 64);
sph_skein512_close(&ctx_skein, static_cast<void*>(&hash[3]));

sph_jh512_init(&ctx_jh);
sph_jh512 (&ctx_jh, static_cast<const void*>(&hash[3]), 64);
sph_jh512_close(&ctx_jh, static_cast<void*>(&hash[4]));

sph_keccak512_init(&ctx_keccak);
sph_keccak512 (&ctx_keccak, static_cast<const void*>(&hash[4]), 64);
sph_keccak512_close(&ctx_keccak, static_cast<void*>(&hash[5]));

sph_luffa512_init(&ctx_luffa);
sph_luffa512 (&ctx_luffa, static_cast<void*>(&hash[5]), 64);
sph_luffa512_close(&ctx_luffa, static_cast<void*>(&hash[6]));

sph_cubehash512_init(&ctx_cubehash);
sph_cubehash512 (&ctx_cubehash, static_cast<const void*>(&hash[6]), 64);
sph_cubehash512_close(&ctx_cubehash, static_cast<void*>(&hash[7]));

sph_shavite512_init(&ctx_shavite);
sph_shavite512(&ctx_shavite, static_cast<const void*>(&hash[7]), 64);
sph_shavite512_close(&ctx_shavite, static_cast<void*>(&hash[8]));

sph_simd512_init(&ctx_simd);
sph_simd512 (&ctx_simd, static_cast<const void*>(&hash[8]), 64);
sph_simd512_close(&ctx_simd, static_cast<void*>(&hash[9]));

sph_echo512_init(&ctx_echo);
sph_echo512 (&ctx_echo, static_cast<const void*>(&hash[9]), 64);
sph_echo512_close(&ctx_echo, static_cast<void*>(&hash[10]));

return hash[10].trim256();
}

#endif // BITCOIN_HASH_H
92 changes: 92 additions & 0 deletions src/hash_x11.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) 2014-2023 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_HASH_X11_H
#define BITCOIN_HASH_X11_H


#include <crypto/x11/sph_blake.h>
#include <crypto/x11/sph_bmw.h>
#include <crypto/x11/sph_cubehash.h>
#include <crypto/x11/sph_echo.h>
#include <crypto/x11/sph_groestl.h>
#include <crypto/x11/sph_jh.h>
#include <crypto/x11/sph_keccak.h>
#include <crypto/x11/sph_luffa.h>
#include <crypto/x11/sph_shavite.h>
#include <crypto/x11/sph_simd.h>
#include <crypto/x11/sph_skein.h>

#include <uint256.h>


/* ----------- Dash Hash ------------------------------------------------ */
template <typename T1>
inline uint256 HashX11(const T1 pbegin, const T1 pend)

{
sph_blake512_context ctx_blake;
sph_bmw512_context ctx_bmw;
sph_groestl512_context ctx_groestl;
sph_jh512_context ctx_jh;
sph_keccak512_context ctx_keccak;
sph_skein512_context ctx_skein;
sph_luffa512_context ctx_luffa;
sph_cubehash512_context ctx_cubehash;
sph_shavite512_context ctx_shavite;
sph_simd512_context ctx_simd;
sph_echo512_context ctx_echo;
static unsigned char pblank[1];

uint512 hash[11];

sph_blake512_init(&ctx_blake);
sph_blake512(&ctx_blake, (pbegin == pend ? pblank : static_cast<const void*>(&pbegin[0])),
(pend - pbegin) * sizeof(pbegin[0]));
sph_blake512_close(&ctx_blake, static_cast<void*>(&hash[0]));

sph_bmw512_init(&ctx_bmw);
sph_bmw512(&ctx_bmw, static_cast<const void*>(&hash[0]), 64);
sph_bmw512_close(&ctx_bmw, static_cast<void*>(&hash[1]));

sph_groestl512_init(&ctx_groestl);
sph_groestl512(&ctx_groestl, static_cast<const void*>(&hash[1]), 64);
sph_groestl512_close(&ctx_groestl, static_cast<void*>(&hash[2]));

sph_skein512_init(&ctx_skein);
sph_skein512(&ctx_skein, static_cast<const void*>(&hash[2]), 64);
sph_skein512_close(&ctx_skein, static_cast<void*>(&hash[3]));

sph_jh512_init(&ctx_jh);
sph_jh512(&ctx_jh, static_cast<const void*>(&hash[3]), 64);
sph_jh512_close(&ctx_jh, static_cast<void*>(&hash[4]));

sph_keccak512_init(&ctx_keccak);
sph_keccak512(&ctx_keccak, static_cast<const void*>(&hash[4]), 64);
sph_keccak512_close(&ctx_keccak, static_cast<void*>(&hash[5]));

sph_luffa512_init(&ctx_luffa);
sph_luffa512(&ctx_luffa, static_cast<void*>(&hash[5]), 64);
sph_luffa512_close(&ctx_luffa, static_cast<void*>(&hash[6]));

sph_cubehash512_init(&ctx_cubehash);
sph_cubehash512(&ctx_cubehash, static_cast<const void*>(&hash[6]), 64);
sph_cubehash512_close(&ctx_cubehash, static_cast<void*>(&hash[7]));

sph_shavite512_init(&ctx_shavite);
sph_shavite512(&ctx_shavite, static_cast<const void*>(&hash[7]), 64);
sph_shavite512_close(&ctx_shavite, static_cast<void*>(&hash[8]));

sph_simd512_init(&ctx_simd);
sph_simd512(&ctx_simd, static_cast<const void*>(&hash[8]), 64);
sph_simd512_close(&ctx_simd, static_cast<void*>(&hash[9]));

sph_echo512_init(&ctx_echo);
sph_echo512(&ctx_echo, static_cast<const void*>(&hash[9]), 64);
sph_echo512_close(&ctx_echo, static_cast<void*>(&hash[10]));

return hash[10].trim256();
}

#endif // BITCOIN_HASH_X11_H
1 change: 1 addition & 0 deletions src/primitives/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <primitives/block.h>

#include <hash.h>
#include <hash_x11.h>
#include <streams.h>
#include <tinyformat.h>

Expand Down
2 changes: 1 addition & 1 deletion test/lint/lint-include-guards.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export LC_ALL=C
HEADER_ID_PREFIX="BITCOIN_"
HEADER_ID_SUFFIX="_H"

REGEXP_EXCLUDE_FILES_WITH_PREFIX="src/(crypto/ctaes/|dashbls/|immer/|leveldb/|crc32c/|secp256k1/|test/fuzz/FuzzedDataProvider.h|tinyformat.h|bench/nanobench.h|univalue/|ctpl_stl.h|bls/|crypto/sph|gsl|util/expected.h)"
REGEXP_EXCLUDE_FILES_WITH_PREFIX="src/(crypto/ctaes/|dashbls/|immer/|leveldb/|crc32c/|secp256k1/|test/fuzz/FuzzedDataProvider.h|tinyformat.h|bench/nanobench.h|univalue/|ctpl_stl.h|bls/|crypto/x11/sph|gsl|util/expected.h)"

EXIT_CODE=0
for HEADER_FILE in $(git ls-files -- "*.h" | grep -vE "^${REGEXP_EXCLUDE_FILES_WITH_PREFIX}")
Expand Down
2 changes: 1 addition & 1 deletion test/lint/lint-includes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Check includes: Check for duplicate includes. Enforce bracket syntax includes.

export LC_ALL=C
IGNORE_REGEXP="/(dashbls|immer|leveldb|secp256k1|univalue|crc32c)/"
IGNORE_REGEXP="/(dashbls|immer|leveldb|secp256k1|univalue|crc32c|crypto/x11)/"

# cd to root folder of git repo for git ls-files to work properly
cd "$(dirname $0)/../.." || exit 1
Expand Down
1 change: 1 addition & 0 deletions test/util/data/non-backported.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ src/util/ranges_set.*
src/util/wpipe.*
src/wallet/bip39*
src/wallet/hdchain.*
src/hash_x11.h
Loading