Repository navigation
fix: reject Hill cipher keys whose determinant is not coprime with 26 - #7780
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
Aditya2375
marked this pull request as ready for review
November 24, 2026 10:47
Aditya2375
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
November 24, 2026 10:47
alxkm
approved these changes
Nov 24, 2026
alxkm
left a comment
Member
There was a problem hiding this comment.
Looks good. Thank you for the contribution.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug:
HillCipher.validateDeterminantonly rejects a key matrix whose determinant is zero modulo 26. A key matrix is invertible modulo 26 only whengcd(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 andtwo different plaintexts collapse to the same ciphertext.
Fix: check coprimality instead of only zero:
Math.floorModreplaces 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.clang-format -i --style=file path/to/your/file.java