# Tutorial # 4.2: Understanding cryptocurrency (Bitcoin) – The workings

As I mentioned previously (in the other post), cryptography is at the heart of a cryptocurrency. Hash functions and public-key cryptography play a key role here.

a.  Hash function and digest: A hash function outputs a fixed sized data called digest for an arbitrary sized input. This fixed size makes it ideal as input for various mathematical operations. Hashes are deterministic (always produce the same output for the same inputs), computationally efficient (requires little computation to process) and collision resistant (difficult to find two inputs that produce the same digest). The major application of the hash function is the creation of digital signatures.

b.  Public-key cryptography: Public-key cryptography uses a pair of keys, a public key that is known to everyone and a private key that is known only to the owner. While the public key can be used to verify the identity of the sender, it cannot be used to derive the private key and thus imitate the sender.

First, let me cover what a transaction consists of. A transaction record covering a transaction contains an input sideconsisting of the digest of the transactions received the sender that are unspent. This can be used to publicly verify the amount as well as from whom the bitcoins were received by the sender. It also contains an output side which indicates to whom and how much is to be sent. Depending on the amount to be sent, multiple input transactions are combined so as to cover the amount. It is important to note that the amount so derived is to be completely spent. For this reason, the output side would most probably also include the sender along with the amount that is to be received as change. Any difference will go to the miner as transaction fee.
Eg. Alice has 50 bitcoins as a result of receiving 20 each from Carol, Dan and 10 from Erin. She is to send 45 bitcoins to Frank. Thus, all 50 bitcoins are included on the input side and 45 is included on the output side. However, Alice wishes to receive 4 bitcoins as change (with 1 contributed as transaction fee) and so will include her own name on the output side.

It is importantto note here that using the names above we are speaking of virtual identities. In the cryptocurrency universe, you are only identified by your public key. Thus, the transaction record contains the public keys of the sender, from whom the sender received the bitcoins and to whom the bitcoins is being sent.

Now the public key cryptography mechanism works as follows:
• The digest of the transaction record and the private key of the sender are mathematically combined to generate a digital signature
• The digital signature is appended to the transaction record and shared publicly
• The transaction record, digital signature and the sender’s public keyare used by the various nodes in the bitcoin network to mathematically validate that the transaction record has indeed come from the sender.
• Once validated, the transaction is then processed further to ensure that the payment is transferred to the receiver as per the transaction record.

The transaction validation and its recording in the public ledger takes place in the following manner.

Multiple transactions, similar to the one described above, are combined together in to a transaction block by a miner. A miner is like any other node (user or peer) in the system, but one that is specifically trying to solve a mathematical problem. Solving of this mathematical problem results in the validation of the transaction block and thus all the transactions in it.

So what kind of mathematical problem are we talking about here?

The solution to the mathematical problem involves another cryptographic concept known as proof of work puzzle. The premise here is that mathematical combination of a challenge string with proof of work has to generate a pre-defined output string.

The challenge string is basically a digest of the transaction block. The digest is calculated by hashing two pairs of transaction at a time till a single hash is obtained for all the transactions in the block. This hash in turn is combined with the hash of the previously accepted transaction block. Thus, each new transaction block incorporates the previous transaction block.

The output stringin case of bitcoins has a predefined number of leading zeroes which is a condition that must be met for the mathematical problem to be considered as solved.

Thus, the proof of work is the only unknown here. The miner has to calculate this so as to be able to combine it with the challenge string and thereby produce the output string. Generation of an output string containing 40 zeroes as prefix will have 2^40 possible combinations of proof of work. Thus, you can imagine that a miner’s work is really hard. While, generating this proof of work is rather difficult, verifying it is particularly easy for one has to simply mathematically combine the suggested proof of work with the already existing challenge string and verify whether it produces the condition set for the output string (i.e. 40 leading zeroes).

The really interesting thing to note here is that the effort involved in the proof of work can easily be doubled or halved by increasing or decreasing the number of zeroes required in the output string (since 2^41 is equal to multiplying 2^40 by 2 and 2^39 is dividing it by 2). This concept is used to control the difficulty and pace of bitcoin generation as mentioned in the other post.

Generating this proof of work is akin to verifying the transaction block and thereby all the transactions within it. As each transaction block contains details of the previous block, it effectively forms a chain called as transaction block chain. As soon as a miner constructs this transaction block chain containing proof of work, it will be broadcasted to all nodes in the network. Once the newly broadcast chain is verified to contain valid transactions that are not already spent by the other nodes on the network, they will start using it and try to append their own transaction blocks to this newly verified transaction block chain. In this manner, each and every bitcoin transaction is validated and executed.

Thus, we can see that the transaction block chain contains information of all the transaction blocks and in turn of all the transactions that had ever taken place. In this context, the transaction block chain is the general ledger, the transaction block is a page in this ledger and the transactions within the block are the line items in the ledger.

Questions may arise on the possibility of simultaneous proof of work solutions, fudging of the ledger or double spending by the sender and this is tackled in the other post.

This site uses Akismet to reduce spam. Learn how your comment data is processed.