1 file changed
@@ -703,7 +703,7 @@ private K_PKE_KeyPair generateK_PkeKeyPair(byte[] seed) { | |||
| 703 | 703 | ||
| 704 | 704 | private K_PKE_CipherText kPkeEncrypt( | |
| 705 | 705 | 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); | ||
| 707 | 707 | byte[] pkBytes = publicKey.keyBytes; | |
| 708 | 708 | byte[] rho = Arrays.copyOfRange(pkBytes, | |
| 709 | 709 | pkBytes.length - 32, pkBytes.length); | |
@@ -792,7 +792,7 @@ private short[][][] generateA(byte[] rho, Boolean transposed) { | |||
| 792 | 792 | System.arraycopy(rho, 0, seedBuf, 0, rho.length); | |
| 793 | 793 | seedBuf[rhoLen + 2] = 0x1F; | |
| 794 | 794 | 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); | ||
| 796 | 796 | int[] iIndex = new int[nrPar]; | |
| 797 | 797 | int[] jIndex = new int[nrPar]; | |
| 798 | 798 | ||
@@ -1550,4 +1550,22 @@ private static int montMul(int b, int c) { | |||
| 1550 | 1550 | ||
| 1551 | 1551 | return (aHigh - ((m * MONT_Q) >> MONT_R_BITS)); // subtract signed high product | |
| 1552 | 1552 | } | |
| 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 | + } | ||
| 1553 | 1571 | } | |
0 commit comments