View Javadoc
1   package civitas.crypto.decriptionshare;
2   
3   import static org.junit.jupiter.api.Assertions.assertFalse;
4   import static org.junit.jupiter.api.Assertions.assertThrows;
5   import static org.junit.jupiter.api.Assertions.assertTrue;
6   import static org.mockito.Mockito.verify;
7   
8   import org.junit.jupiter.api.DisplayName;
9   import org.junit.jupiter.api.Test;
10  import org.mockito.InjectMocks;
11  
12  import civitas.crypto.ciphertext.ElGamalCiphertext;
13  import civitas.crypto.publickey.ElGamalPublicKey;
14  import io.github.magwas.testing.TestBase;
15  
16  class VerifyElGamalDecryptionShareTest extends TestBase implements ElGamalDecryptionShareTestData {
17  	@InjectMocks
18  	VerifyElGamalDecryptionShare verifyElGamalDecryptionShare;
19  
20  	@Test
21  	// @formatter:off
22  	@DisplayName(
23  			"""
24  			verify verifies the proof
25  			- ciphertext.a =? proof.g1
26  			- g =? proof.g2
27  			- proof.v =? share.ai
28  			- proof.w =? key
29  			- the proof verifies
30  			""")
31  	// @formatter:on
32  	void test4() {
33  		boolean actual =
34  				verifyElGamalDecryptionShare.apply(EL_GAMAL_DECRYPTION_SHARE, CIPHERTEXT_E, EL_GAMAL_PUBLIC_KEY_E);
35  		verify(verifyElGamalDecryptionShare.verifyElGamalProofDiscLogEquality)
36  				.apply(EL_GAMAL_DISC_LOG_EQUALITY_FOR_DECRIPTIONSHARE, EL_GAMAL_PARAMETERS);
37  		assertTrue(actual);
38  	}
39  
40  	@Test
41  	@DisplayName("if the proof fails, returns false")
42  	void test() {
43  		boolean actual = verifyElGamalDecryptionShare.apply(
44  				EL_GAMAL_DECRYPTION_SHARE_BADPROOF, CIPHERTEXT_E, EL_GAMAL_PUBLIC_KEY_E);
45  		assertFalse(actual);
46  	}
47  
48  	@Test
49  	@DisplayName("if g2 != g, it fails")
50  	void test4_1() {
51  		assertFalse(verifyElGamalDecryptionShare.apply(
52  				EL_GAMAL_DECRYPTION_SHARE, CIPHERTEXT_E, EL_GAMAL_PUBLIC_KEY_E_BUT_OTHER_PARAMETERS));
53  	}
54  
55  	@Test
56  	@DisplayName("if proof.v != share.ai, it fails")
57  	void testg() {
58  		assertFalse(verifyElGamalDecryptionShare.apply(
59  				EL_GAMAL_DECRYPTION_SHARE_BAD_AI, CIPHERTEXT_E, EL_GAMAL_PUBLIC_KEY_E));
60  	}
61  
62  	@Test
63  	@DisplayName("if proof.w != key, it fails")
64  	void testw() {
65  		assertFalse(verifyElGamalDecryptionShare.apply(
66  				EL_GAMAL_DECRYPTION_SHARE, CIPHERTEXT_E, EL_GAMAL_PUBLIC_KEY_EPRIME));
67  	}
68  
69  	@Test
70  	@DisplayName("verify throws IllegalArgumentException if ciphertext is null")
71  	void test4_2() {
72  		assertThrows(
73  				IllegalArgumentException.class,
74  				() -> assertFalse(verifyElGamalDecryptionShare.apply(
75  						EL_GAMAL_DECRYPTION_SHARE, null, EL_GAMAL_PUBLIC_KEY_EPRIME)));
76  	}
77  
78  	@Test
79  	@DisplayName("verify throws IllegalArgumentException if key is null")
80  	void test4_3() {
81  		assertThrows(
82  				IllegalArgumentException.class,
83  				() -> assertFalse(verifyElGamalDecryptionShare.apply(EL_GAMAL_DECRYPTION_SHARE, CIPHERTEXT_E, null)));
84  	}
85  
86  	@Test
87  	@DisplayName("verify is false if ciphertext.a != g1")
88  	void test4_4() {
89  		assertFalse(verifyElGamalDecryptionShare.apply(
90  				EL_GAMAL_DECRYPTION_SHARE, new ElGamalCiphertext(RANDOMS_2, BIGINT_A), EL_GAMAL_PUBLIC_KEY_EPRIME));
91  	}
92  
93  	@Test
94  	@DisplayName("verify is false if the pubkey's g != g2")
95  	void test4_5() {
96  		assertFalse(verifyElGamalDecryptionShare.apply(
97  				EL_GAMAL_DECRYPTION_SHARE,
98  				EL_GAMAL_CIPHERTEXT,
99  				new ElGamalPublicKey(G_EXP_A, EL_GAMAL_PARAMETERS_GENERATOR_OTHER)));
100 	}
101 
102 	@Test
103 	@DisplayName("verify is false if the pubkey's y != w ")
104 	void test4_6() {
105 		assertFalse(verifyElGamalDecryptionShare.apply(
106 				EL_GAMAL_DECRYPTION_SHARE, EL_GAMAL_CIPHERTEXT, new ElGamalPublicKey(BIGINT_A, EL_GAMAL_PARAMETERS)));
107 	}
108 
109 	@Test
110 	@DisplayName("verify is false if  ai != v")
111 	void test4_7() {
112 		assertFalse(verifyElGamalDecryptionShare.apply(
113 				new ElGamalDecryptionShare(BIGINT_A, EL_GAMAL_DISC_LOG_EQUALITY_FOR_DECOMMITMENT),
114 				EL_GAMAL_CIPHERTEXT,
115 				new ElGamalPublicKey(BIGINT_A, EL_GAMAL_PARAMETERS)));
116 	}
117 }