Template Class Matrix

Inheritance Relationships

Base Type

Template Parameter Order

  1. class Element

Class Documentation

template<class Element>
class lbcrypto::Matrix : public lbcrypto::Serializable

Inheritence diagram for lbcrypto::Matrix:

digraph {
    graph [bgcolor="#00000000"]
    node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2]
    edge [color="#1414CE"]
    "2" [label="lbcrypto::Serializable" tooltip="lbcrypto::Serializable"]
    "1" [label="lbcrypto::Matrix< Element >" tooltip="lbcrypto::Matrix< Element >" fillcolor="#BFBFBF"]
    "1" -> "2" [dir=forward tooltip="public-inheritance"]
}

Collaboration diagram for lbcrypto::Matrix:

digraph {
    graph [bgcolor="#00000000"]
    node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2]
    edge [color="#1414CE"]
    "2" [label="lbcrypto::Serializable" tooltip="lbcrypto::Serializable"]
    "1" [label="lbcrypto::Matrix< Element >" tooltip="lbcrypto::Matrix< Element >" fillcolor="#BFBFBF"]
    "1" -> "2" [dir=forward tooltip="public-inheritance"]
}

Dense row-major matrix of Element values. Elements are created through a caller-supplied zero allocator so that ring elements carrying parameters can be constructed; arithmetic is element-wise or the usual matrix product, and the class provides the gadget, rotation, stacking, and decomposition helpers of the lattice trapdoor code.

Template Parameters

Element – the element type (integer, floating point, or ring element)

Public Types

typedef std::vector<std::vector<Element>> data_t

storage: a vector of rows

typedef std::vector<Element> data_row_t

one row of the matrix

typedef std::function<Element(void)> alloc_func

function returning a freshly allocated element (typically zero)

Public Functions

inline Matrix(alloc_func allocZero, size_t rows, size_t cols)

Constructor that initializes matrix values using a zero allocator

Parameters
  • allocZero – lambda function for zero initialization.

  • rows – number of rows.

  • cols – number of columns.

Matrix(alloc_func allocZero, size_t rows, size_t cols, alloc_func allocGen)

Constructor that initializes matrix values using a distribution generation allocator

Parameters
  • allocZero – lambda function for zero initialization (used for initializing derived matrix objects)

  • rows – number of rows.

  • cols – number of columns.

  • allocGen – lambda function for initialization using a distribution generator.

inline explicit Matrix(alloc_func allocZero = 0)

Constructor of an empty matrix. SetSize must be called on this matrix to use it SetAlloc needs to be called if 0 passed to constructor This mostly exists to support deserializing

Parameters

allocZero – lambda function for zero initialization.

inline void SetSize(size_t rows, size_t cols)

Set the size of a matrix, elements are zeroed out

Parameters
  • rows – number of rows

  • cols – number of columns

inline void SetAllocator(alloc_func allocZero)

SetAllocator - set the function to allocate a zero; basically only required for deserializer

Parameters

allocZero – lambda function for zero initialization

inline Matrix(const Matrix<Element> &other)

Copy constructor

Parameters

other – the matrix object to be copied

Matrix<Element> &operator=(const Matrix<Element> &other)

Assignment operator

Parameters

other – the matrix object whose values are to be copied

Returns

the resulting matrix

inline Matrix<Element> &Ones()

In-place change of the current matrix to a matrix of all ones

Returns

the resulting matrix

Matrix<Element> &ModEq(const Element &modulus)

In-place modulo reduction

Parameters

modulus – the modulus to reduce by

Returns

the resulting matrix (same object)

Matrix<Element> &ModSubEq(Matrix<Element> const &b, const Element &modulus)

In-place modular subtraction

Parameters
  • b – the matrix to be subtracted

  • modulus – the modulus to reduce by

Returns

the resulting matrix (same object)

Matrix<Element> &Fill(const Element &val)

Fill matrix using the same element

Parameters

val – the element the matrix is filled by

Returns

