def threshold_additive_shares(): '''Divides all elements in the shares list into t-n threshold shares using Feldman VSS into n sub-shares with threshold t''' shares = fileOp.read_list("FadditiveShares") t, n = 3, 5 #Generates a (3,5) threshold scheme FIX: Read from file sub_shares, commitment_list = [], [] vss_p, vss_q, gen = [], [], [] for i in shares: feld = RSAFeldmanVSS.feldmanvss(t, n, i) sub_shares.append(feld[0]) #Generate using VSS commitment_list.append(feld[1]) vss_p.append(feld[2]) vss_q.append(feld[3]) gen.append(feld[4]) fileOp.write_list("FvssP", vss_p) fileOp.write_list("FvssQ", vss_q) fileOp.write_list("FvssGen", gen) fileOp.write_list("FvssSubShares", sub_shares) fileOp.write_list("FvssCommitmentList", commitment_list) return
def generate(private_key): '''Start additive share generation with the specified number of shares''' add_shares_num = 10 #FIX: Read from file share_lis = additive_sharing(private_key, add_shares_num) fileOp.write_list("FadditiveShares", share_lis)
def sign(self): signature = fileOp.read_list_noint("Fsignature")[0] signature = int(signature[1:-1], 16) ciphertext = pow(signature, private_key, n) fileOp.write_list("Fciphertext", [ciphertext])
def set_message(self, fname): inp = fname inp = fileOp.read_binary_file(inp) inp = fdh.fdh(inp, (len(bin(n)) - 2)) fileOp.write_list("Fsignature", [inp])
def set_message(self): print("File to be signed:", end=" ") inp = input() inp = fileOp.read_large_data(inp) inp = fdh.fdh(inp, (len(bin(n)) - 2)) fileOp.write_list("Fsignature", [inp])
def generate_prime_pair(b=20): #1024 bit primes '''b = number of bits used for the prime generation''' p = generatePrime.gen_prime_2(b) q = generatePrime.gen_prime_2(b) n = p * q fileOp.write_list("FprimesPQ", [p, q]) fileOp.write_list("FmodulusRSA", [n])
def first_primes(t): '''Generate the first prime numbers upto t using Eratosthenes Seive''' first_primes_list = [] test_lis = [i for i in range(2,t+1)] for i in test_lis: first_primes_list.append(i) for x in range(2,(t+1)//i+1): if x*i in test_lis: test_lis.remove(x*i) fileOp.write_list("FfirstPrimes",test_lis)
def first_primes(t): '''Generate the first prime numbers upto t using Eratosthenes Seive''' first_primes_list = [] test_lis = [i for i in range(2, t + 1)] for i in test_lis: first_primes_list.append(i) for x in range(2, (t + 1) // i + 1): if x * i in test_lis: test_lis.remove(x * i) try: fileOp.write_list("FfirstPrimes", test_lis) except Exception as e: raise Exception("Couldn't write FfirstPrimes.txt to file.")
def refresh_shares(): '''Refreshes all shares in list old_shares,share field size is f''' additive_shares = fileOp.read_list("FadditiveShares")[0] n = fileOp.read_list("FpublicKey")[0] old_shares = additive_shares l = len(old_shares) new_shares = [0 for _ in range(l)] for i in old_shares: share_div = additive_sharing(l,i,0) new_shares = [(a+b) for a,b in zip(new_shares,share_div)] additive_shares = new_shares fileOp.write_list("FadditiveShares",additive_shares) thresholdShares.threshold_additive_shares()
def gen_keys(): p,q = fileOp.read_list("FprimesPQ") n = fileOp.read_list("FmodulusRSA")[0] toit_n = (p-1)*(q-1) #Euler Toitent Function on RSA Modulus #Public Key Generation public_key = coprime.find_coprime(toit_n) #Find public key which is coprime to Toitent Value fileOp.write_list("FpublicKey",[n,public_key]) #Private key Generation private_key = modInverse.modular_inverse(public_key,toit_n) % toit_n fileOp.write_list("FprivateKey",[n,private_key]) #Generate additive shares for private keys additiveShares.generate(private_key) #Make additive share's backup threshold shares using Feldman VSS thresholdShares.threshold_additive_shares()
def refresh_shares(): '''Refreshes all shares in list old_shares,share field size is f''' additive_shares = fileOp.read_list("FadditiveShares") n = fileOp.read_list("FmodulusRSA")[0] old_shares = additive_shares l = len(old_shares) new_shares = [0 for _ in range(l)] #Refresh previous additive shares for i in old_shares: share_div = additiveShares.additive_sharing(i, l) new_shares = [(a + b) for a, b in zip(new_shares, share_div)] #Update new refreshed shares fileOp.write_list("FadditiveShares", new_shares) #Threshold on new shares print("Running") thresholdShares.threshold_additive_shares() print("Done")