What is a computer doing when mining for Bitcoin?

I am not trying to be a crypto miner but I am trying to understand what, exactly, is happening when a computer is trying to mine Bitcoin.

Vaguely I have heard it is trying to solve a cryptographic problem but I am unclear on what that is.

Is it simply trying a string of hashes to match to a given hash? Is it trying to factor a large prime number? Something else?

Also, if someone “solves” the current problem and gets some Bitcoin do all the miners in the world start over from square one on a new problem or do they just keep racing ahead?

From what I understand they’re doing math to locate new bitcoins, which get rarer over time. I think this might be related to finding factors for huge prime numbers. They are also doing the computations that allow the trade of previously found bitcoins between users, computing the resulting hashes from such trades. Doing this work is also rewarded with fractional coins. No single computer would be able to complete the trade hashes in any reasonable amount of time so the job is broken up into thousands of smaller steps and handed off to thousands of distributed mining machines to complete faster.

There are no prime numbers. The block chain consists of the sha 256 hash of the previous block plus the transactions in this block plus some random padding. The minor keeps trying different padding until the first N bits of the sha 256 hash of the block listed above are 0.

This is some what long video about how some of bitcoin works. I found it interesting.

They’re doing busywork. The cyptographic problem they’re trying to solve is arbitrarily created, and is created to be difficult enough that it will take a certain amount of time to solve based on how long it took to solve the last problem. Once one miner solves the problem, they all start over from square one. The cryptographic problem they solve doesn’t serve any purpose other than to make mining machines put in effort, it’s not related to any real-world situation or solving any kind of mathematical theory question. What they actually do is take the current block of bitcoin transactions, the hash of the last block, an arbitrary number, and a bunch of zeros for padding that make it take longer to do the transaction. They then run an SHA256 hash on that data, and hope that the result is below a certain target number. If it is, then they ‘win’ the next bitcoin, if it isn’t then they try another arbitrary number, and keep repeating until someone wins.

Without the arbitrary padding for difficulty, handling the trades would be trivial to run on a single desktop PC.

Thanks!

I am a little unclear on what the block is they use to determine the next block. Bitcoin transactions are happening non-stop and it takes a while for the network to reach consensus on what the actual chain looks like. How does the network decide that “this” transaction and not “that” transaction is what they should use?

The miner’s computer is trying to guess a number known as a ‘nonce’. The number is simply an integer that when added to a previously SHA256 hash, results in a new hash that starts with (for example) six zeroes - or whatever the difficulty level the software has chosen so that the puzzle is solved in about 10 minutes. The mining software starts with ‘1, 2, 3…etc’ and the average number of tries at the moment is about 500 quintillion.

This YouTube lifted the veil of mystery for me and is very simple to understand: Blockchain 101 - A Visual Demo - YouTube

The miner picks which transactions he wants to include in ‘his’ block. Usually he will pick transactions with favorable fees - fees he will receive along with the bitcoin reward if successful.

If a different miner is working on some or possibly all of the same transactions and is beaten to the winning post, his block is discarded and he starts again with a new set of transactions. He get’s nothing.

Remember that the successful miner has now ‘locked’ the previous block to the one he worked on ‘in stone’. THAT block’s hash was known to all miners working on a new set of transactions, and included in the calculation, which is another reason that the unsuccessful miners have to dump the transactions they are working on and start again.

As a seller, having received crypto, it is still good practice to wait until the block with your particular transaction inside is 5 or 6 blocks ‘deep’ before you ship the buyer his stuff. This is because some big mining pools are reeling off two or three blocks in a row which could potentially leave those newer blocks open to some shenanigans.

The reason this works is that SHA256’s output is impossible to predict based on its input other than actually running the whole algorithm. Bitcoin’s block chain is many many many blocks long, and the beginning of each block is the SHA256 hash of the previous block. The nodes accept the block with the longest chain, as it represents the most amount of work. In theory, someone could be running their own operation trying to create a blockchain with the same parameters as Bitcoin all by themselves, but unless they have more computing power than the rest of the world together, the rest of the world is going to be creating blocks that chain together faster. While you could trivially create a long block chain by setting the difficultly for each block low, I assume that there’s a standard as to how difficult each block must be, and a long chain that’s much easier to produce than the one that’s been going around forever is not going to be accepted on the basis of length - it presumably needs to match the difficulty of the other blocks as well. I’m not familiar with the details.

This isn’t what the OP asks, although the actual answer will be related, but, where do the new bitcoins come from, and why are they getting rarer?

But … . . but . .why is it worth any money when the find it?

They’re simply created, as in one of the transactions in the block that you successfully mine says ‘10 coins added to my wallet’ (or however many it is now). They’re getting rarer because that’s how bitcoin is designed to work, over time fewer and fewer are created until eventually there are none. They aren’t actually ‘located’ in any real sense, as they don’t correspond to anything in the real world, and the reason for ‘why’ is ‘because the guy who came up with bitcoin wrote it that way, and you can’t change it without getting buy-in from the people currently mining it’.

