2021SC@SDUSC PALISADE开源库(五)公钥加密模块的示例程序(二)depth-bfvrns.cpp

2021SC@SDUSC

目录

介绍

大体步骤

设置相关参数

参数迭代

执行密钥生成操作

对源数据进行编码

加密

两个密文的同态乘法

7个密文的同态乘法

无任何再线性化的3个密文的同态乘法

3个密文的同态相乘,每次相乘后再线性化

运行结果


介绍

 这次是介绍有关 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;

运行结果

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值