Understanding The Web

Ethereum Bootcamp: Blockchain Cryptography

Posted

Alchemy University’s Ethereum Bootcamp is a 7-week course that builds a strong foundation for blockchain developers. The first week explores blockchain cryptography. The topics covered help developers understand the fundamentals of blockchain technology.  The cryptographic hash is the underlying mechanism that defines blockchain technology. Further leveraging cryptography, digital signatures are another key component of blockchain technology. The Proof of Work (PoW) consensus mechanism illustrates the cooperative and competitive nature of a blockchain.  Finally, the study of data structures offers a deep understanding of how a blockchain persists. 

The First Primitive: Cryptographic Hashes

Why Cryptography?

The first core cryptographic primitive is the hash function.  To expand on this idea, it is important to understand how blockchains work. The purpose of a blockchain is to have a network of computers agree upon a common state of data.  The most common use case for blockchains is the cryptocurrency. The reason blockchains are perfect for cryptocurrencies is because blockchains were invented to solve the problem of trust and double spending. Blockchains create the ideal conditions for a cryptocurrency: a system that is neutral and resistant to censorships and bribes.

Smart contract blockchains

Building on the concept of the blockchain, smart contract blockchains allow developers to decentralize where their code runs.  A defining characteristic of a smart contract is its public availability on the blockchain, which is achieved by compiling the code to the blockchain. This also means that the nodes that maintain the blockchain’s status will also enforce the logic of the code written in the smart contract.

Cryptographic hash functions

Blockchains and smart contracts lead us to the cryptographic hash function. Hash functions take an input of any size and return fixed size output.  While there are many different algorithms for hash functions, the blockchain requires a cryptographic hash function to possess these 5 key properties:

  • Determinisitc: A specific input always produces a specific output
  • Pseudorandom: It is not possible to guess the ouput based on the output of similar inputs
  • One-way: Given an output, the input cannot be derived without guessing.
  • Fast to Compute: A quick calculation performed by computers.
  • Collison resistant: The chance of collision should be infinitesimally small

Because cryptographic hash functions must be deterministic, using common passwords is a mistake.  Rainbow tables map common inputs with their hash output. It would be very easy for a hacker to guess a password on this list by searching for the hash and its associated word.

Digital Signatures: Public Key Cryptography

History of cryptography

Having understood the role cryptography plays in the blockchain, learning the history of cryptography can offer further insight. Classic cryptography involved methods such as the Caesar Cypher which was a very basic method of altering a message.  The evolution of cryptography lead to the idea of a secret key.  In symmetric key cryptography two parties could use agreed upon keys to exchange messages securely.

Asymmetric Encryption

Initially, the concept of a secret key required two parties to exchange the key before sending messages. Whitfield Diffie and Martin Hellman birthed the idea of public key cryptography. The Diffie-Hellman key exchange defines the dynamic of asymmetric encryption 

RSA and ECDSA

Two popular algorithms for public key cryptography are RSA and ECDSA.  RSA uses prime numbers as the foundation of its cryptography. ECDSA uses elliptical curve cryptography. Bitcoin uses form of elliptical curve cryptography known as secp2561k which refers to the parameters used in its public key cryptography.  Public key cryptography improved upon the initial idea of a secret key by creating a system where two parties did not need to meet beforehand to make an exchange.  A signature created with the private key would be given to the second party verifying that it could have only come from the sender’s public address.

The ECSDA Process

Hashing

To understand the process of public key cryptography, a good starting point is a step by step look at the ECDSA process. The first step in the process is ‘hashing’ a message.  Using a hashing algorithm suck as keccak256 a message such as “Hello” is transformed into a hash: “06b3dfaec148fb1bb2b066f10ec285e7c9bf402ab32aa78a5d38e34566810cd2”. Changing the message to “hello” produces a completely different hash: “1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8”.

SIGNING

The next step in the process is to sign a message.  Using another algorithm such as secp256k1, the original message that was hashed is combined with a private key to create a “signed” message.

RECOVERY

This signed message is used within a function that retrieves the public key associated with the private key that signed the message.  This public key can be used to confirm that the message was sent from the address associated with the private keys.

ADDRESS

The important concept to understand in the process is that the private key is never exposed.  Once a private key is derived, a one way hashing function creates a public key that is tied to the private key. Similarly, An address is derived from the public key to use as a medium to initiate transactions.

RECAP

The private key is used to create a signed message.  The signed message is sent to the second party.  The second party confirms the sender of the message through the signed message by recovering a public key.  The public can be used to confirm the address that initiated the transaction. This is the fundamental process used to cryptographically confirm identities in many blockchains.

Blockchain Network: Blockchain Data Structure

Blockchain architecture

A blockchain is a distributed database of a list of validated blocks. Each block contains data in the form of transactions, and each block includes the transactions from the preceding blocks. In computer science, a node is a unit or member of a data structure. A blockchain is composed of nodes scattered throughout the world verifying the state of the blockchain.

Blockchain Demo

This blockchain demo provides a hands on approach to understanding the blockchain. The demo defines contents within a block. The genesis block is the first block in the blockchain. The index is the current position of the blockchain. The timestamp is a record of when the block was created. A hash represents the digital signature of the block. The previous hash leads to the block the precedes the current block. Data includes the transactions in the current block. The nonce is the number used to find a valid hash.  While this information may seem vague, these key components help maintain the integrity of the blockchain.

Data Integrity

The blockchain demo shows a block contains the entire history of the blockchain. This data is nested, allowing each block to trace back to the genesis block. A malicious actor attempting to manipulate the history of a blockchain does so as the most current blocks continue to be validated.  The process of manipulating a blockchain would involve attempting to find a valid hash to effect the block/transaction in question.  Upon success, the malicious actor would need to create new hashes for the blocks that follow and the blocks that have been mined since the malicious act was started.  Because of this dynamic, attempting to manipulate the history of a blockchain is futile.    

Adding a new block

The nodes in a blockchain must follow rules to achieve consensus.  Adding a new block to the blockchain has requirements.  The block index must be one greater than the latest block index. The block’s previous hash is equal to the latest hash.  The block has met the difficulty requirement. The block hash is correctly calculated.  If these requirements are met, a new block can be hashed and miners rewarded.  When a new block is proposed by a node, all the other nodes in the network validate the block. After a set number of validations, a new block is officially added to the chain.

51% attack

Despite all the measures taken, a blockchain can still be compromised.  One way to attack a blockchain is the 51% attack   If a group of miners were to gain control of 51% of the network, it would be possible to manipulate the blockchain. As a block is being validated, the malicious nodes could create their own proposed block and validate it before the true block is validated.  The malicious chain would become the accepted chain.  This would allow double spending which goes against one of the fundamental purposes of a blockchain.  While this is the route malicious actors could take, it would require vast amounts of energy.  The logical path to take would be to play along and collect all the mining rewards. 

Conclusion

Learning about the many moving parts to a blockchain is the first step to take before diving into smart contracts.  Understanding cryptographic hashes teaches the blockchain developer the most primitive aspect of blockchain technology.  Grasping the idea of digital signatures solidifies the concept of public and private key interaction. Reviewing the Proof of Work model helps to create a frame of reference when learning about proof of stake. A familiarity with a blockchain’s data structure provides insight to data storage.  These concepts help produce the fundamental skills that produce optimized smart contracts. Understanding the building blocks of the blockchain will help produce gas efficient smart contract code.