Skip to content

Commit

Permalink
Clear work
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed May 10, 2021
1 parent b916019 commit 710ae81
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 72 deletions.
6 changes: 3 additions & 3 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name=DuinoCoin
version=1.0.0
author=
maintainer=
version=1.0.1
author=ricaun
maintainer=ricaun
sentence=DuinoCoin
paragraph=DuinoCoin
category=Communication
Expand Down
2 changes: 0 additions & 2 deletions src/Ducos1a.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <Arduino.h>

#if defined(ARDUINO_ARCH_AVR)
//#include "avr/Ducos1a_avr.h"
//#include "hash/Ducos1a_hash.h"
#include "sha1/Ducos1a_sha1.h"
#elif defined(ARDUINO_ARCH_ESP8266)
#include "esp/Ducos1a_esp.h"
Expand Down
46 changes: 14 additions & 32 deletions src/DuinoCoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@

DuinoCoin::DuinoCoin(HardwareSerial &stream) : serial(stream)
{
// Grab Arduino chip ID
ID = "DUCOID";
char buff[4];
for (size_t i = 0; i < 8; i++)
{
sprintf(buff, "%02X", (uint8_t) UniqueID8[i]);
ID += buff;
}

}

DuinoCoin::~DuinoCoin(void)
Expand All @@ -24,30 +17,7 @@ void DuinoCoin::begin(void)
while (!serial);
serial.setTimeout(5000);
}
/*
void DuinoCoin::test(Stream &stream)
{
unsigned long time = 0;
time = millis();
stream.print("251 = ");
stream.print(ducos1a("2eebb02e9b4995c15d46727dc0427478e738a505", "73a34a3ca74449c0565961476772714e940c9cf0", 6));
stream.print(" | Time: ");
stream.println(millis() - time);

time = millis();
stream.print("482 = ");
stream.print(ducos1a("68958c4aab39fbfb4453fb4ee0398a580af7abaf", "b430b3b1d76a6b1ae0464b327e786f43801a02cb", 6));
stream.print(" | Time: ");
stream.println(millis() - time);
time = millis();
stream.print("185 = ");
stream.print(ducos1a("f5da2de2ca442868678363e28298bf16f1e3a856", "5a9320c0015d1dd4eee6f90ad1a7d429577abf88", 6));
stream.print(" | Time: ");
stream.println(millis() - time);
}
*/
bool DuinoCoin::loop(void)
{
if (serial.available() > 0) {
Expand All @@ -66,7 +36,7 @@ bool DuinoCoin::loop(void)
// Calculate elapsed time
unsigned long elapsedTime = endTime - startTime;
// Send result back to the program with share time
serial.print(String(ducos1result) + "," + String(elapsedTime) + "," + String(ID) + "\n");
serial.print(String(ducos1result) + "," + String(elapsedTime) + "," + String(getDUCOID()) + "\n");
return true;
}
return false;
Expand All @@ -76,3 +46,15 @@ bool DuinoCoin::loop(void)
uint32_t DuinoCoin::ducos1a(String lastblockhash, String newblockhash, int difficulty) {
return Ducos1a.work(lastblockhash, newblockhash, difficulty);
}

// Grab Arduino chip DUCOID
String DuinoCoin::getDUCOID() {
String ID = "DUCOID";
char buff[4];
for (size_t i = 0; i < 8; i++)
{
sprintf(buff, "%02X", (uint8_t) UniqueID8[i]);
ID += buff;
}
return ID;
}
4 changes: 1 addition & 3 deletions src/DuinoCoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ class DuinoCoin
void begin();
bool loop();

//void test(Stream &stream);

uint32_t ducos1a(String lastblockhash, String newblockhash, int difficulty);
String getDUCOID();

private:
HardwareSerial &serial;
String ID;
};

