Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "crypto/hmac"
- "crypto/sha256"
- "encoding/hex"
- "fmt"
- "math"
- "math/big"
- )
- const (
- AuditSeed = "5d1d88ae4cc8c868372ed48d5e8eb84767212f3c984e6b9bd30940b2ca17e17b"
- GameSeed = "dc26e42bcdf464b1cd88b84e1a77a23991dd577d935e28b1ea950c9868aba53b"
- ClientSeed = "scenic makeshift rock"
- )
- func main() {
- roll := uint64(590)
- result := GameResult(AuditSeed, GameSeed, ClientSeed, roll)
- fmt.Println(result)
- }
- func GameResult(auditSeed, gameSeed, clientSeed string, nonce uint64) uint {
- const nBits = 52
- auditWagerSeed := hmacSha256(auditSeed, fmt.Sprint(nonce))
- gameHashString := hmacSha256(fmt.Sprintf("%s|%s|%d", gameSeed, clientSeed, nonce), auditWagerSeed)
- gameHash, err := hex.DecodeString(gameHashString)
- if err != nil {
- panic(err)
- }
- random := new(big.Int).SetBytes(gameHash)
- random.Rsh(random, sha256.Size*8-nBits)
- r := random.Uint64()
- X := float64(r) / math.Pow(2, nBits) // uniformly distributed in [0; 1)
- result := math.Floor(99 / (1 - X))
- result = math.Max(100, math.Min(result, 100000000))
- return uint(result)
- }
- func hmacSha256(key, message string) string {
- hash := hmac.New(sha256.New, []byte(key))
- hash.Write([]byte(message))
- return hex.EncodeToString(hash.Sum(nil))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement