2021SC@SDUSC
API分析
之前我们详细分析了BGVRns方案的全过程,包括密钥的生成、加密、解密以及同态操作。本篇博客分析调用BGV方案的API。
#include "palisade.h"
using namespace lbcrypto;
int main() {
//Step 1 - 设置CryptoContext对象
// Set the main parameters
// 设置明文的模数
int plaintextModulus = 65537;
double sigma = 3.2;
// 设置加密等级
SecurityLevel securityLevel = HEStd_128_classic;
// 设置深度
uint32_t depth = 2;
//实例化cryptoContext 本次加密的对象是DCRTPloy
CryptoContext<DCRTPoly> cryptoContext =
CryptoContextFactory<DCRTPoly>::genCryptoContextBGVrns(
depth, plaintextModulus, securityLevel, sigma, depth, OPTIMIZED, BV);
cryptoContext->Enable(ENCRYPTION);
cryptoContext->Enable(SHE);
cryptoContext->Enable(LEVELEDSHE);
//Step 2 - Key Generation
//初始化公钥的容器
LPKeyPair<DCRTPoly> keyPair;
//调用CryptoContext对象的KeyGen()方法,生成公钥/私钥
keyPair = cryptoContext->KeyGen();
//生成重新线性化密钥
cryptoContext->EvalMultKeyGen(keyPair.secretKey);
//生成旋转评估键
cryptoContext->EvalAtIndexKeyGen(keyPair.secretKey, {1, 2, -1, -2});
//Step 3 - Encryption
//分别编码三个明文向量
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 = {3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Plaintext plaintext2 = cryptoContext->MakePackedPlaintext(vectorOfInts2);
std::vector<int64_t> vectorOfInts3 = {1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12};
Plaintext plaintext3 = cryptoContext->MakePackedPlaintext(vectorOfInts3);
//加密已编码后的向量
auto ciphertext1 = cryptoContext->Encrypt(keyPair.publicKey, plaintext1);
auto ciphertext2 = cryptoContext->Encrypt(keyPair.publicKey, plaintext2);
auto ciphertext3 = cryptoContext->Encrypt(keyPair.publicKey, plaintext3);
//Step 4 - Evaluation
//同态加操作:将三个密文相加
auto ciphertextAdd12 = cryptoContext->EvalAdd(ciphertext1, ciphertext2);
auto ciphertextAddResult =
cryptoContext->EvalAdd(ciphertextAdd12, ciphertext3);
//同态乘操作:将三个密文相乘
auto ciphertextMul12 = cryptoContext->EvalMult(ciphertext1, ciphertext2);
auto ciphertextMultResult =
cryptoContext->EvalMult(ciphertextMul12, ciphertext3);
//同态旋转操作
auto ciphertextRot1 = cryptoContext->EvalAtIndex(ciphertext1, 1);
auto ciphertextRot2 = cryptoContext->EvalAtIndex(ciphertext1, 2);
auto ciphertextRot3 = cryptoContext->EvalAtIndex(ciphertext1, -1);
auto ciphertextRot4 = cryptoContext->EvalAtIndex(ciphertext1, -2);
//Step 5 - Decryption
//解密同态加的密文
Plaintext plaintextAddResult;
cryptoContext->Decrypt(keyPair.secretKey, ciphertextAddResult,
&plaintextAddResult);
//解密同态乘的密文
Plaintext plaintextMultResult;
cryptoContext->Decrypt(keyPair.secretKey, ciphertextMultResult,
&plaintextMultResult);
//解密同态旋转操作的密文
Plaintext plaintextRot1;
cryptoContext->Decrypt(keyPair.secretKey, ciphertextRot1, &plaintextRot1);
Plaintext plaintextRot2;
cryptoContext->Decrypt(keyPair.secretKey, ciphertextRot2, &plaintextRot2);
Plaintext plaintextRot3;
cryptoContext->Decrypt(keyPair.secretKey, ciphertextRot3, &plaintextRot3);
Plaintext plaintextRot4;
cryptoContext->Decrypt(keyPair.secretKey, ciphertextRot4, &plaintextRot4);
plaintextRot1->SetLength(vectorOfInts1.size());
plaintextRot2->SetLength(vectorOfInts1.size());
plaintextRot3->SetLength(vectorOfInts1.size());
plaintextRot4->SetLength(vectorOfInts1.size());
std::cout << "Plaintext #1: " << plaintext1 << std::endl;
std::cout << "Plaintext #2: " << plaintext2 << std::endl;
std::cout << "Plaintext #3: " << plaintext3 << std::endl;
//输出结果
std::cout << "\nResults of homomorphic computations" << std::endl;
std::cout << "#1 + #2 + #3: " << plaintextAddResult << std::endl;
std::cout << "#1 * #2 * #3: " << plaintextMultResult << std::endl;
std::cout << "Left rotation of #1 by 1: " << plaintextRot1 << std::endl;
std::cout << "Left rotation of #1 by 2: " << plaintextRot2 << std::endl;
std::cout << "Right rotation of #1 by 1: " << plaintextRot3 << std::endl;
std::cout << "Right rotation of #1 by 2: " << plaintextRot4 << std::endl;
return 0;
}
运行结果如图所示: