Skip to content

Adding ciphertext and plaintext from different contexts causes unintended errors #584

Open
@Maokami

Description

@Maokami

Hi, OpenFHE Team!

I found that when ciphertext and plaintext created in different contexts are added, unintended errors can occur.

#include "openfhe.h"

using namespace lbcrypto;

int main() {

    //c1 in cc1
    CCParams<CryptoContextCKKSRNS> parameters;
    parameters.SetMultiplicativeDepth(2);

    CryptoContext<DCRTPoly> cc1 = GenCryptoContext(parameters);

    cc1->Enable(PKE);
    cc1->Enable(LEVELEDSHE);

    auto keys = cc1->KeyGen();

    std::vector<double> x1 = {1};
    Plaintext plaintext = cc1->MakeCKKSPackedPlaintext(x1);
    auto c1 = cc1->Encrypt(keys.publicKey, plaintext);

    //p1 in cc2
    CCParams<CryptoContextCKKSRNS> parameters2;
    parameters2.SetMultiplicativeDepth(3);

    CryptoContext<DCRTPoly> cc2 = GenCryptoContext(parameters2);
    std::vector<double> x2 = {1};
    Plaintext p1 = cc2->MakeCKKSPackedPlaintext(x2);

    // Add(c1,p1) in cc1
    c1 = cc1->EvalAdd(c1,p1);

    return 0;
}

Result:

libc++abi: terminating due to uncaught exception of type lbcrypto::math_error: /project/openfhe-development/src/core/lib/math/hal/intnat/mubintvecnat.cpp:245 ModAddEq called on NativeVectorT's with different parameters.
libc++abi: terminating due to uncaught exception of type lbcrypto::math_error: /project/openfhe-development/src/core/lib/math/hal/intnat/mubintvecnat.cpp:245 ModAddEq called on NativeVectorT's with different parameters.

I believe there are several possible ways to address the issue:

  1. Throw an exception when plaintext and ciphertext created in different contexts are added.
  2. Check if the coefficient modulus of each context matches when adding plaintext and ciphertext from different contexts, and thrwo an exception if they do not.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions