示例#1
0
 def encrypt_ballot_and_cast(self, selection, size, the_url, p_uuid):
     self.c.get(self.locations['logout'])
     e = Election.objects.get(uuid=self.e_uuid)
     rel_selection = to_relative_answers(selection, size)
     encoded = gamma_encode(rel_selection, size, size)
     plaintext = algs.EGPlaintext(encoded, e.public_key)
     randomness = algs.Utils.random_mpz_lt(e.public_key.q)
     cipher = e.public_key.encrypt_with_r(plaintext, randomness, True)
     modulus, generator, order = e.zeus.do_get_cryptosystem()
     enc_proof = prove_encryption(modulus, generator, order, cipher.alpha,
                                  cipher.beta, randomness)
     r = self.c.get(the_url, follow=True)
     self.assertEqual(r.status_code, 200)
     cast_data = {}
     ##############
     ballot = {
         'election_hash': 'foobar',
         'election_uuid': e.uuid,
         'answers': [{
             'encryption_proof': enc_proof,
             'choices': [{
                 'alpha': cipher.alpha,
                 'beta': cipher.beta}]
             }]
         }
     ##############
     enc_vote = datatypes.LDObject.fromDict(
         ballot, type_hint='phoebus/EncryptedVote').wrapped_obj
     cast_data['encrypted_vote'] = enc_vote.toJSON()
     r = self.c.post('/elections/%s/polls/%s/cast'
                     % (self.e_uuid, p_uuid), cast_data)
     voter = self.get_voter_from_url(the_url)
     p = Poll.objects.get(uuid=p_uuid)
     self.verbose('+ Voter %s voting at poll %s' % (voter.name, p.name))
     return r
示例#2
0
def generate_vote(p, g, q, y, choices, nr_candidates=None):
    if isinstance(choices, int):
        nr_candidates = choices
        selection = get_random_selection(nr_candidates, full=0)
    else:
        nr_candidates = nr_candidates or (max(choices) if choices else 0) + 1
        selection = to_relative_answers(choices, nr_candidates)
    encoded = gamma_encode(selection, nr_candidates)
    ct = encrypt(encoded, p, g, q, y)
    alpha, beta, rand = ct
    proof = prove_encryption(p, g, q, alpha, beta, rand)
    commitment, challenge, response = proof
    answer = {}
    answer['encryption_proof'] = (commitment, challenge, response)
    answer['choices'] = [{'alpha': alpha, 'beta': beta}]
    encrypted_vote = {}
    encrypted_vote['answers'] = [answer]
    encrypted_vote['election_uuid'] = ''
    encrypted_vote['election_hash'] = ''
    return encrypted_vote, encoded, rand
示例#3
0
文件: client.py 项目: gchatzip/zeus
def generate_vote(p, g, q, y, choices):
    if isinstance(choices, int):
        nr_candidates = choices
        selection = get_random_selection(nr_candidates, full=0)
    else:
        nr_candidates = (max(choices) if choices else 0) + 1
        selection = to_relative_answers(choices, nr_candidates)
    encoded = gamma_encode(selection, nr_candidates)
    ct = encrypt(encoded, p, g, q, y)
    alpha, beta, rand = ct
    proof = prove_encryption(p, g, q, alpha, beta, rand)
    commitment, challenge, response = proof
    answer = {}
    answer['encryption_proof'] = (commitment, challenge, response)
    answer['choices'] = [{'alpha': alpha, 'beta': beta}]
    encrypted_vote = {}
    encrypted_vote['answers'] = [answer]
    encrypted_vote['election_uuid'] = ''
    encrypted_vote['election_hash'] = ''
    return encrypted_vote, encoded, rand