← 返回首页
8347608: Optimize Java implementation of ML-KEM · openjdk/jdk@ecabea6 · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

Commit ecabea6

Browse files
Ben Perez
committed
8347608: Optimize Java implementation of ML-KEM
Reviewed-by: weijun
1 parent e91088a commit ecabea6

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

  • src/java.base/share/classes/com/sun/crypto/provider

‎src/java.base/share/classes/com/sun/crypto/provider/ML_KEM.java‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ private K_PKE_KeyPair generateK_PkeKeyPair(byte[] seed) {
703703

704704
private K_PKE_CipherText kPkeEncrypt(
705705
K_PKE_EncryptionKey publicKey, byte[] message, byte[] sigma) {
706-
short[][] zeroes = new short[mlKem_k][ML_KEM_N];
706+
short[][] zeroes = shortMatrixAlloc(mlKem_k, ML_KEM_N);
707707
byte[] pkBytes = publicKey.keyBytes;
708708
byte[] rho = Arrays.copyOfRange(pkBytes,
709709
pkBytes.length - 32, pkBytes.length);
@@ -792,7 +792,7 @@ private short[][][] generateA(byte[] rho, Boolean transposed) {
792792
System.arraycopy(rho, 0, seedBuf, 0, rho.length);
793793
seedBuf[rhoLen + 2] = 0x1F;
794794
seedBuf[XOF_BLOCK_LEN - 1] = (byte)0x80;
795-
byte[][] xofBufArr = new byte[nrPar][XOF_BLOCK_LEN + XOF_PAD];
795+
byte[][] xofBufArr = byteMatrixAlloc(nrPar, XOF_BLOCK_LEN + XOF_PAD);
796796
int[] iIndex = new int[nrPar];
797797
int[] jIndex = new int[nrPar];
798798

@@ -1550,4 +1550,22 @@ private static int montMul(int b, int c) {
15501550

15511551
return (aHigh - ((m * MONT_Q) >> MONT_R_BITS)); // subtract signed high product
15521552
}
1553+
1554+
// For multidimensional array initialization, manually allocating each entry is
1555+
// faster than doing the entire initialization in one go
1556+
static short[][] shortMatrixAlloc(int first, int second) {
1557+
short[][] res = new short[first][];
1558+
for (int i = 0; i < first; i++) {
1559+
res[i] = new short[second];
1560+
}
1561+
return res;
1562+
}
1563+
1564+
static byte[][] byteMatrixAlloc(int first, int second) {
1565+
byte[][] res = new byte[first][];
1566+
for (int i = 0; i < first; i++) {
1567+
res[i] = new byte[second];
1568+
}
1569+
return res;
1570+
}
15531571
}

0 commit comments

Comments
 (0)

Footer

© 2026 GitHub, Inc.