Template Class CryptoContextImpl

Inheritance Relationships

Base Type

Template Parameter Order

  1. typename Element

Class Documentation

template<typename Element>
class lbcrypto::CryptoContextImpl : public lbcrypto::Serializable

Inheritence diagram for lbcrypto::CryptoContextImpl:

digraph {
    graph [bgcolor="#00000000"]
    node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2]
    edge [color="#1414CE"]
    "2" [label="lbcrypto::Serializable" tooltip="lbcrypto::Serializable"]
    "1" [label="lbcrypto::CryptoContextImpl< Element >" tooltip="lbcrypto::CryptoContextImpl< Element >" fillcolor="#BFBFBF"]
    "1" -> "2" [dir=forward tooltip="public-inheritance"]
}

Collaboration diagram for lbcrypto::CryptoContextImpl:

digraph {
    graph [bgcolor="#00000000"]
    node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2]
    edge [color="#1414CE"]
    "2" [label="lbcrypto::Serializable" tooltip="lbcrypto::Serializable"]
    "1" [label="lbcrypto::CryptoContextImpl< Element >" tooltip="lbcrypto::CryptoContextImpl< Element >" fillcolor="#BFBFBF"]
    "1" -> "2" [dir=forward tooltip="public-inheritance"]
}

A class to simplify access to OpenFHE’s PKE functionality.

Functionality is accessed by creating an instance of CryptoContextImpl. Various objects are “created” in the instance, but they can only be used with the context in which they were created.

OpenFHE methods are accessed through CryptoContextImpl methods. Guards are implemented to make certain that only valid objects that have been created in the context are used

Contexts are created using GenCryptoContext(), and can be serialized and recovered from serialization

Public Functions

inline void setSchemeId(SCHEME schemeTag)
inline SCHEME getSchemeId() const
inline CryptoContextImpl(CryptoParametersBase<Element> *params = nullptr, SchemeBase<Element> *scheme = nullptr, SCHEME schemeId = SCHEME::INVALID_SCHEME)

Constructor from raw pointers to parameters and scheme.

Parameters
  • params – pointer to CryptoParameters

  • scheme – pointer to Crypto Scheme object

  • schemeId – scheme identifier

inline CryptoContextImpl(std::shared_ptr<CryptoParametersBase<Element>> params, std::shared_ptr<SchemeBase<Element>> scheme, SCHEME schemeId = SCHEME::INVALID_SCHEME)

Constructor from shared pointers to parameters and scheme.

Parameters
  • params – shared pointer to CryptoParameters

  • scheme – sharedpointer to Crypto Scheme object

  • schemeId – scheme identifier

inline CryptoContextImpl(const CryptoContextImpl<Element> &other)

Copy constructor.

Parameters

other – cryptocontext to copy from

inline CryptoContextImpl<Element> &operator=(const CryptoContextImpl<Element> &rhs)

Assignment operator.

Parameters

rhs – cryptocontext to assign values from

Returns

this

inline operator bool() const

Checks the CryptoContextImpl object health.

Returns

true if params and scheme exists in the object

inline void Enable(PKESchemeFeature feature)

Enable a particular feature for use with this CryptoContextImpl.

Parameters

feature – the feature that should be enabled

inline void Enable(uint32_t featureMask)

Enable several features at once.

Parameters

featureMask – bitwise value of several PKESchemeFeatures

inline const std::shared_ptr<SchemeBase<Element>> GetScheme() const

Getter for Scheme.

Returns

Scheme object

inline const std::shared_ptr<CryptoParametersBase<Element>> GetCryptoParameters() const

Getter for CryptoParams.

Returns

CryptoParams

inline size_t GetKeyGenLevel() const

Getter for the level at which evaluation keys should be generated.

Attention

For future use

Returns

level

inline void SetKeyGenLevel(size_t level)

Setter for the level at which evaluation keys should be generated.

Attention

For future use

inline const std::shared_ptr<ParmType> GetElementParams() const

Getter for element params.

Returns

ElementParams

inline const EncodingParams GetEncodingParams() const

Getter for encoding params.

Returns

EncodingParams

inline uint32_t GetCyclotomicOrder() const

Getter for cyclotomic order.

Returns

CyclotomicOrder

inline uint32_t GetRingDimension() const

Getter for ring dimension.

Returns

RingDimension

inline const IntType &GetModulus() const

Getter for ciphertext modulus.

Returns

modulus

inline const IntType &GetRootOfUnity() const

Getter for root of unity.

Returns

RootOfUnity

inline CKKSDataType GetCKKSDataType() const

Getter for the CKKS data type.

Returns

data type of the CKKS data

inline Plaintext MakeStringPlaintext(const std::string &str) const

Creates a plaintext from a string using string encoding.

Parameters

str – Input string to encode.

Returns

Encoded plaintext.

inline Plaintext MakeCoefPackedPlaintext(const std::vector<int64_t> &value, size_t noiseScaleDeg = 1, uint32_t level = 0) const

Encodes a vector of integers into a coefficient-packed plaintext.

Parameters
  • value – Input vector to encode.

  • noiseScaleDeg – Degree of the scaling factor to encode the plaintext at.

  • level – Encryption level for the input vector.

Returns

Encoded plaintext.

inline Plaintext MakePackedPlaintext(const std::vector<int64_t> &value, size_t noiseScaleDeg = 1, uint32_t level = 0) const

Encodes a vector of integers into a packed plaintext.

Parameters
  • value – Input vector to encode.

  • noiseScaleDeg – Degree of the scaling factor to encode the plaintext at.

  • level – Encryption level for the input vector.

Returns

Encoded plaintext.

inline Plaintext MakeCKKSPackedPlaintext(const std::vector<std::complex<double>> &value, size_t noiseScaleDeg = 1, uint32_t level = 0, const std::shared_ptr<ParmType> params = nullptr, uint32_t slots = 0) const

Encodes a vector of complex numbers into a CKKS packed plaintext.

Parameters
  • value – Input vector to encode.

  • noiseScaleDeg – Degree of the scaling factor to encode the plaintext at.

  • level – Encryption level for the input vector.

  • params – Encoding parameters.

  • slots – Number of slots to use.

Returns

Encoded CKKS plaintext.

inline Plaintext MakeCKKSPackedPlaintext(const std::vector<double> &value, size_t noiseScaleDeg = 1, uint32_t level = 0, const std::shared_ptr<ParmType> params = nullptr, uint32_t slots = 0) const

Encodes a vector of real numbers into a CKKS packed plaintext.

Parameters
  • value – Input vector to encode.

  • noiseScaleDeg – Degree of the scaling factor to encode the plaintext at.

  • level – Encryption level for the input vector.

  • params – Encoding parameters.

  • slots – Number of slots to use.

Returns

Encoded CKKS plaintext.

inline KeyPair<Element> KeyGen() const

Generates a standard public/secret key pair.

Returns

Generated key pair.

inline KeyPair<Element> SparseKeyGen() const

Generates a sparse key pair (with special structure and without full entropy) for special use cases like ring reduction.

Attention

Not supported by any crypto scheme currently.

Returns

Generated key pair.

inline Ciphertext<Element> Encrypt(ConstPlaintext &plaintext, const PublicKey<Element> &publicKey) const

Encrypts a plaintext using the given public key.

Parameters
  • plaintext – Plaintext to encrypt.

  • publicKey – Public key to use for encryption.

Returns

Encrypted ciphertext (or null on failure).

inline Ciphertext<Element> Encrypt(const PublicKey<Element> &publicKey, ConstPlaintext &plaintext) const

Encrypts a plaintext using the given public key.

Parameters
  • publicKey – Public key to use for encryption.

  • plaintext – Plaintext to encrypt.

Returns

Encrypted ciphertext (or null on failure).

inline Ciphertext<Element> Encrypt(ConstPlaintext &plaintext, const PrivateKey<Element> &privateKey) const

Encrypts a plaintext using the given private key.

Parameters
  • plaintext – Plaintext to encrypt.

  • privateKey – Private key to use for encryption.

Returns

Encrypted ciphertext (or null on failure).

inline Ciphertext<Element> Encrypt(const PrivateKey<Element> &privateKey, ConstPlaintext &plaintext) const

Encrypts a plaintext using the given private key.

Parameters
  • privateKey – Private key to use for encryption.

  • plaintext – Plaintext to encrypt.

Returns

Encrypted ciphertext (or null on failure).

DecryptResult Decrypt(ConstCiphertext<Element> &ciphertext, const PrivateKey<Element> &privateKey, Plaintext *plaintext)

Decrypts a ciphertext using the given private key.

Parameters
  • ciphertext – Ciphertext to decrypt.

  • privateKey – Private key for decryption.

  • plaintext – Output pointer for the resulting plaintext.

Returns

Decryption result status.

inline DecryptResult Decrypt(const PrivateKey<Element> &privateKey, ConstCiphertext<Element> &ciphertext, Plaintext *plaintext)

Decrypts a ciphertext using the given private key.

Parameters
  • privateKey – Private key for decryption.

  • ciphertext – Ciphertext to decrypt.

  • plaintext – Output pointer for the resulting plaintext.

Returns

Decryption result status.

inline EvalKey<Element> KeySwitchGen(const PrivateKey<Element> &oldPrivateKey, const PrivateKey<Element> &newPrivateKey) const

Generates a key switching key from one secret key to another.

Parameters
  • oldPrivateKey – Original secret key.

  • newPrivateKey – Target secret key.

Returns

New evaluation key for key switching.

inline Ciphertext<Element> KeySwitch(ConstCiphertext<Element> &ciphertext, const EvalKey<Element> &evalKey) const

Applies key switching to a ciphertext using the given evaluation key.

Parameters
  • ciphertext – Input ciphertext.

  • evalKey – Evaluation key for key switching.

Returns

Ciphertext after key switching.

inline void KeySwitchInPlace(Ciphertext<Element> &ciphertext, const EvalKey<Element> &evalKey) const

Applies key switching in place on the given ciphertext.

Parameters
  • ciphertext – Ciphertext to modify.

  • evalKey – Evaluation key for key switching.

inline Ciphertext<Element> EvalNegate(ConstCiphertext<Element> &ciphertext) const

Negates a ciphertext.

Parameters

ciphertext – Input ciphertext.

Returns

Negated ciphertext.

inline void EvalNegateInPlace(Ciphertext<Element> &ciphertext) const

Performs in-place negation of a ciphertext.

Parameters

ciphertext – Ciphertext to negate.

inline Ciphertext<Element> EvalAdd(ConstCiphertext<Element> &ciphertext1, ConstCiphertext<Element> &ciphertext2) const

Homomorphic addition of two ciphertexts.

Parameters
  • ciphertext1 – First addend.

  • ciphertext2 – Second addend.

Returns

Resulting ciphertext.

inline void EvalAddInPlace(Ciphertext<Element> &ciphertext1, ConstCiphertext<Element> &ciphertext2) const

In-place homomorphic addition of two ciphertexts.

Parameters
  • ciphertext1 – First addend (modified in place).

  • ciphertext2 – Second addend.

inline void EvalAddInPlaceNoCheck(Ciphertext<Element> &ctxt1, ConstCiphertext<Element> &ctxt2) const
inline Ciphertext<Element> EvalAddMutable(Ciphertext<Element> &ciphertext1, Ciphertext<Element> &ciphertext2) const

Homomorphic addition of two mutable ciphertexts.

Parameters
  • ciphertext1 – First addend (may be modified).

  • ciphertext2 – Second addend (may be modified).

Returns

Resulting ciphertext.

inline void EvalAddMutableInPlace(Ciphertext<Element> &ciphertext1, Ciphertext<Element> &ciphertext2) const

In-place homomorphic addition of two mutable ciphertexts.

Parameters
  • ciphertext1 – First addend (modified in place).

  • ciphertext2 – Second addend (may be modified).

inline Ciphertext<Element> EvalAdd(ConstCiphertext<Element> &ciphertext, Plaintext &plaintext) const

Homomorphic addition of a ciphertext and a plaintext.

Parameters
  • ciphertext – Input ciphertext.

  • plaintext – Input plaintext.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalAdd(Plaintext &plaintext, ConstCiphertext<Element> &ciphertext) const

