全同态加密(FHE)是一个强大的加密基元,可以在不接触到秘密密钥的情况下对加密数据进行计算。OpenFHE 是一个开源的 FHE 库,包括所有常见 FHE 方案的有效实现:
OpenFHE 还包括以下 FHE 的多方扩展:
OpenFHE 实现了高效的剩余数系统 (RNS) 算法以实现高性能。
https://eprint.iacr.org/2020/086.pdf
FHEW 和 TFHE 是完全同态加密 (FHE) 密码系统,可以在每次门评估后通过自举来评估加密数据上的任意布尔电路。 FHEW 密码系统最初是基于标准(环形、循环安全)LWE 假设设计的,其初始实现能够在不到 1 秒的时间内运行自举。 TFHE 密码系统使用了一些更强的假设,例如(环,循环安全)LWE 在具有二进制秘密分布的环上,并应用了一些其他优化来将引导运行时间减少到小于 0.1 秒。
我们提出了一个统一的框架,包括 FHEW 和 TFHE 密码系统的原始和扩展变体,并在开源的 PALISADE 格密码学库中使用模块化算术来实现它。我们的分析表明,这些密码系统之间的主要区别在于所使用的引导程序。FHEW 的 Alperin-Sherif-Peikert(AP)与 TFHE 的 Gama-Izabachene-Nguyen-Xie(GINX)。TFHE 的所有其他算法优化同样适用于两个密码系统。GINX 的引导方法必须使用二进制秘密,不能直接应用于其他秘密分布。在比较这两种方案的过程中,我们提出了一种简单、轻量级的方法,将 GINX 引导法(例如 TFHE 所采用的方法)扩展到三元均匀和高斯秘密分布,这些秘密分布包括在 HE 社区安全标准中。我们对不同秘密分布的 AP 和 GINX 引导方法的比较表明,TFHE/GINX 密码系统对二元和三元秘密提供了更好的性能,而 FHEW/AP 对高斯秘密更快。我们建议考虑基于三元和高斯秘密的 FHEW 和 TFHE 密码系统的变体,以便由 HE 社区进行标准化。
虚拟机运行(4 核 4gb 内存)
// First plaintext vector is encoded
std::vector<int64_t> vectorOfInts1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Plaintext plaintext1 = cryptoContext->MakePackedPlaintext(vectorOfInts1);
// The encoded vectors are encrypted
std::stringstream s2;
//Serial::Serialize(plaintext1, s1, SerType::BINARY);
Serial::Serialize(ciphertext1, s2, SerType::BINARY);
//std::cout <<"good"<
std::cout <<"The extended times is "<<(double)(sizeof(s2)/sizeof(plaintext1))<<std::endl; //24
std::cout <<sizeof(s2)<<std::endl; //392
//std::cout <<(double)(sizeof(s2)/sizeof(vectorOfInts1))<
安全系数:TOY, MEDIUM, STD128, STD192, and STD256
void RunBGVrnsAdditive() {
CCParams<CryptoContextBGVRNS> parameters;
parameters.SetPlaintextModulus(65537);
CryptoContext<DCRTPoly> cc = GenCryptoContext(parameters);
// Enable features that you wish to use
//启动模块
cc->Enable(PKE);
cc->Enable(KEYSWITCH);
cc->Enable(LEVELEDSHE);
cc->Enable(ADVANCEDSHE);
cc->Enable(MULTIPARTY);
// Set-up of parameters
// Print out the parameters
//输出参数
std::cout << "p = " << cc->GetCryptoParameters()->GetPlaintextModulus() << std::endl;
std::cout << "n = " << cc->GetCryptoParameters()->GetElementParams()->GetCyclotomicOrder() / 2 << std::endl;
std::cout << "log2 q = " << log2(cc->GetCryptoParameters()->GetElementParams()->GetModulus().ConvertToDouble())
<< std::endl;
// Initialize Public Key Containers for 3 parties
//初始化三个密钥
KeyPair<DCRTPoly> kp1;
KeyPair<DCRTPoly> kp2;
KeyPair<DCRTPoly> kp3;
KeyPair<DCRTPoly> kpMultiparty;
// Perform Key Generation Operation
std::cout << "Running key generation (used for source data)..." << std::endl;
// generate the public key for first share
//生成第一个密钥kp1
kp1 = cc->KeyGen();
// generate the public key for two shares
//利用kp1的公钥生成密钥kp2
kp2 = cc->MultipartyKeyGen(kp1.publicKey);
// generate the public key for all three secret shares
//利用kp2的公钥生成密钥kp3
kp3 = cc->MultipartyKeyGen(kp2.publicKey);
if (!kp1.good()) {
std::cout << "Key generation failed!" << std::endl;
exit(1);
}
if (!kp2.good()) {
std::cout << "Key generation failed!" << std::endl;
exit(1);
}
if (!kp3.good()) {
std::cout << "Key generation failed!" << std::endl;
exit(1);
}
// Encode source data
std::vector<int64_t> vectorOfInts1 = {1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0};
std::vector<int64_t> vectorOfInts2 = {1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0};
std::vector<int64_t> vectorOfInts3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0};
Plaintext plaintext1 = cc->MakePackedPlaintext(vectorOfInts1);
Plaintext plaintext2 = cc->MakePackedPlaintext(vectorOfInts2);
Plaintext plaintext3 = cc->MakePackedPlaintext(vectorOfInts3);
// Encryption
//加密
Ciphertext<DCRTPoly> ciphertext1;
Ciphertext<DCRTPoly> ciphertext2;
Ciphertext<DCRTPoly> ciphertext3;
//全部都用kp3来加密明文
ciphertext1 = cc->Encrypt(kp3.publicKey, plaintext1);
ciphertext2 = cc->Encrypt(kp3.publicKey, plaintext2);
ciphertext3 = cc->Encrypt(kp3.publicKey, plaintext3);
// EvalAdd Operation on Re-Encrypted Data
Ciphertext<DCRTPoly> ciphertextAdd12;
Ciphertext<DCRTPoly> ciphertextAdd123;
ciphertextAdd12 = cc->EvalAdd(ciphertext1, ciphertext2);
ciphertextAdd123 = cc->EvalAdd(ciphertextAdd12, ciphertext3);
// Decryption after Accumulation Operation on Encrypted Data with Multiparty
Plaintext plaintextAddNew1;
Plaintext plaintextAddNew2;
Plaintext plaintextAddNew3;
DCRTPoly partialPlaintext1;
DCRTPoly partialPlaintext2;
DCRTPoly partialPlaintext3;
Plaintext plaintextMultipartyNew;
const std::shared_ptr<CryptoParametersBase<DCRTPoly>> cryptoParams = kp1.secretKey->GetCryptoParameters();
const std::shared_ptr<typename DCRTPoly::Params> elementParams = cryptoParams->GetElementParams();
//部分解密
// partial decryption by first party
//kp1用kp1私钥解密part1(kp1解密叫Lead,kp2\kp3解密叫Main)
auto ciphertextPartial1 = cc->MultipartyDecryptLead({ciphertextAdd123}, kp1.secretKey);
// partial decryption by second party
//kp2用kp2私钥解密part2
auto ciphertextPartial2 = cc->MultipartyDecryptMain({ciphertextAdd123}, kp2.secretKey);
// partial decryption by third party
//kp3用kp3私钥解密part3
auto ciphertextPartial3 = cc->MultipartyDecryptMain({ciphertextAdd123}, kp3.secretKey);
std::vector<Ciphertext<DCRTPoly>> partialCiphertextVec;
partialCiphertextVec.push_back(ciphertextPartial1[0]);
partialCiphertextVec.push_back(ciphertextPartial2[0]);
partialCiphertextVec.push_back(ciphertextPartial3[0]);
// partial decryptions are combined together
// 合并所有的part
cc->MultipartyDecryptFusion(partialCiphertextVec, &plaintextMultipartyNew);
std::cout << "\n Original Plaintext: \n" << std::endl;
std::cout << plaintext1 << std::endl;
std::cout << plaintext2 << std::endl;
std::cout << plaintext3 << std::endl;
plaintextMultipartyNew->SetLength(plaintext1->GetLength());
std::cout << "\n Resulting Fused Plaintext adding 3 ciphertexts: \n" << std::endl;
std::cout << plaintextMultipartyNew << std::endl;
std::cout << "\n";
}
void RunCKKS() {
usint batchSize = 16;
CCParams<CryptoContextCKKSRNS> parameters;
parameters.SetMultiplicativeDepth(3);
parameters.SetScalingModSize(50);
parameters.SetBatchSize(batchSize);
CryptoContext<DCRTPoly> cc = GenCryptoContext(parameters);
// enable features that you wish to use
//启动模块
cc->Enable(PKE);
cc->Enable(KEYSWITCH);
cc->Enable(LEVELEDSHE);
cc->Enable(ADVANCEDSHE);
cc->Enable(MULTIPARTY);
// Set-up of parameters
// 设置参数
// Output the generated parameters
std::cout << "p = " << cc->GetCryptoParameters()->GetPlaintextModulus() << std::endl;
std::cout << "n = " << cc->GetCryptoParameters()->GetElementParams()->GetCyclotomicOrder() / 2 << std::endl;
std::cout << "log2 q = " << log2(cc->GetCryptoParameters()->GetElementParams()->GetModulus().ConvertToDouble())
<< std::endl;
// Initialize Public Key Containers
KeyPair<DCRTPoly> kp1;
KeyPair<DCRTPoly> kp2;
KeyPair<DCRTPoly> kpMultiparty;
// Perform Key Generation Operation
// 密钥生成
std::cout << "Running key generation (used for source data)..." << std::endl;
// Round 1 (party A)
// 第一轮(A产生)
std::cout << "Round 1 (party A) started." << std::endl;
// 生成 kp1
kp1 = cc->KeyGen();
// Generate evalmult key part for A
// 由kp1的secretKey生成evalMultKey
auto evalMultKey = cc->KeySwitchGen(kp1.secretKey, kp1.secretKey);
// Generate evalsum key part for A
// 由kp1的secretKey生成evalsum key——evalsum表示向量求后面所有值的和
cc->EvalSumKeyGen(kp1.secretKey);
auto evalSumKeys =
std::make_shared<std::map<usint, EvalKey<DCRTPoly>>>(cc->GetEvalSumKeyMap(kp1.secretKey->GetKeyTag()));
std::cout << "Round 1 of key generation completed." << std::endl;
// Round 2 (party B)
// 第二轮(B产生)
std::cout << "Round 2 (party B) started." << std::endl;
std::cout << "Joint public key for (s_a + s_b) is generated..." << std::endl;
// 由kp1的publicKey生成kp2
kp2 = cc->MultipartyKeyGen(kp1.publicKey);
// 由kp2的secretKey生成 evalMultKey2
auto evalMultKey2 = cc->MultiKeySwitchGen(kp2.secretKey, kp2.secretKey, evalMultKey);
std::cout << "Joint evaluation multiplication key for (s_a + s_b) is generated..." << std::endl;
// 由kp2的evalMultKey2和kp1的evalMultKey生成 evalMultAB
auto evalMultAB = cc->MultiAddEvalKeys(evalMultKey, evalMultKey2, kp2.publicKey->GetKeyTag());
std::cout << "Joint evaluation multiplication key (s_a + s_b) is transformed "
"into s_b*(s_a + s_b)..."
<< std::endl;
//由kp2的secretKey和publickey把evalMultAB 转换为 evalMultBAB
auto evalMultBAB = cc->MultiMultEvalKey(kp2.secretKey, evalMultAB, kp2.publicKey->GetKeyTag());
//由kp2的secretKey和publickey 由kp1的evalSumKeys 生成 evalSumKeysB
auto evalSumKeysB = cc->MultiEvalSumKeyGen(kp2.secretKey, evalSumKeys, kp2.publicKey->GetKeyTag());
std::cout << "Joint evaluation summation key for (s_a + s_b) is generated..." << std::endl;
//由kp2的evalSumKeysB和publickey 以及 kp1的evalSumKeys 生成最终的 evalSumKeysJoin key
auto evalSumKeysJoin = cc->MultiAddEvalSumKeys(evalSumKeys, evalSumKeysB, kp2.publicKey->GetKeyTag());
cc->InsertEvalSumKey(evalSumKeysJoin);
std::cout << "Round 2 of key generation completed." << std::endl;
// 第三轮(A产生)
std::cout << "Round 3 (party A) started." << std::endl;
std::cout << "Joint key (s_a + s_b) is transformed into s_a*(s_a + s_b)..." << std::endl;
//由kp1的secretKey和kp2的publickey把evalMultAB 转换为 evalMultAAB
auto evalMultAAB = cc->MultiMultEvalKey(kp1.secretKey, evalMultAB, kp2.publicKey->GetKeyTag());
std::cout << "Computing the final evaluation multiplication key for (s_a + "
"s_b)*(s_a + s_b)..."
<< std::endl;
//由kp1的evalMultAAB和kp2的evalMultBAB 以及公开的evalMultAB 转换为 evalMultFinal
auto evalMultFinal = cc->MultiAddEvalMultKeys(evalMultAAB, evalMultBAB, evalMultAB->GetKeyTag());
cc->InsertEvalMultKey({evalMultFinal});
std::cout << "Round 3 of key generation completed." << std::endl;
// Encode source data
std::vector<double> vectorOfInts1 = {1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 0};
std::vector<double> vectorOfInts2 = {1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0};
std::vector<double> vectorOfInts3 = {2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0};
Plaintext plaintext1 = cc->MakeCKKSPackedPlaintext(vectorOfInts1);
Plaintext plaintext2 = cc->MakeCKKSPackedPlaintext(vectorOfInts2);
Plaintext plaintext3 = cc->MakeCKKSPackedPlaintext(vectorOfInts3);
// Encryption
Ciphertext<DCRTPoly> ciphertext1;
Ciphertext<DCRTPoly> ciphertext2;
Ciphertext<DCRTPoly> ciphertext3;
// 用kp2.publickey加密
ciphertext1 = cc->Encrypt(kp2.publicKey, plaintext1);
ciphertext2 = cc->Encrypt(kp2.publicKey, plaintext2);
ciphertext3 = cc->Encrypt(kp2.publicKey, plaintext3);
// EvalAdd Operation on Re-Encrypted Data
Ciphertext<DCRTPoly> ciphertextAdd12;
Ciphertext<DCRTPoly> ciphertextAdd123;
ciphertextAdd12 = cc->EvalAdd(ciphertext1, ciphertext2);
ciphertextAdd123 = cc->EvalAdd(ciphertextAdd12, ciphertext3);
auto ciphertextMultTemp = cc->EvalMult(ciphertext1, ciphertext3);
auto ciphertextMult = cc->ModReduce(ciphertextMultTemp);
auto ciphertextEvalSum = cc->EvalSum(ciphertext3, batchSize);
// Decryption after Accumulation Operation on Encrypted Data with Multiparty
Plaintext plaintextAddNew1;
Plaintext plaintextAddNew2;
Plaintext plaintextAddNew3;
DCRTPoly partialPlaintext1;
DCRTPoly partialPlaintext2;
DCRTPoly partialPlaintext3;
Plaintext plaintextMultipartyNew;
const std::shared_ptr<CryptoParametersBase<DCRTPoly>> cryptoParams = kp1.secretKey->GetCryptoParameters();
const std::shared_ptr<typename DCRTPoly::Params> elementParams = cryptoParams->GetElementParams();
// distributed decryption
//各自用自己的secretkey解密(kp1解密叫Lead,kp2解密叫Main)
auto ciphertextPartial1 = cc->MultipartyDecryptLead({ciphertextAdd123}, kp1.secretKey);
auto ciphertextPartial2 = cc->MultipartyDecryptMain({ciphertextAdd123}, kp2.secretKey);
std::vector<Ciphertext<DCRTPoly>> partialCiphertextVec;
partialCiphertextVec.push_back(ciphertextPartial1[0]);
partialCiphertextVec.push_back(ciphertextPartial2[0]);
//合并密钥
cc->MultipartyDecryptFusion(partialCiphertextVec, &plaintextMultipartyNew);
std::cout << "\n Original Plaintext: \n" << std::endl;
std::cout << plaintext1 << std::endl;
std::cout << plaintext2 << std::endl;
std::cout << plaintext3 << std::endl;
plaintextMultipartyNew->SetLength(plaintext1->GetLength());
std::cout << "\n Resulting Fused Plaintext: \n" << std::endl;
std::cout << plaintextMultipartyNew << std::endl;
std::cout << "\n";
Plaintext plaintextMultipartyMult;
//(kp1解密叫Lead,kp2解密叫Main)
ciphertextPartial1 = cc->MultipartyDecryptLead({ciphertextMult}, kp1.secretKey);
ciphertextPartial2 = cc->MultipartyDecryptMain({ciphertextMult}, kp2.secretKey);
std::vector<Ciphertext<DCRTPoly>> partialCiphertextVecMult;
partialCiphertextVecMult.push_back(ciphertextPartial1[0]);
partialCiphertextVecMult.push_back(ciphertextPartial2[0]);
cc->MultipartyDecryptFusion(partialCiphertextVecMult, &plaintextMultipartyMult);
plaintextMultipartyMult->SetLength(plaintext1->GetLength());
std::cout << "\n Resulting Fused Plaintext after Multiplication of plaintexts 1 "
"and 3: \n"
<< std::endl;
std::cout << plaintextMultipartyMult << std::endl;
std::cout << "\n";
Plaintext plaintextMultipartyEvalSum;
ciphertextPartial1 = cc->MultipartyDecryptLead({ciphertextEvalSum}, kp1.secretKey);
ciphertextPartial2 = cc->MultipartyDecryptMain({ciphertextEvalSum}, kp2.secretKey);
std::vector<Ciphertext<DCRTPoly>> partialCiphertextVecEvalSum;
partialCiphertextVecEvalSum.push_back(ciphertextPartial1[0]);
partialCiphertextVecEvalSum.push_back(ciphertextPartial2[0]);
cc->MultipartyDecryptFusion(partialCiphertextVecEvalSum, &plaintextMultipartyEvalSum);
plaintextMultipartyEvalSum->SetLength(plaintext1->GetLength());
std::cout << "\n Fused result after the Summation of ciphertext 3: "
"\n"
<< std::endl;
std::cout << plaintextMultipartyEvalSum << std::endl;
}