

# This file was *autogenerated* from the file server.sage
from sage.all_cmdline import *   # import sage library

_sage_const_115792089210356248762697446949407573530086143415290314195533631308867097853951 = Integer(115792089210356248762697446949407573530086143415290314195533631308867097853951); _sage_const_0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF = Integer(0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF); _sage_const_0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b = Integer(0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b); _sage_const_3 = Integer(3); _sage_const_15957832354939571418537618117378383777560216674381177964707415375932803624163 = Integer(15957832354939571418537618117378383777560216674381177964707415375932803624163); _sage_const_66579344068745538488594410918533596972988648549966873409328261501470196728491 = Integer(66579344068745538488594410918533596972988648549966873409328261501470196728491); _sage_const_1 = Integer(1); _sage_const_2 = Integer(2); _sage_const_256 = Integer(256); _sage_const_0 = Integer(0); _sage_const_8 = Integer(8); _sage_const_10 = Integer(10); _sage_const_1337 = Integer(1337)
from random import randint
# from secrets import flag
# from Crypto.Cipher import AES
# from Crypto.Hash import SHA256
# from Crypto.Util.Padding import pad

class RNG:
    def __init__(self):
        p = _sage_const_115792089210356248762697446949407573530086143415290314195533631308867097853951 
        p = _sage_const_0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF 
        b = _sage_const_0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b 
        self.curve = EllipticCurve(GF(p), [-_sage_const_3 ,b])

        self.P = self.curve.lift_x(_sage_const_15957832354939571418537618117378383777560216674381177964707415375932803624163 )
		# 2347D02339CE6B6F40EBADF8D7968F7685F0E8A74E7AA53586393D157FADACE3
        self.Q = self.curve.lift_x(_sage_const_66579344068745538488594410918533596972988648549966873409328261501470196728491 )
		# 933292C549062B6CD9F2682336B60C4A74286AEAF9B627BF2211661B7E1182AB
        
        self.state = randint(_sage_const_1 , _sage_const_2 **_sage_const_256 )
        
    def next(self):
        r = (self.state * self.P)[_sage_const_0 ].lift()
        self.state = (r * self.P)[_sage_const_0 ].lift()
        return (r * self.Q)[_sage_const_0 ].lift() >> _sage_const_8 

class Casino:
    def __init__(self, rng):
        self.rng = rng
        self.balance = _sage_const_10 

    def play(self):
        print("Your bet: ", end='')
        bet = input()
        if (bet in ["0", "1"]):
            bet = Integer(bet)
            if (self.rng.next() % _sage_const_2  == bet):
                self.balance += _sage_const_1 
            else:
                self.balance -= _sage_const_1 
                if (self.balance == _sage_const_0 ):
                    print("You are broke... play again")
                    exit()
            print(f"Your current balance: {self.balance}")
        else:
            print("Invalid bet option, use either 0 or 1")
            
    def buy_flag(self):
        if (self.balance >= _sage_const_1337 ):
            # key = SHA256.new(str(self.rng.next()).encode('ascii')).digest()
            # cipher = AES.new(key, AES.MODE_ECB)
            # print(cipher.encrypt(pad(flag.encode('ascii'), 16)).hex())
            print("here is your flag!")
            exit(_sage_const_0 )
        else:
            print("No flag for the poor. Gamble more")

def main():
    rng = RNG()
    casino = Casino(rng)

    print("Welcome to the Casino")
    print(f"Your id is {rng.next()}")
    print("What would you like to do?")
    print("(p)lay and win some money")
    print("(b)uy the flag")

    while (True):
        print("> ", end='')
        option = input()

        if (not option in ["b", "p"]):
            print("Unknown option, use 'b' or 'p'")
        elif (option == "b"):
            casino.buy_flag()
        elif (option == "p"):
            casino.play()

if __name__ == '__main__':
    main()


