# Optimized Implementation and Analysis of CHAM in Quantum Computing
## 3 Quantum Implementation of CHAM
### PLU Factorization  
In order to generate \(RK\) using PLU factorization [23], \(P\), \(L\), and \(U\) matrices are to be obtained, respectively, as mentioned in Section 2.3. This can also be easily obtained by using the LU() [[https://doc.sagemath.org/html/en/reference/matrices/sage/matrix/matrix2.html#sage.matrix.matrix2.Matrix.LU](https://doc.sagemath.org/html/en/reference/matrices/sage/matrix/matrix2.html#sage.matrix.matrix2.Matrix.LU)]([https://doc.sagemath.org/html/en/reference/matrices/sage/matrix/matrix2.html#sage.matrix.matrix2.Matrix.LU](https://doc.sagemath.org/html/en/reference/matrices/sage/matrix/matrix2.html#sage.matrix.matrix2.Matrix.LU)) (accessed on 10 April 2023) function of SageMath. Algorithm 1 is a quantum circuit implementation of applying \(U,L\). In each matrix, column \(n\) represents \(k[n-1]\). One thing to note is that the \(P\), \(L\), and \(U\) matrices are constants, which classifies Algorithm 1 as a classical-quantum implementation. The condition values \((U,L)\) used in the if statements in Algorithm 1 are all classical. Thus, the if statements used in Algorithm 1 are for designing the quantum circuit (i.e., programming related), not for internal if statements based on the measurement values of qubits within the quantum circuit. Conceptually, the role of the if statements is similar to that of a for the loop. In the algorithms, not all matrices consist of qubits.The \(U\) process proceeds from 1 to 16 rows of the matrix \(U\) (i.e., from top to bottom). In a row, it is meaningful because only the upper terms of the diagonal term in the matrix \(U\) have non-zero values based on the main diagonal line due to the characteristics of the upper triangular matrix. Therefore, if it searches in the right direction based on the main diagonal line, and if the value is 1, the corresponding element \(k[1+i+j]\) is set as the control qubit and the main diagonal element \(k[i]\) is set as the target qubit and then CNOT gates are applied. The \(L\) process as opposed to \(U\) proceeds in the first row direction in row 16 of the matrix \(L\), i.e., from bottom to top. \(L\) is also meaningful only for the left value of the main diagonal due to the characteristics of the lower triangular matrix. Similar to \(U\), we search the left side of the main diagonal reference, and if the value is 1, CNOT gates are applied to the corresponding element \(k[w-1-j]\) and the main diagonal element \(k[w-1-i]\). In the \(P\) process, the permutation is performed according to the matrix \(P\). Only CNOT gates are used for \(U\) and \(L\) processes, whereas only SWAP gates are used for \(P\) processes, which rearrange the order. In the \(LU\) factorization, only the input matrix is different according to the \(RK\) index (Section 2.1), but the algorithm is the same, so one operation is used. On the other hand, in the permutation step, it is not easy to define an algorithm for the \(P\) matrix for each \(RK\) index, so different SWAP gates are applied each time according to the \(P\) matrix. Algorithm 2 describes a quantum implementation of the CHAM-64/128 Key schedule applying _PLUI_ factorization. In order to recycle \(k_{0\sim 7}\), a reverse operation is performed on quantum gates included in the Transform range at Reverse (transform \(k_{0\sim 7}\)). This part is implemented using ProjectQ's Compute() and Uncompute() commands. Transform corresponds to Compute() and Reverse corresponds to Uncompute(). Since this paper is about key-schedule optimization, only the location where the encryption step is performed is mentioned as a comment, and the implementation algorithm is omitted. Figures 2 and 3 represent quantum circuit diagrams of CHAM-64/128 key schedule with PLU factorization for \(RK_{0\sim 7}\). These diagrams aim to illustrate the quantum operations actually performed when Apply L, U, and permutations are executed. These circuits are only valid for \(RK_{0\sim 7}\), and the circuit diagram differs for \(RK_{8\sim 15}\) due to the use of different applied matrices.