An Introduction to Cryptography and Private/Public Keys in Ethereum

An Introduction to Cryptography and Private/Public Keys in Ethereum

Introduction

Anonymity is one of the characteristics of decentralized blockchain networks. No personal information is required to create a crypto wallet and there is no central entity like a bank that keeps track of each account by making sure no one can withdraw funds that they don’t own. So how does the Ethereum protocol function without a central entity?

With the use of cryptographic and mathematical logic, private and public keys are generated and they are used to control access to cryptocurrencies stored in crypto wallets. In this article, we are going to learn about public and private keys, how they work and why we need them in the Ethereum blockchain.

Before we can fully understand what private and public keys are, we need a basic understanding of what cryptography is, what it is used for and why we need it.

Cryptography

Cryptography means “secret writing” in Greek. It is a method of securing data from unauthorized access. This is done by encrypting the data into a form that wouldn’t make sense to you unless you use a predefined key to decrypt the data.

Here is a simple analogy that will help us understand what cryptography is: Let's say there are two friends Bob and John. Bob wants to send John a message via the internet but there is a notorious hacker who is waiting to intercept his message before it reaches John. The hacker can intercept the message and change its contents before sending it to John.

Fortunately, Bob knows about cryptography. So he encrypts his message before sending it to John. At the other end, John already has a secret key which he uses to decrypt the message. This way, the hacker cannot understand or tamper with Bob’s message to John.

Cryptography is broadly classified into two categories namely:

  1. Symmetric key cryptography

  2. Asymmetric key cryptography

We will focus on asymmetric key cryptography because it is the class of cryptography that is used in the Ethereum blockchain

Asymmetric key cryptography

Asymmetric key cryptography is also known as public key cryptography. It works by using two keys which are the public key and the private key. These keys come in pairs. Public keys are derived from private keys.

The keys are mathematically linked in such a way that it is easy to calculate the public key using the private key but it is practically impossible to calculate the private key using the public key. Sounds like magic right?… Well, it’s not.

This is possible using a special function in mathematics. It is regarded as a one-way function. It is easy to calculate them, but hard to calculate their inverse.

These special functions are based on arithmetic operations on an elliptic curve. In elliptic curve arithmetic, multiplication modulo a prime is simple but division (the inverse) is practically impossible. This is called the discrete logarithm problem and there are currently no known trapdoors.

What is a trapdoor function

A trapdoor function is a function that is easy to perform one way but has a secret that is required to perform the inverse calculation efficiently.

For example, multiplying two large prime numbers together is trivial. But given the product of two large primes, it is very difficult to find the prime factors (a problem called prime factorization). Let’s say we present the number 8,018,009 and tell you it is the product of two primes. Finding those two primes is much harder for you than it was for me to multiply them to produce 8,018,009.

Some of these mathematical functions can be inverted easily if you know some secret information. In the preceding example, if I tell you that one of the prime factors is 2,003, you can trivially find the other one with a simple division: 8,018,009 ÷ 2,003 = 4,003. Such functions are often called trapdoor functions because they are very difficult to invert unless you are given a piece of secret information that can be used as a shortcut to reverse the function. Read more here.

Ethereum uses asymmetric key cryptography based on elliptic curve arithmetic to generate key pairs(public and private keys) because there are currently no known trapdoors. All externally owned account has a key pair associated with it. The key pair is all that is needed to control and access any externally owned Ethereum account.

What is a private key

A private key is a secret number that is randomly generated for every externally owned account. It acts like a password that controls access to funds in a wallet. Anyone with the private keys of a wallet has complete control of all the coins in the wallet.

The randomly generated secret number is passed through a hashing algorithm to produce a hashed version of the number.

A hashing algorithm converts an input value of arbitrary length into another value of fixed length.

Ethereum uses the keccak256 hashing algorithm which produces a 32bytes(256bits) hexadecimal number of 64 characters. Let's say we choose 123 as our secret number. Using the keccak256 hash function, our secret number 123 is hashed to produce the following output.

64e604787cbf194841e7b68d7cd28786f6c9a0a3ab9f8b0a0e87cb4387ab0107

You can confirm this output using Keccak-256 online hash function

Private keys are used to sign transactions and prove that the owner owns the coins in the corresponding crypto wallet. For example, Bob has 5 eth stored in his metamask wallet. Bob wants to send Alice 2 eth. Bob creates a transaction with his metamask wallet which states that he wants to send 2 eth to Alice’s wallet. Bob signs the transaction with his private keys and the transaction is broadcasted to all the nodes in the Ethereum blockchain. The transaction is confirmed and verified using Bob’s public key and this proves that the transaction was sent from Bob’s wallet address. The transaction is processed. 2 eth is subtracted from Bob’s wallet and added to Alice’s account. In the preceding example, we can also see that anyone with Bob’s private keys can sign transactions on his behalf.

What is a public key

Public keys are derived from private keys using the one-way function we learned about earlier. Every private key has a corresponding public key associated with it.

Think of a public key as an account number and the private key is the account number’s PIN. Your public key is the identification number of your account. Your Ethereum address is derived from the last 20bytes of the keccak256 hash of the public key.

A public key is public and as such, it can be shared with anyone. Even though the public key is derived from the private key, it is impossible to calculate the inverse. You cannot derive the private keys from the public keys.

Conclusion

Here is a recap of all we learned in this article

  • Cryptography is a process of securing data through the use of encryption and decryption.

  • Asymmetric key cryptography uses a key pair (private key and public key) for encrypting and decrypting data.

  • Private keys are used to create digital signatures which are used to sign transactions, proving ownership of funds in a crypto wallet and allowing the owner to transfer coins out of the wallet. The use of private keys helps to secure a wallet from unauthorized access.

  • Public keys are public and can be shared with anyone. Your Ethereum wallet address is derived from the public key. The public key is like your bank account number while the private key is your PIN which unlocks your account.

If you want a better understanding of the Ethereum blockchain, I strongly suggest that you read Mastering Ethereum by Andreas M. Antonopoulos and Dr. Gavin Wood

Reference

  • Mastering Ethereum by Andreas M. Antonopoulos and Dr. Gavin Wood (O’Reilly). Copyright 2019 The Ethereum Book LLC and Gavin Wood, 978–1–491–97194–9. The book is free and it is on GitHub https://github.com/ethereumbook

Thanks for reading guys. Until next time, bye!

Connect with me on Twitter | LinkedIn | GitHub |