#endif
3 changes: 1 addition & 2 deletions src/esp/Ducos1a_esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ class Ducos1a_esp
// DUCO-S1A hasher
uint32_t work(String lastblockhash, String newblockhash, int difficulty)
{
// DUCO-S1 algorithm implementation for ESP8266 boards (DUCO-S1A)
newblockhash.toUpperCase();
// DUCO-S1 algorithm implementation for AVR boards (DUCO-S1A)
// Difficulty loop
int ducos1res = 0;
for (int ducos1res = 0; ducos1res < difficulty * 100 + 1; ducos1res++)
{
String result = SHA1::hash(lastblockhash + String(ducos1res));
Expand Down
27 changes: 10 additions & 17 deletions src/esp32/Ducos1a_esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,52 @@ class Ducos1a_esp32
// DUCO-S1A hasher
uint32_t work(String lastblockhash, String newblockhash, int difficulty)
{
// DUCO-S1 algorithm implementation for ESP32 boards (DUCO-S1A)
newblockhash.toUpperCase();
const char *c = newblockhash.c_str();

size_t len = strlen(c);
size_t final_len = len / 2;
unsigned char *job11 = (unsigned char *)malloc((final_len + 1) * sizeof(unsigned char));
uint8_t job[final_len+1];
for (size_t i = 0, j = 0; j < final_len; i += 2, j++)
job11[j] = (c[i] % 32 + 9) % 25 * 16 + (c[i + 1] % 32 + 9) % 25;

byte shaResult1[20];
job[j] = (c[i] % 32 + 9) % 25 * 16 + (c[i + 1] % 32 + 9) % 25;

int ducos1res = 0;
for (int ducos1res = 0; ducos1res < difficulty * 100 + 1; ducos1res++)
{
String hash11 = String(lastblockhash) + String(ducos1res);
const unsigned char *payload1 = (const unsigned char *)hash11.c_str();
unsigned int payloadLenght1 = hash11.length();

byte shaResult1[20];
esp_sha(SHA1, payload1, payloadLenght1, shaResult1);

if (memcmp(shaResult1, job11, sizeof(shaResult1)) == 0)
if (memcmp(shaResult1, job, sizeof(shaResult1)) == 0)
{
// If expected hash is equal to the found hash, return the result
return ducos1res;
}
//delay(10);
yield(); // uncomment if ESP watchdog triggers
}
return 0;
}

uint32_t work2(String lastblockhash, String newblockhash, int difficulty)
{

// DUCO-S1 algorithm implementation for ESP32 boards (DUCO-S1A)
newblockhash.toUpperCase();
const char *c = newblockhash.c_str();

size_t len = strlen(c);
size_t final_len = len / 2;
unsigned char *job1 = (unsigned char *)malloc((final_len + 1) * sizeof(unsigned char));
uint8_t job[final_len+1];
for (size_t i = 0, j = 0; j < final_len; i += 2, j++)
job1[j] = (c[i] % 32 + 9) % 25 * 16 + (c[i + 1] % 32 + 9) % 25;

byte shaResult[20];
job[j] = (c[i] % 32 + 9) % 25 * 16 + (c[i + 1] % 32 + 9) % 25;

int ducos1res = 0;
for (int ducos1res = 0; ducos1res < difficulty * 100 + 1; ducos1res++)
{
String hash1 = String(lastblockhash) + String(ducos1res);
const unsigned char *payload = (const unsigned char *)hash1.c_str();
unsigned int payloadLength = hash1.length();

byte shaResult[20];
mbedtls_md_context_t ctx;
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
mbedtls_md_init(&ctx);
Expand All @@ -72,15 +66,14 @@ class Ducos1a_esp32
mbedtls_md_finish(&ctx, shaResult);
mbedtls_md_free(&ctx);

if (memcmp(shaResult, job1, sizeof(shaResult)) == 0)
if (memcmp(shaResult, job, sizeof(shaResult)) == 0)
{
// If expected hash is equal to the found hash, return the result
return ducos1res;
}
yield(); // uncomment if ESP watchdog triggers
}
return 0;

}
};

Expand Down
3 changes: 1 addition & 2 deletions src/hash/Ducos1a_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class Ducos1a_hash
// DUCO-S1A hasher
uint32_t work(String lastblockhash, String newblockhash, int difficulty)
{
// DUCO-S1 algorithm implementation for AVR boards (DUCO-S1A)
// DUCO-S1 algorithm implementation (DUCO-S1A)
// Difficulty loop
int ducos1res = 0;
for (int ducos1res = 0; ducos1res < difficulty * 100 + 1; ducos1res++)
{
String result = sha1(lastblockhash + String(ducos1res));
Expand Down
17 changes: 6 additions & 11 deletions src/sha1/Ducos1a_sha1.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,29 @@ class Ducos1a_sha1
// DUCO-S1A hasher
uint32_t work(String lastblockhash, String newblockhash, int difficulty)
{
// DUCO-S1 algorithm implementation for AVR boards (DUCO-S1A)
newblockhash.toUpperCase();
const char *c = newblockhash.c_str();

size_t len = strlen(c);
size_t final_len = len / 2;
unsigned char *job = (unsigned char *)malloc((final_len + 1) * sizeof(unsigned char));
uint8_t job[final_len+1];
for (size_t i = 0, j = 0; j < final_len; i += 2, j++)
job[j] = (c[i] % 32 + 9) % 25 * 16 + (c[i + 1] % 32 + 9) % 25;

// DUCO-S1 algorithm implementation for AVR boards (DUCO-S1A)
// Difficulty loop
int ducos1res = 0;
for (int ducos = 0; ducos < difficulty * 100 + 1; ducos++)
for (int ducos1res = 0; ducos1res < difficulty * 100 + 1; ducos1res++)
{
Sha1.init();
Sha1.print(lastblockhash + ducos);
Sha1.print(lastblockhash + ducos1res);
// Get SHA1 result
uint8_t *hash_bytes = Sha1.result();

if (memcmp(hash_bytes, job, sizeof(hash_bytes)) == 0)
{
// If expected hash is equal to the found hash, return the result
ducos1res = ducos;
break;
return ducos1res;
}
}
free( job );
return ducos1res;
return 0;
}
};

Expand Down

0 comments on commit 710ae81

Please sign in to comment.