Replace mbedtls with wolfSSL for hashing nonces

Using a customized wolfSSL library speeds up hashing by about 30% on the ESP32-S3, resulting in 28-29KH/s.
This commit is contained in:
Stefan Berger 2023-06-26 23:29:42 +02:00
parent 2396e9a220
commit 0508c1fc6b
2 changed files with 13 additions and 15 deletions

View File

@ -41,3 +41,4 @@ lib_deps =
https://github.com/tzapu/WiFiManager.git https://github.com/tzapu/WiFiManager.git
mathertel/OneButton @ ^2.0.3 mathertel/OneButton @ ^2.0.3
arduino-libraries/NTPClient arduino-libraries/NTPClient
https://github.com/golden-guy/Arduino_wolfssl.git#v5.5.4

View File

@ -3,10 +3,9 @@
#include <WiFi.h> #include <WiFi.h>
#include <algorithm> #include <algorithm>
#include <TFT_eSPI.h> // Graphics and font library for ILI9341 driver chip #include <TFT_eSPI.h> // Graphics and font library for ILI9341 driver chip
#include <wolfssl/wolfcrypt/sha256.h>
#include "media/Free_Fonts.h" #include "media/Free_Fonts.h"
#include "media/images.h" #include "media/images.h"
#include "mbedtls/md.h"
#include "mbedtls/sha256.h"
#include "OpenFontRender.h" #include "OpenFontRender.h"
#include "stratum.h" #include "stratum.h"
#include "mining.h" #include "mining.h"
@ -225,14 +224,13 @@ void runMiner(void * name){
mMiner.inRun = true; //Set inRun flag mMiner.inRun = true; //Set inRun flag
//Prepare Premining data //Prepare Premining data
mbedtls_sha256_context midstate[32]; Sha256 midstate[32];
unsigned char hash[32]; unsigned char hash[32];
mbedtls_sha256_context ctx; Sha256 sha256;
//Calcular midstate //Calcular midstate
mbedtls_sha256_init(midstate); wc_InitSha256(midstate);
mbedtls_sha256_starts_ret(midstate, 0); wc_Sha256Update(midstate, mMiner.bytearray_blockheader, 64);
mbedtls_sha256_update_ret(midstate, mMiner.bytearray_blockheader, 64);
// search a valid nonce // search a valid nonce
unsigned long nonce = TARGET_NONCE - MAX_NONCE; unsigned long nonce = TARGET_NONCE - MAX_NONCE;
@ -244,14 +242,13 @@ void runMiner(void * name){
//Con midstate //Con midstate
// Primer SHA-256 // Primer SHA-256
mbedtls_sha256_clone(&ctx, midstate); //Clonamos el contexto anterior para continuar el SHA desde allí wc_Sha256Copy(midstate, &sha256);
mbedtls_sha256_update_ret(&ctx, header64, 16); wc_Sha256Update(&sha256, header64, 16);
mbedtls_sha256_finish_ret(&ctx, hash); wc_Sha256Final(&sha256, hash);
// Segundo SHA-256 // Segundo SHA-256
mbedtls_sha256_starts_ret(&ctx, 0); wc_Sha256Update(&sha256, hash, 32);
mbedtls_sha256_update_ret(&ctx, hash, 32); wc_Sha256Final(&sha256, hash);
mbedtls_sha256_finish_ret(&ctx, hash);
/*for (size_t i = 0; i < 32; i++) /*for (size_t i = 0; i < 32; i++)
Serial.printf("%02x", hash[i]); Serial.printf("%02x", hash[i]);
Serial.println(""); */ Serial.println(""); */
@ -300,8 +297,8 @@ void runMiner(void * name){
} }
} // exit if found a valid result or nonce > MAX_NONCE } // exit if found a valid result or nonce > MAX_NONCE
mbedtls_sha256_free(&ctx); wc_Sha256Free(&sha256);
mbedtls_sha256_free(midstate); wc_Sha256Free(midstate);
mMiner.inRun = false; mMiner.inRun = false;
Serial.print(">>> Finished job waiting new data from pool"); Serial.print(">>> Finished job waiting new data from pool");