the resulting matrix

inline Matrix<Element> &Identity()

In-place change of the current matrix to Identity matrix

Returns

the resulting matrix

template<typename T = Element, typename std::enable_if<!std::is_same<T, M2DCRTPoly>::value && !std::is_same<T, M4DCRTPoly>::value && !std::is_same<T, M6DCRTPoly>::value, bool>::type = true>
inline Matrix<T> GadgetVector(int64_t base = 2) const

Sets the first row to be powers of two for when the base is two

Parameters

base – is the base the digits of the matrix are represented in

Returns

the resulting matrix

template<typename T = Element, typename std::enable_if<std::is_same<T, M2DCRTPoly>::value || std::is_same<T, M4DCRTPoly>::value || std::is_same<T, M6DCRTPoly>::value, bool>::type = true>
inline Matrix<T> GadgetVector(int64_t base = 2) const

Gadget matrix for DCRTPoly elements: the digits of every CRT modulus are handled separately, so the first row holds, for each tower i, the powers of the base embedded in tower i only; the other rows are shifted copies of the first row.

Parameters

base – is the base the digits of the matrix are represented in

Returns

the resulting matrix

template<typename T = Element, typename std::enable_if<std::is_same<T, double>::value || std::is_same<T, int>::value || std::is_same<T, int64_t>::value || std::is_same<T, Field2n>::value, bool>::type = true>
inline double Norm() const

Computes the infinity norm; not defined for scalar (double, int, int64_t) and Field2n element types, for which this overload always throws.

Returns

never returns; throws.

template<typename T = Element, typename std::enable_if<!std::is_same<T, double>::value && !std::is_same<T, int>::value && !std::is_same<T, int64_t>::value && !std::is_same<T, Field2n>::value, bool>::type = true>
inline double Norm() const

Computes the infinity norm: the largest Norm() over all elements

Returns

the norm in double format

Matrix<Element> Mult(Matrix<Element> const &other) const

Matrix multiplication

Parameters

other – the multiplier matrix

Returns

the result of multiplication

inline Matrix<Element> operator*(Matrix<Element> const &other) const

Operator for matrix multiplication

Parameters

other – the multiplier matrix

Returns

the result of multiplication

inline Matrix<Element> ScalarMult(Element const &other) const

Multiplication of matrix by a scalar

Parameters

other – the multiplier element

Returns

the result of multiplication

inline Matrix<Element> operator*(Element const &other) const

Operator for scalar multiplication

Parameters

other – the multiplier element

Returns

the result of multiplication

inline bool Equal(Matrix<Element> const &other) const

Equality check

Parameters

other – the matrix object to compare to

Returns

the boolean result

inline bool operator==(Matrix<Element> const &other) const

Operator for equality check

Parameters

other – the matrix object to compare to

Returns

the boolean result

inline bool operator!=(Matrix<Element> const &other) const

Operator for non-equality check

Parameters

other – the matrix object to compare to

Returns

the boolean result

inline const data_t &GetData() const

Get property to access the data as a vector of vectors

Returns

the data as vector of vectors

inline size_t GetRows() const

Get property to access the number of rows in the matrix

Returns

the number of rows

inline size_t GetCols() const

Get property to access the number of columns in the matrix

Returns

the number of columns

inline alloc_func GetAllocator() const

Get property to access the zero allocator for the matrix

Returns

the lambda function corresponding to the element zero allocator

void SetFormat(Format format)

Sets the evaluation or coefficient representation for all ring elements that support the SetFormat method

Parameters

format – the enum value corresponding to coefficient or evaluation representation

inline Matrix<Element> Add(Matrix<Element> const &other) const

Matrix addition

Parameters

other – the matrix to be added

Returns

the resulting matrix

inline Matrix<Element> operator+(Matrix<Element> const &other) const

Operator for matrix addition

Parameters

other – the matrix to be added

Returns

the resulting matrix

Matrix<Element> &operator+=(Matrix<Element> const &other)

