def hash_sbb(self, public): """ Return a (tweaked) hash of the sbb contents. """ board = self.board # next is commented out since we have no no-public posting # in the current code. if False: if public: board = [item for item in board if item[0][0] != "("] board_str = sv.dumps(board) hash_tweak = "hash_sbb" return sv.secure_hash(board_str, hash_tweak, iterate=True)
def hash_sbb(sbb, stop_before_header): """ Return a (tweaked) hash of the sbb contents, including all items up to (but not including) the item with header equal to stop_before_header. (Copied from sv_prover.py) """ sbb_trunc = [] for item in sbb: if item[0] == stop_before_header: break else: sbb_trunc.append(item) sbb_trunc_str = sv.dumps(sbb_trunc) hash_tweak = "hash_sbb" # use iterated hashing to slow down adversarial attack return sv.secure_hash(sbb_trunc_str, hash_tweak, iterate=True)
def hash_sbb(sbb, stop_before_header): """ Return a (tweaked) hash of the sbb contents, including all items up to (but not including) the item with header equal to stop_before_header. (Copied from sv_prover.py) """ sbb_trunc = [] for item in sbb: if item[0] == stop_before_header: break else: sbb_trunc.append(item) sbb_trunc_str = sv.dumps(sbb_trunc) #sv.dump([item for item in sbb_trunc if item[0]=='proof:output_commitments'], 'proof_output_commitments_VERIFIERVERSION.txt') hash_tweak = "hash_sbb" # use iterated hashing to slow down adversarial attack - change in sv_sbb.py as well return sv.secure_hash(sbb_trunc_str, hash_tweak, iterate=False)
def check_receipts(sbb_dict, db): """ Check that receipts are consistent with cast vote commitments. """ receipt_ballot_ids = set(db['receipts'].keys()) cast_vote_dict = sbb_dict['casting:votes']['cast_vote_dict'] for race_id in cast_vote_dict: for p in cast_vote_dict[race_id]: d = dict() for i in cast_vote_dict[race_id][p]: ballot_id = cast_vote_dict[race_id][p][i]['ballot_id'] cu = cast_vote_dict[race_id][p][i]['cu'] cv = cast_vote_dict[race_id][p][i]['cv'] d[i] = {'cu': cu, 'cv': cv} cv_receipt_data = [ballot_id, d] cv_receipt_data_str = sv.dumps(cv_receipt_data) cv_hash = sv.bytes2base64(sv.secure_hash(cv_receipt_data_str)) assert cv_hash == db['receipts'][ballot_id]['hash'] receipt_ballot_ids.remove(ballot_id) assert len(receipt_ballot_ids) == 0 print("check_receipts: passed.")
def hash_sbb(self, public): """ Return a (tweaked) hash of the sbb contents. """ board = self.board # next is commented out since we have no no-public posting # in the current code. if False: if public: board = [item for item in board if item[0][0] != "("] board_str = sv.dumps(board) sbb_trunc = [] for item in board: sbb_trunc.append(item) board_str2 = sv.dumps(sbb_trunc) #sv.dump([item for item in sbb_trunc if item[0]=='proof:output_commitments'], 'proof_output_commitments_SBBVERSION.txt') assert board_str2 == board_str, "board_str2: " + board_str2 + " board_str: " + board_str hash_tweak = "hash_sbb" return sv.secure_hash( board_str, hash_tweak, iterate=False ) # make iterate=True to slow down adversarial attacks change in sv_verifier.py as well
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}