1 package civitas.crypto.proof1ofl.tests;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertThrows;
5 import static org.mockito.Mockito.verify;
6
7 import org.junit.jupiter.api.DisplayName;
8 import org.junit.jupiter.api.Test;
9 import org.mockito.InjectMocks;
10 import org.mockito.Mock;
11
12 import civitas.crypto.CryptoException;
13 import civitas.crypto.ciphertext.ElGamalEncrypt;
14 import civitas.crypto.ciphertextlist.tests.ElGamalCiphertextListTestData;
15 import civitas.crypto.msg.EncodeMessage;
16 import civitas.crypto.proof1ofl.ConstructWellKnownCiphertexts;
17 import civitas.crypto.publickey.tests.ElGamalPublicKeyTestData;
18 import io.github.magwas.konveyor.testing.TestBase;
19
20 class ConstructWellKnownCiphertextsTest extends TestBase
21 implements ElGamalCiphertextListTestData, ElGamalPublicKeyTestData {
22
23 @InjectMocks
24 ConstructWellKnownCiphertexts constructWellKnownCiphertexts;
25
26 @Mock
27 EncodeMessage encodeMessage;
28
29 @Mock
30 ElGamalEncrypt elGamalEncrypt;
31
32 @Test
33 @DisplayName(
34 """
35 generates a ciphertext list using the public key and the count
36 for each number from 1 to n:
37 - encodes the number: n -> g^n mod p
38 - encrypts it using the public key
39 and returns the list of the results
40 """)
41 void test() throws CryptoException {
42
43 assertEquals(CIPHERTEXTLIST_TWO_LONG, constructWellKnownCiphertexts.apply(EL_GAMAL_PUBLIC_KEY_E, 2));
44 verify(encodeMessage).apply(1, EL_GAMAL_PARAMETERS);
45 verify(encodeMessage).apply(2, EL_GAMAL_PARAMETERS);
46 verify(elGamalEncrypt).apply(EL_GAMAL_PUBLIC_KEY_E, ONE_ENCODED, ENCRYPT_FACTOR_ZERO);
47 verify(elGamalEncrypt).apply(EL_GAMAL_PUBLIC_KEY_E, TWO_ENCODED, ENCRYPT_FACTOR_ZERO);
48 }
49
50 @Test
51 @DisplayName("if the length is 1, a single ciphertext is returned as a list")
52 void test1() throws CryptoException {
53 assertEquals(CIPHERTEXTLIST_ONE_LONG, constructWellKnownCiphertexts.apply(EL_GAMAL_PUBLIC_KEY_E, 1));
54 }
55
56 @Test
57 @DisplayName("if the length is less than 1, a CryptoException is thrown ")
58 void test1_1() {
59 assertThrows(CryptoException.class, () -> constructWellKnownCiphertexts.apply(EL_GAMAL_PUBLIC_KEY_E, 0));
60 }
61
62 @Test
63 @DisplayName("if the key is null, a CryptoException is thrown")
64 void test2() {
65 assertThrows(CryptoException.class, () -> constructWellKnownCiphertexts.apply(null, 2));
66 }
67 }