Creating a simple cryptocurrency: part 1

in crypto •  8 years ago  (edited)

Introduction

I've been interested in cryptocurrencies since their beginning, and before that I was already supporting the decentralization movement, including local currencies.

Although I'm happy to see cryptos gaining so much popularity, I think they are getting to be too big and too centralized, and many people perceive them as a way to get rich quick rather than as a facilitator of free commerce and an antidote to central control. Wild speculation and the accompanying boom and bust cycles undermine the usefulness of currency, which depends on stability.

I hope people don't lose sight of the benefits of local currencies, as demonstrated by the experience of the Austrian town of Wörgl:

Wörgl was the site of the "Miracle of Wörgl" during the Great Depression. It was started on July 31, 1932, with the issuing of "Certified Compensation Bills", a form of currency commonly known as Stamp Scrip, or Freigeld...

The experiment resulted in a growth in employment and meant that local government projects such as new houses, a reservoir, a ski jump and a bridge could all be completed, seeming to defy the depression in the rest of the country. Inflation and deflation are also reputed to have been non-existent for the duration of the experiment.

To that end, I'd like to design a cryptocurrency that might be useful for local communities. As such, it should be very simple and not need large amounts of electric power or special computer equipment to operate. Readers are welcome to join in, perhaps by providing suggestions. Disclaimer: I'm not an expert programmer, but the goal is to keep things simple so that an expert is not required to understand the code.

The first step is to chose a programming language. A good choice is Javascript and Node.js because they are mostly platform independent and well-suited for network communication. You may want to install node.js on your system to follow along.

The two most important breakthrough technologies used by cryptos are cryptography and decentralized consensus determination. Let's create two simple Node.js programs to demonstrate cryptographic hashing and signing, which are at the heart of all block chains. Although many cryptographic schemes are available, let's use sha-256 for hashing and ed25519 for signing.

Hashing

Here's how to hash using the Node.js crypto package:

crypto = require('crypto')
buffer = Buffer.from('Some string')
console.log(crypto.createHash('sha256').update(buffer).digest().toString('hex'))


This program displays a 32-byte (256-bit, 64-hex-digit) hash of the string Some string:

2beaf0548e770c4c392196e0ec8e7d6d81cc9280ac9c7f3323e4c6abc231e95a

Cryptographic hashes are amazing. They can't practically be reversed, especially a 256-bit hash, at least until quantum computers mature -- then more bits may be needed. What's more, a slight change to the input string results in a completely unrelated hash. And since there 2256 ~ 1077 different possible hashes (close to the number of atoms in the known universe), the chance of different inputs having the same hash is negligible.

Hashes are used in the Bitcoin proof-of-work scheme: miners compete to be the first to generate a hash with a given number of leading zeros from a tentative block by trying different values of the block's nonce. The difficulty of this computation is adjusted by adjusting the number of required zeros. More zeros are required when miners are more competitive. The first miner to guess a correct nonce gets a bitcoin reward. All other miners can quickly verify that the nonce is correct. The asymmetry in difficulty between computing a hash from a known input and guessing an input that can produce a given hash is what makes cryptography so useful.

Signing

Here's how to sign and verify using the npm ed25519-supercop package:

ed = require('ed25519-supercop')
buffer = Buffer.from('Some string')

seed = Buffer.from('9c442e2f5cfb5f4377435901da4ce0594c505f7c44a2f7d6de78aa2fa42cb689', 'hex') // or ed.createSeed()
console.log(seed.toString('hex'))

keyPair = ed.createKeyPair(seed)
console.log(keyPair.publicKey.toString('hex'))
console.log(keyPair.secretKey.toString('hex'))

signature = ed.sign(buffer, keyPair.publicKey, keyPair.secretKey)
console.log(signature.toString('hex'))

verified = ed.verify(signature, buffer, keyPair.publicKey)
console.log(verified)


First, a key pair is created from a random 32-byte seed (originally created by ed.createSeed()):

9c442e2f5cfb5f4377435901da4ce0594c505f7c44a2f7d6de78aa2fa42cb689

The key pair consists of a 32-byte public key and a 64-byte secret key:

709efff9bb2e3af305070b275fe9defa1f0239910a352cfedc76ad96263a0916
687ad3ca223beacc9d59ffb4451acd0e8b4fa67583b672997433d5a16671ba78cb51a103c437f8cf1dae192a42b361413a0e317e535147484ade8b65ed6f0596


Then, the input string is signed with the key pair, yielding a 64-byte signature:

ed9e4fbc1a841908288c0252d841e5f16cb2ba8420ed2e3419ab1db76ad6577937d847e6e327de4f74044dcfb90328c78c00ace11118d8e300a843b70c64ce01


Only the public key is needed to verify the signature, yielding true.

A verified signature proves that:

  • the string was signed by whomever has the secret key associated with the pubic key
  • the string has not been altered since it was signed

These features of digital signatures make it possible to publicly document the transfer of a cryptocoin from its original owner to a new owner. Block chains consist mostly of such transactions, bundled together in blocks by miners. It is up to each miner to eliminate improper transactions such as transferring a coin not owned or transferring the same coin to multiple recipients.

A drawback of digital signature algorithms is that they are slow when signing large inputs. A clever solution to this problem is to first hash the input (a faster operation), then sign the hash.

I haven't delved into the actual cryptographic machinery encapsulated in the crypto and ed25519-supercop packages; the reader may wish to further explore these remarkable achievements that have made decentralized block chain applications such as cryptocurrencies possible.

part 2 >

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!