Skip to content

Conversation

@singhc7
Copy link
Contributor

@singhc7 singhc7 commented Feb 3, 2026

Fixes #6934

Overview

This PR implements the ElGamal Encryption System, an asymmetric key cryptography algorithm based on the Diffie–Hellman key exchange and the difficulty of computing discrete logarithms.

This implementation focuses on cryptographic security and clean code principles, addressing common pitfalls like weak generator selection and stateful design.

Key Features

  • Safe Prime Generation: explicitly generates Safe Primes ($p = 2q + 1$) to ensure large group order and prevent Pohlig-Hellman attacks.
  • Rigorous Validation: Verifies that the generator $g$ is a primitive root modulo $p$ and enforces strict bounds for private keys ($1 < x < p-1$).
  • Stateless Design: Uses Java record types (KeyPair, CipherText) to maintain a purely functional and thread-safe architecture.
  • Semantic Security: Uses SecureRandom for ephemeral keys to ensure that encrypting the same message twice yields different ciphertexts (Indistinguishability).

Testing Strategy

Comprehensive JUnit 5 tests are included (ElGamalCipherTest.java) covering:

  • Correctness: Basic encryption/decryption flow.
  • Homomorphism: Verifies the Multiplicative Homomorphic property ($E(m_1) * E(m_2) = E(m_1 * m_2)$).
  • Security: Verifies probabilistic encryption (same message = different ciphertexts).
  • Edge Cases: Validates handling of message boundaries (0, 1, $p-1$) and invalid inputs.

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter
Copy link

codecov-commenter commented Feb 3, 2026

Codecov Report

❌ Patch coverage is 57.89474% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.32%. Comparing base (8e30bcb) to head (bca1864).

Files with missing lines Patch % Lines
.../java/com/thealgorithms/ciphers/ElGamalCipher.java 57.89% 6 Missing and 10 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7257      +/-   ##
============================================
- Coverage     79.36%   79.32%   -0.04%     
- Complexity     6981     6990       +9     
============================================
  Files           780      781       +1     
  Lines         22917    22955      +38     
  Branches       4501     4509       +8     
============================================
+ Hits          18187    18209      +22     
- Misses         4013     4019       +6     
- Partials        717      727      +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@singhc7 singhc7 force-pushed the feat/ElGamalEncryption branch 2 times, most recently from 283c3b5 to 6f414a0 Compare February 3, 2026 22:23
@singhc7 singhc7 force-pushed the feat/ElGamalEncryption branch from 6f414a0 to bca1864 Compare February 3, 2026 22:38
@singhc7 singhc7 marked this pull request as ready for review February 3, 2026 22:42
@DenizAltunkapan DenizAltunkapan merged commit a14b234 into TheAlgorithms:master Feb 4, 2026
7 checks passed
@singhc7 singhc7 deleted the feat/ElGamalEncryption branch February 4, 2026 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE REQUEST] ElGamal Encryption Algorithm

3 participants