Skip to content

Commit

Permalink
fixed python verification and added support for min and max, added so…
Browse files Browse the repository at this point in the history
…me debugging
  • Loading branch information
benadida committed Jan 5, 2009
1 parent a42182f commit 7640eab
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 16 deletions.
10 changes: 7 additions & 3 deletions client/heliosclient.py
Expand Up @@ -10,19 +10,23 @@
from crypto import algs, electionalgs

class HeliosClient(object):
def __init__(self, auth_info, host, port):
def __init__(self, auth_info, host, port, prefix=""):
"""
auth_info is consumer_key, ....
"""
self.consumer = oauth.OAuthConsumer(auth_info['consumer_key'],auth_info['consumer_secret'])
self.token = oauth.OAuthToken(auth_info['consumer_key'],auth_info['consumer_secret'])
self.client = oauthclient.MachineOAuthClient(self.consumer, self.token, host, port)
self.prefix = prefix

def get(self, url, parameters = None):
return self.client.access_resource("GET", url, parameters= parameters)
print "getting " + self.prefix + url
return self.client.access_resource("GET", self.prefix + url, parameters= parameters)

def post(self, url, parameters = None):
return self.client.access_resource("POST", url, parameters= parameters)
print "posting " + self.prefix + url
result = self.client.access_resource("POST", self.prefix + url, parameters= parameters)
return result

def params(self):
params_json = self.get("/elections/params")
Expand Down
53 changes: 45 additions & 8 deletions crypto/electionalgs.py
Expand Up @@ -20,10 +20,27 @@ def __init__(self, choices=None, individual_proofs=None, overall_proof=None, ran
self.overall_proof = overall_proof
self.randomness = randomness

def verify(self, pk):
possible_plaintexts = [algs.EGPlaintext(1, pk), algs.EGPlaintext(pk.g, pk)]
homomorphic_sum = 0
@classmethod
def generate_plaintexts(cls, pk, min=0, max=1):
plaintexts = []
running_product = 1

# run the product up to the min
for i in range(max+1):
# if we're in the range, add it to the array
if i >= min:
plaintexts.append(algs.EGPlaintext(running_product, pk))

# next value in running product
running_product = (running_product * pk.g) % pk.p

return plaintexts


def verify(self, pk, min=0, max=1):
possible_plaintexts = self.generate_plaintexts(pk)
homomorphic_sum = 0

for choice_num in range(len(self.choices)):
choice = self.choices[choice_num]
choice.pk = pk
Expand All @@ -36,8 +53,11 @@ def verify(self, pk):
# compute homomorphic sum
homomorphic_sum = choice * homomorphic_sum

# determine possible plaintexts for the sum
sum_possible_plaintexts = self.generate_plaintexts(pk, min=min, max=max)

# verify the sum
return homomorphic_sum.verify_disjunctive_encryption_proof(possible_plaintexts, self.overall_proof, algs.EG_disjunctive_challenge_generator)
return homomorphic_sum.verify_disjunctive_encryption_proof(sum_possible_plaintexts, self.overall_proof, algs.EG_disjunctive_challenge_generator)

def toJSONDict(self):
return {
Expand Down Expand Up @@ -78,7 +98,7 @@ def fromElectionAndAnswer(cls, election, question_num, answer_indexes):
randomness = [None for a in range(len(answers))]

# possible plaintexts [0, 1]
plaintexts = [algs.EGPlaintext(1, pk), algs.EGPlaintext(pk.g, pk)]
plaintexts = cls.generate_plaintexts(pk)

# keep track of number of options selected.
num_selected_answers = 0;
Expand Down Expand Up @@ -110,7 +130,18 @@ def fromElectionAndAnswer(cls, election, question_num, answer_indexes):

# prove that the sum is 0 or 1 (can be "blank vote" for this answer)
# num_selected_answers is 0 or 1, which is the index into the plaintext that is actually encoded
overall_proof = homomorphic_sum.generate_disjunctive_encryption_proof(plaintexts, num_selected_answers, randomness_sum, algs.EG_disjunctive_challenge_generator);
min_answers = 0
if question.has_key('min'):
min_answers = question['min']
max_answers = question['max']

if num_selected_answers < min_answers:
raise Exception("Need to select at least %s answer(s)" % min_answers)

sum_plaintexts = cls.generate_plaintexts(pk, min=min_answers, max=max_answers)

# need to subtract the min from the offset
overall_proof = homomorphic_sum.generate_disjunctive_encryption_proof(sum_plaintexts, num_selected_answers - min_answers, randomness_sum, algs.EG_disjunctive_challenge_generator);

return cls(choices, individual_proofs, overall_proof, randomness)

Expand Down Expand Up @@ -138,8 +169,14 @@ def verify(self, election):
return False

# check proofs on all of answers
for ea in self.encrypted_answers:
if not ea.verify(election.pk):
for question_num in range(len(election.questions)):
ea = self.encrypted_answers[question_num]
question = election.questions[question_num]
min_answers = 0
if question.has_key('min'):
min_answers = question['min']

if not ea.verify(election.pk, min=min_answers, max=question['max']):
return False

return True
Expand Down
4 changes: 2 additions & 2 deletions helios/templates/index.html
Expand Up @@ -17,12 +17,12 @@
</p>

<p>
Check out our <a href="/faq">Frequently Asked Questions</a>.
Check out our <a href="{% url helios.views.faq %}">Frequently Asked Questions</a>.
</p>

<br clear="right" /><br />

<div style="text-align:center; font-size: 20pt;">
<a href="/elections/new">Create an Open-Audit Election</a><br /><br />
<a href="{% url helios.views.election_new %}">Create an Open-Audit Election</a><br /><br />
</div>
{% endblock %}
9 changes: 6 additions & 3 deletions tests/heliosclient.py
Expand Up @@ -16,7 +16,10 @@
# modify variables here
helios = heliosclient.HeliosClient({'consumer_key': 'test', 'consumer_secret': '123'},
host = '174.129.241.146',
port = 80)
# host = "localhost",
port = 80,
prefix = "/helios"
)

# get the El Gamal Parameters
params = helios.params()
Expand All @@ -33,7 +36,7 @@
helios.election_set_reg(election_id, open_reg= True)

# set questions
questions = [{"answers": ["ice-cream", "cake"], "max": 1, "question": "ice-cream or cake?", "short_name": "dessert"}]
questions = [{"answers": ["ice-cream", "cake"], "min": 1, "max": 1, "question": "ice-cream or cake?", "short_name": "dessert"}]
helios.election_questions_save(election_id, questions)

# freeze it
Expand All @@ -48,7 +51,7 @@
# create three ballots
ballot_1 = electionalgs.EncryptedVote.fromElectionAndAnswers(election, [[1]])
ballot_2 = electionalgs.EncryptedVote.fromElectionAndAnswers(election, [[1]])
ballot_3 = electionalgs.EncryptedVote.fromElectionAndAnswers(election, [[0]])
ballot_3 = electionalgs.EncryptedVote.fromElectionAndAnswers(election, [[]])

print "created 3 ballots"

Expand Down

0 comments on commit 7640eab

Please sign in to comment.