export const preprocessKey = (key: string): number[][] => { /** * @param {string} key - A string of 128, 192, or 256 bits. * @returns {number[][]} A 4x4/6/8 matrix representing the key. */ const encoder = new TextEncoder() const byteArray = encoder.encode(key) const validKeyLengths = [128, 192, 256] if (!validKeyLengths.includes(byteArray.length * 8)) { throw new Error('Invalid Key!') } const N_k = (byteArray.length * 8) / 32 const matrix: number[][] = [ Array(N_k).fill(0), Array(N_k).fill(0), Array(N_k).fill(0), Array(N_k).fill(0) ] // Fill the matrix in column-major order, i.e., top-to-bottom + left-to-right // byteArray.length is either 16, 24, or 32. for (let i = 0;i < byteArray.length;i++) { const row = i % 4 const col = Math.floor(i / 4) matrix[row][col] = byteArray[i] } return matrix }