nuts and bolts of blockchain

6
The nuts and bolts of blockchain technology Rohas Nagpal Primechain Technologies Pvt. Ltd. [email protected] Abstract: Blockchain technology has earned the respect of Governments and banks around the world. This document provides a simple introduction to blockchain technology and briefly introduces terms such as cryptography, hash functions, proof-of-work, digital signatures, mining, merkle root. This document is intended for the novice reader and may suffer from errors inherent when a complex topic is (over?) simplified. Depending upon the hemisphere you live in, you probably think that blockchain is either the Rajnikant or Chuck Norris of all technologies. Thanks to the massive media coverage and billions of dollars of investments, blockchain is a word that almost everyone has heard. But a lot of people don’t understand the mathematics behind this revolutionary technology. Well, read on. 1. What is a blockchain? Imagine a world without computer databases. There would be no ecommerce, no ATMs, no Internet banking, no Aadhaar or similar social security scheme, no Facebook, no Gmail, no WhatsApp! Almost everything that makes the Internet so powerful and useful depends upon computer databases. The digital world relies very heavily on computer databases, even though most users are unaware of it. Now imagine a database that is provably immutable / unchangeable and almost impossible to hack. That's a blockchain. At its core, a blockchain is an ordered and timestamp sequence of "blocks of information". Blockchain technology, also called distributed ledger technology, derives its strength from strong cryptography and hash functions. Blockchain technology was invented by the unknown inventor of the bitcoin crypto- currency in 2008. Simply put, the bitcoin crypto-currency runs on the bitcoin blockchain (simply referred to as The Blockchain). The Blockchain is a public blockchain where anyone can become a miner and details of every single bitcoin transaction are stored on each node. Then there are other blockchain platforms such as - bitshares, ethereum, hyperledger, multichain, ripple and stellar. Hyperledger and multichain can be used to public, private (e.g. the Government running a land registry) and consortium blockchains (e.g. a group of banks running a shared Know-Your-Customer or KYC platform).

Upload: rohas-nagpal

Post on 14-Jan-2017

26 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Nuts and bolts of Blockchain

Thenutsandboltsofblockchaintechnology

RohasNagpalPrimechainTechnologiesPvt.Ltd.

[email protected]

Abstract:Blockchain technologyhas earned the respectofGovernments andbanks around the world. This document provides a simple introduction toblockchaintechnologyandbrieflyintroducestermssuchascryptography,hashfunctions, proof-of-work, digital signatures, mining, merkle root. Thisdocument is intended for the novice reader and may suffer from errorsinherentwhenacomplextopicis(over?)simplified.

Dependinguponthehemisphereyoulivein,youprobablythinkthatblockchainiseithertheRajnikantorChuckNorrisofalltechnologies.Thankstothemassivemediacoverageand billions of dollars of investments, blockchain is aword that almost everyone hasheard.Buta lotofpeopledon’tunderstandthemathematicsbehindthisrevolutionarytechnology.Well,readon.1.Whatisablockchain?

Imagineaworldwithoutcomputerdatabases.Therewouldbenoecommerce,noATMs,no Internet banking, no Aadhaar or similar social security scheme, no Facebook, noGmail,noWhatsApp!Almost everything that makes the Internet so powerful and useful depends uponcomputerdatabases.Thedigitalworldreliesveryheavilyoncomputerdatabases,eventhoughmostusersareunawareofit.Now imagine a database that is provably immutable / unchangeable and almostimpossible to hack. That's a blockchain. At its core, a blockchain is an ordered andtimestampsequenceof"blocksofinformation".Blockchain technology, also called distributed ledger technology, derives its strengthfromstrongcryptographyandhashfunctions.Blockchain technology was invented by the unknown inventor of the bitcoin crypto-currencyin2008.Simplyput,thebitcoincrypto-currencyrunsonthebitcoinblockchain(simply referred to as The Blockchain). The Blockchain is a public blockchain whereanyonecanbecomeamineranddetailsofeverysinglebitcointransactionarestoredoneachnode.Then thereareotherblockchainplatformssuchas -bitshares,ethereum,hyperledger,multichain,rippleandstellar.Hyperledgerandmultichaincanbeusedtopublic,private(e.g.theGovernmentrunninga land registry) and consortium blockchains (e.g. a group of banks running a sharedKnow-Your-CustomerorKYCplatform).

