Ejemplo n.º 1
0
 def test_check_vote_neg(self):
     vote = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 1]])
     ss_vote_partitions = util.partition_and_secret_share_vote(vote, local_servers)
     res = 0
     for ss_partition in ss_vote_partitions:
         res = (res + server_util.create_sum_of_row(ss_partition)) % util.get_prime()
     self.assertFalse(server_util.check_rows_and_columns(res))
Ejemplo n.º 2
0
 def test_check_vote(self):
     vote = client_util.create_vote([4, 2, 1, 3])
     ss_vote_partitions = util.partition_and_secret_share_vote(vote, local_servers)
     res = 0
     for ss_partition in ss_vote_partitions:
         res =(res + server_util.create_sum_of_row(ss_partition)) % util.get_prime()
     self.assertTrue(server_util.check_rows_and_columns(res))
Ejemplo n.º 3
0
def vote():
    global communication_number
    verified, data = util.unpack_request(request, str(server_nr))
    if not verified:
        return make_response("Could not verify", 400)
    try:
        # Unpack data
        votes_ = data['votes']
        if type(votes_) == str:
            votes_ = [votes_]
        ids_ = data['ids']
        if type(ids_) in [str, int]:
            ids_ = [ids_]
        server_name = data['server']
        client = data['client']

        # Convert votes_ to list of np.array
        votes = []
        for num, v_ in enumerate(votes_):
            if num in cheating_nums and cheat_id == 1:
                votes.append(cheat_util.col_row_cheat(util.string_to_vote(v_)))
            else:
                votes.append(util.string_to_vote(v_))

        # Attach each share to its proper id
        id_vote_tuple = list(zip(ids_, votes))

        # Insert votes, row and column in db and broadcast
        for (id, vote) in id_vote_tuple:
            row_sum = server_util.create_sum_of_row(vote)
            col_sum = server_util.create_sum_of_row(vote.T)

            db.insert_vote(vote, id, 1, client, server_name, my_name)

            db.insert_row(row_sum, id, 'row', client, server_name, my_name)
            db.insert_col(col_sum, id, 'column', client, server_name, my_name)

            server_util.broadcast_rows_and_cols(row_sum, col_sum, id, servers,
                                                my_name, client)

        # Insert zero check and broadcast
        my_id = servers.index(my_name)

        votes_dict = dict()
        for id, v in id_vote_tuple:
            if (len(cheating_nums) > 0 and id == cheating_nums[0] and cheat_id
                    == util.Protocol.sum_difference_zero_one.value):
                v = np.zeros(v.shape)
            votes_dict[id] = v

        local_parts, communcations = server_util.matrix_zero_one_check(
            my_id, servers, votes_dict, my_name, client)
        communication_number += communcations
        for local_part in local_parts:
            for x, ss in enumerate(local_part[3]):
                db.insert_zero_partition(matrix=ss,
                                         x=x,
                                         i=local_part[1],
                                         j=local_part[2],
                                         client_name=local_part[5],
                                         server=my_name,
                                         db_name=my_name)
    except TypeError as e:
        print("vote", e)
        return Response(status=400)
    return Response(status=200)
Ejemplo n.º 4
0
 def test_create_sum_of_col_neg(self):
     vote = client_util.create_vote([1, 1, 3, 4])
     summed_rows = server_util.create_sum_of_row(vote.T)
     self.assertNotEqual(1, summed_rows[0])
     self.assertEqual(2, summed_rows[0])
Ejemplo n.º 5
0
 def test_create_sum_of_row(self):
     vote = client_util.create_vote([2, 1, 3, 4])
     summed_rows = server_util.create_sum_of_row(vote)
     for sum in summed_rows:
         self.assertEqual(1, sum)