def read_cut_verifier_challenges(sbb_dict, sbb, db): """ Read verifier challenges from proof:cutandchoose_verifier_challenges; save into db. """ chs = sbb_dict['proof:cutandchoose_verifier_challenges']['challenges'] assert isdict(chs, ['cut']) assert isdict(chs['cut'], ['icl', 'opl']) icl = chs['cut']['icl'] assert isinstance(icl, list) assert len(icl) == db['n_reps'] // 2 assert set(icl).issubset(db['k_list']) opl = chs['cut']['opl'] assert isinstance(opl, list) assert len(opl) == db['n_reps'] // 2 assert set(opl).issubset(db['k_list']) assert set(icl).isdisjoint(set(opl)) db['icl'] = icl db['opl'] = opl # now check that icl and opl are consistent with sbb_hash # see make_cut_verifier_challenges in sv_prover.py rand_name = 'cut_verifier_challenges' sbb_hash = sbb_dict['proof:cutandchoose_verifier_challenges']['sbb_hash'] stop_before_header = 'proof:cutandchoose_verifier_challenges' sbb_hash2 = sv.bytes2hex(hash_sbb(sbb, stop_before_header)) assert sbb_hash2 == sbb_hash, "sbb_hash2: " + str( sbb_hash2) + "sbb_hash: " + str(sbb_hash) sv.init_randomness_source(rand_name, sv.hex2bytes(sbb_hash)) pi = sv.random_permutation(db['n_reps'], rand_name) m = db['n_reps'] // 2 pi = [pi[i] for i in range(2 * m)] icl2 = [db['k_list'][i] for i in sorted(pi[:m])] opl2 = [db['k_list'][i] for i in sorted(pi[m:])] assert icl2 == icl assert opl2 == opl print('read_cut_verifier_challenges: successful.')
def read_leftright_verifier_challenges(sbb_dict, sbb, db): """ Read verifier challenges from proof:leftright_verifier_challenges; save into db. """ chs = sbb_dict['proof:leftright_verifier_challenges']['challenges'] assert isdict(chs, ['leftright']) leftright = chs['leftright'] assert isdict(leftright, db['race_ids']) for race_id in leftright.keys(): lr_dict = leftright[race_id] assert set(lr_dict.keys()) == set(db['p_list']) for p in db['p_list']: lr = lr_dict[p] assert lr == 'left' or lr == 'right' db['leftright'] = leftright # now check that icl, opl, and leftright are consistent with sbb_hash # see make_verifier_challenges in sv_prover.py rand_name = 'leftright_verifier_challenges' sbb_hash = sbb_dict['proof:leftright_verifier_challenges']['sbb_hash'] stop_before_header = 'proof:leftright_verifier_challenges' sbb_hash2 = sv.bytes2hex(hash_sbb(sbb, stop_before_header)) assert sbb_hash2 == sbb_hash, "sbb_hash2: " + str( sbb_hash2) + "sbb_hash: " + str(sbb_hash) sv.init_randomness_source(rand_name, sv.hex2bytes(sbb_hash)) leftright2 = make_left_right_challenges(rand_name, db) assert leftright2 == leftright print('read_leftright_verifier_challenges: successful.')
def handle_hash_sbb(self, request_data): """ServerX -> SBB[Compute hash of SBB] Input: public_flag Action: Compute hash of SBB Output: sbb_hash""" public = request_data[SC.MSG_BODY] sbb_hash = self.server.sbb.hash_sbb(public) response = self.get_incomplete_response(request_data[SC.MSG_TITLE]) response[SC.MSG_BODY] = sv.bytes2hex(sbb_hash) print ("response[SC.MSG_BODY]/hash: ", response[SC.MSG_BODY]) return response
def make_cut_verifier_challenges(election, sbb_hash): """ Return a dict containing "verifier challenges" for this proof. This is based on randomness (hash of sbb, fiat-shamir style), but could also incorporate additional random input (e.g. dice rolls). """ election.sbb_hash = sbb_hash rand_name = "cut_verifier_challenges" sv.init_randomness_source(rand_name, sbb_hash) challenges = dict() make_cut_and_choose_challenges(election, rand_name, challenges) sv.update_nested_dict(election.server.challenges, challenges) proof = ("proof:cutandchoose_verifier_challenges", { "sbb_hash": sv.bytes2hex(sbb_hash), "challenges": challenges }) return proof
def read_verifier_challenges(sbb_dict, sbb, db): """ Read verifier challenges from proof:verifier_challenges; save into db. """ chs = sbb_dict['proof:verifier_challenges']['challenges'] assert isdict(chs, ['cut', 'leftright']) assert isdict(chs['cut'], ['icl', 'opl']) icl = chs['cut']['icl'] assert isinstance(icl, list) assert len(icl) == db['n_reps'] // 2 assert set(icl).issubset(db['k_list']) opl = chs['cut']['opl'] assert isinstance(opl, list) assert len(opl) == db['n_reps'] // 2 assert set(opl).issubset(db['k_list']) assert set(icl).isdisjoint(set(opl)) db['icl'] = icl db['opl'] = opl leftright = chs['leftright'] assert isdict(leftright, db['race_ids']) for race_id in leftright.keys(): lr_dict = leftright[race_id] assert set(lr_dict.keys()) == set(db['p_list']) for p in db['p_list']: lr = lr_dict[p] assert lr == 'left' or lr == 'right' db['leftright'] = leftright # now check that icl, opl, and leftright are consistent with sbb_hash # see make_verifier_challenges in sv_prover.py rand_name = 'verifier_challenges' sbb_hash = sbb_dict['proof:verifier_challenges']['sbb_hash'] stop_before_header = 'proof:verifier_challenges' sbb_hash2 = sv.bytes2hex(hash_sbb(sbb, stop_before_header)) assert sbb_hash2 == sbb_hash sv.init_randomness_source(rand_name, sv.hex2bytes(sbb_hash)) pi = sv.random_permutation(db['n_reps'], rand_name) m = db['n_reps'] // 2 pi = [pi[i] for i in range(2 * m)] icl2 = [db['k_list'][i] for i in sorted(pi[:m])] opl2 = [db['k_list'][i] for i in sorted(pi[m:])] assert icl2 == icl assert opl2 == opl leftright2 = make_left_right_challenges(rand_name, db) assert leftright2 == leftright print('read_verifier_challenges: successful.')
def read_verifier_challenges(sbb_dict, sbb, db): """ Read verifier challenges from proof:verifier_challenges; save into db. """ chs = sbb_dict['proof:verifier_challenges']['challenges'] assert isdict(chs, ['cut', 'leftright']) assert isdict(chs['cut'], ['icl', 'opl']) icl = chs['cut']['icl'] assert isinstance(icl, list) assert len(icl) == db['n_reps'] // 2 assert set(icl).issubset(db['k_list']) opl = chs['cut']['opl'] assert isinstance(opl, list) assert len(opl) == db['n_reps'] // 2 assert set(opl).issubset(db['k_list']) assert set(icl).isdisjoint(set(opl)) db['icl'] = icl db['opl'] = opl leftright = chs['leftright'] assert isdict(leftright, db['race_ids']) for race_id in leftright.keys(): lr_dict = leftright[race_id] assert set(lr_dict.keys()) == set(db['p_list']) for p in db['p_list']: lr = lr_dict[p] assert lr == 'left' or lr == 'right' db['leftright'] = leftright # now check that icl, opl, and leftright are consistent with sbb_hash # see make_verifier_challenges in sv_prover.py rand_name = 'verifier_challenges' sbb_hash = sbb_dict['proof:verifier_challenges']['sbb_hash'] stop_before_header = 'proof:verifier_challenges' sbb_hash2 = sv.bytes2hex(hash_sbb(sbb, stop_before_header)) assert sbb_hash2 == sbb_hash sv.init_randomness_source(rand_name, sv.hex2bytes(sbb_hash)) pi = sv.random_permutation(db['n_reps'], rand_name) m = db['n_reps'] // 2 pi = [pi[i] for i in range(2*m)] icl2 = [db['k_list'][i] for i in sorted(pi[:m])] opl2 = [db['k_list'][i] for i in sorted(pi[m:])] assert icl2 == icl assert opl2 == opl leftright2 = make_left_right_challenges(rand_name, db) assert leftright2 == leftright print('read_verifier_challenges: successful.')
def make_verifier_challenges(election): """ Return a dict containing "verifier challenges" for this proof. This is based on randomness (hash of sbb, fiat-shamir style), but could also incorporate additional random input (e.g. dice rolls). """ sbb_hash = election.sbb.hash_sbb(public=True) election.sbb_hash = sbb_hash rand_name = "verifier_challenges" sv.init_randomness_source(rand_name, sbb_hash) challenges = dict() make_cut_and_choose_challenges(election, rand_name, challenges) make_left_right_challenges(election, rand_name, challenges) election.sbb.post("proof:verifier_challenges", {"sbb_hash": sv.bytes2hex(sbb_hash), "challenges": challenges}, time_stamp=False) return challenges
def make_verifier_challenges(election): """ Return a dict containing "verifier challenges" for this proof. This is based on randomness (hash of sbb, fiat-shamir style), but could also incorporate additional random input (e.g. dice rolls). """ sbb_hash = election.sbb.hash_sbb(public=True) election.sbb_hash = sbb_hash rand_name = "verifier_challenges" sv.init_randomness_source(rand_name, sbb_hash) challenges = dict() make_cut_and_choose_challenges(election, rand_name, challenges) make_left_right_challenges(election, rand_name, challenges) election.sbb.post("proof:verifier_challenges", { "sbb_hash": sv.bytes2hex(sbb_hash), "challenges": challenges }, time_stamp=False) return challenges
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}