-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoting.py
More file actions
59 lines (51 loc) · 1.92 KB
/
voting.py
File metadata and controls
59 lines (51 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import numpy as np
from Pyfhel import Pyfhel, PyPtxt, PyCtxt # Pyfhel has most of the functions, plaintext and ciphertext classes
from time import sleep
def convert_bytes(bytes):
return int.from_bytes(bytes, 'big')
def simulate_results(total_registered, candidate_n):
if candidate_n == 3:
results = [int(total_registered * 0.55), int(total_registered * 0.3), int(total_registered * 0.08)] ## Preset
else:
random_result = np.random.dirichlet(np.ones(candidate_n),size=1)
turnout = total_registered * 0.85
results = [int(turnout * x) for x in random_result[0]]
voter_n = np.sum(results)
votes = np.zeros((candidate_n, voter_n), dtype=int)
for i in range(len(votes)):
votes[i].flat[np.random.choice(1 * voter_n, results[i], replace = False)] = 1
return votes, voter_n, results
def vote_encryption(ballot, context):
x_gen = []
for votes in ballot:
print('...encrypted voting', end = '', flush = True)
encrypted = []
for v in votes:
encrypted.append(context.encryptInt(v)) ## Encrypt votes into ciphertexts
print('...', end = '', flush = True)
print()
x_gen.append(encrypted)
# print('Ciphertexts: ', x_gen)
return x_gen
def ballot_safety(ballot):
spoiled_ballots = []
print('Ballot Safety Check Start [Injection Attacks]')
sleep(2)
for b in ballot:
for i in range(0, len(b)):
if (b[i] != 0 and b[i] != 1):
print('Spoiled Vote Found!!', b[i])
spoiled_ballots.append(b[i])
b[i] = 0
if spoiled_ballots == []:
print('No spoiled votes found!')
sleep(2)
return spoiled_ballots
def count_votes(ballot):
results = []
for candidate in ballot:
addition = candidate[0]
for i in range(1, len(candidate)):
addition += candidate[i]
results.append(addition)
return results