#!/usr/bin/env python3 from Crypto.Util.number import ( bytes_to_long, long_to_bytes ) from hashlib import md5 import os, signal import sys import random
BITS = 128
classClawCrane(object): def__init__(self) -> None: self.seed = bytes_to_long(os.urandom(BITS//8)) self.bless = 0 self.score = 0 defget_input(self, prompt="> "): print(prompt, end="") sys.stdout.flush() returninput() defcheck_pos(self, pos, moves): col, row = 0, 0 for move in moves: if move == "W": if row < 15: row += 1 elif move == "S": if row > 0: row -= 1 elif move == "A": if col > 0: col -= 1 elif move == "D": if col < 15: col += 1 else: return -1 print(col, row) return pos == [col, row] defgen_chaos(self, inp): defmapping(x): if x=='W': return"0" if x=='S': return"1" if x=='A': return"2" if x=='D': return"3" vs = int("".join(map(mapping, inp)), 4) chaos = bytes_to_long(md5( long_to_bytes((self.seed + vs) % pow(2,BITS)) ).digest()) self.seed = (self.seed + chaos + 1) % pow(2,BITS) return chaos defdestiny_claw(self, delta): bits = bin(delta)[2:] iflen(bits) < 128+self.bless: bits += "0"*(128+self.bless - len(bits)) c = random.choice(bits) if c=='0': returnTrue else: returnFalse defrun(self): pos = [random.randrange(1,16), random.randrange(1,16)] moves = self.get_input(f"i am at {pos}, claw me.\nYour moves: ") iflen(moves) > 100: print("too many steps") return ifnot self.check_pos(pos, moves): print("sorry, clawed nothing") return r = self.gen_chaos(moves[:64]) print(f"choas: {r}") p, q = map(int, self.get_input(f"give me your claw using p,q and p,q in [0, 18446744073709551615] (e.g.: 1,1): ").split(",")) ifnot (p>0and p<pow(2,BITS//2) and q>0and q<pow(2,BITS//2)): print("not in range") return delta = abs(r*q - p*pow(2,BITS)) if self.destiny_claw(delta): self.score += 10 self.bless = 0 print("you clawed it") else: self.bless += 16 print("sorry, clawed nothing")
whileTrue: a = getRandomNBitInteger(dbits) b = getRandomNBitInteger(bbits) c = getRandomNBitInteger(bbits) p1 = a * b * c + 1 if isPrime(p1): # print("p1 =", p1) break
whileTrue: d = getRandomNBitInteger(dbits) p2 = b * c * d + 1 if isPrime(p2): # print("p2 =", p2) break
whileTrue: e = getRandomNBitInteger(bbits) f = getRandomNBitInteger(bbits) q1 = e * d * f + 1 p3 = a * e * f + 1 if isPrime(q1) and isPrime(p3): # print("p3 =", p3) # print("q1 =", q1) break
whileTrue: d_ = getRandomNBitInteger(dbits) if GCD(a * b * c * d * e * f, d_) != 1: continue e_ = inverse(d_, a * b * c * d * e * f) k1 = (e_ * d_ - 1) // (a * b * c * d * e * f) assert e_ * d_ == (a * b * c * d * e * f) * k1 + 1 q2 = k1 * e * f + 1 q3 = k1 * b * c + 1 if isPrime(q2) and isPrime(q3): # print("q2 =", q2) # print("q3 =", q3) # print("e =", e_) # print("d =", d_) break
whileTrue: a = getRandomNBitInteger(dbits) b = getRandomNBitInteger(bbits) c = getRandomNBitInteger(bbits) p1 = a * b * c + 1 if isPrime(p1): # print("p1 =", p1) break
whileTrue: d = getRandomNBitInteger(dbits) p2 = b * c * d + 1 if isPrime(p2): # print("p2 =", p2) break
whileTrue: e = getRandomNBitInteger(bbits) f = getRandomNBitInteger(bbits) q1 = e * d * f + 1 p3 = a * e * f + 1 if isPrime(q1) and isPrime(p3): # print("p3 =", p3) # print("q1 =", q1) break
whileTrue: d_ = getRandomNBitInteger(dbits) if GCD(a * b * c * d * e * f, d_) != 1: continue e_ = inverse(d_, a * b * c * d * e * f) k1 = (e_ * d_ - 1) // (a * b * c * d * e * f) assert e_ * d_ == (a * b * c * d * e * f) * k1 + 1 q2 = k1 * e * f + 1 q3 = k1 * b * c + 1 if isPrime(q2) and isPrime(q3): # print("q2 =", q2) # print("q3 =", q3) # print("e =", e_) print("d =", d_) break