Skip to content

Commit 0d2707a

Browse files
Merge branch 'master' into fix/pronic-number-overflow
2 parents 772ff2f + 3e421bc commit 0d2707a

12 files changed

Lines changed: 126 additions & 15 deletions

File tree

‎.github/workflows/build.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
steps:
1111
- uses: actions/checkout@v7
1212
- name: Set up JDK
13-
uses: actions/setup-java@v6.2.0
13+
uses: actions/setup-java@v6.3.0
1414
with:
1515
java-version: 21
1616
distribution: 'temurin'

‎.github/workflows/codeql.yml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ jobs:
2424
uses: actions/checkout@v7
2525

2626
- name: Set up JDK
27-
uses: actions/setup-java@v6.2.0
27+
uses: actions/setup-java@v6.3.0
2828
with:
2929
java-version: 21
3030
distribution: 'temurin'
3131

3232
- name: Initialize CodeQL
33-
uses: github/codeql-action/init@v4.39.3
33+
uses: github/codeql-action/init@v4.39.4
3434
with:
3535
languages: 'java-kotlin'
3636

3737
- name: Build
3838
run: mvn --batch-mode --update-snapshots verify
3939

4040
- name: Perform CodeQL Analysis
41-
uses: github/codeql-action/analyze@v4.39.3
41+
uses: github/codeql-action/analyze@v4.39.4
4242
with:
4343
category: "/language:java-kotlin"
4444

@@ -55,12 +55,12 @@ jobs:
5555
uses: actions/checkout@v7
5656

5757
- name: Initialize CodeQL
58-
uses: github/codeql-action/init@v4.39.3
58+
uses: github/codeql-action/init@v4.39.4
5959
with:
6060
languages: 'actions'
6161

6262
- name: Perform CodeQL Analysis
63-
uses: github/codeql-action/analyze@v4.39.3
63+
uses: github/codeql-action/analyze@v4.39.4
6464
with:
6565
category: "/language:actions"
6666
...

‎.github/workflows/infer.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- uses: actions/checkout@v7
1919

2020
- name: Set up JDK
21-
uses: actions/setup-java@v6.2.0
21+
uses: actions/setup-java@v6.3.0
2222
with:
2323
java-version: 21
2424
distribution: 'temurin'

‎src/main/java/com/thealgorithms/ciphers/HillCipher.java‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,22 @@ public String decrypt(String message, int[][] inverseKeyMatrix) {
6868
return plainText.toString();
6969
}
7070