Page 2: Nuts and bolts of Blockchain

2.Themathematicsofitall

Sanya’sanaughtyyounggirlwho’sbeengroundedforaweek.Shewantstosneakoutfor desert with her friends but obviously can’t let her dad know about it. She’s notallowedtousehercellphone,sotheonlywayforhertocallherfriendsisusingthegoodoldlandlineinherdad’sroom.Sincesheregularlygetsgrounded,sheandherfriendshaveworkedoutasimplesystemforsharingsecrets.Whenshesays,“haveyoureadthebookItoldyouabout”sheactuallymeans“let’ssneakouttonight”.Whenshesayssomethingabout“page10”of thebook,shemeans“pickmeupat10pm”.Continuingthelogic,page11wouldmean11pmandsoon.Soonthephonesheasksherfriend“HaveyoureadthebookItoldyouabout?Page12isreallyfunny”,shemeans,“Let’ssneakouttonight,pickmeupatmidnight”.Whatwehavejustseeniscryptography(andarebelliousteenager)inactionintherealworld.Thesentence“Let’ssneakouttonight,pickmeupatmidnight”isplaintext–whatSanyaactuallywantstoconvey.Thesentence“HaveyoureadthebookItoldyouabout?Page12isreallyfunny"istheciphertext–somethingthatanadversary(herdadinthiscase)shouldnotbeabletounderstand.Encryption istheprocessofconvertingplaintexttociphertext.Thereverseprocessisdecryption.This scienceof encrypting anddecryptingmessages (cryptography)hasbeenused forthousandsofyears.ItisbelievedthatwhenJuliusCaesarsentmessagestohisgenerals,hereplacedeveryAinhismessageswithaD,everyBwithanE,andsoonthroughthealphabet.Onlysomeonewhoknewthe“shiftby3”rulecoulddecipherhismessages.Forexample,ifwewanttoencodetheword“SECRET”usingCaesar’skeyvalueof3,weoffsetthealphabetsothatthe3rdletterdown,(D),beginsthealphabet.SostartingwithABCDEFGHIJKLMNOPQRSTUVWXYZ

andslidingeverythingupby3,youget

DEFGHIJKLMNOPQRSTUVWXYZABC

whereD=A,E=B,F=C,andsoon.Using this scheme, theplaintext, “SECRET”encrypts as “VHFUHW”.Toallowsomeoneelsetoreadtheciphertext,youtellhimorherthatthekey is3.Thismethodiscalledsymmetric cryptography and involves using the same key for encrypting as well asdecrypting amessage. This naturally poses a serious problem –what if an adversarygetsholdof thiskey?Atsomepointof timethesenderandreceiverneedtoexchangethekey.That’swhenanadversarycouldgetholdof thekey. Inmoderncryptography,keysareextremelylargenumbers.The secure-key-exchange problem was solved with the birth of asymmetric keycryptography – in which two different but related keys are used - the public key toencryptdataandthecorrespondingprivatekeytodecryptthedata.

Page 3: Nuts and bolts of Blockchain

IfSanyaweretosendanencryptedmessagetoKaran,shewouldencryptthemessageusinghispublickey(whichisavailabletotheworld).Onceencrypted,themessagecanonlybedecryptedusingKaran’sprivatekey(whichwouldonlybeavailabletoKaran).Beforewegetintothenutsandboltsofhowblockchainswork,weneedtounderstandsomemoreconceptsincludinghash functions.Aone-wayhashfunctiontakesaninput(e.g.aPDF file,avideo,anemail,a stringetc.)andproducesa fixed-lengthoutpute.g.160-bits.Thehashfunctionensuresthatiftheinformationischangedinanyway–evenbyjustonebit – an entirelydifferent output value is produced.The table below shows somesampleoutputvaluesusingthesha1(40)hashfunction.Input Hashsanya c75491c89395de9fa4ed29affda0e4d29cbad290SANYA 33fef490220a0e6dee2f16c5a8f78ce491741adcSanya 4c391643f247937bee14c0bcca9ffb985fc0d0baItcanbeseenfromthetableabovethatbychangingtheinputfromsanyatoSANYA,anentirelydifferenthashvalueisgenerated.Whatmustbekeptinmindisthatirrespectiveofthesizeoftheinput,thehashoutputwillalwaysbeofthesamesize.Twothingsmustbeborneinmindwithregardtoone-wayhashfunctions:

