Bitcoin mining sample code
These tables are called rainbow tables and rely on the input already having been hashed. The second step is to get the idea of a proof of work. It might be impossible to find a hash specifically with a string consisting of nothing but the letter "a" but what if we asked for a hash with a single zero at the front?
Altering the last letter of hello world took 26 attempts to finally get hello worlC which equates to 0d7eae0fab3abc2cccc0bb4aabb24ffaf8c. Why is this useful? Because it creates a puzzle whose difficulty is measurable and which it's impossible to perform better than blind guessing. That second property is important because it's the only way to create a fair "mining" system. Miners solve such puzzles as above but which are far more difficult.
For example, find a hash that looks like this: Each hash is can be considered to be just a number. For example, the hash ab3abc2cccc0bb4aabb24ffaf8c has a numeric value of So in mining, the miners have to achieve a hash with a numeric value lower than a specified number.
This number is called the target. If your hash attempt gives you a number less than the target, which is the same thing as having a bunch of zeros at the front of the hash, then you win and you get to "mine the block". To find such a small hash takes millions of attempts, or more accurately, the whole mining network, with everyone trying at the same time, needs millions of billions of tries to get it right.
The part of the content that they are hashing and are allowed to change, a single number, in order to try and get a hash beginning with zeros, is called the nonce. The current block reward of 25 Bitcoins is given to the miner who successfully "mines the block" finds the appropriate hash.
It's not really that mining "generates" the Bitcoin in any sense, it's just that it's written into Bitcoin code that a transaction block starts with a unique transaction called a "coinbase" transaction, which is the only type of transaction with no inputs. It only has an output, consisting of the reward plus the transaction fees.
To make any sense of Bitcoin's solution to this problem, you need to understand also what is meant by "distributed timestamp server" and how proof of work hashes can be used to construct this. It is, very briefly, explained in Sections 3 and 4 of the bitcoin whitepaper.
You're creating a sequence of blocks, tied to each other by including the hash of the last one in the next one. This proves that the next block knew about the last block remember, hashes are totally unpredictable , which proves that it came afterwards.
However, that's not enough; you might know that block 8 comes after block 7, but what if a different block 8, put in by a different miner, also comes after block 7? Worse still, what if these two competing blocks, 8a and 8b contain different transactions, spending money to different places? Which one is the "true" block of transactions? Bitcoin also relies on cryptography. The computational problem is different for every block in the chain, and it involves a particular kind of algorithm called a hash function.
Like any function, a cryptographic hash function takes an input—a string of numbers and letters—and produces an output. But there are three things that set cryptographic hash functions apart:. The hash function that bitcoin relies on—called SHA, and developed by the US National Security Agency—always produces a string that is 64 characters long.
You could run your name through that hash function, or the entire King James Bible. Think of it like mixing paint. If you substitute light pink paint for regular pink paint in the example above, the result is still going to be pretty much the same purple , just a little lighter.
But with hashes, a slight variation in the input results in a completely different output:. The proof-of-work problem that miners have to solve involves taking a hash of the contents of the block that they are working on—all of the transactions, some meta-data like a timestamp , and the reference to the previous block—plus a random number called a nonce.
Their goal is to find a hash that has at least a certain number of leading zeroes. That constraint is what makes the problem more or less difficult. More leading zeroes means fewer possible solutions, and more time required to solve the problem. Every 2, blocks roughly two weeks , that difficulty is reset. If it took miners less than 10 minutes on average to solve those 2, blocks, then the difficulty is automatically increased. If it took longer, then the difficulty is decreased.
Miners search for an acceptable hash by choosing a nonce, running the hash function, and checking. When a miner is finally lucky enough to find a nonce that works, and wins the block, that nonce gets appended to the end of the block, along with the resulting hash. Her first step would be to go in and change the record for that transaction. Then, because she had modified the block, she would have to solve a new proof-of-work problem—find a new nonce—and do all of that computational work, all over again.
Again, due to the unpredictable nature of hash functions, making the slightest change to the original block means starting the proof of work from scratch. But unless the hacker has more computing power at her disposal than all other bitcoin miners combined, she could never catch up. She would always be at least six blocks behind, and her alternative chain would obviously be a counterfeit. She has to find a new one. The code that makes bitcoin mining possible is completely open-source, and developed by volunteers.
But the force that really makes the entire machine go is pure capitalistic competition. Every miner right now is racing to solve the same block simultaneously, but only the winner will get the prize.
In a sense, everybody else was just burning electricity. Yet their presence in the network is critical. But it also solves another problem. It distributes new bitcoins in a relatively fair way—only those people who dedicate some effort to making bitcoin work get to enjoy the coins as they are created. But because mining is a competitive enterprise, miners have come up with ways to gain an edge. One obvious way is by pooling resources. Your machine, right now, is actually working as part of a bitcoin mining collective that shares out the computational load.
Your computer is not trying to solve the block, at least not immediately. It is chipping away at a cryptographic problem, using the input at the top of the screen and combining it with a nonce, then taking the hash to try to find a solution. Solving that problem is a lot easier than solving the block itself, but doing so gets the pool closer to finding a winning nonce for the block. And the pool pays its members in bitcoins for every one of these easier problems they solve.
If you did find a solution, then your bounty would go to Quartz, not you. This whole time you have been mining for us! We just wanted to make the strange and complex world of bitcoin a little easier to understand. An earlier version of this article incorrectly stated that the long pink string of numbers and letters in the interactive at the top is the target output hash your computer is trying to find by running the mining script.
In fact, it is one of the inputs that your computer feeds into the hash function, not the output it is looking for. Obsession Future of Finance.