def test_tiny_circuit(): t = 2 n = 5 c_path = "circuits/tiny_circuit.txt" triples = gen_triples(t, n, 2) mq = Queue() queues = [Queue() for _ in range(n)] x = '01' y = '10' result = '11' x_shares = Shamir(t, n).share_bitstring_secret(x) y_shares = Shamir(t, n).share_bitstring_secret(y) processes = [] for i in range(n): p = Process(target=run_circuit_process, args=(t, n, c_path, i + 1, queues, mq, x_shares[i] + y_shares[i], triples[i])) processes.append(p) for p in processes: p.start() vals = [] while len(vals) < n: if not mq.empty(): vals.append(mq.get()) reconstructed = Shamir(t, n).reconstruct_bitstring_secret(vals) print("result:", reconstructed) assert reconstructed == result, "result incorrect" print("TEST PASSED") for p in processes: p.join() for q in queues: q.close() q.join_thread() mq.close() mq.join_thread()
def share_secret(self): shaTSS = Shamir(self.t, self.n) self.p, alpha = shaTSS.GC(_sage_const_128) shares = shaTSS.DS(self.p, alpha, self.members[_sage_const_0].k) for i in range(_sage_const_1, self.n + _sage_const_1): self.members[i].shares = shares[i - _sage_const_1] print('sended shaTSS')
def gen_threshold_sign(self, h, m, s, shares): shaTSS = Shamir(self.t, self.n) k = shaTSS.SC(self.p, shares).list()[_sage_const_0] kk = bin(int(str(k)))[_sage_const_2:] R = m.parent() f_len = int(kk[:_sage_const_8], _sage_const_2) fs_len = int(kk[_sage_const_8:_sage_const_16], _sage_const_2) R = m.parent() f = R([ -_sage_const_1 * int(x) for x in kk[_sage_const_16:_sage_const_16 + f_len] ]) fs = R([ -_sage_const_1 * int(x) for x in kk[_sage_const_16 + f_len:_sage_const_16 + f_len + fs_len] ]) x = auxmath.normilize_coeffs(-(_sage_const_1 / self.q) * m * fs) y = auxmath.normilize_coeffs(-(_sage_const_1 / self.q) * m * f) s0 = x * f + y * fs s_res = R(s + s0) b = s_res * auxmath.normilize_coeffs2(s_res * self.h0 - m, self.q) print(b) print(b.norm(_sage_const_2))
def test_reconstruction(): t = 2 n = 2*t+1 shamir = Shamir(t, n) secret = randelement() shares = shamir.share_secret(secret) reconstructed = shamir.reconstruct_secret(shares[:t+1]) assert secret == reconstructed, "reconstructed the wrong value"
def share_secret(self): self.k = _sage_const_2048 self.shaTSS = Shamir(self.t, self.n) self.p, alpha = self.shaTSS.GC(self.k) print ('k ' + str(self.members[_sage_const_0 ].k)) shares = self.shaTSS.DS(self.p, alpha, self.members[_sage_const_0 ].k) for i in range(_sage_const_1 , self.n+_sage_const_1 ): self.members[i].shares = shares[i-_sage_const_1 ] self.members[i].alpha = shares[i-_sage_const_1 ][_sage_const_0 ] print ('secret generated')
def gen_triples(t, n, n_triples): triples = [[] for _ in range(n)] for i in range(n_triples): a = randelement() b = randelement() c = a * b a_shares = Shamir(t, n).share_secret(a) b_shares = Shamir(t, n).share_secret(b) c_shares = Shamir(t, n).share_secret(c) for i in range(n): triples[i].append( TripleShare(a_shares[i], b_shares[i], c_shares[i])) return triples
def test_extract_keys(): k = 7 secret = 113 amount_of_keys = 30 shamir = Shamir(k, secret) keys = [shamir.get_key() for _ in range(amount_of_keys)] for n in range(1, amount_of_keys + 1): key_subset = keys[:n] extracted_secret = Shamir.extract_secret(key_subset) if n < k: # Can get secret... but let's assert anyway assert extracted_secret != secret else: assert extracted_secret == secret
def test_secure_addition(): t = 2 n = 2*t+1 shamir = Shamir(t, n) s1 = randelement() s2 = randelement() answer = s1+s2 shares1 = shamir.share_secret(s1) shares2 = shamir.share_secret(s2) # Each party locally adds their share of secret 1 and 2 final_shares = [shares1[i]+shares2[i] for i in range(n)] # Check reconstructed = shamir.reconstruct_secret(final_shares[0:t+1]) assert answer == reconstructed, "reconstructed the wrong value"
def consumer(mq, t, n, processes): vals = [] while len(vals) < n: if not mq.empty(): vals.append(mq.get()) n_triples = len(vals[0]) for i in [0, 1, n_triples - 2, n_triples - 1]: trips = [vals[j][i] for j in range(n)] a_shares = [t.a for t in trips] b_shares = [t.b for t in trips] c_shares = [t.c for t in trips] a = Shamir(t, n).reconstruct_secret(a_shares) b = Shamir(t, n).reconstruct_secret(b_shares) c = Shamir(t, n).reconstruct_secret(c_shares) assert a * b == c, "triples are not equal"
def test_mpc(t, n, c_path, n_triples, inputs, result, reflect=False): triples = gen_triples(t, n, n_triples) mq = Queue() queues = [Queue() for _ in range(n)] share_inputs = [Shamir(t, n).share_bitstring_secret(i) for i in inputs] processes = [] for i in range(n): inputs = [] for si in share_inputs: inputs.extend(si[i]) p = Process(target=run_circuit_process, args=(t, n, c_path, i+1, queues, mq, inputs, triples[i])) processes.append(p) start = time.time() for p in processes: p.start() t1 = Thread(target=consumer, args=(mq, n, result, t, processes, reflect)) t1.start() for p in processes: if p.is_alive(): p.join() t1.join(n) print(f"time: {round(time.time()-start, 4)} seconds") for q in queues: q.close() q.join_thread() mq.join_thread()
async def eval_circuit(t, n, c, index, queues, main_queue, inputs, triples): shamir = Shamir(t, n) dispatcher = Dispatcher(t, n, index, queues, "--UNIQUE COMPUTATION ID--") outputs = await c.evaluate(inputs, shamir=shamir, dispatcher=dispatcher, triples=triples) main_queue.put(outputs)
def shamir_test(l, m, n): s = Shamir('english') seed = "Shamir's Secret Sharing Scheme!"[:l] # take first l characters shares = s.split(seed, m, n) print('original:', seed) print('shares:') for i in range(len(shares)): print('%2d :' % (i + 1), shares[i]) shares = shares[:m] # take first m shares cmb = s.combine(shares) print ('combined:', cmb) if seed == cmb: print('TEST OK') print() else: print('TEST FAILED !!!') sys.exit(1)
def shamir_test(l, m, n): s = Shamir('english') seed = "Shamir's Secret Sharing Scheme!"[:l] # take first l characters shares = s.split(seed, m, n) print('original:', seed) print('shares:') for i in range(len(shares)): print('%2d :' % (i + 1), shares[i]) shares = shares[:m] # take first m shares cmb = s.combine(shares) print('combined:', cmb) if seed == cmb: print('TEST OK') print() else: print('TEST FAILED !!!') sys.exit(1)
def test_mul32_circuit(): t = 2 n = 5 c_path = "circuits/mul32_circuit.txt" triples = gen_triples(t, n, 8000) mq = Queue() queues = [Queue() for _ in range(n)] x = 101 y = 50000 result = x * y x_bin = bin(x)[2:] while len(x_bin) < 32: x_bin = '0' + x_bin y_bin = bin(y)[2:] while len(y_bin) < 32: y_bin = '0' + y_bin x_shares = Shamir(t, n).share_bitstring_secret(x_bin) y_shares = Shamir(t, n).share_bitstring_secret(y_bin) processes = [] for i in range(n): p = Process(target=run_circuit_process, args=(t, n, c_path, i + 1, queues, mq, x_shares[i] + y_shares[i], triples[i])) processes.append(p) for p in processes: p.start() vals = [] while len(vals) < n: if not mq.empty(): vals.append(mq.get()) reconstructed = Shamir(t, n).reconstruct_bitstring_secret(vals) print("result:", reconstructed) assert eval('0b' + reconstructed) == result, "result incorrect" print("TEST PASSED") # TO DO: These processes aren't properly closing, so the test stalls here... # Probably something to do with some processes being locked/stalled? for p in processes: p.join() for q in queues: q.close() q.join_thread() mq.close() mq.join_thread()
def run_triplegen_process(t, n, index, queues, main_queue, batch_size, n_batches): shamir = Shamir(t, n) dispatcher = Dispatcher(t, n, index, queues, "") tg = TripleGeneration(index, shamir, dispatcher, batch_size=batch_size, n_batches=n_batches) tg.run() main_queue.put(tg.triples)
def consumer(mq, n, result, t, processes, reflect): vals = [] while len(vals) < n: if not mq.empty(): vals.append(mq.get()) reconstructed = Shamir(t, n).reconstruct_bitstring_secret(vals)[::-1] if reflect: reconstructed = reconstructed[ int(len(reconstructed) / 2):] + reconstructed[:int(len(reconstructed) / 2)] assert eval('0b' + reconstructed) == eval('0b' + result), "result incorrect"
def run_circuit_process(t, n, c_path, index, queues, main_queue, inputs, triples): print(f"starting node {index}") shamir = Shamir(t, n) messenger = MockMessenger(t, n, index, queues) c = Circuit(c_path) outputs = c.evaluate(inputs, shamir=shamir, messenger=messenger, triples=triples) main_queue.put(outputs) print(f"closing node {index}")
def _testShamir(self, minimum, shares, prime, secret): if self.debug >= 10: print("testShamir(minimum=%d,shares=%d,prime=%d,secret=%d)" % (minimum, shares, prime, secret)) shamir = Shamir(minimum, prime) shamir.setSecret(secret) shamir.randomizeKeys(shares, self.rng) keys = [shamir.getKey(i) for i in range(1, shares + 1)] for t in range(100): self._testShamirRecover(minimum, shares, prime, secret, keys)
def test_mul2_circuit(): t = 2 n = 5 c_path = "bristol_circuits/mul2.txt" n_triples = 2 for x, y in [(0, 1), (0, 0), (1, 1)]: result = (x * y) result = bin(result)[2:] x_bin = bin(x)[2:] y_bin = bin(y)[2:] inputs = Shamir(t, n).share_bitstring_secret(x_bin + y_bin) test_mpc(t, n, c_path, n_triples, inputs, result)
def test_triple_creation(t, n, n_triples): shamir = Shamir(t, n) ## ## ROUND 1 ## player_ab_lists = [[] for _ in range(n)] # Each party for _ in range(n): # computes random shared values ab_lists = shamir.generate_triples_round_1(n_triples) # sends each other party their particular shares (point to point) for i in range(n): player_ab_lists[i].append(ab_lists[i]) ## ## ROUND 2 ## player_a_shares = [] player_b_shares = [] player_c_share_shares = [[] for _ in range(n)] # Each party for i in range(n): # collects received ab_list messages and computes intermediate values a_shares, b_shares, c_share_shares = shamir.generate_triples_round_2( player_ab_lists[i]) player_a_shares.append(a_shares) player_b_shares.append(b_shares) # sends each party their c_share_shares (point to point) for j in range(n): player_c_share_shares[j].append(c_share_shares[j]) ## ## ROUND 3 ## player_triples = [] # Each party for i in range(n): # collects received c_share_share messages and computes the result TripleShare values. triples = shamir.generate_triples_round_3(player_a_shares[i], player_b_shares[i], player_c_share_shares[i]) player_triples.append(triples) ## Check for i in range(len(player_triples[0])): check_a = shamir.reconstruct_secret([p[i].a for p in player_triples]) check_b = shamir.reconstruct_secret([p[i].b for p in player_triples]) check_c = shamir.reconstruct_secret([p[i].c for p in player_triples]) assert check_a * check_b == check_c, "invalid triple was created" return player_triples
async def operation(self, x, y, uuid): bx = bin(x)[2:] while len(bx) < 31: bx = '0' + bx x_shares = Shamir(self.t, self.n).share_bitstring_secret(bx[::-1]) by = bin(y)[2:] while len(by) < 31: by = '0' + by y_shares = Shamir(self.t, self.n).share_bitstring_secret(by[::-1]) tasks = [] for k, v in self.idx2node.items(): msg = {'uuid': uuid, 'x_inputs': serialize_shares(x_shares[k-1]), 'y_inputs': serialize_shares(y_shares[k-1])} host, port = v tasks.append(talk_to_single_server(msg, host, port)) vals = [] for res in asyncio.as_completed(tasks, timeout=CLIENT_TIMEOUT): msg = await res if msg != None: vals.append(deserialize_shares(msg['result'])) if len(vals) > self.t: break return Shamir(self.t, self.n).reconstruct_bitstring_secret(vals)
def test_unnormalized_example(): t = 1 n = 3 c_path = "bristol_circuits/unnormalized_subregion_100_10.txt" n_triples = 200000 ones = ['1' for _ in range(1200)] inputs = Shamir(t, n).share_bitstring_secret(ones) for i in range(len(inputs)): inputs[i] = [0 for _ in range(64)] + inputs[i] result = bin(300)[2:] while len(result) < 64: result = '0' + result result = result * 10 test_mpc(t, n, c_path, n_triples, inputs, result)
def test_add64_circuit(): t = 2 n = 5 c_path = "bristol_circuits/add64.txt" n_triples = 500 for x, y in [(100, 200), (2**30, 2**10), (2**64 - 1, 2**64 - 1)]: result = bin((x + y) % (2**64))[2:] x_bin = bin(x)[2:] while len(x_bin) < 64: x_bin = '0' + x_bin y_bin = bin(y)[2:] while len(y_bin) < 64: y_bin = '0' + y_bin inputs = Shamir(t, n).share_bitstring_secret(x_bin[::-1] + y_bin[::-1]) test_mpc(t, n, c_path, n_triples, inputs, result)
def test_lessthan32_circuit(): t = 2 n = 5 c_path = "bristol_circuits/lessthan32.txt" n_triples = 10000 for x, y in [(111, 2025), (2025, 111), (2**32 - 1, 2**32 - 1)]: result = bin(x < y)[2:] x_bin = bin(x)[2:] while len(x_bin) < 32: x_bin = '0' + x_bin y_bin = bin(y)[2:] while len(y_bin) < 32: y_bin = '0' + y_bin inputs = Shamir(t, n).share_bitstring_secret(x_bin[::-1] + y_bin[::-1]) test_mpc(t, n, c_path, n_triples, inputs, result)
def test_mul64_circuit(): t = 2 n = 5 c_path = "bristol_circuits/mul64.txt" n_triples = 10000 for x, y in [(111, 2025), (2**64 - 1, 2**64 - 1)]: result = bin((x * y))[2:] x_bin = bin(x)[2:] while len(x_bin) < 64: x_bin = '0' + x_bin y_bin = bin(y)[2:] while len(y_bin) < 64: y_bin = '0' + y_bin inputs = Shamir(t, n).share_bitstring_secret(x_bin[::-1] + y_bin[::-1]) test_mpc(t, n, c_path, n_triples, inputs, result, reflect=True)
def test_sub64_circuit(): t = 2 n = 5 c_path = "bristol_circuits/sub64.txt" n_triples = 500 for x, y in [(1000, 2010), (2025, 111), (2**64 - 2, 2**64 - 2)]: result = (x - y) % (2**64) result = bin(result)[2:] x_bin = bin(x)[2:] while len(x_bin) < 64: x_bin = '0' + x_bin y_bin = bin(y)[2:] while len(y_bin) < 64: y_bin = '0' + y_bin inputs = Shamir(t, n).share_bitstring_secret(x_bin[::-1] + y_bin[::-1]) test_mpc(t, n, c_path, n_triples, inputs, result)
def _testShamirRecover(self, minimum, shares, prime, secret, keys): if self.debug >= 100: print( "testShamirRecover(minimum=%d,shares=%d,prime=%d,secret=%s,keys=%s)" % (minimum, shares, prime, secret, keys)) shamir = Shamir(minimum, prime) choices = list(range(shares)) for k in range(minimum): i = self.rng.next(len(choices)) shamir.setKey(choices[i] + 1, keys[choices[i]]) del choices[i] assert shamir.getSecret() == secret for i in range(shares): assert shamir.getKey(i + 1) == keys[i]
def test_secure_multiplication(): t = 2 n = 2 * t + 1 n_muls = 5 shamir = Shamir(t, n) xs = [randelement() for _ in range(n_muls)] ys = [randelement() for _ in range(n_muls)] answers = [xs[i] * ys[i] for i in range(n_muls)] x_sharings = [shamir.share_secret(xs[i]) for i in range(n_muls)] y_sharings = [shamir.share_secret(ys[i]) for i in range(n_muls)] triples = test_triple_creation(t, n, n_muls) ## ## ROUND 1 ## broadcasts = [] # Each party for i in range(n): # computes intermediate values (er_list) for shares to be multiplied er_list = shamir.mul_gates_round_1([x[i] for x in x_sharings], [y[i] for y in y_sharings], triples[i]) # broadcasts the intermediate values to all other parties broadcasts.append(er_list) ## ## ROUND 2 ## player_result_shares = [] # Each party for i in range(n): # Collects broadcasted messages and uses these intermediate values to compute result shares result_shares = shamir.mul_gates_round_2([x[i] for x in x_sharings], [y[i] for y in y_sharings], broadcasts, [t.c for t in triples[i]]) player_result_shares.append(result_shares) # Check values for i in range(len(answers)): reconstructed = shamir.reconstruct_secret( [p[i] for p in player_result_shares]) assert reconstructed == answers[i], "reconstructed the wrong value"
class KeySplitter: def __init__(self, numshares, threshold): self.splitter = Shamir(numshares, threshold) self.numshares = numshares self.threshold = threshold def split(self, key): xshares = [''] * self.numshares yshares = [''] * self.numshares for char in key: xcords, ycords = self.splitter.split(ord(char)) for idx in range(self.numshares): xshares[idx] += chr(xcords[idx]) yshares[idx] += chr(ycords[idx]) return zip(xshares, yshares) def jsonify(self, shares, threshold, split): data = { 'shares': shares, 'threshold': threshold, 'split': [base64.b64encode(split[0]), base64.b64encode(split[1])] } return json.dumps(data)
class TC_(): N = _sage_const_251 NORM_BOUND = _sage_const_321 q = _sage_const_128 df = _sage_const_73 dg = _sage_const_71 t = _sage_const_2 n = _sage_const_5 T = 'transpose' p = -_sage_const_1 H = None t_new = None members = [] #users_count = [range(1, n+1)] active_users = {} def __init__(self): for i in range(self.n + _sage_const_1): print(i) self.add_member() self.share_secret() def get_free_id(self): for i in range(_sage_const_1, self.n + _sage_const_1): if i not in self.active_users.values(): return i return -_sage_const_1 def add_member(self): m = Member(self.N, self.q, self.df, self.dg, self.T) m.eval_k(self.q) self.members.append(m) def get_new_member_params(self, username): id = self.get_free_id() if id == -_sage_const_1: print('All clients already connected') return pickle.dumps(['server full']) f = self.members[id].f h = self.members[id].h fs = self.members[id].fs h0 = self.members[_sage_const_0].h shares = self.members[id].shares self.active_users.update({username: id}) print(self.active_users) return pickle.dumps([f, fs, h, shares, h0, self.N, self.q]) def delete_user(self, username): del self.active_users[username] def share_secret(self): self.k = _sage_const_1024 self.shaTSS = Shamir(self.t, self.n) self.p, alpha = self.shaTSS.GC(self.k) self.T = self.members[_sage_const_0].T print('T: %d' % self.T) print('k ' + str(self.members[_sage_const_0].k)) shares = self.shaTSS.DS(self.p, alpha, self.members[_sage_const_0].k) for i in range(_sage_const_1, self.n + _sage_const_1): self.members[i].shares = shares[i - _sage_const_1] print('secret generated') def change_t(self, t_new): if t_new > self.n or t_new <= self.t: return b'invalid new t' sigma_c = _sage_const_2**(-self.n) appr = ((self.t + t_new)**(_sage_const_1 / _sage_const_2) * _sage_const_2**((self.t + t_new) / _sage_const_2)) g_cvp = log(appr, _sage_const_2) sigma_F = t_new / self.t / self.k * ( log(sigma_c**(-_sage_const_1 / t_new) * self.n * self.t, _sage_const_2) + g_cvp + _sage_const_1) alpha_noise = (_sage_const_1 - (_sage_const_1 + sigma_F) / (t_new / self.t)) H = floor(self.p**alpha_noise / _sage_const_2) for i in range(_sage_const_1, self.n + _sage_const_1): alpha = self.members[i].shares[_sage_const_0] sigma = self.members[i].shares[_sage_const_1] self.members[i].shares[_sage_const_1] = self.shaTSS.SG( [alpha, sigma], self.p, H) #self.members[i].status = ['4', ] self.t_new = t_new self.H = H print(t_new) return b'ok' def get_info_client(self): print(self.active_users) return pickle.dumps( [self.t, self.n, ', '.join(self.active_users.keys())]) #return pickle.dumps([self.q, self.members[0].h, self.p, self.t, self.n, self.NN]) def get_info_dc(self): #print ([self.t, self.n, self.q, self.N, self.p, self.members[0].h, self.H, self.t_new]) return pickle.dumps([ self.t, self.n, self.N, self.q, self.T, self.p, self.members[_sage_const_0].h, self.H, self.t_new ]) def get_user_status(self, username): id = self.active_users[username] return self.members[id].status def set_user_status(self, username, status): id = self.active_users[username] self.members[id].status = status def get_user_shares(self, username): id = self.active_users[username] return self.members[id].shares def gen_partial_sign(self, h, m, s, username): id = self.active_users[username] user = self.members[id] x = Rx(normilize_coeffs( -_sage_const_1 / self.q * m * user.fs))._polynomial % self.q y = Rx(normilize_coeffs( _sage_const_1 / self.q * m * user.f))._polynomial % self.q si = (Rx(x * user.f)._polynomial % self.q + Rx(y * user.fs)._polynomial % self.q) % self.q m = Rx(si * (user.h - h))._polynomial % self.q s = s + si return user.h, m, s def encode_msg(self, D, r): return H(D + str(r), self.N, self.q)
def __init__(self, numshares, threshold): self.splitter = Shamir(numshares, threshold) self.numshares = numshares self.threshold = threshold