Skip to content

Commit e4b8efd

Browse files
Merge branch 'master' into feat/right-side-view-binary-tree
2 parents cc8cad1 + 2f04e81 commit e4b8efd

13 files changed

Lines changed: 387 additions & 12 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.1.0
13+
uses: actions/setup-java@v6.2.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.1.0
27+
uses: actions/setup-java@v6.2.0
2828
with:
2929
java-version: 21
3030
distribution: 'temurin'
3131

3232
- name: Initialize CodeQL
33-
uses: github/codeql-action/init@v4.39.1
33+
uses: github/codeql-action/init@v4.39.3
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.1
41+
uses: github/codeql-action/analyze@v4.39.3
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.1
58+
uses: github/codeql-action/init@v4.39.3
5959
with:
6060
languages: 'actions'
6161

6262
- name: Perform CodeQL Analysis
63-
uses: github/codeql-action/analyze@v4.39.1
63+
uses: github/codeql-action/analyze@v4.39.3
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.1.0
21+
uses: actions/setup-java@v6.2.0
2222
with:
2323
java-version: 21
2424
distribution: 'temurin'
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.thealgorithms.ciphers;
2+
3+
import java.util.Objects;
4+
5+
/**
6+
* Implements the ChaCha20 stream cipher described in RFC 8439.
7+
*
8+
* <p>This implementation is intended for educational purposes and should not
9+
* be used as a replacement for a vetted cryptographic library in production.
10+
*
11+
* @see <a href="https://www.rfc-editor.org/rfc/rfc8439.html">RFC 8439</a>
12+
*/
13+
public final class ChaCha20 {
14+
15+
private static final int BLOCK_SIZE = 64;
16+
private static final long MAX_COUNTER = 0xFFFF_FFFFL;
17+
private static final int[] CONSTANTS = {0x61707865, 0x3320646e, 0x79622d32, 0x6b206574};
18+
19+
private ChaCha20() {
20+
// Utility class
21+
}
22+
23+
/**
24+
* Encrypts bytes with ChaCha20. Decryption uses the same operation.
25+
*
26+
* @param input plaintext bytes; must not be {@code null}
27+
* @param key 32-byte key
28+
* @param nonce 12-byte nonce
29+
* @param initialCounter unsigned 32-bit initial block counter
30+
* @return encrypted bytes
31+
* @throws NullPointerException if an argument is {@code null}
32+
* @throws IllegalArgumentException if a key, nonce, or counter is invalid,
33+
* or the counter would overflow while processing the input
34+
*/
35+
public static byte[] encrypt(byte[] input, byte[] key, byte[] nonce, long initialCounter) {
36+
validateInputs(input, key, nonce, initialCounter);
37+
38+
long blockCount = (input.length + (long) BLOCK_SIZE - 1) / BLOCK_SIZE;
39+
if (blockCount > 0 && initialCounter > MAX_COUNTER - (blockCount - 1)) {
40+
throw new IllegalArgumentException("Counter would overflow");
41+
}
42+
43+
byte[] output = new byte[input.length];
44+
for (int offset = 0; offset < input.length; offset += BLOCK_SIZE) {
45+
long counter = initialCounter + offset / BLOCK_SIZE;
46+
byte[] keyStream = block(key, nonce, counter);
47+
int bytesToProcess = Math.min(BLOCK_SIZE, input.length - offset);
48+
49+
for (int i = 0; i < bytesToProcess; i++) {
50+
output[offset + i] = (byte) (input[offset + i] ^ keyStream[i]);
51+
}
52+
}
53+
return output;
54+
}
55+
56+
/**
57+
* Decrypts bytes with ChaCha20. This is identical to encryption.
58+
*
59+
* @param input ciphertext bytes; must not be {@code null}
60+
* @param key 32-byte key
61+
* @param nonce 12-byte nonce
62+
* @param initialCounter unsigned 32-bit initial block counter
63+
* @return decrypted bytes
64+
*/
65+
public static byte[] decrypt(byte[] input, byte[] key, byte[] nonce, long initialCounter) {
66+
return encrypt(input, key, nonce, initialCounter);
67+
}
68+
69+
private static void validateInputs(byte[] input, byte[] key, byte[] nonce, long counter) {
70+
Objects.requireNonNull(input, "input must not be null");
71+
Objects.requireNonNull(key, "key must not be null");
72+
Objects.requireNonNull(nonce, "nonce must not be null");
73+
74+
if (key.length != 32) {
75+
throw new IllegalArgumentException("Key must be 32 bytes");
76+
}
77+
if (nonce.length != 12) {
78+
throw new IllegalArgumentException("Nonce must be 12 bytes");
79+
}
80+
if (counter < 0 || counter > MAX_COUNTER) {
81+
throw new IllegalArgumentException("Counter must be an unsigned 32-bit value");
82+
}
83+
}
84+
85+
private static byte[] block(byte[] key, byte[] nonce, long counter) {
86+
int[] state = new int[16];
87+
System.arraycopy(CONSTANTS, 0, state, 0, CONSTANTS.length);
88+
89+
for (int i = 0; i < 8; i++) {
90+
state[4 + i] = littleEndianInt(key, i * 4);
91+
}
92+
93+
state[12] = (int) counter;
94+
state[13] = littleEndianInt(nonce, 0);
95+
state[14] = littleEndianInt(nonce, 4);
96+
state[15] = littleEndianInt(nonce, 8);
97+
98+
int[] workingState = state.clone();
99+
for (int i = 0; i < 10; i++) {
100+
quarterRound(workingState, 0, 4, 8, 12);
101+
quarterRound(workingState, 1, 5, 9, 13);
102+
quarterRound(workingState, 2, 6, 10, 14);
103+
quarterRound(workingState, 3, 7, 11, 15);
104+
105+
quarterRound(workingState, 0, 5, 10, 15);
106+
quarterRound(workingState, 1, 6, 11, 12);
107+
quarterRound(workingState, 2, 7, 8, 13);
108+
quarterRound(workingState, 3, 4, 9, 14);
109+
}
110+
111+
byte[] output = new byte[BLOCK_SIZE];
112+
for (int i = 0; i < workingState.length; i++) {
113+
int word = workingState[i] + state[i];
114+
output[i * 4] = (byte) word;
115+
output[i * 4 + 1] = (byte) (word >>> 8);
116+
output[i * 4 + 2] = (byte) (word >>> 16);
117+
output[i * 4 + 3] = (byte) (word >>> 24);
118+
}
119+
return output;
120+
}
121+
122+
static void quarterRound(int[] state, int a, int b, int c, int d) {
123+
state[a] += state[b];
124+
state[d] = Integer.rotateLeft(state[d] ^ state[a], 16);
125+
state[c] += state[d];
126+
state[b] = Integer.rotateLeft(state[b] ^ state[c], 12);
127+
state[a] += state[b];
128+
state[d] = Integer.rotateLeft(state[d] ^ state[a], 8);
129+
state[c] += state[d];
130+
state[b] = Integer.rotateLeft(state[b] ^ state[c], 7);
131+
}
132+
133+
private static int littleEndianInt(byte[] bytes, int offset) {
134+
return (bytes[offset] & 0xFF) | ((bytes[offset + 1] & 0xFF) << 8) | ((bytes[offset + 2] & 0xFF) << 16) | ((bytes[offset + 3] & 0xFF) << 24);
135+
}
136+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ private HexToOct() {
1414
*
1515
* @param hex The Hexadecimal number as a String.
1616
* @return The Decimal equivalent as an integer.
17+
* @throws IllegalArgumentException if the string contains a character that is not a hexadecimal digit
1718
*/
1819
public static int hexToDecimal(String hex) {
1920
String hexDigits = "0123456789ABCDEF";
@@ -23,6 +24,9 @@ public static int hexToDecimal(String hex) {
2324
for (int i = 0; i < hex.length(); i++) {
2425
char hexChar = hex.charAt(i);
2526
int digitValue = hexDigits.indexOf(hexChar);
27+
if (digitValue == -1) {
28+
throw new IllegalArgumentException("Invalid hexadecimal digit: " + hexChar);
29+
}
2630
decimalValue = 16 * decimalValue + digitValue;
2731
}
2832

‎src/main/java/com/thealgorithms/maths/DistanceFormula.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ private DistanceFormula() {
66

77
public static double euclideanDistance(double x1, double y1, double x2, double y2) {
88
double dX = Math.pow(x2 - x1, 2);
9-
double dY = Math.pow(y2 - x1, 2);
9+
double dY = Math.pow(y2 - y1, 2);
1010
return Math.sqrt(dX + dY);
1111
}
1212

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.misc;
2+
3+
/**
4+
* Moves all zeroes in an array to the end while maintaining the relative
5+
* order of non-zero elements. The operation is performed in-place.
6+
*
7+
* <p>Example: [0, 1, 0, 3, 12] -> [1, 3, 12, 0, 0]
8+
*
9+
* <p>Time complexity: O(n), Space complexity: O(1)
10+
*
11+
* @see <a href="https://leetcode.com/problems/move-zeroes/">LeetCode #283</a>
12+
*/
13+
public final class MoveZeroes {
14+
private MoveZeroes() {
15+
}
16+
17+
public static void moveZeroes(int[] nums) {
18+
int insertPos = 0;
19+
for (int num : nums) {
20+
if (num != 0) {
21+
nums[insertPos++] = num;
22+
}
23+
}
24+
while (insertPos < nums.length) {
25+
nums[insertPos++] = 0;
26+
}
27+
}
28+
}

‎src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@
2020
* doesn't exist in the array, an array
2121
* {-1, -1} will be returned.
2222
*/
23-
public class RowColumnWiseSorted2dArrayBinarySearch implements MatrixSearchAlgorithm {
23+
public final class RowColumnWiseSorted2dArrayBinarySearch implements MatrixSearchAlgorithm {
2424

2525
@Override
2626
public <T extends Comparable<T>> int[] find(T[][] matrix, T key) {
2727
return search(matrix, key);
2828
}
2929

3030
public static <T extends Comparable<T>> int[] search(T[][] matrix, T target) {
31+
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0 || target == null) {
32+
return new int[] {-1, -1};
33+
}
34+
3135
int rowPointer = 0; // The pointer at 0th row
3236
int colPointer = matrix[0].length - 1; // The pointer at end column
3337

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.thealgorithms.ciphers;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class ChaCha20Test {
9+
10+
@Test
11+
void quarterRoundMatchesRfc8439Vector() {
12+
int[] state = {0x11111111, 0x01020304, 0x9b8d6f43, 0x01234567};
13+
14+
ChaCha20.quarterRound(state, 0, 1, 2, 3);
15+
16+
assertArrayEquals(new int[] {0xea2a92f4, 0xcb1cf8ce, 0x4581472e, 0x5881c4bb}, state);
17+
}
18+
19+
@Test
20+
void encryptingZeroesMatchesRfc8439BlockVector() {
21+
byte[] key = new byte[32];
22+
for (int i = 0; i < key.length; i++) {
23+
key[i] = (byte) i;
24+
}
25+
26+
byte[] nonce = {0, 0, 0, 9, 0, 0, 0, 0x4a, 0, 0, 0, 0};
27+
byte[] expected = {0x10, (byte) 0xf1, (byte) 0xe7, (byte) 0xe4, (byte) 0xd1, 0x3b, 0x59, 0x15, 0x50, 0x0f, (byte) 0xdd, 0x1f, (byte) 0xa3, 0x20, 0x71, (byte) 0xc4, (byte) 0xc7, (byte) 0xd1, (byte) 0xf4, (byte) 0xc7, 0x33, (byte) 0xc0, 0x68, 0x03, 0x04, 0x22, (byte) 0xaa, (byte) 0x9a,
28+
(byte) 0xc3, (byte) 0xd4, 0x6c, 0x4e, (byte) 0xd2, (byte) 0x82, 0x64, 0x46, 0x07, (byte) 0x9f, (byte) 0xaa, 0x09, 0x14, (byte) 0xc2, (byte) 0xd7, 0x05, (byte) 0xd9, (byte) 0x8b, 0x02, (byte) 0xa2, (byte) 0xb5, 0x12, (byte) 0x9c, (byte) 0xd1, (byte) 0xde, 0x16, 0x4e, (byte) 0xb9,
29+
(byte) 0xcb, (byte) 0xd0, (byte) 0x83, (byte) 0xe8, (byte) 0xa2, 0x50, 0x3c, 0x4e};
30+
31+
byte[] actual = ChaCha20.encrypt(new byte[64], key, nonce, 1);
32+
33+
assertArrayEquals(expected, actual);
34+
}
35+
36+
@Test
37+
void decryptRestoresInputAcrossMultipleBlocks() {
38+
byte[] input = new byte[130];
39+
for (int i = 0; i < input.length; i++) {
40+
input[i] = (byte) (i * 3);
41+
}
42+
43+
byte[] key = new byte[32];
44+
byte[] nonce = new byte[12];
45+
46+
byte[] encrypted = ChaCha20.encrypt(input, key, nonce, 7);
47+
byte[] decrypted = ChaCha20.decrypt(encrypted, key, nonce, 7);
48+
49+
assertArrayEquals(input, decrypted);
50+
}
51+
52+
@Test
53+
void handlesPartialBlocksAroundBlockBoundary() {
54+
for (int length : new int[] {1, 63, 65}) {
55+
byte[] input = new byte[length];
56+
for (int i = 0; i < input.length; i++) {
57+
input[i] = (byte) (i + 1);
58+
}
59+
60+
byte[] key = new byte[32];
61+
byte[] nonce = new byte[12];
62+
byte[] encrypted = ChaCha20.encrypt(input, key, nonce, 0);
63+
64+
assertArrayEquals(input, ChaCha20.decrypt(encrypted, key, nonce, 0));
65+
}
66+
}
67+
68+
@Test
69+
void emptyInputReturnsEmptyOutput() {
70+
assertArrayEquals(new byte[0], ChaCha20.encrypt(new byte[0], new byte[32], new byte[12], 1));
71+
}
72+
73+
@Test
74+
void maximumCounterCanProcessOneBlock() {
75+
byte[] input = new byte[64];
76+
77+
assertArrayEquals(input, ChaCha20.decrypt(ChaCha20.encrypt(input, new byte[32], new byte[12], 0xFFFF_FFFFL), new byte[32], new byte[12], 0xFFFF_FFFFL));
78+
}
79+
80+
@Test
81+
void counterOverflowIsRejected() {
82+
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[65], new byte[32], new byte[12], 0xFFFF_FFFFL));
83+
}
84+
85+
@Test
86+
void invalidKeyNonceAndCounterAreRejected() {
87+
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[1], new byte[31], new byte[12], 0));
88+
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[1], new byte[32], new byte[11], 0));
89+
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[1], new byte[32], new byte[12], -1));
90+
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[1], new byte[32], new byte[12], 0x1_0000_0000L));
91+
}
92+
}

‎src/test/java/com/thealgorithms/conversions/HexToOctTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.conversions;
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

@@ -30,4 +31,11 @@ public void testHexToOctal() {
3031
assertEquals(0, HexToOct.hexToOctal("0"));
3132
assertEquals(7777, HexToOct.hexToOctal("FFF"));
3233
}
34+
35+
@Test
36+
public void testInvalidHexDigitThrows() {
37+
assertThrows(IllegalArgumentException.class, () -> HexToOct.hexToDecimal("1G"));
38+
assertThrows(IllegalArgumentException.class, () -> HexToOct.hexToDecimal("0x1A"));
39+
assertThrows(IllegalArgumentException.class, () -> HexToOct.hexToOctal("Z"));
40+
}
3341
}

0 commit comments

Comments
 (0)