Skip to content

Commit bbb4bc8

Browse files
Add verify_ballot method and edit accompanying test (#457)
* add verify_ballot method and edit accompanying test * change InternalManifest with Manifest in parameter * rename election to manifest in test_verify_ballot Co-authored-by: Keith Fung <[email protected]>
1 parent ecf2fa2 commit bbb4bc8

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/electionguard_verify/verify.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
from electionguard.ballot import (
55
CiphertextBallot,
66
)
7+
from electionguard.election import CiphertextElectionContext
8+
from electionguard.manifest import (
9+
Manifest,
10+
)
711

812

913
@dataclass
@@ -17,13 +21,23 @@ class Verification:
1721
message: Optional[str]
1822

1923

20-
def verify_ballot(ballot: CiphertextBallot) -> Verification:
24+
def verify_ballot(
25+
ballot: CiphertextBallot,
26+
manifest: Manifest,
27+
context: CiphertextElectionContext,
28+
) -> Verification:
2129
"""
2230
Method to verify the validity of a ballot
23-
24-
TEMPORARY method that always returns True!!!
2531
"""
2632

27-
print(f"Verifying ballot {ballot.object_id}")
33+
if not ballot.is_valid_encryption(
34+
manifest.crypto_hash(),
35+
context.elgamal_public_key,
36+
context.crypto_extended_base_hash,
37+
):
38+
return Verification(
39+
False,
40+
message=f"verify_ballot: mismatching ballot encryption {ballot.object_id}",
41+
)
2842

2943
return Verification(True, message=None)

tests/property/test_verify.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class TestVerify(BaseTestCase):
3030
@given(elgamal_keypairs())
3131
def test_verify_ballot(self, keypair: ElGamalKeyPair):
3232
# Arrange
33-
election = election_factory.get_simple_manifest_from_file()
33+
manifest = election_factory.get_simple_manifest_from_file()
3434
internal_manifest, context = election_factory.get_fake_ciphertext_election(
35-
election, keypair.public_key
35+
manifest, keypair.public_key
3636
)
3737

3838
data = ballot_factory.get_simple_ballot_from_file()
@@ -43,8 +43,8 @@ def test_verify_ballot(self, keypair: ElGamalKeyPair):
4343
self.assertIsNotNone(encrypted_ballot)
4444

4545
# Act
46-
verification = verify_ballot(encrypted_ballot)
46+
verification = verify_ballot(encrypted_ballot, manifest, context)
4747

4848
# Assert
4949
self.assertIsNotNone(verification)
50-
self.assertTrue(verification)
50+
self.assertTrue(verification.verified)

0 commit comments

Comments
 (0)