qfall-math 0.2.0

Mathematical foundations for rapid prototyping of lattice-based cryptography
Documentation
// Copyright 2023 Marvin Beckmann
//
// 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/>.

//! [`PolyOverZ`] is a type of polynomial with arbitrarily many coefficients of type
//! [`Z`](crate::integer::Z)
//! This implementation uses the [FLINT](https://flintlib.org/) library.

use flint3_sys::fmpz_poly_struct;
use std::fmt;

mod arithmetic;
mod cmp;
mod coefficient_embedding;
mod default;
mod dot_product;
mod evaluate;
pub(crate) mod fmpz_poly_helpers;
mod from;
mod get;
mod norm;
mod ownership;
mod properties;
mod reduce;
mod sample;
mod serialize;
mod set;
mod to_string;
mod unsafe_functions;

/// [`PolyOverZ`] is a type of polynomial with arbitrarily many coefficients of type
/// [`Z`](crate::integer::Z).
///
// Attributes:
// - `poly`: holds the content of the polynomial
//
/// # Examples
/// ```
/// use qfall_math::integer::{PolyOverZ, Z};
/// use qfall_math::traits::*;
/// use std::str::FromStr;
///
/// // instantiations
/// let poly_1 = PolyOverZ::from_str("4  0 1 2 3").unwrap();
/// let poly_2 = PolyOverZ::default();
///
/// // arithmetic operations
/// let _ = &poly_1 + &poly_2;
/// let _ = &poly_1 * &poly_2;
///
/// // evaluate function
/// let value = Z::from(3);
/// let res: Z = poly_1.evaluate(&value);
///
/// // comparison
/// assert_ne!(poly_1, poly_2);
/// ```
pub struct PolyOverZ {
    pub(crate) poly: fmpz_poly_struct,
}

impl fmt::Debug for PolyOverZ {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "PolyOverZ {{ poly: {}, storage: {{ poly: {}}}}}",
            self,
            debug_fmpz_poly_struct(&self.poly)
        )
    }
}

pub(crate) fn debug_fmpz_poly_struct(value: &fmpz_poly_struct) -> String {
    format!(
        "fmpz_poly_struct {{ coeffs: {:#x}, alloc: {}, length: {} }}",
        value.coeffs.addr(),
        value.alloc,
        value.length
    )
}