1 package civitas.common.mix.voteelementrevelation;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.stereotype.Controller;
5
6 import civitas.common.EncryptedVote;
7 import civitas.common.mix.VoterMix;
8 import civitas.common.mix.votemix.VoteMix;
9 import civitas.crypto.ciphertext.ElGamalCiphertextish;
10 import civitas.crypto.ciphertext.ElGamalReencrypt;
11 import civitas.crypto.publickey.ElGamalPublicKey;
12 import jakarta.annotation.Nonnull;
13
14 @Controller
15 public class VerifyMixVoteElementRevelation {
16
17 @Autowired
18 ElGamalReencrypt elGamalReencrypt;
19
20 public boolean apply(
21 @Nonnull final MixVoteElementRevelation that,
22 @Nonnull final ElGamalPublicKey key,
23 final int fromIndex,
24 final int toIndex,
25 @Nonnull final VoterMix fromMix,
26 @Nonnull final VoterMix toMix) {
27 if (null == fromMix || null == toMix) {
28 throw new NullPointerException();
29 }
30 if (!(fromMix instanceof VoteMix && toMix instanceof VoteMix)) {
31 return false;
32 }
33
34 EncryptedVote fromVote = ((VoteMix) fromMix).votes[fromIndex];
35 EncryptedVote toVote = ((VoteMix) toMix).votes[toIndex];
36 ElGamalCiphertextish fromChoice = fromVote.encChoice();
37 ElGamalCiphertextish fromCapability = fromVote.encCapability();
38 ElGamalCiphertextish toChoice = toVote.encChoice();
39 ElGamalCiphertextish toCapability = toVote.encCapability();
40
41 ElGamalCiphertextish rechoice = elGamalReencrypt.apply(key, fromChoice, that.choiceFactor);
42 ElGamalCiphertextish recapability = elGamalReencrypt.apply(key, fromCapability, that.reencryptFactor);
43 return rechoice.equals(toChoice) && recapability.equals(toCapability);
44 }
45 }