1. It is computationally infeasible to find two different input messages that willyieldthesamehashoutput.

2. Itiscomputationallyinfeasibletoreconstructtheoriginalmessagefromitshashoutput.

Havingunderstoodhashfunctions,let’shavealookatanotherinterestingconceptcalledproof-of-work.Thisisawaytoreducespamanddenialofserviceattacksbyrequiringacomputertospendsometimeandprocessingpowertosolvesomething.One such proof-of-work system that is used in blockchains is hashcash. The basicpremise of hashcash is that if the sender of an email can prove that she has spentreasonabletimeandcomputationalpowertosolvesomepuzzle,itcanbebelievedthatthe sender is not a spammer. The logic is that spamming would be economicallyinfeasible if a spammer had to spend non-trivial time and computational power foreverysingleemailbeingsent.Let’s develop an elementary proof-of-work system, based on hashcash, which can beusedtocontrolspam.Let’[email protected]@primechain.in.Thesendermust includesomethingsimilar tothe following intheheaderoftheemail:

[email protected]:[email protected]:06112016:xxxxThat’s4piecesofinformationseparatedbycolons.Thefirstpieceisthesender’semailaddress, thesecond is thereceiver’semailaddressandthethird is thecurrentdate inDDMMYYYYformat(6thNovember,2016inthisexample).Thefourthpieceissomethingthatneedstobecalculatedbythesender’scomputer.Let’scallitanonce.Theobjectiveistofindaninputthatwouldresultinasha256hashwhichbeginswith4zeros.

Page 4: Nuts and bolts of Blockchain

Sowestart thenonceatavalueof0andthenkeep incrementing it (0,1,2,3…)andcalculatingthehash.Somethinglikethis:Input [email protected]:[email protected]:06112016:0sha256hash 2d87bf06373f4e91b43ab6180e30da0bf3f98efb44c5d5e2f7151b3179413bf6Input [email protected]:[email protected]:06112016:1sha256hash cb3616e4ab0cee86badf0a598d1a151e06289c2c7e35f91554dc1ad7d128a99dInput [email protected]:[email protected]:06112016:2sha256hash 8d04a9e7ccd2c84549744c7fdbd48e3784ea3ab10020499a89349875726e3536Andsoontill..76063Input [email protected]:[email protected]:06112016:76063sha256hash 0000b3c73f0cd6a92158b713fbade5f898dffeefc0a615d050b1ea391bd39906Calculating thismaynot takeagenuinesendera lotof timeandcomputationalpowerbut if a spammerwere tomake these calculations formillionsof emails, itwill takeanon-trivialamountoftimeandcomputationalpower.Atthereceiver’send,thecomputerwillsimplytakethefollowinglinefromtheheaderoftheemailandcalculatethehash.

[email protected]:[email protected]:06112016:76063If the hash beginswith a pre-defined number of zeros (4 in this example), the emailwouldnotbeconsideredspam.Thiswilltakethereceiveratrivialamountoftimeandcomputationalpowersinceitjusthastocalculatethehashofoneinput.Thedatecanbeused as an additional validationparameter – e.g. if the date iswithin 24hours of thetimeofreceipt,theemailwillbeapprovedfordownload.Averyimportantapplicationofpublickeycryptographyisadigital signature.Inthis,thesignerfirstcalculatesthehashofthemessageshewantstodigitallysign.Thenusingher private key and the hash, she creates a digital signature, using the relevantalgorithm.Thisdigitalsignatureisuniquetothemessage.Thesignerthensendsthemessageandthedigitalsignaturetothereceiver.Thereceiverre-computes the hash from the message. The receiver also computes another stringusingthedigitalsignatureandthesigner’spublickey(usingtherelevantalgorithm).Ifthisstringandthehashmatch,thedigitalsignatureisverified.A blockchain is a public ledger containing an ordered and time-stamped record oftransactions. In addition to preventing double-spending, the blockchain prevents themodificationofprevioustransactionrecords.Ablockofoneormorenewtransactionsiscollectedintothetransactiondatapartofablock. Copies of each transaction are hashed, and the hashes are thenpaired, hashed,pairedagain,andhashedagainuntilasinglehashremains-themerklerootofamerkletree.Thisisillustratedbelow:

