Tensor Classes

Base Tensor

class openfhe_numpy.tensor.tensor.BaseTensor[source]

Bases: ABC, Generic[T]

abstract property shape: Tuple[int, ...]
abstract property original_shape: Tuple[int, ...]
abstract property ndim: int
abstract property batch_size: int
abstract property ncols: int
abstract property order: int
abstract property dtype: str
abstract property metadata: dict
abstract clone(data=None)[source]
Return type:

BaseTensor[TypeVar(T)]

Parameters:

data (T | None)

abstract decrypt(*args, **kwargs)[source]
class openfhe_numpy.tensor.tensor.FHETensor(data, original_shape, batch_size, new_shape, order=ArrayEncodingType.ROW_MAJOR, is_padded=True)[source]

Bases: BaseTensor[T], Generic[T]

Concrete base class for tensors in FHE computation.

Parameters:
  • data (T) – Underlying encrypted or encoded data.

  • original_shape (Tuple[int, int]) – Shape before any padding.

  • batch_size (int) – Total number of packed slots.

  • new_shape (Tuple[int, int]) – Since the shape may change after some operations, we need to store the new information.

  • order (int) – Packing order: only support row-major or column-major.

  • is_padded (bool)

__init__(data, original_shape, batch_size, new_shape, order=ArrayEncodingType.ROW_MAJOR, is_padded=True)[source]
Parameters:
property size
is_padded()[source]
property dtype
property data: T

Underlying encrypted/plaintext payload.

property original_shape: Tuple[int, int]

Original shape before any padding was applied.

property shape: Tuple[int, int]

Shape after padding.

property ndim: int

Dimensionality of the original tensor.

property batch_size: int

Total number of packed slots.

property ncols: int

Number of columns after padding

property order: int

Packing order constant (row- or column-major).

property metadata: Dict[str, Any]

Metadata dict for serialization or inspection.

property info: Dict[str, Any]

Tuple of shape and encoding metadata.

Returns:

Contains [None, original_shape, batch_size, ncols, order]

Return type:

Tuple

property is_encrypted: bool
set_batch_size(value)[source]

Set batch size with validation.

Parameters:

value (int)

set_shape(value)[source]
Parameters:

value (Tuple[int, int])

clone(data=None)[source]

Copy the tensor, optionally replacing the data payload.

Return type:

BaseTensor[TypeVar(T)]

Parameters:

data (T | None)

sum(axis=0)[source]
reduce(axis=0)[source]
transpose()[source]
openfhe_numpy.tensor.tensor.copy_tensor(tensor)[source]

Generic copy constructor for FHETensor and subclasses.

Parameters:

tensor (FHETensor) – Tensor to be copied.

Returns:

A new instance with the same metadata and (optionally deep-copied) data.

Return type:

FHETensor

Ciphertext Array

class openfhe_numpy.tensor.ctarray.CTArray(data, original_shape, batch_size, new_shape, order=ArrayEncodingType.ROW_MAJOR, is_padded=True)[source]

Bases: FHETensor[Ciphertext]

Encrypted tensor class for OpenFHE ciphertexts. This class represents encrypted tensors.

Examples

>>> # Create and encrypt a matrix
>>> cc, keys = gen_crypto_context(4)
>>> matrix = np.array([[1, 2], [3, 4]])
>>> encrypted = onp.array(cc, matrix, slots, keys.publicKey)
>>> result = encrypted + encrypted  # or add(encrypted, encrypted)
>>> decrypted = result.decrypt(keys.secretKey)
Parameters:
tensor_priority = 10
decrypt(secret_key, unpack_type=UnpackType.ORIGINAL, new_shape=None)[source]

Decrypt ciphertext using given secret key with flexible formatting options.

Parameters:
  • secret_key (openfhe.PrivateKey) – Secret key used for decryption

  • unpack_type (UnpackType) – Formatting option to apply: - UnpackType.RAW: Return raw decrypted data without reshaping - UnpackType.RESHAPE: Reshape to original dimensions - UnpackType.ROUND: Reshape and round values to integers - UnpackType.AUTO: Auto-detect best format based on data

  • new_shape (tuple or int, optional) – Custom shape to reshape the output array to If None, uses the tensor’s original shape

Returns:

Decrypted data with requested formatting applied

Return type:

np.ndarray

serialize()[source]

Serialize ciphertext and metadata to dictionary.

Return type:

dict

classmethod deserialize(obj)[source]

Deserialize from dictionary back to CTArray.

Return type:

CTArray

Parameters:

obj (dict)

Plaintext Array

class openfhe_numpy.tensor.ptarray.PTArray(data, original_shape, batch_size, new_shape, order=ArrayEncodingType.ROW_MAJOR, is_padded=True)[source]

Bases: FHETensor[Plaintext]

Concrete tensor class for OpenFHE plaintexts.

Parameters:
clone(data=None)[source]

Copy the tensor, optionally replacing the data payload.

decrypt(*args, **kwargs)[source]
serialize()[source]
Return type:

dict

classmethod deserialize(obj)[source]
Return type:

PTArray

Parameters:

obj (dict)