-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathdecryption_share.py
More file actions
307 lines (251 loc) · 8.81 KB
/
decryption_share.py
File metadata and controls
307 lines (251 loc) · 8.81 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from dataclasses import dataclass, field
from typing import Dict, Optional, Tuple, Union
from .chaum_pedersen import ChaumPedersenProof
from .election_object_base import ElectionObjectBase
from .elgamal import ElGamalCiphertext, ElGamalPublicKey
from .group import ElementModP, ElementModQ
from .logs import log_warning
from .type import ContestId, GuardianId, SelectionId
@dataclass
class CiphertextCompensatedDecryptionSelection(ElectionObjectBase):
"""
A compensated fragment of a Guardian's Partial Decryption of a selection generated by an available guardian
"""
guardian_id: GuardianId
"""
The Available Guardian that this share belongs to
"""
missing_guardian_id: GuardianId
"""
The Missing Guardian for whom this share is calculated on behalf of
"""
share: ElementModP
"""
The Share of the decryption of a selection. `M_{i,l} in the spec`
"""
recovery_key: ElementModP
"""
The Recovery Public Key for the missing_guardian that corresponds to the available guardian's share of the secret
"""
proof: ChaumPedersenProof
"""
The Proof that the share was decrypted correctly
"""
ProofOrRecovery = Union[
ChaumPedersenProof, Dict[GuardianId, CiphertextCompensatedDecryptionSelection]
]
@dataclass
class CiphertextDecryptionSelection(ElectionObjectBase):
"""
A Guardian's Partial Decryption of a selection. A CiphertextDecryptionSelection
can be generated by a guardian directly, or it can be compensated for by a quoprum of guardians
When the guardian generates this share directly, the `proof` field is populated with
a `chaumPedersen` proof that the decryption share was generated correctly.
When the share is generated on behalf of this guardian by other guardians, the `recovered_parts`
collection is populated with the `CiphertextCompensatedDecryptionSelection` objects generated
by each available guardian.
"""
guardian_id: GuardianId
"""
The Available Guardian that this share belongs to
"""
share: ElementModP
"""
The Share of the decryption of a selection. `M_i` in the spec
"""
proof: Optional[ChaumPedersenProof] = field(init=True, default=None)
"""
The Proof that the share was decrypted correctly, if the guardian
was available for decryption
"""
recovered_parts: Optional[
Dict[GuardianId, CiphertextCompensatedDecryptionSelection]
] = field(init=True, default=None)
"""
the recovered parts of the decryption provided by available guardians,
if the guardian was missing from decryption
"""
def is_valid(
self,
message: ElGamalCiphertext,
election_public_key: ElGamalPublicKey,
extended_base_hash: ElementModQ,
) -> bool:
"""
Verify that this CiphertextDecryptionSelection is valid for a
specific ElGamal key pair, public key, and election context.
:param message: the `ElGamalCiphertext` to compare
:param election_public_key: the `ElementModP Election Public Key for the Guardian
:param extended_base_hash: The `ElementModQ` election extended base hash.
"""
# verify we have a proof or recovered parts
if self.proof is None and self.recovered_parts is None:
log_warning(
(
f"CiphertextDecryptionSelection is_valid failed for guardian: {self.guardian_id} "
f"selection: {self.object_id} with missing data"
)
)
return False
if self.proof is not None and self.recovered_parts is not None:
log_warning(
(
f"CiphertextDecryptionSelection is_valid failed for guardian: {self.guardian_id} "
f"selection: {self.object_id} cannot have proof and recovery"
)
)
return False
if self.proof is not None and not self.proof.is_valid(
message,
election_public_key,
self.share,
extended_base_hash,
):
log_warning(
(
f"CiphertextDecryptionSelection is_valid failed for guardian: {self.guardian_id} "
f"selection: {self.object_id} with invalid proof"
)
)
return False
if self.recovered_parts is not None:
for (
_compensating_guardian_id,
part,
) in self.recovered_parts.items():
if not part.proof.is_valid(
message,
part.recovery_key,
part.share,
extended_base_hash,
):
log_warning(
(
f"CiphertextDecryptionSelection is_valid failed for guardian: {self.guardian_id} "
f"selection: {self.object_id} with invalid partial proof"
)
)
return False
return True
def create_ciphertext_decryption_selection(
object_id: str,
guardian_id: GuardianId,
share: ElementModP,
proof_or_recovery: ProofOrRecovery,
) -> CiphertextDecryptionSelection:
"""
Create a ciphertext decryption selection
:param object_id: Object id
:param guardian_id: Guardian id
:param description_hash: Description hash
:param share: Share
:param proof_or_recovery: Proof or recovery
"""
if isinstance(proof_or_recovery, ChaumPedersenProof):
return CiphertextDecryptionSelection(
object_id, guardian_id, share, proof=proof_or_recovery
)
if isinstance(proof_or_recovery, dict):
return CiphertextDecryptionSelection(
object_id,
guardian_id,
share,
recovered_parts=proof_or_recovery,
)
log_warning(f"decryption share cannot assign {proof_or_recovery}")
return CiphertextDecryptionSelection(
object_id,
guardian_id,
share,
)
@dataclass
class CiphertextDecryptionContest(ElectionObjectBase):
"""
A Guardian's Partial Decryption of a contest
"""
guardian_id: GuardianId
"""
The Available Guardian that this share belongs to
"""
description_hash: ElementModQ
"""
The ContestDescription Hash
"""
selections: Dict[SelectionId, CiphertextDecryptionSelection]
"""
the collection of decryption shares for this contest's selections
"""
@dataclass
class CiphertextCompensatedDecryptionContest(ElectionObjectBase):
"""
A Guardian's Partial Decryption of a contest
"""
guardian_id: GuardianId
"""
The Available Guardian that this share belongs to
"""
missing_guardian_id: GuardianId
"""
The Missing Guardian for whom this share is calculated on behalf of
"""
description_hash: ElementModQ
"""
The ContestDescription Hash
"""
selections: Dict[SelectionId, CiphertextCompensatedDecryptionSelection]
"""
the collection of decryption shares for this contest's selections
"""
@dataclass
class DecryptionShare(ElectionObjectBase):
"""
A Guardian's Partial Decryption Share of a specific set of contests (Tally or Ballot)
"""
guardian_id: GuardianId
"""
The Available Guardian that this share belongs to
"""
public_key: ElGamalPublicKey
"""
The election public key for the guardian
"""
contests: Dict[ContestId, CiphertextDecryptionContest]
"""
The collection of all contests in the ballot
"""
@dataclass
class CompensatedDecryptionShare(ElectionObjectBase):
"""
A Compensated Partial Decryption Share generated by
an available guardian on behalf of a missing guardian
"""
guardian_id: GuardianId
"""
The Available Guardian that this share belongs to
"""
missing_guardian_id: GuardianId
"""
The Missing Guardian for whom this share is calculated on behalf of
"""
public_key: ElGamalPublicKey
"""
The election public key for the guardian
"""
contests: Dict[ContestId, CiphertextCompensatedDecryptionContest]
"""
The collection of all contests in the ballot
"""
def get_shares_for_selection(
selection_id: str,
shares: Dict[GuardianId, DecryptionShare],
) -> Dict[GuardianId, Tuple[ElementModP, CiphertextDecryptionSelection]]:
"""
Get all of the cast shares for a specific selection
"""
selections: Dict[GuardianId, Tuple[ElementModP, CiphertextDecryptionSelection]] = {}
for share in shares.values():
for contest in share.contests.values():
for selection in contest.selections.values():
if selection.object_id == selection_id:
selections[share.guardian_id] = (share.public_key, selection)
return selections