2021SC@SDUSC
目录
介绍
这次是介绍有关 depth-bfvruns.cpp 的示例程序。
这次介绍的 BFVrns 程序演示了深度6的同态乘法和深度3乘法的三种不同方法。
大体步骤
设置相关参数
std::cout << "\nThis code demonstrates the use of the BFVrns scheme for "
"homomorphic multiplication. "
<< std::endl;
std::cout
<< "This code shows how to auto-generate parameters during run-time "
"based on desired plaintext moduli and security levels. "
<< std::endl;
std::cout << "In this demonstration we use three input plaintext and show "
"how to both add them together and multiply them together. "
<< std::endl;
// 基准变量
TimeVar t;
double processingTime(0.0);
usint plaintextModulus = 536903681;
double sigma = 3.2;
SecurityLevel securityLevel = HEStd_128_classic;
参数迭代
EncodingParams encodingParams(
std::make_shared<EncodingParamsImpl>(plaintextModulus));
设置 evalMults 的Crypto Parameters # = 3 ( 前3个 ) 用于支持7个密文的乘法,即ceiling{log2{7}}最大深度设置为3(后3个),用于生成s^2和s^3的同态求值乘法键。
CryptoContext<DCRTPoly> cryptoContext =
CryptoContextFactory<DCRTPoly>::genCryptoContextBFVrns(
encodingParams, securityLevel, sigma, 0, 3, 0, OPTIMIZED, 3);
启用你希望使用的功能
cryptoContext->Enable(ENCRYPTION);
cryptoContext->Enable(SHE);
std::cout << "\np = "
<< cryptoContext->GetCryptoParameters()->GetPlaintextModulus()
<< std::endl;
std::cout << "n = "
<< cryptoContext->GetCryptoParameters()
->GetElementParams()
->GetCyclotomicOrder() /
2
<< std::endl;
std::cout << "log2 q = "
<< log2(cryptoContext->GetCryptoParameters()
->GetElementParams()
->GetModulus()
.ConvertToDouble())
<< std::endl;
初始化公钥内容
LPKeyPair<DCRTPoly> keyPair;
执行密钥生成操作
std::cout << "\nRunning key generation (used for source data)..."
<< std::endl;
TIC(t);
keyPair = cryptoContext->KeyGen();
processingTime = TOC(t);
std::cout << "Key generation time: " << processingTime << "ms" << std::endl;
if (!keyPair.good()) {
std::cout << "Key generation failed!" << std::endl;
exit(1);
}
std::cout << "Running key generation for homomorphic multiplication "
"evaluation keys..."
<< std::endl;
TIC(t);
cryptoContext->EvalMultKeysGen(keyPair.secretKey);
processingTime = TOC(t);
std::cout
<< "Key generation time for homomorphic multiplication evaluation keys: "
<< processingTime << "ms" << std::endl;
对源数据进行编码
std::vector<int64_t> vectorOfInts1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Plaintext plaintext1 = cryptoContext->MakePackedPlaintext(vectorOfInts1);
std::vector<int64_t> vectorOfInts2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Plaintext plaintext2 = cryptoContext->MakePackedPlaintext(vectorOfInts2);
std::vector<int64_t> vectorOfInts3 = {2, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Plaintext plaintext3 = cryptoContext->MakePackedPlaintext(vectorOfInts3);
std::vector<int64_t> vectorOfInts4 = {2, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Plaintext plaintext4 = cryptoContext->MakePackedPlaintext(vectorOfInts4);
std::vector<int64_t> vectorOfInts5 = {3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Plaintext plaintext5 = cryptoContext->MakePackedPlaintext(vectorOfInts5);
std::vector<int64_t> vectorOfInts6 = {3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Plaintext plaintext6 = cryptoContext->MakePackedPlaintext(vectorOfInts6);
std::vector<int64_t> vectorOfInts7 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Plaintext plaintext7 = cryptoContext->MakePackedPlaintext(vectorOfInts7);
cout << "\nOriginal Plaintext #1: \n";
cout << plaintext1 << endl;
cout << "\nOriginal Plaintext #2: \n";
cout << plaintext2 << endl;
cout << "\nOriginal Plaintext #3: \n";
cout << plaintext3 << endl;
cout << "\nOriginal Plaintext #4: \n";
cout << plaintext4 << endl;
cout << "\nOriginal Plaintext #5: \n";
cout << plaintext5 << endl;
cout << "\nOriginal Plaintext #6: \n";
cout << plaintext6 << endl;
cout << "\nOriginal Plaintext #7: \n";
cout << plaintext7 << endl;
加密
cout << "\nRunning encryption of all plaintexts... ";
vector<Ciphertext<DCRTPoly>> ciphertexts;
TIC(t);
ciphertexts.push_back(cryptoContext->Encrypt(keyPair.publicKey, plaintext1));
ciphertexts.push_back(cryptoContext->Encrypt(keyPair.publicKey, plaintext2));
ciphertexts.push_back(cryptoContext->Encrypt(keyPair.publicKey, plaintext3));
ciphertexts.push_back(cryptoContext->Encrypt(keyPair.publicKey, plaintext4));
ciphertexts.push_back(cryptoContext->Encrypt(keyPair.publicKey, plaintext5));
ciphertexts.push_back(cryptoContext->Encrypt(keyPair.publicKey, plaintext6));
ciphertexts.push_back(cryptoContext->Encrypt(keyPair.publicKey, plaintext7));
processingTime = TOC(t);
cout << "Completed\n";
std::cout << "\nAverage encryption time: " << processingTime / 7 << "ms"
<< std::endl;
两个密文的同态乘法
TIC(t);
auto ciphertextMult = cryptoContext->EvalMult(ciphertexts[0], ciphertexts[1]);
processingTime = TOC(t);
std::cout << "\nTotal time of multiplying 2 ciphertexts using EvalMult w/ "
"relinearization: "
<< processingTime << "ms" << std::endl;
Plaintext plaintextDecMult;
TIC(t);
cryptoContext->Decrypt(keyPair.secretKey, ciphertextMult, &plaintextDecMult);
processingTime = TOC(t);
std::cout << "\nDecryption time: " << processingTime << "ms" << std::endl;
plaintextDecMult->SetLength(plaintext1->GetLength());
cout << "\nResult of homomorphic multiplication of ciphertexts #1 and #2: \n";
cout << plaintextDecMult << endl;
7个密文的同态乘法
cout << "\nRunning a binary-tree multiplication of 7 ciphertexts...";
TIC(t);
auto ciphertextMult7 = cryptoContext->EvalMultMany(ciphertexts);
processingTime = TOC(t);
cout << "Completed\n";
std::cout << "\nTotal time of multiplying 7 ciphertexts using EvalMultMany: "
<< processingTime << "ms" << std::endl;
Plaintext plaintextDecMult7;
cryptoContext->Decrypt(keyPair.secretKey, ciphertextMult7,
&plaintextDecMult7);
plaintextDecMult7->SetLength(plaintext1->GetLength());
cout << "\nResult of 6 homomorphic multiplications: \n";
cout << plaintextDecMult7 << endl;
无任何再线性化的3个密文的同态乘法
cout << "\nRunning a depth-3 multiplication w/o relinearization...";
ciphertextMult12 =
cryptoContext->EvalMultNoRelin(ciphertexts[0], ciphertexts[1]);
ciphertextMult123 =
cryptoContext->EvalMultNoRelin(ciphertextMult12, ciphertexts[2]);
cout << "Completed\n";
cryptoContext->Decrypt(keyPair.secretKey, ciphertextMult123,
&plaintextDecMult123);
plaintextDecMult123->SetLength(plaintext1->GetLength());
cout << "\nResult of 3 homomorphic multiplications: \n";
cout << plaintextDecMult123 << endl;
3个密文的同态相乘,每次相乘后再线性化
cout << "\nRunning a depth-3 multiplication w/ relinearization after each "
"multiplication...";
TIC(t);
ciphertextMult12 = cryptoContext->EvalMult(ciphertexts[0], ciphertexts[1]);
processingTime = TOC(t);
cout << "Completed\n";
std::cout << "Time of multiplying 2 ciphertexts w/ relinearization: "
<< processingTime << "ms" << std::endl;
ciphertextMult123 = cryptoContext->EvalMult(ciphertextMult12, ciphertexts[2]);
cryptoContext->Decrypt(keyPair.secretKey, ciphertextMult123,
&plaintextDecMult123);
plaintextDecMult123->SetLength(plaintext1->GetLength());
cout << "\nResult of 3 homomorphic multiplications: \n";
cout << plaintextDecMult123 << endl;
运行结果