def verify_sums(data, my_name): illegal_votes = set() sorted(data, key=lambda x: x[3]) diff_clients = [] for x in data: if x[3] not in diff_clients: diff_clients.append(x[3]) for client in diff_clients: client_rows = [x for x in data if x[3] == client] verify = verify_consistency(client_rows) if verify[0]: res = 0 used_rows = [] for row in client_rows: row_id = row[1] if row_id not in used_rows: used_rows.append(row_id) res += row[0] sums = res % util.get_prime() for sum in sums: if (sum != 1) & (client not in illegal_votes): illegal_votes.add(client) else: complain_consistency( util.Complaint(my_name, dict(votes=verify[1:]), util.Protocol.check_votes, verify[1][1]), list_remove(util.servers, my_name), util.mediator, my_name.split(":")[-1]) return illegal_votes
def test_message_inconsistency(self): for num, server in enumerate(local_servers): util.post_url(dict(complaint=util.vote_to_string(util.Complaint( server, dict(test="test1"), util.Protocol.check_votes, num+1 % 4 )), server=server, sender=server.split(":")[-1]), mediator + "/messageinconsistency") time.sleep(0.2) print(requests.get(mediator + "/test/printcomplaints"))
def verify_result(): res_count = db.get_results_count(my_name) if res_count == 1: return make_response( util.vote_to_string(db.get_results(db_name=my_name)[0][0]), 200) results = db.get_results(my_name) results_to_verify = [(x[0], 0, x[1]) for x in results] _, v1, v2 = server_util.verify_consistency(results_to_verify) server_util.complain_consistency( util.Complaint(my_name, dict(results=results), util.Protocol.compute_result, -1), server_util.list_remove(util.servers, my_name), util.mediator, my_name) return make_response("Error, multiple results", 400)
def ensure_agreement(): global communication_number illegal_votes = [] illegal = db.get_illegal_votes(my_name) sender_client_dict = defaultdict(list) for sender, client in illegal: sender_client_dict[sender].append(client) illegal_votes.append([x[1] for x in db.get_illegal_votes(my_name)]) to_be_deleted = set() # TODO: Brug verify_consistency til at verificere alting if sender_client_dict[my_name] == []: agreed_illegal_votes = set() sender_client_dict[my_name] = [[]] else: agreed_illegal_votes = set(sender_client_dict[my_name][0]) disagreed_illegal_votes = set() for server in servers: agreed_illegal_votes = agreed_illegal_votes.intersection( sender_client_dict[server][0]) disagreed_illegal_votes = disagreed_illegal_votes.union( sender_client_dict[server][0]) disagreed_illegal_votes = disagreed_illegal_votes.difference( agreed_illegal_votes) to_be_deleted = to_be_deleted.union(agreed_illegal_votes) for client in to_be_deleted: db.remove_vote(client, my_name) if (len(disagreed_illegal_votes) > 0): communication_number += 1 server_util.complain_consistency( util.Complaint(my_name, dict(disagreed=disagreed_illegal_votes), util.Protocol.ensure_vote_agreement, -1), server_util.list_remove(util.servers, my_name), util.mediator, my_name) return Response(status=200)
def zeroone_sum_partition_finalize(): # check for vote validity partition_sums = db.get_zero_partition_sum(my_name) partition_sums_clients = partition_sums.keys() illegal_votes = [] for client in partition_sums_clients: is_breaking = False part_sums = list(partition_sums[client]) res = [[[[0] for x in range(len(servers))] for j in range(len(servers))] for i in range(len(servers))] res2 = [[[0] for j in range(len(servers))] for i in range(len(servers))] for i in range(len(servers)): for j in range(len(servers)): for x in range(len(servers)): val = part_sums[0]['matrix'][i][j][x] for part_sum in part_sums[1:]: part_sum_matrix = part_sum['matrix'] if not np.array_equal( part_sum_matrix[i][j][x], np.zeros(part_sum_matrix[i][j][x].shape)): if not np.array_equal(val, part_sum_matrix[i][j][x]): server_util.complain_consistency( util.Complaint( my_name, dict(i=i, j=j, x=x, client=client, part_sum=part_sum, val=val, val_matrix=part_sums[0]['matrix'], part_sum_matrix=part_sum_matrix), util.Protocol.zero_one_finalize, x), server_util.list_remove( util.servers, [my_name, util.servers[x]]), util.mediator, my_name) is_breaking = False print("zeroone_sum_partition_finalize: ", client) #break server = part_sum['server'] res[i][j][x] = val[0] if is_breaking: break if is_breaking: break res2[i][j] = sum(res[i][j])[0] % util.get_prime() if is_breaking: break if is_breaking: continue sum_res = [sum(ij) for ij in res2] sum_res = np.mod(np.array(sum_res), util.get_prime()) if not np.mod(np.sum(sum_res), util.get_prime()) == 0.0: # Illegal vote. illegal_votes.append(client) if cheating: if client not in illegal_votes: illegal_votes.append(client) else: illegal_votes.remove(client) return illegal_votes
def sumdifferenceshareforzeroone(): # Verify servers have calculated the same difference_dict = db.get_zero_consistency_check(my_name) difference_dict_clients = difference_dict.keys() for client in difference_dict_clients: difference_matrix_list = [[[[] for h in range(len(servers))] for j in range(len(servers))] for i in range(len(servers))] for difference in difference_dict[client]: difference_matrix_list[difference['i']][difference['j']][ difference['x']].append( (difference['diff'], difference['server_a'], difference['server_b'], difference['server'])) # Ensure diff_a = diff_b and sum diff_shares result = [[0 for j in range(len(servers))] for i in range(len(servers))] for i in range(len(servers)): for j in range(len(servers)): res = [] server_difference_dict = defaultdict(list) server_difference_x_dict = defaultdict(list) for x in range(len(servers)): differences = difference_matrix_list[i][j][x] for difference in differences: server_difference_dict[difference[1] + ";" + difference[2]].append( (difference[0], x, difference[3], difference)) server_difference_x_dict[difference[1] + ";" + difference[2] + ";" + str(x)].append( (difference[0], difference[3])) # SERVER PARTITION TESTS server_difference_x_keys = server_difference_x_dict.keys() for key in server_difference_x_keys: first = 0 first_x_tuple = server_difference_x_dict[key][first] first_x_diff = first_x_tuple[0] first_x_server = first_x_tuple[1] while first + 1 < len( server_difference_x_dict[key]) and np.array_equal( first_x_diff, np.zeros(first_x_diff.shape)): first += 1 first_x_tuple = server_difference_x_dict[key][first] first_x_diff = first_x_tuple[0] first_x_server = first_x_tuple[1] diff_x_tuple_set = server_difference_x_dict[key][first:] for diff_x_tuple in diff_x_tuple_set: diff_x = diff_x_tuple[0] if not np.array_equal(diff_x, np.zeros(diff_x.shape)): if not np.array_equal( diff_x, first_x_diff ): # TODO: Hvad skal dette være? Lige nu er det den samme variabel to gange # Disagreement in diff partitions print("sumdifferenceshareforzeroone: ", "Disagreement in difference partitions") server_util.complain_consistency( util.Complaint( my_name, dict(diff2=diff_x_tuple, diff1=first_x_tuple, x=int(key.split(";")[-1]), i=i, j=j, key=key, client=client), util.Protocol. sum_difference_zero_one_partition, key.split(";")[-1]), server_util.list_remove( util.servers, util.servers[int(key.split(";")[-1])]), util.mediator, my_name) # DIFF TESTS server_difference_keys = server_difference_dict.keys() summed_diffs = [] for key in server_difference_keys: summed_diff = 0 used_xs = set() for diff_tuple in server_difference_dict[key]: diff = diff_tuple[0] x = diff_tuple[1] server = diff_tuple[2] if x not in used_xs: used_xs.add(x) summed_diff = summed_diff + diff summed_diffs.append( (summed_diff % util.get_prime(), server, key, server_difference_dict[key])) db.insert_summed_diffs(summed_diffs, client, i, j, my_name) equality = True first = 0 first_element = summed_diffs[first] while first + 1 < len(summed_diffs) and np.array_equal( first_element[0], np.zeros(first_element[0].shape)): first += 1 first_element = summed_diffs[first] for element in summed_diffs[first:]: if not np.array_equal(np.array(element[0]), np.array(first_element[0])): if not np.array_equal(element[0], np.zeros(element[0].shape)): print(my_name) print("el", element[0], element[1]) print("fel", first_element[0], element[1]) equality = False diffs = (element, first_element) server = (element[1], first_element[1]) key = element[1] server_util.complain_consistency( util.Complaint( my_name, dict(diffs=diffs, key=key, i=i, j=j, client=client), util.Protocol.sum_difference_zero_one, x), server_util.list_remove( util.servers, [my_name, util.servers[x]]), util.mediator, my_name) if not equality: # TODO: DO SOMETHING HERE print("sumdifferenceshareforzeroone: ", "Disagreement. Some differences are not equal!") # disagreed_clients.append((client, i, j, difference_matrix_list[i][j][x][1], difference_matrix_list[i][j][x][2])) sum_product_zero_one_check() return Response(status=200)