1 package civitas.crypto.proof1ofl;
2
3 import static org.mockito.Mockito.mock;
4
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.function.Supplier;
9 import java.util.stream.IntStream;
10
11 import civitas.common.CommonUtil;
12 import civitas.common.ConstructTestData;
13 import civitas.common.VoteChoice;
14 import civitas.crypto.ciphertext.ElGamalCiphertext;
15 import civitas.crypto.ciphertextlist.ElGamalCiphertextListTestData;
16 import civitas.util.CivitasBigInteger;
17
18 public interface ElGamalProof1OfLTestData extends ElGamalCiphertextListTestData {
19
20 CivitasBigInteger REENCRYPTED_WELL_KNOWN_CHOICE_A =
21 CIPHERTEXT_LIST.get(MY_CHOICE.ordinal()).getA().modMultiply(BIGINT_G.modPow(FACTOR_E, BIGINT_P), BIGINT_P);
22 CivitasBigInteger REENCRYPTED_WELL_KNOWN_CHOICE_B =
23 CIPHERTEXT_LIST.get(MY_CHOICE.ordinal()).getB().modMultiply(PUBKEY_E.modPow(FACTOR_E, BIGINT_P), BIGINT_P);
24
25 ElGamalCiphertext REENCRYPTED_WELL_KNOWN_CHOICE =
26 new ElGamalCiphertext(REENCRYPTED_WELL_KNOWN_CHOICE_A, REENCRYPTED_WELL_KNOWN_CHOICE_B);
27
28 CivitasBigInteger w = FACTOR_E.modNegate(BIGINT_Q)
29 .modMultiply(DS.get(MY_CHOICE.ordinal()), BIGINT_Q)
30 .modAdd(RS.get(MY_CHOICE.ordinal()), BIGINT_Q);
31
32 List<CivitasBigInteger> EL_GAMAL_PROOF_1_OF_L_AS = IntStream.range(0, NO_OF_WELL_KNOWN_CIPHERTEXTS)
33 .mapToObj(i -> CIPHERTEXT_LIST
34 .get(i)
35 .getA()
36 .modDivide(REENCRYPTED_WELL_KNOWN_CHOICE_A, BIGINT_P)
37 .modPow(DS.get(i), BIGINT_P)
38 .modMultiply(BIGINT_G.modPow(RS.get(i), BIGINT_P), BIGINT_P)
39 .mod(BIGINT_P))
40 .toList();
41 List<CivitasBigInteger> EL_GAMAL_PROOF_1_OF_L_BS = IntStream.range(0, NO_OF_WELL_KNOWN_CIPHERTEXTS)
42 .mapToObj(i -> CIPHERTEXT_LIST
43 .get(i)
44 .getB()
45 .modDivide(REENCRYPTED_WELL_KNOWN_CHOICE_B, BIGINT_P)
46 .modPow(DS.get(i), BIGINT_P)
47 .modMultiply(PUBKEY_E.modPow(RS.get(i), BIGINT_P), BIGINT_P)
48 .mod(BIGINT_P))
49 .toList();
50
51 List<CivitasBigInteger> EL_GAMAL_PROOF_1_OF_L_ENV = ((Supplier<List<CivitasBigInteger>>) () -> {
52 ArrayList<CivitasBigInteger> d = new ArrayList<>(2 + 4 * NO_OF_WELL_KNOWN_CIPHERTEXTS);
53 d.add(REENCRYPTED_WELL_KNOWN_CHOICE_A);
54 d.add(REENCRYPTED_WELL_KNOWN_CHOICE_B);
55 for (int i = 0; i < NO_OF_WELL_KNOWN_CIPHERTEXTS; i++) {
56 d.add(CIPHERTEXT_LIST.get(i).getA());
57 d.add(CIPHERTEXT_LIST.get(i).getB());
58 d.add(EL_GAMAL_PROOF_1_OF_L_AS.get(i));
59 d.add(EL_GAMAL_PROOF_1_OF_L_BS.get(i));
60 }
61 return d;
62 })
63 .get();
64
65 String EL_GAMAL_PROOF_1_OF_L_HASH_BASE64 = "TqT++ODFynAUZo5awzSUsEXBXCS6HlC2CkKk6WEG1e4=";
66 CivitasBigInteger EL_GAMAL_PROOF_1_OF_L_HASH = CommonUtil.asBigint(EL_GAMAL_PROOF_1_OF_L_HASH_BASE64);
67
68 CivitasBigInteger EL_GAMAL_PROOF_1_OF_L_DV = EL_GAMAL_PROOF_1_OF_L_HASH.modSubtract(SUM, BIGINT_Q);
69 CivitasBigInteger EL_GAMAL_PROOF_1_OF_L_RV =
70 w.modAdd(FACTOR_E.modMultiply(EL_GAMAL_PROOF_1_OF_L_DV, BIGINT_Q), BIGINT_Q);
71
72 List<CivitasBigInteger> DVS = ((Supplier<List<CivitasBigInteger>>) () -> {
73 List<CivitasBigInteger> d = new ArrayList<>(DS);
74 d.set(MY_CHOICE.ordinal(), EL_GAMAL_PROOF_1_OF_L_DV);
75 return d;
76 })
77 .get();
78
79 List<CivitasBigInteger> RVS = ((Supplier<List<CivitasBigInteger>>) () -> {
80 List<CivitasBigInteger> r = new ArrayList<>(RS);
81 r.set(MY_CHOICE.ordinal(), EL_GAMAL_PROOF_1_OF_L_RV);
82 return r;
83 })
84 .get();
85
86 List<CivitasBigInteger> DVS_BAD = ((Supplier<List<CivitasBigInteger>>) () -> {
87 List<CivitasBigInteger> d = new ArrayList<>(DVS);
88 d.set(MY_CHOICE.ordinal(), BIGINT_D);
89 return d;
90 })
91 .get();
92
93 ElGamalProof1OfL EL_GAMAL_PROOF_1_OF_L = new ElGamalProof1OfL(
94 NO_OF_WELL_KNOWN_CIPHERTEXTS, DVS.toArray(new CivitasBigInteger[0]), RVS.toArray(new CivitasBigInteger[0]));
95
96 ElGamalProof1OfL EL_GAMAL_PROOF_1_OF_L_BAD = new ElGamalProof1OfL(
97 NO_OF_WELL_KNOWN_CIPHERTEXTS,
98 DVS_BAD.toArray(new CivitasBigInteger[0]),
99 RVS.toArray(new CivitasBigInteger[0]));
100
101 Map<VoteChoice, ElGamalProof1OfL> EL_GAMAL_PROOF_1_OF_L_MAP = ConstructTestData.constructTestData(
102 CHOICES,
103 i -> new ElGamalProof1OfL(
104 NO_OF_WELL_KNOWN_CIPHERTEXTS,
105 DVS.stream()
106 .map(x -> {
107 if (DVS.indexOf(x) == i.ordinal()) {
108 return mock(CivitasBigInteger.class, "dvs" + i);
109 } else {
110 return x;
111 }
112 })
113 .toList()
114 .toArray(new CivitasBigInteger[0]),
115 RVS.stream()
116 .map(x -> {
117 if (RVS.indexOf(x) == i.ordinal()) {
118 return mock(CivitasBigInteger.class, "rvs" + i);
119 } else {
120 return x;
121 }
122 })
123 .toList()
124 .toArray(new CivitasBigInteger[0])));
125 }