For gambling games, players are always afraid that they will be cheated out of money. This is easy to understand because when playing online casino all results seem to be easily changed by technology. Because of such fundamental risks, the Bitcoin gambling community has a solution it called Provably fair. . Just like what Bitcoin solves, this method does not require a third party, it makes the game completely transparent by using advanced encryption methods to ensure that the game results are not compromised. fake . The easiest to use is the Provably fair verifiers tools that allow players to verify the results after each roll to ensure that they are not cheated during the game by entering the information provided by the website. and check the roll results section in short, this is a way that allows the player to check if a number was created really randomly or without being edited in any way. Recommended Verifiers pages
- BetKing verifier
- BetterBets verifier
- BitDice.me verifier
- Bitsler verifier
- Crypto-Games.net verifier
- Just-Dice.com verifier
- Nitrogen Dice verifier
- Primedice verifier
- SafeDice verifier
However, we still feel that something is wrong. To be sure, we should understand how the Provably fair works. This article I will try to explain the most obvious way how it works.
How does provably fair work?
Although there are several ways to implement the Provablu fair and I will try to explain the most common way. With this method, each roll-result will be calculated based on the following variables:
- Serverseed: Provided by gambling site
- Clientseed: Provided by the user’s browser or can be customized by the user
- Nonce: A number that increases with every bet
The player will receive an encrypted hash of serverseed before the player starts gambling and therefore the site will not be able to change it. However, it is encrypted that the player cannot calculate the outcome of his roll. .
The player’s browser will generate a random clientseed or the player can fix it manually. In this way the site certainly would not know ng clientseed of play
Next is Nonce starting from 0 or 1 depending on the website and after each bet the Nonce number will increase by 1.
Calculation of roll result
Suppose we have
1 2 3 4 5 | Clientseed: ClientSeedForDiceSites.com Serverseed unhashed: 293d5d2ddd365f54759283a8097ab2640cbe6f8864adc2b1b31e65c14c999f04 Serverseed hashed: 5ac59780d512265230d5efb3cc238886dc1b457a80b54fbf1f920b99c6505801 Nonce: for the first bet it is 0 on this dice site (PrimeDice) |
PrimeDice first uses HMAC hashes with values according to the following formula:
1 2 3 4 5 6 | hmac-sha512(server_seed, client_seed-nonce) # Theo ví dụ hmac-sha512(293d5d2ddd365f54759283a8097ab2640cbe6f8864adc2b1b31e65c14c999f04, ClientSeedForDiceSites.com-0) |
We will use an online tool freeformatter.com/hmac-generator.html to calculate In a nutshell, we sign the clientseed + nonce with the serverseed key according to HMAC-SHA512
Result
1 2 | aa671aad5e4565ebffb8dc5c185e4df1ae6d9aca2578b5c03ec9c7750f881922276d8044e5e3d84f158ce411f667e224e9b0c1ac50fc94e9c5eb883a678f6ca2 |
Now select the first 5 characters (aa671) and convert from hex value to decimal. You can also use online tools like statman.info/conversions/hexadecimal.html to calculate.
If the result is a number> 999,999 then we will take the next 5 characters (aad5e) in our case, 697969 is satisfied. Now we’re going to take that module’s number for 10000 and divide it by 100
1 2 3 4 5 6 | 697969%(10000)/100 # Kết quả 79.69% |
For the next bet the site will do the same so only the Nonce change increases with every roll and so the end result is random.
Verifying your roll results
Step 1
First check serverseed hashed to see if it is hashed from serverseed. We use another online tool to test ( xorbin.com/tools/sha256-hash-calculator ) to generate SHA256 from serverseed. You will compare this code with the server’s hashed code, if it is the same, then it is serverseed that was used for the roll result.
Step 2
Repeat the calculation and compare it with the returned roll server results
Use the probably fair tool
Because you can make tens, hundreds or even thousands of bets, it will be really hard every time we place a bet and then have to calculate it manually. So the Bitcoin Gambling community has supported a lot of tools so people can confirm their roll results to see if there’s really no change.
For example using Primedice you simply enter the required web page parameters
If the result is the same then we know that we don’t get cheated when playing gambling )))
Development
The theory stops here is then bh to practice.
Each result will be based on client-seed , server-seed and nonce
Generate random numbers
First we will randomly generate the client and server seed
Client seed:
1 2 3 4 5 6 7 8 9 | <span class="token keyword">function</span> <span class="token function">randomString</span> <span class="token punctuation">(</span> length <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">const</span> availableChars <span class="token operator">=</span> <span class="token string">"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"</span> <span class="token punctuation">;</span> <span class="token keyword">let</span> randomString <span class="token operator">=</span> <span class="token string">''</span> <span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span> <span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span> i <span class="token operator"><</span> length <span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> randomString <span class="token operator">+=</span> availableChars <span class="token punctuation">[</span> Math <span class="token punctuation">.</span> <span class="token function">floor</span> <span class="token punctuation">(</span> Math <span class="token punctuation">.</span> <span class="token function">random</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token operator">*</span> availableChars <span class="token punctuation">.</span> length <span class="token punctuation">)</span> <span class="token punctuation">]</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> randomString <span class="token punctuation">;</span> <span class="token punctuation">}</span> |
We can easily get random values from the above function for example:
1 2 3 4 | <span class="token function">randomString</span> <span class="token punctuation">(</span> <span class="token number">30</span> <span class="token punctuation">)</span> <span class="token comment">// jd2X84d1jvUvIqcaRFmVWIsot2zIeH</span> <span class="token function">randomString</span> <span class="token punctuation">(</span> <span class="token number">30</span> <span class="token punctuation">)</span> <span class="token comment">// XaX6MWShOwxAZbggz4dfTYVd6EuIzw</span> <span class="token function">randomString</span> <span class="token punctuation">(</span> <span class="token number">30</span> <span class="token punctuation">)</span> <span class="token comment">// 95zrd3MsUZJ6AOE9OdPu9ctKxmgQpy</span> |
Server seed This code is generated on the server side and we can use the encryption function
1 2 3 4 | <span class="token comment">// Generate 256 random characters</span> <span class="token keyword">const</span> crypto <span class="token operator">=</span> <span class="token function">require</span> <span class="token punctuation">(</span> <span class="token string">'crypto'</span> <span class="token punctuation">)</span> crypto <span class="token punctuation">.</span> <span class="token function">randomBytes</span> <span class="token punctuation">(</span> <span class="token number">256</span> <span class="token punctuation">)</span> <span class="token punctuation">.</span> <span class="token function">toString</span> <span class="token punctuation">(</span> <span class="token string">'hex'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> |
Combine all then calculated according to SHA-512 hash
1 2 3 4 5 6 7 8 | <span class="token keyword">const</span> crypto <span class="token operator">=</span> <span class="token function">require</span> <span class="token punctuation">(</span> <span class="token string">'crypto'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">const</span> combination <span class="token operator">=</span> <span class="token operator"><</span> <span class="token constant">SERVER</span> <span class="token constant">SEED</span> <span class="token operator">></span> <span class="token operator">+</span> <span class="token operator"><</span> <span class="token constant">CLIENT</span> <span class="token constant">SEED</span> <span class="token operator">></span> <span class="token operator">+</span> <span class="token operator"><</span> <span class="token constant">NONCE</span> <span class="token operator">></span> <span class="token keyword">const</span> hash <span class="token operator">=</span> crypto <span class="token punctuation">.</span> <span class="token function">createHash</span> <span class="token punctuation">(</span> <span class="token string">'sha512'</span> <span class="token punctuation">)</span> <span class="token punctuation">.</span> <span class="token function">update</span> <span class="token punctuation">(</span> combination <span class="token punctuation">)</span> <span class="token punctuation">.</span> <span class="token function">digest</span> <span class="token punctuation">(</span> <span class="token string">'hex'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> hash <span class="token punctuation">)</span> <span class="token comment">// 1f40fc92da241694750979ee6cf582f2d5d7d28e...</span> |
Now we can send the results to the client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <span class="token keyword">const</span> <span class="token function-variable function">getResult</span> <span class="token operator">=</span> hashedValue <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token comment">// the offset of the interval</span> <span class="token keyword">let</span> index <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span> <span class="token comment">// result variable</span> <span class="token keyword">let</span> result <span class="token punctuation">;</span> <span class="token keyword">do</span> <span class="token punctuation">{</span> <span class="token comment">// get the decimal value from an interval of 5 hex letters</span> result <span class="token operator">=</span> <span class="token function">parseInt</span> <span class="token punctuation">(</span> hashedValue <span class="token punctuation">.</span> <span class="token function">substring</span> <span class="token punctuation">(</span> index <span class="token operator">*</span> <span class="token number">5</span> <span class="token punctuation">,</span> index <span class="token operator">*</span> <span class="token number">5</span> <span class="token operator">+</span> <span class="token number">5</span> <span class="token punctuation">)</span> <span class="token punctuation">,</span> <span class="token number">16</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// increment the offset in case we will need to repeat the operation above</span> index <span class="token operator">+=</span> <span class="token number">1</span> <span class="token punctuation">;</span> <span class="token comment">// if all the numbers were over 999999 and we reached the end of the string, we set that to a default value of 9999 (99 as a result)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> index <span class="token operator">*</span> <span class="token number">5</span> <span class="token operator">+</span> <span class="token number">5</span> <span class="token operator">></span> <span class="token number">129</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> result <span class="token operator">=</span> <span class="token number">9999</span> <span class="token punctuation">;</span> <span class="token keyword">break</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">while</span> <span class="token punctuation">(</span> result <span class="token operator">>=</span> <span class="token number">1e6</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// the result is between 0-999999 and we need to convert if into a 4 digit number</span> <span class="token comment">// we a apply a modulus of 1000 and the 4 digit number is further split into a 2 digit number with decimals</span> <span class="token keyword">return</span> <span class="token punctuation">[</span> result <span class="token operator">%</span> <span class="token number">1e4</span> <span class="token punctuation">]</span> <span class="token operator">*</span> <span class="token number">1e-2</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">;</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token function">getResult</span> <span class="token punctuation">(</span> <span class="token string">'d9b9b9774d35566bdbd33a0050b15f7f3bc0ec68cff1b593994c04b05767573c0c002cb965d506e5ccedf803db8b58c27406e320f2c13dcbc247448dedccec12'</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> |
The article was translated from: https://dicesites.com/provably-fair
https://medium.com/@alexcambose/provably-fair-system-in-javascript-6457e028d2aa