Page 5: Nuts and bolts of Blockchain

Illustration1:Merkletree

4f68594945ccded4d77a01992db7f4c5 is the merkle root of the 4 transactions (orpiecesofdata)intheillustrationabove.Thisisstoredintheblockheader.Additionally,eachblockalsostoresthehashoftheheaderofthepreviousblock.This chains the blocks together and ensures that a transaction cannot be modifiedwithoutmodifying the block that records it and all following blocks. Transactions arealsochainedtogether.Thisisillustratedbelow:

Illustration2:Ablockchain

Blockchains use a proof-of-work technique similar (but more complex) than the onediscussed earlier in this article. Since good cryptographic hash algorithms convert

Page 6: Nuts and bolts of Blockchain

arbitraryinputsinto“seemingly-random”hashes,itisnotfeasibletomodifytheinputtomakethehashpredictable.Toprovethatshedidsomeextraworktocreateablock,aminermustcreateahashoftheblockheader,whichdoesnotexceedacertainvalue.Thetermminermustnotbecomparedwithagoldorcoalminerintherealworld.Whileagoldminerdigsintotheearthtodiscovergold,ablockchainminerusescomputationalpower to calculate hashes. To add an entire block to the block chain, aminer mustsuccessfullyhashablockheadertoavaluebelowthetargetthreshold.Thefirst-everblockisknownasthegenesisblock.Eachsubsequentblockisaddressedby itsblockheight,whichrepresents thenumberofblocksbetween itandthegenesisblock.New blocks are added to the block chain if their hash is at least as challenging as adifficultyvalueexpectedbytheconsensusprotocole.g.accordingtothebitcoinprotocol,itshouldtake2weeksfor2016blockstobegenerated.Ifthetimetakenismoreorlessthan 2 weeks then the difficulty value is relatively decreased or increased every 2weeks.3.Inconclusion

Ibelievethatby2020,blockchaintechnologywillenablemassivesocialupliftmentandeconomicprosperity,thelikesofwhichtheworldhasneverseenbefore.Blockchainswillminimisefraudandmaximiseefficiency,security&transparencyinsupplychains,healthcare,globalmoneysystems,financialtechnologies,democraticelections,auctionofpublicassets,energytrading,electronicrecordauthentication,deliveryofGovernmentservices,IoT(InternetofThings)andmore.According to theReserveBank of India - "With its potential to fight counterfeiting, the'blockchain'islikelytobringaboutamajortransformationinthefunctioningoffinancialmarkets,collateralidentification(landrecordsforinstance)andpaymentssystem.According to theUKGovernment,distributed ledger technologieshavethepotentialtohelpgovernmentstocollect taxes,deliverbenefits, issuepassports,record landregistries,assurethesupplychainofgoodsandgenerallyensuretheintegrityofgovernmentrecordsand services…. In summary, distributed ledger technology provides the framework forgovernmenttoreducefraud,corruption,errorandthecostofpaper-intensiveprocesses.Ithasthepotentialtoredefinetherelationshipbetweengovernmentandthecitizenintermsofdatasharing,transparencyandtrust.Ithassimilarpossibilitiesfortheprivatesector.AccordingtotheReserveBankofAustraliagovernorGlennStevens,theblockchaintechnologycouldbringsignificantbenefitstotheglobalbankingsystem.Inarecentannouncement,theCrownPrinceofDubaiannouncedastrategicplanthatwouldseeallDubaigovernmentdocumentssecuredonablockchainby2020.