openfheorg / openfhe-development

This is the development repository for the OpenFHE library. The current (stable) version is v1.2.1 (released on September 10, 2024).
BSD 2-Clause "Simplified" License
713 stars 185 forks source link

Uncaught exception 'std::out_of_range' occurs when using EvalRotate in BFV without corresponding EvalKey #675

Closed Maokami closed 6 months ago

Maokami commented 6 months ago

Hello OpenFHE team!

The following code throws an exception with the message terminate called after throwing an instance of 'std::out_of_range' what(): map::at.

#include <iostream>
#include "openfhe.h"

using namespace lbcrypto;
using namespace std;

int main(void) {
  CCParams<CryptoContextBFVRNS> parameters;
  parameters.SetRingDim(32768);
  size_t plaintext_modulus = 65537;
  parameters.SetPlaintextModulus(plaintext_modulus);
  parameters.SetMultiplicativeDepth(2);
  CryptoContext<DCRTPoly> cc = GenCryptoContext(parameters);
  cc->Enable(PKE);
  cc->Enable(KEYSWITCH);
  cc->Enable(LEVELEDSHE);
  KeyPair<DCRTPoly> keyPair;
  keyPair = cc->KeyGen();
  cc->EvalMultKeyGen(keyPair.secretKey);
  size_t slots(cc->GetRingDimension());
  vector<int64_t> tmp_vec_(slots);
  Plaintext tmp;
  vector<int> rots = {1};
  cc->EvalRotateKeyGen(keyPair.secretKey, rots);
  Ciphertext<DCRTPoly> tmp_;

  Ciphertext<DCRTPoly> x;
  int c;
  vector<int64_t> tmp_vec_1 = { 1, 2, 3 };
  tmp = cc->MakePackedPlaintext(tmp_vec_1);
  x = cc->Encrypt(keyPair.publicKey, tmp);

  // use EvalRotate without corresponding EvalKey
  x = cc->EvalRotate(x, 0);
  return 0;
}

When I changed the scheme to BGV or CKKS, the code produced a more detailed error message:

terminate called after throwing an instance of 'lbcrypto::openfhe_error'
  what():  OpenFHE/src/pke/lib/schemebase/base-leveledshe.cpp:422 EvalKey for index [1] is not found. [called from: OpenFHE/src/pke/lib/schemebase/base-leveledshe.cpp:EvalAtIndex():l.540]
Maokami commented 6 months ago

related to #362

yspolyakov commented 6 months ago

Please ask this question in the Discourse forum: https://openfhe.discourse.group/ BTW, EvalRotate is executed for index 0. What is meant by that? Should it not be 1? But please continue the discussion in the forum. I am closing the issue.