Esempio n. 1
0
def check_input_consistency_output_openings(sbb_dict, db):
    """ Check that output openings are correct,
        for those halves that are opened.
    """
    oooc = sbb_dict['proof:input_consistency:output_openings']\
                   ['opened_commitments']
    occ = sbb_dict['proof:output_commitments']['commitments']
    for race_id in db['races']:
        ooocr = oooc[race_id]
        occr = occ[race_id]
        for k in db['icl']:
            ooocrk = ooocr[k]
            occrk = occr[k]
            for p in db['p_list']:
                ooocrkp = ooocrk[p]
                occrkp = occrk[p]
                for i in db['row_list']:
                    ooocrkpi = ooocrkp[i]
                    occrkpi = occrkp[i]
                    if 'u' in ooocrkpi:
                        assert occrkpi['cu'] == sv.com(ooocrkpi['u'],
                                                       ooocrkpi['ru'])
                    else:
                        assert occrkpi['cv'] == sv.com(ooocrkpi['v'],
                                                       ooocrkpi['rv'])
    print('check_input_consistency_output_openings: passed.')
def check_input_consistency_output_openings(sbb_dict, db):
    """ Check that output openings are correct,
        for those halves that are opened.
    """
    oooc = sbb_dict['proof:input_consistency:output_openings']\
                   ['opened_commitments']
    occ = sbb_dict['proof:output_commitments']['commitments']
    for race_id in db['races']:
        ooocr = oooc[race_id]
        occr = occ[race_id]
        for k in db['icl']:
            ooocrk = ooocr[k]
            occrk = occr[k]
            for p in db['p_list']:
                ooocrkp = ooocrk[p]
                occrkp = occrk[p]
                for i in db['row_list']:
                    ooocrkpi = ooocrkp[i]
                    occrkpi = occrkp[i]
                    if 'u' in ooocrkpi:
                        assert occrkpi['cu'] == sv.com(ooocrkpi['u'],
                                                       ooocrkpi['ru'])
                    else:
                        assert occrkpi['cv'] == sv.com(ooocrkpi['v'],
                                                       ooocrkpi['rv'])
    print('check_input_consistency_output_openings: passed.')
Esempio n. 3
0
def compute_output_commitments(election):
    """ Make commitments to all output values and save them in sdbp.

    For each race,
    for each of n_reps copies (indexed by k),
    for each row (indexed by i)
    for each of the n vote shares (call them y)
    compute two commitments (cu and cv) to split-value rep (u,v) of y.
    using randomization values ru and rv.
    """
    cols = election.server.cols
    full_output = dict()
    for race in election.races:
        race_modulus = race.race_modulus
        race_id = race.race_id
        full_output[race_id] = dict()
        for k in election.k_list:
            full_output[race_id][k] = dict()
            for py in election.p_list:
                full_output[race_id][k][py] = dict()
                for i in election.server.row_list:
                    rand_name = \
                        election.server.sdb[race_id][i][cols-1]['rand_name']
                    sdbp = election.server.sdb[race_id][i][cols - 1][k]
                    y = sdbp['y'][py]
                    (u, v) = sv.get_sv_pair(y, rand_name, race_modulus)
                    ru = sv.bytes2base64(sv.get_random_from_source(rand_name))
                    rv = sv.bytes2base64(sv.get_random_from_source(rand_name))
                    cu = sv.com(u, ru)
                    cv = sv.com(v, rv)
                    sdbp['u'][py] = u
                    sdbp['v'][py] = v
                    sdbp['ru'][py] = ru
                    sdbp['rv'][py] = rv
                    sdbp['cu'][py] = cu
                    sdbp['cv'][py] = cv
                    ballot = {
                        'y': y,
                        'u': u,
                        'v': v,
                        'ru': ru,
                        'rv': rv,
                        'cu': cu,
                        'cv': cv
                    }
                    full_output[race_id][k][py][i] = ballot
    election.full_output = full_output
