Exemple #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)
Exemple #2
0
def do_decrypt(savefile, outfile, keyfile, nr_parallel):
    poll_index = 0
    curr_file = savefile + ".0"

    with open(keyfile) as f:
        key = load(f)

    secret = int(key['x'])
    pk = key['public_key']
    modulus = int(pk['p'])
    generator = int(pk['g'])
    order = int(pk['q'])
    del key

    while (os.path.isfile(curr_file)):

        curr_outfile = outfile + ".%d" % poll_index
        if exists(curr_outfile):
            m = "file '%s' already exists, will not overwrite" % (
                curr_outfile, )
            sys.stderr.write(m + "\n")
            poll_index += 1
            curr_file = savefile + ".%d" % poll_index
            continue

        with open(curr_file) as f:
            tally = load(f)['tally']

        ciphers = [(int(ct['alpha']), int(ct['beta']))
                   for ct in tally['tally'][0]]
        factors = compute_decryption_factors(modulus,
                                             generator,
                                             order,
                                             secret,
                                             ciphers,
                                             nr_parallel=nr_parallel)
        decryption_factors = []
        factor_append = decryption_factors.append
        decryption_proofs = []
        proof_append = decryption_proofs.append

        for factor, proof in factors:
            factor_append(factor)
            f = {}
            f['commitment'] = {'A': proof[0], 'B': proof[1]}
            f['challenge'] = proof[2]
            f['response'] = proof[3]
            proof_append(f)

        factors_and_proofs = {
            'decryption_factors': [decryption_factors],
            'decryption_proofs': [decryption_proofs]
        }

        with open(curr_outfile, "w") as f:
            dump(factors_and_proofs, f)

        poll_index += 1
        curr_file = savefile + ".%d" % poll_index
    return factors
Exemple #3
0
def do_decrypt(savefile, outfile, keyfile, nr_parallel):
    poll_index = 0
    curr_file = savefile + ".0"

    with open(keyfile) as f:
        key = load(f)

    secret = int(key['x'])
    pk = key['public_key']
    modulus = int(pk['p'])
    generator = int(pk['g'])
    order = int(pk['q'])
    public = int(pk['y'])
    del key


    while(os.path.isfile(curr_file)):

        curr_outfile = outfile + ".%d" % poll_index
        if exists(curr_outfile):
            m = "file '%s' already exists, will not overwrite" % (curr_outfile,)
            sys.stderr.write(m + "\n")
            poll_index += 1
            curr_file = savefile + ".%d" % poll_index
            continue

        with open(curr_file) as f:
            tally = load(f)['tally']

        ciphers = [(int(ct['alpha']), int(ct['beta']))
                for ct in tally['tally'][0]]
        factors = compute_decryption_factors(modulus, generator, order,
                                            secret, ciphers,
                                            nr_parallel=nr_parallel)
        decryption_factors = []
        factor_append = decryption_factors.append
        decryption_proofs = []
        proof_append = decryption_proofs.append

        for factor, proof in factors:
            factor_append(factor)
            f = {}
            f['commitment'] = {'A': proof[0], 'B': proof[1]}
            f['challenge'] = proof[2]
            f['response'] = proof[3]
            proof_append(f)

        factors_and_proofs = {
            'decryption_factors': [decryption_factors],
            'decryption_proofs': [decryption_proofs]
        }

        with open(curr_outfile, "w") as f:
            dump(factors_and_proofs, f)

        poll_index += 1
        curr_file = savefile + ".%d" % poll_index
    return factors
Exemple #4
0
def do_decrypt(savefile, outfile, keyfile, nr_parallel):
    if exists(outfile):
        m = "file '%s' already exists, will not overwrite" % (outfile,)
        raise ValueError(m)

    with open(keyfile) as f:
        key = load(f)

    secret = int(key['x'])
    pk = key['public_key']
    modulus = int(pk['p'])
    generator = int(pk['g'])
    order = int(pk['q'])
    public = int(pk['y'])
    del key

    with open(savefile) as f:
        tally = load(f)

    ciphers = [(int(ct['alpha']), int(ct['beta']))
               for ct in tally['tally'][0]]
    factors = compute_decryption_factors(modulus, generator, order,
                                         secret, ciphers,
                                         nr_parallel=nr_parallel)
    decryption_factors = []
    factor_append = decryption_factors.append
    decryption_proofs = []
    proof_append = decryption_proofs.append

    for factor, proof in factors:
        factor_append(factor)
        f = {}
        f['commitment'] = {'A': proof[0], 'B': proof[1]}
        f['challenge'] = proof[2]
        f['response'] = proof[3]
        proof_append(f)

    factors_and_proofs = {
            'decryption_factors': [decryption_factors],
            'decryption_proofs': [decryption_proofs] }

    with open(outfile, "w") as f:
        dump(factors_and_proofs, f)

    return factors