CryptoParametersRNS incorrect equality check

Description

CryptoParametersRNS inherits from CryptoParametersRLWE<DCRTPoly> thus inherits from CryptoParametersBase<DCRTPoly>. However, in its equality check, it only checks the Base part and leaves the RLWE part behind, rendering incorrect behavior when ParametersRLWE are different (e.g. m_digitSize are different)

class CryptoParametersRNS : public CryptoParametersRLWE<DCRTPoly> {

The following patch should be applied.

diff --git a/src/pke/include/schemerns/rns-cryptoparameters.h b/src/pke/include/schemerns/rns-cryptoparameters.h
index 2c2692c..b251f6e 100644
--- a/src/pke/include/schemerns/rns-cryptoparameters.h
+++ b/src/pke/include/schemerns/rns-cryptoparameters.h
@@ -192,7 +192,7 @@ public:
         if (el == nullptr)
             return false;

-        return CryptoParametersBase<DCRTPoly>::operator==(rhs) && m_scalTechnique == el->GetScalingTechnique() &&
+        return CryptoParametersRLWE<DCRTPoly>::operator==(rhs) && m_scalTechnique == el->GetScalingTechnique() &&
                m_ksTechnique == el->GetKeySwitchTechnique() && m_multTechnique == el->GetMultiplicationTechnique() &&
                m_encTechnique == el->GetEncryptionTechnique() && m_numPartQ == el->GetNumPartQ() &&
                m_auxBits == el->GetAuxBits() && m_extraBits == el->GetExtraBits() && m_PREMode == el->GetPREMode() &&

Example

When generating two CryptoContext with different digitSize, the second CryptoContext is actually the same context as the first because the CryptoContextFactory wrongly asserts their parameters are the same

    CCParams<CryptoContextBGVRNS> parameters;
    parameters.SetMultiplicativeDepth(3);
    parameters.SetPlaintextModulus(536903681);
    parameters.SetKeySwitchTechnique(BV);
    parameters.SetDigitSize(2);
    CryptoContext<DCRTPoly> cryptoContext = GenCryptoContext(parameters);
    cryptoContext->Enable(PKE);
    cryptoContext->Enable(KEYSWITCH);
    cryptoContext->Enable(LEVELEDSHE);
    KeyPair<DCRTPoly> keyPair;
    keyPair = cryptoContext->KeyGen();
    cryptoContext->EvalMultKeysGen(keyPair.secretKey);
    // inside src/pke/lib/keyswitch/keyswitch-bv.cpp
    // GetDigitSize() == 2

    parameters.SetDigitSize(30);
    CryptoContext<DCRTPoly> cryptoContext2 = GenCryptoContext(parameters);
    cryptoContext2->Enable(PKE);
    cryptoContext2->Enable(KEYSWITCH);
    cryptoContext2->Enable(LEVELEDSHE);
    KeyPair<DCRTPoly> keyPair2;
    keyPair2 = cryptoContext2->KeyGen();
    cryptoContext2->EvalMultKeysGen(keyPair2.secretKey);
    // inside src/pke/lib/keyswitch/keyswitch-bv.cpp
    // GetDigitSize() == 2, which should be 30

Thank you for reporting this bug. We created an issue for it: Fix a bug in CryptoParametersRNS' "operator==()" · Issue #905 · openfheorg/openfhe-development · GitHub

1 Like