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
145 lines (139 loc) · 4.47 KB
/
Arduino_Code.ino
File metadata and controls
145 lines (139 loc) · 4.47 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//////////////////////////////////////////////////////////
// _____ _ _____ _
// | __ \ (_) / ____| (_)
// | | | |_ _ _ _ __ ___ ______| | ___ _ _ __
// | | | | | | | | '_ \ / _ \______| | / _ \| | '_ \
// | |__| | |_| | | | | | (_) | | |___| (_) | | | | |
// |_____/ \__,_|_|_| |_|\___/ \_____\___/|_|_| |_|
// Code for Arduino boards v2.53
// © Duino-Coin Community 2019-2021
// Distributed under MIT License
//////////////////////////////////////////////////////////
// https://github.com/revoxhere/duino-coin - GitHub
// https://duinocoin.com - Official Website
// https://discord.gg/k48Ht5y - Discord
//////////////////////////////////////////////////////////
// If you don't know what to do, visit official website
// and navigate to Getting Started page. Happy mining!
//////////////////////////////////////////////////////////
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
// Include SHA1 library
// Authors: https://github.com/daknuett, https://github.com/JoyBed, https://github.com/revox
// Improvements: https://github.com/joaquinbvw
#include "sha1.h"
// Include Arduino identifier library
// Author: https://github.com/ricaun
#include "uniqueID.h"
// Create globals
String lastblockhash = "";
String newblockhash = "";
String DUCOID = "";
#ifdef ARDUINO_ARCH_AVR
uint16_t difficulty = 0;
uint16_t ducos1result = 0;
#else
uint32_t difficulty = 0;
uint32_t ducos1result = 0;
#endif
const uint16_t job_maxsize = 104; // 40+40+20+3 is the maximum size of a job
uint8_t job[job_maxsize];
// Setup stuff
void setup() {
// Prepare built-in led pin as output
pinMode(LED_BUILTIN, OUTPUT);
DUCOID = get_DUCOID();
// Open serial port
Serial.begin(115200);
Serial.setTimeout(3334);
while(!Serial); // For Arduino Leonardo or any board with the ATmega32U4
Serial.flush();
}
// DUCO-S1A hasher
#ifdef ARDUINO_ARCH_AVR
uint16_t ducos1a(String lastblockhash, String newblockhash, uint16_t difficulty)
#else
uint32_t ducos1a(String lastblockhash, String newblockhash, uint16_t difficulty)
#endif
{
// DUCO-S1 algorithm implementation for AVR boards (DUCO-S1A)
newblockhash.toUpperCase();
const char *c = newblockhash.c_str();
size_t final_len = newblockhash.length() >> 1;
for (size_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
#ifdef ARDUINO_ARCH_AVR
// If the difficulty is too high for AVR architecture then return 0
if (difficulty > 655)
return 0;
for (uint16_t ducos1res = 0; ducos1res < difficulty * 100 + 1; ducos1res++)
#else
for (uint32_t ducos1res = 0; ducos1res < difficulty * 100 + 1; ducos1res++)
#endif
{
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;
}
// Grab Arduino chip DUCOID
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;
}
// Infinite loop
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 = Serial.readStringUntil(',').toInt();
// 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) + "," + String(elapsedTime) + "," + DUCOID + "\n");
// Turn on built-in led
#ifdef ARDUINO_ARCH_AVR
PORTB = PORTB | B00100000;
#else
digitalWrite(LED_BUILTIN, HIGH);
#endif
// Wait a bit
delay(25);
// Turn off built-in led
#ifdef ARDUINO_ARCH_AVR
PORTB = PORTB & B11011111;
#else
digitalWrite(LED_BUILTIN, LOW);
#endif
}
}