Homomorphic addition of a plaintext and a ciphertext.

Parameters
  • plaintext – Input plaintext.

  • ciphertext – Input ciphertext.

Returns

Resulting ciphertext.

inline void EvalAddInPlace(Ciphertext<Element> &ciphertext, Plaintext &plaintext) const

In-place addition of a ciphertext and a plaintext.

Parameters
  • ciphertext – Ciphertext to modify.

  • plaintext – Plaintext to add.

inline void EvalAddInPlace(Plaintext &plaintext, Ciphertext<Element> &ciphertext) const

In-place addition of a plaintext and a ciphertext.

Parameters
  • plaintext – Plaintext to add.

  • ciphertext – Ciphertext to modify.

inline Ciphertext<Element> EvalAddMutable(Ciphertext<Element> &ciphertext, Plaintext &plaintext) const

Homomorphic addition of a mutable ciphertext and a plaintext.

Parameters
  • ciphertext – Input ciphertext (may be modified).

  • plaintext – Input plaintext.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalAddMutable(Plaintext &plaintext, Ciphertext<Element> &ciphertext) const

Homomorphic addition of a plaintext and a mutable ciphertext.

Parameters
  • plaintext – Input plaintext.

  • ciphertext – Input ciphertext (may be modified).

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalAdd(ConstCiphertext<Element> &ciphertext, double scalar) const

Homomorphic addition of a ciphertext and a real number (CKKS only).

Parameters
  • ciphertext – Input ciphertext.

  • scalar – Real number to add.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalAdd(double scalar, ConstCiphertext<Element> &ciphertext) const

Homomorphic addition of a real number and a ciphertext (CKKS only).

Parameters
  • scalar – Real number to add.

  • ciphertext – Input ciphertext.

Returns

Resulting ciphertext.

inline void EvalAddInPlace(Ciphertext<Element> &ciphertext, double scalar) const

In-place addition of a ciphertext and a real number (CKKS only).

Parameters
  • ciphertext – Ciphertext to modify.

  • scalar – Real number to add.

inline void EvalAddInPlace(double scalar, Ciphertext<Element> &ciphertext) const

In-place addition of a real number and a ciphertext (CKKS only).

Parameters
  • scalar – Real number to add.

  • ciphertext – Ciphertext to modify.

inline Ciphertext<Element> EvalAdd(ConstCiphertext<Element> &ciphertext, std::complex<double> scalar) const

Homomorphic addition of a ciphertext and a complex number (CKKS only).

Parameters
  • ciphertext – Input ciphertext.

  • scalar – Complex number to add.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalAdd(std::complex<double> scalar, ConstCiphertext<Element> &ciphertext) const

Homomorphic addition of a complex number and a ciphertext (CKKS only).

Parameters
  • scalar – Complex number to add.

  • ciphertext – Input ciphertext.

Returns

Resulting ciphertext.

inline void EvalAddInPlace(Ciphertext<Element> &ciphertext, std::complex<double> scalar) const

In-place addition of a ciphertext and a complex number (CKKS only).

Parameters
  • ciphertext – Ciphertext to modify.

  • scalar – Complex number to add.

inline void EvalAddInPlace(std::complex<double> scalar, Ciphertext<Element> &ciphertext) const

In-place addition of a complex number and a ciphertext (CKKS only).

Parameters
  • scalar – Complex number to add.

  • ciphertext – Ciphertext to modify.

inline Ciphertext<Element> EvalSub(ConstCiphertext<Element> &ciphertext1, ConstCiphertext<Element> &ciphertext2) const

Homomorphic subtraction of two ciphertexts.

Parameters
  • ciphertext1 – Minuend.

  • ciphertext2 – Subtrahend.

Returns

Resulting ciphertext.

inline void EvalSubInPlace(Ciphertext<Element> &ciphertext1, ConstCiphertext<Element> &ciphertext2) const

In-place homomorphic subtraction of two ciphertexts.

Parameters
  • ciphertext1 – Minuend (modified in place).

  • ciphertext2 – Subtrahend.

inline Ciphertext<Element> EvalSubMutable(Ciphertext<Element> &ciphertext1, Ciphertext<Element> &ciphertext2) const

Homomorphic subtraction of two mutable ciphertexts.

Parameters
  • ciphertext1 – Minuend (may be modified).

  • ciphertext2 – Subtrahend (may be modified).

Returns

Resulting ciphertext.

inline void EvalSubMutableInPlace(Ciphertext<Element> &ciphertext1, Ciphertext<Element> &ciphertext2) const

In-place homomorphic subtraction of two mutable ciphertexts.

Parameters
  • ciphertext1 – Minuend (modified in place).

  • ciphertext2 – Subtrahend (may be modified).

inline Ciphertext<Element> EvalSub(ConstCiphertext<Element> &ciphertext, Plaintext &plaintext) const

Homomorphic subtraction of a ciphertext and a plaintext.

Parameters
  • ciphertext – Minuend.

  • plaintext – Subtrahend.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalSub(Plaintext &plaintext, ConstCiphertext<Element> &ciphertext) const

Homomorphic subtraction of a ciphertext from a plaintext.

Parameters
  • plaintext – Minuend.

  • ciphertext – Subtrahend.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalSubMutable(Ciphertext<Element> &ciphertext, Plaintext &plaintext) const

Homomorphic subtraction of a plaintext from a mutable ciphertext.

Parameters
  • ciphertext – Minuend (may be modified).

  • plaintext – Subtrahend.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalSubMutable(Plaintext &plaintext, Ciphertext<Element> &ciphertext) const

Homomorphic subtraction of a mutable ciphertext from a plaintext.

Parameters
  • plaintext – Minuend.

  • ciphertext – Subtrahend (may be modified).

Returns

Resulting ciphertext.

inline void EvalSubInPlace(Ciphertext<Element> &ciphertext, ConstPlaintext &plaintext) const

In-place subtraction of a plaintext from a ciphertext.

Parameters
  • ciphertext – Minuend (modified in place).

  • plaintext – Subtrahend.

inline void EvalSubInPlace(Plaintext &plaintext, Ciphertext<Element> &ciphertext) const

In-place subtraction of a ciphertext from a plaintext.

Parameters
  • plaintext – Minuend (may be modified).

  • ciphertext – Ciphertext to modify.

inline Ciphertext<Element> EvalSub(ConstCiphertext<Element> &ciphertext, double scalar) const

Homomorphic subtraction of a real number from a ciphertext (CKKS only).

Parameters
  • ciphertext – Input ciphertext.

  • scalar – Real number to subtract.

Returns

Resulting ciphertext (ciphertext - scalar).

inline Ciphertext<Element> EvalSub(double scalar, ConstCiphertext<Element> &ciphertext) const

Homomorphic subtraction of a ciphertext from a real number (CKKS only).

Parameters
  • scalar – Real number.

  • ciphertext – Ciphertext to subtract.

Returns

Resulting ciphertext (scalar - ciphertext).

inline void EvalSubInPlace(Ciphertext<Element> &ciphertext, double scalar) const

In-place subtraction of a real number from a ciphertext (CKKS only).

Parameters
  • ciphertext – Ciphertext to modify.

  • scalar – Real number to subtract.

inline void EvalSubInPlace(double scalar, Ciphertext<Element> &ciphertext) const

In-place subtraction of a ciphertext from a real number (CKKS only).

Parameters
  • scalar – Real number.

  • ciphertext – Ciphertext to modify.

inline Ciphertext<Element> EvalSub(ConstCiphertext<Element> &ciphertext, std::complex<double> scalar) const

Homomorphic subtraction of a complex number from a ciphertext (CKKS only).

Parameters
  • ciphertext – Input ciphertext.

  • scalar – Complex number to subtract.

Returns

Resulting ciphertext (ciphertext - scalar).

inline Ciphertext<Element> EvalSub(std::complex<double> scalar, ConstCiphertext<Element> &ciphertext) const

Homomorphic subtraction of a ciphertext from a complex number (CKKS only).

Parameters
  • scalar – Complex number.

  • ciphertext – Ciphertext to subtract.

Returns

Resulting ciphertext (scalar - ciphertext).

inline void EvalSubInPlace(Ciphertext<Element> &ciphertext, std::complex<double> scalar) const

In-place subtraction of a complex number from a ciphertext (CKKS only).

Parameters
  • ciphertext – Ciphertext to modify.

  • scalar – Complex number to subtract.

inline void EvalSubInPlace(std::complex<double> scalar, Ciphertext<Element> &ciphertext) const

In-place subtraction of a ciphertext from a complex number (CKKS only).

Parameters
  • scalar – Complex number.

  • ciphertext – Ciphertext to modify.

void EvalMultKeyGen(const PrivateKey<Element> &key)

Creates a relinearization key (for s^2) that can be used with the OpenFHE EvalMult operator.

Note

the new evaluation key is stored in cryptocontext

Parameters

key – secret key

void EvalMultKeysGen(const PrivateKey<Element> &key)

Creates a vector evalmult keys that can be used with the OpenFHE EvalMult operator.

Note

1st key (for s^2) is used for multiplication of ciphertexts of depth 1, 2nd key (for s^3) is used for multiplication of ciphertexts of depth 2, etc. A vector of new evaluation keys is stored in crytpocontext

Parameters

key – secret key

inline Ciphertext<Element> EvalMult(ConstCiphertext<Element> &ciphertext1, ConstCiphertext<Element> &ciphertext2) const

Homomorphic multiplication of two ciphertexts using a relinearization key.

Parameters
  • ciphertext1 – Multiplier.

  • ciphertext2 – Multiplicand.

Returns

Resulting ciphertext (ciphertext1 * ciphertext2).

inline Ciphertext<Element> EvalMultMutable(Ciphertext<Element> &ciphertext1, Ciphertext<Element> &ciphertext2) const

Homomorphic multiplication of two mutable ciphertexts using a relinearization key.

Parameters
  • ciphertext1 – Multiplier (may be modified).

  • ciphertext2 – Multiplicand (may be modified).

Returns

Resulting ciphertext (ciphertext1 * ciphertext2).

inline void EvalMultMutableInPlace(Ciphertext<Element> &ciphertext1, Ciphertext<Element> &ciphertext2) const

In-place homomorphic multiplication of two mutable ciphertexts using a relinearization key.

Parameters
  • ciphertext1 – Multiplier (modified in place).

  • ciphertext2 – Multiplicand (may be modified).

inline Ciphertext<Element> EvalSquare(ConstCiphertext<Element> &ciphertext) const

Homomorphic squaring of a ciphertext using a relinearization key.

Parameters

ciphertext – Input ciphertext.

Returns

Squared ciphertext.

inline Ciphertext<Element> EvalSquareMutable(Ciphertext<Element> &ciphertext) const

Homomorphic squaring of a mutable ciphertext using a relinearization key.

Parameters

ciphertext – Input ciphertext (may be modified).

Returns

Squared ciphertext.

inline void EvalSquareInPlace(Ciphertext<Element> &ciphertext) const

In-place homomorphic squaring of a ciphertext using a relinearization key.

Parameters

ciphertext – Ciphertext to square (modified in place).

inline Ciphertext<Element> EvalMultNoRelin(ConstCiphertext<Element> &ciphertext1, ConstCiphertext<Element> &ciphertext2) const

Homomorphic multiplication of two ciphertexts without relinearization.

Parameters
  • ciphertext1 – Multiplier.

  • ciphertext2 – Multiplicand.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalMultNoRelinNoCheck(ConstCiphertext<Element> &ctxt1, ConstCiphertext<Element> &ctxt2) const
inline Ciphertext<Element> Relinearize(ConstCiphertext<Element> &ciphertext) const

Relinearizes a ciphertext to reduce it to two components (2 polynomials per ciphertext).

Parameters

ciphertext – Input ciphertext.

Returns

Relinearized ciphertext.

inline void RelinearizeInPlace(Ciphertext<Element> &ciphertext) const

In-place relinearization of a ciphertext to reduce it to two components (2 polynomials per ciphertext).

Parameters

ciphertext – Ciphertext to relinearize (modified in place).

