def get_users_priv_cubes(): user = generate_user() cube = generate_cube() info = generate_info() r = remote(*ADDR) register(r, user, cube, info) users_list = list_users(r) priv_cubes = {} base_cube_inversed = Cube().move("U R' U' F'") for server_user in users_list: if server_user == user: continue server_user_pub_cube = get_users_pub_cube(r, server_user) priv_cubes[server_user] = [ Cube(server_user_pub_cube).multiply(base_cube_inversed.pow(i)) for i in range(7) ] _exit(r) r.close() return priv_cubes
def login(r, user: str, cube: Cube): r.sendlineafter(b'> ', b'2') r.sendlineafter(b'> ', user) r.sendlineafter(b'> ', cube.as_str()) r.recvuntil(b'Your public cube is ') pub_cube = r.recvline(False).decode() try: return Cube.from_str(pub_cube) except Exception: return None
def _extend_keys(self, l): gs = [self.g] xs = [self.x] for _ in range(l - 1): gs.append(Cube(gs[-1]).move("U x'")) xs.append((xs[-1] + 0xDEADBEAF) % ORDER) return gs, xs
def get_users_pub_cube(r, user): r.sendlineafter(b'> ', b'3') r.sendlineafter(b"> ", user) r.recvuntil(b"User's public cube: ") pub_cube = r.recvline(False).decode() if pub_cube: return Cube.from_str(pub_cube)
def get_user_data(self, login): db = self._get_db() data = db.hgetall(f'{CUBES_PREFIX}{login}') pub_cube = Cube.from_str(data[b'pub_cube'].decode()) priv_key = int(data[b'priv_key'].decode()) info = data[b'info'].decode() if data: return pub_cube, priv_key, info
def long2cube(number): cp = [] corners = list(range(8)) for i in range(8, 0, -1): number, index = number // i, number % i cp.append(corners.pop(index)) ep = [] edges = list(range(12)) for i in range(12, 2, -1): number, index = number // i, number % i ep.append(edges.pop(index)) if get_parity(cp[:]) ^ get_parity(ep[:] + edges[:]): ep.append(edges[1]) ep.append(edges[0]) else: ep.append(edges[0]) ep.append(edges[1]) co = [] for i in range(7): number, index = number // 3, number % 3 co.append(index) co.append((24 - sum(co)) % 3) eo = [] for i in range(12): number, index = number // 2, number % 2 eo.append(index) eo.append((24 - sum(eo)) % 2) cube = Cube.from_dict({ 'center': list(range(8)), 'cp': cp, 'ep': ep, 'co': co, 'eo': eo }) return cube
def login(self): print("Your name:") login = input("> ") if not self.db.check_user(login): print('There are no user with your login! Register first!') return pub_cube, priv_key, info = self.db.get_user_data(login) print(f'Give me your cube:') try: priv_cube = Cube.from_str(input("> ").strip()) except Exception: print("This is not a cube!") return if priv_cube.multiply(self.base_cube.pow(priv_key)) != pub_cube: print('Wrong cube!') return print(f'Hello!\nYour public cube is {pub_cube.as_str()}') return login, pub_cube, info
def register(self): print("Your name:") login = input("> ") if self.db.check_user(login): print('There exist user with your login!') return print('Give me your cube:') try: priv_cube = Cube.from_str(input("> ").strip()) except Exception: print("This is not a cube!") return priv_key = randint(1, ORDER) pub_cube = priv_cube.multiply(self.base_cube.pow(priv_key)) print('Anything about yourself:') info = input("> ") self.db.set_user_data(login, pub_cube, priv_key, info) print(f'Hello!\nYour public cube is {pub_cube.as_str()}') return login, pub_cube, info
def flatten(c: cube.Cube) -> np.array: """ Flatten a cube into a 54x1 vector. """ return np.array([x for row in np.array(c.to_face()) for x in row])
def _decrypt(self, c1, c2, g, x): return long_to_bytes(cube2long(Cube(c2).multiply(c1.pow(ORDER - x))))
def _encrypt(self, msg, g, x): m = long2cube(bytes_to_long(msg)) y = randint(1, ORDER - 1) return g.pow(y), Cube(m).multiply(g.pow(x * y))
def __init__(self, g=None, x=None): self.g = g or Cube.generate() self.x = x or randint(1, ORDER - 1)
def generate_cube(): return Cube.generate()
b = bidirectional(r) b.biSearch() print('bidirectional search') print('path :', b.path) print('max memory', b.maxMemory) print('visited', b.visited) print('expand', b.expand) print('act', act2((b.path))) # # third q input c = [int(x) for x in input().split()] # # r = Cube([[[ 'y', 'b', 'y', 'b', 'g', 'y', 'g', 'y', 'w', 'g', 'w', 'g', 'b', 'w', 'b', 'w', 'r', 'r', 'r', 'r', 'o', 'o', 'o', 'o' ]]], [[[ 'b', 'b', 'b', 'b', 'y', 'y', 'y', 'y', 'g', 'g', 'g', 'g', 'w', 'w', 'w', 'w', 'r', 'r', 'r', 'r', 'o', 'o', 'o', 'o' ]]]) b = BFS(r) b.bfsSearch() print('bfs ') print('path :', b.path) print('max memory', b.maxMemory) print('visited', b.visited) print('expand', b.expand) print('act', act3((b.path))) #
def extend_keys(g, l): gs = [g] for _ in range(l - 1): gs.append(Cube(gs[-1]).move("U x'")) return gs
def _solve(g, x, c1, c2): m = Cube(c2).multiply(Cube(c1).pow(ORDER - x)) return long_to_bytes(cube2long(m))
class AuthManager: def __init__(self, db): self.db = db self.base_cube = Cube().move("F U R U'") def login(self): print("Your name:") login = input("> ") if not self.db.check_user(login): print('There are no user with your login! Register first!') return pub_cube, priv_key, info = self.db.get_user_data(login) print(f'Give me your cube:') try: priv_cube = Cube.from_str(input("> ").strip()) except Exception: print("This is not a cube!") return if priv_cube.multiply(self.base_cube.pow(priv_key)) != pub_cube: print('Wrong cube!') return print(f'Hello!\nYour public cube is {pub_cube.as_str()}') return login, pub_cube, info def register(self): print("Your name:") login = input("> ") if self.db.check_user(login): print('There exist user with your login!') return print('Give me your cube:') try: priv_cube = Cube.from_str(input("> ").strip()) except Exception: print("This is not a cube!") return priv_key = randint(1, ORDER) pub_cube = priv_cube.multiply(self.base_cube.pow(priv_key)) print('Anything about yourself:') info = input("> ") self.db.set_user_data(login, pub_cube, priv_key, info) print(f'Hello!\nYour public cube is {pub_cube.as_str()}') return login, pub_cube, info def process(self): print(MENU) try: _input = int(input("> ").strip()) except Exception: return if _input == 0: print("Bye bye!") return elif _input == 1: user = self.register() elif _input == 2: user = self.login() else: return if user is None: return return user
def __init__(self, db): self.db = db self.base_cube = Cube().move("F U R U'")