SealCkksContext#
- class SealCkksContext#
An implementation of HeContext using the SEAL CKKS backend.
- get_accurate_scale(self: pyhelayers.SealCkksContext, chain_index: int) float #
Returns the accurate scale to be used for a given chain index. Only supported when accurate scaling mode is enabled. Cannot be called before the context is initialized.
- Parameters:
chain_index – The chain index to get the scale of.
- get_use_accurate_scaling(self: pyhelayers.SealCkksContext) bool #
Returns whether accurate scaling mode is enabled or not.
- init(self: pyhelayers.SealCkksContext, req: pyhelayers.HeConfigRequirement) None #
Initializes this SealCkksContext according the given requirement.
- Parameters:
req – an HeConfigRequirement object, indicating the required configuration parameters (e.g. securityLevel, numSlots).
- Raises:
RuntimeError – if it is not possible to fulfill the given requirement.
- set_use_accurate_scaling(self: pyhelayers.SealCkksContext, use_accurate_scaling: bool) None #
Sets whether to enable the accurate scaling mode or not (default: enabled). Cannot be called after the context is initialized.
- Parameters:
use_accurate_scaling – Whether to enable the accurate scaling mode or not.
-
class SealCkksContext : public helayers::HeContext#
An implementation of HeContext for CKKS scheme in Seal.
It can be either initialized via parameters, via an HeConfigRequirement, or loaded from a file.
It is recommended not to use directly after initialization, but use an HeContext reference instead.
Public Functions
-
SealCkksContext()#
A constructor.
-
~SealCkksContext() override#
-
virtual void init(const HeConfigRequirement &req) override#
Initializes this SealCkksContext using the given configuration requirement.
- Parameters:
req – The configuration requirement.
- Throws:
runtime_error – If this SealCkksContext is already initialized.
runtime_error – If the given configuration requirement is not feasible.
runtime_error – If the required securtiy level is above 256.
runtime_error – If the signature of this SealCkksContext is not empty and it is not consistent with the new signature corresponding to the given requirement.
-
void initAndSaveOrLoad(const HeConfigRequirement &req)#
Initializes this SealCkksContext using the given configuration requirement.
- Parameters:
req – The configuration requirement.
-
virtual void printSignature(std::ostream &out) const override#
Prints a summary of library details and configuration params.
- Parameters:
out – Output stream to write to.
-
virtual bool isConfigRequirementFeasible(const HeConfigRequirement &req) const override#
Returns true if the received configuration requirement is feasible under this SealCkksContext, and false otherwise.
- Parameters:
req – The requirement to check its feasibility.
-
virtual int getBestFeasibleFractionalPartPrecision(const HeConfigRequirement &req) const override#
Returns the best feasible fractional part precision under the given configuration requirement.
If there is no such a fractional part precision, returns -1. All the fields of the given requirement other than req.fractionalPartPrecision are expected to be present, while any value in req.fractionalPartPrecision will be ignored.
- Parameters:
req – The requirement to get best fractional part precision for.
-
virtual int getBestFeasibleMulDepth(const HeConfigRequirement &req) const override#
Returns the best feasible multiplication depth under the given configuration requirement.
If there is no such a multiplication depth, returns -1. All the fields of the given requirement other than req.multiplicationDepth are expected to be present, while any value in req.multiplicationDepth will be ignored.
- Parameters:
req – The requirement to get best multiplication depth for.
-
int getMaxPolyModulusLength(int securityLevel, int polyModulusDegree) const#
returns the maximum polynomial modulus length that is supported with the given securityLevel and polyModulusDegree.
- Parameters:
securityLevel – The securtiy level.
polyModulusDegree – The polynomial modulus degree.
- Throws:
runtime_error – If securityLevel is greater than 256.
-
virtual void debugPrint(const std::string &title = "", Verbosity verbosity = VERBOSITY_REGULAR, std::ostream &out = std::cout) const override#
Prints the content of this object.
- Parameters:
title – Text to add to the print
verbosity – Verbosity level
out – Output stream
-
virtual std::shared_ptr<AbstractCiphertext> createAbstractCipher() const override#
Returns a pointer to a SealCkksCiphertext, initialized with this SealCkksContext.
-
virtual std::shared_ptr<AbstractPlaintext> createAbstractPlain() const override#
Returns a pointer to a SealCkksPlaintext, initialized with this SealCkksContext.
-
virtual std::shared_ptr<AbstractEncoder> getEncoder() const override#
Returns a pointer to a SealCkksEncoder, initialized with this SealCkksContext.
-
virtual std::shared_ptr<AbstractBootstrapEvaluator> createAbstractBootstrapEvaluator(const BootstrapConfig &bsConfig) const override#
Do not use. Should be made private.
-
virtual int getTopChainIndex() const override#
Returns the top chain index.
-
void print(std::ostream &out)#
Prints a summary of library details and configuration params to the given ostream.
- Parameters:
out – The ostream to write to.
- Throws:
runtime_error – If failed verifying that the underlying Seal scheme is the CKKS scheme.
-
virtual std::shared_ptr<std::vector<uint64_t>> getModulusChain() const override#
Returns the modulus chain: the value of each prime in the chain.
- Throws:
runtime_error – If the context is not yet initialized.
-
inline virtual int slotCount() const override#
The number of slots in each CTile (ciphertext) or PTile (plaintext) created over this context.
-
virtual int getSecurityLevel() const override#
Returns the security level supplied by this context.
-
inline const seal::CKKSEncoder &getSealEncoder() const#
Returns the CKKSEncoder object of the underlying Seal library.
-
inline const seal::Evaluator &getEvaluator() const#
Returns the Evaluator object of the underlying Seal library.
-
const seal::Encryptor &getEncryptor() const#
Returns the Encryptor object of the underlying Seal library.
-
inline const seal::Decryptor &getDecryptor() const#
Returns the Decryptor object of the underlying Seal library.
- Throws:
runtime_error – if this SealCkksContext does not have a secret key.
-
inline std::shared_ptr<seal::SEALContext> getContext() const#
Returns the SEALContext object of the underlying Seal library.
-
const seal::parms_id_type &getParmsId(int chainIndex) const#
Returns the ParmsId object of the underlying Seal library corresponding to the given chain index.
- Parameters:
chainIndex – The chain index to get its ParmsId.
- Throws:
runtime_error – If the given chain index is illegal.
-
const seal::RelinKeys &getRelinKeys() const#
Returns the RelinKeys object of the underlying Seal library.
-
const seal::GaloisKeys &getGalKeys() const#
Returns the GaloisKeys object of the underlying Seal library.
-
virtual bool hasSecretKey() const override#
Returns whether this context contains a secret key.
If not, decryption and other operations relying on decryption will not be available (will throw an exception).
-
virtual void saveSecretKey(std::ostream &out, bool seedOnly = false) override#
Save secret key to the given ostream.
- Parameters:
out – The binary stream to save to.
seedOnly – If true, only the seed used to control the secret key’s randomness will be saved, rather than the whole secret key.
- Throws:
runtime_error – If this HeContext doesn’t have a secret key. i.e. hasSecretKey() is false.
runtime_error – If seedOnly is true but the underlying HeContext does not support secret key I/O using seeds.
-
virtual void loadSecretKey(std::istream &in, bool seedOnly = false) override#
Load secret key from the given istream.
- Parameters:
in – The binary stream to load from.
seedOnly – If true, the seed of the secret key will be loaded from the given binary stream and the secret key will be generated using this seed.
- Throws:
runtime_error – If this HeContext already has a secret key. i.e. hasSecretKey() is true.
runtime_error – If seedOnly is true but the underlying HeContext does not support secret key I/O using seeds.
-
int getBitCountAtChainIndex(int chainIndex) const#
Returns the bit count corresponding to the given chain index.
- Parameters:
chainIndex – To get its bit count.
-
int getTotalBitCountAtChainIndex(int chainIndex) const#
Returns The sum of the bit counts of all chain indices starting from 0 and up to the given chainIndex, inclusive.
- Parameters:
chainIndex – The chain index up to which the sum of bit counts will be calculated.
-
inline virtual std::string getLibraryName() const override#
Returns the name of the underlying library.
-
inline virtual std::string getSchemeName() const override#
Returns the name of the underlying scheme.
-
inline virtual std::string getSignature() const override#
Returns a signature of the context that distinguishes it enough to be able to load previously stored contexts based on their signatures.
-
virtual std::map<std::string, int64_t> getEstimatedMeasures() const override#
Returns a map estimating the run time and memory consumed by different operations made with this SealCkksContext.
This function assumes the existence of a seal.json file storing the required measures.
- Throws:
runtime_error – If the seal.json file which is expected to contain the required measures, does not exist.
-
void setUseAccurateScaling(bool useAccurateScaling)#
Sets whether to enable the accurate scaling mode or not (default:enabled).
Cannot be called after the context is initialized.
- Parameters:
useAccurateScaling – whether to enable the accurate scaling mode or not.
- Throws:
runtime_error – If this SealCkksContext is already intialized.
-
bool getUseAccurateScaling() const#
Returns whether accurate scaling mode is enabled or not.
-
virtual double getAccurateScale(int chainIndex) const override#
See parent method.
-
virtual int getMinChainIndexForBootstrapping() const override#
Assuming this HeContext is bootstrappable, returns the minimum level in which ciphertexts can be bootstrapped.
-
virtual int getChainIndexAfterBootstrapping() const override#
Assuming this HeContext is bootstrappable, returns the level of ciphertexts after bootstrapping.
-
virtual BootstrapConfig getBootstrapChainIndexes(const HeConfigRequirement &req) const override#
Returns a bootstrap config object with two chain index values: the minimum level in which ciphertexts can be bootstrapped and the level of ciphertexts after bootstrapping.
- Parameters:
req – The requirement, must indicate a bootstrappable object.
-
virtual int getMinSupportedNumSlots() const override#
Returns the minimal number of slots supported by this HE context.
-
virtual int getMaxSupportedNumSlots() const override#
Returns the maximal number of slots supported by this HE context.
-
virtual std::vector<double> getMaxAllowedValues() const override#
See parent method.
-
virtual void assertSavePublicFunctionsSupported(const PublicFunctions &publicFunctions) const override#
Verifies that this HeContext supports calling save() method with the given PublicFunctions object.
- Parameters:
publicFunctions – The PublicFunctions object.
- Throws:
invalid_argument – If saving the given PublicFunctions object is not supported by this HeContext.
-
virtual bool publicFunctionsSupported(const PublicFunctions &publicFunctions) const override#
Returns whether the given PublicFunctions object is supported by this HeContext.
The given PublicFunctions object is assumed to be valid (see also PublicFunctions::validate()).
- Parameters:
publicFunctions – The PublicFunctions object.
-
SealCkksContext()#