Esempio n. 4
0
def check_input_consistency_input_openings(sbb_dict, db):
    """ Check that input openings are correct,
        for those halves that are opened.
    """
    oc = sbb_dict['proof:input_consistency:input_openings']\
                 ['opened_commitments']
    cv = sbb_dict['casting:votes']['cast_vote_dict']
    for race_id in db['races']:
        ocr = oc[race_id]
        cvr = cv[race_id]
        for p in db['p_list']:
            ocrp = ocr[p]
            cvrp = cvr[p]
            for i in db['row_list']:
                ocrpi = ocrp[i]
                cvrpi = cvrp[i]
                if 'u' in ocrpi:
                    assert cvrpi['cu'] == sv.com(ocrpi['u'], ocrpi['ru'])
                else:
                    assert cvrpi['cv'] == sv.com(ocrpi['v'], ocrpi['rv'])
    print('check_input_consistency_input_openings: passed.')
Esempio n. 5
0
def compute_output_commitments(election):
    """ Make commitments to all output values and save them in sdbp.

    For each race,
    for each of n_reps copies (indexed by k),
    for each row (indexed by i)
    for each of the n vote shares (call them y)
    compute two commitments (cu and cv) to split-value rep (u,v) of y.
    using randomization values ru and rv.
    """
    cols = election.server.cols
    full_output = dict()
    for race in election.races:
        race_modulus = race.race_modulus
        race_id = race.race_id
        full_output[race_id] = dict()
        for k in election.k_list:
            full_output[race_id][k] = dict()
            for py in election.p_list:
                full_output[race_id][k][py] = dict()
                for i in election.server.row_list:
                    rand_name = \
                        election.server.sdb[race_id][i][cols-1]['rand_name']
                    sdbp = election.server.sdb[race_id][i][cols-1][k]
                    y = sdbp['y'][py]
                    (u, v) = sv.get_sv_pair(y, rand_name, race_modulus)
                    ru = sv.bytes2base64(sv.get_random_from_source(rand_name))
                    rv = sv.bytes2base64(sv.get_random_from_source(rand_name))
                    cu = sv.com(u, ru)
                    cv = sv.com(v, rv)
                    sdbp['u'][py] = u
                    sdbp['v'][py] = v
                    sdbp['ru'][py] = ru
                    sdbp['rv'][py] = rv
                    sdbp['cu'][py] = cu
                    sdbp['cv'][py] = cv
                    ballot = {'y': y, 'u': u, 'v': v,
                              'ru': ru, 'rv': rv, 'cu': cu, 'cv': cv}
                    full_output[race_id][k][py][i] = ballot
    election.full_output = full_output
def check_opened_output_commitments(sbb_dict, db):
    """ Check that opened output commitments open correctly.
    """
    coms = \
        sbb_dict['proof:outcome_check']\
                ['opened_output_commitments']
    assert isdict(coms, db['race_ids'])
    for race_id in db['race_ids']:
        assert isdict(coms[race_id],
                      db['opl']), "sv.dumps(coms[race_id]): " + sv.dumps(
                          coms[race_id]) + " db['opl']: " + sv.dumps(db['opl'])
        for k in db['opl']:
            assert isdict(coms[race_id][k], db['p_list'])
            for p in db['p_list']:
                assert isdict(coms[race_id][k][p], db['row_list'])
                for i in db['row_list']:
                    assert isdict(coms[race_id][k][p][i],
                                  ['ru', 'rv', 'u', 'v', 'y'])
                    ru = coms[race_id][k][p][i]['ru']
                    assert isinstance(ru, str)
                    rv = coms[race_id][k][p][i]['rv']
                    assert isinstance(rv, str)
                    u = coms[race_id][k][p][i]['u']
                    assert isinstance(u, int) and \
                        0 <= u < db['races'][race_id]['race_modulus']
                    v = coms[race_id][k][p][i]['v']
                    assert isinstance(v, int) and \
                        0 <= v < db['races'][race_id]['race_modulus']
                    y = coms[race_id][k][p][i]['y']
                    assert isinstance(y, int) and \
                        0 <= y < db['races'][race_id]['race_modulus']
                    assert y == (u + v) % db['races'][race_id]['race_modulus']
                    cu = sbb_dict['proof:output_commitments']\
                         ['commitments'][race_id][k][p][i]['cu']
                    cv = sbb_dict['proof:output_commitments']\
                         ['commitments'][race_id][k][p][i]['cv']
                    assert cu == sv.com(u, ru)
                    assert cv == sv.com(v, rv)
    print('check_opened_output_commitments: passed.')
