-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathdecrypt_step.py
More file actions
74 lines (61 loc) · 2.7 KB
/
decrypt_step.py
File metadata and controls
74 lines (61 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from typing import List
from electionguard.guardian import Guardian
from electionguard.utils import get_optional
from electionguard.ballot import SubmittedBallot
from electionguard.manifest import Manifest
from electionguard.tally import CiphertextTally
from electionguard.decryption_mediator import DecryptionMediator
from electionguard.election_polynomial import LagrangeCoefficientsRecord
from ..cli_models import BuildElectionResults, CliDecryptResults
from .cli_step_base import CliStepBase
class DecryptStep(CliStepBase):
"""Responsible for decrypting a tally and/or cast ballots"""
def _get_lagrange_coefficients(
self, decryption_mediator: DecryptionMediator
) -> LagrangeCoefficientsRecord:
lagrange_coefficients = LagrangeCoefficientsRecord(
decryption_mediator.get_lagrange_coefficients()
)
coefficient_count = len(lagrange_coefficients.coefficients)
self.print_value("Lagrange coefficients retrieved", coefficient_count)
return lagrange_coefficients
def decrypt(
self,
ciphertext_tally: CiphertextTally,
spoiled_ballots: List[SubmittedBallot],
guardians: List[Guardian],
build_election_results: BuildElectionResults,
manifest: Manifest,
) -> CliDecryptResults:
self.print_header("Decrypting tally")
decryption_mediator = DecryptionMediator(
"decryption-mediator",
build_election_results.context,
)
context = build_election_results.context
self.print_value("Cast ballots", ciphertext_tally.cast())
self.print_value("Spoiled ballots", ciphertext_tally.spoiled())
self.print_value("Total ballots", len(ciphertext_tally))
count = 0
for guardian in guardians:
guardian_key = guardian.share_key()
tally_share = get_optional(
guardian.compute_tally_share(ciphertext_tally, context)
)
ballot_shares = guardian.compute_ballot_shares(spoiled_ballots, context)
decryption_mediator.announce(guardian_key, tally_share, ballot_shares)
count += 1
self.print_value(f"Guardian Present: {guardian.id}")
lagrange_coefficients = self._get_lagrange_coefficients(decryption_mediator)
plaintext_tally = get_optional(
decryption_mediator.get_plaintext_tally(ciphertext_tally, manifest)
)
plaintext_spoiled_ballots = get_optional(
decryption_mediator.get_plaintext_ballots(spoiled_ballots, manifest)
)
return CliDecryptResults(
plaintext_tally,
plaintext_spoiled_ballots,
ciphertext_tally,
lagrange_coefficients,
)