Last active
February 8, 2025 21:11
-
-
Save portlandhodl/e3042cf8cfd082d960ba6231d8556c77 to your computer and use it in GitHub Desktop.
Double Sha256 Benchamrk w/ Hardware Support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <chrono> | |
#include <openssl/sha.h> | |
#include <cpuid.h> | |
bool check_sha_support() { | |
unsigned int eax, ebx, ecx, edx; | |
if (__get_cpuid(7, &eax, &ebx, &ecx, &edx)) { | |
return (ebx & (1 << 29)) != 0; // Check bit 29 for SHA extensions | |
} | |
return false; | |
} | |
void double_sha256(const unsigned char* input, size_t length, unsigned char* output) { | |
SHA256_CTX sha256; | |
unsigned char intermediate[32]; | |
// First SHA256 | |
SHA256_Init(&sha256); | |
SHA256_Update(&sha256, input, length); | |
SHA256_Final(intermediate, &sha256); | |
// Second SHA256 | |
SHA256_Init(&sha256); | |
SHA256_Update(&sha256, intermediate, 32); | |
SHA256_Final(output, &sha256); | |
} | |
int main() { | |
unsigned char header[80] = {0}; // Test header filled with zeros | |
unsigned char hash[32]; | |
std::cout << "SHA Hardware support: " << (check_sha_support() ? "Yes" : "No") << std::endl; | |
const int NUM_ITERATIONS = 1000000; | |
auto start = std::chrono::high_resolution_clock::now(); | |
for (int i = 0; i < NUM_ITERATIONS; i++) { | |
double_sha256(header, 80, hash); | |
} | |
auto end = std::chrono::high_resolution_clock::now(); | |
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start); | |
double seconds = duration.count() / 1000000.0; | |
double hashes_per_second = (NUM_ITERATIONS / seconds); | |
std::cout << "Performed " << NUM_ITERATIONS << " double SHA256 hashes in " | |
<< seconds << " seconds" << std::endl; | |
std::cout << "Hash rate: " << hashes_per_second / 1000000.0 << " MH/s" << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment