PyCtxt¶
- class Pyfhel.PyCtxt(PyCtxt copy_ctxt=None, Pyfhel pyfhel=None, fileName=None, bytestring=None, scheme=None)¶
Bases:
objectCiphertext class of Pyfhel, contains a value/vector of encrypted ints/doubles.
This class references SEAL and PALISADE ciphertexts, using the one corresponding to the backend selected in Pyfhel. By default, it is SEAL.
- __init__(PyCtxt copy_ctxt=None, Pyfhel pyfhel=None, fileName=None, bytestring=None, scheme=None)¶
Initializes an empty PyCtxt ciphertext.
- To fill the ciphertext during initialization you can:
Provide a PyCtxt to deep copy.
Provide a pyfhel instance to act as its backend.
Provide a fileName and an scheme to load the data from a saved file.
- fileName¶
Load PyCtxt from this file. Requires non-empty scheme.
- Type:
str, pathlib.Path, optional
Methods
__add__(self, other)__add__(other)
__sub__(self, other)__sub__(other)
__neg__(self)__neg__()
__mul__(self, other)Multiplies this ciphertext with either another PyCtxt or a PyPtxt plaintext.
__pow__(self, exponent, modulo)__pow__(exponent)
__rshift__(self, k)__rshift__(k)
__invert__(self)__invert__()
__repr__(self)__repr__()
__bytes__(self)__bytes__()
__init__(PyCtxt copy_ctxt=None, ...[, ...])Initializes an empty PyCtxt ciphertext.
copy(self)copy() -> PyCtxt
decrypt(self)decrypt()
encode_operand(self, other)encode_operand(other)
encrypt(self, value)encrypt(value)
from_bytes(self, bytes content[, scheme])from_bytes(bytes content, scheme)
get_multiplicative_inverse(self, other)get_multiplicative_inverse(other)
load(self, str fileName[, scheme])load(self, str fileName, scheme)
round_scale(self)round_scale()
save(self, str fileName, str compr_mode=)save(str fileName)
set_scale(self, double new_scale)set_scale(double new_scale)
size(self)Current size of the ciphertext.
sizeof_ciphertext(self, str compr_mode=)Returns the number of bytes that will be written when saving the ciphertext.
to_bytes(self, str compr_mode=)to_bytes()
Attributes
capacityMaximum size the ciphertext can hold.
mod_levelreturns the number of moduli consumed so far.
noiseBudgetNoise budget.
scalemultiplying factor to encode values in ckks.
scale_bitsnumber of bits in scale to encode values in ckks
schemereturns the FHE scheme of this ciphertext.
API description
- __add__(self, other)¶
__add__(other)
Sums this ciphertext with either another PyCtx or a PyPtxt plaintext.
Sums with a PyPtxt/PyCtxt, storing the result a new ciphertext.
- Parameters:
- Returns:
Ciphertext resulting of addition.
- Return type:
- Raises:
TypeError – if other doesn’t have a valid type.
See also
- __bytes__(self)¶
__bytes__()
Serialize current ciphertext to bytes
- __invert__(self)¶
__invert__()
Relinarizes this ciphertext in-place.
Requires valid relinearization keys.
See also
- __mul__(self, other)¶
Multiplies this ciphertext with either another PyCtxt or a PyPtxt plaintext.
Multiplies with a PyPtxt/PyCtxt, storing the result in a new ciphertext.
- Parameters:
other (PyCtxt, PyPtxt) – Multiplier, to be multiplied with this ciphertext.
- Returns:
Ciphertext resulting of multiplication
- Return type:
- Raises:
TypeError – if other doesn’t have a valid type.
See also
- __neg__(self)¶
__neg__()
Negates this ciphertext.
- __pow__(self, exponent, modulo)¶
__pow__(exponent)
Exponentiates this ciphertext to the desired exponent.
Exponentiates to the desired exponent.
- Parameters:
exponent (int) – Exponent for the power.
See also
- __repr__(self)¶
__repr__()
Prints information about the current ciphertext
- __rshift__(self, k)¶
__rshift__(k)
Rotates this ciphertext k positions to the right.
- Parameters:
k (int) – Number of positions to rotate.
- __sub__(self, other)¶
__sub__(other)
Subtracts this ciphertext with either another PyCtxt or a PyPtxt plaintext.
Subtracts with a PyPtxt/PyCtxt, storing the result in a new ciphertext.
- Parameters:
other (PyCtxt, PyPtxt) – Substrahend, to be subtracted from this ciphertext.
- Returns:
Ciphertext resulting of subtraction
- Return type:
- Raises:
TypeError – if other doesn’t have a valid type.
See also
- decrypt(self)¶
decrypt()
Decrypts itself using _pyfhel.
See also
- encode_operand(self, other)¶
encode_operand(other)
Encodes the given value into a PyPtxt using _pyfhel.
- Parameters:
other (int, float, np.array, list) – Encodes accordingly to the type
- Returns:
Encoded value
- Return type:
See also
- encrypt(self, value)¶
encrypt(value)
Encrypts the given value using _pyfhel.
See also
- from_bytes(self, bytes content, scheme=None) void¶
from_bytes(bytes content, scheme)
Recover the serialized ciphertext from a binary/bytes string.
- Parameters:
- Returns:
None
See also
to_Scheme_t()
- get_multiplicative_inverse(self, other)¶
get_multiplicative_inverse(other)
Returns the inverse of the given Value/s.
- load(self, str fileName, scheme=None) size_t¶
load(self, str fileName, scheme)
Load the ciphertext from a file.
- Parameters:
- Returns:
number of loaded bytes.
- Return type:
size_t
See also
to_Scheme_t()
- round_scale(self) void¶
round_scale()
Rounds the scale of the ciphertext to the nearest power of 2.
- save(self, str fileName, str compr_mode='zstd') size_t¶
save(str fileName)
Save the ciphertext into a file. The file can new one or exist already, in which case it will be overwriten.
- Parameters:
fileName – (str) File where the ciphertext will be stored.
compr_mode – (str) Compression mode. One of “none”, “zlib”, “zstd”.
- Returns:
Number of bytes written.
- Return type:
size_t
- set_scale(self, double new_scale) void¶
set_scale(double new_scale)
Sets the scale of the ciphertext.
- Parameters:
scale (double) – new scale of the ciphertext.
- sizeof_ciphertext(self, str compr_mode='none') size_t¶
Returns the number of bytes that will be written when saving the ciphertext.
- Parameters:
compr_mode – (str) Compression mode. One of “none”, “zlib”, “zstd”.
- Returns:
Number of bytes that will be written.
- Return type:
size_t
- to_bytes(self, str compr_mode='none') bytes¶
to_bytes()
Serialize the ciphertext into a binary/bytes string.
- Parameters:
compr_mode – (str) Compression mode. One of “none”, “zlib”, “zstd”.
- Returns:
serialized ciphertext
- Return type:
- _pyfhel¶
A Pyfhel instance, used for operations
- mod_level¶
returns the number of moduli consumed so far.
Only usable in ckks.
- Type:
mod_level
- noiseBudget¶
Noise budget.
A value of 0 means that it cannot be decrypted correctly anymore.
See also
noiseLevel()- Type:
- scale¶
multiplying factor to encode values in ckks.
- Type:
double
- scheme¶
returns the FHE scheme of this ciphertext.
Can be set to: none, bfv, bgv (INTEGER) or ckks (FRACTIONAL).
See also
to_Scheme_t()- Type:
scheme