|
| 1 | +package com.thealgorithms.ciphers; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | + |
| 9 | +import java.math.BigInteger; |
| 10 | +import java.util.stream.Stream; |
| 11 | +import org.junit.jupiter.api.BeforeAll; |
| 12 | +import org.junit.jupiter.api.DisplayName; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.params.ParameterizedTest; |
| 15 | +import org.junit.jupiter.params.provider.MethodSource; |
| 16 | + |
| 17 | +/** |
| 18 | + * Unit tests for ElGamalCipher. |
| 19 | + * Includes property-based testing (homomorphism), probabilistic checks, |
| 20 | + * and boundary validation. |
| 21 | + */ |
| 22 | +class ElGamalCipherTest { |
| 23 | + |
| 24 | + private static ElGamalCipher.KeyPair sharedKeys; |
| 25 | + |
| 26 | + @BeforeAll |
| 27 | + static void setup() { |
| 28 | + // Generate 256-bit keys for efficient unit testing |
| 29 | + sharedKeys = ElGamalCipher.generateKeys(256); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + @DisplayName("Test Key Generation Validity") |
| 34 | + void testKeyGeneration() { |
| 35 | + assertNotNull(sharedKeys.p()); |
| 36 | + assertNotNull(sharedKeys.g()); |
| 37 | + assertNotNull(sharedKeys.x()); |
| 38 | + assertNotNull(sharedKeys.y()); |
| 39 | + |
| 40 | + // Verify generator bounds: 1 < g < p |
| 41 | + assertTrue(sharedKeys.g().compareTo(BigInteger.ONE) > 0); |
| 42 | + assertTrue(sharedKeys.g().compareTo(sharedKeys.p()) < 0); |
| 43 | + |
| 44 | + // Verify private key bounds: 1 < x < p-1 |
| 45 | + assertTrue(sharedKeys.x().compareTo(BigInteger.ONE) > 0); |
| 46 | + assertTrue(sharedKeys.x().compareTo(sharedKeys.p().subtract(BigInteger.ONE)) < 0); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @DisplayName("Security Check: Probabilistic Encryption") |
| 51 | + void testSemanticSecurity() { |
| 52 | + // Encrypting the same message twice MUST yield different ciphertexts |
| 53 | + // due to the random ephemeral key 'k'. |
| 54 | + BigInteger message = new BigInteger("123456789"); |
| 55 | + |
| 56 | + ElGamalCipher.CipherText c1 = ElGamalCipher.encrypt(message, sharedKeys.p(), sharedKeys.g(), sharedKeys.y()); |
| 57 | + ElGamalCipher.CipherText c2 = ElGamalCipher.encrypt(message, sharedKeys.p(), sharedKeys.g(), sharedKeys.y()); |
| 58 | + |
| 59 | + // Check that the ephemeral keys (and thus 'a' components) were different |
| 60 | + assertNotEquals(c1.a(), c2.a(), "Ciphertexts must be randomized (Semantic Security violation)"); |
| 61 | + assertNotEquals(c1.b(), c2.b()); |
| 62 | + |
| 63 | + // But both must decrypt to the original message |
| 64 | + assertEquals(ElGamalCipher.decrypt(c1, sharedKeys.x(), sharedKeys.p()), message); |
| 65 | + assertEquals(ElGamalCipher.decrypt(c2, sharedKeys.x(), sharedKeys.p()), message); |
| 66 | + } |
| 67 | + |
| 68 | + @ParameterizedTest |
| 69 | + @MethodSource("provideMessages") |
| 70 | + @DisplayName("Parameterized Test: Encrypt and Decrypt various messages") |
| 71 | + void testEncryptDecrypt(String messageStr) { |
| 72 | + BigInteger message = new BigInteger(messageStr.getBytes()); |
| 73 | + |
| 74 | + // Skip if message exceeds the test key size (256 bits) |
| 75 | + if (message.compareTo(sharedKeys.p()) >= 0) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + ElGamalCipher.CipherText ciphertext = ElGamalCipher.encrypt(message, sharedKeys.p(), sharedKeys.g(), sharedKeys.y()); |
| 80 | + BigInteger decrypted = ElGamalCipher.decrypt(ciphertext, sharedKeys.x(), sharedKeys.p()); |
| 81 | + |
| 82 | + assertEquals(message, decrypted, "Decrypted BigInteger must match original"); |
| 83 | + assertEquals(messageStr, new String(decrypted.toByteArray()), "Decrypted string must match original"); |
| 84 | + } |
| 85 | + |
| 86 | + static Stream<String> provideMessages() { |
| 87 | + return Stream.of("Hello World", "TheAlgorithms", "A", "1234567890", "!@#$%^&*()"); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + @DisplayName("Edge Case: Message equals 0") |
| 92 | + void testMessageZero() { |
| 93 | + BigInteger zero = BigInteger.ZERO; |
| 94 | + ElGamalCipher.CipherText ciphertext = ElGamalCipher.encrypt(zero, sharedKeys.p(), sharedKeys.g(), sharedKeys.y()); |
| 95 | + BigInteger decrypted = ElGamalCipher.decrypt(ciphertext, sharedKeys.x(), sharedKeys.p()); |
| 96 | + |
| 97 | + assertEquals(zero, decrypted, "Should successfully encrypt/decrypt zero"); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + @DisplayName("Edge Case: Message equals p-1") |
| 102 | + void testMessageMaxBound() { |
| 103 | + BigInteger pMinus1 = sharedKeys.p().subtract(BigInteger.ONE); |
| 104 | + ElGamalCipher.CipherText ciphertext = ElGamalCipher.encrypt(pMinus1, sharedKeys.p(), sharedKeys.g(), sharedKeys.y()); |
| 105 | + BigInteger decrypted = ElGamalCipher.decrypt(ciphertext, sharedKeys.x(), sharedKeys.p()); |
| 106 | + |
| 107 | + assertEquals(pMinus1, decrypted, "Should successfully encrypt/decrypt p-1"); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + @DisplayName("Negative Test: Message >= p should fail") |
| 112 | + void testMessageTooLarge() { |
| 113 | + BigInteger tooLarge = sharedKeys.p(); |
| 114 | + assertThrows(IllegalArgumentException.class, () -> ElGamalCipher.encrypt(tooLarge, sharedKeys.p(), sharedKeys.g(), sharedKeys.y())); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + @DisplayName("Negative Test: Decrypt with wrong private key") |
| 119 | + void testWrongKeyDecryption() { |
| 120 | + BigInteger message = new BigInteger("99999"); |
| 121 | + ElGamalCipher.CipherText ciphertext = ElGamalCipher.encrypt(message, sharedKeys.p(), sharedKeys.g(), sharedKeys.y()); |
| 122 | + |
| 123 | + // Generate a fake private key |
| 124 | + BigInteger wrongX = sharedKeys.x().add(BigInteger.ONE); |
| 125 | + |
| 126 | + BigInteger decrypted = ElGamalCipher.decrypt(ciphertext, wrongX, sharedKeys.p()); |
| 127 | + |
| 128 | + assertNotEquals(message, decrypted, "Decryption with wrong key must yield incorrect result"); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + @DisplayName("Property Test: Multiplicative Homomorphism") |
| 133 | + void testHomomorphism() { |
| 134 | + BigInteger m1 = new BigInteger("50"); |
| 135 | + BigInteger m2 = new BigInteger("10"); |
| 136 | + |
| 137 | + ElGamalCipher.CipherText c1 = ElGamalCipher.encrypt(m1, sharedKeys.p(), sharedKeys.g(), sharedKeys.y()); |
| 138 | + ElGamalCipher.CipherText c2 = ElGamalCipher.encrypt(m2, sharedKeys.p(), sharedKeys.g(), sharedKeys.y()); |
| 139 | + |
| 140 | + // Multiply ciphertexts component-wise: (a1*a2, b1*b2) |
| 141 | + BigInteger aNew = c1.a().multiply(c2.a()).mod(sharedKeys.p()); |
| 142 | + BigInteger bNew = c1.b().multiply(c2.b()).mod(sharedKeys.p()); |
| 143 | + ElGamalCipher.CipherText cCombined = new ElGamalCipher.CipherText(aNew, bNew); |
| 144 | + |
| 145 | + BigInteger decrypted = ElGamalCipher.decrypt(cCombined, sharedKeys.x(), sharedKeys.p()); |
| 146 | + BigInteger expected = m1.multiply(m2).mod(sharedKeys.p()); |
| 147 | + |
| 148 | + assertEquals(expected, decrypted, "Cipher must satisfy multiplicative homomorphism"); |
| 149 | + } |
| 150 | +} |
0 commit comments