-
Notifications
You must be signed in to change notification settings - Fork 19
/
BoardroomVoting.v
867 lines (799 loc) · 34.3 KB
/
BoardroomVoting.v
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
From Coq Require Import List. Import ListNotations.
From Coq Require Import ZArith.
From Coq Require Import Znumtheory.
From Coq Require Import Permutation.
From Coq Require Import Lia.
From ConCert.Utils Require Import Automation.
From ConCert.Utils Require Import Extras.
From ConCert.Utils Require Import RecordUpdate.
From ConCert.Execution Require Import Blockchain.
From ConCert.Execution Require Import Containers.
From ConCert.Execution Require Import Monad.
From ConCert.Execution Require Import ResultMonad.
From ConCert.Execution Require Import Serializable.
From ConCert.Execution Require Import ContractMonads.
From ConCert.Execution Require Import ContractCommon. Import AddressMap.
From ConCert.Examples.BoardroomVoting Require Import BoardroomMath.
Module Type BoardroomParams.
Parameter A : Type.
Parameter H : list positive -> positive.
Parameter ser : Serializable A.
Parameter axioms : BoardroomAxioms A.
Parameter gen : Generator axioms.
Parameter discr_log : DiscreteLog axioms gen.
Parameter Base : ChainBase.
End BoardroomParams.
Module BoardroomVoting (Params : BoardroomParams).
Import Params.
#[local]
Existing Instance ser.
#[local]
Existing Instance axioms.
#[local]
Existing Instance gen.
#[local]
Existing Instance discr_log.
#[local]
Existing Instance Base.
(* Allow us to automatically derive Serializable instances *)
Set Nonrecursive Elimination Schemes.
Record Setup :=
build_setup {
eligible_voters : AddrMap unit;
finish_registration_by : nat;
finish_commit_by : option nat;
finish_vote_by : nat;
registration_deposit : Amount;
}.
Record VoterInfo :=
build_voter_info {
voter_index : nat;
vote_hash : positive;
public_vote : A;
}.
Record State :=
build_state {
owner : Address;
registered_voters : AddrMap VoterInfo;
public_keys : list A;
setup : Setup;
tally : option nat;
}.
Definition Error : Type := nat.
Definition default_error : Error := 1%nat.
(* w, a1, b1, a2, b2, d1, d2 *)
Definition VoteProof := (Z * A * A * A * A * Z * Z * Z * Z)%type.
Inductive Msg :=
| signup (pk : A) (proof : A * Z)
| commit_to_vote (hash : positive)
| submit_vote (v : A) (proof : VoteProof)
| tally_votes.
(* begin hide *)
MetaCoq Run (make_setters VoterInfo).
MetaCoq Run (make_setters State).
(* end hide *)
Section Serialization.
Global Instance Setup_serializable : Serializable Setup :=
Derive Serializable Setup_rect<build_setup>.
Global Instance VoterInfo_serializable : Serializable VoterInfo :=
Derive Serializable VoterInfo_rect<build_voter_info>.
Global Instance State_serializable : Serializable State :=
Derive Serializable State_rect<build_state>.
Global Instance Msg_serializable : Serializable Msg :=
Derive Serializable Msg_rect<signup, commit_to_vote, submit_vote, tally_votes>.
End Serialization.
Local Open Scope broom.
Definition encodeA : A -> positive := countable.encode.
Definition encodeNat : nat -> positive := countable.encode.
Definition hash_sk_data (gv pk : A) (i : nat) : positive :=
H [encodeA (generator : A); encodeA gv; encodeA pk; encodeNat i].
(* This follows the original open vote protocol paper. It is a schnorr signature
with the fiat-shamir heuristic applied. *)
Definition secret_key_proof (sk : Z) (v : Z) (i : nat) : A * Z :=
let gv : A := generator^v in
let pk := compute_public_key sk in
let z := Zpos (hash_sk_data gv pk i) in
let r := (v - sk * z)%Z in
(gv, r).
Definition verify_secret_key_proof (pk : A) (i : nat) (proof : A * Z) : bool :=
let (gv, r) := proof in
let z := Zpos (hash_sk_data gv pk i) in
elmeqb gv (generator^r * (pk^z)).
Definition hash_sv_data (i : nat) (pk rk a1 b1 a2 b2 : A) : positive :=
H (encodeNat i :: map encodeA [pk; rk; a1; b1; a2; b2]).
Definition secret_vote_proof (sk : Z) (rk : A) (sv : bool) (i : nat) (w r d : Z) : VoteProof :=
let pk : A := compute_public_key sk in
let pv : A := compute_public_vote rk sk sv in
if sv then
let a1 : A := generator^r * pk^d in
let b1 : A := rk^r * pv^d in
let a2 : A := generator^w in
let b2 : A := rk^w in
let c := Zpos (hash_sv_data i pk rk a1 b1 a2 b2) in
let d2 := c - d in
let r2 := w - sk*d2 in
(w, a1, b1, a2, b2, d, d2, r, r2)
else
let a1 := generator^w in
let b1 := rk^w in
let a2 := generator^r * pk^d in
let b2 := rk^r * (pv * inv generator)^d in
let c := Zpos (hash_sv_data i pk rk a1 b1 a2 b2) in
let d1 := c - d in
let r1 := w - sk*d1 in
(w, a1, b1, a2, b2, d1, d, r1, r).
Local Open Scope bool.
Definition verify_secret_vote_proof (pk rk pv : A) (i : nat) (proof : VoteProof) : bool :=
let '(w, a1, b1, a2, b2, d1, d2, r1, r2) := proof in
let c := hash_sv_data i pk rk a1 b1 a2 b2 in
(Zpos c =? d1 + d2)%Z &&
(a1 =? generator^r1 * pk^d1)%broom &&
(b1 =? rk^r1 * pv^d1)%broom &&
(a2 =? generator^r2 * pk^d2)%broom &&
(b2 =? rk^r2 * (pv * inv generator)^d2)%broom.
Definition make_signup_msg (sk : Z) (v : Z) (i : nat) : Msg :=
signup (compute_public_key sk) (secret_key_proof sk v i).
Definition make_commit_msg (pks : list A) (my_index : nat) (sk : Z) (sv : bool) : Msg :=
let pv := compute_public_vote (reconstructed_key pks my_index) sk sv in
commit_to_vote (H [encodeA pv]).
Definition make_vote_msg (pks : list A) (my_index : nat) (sk : Z) (sv : bool) (w r d : Z) : Msg :=
let rk := reconstructed_key pks my_index in
submit_vote (compute_public_vote rk sk sv)
(secret_vote_proof sk rk sv my_index w r d).
Definition assert_true_init (check : bool) : ContractIniter Setup Error unit :=
@lift _ (fun T => result T Error) _ _ (if check then Ok tt else Err default_error).
Definition assert_true (check : bool) : ContractReceiver State Msg Error unit :=
@lift _ (fun T => result T Error) _ _ (if check then Ok tt else Err default_error).
Definition assert_false (check : bool) : ContractReceiver State Msg Error unit :=
@lift _ (fun T => result T Error) _ _ (if check then Err default_error else Ok tt).
Definition assert_some {A : Type} (check : option A) : ContractReceiver State Msg Error unit :=
@lift _ (fun T => result T Error) _ _ (if check then Ok tt else Err default_error).
Definition assert_none {A : Type} (check : option A) : ContractReceiver State Msg Error unit :=
@lift _ (fun T => result T Error) _ _ (if check then Err default_error else Ok tt).
(* A necessary aliasing to make extraction work *)
Definition ContractIniterSetupState := ContractIniter Setup Error State.
Definition init : ContractIniterSetupState :=
do owner <- lift caller_addr;
do setup <- deployment_setup;
do assert_true_init
(finish_registration_by setup <? finish_vote_by setup)%nat;
accept_deployment
{| owner := owner;
registered_voters := AddressMap.empty;
public_keys := [];
setup := setup;
tally := None; |}.
Definition ContractReceiverStateMsgState := ContractReceiver State Msg Error State.
Definition handle_signup pk prf state caller cur_slot : ContractReceiverStateMsgState :=
do assert_false (finish_registration_by (setup state) <? cur_slot)%nat;
do assert_some (AddressMap.find caller (eligible_voters (setup state)));
do assert_none (AddressMap.find caller (registered_voters state));
do amt <- lift call_amount;
do assert_true (amt =? (registration_deposit (setup state)))%Z;
do assert_true (Z.of_nat (length (public_keys state)) <? order - 2);
let index := length (public_keys state) in
do assert_true (verify_secret_key_proof pk index prf);
let inf := {| voter_index := index;
vote_hash := 1%positive;
public_vote := zero; |} in
let new_state := state<|registered_voters ::= AddressMap.add caller inf|>
<|public_keys ::= fun l => l ++ [pk]|> in
accept_call new_state.
Definition handle_commit_to_vote hash state caller cur_slot : ContractReceiverStateMsgState :=
do commit_by <- lift (result_of_option (finish_commit_by (setup state)) default_error);
do assert_false (commit_by <? cur_slot)%nat;
do inf <- lift (result_of_option (AddressMap.find caller (registered_voters state)) default_error);
let inf := inf<|vote_hash := hash|> in
accept_call (state<|registered_voters ::= AddressMap.add caller inf|>).
Definition handle_submit_vote v proof state caller cur_slot : ContractReceiverStateMsgState :=
do assert_false (finish_vote_by (setup state) <? cur_slot)%nat;
do inf <- lift (result_of_option (AddressMap.find caller (registered_voters state)) default_error);
do @lift _ (fun T => result T Error) _ _ (if finish_commit_by (setup state) then
if (H [encodeA v] =? vote_hash inf)%positive then Ok tt else Err default_error
else
Ok tt);
do @lift _ (fun T => result T Error) _ _ (if verify_secret_vote_proof
(nth (voter_index inf) (public_keys state) 0)
(reconstructed_key (public_keys state) (voter_index inf))
v
(voter_index inf)
proof then Ok tt else Err default_error);
let inf := inf<|public_vote := v|> in
accept_call (state<|registered_voters ::= AddressMap.add caller inf|>).
Definition handle_tally_votes state cur_slot : ContractReceiverStateMsgState :=
do assert_false (cur_slot <? finish_vote_by (setup state))%nat;
do assert_none (tally state);
let voters := AddressMap.values (registered_voters state) in
do assert_false (existsb
(fun vi => if elmeqb (public_vote vi) zero then true else false)
voters);
let votes := map public_vote voters in
do res <- @lift _ (fun T => result T Error) _ _ (result_of_option (bruteforce_tally votes) default_error);
accept_call (state<|tally := Some res|>).
Definition receive : ContractReceiverStateMsgState :=
do state <- my_state;
do caller <- lift caller_addr;
do cur_slot <- lift current_slot;
do msg <- call_msg default_error;
match msg with
| signup pk prf => handle_signup pk prf state caller cur_slot
| commit_to_vote hash => handle_commit_to_vote hash state caller cur_slot
| submit_vote v proof => handle_submit_vote v proof state caller cur_slot
| tally_votes => handle_tally_votes state cur_slot
end.
Definition boardroom_voting : Contract Setup Msg State Error :=
build_contract init receive.
Section Theories.
Record SecretVoterInfo :=
build_secret_voter_info {
svi_index : nat;
(* Secret key *)
svi_sk : Z;
(* Chosen randomness for knowledge of secret key proof *)
svi_sk_r : Z;
(* Secret vote *)
svi_sv : bool;
(* Chosen random w for vote proof *)
svi_sv_w : Z;
(* Chosen random r for vote proof *)
svi_sv_r : Z;
(* Chosen random d for vote proof *)
svi_sv_d : Z;
}.
(* begin hide *)
MetaCoq Run (make_setters SecretVoterInfo).
(* end hide *)
(* For correctness we assume that all signups and vote messages were
created using the make_signup_msg and make_vote_msg functions from
the contract *)
Fixpoint MsgAssumption
(pks : list A)
(parties : Address -> SecretVoterInfo)
(calls : list (ContractCallInfo Msg)) : Prop :=
match calls with
| call :: calls =>
let party := parties (Blockchain.call_from call) in
match Blockchain.call_msg call with
| Some (signup pk prf as m) => m = make_signup_msg (svi_sk party) (svi_sk_r party)
(svi_index party)
| Some (submit_vote _ _ as m) =>
m = make_vote_msg
pks
(svi_index party)
(svi_sk party)
(svi_sv party)
(svi_sv_w party)
(svi_sv_r party)
(svi_sv_d party)
| _ => True
end /\ MsgAssumption pks parties calls
| [] => True
end.
Definition signups (calls : list (ContractCallInfo Msg)) : list (Address * A) :=
(* reverse the signups since the calls will have the last one at the head *)
rev (map_option (fun call => match Blockchain.call_msg call with
| Some (signup pk prf) => Some (Blockchain.call_from call, pk)
| _ => None
end) calls).
(* The index map and public keys list provided also needs to match the
order in which parties signed up in the contract. *)
Definition SignupOrderAssumption
(pks : list A)
(parties : Address -> SecretVoterInfo)
(calls : list (ContractCallInfo Msg)) : Prop :=
All (fun '((addr, pk), i) => svi_index (parties addr) = i /\ nth_error pks i = Some pk)
(zip (signups calls) (seq 0 (length (signups calls)))).
Local Open Scope nat.
Lemma no_outgoing bstate caddr :
reachable bstate ->
env_contracts bstate caddr = Some (boardroom_voting : WeakContract) ->
outgoing_acts bstate caddr = [].
Proof.
intros.
apply (lift_outgoing_acts_nil boardroom_voting); try easy.
intros.
destruct msg as [msg|]; cbn -[Nat.ltb] in *; try congruence.
destruct msg.
- destruct (_ <? _); cbn in *; try congruence.
destruct (AddressMap.find _ _); cbn in *; try congruence.
destruct (AddressMap.find _ _); cbn in *; try congruence.
destruct (_ =? _)%Z; cbn in *; try congruence.
destruct (_ <? _)%Z; cbn in *; try congruence.
destruct (verify_secret_key_proof _ _ _); cbn in *; congruence.
- destruct (finish_commit_by _); cbn -[Nat.ltb] in *; try congruence.
destruct (_ <? _); cbn in *; try congruence.
destruct (AddressMap.find _ _); cbn in *; congruence.
- destruct (_ <? _); cbn in *; try congruence.
destruct (AddressMap.find _ _); cbn in *; try congruence.
destruct (if finish_commit_by _ then _ else _); cbn in *; try congruence.
destruct (verify_secret_vote_proof _ _ _ _); cbn in *; congruence.
- destruct (_ <? _); cbn in *; try congruence.
destruct (tally _); cbn in *; try congruence.
destruct (existsb _ _); cbn in *; try congruence.
destruct (bruteforce_tally _); cbn in *; congruence.
Qed.
Lemma Permutation_modify k vold vnew (m : AddrMap VoterInfo) :
FMap.find k m = Some vold ->
voter_index vold = voter_index vnew ->
Permutation (map (fun '(_, v) => voter_index v)
(FMap.elements m))
(seq 0 (FMap.size m)) ->
Permutation
(map (fun '(_, v0) => voter_index v0)
(FMap.elements (FMap.add k vnew m)))
(seq 0 (FMap.size m)).
Proof.
intros find_some index old_perm.
rewrite <- old_perm.
rewrite <- (FMap.add_id _ _ _ find_some) at 2.
rewrite <- (FMap.add_remove k vold).
rewrite (FMap.elements_add_existing k vold vnew) by auto.
rewrite FMap.elements_add by auto.
cbn.
now rewrite index.
Qed.
Lemma all_signups pks parties calls :
SignupOrderAssumption pks parties calls ->
length (signups calls) = length pks ->
map snd (signups calls) = pks.
Proof.
intros order len_signups.
unfold SignupOrderAssumption in order.
revert parties pks len_signups order.
induction (signups calls) as [|[addr pk] xs IH]; intros parties pks len_signups order.
- destruct pks; cbn in *; congruence.
- cbn in *.
destruct pks as [|pk' pks]; cbn in *; try lia.
destruct order as [[index_eq nth_eq] all].
f_equal; try congruence.
apply (IH (fun addr => (parties addr)<|svi_index ::= fun i => i - 1|>));
[lia|].
clear -all.
rewrite <- (map_id xs) in all at 1.
rewrite <- seq_shift in all.
rewrite zip_map in all.
apply All_map in all.
apply (All_ext_in _ _ _ all).
intros.
destruct a, p.
cbn in *.
split; [|tauto].
destruct H1; lia.
Qed.
Local Open Scope broom.
Lemma elmeqb_eq (a a' : A) :
(a =? a') = true <-> a == a'.
Proof.
destruct (elmeqb_spec a a'); [tauto|].
split; congruence.
Qed.
Hint Resolve
pow_nonzero generator_nonzero int_domain generator_nonzero compute_public_key_unit
reconstructed_key_unit
: broom.
Lemma verify_secret_key_proof_spec sk v i :
verify_secret_key_proof (compute_public_key sk) i (secret_key_proof sk v i) = true.
Proof with auto with broom.
cbn.
apply elmeqb_eq.
apply log_both...
rewrite log_pow...
rewrite log_mul...
unfold "exp=".
assert (order - 1 <> 0)%Z by (pose proof order_ge_2; lia).
rewrite Z.add_mod...
rewrite !log_pow...
rewrite log_generator.
rewrite !Z.mul_1_r.
unfold compute_public_key.
rewrite <- Z.mul_mod_idemp_r...
rewrite log_pow...
rewrite log_generator.
rewrite Z.mul_1_r.
rewrite Z.mul_mod_idemp_r...
rewrite <- Z.add_mod...
f_equal.
lia.
Qed.
Lemma verify_secret_vote_proof_spec sk pks sv i w r d :
All (fun pk => pk !== 0) pks ->
verify_secret_vote_proof
(compute_public_key sk)
(reconstructed_key pks i)
(compute_public_vote (reconstructed_key pks i) sk sv)
i
(secret_vote_proof sk (reconstructed_key pks i) sv i w r d) = true.
Proof.
intros all_units.
set (rk := reconstructed_key pks i).
unfold verify_secret_vote_proof, secret_vote_proof.
cbn.
destruct sv.
- set (h := hash_sv_data _ _ _ _ _ _ _).
rewrite Zplus_minus.
rewrite Pos.eqb_refl, !elmeqb_refl.
cbn.
unfold compute_public_key.
rewrite pow_pow by (auto with broom).
rewrite <- pow_plus by (auto with broom).
rewrite Z.sub_add.
rewrite elmeqb_refl.
cbn.
unfold compute_public_vote.
rewrite <- (mul_assoc (rk^sk)).
rewrite (mul_comm generator).
rewrite inv_inv_l by (auto with broom).
rewrite (mul_comm (rk^sk)), mul_1_l.
rewrite pow_pow by (subst rk; auto with broom).
rewrite <- pow_plus by (subst rk; auto with broom).
rewrite Z.sub_add.
now rewrite elmeqb_refl.
- set (h := hash_sv_data _ _ _ _ _ _ _).
rewrite Z.sub_add.
rewrite Pos.eqb_refl, !elmeqb_refl.
cbn.
unfold compute_public_key.
rewrite pow_pow by (auto with broom).
rewrite <- pow_plus by (auto with broom).
rewrite Z.sub_add.
rewrite elmeqb_refl.
cbn.
unfold compute_public_vote.
rewrite (mul_comm (rk^sk)), mul_1_l.
rewrite pow_pow by (subst rk; auto with broom).
rewrite <- pow_plus by (subst rk; auto with broom).
rewrite Z.sub_add.
now rewrite elmeqb_refl.
Qed.
Local Set Keyed Unification.
Definition has_tallied (calls : list (ContractCallInfo Msg)) : bool :=
existsb (fun c => match Blockchain.call_msg c with
| Some tally_votes => true
| _ => false
end) calls.
Theorem boardroom_voting_correct_strong
(bstate : ChainState)
(caddr : Address)
(trace : ChainTrace empty_state bstate)
(parties : Address -> SecretVoterInfo)
(pks : list A) :
env_contracts bstate caddr = Some (boardroom_voting : WeakContract) ->
exists (cstate : State)
(depinfo : DeploymentInfo Setup)
(inc_calls : list (ContractCallInfo Msg)),
deployment_info Setup trace caddr = Some depinfo /\
contract_state bstate caddr = Some cstate /\
incoming_calls Msg trace caddr = Some inc_calls /\
finish_registration_by (setup cstate) < finish_vote_by (setup cstate) /\
(Blockchain.current_slot bstate < finish_vote_by (setup cstate) ->
has_tallied inc_calls = false) /\
length (public_keys cstate) = FMap.size (registered_voters cstate) /\
public_keys cstate = map snd (signups inc_calls) /\
(Z.of_nat (length (public_keys cstate)) < order - 1)%Z /\
(MsgAssumption pks parties inc_calls ->
SignupOrderAssumption pks parties inc_calls ->
(finish_registration_by (setup cstate) < Blockchain.current_slot bstate ->
length pks = length (signups inc_calls)) ->
Permutation (map (fun '(_, v) => voter_index v)
(FMap.elements (registered_voters cstate)))
(seq 0 (length (public_keys cstate))) /\
Permutation (FMap.keys (registered_voters cstate))
(map fst (signups inc_calls)) /\
(forall addr inf,
FMap.find addr (registered_voters cstate) = Some inf ->
voter_index inf < length (public_keys cstate) /\
voter_index inf = svi_index (parties addr) /\
nth_error (public_keys cstate) (voter_index inf) =
Some (compute_public_key (svi_sk (parties addr))) /\
(public_vote inf == zero \/
public_vote inf = compute_public_vote
(reconstructed_key pks (voter_index inf))
(svi_sk (parties addr))
(svi_sv (parties addr)))) /\
((has_tallied inc_calls = false ->
tally cstate = None) /\
(has_tallied inc_calls = true ->
tally cstate = Some (sumnat (fun party => if svi_sv (parties party) then 1 else 0)%nat
(map fst (signups inc_calls)))))).
Proof.
contract_induction; intros.
- [AddBlockFacts]: exact (fun _ old_slot _ _ new_slot _ => old_slot < new_slot).
subst AddBlockFacts.
cbn in facts.
destruct_and_split; try tauto.
+ eauto with lia.
+ intros; eauto with lia.
- cbn -[Nat.ltb] in *.
destruct (_ <? _) eqn:ltb; [|congruence].
apply Nat.ltb_lt in ltb.
inversion_clear init_some.
cbn.
split; auto.
split; auto.
split; [symmetry; apply FMap.size_empty|].
split; [auto|].
pose proof order_ge_2.
split; [lia|].
intros _ _ _.
unfold FMap.keys.
unfold AddressMap.empty in *.
rewrite @FMap.elements_empty.
split; [auto|].
split; [auto|].
split; [|easy].
intros ? ? find.
now rewrite @FMap.find_empty in find.
- auto.
- cbn -[Nat.ltb] in receive_some.
destruct msg as [msg|]; cbn -[Nat.ltb] in *; [|congruence].
destruct msg.
unfold AddressMap.add in *. unfold AddressMap.find in *.
+ (* signup *)
destruct (_ <? _)%nat eqn:intime in receive_some; cbn -[Nat.ltb] in *; [congruence|].
apply Nat.ltb_ge in intime.
destruct (FMap.find _ _) in receive_some; cbn in *; [|congruence].
destruct (FMap.find _ _) eqn:new in receive_some; cbn in *; [congruence|].
destruct (_ =? _)%Z in receive_some; cbn in *; [|congruence].
destruct (_ <? _)%Z eqn:lt in receive_some; cbn in *; [|congruence].
destruct (verify_secret_key_proof _ _ _) eqn:verify_zkp in receive_some;
cbn in *; [|congruence].
inversion_clear receive_some.
cbn.
split; [lia|].
split; [tauto|].
split.
{ rewrite app_length, FMap.size_add_new by auto; cbn; lia. }
apply Z.ltb_lt in lt.
rewrite app_length in *.
cbn.
fold (has_tallied prev_inc_calls).
fold (signups prev_inc_calls).
rewrite app_length, map_app; cbn.
split; [destruct_and_split; congruence|].
split; [lia|].
intros [signup_assum msg_assum] order_assum num_signups_assum.
destruct IH as (reg_lt & cur_lt & _ & pks_signups & _ & IH).
unshelve epose proof (IH _ _ _) as IH.
* auto.
* rewrite seq_app in order_assum.
rewrite zip_app in order_assum by (now rewrite seq_length).
apply All_app in order_assum.
tauto.
* intros.
lia.
* split.
{ destruct IH as (perm & _).
cbn.
rewrite FMap.elements_add by auto.
cbn.
rewrite seq_app.
cbn.
perm_simplify. }
split.
{ destruct IH as (_ & perm & _).
rewrite map_app.
unfold FMap.keys.
rewrite FMap.elements_add by auto.
cbn.
now perm_simplify. }
split; cycle 1.
{
split; [easy|].
intros tallied.
specialize (cur_lt ltac:(lia)).
congruence.
}
intros addr inf find_add.
destruct (address_eqb_spec addr (ctx_from ctx)) as [->|].
-- rewrite (FMap.find_add (ctx_from ctx)) in find_add.
inversion_clear find_add.
cbn.
unfold make_signup_msg in signup_assum.
rewrite nth_error_snoc.
rewrite seq_app, zip_app in order_assum by (now rewrite seq_length).
apply All_app in order_assum.
cbn in order_assum.
destruct order_assum as [_ []].
split; [lia|].
rewrite pks_signups, map_length.
split; [symmetry; tauto|].
split; [congruence|].
left; easy.
-- rewrite FMap.find_add_ne in find_add by auto.
destruct IH as (_ & _ & IH & _).
specialize (IH _ _ find_add).
split; [lia|].
now rewrite nth_error_app1 by lia.
+ (* commit_to_vote *)
destruct (finish_commit_by _); cbn -[Nat.ltb] in *; [|congruence].
destruct (_ <? _); cbn in *; [congruence|].
unfold AddressMap.find in *.
destruct (FMap.find _ _) eqn:found; cbn in *; [|congruence].
inversion_clear receive_some; cbn.
split; [lia|].
split; [tauto|].
split.
unfold AddressMap.add.
{ rewrite FMap.size_add_existing by congruence; tauto. }
split; [tauto|].
split; [tauto|].
intros [_ msg_assum] order_assum num_signups_assum.
destruct IH as (_ & _ & len_pks & _ & _ & IH).
specialize (IH msg_assum order_assum num_signups_assum).
setoid_rewrite (FMap.keys_already _ _ _ _ found).
split.
{
destruct IH as (perm & _).
rewrite len_pks in *.
apply Permutation_modify with (vold := v); auto.
}
split; [tauto|].
split; [|tauto].
intros addr inf find_add.
unfold AddressMap.add in *.
destruct IH as (_ & _ & IH & _).
destruct (address_eqb_spec addr (ctx_from ctx)) as [->|].
* rewrite FMap.find_add in find_add.
inversion_clear find_add; cbn.
auto.
* rewrite FMap.find_add_ne in find_add by auto.
auto.
+ (* submit_vote *)
destruct (_ <? _); cbn -[Nat.ltb] in *; [congruence|].
unfold AddressMap.find in *.
destruct (FMap.find _ _) eqn:found; cbn in *; [|congruence].
destruct (if finish_commit_by _ then _ else _); cbn in *; [|congruence].
destruct (verify_secret_vote_proof _ _ _ _); cbn in *; [|congruence].
inversion_clear receive_some; cbn.
split; [lia|].
split; [tauto|].
rewrite FMap.size_add_existing by congruence.
split; [tauto|].
split; [tauto|].
split; [tauto|].
intros [vote_assum msg_assum] order_assum num_signups_assum.
destruct IH as (_ & _ & len_pks & _ & _ & IH).
specialize (IH msg_assum order_assum num_signups_assum).
setoid_rewrite (FMap.keys_already _ _ _ _ found).
split.
{ destruct IH as (perm & _).
rewrite len_pks in *.
apply Permutation_modify with (vold := v0); auto. }
split; [tauto|].
split; [|tauto].
intros addr inf find_add.
destruct IH as (_ & _ & IH & _).
destruct (address_eqb_spec addr (ctx_from ctx)) as [->|].
* rewrite FMap.find_add in find_add.
inversion_clear find_add; cbn.
specialize (IH _ _ found).
repeat split; try tauto.
right.
unfold make_vote_msg in *.
inversion vote_assum.
destruct_hyps.
replace (svi_index (parties (ctx_from ctx))) with (voter_index v0) by congruence.
easy.
* rewrite FMap.find_add_ne in find_add by auto.
auto.
+ (* tally_votes *)
destruct (_ <? _) eqn:intime; cbn in *; [congruence|].
destruct (tally prev_state); cbn in *; [congruence|].
destruct (existsb _ _) eqn:all_voted; cbn in *; [congruence|].
destruct (bruteforce_tally _) eqn:bruteforce; cbn -[Nat.ltb] in *; [|congruence].
inversion_clear receive_some; cbn.
apply Nat.ltb_ge in intime.
split; [lia|].
split; [intros; lia|].
split; [tauto|].
split; [tauto|].
split; [tauto|].
intros (_ & msg_assum) order_assum num_signups_assum.
split; [tauto|].
split; [tauto|].
split; [tauto|].
split; [easy|].
intros _.
apply f_equal.
destruct IH as (finish_before_vote & _ & len_pks & pks_signups & party_count & IH).
specialize (IH msg_assum order_assum num_signups_assum).
destruct IH as (perm & perm' & addrs & _).
unfold AddressMap.values in *.
unfold FMap.values in bruteforce.
rewrite map_map in bruteforce.
rewrite (map_ext_in _ (fun '(_, v) => public_vote v)) in bruteforce
by (now intros []).
rewrite (bruteforce_tally_correct
(FMap.elements (registered_voters prev_state))
(fun '(_, v) => voter_index v)
(fun '(addr, _) => svi_sk (parties addr))
(public_keys prev_state)
(fun kvp => svi_sv (parties (fst kvp)))
(fun '(_, v) => public_vote v)) in bruteforce.
* inversion bruteforce.
rewrite <- (sumnat_map fst (fun a => if svi_sv (parties a) then 1 else 0))%nat.
now setoid_rewrite perm'.
* now rewrite FMap.length_elements, <- len_pks.
* now rewrite FMap.length_elements, <- len_pks.
* now rewrite FMap.length_elements, <- len_pks.
* intros [k v] kvpin.
apply FMap.In_elements in kvpin.
specialize (addrs _ _ kvpin).
tauto.
* intros [k v] kvpin.
rewrite existsb_forallb in all_voted.
apply Bool.negb_false_iff in all_voted.
rewrite forallb_forall in all_voted.
unshelve epose proof (all_voted v _) as all_voted.
{
apply in_map_iff.
exists (k, v).
tauto.
}
apply Bool.negb_true_iff in all_voted.
destruct (elmeqb_spec (public_vote v) zero); [congruence|].
apply FMap.In_elements in kvpin.
specialize (addrs _ _ kvpin).
cbn.
destruct addrs as (_ & _ & _ & []); [easy|].
fold (signups prev_inc_calls) (SignupOrderAssumption pks parties prev_inc_calls) in *.
rewrite pks_signups.
specialize (num_signups_assum ltac:(lia)).
now rewrite (all_signups pks parties) by auto.
- [CallFacts]: exact (fun _ ctx _ _ _ => ctx_from ctx <> ctx_contract_address ctx).
subst CallFacts; cbn in *; congruence.
- auto.
- [DeployFacts]: exact (fun _ _ => True).
unset_all; subst; cbn in *.
destruct_chain_step; auto.
+ destruct valid_header; auto.
+ destruct_action_eval; auto.
intros.
apply trace_reachable in from_reachable.
pose proof (no_outgoing _ _ from_reachable H0).
unfold outgoing_acts in H2.
rewrite queue_prev in H2.
cbn in H2.
destruct (address_eqb_spec (act_from act) to_addr); cbn in *; try congruence.
subst.
cbn in *. congruence.
Qed.
Theorem boardroom_voting_correct
(bstate : ChainState)
(caddr : Address)
(trace : ChainTrace empty_state bstate)
(* list of all public keys, in the order of signups *)
(pks : list A)
(* function mapping a party to information about him *)
(parties : Address -> SecretVoterInfo) :
env_contracts bstate caddr = Some (boardroom_voting : WeakContract) ->
exists (cstate : State)
(depinfo : DeploymentInfo Setup)
(inc_calls : list (ContractCallInfo Msg)),
deployment_info Setup trace caddr = Some depinfo /\
contract_state bstate caddr = Some cstate /\
incoming_calls Msg trace caddr = Some inc_calls /\
(* assuming that the message sent were created with the
functions provided by this smart contract *)
MsgAssumption pks parties inc_calls ->
(* ..and that people signed up in the order given by 'index'
and 'pks' *)
SignupOrderAssumption pks parties inc_calls ->
(* ..and that the correct number of people register *)
(finish_registration_by (setup cstate) < Blockchain.current_slot bstate ->
length pks = length (signups inc_calls)) ->
(* then if we have not tallied yet, the tally is none *)
((has_tallied inc_calls = false -> tally cstate = None) /\
(* or if we have tallied yet, the tally is correct *)
(has_tallied inc_calls = true ->
tally cstate = Some (sumnat (fun party => if svi_sv (parties party) then 1 else 0)%nat
(map fst (signups inc_calls))))).
Proof.
intros deployed.
destruct (boardroom_voting_correct_strong bstate caddr trace parties pks deployed)
as (cstate & depinfo & inc_calls & P).
exists cstate, depinfo, inc_calls.
tauto.
Qed.
End Theories.
End BoardroomVoting.