inline Ciphertext<Element> EvalMultAndRelinearize(ConstCiphertext<Element> &ciphertext1, ConstCiphertext<Element> &ciphertext2) const

Homomorphic multiplication of two ciphertexts followed by relinearization to the lowest level.

Parameters
  • ciphertext1 – First input ciphertext.

  • ciphertext2 – Second input ciphertext.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalMultNoCheck(ConstCiphertext<Element> &ctxt, NativeInteger k) const
inline Ciphertext<Element> EvalMult(ConstCiphertext<Element> &ciphertext, ConstPlaintext &plaintext) const

Homomorphic multiplication of a ciphertext by a plaintext.

Parameters
  • ciphertext – Multiplier.

  • plaintext – Multiplicand.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalMult(ConstPlaintext &plaintext, ConstCiphertext<Element> &ciphertext) const

Homomorphic multiplication of a plaintext by a ciphertext.

Parameters
  • plaintext – Multiplier.

  • ciphertext – Multiplicand.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalMultMutable(Ciphertext<Element> &ciphertext, Plaintext &plaintext) const

Homomorphic multiplication of a mutable ciphertext and a plaintext.

Parameters
  • ciphertext – Multiplier (may be modified).

  • plaintext – Multiplicand.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalMultMutable(Plaintext &plaintext, Ciphertext<Element> &ciphertext) const

Homomorphic multiplication of a mutable plaintext and a ciphertext.

Parameters
  • plaintext – Multiplier.

  • ciphertext – Multiplicand.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalMult(ConstCiphertext<Element> &ciphertext, double scalar) const

Homomorphic multiplication of a ciphertext by a real number (CKKS only).

Parameters
  • ciphertext – Multiplier.

  • scalar – Real number multiplicand.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalMult(double scalar, ConstCiphertext<Element> &ciphertext) const

Homomorphic multiplication of a ciphertext by a real number (CKKS only).

Parameters
  • scalar – Real number multiplier.

  • ciphertext – Multiplicand.

Returns

Resulting ciphertext.

inline void EvalMultInPlace(Ciphertext<Element> &ciphertext, double scalar) const

In-place multiplication of a ciphertext by a real number (CKKS only).

Parameters
  • ciphertext – Ciphertext to modify.

  • scalar – Real number multiplicand.

inline void EvalMultInPlace(double scalar, Ciphertext<Element> &ciphertext) const

In-place multiplication of a ciphertext by a real number (CKKS only).

Parameters
  • scalar – Real number multiplier.

  • ciphertext – Ciphertext to modify (multiplicand).

inline Ciphertext<Element> EvalMult(ConstCiphertext<Element> &ciphertext, std::complex<double> scalar) const

Homomorphic multiplication of a ciphertext by a complex number (CKKS only).

Parameters
  • ciphertext – Multiplier.

  • scalar – Complex number multiplicand.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalMult(std::complex<double> scalar, ConstCiphertext<Element> &ciphertext) const

Homomorphic multiplication of a ciphertext by a complex number (CKKS only).

Parameters
  • scalar – Complex number multiplier.

  • ciphertext – Multiplicand.

Returns

Resulting ciphertext.

inline void EvalMultInPlace(Ciphertext<Element> &ciphertext, std::complex<double> scalar) const

In-place multiplication of a ciphertext by a complex number (CKKS only).

Parameters
  • ciphertext – Ciphertext to modify.

  • scalar – Complex number multiplicand.

inline void EvalMultInPlace(std::complex<double> scalar, Ciphertext<Element> &ciphertext) const

In-place multiplication of a ciphertext by a complex number (CKKS only).

Parameters
  • scalar – Complex number multiplier.

  • ciphertext – Ciphertext to modify (multiplicand).

inline std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> EvalAutomorphismKeyGen(const PrivateKey<Element> privateKey, const std::vector<uint32_t> &indexList) const

Generates automorphism evaluation keys for the given private key.

Parameters
  • privateKey – Private key to use for key generation.

  • indexList – List of automorphism indices to be computed.

Returns

Map of generated evaluation keys.

inline Ciphertext<Element> EvalAutomorphism(ConstCiphertext<Element> &ciphertext, uint32_t i, const std::map<uint32_t, EvalKey<Element>> &evalKeyMap, CALLER_INFO_ARGS_HDR) const

Applies an automorphism to a ciphertext using the given evaluation keys.

Parameters
  • ciphertext – Input ciphertext.

  • i – Automorphism index.

  • evalKeyMap – Map of evaluation keys generated by EvalAutomorphismKeyGen.

Returns

Transformed ciphertext.

inline uint32_t FindAutomorphismIndex(const uint32_t idx) const

Computes the automorphism index for a given vector index.

Parameters

idx – Vector index.

Returns

Corresponding automorphism index.

inline std::vector<uint32_t> FindAutomorphismIndices(const std::vector<uint32_t> &idxList) const

Computes automorphism indices for a list of vector indices.

Parameters

idxList – List of vector indices.

Returns

Vector of corresponding automorphism indices.

inline Ciphertext<Element> EvalRotate(ConstCiphertext<Element> &ciphertext, int32_t index) const

Rotates a ciphertext by the given index using a stored rotation key.

Parameters
  • ciphertext – Input ciphertext.

  • index – Rotation index (positive for left, negative for right).

Returns

Rotated ciphertext.

inline std::shared_ptr<std::vector<Element>> EvalFastRotationPrecompute(ConstCiphertext<Element> &ciphertext) const

implements the precomputation step of hoisted automorphisms.

Please refer to Section 5 of Halevi and Shoup, “Faster Homomorphic linear transformations in HELib.” for more details, link: https://eprint.iacr.org/2018/244.

Generally, automorphisms are performed with three steps: (1) the automorphism is applied on the ciphertext (2) the automorphed values are decomposed into digits (3) key switching is applied to make it possible to further compute on the ciphertext.

Hoisted automorphisms is a technique that performs the digit decomposition for the original ciphertext first, and then performs the automorphism and the key switching on the decomposed digits. The benefit of this is that the digit decomposition is independent of the automorphism rotation index, so it can be reused for multiple different indices. This can greatly improve performance when we have to compute many automorphisms on the same ciphertext. This routinely happens when we do permutations (EvalPermute).

it implements the digit decomposition step of hoisted automorphisms.

Parameters

ciphertext – Input ciphertext on which to do the precomputation (digit decomposition).

Returns

Pointer to precomputed rotation data.

inline Ciphertext<Element> EvalFastRotation(ConstCiphertext<Element> &ciphertext, const uint32_t index, const uint32_t m, const std::shared_ptr<std::vector<Element>> digits) const

Implements the automorphism and key switching step of hoisted automorphisms.

Please refer to Section 5 of Halevi and Shoup, “Faster Homomorphic linear transformations in HELib.” for more details, link: https://eprint.iacr.org/2018/244.

Generally, automorphisms are performed with three steps: (1) the automorphism is applied on the ciphertext (2) the automorphed values are decomposed into digits (3) key switching is applied to make it possible to further compute on the ciphertext.

Hoisted automorphisms is a technique that performs the digit decomposition for the original ciphertext first, and then performs the automorphism and the key switching on the decomposed digits. The benefit of this is that the digit decomposition is independent of the automorphism rotation index, so it can be reused for multiple different indices. This can greatly improve performance when we have to compute many automorphisms on the same ciphertext. This routinely happens when we do permutations (EvalPermute).

This method assumes that all required rotation keys exist. This may not be true if we are using baby-step/giant-step key switching. Please refer to Section 5.1 of the above reference and EvalPermuteBGStepHoisted to see how to deal with this issue.

Parameters
  • ciphertext – Input ciphertext.

  • index – Rotation index (positive for left, negative for right).

  • m – Cyclotomic order.

  • digits – Precomputed rotation data (the digit decomposition created by EvalFastRotationPrecompute).

Returns

Rotated ciphertext.

inline Ciphertext<Element> EvalFastRotation(ConstCiphertext<Element> &ciphertext, const uint32_t index, const std::shared_ptr<std::vector<Element>> digits) const

Implements the automorphism and key switching step of hoisted automorphisms.

Please refer to Section 5 of Halevi and Shoup, “Faster Homomorphic linear transformations in HELib.” for more details, link: https://eprint.iacr.org/2018/244.

Generally, automorphisms are performed with three steps: (1) the automorphism is applied on the ciphertext (2) the automorphed values are decomposed into digits (3) key switching is applied to make it possible to further compute on the ciphertext.

Hoisted automorphisms is a technique that performs the digit decomposition for the original ciphertext first, and then performs the automorphism and the key switching on the decomposed digits. The benefit of this is that the digit decomposition is independent of the automorphism rotation index, so it can be reused for multiple different indices. This can greatly improve performance when we have to compute many automorphisms on the same ciphertext. This routinely happens when we do permutations (EvalPermute).

This method assumes that all required rotation keys exist. This may not be true if we are using baby-step/giant-step key switching. Please refer to Section 5.1 of the above reference and EvalPermuteBGStepHoisted to see how to deal with this issue.

The cyclotomic order is computed from the CryptoContext.

Parameters
  • ciphertext – Input ciphertext.

  • index – Rotation index (positive for left, negative for right).

  • digits – Precomputed rotation data (the digit decomposition created by EvalFastRotationPrecompute).

Returns

Rotated ciphertext.

inline Ciphertext<Element> EvalFastRotationExt(ConstCiphertext<Element> &ciphertext, uint32_t index, const std::shared_ptr<std::vector<Element>> digits, bool addFirst) const

Performs fast (hoisted) rotation in the extended CRT basis P*Q. Only supported with hybrid key switching.

Parameters
  • ciphertext – Input ciphertext.

  • index – Rotation index (positive for left, negative for right).

  • digits – Precomputed digits for the ciphertext.

  • addFirst – If true, the first element c0 is also computed.

Returns

Rotated ciphertext in extended basis.

inline Ciphertext<Element> KeySwitchDown(ConstCiphertext<Element> &ciphertext) const

Scales a ciphertext down from the extended CRT basis P*Q to Q. Only supported with hybrid key switching.

Parameters

ciphertext – Input ciphertext in extended basis.

Returns

Scaled ciphertext in basis Q.

inline Element KeySwitchDownFirstElement(ConstCiphertext<Element> &ciphertext) const

Scales down the first polynomial c0 from extended CRT basis P*Q to Q. Only supported with hybrid key switching.

Parameters

ciphertext – Input ciphertext in extended basis.

Returns

Scaled polynomial c0 in basis Q.

inline Ciphertext<Element> KeySwitchExt(ConstCiphertext<Element> &ciphertext, bool addFirst) const

Extends a ciphertext from basis Q to the extended CRT basis P*Q. Only supported with hybrid key switching.

Parameters
  • ciphertext – Input ciphertext in basis Q.

  • addFirst – If true, includes the first component c0 in the output.

Returns

Extended ciphertext in basis P*Q.

void EvalAtIndexKeyGen(const PrivateKey<Element> privateKey, const std::vector<int32_t> &indexList)

Generates evaluation keys for a list of rotation indices.

Parameters
  • privateKey – Private key used for key generation.

  • indexList – List of rotation indices.

inline void EvalRotateKeyGen(const PrivateKey<Element> privateKey, const std::vector<int32_t> &indexList)

Generates rotation evaluation keys for a list of indices. Internally calls EvalAtIndexKeyGen.

Parameters
  • privateKey – Private key used for key generation.

  • indexList – List of rotation indices.

Ciphertext<Element> EvalAtIndex(ConstCiphertext<Element> &ciphertext, int32_t index) const

Rotates a ciphertext by the given index using stored rotation keys. Positive index = left shift; negative index = right shift.

Parameters
  • ciphertext – Input ciphertext.

  • index – Rotation index.

Returns

Rotated ciphertext.

inline Ciphertext<Element> ComposedEvalMult(ConstCiphertext<Element> &ciphertext1, ConstCiphertext<Element> &ciphertext2) const

Performs multiplication, relinearization, and rescaling in one step. Uses a relinearization key from the crypto context.

Parameters
  • ciphertext1 – First ciphertext.

  • ciphertext2 – Second ciphertext.

Returns

Resulting ciphertext.

inline Ciphertext<Element> Rescale(ConstCiphertext<Element> &ciphertext) const

