# An Efficient BGV-type Encryption Scheme for IoT Systems
## Appendix A Source Code of Our Implementation
### stdafx.h
#### #pragma once
### FHE_op.h

#include <NTL/ZZ.h>
#include <NTL/BasicThreadPool.h>
#include <NTL/lzz_pXFactoring.h>
#include \"FHE.h\"
#include \"time.h\"
#include <iostream>

### Test_FHE_op.cpp

#include \"FHE_op.h\" using namespace std; int main(int argc, char \"argv) { long r = 1;//lifting long c = 2;//number of columns in key-switching matrices, \((a,b)\) long k = 32;//security parameter, corresponding to the 32-bit \"int\" space long s = 0;//minimum number of slots long d = 0;//degree of the field extension long w = 64;// Hamming weight of secret key long m = 5; long p = 11; long L = 5000;//Number of levels in the modulus chain, decide the max multiplication time cout << \"Initializing context...\" << flush; FHEcontext context(m,p,r);//initialize context buildModChain(context, L, c);//modify the context cout << \"OK!\" << endl; cout << \"Creating polynomial...\" << flush; NTL::ZZX G = context.alMod.getFactorsOverZZ()[0];//creates the polynomial cout << \"OK!\" << endl; cout << \"Generating keys...\" << flush; FHESecKey secretKey(context);//construct a secret key structure const FHEPubKey& publicKey = secretKey;//FHESecKey is a subclass of FHEPubKey secretKey.GenSecKey(w);//actually generate a secret key with Hamming weight w cout << \"OK!\" << endl; Ctxt Acc(publicKey); Ctxt ctxt(publicKey); publicKey.Encrypt(Acc, NTL::to_ZZX(1)); publicKey.Encrypt(ctxt, NTL::to_ZZX(2)); NTL::ZZX ptAcc; clock_t start, finish; double Total_time; start = clock(); for(int i=0;i<int(L/5);i+=2)//To ensure the capacity of homomorphic operations { Acc\"=ctxt; //secretKey.Decrypt(ptAcc, Acc); //cout << \"ptAcc \"<< i+1 << \"=\" << ptAcc[0] << endl; Acc+=ctxt; //secretKey.Decrypt(ptAcc, Acc); //cout << \"ptAcc \"<< i+1 << \"=\" << ptAcc[0] << endl;} } finish = clock();  Total_time = (double)(finish - start) / CLOCKS_PER_SEC;  printf(\"\
\")n running time: %f seconds \
\", Total_time);  return 0; }

## Appendix C Benchmark of RSA

This section gives the source code of a benchmark of RSA. #include <stdio.h>
#include <tchar.h>
#include <time.h>
#define test_time 1000000 int p,q,n,phin,e,d; void setup_and_key_gen() {  p=127,q=131;  n=p*q;  phin=(p-1)*(q-1);  printf(\"p:%d,q:%d,n:%d,phi(n):%d\
\",p,q,n,phin);  e=53;  for(d=1;;d++)  {  if(d*e%phin==1)break;  }  printf(\"e:%d,d:%d\
\",e,d); } int enc(int ptint e,int n) {  int ct=1;  for(int i=0;i<e;i++)  {  ct = ct * pt;  ct = ct % n;  }  return ct; } int dec(int ct,int d,int n) {  int dpt=1;  for(int i=0;i<d;i++)  {  dpt = dpt * ct;  dpt = dpt % n;  }  return dpt; } int _t_main(int argc,_TCHAR* argv[])

\(\{\) setup_and_key_gen();

int pt=2;

int ct=enc(pt,e,n);

int ct1=enc(pt,e,n);

double result;

clock_t start,end;

start = clock();

for(int i=0;i<test_time-1;i++)

\(\{\)

ct = ct\({}^{\text{*}}\)ct1;

ct = ct\({}^{\text{*}}\)on;

\(\}\)

end = clock();

result = (double)(end - start);

printf(\"rsa mul for %d times, time:%f seconds\
\", test_time, result / CLOCKS_PER_SEC);

char c;

scanf(\"%c\",&c);

return 0;

## References

* _[PERSON] et al. (2019)_ [PERSON]; [PERSON]; [PERSON] Implementing Fingerprint Recognition on One-Time Password Device to Enhance User Authentication. In _International Symposium on Cyberspace Safety and Security_; Springer: Cham, Switzerland, 2019; pp. 448-461. * _[PERSON] et al. (2020)_ [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON] Hybridization of cognitive computing for food services. _Appl. Soft Comp._**2020**, _89_, 106051. [CrossRef]
* _[PERSON] and [PERSON] (2019)_ [PERSON]; [PERSON] Security Solution Based on Raspberry PI and IoT. In _International Symposium on Cyberspace Safety and Security_; Springer: Cham, Switzerland, 2019; pp. 162-171. * _[PERSON] et al. (2020)_ [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON] Unbalanced private set intersection cardinality protocol with low communication cost. _Future Gener. Comp. Syst._**2020**, _102_, 1054-1061. [CrossRef]
* _[PERSON] et al. (2020)_ [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON] Private machine learning classification based on fully homomorphic encryption.