# How Mining Works: The Cryptographic Puzzle

**A free video tutorial from**Hadelin de Ponteves

AI Entrepreneur

4.4 instructor rating â€¢
73 courses â€¢
964,648 students

### Learn more from the full course

Blockchain A-Zâ„¢: Learn How To Build Your First BlockchainHarness the power of the most disruptive technology since the internet through real life examples! Master Blockchain Now

14:38:58 of on-demand video â€¢ Updated May 2020

- Why this is important
- Understand the theory behind Blockchain
- Create a Blockchain
- Understand the theory behind Cryptocurrency's
- Understand the theory behind Cryptocurrency Transactions
- Create their own currency
- Understand the theory behind Smart Contracts
- Create their own Smart Contracts

English [Auto]
All right part two how mining works. Let's have a look. So we left off last summer here where we have this block and we now know that we can control or we can vary the hash of the block by varying the non-SS the number the actual field that we identified. OK so how does this tie in with money. Well let's have a look. The first thing that we're going to need to do is to make a statement. The statement is that a hash is a number. And if you already know this then this will just take like a minute if you don't and this will be very useful for us going forward. So here is an example of a hash. This is a proper shower 256 hash it takes up sixty four bits 64 digits. Sixty four is here sort of. And the statement is that this is actually a number it's not just a word it's just a combination of characters and just the label. It's an actual numbers. You can increment it you can do both medical operations with it. And why is that. Well a hash is actually a hexadecimal number. So here you can see digits from 0 to 9. And also you can see some letters but the letters are limited to f as a b c d e f. And what a hexadecimal number is is it's like a decimal number. But instead of just having digits from 0 to 9 it also has those six extra ones so a stands for 10 B stands for 11. C stands for 12 D stands for 13 E stands for 14 F stands for 15. And so that's all there is to it. And we can actually convert this hexadecimal number into a normal decimal number and it will look like this. So there's our decimal representation of our hexadecimal number as you can see the number is visually longer and that's because in the decimal system we only have 9 digits whereas in the hexadecimal system we have in the decimal system we have 10 digits. Funny but in the hexadecimal system we have 16 digits so therefore every digit can encode at a higher value. If you if you will if you want to call it that every digit will encode at a higher value and the hexadecimal system and therefore visually the number is shorter. But in essence the overall value of the number is actually exactly the same as just represented in a different system. It's like we can take a decimal number and convert it to a binary into ones and zeros will be very long but it'll still be the same number just represent indifferences system. Same thing with hexadecimal we can take the same decimal number to merge hexadecimal. We can convert to a system we like we can call it into system of the base of 57 if we if we wish but just that those are the commonly used ones the decimal one which we use in the real world binary which computers use hexadecimal is also used for some things. One of them is hash numbers. Then here's another example. There's another hash number. Also of course hexadecimal. This is another shot 256 hash as you can see at the start there is leading zeros. You don't have to write them out. I included them here just to illustrate that the visual length of this number is the same as the previous one. It is also 64 characters or digits in length. And if you convert this to a decimal number there we go. There is a decimal version as well. Don't have to write out the leadings there is just a smaller number. And here's another hash is also. This one is even smaller because it has so many more leading zeros. And this is representation and desk. OK. So we've identified that hashes a number what does that give us now. Why is that valuable. Well now we can actually illustrate this. We can draw hashes because we know that there are numbers so we know they are consecutive or some are smaller Some are bigger. So let's draw a pool of hash. We call this pool the pool of all possible hashes at the bottom we have the smallest ones top we have the largest one. And basically what this says is this is all possible variations of this hash value so starting from the smallest that 0 0 0 0 and then going all the way up like that. Doesn't matter like how exactly is structured. But like I say it goes like a snake all the way up. It increases increase increase increase increases until it gets the very top where that what will the highest one be. While that's when all of these digits are f f f f f all the way. So that's the highest. And so we got to order them like that and it's going to be like a map for us. And so now we're going to see where these hashes fall on the map. So we know that. Well let's say this top one for somewhere over there. This one is smaller so it has to fall lower. This was was as far as low. And so there we go. Those are our hashes there illustrated on the map. We can get rid of the decimal representations now. And this leads us up this is a very nice set up. This is in combination of the previous tutorial. Very nice set up for what is mining all about. Like why is what's this whole fuss about. How does it work in the start of how does it work. Will get to the US after that. So how mining works is in essence the blog Shanes system or the Bush algorithm will set a target so there's a target set for minors to accomplish a certain hash. So let's say the targets are here target. There we go. That's our target. And the way it works is any hash. Higher than Target doesn't count. So if you find a hash for your block that is above the target that doesn't count. That's not good enough for the blocking. It has to in order to be include in the blog. It has to be locked be below the target. And the important thing to know is this is completely arbitrary. There is no there is no economical reason for it. There is no like logical or computational mathematical cryptographically reason for it. It's just a way to put hurdles in the way of my mind has to create a challenge for them to solve. That's the only reason they're there is this started. No other is. And so in this case if a minor found this hash it wouldn't count to me not good enough there would not be allowed to create a block with this hash number. This hash is also not good enough. It's above the target. But if you found this hash you'd be welcome to create the blog. You'll be allowed to create a blog and that's that's when you will be considered the miner who actually mined the block because you found a hash like that and will like will. This is like just in a high level we will go into detail like in a second in this but before we do I wanted to give you guys a small tip that a good way of thinking about the target is actually in terms of leading zeroes. So as you can see the lower you are the smaller the number and the more leading zeros will be. So if you think of the target rather than as it like I remember trying to remember what the target is in terms of hashing. Just remember the number of leading zeros for instance for eating zero. So let's talk about that and it actually in the practical tutorials and will be showing you how to code a block chain and you will be using these for leading zeros. You'll see that in the practical side of things. So that's how we're going to think about the target. And now let's let's discuss what we just talked about the whole mining because I know it's right now. It's very vague very unclear so let's make it clear make it super clear after the story you walk away and your higher percent sure what mining is all about. So let's go back to our what we discussed in the Prius. So here we've got the block and we know that what we have inside this block is the block a number of the non-SS the data the previous hash and most of the fields here are you cannot touch. You cannot tamper with them you cannot change the block number because the block number. So we're trying to add forget about this part. It's not it's is. We only have the chain on the left there's no more chain on the right. Right. So this doesn't exist. So we want to add and you block so we know that block number we're going to be adding block number three. OK. So you can change that the data in the block. So as we discussed we don't want to change the data we can't change it. In reality it is a bit more to it like in module 2 of this course will understand how we can change or vary the transactions that go into the block. But for now for us to understand how mining works is good enough to agree that we have this set of transactions that needs to go into the block and we cannot change it because if we agree that the list of transactions that then we cannot change any details of each section because we don't want to tamper with the data. And that's the whole concept whole point of block chains that it's an immutable ledger. Then we can also change the hash of the previous block. Chant cannot change this value because it has to be cryptographically linked to the hash of the previous block. So the only thing that we can actually change in this whole block is the nots and the non-SS let's us allows us to vary the hash of the card block. So let's see what that means for mining. So if we plot this hash onto our map of hashes you'll see that it might be somewhere over there. It's hypothesized that it's over there. And that's that's the hash and this label is just to remind us that this hash was generated from this block when the non-SS was 23. So next let's change and wants 22. Now you can see the hash is over here it's the smaller hash and that was generated by non-SS 22. And so we could just keep going doing that because we will not be allowed to add this block into the block chain until the hash is below the target. So all we can do is just keep guessing different nonces as you can see there we go. And that's essentially what miners do. Miners just sit there and they change this field they changed the non-SS in order to try guess a value of the non said will generate a hash below the target. That's as simple as that. And once they do generate such value So for instance they find unknowns at some point at random by accident through brute forcing they find the non-SS. For instance 5000 12 that generates a hash below the value. Then they win that's it. They get this nonces commonly called among miners. It's called the golden knots and because it generated that has military as you can see as those four leading zeros is below the target. And that said so then once that's done they're allowed to add the block to the block chain and they get the reward which again will discuss much a lot better. But the whole point is that that's at that point that's when the block is accepted by the block chain only when the hatch is below the target. The rest of the time all these the the whole thing that these miners are doing is they're just sitting there or while there are these and knows these rigs of nodes and they're just churning away they just iterating this non-SS changing a changing and changing it to hopefully guess the right hash and whoever gets it first adds a block and then the whole thing starts again for the next one. So there's a couple of three important things to point out here. So very discussed about the transaction there. We'll talk more about that and module 2. The important thing about hushes is the avalanche effect so remember we discussed the avalanche effect that by changing the nons a tiny little bit the hash changes complete and why that's important for mining is to keep people from cheating the system. So here you can see that the non-SS 4:23. So the hasher 23 Let's go back. So here you can see that the hash 4:23 was up here then the hash 4:22 is down here. And if for instance if changing the notions a little bit would change the hash a little bit that would allow miners to predict that. OK. Looks like by changing by reducing the odds the hash is going down so we'll have to go is 21 2019 18 17 and finally I'll get below the target. I'll get over here but that's not the case. As you can see at 21 it goes up here you know 76000 is here but 143 is here is this completely all over the place is completely unpredictable and that's a very important feature. Now we can see that why that's such an important feature of the hash why the avalanche effect is so important in the hash because we thought that this whole cryptographic puzzle this concept of finding the hash is called the right hash is called the cryptographic puzzle. This whole cryptographic puzzle would not even exist because you could just like find the way you find your way like a shortcut into getting a small astronaut. And that even further illustrates the importance of the hashing algorithm like the reason for us using a hashing algorithm is because you cannot reverse engineer it. You cannot crack it and predict what hash you will get based on the non-C input as soon as somebody is able to do that. The whole of this concept is going to fall apart because people will be just being able to predict the non-SS and then just put in and they'll know what hash they get so they won't have to put in the work in order you know put in like hours and hours and hours of machine time in order to crack unknowns in order to get the right hash below the target. They'll just put it in the know they'll calculate what mods they need to get a high below the target and that's it. They will put in the block. So that's what the 256 promise is that nobody's been able to crack it. There's been attempts lots and lots of attempts to do that people but people haven't gotten even close to cracking it. They were previous hash algorithms such as shall one M.D. I think Google and before him do D5 which have been cracked. But every time they've been updated into stronger ones. We've got shots of 56 there's also shot five 12. There's a shot three and so on that Algren's that was wrong. But for now plug chains use term and 56. So those are some important features to remember about or to know about the hashing algorithm why it's so important in Blodgett's. OK. So that's the second important thing that we needed to point out here. So there we go that's our golden Non's at the bottom. And yeah so that's in essence how mining works. You just need to keep iterating the notes until you get a hash below the target then you get to add that block or whoever gets there first guess at that block and then the whole thing starts again for the next for the next block. Yeah. And so finally I just wanted to ask you if you see there's an error on the slide and I was wondering if you were able to pick it up if you were able to see it. Actually I just noticed it myself just not so long ago. This previous hash is it couldn't be possibly correct because it has to start with four leading zeros right. Why the block number 2 would have not been accepted into the chain. If this hash started not before leadings or so that's that's an error on my part preparing the slides but it illustrates kind of like reinforces the concept that blocks would have to be below the target the hashes have to be below the target for blocks to be except So in the in the proper blotching this previous hash would have been also below the target. So there we go that's in a sense like this is how conceptually mining works of course there's lots more to it like for instance we talked about the fact that we can actually there's ways for us to control what transaction going into here. We'll talk about that and more. There's more to the non-SS there's more components or not there is actually more fields that go into my book and we'll talk about those things in modules too. But so if you're like if you already have some questions and some things that you know you might have read somewhere about blotching mining or you have some ideas already and some things for you right now like are you know like you've got more questions that haven't been answered yet. Don't worry we'll answer those in module two of the course. And going further into further details. But right now this is what we need. This is the foundation that we need order to progress with this part of the Course Mudgal one and one with the practical trials that are coming and after that we'll build on top and we'll increase our foundation and increase our knowledge like incrementally as we go through of course. On that note thank you so much. I hope that now it's clear how mining works and what this whole fuss is about what the cryptographic challenge is why people are racing to get to the best the golden non-SS and why there's so many miners around the world why they need the computation power and I can't wait to see how the next eternal until then. Dirk Lachance.