{"id":138,"date":"2017-06-13T15:03:07","date_gmt":"2017-06-13T19:03:07","guid":{"rendered":"http:\/\/dwan.org\/?p=138"},"modified":"2019-10-25T15:18:03","modified_gmt":"2019-10-25T19:18:03","slug":"proof-of-work-and-the-nonce","status":"publish","type":"post","link":"https:\/\/dwan.org\/index.php\/2017\/06\/13\/proof-of-work-and-the-nonce\/","title":{"rendered":"Proof of work and the nonce"},"content":{"rendered":"<p>The blockchain technology ecosystem brings together a diverse set of codes and algorithms that have been developed over the past 50-ish years. It includes decades old cryptographic techniques like hashing and symmetric\/asymmetric key encryption, and also includes relatively recent innovations related to distributed consensus.<\/p>\n<p>The Blockchain ecosystem reminds me of the classic radio tag-line: It\u2019s the best of the 80\u2019s, 90\u2019s, and today.<\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Proof-of-work_system\">Proof of work<\/a> is one component of that ecosystem.  It is used to prevent <a href=\"https:\/\/en.wikipedia.org\/wiki\/Denial-of-service_attack\">denial of service<\/a> attacks, in which large numbers of messages swamp and degrade a system.  The system works by imposing a computational cost on the creation of valid messages.  Receivers check whether messages are valid before they pay any attention to the contents.<\/p>\n<p>The proof of work described in <a href=\"https:\/\/bitcoin.org\/bitcoin.pdf\">the original Blockchain paper<\/a> is based on a system called <a href=\"http:\/\/www.hashcash.org\/\">Hashcash<\/a>, that was developed in 1998 to combat spam email.  The sender is required to find a value called a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cryptographic_nonce\"><b>nonce<\/b><\/a> that is specific to a particular message, and that demonstrates that they put effort into creating the message.  A valid nonce is rare to find by chance, but easy to verify once found.<\/p>\n<p>This property \u2013 numbers and relationships that are challenging to find, but trivial to verify \u2013 is the basis of most of modern cryptography.  <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cryptographic_hash_function\">Hash functions<\/a> are one example. A hash function takes arbitrary input and returns a value within a fixed range. In a good cryptographic hash, the result (sometimes simply called the \u201chash\u201d of the input) is randomly distributed across that range.  It is difficult to author an input to get any particular hash value.<\/p>\n<p>The hashcash algorithm is simple:  The <b>nonce<\/b> is combined with the message to be sent, and the combination is hashed.  The hash result must be small relative to all possible hash results.  Exactly <i>how<\/i> small is a parameter that can be used to tune the algorithm.<\/p>\n<p>For example, if the hash function returns a 256 bit value, there are 2<sup>256<\/sup> possible results. If we insist the nonce be a value that makes the first 16 of those bits \u20180\u2019, we are insisting that senders find one of 2<sup>240<\/sup> values from among 2<sup>256<\/sup> possible hash results.  The probability of this happening by random chance are one in 2<sup>16<\/sup>, or something like 1 in 65,000.<\/p>\n<p>On <i>average<\/i>  (assuming that we have picked a good hash function) senders will have to try 2<sup>16<\/sup> nonces before finding a valid one.  If we assume that each hash takes 1 second to calculate on a single CPU, the sender would invest (on average) slightly under a CPU day per message.<\/p>\n<p>In the email system proposed in 1998 (I would love to use something like this, by the way) senders invest some amount of computation in creating a nonce for each message.  Receivers sort or apply thresholds based on the value of the hash.  Low numbered hashes represent an investment in the message.  Human beings who type or dictate messages to small numbers of recipients won\u2019t even notice the additional compute effort.  Mass marketing campaigns will be expensive.<\/p>\n<p><i>This exact computation<\/i> is the work of \u201cmining\u201d in the Bitcoin network. The language of \u201cmining\u201d or \u201cfinding\u201d bitcoins obscures the fact that we\u2019re actually searching for nonces.<\/p>\n<p>Of course, compute power keeps getting cheaper, so we need to have a flexible system.  Fortunately, the tunable parameter of the nonce makes this simple.  If compute performance on hash functions were governed by <a href=\"https:\/\/en.wikipedia.org\/wiki\/Moore%27s_law\">Moore\u2019s law<\/a> (it\u2019s actually a bit more complex), then we would need to increase the strictness of our nonce by one bit every two years.<\/p>\n<p>The Bitcoin network has been tuning its proof of work to produce valid blocks at a remarkably consistent rate of about one every ten minutes since 2010.<\/p>\n<p><i>P.s:  Thanks to Eleanor of <a href=\"http:\/\/diamondagedatascience.com\">Diamond Age Data Science<\/a> for <a href=\"https:\/\/diamondagedatascience.com\/2017\/05\/21\/probability-vs-likelihood-whats-the-difference\/\">this post<\/a> explaining the difference between probabilities and likelihoods.  An earlier version of this post used the words incorrectly.<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The blockchain technology ecosystem brings together a diverse set of codes and algorithms that have been developed over the past 50-ish years. It includes decades old cryptographic techniques like hashing and symmetric\/asymmetric key encryption, and also includes relatively recent innovations related to distributed consensus. The&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[3],"class_list":["post-138","post","type-post","status-publish","format-standard","hentry","category-blockchain","tag-blockchain"],"_links":{"self":[{"href":"https:\/\/dwan.org\/index.php\/wp-json\/wp\/v2\/posts\/138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dwan.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dwan.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dwan.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dwan.org\/index.php\/wp-json\/wp\/v2\/comments?post=138"}],"version-history":[{"count":6,"href":"https:\/\/dwan.org\/index.php\/wp-json\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":1167,"href":"https:\/\/dwan.org\/index.php\/wp-json\/wp\/v2\/posts\/138\/revisions\/1167"}],"wp:attachment":[{"href":"https:\/\/dwan.org\/index.php\/wp-json\/wp\/v2\/media?parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dwan.org\/index.php\/wp-json\/wp\/v2\/categories?post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dwan.org\/index.php\/wp-json\/wp\/v2\/tags?post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}