from Crypto.Util.number import * import random import os
classidek():
def__init__(self, secret : bytes):
self.secret = secret self.p = None
self.poly = None
defset_p(self, p : int):
if isPrime(p): self.p = p
defgen_poly(self, deg : int):
s = bytes_to_long(self.secret) l = s.bit_length() self.poly = [random.randint(0, 2**l) for _ inrange(deg + 1)] index = random.randint(deg//4 + 1, 3*deg//4 - 1) self.poly[index] = s
defget_share(self, point : int):
ifnot self.p ornot self.poly: returnNone
returnsum([coef * pow(point, i, self.p) for i, coef inenumerate(self.poly)]) % self.p
defget_shares(self, points : list[int]):
return [self.get_share(point) for point in points]
defbanner():
print("==============================================") print("=== Welcome to idek Secret Sharing Service ===") print("==============================================") print("")
defmenu():
print("") print("[1] Oracle") print("[2] Verify") print("[3] Exit") op = int(input(">>> ")) return op
if __name__ == '__main__':
S = idek(os.urandom(80)) deg = 16 seen = []
banner()
for _ inrange(17):
op = menu() if op == 1: p = int(input("What's Your Favorite Prime : ")) assert p.bit_length() == 64and isPrime(p) and p notin seen seen += [p] S.set_p(p) S.gen_poly(deg) L = list(map(int, input("> ").split(","))) assertlen(L) <= 3*deg//4 print(f"Here are your shares : {S.get_shares(L)}") elif op == 2: if S.secret.hex() == input("Guess the secret : "): withopen("flag.txt", "rb") as f: print(f.read()) else: print("Try harder.") elif op == 3: print("Bye!") break else: print("Unknown option.")