Ever wanted to hold a contest but wanted to show that you weren’t playing favorites? Ever felt cheated at arcade games like the Tower of Power because you weren’t sure if they actually let you win? Those days are over.
When I learned about this idea of “provably fair gambling” first from from SatoshiDice then again from Namecoin, I had so much fun with the idea that I decided to spin off my own contest and help someone from UCR at the same time.
A random number from 0-1 and a random string will be generated. I will not go over how to generate these, but I recommend using a good uniform distribution random number generator for the contest number. The number is called the “contest number” and the string is called the “salt”. Both are concatenated to form a hash input. Example:
Contest number: “0.607561205055”
Input: ” 0.607561205055_@f&#@RTWVSg34tgeRY4ebntryk67U*YRegszbfgreke%@xfnZ”
The input then goes through a SHA256 hashing function. This is where the magic happens. The result, or “hash digest”, is published when the contest starts. Example:
- The hashing algorithm effectively hides the contest number but shows that I generated one already. I just didn’t share it with you. This same method is used on your computer to store passwords.
- The salt is very long with capitals and special characters so that it is hard to brute force.
- Even if someone was able to brute force the contest number, they would still have to follow condition #2 in the latter half of this post. And anyone who can brute force this hash doesn’t need the prizes I am providing. They should be hired already.
When the contest ends, the list of top-level commenters is sorted chronologically and filtered for the word “entry” as the first word. Second entries and beyond from the same person are removed. For example, the initial comment list may look like this:
and is filtered to this list:
- Alice Marten
- Brian Edwards
- Arianna Smith
- Bob Dylan
If there are too many comments then you can use the Facebook Graph API to sort top-level comments in chronological order.
The winning number = ceil((contest number) (number of entries))
In this case, the winning number would be ceil(0.607561205055 * 4)=3 and the winning name would be “Arianna Smith”. If the winner does not want the prize, the next person on the list will be winner, wrapping around until there is no one left.
Once the winner has been determined, the contest facilitator would post a comment revealing the number of entries, winner’s name, and input containing the contest number and salt. The post would resemble this:
The winner is Arianna Smith! The hash input is 0.607561205055_@f&#@RTWVSg34tgeRY4ebntryk67U*YRegszbfgreke%@xfnZ
This way, anyone can now hash the input to obtain the digest specified at the beginning of the contest.
To generate a SHA256 hash, use one of the following:
Mac: $ echo -n “input” | shasum5.16 -a 256 # the input cannot contain special characters like ! &
If you enjoyed this article, then you will enjoy cryptography. And you will love the idea of bitcoin as much as I do. Read about bitcoin on the following websites:
Have fun holding your own contests!
EDIT: I found a reddit thread that wants to achieve the same thing as this post, but does it in a slightly better way. It uses the bitcoin block hash as the “Secret”. This is better because in my scheme, I pick the secret beforehand and could possibly influence the contest results. But by using the bitcoin block hash, its an agreed upon random number that no one knows ahead of time. By dividing the block hash by the target, you can get a uniform distribution between 0 and 1 which you can then use instead of the above secret.