From 0508c1fc6bc512897369c3ac8b9d02b13ddc1cfb Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Mon, 26 Jun 2023 23:29:42 +0200 Subject: [PATCH] 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. --- platformio.ini | 1 + src/mining.cpp | 27 ++++++++++++--------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/platformio.ini b/platformio.ini index b8778be..c1955e4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -41,3 +41,4 @@ lib_deps = https://github.com/tzapu/WiFiManager.git mathertel/OneButton @ ^2.0.3 arduino-libraries/NTPClient + https://github.com/golden-guy/Arduino_wolfssl.git#v5.5.4 diff --git a/src/mining.cpp b/src/mining.cpp index 64fd00f..fdefd58 100644 --- a/src/mining.cpp +++ b/src/mining.cpp @@ -3,10 +3,9 @@ #include #include #include // Graphics and font library for ILI9341 driver chip +#include #include "media/Free_Fonts.h" #include "media/images.h" -#include "mbedtls/md.h" -#include "mbedtls/sha256.h" #include "OpenFontRender.h" #include "stratum.h" #include "mining.h" @@ -225,14 +224,13 @@ void runMiner(void * name){ mMiner.inRun = true; //Set inRun flag //Prepare Premining data - mbedtls_sha256_context midstate[32]; + Sha256 midstate[32]; unsigned char hash[32]; - mbedtls_sha256_context ctx; + Sha256 sha256; //Calcular midstate - mbedtls_sha256_init(midstate); - mbedtls_sha256_starts_ret(midstate, 0); - mbedtls_sha256_update_ret(midstate, mMiner.bytearray_blockheader, 64); + wc_InitSha256(midstate); + wc_Sha256Update(midstate, mMiner.bytearray_blockheader, 64); // search a valid nonce unsigned long nonce = TARGET_NONCE - MAX_NONCE; @@ -244,14 +242,13 @@ void runMiner(void * name){ //Con midstate // Primer SHA-256 - mbedtls_sha256_clone(&ctx, midstate); //Clonamos el contexto anterior para continuar el SHA desde allĂ­ - mbedtls_sha256_update_ret(&ctx, header64, 16); - mbedtls_sha256_finish_ret(&ctx, hash); + wc_Sha256Copy(midstate, &sha256); + wc_Sha256Update(&sha256, header64, 16); + wc_Sha256Final(&sha256, hash); // Segundo SHA-256 - mbedtls_sha256_starts_ret(&ctx, 0); - mbedtls_sha256_update_ret(&ctx, hash, 32); - mbedtls_sha256_finish_ret(&ctx, hash); + wc_Sha256Update(&sha256, hash, 32); + wc_Sha256Final(&sha256, hash); /*for (size_t i = 0; i < 32; i++) Serial.printf("%02x", hash[i]); Serial.println(""); */ @@ -300,8 +297,8 @@ void runMiner(void * name){ } } // exit if found a valid result or nonce > MAX_NONCE - mbedtls_sha256_free(&ctx); - mbedtls_sha256_free(midstate); + wc_Sha256Free(&sha256); + wc_Sha256Free(midstate); mMiner.inRun = false; Serial.print(">>> Finished job waiting new data from pool");