HeContext#

class HeContext#

An abstract main class representing an underlying HE library & scheme, configured, initialized, and ready to start working.

Upon initialization, use on of the inheriting concrete classes such as HelibCkksContext. Then continue with a generic reference to HeContext to allow scheme obliviousness

This class bundles together all the data structures required for performing HE operations, including the keys. It either contains a public/private key pair, allowing all operations including decryption, or just the public key allowing everything but decryption.

It is used as input in constructor of many other classes in this library. For example, to create a ciphertext using the library represent by an HeContext, write: CTile c(he);

It can be further used to query various properties of the underlying scheme and library.

clone(self: pyhelayers.HeContext) pyhelayers.HeContext#

Returns an uninitialized context of the same type.

static create(*args, **kwargs)#

Overloaded function.

  1. create(context_name: str, req: pyhelayers.HeConfigRequirement) -> pyhelayers.HeContext

    Creates and returns an initialized HE context given a context name and config requirement.

    param context_name:

    The context name.

    type context_name:

    string

    param req:

    The config requirement.

    type req:

    pyhelayers.HeConfigRequirement

  2. create(prioritized_context_names: List[str]) -> pyhelayers.HeContext

    Creates an HE context instance from the first available context name.

    param prioritized_context_names:

    Prioritized list of contexts names (highest priority first).

    raise ValueError:

    If no matching context is found.

get_automatic_bootstrapping(self: pyhelayers.HeContext) bool#

Returns the value of the automaticBootstrapping flag

Return type:

boolean

get_context_id(self: pyhelayers.HeContext) int#

Returns the context id that identifies the specific key set used by this context.

Return type:

int

get_default_device(self: pyhelayers.HeContext) Tuple[pyhelayers.DeviceType, int]#

Returns the default device of the ciphertexts and plaintexts generated using this HeContext.

Return type:

DeviceType, int

get_default_scale(self: pyhelayers.HeContext) float#

Returns default scale used in encoding (where applicable).

Return type:

double

get_he_config_requirement(self: pyhelayers.HeContext) pyhelayers.HeConfigRequirement#

Returns the actual HeConfigRequirement object used by this HeContext, obtained by applying getActualConfigRequirement over the HeConfigRequirement object provided when initializing this HeContext.

get_hybrid_device_utilization(self: pyhelayers.HeContext) bool#

Returns indication for whether a policy of hybrid utilization of devices of different types is applied.

Return type:

boolean

get_library_name(self: pyhelayers.HeContext) str#

Returns the name of the underlying library.

Return type:

string

get_modulus_chain(self: pyhelayers.HeContext) numpy.ndarray[numpy.uint64]#

Returns the modulus chain: the value of each prime in the chain (where applicable).

Return type:

pyarrayuint64

get_num_devices_of_type(self: pyhelayers.HeContext, type: pyhelayers.DeviceType) int#

Returns the number of devices of the given type available for this HeContext to use.

Parameters:

type – The device type.

get_public_functions(self: pyhelayers.HeContext) pyhelayers.PublicFunctions#

Returns a PublicFunctions object specifying the functionalities that are supported by the public keys of this HeContext

get_scheme_name(self: pyhelayers.HeContext) str#

Returns the name of the underlying scheme.

Return type:

string

get_security_level(self: pyhelayers.HeContext) int#

Returns the security level supplied by this context.

Return type:

int

get_signature(self: pyhelayers.HeContext) str#

Returns a signature of the context that distinguishes it enough to be able to load previously stored contexts based on their signatures.

Return type:

string

get_top_chain_index(self: pyhelayers.HeContext) int#

Returns the highest available chain index. (for schemes where it is applicable).

Return type:

int

get_traits(self: pyhelayers.HeContext) pyhelayers.HeTraits#

Returns an HeTraits object containing various properties of the underlying scheme.

has_secret_key(self: pyhelayers.HeContext) bool#

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).

Return type:

bool

init_for_generic_packing(self: pyhelayers.HeContext, light: bool = False) None#

Initializes HE context to be used for generic packing.

Parameters:

light – Whether to use a lighter HE context with smaller

multiplication depth (default: false).

static is_registered_context(context_name: str) bool#

Returns indication to whether a registered context exists under a given context name.

Parameters:

context_name – The context name.

load_from_buffer(self: pyhelayers.HeContext, buf: bytes) None#

Loads this HeContext from a buffer.

Parameters:

buf – a memory buffer to load the object from.

load_from_file(self: pyhelayers.HeContext, file_name: str) int#

Loads this HeContext from a file saved by save_to_file().

Parameters:

file_name (string) – File to load from.

load_secret_key(self: pyhelayers.HeContext, buf: bytes, seed_only: bool = False) None#

Load secret key from a buffer.

Parameters:
  • buf (A memory buffer) – A memory buffer to load the HeContext from.

  • seed_only – If True, the seed of the secret key will be loaded from the given buffer and the secret key will be generated using this seed. Defaults to False.

Raises:
  • RuntimeError – If this HeContext already has a secret key.

  • RuntimeError – If seed_only is True but the underlying HeContext does not support secret key I/O using seeds.

load_secret_key_from_file(self: pyhelayers.HeContext, file_name: str, seed_only: bool = False) None#

Loads context saved by the saveToFile() method.

Parameters:
  • file_name (string) – File to load from.

  • seed_only (bool) – If True, the seed of the secret key will be loaded from the given file and the secret key will be generated using this seed. Defaults to False.

Raises:

RuntimeError – If this HeContext already has a secret key.

print_signature(self: pyhelayers.HeContext) str#

Prints a summary of library details and configuration params.

public_functions_supported(self: pyhelayers.HeContext, public_functions: pyhelayers.PublicFunctions) bool#

Returns whether the given PublicFunctions object is supported by this HeContext.

Parameters:

public_functions – The PublicFunctions object.

save_secret_key(self: pyhelayers.HeContext, seed_only: bool = False) bytes#

Save secret key into a buffer.

Parameters:

seed_only (bool) – If True, only the seed used to control the secret key’s randomness will be saved, rather than the whole secret key. Defaults to False.

Raises:
  • RuntimeError – If this HeContext doesn’t have a secret key.

  • RuntimeError – If seed_only is True but the underlying HeContext does not support secret key I/O using seeds.

Returns:

a memory buffer containing the saved secret key.

save_secret_key_to_file(self: pyhelayers.HeContext, file_name: str, seed_only: bool = False) None#

Save secret key to the given file.

Parameters:
  • file_name (string) – File to write to.

  • seed_only (bool) – If True, only the seed used to control the secret key’s randomness will be saved, rather than the whole secret key. Defaults to False.

Raises:

RuntimeError – If this HeContext does not have a secret key.

save_to_buffer(*args, **kwargs)#

Overloaded function.

  1. save_to_buffer(self: pyhelayers.HeContext) -> bytes

    Saves this HeContext into a buffer.

    return:

    a memory buffer containing the saved object.

  2. save_to_buffer(self: pyhelayers.HeContext, public_functions: pyhelayers.PublicFunctions) -> bytes

    Saves this HeContext into a buffer.

    param public_functions:

    determines which of the public keys should be saved.

    type public_functions:

    pyhelayers.PublicFunctions

    return:

    a memory buffer containing the saved object.

save_to_file(*args, **kwargs)#

Overloaded function.

  1. save_to_file(self: pyhelayers.HeContext, file_name: str) -> int

    Saves this HeContext to a file in binary form.

    param file_name:

    File to write to.

    type file_name:

    string

  2. save_to_file(self: pyhelayers.HeContext, file_name: str, public_fuctions: pyhelayers.PublicFunctions) -> int

    Saves this HeContext to a file in binary form.

    param file_name:

    File to write to.

    type file_name:

    string

    param public_functions:

    determines which of the public keys should be saved.

    type public_functions:

    pyhelayers.PublicFunctions

set_automatic_bootstrapping(self: pyhelayers.HeContext, v: bool) None#

Sets the automaticBootstrapping flag to the given v. When automaticBootstrapping is set to true, ciphertexts created using this HeContext will be bootstrapped automatically when needed.

Parameters:

v – The value to set.

set_default_device(self: pyhelayers.HeContext, type: pyhelayers.DeviceType, device_id: int = 0, hybrid_utilization: bool = False) None#

Sets the default device of the ciphertexts and plaintexts generated using this HeContext, and the hybrid devices utilization policy.

Parameters:
  • type – the new default device type.

  • device_id – the new default device ID.

  • hybrid_utilization – Whether a policy of hybrid utilization of

devices of different types shall be applied. Such policy is only applicable for DEVICE_CPU as the default device type. In such policy, the GPU may be utilized alongside CPU for parts of the computation (e.g., for performing bootstraps).

set_default_scale(self: pyhelayers.HeContext, v: float) None#

Sets the default scale to be used in encoding (where applicable)..

Parameters:

v – The default scale.

slot_count(self: pyhelayers.HeContext) int#

The number of slots in each CTile (ciphertext) or PTile (plaintext) created over this context.

Return type:

int