Skip to content

Instantly share code, notes, and snippets.

@particle4dev
Forked from Bunjin/RNG.sol
Created September 12, 2019 10:24
Show Gist options
  • Save particle4dev/a241222f16a7126ad153635902690b00 to your computer and use it in GitHub Desktop.
Save particle4dev/a241222f16a7126ad153635902690b00 to your computer and use it in GitHub Desktop.
Blockhash RNG immune to tampering for PoS and PoW
contract RNG {
bytes32 encrypted_seed;
bytes32 revealed_seed;
bytes32 revealed_secret;
uint odds;
uint bet_blocknumber;
uint bet_input;
bool bet_made;
uint bet_result;
string log;
function RNG(){
odds=100;
//example seed
//made by secret = “0x006ee0953c7Bb75CA6De27f58b5512390B1C4d6a”
// seed=”0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8"
encrypted_seed=”0x903e88616c7587ec063b5c79f4f5aad24de25f1e149a87edeeb964cbfbf70b38";
}
function bet(uint integer_0_to_odds_minus_1){
if (bet_made) throw;
if (integer_0_to_odds_minus_one <= odds) {
bet_input = integer_0_to_odds_minus_one;
bet_blocknumber=block.number;
bet_made = true;
}
}
function reveal(bytes32 rev_seed_, bytes32 rev_secret_) {
//verify that bet has been made
if (!bet_made) throw;
//verify that the block of the bet has been mined.
if (block.number>bet_blocknumber){
//verify if the revealed seed and secret match with encrypted committed hash.
if ( sha3(rev_seed_,rev_secret_)!=encrypted_seed) throw;
revealed_seed=rev_seed_;
revealed_secret=rev_secret_;
}
else throw;
//The EVM only stored the last 256 blockhashes
// After that it’s too late to reveal and an alternative resolution should be provided here
if (block.number>bet_blocknumber+250) throw;
//compute pseudo RNG
bet_result=uint(sha3(revealed_seed,block.blockhash(bet_blocknumber)))%(odds);
//solve bet
if (bet_input == bet_result){
log=”You won”;
}
else{
log=”You lose”;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment