Skip to content

Commit ef6ce80

Browse files
Modify encrypt_ballot to pass on should_verify_proofs (#460)
* Modify encrypt_ballot to pass on should_verify_proofs * Update test_encrypt.py Fix linting in test * Run black lint Co-authored-by: Keith Fung <[email protected]>
1 parent 21194fe commit ef6ce80

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/electionguard/encrypt.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ def encrypt_contest(
309309
elgamal_public_key,
310310
crypto_extended_base_hash,
311311
contest_nonce,
312+
should_verify_proofs=should_verify_proofs,
312313
)
313314
break
314315

@@ -321,6 +322,7 @@ def encrypt_contest(
321322
elgamal_public_key,
322323
crypto_extended_base_hash,
323324
contest_nonce,
325+
should_verify_proofs=should_verify_proofs,
324326
)
325327

326328
if encrypted_selection is None:
@@ -352,7 +354,7 @@ def encrypt_contest(
352354
crypto_extended_base_hash=crypto_extended_base_hash,
353355
nonce_seed=contest_nonce,
354356
is_placeholder=True,
355-
should_verify_proofs=True,
357+
should_verify_proofs=should_verify_proofs,
356358
)
357359
if encrypted_selection is None:
358360
return None # log will have happened earlier
@@ -451,7 +453,11 @@ def encrypt_ballot(
451453
log_info(f": encryption_seed : {encryption_seed.to_hex()}")
452454

453455
encrypted_contests = encrypt_ballot_contests(
454-
ballot, internal_manifest, context, nonce_seed
456+
ballot,
457+
internal_manifest,
458+
context,
459+
nonce_seed,
460+
should_verify_proofs=should_verify_proofs,
455461
)
456462
if encrypted_contests is None:
457463
return None
@@ -487,6 +493,7 @@ def encrypt_ballot_contests(
487493
description: InternalManifest,
488494
context: CiphertextElectionContext,
489495
nonce_seed: ElementModQ,
496+
should_verify_proofs: bool = True,
490497
) -> Optional[List[CiphertextBallotContest]]:
491498
"""Encrypt contests from a plaintext ballot with a specific style"""
492499
encrypted_contests: List[CiphertextBallotContest] = []
@@ -509,6 +516,7 @@ def encrypt_ballot_contests(
509516
context.elgamal_public_key,
510517
context.crypto_extended_base_hash,
511518
nonce_seed,
519+
should_verify_proofs=should_verify_proofs,
512520
)
513521

514522
if encrypted_contest is None:

tests/property/test_encrypt.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from unittest import skip
2+
from unittest.mock import patch
23
from copy import deepcopy
34
from datetime import timedelta
45
from random import Random
@@ -750,3 +751,35 @@ def test_encrypt_ballot_with_derivative_nonces_regenerates_valid_proofs(
750751
context.crypto_extended_base_hash,
751752
)
752753
)
754+
755+
def test_encrypt_ballot_with_verify_proofs_false_passed_on(self):
756+
"""
757+
This test is for https://github.com/microsoft/electionguard-python/issues/459
758+
"""
759+
with patch("electionguard.encrypt.encrypt_contest") as patched_contest, patch(
760+
"electionguard.encrypt.encrypt_selection"
761+
) as patched_selection:
762+
# Arrange
763+
keypair = elgamal_keypair_from_secret(int_to_q(2))
764+
manifest = election_factory.get_fake_manifest()
765+
internal_manifest, context = election_factory.get_fake_ciphertext_election(
766+
manifest, keypair.public_key
767+
)
768+
subject = election_factory.get_fake_ballot(internal_manifest)
769+
self.assertTrue(
770+
subject.is_valid(internal_manifest.ballot_styles[0].object_id)
771+
)
772+
773+
patched_contest.side_effect = encrypt_contest
774+
patched_selection.side_effect = encrypt_selection
775+
776+
# Act
777+
encrypt_ballot(
778+
subject, internal_manifest, context, SEED, should_verify_proofs=False
779+
)
780+
781+
# Assert
782+
for call in patched_contest.call_args_list:
783+
self.assertFalse(call.kwargs.get("should_verify_proofs"))
784+
for call in patched_selection.call_args_list:
785+
self.assertFalse(call.kwargs.get("should_verify_proofs"))

0 commit comments

Comments
 (0)