Rescales a ciphertext by reducing its modulus (alias for ModReduce in CKKS).

Parameters

ciphertext – Input ciphertext.

Returns

Rescaled ciphertext.

inline void RescaleInPlace(Ciphertext<Element> &ciphertext) const

In-place rescaling of a ciphertext (alias for ModReduceInPlace in CKKS).

Parameters

ciphertext – Ciphertext to rescale.

inline Ciphertext<Element> ModReduce(ConstCiphertext<Element> &ciphertext) const

Performs modulus reduction on a ciphertext (used in BGV/CKKS).

Parameters

ciphertext – Input ciphertext.

Returns

Modulus-reduced ciphertext.

inline void ModReduceInPlace(Ciphertext<Element> &ciphertext) const

In-place modulus reduction of a ciphertext (used in BGV/CKKS).

Parameters

ciphertext – Ciphertext to reduce.

inline Ciphertext<Element> LevelReduce(ConstCiphertext<Element> &ciphertext, const EvalKey<Element> evalKey, size_t levels = 1) const

Reduces the number of RNS limbs (levels) in a ciphertext and evaluation key.

Note

Supported in BGV and CKKS. In CKKS with COMPOSITESCALING*, levels are scaled by the composite degree.

Parameters
  • ciphertext – Input ciphertext.

  • evalKey – Evaluation key (modified in place).

  • levels – Number of levels to drop.

Returns

Ciphertext with reduced levels.

inline void LevelReduceInPlace(Ciphertext<Element> &ciphertext, const EvalKey<Element> evalKey, size_t levels = 1) const

In-place reduction of RNS limbs (levels) in a ciphertext and evaluation key.

Note

Supported in BGV and CKKS. In CKKS with COMPOSITESCALING*, levels are scaled by the composite degree.

Parameters
  • ciphertext – Ciphertext to modify.

  • evalKey – Evaluation key (modified in place).

  • levels – Number of levels to drop.

inline Ciphertext<Element> Compress(ConstCiphertext<Element> &ciphertext, uint32_t towersLeft = 1, size_t noiseScaleDeg = 1) const

Compresses a ciphertext by reducing its modulus to lower communication cost.

Parameters
  • ciphertext – Input ciphertext.

  • towersLeft – Number of RNS limbs to retain.

Returns

Compressed ciphertext.

inline Ciphertext<Element> EvalAddMany(const std::vector<Ciphertext<Element>> &ciphertextVec) const

Homomorphic addition of multiple ciphertexts using a binary tree approach.

Parameters

ciphertextVec – Vector of ciphertexts.

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalAddManyInPlace(std::vector<Ciphertext<Element>> &ciphertextVec) const

In-place homomorphic addition of multiple ciphertexts using a binary tree approach.

Parameters

ciphertextVec – Vector of ciphertexts (modified in place to store intermediate results).

Returns

Resulting ciphertext.

inline Ciphertext<Element> EvalMultMany(const std::vector<Ciphertext<Element>> &ciphertextVec) const

Homomorphic multiplication of multiple ciphertexts using a binary tree approach, followed by relinearization to reduce ciphertext size to two elements after each multiplication.

Note

Assumes each multiplication produces a ciphertext within the supported ring size (for the secret key degree used by EvalMultsKeyGen). Otherwise, it throws an error

Parameters

ciphertextVec – Vector of ciphertexts to multiply.

Returns

Resulting ciphertext.

template<typename VectorDataType = double>
inline Ciphertext<Element> EvalLinearWSum(std::vector<ReadOnlyCiphertext<Element>> &ciphertextVec, const std::vector<VectorDataType> &constantVec) const

Computes a linear weighted sum of ciphertexts (CKKS only).

Parameters
  • ciphertextVec – List of ciphertexts.

  • constantVec – Corresponding weights.

Returns

Weighted sum as a ciphertext.

template<typename VectorDataType = double>
inline Ciphertext<Element> EvalLinearWSum(const std::vector<VectorDataType> &constantsVec, std::vector<ReadOnlyCiphertext<Element>> &ciphertextVec) const

Computes a linear weighted sum of ciphertexts (CKKS only).

Parameters
  • constantVec – Corresponding weights.

  • ciphertextVec – List of ciphertexts.

Returns

Weighted sum as a ciphertext.

template<typename VectorDataType = double>
inline Ciphertext<Element> EvalLinearWSumMutable(std::vector<Ciphertext<Element>> &ciphertextVec, const std::vector<VectorDataType> &constantsVec) const

Computes a linear weighted sum using mutable ciphertexts (CKKS only).

Parameters
  • ciphertextVec – List of mutable ciphertexts.

  • constantsVec – Corresponding weights.

Returns

Weighted sum as a ciphertext.

template<typename VectorDataType = double>
inline Ciphertext<Element> EvalLinearWSumMutable(const std::vector<VectorDataType> &constantsVec, std::vector<Ciphertext<Element>> &ciphertextVec) const

Computes a linear weighted sum using mutable ciphertexts (CKKS only).

Parameters
  • constantsVec – Corresponding weights.

  • ciphertextVec – List of mutable ciphertexts.

Returns

Weighted sum as a ciphertext.

template<typename VectorDataType = double>
inline std::shared_ptr<seriesPowers<Element>> EvalPowers(ConstCiphertext<Element> &ciphertext, const std::vector<VectorDataType> &coefficients) const

Computes the powers of a ciphertext to be used when evaluating a polynomial (CKKS only). Uses EvalPowersLinear() for low polynomial degrees (degree < 5), or EvalPowersPS() for higher degrees.

Parameters
  • ciphertext – Input ciphertext.

  • coefficients – Polynomial coefficients (vector’s size = (degree + 1)).

Returns

Resulting structure of powers.

template<typename VectorDataType = double>
inline Ciphertext<Element> EvalPoly(ConstCiphertext<Element> &ciphertext, const std::vector<VectorDataType> &coefficients) const

Evaluates a polynomial (given as a power series) on a ciphertext (CKKS only). Use EvalPolyLinear() for low polynomial degrees (degree < 5), or EvalPolyPS() for higher degrees.

Parameters
  • ciphertext – Input ciphertext.

  • coefficients – Polynomial coefficients (vector’s size = (degree + 1)).

Returns

Resulting ciphertext.

template<typename VectorDataType = double>
inline Ciphertext<Element> EvalPolyWithPrecomp(std::shared_ptr<seriesPowers<Element>> powers, const std::vector<VectorDataType> &coefficients) const
template<typename VectorDataType = double>
inline Ciphertext<Element> EvalPolyLinear(ConstCiphertext<Element> &ciphertext, const std::vector<VectorDataType> &coefficients) const

Naive polynomial evaluation using a binary tree approach (efficient for low-degree polynomials, <10). Polynomials are given as a power series. Supported only in CKKS.

Parameters
  • ciphertext – Input ciphertext.

  • coefficients – Polynomial coefficients (vector’s size = degree).

Returns

Resulting ciphertext.

template<typename VectorDataType = double>
inline Ciphertext<Element> EvalPolyPS(ConstCiphertext<Element> &ciphertext, const std::vector<VectorDataType> &coefficients) const

Evaluates a polynomial (given as a power series) using the Paterson-Stockmeyer method (efficient for high-degree polynomials). Supported only in CKKS.

Parameters
  • ciphertext – Input ciphertext.

  • coefficients – Polynomial coefficients (vector’s size = degree).

Returns

Resulting ciphertext.

template<typename VectorDataType = double>
inline std::shared_ptr<seriesPowers<Element>> EvalChebyPolys(ConstCiphertext<Element> &ciphertext, const std::vector<VectorDataType> &coefficients, double a, double b) const

Computes the Chebyshev polynomials for a ciphertext to be used when evaluating a polynomial (CKKS only). Uses EvalChebyPolyLinear() for low polynomial degrees (degree < 5), or EvalChebyPolyPS() for higher degrees. Uses a linear transformation to map [a, b] to [-1, 1] using linear transformation 1 + 2(x-a)/(b-a), then applies either EvalChebyshevSeriesLinear (degree < 5) or EvalChebyshevSeriesPS depending on degree.

Parameters
  • ciphertext – Input ciphertext.

  • coefficients – Polynomial coefficients (vector’s size = (degree + 1)).

  • a – Lower bound of argument for which the coefficients were found.

  • b – Upper bound of argument for which the coefficients were found.

Returns

Resulting structure of Chebyshev polynomials.

template<typename VectorDataType = double>
inline Ciphertext<Element> EvalChebyshevSeries(ConstCiphertext<Element> &ciphertext, const std::vector<VectorDataType> &coefficients, double a, double b) const

Evaluates a Chebyshev interpolated polynomial on a ciphertext. Uses a linear transformation to map [a, b] to [-1, 1] using linear transformation 1 + 2(x-a)/(b-a), then applies either EvalChebyshevSeriesLinear (degree < 5) or EvalChebyshevSeriesPS depending on degree. Supported only in CKKS.

Parameters
  • ciphertext – Input ciphertext.

  • coefficients – Chebyshev series coefficients.

  • a – Lower bound of argument for which the coefficients were found.

  • b – Upper bound of argument for which the coefficients were found.

Returns

Resulting ciphertext.

template<typename VectorDataType = double>
inline Ciphertext<Element> EvalChebyshevSeriesWithPrecomp(std::shared_ptr<seriesPowers<Element>> polys, const std::vector<VectorDataType> &coefficients) const
template<typename VectorDataType = double>
inline Ciphertext<Element> EvalChebyshevSeriesLinear(ConstCiphertext<Element> &ciphertext, const std::vector<VectorDataType> &coefficients, double a, double b) const

Evaluates a Chebyshev interpolated polynomial using a naive linear method. Maps [a, b] to [-1, 1] using linear transformation 1 + 2(x-a)/(b-a). Supported only in CKKS.

Parameters
  • ciphertext – Input ciphertext.

  • coefficients – Chebyshev series coefficients.

  • a – Lower bound of argument for which the coefficients were found.

  • b – Upper bound of argument for which the coefficients were found.

Returns

Resulting ciphertext.

template<typename VectorDataType = double>
inline Ciphertext<Element> EvalChebyshevSeriesPS(ConstCiphertext<Element> &ciphertext, const std::vector<VectorDataType> &coefficients, double a, double b) const

Evaluates a Chebyshev interpolated polynomial using the Paterson-Stockmeyer method. Maps [a, b] to [-1, 1] using linear transformation 1 + 2(x-a)/(b-a). Supported only in CKKS.

Parameters
  • ciphertext – Input ciphertext.

  • coefficients – Chebyshev series coefficients.

  • a – Lower bound of argument for which the coefficients were found.

  • b – Upper bound of argument for which the coefficients were found.

Returns

Resulting ciphertext.

Ciphertext<Element> EvalChebyshevFunction(std::function<double(double)> func, ConstCiphertext<Element> &ciphertext, double a, double b, uint32_t degree) const

Evaluates a smooth function on a ciphertext using Chebyshev polynomial approximation over [a, b]. Supported only in CKKS.

Parameters
  • func – Function to approximate.

  • ciphertext – Input ciphertext.

  • a – Lower bound of argument for which the coefficients were found.

  • b – Upper bound of argument for which the coefficients were found.

  • degree – Degree of the Chebyshev approximation.

Returns

Ciphertext after function evaluation.

Ciphertext<Element> EvalSin(ConstCiphertext<Element> &ciphertext, double a, double b, uint32_t degree) const

Evaluates an approximate sine function on a ciphertext using Chebyshev approximation. Supported only in CKKS.

Parameters
  • ciphertext – Input ciphertext.

  • a – Lower bound of argument for which the coefficients were found.

  • b – Upper bound of argument for which the coefficients were found.

  • degree – Degree of the Chebyshev approximation.

Returns

Ciphertext after sine approximation.

Ciphertext<Element> EvalCos(ConstCiphertext<Element> &ciphertext, double a, double b, uint32_t degree) const

Evaluates an approximate cosine function on a ciphertext using Chebyshev approximation. Supported only in CKKS.

