Skip to content

fix: reject Hill cipher keys whose determinant is not coprime with 26 - #7780

Merged
alxkm merged 3 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-hill-key-validation
Nov 24, 2026
Merged

alxkm merged 3 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-hill-key-validation

Conversation

@Aditya2375

Copy link
Copy Markdown
Contributor

Bug: HillCipher.validateDeterminant only rejects a key matrix whose determinant is zero modulo 26. A key matrix is invertible modulo 26 only when gcd(det, 26) = 1, so keys with determinant 2, 4, 6, 13, ... pass validation even though they have no inverse. Encryption with such a key is lossy and decryption is impossible.

Demonstration against the current code: with key [[1, 0], [0, 2]] (det = 2), validation passes and

encrypt("AA") = "AA"
encrypt("AN") = "AA"

two different plaintexts collapse to the same ciphertext.

Fix: check coprimality instead of only zero:

int det = Math.floorMod(determinant(keyMatrix, n), 26);
if (gcd(det, 26) != 1) {
    throw new IllegalArgumentException("Invalid key matrix. Determinant must be coprime with 26 to be invertible modulo 26.");
}

Math.floorMod replaces the signed % so negative determinants are classified correctly (e.g. det = -1 is a valid key; the old % gave -1 here too, but % conflates, say, -26 and 0 only by luck of zero being symmetric - floorMod makes the intent explicit and keeps the gcd input non-negative).

Tests: three new tests in HillCipherTest: reject det = 2, reject det = 13, and accept det = -1 (encrypt("HI", [[1,2],[1,1]]) = "XP", hand-verified). All 5 tests pass locally (JUnit 5, JDK 21); the two rejection tests fail on the unpatched code. The pre-existing ACT/POH round-trip tests are unaffected.

  • 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

The old validation only rejected a determinant of zero modulo 26. A key matrix is invertible mod 26 only when gcd(det, 26) = 1, so keys with determinant 2, 13, etc. passed validation but made encryption lossy: with key [[1,0],[0,2]] both "AA" and "AN" encrypt to "AA". Check coprimality instead, using floorMod so negative determinants are classified correctly.
Regression tests for the key-validation fix: keys with determinant 2 or 13 must be rejected even though they are not zero mod 26, and a key with determinant -1 (coprime) must still be accepted.
@codecov-commenter

codecov-commenter commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.44%. Comparing base (9870de2) to head (eb4387e).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7780      +/-   ##
============================================
+ Coverage     82.43%   82.44%   +0.01%     
- Complexity     8507     8512       +5     
============================================
  Files           847      847              
  Lines         26291    26292       +1     
  Branches       5134     5134              
============================================
+ Hits          21673    21677       +4     
+ Misses         3826     3825       -1     
+ Partials        792      790       -2     

☔ View full report in Codecov by Harness.
📢 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.

@Aditya2375
Aditya2375 marked this pull request as ready for review November 24, 2026 10:47

@alxkm alxkm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thank you for the contribution.

@alxkm
alxkm merged commit c86ec0a into TheAlgorithms:master Nov 24, 2026
7 checks passed
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.

3 participants