Bitcoin Mining Algorithm Explained

Hey G33ks,With the current mining craze, A very frequent question I ask about mining is: "What is my PC actually doing?" So for those people, I have decided to make this post.This post does assume that you already know what mining does in the global lines. If not, then go watch this video first: [embed]https://www.youtube.com/watch?v=GmOzih6I1zs[/embed]It also assumes that you know what hashing is and what it does. If not, then go watch this video: [embed]https://www.youtube.com/watch?v=--tnZMuoK3E[/embed]I have written the protocol in Python for this demonstration. The core principle will remain the same across all the programming languages, however, the exact way it is done will differ. This post also only takes into account the SHA256D hashing algorithm used by Bitcoin. While other coins like Litecoin (Scrypt) and Ethereum (Ethash) still use this general principle, the specifics (eg. outputs) might be different. Now that we have that out of the way, let's get into it!

Proof-of-Work and Difficulty

First of all, why does Bitcoin require so much hashing power in the first place? Bitcoin uses aan algorithm that is called proof-of-work (PoW). This function basically requires you to be able to show that you've put a certain amount of effort in a hash. You can compare it to a school project. You don't just have to make the project, but also put some effort in it (which can be proven both by the way it looks, the way it functions and whether it satisfies the evaluator's requirements). In Bitcoin, this is exactly what the PoW algorithm is for! Take block #505259 for example: it has the following hash:
0000000000000000006b7c05a0f698a1e2f848f283ecc3cd0c8b8763a152ca00  
You see all those 0's in front? That is the requirement for the PoW algorithm! The more 0's a hash needs, the more difficult it is to find a working hash! As you know, a hash changes dramatically when changing even the slightest amount of data. And you have to change a specific value a lot in order to find a suitable hash. This is also what is called the mining difficulty, as it requires exponentially more effort to find more 0's in your hash. One 0 can be found quickly by any CPU. Two 0's takes a bit longer, but still doesn't take a lot of time either. Three 0's can be noticeable depending on the CPU. Four 0's can take a bit of time depending on the CPU. Five 0's might take you a while depending on the CPU. Six 0's will take even a decently fast CPU a while. Seven or more 0's can easily take an hour.This difficulty (the amount of 0's it takes to find a satisfying hash) is adjusted to become more or less as the total network hash rate decreases or increases. This is done to make the amount of time in between blocks somewhat stable. Remember, the higher the hash rate, the sooner you will find a satisfying hash, thus find a block.Not only that, but even with the same amount of 0's, you also need to get your hash below a certain target. For example, you and your friend throw two stones each. The target is at 10 meters, but here are some rules: You can't go over 10 meters (the target). You can't go below 9 meters (can be compared to the amount of 0's needed in a hash). Your friend throws his first stone and it lands at 10.5 meters. Then it's your turn. You throw your stone and land at 10.2 meters. You are still too far away in order to win. Your friend then throws his other stone. It lands at 10.1 meters. Still not close enough. You then throw your other stone. It lands at 9.9 meters. Your stone is landed farther than 9 meters (thus has enough 0's) and closer than 10 meters (the target). Therefore, you win!

So what are we hashing exactly

Second, we need to know what we are hashing exactly. The In Bitcoin, we hash a so-called "block header". This header contains all the information we need in order to both satisfy the PoW algorithm and validate a block (and making transactions permanent). This is the data that we need to hash in order to create our block: [table] [thead] [th]Field[/th] [th]Purpose[/th] [th]Updates when...[/th] [th]Size (bytes)[/th] [/thead] [tbody] [tr] [td]Version[/td] [td]The Block version[/td] [td]You upgrade the software and it specifies a new version[/td] [td]4[/td] [/tr] [tr] [td]hashPrevBlock[/td] [td]The hash of the previous block[/td] [td]A new block is mined[/td] [td]32[/td] [/tr] [tr] [td]MerkleRoot[/td] [td]a MerkleRoot hash of all transactions in the block[/td] [td]A transaction is accepted by the miner[/td] [td]32[/td] [/tr] [tr] [td]Timestamp[/td] [td]The current timestamp in seconds since the epoch[/td] [td]Every few seconds[/td] [td]4[/td] [/tr] [tr] [td]Bits[/td] [td]Current target in compart format[/td] [td]The difficulty is adjusted[/td] [td]4[/td] [/tr] [tr] [td]Nonce[/td] [td]32-bit integer starting from 0[/td] [td]A hash is tried (incremental)[/td] [td]4[/td] [/tr] [/tbody] [/table]So now that we have the data we need, how do we create our block header? Well, this is where my Python code comes in :)First of all, we gather all of our variables. "] We then slap all of our values together into out block header! We then hash our block header using SHA256 twice ("creating" the SHA256D algorithm). We then reverse our output hash and compare it against our target (again, the hash value has to be lower than our target!) If we have a valid block, we can then broadcast it to the network together with all the data we used to get this block (so that they can verify wether you didn't cheat). 
 
Now that we're on the topic of cheating, with Mining, it is very difficult to cheat. Everything can be re-checked on validity. Remember, if you change even the slightest value, the entire hash will change :  
 
And with that said, you should now know how the blockchain works! At least, I hope you do. If you don't understand it yet, please don't hesitate to ask me a question down in the comments.I'll see you on the blockchain! [g33kout]

Comments


Leave a comment


Please login to leave comment!