Skip to content

Commit

Permalink
Add SSE4 based SHA256
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa authored and xanimo committed Apr 24, 2024
1 parent 37692f8 commit 6e36af6
Show file tree
Hide file tree
Showing 3 changed files with 1,531 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ crypto_libdogecoin_crypto_la_SOURCES = \
crypto/sha1.cpp \
crypto/sha1.h \
crypto/sha256.cpp \
crypto/sha256_sse4.cpp \
crypto/sha256.h \
crypto/sha512.cpp \
crypto/sha512.h
Expand Down
31 changes: 24 additions & 7 deletions src/crypto/sha256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "crypto/sha256.h"

#include "crypto/common.h"
#include "support/experimental.h"

#include <string.h>
#include <atomic>

#if (defined(__ia64__) || defined(__x86_64__)) && \
!defined(__APPLE__) && \
Expand Down Expand Up @@ -55,6 +55,14 @@ static const uint32_t K[] =
};
#endif /** ARM Headers */

#if defined(__x86_64__) || defined(__amd64__)
#include <cpuid.h>
namespace sha256_sse4
{
void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks);
}
#endif

// Internal implementation code.
namespace
{
Expand Down Expand Up @@ -358,13 +366,16 @@ void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks)
#define HWCAP_SHA2 (1<<6)
#include <sys/auxv.h>
#elif defined(__WIN64__)
#include <intrin.h>
bool isAVX (void) {
int cpuinfo[4];
__cpuid(cpuinfo, 1);
return ((cpuinfo[2] & (1 << 28)) != 0);
#if (defined(__x86_64__) || defined(__amd64__) || defined(__i386__))
/** Check whether the OS has enabled AVX registers. */
bool AVXEnabled()
{
uint32_t a, d;
__asm__("xgetbv" : "=a"(a), "=d"(d) : "c"(0));
return (a & 6) == 6;
}
#endif
#endif

/** Define a function pointer for Transform */
void (*transform_ptr) (uint32_t*, const unsigned char*, size_t) = &Transform;
Expand All @@ -383,8 +394,14 @@ void inline Initialize_transform_ptr(void)
if (__builtin_cpu_supports("avx2"))
transform_ptr = &Transform_AVX2;
#elif USE_AVX2 && defined(__WIN64__)
if (isAVX)
if (AVXEnabled)
transform_ptr = &Transform_AVX2;
#elif USE_ASM
#if defined(__x86_64__) || defined(__amd64__)
uint32_t eax, ebx, ecx, edx;
if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) && (ecx >> 19) & 1)
transform_ptr = sha256_sse4::Transform;
#endif
#endif
}

Expand Down
Loading

0 comments on commit 6e36af6

Please sign in to comment.