Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
3.9 kB
20
Indexable
import hmac
import hashlib
import csv
import os
from typing import List, Tuple

def byteGenerator(serverSeed: str, clientSeed: str, nonce: int, cursor: int):
    currentRound = cursor // 32
    currentRoundCursor = cursor % 32
    while True:
        message = f"{clientSeed}:{nonce}:{currentRound}".encode()
        hmacDigest = hmac.new(serverSeed.encode(), message, hashlib.sha256).digest()
        while currentRoundCursor < 32:
            yield hmacDigest[currentRoundCursor]
            currentRoundCursor += 1
        currentRoundCursor = 0
        currentRound += 1

def generateFloats(serverSeed: str, clientSeed: str, nonce: int, cursor: int, count: int) -> List[float]:
    rng = byteGenerator(serverSeed, clientSeed, nonce, cursor)
    bytesList = [next(rng) for _ in range(count * 4)]
    return [sum(byte / 256**(i+1) for i, byte in enumerate(bytesList[j:j+4])) for j in range(0, len(bytesList), 4)]

def plinkoEvent(floatValue: float) -> int:
    return 0 if floatValue * 2 < 1 else 1  # 0 for left, 1 for right

def getPlinkoResult(serverSeed: str, clientSeed: str, nonce: int, cursor: int, rows: int) -> int:
    floats = generateFloats(serverSeed, clientSeed, nonce, cursor, rows)
    return sum(plinkoEvent(floatValue) for floatValue in floats)

def generateNextSeeds(serverSeed: str, clientSeed: str) -> Tuple[str, str]:
    nextServerSeed = hashlib.sha256(serverSeed.encode()).hexdigest()
    nextClientSeed = hashlib.sha256(clientSeed.encode()).hexdigest()
    return nextServerSeed, nextClientSeed

def isValidSeedPair(serverSeed: str, clientSeed: str, nonce: int, cursor: int, rows: int) -> bool:
    result = getPlinkoResult(serverSeed, clientSeed, nonce, cursor, rows)
    return result in [0, 16]

def generatePlinkoResultsCsv(initialServerSeed: str, initialClientSeed: str, nonceRange: Tuple[int, int], cursor: int, rows: int, iterations: int, outputFile: str):
    try:
        with open(outputFile, 'w', newline='') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(['Iteration', 'ServerSeed', 'ClientSeed', 'Nonce', 'PlinkoResult'])
            
            serverSeed, clientSeed = initialServerSeed, initialClientSeed
            nonceStart, nonceEnd = nonceRange
            iterationCount = 0

            while iterationCount < iterations:
                if isValidSeedPair(serverSeed, clientSeed, nonceStart, cursor, rows):
                    for nonce in range(nonceStart, nonceEnd):
                        plinkoResult = getPlinkoResult(serverSeed, clientSeed, nonce, cursor, rows)
                        writer.writerow([iterationCount + 1, serverSeed, clientSeed, nonce, plinkoResult])
                    iterationCount += 1
                else:
                    print(f"Skipping invalid seed pair: {serverSeed[:10]}... {clientSeed[:10]}...")
                
                serverSeed, clientSeed = generateNextSeeds(serverSeed, clientSeed)

    except PermissionError:
        print(f"Write error, creating new csv file.")
        baseFilename, ext = os.path.splitext(outputFile)
        newFilename = f"{baseFilename}New{ext}"
        generatePlinkoResultsCsv(initialServerSeed, initialClientSeed, nonceRange, cursor, rows, iterations, newFilename)

def main():
    initialServerSeed = input("Enter initial unhashed server seed: ")
    initialClientSeed = input("Enter initial client seed: ")
    rows = int(input("Insert desired rows: "))
    nonceStart = int(input("Enter start nonce: "))
    nonceEnd = int(input("Enter end nonce: "))
    iterations = int(input("Enter number of iterations: "))
    cursor = 0
    outputFile = 'plinkoResults.csv'

    generatePlinkoResultsCsv(initialServerSeed, initialClientSeed, (nonceStart, nonceEnd), cursor, rows, iterations, outputFile)
    print(f"Plinko results have been written to {outputFile}")

if __name__ == "__main__":
    main()

    
Leave a Comment