qfall-math 0.1.1

Mathematical foundations for rapid prototyping of lattice-based cryptography
Documentation
// Copyright © 2023 Marcel Luca Schmidt, Niklas Siemer
//
// This file is part of qFALL-math.
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.

//! `MatZ` is a type of matrix with integer entries of arbitrary length.
//! This implementation uses the [FLINT](https://flintlib.org/) library.

use crate::utils::parse::partial_string;
use flint_sys::fmpz_mat::fmpz_mat_struct;
use std::fmt;

mod arithmetic;
mod basis_reductions;
mod cmp;
mod concat;
mod default;
mod determinant;
mod forms;
mod from;
mod get;
mod inverse;
mod norm;
mod ownership;
mod properties;
mod sample;
mod serialize;
mod set;
mod sort;
mod tensor;
mod to_string;
mod trace;
mod transpose;
mod unsafe_functions;
mod vector;

/// [`MatZ`] is a matrix with entries of type [`Z`](crate::integer::Z).
///
/// Attributes:
/// - `matrix`: holds [FLINT](https://flintlib.org/)'s [struct](fmpz_mat_struct)
///   of the [`Z`](crate::integer::Z) matrix
///
/// # Examples
/// ## Matrix usage
/// ```
/// use qfall_math::{
///     integer::{MatZ, Z},
///     traits::{MatrixGetEntry, MatrixSetEntry},
/// };
///
/// // instantiate new matrix
/// let id_mat = MatZ::identity(2, 2);
///
/// // clone object, set and get entry
/// let mut clone = id_mat.clone();
/// clone.set_entry(0, 0, 2);
/// assert_eq!(clone.get_entry(1, 1).unwrap(), Z::ONE);
///
/// // multiplication, transposition and comparison
/// assert_eq!(id_mat.transpose() * &clone, clone);
///
/// // to_string incl. (de-)serialization
/// assert_eq!("[[1, 0],[0, 1]]", &id_mat.to_string());
/// assert_eq!(
///     "{\"matrix\":\"[[1, 0],[0, 1]]\"}",
///     serde_json::to_string(&id_mat).unwrap()
/// );
/// ```
///
/// ## Vector usage
/// ```
/// use qfall_math::{
///     integer::{MatZ, Z},
/// };
/// use std::str::FromStr;
///
/// let row_vec = MatZ::from_str("[[1, 1, 1]]").unwrap();
/// let col_vec = MatZ::from_str("[[1],[-1],[0]]").unwrap();
///
/// // check if matrix instance is vector
/// assert!(row_vec.is_row_vector());
/// assert!(col_vec.is_column_vector());
///
/// // dot product
/// assert_eq!(row_vec.dot_product(&col_vec).unwrap(), Z::ZERO);
///
/// // norm calculation
/// assert_eq!(col_vec.norm_eucl_sqrd().unwrap(), Z::from(2));
/// assert_eq!(row_vec.norm_infty().unwrap(), Z::ONE);
/// ```
pub struct MatZ {
    pub(crate) matrix: fmpz_mat_struct,
}

impl fmt::Debug for MatZ {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "MatZ: {{matrix: {}, storage: {:?}}}",
            // printing the entire matrix is not meaningful for large matrices
            partial_string(self, 3, 3),
            self.matrix
        )
    }
}