encode_to_plain_matrix
Engine.encode_to_plain_matrix(matrix, level)
- Encodes a matrix into a plain matrix format for matrix multiplication.
- Input:
- matrix: A Python list or numpy array. Data types supported include int, double, and complex double.
- The matrix should be square, with both dimensions equal to
Engine.slot_count.
- level: optional
- Specifies the maximum multiplication level for encoding. Lower levels reduce space usage but also reduce the maximum multiplication depth.
- The level should be an integer greater than or equal to 1.
- diagonal_indices: optional
- Specifies the diagonal indices to encode. If not provided, all diagonals are encoded by default.
- Output:
import numpy as np
from desilofhe import Engine
engine = Engine(slot_count=4)
message = np.arange(4 * 4).reshape(4, 4)
plain_matrix = engine.encode_to_plain_matrix(message)
# Encode with specific level and diagonal indices
plain_matrix_level_2 = engine.encode_to_plain_matrix(message, level=2)
# For diagonal indices, each position in a 4x4 matrix belongs to a diagonal as follows:
# [[0, 3, 2, 1],
# [1, 0, 3, 2],
# [2, 1, 0, 3],
# [3, 2, 1, 0]]
# fmt: off
example_sparse_message = np.array([
[0, 1, 2, 0],
[0, 0, 3, 4],
[5, 0, 0, 6],
[7, 8, 0, 0],
])
# fmt: on
# Since diagonals 0 and 1 contain only zeros, we can encode only diagonals 2 and 3
sparse_diagonal_indices = [2, 3]
# This reduces memory usage by skipping zero-filled diagonals
sparse_plain_matrix = engine.encode_to_plain_matrix(
example_sparse_message, level=2, diagonal_indices=sparse_diagonal_indices
)