# Secure Convolution Neural Network Inference Based on Homomorphic Encryption
## 4 Our Scheme
### Batch Processing in Inference Stage
```
1:Image matrix of the size \(h\times w\), kernel size 4 (height and width), batch size \(bs\), strides 2 (height and width), each slots of every window to batch operation
2:The \(k\) ciphertexts \(ct.S_{[i]}\) for \(1\leq i\leq k\), each cipher stores the same position of convlution window of batch images
3:each slots \(\leftarrow\) 4 \(\times\) 4 + the number of dense outputs
4:\(W\leftarrow\) Imagedivider (images, kernel, 0)
5:for\(i\) := 1 to batch size do
6:for\(k\) := 0 to \(W\).rows do
7:for\(j\) := 0 to each slots-4\({}^{2}\)do
8:\(W_{i}\gets 0\)
9:endfor
10:for\(p\) := 0 to 4\({}^{2}\)do
11:\(W_{i}\leftarrow\) Imagedivider(images, kernel, \(i\))
12:endfor
13:endfor
14:endfor
15:for i := 0 to \(W\).rows do
16:\(ct.C\gets Enc_{pk}(W_{i})\)
17:\(S_{i}\gets ct.C\)
18:endfor
19:return\(S\)
```  
**Algorithm 2** Image Padding  
**Input:** Image matrix of the size \(h\times w\), kernel size 4 (height and width), batch size \(bs\), strides 2 (height and width), each slots of every window to batch operation  
**Output:** The \(k\) ciphertexts \(ct.S_{[i]}\) for \(1\leq i\leq k\), each cipher stores the same position of convlution window of batch images  
```
1:each slots \(\leftarrow\) 4 \(\times\) 4 + the number of dense outputs
2:\(W\leftarrow\) Imagedivider (images, kernel, 0)
3:for i := 1 to batch size do
4:for\(k\) := 0 to \(W\).rows do
5:for j := 0 to each slots-4\({}^{2}\)do
6:\(W_{i}\gets 0\)
7:endfor
8:for p := 0 to 4\({}^{2}\)do
9:\(W_{i}\leftarrow\) Imagedivider(images, kernel, \(i\))
10:endfor
11:endfor
12:endfor
13:for i := 0 to \(W\).rows do
14:\(ct.C\gets Enc_{pk}(W_{i})\)
15:\(S_{i}\gets ct.C\)
16:endfor
17:return\(S\)
```  
**Algorithm 3** Image Padding  
**Input:** Image matrix of the size \(h\times w\), kernel size 4 (height and width), batch size \(bs\), strides 2 (height and width), each slots of every window to batch operation  
**Output:** The \(k\) ciphertexts \(ct.S_{[i]}\) for \(1\leq i\leq k\), each cipher stores the same position of convlution window of batch images  
```
1:each slots \(\leftarrow\) 4 \(\times\) 4 + the number of dense outputs
2:\(W\leftarrow\) Imagedivider (images, kernel, 0)
3:for i := 1 to batch size do
4:for k := 0 to \(W\).rows do
5:for j := 0 to each slots-4\({}^{2}\)do
6:\(W_{i}\gets 0\)
7:endfor
8:for p := 0 to 4\({}^{2}\)do
9:\(W_{i}\leftarrow\) Imagedivider(images, kernel, \(i\))
10:endfor
11:endfor
12:endfor
13:for i := 0 to \(W\).rows do
14:\(ct.C\gets Enc_{pk}(W_{i})\)
15:\(S_{i}\gets ct.C\)
16:endfor
17:return\(S\)
```  
**Algorithm 4** Image Padding  
**Input:** Image matrix of the size \(h\times w\), kernel size 4 (height and width), batch size \(bs\), strides 2 (height and width), each slots of every window to batch operation  
**Output:** The \(k\) ciphertexts \(ct.S_{[i]}\) for \(1\leq i\leq k\), each cipher stores the same position of convlution window of batch images  
```
1:each slots \(\leftarrow\) 4 \(\times\) 4 + the number of dense outputs
2:\(W\leftarrow\) Imagedivider (images, kernel, 0)
3:for i := 1 to batch size do
4:for k := 0 to \(W\).rows do
5:for j := 0 to each slots-4\({}^{2}\)do
6:\(W_{i}\gets 0\)
7:endfor
8:for p := 0 to 4\({}^{2}\)do
9:\(W_{i}\leftarrow\) Imagedivider(images, kernel, \(i\))
10:endfor
11:endfor
12:endfor
13:for i := 0 to \(W\).rows do
14:\(ct.C\gets Enc_{pk}(W_{i})\)
15:\(S_{i}\gets ct.C\)
16:endfor
17:return\(S\)
```  
**Algorithm 5** Image Padding  
**Input:** Image matrix of the size \(h\times w\), kernel size 4 (height and width), batch size \(bs\), strides 2 (height and width), each slots of every window to batch operation  
**Output:** The \(k\) ciphertexts \(ct.S_{[i]}\) for \(1\leq i\leq k\), each cipher stores the same position of convlution window of batch images  
1:each slots \(\leftarrow\) 4 \(\times\) 4 + the number of dense outputs
2:\(W\leftarrow\) Imagedivider (images, kernel, 0)
3:for i := 1 to batch size do
4:for k := 0 to \(W\).rows do
5:for j := 0 to each slots-4\({}^{2}\)do
6:\(W_{i}\gets 0\)
7:endfor
18:for p := 0 to 4\({}^{2}\)do
19:\(W_{i}\leftarrow\) Imagedivider(images, kernel, 0)
20:endfor
21:endfor
22:endfor
23:for i := 0 to \(W\).rows do
24:\(ct.C\gets Enc_{pk}(W_{i})\)
25:\(S_{i}\gets ct.C\)
26:endfor
27:return\(S\) ```
**Algorithm 5** Image Padding  
**Input:** Image matrix of the size \(h\times w\), kernel size 4 (height and width), batch size \(bs\), strides 2 (height and width), each slots of every window to batch operation  
**Output:** The \(k\) ciphertexts \(ct.S_{[i]}\) for \(1\leq i\leq k\), each cipher stores the same position of convlution window of batch images  
1:each slots \(\leftarrow\) 4 \(\times\) 4 + the number of dense outputs
2:\(W\leftarrow\) Imagedivider (images, kernel, 0)
3:for i := 1 to batch size do
4:for k := 0 to \(W\).rows do
5:for j := 0 to each slots-4\({}^{2}\)do
6:\(W_{i}\gets 0\)
7:endfor
8:for p := 0 to 4\({}^{2}\)do
9:\(W_{i}\leftarrow\) Imagedivider(images, kernel, 0)
10:endfor
11:endfor
12:endfor
13:for i := 0 to \(W\).rows do
14:\(ct.C\gets Enc_{pk}(W_{i})\)
15:\(S_{i}\gets ct.C\)
16:endfor
17:return\(S\)
```

**Algorithm 6** Image Padding

**Input:** Image matrix of the size \(h\times w\), kernel size 4 (height and width), batch size \(bs\), strides 2 (height and width), each slots of every window to batch operation

**Output:** The \(k\) ciphertexts \(ct.S_{[i]}\) for \(1\leq i\leq k\), each cipher stores the same position of convlution window of batch images

1:each slots \(\leftarrow\) 4 \(\times\) 4 + the number of dense outputs
2:\(W\leftarrow\) Imageedivider (images, kernel, 0)
3:for i := 1 to batch size do
4:for k := 0 to \(W\).rows do
5:for j := 0 to each slots-4\({}^{2}\)do
6:\(W_{i}\gets 0\)
7:endfor
8:for p := 0 to 4\({}^{2}\)do
9:\(W_{i}\leftarrow\) Imagedivider(images, kernel, 0)



After the image window is filled, each row of the window matrix contains a batch of windows in the same position of the picture, and 0 in between.