71-
// Validates that the determinant of the key matrix is not zero modulo 26
71+
// Validates that the key matrix is invertible modulo 26, which requires the
72+
// determinant to be coprime with 26. Checking only for a zero determinant is not
73+
// enough: a determinant of 2 or 13 (or any other non-unit) passes that check but
74+
// has no inverse, so encryption collapses distinct plaintexts onto the same
75+
// ciphertext and decryption becomes impossible.
7276
private void validateDeterminant(int[][] keyMatrix, int n) {
73-
int det = determinant(keyMatrix, n) % 26;
74-
if (det == 0) {
75-
throw new IllegalArgumentException("Invalid key matrix. Determinant is zero modulo 26.");
77+
int det = Math.floorMod(determinant(keyMatrix, n), 26);
78+
if (gcd(det, 26) != 1) {
79+
throw new IllegalArgumentException("Invalid key matrix. Determinant must be coprime with 26 to be invertible modulo 26.");
7680
}
7781
}
7882

83+
private int gcd(int a, int b) {
84+
return b == 0 ? a : gcd(b, a % b);
85+
}
86+
7987
// Computes the determinant of a matrix recursively
8088
private int determinant(int[][] matrix, int n) {
8189
int det = 0;

‎src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ public class PlayfairCipher {
66
private String key;
77

88
public PlayfairCipher(String key) {
9-
this.key = key;
9+
// Normalize the key the same way the plaintext is normalized in encrypt():
10+
// uppercase, J folded into I, and non-letters removed. Without this a
11+
// lowercase or mixed-case key, a key containing J, or a key with
12+
// punctuation silently produces a wrong matrix.
13+
this.key = key.toUpperCase().replace("J", "I").replaceAll("[^A-Z]", "");
1014
generateMatrix();
1115
}
1216

‎src/main/java/com/thealgorithms/conversions/OctalToBinary.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private OctalToBinary() {
3737
*/
3838
public static long convertOctalToBinary(int octalNumber) {
3939
long binaryNumber = 0;
40-
int digitPosition = 1;
40+
long digitPosition = 1;
4141

4242
while (octalNumber != 0) {
4343
int octalDigit = octalNumber % 10;

‎src/main/java/com/thealgorithms/misc/MedianOfRunningArrayInteger.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
public final class MedianOfRunningArrayInteger extends MedianOfRunningArray<Integer> {
44
@Override
55
public Integer calculateAverage(final Integer a, final Integer b) {
6-
return (a + b) / 2;
6+
// Widen to long so that summing values near Integer.MAX_VALUE does not overflow.
7+
final long sum = (long) a + b;
8+
return (int) (sum / 2);
79
}
810
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.thealgorithms.misc;
22

3+
import java.math.BigInteger;
4+
35
public final class MedianOfRunningArrayLong extends MedianOfRunningArray<Long> {
46
@Override
57
public Long calculateAverage(final Long a, final Long b) {
6-
return (a + b) / 2L;
8+
// a + b overflows a long near its bounds and there is no wider primitive to widen to.
9+
return BigInteger.valueOf(a).add(BigInteger.valueOf(b)).divide(BigInteger.TWO).longValueExact();
710
}
811
}

‎src/test/java/com/thealgorithms/ciphers/HillCipherTest.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.ciphers;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import org.junit.jupiter.api.Test;
67

@@ -33,4 +34,25 @@ void hillCipherDecryptTest() {
3334
// then
3435
assertEquals("ACT", plainText);
3536
}
37+
38+
@Test
39+
void rejectsNonInvertibleKeyWithEvenDeterminant() {
40+
// det = 2, gcd(2, 26) = 2: passes the old zero-check but has no inverse mod 26
41+
int[][] keyMatrix = {{1, 0}, {0, 2}};
42+
assertThrows(IllegalArgumentException.class, () -> hillCipher.encrypt("AA", keyMatrix));
43+
}
44+
45+
@Test
46+
void rejectsNonInvertibleKeyWithDeterminantThirteen() {
47+
// det = 13, gcd(13, 26) = 13: also not invertible mod 26
48+
int[][] keyMatrix = {{13, 0}, {0, 1}};
49+
assertThrows(IllegalArgumentException.class, () -> hillCipher.encrypt("BB", keyMatrix));
50+
}
51+
52+
@Test
53+
void acceptsKeyWithNegativeCoprimeDeterminant() {
54+
// det = -1, coprime with 26: a valid key that a naive signed modulo would misclassify
55+
int[][] keyMatrix = {{1, 2}, {1, 1}};
56+
assertEquals("XP", hillCipher.encrypt("HI", keyMatrix));
57+
}
3658
}

‎src/test/java/com/thealgorithms/ciphers/PlayfairTest.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,30 @@ public void testEncryptionAndDecryption() {
3434

3535
assertEquals(plaintext, decryptedText);
3636
}
37+
38+
@Test
39+
public void testLowercaseKeyBehavesLikeUppercase() {
40+
PlayfairCipher lowercase = new PlayfairCipher("monarchy");
41+
PlayfairCipher uppercase = new PlayfairCipher("MONARCHY");
42+
43+
assertEquals(uppercase.encrypt("HELLO"), lowercase.encrypt("HELLO"));
44+
assertEquals("CFSUPM", lowercase.encrypt("HELLO"));
45+
}
46+
47+
@Test
48+
public void testKeyContainingJStillCoversZ() {
49+
PlayfairCipher playfairCipher = new PlayfairCipher("JUMP");
50+
51+
String decryptedText = playfairCipher.decrypt(playfairCipher.encrypt("ZOO"));
52+
53+
assertEquals("ZOXO", decryptedText);
54+
}
55+
56+
@Test
57+
public void testKeyWithNonLettersIsStripped() {
58+
PlayfairCipher messy = new PlayfairCipher("Mon@rchy2026");
59+
PlayfairCipher clean = new PlayfairCipher("MONRCHY");
60+
61+
assertEquals(clean.encrypt("HELLO"), messy.encrypt("HELLO"));
62+
}
3763
}

0 commit comments

Comments
 (0)