Tensor Classes

Base Tensor

class openfhe_numpy.tensor.tensor.BaseTensor[source]

Bases: ABC, Generic[Template]

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 info: dict
abstractmethod clone(data=None)[source]
Return type:

BaseTensor[TypeVar(Template)]

Parameters:

data (Template)

abstractmethod decrypt(*args, **kwargs)[source]
class openfhe_numpy.tensor.tensor.PackedArrayInformation(data, original_shape, ndim, batch_size, shape, order)[source]

Bases: object

Parameters:
data: list | ndarray | Template
original_shape: tuple[int, int]
ndim: int
batch_size: int
shape: tuple[int, int]
order: int
__init__(data, original_shape, ndim, batch_size, shape, order)
Parameters:
Return type:

None

class openfhe_numpy.tensor.tensor.FHETensor(data, original_shape, batch_size, new_shape, order=0)[source]

Bases: BaseTensor[Template], Generic[Template]

Concrete base class for tensors in FHE computation.

Parameters:
  • data (Union[list, ndarray, PackedArrayInformation]) – Underlying encrypted or encoded of a packed encoding array.

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

__init__(data, original_shape, batch_size, new_shape, order=0)[source]
Overloads:
  • self, data (TPL), original_shape (Tuple[int, int]), batch_size (int), new_shape (Tuple[int, int]), order (int) → None

  • self, info (PackedArrayInformation) → None

Parameters:
Return type:

None

extra
property size
property dtype
property data: Template

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 nrows: int

Number of rows after padding

property order: int

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

property is_encrypted: int
property info: Dict[str, Any]

Metadata dict for serialization or inspection.

property T
clone(data=None)[source]

Copy the tensor, optionally replacing the data payload.

Return type:

BaseTensor[TypeVar(Template)]

Parameters:

data (Template | 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=0)[source]

Bases: FHETensor[Ciphertext]

Encrypted tensor class for OpenFHE ciphertexts. Represents encrypted matrices or vectors.

Parameters:
tensor_priority = 10
property crypto_context

Get the underlying crypto context

property zeros

Get the zeros ciphertext

decrypt(secret_key, unpack_type=UnpackType.ORIGINAL, new_shape=None)[source]

Decrypt the ciphertext and format the output.

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

  • unpack_type (UnpackType) –

    • RAW: raw data, no reshape

    • ORIGINAL: reshape to original dimensions

    • ROUND: reshape and round to integers (not support now)

    • AUTO: auto-detect best format (not support now)

  • new_shape (Union[Tuple[int, ...], int, None]) – Custom shape for the output array. If None, uses original shape.

Returns:

The decrypted data, formatted by ‘unpack_type’.

Return type:

ndarray

serialize()[source]

Serialize ciphertext and metadata to a dictionary.

Return type:

dict

classmethod deserialize(obj)[source]

Deserialize a dictionary back into a CTArray.

Return type:

CTArray

Parameters:

obj (dict)

cumulative_sum(axis=0)[source]

Compute the cumulative sum of tensor elements along a given axis.

Parameters:

axis (int) – Axis along which the cumulative sum is computed. Default is 0.

Returns:

A new tensor with cumulative sums along the specified axis.

Return type:

CTArray

gen_sum_row_key(secret_key)[source]
Return type:

EvalKey

Parameters:

secret_key (openfhe.PrivateKey)

extra

Plaintext Array

class openfhe_numpy.tensor.ptarray.PTArray(data, original_shape, batch_size, new_shape, order=0)[source]

Bases: FHETensor[Plaintext]

Concrete tensor class for OpenFHE plaintexts.

Parameters:
clone(data=None)[source]

Copy the tensor, optionally replacing the data payload.

encrypt(crypto_context, public_key)[source]
Parameters:
  • crypto_context (openfhe.CryptoContext)

  • public_key (openfhe.PublicKey)

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

dict

classmethod deserialize(obj)[source]
Return type:

PTArray

Parameters:

obj (dict)

extra