View Javadoc
1   package civitas.common.votersubmission;
2   
3   import org.springframework.beans.factory.annotation.Autowired;
4   import org.springframework.stereotype.Controller;
5   
6   import civitas.common.CommonConstants;
7   import civitas.common.VoteChoice;
8   import civitas.common.ballotdesign.BallotDesign;
9   import civitas.common.ballotdesign.CalculatePositionInBallot;
10  import civitas.common.verifiablevote.VerifiableVote;
11  import civitas.common.verifiablevote.VerifyVerifiableVote;
12  import civitas.crypto.ciphertextlist.CiphertextList;
13  import civitas.crypto.publickey.ElGamalPublicKey;
14  
15  @Controller
16  public class CheckVoterSubmission implements CommonConstants {
17  
18  	@Autowired
19  	CalculatePositionInBallot calculatePositionInBallot;
20  
21  	@Autowired
22  	VerifyVerifiableVote verifyVerifiableVote;
23  
24  	public final void apply(
25  			final BallotDesign that,
26  			final VoterSubmission vs,
27  			final String baseContext,
28  			final CiphertextList ciphertexts,
29  			final ElGamalPublicKey pubKey) {
30  		apply(that, vs, 0, baseContext, ciphertexts, pubKey);
31  	}
32  
33  	public void apply(
34  			final BallotDesign that,
35  			final VoterSubmission vs,
36  			final int startIndex,
37  			final String context,
38  			final CiphertextList ciphertexts,
39  			final ElGamalPublicKey pubKey) {
40  		if (pubKey == null) {
41  			throw new IllegalArgumentException("Invalid public Key");
42  		}
43  		if (context == null) {
44  			throw new IllegalArgumentException("Invalid context");
45  		}
46  		if (vs == null) {
47  			throw new IllegalArgumentException("Invalid voter submission.");
48  		}
49  		int k = that.getCandidates().length;
50  		for (int i = 0; i < k; i++) {
51  			for (int j = i + 1; j < k; j++) {
52  				VerifiableVote vv = vs.votes[startIndex + calculatePositionInBallot.apply(i, j, k)];
53  				if (vv == null) {
54  					throw new IllegalArgumentException("Invalid verifiable vote.");
55  				}
56  				String vvcontext = vv.context();
57  				String desiredContext = context + KIND + i + ":" + j;
58  				if (!desiredContext.equals(vvcontext)) {
59  					throw new IllegalArgumentException("Vote did not have correct context");
60  				}
61  
62  				if (!verifyVerifiableVote.apply(vv, pubKey, ciphertexts, VoteChoice.values().length + 1)) {
63  					throw new IllegalArgumentException("Vote choice odes not pass verification.");
64  				}
65  			}
66  		}
67  	}
68  }