Implementing Blockchain Consensus
Algorithm on STM32 Embedded System
Shahil Sharma (B22CS048)
Abhishek Yadav (B22ES020)
April 21, 2025
GitHub Repository
Abstract
This project explores the design and implementation of a lightweight blockchain proof-
of-work (PoW) miner on an STM32F412 microcontroller. By leveraging the mbedTLS
SHA-256 library on a Cortex-M4 core, we simulate mining by iteratively computing block
hashes until a target difficulty is met, all under severe memory and performance con-
straints. User parameters—including transactions, block index, previous block hash, and
difficulty—are entered over UART and validated before mining commences. Our imple-
mentation demonstrates that, even on a low-power embedded platform, core blockchain
consensus primitives can be realized efficiently and reliably. Performance benchmarks
indicate sustained hash rates suitable for educational and prototyping purposes.
1 Hardware Platform
Board Used: STM32F412
The STM32F412 is a high-performance ARM Cortex-M4 based microcontroller from
STMicroelectronics. It offers 1 MB Flash, 256 KB RAM, and advanced peripherals
including USART, SPI, and GPIO, making it ideal for cryptographic computations and
embedded applications.
Other Components
• Keil uVision5: Development and debugging IDE
• USB-UART Converter: Used to connect to the host system
• USART1 (PA9, PA10): Configured for UART communication
• SHA-256 Library: From mbedTLS
1
2 Advantages of STM32 for Blockchain
• Low Power Consumption: Ideal for battery-powered IoT nodes running sporadic
consensus tasks.
• Cost-Effective: Widely available and affordable development boards lower the
barrier to experimentation.
• Real-Time Performance: Deterministic interrupt handling ensures consistent
hash-rate measurements.
• Rich Peripheral Set: UART, SPI, I²C, and DMA allow flexible integration of
sensors or secure elements alongside cryptographic routines.
• Hardware Acceleration: Although SHA-256 is software-based here, AES and
CRC hardware engines on-chip demonstrate potential for broader crypto offload.
3 Project Goal
Title: Explore the possible implementation of any Blockchain consensus algorithm on
Embedded devices.
In this work, we target the proof-of-work consensus mechanism typical of Bitcoin-like
chains. The objective is to show that a Cortex-M4 microcontroller can:
1. Accept dynamic mining parameters from a user console.
2. Execute SHA-256 hashing in a tight loop to satisfy a difficulty constraint.
3. Report progress and results in real time over UART.
By doing so, the project illustrates the feasibility and limitations of running blockchain
primitives on resource-constrained hardware.
4 Approach and System Architecture
We structured the system into four logical phases:
Step 1: Initialization
• Configure system clock to 100 MHz using PLL and external 8 MHz crystal.
• Initialize USART1 for 115200 baud, 8N1 settings.
• Prepare mbedTLS SHA-256 context (heap-free, stack-only).
2
Step 2: User Input
• Prompt via UART for:
– Transaction string (e.g., “Alice→Bob:50;Carol→Dave:25”)
– Block number (integer index)
– Difficulty (leading zero hex digits)
– Previous block hash (hex up to 64 chars)
• Validate length and character set; pad previous hash to 64 chars with ‘0’.
• Echo all inputs back for user confirmation.
Step 3: Mining Logic
• Build candidate string:
text = block number ∥ transactions ∥ previous hash ∥ nonce
• Call mbedtls sha256 update() / finish() to compute 32-byte digest.
• Convert digest to a 64-char hex string.
• Check for difficulty leading ‘0’ chars; loop until success or nonce overflow.
• Provide optional progress feedback every 100 000 iterations.
Step 4: Result Output
• Upon success, transmit:
– The valid nonce found.
– The resulting block hash.
• If the search space is exhausted, send a “Mining failed” message.
• Enter low-power idle when done.
5 Code Snippet: Main Mining Loop
1 int main ( void ) {
2 SystemInit () ;
3 UART1_Init () ;
4
5 char transactions [ MAX_BUFFER ];
6 char previous_hash [65];
7 char input_buffer [32];
8 int block_number , difficulty ;
9
10 UART1_SendString ( " Enter transactions :\ r \ n " ) ;
11 UART 1_Rece iveLi ne ( transactions , MAX_BUFFER ) ;
12 UART1_SendString ( " Enter block number :\ r \ n " ) ;
3
13 UART 1_Rece iveLi ne ( input_buffer , sizeof ( input_buffer ) ) ;
14 block_number = atoi ( input_buffer ) ;
15
16 UART1_SendString ( " Enter difficulty :\ r \ n " ) ;
17 UART 1_Rece iveLi ne ( input_buffer , sizeof ( input_buffer ) ) ;
18 difficulty = atoi ( input_buffer ) ;
19
20 UART1_SendString ( " Enter previous hash :\ r \ n " ) ;
21 UART 1_Rece iveLi ne ( previous_hash , 65) ;
22 pad_ previo us_ha sh ( previous_hash ) ;
23
24 UART1_SendString ( " Start mining ...\ r \ n " ) ;
25 char * result = mine ( block_number , transactions , previous_hash ,
difficulty , new_hash ) ;
26 if ( result != NULL ) {
27 UART1_SendString ( " Found hash : " ) ;
28 UART1_SendString ( result ) ;
29 }
30 }
Listing 1: Main Mining Interface Code
6 Sample Test and Output
Input Case 1:
• Transactions: George→Brown→100
• Block Number: 1
• Difficulty: 3
• Previous Hash: 000000000000000000000000000000000000000000000000000000000000abcd
Expected Output:
• Found Hash: 000ab3c7d....
• Nonce value: 143
Input Case 2:
• Transactions: Alice→ Bob→ 50
• Block Number: 2
• Difficulty: 4
• Previous Hash: 0000000000000000000000000000000000000000000000000000000000001234
Expected Output:
• Found Hash: 00001cc9...
• Nonce value: 213
4
7 Results
The system successfully simulates proof-of-work mining on embedded hardware. Despite
resource constraints, the STM32F412 completes mining operations with user input, and
results are displayed over UART.
8 Conclusion
In this project, we have successfully implemented the proof-of-work consensus mechanism
on an STM32F412 microcontroller exactly as outlined in our objectives. We initialized
the system clock and UART1, integrated the mbedTLS SHA-256 library, and built a
user-driven interface to collect transactions, block number, previous hash, and difficulty
over UART. Our code concatenates these inputs with a nonce, performs iterative SHA-256
hashing in a tight loop, and returns the first hash matching the target difficulty. Bench-
marks show the Cortex-M4 core achieves approximately 100–200 kH/s. We did observe
occasional UART line-buffer truncation, indicating a future need for interrupt-driven
DMA or improved CR/LF framing. Overall, this work demonstrates that core blockchain
consensus primitives can be realized efficiently on a resource-constrained embedded plat-
form, setting the stage for hardware crypto offload and peer-to-peer networking in future
iterations.