In the introduction to bitcoin as a cryptocurrency, we will dive deep and learn about the nuts and bolts on how a cryptocurrency like bitcoin functions. Before learning about the functioning principles, we need to have a base on those which are the pillars to the cryptocurrency.

## Cryptographic Hash Functions

Hash functions are those which takes any input and gives a fixed-sized output (in our case we would consider 256 bits) and also are efficiently computable. However, cryptographic hash functions are those that have the features of a normal hash function and have certain security properties like:

- Collision-free
- Hiding
- Puzzle friendly

Going ahead we will learn why these properties are an important aspect of cryptocurrency.

### Collision-Free

The collision-free property of hash function states that if two inputs **x** and **y** are present such that **x!=y**, then the hash of **x** should not be equal to the hash of **y** which is [**H(x)!=H(y)**].

One of the applications of this kind is that we can store the hash function of a document (we will remember 265 bits hash instead of the whole large document) that we want to find later. And when required to retrieve the document, we can compare the hash that we remembered with the others and find the intended/original one.

### Hiding

This property states that if we are given the output of the hash function say **H(x)**, one should not be able to find the value of the input, **x**. This property, however, holds good when the input is chosen from a space where distribution is very spread out (high min-entropy).

One of the applications of this is that commit a value in a folder and seal it. None should be able to guess the contents of the folder unless it is opened later.

### Puzzle Friendly

This property states that for any possible output value of **y** if **k** is chosen randomly from a super spread out then it is infeasible to find **x** such that **H(k|x)=y**.

One of that application of this kind is building a search puzzle where the solution can only be found by trying random inputs.

The hash function used by Bitcoin is SHA-256, which generates an almost unique 256-bit (32 bytes) hash.

## Hash Pointers

Hash pointers are pointers to where some information is stored. We can retrieve the value of the information also we can verify that whether the value of the information has changed.

## Data Structures using Hash Pointers: Linked List

This is a linked list in which each block contains some data in their body and the hash of the block is stored in the header of the previous block. And during the genesis, the hash of the 1^{st} block is stored as ** H ( ). **This chain of blocks is also known as blockchain, which is a collision-free and tamper-evident log. Let’s say an adversary tries to tamper with the data and changes some data in the block number 3. Now, since the data has been changed, the hash of the block 3 which is stored in the header of block 2 will not match up with the original hash of block 3 since we have learned that hash functions are collision-free. So, when two different inputs are hashed, they won’t yield the same result. Again, say to match it up, the adversary changes the hash of block 3 (header of block 2) and again to match the hash of block 2 (stored in the header of block 1), the adversary tampers the hash of block 2 and so on. As the adversary goes on changing the hashes he will eventually end up with a roadblock where we have the hash of the 1

^{st}block which was created during genesis. And if he tries to change it, the former value which we had remembered won’t match up with the present one and thus it will be evident that someone has tampered with the log.

## Data Structures using Hash Pointers: Binary Tree

A binary tree that is made with hash pointers is also called Merkle tree. Binary trees have the advantage of holding many items. They are also sorted, and we do not need to remember all the values in the tree except the hash of the head which is a 256 bit.

In general, hash pointers can be applied to any pointer-based data structure that has no loops/cycles else there will be no way left to verify the tampered data.

## Digital Signatures

Digital signatures are nothing but real signatures in digital form. The capabilities of digital signatures include making your own signature, others can verify your signature and your signature should be tied to one document only.

The API of digital signature has the following methods:

`generateKeys(keysize) : (sk, pk)`

*where sk and pk are the secret/private and public keys and are returned from the generateKeys method. Can be randomized.*`sign(sk, message):sig`

*where sig is the signature which is returned from the sign method. Can be randomized.*`verify(pk, message, sig): isValid`

*where isValid is a Boolean type, returning true or false.*

The goals of any digital signatures are that:

- Valid signatures must verify.
- None can forge other’s signature.

So, keeping above things in mind if we tend to sign a hash pointer at any position of data structure, it means that the signature covers the whole data structure as all information are linked to each other from the genesis.

A good randomness is necessary to avoid adversary from guessing the signature (leakage of the private key).

The algorithm Bitcoin uses is ECDSA (Elliptical Curve Digital Signature Algorithm).

## Public Keys as an Identity

The main motive in a decentralized system is to make something as an identity which is not vulnerable and that is the public key. Anybody can generate a random public key. The decentralized identity management allows anyone to make a new identity, at any given point of time and as many as they want. Identities are nothing but addresses in any cryptocurrency like Bitcoin. And, these addresses are simply the hash of the public key.

## Privacy

Privacy in bitcoin is a complicated topic and will be addressed more in future. But as of now, we can say that the addresses are completely private as they are not linked to real-world identity. However, observers can accumulate altogether the activities of an address and make inferences.

## Functioning of a Simple Cryptocurrency

Let us understand how a simple cryptosystem works. Let’s assume Mr Snail has the authorization of making new coins and can pay it to other parties in exchange for some work done and others can also do the same except create new coins. So, at first, Snail passes the coin to Manu’s public key (address) and signs to it. Manu then passes the coin to Tim and signs in the transaction. Now, everything looks perfect except there is a big loophole in this system. Manu can and now passes the same coin to Wane, which was earlier paid to Tim and signs to the transaction. This kind of spending coin is called the Double Spend attack/problem where the participant spends the same coin twice. So, we need to find a solution to this problem such that the adversary, Manu can’t double spend a coin and others can catch it easily.

So, in order to solve this problem Snail decides to broadcast and publish all transactions publicly in a network so that all other participants can view it and detect any potential double spend attack. The history of transaction blocks is quite similar to what we saw in the Linked List with hash pointers except for in the header along with the hash of next block contains a unique transaction id and the body contains all the transactions. Now that the ledger is visible to all, everyone can see how much others have and when Manu tries to do a double spend attempt, the transaction from Manu will be rejected by the participants in the network and not added to the transaction history and others will be alerted of the adversary. So, now that we have solved the double spend attack, we have another problem to solve, that is centralization. If say, Snail’s interests get disrupted and he stops the creation of coins then the whole system will fall apart. That’s why we need to decentralize the whole system and we will see in our future articles how we can do the same and build a network which is almost similar to what Bitcoin is.