Because people want to spend money on them. There’s no intrinsic value to them, but some people like to buy them. A lot of people just put money in them temporarily to buy something with them (usually something illicit) or to transfer money to someone else. Some people buy them to speculate on their value getting bigger, or to execute scams with them, or because they are true believers.

The same reason anything is worth any money. Because people are willing to pay for it.

Other people have already described what mining is, so I just want to specifically point out the issues here.

Mining doesn’t locate bitcoins, the release of new bitcoins is a guaranteed reward to incentivize people to validate transactions. Bitcoins are not awarded fractionally, this is a side effect of distributing the mined bitcoin reward (currently 12.5 BTC) across a large number of miners in a pool. Eventually the reward will be 0 after all possible bitcoins are released and miners will be paid by transaction fees only.

There is nothing innately complex about mining. When Bitcoin was first established mining was done quickly by single computers. The difficulty of mining is directly correlated with the number of miners. In fact, increasing the number of miners directly increases the difficulty of mining. This is to resist Bitcoin being taken over by someone swooping in with a powerful computer. Bitcoin is designed for a block (batch of transactions) to validate with an average time of 10 minutes. If all the miners disappeared save for a handful, the difficulty would eventually drop back down to make solo mining practical again.

Gold and silver coins had value because the metals themselves had value. Of course, the only reason the metals had value was because everyone agreed that those metals had value. But these coins were based on a value of a particular material.

Bank notes like dollar bills were originally just slips of paper that said that such-and-such bank had a certain amount of gold sitting in its vault. You could go to said bank and hand them the slip of paper and get the gold back. While the paper itself was worthless, it represented something of tangible value.

Dollar bills used to backed by gold. In the 1970s, the U.S. abandoned the gold standard. Now, those paper dollar bills are only worth a dollar because everyone agrees that they are worth a dollar. There is nothing physical backing them up. A dollar bill does have the advantage that a very large government (the United States) says that they are worth a dollar, so that helps. If people lose faith in their government, those dollar bills could become worthless. For a couple of examples of this, you can look at Germany in the 1920s and Zimbabwe in much more recent years.

Bitcoins have value because people want them to have value. They have absolutely nothing backing them up, and unlike paper dollars, they don’t even have the advantage of having any government backing them up. The risk with these types of currencies is that, since they don’t have anything backing them, they could theoretically collapse, possibly overnight, and become completely worthless.

One of the reasons they have value is that you can’t just easily go out and make new ones. Their supply is limited. This is why they need the complex mathematical computations to generate them. One of the reasons Germany’s currency collapsed in the 1920s is that when their economy started to tank, the German government responded by just printing more and more bank notes. Since money was easy to generate, it had no value. Similarly, if bitcoins could be easily generated, then anyone could make a bizillion bitcoins and they would have no value.

On a similar note, the U.S. government prints its own money. We are trillions of dollars in debt. We could easily wipe out that debt by simply printing a few trillion dollar bills and paying our debts. But, if we did that, the value of the dollar would drop like stone and become close to worthless. We do on occasion just print up a bunch more money just because we want to, but it has to be done very carefully and in very limited amounts so that we don’t ruin the value of the dollar.

The longest chain is what becomes the consensus chain. Suppose 30% of miners start with Transaction A and 70% start with transaction B. As more transactions are processed the B groups chain will get longer and longer than the A groups. Eventually the A group will see this, give up, and switch over to B’s chain.

One way to think about Bitcoin is that it’s a set of rules for writing and broadcasting a ledger of transactions.

There are a variety of rules intended to accomplish goals like “make sure that transactions were actually made by people who control the sending account” and “make sure that the ledger doesn’t get too big”

As people have already explained, one of the rules is that in order to record transactions, you need to find a value that, when combined with the transactions, results in a hash output with certain properties. This is computationally expensive on purpose, since this rule makes it very difficult to “double spend” by going back and rewriting the ledger.

But, we also need a rule that will provide an incentive to do all those calculations and keep things running. And that rule is that whenever you mine a block, in addition to all the transactions that have a sender and a receiver, you get to include a single transaction with no sender that is received by the account of your choice. That’s how bitcoins are “created”. That’s the incentive to participate as a miner (well, there are also transaction fees. I assume people understand how those work). Miners aren’t “finding” bitcoins. It’s not like there’s a supply of yet-undiscovered bitcoins somewhere that can be found. There’s just a record of transactions, and a few of those transactions have no sender.

A (loose) analogy would be if the Bureau of Engraving and Printing, rather than paying their employees by check drawn on an account somewhere, just let them print up some cash to take home each week.

The amount of bitcoins that you get to put in that magic transaction is also specified in another rule, and it gets cut in half periodically, by another rule. Which is why you can calculate how many bitcoins there will ever be. Assuming none of the rules change.