esp32 bitcoin mining

Inspired by the ESP8266 Bitcoin miner i was curious how much better a ESP32 would perform. So i implemented the mining algorithm on it.

Bitcoin mining in a nutshell

It is simply a double SHA256 hash (hashing the output again) over the block header, a 80 byte sequence containing the version, previous block hash, merkle root (verifies the transactions), time, current difficulty and nonce.

The miner searches for a hash that is smaller than the difficulty value, that is why you can see that all all block hashes in the chain start with multiple zero bytes. To generate different inputs they vary the nonce value (and maybe time).


As demonstration you can see the double SHA256 in CyberChef, the line breaks in the input indicate the different parts of the header.

The developer reference is a also good resource to get more information.

ESP32 implementation

To make this experiment as realistic as possible i wanted to mine a real block, instead of only doing one hash. Of course with this low expected performance it makes no sense to try to find a new block (more on that later).

So i picked a already mined/known block, my choice was number 56333 from where i calculated the start parameters so that it finds a valid block with a predefined hash/computation amount.

The code uses the mbed TLS library (based on this tutorial) to do the actual hashing, in addition it checks every calucated hash, stops if it is valid or increases the nonce value and tries again… You can play around with it, i have pushed it to this GitHub repo, as PlatformIO project. The rounds variable determines how many hashes (and time) it will take.

After the mining stopped it prints the hash and the hash rate to serial, for example with 10 000 rounds:

Started mining...
Valid Block found!
Hash: 0000000000000000000740ba375bbafbb871e0e201ded9c260a8b6aa1537c3f0
With nonce: 423644052 | 0x19404b94
In 10000 rounds, 831.635000 ms
Hash Rate: 12.024506 kH/s

As you can see it achives about 12 KH/s, that is 10x as much as the ESP8266. This runs also only one core of the ESP32, utilizing both cores that means 24 KH/s!

Note about Hardware Acceleration

The ESP32 has hardware acceleration for SHA256 but it is currently not enabled in the Arduino Framework (GitHub issue). So i changed the sdkconfig and recompiled it. like described here, just replacing libmbedtls.a. But it has actually slightly lower performance… Espressif writes also in the docs that it could be slower, so i guess this is not unusual.

Let’s get rich!

Now what can we expect from mining on the real Bitcoin blockchain?

Currently a block takes about 600s with 40 EH/s, that means 24 ZH (Zetta Hashes, 1021) total. We can do 24 KH/s (103), so on average it would take 1018 seconds or 277 777 billion hours or 11 574 billion days or 31 billion years for one block!

The age of our universe is about 13.8 billion years, so i would recommend to use more than one ESP, to waste not too many universes ;)

One block gives about 12.7 BTC (12.5 + transaction fees) = 50 000$, so that makes 0.0000015$ per year. Not much and obviously not worth it as individual.

Even if you had 100 million devices (that would be more than 3 times the size of Bredolab, the largest known botnet to date with 30 mio. infected computers) it would produce still only bitcoin fractions worth 150$ per year.