## 4 RIS-Assisted Secret Key Extraction Method
### Optimizing the Best RIS Configuration \((H_{opt})\)  
The use of the Hadamard matrix in the configuration of the RIS offers several advantages, including low complexity, high efficiency, and improved performance. This makes the Hadamard matrix effective for scenarios where reducing interference, enhancing privacy, and increasing energy efficiency are critical objectives in wireless communication systems [27]. The Hadamard matrix offers a suite of orthogonal and binary phase shift values that can be applied to the elements of the RIS to influence the reflection of incoming electromagnetic waves in a specific direction or with a preferred phase shift. The flexibility and efficacy of the Hadamard matrix in configuring the RIS to achieve these objectives while minimizing complexity makes it a promising solution for wireless communication challenges. This paper involves the measurement of the average signal-to-noise ratio (\(\overline{\text{SNR}}\)) for every configuration (\(H\)) of the OFDM system. Based on these measurements, we developed an optimization method for the RIS configuration, which is presented in Algorithm 1. This method encompasses four phases: initialization, scanning toward Bob, scanning toward Eve, and configuration optimization.  
* Initialization: [PERSON] initializes the Hadamard codebook \(HD=\sum_{i=1}^{N_{x}\times N_{y}}H_{i}\), where \(N_{x}\) and \(N_{y}\) are the number of elements in the RIS's \(x\) and \(y\) coordinates, respectively.  
Figure 3: RIS-assisted channel modeling.  
* Scanning toward Bob: [PERSON] scans the average SNR value for the received OFDM symbols from [PERSON], denoted as \(\overline{\text{SNR}}_{i}^{[PERSON]}\), for each configuration \(H_{i}\) within the set of all possible configurations, \(HD\), where \(i=1,2,\ldots,N_{x}\times N_{y}\).
* Scanning toward Eve: [PERSON] scans the average SNR value of the received OFDM symbols from [PERSON], denoted as \(\overline{\text{SNR}}_{i}^{[PERSON]}\), for each configuration \(H_{i}\) within the set of all possible configurations, \(HD\), where \(i=1,2,\ldots,N_{x}\times N_{y}\).
* Configuration optimization: [PERSON] computes the ratio of the average SNR for [PERSON] \(\overline{\text{SNR}}_{i}^{[PERSON]}\) over the average SNR for [PERSON] \(\overline{\text{SNR}}_{i}^{Eve}\), denoted as \(C_{i}\), for \(i=1,2,\ldots,N_{x}\times N_{y}\). The maximum value of \(C_{i}\), referred to as \(C_{max}\), is then determined from the set of all values of \(C_{i}\). The optimum configuration, denoted as \(H_{opt}\), is identified as the configuration \(H_{i}\) that corresponds to the maximum value of \(C_{max}\). This calculation maximizes [PERSON]'s average SNR while minimizing [PERSON]'s average SNR.  
Initialization  
```
1: The Hadamard codebook \(HD=\sum_{i=1}^{N_{x}\times N_{y}}H_{i}\) for the \((N_{x}\times N_{y})\) RIS reflecting units
2: Two empty variables, \(SNR^{[PERSON]}\) and \(SNR^{[PERSON]}\), used to store the measured SNRs
3: An empty variable \(C\)
4: [PERSON] is communicating with the legitimate terminal ([PERSON])
5:for\(i=1:(N_{x}\times N_{y})\)do
6: Measuring the average SNR value \((\overline{\text{SNR}}_{i}^{[PERSON]})\) for each Hadamard matrix \((H_{i})\)
7: Appending the measured \(\overline{\text{SNR}}_{i}^{[PERSON]}\) to \(SNR^{[PERSON]}\)
8:endfor
9: [PERSON] is communicating with the illegitimate terminal ([PERSON])
10:for\(i=1:(N_{x}\times N_{y})\)do
11: Measuring the average SNR value \((\overline{\text{SNR}}_{i}^{[PERSON]})\) for each Hadamard matrix \((H_{i})\)
12: Appending the measured \(\overline{\text{SNR}}_{i}^{[PERSON]}\) to \(SNR^{Eve}\)
13:endfor
14: Optimizing the best configuration
15:for\(i=1:(N_{x}\times N_{y})\)do
16: Computing \(C_{i}=\frac{\overline{\text{SNR}}_{i}^{Bob}}{\overline{\text{SNR}}_{i}^{Bob}}\)
17: Appending the computed \(C_{i}\) to \(C\)
18:endfor
19: Finding the best configuration (\(H_{opt}=H_{i}\)) corresponding to \(C_{max}=\max(C_{i}\in C)\)
```  
**Algorithm 1** Optimizing the Best RIS Configuration \((H_{opt})\).