Magic Dice Tech Talk - Provably fair and transparent

in #steemit2 years ago (edited)

Magic Dice is a provably fair and transparent dice game built on the Steem blockchain. If you haven't tried it yet, go to https://magic-dice.com and give it a try. In this post we want to explain the tech behind the game. This post is rather technical. If you just want some general information about the game, go to the introduction post.





Random Numbers

Random numbers for Magic Dice are generated by the use of two seeds: Server seed and client seed. Every player can choose his own client seed. The server seed is generated on the server, hashed and posted to the Steem blockchain. Since the server seed hash was published before every dice roll, there is no way for us to alter the outcome without the verification failing.

The hash is a SHA256 hash which can be created and verified with following script:

const hash = crypto.createHash('sha256').update(serverSeed).digest('hex');





Seed/Hash Publishing

The following image shows how seeds and seed hashes are published:

A new seed and seed hash (Seed 1/SeedHash 1) are generated on the server. SeedHash1 is posted to the blockchain. After n blocks (n is 20 atm), a new seed and seed hash (Seed 2/SeedHash 2) are generated. The new seed hash is posted to the blockchain. On the next block, the old seed is published. A seed/seed hash is valid after the block it was published until a new seed/seed hash is published (Including the block where the new seed hash was published).

In this example Seed1 is valid from Block 2 - Block 21. The seed is generated and the seed hash published before the bets take place. That means that it can be proven that all server seeds were generated before any user placed a bet. After a seed was published, all bets placed earlier can be verified. At the moment that means that a dice roll can be verified after max. 20 blocks or around 1 minute. In the future we might reduce that time to make it possible to verify dice rolls earlier.

You can see on https://steemd.com/@magicdice how the seeds/hashes are published via custom_json:





Dice Roll Script

Dice rolls are calculated with following script:

let cryptoHmac = crypto.createHmac('sha256', serverSeed);
let resultSeed = cryptoHmac.update(`${clientSeed}-${blockNumber}`).digest('hex');
let resultNumber = parseInt(resultSeed.substr(0, 10), 16);
let diceRoll = (resultNumber % 100) + 1;

Additionally to the client seed, the block number acts as nonce. To create the result seed, the server seed, client seed and block number are used and hashed with HMAC SHA256. The first 10 characters of the result seed hex are used and parsed to integer. The final dice roll is then calculated from the result number via % 100 + 1.





Verify Dice Roll

Every dice roll can be verified on https://magic-dice.com/verify. Enter the bet Id that's included in every transaction. Note that dice rolls can only be verified after the server seed has been published. The page includes a script that can be copied and executed on an online Node.js editor like Repl.it. The script includes the whole calculation of the dice roll and all necessary values.





Report

We have uploaded a report script for Magic Dice on Github - Magic Dice Report. This script allows to verify all dice rolls for a certain range of blocks. Additionally it shows how many bets were placed, total payout, highest payout, ... We plan to include an option to create a report for Steemit as well. We are going to create daily transparency reports and encourage other users to do the same.

If you have any questions, just contact us on Discord or leave a comment.


Sort:  

If you want to sell this file for Bitcoins, use https://SatoshiDisk.com/
Don't need to create account or pass KYC. Upload, Share, Profit!