Parameters
  • ciphertext – Input ciphertext.

  • a – Lower bound of argument for which the coefficients were found.

  • b – Upper bound of argument for which the coefficients were found.

  • degree – Degree of the Chebyshev approximation.

Returns

Ciphertext after cosine approximation.

Ciphertext<Element> EvalLogistic(ConstCiphertext<Element> &ciphertext, double a, double b, uint32_t degree) const

Evaluates an approximate logistic function 1 / (1 + exp(-x)) on a ciphertext using Chebyshev approximation. Supported only in CKKS.

Parameters
  • ciphertext – Input ciphertext.

  • a – Lower bound of argument for which the coefficients were found.

  • b – Upper bound of argument for which the coefficients were found.

  • degree – Degree of the Chebyshev approximation.

Returns

Ciphertext after logistic approximation.

Ciphertext<Element> EvalDivide(ConstCiphertext<Element> &ciphertext, double a, double b, uint32_t degree) const

Evaluates an approximate reciprocal function 1 / x (for x ≥ 1) on a ciphertext using Chebyshev approximation. Supported only in CKKS.

Parameters
  • ciphertext – Input ciphertext.

  • a – Lower bound of argument for which the coefficients were found.

  • b – Upper bound of argument for which the coefficients were found.

  • degree – Degree of the Chebyshev approximation.

Returns

Ciphertext after reciprocal approximation.

void EvalSumKeyGen(const PrivateKey<Element> privateKey)

Generates evaluation keys required for homomorphic summation (EvalSum).

Parameters

privateKey – Private key used for key generation.

std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> EvalSumRowsKeyGen(const PrivateKey<Element> privateKey, uint32_t rowSize = 0, uint32_t subringDim = 0)

Generates automorphism keys for EvalSumRows (only for packed encoding).

Parameters
  • privateKey – Private key used for key generation.

  • publicKey – Public key (used in NTRU schemes; unused now).

  • rowSize – Number of slots per row in the packed matrix.

  • subringDim – Subring dimension (use cyclotomic order if 0).

Returns

Map of generated evaluation keys.

inline std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> EvalSumRowsKeyGen(const PrivateKey<Element> privateKey, const PublicKey<Element> publicKey, uint32_t rowSize = 0, uint32_t subringDim = 0)
std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> EvalSumColsKeyGen(const PrivateKey<Element> privateKey)

Generates automorphism keys for EvalSumCols (only for packed encoding).

Parameters
  • privateKey – Private key used for key generation.

  • publicKey – Public key (used in NTRU schemes; unused now).

Returns

Map of generated evaluation keys.

Ciphertext<Element> EvalSum(ConstCiphertext<Element> &ciphertext, uint32_t batchSize) const

Computes the sum of all components in a packed ciphertext vector.

Parameters
  • ciphertext – Input ciphertext.

  • batchSize – Number of slots to sum over.

Returns

Resulting ciphertext containing the sum.

Ciphertext<Element> EvalSumRows(ConstCiphertext<Element> &ciphertext, uint32_t numRows, const std::map<uint32_t, EvalKey<Element>> &evalSumKeyMap, uint32_t subringDim = 0) const

Sums all elements across each row in a packed-encoded matrix ciphertext.

Parameters
  • ciphertext – Input ciphertext.

  • numRows – Number of rows in the matrix.

  • evalSumKeyMap – Map of evaluation keys generated for row summation.

  • subringDim – Subring dimension (use full cyclotomic order if 0).

Returns

Ciphertext containing row-wise sums.

Ciphertext<Element> EvalSumCols(ConstCiphertext<Element> &ciphertext, uint32_t numCols, const std::map<uint32_t, EvalKey<Element>> &evalSumKeyMap) const

Sums all elements across each column in a packed-encoded matrix ciphertext.

Parameters
  • ciphertext – Input ciphertext.

  • numCols – Number of columns in the matrix.

  • evalSumKeyMap – Map of evaluation keys generated for column summation.

Returns

Ciphertext containing column-wise sums.

Ciphertext<Element> EvalInnerProduct(ConstCiphertext<Element> &ciphertext1, ConstCiphertext<Element> &ciphertext2, uint32_t batchSize) const

Computes the inner product of two ciphertext vectors using packed encoding and EvalSum.

Parameters
  • ciphertext1 – First input ciphertext vector.

  • ciphertext2 – Second input ciphertext vector.

  • batchSize – Number of slots to sum over.

Returns

Ciphertext containing the inner product.

Ciphertext<Element> EvalInnerProduct(ConstCiphertext<Element> &ciphertext, ConstPlaintext &plaintext, uint32_t batchSize) const

Computes the inner product of a ciphertext and a plaintext using packed encoding and EvalSum.

Parameters
  • ciphertext – Encrypted input vector.

  • plaintext – Plaintext input vector.

  • batchSize – Number of slots to sum over.

Returns

Ciphertext containing the inner product.

Ciphertext<Element> EvalMerge(const std::vector<Ciphertext<Element>> &ciphertextVec) const

Merges multiple ciphertexts with values in slot 0 into a single packed ciphertext.

Note

Requires rotation keys for the necessary indices.

Parameters

ciphertextVec – Vector of ciphertexts to merge.

Returns

Merged ciphertext with values placed into slots in order.

inline EvalKey<Element> ReKeyGen(const PrivateKey<Element> oldPrivateKey, const PublicKey<Element> newPublicKey) const

Generates a re-encryption key for Proxy Re-Encryption (PRE).

Parameters
  • oldPrivateKey – Original private key.

  • newPublicKey – Public key of the target recipient.

Returns

Re-encryption evaluation key.

EvalKey<Element> ReKeyGen(const PrivateKey<Element> originalPrivateKey, const PrivateKey<Element> newPrivateKey) const __attribute__((deprecated("functionality removed from OpenFHE")))

Produces an Eval Key that OpenFHE can use for Proxy Re-Encryption.

Attention

This functionality has been completely removed from OpenFHE

Parameters
  • oldPrivateKey – original secret key

  • newPrivateKey – new secret key

Returns

new evaluation key

inline Ciphertext<Element> ReEncrypt(ConstCiphertext<Element> &ciphertext, EvalKey<Element> evalKey, const PublicKey<Element> publicKey = nullptr) const

Re-encrypts a ciphertext using a re-encryption key for Proxy Re-Encryption.

Parameters
  • ciphertext – Input ciphertext.

  • evalKey – Re-encryption key.

  • publicKey – Optional public key of the recipient.

Returns

Re-encrypted ciphertext.

inline KeyPair<Element> MultipartyKeyGen(const std::vector<PrivateKey<Element>> &privateKeyVec)

Generates a joined public key from a set of secret key shares (Threshold FHE).

Attention

Only for debugging purposes. Not for production use.

Parameters

privateKeyVec – Vector of secret key shares.

Returns

Key pair containing this party’s private key and the joined public key.

inline KeyPair<Element> MultipartyKeyGen(const PublicKey<Element> publicKey, bool makeSparse = false, bool fresh = false)

Generates a joined public key using a prior public key and the current party’s secret share (Threshold FHE).

Parameters
  • publicKey – joined public key from prior parties.

  • makeSparse – Use ring reduction (no longer supported).

  • fresh – Indicates if proxy re-encryption is used in the multi-party protocol or star topology is used.

Returns

Key pair containing this party’s private key and the updated joined public key.

inline std::vector<Ciphertext<Element>> MultipartyDecryptLead(const std::vector<Ciphertext<Element>> &ciphertextVec, const PrivateKey<Element> privateKey) const

Performs partial decryption as the lead decryption party (Threshold FHE).

Parameters
  • ciphertextVec – Vector of ciphertexts to decrypt.

  • privateKey – Secret key share of the lead party.

Returns

Vector of partially decrypted ciphertexts.

inline std::vector<Ciphertext<Element>> MultipartyDecryptMain(const std::vector<Ciphertext<Element>> &ciphertextVec, const PrivateKey<Element> privateKey) const

Performs partial decryption by non-lead parties in a Threshold FHE setting.

Parameters
  • ciphertextVec – Vector of ciphertexts to decrypt.

  • privateKey – Secret key share of a non-lead party.

Returns

Vector of partially decrypted ciphertexts.

inline DecryptResult MultipartyDecryptFusion(const std::vector<Ciphertext<Element>> &partialCiphertextVec, Plaintext *plaintext) const

Combines partially decrypted ciphertexts into the final plaintext result (Threshold FHE).

Parameters
  • partialCiphertextVec – Vector of partial decryptions.

  • plaintext – Output plaintext.

Returns

Decoding result.

inline EvalKey<Element> MultiKeySwitchGen(const PrivateKey<Element> originalPrivateKey, const PrivateKey<Element> newPrivateKey, const EvalKey<Element> evalKey) const

Generates a new joined evaluation key from a prior key and secret key shares (Threshold FHE).

Parameters
  • originalPrivateKey – Original private key.

  • newPrivateKey – New private key.

  • evalKey – Prior joined evaluation key.

Returns

New joined evaluation key.

inline std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> MultiEvalAutomorphismKeyGen(const PrivateKey<Element> privateKey, const std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> evalKeyMap, const std::vector<uint32_t> &indexList, const std::string &keyTag = "")

Generates joined automorphism keys from the current secret share and prior keys (Threshold FHE).

Parameters
  • privateKey – Secret key share.

  • evalKeyMap – Prior joined automorphism keys.

  • indexList – List of automorphism indices.

  • keyTag – Secret key tag (optional).

Returns

Map of updated joined automorphism keys.

inline std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> MultiEvalAtIndexKeyGen(const PrivateKey<Element> privateKey, const std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> evalKeyMap, const std::vector<int32_t> &indexList, const std::string &keyTag = "")

Generates joined rotation keys from the current secret share and prior keys (Threshold FHE).

Parameters
  • privateKey – Secret key share.

  • evalKeyMap – Prior joined rotation keys.

  • indexList – List of rotation indices.

  • keyTag – Secret key tag (optional).

Returns

Map of updated joined rotation keys.

inline std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> MultiEvalSumKeyGen(const PrivateKey<Element> privateKey, const std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> evalKeyMap, const std::string &keyTag = "")

Generates joined summation evaluation keys from the current secret share and prior keys (Threshold FHE).

Parameters
  • privateKey – Secret key share.

  • evalKeyMap – Prior summation evaluation keys.

  • keyTag – Secret key tag (optional).

Returns

Map of updated summation evaluation keys.

inline EvalKey<Element> MultiAddEvalKeys(EvalKey<Element> evalKey1, EvalKey<Element> evalKey2, const std::string &keyTag = "")

Adds two evaluation keys to produce a new joined evaluation key (Threshold FHE).

Parameters
  • evalKey1 – First evaluation key.

  • evalKey2 – Second evaluation key.

  • keyTag – Secret key tag (optional).

Returns

Joined evaluation key.

inline EvalKey<Element> MultiMultEvalKey(PrivateKey<Element> privateKey, EvalKey<Element> evalKey, const std::string &keyTag = "")

Generates a joined partial evaluation key for homomorphic multiplication from a partial key and current secret share (Threshold FHE).

Parameters
  • privateKey – Current secret share.

  • evalKey – Prior partial evaluation key.

  • keyTag – Secret key tag (optional).

Returns

Joined evaluation key.

inline std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> MultiAddEvalSumKeys(const std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> evalKeyMap1, const std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> evalKeyMap2, const std::string &keyTag = "")

Adds two summation evaluation key sets (Threshold FHE).

Parameters
  • evalKeyMap1 – First summation key set.

  • evalKeyMap2 – Second summation key set.

  • keyTag – Secret key tag (optional).

Returns

Combined summation evaluation key set.

inline std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> MultiAddEvalAutomorphismKeys(const std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> evalKeyMap1, const std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> evalKeyMap2, const std::string &keyTag = "")

Adds two automorphism evaluation key sets (Threshold FHE).

Parameters
  • evalKeyMap1 – First automorphism key set.

  • evalKeyMap2 – Second automorphism key set.

  • keyTag – Secret key tag (optional).

Returns

Combined automorphism evaluation key set.

inline PublicKey<Element> MultiAddPubKeys(PublicKey<Element> publicKey1, PublicKey<Element> publicKey2, const std::string &keyTag = "")

