Hi! I’m Brad!

I’m an award-winning software developer from Laramie, Wyoming.

Category: Programming

  • Why Is Bitcoin Valuable?

    Bitcoin.  It’s everywhere in the news.  It was 2013’s best investment, and it’s taking the world by storm.

    So why is it a big deal?  And why does it have value?  Well, this is my attempt of explaining one of the fundamental facets of currency: a proof of work.

    Disclaimer: I am no economist.  There are other variables at play when valuing a currency.  Please bring these discrepancies and concerns to my attention in the comments.

    Proof of Work

    Proof of Work is arguably one of the biggest things that make a currency valuable.  The idea is that when we have something, and when we want to use that as currency, we need to know that it wasn’t produced trivially.

    Traditionally, up until recently, we have used gold (or other precious metals) as proof of work.  We understand that gold (etc) is rare, and if someone is able to produce a quantity of gold, we know that they worked a specific, non-trivial amount to obtain that gold.  As a result, our currencies have been defined by precious assets.

    Cool.  But what about Bitcoin?

    Glad you asked!  Bitcoin derives its value from computation.  I.E., Bitcoin is valuable because we use computers to create it.  But how do we create a proof of work out of our computing power?  Easy.  We create problems that take computers a very long time to solve.  In the design of Bitcoin, this is achieved with hashing.

    Okay… What’s hashing?

    According to Wikipedia, hashes are defined as

    [functions] primarily used to generate fixed-length output data that acts as a shortened reference to the original data. This is useful when the original data is too cumbersome to use in its entirety.

    Consider a hash as a boiled-down version of its input.  The hash of a pot of soup would be some sort of reduction scraped off the bottom of a pan.

    What is fascinating about hashes (and this is where the soup analogy stops working) is that hashes are one way.  In most cases, it is impossible to effectively reverse a hash without using trial and error.

    In other words, there is no way to tell what a hash’s input was.

    This is where Bitcoin’s value comes from.

    Hashes and the Blockchain

    Bitcoin transactions are recorded in a special, public data structure called a block chain.  Special programs called miners are used to add new blocks to the end of the block chain.

    A miner’s task is to gather recent transaction metadata and a few other pieces of metadata, and find a random number that, when combined with the rest of the metadata, produces a hash that meets the difficulty requirement of the network.

    For example, a recently mined block has the following hash

    00000000000000013c91aad4c2645706294bcaeae0b7117ea1c8ffc97feb5167

    What’s worth note is that this block’s hash has 15 zeroes at the beginning.  This is evidence of the proof of work.  Finding a hash of the block is incredibly difficult right now, and as the Bitcoin network’s computing power increases, the required hashing difficulty increases accordingly.

    How does a computer find a hash?

    1. Gather necessary metadata and structure it according to standard.
    2. Generate a random number and place it in the block.
    3. Hash the block.
      1. If the hash meets the network’s difficulty requirements the block is appended to the block chain, and the next block begins.
      2. If the hash doesn’t meet the network’s requirements, the computer is welcome to try again until it finds a valid hash.

    At the moment, it would take one computer at least 98 years to hash a block by itselfThis is a proof of work.

    Why are hashes good for this?

    Again, good question!  Good hashes are full of entropy.  This is to say that minor changes in the input has a massive effect on the actual output of a hash.

    The following inputs were hashed using MD5.  The passage on the right is missing the period at the end.  Otherwise, the passages are identical.

    Input

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus varius nulla mauris, ac ornare eros venenatis quis. Curabitur elementum risus vitae arcu mollis, vel accumsan nulla tincidunt. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Maecenas et condimentum lacus. Maecenas eu nisl ac arcu congue interdum. Sed risus elit, consectetur et venenatis eu, egestas eget nibh. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec a ligula purus. Vestibulum magna ligula, consequat sed varius sit amet, mollis sollicitudin lorem. Nulla et elit neque.

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus varius nulla mauris, ac ornare eros venenatis quis. Curabitur elementum risus vitae arcu mollis, vel accumsan nulla tincidunt. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Maecenas et condimentum lacus. Maecenas eu nisl ac arcu congue interdum. Sed risus elit, consectetur et venenatis eu, egestas eget nibh. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec a ligula purus. Vestibulum magna ligula, consequat sed varius sit amet, mollis sollicitudin lorem. Nulla et elit neque
    Output (md5) 35f45fa061f48a3d9cfc9ef3dee03aa1 313fb0e8e65bf7ac0a3f16f30f958e17

    What’s interesting is that the hashes are dramatically different.  Consequently, it is nearly impossible to effectively game the inputs of a hash with an understanding of how it will affect the output.

    Interestingly, these two hashes are such rare combinations of letters and numbers that performing a Google search for either hash will probably only lead to this article.

    Most computers with a good graphics card are capable of computing around 30-50 million hashes per second.  That’s 30 to 50 million GUESSES per second.

    The size of the entire network is at 17625 Thash/s.  In other words, the network is guessing 17,625 billion hashes per second attempting.

    Hashes are relatively easy to calculate.  So once a random number is appended to the block and calculated to a valid hash, it can be recalculated instantly and reliability.  This verifies the proof of work.

    The miner that successfully hashes the block is awarded an amount of Bitcoins for their work.  This is how Bitcoins are minted.The first miner to hash the block successfully gets the reward.  As a result, the network races to verify transactions.  Most Bitcoin transactions are committed to the blockchain within 10 minutes!

    Safety.

    This intensive hashing process also makes it virtually impossible to adjust the blockchain retroactively.  If an attacker wanted to undo or alter a transaction that happened in the past, the attacker would have to re-hash the block and every block after it since each block contains the hash of the block before it.  This is too much work for one adversary to achieve in a million lifetimes.

    As a result, once a transaction has been committed to the blockchain, it is safe for eternity.  Bitcoin does not have refunds, or chargebacks for this reason.

    Power.

    Remember when I stated that one computer would take nearly 98 years to solve a block?  This leads to the exploitation of a very precious resource: our computing power.  At the moment, the Bitcoin network is capable of mining a block in about 10 minutes.

    Mining is most commonly completed in pools that allow participants to solve hashes and share on the minted coins.  You are rewarded in proportion to the amount of work your computer completed.

    Conclusion

    Bitcoin is valuable, and we aren’t just pretending like it is for the sake of fantasy.  As with every other valuable currency, there is a proof of work involved in discovering new Bitcoins.

    Rather than physically mining gold or precious metals, the Bitcoin network is seeking an answer (the hash) to a math problem that can only be found by trial and error.  The hash uses transaction metadata and a random number so that any changes in transactions would require rehashing each block of transactions.  As a result, it is impossible to retroactively adjust a transaction.

    Try it!

    Bitcoin is cool.  It makes it easy to send money avoiding restrictions regarding exchange rates and repatriation of money.

    If you are looking to try Bitcoin, head to trybtc.com.  They’ll transfer a tiny amount of Bitcoin to a wallet of your choice.

    Thanks for reading!  If you want to send me Bitcoin for any reason, my public address is 16sVVZiJuCUBpWhKRDse1AzSkNvxHNgceT

  • jQuery Goodies!

    I started a new GitHub repo to fill with jQuery utilities and behaviors.

    The initial commit is filled with viewport sizing utilities.  The utilities make it easy to use viewport-relative sizing in situations where browsers do not support VW and VH.

    Something that I use a lot is the Letterboxing module.  Simply by adding .jq-letterbox, you can strictly enforce an element to have a widescreen aspect ratio.  This is very handy for sites that show lots of YouTube movie trailers.

    Grab the Repo

    https://github.com/bradkovach/jqUtilities

    Installation

    1. Import a recent version of jQuery

    Note: script tags do not need type=”text/javascript” when using HTML5.

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

    2. Import the utilities AFTER jQuery

    <script src="jqUtilities.viewport.js"></script>

     Usage

    Full usage information is available at the head of the jqUtilities.viewport.js file.

    /*
    .js-vh: Viewport Height positioning
    	required classes:
    		js-vh
    	required attributes:
    		data-vh: a numeric value greater than 0 (zero).  1vw is 1% of the viewport's height.
    	example:
    		<div class="js-vh" data-vh="50"
    			<!-- this will be half the height of the viewport -->
    		</div>
    	output:
    		via jqu_log: reports new height in vh
    		via jqu_log: reports when requested vh is invalid
    */

    Help

    By default, these utilities log errors to the console.

    Known Limitations

    • Mixed markup and style information.  Style information is hard-coded into HTML markup.  This might be undesirable.
    • This is not a polyfill.  It is a workaround.  Nice polyfills exist, and that might be a better option for you.

     

  • The Developer’s New Struggle: Data Caps

    These past 10 years have hosted a huge evolution in our data distribution technology. Since 2000, we’ve watched household broadband speeds move from about 0.5 mbps to somewhere around 20 mbps on average. This is awesome.

    We have new LTE networks that are capable of pushing around 50 mbps over the air. Please note: this is faster than most household connections.

    We live in an era of increasing bandwidth, and this is good news for web developers. This allowed sites like YouTube to become popular, because they finally had customers that could enjoy their product.

    But developers have new struggles! Now, developing truly mobile applications (both native apps and web apps) must account for the new obstacle: carrier data caps.

    How annoying. We’ve finally been given a trove of bandwidth (both low latency and high capacity), and we have to start using it very carefully–dare I say responsibly…

    It’s food for thought. We have better networks and more ubiquitous access but increasingly more consumers have charted courses for the edge of a bandwidth cliff every single month.

    So we must continue to adapt.

    Things That Are Concerning

    • Popular development frameworks can be rather bloated.  Twitter Bootstrap (minified) is 101kb and another 17kb if you want the responsive tweaks. .
    • Responsive images are not really taking off. Even Apple does not have a decent implementation for responsive image serving that doesn’t involve convoluted Javascript.
    • Developers that don’t pay attention to client-side caching techniques.  Make browsers get changes by using ?ver=[autoincrement] to force browsers to reload only when content changes.
    • Everybody wants “apps” and these apps need data. Lots of these apps send a lot of data (either as JSON or XML) without implementing a cache mechanism.  Twitter uses a neat trick to keep data transfer down: the since_id query parameter.  This minimizes payload by ignoring content that the client probably already has.
    • XML has too much data overhead on structure to be considered efficient, yet it is hailed as a great data interchange format–NOT FOR THE WEB!  Typically ever XML tag is matched by a closing version of the tag.  Look at the overhead here: <strong>this is bold text</strong>.  The strong tag and its closing tag take up 17 characters, which is equal to the length of the content it formats.  This is a massive overhead!  Mobile clients shouldn’t rely on XML for updating content (like a timeline of continuously updating information).

    This was merely a brief rant about implications about data load.  This doesn’t even consider some of the changes that occur with data throttling, and packet shaping.

    Worrying about data overhead is the new black.

  • Find Your Contrahobby

    We’ve all been there: you’ve indulged in something you love so much that you get sick of it.  This something you love is your passion.  Hopefully this passion is your life’s work.  Maybe you’re a scientist working to find a cure.  Maybe you’re really into Dungeons and Dragons.  Perhaps you knit, or craft something with your hands.  You genuinely love it.  You love it so much that it is your hobby.

    You love it, but now, unfortunately, you’ve overindulged.  You’re looking at something you love and you’re JUST. FUCKING. SICK. OF. IT.

    So what do you do?  You’re experiencing burn out.  This happens to college students every semester.  And unfortunately, this happens to the best of us on occasion.  Well, it’s my opinion that you need to find your contrahobby.  Don’t bother Googling this word, because I made it up.

    Let me first explain my personal etymology of this word.  It’s composed of a familiar word with a familiar prefix:

    Hobby – An activity done regularly in one’s leisure time for pleasure.
    Contra – against, in opposition or contrast to.

    So what I’m proposing is you find a hobby that is in opposition or contrast to your regular hobby.

    I think of the word contra because of a simple concept in Discrete Mathematics: the contrapositive.  A contrapositive is a statement crafted in such a way that it is equally valid to another statement, but entirely different.  Formally, the definition states

    If P, then Q;
    If not Q, then not P;

    For example, take a look at this contrapositive:

    If Socrates is man, then Socrates is human;
    If Socrates is NOT human, then Socrates is NOT man.

    They’re both equally true.  Both say the exact same thing about Socrates, just differently.

    The contrahobby has a similar purpose.  It uses a different approach to arrive at the same solution (the solution being that my mind is occupied with something that helps me unwind, relax, and enjoy myself).

    Now, the proof of this won’t work out mathematically, but it does make sense topically.

    My hobby (and my work) is web development.  I like reading about web development in my spare time, and I like playing around with many new techniques to make something work better, faster, or more intuitively.

    But when that gets old, and my mind won’t take anymore, I turn to my contrahobby: cooking.  Yes, cooking.  It doesn’t make sense.  It doesn’t seem related to my interests–and that’s what makes it a contrahobby.  It is a very NOT computer way to solve a problem that normally takes a computer to solve.

    So, hopefully I’ve illustrated the contrahobby concept to you adequately and I’ve given you some insight into another way to solve your problems.

    Do you have a contrahobby?  Are you burnt out?  Let’s talk about it.  Leave a comment, or send me a tweet @bradkovach.

     

  • Increase PHP’s upload capacity

    Handy piece of code I use all the time when set up WordPress installation. Change the 30M to whatever you want. 2048M is good for 2 gigabytes.

    php_value upload_max_filesize 30M
    php_value post_max_size 30M

    via Can’t get past the 2MB upload limit with .htaccess or php.ini drupal.org.

  • #challenge: Write an API

    If you manage a set of code, take some time today to improve your flexibility by replacing a certain set of code with an API.

    Common characteristics of an API

    • A logical group of functions surrounding a task or set of data.
    • A reusable system that is easy to document, and a joy to use.
    • Easier bug fixes.  Fix it once, and it’s fixed everywhere.

    You might have a set of tasks or functions on a website, or in a program that could really benefit from reduced code duplication. Things like…

    • Email systems.  Do you send emails a lot?  Reduce the work involved in sending emails, by writing a nice email API. $email->setSubject(“Hello!”); $email->setMessage($message); $email->setRecipient($to); $email->send();
    • Formatting systems. If you have structured semantic data, waste less time formatting it by creating a slick formatting engine.  Array goes in; formatted data comes out.$formatting->printUserList($users);
    • Logging API.  Create a simple system for you to write entries to a log file.  Rather than complicated stacks of code, use a simple function call like $debug->log(“There was a problem”)
  • Freebie: Handy VBscript Nonce Script

    Here’s a handy snippet to help you keep your VBscript forms secure. (If you’re ever using vbscript for any reason)

    <%
    
    Session("last_nonce") = Session("current_nonce")
    
    sent = Request.Form("current_nonce")
    valid = Session("last_nonce")
    
    max=100000
    min=1
    Randomize
    
    Session("uwbkst_nonce") = "n"&(Int((max-min+1)*Rnd+min))
    
    %>

    And then, somewhere in your form, include

    <input type="text" name="uwbkst_nonce" value="<%=Session("uwbkst_nonce") %>">

    And then in your form processor file

    
    <% If sent = valid Then
    ' do something
    End If %>
    
  • Make Twitter Tools respect your Tweets

    Twitter Tools is defacto Twitter-WordPress integration software, but it has one glitch that is easy to fix: tweet posts are formatted so horribly, it’s criminal.

    By default Twitter Tools places the full text of a Tweet in post_content, and an abbreviated, ugly version of the tweet in post_title.  This is fine, because there are (usually, depending on your theme) formats for the proper display of a status.

    Open twitter-tools.php.  After line 515, which reads

    add_post_meta($post_id, 'aktt_twitter_id', $tweet->tw_id, true);

    type a new line that reads

    set_post_format($post_id, 'status');

    Next time Twitter Tools fetches tweets, it will format them as statuses.