def check_input_consistency_input_openings(sbb_dict, db):
    """ Check that input openings are correct,
        for those halves that are opened.
    """
    oc = sbb_dict['proof:input_consistency:input_openings']\
                 ['opened_commitments']
    cv = sbb_dict['casting:votes']['cast_vote_dict']
    for race_id in db['races']:
        ocr = oc[race_id]
        cvr = cv[race_id]
        for p in db['p_list']:
            ocrp = ocr[p]
            cvrp = cvr[p]
            for i in db['row_list']:
                ocrpi = ocrp[i]
                cvrpi = cvrp[i]
                if 'u' in ocrpi:
                    assert cvrpi['cu'] == sv.com(ocrpi['u'],
                                                 ocrpi['ru'])
                else:
                    assert cvrpi['cv'] == sv.com(ocrpi['v'],
                                                 ocrpi['rv'])
    print('check_input_consistency_input_openings: passed.')
def check_opened_output_commitments(sbb_dict, db):
    """ Check that opened output commitments open correctly.
    """
    coms = \
        sbb_dict['proof:outcome_check']\
                ['opened_output_commitments']
    assert isdict(coms, db['race_ids'])
    for race_id in db['race_ids']:
        assert isdict(coms[race_id], db['opl'])
        for k in db['opl']:
            assert isdict(coms[race_id][k], db['p_list'])
            for p in db['p_list']:
                assert isdict(coms[race_id][k][p], db['row_list'])
                for i in db['row_list']:
                    assert isdict(coms[race_id][k][p][i],
                                  ['ru', 'rv', 'u', 'v', 'y'])
                    ru = coms[race_id][k][p][i]['ru']
                    assert isinstance(ru, str)
                    rv = coms[race_id][k][p][i]['rv']
                    assert isinstance(rv, str)
                    u = coms[race_id][k][p][i]['u']
                    assert isinstance(u, int) and \
                        0 <= u < db['races'][race_id]['race_modulus']
                    v = coms[race_id][k][p][i]['v']
                    assert isinstance(v, int) and \
                        0 <= v < db['races'][race_id]['race_modulus']
                    y = coms[race_id][k][p][i]['y']
                    assert isinstance(y, int) and \
                        0 <= y < db['races'][race_id]['race_modulus']
                    assert y == (u+v) % db['races'][race_id]['race_modulus']
                    cu = sbb_dict['proof:output_commitments']\
                         ['commitments'][race_id][k][p][i]['cu']
                    cv = sbb_dict['proof:output_commitments']\
                         ['commitments'][race_id][k][p][i]['cv']
                    assert cu == sv.com(u, ru)
                    assert cv == sv.com(v, rv)
    print('check_opened_output_commitments: passed.')
