# Cryptography 101Bitcon

Lets start with the basics, in my previous post “Paid to put a block on a chain -Blockchin”, I mentioned that in order for me to send you a Bitcoin I’ll need your public key. Using that key I’ll encrypt the transaction, send it to you and you’ll decrypt it up using your private key. But there is some information I missed out which I’ll cover now.

**Private and public keys**

When you create a wallet, you’ll have a public and private key generated for you. The public key is created from your private key, this is important for when we sign our transaction, how else do we know where the transaction came from? This stops people from committing fraud, ensure tighter security for all. And a private key is made up of 51 alphanumeric,upper and lower case characters.

“Security concern” — Can I reverse a public key to find out the private key. Yes! It would take you around **204 trevigintillion years** and that is using **major **computer power! This is all thanks to the mathematical algorithms used to generate these keys.

When I send you a Bitcoin using your public key, I’ll sign the transaction with my private key to say that it came from me. So when you receive it, you’ll decrypt the transaction using your key but you’ll see my signature, but you won’t make head and tails of it because the signature needs decrypting as well! This is done using MY public key and once decrypted it **won’t** give you my private key but it give you data that proves that I sent it.

Here is another nice image to give a visual explanation of the above.

### Miners

So, if this is so secure why do we need miners? Why can’t we use the current transaction to update the blockchain with this record. Well that’s because it stops people from double spending, adjusting the blockchain and compromising the validity of the transaction. By knowing the private key, a single party or multiple parties can work together to run riot by simply reversing the transaction, double spend using the same transaction ID.

What the miners will do gather the transaction into a “transaction block” and hash the value(value is like the data within the block, that contains the details of me sending you the Bitcoin) in the block to generate a new set of values.

Since the miners don’t have access to your private keys, they cannot reverse the original block and by changing the value, no one else with the private key of the original transaction can reverse the new values produced by the miners.

So the blockchain requires the value to have 4 zeros in front of the original value before it can be added to the blockchain. This stops those with the private key to tamper with the value, ensuring more security and validity of the transaction. When the miners make the changes to generate a new value, this is called hashing.

Here is an example — I created a simple hash function in python to hash a value (note that the values used by the cryptography method are extreme more complex. Like RSA)— *hash(“medium”) =1464919875 hash(“medium!”)=*1121407273

As you can see the addition of ‘!’ has changed the numeric sequence. So if I wanted to get it added to the blockchain I need a set of zeros in front of it. But I don’t know which value they would be, but that’s where all the computer power, time and complex algorithms come into it. The computer power is used to find these hash value and it speed depends on the power, this where the term hashing power comes into play. How much power does your computer to have depends on the amount of hashes it can calculate in a second.

Hence why you get paid for mining. But for time saving, I’m just going to want a single four at the start- “435892841”.

Now if that’s all you had to work on for a Bitcoin transaction, or even maybe a little more it would still be almost impossible to work out, I say almost because it could take a crazy amount of lifetimes to work out :) and I cheated here, I just said “a single four” because I couldn’t get a single zero in front of it ;)

Now as time goes on, formulating these hashing gets more difficult because every 2,016 block the network changes the required hash value.

The difficulty could increase of decrease, but over time is mainly increase which means miners need to increase their power to compute more complex hashes.

### Merkle Tree

In order to ensure data integrity, the Merkle tree process comes into play. Merkle tree is used to verify the large data structures and as we can see from above the calculations and outputs can be complexly large. The process of hashing the data currently used is the Merkle tree. Where the miners hash the values through the merkle tree, gain a merkle root. The merkle root can be used in reverse to ensure no data has been tampered with. For example:

the numbers 1,2,3,4 are the transaction. The black lines represent a hash process and the outcome. So here hash(1+2) = 3 and hash(3+4)=7.

Then hash it again hash(3+7) =10. 10= Merkle root. So, if you only have the value 10 and I give you the number 3 and 4 you can work out the full transaction in reverse. 10–3 = 7. From 7 -4 =3 and then the remaining numbers are 1 and 2 on the other side. This will authenticate the transaction is valid and the miners have done their job as well.

Because if you change a single value then the values will change leading to a faulty transaction.

In Bitorrent merkle root is obtained from a trusted site, that root is then compared to other sites and if they match , the torrent will go through it’s peer to peer process but if it doesn’t match then it will disregard it.

So after all this, just the tip of the ice berg, is what it takes to get a transaction verified, secured and added to the blockchain.

Now I’m going to be honest, I’m pretty bad at explaining the technical aspects of these topics, but that’s where practice, error and learning comes into it.

But I’ll be going further into depth about the public, private keys, hashing and other aspect of cryptography because this is the key pieces of information and process that make Bitcoin so secure and other **crypto**currencies. If you can get your head around this and make major ground on here then you will definetly be ahead of the curve, even though each cryptocurrines has its unique features.

As always, feedback etc is welcomed!