Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "crypto/hmac"
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "fmt"
  8. "math"
  9. "math/big"
  10. )
  11.  
  12. const (
  13. AuditSeed = "5d1d88ae4cc8c868372ed48d5e8eb84767212f3c984e6b9bd30940b2ca17e17b"
  14. GameSeed = "dc26e42bcdf464b1cd88b84e1a77a23991dd577d935e28b1ea950c9868aba53b"
  15. ClientSeed = "scenic makeshift rock"
  16. )
  17.  
  18. func main() {
  19.  
  20. roll := uint64(590)
  21.  
  22. result := GameResult(AuditSeed, GameSeed, ClientSeed, roll)
  23.  
  24. fmt.Println(result)
  25.  
  26. }
  27.  
  28. func GameResult(auditSeed, gameSeed, clientSeed string, nonce uint64) uint {
  29. const nBits = 52
  30.  
  31. auditWagerSeed := hmacSha256(auditSeed, fmt.Sprint(nonce))
  32. gameHashString := hmacSha256(fmt.Sprintf("%s|%s|%d", gameSeed, clientSeed, nonce), auditWagerSeed)
  33. gameHash, err := hex.DecodeString(gameHashString)
  34. if err != nil {
  35. panic(err)
  36. }
  37.  
  38. random := new(big.Int).SetBytes(gameHash)
  39. random.Rsh(random, sha256.Size*8-nBits)
  40. r := random.Uint64()
  41.  
  42. X := float64(r) / math.Pow(2, nBits) // uniformly distributed in [0; 1)
  43.  
  44. result := math.Floor(99 / (1 - X))
  45. result = math.Max(100, math.Min(result, 100000000))
  46. return uint(result)
  47. }
  48.  
  49. func hmacSha256(key, message string) string {
  50. hash := hmac.New(sha256.New, []byte(key))
  51. hash.Write([]byte(message))
  52. return hex.EncodeToString(hash.Sum(nil))
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement