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
724 stars 188 forks source link

Key's Serialization for BFV scheme #753

Closed g-s-m closed 5 months ago

g-s-m commented 5 months ago

Hi! I've discovered an issue with key's serialization for BFV scheme. The issue was discovered using openfhe-python, but it looks like a problem in openfhe library. The issue was found in the following source code

    parameters = CCParamsBFVRNS()

    parameters.SetPlaintextModulus(params.plaintext_modulus)
    parameters.SetMultiplicativeDepth(params.mult_depth)
    parameters.SetMaxRelinSkDeg(params.max_relin_sk_deg)

    cc = GenCryptoContext(parameters)

    cc.Enable(PKESchemeFeature.PKE)
    cc.Enable(PKESchemeFeature.KEYSWITCH)
    cc.Enable(PKESchemeFeature.LEVELEDSHE)
    cc.Enable(PKESchemeFeature.ADVANCEDSHE)
    cc.Enable(PKESchemeFeature.FHE)

    key_pair = cc.KeyGen()

    if not SerializeToFile(crypto_params["key_pub"], key_pair.publicKey, BINARY):
        raise Exception("Exception writing public key to pubkey.txt")

Public key could not be serialized due to a error:

2024-04-29 09:57:08,248 [MainThread  ] [ERROR]  Trying to save an unregistered polymorphic type (lbcrypto::FHEBFVRNS).
Make sure your type is registered with CEREAL_REGISTER_TYPE and that the archive you are using was included (and registered with CEREAL_REGISTER_ARCHIVE) prior to calling CEREAL_REGISTER_TYPE.
If your type is already registered and you still see this error, you may need to use CEREAL_REGISTER_DYNAMIC_INIT.

During a discussion @dsuponitskiy pointed out to the section of source code leads to the error and suggested a fix. Based on this I've prepared a pull request: https://github.com/openfheorg/openfhe-development/pull/752 .