Operator for in-place addition

Parameters

other – the matrix to be added

Returns

the resulting matrix (same object)

inline Matrix<Element> Sub(Matrix<Element> const &other) const

Matrix subtraction

Parameters

other – the matrix to be subtracted

Returns

the resulting matrix

inline Matrix<Element> operator-(Matrix<Element> const &other) const

Operator for matrix subtraction

Parameters

other – the matrix to be subtracted

Returns

the resulting matrix

Matrix<Element> &operator-=(Matrix<Element> const &other)

Operator for in-place matrix subtraction

Parameters

other – the matrix to be subtracted

Returns

the resulting matrix (same object)

Matrix<Element> Transpose() const

Matrix transposition

Returns

the resulting matrix

void Determinant(Element *result) const

Matrix determinant - found using Laplace formula with complexity O(d!), where d is the dimension

Parameters

result – where the result is stored

Matrix<Element> CofactorMatrix() const

Cofactor matrix - the matrix of determinants of the minors A_{ij} multiplied by -1^{i+j}

Returns

the cofactor matrix for the given matrix

Matrix<Element> &VStack(Matrix<Element> const &other)

Add rows to bottom of the matrix

Parameters

other – the matrix to be added to the bottom of current matrix

Returns

the resulting matrix

inline Matrix<Element> &HStack(Matrix<Element> const &other)

Add columns the right of the matrix

Parameters

other – the matrix to be added to the right of current matrix

Returns

the resulting matrix

inline Element &operator()(size_t row, size_t col)

Matrix indexing operator - writeable instance of the element

Parameters
  • row – row index

  • col – column index

Returns

the element at the index

inline Element const &operator()(size_t row, size_t col) const

Matrix indexing operator - read-only instance of the element

Parameters
  • row – row index

  • col – column index

Returns

the element at the index

inline Matrix<Element> ExtractRow(size_t row) const

Matrix row extractor

Parameters

row – row index

Returns

the row at the index

inline Matrix<Element> ExtractCol(size_t col) const

Matrix column extractor

Parameters

col – col index

Returns

the col at the index

inline Matrix<Element> ExtractRows(size_t row_start, size_t row_end) const

Matrix rows extractor in a range from row_start to row_end; inclusive

Parameters
  • row_start – index of the first row to extract

  • row_end – index of the last row to extract

Returns

the rows in the range delimited by indices inclusive

void SwitchFormat()

Call switch format for each (ring) element

Matrix<Element> MultByUnityVector() const

Multiply the matrix by a vector whose elements are all 1’s. This causes the elements of each row of the matrix to be added and placed into the corresponding position in the output vector.

Returns

the rows x 1 matrix of row sums

Matrix<Element> MultByRandomVector(std::vector<int> ranvec) const

Multiply the matrix by a vector of random 1’s and 0’s, which is the same as adding select elements in each row together. Return a vector that is a rows x 1 matrix.

Parameters

ranvec – the 0/1 vector of length cols selecting the columns to add

Returns

the rows x 1 matrix of selected row sums

template<class Archive>
inline void save(Archive &ar, std::uint32_t const version) const
template<class Archive>
inline void load(Archive &ar, std::uint32_t const version)
inline virtual std::string SerializedObjectName() const override

Returns the name identifying the derived class in serialized form; every serializable class provides it.

Returns

the serialized object name

Matrix<NativeInteger> &ModEq(const NativeInteger &element)
Matrix<BigInteger> &ModEq(const BigInteger &element)
Matrix<NativeInteger> &ModSubEq(Matrix<NativeInteger> const &b, const NativeInteger &element)
Matrix<BigInteger> &ModSubEq(Matrix<BigInteger> const &b, const BigInteger &element)

Public Static Functions

static inline uint32_t SerializedVersion()

Friends

inline friend std::ostream &operator<<(std::ostream &os, const Matrix<Element> &m)

Stream output operator

Parameters
  • os – stream

  • m – matrix to be outputted

Returns

the chained stream