コード例 #1
0
def test_decryption():
    g = _default_crypto['generator']
    p = _default_crypto['modulus']
    q = _default_crypto['order']

    texts = [0, 1, 2, 3, 4]
    keys = [13, 14, 15, 16]
    publics = [pow(g, x, p) for x in keys]
    pk = 1
    for y in publics:
        pk = (pk * y) % p
    cts = []
    rands = []
    for t in texts:
        ct = encrypt(t, p, g, q, pk)
        cts.append((ct[0], ct[1]))
        rands.append(ct[2])

    all_factors = []
    for x in keys:
        factors = compute_decryption_factors(p, g, q, x, cts)
        all_factors.append(factors)

    master_factors = combine_decryption_factors(p, all_factors)
    pts = []
    for (alpha, beta), factor in zip(cts, master_factors):
        pts.append(decrypt_with_decryptor(p, g, q, beta, factor))
    assert pts == texts

    cfm = {
        'modulus': p,
        'generator': g,
        'order': q,
        'public': pk,
        'original_ciphers': cts,
        'mixed_ciphers': cts
    }

    mix1 = mix_ciphers(cfm)
    mix = mix_ciphers(mix1)
    cts = mix['mixed_ciphers']
    all_factors = []
    for x in keys:
        factors = compute_decryption_factors(p, g, q, x, cts)
        all_factors.append(factors)

    master_factors = combine_decryption_factors(p, all_factors)
    pts = []
    for (alpha, beta), factor in zip(cts, master_factors):
        pts.append(decrypt_with_decryptor(p, g, q, beta, factor))
    assert sorted(pts) == sorted(texts)
コード例 #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
コード例 #4
0
def main_verify(sigfile, randomness=None, plaintext=None):
    with open(sigfile) as f:
        signature = f.read()

    vote_info = verify_vote_signature(signature)
    signed_vote, crypto, trustees, candidates, comments = vote_info

    eb = signed_vote['encrypted_ballot']
    public = eb['public']
    modulus, generator, order = crypto
    beta = eb['beta']
    print 'VERIFIED: Authentic Signature'
    if randomness is None:
        return

    nr_candidates = len(candidates)
    encoded = decrypt_with_randomness(modulus, generator, order,
                                      public, beta, randomness)
    if plaintext is not None:
        if plaintext != encoded:
            print 'FAILED: Plaintext Mismatch'

        ct = encrypt(plaintext, modulus, generator, order, randomness)
        _alpha, _beta, _randomness = ct
        alpha = eb['alpha']
        if (alpha, beta) != (_alpha, _beta):
            print 'FAILED: Invalid Encryption'

    max_encoded = gamma_encoding_max(nr_candidates) + 1
    print "plaintext:       %d" % encoded
    print "max plaintext:   %d" % max_encoded
    if encoded > max_encoded:
        print "FAILED: Invalid Vote. Cannot decode."
        return

    selection = gamma_decode(encoded, nr_candidates)
    choices = to_absolute_answers(selection, nr_candidates)
    print ""
    for i, o in enumerate(choices):
        print "%d: [%d] %s" % (i, o, candidates[o])
    print ""
コード例 #5
0
ファイル: client.py プロジェクト: gchatzip/zeus
def main_verify(sigfile, randomness=None, plaintext=None):
    with open(sigfile) as f:
        signature = f.read()

    vote_info = verify_vote_signature(signature)
    signed_vote, crypto, trustees, candidates, comments = vote_info

    eb = signed_vote['encrypted_ballot']
    public = eb['public']
    modulus, generator, order = crypto
    beta = eb['beta']
    print 'VERIFIED: Authentic Signature'
    if randomness is None:
        return

    nr_candidates = len(candidates)
    encoded = decrypt_with_randomness(modulus, generator, order,
                                      public, beta, randomness)
    if plaintext is not None:
        if plaintext != encoded:
            print 'FAILED: Plaintext Mismatch'

        ct = encrypt(plaintext, modulus, generator, order, randomness)
        _alpha, _beta, _randomness = ct
        alpha = eb['alpha']
        if (alpha, beta) != (_alpha, _beta):
            print 'FAILED: Invalid Encryption'

    max_encoded = gamma_encoding_max(nr_candidates) + 1
    print "plaintext:       %d" % encoded
    print "max plaintext:   %d" % max_encoded
    if encoded > max_encoded:
        print "FAILED: Invalid Vote. Cannot decode."
        return

    selection = gamma_decode(encoded, nr_candidates)
    choices = to_absolute_answers(selection, nr_candidates)
    print ""
    for i, o in enumerate(choices):
        print "%d: [%d] %s" % (i, o, candidates[o])
    print ""