View Javadoc
1   package civitas.crypto.proofvote;
2   
3   import java.util.List;
4   
5   import org.springframework.beans.factory.annotation.Autowired;
6   import org.springframework.stereotype.Controller;
7   
8   import civitas.crypto.algorithms.ConvertHashToBigInt;
9   import civitas.crypto.ciphertext.ElGamalCiphertextish;
10  import civitas.crypto.messagedigest.CryptoHash;
11  import civitas.crypto.parameters.ElGamalParameters;
12  import civitas.util.CivitasBigInteger;
13  
14  @Controller
15  public class VerifyProofVote {
16  
17  	@Autowired
18  	CryptoHash cryptoHash;
19  
20  	@Autowired
21  	CalculateProofEnvironment calculateProofEnvironment;
22  
23  	@Autowired
24  	ConvertHashToBigInt convertHashToBigInt;
25  
26  	public boolean apply(
27  			final ProofVote that,
28  			final ElGamalParameters params,
29  			final ElGamalCiphertextish encCapability,
30  			final ElGamalCiphertextish encChoice,
31  			final String context) {
32  		CivitasBigInteger a1 = encCapability.getA();
33  		CivitasBigInteger a2 = encChoice.getA();
34  		CivitasBigInteger p = params.p;
35  		CivitasBigInteger q = params.q;
36  
37  		List<CivitasBigInteger> e = calculateProofEnvironment.apply(params, encCapability, encChoice, context);
38  		e.add(params.g.modPow(that.getS1(), p).modMultiply(a1.modPow(that.getC(), p), p));
39  		e.add(params.g.modPow(that.getS2(), p).modMultiply(a2.modPow(that.getC(), p), p));
40  
41  		byte[] hash = cryptoHash.apply(e);
42  		CivitasBigInteger x = convertHashToBigInt.apply(hash).mod(q);
43  		return that.getC().equals(x);
44  	}
45  }