forked from ian-h-chamberlain/Crypto-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulletinBoard.py
60 lines (52 loc) · 2.03 KB
/
bulletinBoard.py
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
60
import math
import utilities
# BulletinBoard - tally votes and send results to EM
class BulletinBoard:
def __init__(self, em, numCandidates):
# initialize the vote board
self.numCandidates = numCandidates
self.voteBoard = []
self.electionBoard = em
self.public_key = em.public_key
self.A = int(math.log(self.public_key.n))**100
def sendVote(self,vote,signature):
# now verify signature of vote
if not utilities.verify(vote, signature, self.electionBoard.rsa_pub):
print ("Vote is not signed by the authority! discounting")
return
''' verify each vote is only for one candidate
verification strategy:
Randomly permute the vote array
Send to EM
EM checks each value is 0 or 1 and the sum is 1
Also ensure there are a correct number of candidates
'''
permute = utilities.permute(vote)
if not self.electionBoard.checkValidity(permute):
print("Invalid vote detected!")
return # cannot count this vote
if len(vote) != self.numCandidates:
print("Invalid vote detected!")
return # cannot count this vote
self.temp_vote = vote
def createChallenge(self,u,i):
self.u = u
self.c = self.temp_vote[i]
self.e = utilities.makeChallenge(self.A)
return self.e
def sendAnswer(self,v,w):
return utilities.checkChallenge(self.public_key,self.u,self.e,self.c,v,w)
def acceptVote(self):
self.voteBoard.append(self.temp_vote)
def tallyResults(self):
# initialize the total
totals = [0 for i in range(self.numCandidates)]
# now actually tally the votes
for vote in self.voteBoard:
for i in range(len(vote)):
if totals[i]==0:
totals[i] = vote[i]
else:
totals[i] = totals[i]*vote[i]
# report results to EM
self.electionBoard.reportResults(totals)