forked from stonecoldpat/anonymousvoting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnonymousVoting.sol
executable file
·1279 lines (1072 loc) · 41.8 KB
/
AnonymousVoting.sol
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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pragma solidity ^0.4.3;
/**
* @title ECCMath
*
* Functions for working with integers, curve-points, etc.
*
* @author Andreas Olofsson ([email protected])
*/
library ECCMath {
/// @dev Modular inverse of a (mod p) using euclid.
/// "a" and "p" must be co-prime.
/// @param a The number.
/// @param p The mmodulus.
/// @return x such that ax = 1 (mod p)
function invmod(uint a, uint p) internal constant returns (uint) {
if (a == 0 || a == p || p == 0)
throw;
if (a > p)
a = a % p;
int t1;
int t2 = 1;
uint r1 = p;
uint r2 = a;
uint q;
while (r2 != 0) {
q = r1 / r2;
(t1, t2, r1, r2) = (t2, t1 - int(q) * t2, r2, r1 - q * r2);
}
if (t1 < 0)
return (p - uint(-t1));
return uint(t1);
}
/// @dev Modular exponentiation, b^e % m
/// Basically the same as can be found here:
/// https://github.com/ethereum/serpent/blob/develop/examples/ecc/modexp.se
/// @param b The base.
/// @param e The exponent.
/// @param m The modulus.
/// @return x such that x = b**e (mod m)
function expmod(uint b, uint e, uint m) internal constant returns (uint r) {
if (b == 0)
return 0;
if (e == 0)
return 1;
if (m == 0)
throw;
r = 1;
uint bit = 2 ** 255;
bit = bit;
assembly {
loop:
jumpi(end, iszero(bit))
r := mulmod(mulmod(r, r, m), exp(b, iszero(iszero(and(e, bit)))), m)
r := mulmod(mulmod(r, r, m), exp(b, iszero(iszero(and(e, div(bit, 2))))), m)
r := mulmod(mulmod(r, r, m), exp(b, iszero(iszero(and(e, div(bit, 4))))), m)
r := mulmod(mulmod(r, r, m), exp(b, iszero(iszero(and(e, div(bit, 8))))), m)
bit := div(bit, 16)
jump(loop)
end:
}
}
/// @dev Converts a point (Px, Py, Pz) expressed in Jacobian coordinates to (Px", Py", 1).
/// Mutates P.
/// @param P The point.
/// @param zInv The modular inverse of "Pz".
/// @param z2Inv The square of zInv
/// @param prime The prime modulus.
/// @return (Px", Py", 1)
function toZ1(uint[3] memory P, uint zInv, uint z2Inv, uint prime) internal constant {
P[0] = mulmod(P[0], z2Inv, prime);
P[1] = mulmod(P[1], mulmod(zInv, z2Inv, prime), prime);
P[2] = 1;
}
/// @dev See _toZ1(uint[3], uint, uint).
/// Warning: Computes a modular inverse.
/// @param PJ The point.
/// @param prime The prime modulus.
/// @return (Px", Py", 1)
function toZ1(uint[3] PJ, uint prime) internal constant {
uint zInv = invmod(PJ[2], prime);
uint zInv2 = mulmod(zInv, zInv, prime);
PJ[0] = mulmod(PJ[0], zInv2, prime);
PJ[1] = mulmod(PJ[1], mulmod(zInv, zInv2, prime), prime);
PJ[2] = 1;
}
}
library Secp256k1 {
// TODO separate curve from crypto primitives?
// Field size
uint constant pp = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F;
// Base point (generator) G
uint constant Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798;
uint constant Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8;
// Order of G
uint constant nn = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141;
// Cofactor
// uint constant hh = 1;
// Maximum value of s
uint constant lowSmax = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0;
// For later
// uint constant lambda = "0x5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72";
// uint constant beta = "0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee";
/// @dev See Curve.onCurve
function onCurve(uint[2] P) internal constant returns (bool) {
uint p = pp;
if (0 == P[0] || P[0] == p || 0 == P[1] || P[1] == p)
return false;
uint LHS = mulmod(P[1], P[1], p);
uint RHS = addmod(mulmod(mulmod(P[0], P[0], p), P[0], p), 7, p);
return LHS == RHS;
}
/// @dev See Curve.isPubKey
function isPubKey(uint[2] memory P) internal constant returns (bool isPK) {
isPK = onCurve(P);
}
/// @dev See Curve.isPubKey
// TODO: We assume we are given affine co-ordinates for now
function isPubKey(uint[3] memory P) internal constant returns (bool isPK) {
uint[2] memory a_P;
a_P[0] = P[0];
a_P[1] = P[1];
isPK = onCurve(a_P);
}
/// @dev See Curve.validateSignature
function validateSignature(bytes32 message, uint[2] rs, uint[2] Q) internal constant returns (bool) {
uint n = nn;
uint p = pp;
if(rs[0] == 0 || rs[0] >= n || rs[1] == 0 || rs[1] > lowSmax)
return false;
if (!isPubKey(Q))
return false;
uint sInv = ECCMath.invmod(rs[1], n);
uint[3] memory u1G = _mul(mulmod(uint(message), sInv, n), [Gx, Gy]);
uint[3] memory u2Q = _mul(mulmod(rs[0], sInv, n), Q);
uint[3] memory P = _add(u1G, u2Q);
if (P[2] == 0)
return false;
uint Px = ECCMath.invmod(P[2], p); // need Px/Pz^2
Px = mulmod(P[0], mulmod(Px, Px, p), p);
return Px % n == rs[0];
}
/// @dev See Curve.compress
function compress(uint[2] P) internal constant returns (uint8 yBit, uint x) {
x = P[0];
yBit = P[1] & 1 == 1 ? 1 : 0;
}
/// @dev See Curve.decompress
function decompress(uint8 yBit, uint x) internal constant returns (uint[2] P) {
uint p = pp;
var y2 = addmod(mulmod(x, mulmod(x, x, p), p), 7, p);
var y_ = ECCMath.expmod(y2, (p + 1) / 4, p);
uint cmp = yBit ^ y_ & 1;
P[0] = x;
P[1] = (cmp == 0) ? y_ : p - y_;
}
// Point addition, P + Q
// inData: Px, Py, Pz, Qx, Qy, Qz
// outData: Rx, Ry, Rz
function _add(uint[3] memory P, uint[3] memory Q) internal constant returns (uint[3] memory R) {
if(P[2] == 0)
return Q;
if(Q[2] == 0)
return P;
uint p = pp;
uint[4] memory zs; // Pz^2, Pz^3, Qz^2, Qz^3
zs[0] = mulmod(P[2], P[2], p);
zs[1] = mulmod(P[2], zs[0], p);
zs[2] = mulmod(Q[2], Q[2], p);
zs[3] = mulmod(Q[2], zs[2], p);
uint[4] memory us = [
mulmod(P[0], zs[2], p),
mulmod(P[1], zs[3], p),
mulmod(Q[0], zs[0], p),
mulmod(Q[1], zs[1], p)
]; // Pu, Ps, Qu, Qs
if (us[0] == us[2]) {
if (us[1] != us[3])
return;
else {
return _double(P);
}
}
uint h = addmod(us[2], p - us[0], p);
uint r = addmod(us[3], p - us[1], p);
uint h2 = mulmod(h, h, p);
uint h3 = mulmod(h2, h, p);
uint Rx = addmod(mulmod(r, r, p), p - h3, p);
Rx = addmod(Rx, p - mulmod(2, mulmod(us[0], h2, p), p), p);
R[0] = Rx;
R[1] = mulmod(r, addmod(mulmod(us[0], h2, p), p - Rx, p), p);
R[1] = addmod(R[1], p - mulmod(us[1], h3, p), p);
R[2] = mulmod(h, mulmod(P[2], Q[2], p), p);
}
// Point addition, P + Q. P Jacobian, Q affine.
// inData: Px, Py, Pz, Qx, Qy
// outData: Rx, Ry, Rz
function _addMixed(uint[3] memory P, uint[2] memory Q) internal constant returns (uint[3] memory R) {
if(P[2] == 0)
return [Q[0], Q[1], 1];
if(Q[1] == 0)
return P;
uint p = pp;
uint[2] memory zs; // Pz^2, Pz^3, Qz^2, Qz^3
zs[0] = mulmod(P[2], P[2], p);
zs[1] = mulmod(P[2], zs[0], p);
uint[4] memory us = [
P[0],
P[1],
mulmod(Q[0], zs[0], p),
mulmod(Q[1], zs[1], p)
]; // Pu, Ps, Qu, Qs
if (us[0] == us[2]) {
if (us[1] != us[3]) {
P[0] = 0;
P[1] = 0;
P[2] = 0;
return;
}
else {
_double(P);
return;
}
}
uint h = addmod(us[2], p - us[0], p);
uint r = addmod(us[3], p - us[1], p);
uint h2 = mulmod(h, h, p);
uint h3 = mulmod(h2, h, p);
uint Rx = addmod(mulmod(r, r, p), p - h3, p);
Rx = addmod(Rx, p - mulmod(2, mulmod(us[0], h2, p), p), p);
R[0] = Rx;
R[1] = mulmod(r, addmod(mulmod(us[0], h2, p), p - Rx, p), p);
R[1] = addmod(R[1], p - mulmod(us[1], h3, p), p);
R[2] = mulmod(h, P[2], p);
}
// Same as addMixed but params are different and mutates P.
function _addMixedM(uint[3] memory P, uint[2] memory Q) internal constant {
if(P[1] == 0) {
P[0] = Q[0];
P[1] = Q[1];
P[2] = 1;
return;
}
if(Q[1] == 0)
return;
uint p = pp;
uint[2] memory zs; // Pz^2, Pz^3, Qz^2, Qz^3
zs[0] = mulmod(P[2], P[2], p);
zs[1] = mulmod(P[2], zs[0], p);
uint[4] memory us = [
P[0],
P[1],
mulmod(Q[0], zs[0], p),
mulmod(Q[1], zs[1], p)
]; // Pu, Ps, Qu, Qs
if (us[0] == us[2]) {
if (us[1] != us[3]) {
P[0] = 0;
P[1] = 0;
P[2] = 0;
return;
}
else {
_doubleM(P);
return;
}
}
uint h = addmod(us[2], p - us[0], p);
uint r = addmod(us[3], p - us[1], p);
uint h2 = mulmod(h, h, p);
uint h3 = mulmod(h2, h, p);
uint Rx = addmod(mulmod(r, r, p), p - h3, p);
Rx = addmod(Rx, p - mulmod(2, mulmod(us[0], h2, p), p), p);
P[0] = Rx;
P[1] = mulmod(r, addmod(mulmod(us[0], h2, p), p - Rx, p), p);
P[1] = addmod(P[1], p - mulmod(us[1], h3, p), p);
P[2] = mulmod(h, P[2], p);
}
// Point doubling, 2*P
// Params: Px, Py, Pz
// Not concerned about the 1 extra mulmod.
function _double(uint[3] memory P) internal constant returns (uint[3] memory Q) {
uint p = pp;
if (P[2] == 0)
return;
uint Px = P[0];
uint Py = P[1];
uint Py2 = mulmod(Py, Py, p);
uint s = mulmod(4, mulmod(Px, Py2, p), p);
uint m = mulmod(3, mulmod(Px, Px, p), p);
var Qx = addmod(mulmod(m, m, p), p - addmod(s, s, p), p);
Q[0] = Qx;
Q[1] = addmod(mulmod(m, addmod(s, p - Qx, p), p), p - mulmod(8, mulmod(Py2, Py2, p), p), p);
Q[2] = mulmod(2, mulmod(Py, P[2], p), p);
}
// Same as double but mutates P and is internal only.
function _doubleM(uint[3] memory P) internal constant {
uint p = pp;
if (P[2] == 0)
return;
uint Px = P[0];
uint Py = P[1];
uint Py2 = mulmod(Py, Py, p);
uint s = mulmod(4, mulmod(Px, Py2, p), p);
uint m = mulmod(3, mulmod(Px, Px, p), p);
var PxTemp = addmod(mulmod(m, m, p), p - addmod(s, s, p), p);
P[0] = PxTemp;
P[1] = addmod(mulmod(m, addmod(s, p - PxTemp, p), p), p - mulmod(8, mulmod(Py2, Py2, p), p), p);
P[2] = mulmod(2, mulmod(Py, P[2], p), p);
}
// Multiplication dP. P affine, wNAF: w=5
// Params: d, Px, Py
// Output: Jacobian Q
function _mul(uint d, uint[2] memory P) internal constant returns (uint[3] memory Q) {
uint p = pp;
if (d == 0) // TODO
return;
uint dwPtr; // points to array of NAF coefficients.
uint i;
// wNAF
assembly
{
let dm := 0
dwPtr := mload(0x40)
mstore(0x40, add(dwPtr, 512)) // Should lower this.
loop:
jumpi(loop_end, iszero(d))
jumpi(even, iszero(and(d, 1)))
dm := mod(d, 32)
mstore8(add(dwPtr, i), dm) // Don"t store as signed - convert when reading.
d := add(sub(d, dm), mul(gt(dm, 16), 32))
even:
d := div(d, 2)
i := add(i, 1)
jump(loop)
loop_end:
}
dwPtr = dwPtr;
// Pre calculation
uint[3][8] memory PREC; // P, 3P, 5P, 7P, 9P, 11P, 13P, 15P
PREC[0] = [P[0], P[1], 1];
var X = _double(PREC[0]);
PREC[1] = _addMixed(X, P);
PREC[2] = _add(X, PREC[1]);
PREC[3] = _add(X, PREC[2]);
PREC[4] = _add(X, PREC[3]);
PREC[5] = _add(X, PREC[4]);
PREC[6] = _add(X, PREC[5]);
PREC[7] = _add(X, PREC[6]);
uint[16] memory INV;
INV[0] = PREC[1][2]; // a1
INV[1] = mulmod(PREC[2][2], INV[0], p); // a2
INV[2] = mulmod(PREC[3][2], INV[1], p); // a3
INV[3] = mulmod(PREC[4][2], INV[2], p); // a4
INV[4] = mulmod(PREC[5][2], INV[3], p); // a5
INV[5] = mulmod(PREC[6][2], INV[4], p); // a6
INV[6] = mulmod(PREC[7][2], INV[5], p); // a7
INV[7] = ECCMath.invmod(INV[6], p); // a7inv
INV[8] = INV[7]; // aNinv (a7inv)
INV[15] = mulmod(INV[5], INV[8], p); // z7inv
for(uint k = 6; k >= 2; k--) { // z6inv to z2inv
INV[8] = mulmod(PREC[k + 1][2], INV[8], p);
INV[8 + k] = mulmod(INV[k - 2], INV[8], p);
}
INV[9] = mulmod(PREC[2][2], INV[8], p); // z1Inv
for(k = 0; k < 7; k++) {
ECCMath.toZ1(PREC[k + 1], INV[k + 9], mulmod(INV[k + 9], INV[k + 9], p), p);
}
// Mult loop
while(i > 0) {
uint dj;
uint pIdx;
i--;
assembly {
dj := byte(0, mload(add(dwPtr, i)))
}
_doubleM(Q);
if (dj > 16) {
pIdx = (31 - dj) / 2; // These are the "negative ones", so invert y.
_addMixedM(Q, [PREC[pIdx][0], p - PREC[pIdx][1]]);
}
else if (dj > 0) {
pIdx = (dj - 1) / 2;
_addMixedM(Q, [PREC[pIdx][0], PREC[pIdx][1]]);
}
}
}
}
contract owned {
address public owner;
/* Initialise contract creator as owner */
function owned() {
owner = msg.sender;
}
/* Function to dictate that only the designated owner can call a function */
modifier onlyOwner {
if(owner != msg.sender) throw;
_;
}
/* Transfer ownership of this contract to someone else */
function transferOwnership(address newOwner) onlyOwner() {
owner = newOwner;
}
}
/*
* @title AnonymousVoting
* Open Vote Network
* A self-talling protocol that supports voter privacy.
*
* Author: Patrick McCorry
*/
contract AnonymousVoting is owned {
// Modulus for public keys
uint constant pp = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F;
// Base point (generator) G
uint constant Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798;
uint constant Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8;
// Modulus for private keys (sub-group)
uint constant nn = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141;
uint[2] G;
//Every address has an index
//This makes looping in the program easier.
address[] public addresses;
mapping (address => uint) public addressid; // Address to Counter
mapping (uint => Voter) public voters;
mapping (address => bool) public eligible; // White list of addresses allowed to vote
mapping (address => bool) public registered; // Address registered?
mapping (address => bool) public votecast; // Address voted?
mapping (address => bool) public commitment; // Have we received their commitment?
mapping (address => uint) public refunds; // Have we received their commitment?
struct Voter {
address addr;
uint[2] registeredkey;
uint[2] reconstructedkey;
bytes32 commitment;
uint[2] vote;
}
// Work around function to fetch details about a voter
function getVoter() returns (uint[2] _registeredkey, uint[2] _reconstructedkey, bytes32 _commitment){
uint index = addressid[msg.sender];
_registeredkey = voters[index].registeredkey;
_reconstructedkey = voters[index].reconstructedkey;
_commitment = voters[index].commitment;
}
// List of timers that each phase MUST end by an explicit time in UNIX timestamp.
// Ethereum works in SECONDS. Not milliseconds.
uint public finishSignupPhase; // Election Authority to transition to next phase.
uint public endSignupPhase; // Election Authority does not transition to next phase by this time.
uint public endCommitmentPhase; // Voters have not sent their commitments in by this time.
uint public endVotingPhase; // Voters have not submitted their vote by this stage.
uint public endRefundPhase; // Voters must claim their refund by this stage.
uint public totalregistered; //Total number of participants that have submited a voting key
uint public totaleligible;
uint public totalcommitted;
uint public totalvoted;
uint public totalrefunded;
uint public totaltorefund;
string public question;
uint[2] public finaltally; // Final tally
bool public commitmentphase; // OPTIONAL phase.
uint public depositrequired;
uint public gap; // Minimum amount of time between time stamps.
address public charity;
// TODO: Why cant election authority receive the spoils?
uint public lostdeposit; // This money is collected from non active voters...
enum State { SETUP, SIGNUP, COMMITMENT, VOTE, FINISHED }
State public state;
modifier inState(State s) {
if(state != s) {
throw;
}
_;
}
// 2 round anonymous voting protocol
// TODO: Right now due to gas limits there is an upper limit
// on the number of participants that we can have voting...
// I need to split the functions up... so if they cannot
// finish their entire workload in 1 transaction, then
// it does the maximum. This way we can chain transactions
// to complete the job...
function AnonymousVoting(uint _gap, address _charity) {
G[0] = Gx;
G[1] = Gy;
state = State.SETUP;
question = "No question set";
gap = _gap; // Minimum gap period between stages
charity = _charity;
}
// Owner of contract sets a whitelist of addresses that are eligible to vote.
function setEligible(address[] addr) onlyOwner {
// We can only handle up 50 people at the moment.
if(totaleligible > 50) {
throw;
}
// Sign up the addresses
for(uint i=0; i<addr.length; i++) {
if(!eligible[addr[i]]) {
eligible[addr[i]] = true;
addresses.push(addr[i]);
totaleligible += 1;
}
}
}
// Owner of contract declares that eligible addresses begin round 1 of the protocol
// Time is the number of 'blocks' we must wait until we can move onto round 2.
function beginSignUp(string _question, bool enableCommitmentPhase, uint _finishSignupPhase, uint _endSignupPhase, uint _endCommitmentPhase, uint _endVotingPhase, uint _endRefundPhase, uint _depositrequired) inState(State.SETUP) onlyOwner payable returns (bool){
// We have lots of timers. let's explain each one
// _finishSignUpPhase - Voters should be signed up before this timer
// Voter is refunded if any of the timers expire:
// _endSignUpPhase - Election Authority never finished sign up phase
// _endCommitmentPhase - One or more voters did not send their commitments in time
// _endVotingPhase - One or more voters did not send their votes in time
// _endRefundPhase - Provide time for voters to get their money back.
// Why is there no endTally? Because anyone can call it!
// Represented in UNIX time...
// TODO: Set to block timestamp...
// TODO: Enforce gap to be at least 1 hour.. may break unit testing
// Make sure 3 people are at least eligible to vote..
// Deposit can be zero or more WEI
if(_finishSignupPhase > 0 + gap && addresses.length >= 3 && _depositrequired >= 0) {
// Ensure each time phase finishes in the future...
// Ensure there is a gap of 'x time' between each phase.
if(_endSignupPhase-gap < _finishSignupPhase) {
return false;
}
// We need to check Commitment timestamps if phase is enabled.
if(enableCommitmentPhase) {
// Make sure there is a gap between 'end of registration' and 'end of commitment' phases.
if(_endCommitmentPhase-gap < _endSignupPhase) {
return false;
}
// Make sure there is a gap between 'end of commitment' and 'end of vote' phases.
if(_endVotingPhase-gap < _endCommitmentPhase) {
return false;
}
} else {
// We have no commitment phase.
// Make sure there is a gap between 'end of registration' and 'end of vote' phases.
if(_endVotingPhase-gap < _endSignupPhase) {
return false;
}
}
// Provide time for people to get a refund once the voting phase has ended.
if(_endRefundPhase-gap < _endVotingPhase) {
return false;
}
// Require Election Authority to deposit ether.
if(msg.value != _depositrequired) {
return false;
}
// Store the election authority's deposit
// Note: This deposit is only lost if the
// election authority does not begin the election
// or call the tally function before the timers expire.
refunds[msg.sender] = msg.value;
// All time stamps are reasonable.
// We can now begin the signup phase.
state = State.SIGNUP;
// All timestamps should be in UNIX..
finishSignupPhase = _finishSignupPhase;
endSignupPhase = _endSignupPhase;
endCommitmentPhase = _endCommitmentPhase;
endVotingPhase = _endVotingPhase;
endRefundPhase = _endRefundPhase;
question = _question;
commitmentphase = enableCommitmentPhase;
depositrequired = _depositrequired; // Deposit required from all voters
return true;
}
return false;
}
// This function determines if one of the deadlines have been missed
// If a deadline has been missed - then we finish the election,
// and allocate refunds to the correct people depending on the situation.
function deadlinePassed() returns (bool){
uint refund = 0;
// Has the Election Authority missed the signup deadline?
// Election Authority will forfeit his deposit.
if(state == State.SIGNUP && block.timestamp > endSignupPhase) {
// Nothing to do. All voters are refunded.
state = State.FINISHED;
totaltorefund = totalregistered;
// Election Authority forfeits his deposit...
// If 3 or more voters had signed up...
if(addresses.length >= 3) {
// Election Authority forfeits deposit
refund = refunds[owner];
refunds[owner] = 0;
lostdeposit = lostdeposit + refund;
}
return true;
}
// Has a voter failed to send their commitment?
// Election Authority DOES NOT forgeit his deposit.
if(state == State.COMMITMENT && block.timestamp > endCommitmentPhase) {
// Check which voters have not sent their commitment
for(uint i=0; i<totalregistered; i++) {
// Voters forfeit their deposit if failed to send a commitment
if(!commitment[voters[i].addr]) {
refund = refunds[voters[i].addr];
refunds[voters[i].addr] = 0;
lostdeposit = lostdeposit + refund;
} else {
// We will need to refund this person.
totaltorefund = totaltorefund + 1;
}
}
state = State.FINISHED;
return true;
}
// Has a voter failed to send in their vote?
// Eletion Authority does NOT forfeit his deposit.
if(state == State.VOTE && block.timestamp > endVotingPhase) {
// Check which voters have not cast their vote
for(i=0; i<totalregistered; i++) {
// Voter forfeits deposit if they have not voted.
if(!votecast[voters[i].addr]) {
refund = refunds[voters[i].addr];
refunds[voters[i].addr] = 0;
lostdeposit = lostdeposit + refund;
} else {
// Lets make sure refund has not already been issued...
if(refunds[voters[i].addr] > 0) {
// We will need to refund this person.
totaltorefund = totaltorefund + 1;
}
}
}
state = State.FINISHED;
return true;
}
// Has the deadline passed for voters to claim their refund?
// Only owner can call. Owner must be refunded (or forfeited).
// Refund period is over or everyone has already been refunded.
if(state == State.FINISHED && msg.sender == owner && refunds[owner] == 0 && (block.timestamp > endRefundPhase || totaltorefund == totalrefunded)) {
// Collect all unclaimed refunds. We will send it to charity.
for(i=0; i<totalregistered; i++) {
refund = refunds[voters[i].addr];
refunds[voters[i].addr] = 0;
lostdeposit = lostdeposit + refund;
}
uint[2] memory empty;
for(i=0; i<addresses.length; i++) {
address addr = addresses[i];
eligible[addr] = false; // No longer eligible
registered[addr] = false; // Remove voting registration
voters[i] = Voter({addr: 0, registeredkey: empty, reconstructedkey: empty, vote: empty, commitment: 0});
addressid[addr] = 0; // Remove index
votecast[addr] = false; // Remove that vote was cast
commitment[addr] = false;
}
// Reset timers.
finishSignupPhase = 0;
endSignupPhase = 0;
endCommitmentPhase = 0;
endVotingPhase = 0;
endRefundPhase = 0;
delete addresses;
// Keep track of voter activity
totalregistered = 0;
totaleligible = 0;
totalcommitted = 0;
totalvoted = 0;
// General values that need reset
question = "No question set";
finaltally[0] = 0;
finaltally[1] = 0;
commitmentphase = false;
depositrequired = 0;
totalrefunded = 0;
totaltorefund = 0;
state = State.SETUP;
return true;
}
// No deadlines have passed...
return false;
}
// Called by participants to register their voting public key
// Participant mut be eligible, and can only register the first key sent key.
function register(uint[2] xG, uint[3] vG, uint r) inState(State.SIGNUP) payable returns (bool) {
// HARD DEADLINE
if(block.timestamp > finishSignupPhase) {
throw; // throw returns the voter's ether, but exhausts their gas.
}
// Make sure the ether being deposited matches what we expect.
if(msg.value != depositrequired) {
return false;
}
// Only white-listed addresses can vote
if(eligible[msg.sender]) {
if(verifyZKP(xG,r,vG) && !registered[msg.sender]) {
// Store deposit
refunds[msg.sender] = msg.value;
// Update voter's registration
uint[2] memory empty;
addressid[msg.sender] = totalregistered;
voters[totalregistered] = Voter({addr: msg.sender, registeredkey: xG, reconstructedkey: empty, vote: empty, commitment: 0});
registered[msg.sender] = true;
totalregistered += 1;
return true;
}
}
return false;
}
// Timer has expired - we want to start computing the reconstructed keys
function finishRegistrationPhase() inState(State.SIGNUP) onlyOwner returns(bool) {
// Make sure at least 3 people have signed up...
if(totalregistered < 3) {
return;
}
// We can only compute the public keys once participants
// have been given an opportunity to register their
// voting public key.
if(block.timestamp < finishSignupPhase) {
return;
}
// Election Authority has a deadline to begin election
if(block.timestamp > endSignupPhase) {
return;
}
uint[2] memory temp;
uint[3] memory yG;
uint[3] memory beforei;
uint[3] memory afteri;
// Step 1 is to compute the index 1 reconstructed key
afteri[0] = voters[1].registeredkey[0];
afteri[1] = voters[1].registeredkey[1];
afteri[2] = 1;
for(uint i=2; i<totalregistered; i++) {
Secp256k1._addMixedM(afteri, voters[i].registeredkey);
}
ECCMath.toZ1(afteri,pp);
voters[0].reconstructedkey[0] = afteri[0];
voters[0].reconstructedkey[1] = pp - afteri[1];
// Step 2 is to add to beforei, and subtract from afteri.
for(i=1; i<totalregistered; i++) {
if(i==1) {
beforei[0] = voters[0].registeredkey[0];
beforei[1] = voters[0].registeredkey[1];
beforei[2] = 1;
} else {
Secp256k1._addMixedM(beforei, voters[i-1].registeredkey);
}
// If we have reached the end... just store beforei
// Otherwise, we need to compute a key.
// Counting from 0 to n-1...
if(i==(totalregistered-1)) {
ECCMath.toZ1(beforei,pp);
voters[i].reconstructedkey[0] = beforei[0];
voters[i].reconstructedkey[1] = beforei[1];
} else {
// Subtract 'i' from afteri
temp[0] = voters[i].registeredkey[0];
temp[1] = pp - voters[i].registeredkey[1];
// Grab negation of afteri (did not seem to work with Jacob co-ordinates)
Secp256k1._addMixedM(afteri,temp);
ECCMath.toZ1(afteri,pp);
temp[0] = afteri[0];
temp[1] = pp - afteri[1];
// Now we do beforei - afteri...
yG = Secp256k1._addMixed(beforei, temp);
ECCMath.toZ1(yG,pp);
voters[i].reconstructedkey[0] = yG[0];
voters[i].reconstructedkey[1] = yG[1];
}
}
// We have computed each voter's special voting key.
// Now we either enter the commitment phase (option) or voting phase.
if(commitmentphase) {
state = State.COMMITMENT;
} else {
state = State.VOTE;
}
}
/*
* OPTIONAL STAGE: All voters submit the hash of their vote.
* Why? The final voter that submits their vote gets to see the tally result
* before anyone else. This provides the voter with an additional advantage
* compared to all other voters. To get around this issue; we can force all
* voters to commit to their vote in advance.... and votes are only revealed
* once all voters have committed. This way the final voter has no additional
* advantage as they cannot change their vote depending on the tally.
* However... we cannot enforce the pre-image to be a hash, and someone could
* a commitment that is not a vote. This will break the election, but you
* will be able to determine who did it (and possibly punish them!).
*/
function submitCommitment(bytes32 h) inState(State.COMMITMENT) {
//All voters have a deadline to send their commitment
if(block.timestamp > endCommitmentPhase) {
return;
}
if(!commitment[msg.sender]) {
commitment[msg.sender] = true;
uint index = addressid[msg.sender];
voters[index].commitment = h;
totalcommitted = totalcommitted + 1;
// Once we have recorded all commitments... let voters vote!
if(totalcommitted == totalregistered) {
state = State.VOTE;
}
}
}
// Given the 1 out of 2 ZKP - record the users vote!
function submitVote(uint[4] params, uint[2] y, uint[2] a1, uint[2] b1, uint[2] a2, uint[2] b2) inState(State.VOTE) returns (bool) {
// HARD DEADLINE
if(block.timestamp > endVotingPhase) {
return;
}
uint c = addressid[msg.sender];
// Make sure the sender can vote, and hasn't already voted.
if(registered[msg.sender] && !votecast[msg.sender]) {
// OPTIONAL Phase: Voters need to commit to their vote in advance.
// Time to verify if this vote matches the voter's previous commitment.
if(commitmentphase) {
// Voter has previously committed to the entire zero knowledge proof...
bytes32 h = sha3(msg.sender, params, voters[c].registeredkey, voters[c].reconstructedkey, y, a1, b1, a2, b2);
// No point verifying the ZKP if it doesn't match the voter's commitment.
if(voters[c].commitment != h) {
return false;
}
}
// Verify the ZKP for the vote being cast
if(verify1outof2ZKP(params, y, a1, b1, a2, b2)) {
voters[c].vote[0] = y[0];
voters[c].vote[1] = y[1];
votecast[msg.sender] = true;
totalvoted += 1;
// Refund the sender their ether..
// Voter has finished their part of the protocol...
uint refund = refunds[msg.sender];
refunds[msg.sender] = 0;
// We can still fail... Safety first.
// If failed... voter can call withdrawRefund()
// to collect their money once the election has finished.
if (!msg.sender.send(refund)) {
refunds[msg.sender] = refund;
}
return true;
}
}
// Either vote has already been cast, or ZKP verification failed.
return false;
}
// Assuming all votes have been submitted. We can leak the tally.
// We assume Election Authority performs this function. It could be anyone.
// Election Authority gets deposit upon tallying.
// TODO: Anyone can do this function. Perhaps remove refund code - and force Election Authority
// to explicit withdraw it? Election cannot reset until he is refunded - so that should be OK
function computeTally() inState(State.VOTE) onlyOwner {