The following is an introduction to a series of tutorials on implementing a Blockchain from first principles and an application of the Blockchain: Cryptocurrency. The on-going series will provide a theoretical overview of blockchain technology and how it relates to crypytocurrencies and an implementation in C.
One popular application of the Blockchain is a Cryptocurrency. The distinction is important — a cryptocurrency is one interpretation of the Blockchain. The Blockchain creates a foundation for transactions (an interaction between one or more parties) to take place in a trustful, and transparent manner. A Blockchain therefore can be viewed as a block of transactions that are chained together: each block points to the previous block of transactions forming a cryptographic chain. These factors allow for the possibility of a cryptocurrency among numerous other applications.
The cyroptographic aspects of the Blockchain create reasonable assurance that a transaction occurred the way it is reported to have occurred despite the trust worthiness of the reporting party. This removes forgery, and other devious means that can create distrust in a system. Additionally the “reward” system common in a Blockchain provides an incentive for the members of the network to act in a responsible manner such as maintaining accuracy of the transactions that have occurred up to the current transaction : Any node is free to reject a block of transactions, that it finds to be incorrect after a series of mathematical assessments. A node in the network is incentivized to maintain accuracy in order for its transactions to be accepted by other nodes in the network and thus benefit from the associated transaction fee and possibly win the mining reward if it solves a cryptographic puzzle.
The transactions in the Blockchain network are observed and witnessed essentially by every participant in the network. Not only can a transaction be proved to have happened through cryptography, an overwhelming majority of all the parties in the network have a copy of that transaction. A transaction that maybe cryptographically correct (signed and verified) but wasn’t made from the network’s previous transactions can be rejected when it is introduced in the network due to the fact that the history of all transactions made is known by the majority of participants in the network.
Now to creating a cryptocurrency as a use-case of the Blockchain:
Aspects of a Cryptocurrency
A cryptographic hash must be such that no two values can produce, or “hash”, to the same digest (Collision Free) and it must be infeasible to guess the value that produced the digest (Hiding Principle) — no number or pattern should be more likely to produce a given hash. SHA256 and RIPEMD160 are some of the commonly used cryptographic hashing algorithms in Bitcoins.
A digital signature, as it relates to a Bitcoin-like cryptocurrency, provides a way to use cryptographic hashes to create a concept of an identity (authentication) and approval (authorization). The following properties, although not an exhaustive list, must be held for digital signatures in Bitcoin:
A randomly generated private-public key pair (private-key, public-key): A private key is a randomly generated 256-bit string and a A public key is the 256 bit private key multiplied by a point on an elliptic curve (in Bitcoin it’s secp256k1). PubKey = PrivKey * G : where G is a “generator point” on the elliptic curve.
Private key can be used to sign a message and produce a signature which is a signed message.
The public key, message, and signed message (signature) can be used to verify that the public key is the result of the private key signing the message. A signature can’t be forged.
An Address is the hashed public-key as such: RIPEMD160(SHA256(public-key)).
When creating a cryptocurrency there must be some type of value if the transactions made are to be used in place of money. Scarcity in the physical world can create value — for example scarcity in resources, or minerals, or a particular skillset may increase the value of a that good or service. The Blockchain periodically generates bitcoins and awards them to whichever participant solves a cryptographic puzzle (challenge) first.
However for bitcoins to be valuable at all the participants in the network must believe that bitcoins are valuable due to the security of the network . For a cryptocurrency to be secure, in addition to the cryptography used, it has to have many participants of which most will be assumed to act according to the rules. This is sort of a chicken & egg scenario known as bootstrapping.
Connecting all the parts
The Blockchain isn’t a single technology there are multiple pieces to the puzzle. What to expect in this tutorial:
Generating cryptographically secure Private & Public Key.
Creating addresses from public key(s).
Creating signed transactions and verifying signed transactions
Implementing base58check and base58encoding to represent transactions and addresses.
Implementing a wallet to generalize the above.
Creating a public ledger of blocks of transactions (the “Blockchain”).
Implementing a Peer-to-Peer network to broadcast the transactions to the network.
Implementing smart contracts
By all means the code is provided as is with no guarantee of suitability despite that all reasonable measures have been taken to produce a complete and secure work. This has been a project to truly learn and understand the core of blockchain technology and explore some of its uses. I intend to complete the series as time permits in the span of a month or so 🙂 But I caution the reader to not expect regular updates either.
Here are some reference materials I’ve been using to learn more about blockchain technology and bitcoin in particular
– Bitcoin book (http://bitcoinbook.cs.princeton.edu)
– “Mastering Bitcoins” Unlocking Cryptocurrencies