-
Notifications
You must be signed in to change notification settings - Fork 96
/
decryption.py
671 lines (578 loc) · 22.7 KB
/
decryption.py
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
from typing import Dict, List, Optional, Tuple
from electionguard.chaum_pedersen import ChaumPedersenProof, make_chaum_pedersen
from electionguard.elgamal import ElGamalCiphertext
from .ballot import (
SubmittedBallot,
CiphertextSelection,
CiphertextContest,
)
from .decryption_share import (
CiphertextDecryptionSelection,
CiphertextCompensatedDecryptionSelection,
CiphertextDecryptionContest,
CiphertextCompensatedDecryptionContest,
create_ciphertext_decryption_selection,
DecryptionShare,
CompensatedDecryptionShare,
)
from .election import CiphertextElectionContext
from .election_polynomial import compute_lagrange_coefficient
from .group import (
ElementModP,
ElementModQ,
ONE_MOD_P,
mult_p,
pow_p,
pow_q,
rand_q,
)
from .key_ceremony import ElectionKeyPair, ElectionPartialKeyBackup, ElectionPublicKey
from .logs import log_warning
from .scheduler import Scheduler
from .tally import CiphertextTally
from .type import ContestId, GuardianId, SelectionId
RecoveryPublicKey = ElementModP
def compute_decryption_share(
guardian_keys: ElectionKeyPair,
tally: CiphertextTally,
context: CiphertextElectionContext,
scheduler: Optional[Scheduler] = None,
) -> Optional[DecryptionShare]:
"""
Compute the decryption for all of the contests in the Ciphertext Tally
:param guardian_keys: Guardian's election key pair
:param tally: Encrypted tally to get decryption share of
:param context: Election context
:param scheduler: Scheduler
:return: Return a guardian's decryption share of tally or None if error
"""
contests: Dict[ContestId, CiphertextDecryptionContest] = {}
for contest in tally.contests.values():
contest_share = compute_decryption_share_for_contest(
guardian_keys,
CiphertextContest(
contest.object_id,
contest.sequence_order,
contest.description_hash,
list(contest.selections.values()),
),
context,
scheduler,
)
if contest_share is None:
return None
contests[contest.object_id] = contest_share
return DecryptionShare(
tally.object_id,
guardian_keys.owner_id,
guardian_keys.share().key,
contests,
)
def compute_compensated_decryption_share(
guardian_key: ElectionPublicKey,
missing_guardian_key: ElectionPublicKey,
missing_guardian_backup: ElectionPartialKeyBackup,
tally: CiphertextTally,
context: CiphertextElectionContext,
scheduler: Optional[Scheduler] = None,
) -> Optional[CompensatedDecryptionShare]:
"""
Compute the compensated decryption for all of the contests in the Ciphertext Tally
:param guardian_key: Guardian's election public key
:param missing_guardian_key: Missing guardian's election public key
:param missing_guardian_backup: Missing guardian's election partial key backup
:param tally: Encrypted tally to get decryption share of
:param context: Election context
:param scheduler: Scheduler
:return: Return a guardian's compensated decryption share of tally for the missing guardian
or None if error
"""
contests: Dict[ContestId, CiphertextCompensatedDecryptionContest] = {}
for contest in tally.contests.values():
contest_share = compute_compensated_decryption_share_for_contest(
guardian_key,
missing_guardian_key,
missing_guardian_backup,
CiphertextContest(
contest.object_id,
contest.sequence_order,
contest.description_hash,
list(contest.selections.values()),
),
context,
scheduler,
)
if contest_share is None:
return None
contests[contest.object_id] = contest_share
return CompensatedDecryptionShare(
tally.object_id,
guardian_key.owner_id,
missing_guardian_key.owner_id,
guardian_key.key,
contests,
)
def compute_decryption_share_for_ballot(
guardian_keys: ElectionKeyPair,
ballot: SubmittedBallot,
context: CiphertextElectionContext,
scheduler: Optional[Scheduler] = None,
) -> Optional[DecryptionShare]:
"""
Compute the decryption for a single ballot
:param guardian_keys: Guardian's election key pair
:param ballot: Ballot to be decrypted
:param context: The public election encryption context
:param scheduler: Scheduler
:return: Decryption share for ballot or `None` if there is an error
"""
contests: Dict[ContestId, CiphertextDecryptionContest] = {}
for contest in ballot.contests:
contest_share = compute_decryption_share_for_contest(
guardian_keys,
CiphertextContest(
contest.object_id,
contest.sequence_order,
contest.description_hash,
contest.ballot_selections,
),
context,
scheduler,
)
if contest_share is None:
return None
contests[contest.object_id] = contest_share
return DecryptionShare(
ballot.object_id,
guardian_keys.owner_id,
guardian_keys.share().key,
contests,
)
def compute_compensated_decryption_share_for_ballot(
guardian_key: ElectionPublicKey,
missing_guardian_key: ElectionPublicKey,
missing_guardian_backup: ElectionPartialKeyBackup,
ballot: SubmittedBallot,
context: CiphertextElectionContext,
scheduler: Optional[Scheduler] = None,
) -> Optional[CompensatedDecryptionShare]:
"""
Compute the compensated decryption for a single ballot
:param guardian_key: Guardian's election public key
:param missing_guardian_key: Missing guardian's election public key
:param missing_guardian_backup: Missing guardian's election partial key backup
:param ballot: Encrypted ballot to get decryption share of
:param context: Election context
:param scheduler: Scheduler
:return: Return a guardian's compensated decryption share of ballot for the missing guardian
or None if error
"""
contests: Dict[ContestId, CiphertextCompensatedDecryptionContest] = {}
for contest in ballot.contests:
contest_share = compute_compensated_decryption_share_for_contest(
guardian_key,
missing_guardian_key,
missing_guardian_backup,
CiphertextContest(
contest.object_id,
contest.sequence_order,
contest.description_hash,
contest.ballot_selections,
),
context,
scheduler,
)
if contest_share is None:
return None
contests[contest.object_id] = contest_share
return CompensatedDecryptionShare(
ballot.object_id,
guardian_key.owner_id,
missing_guardian_key.owner_id,
guardian_key.key,
contests,
)
def compute_decryption_share_for_contest(
guardian_keys: ElectionKeyPair,
contest: CiphertextContest,
context: CiphertextElectionContext,
scheduler: Optional[Scheduler] = None,
) -> Optional[CiphertextDecryptionContest]:
"""
Compute the decryption share for a single contest
:param guardian_keys: Guardian's election key pair
:param contest: Contest to be decrypted
:param context: The public election encryption context
:param scheduler: Scheduler
:return: Decryption share for contest or `None` if there is an error
"""
if not scheduler:
scheduler = Scheduler()
selections: Dict[SelectionId, CiphertextDecryptionSelection] = {}
decryptions: List[Optional[CiphertextDecryptionSelection]] = scheduler.schedule(
compute_decryption_share_for_selection,
[(guardian_keys, selection, context) for selection in contest.selections],
with_shared_resources=True,
)
for decryption in decryptions:
if decryption is None:
return None
selections[decryption.object_id] = decryption
return CiphertextDecryptionContest(
contest.object_id,
guardian_keys.owner_id,
contest.description_hash,
selections,
)
def compute_compensated_decryption_share_for_contest(
guardian_key: ElectionPublicKey,
missing_guardian_key: ElectionPublicKey,
missing_guardian_backup: ElectionPartialKeyBackup,
contest: CiphertextContest,
context: CiphertextElectionContext,
scheduler: Optional[Scheduler] = None,
) -> Optional[CiphertextCompensatedDecryptionContest]:
"""
Compute the compensated decryption share for a single contest
:param guardian_key: The election public key of the available guardian that will partially decrypt the selection
:param missing_guardian_key: Election public key of the guardian that is missing
:param missing_guardian_backup: Election partial key backup of the missing guardian
:param contest: The specific contest to decrypt
:param context: The public election encryption context
:return: a `CiphertextCompensatedDecryptionContest` or `None` if there is an error
"""
if not scheduler:
scheduler = Scheduler()
selections: Dict[SelectionId, CiphertextCompensatedDecryptionSelection] = {}
selection_decryptions: List[
Optional[CiphertextCompensatedDecryptionSelection]
] = scheduler.schedule(
compute_compensated_decryption_share_for_selection,
[
(
guardian_key,
missing_guardian_key,
missing_guardian_backup,
selection,
context,
)
for selection in contest.selections
],
with_shared_resources=True,
)
for decryption in selection_decryptions:
if decryption is None:
return None
selections[decryption.object_id] = decryption
return CiphertextCompensatedDecryptionContest(
contest.object_id,
guardian_key.owner_id,
missing_guardian_key.owner_id,
contest.description_hash,
selections,
)
def compute_decryption_share_for_selection(
guardian_keys: ElectionKeyPair,
selection: CiphertextSelection,
context: CiphertextElectionContext,
) -> Optional[CiphertextDecryptionSelection]:
"""
Compute a partial decryption for a specific selection
:param guardian_keys: Election keys for the guardian who will partially decrypt the selection
:param selection: The specific selection to decrypt
:param context: The public election encryption context
:return: a `CiphertextDecryptionSelection` or `None` if there is an error
"""
(decryption, proof) = partially_decrypt(
guardian_keys, selection.ciphertext, context.crypto_extended_base_hash
)
if proof.is_valid(
selection.ciphertext,
guardian_keys.key_pair.public_key,
decryption,
context.crypto_extended_base_hash,
):
return create_ciphertext_decryption_selection(
selection.object_id,
guardian_keys.owner_id,
decryption,
proof,
)
log_warning(
f"compute decryption share proof failed for guardian {guardian_keys.owner_id}"
f"and {selection.object_id} with invalid proof"
)
return None
def compute_compensated_decryption_share_for_selection(
guardian_key: ElectionPublicKey,
missing_guardian_key: ElectionPublicKey,
missing_guardian_backup: ElectionPartialKeyBackup,
selection: CiphertextSelection,
context: CiphertextElectionContext,
) -> Optional[CiphertextCompensatedDecryptionSelection]:
"""
Compute a compensated decryption share for a specific selection using the
available guardian's share of the missing guardian's private key polynomial
:param guardian_key: The election public key of the available guardian that will partially decrypt the selection
:param missing_guardian_key: Election public key of the guardian that is missing
:param missing_guardian_backup: Election partial key backup of the missing guardian
:param selection: The specific selection to decrypt
:param context: The public election encryption context
:return: a `CiphertextCompensatedDecryptionSelection` or `None` if there is an error
"""
compensated = compensate_decrypt(
missing_guardian_backup,
selection.ciphertext,
context.crypto_extended_base_hash,
)
if compensated is None:
log_warning(
(
f"compute compensated decryption share failed for {guardian_key.owner_id} "
f"missing: {missing_guardian_key.owner_id} {selection.object_id}"
)
)
return None
(decryption, proof) = compensated
recovery_public_key = compute_recovery_public_key(
guardian_key, missing_guardian_key
)
if proof.is_valid(
selection.ciphertext,
recovery_public_key,
decryption,
context.crypto_extended_base_hash,
):
share = CiphertextCompensatedDecryptionSelection(
selection.object_id,
guardian_key.owner_id,
missing_guardian_key.owner_id,
decryption,
recovery_public_key,
proof,
)
return share
log_warning(
(
f"compute compensated decryption share proof failed for {guardian_key.owner_id} "
f"missing: {missing_guardian_key.owner_id} {selection.object_id}"
)
)
return None
def partially_decrypt(
guardian_keys: ElectionKeyPair,
elgamal: ElGamalCiphertext,
extended_base_hash: ElementModQ,
nonce_seed: ElementModQ = None,
) -> Tuple[ElementModP, ChaumPedersenProof]:
"""
Compute a partial decryption of an elgamal encryption
:param elgamal: the `ElGamalCiphertext` that will be partially decrypted
:param extended_base_hash: the extended base hash of the election that
was used to generate t he ElGamal Ciphertext
:param nonce_seed: an optional value used to generate the `ChaumPedersenProof`
if no value is provided, a random number will be used.
:return: a `Tuple[ElementModP, ChaumPedersenProof]` of the decryption and its proof
"""
if nonce_seed is None:
nonce_seed = rand_q()
# TODO: ISSUE #47: Decrypt the election secret key
# 𝑀_i = 𝐴^𝑠𝑖 mod 𝑝
partial_decryption = elgamal.partial_decrypt(guardian_keys.key_pair.secret_key)
# 𝑀_i = 𝐴^𝑠𝑖 mod 𝑝 and 𝐾𝑖 = 𝑔^𝑠𝑖 mod 𝑝
proof = make_chaum_pedersen(
message=elgamal,
s=guardian_keys.key_pair.secret_key,
m=partial_decryption,
seed=nonce_seed,
hash_header=extended_base_hash,
)
return (partial_decryption, proof)
def compensate_decrypt(
missing_guardian_backup: ElectionPartialKeyBackup,
ciphertext: ElGamalCiphertext,
extended_base_hash: ElementModQ,
nonce_seed: ElementModQ = None,
) -> Optional[Tuple[ElementModP, ChaumPedersenProof]]:
"""
Compute a compensated partial decryption of an elgamal encryption
on behalf of the missing guardian
:param missing_guardian_backup: Missing guardians backup
:param ciphertext: the `ElGamalCiphertext` that will be partially decrypted
:param extended_base_hash: the extended base hash of the election that
was used to generate t he ElGamal Ciphertext
:param nonce_seed: an optional value used to generate the `ChaumPedersenProof`
if no value is provided, a random number will be used.
:return: a `Tuple[ElementModP, ChaumPedersenProof]` of the decryption and its proof
"""
if nonce_seed is None:
nonce_seed = rand_q()
partial_secret_key = missing_guardian_backup.coordinate
# 𝑀_{𝑖,l} = 𝐴^P𝑖_{l}
partial_decryption = ciphertext.partial_decrypt(partial_secret_key)
# 𝑀_{𝑖,l} = 𝐴^𝑠𝑖 mod 𝑝 and 𝐾𝑖 = 𝑔^𝑠𝑖 mod 𝑝
proof = make_chaum_pedersen(
ciphertext,
partial_secret_key,
partial_decryption,
nonce_seed,
extended_base_hash,
)
return (partial_decryption, proof)
def compute_recovery_public_key(
guardian_key: ElectionPublicKey,
missing_guardian_key: ElectionPublicKey,
) -> RecoveryPublicKey:
"""
Compute the recovery public key,
corresponding to the secret share Pi(l)
K_ij^(l^j) for j in 0..k-1. K_ij is coefficients[j].public_key
"""
pub_key = ONE_MOD_P
for index, commitment in enumerate(missing_guardian_key.coefficient_commitments):
exponent = pow_q(guardian_key.sequence_order, index)
pub_key = mult_p(pub_key, pow_p(commitment, exponent))
return pub_key
def reconstruct_decryption_share(
missing_guardian_key: ElectionPublicKey,
tally: CiphertextTally,
shares: Dict[GuardianId, CompensatedDecryptionShare],
lagrange_coefficients: Dict[GuardianId, ElementModQ],
) -> DecryptionShare:
"""
Reconstruct the missing Decryption Share for a missing guardian
from the collection of compensated decryption shares
:param missing_guardian_id: The guardian id for the missing guardian
:param public_key: The public key of the guardian creating share
:param tally: The collection of `CiphertextTallyContest` that is cast
:shares: the collection of `CompensatedTallyDecryptionShare` for the missing guardian from available guardians
:lagrange_coefficients: the lagrange coefficients corresponding to the available guardians that provided shares
"""
contests: Dict[ContestId, CiphertextDecryptionContest] = {}
for contest in tally.contests.values():
contests[contest.object_id] = reconstruct_decryption_contest(
missing_guardian_key.owner_id,
CiphertextContest(
contest.object_id,
contest.sequence_order,
contest.description_hash,
list(contest.selections.values()),
),
shares,
lagrange_coefficients,
)
return DecryptionShare(
tally.object_id,
missing_guardian_key.owner_id,
missing_guardian_key.key,
contests,
)
def reconstruct_decryption_share_for_ballot(
missing_guardian_key: ElectionPublicKey,
ballot: SubmittedBallot,
shares: Dict[GuardianId, CompensatedDecryptionShare],
lagrange_coefficients: Dict[GuardianId, ElementModQ],
) -> DecryptionShare:
"""
Reconstruct a missing ballot Decryption share for a missing guardian
from the collection of compensated decryption shares
:param missing_guardian_id: The guardian id for the missing guardian
:param public_key: the public key for the missing guardian
:param ballot: The `SubmittedBallot` to reconstruct
:shares: the collection of `CompensatedBallotDecryptionShare` for
the missing guardian, each keyed by the ID of the guardian that produced it from available guardians
:lagrange_coefficients: the lagrange coefficients corresponding to the available guardians that provided shares
"""
contests: Dict[ContestId, CiphertextDecryptionContest] = {}
for contest in ballot.contests:
contests[contest.object_id] = reconstruct_decryption_contest(
missing_guardian_key.owner_id,
CiphertextContest(
contest.object_id,
contest.sequence_order,
contest.description_hash,
contest.ballot_selections,
),
shares,
lagrange_coefficients,
)
return DecryptionShare(
ballot.object_id,
missing_guardian_key.owner_id,
missing_guardian_key.key,
contests,
)
def reconstruct_decryption_contest(
missing_guardian_id: GuardianId,
contest: CiphertextContest,
shares: Dict[GuardianId, CompensatedDecryptionShare],
lagrange_coefficients: Dict[GuardianId, ElementModQ],
) -> CiphertextDecryptionContest:
"""
Recontruct the missing Decryption Share for a missing guardian
from the collection of compensated decryption shares
:param missing_guardian_id: The guardian id for the missing guardian
:param contest: The CiphertextContest to decrypt
:shares: the collection of `CompensatedDecryptionShare` for the missing guardian from available guardians
:lagrange_coefficients: the lagrange coefficients corresponding to the available guardians that provided shares
"""
contest_shares: Dict[GuardianId, CiphertextCompensatedDecryptionContest] = {
available_guardian_id: compensated_share.contests[contest.object_id]
for available_guardian_id, compensated_share in shares.items()
}
selections: Dict[SelectionId, CiphertextDecryptionSelection] = {}
for selection in contest.selections:
# collect all of the shares generated for each selection
compensated_selection_shares: Dict[
GuardianId, CiphertextCompensatedDecryptionSelection
] = {
available_guardian_id: compensated_contest.selections[selection.object_id]
for available_guardian_id, compensated_contest in contest_shares.items()
}
share_pow_p = []
for available_guardian_id, share in compensated_selection_shares.items():
share_pow_p.append(
pow_p(share.share, lagrange_coefficients[available_guardian_id])
)
reconstructed_share = mult_p(*share_pow_p)
selections[selection.object_id] = create_ciphertext_decryption_selection(
selection.object_id,
missing_guardian_id,
reconstructed_share,
compensated_selection_shares,
)
return CiphertextDecryptionContest(
contest.object_id,
missing_guardian_id,
contest.description_hash,
selections,
)
def compute_lagrange_coefficients_for_guardians(
available_guardians_keys: List[ElectionPublicKey],
) -> Dict[GuardianId, ElementModQ]:
"""
Produce all Lagrange coefficients for a collection of available
Guardians, to be used when reconstructing a missing share.
"""
return {
guardian_keys.owner_id: compute_lagrange_coefficients_for_guardian(
guardian_keys, available_guardians_keys
)
for guardian_keys in available_guardians_keys
}
def compute_lagrange_coefficients_for_guardian(
guardian_key: ElectionPublicKey,
other_guardians_keys: List[ElectionPublicKey],
) -> ElementModQ:
"""
Produce a Lagrange coefficient for a single Guardian, to be used when reconstructing a missing share.
"""
other_guardian_orders = [
g.sequence_order
for g in other_guardians_keys
if g.owner_id != guardian_key.owner_id
]
return compute_lagrange_coefficient(
guardian_key.sequence_order,
*other_guardian_orders,
)