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