Adds two public keys to produce a combined public key (Threshold FHE).

Parameters
  • publicKey1 – First public key.

  • publicKey2 – Second public key.

  • keyTag – Secret key tag (optional).

Returns

Combined public key.

inline EvalKey<Element> MultiAddEvalMultKeys(EvalKey<Element> evalKey1, EvalKey<Element> evalKey2, const std::string &keyTag = "")

Adds two partial evaluation keys for multiplication (Threshold FHE).

Parameters
  • evalKey1 – First evaluation key.

  • evalKey2 – Second evaluation key.

  • keyTag – Secret key tag (optional).

Returns

Combined evaluation key.

inline Ciphertext<Element> IntBootDecrypt(const PrivateKey<Element> privateKey, ConstCiphertext<Element> &ciphertext) const

Performs masked decryption for interactive bootstrapping (2-party protocol).

Note

For Server, expects ciphertext with both polynomials a and b. For Client, expects only the linear term a. Includes rounding as part of decryption.

Parameters
  • privateKey – Secret key share.

  • ciphertext – Input ciphertext.

Returns

Masked decrypted ciphertext.

inline Ciphertext<Element> IntBootEncrypt(const PublicKey<Element> publicKey, ConstCiphertext<Element> &ciphertext) const

Encrypts Client’s masked decryption for interactive bootstrapping. Increases ciphertext modulus to allow further computation. Done by Client.

Parameters
  • publicKey – Joined public key (Threshold FHE).

  • ciphertext – Input ciphertext.

Returns

Encrypted ciphertext.

inline Ciphertext<Element> IntBootAdd(ConstCiphertext<Element> &ciphertext1, ConstCiphertext<Element> &ciphertext2) const

Combines encrypted and unencrypted masked decryptions in 2-party interactive bootstrapping. It is the last step in the boostrapping.

Parameters
  • ciphertext1 – Encrypted masked decryption.

  • ciphertext2 – Unencrypted masked decryption.

Returns

Refreshed ciphertext.

inline Ciphertext<Element> IntBootAdjustScale(ConstCiphertext<Element> &ciphertext) const

Prepares a ciphertext for interactive bootstrapping.

Note

CKKS FIXEDMANUAL/FIXEDAUTO: requires ≥ 2 towers; reduces to 2 towers and sets scale to Delta (not a power of Delta). CKKS FLEXIBLEAUTO: requires ≥ 3 towers; adjusts scale to level 0.

Parameters

ciphertext – Input ciphertext.

Returns

Adjusted ciphertext.

Ciphertext<Element> IntMPBootAdjustScale(ConstCiphertext<Element> &ciphertext) const

Prepares a ciphertext for multi-party interactive bootstrapping (Threshold FHE).

Parameters

ciphertext – Input ciphertext.

Returns

Adjusted ciphertext.

Ciphertext<Element> IntMPBootRandomElementGen(const PublicKey<Element> publicKey) const

Generates a common random polynomial for Multi-Party Interactive Bootstrapping (Threshold FHE).

Parameters

publicKey – Scheme public key or lead party’s public key.

Returns

Random ring element as ciphertext.

Ciphertext<Element> IntMPBootRandomElementGen(ConstCiphertext<Element> &ciphertext) const

Generates a common random polynomial for Multi-Party Interactive Bootstrapping (Threshold FHE) using an existing ciphertext, eliminating the need to supply a public key explicitly.

Parameters

ciphertext – Reference ciphertext used to derive the cryptocontext and parameters.

Returns

Random ring element as ciphertext.

std::vector<Ciphertext<Element>> IntMPBootDecrypt(const PrivateKey<Element> privateKey, ConstCiphertext<Element> &ciphertext, ConstCiphertext<Element> &a) const

Performs masked decryption as part of Multi-Party Interactive Bootstrapping (Threshold FHE). Each party calls this function as part of the protocol.

Parameters
  • privateKey – Secret key share for the party.

  • ciphertext – Input ciphertext.

  • a – Common random polynomial.

Returns

Vector of masked decryption shares.

std::vector<Ciphertext<Element>> IntMPBootAdd(std::vector<std::vector<Ciphertext<Element>>> &sharesPairVec) const

Aggregates masked decryption and re-encryption shares (Threshold FHE). It is the second step of the interactive multiparty bootstrapping procedure.

Parameters

sharesPairVec – Vector of (h_0i, h_1i) shares from each party.

Returns

Aggregated pair of shares (h_0, h_1).

Ciphertext<Element> IntMPBootEncrypt(const PublicKey<Element> publicKey, const std::vector<Ciphertext<Element>> &sharesPair, ConstCiphertext<Element> &a, ConstCiphertext<Element> &ciphertext) const

Encrypts the lead party’s masked decryption result as the final step of Multi-Party Interactive Bootstrapping (Threshold FHE). It increases the ciphertext modulus and enables future computations. This operation is done by the lead party as the final step of interactive multi-party bootstrapping.

Parameters
  • publicKey – Lead party’s public key.

  • sharesPair – Aggregated masked decryption and re-encryption shares.

  • a – Common random polynomial.

  • ciphertext – Input ciphertext.

Returns

Encrypted refreshed ciphertext.

inline std::unordered_map<uint32_t, Element> ShareKeys(const PrivateKey<Element> &sk, uint32_t N, uint32_t threshold, uint32_t index, const std::string &shareType) const

Performs secret sharing of a secret key for Threshold FHE with aborts.

Parameters
  • sk – Secret key to be shared.

  • N – Total number of parties.

  • threshold – Threshold number of parties required to reconstruct the key.

  • index – Index of the current party.

  • shareType – Type of secret sharing (“additive” or “shamir”).

Returns

Map of secret key shares indexed by party ID.

void RecoverSharedKey(PrivateKey<Element> &sk, std::unordered_map<uint32_t, Element> &sk_shares, uint32_t N, uint32_t threshold, const std::string &shareType) const

Recovers a secret key share from existing shares for Threshold FHE with aborts.

Parameters
  • sk – Output: recovered secret key.

  • sk_shares – Map of secret key shares indexed by party ID.

  • N – Total number of parties.

  • threshold – Threshold number of parties required to reconstruct the key.

  • shareType – Type of secret sharing (“additive” or “shamir”).

inline void EvalBootstrapSetup(std::vector<uint32_t> levelBudget = {5, 4}, std::vector<uint32_t> dim1 = {0, 0}, uint32_t slots = 0, uint32_t correctionFactor = 0, bool precompute = true, bool BTSlotsEncoding = false)

Sets all bootstrapping parameters for both linear and FFT-like methods. Supported only in CKKS.

Bootstrap functionality: There are three methods that have to be called in this specific order:

  1. EvalBootstrapSetup: computes and encodes the coefficients for encoding and decoding and stores the necessary parameters

  2. EvalBootstrapKeyGen: computes and stores the keys for rotations and conjugation

  3. EvalBootstrapPrecompute: computes and stores the plaintexts for encoding and decoding if not already done in EvalBootstrapSetup

  4. EvalBootstrap: refreshes the given ciphertext

Parameters
  • levelBudget – Vector of level budgets for encoding and decoding.

  • dim1 – Inner dimensions for baby-step giant-step routine.

  • slots – Number of slots to be bootstrapped.

  • correctionFactor – Internal rescaling factor to improve precision (only for NATIVE_SIZE=64; 0 = default).

  • precompute – Whether to precompute plaintexts for encoding/decoding.

  • BTSlotsEncoding – Whether the approximate modular reduction happens over the message being in slots or coefficients

inline void EvalBootstrapKeyGen(const PrivateKey<Element> privateKey, uint32_t slots)

Generates automorphism keys for EvalBootstrap. Uses baby-step/giant-step strategy. Supported only in CKKS.

Parameters
  • privateKey – Secret key.

  • slots – Number of slots to support permutations on.

inline void EvalBootstrapPrecompute(uint32_t slots = 0)

Precomputes plaintexts for encoding and decoding used in bootstrapping. Supported only in CKKS.

Parameters

slots – Number of slots to be bootstrapped.

inline Ciphertext<Element> EvalBootstrap(ConstCiphertext<Element> &ciphertext, uint32_t numIterations = 1, uint32_t precision = 0) const

Evaluates bootstrapping on a ciphertext using FFT-like or linear method. Supported only in CKKS.

Parameters
  • ciphertext – Input ciphertext.

  • numIterations – Number of Meta-BTS iterations to improve precision.

  • precision – Initial bootstrapping precision (set to 0 for default; tune experimentally).

Returns

Refreshed ciphertext.

inline Ciphertext<Element> EvalBootstrapStCFirst(ConstCiphertext<Element> &ciphertext, uint32_t numIterations = 1, uint32_t precision = 0) const
template<typename VectorDataType>
inline void EvalFBTSetup(const std::vector<VectorDataType> &coeffs, uint32_t numSlots, const BigInteger &PIn, const BigInteger &POut, const BigInteger &Bigq, const PublicKey<DCRTPoly> &pubKey, const std::vector<uint32_t> &dim1, const std::vector<uint32_t> &levelBudget, uint32_t lvlsAfterBoot = 0, uint32_t depthLeveledComputation = 0, size_t order = 1)
template<typename VectorDataType>
inline Ciphertext<Element> EvalFBT(ConstCiphertext<Element> &ciphertext, const std::vector<VectorDataType> &coeffs, uint32_t digitBitSize, const BigInteger &initialScaling, uint64_t postScaling, uint32_t levelToReduce = 0, size_t order = 1)
template<typename VectorDataType>
inline Ciphertext<Element> EvalFBTNoDecoding(ConstCiphertext<Element> &ciphertext, const std::vector<VectorDataType> &coeffs, uint32_t digitBitSize, const BigInteger &initialScaling, size_t order = 1)
inline Ciphertext<Element> EvalHomDecoding(ConstCiphertext<Element> &ciphertext, uint64_t postScaling, uint32_t levelToReduce = 0)
template<typename VectorDataType>
inline std::shared_ptr<seriesPowers<Element>> EvalMVBPrecompute(ConstCiphertext<Element> &ciphertext, const std::vector<VectorDataType> &coeffs, uint32_t digitBitSize, const BigInteger &initialScaling, size_t order = 1)
template<typename VectorDataType>
inline Ciphertext<Element> EvalMVB(const std::shared_ptr<seriesPowers<Element>> ciphertexts, const std::vector<VectorDataType> &coeffs, uint32_t digitBitSize, const uint64_t postScaling, uint32_t levelToReduce = 0, size_t order = 1)
template<typename VectorDataType>
inline Ciphertext<Element> EvalMVBNoDecoding(const std::shared_ptr<seriesPowers<Element>> ciphertexts, const std::vector<VectorDataType> &coeffs, uint32_t digitBitSize, size_t order = 1)
template<typename VectorDataType>
inline Ciphertext<Element> EvalHermiteTrigSeries(ConstCiphertext<Element> &ciphertext, const std::vector<std::complex<double>> &coefficientsCheb, double a, double b, const std::vector<VectorDataType> &coefficientsHerm, size_t precomp = 0)
inline uint32_t GetCKKSBootCorrectionFactor()
inline void SetCKKSBootCorrectionFactor(uint32_t cf)
inline LWEPrivateKey EvalCKKStoFHEWSetup(SchSwchParams params)

Sets all parameters for switching from CKKS to FHEW.

Scheme switching between CKKS and FHEW functionality There are three methods that have to be called in this specific order:

  1. EvalCKKStoFHEWSetup: generates a FHEW cryptocontext and returns the key, computes and encodes the coefficients for encoding and decoding and stores the necessary parameters

  2. EvalCKKStoFHEWKeyGen: computes and stores the keys for rotations and conjugation

  3. EvalCKKStoFHEW: returns the FHEW/CGGI ciphertext 1’. EvalFHEWtoCKKSwitchetup: takes in the CKKS cryptocontext and sets the parameters 2’. EvalFHEWtoCKKSKeyGen: computes and stores the switching key and the keys for rotations and conjugation 3’. EvalFHEWtoCKKS: returns the CKKS ciphertext 1’’. EvalSchemeSwitchingSetup: generates a FHEW cryptocontext and returns the key, computes and encodes the coefficients for encoding and decoding and stores the necessary parameters 2’’. EvalSchemeSwitchingKeyGen: computes and stores the switching key and the keys for rotations and conjugation 3’’. EvalCompareSchemeSwitching/EvalFuncSchemeSwitching: returns the CKKS ciphertext of the function specified