def compute_output_commitments(election, row_index, col_index):
    """ Make commitments to all output values and save them in sdbp.

    For each race,
    for each of n_reps copies (indexed by k),
    for each row (indexed by i)
    for each of the n vote shares (call them y)
    compute two commitments (cu and cv) to split-value rep (u,v) of y.
    using randomization values ru and rv.
    """
    cols = election.server.cols
    full_output = dict()
    for race in election.races:
        race_modulus = race.race_modulus
        race_id = race.race_id
        full_output[race_id] = dict()
        for k in election.k_list:
            full_output[race_id][k] = dict()
            for py in election.p_list:
                full_output[race_id][k][py] = dict()
                if col_index == cols - 1:  # TODO move this logic to ServerMix handler
                    i = row_index
                    rand_name = \
                        election.server.sdb[race_id][i][cols-1]['rand_name']
                    sv.init_randomness_source(
                        rand_name)  # TODO optional remove later
                    sdbp = election.server.sdb[race_id][i][cols - 1][k]
                    y = sdbp['y'][py]
                    (u, v) = sv.get_sv_pair(y, rand_name, race_modulus)
                    ru = sv.bytes2base64(sv.get_random_from_source(rand_name))
                    rv = sv.bytes2base64(sv.get_random_from_source(rand_name))
                    cu = sv.com(u, ru)
                    cv = sv.com(v, rv)
                    sdbp['u'][py] = u
                    sdbp['v'][py] = v
                    sdbp['ru'][py] = ru
                    sdbp['rv'][py] = rv
                    sdbp['cu'][py] = cu
                    sdbp['cv'][py] = cv
                    ballot = {
                        'y': y,
                        'u': u,
                        'v': v,
                        'ru': ru,
                        'rv': rv,
                        'cu': cu,
                        'cv': cv
                    }
                    full_output[race_id][k][py][i] = ballot
    election.full_output = full_output
    coms = dict()
    # same as full_output, but only giving non-secret values (i.e. cu, cv)
    for race in election.races:
        race_id = race.race_id
        coms[race_id] = dict()
        for k in election.k_list:
            coms[race_id][k] = dict()
            for py in election.p_list:
                coms[race_id][k][py] = dict()
                '''for i in election.server.row_list: # TODO remove this for loop -> shared memory
                    coms[race_id][k][py][i] = \
                        {'cu': full_output[race_id][k][py][i]['cu'],
                         'cv': full_output[race_id][k][py][i]['cv']}'''
                coms[race_id][k][py] = dict()
                if col_index == cols - 1:  # TODO move this logic to ServerMix handler
                    i = row_index
                    coms[race_id][k][py][i] = \
                        {'cu': full_output[race_id][k][py][i]['cu'],
                         'cv': full_output[race_id][k][py][i]['cv']}
    election.output_commitments = coms
    return ("proof:output_commitments", {"commitments": coms})
Esempio n. 10
0
    def cast_vote(self, race):
        """ Cast random vote for this voter for this race in simulated election.

        Of course, in a real election, choices come from voter via tablet.
        """

        election = self.election
        cvs = election.cast_votes
        race_id = race.race_id
        race_modulus = race.race_modulus
        rand_name = self.rand_name
        px = self.px

        # cast random vote (for this simulation, it's random)
        choice_str = race.random_choice()  # returns a string
        choice_int = race.choice_str2int(choice_str)  # convert to integer

        # ballot_id is random hex string of desired length
        ballot_id_len = election.ballot_id_len
        ballot_id = sv.bytes2hex(sv.get_random_from_source(rand_name))
        ballot_id = ballot_id[:ballot_id_len]
        assert len(ballot_id) == election.ballot_id_len

        # secret-share choice
        n = election.server.rows
        t = election.server.threshold
        share_list = sv.share(choice_int, n, t, rand_name, race_modulus)

        # double-check that shares reconstruct to desired choice
        assert choice_int == sv.lagrange(share_list, n, t, race_modulus)
        # double-check that shares are have indices 1, 2, ..., n
        assert all([share_list[i][0] == i + 1 for i in range(n)])
        # then strip off indices, since they are equal to row number + 1
        share_list = [share[1] for share in share_list]

        # save ballots on election data structure
        for row, x in enumerate(share_list):
            (u, v) = sv.get_sv_pair(x, rand_name, race_modulus)
            ru = sv.bytes2base64(sv.get_random_from_source(rand_name))
            rv = sv.bytes2base64(sv.get_random_from_source(rand_name))
            cu = sv.com(u, ru)
            cv = sv.com(v, rv)
            i = election.server.row_list[row]
            vote = {
                "ballot_id": ballot_id,
                "x": x,
                "u": u,
                "v": v,
                "ru": ru,
                "rv": rv,
                "cu": cu,
                "cv": cv
            }
            cvs[race_id][px][i] = vote

        # compute voter receipt as hash of her ballot_id and commitments
        # note that voter gets a receipt for each race she votes in
        receipt_data = [ballot_id]
        d = dict()
        for i in election.server.row_list:
            cu = cvs[race_id][px][i]['cu']
            cv = cvs[race_id][px][i]['cv']
            d[i] = {'cu': cu, 'cv': cv}
        receipt_data.append(d)
        receipt_data_str = sv.dumps(receipt_data)
        receipt_hash = sv.bytes2base64(sv.secure_hash(receipt_data_str))
        self.receipts[ballot_id] = {'race_id': race_id, 'hash': receipt_hash}