forked from duino-coin/duino-coin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino_Code.ino
More file actions
131 lines (123 loc) · 4.14 KB
/
Arduino_Code.ino
File metadata and controls
131 lines (123 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
,------. ,--. ,-----. ,--.
| .-. \ ,--.,--.`--',--,--, ,---. ,-----.' .--./ ,---. `--',--,--,
| | \ :| || |,--.| \| .-. |'-----'| | | .-. |,--.| \
| '--' /' '' '| || || |' '-' ' ' '--'\' '-' '| || || |
`-------' `----' `--'`--''--' `---' `-----' `---' `--'`--''--'
Official code for Arduino boards version 2.7
Duino-Coin Team & Community 2019-2021 © MIT Licensed
https://duinocoin.com
https://github.com/revoxhere/duino-coin
If you don't know where to start, visit official website and navigate to
the Getting Started page. Have fun mining!
*/
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
/* For 8-bit microcontrollers we should use 16 bit variables since the
difficulty is low, for all the other cases should be 32 bits. */
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
typedef uint16_t uintDiff;
#else
typedef uint32_t uintDiff;
#endif
// Arduino identifier library - https://github.com/ricaun
#include "uniqueID.h"
#include "sha1.h"
// Create globals
String lastblockhash = "";
String newblockhash = "";
String DUCOID = "";
uintDiff difficulty = 0;
uintDiff ducos1result = 0;
// 40+40+20+3 is the maximum size of a job
const uint16_t job_maxsize = 104;
uint8_t job[job_maxsize];
void setup() {
// Prepare built-in led pin as output
pinMode(LED_BUILTIN, OUTPUT);
DUCOID = get_DUCOID();
// Open serial port
Serial.begin(115200);
Serial.setTimeout(10000);
while (!Serial)
; // For Arduino Leonardo or any board with the ATmega32U4
Serial.flush();
}
// DUCO-S1A hasher
uintDiff ducos1a(String lastblockhash, String newblockhash,
uintDiff difficulty) {
newblockhash.toUpperCase();
const char *c = newblockhash.c_str();
uint8_t final_len = newblockhash.length() >> 1;
for (uint8_t i = 0, j = 0; j < final_len; i += 2, j++)
job[j] = ((((c[i] & 0x1F) + 9) % 25) << 4) + ((c[i + 1] & 0x1F) + 9) % 25;
// Difficulty loop
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
// If the difficulty is too high for AVR architecture then return 0
if (difficulty > 655) return 0;
#endif
for (uintDiff ducos1res = 0; ducos1res < difficulty * 100 + 1; ducos1res++) {
Sha1.init();
Sha1.print(lastblockhash + String(ducos1res));
// Get SHA1 result
uint8_t *hash_bytes = Sha1.result();
if (memcmp(hash_bytes, job, SHA1_HASH_LEN * sizeof(char)) == 0) {
// If expected hash is equal to the found hash, return the result
return ducos1res;
}
}
return 0;
}
String get_DUCOID() {
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;
}
void loop() {
// Wait for serial data
if (Serial.available() > 0) {
memset(job, 0, job_maxsize);
// Read last block hash
lastblockhash = Serial.readStringUntil(',');
// Read expected hash
newblockhash = Serial.readStringUntil(',');
// Read difficulty
difficulty = strtoul(Serial.readStringUntil(',').c_str(), NULL, 10);
// Clearing the receive buffer reading one job.
while (Serial.available()) Serial.read();
// Start time measurement
uint32_t startTime = micros();
// Call DUCO-S1A hasher
ducos1result = ducos1a(lastblockhash, newblockhash, difficulty);
// Calculate elapsed time
uint32_t elapsedTime = micros() - startTime;
// Clearing the receive buffer before sending the result.
while (Serial.available()) Serial.read();
// Send result back to the program with share time
Serial.print(String(ducos1result, 2)
+ ","
+ String(elapsedTime, 2)
+ ","
+ String(DUCOID)
+ "\n");
// Turn on built-in led
#if defined(ARDUINO_ARCH_AVR)
PORTB = PORTB | B00100000;
#else
digitalWrite(LED_BUILTIN, HIGH);
#endif
// Wait a bit
delay(25);
// Turn off built-in led
#if defined(ARDUINO_ARCH_AVR)
PORTB = PORTB & B11011111;
#else
digitalWrite(LED_BUILTIN, LOW);
#endif
}
}