Note

TODO: Add overload for pre-generated BinFHEContext.

Parameters

params – Parameters for CKKS-to-FHEW scheme switching.

Returns

FHEW secret key.

inline void EvalCKKStoFHEWKeyGen(const KeyPair<Element> &keyPair, ConstLWEPrivateKey &lwesk)

Generates keys for CKKS-to-FHEW scheme switching: rotation keys, conjugation keys, and switching key.

Parameters
  • keyPair – CKKS key pair.

  • lwesk – FHEW secret key.

inline void EvalCKKStoFHEWPrecompute(double scale = 1.0)

Performs precomputations for CKKS homomorphic decoding. Allows setting a custom scale factor. Given as a separate method than EvalCKKStoFHEWSetup to allow the user to specify a scale that depends on the CKKS and FHEW cryptocontexts.

Parameters

scale – Scaling factor for the linear transform matrix.

inline std::vector<std::shared_ptr<LWECiphertextImpl>> EvalCKKStoFHEW(ConstCiphertext<Element> &ciphertext, uint32_t numCtxts = 0)

Switches a CKKS ciphertext to a vector of FHEW ciphertexts.

Parameters
  • ciphertext – Input CKKS ciphertext.

  • numCtxts – Number of coefficients to extract (defaults to number of slots if 0).

Returns

Vector of LWE ciphertexts.

inline void EvalFHEWtoCKKSSetup(const std::shared_ptr<BinFHEContext> &ccLWE, uint32_t numSlotsCKKS = 0, uint32_t logQ = 25)

Sets parameters for switching from FHEW to CKKS. Requires existing CKKS context.

Parameters
  • ccLWE – Source FHEW crypto context.

  • numSlotsCKKS – Number of slots in resulting CKKS ciphertext.

  • logQ – Ciphertext modulus size in FHEW (for high precision).

inline void EvalFHEWtoCKKSKeyGen(const KeyPair<Element> &keyPair, ConstLWEPrivateKey &lwesk, uint32_t numSlots = 0, uint32_t numCtxts = 0, uint32_t dim1 = 0, uint32_t L = 0)

Generates keys for switching from FHEW to CKKS.

Parameters
  • keyPair – CKKS key pair.

  • lwesk – FHEW secret key.

  • numSlots – Number of slots for CKKS encryption.

  • numCtxts – Number of LWE ciphertext values to encrypt.

  • dim1 – Baby-step parameter for linear transform.

  • L – Target level for homomorphic decoding matrix.

inline Ciphertext<Element> EvalFHEWtoCKKS(std::vector<std::shared_ptr<LWECiphertextImpl>> &LWECiphertexts, uint32_t numCtxts = 0, uint32_t numSlots = 0, uint32_t p = 4, double pmin = 0.0, double pmax = 2.0, uint32_t dim1 = 0) const

Switches a vector of FHEW ciphertexts to a single CKKS ciphertext.

Parameters
  • LWECiphertexts – Input vector of FHEW ciphertexts.

  • numCtxts – Number of values to encode.

  • numSlots – Number of CKKS slots to use.

  • p – Plaintext modulus (default = 4).

  • pmin – Minimum expected plaintext value (default = 0.0).

  • pmax – Maximum expected plaintext value (default = 2.0).

  • dim1 – Baby-step parameter (used in argmin).

Returns

CKKS ciphertext encoding the input LWE messages.

inline void SetParamsFromCKKSCryptocontext(SchSwchParams &params)

Sets scheme switching parameters using the current CKKS crypto context.

Parameters

params – Scheme switching parameter object to populate.

inline LWEPrivateKey EvalSchemeSwitchingSetup(SchSwchParams &params)

Sets parameters for switching between CKKS and FHEW.

Note

TODO: Add overload for pre-generated BinFHEContext.

Parameters

params – Scheme switching parameter object.

Returns

FHEW secret key.

inline void EvalSchemeSwitchingKeyGen(const KeyPair<Element> &keyPair, ConstLWEPrivateKey &lwesk)

Generates keys for switching between CKKS and FHEW.

Parameters
  • keyPair – CKKS key pair.

  • lwesk – FHEW secret key.

inline void EvalCompareSwitchPrecompute(uint32_t pLWE = 0, double scaleSign = 1.0, bool unit = false)

Performs precomputations for scheme switching in CKKS-to-FHEW comparison. Given as a separate method than EvalSchemeSwitchingSetup to allow the user to specify a scale.

Parameters
  • pLWE – Target plaintext modulus for FHEW ciphertexts.

  • scaleSign – Scaling factor for CKKS ciphertexts before switching.

  • unit – Indicates if input messages are normalized to unit circle.

inline Ciphertext<Element> EvalCompareSchemeSwitching(ConstCiphertext<Element> &ciphertext1, ConstCiphertext<Element> &ciphertext2, uint32_t numCtxts = 0, uint32_t numSlots = 0, uint32_t pLWE = 0, double scaleSign = 1.0, bool unit = false)

Compares two CKKS ciphertexts using FHEW-based scheme switching and returns CKKS result.

Parameters
  • ciphertext1 – First input CKKS ciphertext.

  • ciphertext2 – Second input CKKS ciphertext.

  • numCtxts – Number of coefficients to extract.

  • numSlots – Number of slots to encode in the result.

  • pLWE – Target plaintext modulus for FHEW ciphertexts.

  • scaleSign – Scaling factor for CKKS ciphertexts before switching.

  • unit – Indicates if input messages are normalized to unit circle.

Returns

CKKS ciphertext encoding sign comparison result.

inline std::vector<Ciphertext<Element>> EvalMinSchemeSwitching(ConstCiphertext<Element> &ciphertext, PublicKey<Element> &publicKey, uint32_t numValues = 0, uint32_t numSlots = 0, uint32_t pLWE = 0, double scaleSign = 1.0)

Computes minimum and index of the first packed values using scheme switching.

Parameters
  • ciphertext – Input CKKS ciphertext.

  • publicKey – CKKS public key.

  • numValues – Number of values to compare (we assume that numValues is a power of two).

  • numSlots – Number of output slots.

  • pLWE – Target plaintext modulus for FHEW.

  • scaleSign – Scaling factor before switching to FHEW. The resulting FHEW ciphertexts will encrypt values modulo pLWE, so scaleSign should account for this pLWE and is given here only if the homomorphic decoding matrix is not scaled with the desired values

Returns

A vector of two CKKS ciphertexts: [min, argmin]. The ciphertexts have junk after the first slot in the first ciphertext and after numValues in the second ciphertext if oneHot=true and after the first slot if oneHot=false.

inline std::vector<Ciphertext<Element>> EvalMinSchemeSwitchingAlt(ConstCiphertext<Element> &ciphertext, PublicKey<Element> &publicKey, uint32_t numValues = 0, uint32_t numSlots = 0, uint32_t pLWE = 0, double scaleSign = 1.0)

Computes minimum and index using more FHEW operations than CKKS with higher precision, but slower than EvalMinSchemeSwitching.

Parameters
  • ciphertext – Input CKKS ciphertext.

  • publicKey – CKKS public key.

  • numValues – Number of packed values to compare.

  • numSlots – Number of slots in the output ciphertexts.

  • pLWE – Target plaintext modulus for FHEW ciphertexts.

  • scaleSign – Scaling factor before switching to FHEW.

Returns

A vector with two CKKS ciphertexts: [min, argmin].

inline std::vector<Ciphertext<Element>> EvalMaxSchemeSwitching(ConstCiphertext<Element> &ciphertext, PublicKey<Element> &publicKey, uint32_t numValues = 0, uint32_t numSlots = 0, uint32_t pLWE = 0, double scaleSign = 1.0)

Computes maximum and index from the first packed values using scheme switching.

Parameters
  • ciphertext – Input CKKS ciphertext.

  • publicKey – CKKS public key.

  • numValues – Number of values to compare (we assume that numValues is a power of two).

  • numSlots – Number of output slots.

  • pLWE – Target plaintext modulus for FHEW.

  • scaleSign – Scaling factor before switching to FHEW.

Returns

A vector of two CKKS ciphertexts: [max, argmax]. The ciphertexts have junk after the first slot in the first ciphertext and after numValues in the second ciphertext if oneHot=true and after the first slot if oneHot=false.

inline std::vector<Ciphertext<Element>> EvalMaxSchemeSwitchingAlt(ConstCiphertext<Element> &ciphertext, PublicKey<Element> &publicKey, uint32_t numValues = 0, uint32_t numSlots = 0, uint32_t pLWE = 0, double scaleSign = 1.0)

Computes max and index via scheme switching, with more FHEW operations for better precision than EvalMaxSchemeSwitching.

Parameters
  • ciphertext – Input CKKS ciphertext.

  • publicKey – CKKS public key.

  • numValues – Number of values to compare.

  • numSlots – Number of output slots.

  • pLWE – Target plaintext modulus for FHEW.

  • scaleSign – Scaling factor before switching to FHEW.

Returns

A vector of two CKKS ciphertexts: [max, argmax].

inline std::shared_ptr<lbcrypto::BinFHEContext> GetBinCCForSchemeSwitch() const

Returns the BinFHE context used for scheme switching.

Returns

BinFHE context.

inline void SetBinCCForSchemeSwitch(std::shared_ptr<lbcrypto::BinFHEContext> ccLWE)

Sets the BinFHE context to be used for scheme switching.

Parameters

ccLWE – BinFHE context.

inline Ciphertext<Element> GetSwkFC() const

Gets the FHEW-to-CKKS scheme switching key ciphertext.

Returns

Switching key ciphertext.

inline void SetSwkFC(Ciphertext<Element> FHEWtoCKKSswk)

Sets the FHEW-to-CKKS scheme switching key ciphertext.

Parameters

FHEWtoCKKSswk – Switching key ciphertext.

template<class Archive>
inline void save(Archive &ar, std::uint32_t const version) const
template<class Archive>
inline void load(Archive &ar, std::uint32_t const version)
inline virtual std::string SerializedObjectName() const override
DecryptResult MultipartyDecryptFusion(const std::vector<Ciphertext<DCRTPoly>> &partialCiphertextVec, Plaintext *plaintext) const
std::unordered_map<uint32_t, DCRTPoly> ShareKeys(const PrivateKey<DCRTPoly> &sk, uint32_t N, uint32_t threshold, uint32_t index, const std::string &shareType) const
Plaintext GetPlaintextForDecrypt(PlaintextEncodings pte, std::shared_ptr<ParmType> evp, EncodingParams ep, CKKSDataType cdt)
DecryptResult Decrypt(ConstCiphertext<DCRTPoly> &ciphertext, const PrivateKey<DCRTPoly> &privateKey, Plaintext *plaintext)
DecryptResult MultipartyDecryptFusion(const std::vector<Ciphertext<DCRTPoly>> &partialCiphertextVec, Plaintext *plaintext) const
std::unordered_map<uint32_t, DCRTPoly> ShareKeys(const PrivateKey<DCRTPoly> &sk, uint32_t N, uint32_t threshold, uint32_t index, const std::string &shareType) const
void RecoverSharedKey(PrivateKey<DCRTPoly> &sk, std::unordered_map<uint32_t, DCRTPoly> &sk_shares, uint32_t N, uint32_t threshold, const std::string &shareType) const

Public Static Functions

static void ClearStaticMapsAndVectors()

Clears various caches within the library.

template<typename ST>
static inline bool SerializeEvalMultKey(std::ostream &ser, const ST &sertype, const std::string &keyTag = "")

Serializes either all EvalMult keys (if keyTag is empty) or the EvalMult keys for keyTag.

Parameters
  • ser – stream to serialize to

  • sertype – type of serialization

  • keyTag – secret key tag

Returns

true on success

template<typename ST>
static inline bool SerializeEvalMultKey(std::ostream &ser, const ST &sertype, const CryptoContext<Element> cc)

