def _mk_corrupt_encoding(self, fake_nr_candidates, audit_code): """ Meant to be used for testing audit-vote submission: make sure that the provided audit_code is not None """ cryptosys = self.get_cryptosys() def get_decrypted_value(adapted_vote): _, _, _, encrypted_ballot, _, _, voter_secret, _, _, _, _ = \ extract_vote(adapted_vote) ciphertext, _ = cryptosys.extract_ciphertext_proof( encrypted_ballot) election_key = self.get_election_key() decrypted = cryptosys.decrypt_with_randomness( ciphertext, election_key, voter_secret).value return decrypted audit_vote = self._mk_random_vote(selection=None, audit_code=audit_code, publish=True) copy = deepcopy(audit_vote) adapted_vote = adapt_vote(cryptosys, copy) while get_decrypted_value(adapted_vote) <= \ gamma_encoding_max(fake_nr_candidates): audit_vote = self._mk_random_vote(selection=None, audit_code=audit_code, publish=True) copy = deepcopy(audit_vote) adapted_vote = adapt_vote(cryptosys, copy) return audit_vote
def test_vote_adaptment_success(self): _, cryptosys, validator, voter, messages = self.get_context() vote = voter.mk_genuine_vote() adapted = adapt_vote(cryptosys, deepcopy(vote)) try: assert adapted == validator.adapt_vote(vote) messages.append('[+] Vote successfully adapted') except AssertionError: err = 'Vote wrongly adapted' self.__fail(err)
def test_genuine_vote_validation_success(self): _, cryptosys, validator, voter, messages = self.get_context() vote = voter.mk_genuine_vote() vote = adapt_vote(cryptosys, vote) try: validator.validate_genuine_vote(vote) messages.append('[+] Vote: Successfully validated') except InvalidVoteError: err = 'Valid vote erroneously invalidated' self.__fail(err)
def test_signature_verification_success(self): _, cryptosys, signer, verifier, _, messages = self.get_context() for label, mk_vote in self.get_vote_makings(): vote = mk_vote() vote = adapt_vote(cryptosys, vote) with self.subTest(vote=vote): vote_signature = signer.sign_vote(vote, ['comment 1', 'comment 2,']) try: verifier.verify_vote_signature(vote_signature) messages.append(f'[+] {label} Siganture successfully verified') except InvalidVoteSignature: err = f'{label} valid signature erroneously not verified' self.__fail(err)
def test_essential_signature_verification_failure(self): _, cryptosys, signer, verifier, _, messages = self.get_context() for label, mk_vote in self.get_vote_makings(): vote = mk_vote() vote = adapt_vote(cryptosys, vote) err = 'Tampered message failed to be detected' vote_signature = mk_vote_signature(cryptosys, signer, vote, destroy_integrity=True) with self.subTest(vote_signature=vote_signature): try: verifier.verify_vote_signature(vote_signature) except InvalidVoteSignature: messages.append(f'[+] {label} Tampered message succesfully detected') else: self.__fail(f'{label} {err}')
def test_signature_verification_failure_upon_invalid_encryption(self): _, cryptosys, signer, verifier, _, messages = self.get_context() for label, mk_vote in self.get_vote_makings(): vote = mk_vote() vote = adapt_vote(cryptosys, vote) with self.subTest(vote=vote): vote_signature = mk_vote_signature(cryptosys, signer, vote, corrupt_proof=True) try: verifier.verify_vote_signature(vote_signature) except InvalidVoteSignature: messages.append(f'[+] {label} Invalid encryption successfully detected') else: err = f'{label} Invalid encryption failed to be detected' self.__fail(err)
def test_audit_vote_validation_success(self): _, cryptosys, validator, voter, messages = self.get_context() audit_vote = voter.mk_audit_vote() audit_vote = adapt_vote(cryptosys, audit_vote) missing, failed = validator.validate_audit_votes(audit_votes=[ audit_vote, ]) try: assert not missing and not failed messages.append('[+] Audit-vote: Successfully validated') except AssertionError: err = 'Valid audit-vote erroneously invalidated' self.__fail(err)
def test_audit_vote_validation_failures(self): election, cryptosys, validator, voter, messages = self.get_context() err = 'Invalid audit-vote failed to be detected' for kwargs, msg in ( ({ 'missing': True }, 'missing secret'), ({ 'corrupt_proof': True }, 'invalid encryption'), ({ 'corrupt_alpha': True }, 'invalid secret'), ({ 'corrupt_encoding': True }, 'max-gamma exceeded'), ): if msg == 'max-gamma exceeded': # ~ Dramatically reduce the number of candidates so that # ~ decrypting the ballot with the voter's secret # ~ exceeds max-gamma encoding of their number save_candidates = election.get_candidates() election.set_candidates(save_candidates[:1]) fake_nr_candidates = len(election.get_candidates()) kwargs.update({'fake_nr_candidates': fake_nr_candidates}) audit_vote = voter.mk_audit_vote(**kwargs) with self.subTest(audit_vote=audit_vote): audit_vote = adapt_vote(cryptosys, audit_vote) missing, failed = validator.validate_audit_votes( (audit_vote, )) try: if msg == 'missing secret': assert missing == [ audit_vote, ] and not failed else: assert not missing and failed == [ audit_vote, ] messages.append( f'[+] Audit-vote: Invalid detected ({msg})') except AssertionError: self.__fail(f'{err} ({msg})') if msg == 'max-gamma exceeded': election.set_candidates( save_candidates) # Restore for subsequent tests
def test_signature_verification_failures_upon_election_mismatch(self): _, cryptosys, signer, verifier, _, messages = self.get_context() for label, mk_vote in self.get_vote_makings(): vote = mk_vote() vote = adapt_vote(cryptosys, vote) err = 'Election mismatch failed to be detected' for kwargs, msg in ( ({'corrupt_crypto': True}, 'crypto discord'), ({'corrupt_public': True}, 'key discord'), ({'corrupt_trustees': True}, 'trustees discord'), ({'corrupt_candidates': True}, 'candidates discord'), ): vote_signature = mk_vote_signature(cryptosys, signer, vote, **kwargs) with self.subTest(vote_signature=vote_signature): try: verifier.verify_vote_signature(vote_signature) except InvalidVoteSignature: messages.append(f'[+] {label} Election mismatch successfully detected') else: self.__fail(f'{label} {err} {msg}')
def test_genuine_vote_validation_failures(self): _, cryptosys, validator, voter, messages = self.get_context() err = 'Invalid vote failed to be detected' for kwargs, msg in ( ({ 'corrupt_proof': True }, 'invalid encryption'), ({ 'corrupt_fingerprint': True }, 'fingerprint mismatch'), ): vote = voter.mk_genuine_vote(**kwargs) with self.subTest(vote=vote): vote = adapt_vote(cryptosys, vote) try: validator.validate_genuine_vote(vote) except InvalidVoteError: messages.append(f'[+] Vote: Invalid detected ({msg})') else: self.__fail(f'{err} ({msg})')