Serializes all EvalMult keys associated with the given CryptoContext.

Parameters
  • ser – stream to serialize to

  • sertype – type of serialization

  • cc – the CryptoContext whose keys should be serialized

Returns

true on success

template<typename ST>
static inline bool DeserializeEvalMultKey(std::istream &ser, const ST &sertype)

Deserializes EvalMult keys.

Attention

Silently replaces any existing matching keys and creates a new CryptoContextImpl if necessary

Parameters
  • ser – stream to deserialize from

  • sertype – type of serialization

Returns

true on success

static void ClearEvalMultKeys()

Clears the entire EvalMultKey cache.

static void ClearEvalMultKeys(const std::string &keyTag)

Clears the EvalMultKey cache for the given keyTag or the entire EvalMultKey cache if keyTag is empty.

Parameters

keyTag – secret key tag

static void ClearEvalMultKeys(const CryptoContext<Element> &cc)

Clears EvalMultKey cache for the given context.

Parameters

cc – the context to clear all EvalMultKey for

static void InsertEvalMultKey(const std::vector<EvalKey<Element>> &evalKeyVec, const std::string &keyTag = "")

Adds the given vector of keys for the given keyTag to the map of all EvalMult keys.

Attention

Silently replaces any existing matching keys and if keyTag is empty, then the key tag is retrieved from evalKeyVec

Parameters
  • evalKeyVec – vector of keys

  • keyTag – secret key tag

template<typename ST>
static inline bool SerializeEvalSumKey(std::ostream &ser, const ST &sertype, const std::string &keyTag = "")

Serializes either all EvalSum keys (if keyTag is empty) or the EvalSum keys for keyTag.

Parameters
  • ser – stream to serialize to

  • sertype – type of serialization

  • keyTag – secret key tag

Returns

true on success

template<typename ST>
static inline bool SerializeEvalSumKey(std::ostream &ser, const ST &sertype, const CryptoContext<Element> cc)

Serializes all EvalSum keys associated with the given CryptoContext.

Parameters
  • ser – stream to serialize to

  • sertype – type of serialization

  • cc – the CryptoContext whose keys should be serialized

Returns

true on success

template<typename ST>
static inline bool DeserializeEvalSumKey(std::istream &ser, const ST &sertype)

Deserializes EvalSum keys.

Attention

Silently replaces any existing matching keys and creates a new CryptoContextImpl if necessary

Parameters
  • ser – stream to deserialize from

  • sertype – type of serialization

Returns

true on success

static void ClearEvalSumKeys()

Clears the entire EvalSumKey cache.

static void ClearEvalSumKeys(const std::string &keyTag)

Clears the EvalSumKey cache for the given keyTag or the entire EvalSumKey cache if keyTag is empty.

ClearEvalMultKeys - flush EvalMultKey cache for a given id

Parameters
  • keyTag – secret key tag

  • keyTag

static void ClearEvalSumKeys(const CryptoContext<Element> cc)

Clears EvalSumKey cache for the given context.

ClearEvalMultKeys - flush EvalMultKey cache for a given context

Parameters
  • cc – the context to clear all EvalSumKey for

  • cc

static inline void InsertEvalSumKey(const std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> mapToInsert, std::string keyTag = "")

Adds the given map of keys for the given keyTag to the map of all EvalSum keys.

Attention

Silently replaces any existing matching keys and if keyTag is empty, then the key tag is retrieved from mapToInsert

Parameters
  • mapToInsert – map of keys

  • keyTag – secret key tag

template<typename ST>
static inline bool SerializeEvalAutomorphismKey(std::ostream &ser, const ST &sertype, const std::string &keyTag = "")

Serializes either all EvalAutomorphism keys (if keyTag is empty) or the EvalAutomorphism keys for keyTag.

Parameters
  • ser – stream to serialize to

  • sertype – type of serialization

  • keyTag – secret key tag

Returns

true on success

template<typename ST>
static inline bool SerializeEvalAutomorphismKey(std::ostream &ser, const ST &sertype, const CryptoContext<Element> cc)

Serializes all EvalAutomorphism keys associated with the given CryptoContext.

Parameters
  • ser – stream to serialize to

  • sertype – type of serialization

  • cc – the CryptoContext whose keys should be serialized

Returns

true on success

template<typename ST>
static inline bool SerializeEvalAutomorphismKey(std::ostream &ser, const ST &sertype, const std::string &keyTag, const std::vector<uint32_t> &indexList)

Serializes EvalAutomorphism keys for an array of specific indices associated with the given keyTag.

Parameters
  • ser – stream to serialize to

  • sertype – type of serialization

  • keyTag – secret key tag

  • indexList – array of specific indices to serialize keys for

Returns

true on success

template<typename ST>
static inline bool DeserializeEvalAutomorphismKey(std::ostream &ser, const ST &sertype, const std::string &keyTag, const std::vector<uint32_t> &indexList)

Deserializes EvalAutomorphism keys for an array of specific indices associated with the given keyTag.

Parameters
  • ser – stream to deserialize from

  • sertype – type of serialization

  • keyTag – secret key tag

  • indexList – array of specific indices to deserialize keys for

Returns

true on success

template<typename ST>
static inline bool DeserializeEvalAutomorphismKey(std::istream &ser, const ST &sertype)

Deserializes EvalAutomorphism keys.

Attention

Silently replaces any existing matching keys and creates a new CryptoContextImpl if necessary

Parameters
  • ser – stream to deserialize from

  • sertype – type of serialization

Returns

true on success

static void ClearEvalAutomorphismKeys()

Clears the entire EvalAutomorphismKey cache.

static void ClearEvalAutomorphismKeys(const std::string &keyTag)

Clears the EvalAutomorphismKey cache for the given keyTag or the entire EvalAutomorphismKey cache if keyTag is empty.

ClearEvalAutomorphismKeys - flush EvalAutomorphismKey cache for a given id

Parameters
  • keyTag – secret key tag

  • keyTag

static void ClearEvalAutomorphismKeys(const CryptoContext<Element> cc)

Clears EvalAutomorphismKey cache for the given context.

ClearEvalAutomorphismKeys - flush EvalAutomorphismKey cache for a given context

Parameters
  • cc – the context to clear all EvalAutomorphismKeys for

  • cc

static void InsertEvalAutomorphismKey(const std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> mapToInsert, const std::string &keyTag = "")

Adds the given map of keys for the given keyTag to the map of all EvalAutomorphism keys.

Attention

Silently replaces any existing matching keys and if keyTag is empty, then the key tag is retrieved from mapToInsert

Parameters
  • mapToInsert – map of keys

  • keyTag – secret key tag

static std::map<std::string, std::vector<EvalKey<Element>>> &GetAllEvalMultKeys()

Gets a map of all relinearization/evaluation multiplication keys.

Returns

std::map where the map key/data pair is “keyTag”/”EvalMultKeys vector”

static const std::vector<EvalKey<Element>> &GetEvalMultKeyVector(const std::string &keyTag)

Gets a vector of relinearization/evaluation multiplication keys for the given keyTag.

Parameters

keyTag – secret key tag

Returns

vector of EvalMultKeys

static std::map<std::string, std::shared_ptr<std::map<uint32_t, EvalKey<Element>>>> &GetAllEvalAutomorphismKeys()

Gets a map of all EvalAutomorphism keys.

Returns

std::map where the map key/data pair is “keyTag”/”shared_ptr to EvalMultKey map”

static std::shared_ptr<std::map<uint32_t, EvalKey<Element>>> GetEvalAutomorphismKeyMapPtr(const std::string &keyTag)

Gets a map of EvalAutomorphism keys for the given keyTag.

Parameters

keyTag – secret key tag

Returns

shared_ptr to EvalAutomorphismKey map

static inline std::map<uint32_t, EvalKey<Element>> &GetEvalAutomorphismKeyMap(const std::string &keyTag)

Gets a map of EvalAutomorphism keys for the given keyTag.

Parameters

keyTag – secret key tag

Returns

EvalAutomorphismKey map

static std::map<std::string, std::shared_ptr<std::map<uint32_t, EvalKey<Element>>>> &GetAllEvalSumKeys()

Gets a map of all summation keys.

Returns

std::map where the map key/data pair is “keyTag”/”shared_ptr to EvalSumKey map”

static const std::map<uint32_t, EvalKey<Element>> &GetEvalSumKeyMap(const std::string &keyTag)

Gets a map of EvalSum keys for the given keyTag.

Parameters

keyTag – secret key tag

Returns

EvalSumKey map

static Plaintext GetPlaintextForDecrypt(PlaintextEncodings pte, std::shared_ptr<ParmType> evp, EncodingParams ep, CKKSDataType cdt = REAL)

Returns a plaintext object for decryption based on encoding type and parameters.

Parameters
  • pte – Plaintext encoding type.

  • evp – Element parameters.

  • ep – Encoding parameters.

  • cdt – CKKS data type.

Returns

Plaintext for decryption.

static inline std::set<uint32_t> GetEvalAutomorphismNoKeyIndices(const std::string &keyTag, const std::set<uint32_t> &indices)

Gets indices that do not have automorphism keys for the given secret key tag in the key map.

Parameters
  • keyTag – secret key tag

  • indexList – array of specific indices to check the key map against

Returns

indices that do not have automorphism keys associated with

static std::set<uint32_t> GetExistingEvalAutomorphismKeyIndices(const std::string &keyTag)

Returns automorphism indices for all existing evaluation keys.

Parameters

keyTag – Secret key tag.

Returns

Set of indices found for the given key tag. Empty if none exist.

static std::set<uint32_t> GetUniqueValues(const std::set<uint32_t> &oldValues, const std::set<uint32_t> &newValues)

Compares two sets and returns unique values from the second set.

Parameters
  • oldValues – First set to compare against.

  • newValues – Second set to extract unique values from.

Returns

Set of values present in newValues but not in oldValues.

static inline uint32_t SerializedVersion()

Protected Functions

inline void TypeCheck(ConstCiphertext<Element> &a, ConstCiphertext<Element> &b, CALLER_INFO_ARGS_HDR) const

TypeCheck makes sure that an operation between two ciphertexts is permitted.

Parameters
  • a – ciphertext1

  • b – ciphertext2

inline void TypeCheck(ConstCiphertext<Element> &a, ConstPlaintext &b, CALLER_INFO_ARGS_HDR) const

TypeCheck makes sure that an operation between a ciphertext and a plaintext is permitted.

Parameters
  • a – ciphertext

  • b – plaintext

inline bool Mismatched(const CryptoContext<Element> a) const
template<typename T>
inline void ValidateKey(const T &key, CALLER_INFO_ARGS_HDR) const
inline void ValidateCiphertext(ConstCiphertext<Element> &ciphertext, CALLER_INFO_ARGS_HDR) const
inline void ValidateSeriesPowers(std::shared_ptr<seriesPowers<Element>> powers, CALLER_INFO_ARGS_HDR) const
inline virtual Plaintext MakeCKKSPackedPlaintextInternal(const std::vector<std::complex<double>> &value, size_t noiseScaleDeg, uint32_t level, const std::shared_ptr<ParmType> params, uint32_t slots) const
inline uint32_t GetCompositeDegreeFromCtxt() const

Getter for composite degree of the current scheme crypto context.

Returns

integer value corresponding to composite degree

Protected Attributes

std::shared_ptr<CryptoParametersBase<Element>> m_params = {nullptr}
std::shared_ptr<SchemeBase<Element>> m_scheme = {nullptr}
SCHEME m_schemeId = {SCHEME::INVALID_SCHEME}
uint32_t m_keyGenLevel = {0}

Friends

inline friend bool operator==(const CryptoContextImpl<Element> &a, const CryptoContextImpl<Element> &b)

Equality comparison operator.

Attention

this is for internal use only

Parameters
  • a – cryptocontext object1

  • b – cryptocontext object2

Returns

true if the implementations have identical params and scheme

inline friend bool operator!=(const CryptoContextImpl<Element> &a, const CryptoContextImpl<Element> &b)

Inequality comparison operator.

Attention

this is for internal use only

Parameters
  • a – cryptocontext object1

  • b – cryptocontext object2

Returns

true if the implementations do not have identical params and scheme